blob: f7701295937dda4fcd6537c58a9236f7b91fcd54 [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
18 * IT8726F Super I/O chip w/LPC interface
19 * Sis950 A clone of the IT8705F
20 *
21 * Copyright (C) 2001 Chris Gauthron
22 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/slab.h>
42#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020043#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040044#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020045#include <linux/hwmon-sysfs.h>
46#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040047#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010048#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020049#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020050#include <linux/string.h>
51#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010052#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020053#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
corentin.labbeb74f3fd2007-06-13 20:27:36 +020055#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010057enum chips { it87, it8712, it8716, it8718, it8720 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jean Delvare67b671b2007-12-06 23:13:42 +010059static unsigned short force_id;
60module_param(force_id, ushort, 0);
61MODULE_PARM_DESC(force_id, "Override the detected device ID");
62
corentin.labbeb74f3fd2007-06-13 20:27:36 +020063static struct platform_device *pdev;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define REG 0x2e /* The register to read/write */
66#define DEV 0x07 /* Register: Logical device select */
67#define VAL 0x2f /* The value to read/write */
68#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010069
70/* The device with the IT8718F/IT8720F VID value in it */
71#define GPIO 0x07
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define DEVID 0x20 /* Register: Device ID */
74#define DEVREV 0x22 /* Register: Device Revision */
75
76static inline int
77superio_inb(int reg)
78{
79 outb(reg, REG);
80 return inb(VAL);
81}
82
Jean Delvare436cad22010-07-09 16:22:48 +020083static inline void
84superio_outb(int reg, int val)
85{
86 outb(reg, REG);
87 outb(val, VAL);
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int superio_inw(int reg)
91{
92 int val;
93 outb(reg++, REG);
94 val = inb(VAL) << 8;
95 outb(reg, REG);
96 val |= inb(VAL);
97 return val;
98}
99
100static inline void
Jean Delvare87673dd2006-08-28 14:37:19 +0200101superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200104 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107static inline void
108superio_enter(void)
109{
110 outb(0x87, REG);
111 outb(0x01, REG);
112 outb(0x55, REG);
113 outb(0x55, REG);
114}
115
116static inline void
117superio_exit(void)
118{
119 outb(0x02, REG);
120 outb(0x02, VAL);
121}
122
Jean Delvare87673dd2006-08-28 14:37:19 +0200123/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define IT8712F_DEVID 0x8712
125#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200126#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200127#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100128#define IT8720F_DEVID 0x8720
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400129#define IT8726F_DEVID 0x8726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define IT87_ACT_REG 0x30
131#define IT87_BASE_REG 0x60
132
Jean Delvare87673dd2006-08-28 14:37:19 +0200133/* Logical device 7 registers (IT8712F and later) */
Jean Delvare895ff262009-12-09 20:35:47 +0100134#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100135#define IT87_SIO_GPIO5_REG 0x29
Jean Delvare87673dd2006-08-28 14:37:19 +0200136#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
137#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100138#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/* Update battery voltage after every reading if true */
141static int update_vbat;
142
143/* Not all BIOSes properly configure the PWM registers */
144static int fix_pwm_polarity;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* Many IT87 constants specified below */
147
148/* Length of ISA address segment */
149#define IT87_EXTENT 8
150
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500151/* Length of ISA address segment for Environmental Controller */
152#define IT87_EC_EXTENT 2
153
154/* Offset of EC registers from ISA base address */
155#define IT87_EC_OFFSET 5
156
157/* Where are the ISA address/data registers relative to the EC base address */
158#define IT87_ADDR_REG_OFFSET 0
159#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/*----- The IT87 registers -----*/
162
163#define IT87_REG_CONFIG 0x00
164
165#define IT87_REG_ALARM1 0x01
166#define IT87_REG_ALARM2 0x02
167#define IT87_REG_ALARM3 0x03
168
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100169/* The IT8718F and IT8720F have the VID value in a different register, in
170 Super-I/O configuration space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#define IT87_REG_VID 0x0a
Andrew Paprocki04751692008-08-06 22:41:06 +0200172/* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
173 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
174 mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200176#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
179
Jean Delvarec7f1f712007-09-03 17:11:46 +0200180static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
181static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
182static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
183static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#define IT87_REG_FAN_MAIN_CTRL 0x13
185#define IT87_REG_FAN_CTL 0x14
186#define IT87_REG_PWM(nr) (0x15 + (nr))
187
188#define IT87_REG_VIN(nr) (0x20 + (nr))
189#define IT87_REG_TEMP(nr) (0x29 + (nr))
190
191#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
192#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
193#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
194#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196#define IT87_REG_VIN_ENABLE 0x50
197#define IT87_REG_TEMP_ENABLE 0x51
Jean Delvared9b327c2010-03-05 22:17:17 +0100198#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200#define IT87_REG_CHIPID 0x58
201
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100202#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
203#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
206#define IN_FROM_REG(val) ((val) * 16)
207
208static inline u8 FAN_TO_REG(long rpm, int div)
209{
210 if (rpm == 0)
211 return 255;
212 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
213 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
214 254);
215}
216
Jean Delvare17d648b2006-08-28 14:23:46 +0200217static inline u16 FAN16_TO_REG(long rpm)
218{
219 if (rpm == 0)
220 return 0xffff;
221 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
Jean Delvare17d648b2006-08-28 14:23:46 +0200225/* The divider is fixed to 2 in 16-bit mode */
226#define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
229 ((val)+500)/1000),-128,127))
Jean Delvaree267d252009-03-12 13:36:39 +0100230#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232#define PWM_TO_REG(val) ((val) >> 1)
233#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
234
235static int DIV_TO_REG(int val)
236{
237 int answer = 0;
Jean Delvareb9e349f2006-08-28 14:26:22 +0200238 while (answer < 7 && (val >>= 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 answer++;
240 return answer;
241}
242#define DIV_FROM_REG(val) (1 << (val))
243
Jean Delvaref8d0c192007-02-14 21:15:02 +0100244static const unsigned int pwm_freq[8] = {
245 48000000 / 128,
246 24000000 / 128,
247 12000000 / 128,
248 8000000 / 128,
249 6000000 / 128,
250 3000000 / 128,
251 1500000 / 128,
252 750000 / 128,
253};
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200256struct it87_sio_data {
257 enum chips type;
258 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200259 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200260 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100261 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200262 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100263 /* Features skipped based on config or DMI */
Jean Delvare895ff262009-12-09 20:35:47 +0100264 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100265 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200266 u8 skip_pwm;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200267};
268
Jean Delvareed6bafb2007-02-14 21:15:03 +0100269/* For each registered chip, we need to keep some data in memory.
270 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700272 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 enum chips type;
Andrew Paprocki04751692008-08-06 22:41:06 +0200274 u8 revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200276 unsigned short addr;
277 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100278 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 char valid; /* !=0 if following fields are valid */
280 unsigned long last_updated; /* In jiffies */
281
282 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200283 u8 in_max[8]; /* Register value */
284 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200285 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvarec7f1f712007-09-03 17:11:46 +0200286 u16 fan[5]; /* Register values, possibly combined */
287 u16 fan_min[5]; /* Register values, possibly combined */
Jean Delvaree267d252009-03-12 13:36:39 +0100288 s8 temp[3]; /* Register value */
289 s8 temp_high[3]; /* Register value */
290 s8 temp_low[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 u8 sensor; /* Register value */
292 u8 fan_div[3]; /* Register encoding, shifted right */
293 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100294 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100296 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100298 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100299
300 /* The following 3 arrays correspond to the same registers. The
301 * meaning of bits 6-0 depends on the value of bit 7, and we want
302 * to preserve settings on mode changes, so we have to track all
303 * values separately. */
304 u8 pwm_ctrl[3]; /* Register value */
305 u8 pwm_duty[3]; /* Manual PWM value set by user (bit 6-0) */
306 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100307
308 /* Automatic fan speed control registers */
309 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
310 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
Andrew Paprocki04751692008-08-06 22:41:06 +0200313static inline int has_16bit_fans(const struct it87_data *data)
314{
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200315 /* IT8705F Datasheet 0.4.1, 3h == Version G.
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200316 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200317 These are the first revisions with 16bit tachometer support. */
318 return (data->type == it87 && data->revision >= 0x03)
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200319 || (data->type == it8712 && data->revision >= 0x08)
Andrew Paprocki04751692008-08-06 22:41:06 +0200320 || data->type == it8716
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100321 || data->type == it8718
322 || data->type == it8720;
Andrew Paprocki04751692008-08-06 22:41:06 +0200323}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100325static inline int has_old_autopwm(const struct it87_data *data)
326{
327 /* The old automatic fan speed control interface is implemented
328 by IT8705F chips up to revision F and IT8712F chips up to
329 revision G. */
330 return (data->type == it87 && data->revision < 0x03)
331 || (data->type == it8712 && data->revision < 0x08);
332}
333
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200334static int it87_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200335static int __devexit it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200337static int it87_read_value(struct it87_data *data, u8 reg);
338static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200340static int it87_check_pwm(struct device *dev);
341static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200344static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100345 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200346 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200347 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100348 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200349 .probe = it87_probe,
350 .remove = __devexit_p(it87_remove),
Jean Delvarefde09502005-07-19 23:51:07 +0200351};
352
Jean Delvare20ad93d2005-06-05 11:53:25 +0200353static ssize_t show_in(struct device *dev, struct device_attribute *attr,
354 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200356 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
357 int nr = sensor_attr->index;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct it87_data *data = it87_update_device(dev);
360 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
361}
362
Jean Delvare20ad93d2005-06-05 11:53:25 +0200363static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
364 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200366 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
367 int nr = sensor_attr->index;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct it87_data *data = it87_update_device(dev);
370 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
371}
372
Jean Delvare20ad93d2005-06-05 11:53:25 +0200373static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
374 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200376 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
377 int nr = sensor_attr->index;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct it87_data *data = it87_update_device(dev);
380 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
381}
382
Jean Delvare20ad93d2005-06-05 11:53:25 +0200383static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
384 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200386 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
387 int nr = sensor_attr->index;
388
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200389 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100390 unsigned long val;
391
392 if (strict_strtoul(buf, 10, &val) < 0)
393 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100395 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 data->in_min[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200397 it87_write_value(data, IT87_REG_VIN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100399 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return count;
401}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200402static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
403 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200405 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
406 int nr = sensor_attr->index;
407
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200408 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100409 unsigned long val;
410
411 if (strict_strtoul(buf, 10, &val) < 0)
412 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100414 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 data->in_max[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200416 it87_write_value(data, IT87_REG_VIN_MAX(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100418 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return count;
420}
421
422#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200423static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
424 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200427static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
428 show_in_min, set_in_min, offset); \
429static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
430 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432show_in_offset(0);
433limit_in_offset(0);
434show_in_offset(1);
435limit_in_offset(1);
436show_in_offset(2);
437limit_in_offset(2);
438show_in_offset(3);
439limit_in_offset(3);
440show_in_offset(4);
441limit_in_offset(4);
442show_in_offset(5);
443limit_in_offset(5);
444show_in_offset(6);
445limit_in_offset(6);
446show_in_offset(7);
447limit_in_offset(7);
448show_in_offset(8);
449
450/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200451static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
452 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200454 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
455 int nr = sensor_attr->index;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct it87_data *data = it87_update_device(dev);
458 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
459}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200460static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
461 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200463 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
464 int nr = sensor_attr->index;
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct it87_data *data = it87_update_device(dev);
467 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
468}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200469static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
470 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200472 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
473 int nr = sensor_attr->index;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct it87_data *data = it87_update_device(dev);
476 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
477}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200478static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
479 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200481 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
482 int nr = sensor_attr->index;
483
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200484 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100485 long val;
486
487 if (strict_strtol(buf, 10, &val) < 0)
488 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100490 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 data->temp_high[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200492 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100493 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return count;
495}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200496static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
497 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200499 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
500 int nr = sensor_attr->index;
501
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200502 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100503 long val;
504
505 if (strict_strtol(buf, 10, &val) < 0)
506 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100508 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 data->temp_low[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200510 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100511 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return count;
513}
514#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200515static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
516 show_temp, NULL, offset - 1); \
517static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
518 show_temp_max, set_temp_max, offset - 1); \
519static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
520 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522show_temp_offset(1);
523show_temp_offset(2);
524show_temp_offset(3);
525
Jean Delvare20ad93d2005-06-05 11:53:25 +0200526static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
527 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200529 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
530 int nr = sensor_attr->index;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100533 u8 reg = data->sensor; /* In case the value is updated while
534 we use it */
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (reg & (1 << nr))
537 return sprintf(buf, "3\n"); /* thermal diode */
538 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200539 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return sprintf(buf, "0\n"); /* disabled */
541}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200542static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
543 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200545 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
546 int nr = sensor_attr->index;
547
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200548 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100549 long val;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200550 u8 reg;
Jean Delvaref5f64502010-03-05 22:17:19 +0100551
552 if (strict_strtol(buf, 10, &val) < 0)
553 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Jean Delvare8acf07c2010-04-14 16:14:09 +0200555 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
556 reg &= ~(1 << nr);
557 reg &= ~(8 << nr);
Jean Delvare4ed10772008-10-17 17:51:16 +0200558 if (val == 2) { /* backwards compatibility */
559 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
560 "instead\n");
561 val = 4;
562 }
563 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200565 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200566 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200567 reg |= 8 << nr;
568 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200570
571 mutex_lock(&data->update_lock);
572 data->sensor = reg;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200573 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200574 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100575 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return count;
577}
578#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200579static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
580 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582show_sensor_offset(1);
583show_sensor_offset(2);
584show_sensor_offset(3);
585
586/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100587
588static int pwm_mode(const struct it87_data *data, int nr)
589{
590 int ctrl = data->fan_main_ctrl & (1 << nr);
591
592 if (ctrl == 0) /* Full speed */
593 return 0;
594 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
595 return 2;
596 else /* Manual mode */
597 return 1;
598}
599
Jean Delvare20ad93d2005-06-05 11:53:25 +0200600static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
601 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200603 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
604 int nr = sensor_attr->index;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100607 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 DIV_FROM_REG(data->fan_div[nr])));
609}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200610static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
611 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200613 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
614 int nr = sensor_attr->index;
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100617 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
618 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200620static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
621 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200623 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
624 int nr = sensor_attr->index;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct it87_data *data = it87_update_device(dev);
627 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
628}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100629static ssize_t show_pwm_enable(struct device *dev,
630 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200632 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
633 int nr = sensor_attr->index;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100636 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200638static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
639 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200641 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
642 int nr = sensor_attr->index;
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100645 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100647static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
648 char *buf)
649{
650 struct it87_data *data = it87_update_device(dev);
651 int index = (data->fan_ctl >> 4) & 0x07;
652
653 return sprintf(buf, "%u\n", pwm_freq[index]);
654}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200655static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
656 const char *buf, size_t count)
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
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200661 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100662 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100663 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Jean Delvaref5f64502010-03-05 22:17:19 +0100665 if (strict_strtol(buf, 10, &val) < 0)
666 return -EINVAL;
667
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100668 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200669 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800670 switch (nr) {
Jean Delvare5f2dc792010-03-05 22:17:18 +0100671 case 0:
672 data->fan_div[nr] = reg & 0x07;
673 break;
674 case 1:
675 data->fan_div[nr] = (reg >> 3) & 0x07;
676 break;
677 case 2:
678 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
679 break;
Jean Delvare07eab462005-11-23 15:44:31 -0800680 }
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200683 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100684 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return count;
686}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200687static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
688 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200690 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
691 int nr = sensor_attr->index;
692
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200693 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100694 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200695 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 u8 old;
697
Jean Delvaref5f64502010-03-05 22:17:19 +0100698 if (strict_strtoul(buf, 10, &val) < 0)
699 return -EINVAL;
700
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100701 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200702 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200704 /* Save fan min limit */
705 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 switch (nr) {
708 case 0:
709 case 1:
710 data->fan_div[nr] = DIV_TO_REG(val);
711 break;
712 case 2:
713 if (val < 8)
714 data->fan_div[nr] = 1;
715 else
716 data->fan_div[nr] = 3;
717 }
718 val = old & 0x80;
719 val |= (data->fan_div[0] & 0x07);
720 val |= (data->fan_div[1] & 0x07) << 3;
721 if (data->fan_div[2] == 3)
722 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200723 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200725 /* Restore fan min limit */
726 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200727 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200728
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100729 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return count;
731}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100732
733/* Returns 0 if OK, -EINVAL otherwise */
734static int check_trip_points(struct device *dev, int nr)
735{
736 const struct it87_data *data = dev_get_drvdata(dev);
737 int i, err = 0;
738
739 if (has_old_autopwm(data)) {
740 for (i = 0; i < 3; i++) {
741 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
742 err = -EINVAL;
743 }
744 for (i = 0; i < 2; i++) {
745 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
746 err = -EINVAL;
747 }
748 }
749
750 if (err) {
751 dev_err(dev, "Inconsistent trip points, not switching to "
752 "automatic mode\n");
753 dev_err(dev, "Adjust the trip points and try again\n");
754 }
755 return err;
756}
757
Jean Delvare20ad93d2005-06-05 11:53:25 +0200758static ssize_t set_pwm_enable(struct device *dev,
759 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200761 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
762 int nr = sensor_attr->index;
763
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200764 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100765 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Jean Delvaref5f64502010-03-05 22:17:19 +0100767 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100768 return -EINVAL;
769
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100770 /* Check trip points before switching to automatic mode */
771 if (val == 2) {
772 if (check_trip_points(dev, nr) < 0)
773 return -EINVAL;
774 }
775
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100776 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 if (val == 0) {
779 int tmp;
780 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200781 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
782 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* set on/off mode */
784 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100785 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
786 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100787 } else {
788 if (val == 1) /* Manual mode */
789 data->pwm_ctrl[nr] = data->pwm_duty[nr];
790 else /* Automatic mode */
791 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
792 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* set SmartGuardian mode */
794 data->fan_main_ctrl |= (1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100795 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
796 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100799 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return count;
801}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200802static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
803 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200805 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
806 int nr = sensor_attr->index;
807
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200808 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100809 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Jean Delvaref5f64502010-03-05 22:17:19 +0100811 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return -EINVAL;
813
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100814 mutex_lock(&data->update_lock);
Jean Delvareb99883d2010-03-05 22:17:15 +0100815 data->pwm_duty[nr] = PWM_TO_REG(val);
816 /* If we are in manual mode, write the duty cycle immediately;
817 * otherwise, just store it for later use. */
818 if (!(data->pwm_ctrl[nr] & 0x80)) {
819 data->pwm_ctrl[nr] = data->pwm_duty[nr];
820 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
821 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100822 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return count;
824}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100825static ssize_t set_pwm_freq(struct device *dev,
826 struct device_attribute *attr, const char *buf, size_t count)
827{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200828 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100829 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100830 int i;
831
Jean Delvaref5f64502010-03-05 22:17:19 +0100832 if (strict_strtoul(buf, 10, &val) < 0)
833 return -EINVAL;
834
Jean Delvaref8d0c192007-02-14 21:15:02 +0100835 /* Search for the nearest available frequency */
836 for (i = 0; i < 7; i++) {
837 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
838 break;
839 }
840
841 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200842 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100843 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200844 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100845 mutex_unlock(&data->update_lock);
846
847 return count;
848}
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100849static ssize_t show_pwm_temp_map(struct device *dev,
850 struct device_attribute *attr, char *buf)
851{
852 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
853 int nr = sensor_attr->index;
854
855 struct it87_data *data = it87_update_device(dev);
856 int map;
857
858 if (data->pwm_temp_map[nr] < 3)
859 map = 1 << data->pwm_temp_map[nr];
860 else
861 map = 0; /* Should never happen */
862 return sprintf(buf, "%d\n", map);
863}
864static ssize_t set_pwm_temp_map(struct device *dev,
865 struct device_attribute *attr, const char *buf, size_t count)
866{
867 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
868 int nr = sensor_attr->index;
869
870 struct it87_data *data = dev_get_drvdata(dev);
871 long val;
872 u8 reg;
873
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100874 /* This check can go away if we ever support automatic fan speed
875 control on newer chips. */
876 if (!has_old_autopwm(data)) {
877 dev_notice(dev, "Mapping change disabled for safety reasons\n");
878 return -EINVAL;
879 }
880
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100881 if (strict_strtol(buf, 10, &val) < 0)
882 return -EINVAL;
883
884 switch (val) {
885 case (1 << 0):
886 reg = 0x00;
887 break;
888 case (1 << 1):
889 reg = 0x01;
890 break;
891 case (1 << 2):
892 reg = 0x02;
893 break;
894 default:
895 return -EINVAL;
896 }
897
898 mutex_lock(&data->update_lock);
899 data->pwm_temp_map[nr] = reg;
900 /* If we are in automatic mode, write the temp mapping immediately;
901 * otherwise, just store it for later use. */
902 if (data->pwm_ctrl[nr] & 0x80) {
903 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
904 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
905 }
906 mutex_unlock(&data->update_lock);
907 return count;
908}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100910static ssize_t show_auto_pwm(struct device *dev,
911 struct device_attribute *attr, char *buf)
912{
913 struct it87_data *data = it87_update_device(dev);
914 struct sensor_device_attribute_2 *sensor_attr =
915 to_sensor_dev_attr_2(attr);
916 int nr = sensor_attr->nr;
917 int point = sensor_attr->index;
918
919 return sprintf(buf, "%d\n", PWM_FROM_REG(data->auto_pwm[nr][point]));
920}
921
922static ssize_t set_auto_pwm(struct device *dev,
923 struct device_attribute *attr, const char *buf, size_t count)
924{
925 struct it87_data *data = dev_get_drvdata(dev);
926 struct sensor_device_attribute_2 *sensor_attr =
927 to_sensor_dev_attr_2(attr);
928 int nr = sensor_attr->nr;
929 int point = sensor_attr->index;
930 long val;
931
932 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
933 return -EINVAL;
934
935 mutex_lock(&data->update_lock);
936 data->auto_pwm[nr][point] = PWM_TO_REG(val);
937 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
938 data->auto_pwm[nr][point]);
939 mutex_unlock(&data->update_lock);
940 return count;
941}
942
943static ssize_t show_auto_temp(struct device *dev,
944 struct device_attribute *attr, char *buf)
945{
946 struct it87_data *data = it87_update_device(dev);
947 struct sensor_device_attribute_2 *sensor_attr =
948 to_sensor_dev_attr_2(attr);
949 int nr = sensor_attr->nr;
950 int point = sensor_attr->index;
951
952 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
953}
954
955static ssize_t set_auto_temp(struct device *dev,
956 struct device_attribute *attr, const char *buf, size_t count)
957{
958 struct it87_data *data = dev_get_drvdata(dev);
959 struct sensor_device_attribute_2 *sensor_attr =
960 to_sensor_dev_attr_2(attr);
961 int nr = sensor_attr->nr;
962 int point = sensor_attr->index;
963 long val;
964
965 if (strict_strtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
966 return -EINVAL;
967
968 mutex_lock(&data->update_lock);
969 data->auto_temp[nr][point] = TEMP_TO_REG(val);
970 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
971 data->auto_temp[nr][point]);
972 mutex_unlock(&data->update_lock);
973 return count;
974}
975
Jean Delvare20ad93d2005-06-05 11:53:25 +0200976#define show_fan_offset(offset) \
977static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
978 show_fan, NULL, offset - 1); \
979static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
980 show_fan_min, set_fan_min, offset - 1); \
981static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
982 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984show_fan_offset(1);
985show_fan_offset(2);
986show_fan_offset(3);
987
988#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200989static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
990 show_pwm_enable, set_pwm_enable, offset - 1); \
991static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +0100992 show_pwm, set_pwm, offset - 1); \
993static DEVICE_ATTR(pwm##offset##_freq, \
994 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100995 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
996static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100997 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
998 offset - 1); \
999static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1000 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1001 offset - 1, 0); \
1002static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1003 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1004 offset - 1, 1); \
1005static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1006 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1007 offset - 1, 2); \
1008static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1009 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1010static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1011 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1012 offset - 1, 1); \
1013static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1014 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1015 offset - 1, 0); \
1016static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1017 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1018 offset - 1, 2); \
1019static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1020 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1021 offset - 1, 3); \
1022static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1023 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1024 offset - 1, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026show_pwm_offset(1);
1027show_pwm_offset(2);
1028show_pwm_offset(3);
1029
Jean Delvare17d648b2006-08-28 14:23:46 +02001030/* A different set of callbacks for 16-bit fans */
1031static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1032 char *buf)
1033{
1034 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1035 int nr = sensor_attr->index;
1036 struct it87_data *data = it87_update_device(dev);
1037 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1038}
1039
1040static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1041 char *buf)
1042{
1043 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1044 int nr = sensor_attr->index;
1045 struct it87_data *data = it87_update_device(dev);
1046 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1047}
1048
1049static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1050 const char *buf, size_t count)
1051{
1052 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1053 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001054 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001055 long val;
1056
1057 if (strict_strtol(buf, 10, &val) < 0)
1058 return -EINVAL;
Jean Delvare17d648b2006-08-28 14:23:46 +02001059
1060 mutex_lock(&data->update_lock);
1061 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001062 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001063 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001064 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001065 data->fan_min[nr] >> 8);
1066 mutex_unlock(&data->update_lock);
1067 return count;
1068}
1069
1070/* We want to use the same sysfs file names as 8-bit fans, but we need
1071 different variable names, so we have to use SENSOR_ATTR instead of
1072 SENSOR_DEVICE_ATTR. */
1073#define show_fan16_offset(offset) \
1074static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1075 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1076 show_fan16, NULL, offset - 1); \
1077static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1078 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1079 show_fan16_min, set_fan16_min, offset - 1)
1080
1081show_fan16_offset(1);
1082show_fan16_offset(2);
1083show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001084show_fan16_offset(4);
1085show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +02001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001088static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1089 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001092 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
Jean Delvare1d66c642005-04-18 21:16:59 -07001094static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Jean Delvare0124dd72007-11-25 16:16:41 +01001096static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1097 char *buf)
1098{
1099 int bitnr = to_sensor_dev_attr(attr)->index;
1100 struct it87_data *data = it87_update_device(dev);
1101 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1102}
1103static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1104static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1105static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1106static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1107static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1108static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1109static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1110static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1111static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1112static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1113static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1114static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1115static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1116static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1117static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1118static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1119
Jean Delvared9b327c2010-03-05 22:17:17 +01001120static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1121 char *buf)
1122{
1123 int bitnr = to_sensor_dev_attr(attr)->index;
1124 struct it87_data *data = it87_update_device(dev);
1125 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1126}
1127static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1128 const char *buf, size_t count)
1129{
1130 int bitnr = to_sensor_dev_attr(attr)->index;
1131 struct it87_data *data = dev_get_drvdata(dev);
1132 long val;
1133
1134 if (strict_strtol(buf, 10, &val) < 0
1135 || (val != 0 && val != 1))
1136 return -EINVAL;
1137
1138 mutex_lock(&data->update_lock);
1139 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1140 if (val)
1141 data->beeps |= (1 << bitnr);
1142 else
1143 data->beeps &= ~(1 << bitnr);
1144 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1145 mutex_unlock(&data->update_lock);
1146 return count;
1147}
1148
1149static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1150 show_beep, set_beep, 1);
1151static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1152static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1153static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1154static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1155static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1156static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1157static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1158/* fanX_beep writability is set later */
1159static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1160static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1161static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1162static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1163static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1164static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1165 show_beep, set_beep, 2);
1166static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1167static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1168
Jean Delvare5f2dc792010-03-05 22:17:18 +01001169static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1170 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Jean Delvare90d66192007-10-08 18:24:35 +02001172 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001173 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001175static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1176 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001178 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001179 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Jean Delvaref5f64502010-03-05 22:17:19 +01001181 if (strict_strtoul(buf, 10, &val) < 0)
1182 return -EINVAL;
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 data->vrm = val;
1185
1186 return count;
1187}
1188static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Jean Delvare5f2dc792010-03-05 22:17:18 +01001190static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1191 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 struct it87_data *data = it87_update_device(dev);
1194 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1195}
1196static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001197
Jean Delvare738e5e02010-08-14 21:08:50 +02001198static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1199 char *buf)
1200{
1201 static const char *labels[] = {
1202 "+5V",
1203 "5VSB",
1204 "Vbat",
1205 };
1206 int nr = to_sensor_dev_attr(attr)->index;
1207
1208 return sprintf(buf, "%s\n", labels[nr]);
1209}
1210static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1211static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1212static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1213
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001214static ssize_t show_name(struct device *dev, struct device_attribute
1215 *devattr, char *buf)
1216{
1217 struct it87_data *data = dev_get_drvdata(dev);
1218 return sprintf(buf, "%s\n", data->name);
1219}
1220static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1221
Jean Delvare87808be2006-09-24 21:17:13 +02001222static struct attribute *it87_attributes[] = {
1223 &sensor_dev_attr_in0_input.dev_attr.attr,
1224 &sensor_dev_attr_in1_input.dev_attr.attr,
1225 &sensor_dev_attr_in2_input.dev_attr.attr,
1226 &sensor_dev_attr_in3_input.dev_attr.attr,
1227 &sensor_dev_attr_in4_input.dev_attr.attr,
1228 &sensor_dev_attr_in5_input.dev_attr.attr,
1229 &sensor_dev_attr_in6_input.dev_attr.attr,
1230 &sensor_dev_attr_in7_input.dev_attr.attr,
1231 &sensor_dev_attr_in8_input.dev_attr.attr,
1232 &sensor_dev_attr_in0_min.dev_attr.attr,
1233 &sensor_dev_attr_in1_min.dev_attr.attr,
1234 &sensor_dev_attr_in2_min.dev_attr.attr,
1235 &sensor_dev_attr_in3_min.dev_attr.attr,
1236 &sensor_dev_attr_in4_min.dev_attr.attr,
1237 &sensor_dev_attr_in5_min.dev_attr.attr,
1238 &sensor_dev_attr_in6_min.dev_attr.attr,
1239 &sensor_dev_attr_in7_min.dev_attr.attr,
1240 &sensor_dev_attr_in0_max.dev_attr.attr,
1241 &sensor_dev_attr_in1_max.dev_attr.attr,
1242 &sensor_dev_attr_in2_max.dev_attr.attr,
1243 &sensor_dev_attr_in3_max.dev_attr.attr,
1244 &sensor_dev_attr_in4_max.dev_attr.attr,
1245 &sensor_dev_attr_in5_max.dev_attr.attr,
1246 &sensor_dev_attr_in6_max.dev_attr.attr,
1247 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001248 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1249 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1250 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1251 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1252 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1253 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1254 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1255 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001256
1257 &sensor_dev_attr_temp1_input.dev_attr.attr,
1258 &sensor_dev_attr_temp2_input.dev_attr.attr,
1259 &sensor_dev_attr_temp3_input.dev_attr.attr,
1260 &sensor_dev_attr_temp1_max.dev_attr.attr,
1261 &sensor_dev_attr_temp2_max.dev_attr.attr,
1262 &sensor_dev_attr_temp3_max.dev_attr.attr,
1263 &sensor_dev_attr_temp1_min.dev_attr.attr,
1264 &sensor_dev_attr_temp2_min.dev_attr.attr,
1265 &sensor_dev_attr_temp3_min.dev_attr.attr,
1266 &sensor_dev_attr_temp1_type.dev_attr.attr,
1267 &sensor_dev_attr_temp2_type.dev_attr.attr,
1268 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001269 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1270 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1271 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001272
1273 &dev_attr_alarms.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001274 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001275 NULL
1276};
1277
1278static const struct attribute_group it87_group = {
1279 .attrs = it87_attributes,
1280};
1281
Jean Delvared9b327c2010-03-05 22:17:17 +01001282static struct attribute *it87_attributes_beep[] = {
1283 &sensor_dev_attr_in0_beep.dev_attr.attr,
1284 &sensor_dev_attr_in1_beep.dev_attr.attr,
1285 &sensor_dev_attr_in2_beep.dev_attr.attr,
1286 &sensor_dev_attr_in3_beep.dev_attr.attr,
1287 &sensor_dev_attr_in4_beep.dev_attr.attr,
1288 &sensor_dev_attr_in5_beep.dev_attr.attr,
1289 &sensor_dev_attr_in6_beep.dev_attr.attr,
1290 &sensor_dev_attr_in7_beep.dev_attr.attr,
1291
1292 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1293 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1294 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1295 NULL
1296};
1297
1298static const struct attribute_group it87_group_beep = {
1299 .attrs = it87_attributes_beep,
1300};
1301
Jean Delvare723a0aa2010-03-05 22:17:16 +01001302static struct attribute *it87_attributes_fan16[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001303 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1304 &sensor_dev_attr_fan1_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001305 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1306 NULL
1307}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001308 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1309 &sensor_dev_attr_fan2_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001310 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1311 NULL
1312}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001313 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1314 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001315 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1316 NULL
1317}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001318 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1319 &sensor_dev_attr_fan4_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001320 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1321 NULL
1322}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001323 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1324 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001325 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1326 NULL
1327} };
Jean Delvare87808be2006-09-24 21:17:13 +02001328
Jean Delvare723a0aa2010-03-05 22:17:16 +01001329static const struct attribute_group it87_group_fan16[5] = {
1330 { .attrs = it87_attributes_fan16[0] },
1331 { .attrs = it87_attributes_fan16[1] },
1332 { .attrs = it87_attributes_fan16[2] },
1333 { .attrs = it87_attributes_fan16[3] },
1334 { .attrs = it87_attributes_fan16[4] },
1335};
1336
1337static struct attribute *it87_attributes_fan[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001338 &sensor_dev_attr_fan1_input.dev_attr.attr,
1339 &sensor_dev_attr_fan1_min.dev_attr.attr,
1340 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001341 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1342 NULL
1343}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001344 &sensor_dev_attr_fan2_input.dev_attr.attr,
1345 &sensor_dev_attr_fan2_min.dev_attr.attr,
1346 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001347 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1348 NULL
1349}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001350 &sensor_dev_attr_fan3_input.dev_attr.attr,
1351 &sensor_dev_attr_fan3_min.dev_attr.attr,
1352 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001353 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001354 NULL
1355} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001356
Jean Delvare723a0aa2010-03-05 22:17:16 +01001357static const struct attribute_group it87_group_fan[3] = {
1358 { .attrs = it87_attributes_fan[0] },
1359 { .attrs = it87_attributes_fan[1] },
1360 { .attrs = it87_attributes_fan[2] },
1361};
1362
1363static const struct attribute_group *
1364it87_get_fan_group(const struct it87_data *data)
1365{
1366 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1367}
1368
1369static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001370 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001371 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001372 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001373 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001374 NULL
1375}, {
1376 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1377 &sensor_dev_attr_pwm2.dev_attr.attr,
1378 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001379 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001380 NULL
1381}, {
1382 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1383 &sensor_dev_attr_pwm3.dev_attr.attr,
1384 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001385 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001386 NULL
1387} };
Jean Delvare87808be2006-09-24 21:17:13 +02001388
Jean Delvare723a0aa2010-03-05 22:17:16 +01001389static const struct attribute_group it87_group_pwm[3] = {
1390 { .attrs = it87_attributes_pwm[0] },
1391 { .attrs = it87_attributes_pwm[1] },
1392 { .attrs = it87_attributes_pwm[2] },
1393};
1394
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001395static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1396 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1397 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1398 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1399 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1400 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1401 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1402 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1403 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1404 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1405 NULL
1406}, {
1407 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1408 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1409 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1410 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1411 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1412 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1413 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1414 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1415 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1416 NULL
1417}, {
1418 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1419 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1420 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1421 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1422 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1423 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1424 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1425 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1426 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1427 NULL
1428} };
1429
1430static const struct attribute_group it87_group_autopwm[3] = {
1431 { .attrs = it87_attributes_autopwm[0] },
1432 { .attrs = it87_attributes_autopwm[1] },
1433 { .attrs = it87_attributes_autopwm[2] },
1434};
1435
Jean Delvared9b327c2010-03-05 22:17:17 +01001436static struct attribute *it87_attributes_fan_beep[] = {
1437 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1438 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1439 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1440 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1441 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1442};
1443
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001444static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001445 &dev_attr_vrm.attr,
1446 &dev_attr_cpu0_vid.attr,
1447 NULL
1448};
1449
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001450static const struct attribute_group it87_group_vid = {
1451 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001452};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jean Delvare738e5e02010-08-14 21:08:50 +02001454static struct attribute *it87_attributes_label[] = {
1455 &sensor_dev_attr_in3_label.dev_attr.attr,
1456 &sensor_dev_attr_in7_label.dev_attr.attr,
1457 &sensor_dev_attr_in8_label.dev_attr.attr,
1458 NULL
1459};
1460
1461static const struct attribute_group it87_group_label = {
1462 .attrs = it87_attributes_vid,
1463};
1464
Jean Delvare2d8672c2005-07-19 23:56:35 +02001465/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001466static int __init it87_find(unsigned short *address,
1467 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 int err = -ENODEV;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001470 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001471 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 superio_enter();
Jean Delvare67b671b2007-12-06 23:13:42 +01001474 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001475
1476 switch (chip_type) {
1477 case IT8705F_DEVID:
1478 sio_data->type = it87;
1479 break;
1480 case IT8712F_DEVID:
1481 sio_data->type = it8712;
1482 break;
1483 case IT8716F_DEVID:
1484 case IT8726F_DEVID:
1485 sio_data->type = it8716;
1486 break;
1487 case IT8718F_DEVID:
1488 sio_data->type = it8718;
1489 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001490 case IT8720F_DEVID:
1491 sio_data->type = it8720;
1492 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001493 case 0xffff: /* No device at all */
1494 goto exit;
1495 default:
1496 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
1497 chip_type);
1498 goto exit;
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Jean Delvare87673dd2006-08-28 14:37:19 +02001501 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1503 pr_info("it87: Device not activated, skipping\n");
1504 goto exit;
1505 }
1506
1507 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1508 if (*address == 0) {
1509 pr_info("it87: Base address not set, skipping\n");
1510 goto exit;
1511 }
1512
1513 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001514 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001516 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Jean Delvare738e5e02010-08-14 21:08:50 +02001518 /* in8 (Vbat) is always internal */
1519 sio_data->internal = (1 << 2);
1520
Jean Delvare87673dd2006-08-28 14:37:19 +02001521 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001522 if (sio_data->type == it87) {
1523 /* The IT8705F doesn't have VID pins at all */
1524 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001525
1526 /* The IT8705F has a different LD number for GPIO */
1527 superio_select(5);
1528 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01001529 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001530 int reg;
1531
1532 superio_select(GPIO);
Jean Delvare895ff262009-12-09 20:35:47 +01001533 /* We need at least 4 VID pins */
1534 reg = superio_inb(IT87_SIO_GPIO3_REG);
1535 if (reg & 0x0f) {
1536 pr_info("it87: VID is disabled (pins used for GPIO)\n");
1537 sio_data->skip_vid = 1;
1538 }
1539
Jean Delvare591ec652009-12-09 20:35:48 +01001540 /* Check if fan3 is there or not */
1541 if (reg & (1 << 6))
1542 sio_data->skip_pwm |= (1 << 2);
1543 if (reg & (1 << 7))
1544 sio_data->skip_fan |= (1 << 2);
1545
1546 /* Check if fan2 is there or not */
1547 reg = superio_inb(IT87_SIO_GPIO5_REG);
1548 if (reg & (1 << 1))
1549 sio_data->skip_pwm |= (1 << 1);
1550 if (reg & (1 << 2))
1551 sio_data->skip_fan |= (1 << 1);
1552
Jean Delvare895ff262009-12-09 20:35:47 +01001553 if ((sio_data->type == it8718 || sio_data->type == it8720)
1554 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001555 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001556
1557 reg = superio_inb(IT87_SIO_PINX2_REG);
Jean Delvare436cad22010-07-09 16:22:48 +02001558 /*
1559 * The IT8720F has no VIN7 pin, so VCCH should always be
1560 * routed internally to VIN7 with an internal divider.
1561 * Curiously, there still is a configuration bit to control
1562 * this, which means it can be set incorrectly. And even
1563 * more curiously, many boards out there are improperly
1564 * configured, even though the IT8720F datasheet claims
1565 * that the internal routing of VCCH to VIN7 is the default
1566 * setting. So we force the internal routing in this case.
1567 */
1568 if (sio_data->type == it8720 && !(reg & (1 << 1))) {
1569 reg |= (1 << 1);
1570 superio_outb(IT87_SIO_PINX2_REG, reg);
1571 pr_notice("it87: Routing internal VCCH to in7\n");
1572 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001573 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001574 sio_data->internal |= (1 << 0);
Jean Delvare87673dd2006-08-28 14:37:19 +02001575 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02001576 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01001577
1578 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02001579 }
Jean Delvared9b327c2010-03-05 22:17:17 +01001580 if (sio_data->beep_pin)
1581 pr_info("it87: Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02001582
Jean Delvare98dd22c2008-10-09 15:33:58 +02001583 /* Disable specific features based on DMI strings */
1584 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1585 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1586 if (board_vendor && board_name) {
1587 if (strcmp(board_vendor, "nVIDIA") == 0
1588 && strcmp(board_name, "FN68PT") == 0) {
1589 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1590 connected to a fan, but to something else. One user
1591 has reported instant system power-off when changing
1592 the PWM2 duty cycle, so we disable it.
1593 I use the board name string as the trigger in case
1594 the same board is ever used in other systems. */
1595 pr_info("it87: Disabling pwm2 due to "
1596 "hardware constraints\n");
1597 sio_data->skip_pwm = (1 << 1);
1598 }
1599 }
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601exit:
1602 superio_exit();
1603 return err;
1604}
1605
Jean Delvare723a0aa2010-03-05 22:17:16 +01001606static void it87_remove_files(struct device *dev)
1607{
1608 struct it87_data *data = platform_get_drvdata(pdev);
1609 struct it87_sio_data *sio_data = dev->platform_data;
1610 const struct attribute_group *fan_group = it87_get_fan_group(data);
1611 int i;
1612
1613 sysfs_remove_group(&dev->kobj, &it87_group);
Jean Delvared9b327c2010-03-05 22:17:17 +01001614 if (sio_data->beep_pin)
1615 sysfs_remove_group(&dev->kobj, &it87_group_beep);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001616 for (i = 0; i < 5; i++) {
1617 if (!(data->has_fan & (1 << i)))
1618 continue;
1619 sysfs_remove_group(&dev->kobj, &fan_group[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01001620 if (sio_data->beep_pin)
1621 sysfs_remove_file(&dev->kobj,
1622 it87_attributes_fan_beep[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001623 }
1624 for (i = 0; i < 3; i++) {
1625 if (sio_data->skip_pwm & (1 << 0))
1626 continue;
1627 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001628 if (has_old_autopwm(data))
1629 sysfs_remove_group(&dev->kobj,
1630 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001631 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001632 if (!sio_data->skip_vid)
1633 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02001634 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001635}
1636
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001637static int __devinit it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001640 struct resource *res;
1641 struct device *dev = &pdev->dev;
1642 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001643 const struct attribute_group *fan_group;
1644 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01001646 int fan_beep_need_rw;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001647 static const char *names[] = {
1648 "it87",
1649 "it8712",
1650 "it8716",
1651 "it8718",
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001652 "it8720",
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001653 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001655 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001656 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001657 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1658 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001659 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001660 err = -EBUSY;
1661 goto ERROR0;
1662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Jean Delvare5f2dc792010-03-05 22:17:18 +01001664 data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
1665 if (!data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 err = -ENOMEM;
1667 goto ERROR1;
1668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001670 data->addr = res->start;
1671 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001672 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001673 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001676 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1677 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001678 err = -ENODEV;
1679 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001682 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001684 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001687 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001690 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001693 err = sysfs_create_group(&dev->kobj, &it87_group);
1694 if (err)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001695 goto ERROR2;
Jean Delvare17d648b2006-08-28 14:23:46 +02001696
Jean Delvared9b327c2010-03-05 22:17:17 +01001697 if (sio_data->beep_pin) {
1698 err = sysfs_create_group(&dev->kobj, &it87_group_beep);
1699 if (err)
1700 goto ERROR4;
1701 }
1702
Jean Delvare9060f8b2006-08-28 14:24:17 +02001703 /* Do not create fan files for disabled fans */
Jean Delvare723a0aa2010-03-05 22:17:16 +01001704 fan_group = it87_get_fan_group(data);
Jean Delvared9b327c2010-03-05 22:17:17 +01001705 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001706 for (i = 0; i < 5; i++) {
1707 if (!(data->has_fan & (1 << i)))
1708 continue;
1709 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
1710 if (err)
1711 goto ERROR4;
Jean Delvared9b327c2010-03-05 22:17:17 +01001712
1713 if (sio_data->beep_pin) {
1714 err = sysfs_create_file(&dev->kobj,
1715 it87_attributes_fan_beep[i]);
1716 if (err)
1717 goto ERROR4;
1718 if (!fan_beep_need_rw)
1719 continue;
1720
1721 /* As we have a single beep enable bit for all fans,
1722 * only the first enabled fan has a writable attribute
1723 * for it. */
1724 if (sysfs_chmod_file(&dev->kobj,
1725 it87_attributes_fan_beep[i],
1726 S_IRUGO | S_IWUSR))
1727 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
1728 i + 1);
1729 fan_beep_need_rw = 0;
1730 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001731 }
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001734 for (i = 0; i < 3; i++) {
1735 if (sio_data->skip_pwm & (1 << i))
1736 continue;
1737 err = sysfs_create_group(&dev->kobj,
1738 &it87_group_pwm[i]);
1739 if (err)
Jean Delvare98dd22c2008-10-09 15:33:58 +02001740 goto ERROR4;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001741
1742 if (!has_old_autopwm(data))
1743 continue;
1744 err = sysfs_create_group(&dev->kobj,
1745 &it87_group_autopwm[i]);
1746 if (err)
1747 goto ERROR4;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750
Jean Delvare895ff262009-12-09 20:35:47 +01001751 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02001752 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001753 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001754 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001755 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
1756 if (err)
Jean Delvare87808be2006-09-24 21:17:13 +02001757 goto ERROR4;
1758 }
1759
Jean Delvare738e5e02010-08-14 21:08:50 +02001760 /* Export labels for internal sensors */
1761 for (i = 0; i < 3; i++) {
1762 if (!(sio_data->internal & (1 << i)))
1763 continue;
1764 err = sysfs_create_file(&dev->kobj,
1765 it87_attributes_label[i]);
1766 if (err)
1767 goto ERROR4;
1768 }
1769
Tony Jones1beeffe2007-08-20 13:46:20 -07001770 data->hwmon_dev = hwmon_device_register(dev);
1771 if (IS_ERR(data->hwmon_dev)) {
1772 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001773 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775
1776 return 0;
1777
Jean Delvare87808be2006-09-24 21:17:13 +02001778ERROR4:
Jean Delvare723a0aa2010-03-05 22:17:16 +01001779 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780ERROR2:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001781 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 kfree(data);
1783ERROR1:
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001784 release_region(res->start, IT87_EC_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785ERROR0:
1786 return err;
1787}
1788
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001789static int __devexit it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001791 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Tony Jones1beeffe2007-08-20 13:46:20 -07001793 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001794 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001795
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001796 release_region(data->addr, IT87_EC_EXTENT);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001797 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001798 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 return 0;
1801}
1802
Jean Delvare7f999aa2007-02-14 21:15:03 +01001803/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1805 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001806static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001808 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1809 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
Jean Delvare7f999aa2007-02-14 21:15:03 +01001812/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1814 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001815static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001817 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1818 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
1821/* Return 1 if and only if the PWM interface is safe to use */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001822static int __devinit it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001824 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1826 * and polarity set to active low is sign that this is the case so we
1827 * disable pwm control to protect the user. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001828 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if ((tmp & 0x87) == 0) {
1830 if (fix_pwm_polarity) {
1831 /* The user asks us to attempt a chip reconfiguration.
1832 * This means switching to active high polarity and
1833 * inverting all fan speed values. */
1834 int i;
1835 u8 pwm[3];
1836
1837 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001838 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 IT87_REG_PWM(i));
1840
1841 /* If any fan is in automatic pwm mode, the polarity
1842 * might be correct, as suspicious as it seems, so we
1843 * better don't change anything (but still disable the
1844 * PWM interface). */
1845 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001846 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001848 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 tmp | 0x87);
1850 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001851 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 IT87_REG_PWM(i),
1853 0x7f & ~pwm[i]);
1854 return 1;
1855 }
1856
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001857 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 "too broken to be fixed\n");
1859 }
1860
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001861 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 "defaults, disabling PWM interface\n");
1863 return 0;
1864 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001865 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 "sane, won't touch\n");
1867 }
1868
1869 return 1;
1870}
1871
1872/* Called when we have found a new IT87. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001873static void __devinit it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Jean Delvare591ec652009-12-09 20:35:48 +01001875 struct it87_sio_data *sio_data = pdev->dev.platform_data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001876 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01001878 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Jean Delvareb99883d2010-03-05 22:17:15 +01001880 /* For each PWM channel:
1881 * - If it is in automatic mode, setting to manual mode should set
1882 * the fan to full speed by default.
1883 * - If it is in manual mode, we need a mapping to temperature
1884 * channels to use when later setting to automatic mode later.
1885 * Use a 1:1 mapping by default (we are clueless.)
1886 * In both cases, the value can (and should) be changed by the user
1887 * prior to switching to a different mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01001889 data->pwm_temp_map[i] = i;
1890 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001891 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
1893
Jean Delvarec5df9b72006-08-28 14:37:54 +02001894 /* Some chips seem to have default value 0xff for all limit
1895 * registers. For low voltage limits it makes no sense and triggers
1896 * alarms, so change to 0 instead. For high temperature limits, it
1897 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1898 * but is still confusing, so change to 127 degrees C. */
1899 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001900 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001901 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001902 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001903 }
1904 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001905 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001906 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001907 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001908 }
1909
Jean Delvarea00afb92010-04-14 16:14:09 +02001910 /* Temperature channels are not forcibly enabled, as they can be
1911 * set to two different sensor types and we can't guess which one
1912 * is correct for a given system. These channels can be enabled at
1913 * run-time through the temp{1-3}_type sysfs accessors if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001916 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if ((tmp & 0xff) == 0) {
1918 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001919 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921
1922 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01001923 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001924 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01001925 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01001927 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01001928 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1929 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001931 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Jean Delvare17d648b2006-08-28 14:23:46 +02001933 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02001934 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001935 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001936 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001937 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02001938 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001939 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02001940 tmp | 0x07);
1941 }
Andrew Paprocki816d8c62008-08-06 22:41:06 +02001942 /* IT8705F only supports three fans. */
1943 if (data->type != it87) {
1944 if (tmp & (1 << 4))
1945 data->has_fan |= (1 << 3); /* fan4 enabled */
1946 if (tmp & (1 << 5))
1947 data->has_fan |= (1 << 4); /* fan5 enabled */
1948 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001949 }
1950
Jean Delvare591ec652009-12-09 20:35:48 +01001951 /* Fan input pins may be used for alternative functions */
1952 data->has_fan &= ~sio_data->skip_fan;
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001955 it87_write_value(data, IT87_REG_CONFIG,
1956 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 | (update_vbat ? 0x41 : 0x01));
1958}
1959
Jean Delvareb99883d2010-03-05 22:17:15 +01001960static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
1961{
1962 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
1963 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
1964 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
1965 else /* Manual mode */
1966 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001967
1968 if (has_old_autopwm(data)) {
1969 int i;
1970
1971 for (i = 0; i < 5 ; i++)
1972 data->auto_temp[nr][i] = it87_read_value(data,
1973 IT87_REG_AUTO_TEMP(nr, i));
1974 for (i = 0; i < 3 ; i++)
1975 data->auto_pwm[nr][i] = it87_read_value(data,
1976 IT87_REG_AUTO_PWM(nr, i));
1977 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001978}
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980static struct it87_data *it87_update_device(struct device *dev)
1981{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001982 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 int i;
1984
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001985 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1988 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (update_vbat) {
1990 /* Cleared after each update, so reenable. Value
Jean Delvare5f2dc792010-03-05 22:17:18 +01001991 returned by this read will be previous value */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001992 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01001993 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995 for (i = 0; i <= 7; i++) {
1996 data->in[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01001997 it87_read_value(data, IT87_REG_VIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 data->in_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01001999 it87_read_value(data, IT87_REG_VIN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 data->in_max[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002001 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
Jean Delvare3543a532006-08-28 14:27:25 +02002003 /* in8 (battery) has no limit registers */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002004 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Jean Delvarec7f1f712007-09-03 17:11:46 +02002006 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002007 /* Skip disabled fans */
2008 if (!(data->has_fan & (1 << i)))
2009 continue;
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 data->fan_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002012 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002013 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002014 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002015 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002016 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002017 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002018 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002019 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002020 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023 for (i = 0; i < 3; i++) {
2024 data->temp[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002025 it87_read_value(data, IT87_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 data->temp_high[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002027 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 data->temp_low[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002029 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
2031
Jean Delvare17d648b2006-08-28 14:23:46 +02002032 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002033 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002034 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002035 data->fan_div[0] = i & 0x07;
2036 data->fan_div[1] = (i >> 3) & 0x07;
2037 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002041 it87_read_value(data, IT87_REG_ALARM1) |
2042 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2043 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002044 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002045
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002046 data->fan_main_ctrl = it87_read_value(data,
2047 IT87_REG_FAN_MAIN_CTRL);
2048 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002049 for (i = 0; i < 3; i++)
2050 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002052 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Andrew Paprocki04751692008-08-06 22:41:06 +02002053 /* The 8705 does not have VID capability.
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002054 The 8718 and the 8720 don't use IT87_REG_VID for the
2055 same purpose. */
Jean Delvare17d648b2006-08-28 14:23:46 +02002056 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002057 data->vid = it87_read_value(data, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02002058 /* The older IT8712F revisions had only 5 VID pins,
2059 but we assume it is always safe to read 6 bits. */
2060 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
2062 data->last_updated = jiffies;
2063 data->valid = 1;
2064 }
2065
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002066 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 return data;
2069}
2070
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002071static int __init it87_device_add(unsigned short address,
2072 const struct it87_sio_data *sio_data)
2073{
2074 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002075 .start = address + IT87_EC_OFFSET,
2076 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002077 .name = DRVNAME,
2078 .flags = IORESOURCE_IO,
2079 };
2080 int err;
2081
Jean Delvareb9acb642009-01-07 16:37:35 +01002082 err = acpi_check_resource_conflict(&res);
2083 if (err)
2084 goto exit;
2085
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002086 pdev = platform_device_alloc(DRVNAME, address);
2087 if (!pdev) {
2088 err = -ENOMEM;
2089 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
2090 goto exit;
2091 }
2092
2093 err = platform_device_add_resources(pdev, &res, 1);
2094 if (err) {
2095 printk(KERN_ERR DRVNAME ": Device resource addition failed "
2096 "(%d)\n", err);
2097 goto exit_device_put;
2098 }
2099
2100 err = platform_device_add_data(pdev, sio_data,
2101 sizeof(struct it87_sio_data));
2102 if (err) {
2103 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
2104 goto exit_device_put;
2105 }
2106
2107 err = platform_device_add(pdev);
2108 if (err) {
2109 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
2110 err);
2111 goto exit_device_put;
2112 }
2113
2114 return 0;
2115
2116exit_device_put:
2117 platform_device_put(pdev);
2118exit:
2119 return err;
2120}
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122static int __init sm_it87_init(void)
2123{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002124 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002125 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002126 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002127
Jean Delvare98dd22c2008-10-09 15:33:58 +02002128 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002129 err = it87_find(&isa_address, &sio_data);
2130 if (err)
2131 return err;
2132 err = platform_driver_register(&it87_driver);
2133 if (err)
2134 return err;
2135
2136 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002137 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002138 platform_driver_unregister(&it87_driver);
2139 return err;
2140 }
2141
2142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145static void __exit sm_it87_exit(void)
2146{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002147 platform_device_unregister(pdev);
2148 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
2151
Jean Delvaref1d8e332007-11-25 16:14:44 +01002152MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02002153 "Jean Delvare <khali@linux-fr.org>");
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002154MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8720F/8726F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155module_param(update_vbat, bool, 0);
2156MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2157module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002158MODULE_PARM_DESC(fix_pwm_polarity,
2159 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160MODULE_LICENSE("GPL");
2161
2162module_init(sm_it87_init);
2163module_exit(sm_it87_exit);