blob: 14a5d981be7d07030a264352dbc8d8af1e158c2d [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 Delvare44c1bcd2010-10-28 20:31:51 +020020 * IT8758E Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010021 * Sis950 A clone of the IT8705F
22 *
23 * Copyright (C) 2001 Chris Gauthron
24 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
25 *
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
30 *
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/init.h>
43#include <linux/slab.h>
44#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020045#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040046#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020047#include <linux/hwmon-sysfs.h>
48#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040049#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010050#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020051#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020052#include <linux/string.h>
53#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010054#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020055#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
corentin.labbeb74f3fd2007-06-13 20:27:36 +020057#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jean Delvare44c1bcd2010-10-28 20:31:51 +020059enum chips { it87, it8712, it8716, it8718, it8720, it8721 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Jean Delvare67b671b2007-12-06 23:13:42 +010061static unsigned short force_id;
62module_param(force_id, ushort, 0);
63MODULE_PARM_DESC(force_id, "Override the detected device ID");
64
corentin.labbeb74f3fd2007-06-13 20:27:36 +020065static struct platform_device *pdev;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define REG 0x2e /* The register to read/write */
68#define DEV 0x07 /* Register: Logical device select */
69#define VAL 0x2f /* The value to read/write */
70#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010071
72/* The device with the IT8718F/IT8720F VID value in it */
73#define GPIO 0x07
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define DEVID 0x20 /* Register: Device ID */
76#define DEVREV 0x22 /* Register: Device Revision */
77
78static inline int
79superio_inb(int reg)
80{
81 outb(reg, REG);
82 return inb(VAL);
83}
84
Jean Delvare436cad22010-07-09 16:22:48 +020085static inline void
86superio_outb(int reg, int val)
87{
88 outb(reg, REG);
89 outb(val, VAL);
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int superio_inw(int reg)
93{
94 int val;
95 outb(reg++, REG);
96 val = inb(VAL) << 8;
97 outb(reg, REG);
98 val |= inb(VAL);
99 return val;
100}
101
102static inline void
Jean Delvare87673dd2006-08-28 14:37:19 +0200103superio_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
109static inline void
110superio_enter(void)
111{
112 outb(0x87, REG);
113 outb(0x01, REG);
114 outb(0x55, REG);
115 outb(0x55, REG);
116}
117
118static inline void
119superio_exit(void)
120{
121 outb(0x02, REG);
122 outb(0x02, VAL);
123}
124
Jean Delvare87673dd2006-08-28 14:37:19 +0200125/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define IT8712F_DEVID 0x8712
127#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200128#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200129#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100130#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200131#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400132#define IT8726F_DEVID 0x8726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define IT87_ACT_REG 0x30
134#define IT87_BASE_REG 0x60
135
Jean Delvare87673dd2006-08-28 14:37:19 +0200136/* Logical device 7 registers (IT8712F and later) */
Jean Delvare895ff262009-12-09 20:35:47 +0100137#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100138#define IT87_SIO_GPIO5_REG 0x29
Jean Delvare87673dd2006-08-28 14:37:19 +0200139#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
140#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100141#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* Update battery voltage after every reading if true */
144static int update_vbat;
145
146/* Not all BIOSes properly configure the PWM registers */
147static int fix_pwm_polarity;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/* Many IT87 constants specified below */
150
151/* Length of ISA address segment */
152#define IT87_EXTENT 8
153
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500154/* Length of ISA address segment for Environmental Controller */
155#define IT87_EC_EXTENT 2
156
157/* Offset of EC registers from ISA base address */
158#define IT87_EC_OFFSET 5
159
160/* Where are the ISA address/data registers relative to the EC base address */
161#define IT87_ADDR_REG_OFFSET 0
162#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/*----- The IT87 registers -----*/
165
166#define IT87_REG_CONFIG 0x00
167
168#define IT87_REG_ALARM1 0x01
169#define IT87_REG_ALARM2 0x02
170#define IT87_REG_ALARM3 0x03
171
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100172/* The IT8718F and IT8720F have the VID value in a different register, in
173 Super-I/O configuration space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#define IT87_REG_VID 0x0a
Andrew Paprocki04751692008-08-06 22:41:06 +0200175/* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
176 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
177 mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200179#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
182
Jean Delvarec7f1f712007-09-03 17:11:46 +0200183static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
184static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
185static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
186static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#define IT87_REG_FAN_MAIN_CTRL 0x13
188#define IT87_REG_FAN_CTL 0x14
189#define IT87_REG_PWM(nr) (0x15 + (nr))
190
191#define IT87_REG_VIN(nr) (0x20 + (nr))
192#define IT87_REG_TEMP(nr) (0x29 + (nr))
193
194#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
195#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
196#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
197#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#define IT87_REG_VIN_ENABLE 0x50
200#define IT87_REG_TEMP_ENABLE 0x51
Jean Delvared9b327c2010-03-05 22:17:17 +0100201#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203#define IT87_REG_CHIPID 0x58
204
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100205#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
206#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200209struct it87_sio_data {
210 enum chips type;
211 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200212 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200213 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100214 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200215 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100216 /* Features skipped based on config or DMI */
Jean Delvare895ff262009-12-09 20:35:47 +0100217 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100218 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200219 u8 skip_pwm;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200220};
221
Jean Delvareed6bafb2007-02-14 21:15:03 +0100222/* For each registered chip, we need to keep some data in memory.
223 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700225 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 enum chips type;
Andrew Paprocki04751692008-08-06 22:41:06 +0200227 u8 revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200229 unsigned short addr;
230 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100231 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 char valid; /* !=0 if following fields are valid */
233 unsigned long last_updated; /* In jiffies */
234
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200235 u16 in_scaled; /* Internal voltage sensors are scaled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200237 u8 in_max[8]; /* Register value */
238 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200239 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvarec7f1f712007-09-03 17:11:46 +0200240 u16 fan[5]; /* Register values, possibly combined */
241 u16 fan_min[5]; /* Register values, possibly combined */
Jean Delvaree267d252009-03-12 13:36:39 +0100242 s8 temp[3]; /* Register value */
243 s8 temp_high[3]; /* Register value */
244 s8 temp_low[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 u8 sensor; /* Register value */
246 u8 fan_div[3]; /* Register encoding, shifted right */
247 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100248 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100250 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100252 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100253
254 /* The following 3 arrays correspond to the same registers. The
255 * meaning of bits 6-0 depends on the value of bit 7, and we want
256 * to preserve settings on mode changes, so we have to track all
257 * values separately. */
258 u8 pwm_ctrl[3]; /* Register value */
259 u8 pwm_duty[3]; /* Manual PWM value set by user (bit 6-0) */
260 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100261
262 /* Automatic fan speed control registers */
263 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
264 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265};
266
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200267static u8 in_to_reg(const struct it87_data *data, int nr, long val)
268{
269 long lsb;
270
271 if (data->type == it8721) {
272 if (data->in_scaled & (1 << nr))
273 lsb = 24;
274 else
275 lsb = 12;
276 } else
277 lsb = 16;
278
279 val = DIV_ROUND_CLOSEST(val, lsb);
280 return SENSORS_LIMIT(val, 0, 255);
281}
282
283static int in_from_reg(const struct it87_data *data, int nr, int val)
284{
285 if (data->type == it8721) {
286 if (data->in_scaled & (1 << nr))
287 return val * 24;
288 else
289 return val * 12;
290 } else
291 return val * 16;
292}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200293
294static inline u8 FAN_TO_REG(long rpm, int div)
295{
296 if (rpm == 0)
297 return 255;
298 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
299 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
300 254);
301}
302
303static inline u16 FAN16_TO_REG(long rpm)
304{
305 if (rpm == 0)
306 return 0xffff;
307 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
308}
309
310#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
311 1350000 / ((val) * (div)))
312/* The divider is fixed to 2 in 16-bit mode */
313#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
314 1350000 / ((val) * 2))
315
316#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
317 ((val) + 500) / 1000), -128, 127))
318#define TEMP_FROM_REG(val) ((val) * 1000)
319
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200320static u8 pwm_to_reg(const struct it87_data *data, long val)
321{
322 if (data->type == it8721)
323 return val;
324 else
325 return val >> 1;
326}
327
328static int pwm_from_reg(const struct it87_data *data, u8 reg)
329{
330 if (data->type == it8721)
331 return reg;
332 else
333 return (reg & 0x7f) << 1;
334}
335
Jean Delvare0df6454d2010-10-28 20:31:51 +0200336
337static int DIV_TO_REG(int val)
338{
339 int answer = 0;
340 while (answer < 7 && (val >>= 1))
341 answer++;
342 return answer;
343}
344#define DIV_FROM_REG(val) (1 << (val))
345
346static const unsigned int pwm_freq[8] = {
347 48000000 / 128,
348 24000000 / 128,
349 12000000 / 128,
350 8000000 / 128,
351 6000000 / 128,
352 3000000 / 128,
353 1500000 / 128,
354 750000 / 128,
355};
356
Andrew Paprocki04751692008-08-06 22:41:06 +0200357static inline int has_16bit_fans(const struct it87_data *data)
358{
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200359 /* IT8705F Datasheet 0.4.1, 3h == Version G.
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200360 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200361 These are the first revisions with 16bit tachometer support. */
362 return (data->type == it87 && data->revision >= 0x03)
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200363 || (data->type == it8712 && data->revision >= 0x08)
Andrew Paprocki04751692008-08-06 22:41:06 +0200364 || data->type == it8716
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100365 || data->type == it8718
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200366 || data->type == it8720
367 || data->type == it8721;
Andrew Paprocki04751692008-08-06 22:41:06 +0200368}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100370static inline int has_old_autopwm(const struct it87_data *data)
371{
372 /* The old automatic fan speed control interface is implemented
373 by IT8705F chips up to revision F and IT8712F chips up to
374 revision G. */
375 return (data->type == it87 && data->revision < 0x03)
376 || (data->type == it8712 && data->revision < 0x08);
377}
378
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200379static int it87_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200380static int __devexit it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200382static int it87_read_value(struct it87_data *data, u8 reg);
383static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200385static int it87_check_pwm(struct device *dev);
386static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200389static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100390 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200391 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200392 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100393 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200394 .probe = it87_probe,
395 .remove = __devexit_p(it87_remove),
Jean Delvarefde09502005-07-19 23:51:07 +0200396};
397
Jean Delvare20ad93d2005-06-05 11:53:25 +0200398static ssize_t show_in(struct device *dev, struct device_attribute *attr,
399 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200401 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
402 int nr = sensor_attr->index;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200405 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Jean Delvare20ad93d2005-06-05 11:53:25 +0200408static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
409 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200411 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
412 int nr = sensor_attr->index;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200415 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Jean Delvare20ad93d2005-06-05 11:53:25 +0200418static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
419 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200421 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
422 int nr = sensor_attr->index;
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200425 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Jean Delvare20ad93d2005-06-05 11:53:25 +0200428static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
429 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200431 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
432 int nr = sensor_attr->index;
433
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200434 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100435 unsigned long val;
436
437 if (strict_strtoul(buf, 10, &val) < 0)
438 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100440 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200441 data->in_min[nr] = in_to_reg(data, nr, val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200442 it87_write_value(data, IT87_REG_VIN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100444 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return count;
446}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200447static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
448 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200450 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
451 int nr = sensor_attr->index;
452
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200453 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100454 unsigned long val;
455
456 if (strict_strtoul(buf, 10, &val) < 0)
457 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100459 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200460 data->in_max[nr] = in_to_reg(data, nr, val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200461 it87_write_value(data, IT87_REG_VIN_MAX(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100463 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return count;
465}
466
467#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200468static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
469 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200472static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
473 show_in_min, set_in_min, offset); \
474static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
475 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477show_in_offset(0);
478limit_in_offset(0);
479show_in_offset(1);
480limit_in_offset(1);
481show_in_offset(2);
482limit_in_offset(2);
483show_in_offset(3);
484limit_in_offset(3);
485show_in_offset(4);
486limit_in_offset(4);
487show_in_offset(5);
488limit_in_offset(5);
489show_in_offset(6);
490limit_in_offset(6);
491show_in_offset(7);
492limit_in_offset(7);
493show_in_offset(8);
494
495/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200496static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
497 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200499 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
500 int nr = sensor_attr->index;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct it87_data *data = it87_update_device(dev);
503 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
504}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200505static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
506 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200508 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
509 int nr = sensor_attr->index;
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct it87_data *data = it87_update_device(dev);
512 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
513}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200514static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
515 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200517 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
518 int nr = sensor_attr->index;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct it87_data *data = it87_update_device(dev);
521 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
522}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200523static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
524 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200526 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
527 int nr = sensor_attr->index;
528
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200529 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100530 long val;
531
532 if (strict_strtol(buf, 10, &val) < 0)
533 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100535 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 data->temp_high[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200537 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100538 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return count;
540}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200541static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
542 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200544 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
545 int nr = sensor_attr->index;
546
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200547 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100548 long val;
549
550 if (strict_strtol(buf, 10, &val) < 0)
551 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100553 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 data->temp_low[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200555 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100556 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return count;
558}
559#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200560static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
561 show_temp, NULL, offset - 1); \
562static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
563 show_temp_max, set_temp_max, offset - 1); \
564static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
565 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567show_temp_offset(1);
568show_temp_offset(2);
569show_temp_offset(3);
570
Jean Delvare20ad93d2005-06-05 11:53:25 +0200571static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
572 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200574 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
575 int nr = sensor_attr->index;
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100578 u8 reg = data->sensor; /* In case the value is updated while
579 we use it */
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (reg & (1 << nr))
582 return sprintf(buf, "3\n"); /* thermal diode */
583 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200584 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return sprintf(buf, "0\n"); /* disabled */
586}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200587static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
588 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200590 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
591 int nr = sensor_attr->index;
592
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200593 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100594 long val;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200595 u8 reg;
Jean Delvaref5f64502010-03-05 22:17:19 +0100596
597 if (strict_strtol(buf, 10, &val) < 0)
598 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Jean Delvare8acf07c2010-04-14 16:14:09 +0200600 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
601 reg &= ~(1 << nr);
602 reg &= ~(8 << nr);
Jean Delvare4ed10772008-10-17 17:51:16 +0200603 if (val == 2) { /* backwards compatibility */
604 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
605 "instead\n");
606 val = 4;
607 }
608 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200610 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200611 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200612 reg |= 8 << nr;
613 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200615
616 mutex_lock(&data->update_lock);
617 data->sensor = reg;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200618 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200619 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100620 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return count;
622}
623#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200624static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
625 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627show_sensor_offset(1);
628show_sensor_offset(2);
629show_sensor_offset(3);
630
631/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100632
633static int pwm_mode(const struct it87_data *data, int nr)
634{
635 int ctrl = data->fan_main_ctrl & (1 << nr);
636
637 if (ctrl == 0) /* Full speed */
638 return 0;
639 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
640 return 2;
641 else /* Manual mode */
642 return 1;
643}
644
Jean Delvare20ad93d2005-06-05 11:53:25 +0200645static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
646 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200648 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
649 int nr = sensor_attr->index;
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100652 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 DIV_FROM_REG(data->fan_div[nr])));
654}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200655static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
656 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200658 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
659 int nr = sensor_attr->index;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100662 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
663 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200665static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
666 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200668 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
669 int nr = sensor_attr->index;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct it87_data *data = it87_update_device(dev);
672 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
673}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100674static ssize_t show_pwm_enable(struct device *dev,
675 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200677 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
678 int nr = sensor_attr->index;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100681 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200683static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
684 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200686 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
687 int nr = sensor_attr->index;
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200690 return sprintf(buf, "%d\n",
691 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100693static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
694 char *buf)
695{
696 struct it87_data *data = it87_update_device(dev);
697 int index = (data->fan_ctl >> 4) & 0x07;
698
699 return sprintf(buf, "%u\n", pwm_freq[index]);
700}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200701static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
702 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200704 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
705 int nr = sensor_attr->index;
706
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200707 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100708 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100709 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Jean Delvaref5f64502010-03-05 22:17:19 +0100711 if (strict_strtol(buf, 10, &val) < 0)
712 return -EINVAL;
713
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100714 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200715 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800716 switch (nr) {
Jean Delvare5f2dc792010-03-05 22:17:18 +0100717 case 0:
718 data->fan_div[nr] = reg & 0x07;
719 break;
720 case 1:
721 data->fan_div[nr] = (reg >> 3) & 0x07;
722 break;
723 case 2:
724 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
725 break;
Jean Delvare07eab462005-11-23 15:44:31 -0800726 }
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200729 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100730 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return count;
732}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200733static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
734 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200736 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
737 int nr = sensor_attr->index;
738
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200739 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100740 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200741 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 u8 old;
743
Jean Delvaref5f64502010-03-05 22:17:19 +0100744 if (strict_strtoul(buf, 10, &val) < 0)
745 return -EINVAL;
746
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100747 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200748 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200750 /* Save fan min limit */
751 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 switch (nr) {
754 case 0:
755 case 1:
756 data->fan_div[nr] = DIV_TO_REG(val);
757 break;
758 case 2:
759 if (val < 8)
760 data->fan_div[nr] = 1;
761 else
762 data->fan_div[nr] = 3;
763 }
764 val = old & 0x80;
765 val |= (data->fan_div[0] & 0x07);
766 val |= (data->fan_div[1] & 0x07) << 3;
767 if (data->fan_div[2] == 3)
768 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200769 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200771 /* Restore fan min limit */
772 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200773 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200774
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100775 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return count;
777}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100778
779/* Returns 0 if OK, -EINVAL otherwise */
780static int check_trip_points(struct device *dev, int nr)
781{
782 const struct it87_data *data = dev_get_drvdata(dev);
783 int i, err = 0;
784
785 if (has_old_autopwm(data)) {
786 for (i = 0; i < 3; i++) {
787 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
788 err = -EINVAL;
789 }
790 for (i = 0; i < 2; i++) {
791 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
792 err = -EINVAL;
793 }
794 }
795
796 if (err) {
797 dev_err(dev, "Inconsistent trip points, not switching to "
798 "automatic mode\n");
799 dev_err(dev, "Adjust the trip points and try again\n");
800 }
801 return err;
802}
803
Jean Delvare20ad93d2005-06-05 11:53:25 +0200804static ssize_t set_pwm_enable(struct device *dev,
805 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200807 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
808 int nr = sensor_attr->index;
809
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200810 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100811 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Jean Delvaref5f64502010-03-05 22:17:19 +0100813 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100814 return -EINVAL;
815
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100816 /* Check trip points before switching to automatic mode */
817 if (val == 2) {
818 if (check_trip_points(dev, nr) < 0)
819 return -EINVAL;
820 }
821
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100822 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 if (val == 0) {
825 int tmp;
826 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200827 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
828 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* set on/off mode */
830 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100831 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
832 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100833 } else {
834 if (val == 1) /* Manual mode */
835 data->pwm_ctrl[nr] = data->pwm_duty[nr];
836 else /* Automatic mode */
837 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
838 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /* set SmartGuardian mode */
840 data->fan_main_ctrl |= (1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100841 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
842 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100845 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return count;
847}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200848static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
849 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200851 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
852 int nr = sensor_attr->index;
853
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200854 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100855 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Jean Delvaref5f64502010-03-05 22:17:19 +0100857 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -EINVAL;
859
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100860 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200861 data->pwm_duty[nr] = pwm_to_reg(data, val);
Jean Delvareb99883d2010-03-05 22:17:15 +0100862 /* If we are in manual mode, write the duty cycle immediately;
863 * otherwise, just store it for later use. */
864 if (!(data->pwm_ctrl[nr] & 0x80)) {
865 data->pwm_ctrl[nr] = data->pwm_duty[nr];
866 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
867 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100868 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return count;
870}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100871static ssize_t set_pwm_freq(struct device *dev,
872 struct device_attribute *attr, const char *buf, size_t count)
873{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200874 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100875 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100876 int i;
877
Jean Delvaref5f64502010-03-05 22:17:19 +0100878 if (strict_strtoul(buf, 10, &val) < 0)
879 return -EINVAL;
880
Jean Delvaref8d0c192007-02-14 21:15:02 +0100881 /* Search for the nearest available frequency */
882 for (i = 0; i < 7; i++) {
883 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
884 break;
885 }
886
887 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200888 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100889 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200890 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100891 mutex_unlock(&data->update_lock);
892
893 return count;
894}
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100895static ssize_t show_pwm_temp_map(struct device *dev,
896 struct device_attribute *attr, char *buf)
897{
898 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
899 int nr = sensor_attr->index;
900
901 struct it87_data *data = it87_update_device(dev);
902 int map;
903
904 if (data->pwm_temp_map[nr] < 3)
905 map = 1 << data->pwm_temp_map[nr];
906 else
907 map = 0; /* Should never happen */
908 return sprintf(buf, "%d\n", map);
909}
910static ssize_t set_pwm_temp_map(struct device *dev,
911 struct device_attribute *attr, const char *buf, size_t count)
912{
913 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
914 int nr = sensor_attr->index;
915
916 struct it87_data *data = dev_get_drvdata(dev);
917 long val;
918 u8 reg;
919
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100920 /* This check can go away if we ever support automatic fan speed
921 control on newer chips. */
922 if (!has_old_autopwm(data)) {
923 dev_notice(dev, "Mapping change disabled for safety reasons\n");
924 return -EINVAL;
925 }
926
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100927 if (strict_strtol(buf, 10, &val) < 0)
928 return -EINVAL;
929
930 switch (val) {
931 case (1 << 0):
932 reg = 0x00;
933 break;
934 case (1 << 1):
935 reg = 0x01;
936 break;
937 case (1 << 2):
938 reg = 0x02;
939 break;
940 default:
941 return -EINVAL;
942 }
943
944 mutex_lock(&data->update_lock);
945 data->pwm_temp_map[nr] = reg;
946 /* If we are in automatic mode, write the temp mapping immediately;
947 * otherwise, just store it for later use. */
948 if (data->pwm_ctrl[nr] & 0x80) {
949 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
950 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
951 }
952 mutex_unlock(&data->update_lock);
953 return count;
954}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100956static ssize_t show_auto_pwm(struct device *dev,
957 struct device_attribute *attr, char *buf)
958{
959 struct it87_data *data = it87_update_device(dev);
960 struct sensor_device_attribute_2 *sensor_attr =
961 to_sensor_dev_attr_2(attr);
962 int nr = sensor_attr->nr;
963 int point = sensor_attr->index;
964
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200965 return sprintf(buf, "%d\n",
966 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100967}
968
969static ssize_t set_auto_pwm(struct device *dev,
970 struct device_attribute *attr, const char *buf, size_t count)
971{
972 struct it87_data *data = dev_get_drvdata(dev);
973 struct sensor_device_attribute_2 *sensor_attr =
974 to_sensor_dev_attr_2(attr);
975 int nr = sensor_attr->nr;
976 int point = sensor_attr->index;
977 long val;
978
979 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
980 return -EINVAL;
981
982 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200983 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100984 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
985 data->auto_pwm[nr][point]);
986 mutex_unlock(&data->update_lock);
987 return count;
988}
989
990static ssize_t show_auto_temp(struct device *dev,
991 struct device_attribute *attr, char *buf)
992{
993 struct it87_data *data = it87_update_device(dev);
994 struct sensor_device_attribute_2 *sensor_attr =
995 to_sensor_dev_attr_2(attr);
996 int nr = sensor_attr->nr;
997 int point = sensor_attr->index;
998
999 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1000}
1001
1002static ssize_t set_auto_temp(struct device *dev,
1003 struct device_attribute *attr, const char *buf, size_t count)
1004{
1005 struct it87_data *data = dev_get_drvdata(dev);
1006 struct sensor_device_attribute_2 *sensor_attr =
1007 to_sensor_dev_attr_2(attr);
1008 int nr = sensor_attr->nr;
1009 int point = sensor_attr->index;
1010 long val;
1011
1012 if (strict_strtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1013 return -EINVAL;
1014
1015 mutex_lock(&data->update_lock);
1016 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1017 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1018 data->auto_temp[nr][point]);
1019 mutex_unlock(&data->update_lock);
1020 return count;
1021}
1022
Jean Delvare20ad93d2005-06-05 11:53:25 +02001023#define show_fan_offset(offset) \
1024static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1025 show_fan, NULL, offset - 1); \
1026static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1027 show_fan_min, set_fan_min, offset - 1); \
1028static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1029 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031show_fan_offset(1);
1032show_fan_offset(2);
1033show_fan_offset(3);
1034
1035#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +02001036static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1037 show_pwm_enable, set_pwm_enable, offset - 1); \
1038static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +01001039 show_pwm, set_pwm, offset - 1); \
1040static DEVICE_ATTR(pwm##offset##_freq, \
1041 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001042 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1043static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001044 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1045 offset - 1); \
1046static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1047 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1048 offset - 1, 0); \
1049static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1050 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1051 offset - 1, 1); \
1052static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1053 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1054 offset - 1, 2); \
1055static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1056 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1057static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1058 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1059 offset - 1, 1); \
1060static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1061 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1062 offset - 1, 0); \
1063static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1064 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1065 offset - 1, 2); \
1066static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1067 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1068 offset - 1, 3); \
1069static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1070 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1071 offset - 1, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073show_pwm_offset(1);
1074show_pwm_offset(2);
1075show_pwm_offset(3);
1076
Jean Delvare17d648b2006-08-28 14:23:46 +02001077/* A different set of callbacks for 16-bit fans */
1078static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1079 char *buf)
1080{
1081 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1082 int nr = sensor_attr->index;
1083 struct it87_data *data = it87_update_device(dev);
1084 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1085}
1086
1087static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1088 char *buf)
1089{
1090 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1091 int nr = sensor_attr->index;
1092 struct it87_data *data = it87_update_device(dev);
1093 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1094}
1095
1096static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1097 const char *buf, size_t count)
1098{
1099 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1100 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001101 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001102 long val;
1103
1104 if (strict_strtol(buf, 10, &val) < 0)
1105 return -EINVAL;
Jean Delvare17d648b2006-08-28 14:23:46 +02001106
1107 mutex_lock(&data->update_lock);
1108 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001109 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001110 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001111 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001112 data->fan_min[nr] >> 8);
1113 mutex_unlock(&data->update_lock);
1114 return count;
1115}
1116
1117/* We want to use the same sysfs file names as 8-bit fans, but we need
1118 different variable names, so we have to use SENSOR_ATTR instead of
1119 SENSOR_DEVICE_ATTR. */
1120#define show_fan16_offset(offset) \
1121static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1122 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1123 show_fan16, NULL, offset - 1); \
1124static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1125 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1126 show_fan16_min, set_fan16_min, offset - 1)
1127
1128show_fan16_offset(1);
1129show_fan16_offset(2);
1130show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001131show_fan16_offset(4);
1132show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +02001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001135static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1136 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001139 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
Jean Delvare1d66c642005-04-18 21:16:59 -07001141static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Jean Delvare0124dd72007-11-25 16:16:41 +01001143static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1144 char *buf)
1145{
1146 int bitnr = to_sensor_dev_attr(attr)->index;
1147 struct it87_data *data = it87_update_device(dev);
1148 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1149}
1150static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1151static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1152static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1153static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1154static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1155static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1156static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1157static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1158static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1159static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1160static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1161static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1162static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1163static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1164static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1165static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1166
Jean Delvared9b327c2010-03-05 22:17:17 +01001167static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1168 char *buf)
1169{
1170 int bitnr = to_sensor_dev_attr(attr)->index;
1171 struct it87_data *data = it87_update_device(dev);
1172 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1173}
1174static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1175 const char *buf, size_t count)
1176{
1177 int bitnr = to_sensor_dev_attr(attr)->index;
1178 struct it87_data *data = dev_get_drvdata(dev);
1179 long val;
1180
1181 if (strict_strtol(buf, 10, &val) < 0
1182 || (val != 0 && val != 1))
1183 return -EINVAL;
1184
1185 mutex_lock(&data->update_lock);
1186 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1187 if (val)
1188 data->beeps |= (1 << bitnr);
1189 else
1190 data->beeps &= ~(1 << bitnr);
1191 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1192 mutex_unlock(&data->update_lock);
1193 return count;
1194}
1195
1196static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1197 show_beep, set_beep, 1);
1198static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1199static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1200static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1201static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1202static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1203static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1204static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1205/* fanX_beep writability is set later */
1206static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1207static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1208static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1209static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1210static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1211static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1212 show_beep, set_beep, 2);
1213static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1214static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1215
Jean Delvare5f2dc792010-03-05 22:17:18 +01001216static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1217 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Jean Delvare90d66192007-10-08 18:24:35 +02001219 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001220 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001222static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1223 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001225 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001226 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Jean Delvaref5f64502010-03-05 22:17:19 +01001228 if (strict_strtoul(buf, 10, &val) < 0)
1229 return -EINVAL;
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 data->vrm = val;
1232
1233 return count;
1234}
1235static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Jean Delvare5f2dc792010-03-05 22:17:18 +01001237static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1238 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct it87_data *data = it87_update_device(dev);
1241 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1242}
1243static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001244
Jean Delvare738e5e02010-08-14 21:08:50 +02001245static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1246 char *buf)
1247{
1248 static const char *labels[] = {
1249 "+5V",
1250 "5VSB",
1251 "Vbat",
1252 };
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001253 static const char *labels_it8721[] = {
1254 "+3.3V",
1255 "3VSB",
1256 "Vbat",
1257 };
1258 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001259 int nr = to_sensor_dev_attr(attr)->index;
1260
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001261 return sprintf(buf, "%s\n", data->type == it8721 ? labels_it8721[nr]
1262 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001263}
1264static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1265static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1266static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1267
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001268static ssize_t show_name(struct device *dev, struct device_attribute
1269 *devattr, char *buf)
1270{
1271 struct it87_data *data = dev_get_drvdata(dev);
1272 return sprintf(buf, "%s\n", data->name);
1273}
1274static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1275
Jean Delvare87808be2006-09-24 21:17:13 +02001276static struct attribute *it87_attributes[] = {
1277 &sensor_dev_attr_in0_input.dev_attr.attr,
1278 &sensor_dev_attr_in1_input.dev_attr.attr,
1279 &sensor_dev_attr_in2_input.dev_attr.attr,
1280 &sensor_dev_attr_in3_input.dev_attr.attr,
1281 &sensor_dev_attr_in4_input.dev_attr.attr,
1282 &sensor_dev_attr_in5_input.dev_attr.attr,
1283 &sensor_dev_attr_in6_input.dev_attr.attr,
1284 &sensor_dev_attr_in7_input.dev_attr.attr,
1285 &sensor_dev_attr_in8_input.dev_attr.attr,
1286 &sensor_dev_attr_in0_min.dev_attr.attr,
1287 &sensor_dev_attr_in1_min.dev_attr.attr,
1288 &sensor_dev_attr_in2_min.dev_attr.attr,
1289 &sensor_dev_attr_in3_min.dev_attr.attr,
1290 &sensor_dev_attr_in4_min.dev_attr.attr,
1291 &sensor_dev_attr_in5_min.dev_attr.attr,
1292 &sensor_dev_attr_in6_min.dev_attr.attr,
1293 &sensor_dev_attr_in7_min.dev_attr.attr,
1294 &sensor_dev_attr_in0_max.dev_attr.attr,
1295 &sensor_dev_attr_in1_max.dev_attr.attr,
1296 &sensor_dev_attr_in2_max.dev_attr.attr,
1297 &sensor_dev_attr_in3_max.dev_attr.attr,
1298 &sensor_dev_attr_in4_max.dev_attr.attr,
1299 &sensor_dev_attr_in5_max.dev_attr.attr,
1300 &sensor_dev_attr_in6_max.dev_attr.attr,
1301 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001302 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1303 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1304 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1305 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1306 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1307 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1308 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1309 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001310
1311 &sensor_dev_attr_temp1_input.dev_attr.attr,
1312 &sensor_dev_attr_temp2_input.dev_attr.attr,
1313 &sensor_dev_attr_temp3_input.dev_attr.attr,
1314 &sensor_dev_attr_temp1_max.dev_attr.attr,
1315 &sensor_dev_attr_temp2_max.dev_attr.attr,
1316 &sensor_dev_attr_temp3_max.dev_attr.attr,
1317 &sensor_dev_attr_temp1_min.dev_attr.attr,
1318 &sensor_dev_attr_temp2_min.dev_attr.attr,
1319 &sensor_dev_attr_temp3_min.dev_attr.attr,
1320 &sensor_dev_attr_temp1_type.dev_attr.attr,
1321 &sensor_dev_attr_temp2_type.dev_attr.attr,
1322 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001323 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1324 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1325 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001326
1327 &dev_attr_alarms.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001328 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001329 NULL
1330};
1331
1332static const struct attribute_group it87_group = {
1333 .attrs = it87_attributes,
1334};
1335
Jean Delvared9b327c2010-03-05 22:17:17 +01001336static struct attribute *it87_attributes_beep[] = {
1337 &sensor_dev_attr_in0_beep.dev_attr.attr,
1338 &sensor_dev_attr_in1_beep.dev_attr.attr,
1339 &sensor_dev_attr_in2_beep.dev_attr.attr,
1340 &sensor_dev_attr_in3_beep.dev_attr.attr,
1341 &sensor_dev_attr_in4_beep.dev_attr.attr,
1342 &sensor_dev_attr_in5_beep.dev_attr.attr,
1343 &sensor_dev_attr_in6_beep.dev_attr.attr,
1344 &sensor_dev_attr_in7_beep.dev_attr.attr,
1345
1346 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1347 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1348 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1349 NULL
1350};
1351
1352static const struct attribute_group it87_group_beep = {
1353 .attrs = it87_attributes_beep,
1354};
1355
Jean Delvare723a0aa2010-03-05 22:17:16 +01001356static struct attribute *it87_attributes_fan16[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001357 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1358 &sensor_dev_attr_fan1_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001359 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1360 NULL
1361}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001362 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1363 &sensor_dev_attr_fan2_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001364 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1365 NULL
1366}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001367 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1368 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001369 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1370 NULL
1371}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001372 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1373 &sensor_dev_attr_fan4_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001374 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1375 NULL
1376}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001377 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1378 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001379 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1380 NULL
1381} };
Jean Delvare87808be2006-09-24 21:17:13 +02001382
Jean Delvare723a0aa2010-03-05 22:17:16 +01001383static const struct attribute_group it87_group_fan16[5] = {
1384 { .attrs = it87_attributes_fan16[0] },
1385 { .attrs = it87_attributes_fan16[1] },
1386 { .attrs = it87_attributes_fan16[2] },
1387 { .attrs = it87_attributes_fan16[3] },
1388 { .attrs = it87_attributes_fan16[4] },
1389};
1390
1391static struct attribute *it87_attributes_fan[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001392 &sensor_dev_attr_fan1_input.dev_attr.attr,
1393 &sensor_dev_attr_fan1_min.dev_attr.attr,
1394 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001395 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1396 NULL
1397}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001398 &sensor_dev_attr_fan2_input.dev_attr.attr,
1399 &sensor_dev_attr_fan2_min.dev_attr.attr,
1400 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001401 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1402 NULL
1403}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001404 &sensor_dev_attr_fan3_input.dev_attr.attr,
1405 &sensor_dev_attr_fan3_min.dev_attr.attr,
1406 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001407 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001408 NULL
1409} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001410
Jean Delvare723a0aa2010-03-05 22:17:16 +01001411static const struct attribute_group it87_group_fan[3] = {
1412 { .attrs = it87_attributes_fan[0] },
1413 { .attrs = it87_attributes_fan[1] },
1414 { .attrs = it87_attributes_fan[2] },
1415};
1416
1417static const struct attribute_group *
1418it87_get_fan_group(const struct it87_data *data)
1419{
1420 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1421}
1422
1423static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001424 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001425 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001426 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001427 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001428 NULL
1429}, {
1430 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1431 &sensor_dev_attr_pwm2.dev_attr.attr,
1432 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001433 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001434 NULL
1435}, {
1436 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1437 &sensor_dev_attr_pwm3.dev_attr.attr,
1438 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001439 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001440 NULL
1441} };
Jean Delvare87808be2006-09-24 21:17:13 +02001442
Jean Delvare723a0aa2010-03-05 22:17:16 +01001443static const struct attribute_group it87_group_pwm[3] = {
1444 { .attrs = it87_attributes_pwm[0] },
1445 { .attrs = it87_attributes_pwm[1] },
1446 { .attrs = it87_attributes_pwm[2] },
1447};
1448
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001449static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1450 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1451 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1452 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1453 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1454 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1455 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1456 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1457 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1458 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1459 NULL
1460}, {
1461 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1462 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1463 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1464 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1465 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1466 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1467 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1468 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1469 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1470 NULL
1471}, {
1472 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1473 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1474 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1475 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1476 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1477 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1478 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1479 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1480 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1481 NULL
1482} };
1483
1484static const struct attribute_group it87_group_autopwm[3] = {
1485 { .attrs = it87_attributes_autopwm[0] },
1486 { .attrs = it87_attributes_autopwm[1] },
1487 { .attrs = it87_attributes_autopwm[2] },
1488};
1489
Jean Delvared9b327c2010-03-05 22:17:17 +01001490static struct attribute *it87_attributes_fan_beep[] = {
1491 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1492 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1493 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1494 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1495 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1496};
1497
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001498static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001499 &dev_attr_vrm.attr,
1500 &dev_attr_cpu0_vid.attr,
1501 NULL
1502};
1503
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001504static const struct attribute_group it87_group_vid = {
1505 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001506};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Jean Delvare738e5e02010-08-14 21:08:50 +02001508static struct attribute *it87_attributes_label[] = {
1509 &sensor_dev_attr_in3_label.dev_attr.attr,
1510 &sensor_dev_attr_in7_label.dev_attr.attr,
1511 &sensor_dev_attr_in8_label.dev_attr.attr,
1512 NULL
1513};
1514
1515static const struct attribute_group it87_group_label = {
1516 .attrs = it87_attributes_vid,
1517};
1518
Jean Delvare2d8672c2005-07-19 23:56:35 +02001519/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001520static int __init it87_find(unsigned short *address,
1521 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 int err = -ENODEV;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001524 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001525 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 superio_enter();
Jean Delvare67b671b2007-12-06 23:13:42 +01001528 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001529
1530 switch (chip_type) {
1531 case IT8705F_DEVID:
1532 sio_data->type = it87;
1533 break;
1534 case IT8712F_DEVID:
1535 sio_data->type = it8712;
1536 break;
1537 case IT8716F_DEVID:
1538 case IT8726F_DEVID:
1539 sio_data->type = it8716;
1540 break;
1541 case IT8718F_DEVID:
1542 sio_data->type = it8718;
1543 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001544 case IT8720F_DEVID:
1545 sio_data->type = it8720;
1546 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001547 case IT8721F_DEVID:
1548 sio_data->type = it8721;
1549 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001550 case 0xffff: /* No device at all */
1551 goto exit;
1552 default:
1553 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
1554 chip_type);
1555 goto exit;
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Jean Delvare87673dd2006-08-28 14:37:19 +02001558 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1560 pr_info("it87: Device not activated, skipping\n");
1561 goto exit;
1562 }
1563
1564 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1565 if (*address == 0) {
1566 pr_info("it87: Base address not set, skipping\n");
1567 goto exit;
1568 }
1569
1570 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001571 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001573 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Jean Delvare738e5e02010-08-14 21:08:50 +02001575 /* in8 (Vbat) is always internal */
1576 sio_data->internal = (1 << 2);
1577
Jean Delvare87673dd2006-08-28 14:37:19 +02001578 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001579 if (sio_data->type == it87) {
1580 /* The IT8705F doesn't have VID pins at all */
1581 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001582
1583 /* The IT8705F has a different LD number for GPIO */
1584 superio_select(5);
1585 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01001586 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001587 int reg;
1588
1589 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001590
Jean Delvare895ff262009-12-09 20:35:47 +01001591 reg = superio_inb(IT87_SIO_GPIO3_REG);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001592 if (sio_data->type == it8721) {
1593 /* The IT8721F/IT8758E doesn't have VID pins at all */
Jean Delvare895ff262009-12-09 20:35:47 +01001594 sio_data->skip_vid = 1;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001595 } else {
1596 /* We need at least 4 VID pins */
1597 if (reg & 0x0f) {
1598 pr_info("it87: VID is disabled (pins used for GPIO)\n");
1599 sio_data->skip_vid = 1;
1600 }
Jean Delvare895ff262009-12-09 20:35:47 +01001601 }
1602
Jean Delvare591ec652009-12-09 20:35:48 +01001603 /* Check if fan3 is there or not */
1604 if (reg & (1 << 6))
1605 sio_data->skip_pwm |= (1 << 2);
1606 if (reg & (1 << 7))
1607 sio_data->skip_fan |= (1 << 2);
1608
1609 /* Check if fan2 is there or not */
1610 reg = superio_inb(IT87_SIO_GPIO5_REG);
1611 if (reg & (1 << 1))
1612 sio_data->skip_pwm |= (1 << 1);
1613 if (reg & (1 << 2))
1614 sio_data->skip_fan |= (1 << 1);
1615
Jean Delvare895ff262009-12-09 20:35:47 +01001616 if ((sio_data->type == it8718 || sio_data->type == it8720)
1617 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001618 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001619
1620 reg = superio_inb(IT87_SIO_PINX2_REG);
Jean Delvare436cad22010-07-09 16:22:48 +02001621 /*
1622 * The IT8720F has no VIN7 pin, so VCCH should always be
1623 * routed internally to VIN7 with an internal divider.
1624 * Curiously, there still is a configuration bit to control
1625 * this, which means it can be set incorrectly. And even
1626 * more curiously, many boards out there are improperly
1627 * configured, even though the IT8720F datasheet claims
1628 * that the internal routing of VCCH to VIN7 is the default
1629 * setting. So we force the internal routing in this case.
1630 */
1631 if (sio_data->type == it8720 && !(reg & (1 << 1))) {
1632 reg |= (1 << 1);
1633 superio_outb(IT87_SIO_PINX2_REG, reg);
1634 pr_notice("it87: Routing internal VCCH to in7\n");
1635 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001636 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001637 sio_data->internal |= (1 << 0);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001638 if ((reg & (1 << 1)) || sio_data->type == it8721)
Jean Delvare738e5e02010-08-14 21:08:50 +02001639 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01001640
1641 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02001642 }
Jean Delvared9b327c2010-03-05 22:17:17 +01001643 if (sio_data->beep_pin)
1644 pr_info("it87: Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02001645
Jean Delvare98dd22c2008-10-09 15:33:58 +02001646 /* Disable specific features based on DMI strings */
1647 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1648 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1649 if (board_vendor && board_name) {
1650 if (strcmp(board_vendor, "nVIDIA") == 0
1651 && strcmp(board_name, "FN68PT") == 0) {
1652 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1653 connected to a fan, but to something else. One user
1654 has reported instant system power-off when changing
1655 the PWM2 duty cycle, so we disable it.
1656 I use the board name string as the trigger in case
1657 the same board is ever used in other systems. */
1658 pr_info("it87: Disabling pwm2 due to "
1659 "hardware constraints\n");
1660 sio_data->skip_pwm = (1 << 1);
1661 }
1662 }
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664exit:
1665 superio_exit();
1666 return err;
1667}
1668
Jean Delvare723a0aa2010-03-05 22:17:16 +01001669static void it87_remove_files(struct device *dev)
1670{
1671 struct it87_data *data = platform_get_drvdata(pdev);
1672 struct it87_sio_data *sio_data = dev->platform_data;
1673 const struct attribute_group *fan_group = it87_get_fan_group(data);
1674 int i;
1675
1676 sysfs_remove_group(&dev->kobj, &it87_group);
Jean Delvared9b327c2010-03-05 22:17:17 +01001677 if (sio_data->beep_pin)
1678 sysfs_remove_group(&dev->kobj, &it87_group_beep);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001679 for (i = 0; i < 5; i++) {
1680 if (!(data->has_fan & (1 << i)))
1681 continue;
1682 sysfs_remove_group(&dev->kobj, &fan_group[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01001683 if (sio_data->beep_pin)
1684 sysfs_remove_file(&dev->kobj,
1685 it87_attributes_fan_beep[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001686 }
1687 for (i = 0; i < 3; i++) {
1688 if (sio_data->skip_pwm & (1 << 0))
1689 continue;
1690 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001691 if (has_old_autopwm(data))
1692 sysfs_remove_group(&dev->kobj,
1693 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001694 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001695 if (!sio_data->skip_vid)
1696 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02001697 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001698}
1699
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001700static int __devinit it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001703 struct resource *res;
1704 struct device *dev = &pdev->dev;
1705 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001706 const struct attribute_group *fan_group;
1707 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01001709 int fan_beep_need_rw;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001710 static const char *names[] = {
1711 "it87",
1712 "it8712",
1713 "it8716",
1714 "it8718",
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001715 "it8720",
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001716 "it8721",
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001717 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001719 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001720 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001721 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1722 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001723 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001724 err = -EBUSY;
1725 goto ERROR0;
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Jean Delvare5f2dc792010-03-05 22:17:18 +01001728 data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
1729 if (!data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 err = -ENOMEM;
1731 goto ERROR1;
1732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001734 data->addr = res->start;
1735 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001736 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001737 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001740 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1741 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001742 err = -ENODEV;
1743 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001746 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001748 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001751 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001753 /* Starting with IT8721F, we handle scaling of internal voltages */
1754 if (data->type == it8721) {
1755 if (sio_data->internal & (1 << 0))
1756 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1757 if (sio_data->internal & (1 << 1))
1758 data->in_scaled |= (1 << 7); /* in7 is VSB */
1759 if (sio_data->internal & (1 << 2))
1760 data->in_scaled |= (1 << 8); /* in8 is Vbat */
1761 }
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001764 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001767 err = sysfs_create_group(&dev->kobj, &it87_group);
1768 if (err)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001769 goto ERROR2;
Jean Delvare17d648b2006-08-28 14:23:46 +02001770
Jean Delvared9b327c2010-03-05 22:17:17 +01001771 if (sio_data->beep_pin) {
1772 err = sysfs_create_group(&dev->kobj, &it87_group_beep);
1773 if (err)
1774 goto ERROR4;
1775 }
1776
Jean Delvare9060f8b2006-08-28 14:24:17 +02001777 /* Do not create fan files for disabled fans */
Jean Delvare723a0aa2010-03-05 22:17:16 +01001778 fan_group = it87_get_fan_group(data);
Jean Delvared9b327c2010-03-05 22:17:17 +01001779 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001780 for (i = 0; i < 5; i++) {
1781 if (!(data->has_fan & (1 << i)))
1782 continue;
1783 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
1784 if (err)
1785 goto ERROR4;
Jean Delvared9b327c2010-03-05 22:17:17 +01001786
1787 if (sio_data->beep_pin) {
1788 err = sysfs_create_file(&dev->kobj,
1789 it87_attributes_fan_beep[i]);
1790 if (err)
1791 goto ERROR4;
1792 if (!fan_beep_need_rw)
1793 continue;
1794
1795 /* As we have a single beep enable bit for all fans,
1796 * only the first enabled fan has a writable attribute
1797 * for it. */
1798 if (sysfs_chmod_file(&dev->kobj,
1799 it87_attributes_fan_beep[i],
1800 S_IRUGO | S_IWUSR))
1801 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
1802 i + 1);
1803 fan_beep_need_rw = 0;
1804 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001805 }
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001808 for (i = 0; i < 3; i++) {
1809 if (sio_data->skip_pwm & (1 << i))
1810 continue;
1811 err = sysfs_create_group(&dev->kobj,
1812 &it87_group_pwm[i]);
1813 if (err)
Jean Delvare98dd22c2008-10-09 15:33:58 +02001814 goto ERROR4;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001815
1816 if (!has_old_autopwm(data))
1817 continue;
1818 err = sysfs_create_group(&dev->kobj,
1819 &it87_group_autopwm[i]);
1820 if (err)
1821 goto ERROR4;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824
Jean Delvare895ff262009-12-09 20:35:47 +01001825 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02001826 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001827 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001828 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001829 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
1830 if (err)
Jean Delvare87808be2006-09-24 21:17:13 +02001831 goto ERROR4;
1832 }
1833
Jean Delvare738e5e02010-08-14 21:08:50 +02001834 /* Export labels for internal sensors */
1835 for (i = 0; i < 3; i++) {
1836 if (!(sio_data->internal & (1 << i)))
1837 continue;
1838 err = sysfs_create_file(&dev->kobj,
1839 it87_attributes_label[i]);
1840 if (err)
1841 goto ERROR4;
1842 }
1843
Tony Jones1beeffe2007-08-20 13:46:20 -07001844 data->hwmon_dev = hwmon_device_register(dev);
1845 if (IS_ERR(data->hwmon_dev)) {
1846 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001847 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849
1850 return 0;
1851
Jean Delvare87808be2006-09-24 21:17:13 +02001852ERROR4:
Jean Delvare723a0aa2010-03-05 22:17:16 +01001853 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854ERROR2:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001855 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 kfree(data);
1857ERROR1:
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001858 release_region(res->start, IT87_EC_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859ERROR0:
1860 return err;
1861}
1862
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001863static int __devexit it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001865 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Tony Jones1beeffe2007-08-20 13:46:20 -07001867 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001868 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001869
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001870 release_region(data->addr, IT87_EC_EXTENT);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001871 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001872 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 return 0;
1875}
1876
Jean Delvare7f999aa2007-02-14 21:15:03 +01001877/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1879 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001880static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001882 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1883 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Jean Delvare7f999aa2007-02-14 21:15:03 +01001886/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1888 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001889static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001891 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1892 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
1895/* Return 1 if and only if the PWM interface is safe to use */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001896static int __devinit it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001898 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1900 * and polarity set to active low is sign that this is the case so we
1901 * disable pwm control to protect the user. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001902 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if ((tmp & 0x87) == 0) {
1904 if (fix_pwm_polarity) {
1905 /* The user asks us to attempt a chip reconfiguration.
1906 * This means switching to active high polarity and
1907 * inverting all fan speed values. */
1908 int i;
1909 u8 pwm[3];
1910
1911 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001912 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 IT87_REG_PWM(i));
1914
1915 /* If any fan is in automatic pwm mode, the polarity
1916 * might be correct, as suspicious as it seems, so we
1917 * better don't change anything (but still disable the
1918 * PWM interface). */
1919 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001920 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001922 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 tmp | 0x87);
1924 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001925 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 IT87_REG_PWM(i),
1927 0x7f & ~pwm[i]);
1928 return 1;
1929 }
1930
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001931 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 "too broken to be fixed\n");
1933 }
1934
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001935 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 "defaults, disabling PWM interface\n");
1937 return 0;
1938 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001939 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 "sane, won't touch\n");
1941 }
1942
1943 return 1;
1944}
1945
1946/* Called when we have found a new IT87. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001947static void __devinit it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Jean Delvare591ec652009-12-09 20:35:48 +01001949 struct it87_sio_data *sio_data = pdev->dev.platform_data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001950 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01001952 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Jean Delvareb99883d2010-03-05 22:17:15 +01001954 /* For each PWM channel:
1955 * - If it is in automatic mode, setting to manual mode should set
1956 * the fan to full speed by default.
1957 * - If it is in manual mode, we need a mapping to temperature
1958 * channels to use when later setting to automatic mode later.
1959 * Use a 1:1 mapping by default (we are clueless.)
1960 * In both cases, the value can (and should) be changed by the user
1961 * prior to switching to a different mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01001963 data->pwm_temp_map[i] = i;
1964 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001965 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967
Jean Delvarec5df9b72006-08-28 14:37:54 +02001968 /* Some chips seem to have default value 0xff for all limit
1969 * registers. For low voltage limits it makes no sense and triggers
1970 * alarms, so change to 0 instead. For high temperature limits, it
1971 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1972 * but is still confusing, so change to 127 degrees C. */
1973 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001974 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001975 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001976 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001977 }
1978 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001979 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001980 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001981 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001982 }
1983
Jean Delvarea00afb92010-04-14 16:14:09 +02001984 /* Temperature channels are not forcibly enabled, as they can be
1985 * set to two different sensor types and we can't guess which one
1986 * is correct for a given system. These channels can be enabled at
1987 * run-time through the temp{1-3}_type sysfs accessors if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001990 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if ((tmp & 0xff) == 0) {
1992 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001993 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995
1996 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01001997 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001998 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01001999 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002001 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002002 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2003 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002005 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Jean Delvare17d648b2006-08-28 14:23:46 +02002007 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02002008 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002009 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002010 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002011 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002012 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002013 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002014 tmp | 0x07);
2015 }
Andrew Paprocki816d8c62008-08-06 22:41:06 +02002016 /* IT8705F only supports three fans. */
2017 if (data->type != it87) {
2018 if (tmp & (1 << 4))
2019 data->has_fan |= (1 << 3); /* fan4 enabled */
2020 if (tmp & (1 << 5))
2021 data->has_fan |= (1 << 4); /* fan5 enabled */
2022 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002023 }
2024
Jean Delvare591ec652009-12-09 20:35:48 +01002025 /* Fan input pins may be used for alternative functions */
2026 data->has_fan &= ~sio_data->skip_fan;
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002029 it87_write_value(data, IT87_REG_CONFIG,
2030 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 | (update_vbat ? 0x41 : 0x01));
2032}
2033
Jean Delvareb99883d2010-03-05 22:17:15 +01002034static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2035{
2036 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2037 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2038 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2039 else /* Manual mode */
2040 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002041
2042 if (has_old_autopwm(data)) {
2043 int i;
2044
2045 for (i = 0; i < 5 ; i++)
2046 data->auto_temp[nr][i] = it87_read_value(data,
2047 IT87_REG_AUTO_TEMP(nr, i));
2048 for (i = 0; i < 3 ; i++)
2049 data->auto_pwm[nr][i] = it87_read_value(data,
2050 IT87_REG_AUTO_PWM(nr, i));
2051 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002052}
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054static struct it87_data *it87_update_device(struct device *dev)
2055{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002056 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int i;
2058
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002059 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2062 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (update_vbat) {
2064 /* Cleared after each update, so reenable. Value
Jean Delvare5f2dc792010-03-05 22:17:18 +01002065 returned by this read will be previous value */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002066 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002067 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
2069 for (i = 0; i <= 7; i++) {
2070 data->in[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002071 it87_read_value(data, IT87_REG_VIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 data->in_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002073 it87_read_value(data, IT87_REG_VIN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 data->in_max[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002075 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Jean Delvare3543a532006-08-28 14:27:25 +02002077 /* in8 (battery) has no limit registers */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002078 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Jean Delvarec7f1f712007-09-03 17:11:46 +02002080 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002081 /* Skip disabled fans */
2082 if (!(data->has_fan & (1 << i)))
2083 continue;
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 data->fan_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002086 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002087 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002088 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002089 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002090 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002091 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002092 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002093 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002094 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097 for (i = 0; i < 3; i++) {
2098 data->temp[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002099 it87_read_value(data, IT87_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 data->temp_high[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002101 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 data->temp_low[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002103 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105
Jean Delvare17d648b2006-08-28 14:23:46 +02002106 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002107 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002108 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002109 data->fan_div[0] = i & 0x07;
2110 data->fan_div[1] = (i >> 3) & 0x07;
2111 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002115 it87_read_value(data, IT87_REG_ALARM1) |
2116 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2117 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002118 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002119
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002120 data->fan_main_ctrl = it87_read_value(data,
2121 IT87_REG_FAN_MAIN_CTRL);
2122 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002123 for (i = 0; i < 3; i++)
2124 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002126 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Andrew Paprocki04751692008-08-06 22:41:06 +02002127 /* The 8705 does not have VID capability.
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002128 The 8718 and later don't use IT87_REG_VID for the
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002129 same purpose. */
Jean Delvare17d648b2006-08-28 14:23:46 +02002130 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002131 data->vid = it87_read_value(data, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02002132 /* The older IT8712F revisions had only 5 VID pins,
2133 but we assume it is always safe to read 6 bits. */
2134 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
2136 data->last_updated = jiffies;
2137 data->valid = 1;
2138 }
2139
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002140 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 return data;
2143}
2144
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002145static int __init it87_device_add(unsigned short address,
2146 const struct it87_sio_data *sio_data)
2147{
2148 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002149 .start = address + IT87_EC_OFFSET,
2150 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002151 .name = DRVNAME,
2152 .flags = IORESOURCE_IO,
2153 };
2154 int err;
2155
Jean Delvareb9acb642009-01-07 16:37:35 +01002156 err = acpi_check_resource_conflict(&res);
2157 if (err)
2158 goto exit;
2159
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002160 pdev = platform_device_alloc(DRVNAME, address);
2161 if (!pdev) {
2162 err = -ENOMEM;
2163 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
2164 goto exit;
2165 }
2166
2167 err = platform_device_add_resources(pdev, &res, 1);
2168 if (err) {
2169 printk(KERN_ERR DRVNAME ": Device resource addition failed "
2170 "(%d)\n", err);
2171 goto exit_device_put;
2172 }
2173
2174 err = platform_device_add_data(pdev, sio_data,
2175 sizeof(struct it87_sio_data));
2176 if (err) {
2177 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
2178 goto exit_device_put;
2179 }
2180
2181 err = platform_device_add(pdev);
2182 if (err) {
2183 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
2184 err);
2185 goto exit_device_put;
2186 }
2187
2188 return 0;
2189
2190exit_device_put:
2191 platform_device_put(pdev);
2192exit:
2193 return err;
2194}
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196static int __init sm_it87_init(void)
2197{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002198 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002199 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002200 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002201
Jean Delvare98dd22c2008-10-09 15:33:58 +02002202 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002203 err = it87_find(&isa_address, &sio_data);
2204 if (err)
2205 return err;
2206 err = platform_driver_register(&it87_driver);
2207 if (err)
2208 return err;
2209
2210 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002211 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002212 platform_driver_unregister(&it87_driver);
2213 return err;
2214 }
2215
2216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
2219static void __exit sm_it87_exit(void)
2220{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002221 platform_device_unregister(pdev);
2222 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
2225
Jean Delvaref1d8e332007-11-25 16:14:44 +01002226MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02002227 "Jean Delvare <khali@linux-fr.org>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002228MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229module_param(update_vbat, bool, 0);
2230MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2231module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002232MODULE_PARM_DESC(fix_pwm_polarity,
2233 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234MODULE_LICENSE("GPL");
2235
2236module_init(sm_it87_init);
2237module_exit(sm_it87_exit);