blob: 5be09c048c5f35bc8b19914b58a20e3b4c3e32d5 [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
83static int superio_inw(int reg)
84{
85 int val;
86 outb(reg++, REG);
87 val = inb(VAL) << 8;
88 outb(reg, REG);
89 val |= inb(VAL);
90 return val;
91}
92
93static inline void
Jean Delvare87673dd2006-08-28 14:37:19 +020094superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +020097 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100static inline void
101superio_enter(void)
102{
103 outb(0x87, REG);
104 outb(0x01, REG);
105 outb(0x55, REG);
106 outb(0x55, REG);
107}
108
109static inline void
110superio_exit(void)
111{
112 outb(0x02, REG);
113 outb(0x02, VAL);
114}
115
Jean Delvare87673dd2006-08-28 14:37:19 +0200116/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define IT8712F_DEVID 0x8712
118#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200119#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200120#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100121#define IT8720F_DEVID 0x8720
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400122#define IT8726F_DEVID 0x8726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define IT87_ACT_REG 0x30
124#define IT87_BASE_REG 0x60
125
Jean Delvare87673dd2006-08-28 14:37:19 +0200126/* Logical device 7 registers (IT8712F and later) */
Jean Delvare895ff262009-12-09 20:35:47 +0100127#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100128#define IT87_SIO_GPIO5_REG 0x29
Jean Delvare87673dd2006-08-28 14:37:19 +0200129#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
130#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100131#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/* Update battery voltage after every reading if true */
134static int update_vbat;
135
136/* Not all BIOSes properly configure the PWM registers */
137static int fix_pwm_polarity;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/* Many IT87 constants specified below */
140
141/* Length of ISA address segment */
142#define IT87_EXTENT 8
143
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500144/* Length of ISA address segment for Environmental Controller */
145#define IT87_EC_EXTENT 2
146
147/* Offset of EC registers from ISA base address */
148#define IT87_EC_OFFSET 5
149
150/* Where are the ISA address/data registers relative to the EC base address */
151#define IT87_ADDR_REG_OFFSET 0
152#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154/*----- The IT87 registers -----*/
155
156#define IT87_REG_CONFIG 0x00
157
158#define IT87_REG_ALARM1 0x01
159#define IT87_REG_ALARM2 0x02
160#define IT87_REG_ALARM3 0x03
161
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100162/* The IT8718F and IT8720F have the VID value in a different register, in
163 Super-I/O configuration space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define IT87_REG_VID 0x0a
Andrew Paprocki04751692008-08-06 22:41:06 +0200165/* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
166 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
167 mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200169#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
172
Jean Delvarec7f1f712007-09-03 17:11:46 +0200173static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
174static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
175static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
176static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#define IT87_REG_FAN_MAIN_CTRL 0x13
178#define IT87_REG_FAN_CTL 0x14
179#define IT87_REG_PWM(nr) (0x15 + (nr))
180
181#define IT87_REG_VIN(nr) (0x20 + (nr))
182#define IT87_REG_TEMP(nr) (0x29 + (nr))
183
184#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
185#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
186#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
187#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#define IT87_REG_VIN_ENABLE 0x50
190#define IT87_REG_TEMP_ENABLE 0x51
Jean Delvared9b327c2010-03-05 22:17:17 +0100191#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193#define IT87_REG_CHIPID 0x58
194
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100195#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
196#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
199#define IN_FROM_REG(val) ((val) * 16)
200
201static inline u8 FAN_TO_REG(long rpm, int div)
202{
203 if (rpm == 0)
204 return 255;
205 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
206 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
207 254);
208}
209
Jean Delvare17d648b2006-08-28 14:23:46 +0200210static inline u16 FAN16_TO_REG(long rpm)
211{
212 if (rpm == 0)
213 return 0xffff;
214 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
Jean Delvare17d648b2006-08-28 14:23:46 +0200218/* The divider is fixed to 2 in 16-bit mode */
219#define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
222 ((val)+500)/1000),-128,127))
Jean Delvaree267d252009-03-12 13:36:39 +0100223#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#define PWM_TO_REG(val) ((val) >> 1)
226#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
227
228static int DIV_TO_REG(int val)
229{
230 int answer = 0;
Jean Delvareb9e349f2006-08-28 14:26:22 +0200231 while (answer < 7 && (val >>= 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 answer++;
233 return answer;
234}
235#define DIV_FROM_REG(val) (1 << (val))
236
Jean Delvaref8d0c192007-02-14 21:15:02 +0100237static const unsigned int pwm_freq[8] = {
238 48000000 / 128,
239 24000000 / 128,
240 12000000 / 128,
241 8000000 / 128,
242 6000000 / 128,
243 3000000 / 128,
244 1500000 / 128,
245 750000 / 128,
246};
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200249struct it87_sio_data {
250 enum chips type;
251 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200252 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200253 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100254 u8 beep_pin;
Jean Delvare591ec652009-12-09 20:35:48 +0100255 /* Features skipped based on config or DMI */
Jean Delvare895ff262009-12-09 20:35:47 +0100256 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100257 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200258 u8 skip_pwm;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200259};
260
Jean Delvareed6bafb2007-02-14 21:15:03 +0100261/* For each registered chip, we need to keep some data in memory.
262 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700264 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 enum chips type;
Andrew Paprocki04751692008-08-06 22:41:06 +0200266 u8 revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200268 unsigned short addr;
269 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100270 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 char valid; /* !=0 if following fields are valid */
272 unsigned long last_updated; /* In jiffies */
273
274 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200275 u8 in_max[8]; /* Register value */
276 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200277 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvarec7f1f712007-09-03 17:11:46 +0200278 u16 fan[5]; /* Register values, possibly combined */
279 u16 fan_min[5]; /* Register values, possibly combined */
Jean Delvaree267d252009-03-12 13:36:39 +0100280 s8 temp[3]; /* Register value */
281 s8 temp_high[3]; /* Register value */
282 s8 temp_low[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 u8 sensor; /* Register value */
284 u8 fan_div[3]; /* Register encoding, shifted right */
285 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100286 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100288 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100290 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100291
292 /* The following 3 arrays correspond to the same registers. The
293 * meaning of bits 6-0 depends on the value of bit 7, and we want
294 * to preserve settings on mode changes, so we have to track all
295 * values separately. */
296 u8 pwm_ctrl[3]; /* Register value */
297 u8 pwm_duty[3]; /* Manual PWM value set by user (bit 6-0) */
298 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100299
300 /* Automatic fan speed control registers */
301 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
302 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303};
304
Andrew Paprocki04751692008-08-06 22:41:06 +0200305static inline int has_16bit_fans(const struct it87_data *data)
306{
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200307 /* IT8705F Datasheet 0.4.1, 3h == Version G.
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200308 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200309 These are the first revisions with 16bit tachometer support. */
310 return (data->type == it87 && data->revision >= 0x03)
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200311 || (data->type == it8712 && data->revision >= 0x08)
Andrew Paprocki04751692008-08-06 22:41:06 +0200312 || data->type == it8716
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100313 || data->type == it8718
314 || data->type == it8720;
Andrew Paprocki04751692008-08-06 22:41:06 +0200315}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100317static inline int has_old_autopwm(const struct it87_data *data)
318{
319 /* The old automatic fan speed control interface is implemented
320 by IT8705F chips up to revision F and IT8712F chips up to
321 revision G. */
322 return (data->type == it87 && data->revision < 0x03)
323 || (data->type == it8712 && data->revision < 0x08);
324}
325
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200326static int it87_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200327static int __devexit it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200329static int it87_read_value(struct it87_data *data, u8 reg);
330static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200332static int it87_check_pwm(struct device *dev);
333static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200336static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100337 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200338 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200339 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100340 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200341 .probe = it87_probe,
342 .remove = __devexit_p(it87_remove),
Jean Delvarefde09502005-07-19 23:51:07 +0200343};
344
Jean Delvare20ad93d2005-06-05 11:53:25 +0200345static ssize_t show_in(struct device *dev, struct device_attribute *attr,
346 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200348 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
349 int nr = sensor_attr->index;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct it87_data *data = it87_update_device(dev);
352 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
353}
354
Jean Delvare20ad93d2005-06-05 11:53:25 +0200355static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
356 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200358 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
359 int nr = sensor_attr->index;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct it87_data *data = it87_update_device(dev);
362 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
363}
364
Jean Delvare20ad93d2005-06-05 11:53:25 +0200365static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
366 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200368 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
369 int nr = sensor_attr->index;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct it87_data *data = it87_update_device(dev);
372 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
373}
374
Jean Delvare20ad93d2005-06-05 11:53:25 +0200375static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
376 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200378 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
379 int nr = sensor_attr->index;
380
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200381 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100382 unsigned long val;
383
384 if (strict_strtoul(buf, 10, &val) < 0)
385 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100387 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 data->in_min[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200389 it87_write_value(data, IT87_REG_VIN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100391 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return count;
393}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200394static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
395 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200397 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
398 int nr = sensor_attr->index;
399
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200400 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100401 unsigned long val;
402
403 if (strict_strtoul(buf, 10, &val) < 0)
404 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100406 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 data->in_max[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200408 it87_write_value(data, IT87_REG_VIN_MAX(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100410 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return count;
412}
413
414#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200415static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
416 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200419static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
420 show_in_min, set_in_min, offset); \
421static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
422 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424show_in_offset(0);
425limit_in_offset(0);
426show_in_offset(1);
427limit_in_offset(1);
428show_in_offset(2);
429limit_in_offset(2);
430show_in_offset(3);
431limit_in_offset(3);
432show_in_offset(4);
433limit_in_offset(4);
434show_in_offset(5);
435limit_in_offset(5);
436show_in_offset(6);
437limit_in_offset(6);
438show_in_offset(7);
439limit_in_offset(7);
440show_in_offset(8);
441
442/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200443static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
444 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200446 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
447 int nr = sensor_attr->index;
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct it87_data *data = it87_update_device(dev);
450 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
451}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200452static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
453 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200455 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
456 int nr = sensor_attr->index;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct it87_data *data = it87_update_device(dev);
459 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
460}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200461static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
462 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200464 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
465 int nr = sensor_attr->index;
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct it87_data *data = it87_update_device(dev);
468 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
469}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200470static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
471 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200473 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
474 int nr = sensor_attr->index;
475
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200476 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100477 long val;
478
479 if (strict_strtol(buf, 10, &val) < 0)
480 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100482 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 data->temp_high[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200484 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100485 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return count;
487}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200488static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
489 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200491 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
492 int nr = sensor_attr->index;
493
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200494 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100495 long val;
496
497 if (strict_strtol(buf, 10, &val) < 0)
498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100500 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 data->temp_low[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200502 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100503 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return count;
505}
506#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200507static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
508 show_temp, NULL, offset - 1); \
509static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
510 show_temp_max, set_temp_max, offset - 1); \
511static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
512 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514show_temp_offset(1);
515show_temp_offset(2);
516show_temp_offset(3);
517
Jean Delvare20ad93d2005-06-05 11:53:25 +0200518static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
519 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200521 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
522 int nr = sensor_attr->index;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100525 u8 reg = data->sensor; /* In case the value is updated while
526 we use it */
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (reg & (1 << nr))
529 return sprintf(buf, "3\n"); /* thermal diode */
530 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200531 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return sprintf(buf, "0\n"); /* disabled */
533}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200534static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
535 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200537 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
538 int nr = sensor_attr->index;
539
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200540 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100541 long val;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200542 u8 reg;
Jean Delvaref5f64502010-03-05 22:17:19 +0100543
544 if (strict_strtol(buf, 10, &val) < 0)
545 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Jean Delvare8acf07c2010-04-14 16:14:09 +0200547 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
548 reg &= ~(1 << nr);
549 reg &= ~(8 << nr);
Jean Delvare4ed10772008-10-17 17:51:16 +0200550 if (val == 2) { /* backwards compatibility */
551 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
552 "instead\n");
553 val = 4;
554 }
555 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200557 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200558 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200559 reg |= 8 << nr;
560 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200562
563 mutex_lock(&data->update_lock);
564 data->sensor = reg;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200565 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200566 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100567 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return count;
569}
570#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200571static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
572 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574show_sensor_offset(1);
575show_sensor_offset(2);
576show_sensor_offset(3);
577
578/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100579
580static int pwm_mode(const struct it87_data *data, int nr)
581{
582 int ctrl = data->fan_main_ctrl & (1 << nr);
583
584 if (ctrl == 0) /* Full speed */
585 return 0;
586 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
587 return 2;
588 else /* Manual mode */
589 return 1;
590}
591
Jean Delvare20ad93d2005-06-05 11:53:25 +0200592static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
593 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200595 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
596 int nr = sensor_attr->index;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100599 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 DIV_FROM_REG(data->fan_div[nr])));
601}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200602static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
603 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200605 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
606 int nr = sensor_attr->index;
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100609 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
610 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200612static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
613 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200615 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
616 int nr = sensor_attr->index;
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 struct it87_data *data = it87_update_device(dev);
619 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
620}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100621static ssize_t show_pwm_enable(struct device *dev,
622 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200624 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
625 int nr = sensor_attr->index;
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100628 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200630static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
631 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200633 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
634 int nr = sensor_attr->index;
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100637 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100639static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
640 char *buf)
641{
642 struct it87_data *data = it87_update_device(dev);
643 int index = (data->fan_ctl >> 4) & 0x07;
644
645 return sprintf(buf, "%u\n", pwm_freq[index]);
646}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200647static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
648 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200650 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
651 int nr = sensor_attr->index;
652
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200653 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100654 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100655 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jean Delvaref5f64502010-03-05 22:17:19 +0100657 if (strict_strtol(buf, 10, &val) < 0)
658 return -EINVAL;
659
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100660 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200661 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800662 switch (nr) {
Jean Delvare5f2dc792010-03-05 22:17:18 +0100663 case 0:
664 data->fan_div[nr] = reg & 0x07;
665 break;
666 case 1:
667 data->fan_div[nr] = (reg >> 3) & 0x07;
668 break;
669 case 2:
670 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
671 break;
Jean Delvare07eab462005-11-23 15:44:31 -0800672 }
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200675 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100676 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return count;
678}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200679static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
680 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200682 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
683 int nr = sensor_attr->index;
684
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200685 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100686 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200687 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 u8 old;
689
Jean Delvaref5f64502010-03-05 22:17:19 +0100690 if (strict_strtoul(buf, 10, &val) < 0)
691 return -EINVAL;
692
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100693 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200694 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200696 /* Save fan min limit */
697 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 switch (nr) {
700 case 0:
701 case 1:
702 data->fan_div[nr] = DIV_TO_REG(val);
703 break;
704 case 2:
705 if (val < 8)
706 data->fan_div[nr] = 1;
707 else
708 data->fan_div[nr] = 3;
709 }
710 val = old & 0x80;
711 val |= (data->fan_div[0] & 0x07);
712 val |= (data->fan_div[1] & 0x07) << 3;
713 if (data->fan_div[2] == 3)
714 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200715 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200717 /* Restore fan min limit */
718 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200719 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200720
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100721 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return count;
723}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100724
725/* Returns 0 if OK, -EINVAL otherwise */
726static int check_trip_points(struct device *dev, int nr)
727{
728 const struct it87_data *data = dev_get_drvdata(dev);
729 int i, err = 0;
730
731 if (has_old_autopwm(data)) {
732 for (i = 0; i < 3; i++) {
733 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
734 err = -EINVAL;
735 }
736 for (i = 0; i < 2; i++) {
737 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
738 err = -EINVAL;
739 }
740 }
741
742 if (err) {
743 dev_err(dev, "Inconsistent trip points, not switching to "
744 "automatic mode\n");
745 dev_err(dev, "Adjust the trip points and try again\n");
746 }
747 return err;
748}
749
Jean Delvare20ad93d2005-06-05 11:53:25 +0200750static ssize_t set_pwm_enable(struct device *dev,
751 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200753 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
754 int nr = sensor_attr->index;
755
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200756 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100757 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Jean Delvaref5f64502010-03-05 22:17:19 +0100759 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100760 return -EINVAL;
761
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100762 /* Check trip points before switching to automatic mode */
763 if (val == 2) {
764 if (check_trip_points(dev, nr) < 0)
765 return -EINVAL;
766 }
767
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100768 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if (val == 0) {
771 int tmp;
772 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200773 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
774 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /* set on/off mode */
776 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100777 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
778 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100779 } else {
780 if (val == 1) /* Manual mode */
781 data->pwm_ctrl[nr] = data->pwm_duty[nr];
782 else /* Automatic mode */
783 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
784 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* set SmartGuardian mode */
786 data->fan_main_ctrl |= (1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100787 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
788 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100791 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return count;
793}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200794static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
795 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200797 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
798 int nr = sensor_attr->index;
799
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200800 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100801 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Jean Delvaref5f64502010-03-05 22:17:19 +0100803 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -EINVAL;
805
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100806 mutex_lock(&data->update_lock);
Jean Delvareb99883d2010-03-05 22:17:15 +0100807 data->pwm_duty[nr] = PWM_TO_REG(val);
808 /* If we are in manual mode, write the duty cycle immediately;
809 * otherwise, just store it for later use. */
810 if (!(data->pwm_ctrl[nr] & 0x80)) {
811 data->pwm_ctrl[nr] = data->pwm_duty[nr];
812 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
813 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100814 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return count;
816}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100817static ssize_t set_pwm_freq(struct device *dev,
818 struct device_attribute *attr, const char *buf, size_t count)
819{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200820 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100821 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100822 int i;
823
Jean Delvaref5f64502010-03-05 22:17:19 +0100824 if (strict_strtoul(buf, 10, &val) < 0)
825 return -EINVAL;
826
Jean Delvaref8d0c192007-02-14 21:15:02 +0100827 /* Search for the nearest available frequency */
828 for (i = 0; i < 7; i++) {
829 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
830 break;
831 }
832
833 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200834 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100835 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200836 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100837 mutex_unlock(&data->update_lock);
838
839 return count;
840}
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100841static ssize_t show_pwm_temp_map(struct device *dev,
842 struct device_attribute *attr, char *buf)
843{
844 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
845 int nr = sensor_attr->index;
846
847 struct it87_data *data = it87_update_device(dev);
848 int map;
849
850 if (data->pwm_temp_map[nr] < 3)
851 map = 1 << data->pwm_temp_map[nr];
852 else
853 map = 0; /* Should never happen */
854 return sprintf(buf, "%d\n", map);
855}
856static ssize_t set_pwm_temp_map(struct device *dev,
857 struct device_attribute *attr, const char *buf, size_t count)
858{
859 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
860 int nr = sensor_attr->index;
861
862 struct it87_data *data = dev_get_drvdata(dev);
863 long val;
864 u8 reg;
865
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100866 /* This check can go away if we ever support automatic fan speed
867 control on newer chips. */
868 if (!has_old_autopwm(data)) {
869 dev_notice(dev, "Mapping change disabled for safety reasons\n");
870 return -EINVAL;
871 }
872
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100873 if (strict_strtol(buf, 10, &val) < 0)
874 return -EINVAL;
875
876 switch (val) {
877 case (1 << 0):
878 reg = 0x00;
879 break;
880 case (1 << 1):
881 reg = 0x01;
882 break;
883 case (1 << 2):
884 reg = 0x02;
885 break;
886 default:
887 return -EINVAL;
888 }
889
890 mutex_lock(&data->update_lock);
891 data->pwm_temp_map[nr] = reg;
892 /* If we are in automatic mode, write the temp mapping immediately;
893 * otherwise, just store it for later use. */
894 if (data->pwm_ctrl[nr] & 0x80) {
895 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
896 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
897 }
898 mutex_unlock(&data->update_lock);
899 return count;
900}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100902static ssize_t show_auto_pwm(struct device *dev,
903 struct device_attribute *attr, char *buf)
904{
905 struct it87_data *data = it87_update_device(dev);
906 struct sensor_device_attribute_2 *sensor_attr =
907 to_sensor_dev_attr_2(attr);
908 int nr = sensor_attr->nr;
909 int point = sensor_attr->index;
910
911 return sprintf(buf, "%d\n", PWM_FROM_REG(data->auto_pwm[nr][point]));
912}
913
914static ssize_t set_auto_pwm(struct device *dev,
915 struct device_attribute *attr, const char *buf, size_t count)
916{
917 struct it87_data *data = dev_get_drvdata(dev);
918 struct sensor_device_attribute_2 *sensor_attr =
919 to_sensor_dev_attr_2(attr);
920 int nr = sensor_attr->nr;
921 int point = sensor_attr->index;
922 long val;
923
924 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
925 return -EINVAL;
926
927 mutex_lock(&data->update_lock);
928 data->auto_pwm[nr][point] = PWM_TO_REG(val);
929 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
930 data->auto_pwm[nr][point]);
931 mutex_unlock(&data->update_lock);
932 return count;
933}
934
935static ssize_t show_auto_temp(struct device *dev,
936 struct device_attribute *attr, char *buf)
937{
938 struct it87_data *data = it87_update_device(dev);
939 struct sensor_device_attribute_2 *sensor_attr =
940 to_sensor_dev_attr_2(attr);
941 int nr = sensor_attr->nr;
942 int point = sensor_attr->index;
943
944 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
945}
946
947static ssize_t set_auto_temp(struct device *dev,
948 struct device_attribute *attr, const char *buf, size_t count)
949{
950 struct it87_data *data = dev_get_drvdata(dev);
951 struct sensor_device_attribute_2 *sensor_attr =
952 to_sensor_dev_attr_2(attr);
953 int nr = sensor_attr->nr;
954 int point = sensor_attr->index;
955 long val;
956
957 if (strict_strtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
958 return -EINVAL;
959
960 mutex_lock(&data->update_lock);
961 data->auto_temp[nr][point] = TEMP_TO_REG(val);
962 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
963 data->auto_temp[nr][point]);
964 mutex_unlock(&data->update_lock);
965 return count;
966}
967
Jean Delvare20ad93d2005-06-05 11:53:25 +0200968#define show_fan_offset(offset) \
969static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
970 show_fan, NULL, offset - 1); \
971static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
972 show_fan_min, set_fan_min, offset - 1); \
973static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
974 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976show_fan_offset(1);
977show_fan_offset(2);
978show_fan_offset(3);
979
980#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200981static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
982 show_pwm_enable, set_pwm_enable, offset - 1); \
983static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +0100984 show_pwm, set_pwm, offset - 1); \
985static DEVICE_ATTR(pwm##offset##_freq, \
986 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100987 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
988static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100989 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
990 offset - 1); \
991static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
992 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
993 offset - 1, 0); \
994static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
995 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
996 offset - 1, 1); \
997static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
998 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
999 offset - 1, 2); \
1000static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1001 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1002static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1003 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1004 offset - 1, 1); \
1005static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1006 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1007 offset - 1, 0); \
1008static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1009 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1010 offset - 1, 2); \
1011static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1012 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1013 offset - 1, 3); \
1014static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1015 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1016 offset - 1, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018show_pwm_offset(1);
1019show_pwm_offset(2);
1020show_pwm_offset(3);
1021
Jean Delvare17d648b2006-08-28 14:23:46 +02001022/* A different set of callbacks for 16-bit fans */
1023static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1024 char *buf)
1025{
1026 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1027 int nr = sensor_attr->index;
1028 struct it87_data *data = it87_update_device(dev);
1029 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1030}
1031
1032static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1033 char *buf)
1034{
1035 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1036 int nr = sensor_attr->index;
1037 struct it87_data *data = it87_update_device(dev);
1038 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1039}
1040
1041static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1042 const char *buf, size_t count)
1043{
1044 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1045 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001046 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001047 long val;
1048
1049 if (strict_strtol(buf, 10, &val) < 0)
1050 return -EINVAL;
Jean Delvare17d648b2006-08-28 14:23:46 +02001051
1052 mutex_lock(&data->update_lock);
1053 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001054 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001055 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001056 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001057 data->fan_min[nr] >> 8);
1058 mutex_unlock(&data->update_lock);
1059 return count;
1060}
1061
1062/* We want to use the same sysfs file names as 8-bit fans, but we need
1063 different variable names, so we have to use SENSOR_ATTR instead of
1064 SENSOR_DEVICE_ATTR. */
1065#define show_fan16_offset(offset) \
1066static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1067 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1068 show_fan16, NULL, offset - 1); \
1069static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1070 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1071 show_fan16_min, set_fan16_min, offset - 1)
1072
1073show_fan16_offset(1);
1074show_fan16_offset(2);
1075show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001076show_fan16_offset(4);
1077show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +02001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001080static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1081 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001084 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
Jean Delvare1d66c642005-04-18 21:16:59 -07001086static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Jean Delvare0124dd72007-11-25 16:16:41 +01001088static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1089 char *buf)
1090{
1091 int bitnr = to_sensor_dev_attr(attr)->index;
1092 struct it87_data *data = it87_update_device(dev);
1093 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1094}
1095static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1096static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1097static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1098static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1099static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1100static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1101static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1102static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1103static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1104static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1105static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1106static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1107static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1108static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1109static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1110static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1111
Jean Delvared9b327c2010-03-05 22:17:17 +01001112static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1113 char *buf)
1114{
1115 int bitnr = to_sensor_dev_attr(attr)->index;
1116 struct it87_data *data = it87_update_device(dev);
1117 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1118}
1119static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1120 const char *buf, size_t count)
1121{
1122 int bitnr = to_sensor_dev_attr(attr)->index;
1123 struct it87_data *data = dev_get_drvdata(dev);
1124 long val;
1125
1126 if (strict_strtol(buf, 10, &val) < 0
1127 || (val != 0 && val != 1))
1128 return -EINVAL;
1129
1130 mutex_lock(&data->update_lock);
1131 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1132 if (val)
1133 data->beeps |= (1 << bitnr);
1134 else
1135 data->beeps &= ~(1 << bitnr);
1136 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1137 mutex_unlock(&data->update_lock);
1138 return count;
1139}
1140
1141static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1142 show_beep, set_beep, 1);
1143static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1144static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1145static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1146static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1147static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1148static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1149static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1150/* fanX_beep writability is set later */
1151static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1152static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1153static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1154static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1155static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1156static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1157 show_beep, set_beep, 2);
1158static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1159static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1160
Jean Delvare5f2dc792010-03-05 22:17:18 +01001161static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1162 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Jean Delvare90d66192007-10-08 18:24:35 +02001164 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001165 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001167static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1168 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001170 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001171 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Jean Delvaref5f64502010-03-05 22:17:19 +01001173 if (strict_strtoul(buf, 10, &val) < 0)
1174 return -EINVAL;
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 data->vrm = val;
1177
1178 return count;
1179}
1180static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Jean Delvare5f2dc792010-03-05 22:17:18 +01001182static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1183 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 struct it87_data *data = it87_update_device(dev);
1186 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1187}
1188static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001189
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001190static ssize_t show_name(struct device *dev, struct device_attribute
1191 *devattr, char *buf)
1192{
1193 struct it87_data *data = dev_get_drvdata(dev);
1194 return sprintf(buf, "%s\n", data->name);
1195}
1196static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1197
Jean Delvare87808be2006-09-24 21:17:13 +02001198static struct attribute *it87_attributes[] = {
1199 &sensor_dev_attr_in0_input.dev_attr.attr,
1200 &sensor_dev_attr_in1_input.dev_attr.attr,
1201 &sensor_dev_attr_in2_input.dev_attr.attr,
1202 &sensor_dev_attr_in3_input.dev_attr.attr,
1203 &sensor_dev_attr_in4_input.dev_attr.attr,
1204 &sensor_dev_attr_in5_input.dev_attr.attr,
1205 &sensor_dev_attr_in6_input.dev_attr.attr,
1206 &sensor_dev_attr_in7_input.dev_attr.attr,
1207 &sensor_dev_attr_in8_input.dev_attr.attr,
1208 &sensor_dev_attr_in0_min.dev_attr.attr,
1209 &sensor_dev_attr_in1_min.dev_attr.attr,
1210 &sensor_dev_attr_in2_min.dev_attr.attr,
1211 &sensor_dev_attr_in3_min.dev_attr.attr,
1212 &sensor_dev_attr_in4_min.dev_attr.attr,
1213 &sensor_dev_attr_in5_min.dev_attr.attr,
1214 &sensor_dev_attr_in6_min.dev_attr.attr,
1215 &sensor_dev_attr_in7_min.dev_attr.attr,
1216 &sensor_dev_attr_in0_max.dev_attr.attr,
1217 &sensor_dev_attr_in1_max.dev_attr.attr,
1218 &sensor_dev_attr_in2_max.dev_attr.attr,
1219 &sensor_dev_attr_in3_max.dev_attr.attr,
1220 &sensor_dev_attr_in4_max.dev_attr.attr,
1221 &sensor_dev_attr_in5_max.dev_attr.attr,
1222 &sensor_dev_attr_in6_max.dev_attr.attr,
1223 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001224 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1225 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1226 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1227 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1228 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1229 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1230 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1231 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001232
1233 &sensor_dev_attr_temp1_input.dev_attr.attr,
1234 &sensor_dev_attr_temp2_input.dev_attr.attr,
1235 &sensor_dev_attr_temp3_input.dev_attr.attr,
1236 &sensor_dev_attr_temp1_max.dev_attr.attr,
1237 &sensor_dev_attr_temp2_max.dev_attr.attr,
1238 &sensor_dev_attr_temp3_max.dev_attr.attr,
1239 &sensor_dev_attr_temp1_min.dev_attr.attr,
1240 &sensor_dev_attr_temp2_min.dev_attr.attr,
1241 &sensor_dev_attr_temp3_min.dev_attr.attr,
1242 &sensor_dev_attr_temp1_type.dev_attr.attr,
1243 &sensor_dev_attr_temp2_type.dev_attr.attr,
1244 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001245 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1246 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1247 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001248
1249 &dev_attr_alarms.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001250 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001251 NULL
1252};
1253
1254static const struct attribute_group it87_group = {
1255 .attrs = it87_attributes,
1256};
1257
Jean Delvared9b327c2010-03-05 22:17:17 +01001258static struct attribute *it87_attributes_beep[] = {
1259 &sensor_dev_attr_in0_beep.dev_attr.attr,
1260 &sensor_dev_attr_in1_beep.dev_attr.attr,
1261 &sensor_dev_attr_in2_beep.dev_attr.attr,
1262 &sensor_dev_attr_in3_beep.dev_attr.attr,
1263 &sensor_dev_attr_in4_beep.dev_attr.attr,
1264 &sensor_dev_attr_in5_beep.dev_attr.attr,
1265 &sensor_dev_attr_in6_beep.dev_attr.attr,
1266 &sensor_dev_attr_in7_beep.dev_attr.attr,
1267
1268 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1269 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1270 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1271 NULL
1272};
1273
1274static const struct attribute_group it87_group_beep = {
1275 .attrs = it87_attributes_beep,
1276};
1277
Jean Delvare723a0aa2010-03-05 22:17:16 +01001278static struct attribute *it87_attributes_fan16[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001279 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1280 &sensor_dev_attr_fan1_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001281 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1282 NULL
1283}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001284 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1285 &sensor_dev_attr_fan2_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001286 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1287 NULL
1288}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001289 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1290 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001291 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1292 NULL
1293}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001294 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1295 &sensor_dev_attr_fan4_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001296 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1297 NULL
1298}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001299 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1300 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001301 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1302 NULL
1303} };
Jean Delvare87808be2006-09-24 21:17:13 +02001304
Jean Delvare723a0aa2010-03-05 22:17:16 +01001305static const struct attribute_group it87_group_fan16[5] = {
1306 { .attrs = it87_attributes_fan16[0] },
1307 { .attrs = it87_attributes_fan16[1] },
1308 { .attrs = it87_attributes_fan16[2] },
1309 { .attrs = it87_attributes_fan16[3] },
1310 { .attrs = it87_attributes_fan16[4] },
1311};
1312
1313static struct attribute *it87_attributes_fan[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001314 &sensor_dev_attr_fan1_input.dev_attr.attr,
1315 &sensor_dev_attr_fan1_min.dev_attr.attr,
1316 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001317 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1318 NULL
1319}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001320 &sensor_dev_attr_fan2_input.dev_attr.attr,
1321 &sensor_dev_attr_fan2_min.dev_attr.attr,
1322 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001323 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1324 NULL
1325}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001326 &sensor_dev_attr_fan3_input.dev_attr.attr,
1327 &sensor_dev_attr_fan3_min.dev_attr.attr,
1328 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001329 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001330 NULL
1331} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001332
Jean Delvare723a0aa2010-03-05 22:17:16 +01001333static const struct attribute_group it87_group_fan[3] = {
1334 { .attrs = it87_attributes_fan[0] },
1335 { .attrs = it87_attributes_fan[1] },
1336 { .attrs = it87_attributes_fan[2] },
1337};
1338
1339static const struct attribute_group *
1340it87_get_fan_group(const struct it87_data *data)
1341{
1342 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1343}
1344
1345static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001346 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001347 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001348 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001349 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001350 NULL
1351}, {
1352 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1353 &sensor_dev_attr_pwm2.dev_attr.attr,
1354 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001355 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001356 NULL
1357}, {
1358 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1359 &sensor_dev_attr_pwm3.dev_attr.attr,
1360 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001361 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001362 NULL
1363} };
Jean Delvare87808be2006-09-24 21:17:13 +02001364
Jean Delvare723a0aa2010-03-05 22:17:16 +01001365static const struct attribute_group it87_group_pwm[3] = {
1366 { .attrs = it87_attributes_pwm[0] },
1367 { .attrs = it87_attributes_pwm[1] },
1368 { .attrs = it87_attributes_pwm[2] },
1369};
1370
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001371static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1372 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1373 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1374 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1375 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1376 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1377 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1378 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1379 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1380 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1381 NULL
1382}, {
1383 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1384 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1385 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1386 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1387 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1388 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1389 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1390 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1391 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1392 NULL
1393}, {
1394 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1395 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1396 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1397 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1398 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1399 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1400 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1401 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1402 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1403 NULL
1404} };
1405
1406static const struct attribute_group it87_group_autopwm[3] = {
1407 { .attrs = it87_attributes_autopwm[0] },
1408 { .attrs = it87_attributes_autopwm[1] },
1409 { .attrs = it87_attributes_autopwm[2] },
1410};
1411
Jean Delvared9b327c2010-03-05 22:17:17 +01001412static struct attribute *it87_attributes_fan_beep[] = {
1413 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1414 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1415 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1416 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1417 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1418};
1419
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001420static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001421 &dev_attr_vrm.attr,
1422 &dev_attr_cpu0_vid.attr,
1423 NULL
1424};
1425
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001426static const struct attribute_group it87_group_vid = {
1427 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001428};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Jean Delvare2d8672c2005-07-19 23:56:35 +02001430/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001431static int __init it87_find(unsigned short *address,
1432 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 int err = -ENODEV;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001435 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001436 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 superio_enter();
Jean Delvare67b671b2007-12-06 23:13:42 +01001439 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001440
1441 switch (chip_type) {
1442 case IT8705F_DEVID:
1443 sio_data->type = it87;
1444 break;
1445 case IT8712F_DEVID:
1446 sio_data->type = it8712;
1447 break;
1448 case IT8716F_DEVID:
1449 case IT8726F_DEVID:
1450 sio_data->type = it8716;
1451 break;
1452 case IT8718F_DEVID:
1453 sio_data->type = it8718;
1454 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001455 case IT8720F_DEVID:
1456 sio_data->type = it8720;
1457 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001458 case 0xffff: /* No device at all */
1459 goto exit;
1460 default:
1461 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
1462 chip_type);
1463 goto exit;
1464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Jean Delvare87673dd2006-08-28 14:37:19 +02001466 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1468 pr_info("it87: Device not activated, skipping\n");
1469 goto exit;
1470 }
1471
1472 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1473 if (*address == 0) {
1474 pr_info("it87: Base address not set, skipping\n");
1475 goto exit;
1476 }
1477
1478 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001479 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001481 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Jean Delvare87673dd2006-08-28 14:37:19 +02001483 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001484 if (sio_data->type == it87) {
1485 /* The IT8705F doesn't have VID pins at all */
1486 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001487
1488 /* The IT8705F has a different LD number for GPIO */
1489 superio_select(5);
1490 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01001491 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001492 int reg;
1493
1494 superio_select(GPIO);
Jean Delvare895ff262009-12-09 20:35:47 +01001495 /* We need at least 4 VID pins */
1496 reg = superio_inb(IT87_SIO_GPIO3_REG);
1497 if (reg & 0x0f) {
1498 pr_info("it87: VID is disabled (pins used for GPIO)\n");
1499 sio_data->skip_vid = 1;
1500 }
1501
Jean Delvare591ec652009-12-09 20:35:48 +01001502 /* Check if fan3 is there or not */
1503 if (reg & (1 << 6))
1504 sio_data->skip_pwm |= (1 << 2);
1505 if (reg & (1 << 7))
1506 sio_data->skip_fan |= (1 << 2);
1507
1508 /* Check if fan2 is there or not */
1509 reg = superio_inb(IT87_SIO_GPIO5_REG);
1510 if (reg & (1 << 1))
1511 sio_data->skip_pwm |= (1 << 1);
1512 if (reg & (1 << 2))
1513 sio_data->skip_fan |= (1 << 1);
1514
Jean Delvare895ff262009-12-09 20:35:47 +01001515 if ((sio_data->type == it8718 || sio_data->type == it8720)
1516 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001517 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001518
1519 reg = superio_inb(IT87_SIO_PINX2_REG);
1520 if (reg & (1 << 0))
1521 pr_info("it87: in3 is VCC (+5V)\n");
1522 if (reg & (1 << 1))
1523 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
Jean Delvared9b327c2010-03-05 22:17:17 +01001524
1525 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02001526 }
Jean Delvared9b327c2010-03-05 22:17:17 +01001527 if (sio_data->beep_pin)
1528 pr_info("it87: Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02001529
Jean Delvare98dd22c2008-10-09 15:33:58 +02001530 /* Disable specific features based on DMI strings */
1531 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1532 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1533 if (board_vendor && board_name) {
1534 if (strcmp(board_vendor, "nVIDIA") == 0
1535 && strcmp(board_name, "FN68PT") == 0) {
1536 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1537 connected to a fan, but to something else. One user
1538 has reported instant system power-off when changing
1539 the PWM2 duty cycle, so we disable it.
1540 I use the board name string as the trigger in case
1541 the same board is ever used in other systems. */
1542 pr_info("it87: Disabling pwm2 due to "
1543 "hardware constraints\n");
1544 sio_data->skip_pwm = (1 << 1);
1545 }
1546 }
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548exit:
1549 superio_exit();
1550 return err;
1551}
1552
Jean Delvare723a0aa2010-03-05 22:17:16 +01001553static void it87_remove_files(struct device *dev)
1554{
1555 struct it87_data *data = platform_get_drvdata(pdev);
1556 struct it87_sio_data *sio_data = dev->platform_data;
1557 const struct attribute_group *fan_group = it87_get_fan_group(data);
1558 int i;
1559
1560 sysfs_remove_group(&dev->kobj, &it87_group);
Jean Delvared9b327c2010-03-05 22:17:17 +01001561 if (sio_data->beep_pin)
1562 sysfs_remove_group(&dev->kobj, &it87_group_beep);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001563 for (i = 0; i < 5; i++) {
1564 if (!(data->has_fan & (1 << i)))
1565 continue;
1566 sysfs_remove_group(&dev->kobj, &fan_group[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01001567 if (sio_data->beep_pin)
1568 sysfs_remove_file(&dev->kobj,
1569 it87_attributes_fan_beep[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001570 }
1571 for (i = 0; i < 3; i++) {
1572 if (sio_data->skip_pwm & (1 << 0))
1573 continue;
1574 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001575 if (has_old_autopwm(data))
1576 sysfs_remove_group(&dev->kobj,
1577 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001578 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001579 if (!sio_data->skip_vid)
1580 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001581}
1582
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001583static int __devinit it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001586 struct resource *res;
1587 struct device *dev = &pdev->dev;
1588 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001589 const struct attribute_group *fan_group;
1590 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01001592 int fan_beep_need_rw;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001593 static const char *names[] = {
1594 "it87",
1595 "it8712",
1596 "it8716",
1597 "it8718",
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001598 "it8720",
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001599 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001601 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001602 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001603 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1604 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001605 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001606 err = -EBUSY;
1607 goto ERROR0;
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Jean Delvare5f2dc792010-03-05 22:17:18 +01001610 data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
1611 if (!data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 err = -ENOMEM;
1613 goto ERROR1;
1614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001616 data->addr = res->start;
1617 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001618 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001619 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001622 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1623 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001624 err = -ENODEV;
1625 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001628 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001630 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001633 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001636 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001639 err = sysfs_create_group(&dev->kobj, &it87_group);
1640 if (err)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001641 goto ERROR2;
Jean Delvare17d648b2006-08-28 14:23:46 +02001642
Jean Delvared9b327c2010-03-05 22:17:17 +01001643 if (sio_data->beep_pin) {
1644 err = sysfs_create_group(&dev->kobj, &it87_group_beep);
1645 if (err)
1646 goto ERROR4;
1647 }
1648
Jean Delvare9060f8b2006-08-28 14:24:17 +02001649 /* Do not create fan files for disabled fans */
Jean Delvare723a0aa2010-03-05 22:17:16 +01001650 fan_group = it87_get_fan_group(data);
Jean Delvared9b327c2010-03-05 22:17:17 +01001651 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001652 for (i = 0; i < 5; i++) {
1653 if (!(data->has_fan & (1 << i)))
1654 continue;
1655 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
1656 if (err)
1657 goto ERROR4;
Jean Delvared9b327c2010-03-05 22:17:17 +01001658
1659 if (sio_data->beep_pin) {
1660 err = sysfs_create_file(&dev->kobj,
1661 it87_attributes_fan_beep[i]);
1662 if (err)
1663 goto ERROR4;
1664 if (!fan_beep_need_rw)
1665 continue;
1666
1667 /* As we have a single beep enable bit for all fans,
1668 * only the first enabled fan has a writable attribute
1669 * for it. */
1670 if (sysfs_chmod_file(&dev->kobj,
1671 it87_attributes_fan_beep[i],
1672 S_IRUGO | S_IWUSR))
1673 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
1674 i + 1);
1675 fan_beep_need_rw = 0;
1676 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001677 }
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001680 for (i = 0; i < 3; i++) {
1681 if (sio_data->skip_pwm & (1 << i))
1682 continue;
1683 err = sysfs_create_group(&dev->kobj,
1684 &it87_group_pwm[i]);
1685 if (err)
Jean Delvare98dd22c2008-10-09 15:33:58 +02001686 goto ERROR4;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001687
1688 if (!has_old_autopwm(data))
1689 continue;
1690 err = sysfs_create_group(&dev->kobj,
1691 &it87_group_autopwm[i]);
1692 if (err)
1693 goto ERROR4;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
Jean Delvare895ff262009-12-09 20:35:47 +01001697 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02001698 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001699 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001700 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001701 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
1702 if (err)
Jean Delvare87808be2006-09-24 21:17:13 +02001703 goto ERROR4;
1704 }
1705
Tony Jones1beeffe2007-08-20 13:46:20 -07001706 data->hwmon_dev = hwmon_device_register(dev);
1707 if (IS_ERR(data->hwmon_dev)) {
1708 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001709 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
1712 return 0;
1713
Jean Delvare87808be2006-09-24 21:17:13 +02001714ERROR4:
Jean Delvare723a0aa2010-03-05 22:17:16 +01001715 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716ERROR2:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001717 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 kfree(data);
1719ERROR1:
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001720 release_region(res->start, IT87_EC_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721ERROR0:
1722 return err;
1723}
1724
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001725static int __devexit it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001727 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Tony Jones1beeffe2007-08-20 13:46:20 -07001729 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001730 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001731
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001732 release_region(data->addr, IT87_EC_EXTENT);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001733 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001734 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 return 0;
1737}
1738
Jean Delvare7f999aa2007-02-14 21:15:03 +01001739/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1741 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001742static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001744 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1745 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
Jean Delvare7f999aa2007-02-14 21:15:03 +01001748/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1750 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001751static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001753 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1754 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
1757/* Return 1 if and only if the PWM interface is safe to use */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001758static int __devinit it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001760 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1762 * and polarity set to active low is sign that this is the case so we
1763 * disable pwm control to protect the user. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001764 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if ((tmp & 0x87) == 0) {
1766 if (fix_pwm_polarity) {
1767 /* The user asks us to attempt a chip reconfiguration.
1768 * This means switching to active high polarity and
1769 * inverting all fan speed values. */
1770 int i;
1771 u8 pwm[3];
1772
1773 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001774 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 IT87_REG_PWM(i));
1776
1777 /* If any fan is in automatic pwm mode, the polarity
1778 * might be correct, as suspicious as it seems, so we
1779 * better don't change anything (but still disable the
1780 * PWM interface). */
1781 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001782 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001784 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 tmp | 0x87);
1786 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001787 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 IT87_REG_PWM(i),
1789 0x7f & ~pwm[i]);
1790 return 1;
1791 }
1792
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001793 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 "too broken to be fixed\n");
1795 }
1796
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001797 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 "defaults, disabling PWM interface\n");
1799 return 0;
1800 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001801 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 "sane, won't touch\n");
1803 }
1804
1805 return 1;
1806}
1807
1808/* Called when we have found a new IT87. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001809static void __devinit it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Jean Delvare591ec652009-12-09 20:35:48 +01001811 struct it87_sio_data *sio_data = pdev->dev.platform_data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001812 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01001814 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Jean Delvareb99883d2010-03-05 22:17:15 +01001816 /* For each PWM channel:
1817 * - If it is in automatic mode, setting to manual mode should set
1818 * the fan to full speed by default.
1819 * - If it is in manual mode, we need a mapping to temperature
1820 * channels to use when later setting to automatic mode later.
1821 * Use a 1:1 mapping by default (we are clueless.)
1822 * In both cases, the value can (and should) be changed by the user
1823 * prior to switching to a different mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01001825 data->pwm_temp_map[i] = i;
1826 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001827 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829
Jean Delvarec5df9b72006-08-28 14:37:54 +02001830 /* Some chips seem to have default value 0xff for all limit
1831 * registers. For low voltage limits it makes no sense and triggers
1832 * alarms, so change to 0 instead. For high temperature limits, it
1833 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1834 * but is still confusing, so change to 127 degrees C. */
1835 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001836 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001837 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001838 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001839 }
1840 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001841 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001842 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001843 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001844 }
1845
Jean Delvarea00afb92010-04-14 16:14:09 +02001846 /* Temperature channels are not forcibly enabled, as they can be
1847 * set to two different sensor types and we can't guess which one
1848 * is correct for a given system. These channels can be enabled at
1849 * run-time through the temp{1-3}_type sysfs accessors if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001852 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if ((tmp & 0xff) == 0) {
1854 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001855 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857
1858 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01001859 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001860 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01001861 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01001863 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01001864 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1865 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001867 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Jean Delvare17d648b2006-08-28 14:23:46 +02001869 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02001870 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001871 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001872 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001873 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02001874 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001875 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02001876 tmp | 0x07);
1877 }
Andrew Paprocki816d8c62008-08-06 22:41:06 +02001878 /* IT8705F only supports three fans. */
1879 if (data->type != it87) {
1880 if (tmp & (1 << 4))
1881 data->has_fan |= (1 << 3); /* fan4 enabled */
1882 if (tmp & (1 << 5))
1883 data->has_fan |= (1 << 4); /* fan5 enabled */
1884 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001885 }
1886
Jean Delvare591ec652009-12-09 20:35:48 +01001887 /* Fan input pins may be used for alternative functions */
1888 data->has_fan &= ~sio_data->skip_fan;
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001891 it87_write_value(data, IT87_REG_CONFIG,
1892 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 | (update_vbat ? 0x41 : 0x01));
1894}
1895
Jean Delvareb99883d2010-03-05 22:17:15 +01001896static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
1897{
1898 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
1899 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
1900 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
1901 else /* Manual mode */
1902 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001903
1904 if (has_old_autopwm(data)) {
1905 int i;
1906
1907 for (i = 0; i < 5 ; i++)
1908 data->auto_temp[nr][i] = it87_read_value(data,
1909 IT87_REG_AUTO_TEMP(nr, i));
1910 for (i = 0; i < 3 ; i++)
1911 data->auto_pwm[nr][i] = it87_read_value(data,
1912 IT87_REG_AUTO_PWM(nr, i));
1913 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001914}
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916static struct it87_data *it87_update_device(struct device *dev)
1917{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001918 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 int i;
1920
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001921 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1924 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (update_vbat) {
1926 /* Cleared after each update, so reenable. Value
Jean Delvare5f2dc792010-03-05 22:17:18 +01001927 returned by this read will be previous value */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001928 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01001929 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
1931 for (i = 0; i <= 7; i++) {
1932 data->in[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01001933 it87_read_value(data, IT87_REG_VIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 data->in_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01001935 it87_read_value(data, IT87_REG_VIN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 data->in_max[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01001937 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Jean Delvare3543a532006-08-28 14:27:25 +02001939 /* in8 (battery) has no limit registers */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001940 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Jean Delvarec7f1f712007-09-03 17:11:46 +02001942 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02001943 /* Skip disabled fans */
1944 if (!(data->has_fan & (1 << i)))
1945 continue;
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 data->fan_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01001948 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001949 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001950 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02001951 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02001952 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001953 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001954 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001955 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001956 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02001957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 for (i = 0; i < 3; i++) {
1960 data->temp[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01001961 it87_read_value(data, IT87_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 data->temp_high[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01001963 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 data->temp_low[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01001965 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967
Jean Delvare17d648b2006-08-28 14:23:46 +02001968 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02001969 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001970 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02001971 data->fan_div[0] = i & 0x07;
1972 data->fan_div[1] = (i >> 3) & 0x07;
1973 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001977 it87_read_value(data, IT87_REG_ALARM1) |
1978 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1979 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01001980 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01001981
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001982 data->fan_main_ctrl = it87_read_value(data,
1983 IT87_REG_FAN_MAIN_CTRL);
1984 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01001985 for (i = 0; i < 3; i++)
1986 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001988 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Andrew Paprocki04751692008-08-06 22:41:06 +02001989 /* The 8705 does not have VID capability.
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001990 The 8718 and the 8720 don't use IT87_REG_VID for the
1991 same purpose. */
Jean Delvare17d648b2006-08-28 14:23:46 +02001992 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001993 data->vid = it87_read_value(data, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02001994 /* The older IT8712F revisions had only 5 VID pins,
1995 but we assume it is always safe to read 6 bits. */
1996 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998 data->last_updated = jiffies;
1999 data->valid = 1;
2000 }
2001
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002002 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 return data;
2005}
2006
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002007static int __init it87_device_add(unsigned short address,
2008 const struct it87_sio_data *sio_data)
2009{
2010 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002011 .start = address + IT87_EC_OFFSET,
2012 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002013 .name = DRVNAME,
2014 .flags = IORESOURCE_IO,
2015 };
2016 int err;
2017
Jean Delvareb9acb642009-01-07 16:37:35 +01002018 err = acpi_check_resource_conflict(&res);
2019 if (err)
2020 goto exit;
2021
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002022 pdev = platform_device_alloc(DRVNAME, address);
2023 if (!pdev) {
2024 err = -ENOMEM;
2025 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
2026 goto exit;
2027 }
2028
2029 err = platform_device_add_resources(pdev, &res, 1);
2030 if (err) {
2031 printk(KERN_ERR DRVNAME ": Device resource addition failed "
2032 "(%d)\n", err);
2033 goto exit_device_put;
2034 }
2035
2036 err = platform_device_add_data(pdev, sio_data,
2037 sizeof(struct it87_sio_data));
2038 if (err) {
2039 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
2040 goto exit_device_put;
2041 }
2042
2043 err = platform_device_add(pdev);
2044 if (err) {
2045 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
2046 err);
2047 goto exit_device_put;
2048 }
2049
2050 return 0;
2051
2052exit_device_put:
2053 platform_device_put(pdev);
2054exit:
2055 return err;
2056}
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058static int __init sm_it87_init(void)
2059{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002060 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002061 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002062 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002063
Jean Delvare98dd22c2008-10-09 15:33:58 +02002064 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002065 err = it87_find(&isa_address, &sio_data);
2066 if (err)
2067 return err;
2068 err = platform_driver_register(&it87_driver);
2069 if (err)
2070 return err;
2071
2072 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002073 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002074 platform_driver_unregister(&it87_driver);
2075 return err;
2076 }
2077
2078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
2081static void __exit sm_it87_exit(void)
2082{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002083 platform_device_unregister(pdev);
2084 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
2087
Jean Delvaref1d8e332007-11-25 16:14:44 +01002088MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02002089 "Jean Delvare <khali@linux-fr.org>");
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002090MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8720F/8726F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091module_param(update_vbat, bool, 0);
2092MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2093module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002094MODULE_PARM_DESC(fix_pwm_polarity,
2095 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096MODULE_LICENSE("GPL");
2097
2098module_init(sm_it87_init);
2099module_exit(sm_it87_exit);