blob: 41b55a74ce890f3fd1ee20f18967305ba6ec7587 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring.
4
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05005 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
Jean Delvare91749992005-10-08 00:10:00 +020013 Supports: IT8705F Super I/O chip w/LPC interface
Jean Delvare8e9afcb2006-12-12 18:18:28 +010014 IT8712F Super I/O chip w/LPC interface
Jean Delvare17d648b2006-08-28 14:23:46 +020015 IT8716F Super I/O chip w/LPC interface
Jean Delvare87673dd2006-08-28 14:37:19 +020016 IT8718F Super I/O chip w/LPC interface
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010017 IT8720F Super I/O chip w/LPC interface
Rudolf Marek08a8f6e2007-06-09 10:11:16 -040018 IT8726F Super I/O chip w/LPC interface
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 Sis950 A clone of the IT8705F
20
Jean Delvaref1d8e332007-11-25 16:14:44 +010021 Copyright (C) 2001 Chris Gauthron
Jean Delvare0124dd72007-11-25 16:16:41 +010022 Copyright (C) 2005-2007 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
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*/
38
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 */
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/* Update battery voltage after every reading if true */
133static int update_vbat;
134
135/* Not all BIOSes properly configure the PWM registers */
136static int fix_pwm_polarity;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Many IT87 constants specified below */
139
140/* Length of ISA address segment */
141#define IT87_EXTENT 8
142
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500143/* Length of ISA address segment for Environmental Controller */
144#define IT87_EC_EXTENT 2
145
146/* Offset of EC registers from ISA base address */
147#define IT87_EC_OFFSET 5
148
149/* Where are the ISA address/data registers relative to the EC base address */
150#define IT87_ADDR_REG_OFFSET 0
151#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/*----- The IT87 registers -----*/
154
155#define IT87_REG_CONFIG 0x00
156
157#define IT87_REG_ALARM1 0x01
158#define IT87_REG_ALARM2 0x02
159#define IT87_REG_ALARM3 0x03
160
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100161/* The IT8718F and IT8720F have the VID value in a different register, in
162 Super-I/O configuration space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define IT87_REG_VID 0x0a
Andrew Paprocki04751692008-08-06 22:41:06 +0200164/* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
165 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
166 mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200168#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
171
Jean Delvarec7f1f712007-09-03 17:11:46 +0200172static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
173static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
174static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
175static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#define IT87_REG_FAN_MAIN_CTRL 0x13
177#define IT87_REG_FAN_CTL 0x14
178#define IT87_REG_PWM(nr) (0x15 + (nr))
179
180#define IT87_REG_VIN(nr) (0x20 + (nr))
181#define IT87_REG_TEMP(nr) (0x29 + (nr))
182
183#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
184#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
185#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
186#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#define IT87_REG_VIN_ENABLE 0x50
189#define IT87_REG_TEMP_ENABLE 0x51
190
191#define IT87_REG_CHIPID 0x58
192
193#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
194#define IN_FROM_REG(val) ((val) * 16)
195
196static inline u8 FAN_TO_REG(long rpm, int div)
197{
198 if (rpm == 0)
199 return 255;
200 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
201 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
202 254);
203}
204
Jean Delvare17d648b2006-08-28 14:23:46 +0200205static inline u16 FAN16_TO_REG(long rpm)
206{
207 if (rpm == 0)
208 return 0xffff;
209 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
Jean Delvare17d648b2006-08-28 14:23:46 +0200213/* The divider is fixed to 2 in 16-bit mode */
214#define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
217 ((val)+500)/1000),-128,127))
Jean Delvaree267d252009-03-12 13:36:39 +0100218#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define PWM_TO_REG(val) ((val) >> 1)
221#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
222
223static int DIV_TO_REG(int val)
224{
225 int answer = 0;
Jean Delvareb9e349f2006-08-28 14:26:22 +0200226 while (answer < 7 && (val >>= 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 answer++;
228 return answer;
229}
230#define DIV_FROM_REG(val) (1 << (val))
231
Jean Delvaref8d0c192007-02-14 21:15:02 +0100232static const unsigned int pwm_freq[8] = {
233 48000000 / 128,
234 24000000 / 128,
235 12000000 / 128,
236 8000000 / 128,
237 6000000 / 128,
238 3000000 / 128,
239 1500000 / 128,
240 750000 / 128,
241};
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200244struct it87_sio_data {
245 enum chips type;
246 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200247 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200248 u8 vid_value;
Jean Delvare591ec652009-12-09 20:35:48 +0100249 /* Features skipped based on config or DMI */
Jean Delvare895ff262009-12-09 20:35:47 +0100250 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100251 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200252 u8 skip_pwm;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200253};
254
Jean Delvareed6bafb2007-02-14 21:15:03 +0100255/* For each registered chip, we need to keep some data in memory.
256 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700258 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 enum chips type;
Andrew Paprocki04751692008-08-06 22:41:06 +0200260 u8 revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200262 unsigned short addr;
263 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100264 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 char valid; /* !=0 if following fields are valid */
266 unsigned long last_updated; /* In jiffies */
267
268 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200269 u8 in_max[8]; /* Register value */
270 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200271 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvarec7f1f712007-09-03 17:11:46 +0200272 u16 fan[5]; /* Register values, possibly combined */
273 u16 fan_min[5]; /* Register values, possibly combined */
Jean Delvaree267d252009-03-12 13:36:39 +0100274 s8 temp[3]; /* Register value */
275 s8 temp_high[3]; /* Register value */
276 s8 temp_low[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 u8 sensor; /* Register value */
278 u8 fan_div[3]; /* Register encoding, shifted right */
279 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100280 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 u32 alarms; /* Register encoding, combined */
282 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100283 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100284
285 /* The following 3 arrays correspond to the same registers. The
286 * meaning of bits 6-0 depends on the value of bit 7, and we want
287 * to preserve settings on mode changes, so we have to track all
288 * values separately. */
289 u8 pwm_ctrl[3]; /* Register value */
290 u8 pwm_duty[3]; /* Manual PWM value set by user (bit 6-0) */
291 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292};
293
Andrew Paprocki04751692008-08-06 22:41:06 +0200294static inline int has_16bit_fans(const struct it87_data *data)
295{
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200296 /* IT8705F Datasheet 0.4.1, 3h == Version G.
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200297 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200298 These are the first revisions with 16bit tachometer support. */
299 return (data->type == it87 && data->revision >= 0x03)
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200300 || (data->type == it8712 && data->revision >= 0x08)
Andrew Paprocki04751692008-08-06 22:41:06 +0200301 || data->type == it8716
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100302 || data->type == it8718
303 || data->type == it8720;
Andrew Paprocki04751692008-08-06 22:41:06 +0200304}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200306static int it87_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200307static int __devexit it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200309static int it87_read_value(struct it87_data *data, u8 reg);
310static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200312static int it87_check_pwm(struct device *dev);
313static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200316static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100317 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200318 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200319 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100320 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200321 .probe = it87_probe,
322 .remove = __devexit_p(it87_remove),
Jean Delvarefde09502005-07-19 23:51:07 +0200323};
324
Jean Delvare20ad93d2005-06-05 11:53:25 +0200325static ssize_t show_in(struct device *dev, struct device_attribute *attr,
326 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200328 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
329 int nr = sensor_attr->index;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct it87_data *data = it87_update_device(dev);
332 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
333}
334
Jean Delvare20ad93d2005-06-05 11:53:25 +0200335static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
336 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200338 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
339 int nr = sensor_attr->index;
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct it87_data *data = it87_update_device(dev);
342 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
343}
344
Jean Delvare20ad93d2005-06-05 11:53:25 +0200345static ssize_t show_in_max(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_max[nr]));
353}
354
Jean Delvare20ad93d2005-06-05 11:53:25 +0200355static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
356 const char *buf, size_t count)
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
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200361 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 unsigned long val = simple_strtoul(buf, NULL, 10);
363
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100364 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 data->in_min[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200366 it87_write_value(data, IT87_REG_VIN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100368 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return count;
370}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200371static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
372 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200374 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
375 int nr = sensor_attr->index;
376
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200377 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 unsigned long val = simple_strtoul(buf, NULL, 10);
379
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100380 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 data->in_max[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200382 it87_write_value(data, IT87_REG_VIN_MAX(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100384 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return count;
386}
387
388#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200389static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
390 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200393static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
394 show_in_min, set_in_min, offset); \
395static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
396 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398show_in_offset(0);
399limit_in_offset(0);
400show_in_offset(1);
401limit_in_offset(1);
402show_in_offset(2);
403limit_in_offset(2);
404show_in_offset(3);
405limit_in_offset(3);
406show_in_offset(4);
407limit_in_offset(4);
408show_in_offset(5);
409limit_in_offset(5);
410show_in_offset(6);
411limit_in_offset(6);
412show_in_offset(7);
413limit_in_offset(7);
414show_in_offset(8);
415
416/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200417static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
418 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200420 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
421 int nr = sensor_attr->index;
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct it87_data *data = it87_update_device(dev);
424 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
425}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200426static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
427 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200429 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
430 int nr = sensor_attr->index;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 struct it87_data *data = it87_update_device(dev);
433 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
434}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200435static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
436 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200438 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
439 int nr = sensor_attr->index;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct it87_data *data = it87_update_device(dev);
442 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
443}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200444static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
445 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200447 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
448 int nr = sensor_attr->index;
449
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200450 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int val = simple_strtol(buf, NULL, 10);
452
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100453 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 data->temp_high[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200455 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100456 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return count;
458}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200459static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
460 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200462 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
463 int nr = sensor_attr->index;
464
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200465 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int val = simple_strtol(buf, NULL, 10);
467
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100468 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 data->temp_low[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200470 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100471 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return count;
473}
474#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200475static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
476 show_temp, NULL, offset - 1); \
477static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
478 show_temp_max, set_temp_max, offset - 1); \
479static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
480 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482show_temp_offset(1);
483show_temp_offset(2);
484show_temp_offset(3);
485
Jean Delvare20ad93d2005-06-05 11:53:25 +0200486static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
487 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200489 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
490 int nr = sensor_attr->index;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct it87_data *data = it87_update_device(dev);
493 u8 reg = data->sensor; /* In case the value is updated while we use it */
494
495 if (reg & (1 << nr))
496 return sprintf(buf, "3\n"); /* thermal diode */
497 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200498 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return sprintf(buf, "0\n"); /* disabled */
500}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200501static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
502 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200504 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
505 int nr = sensor_attr->index;
506
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200507 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 int val = simple_strtol(buf, NULL, 10);
509
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100510 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 data->sensor &= ~(1 << nr);
513 data->sensor &= ~(8 << nr);
Jean Delvare4ed10772008-10-17 17:51:16 +0200514 if (val == 2) { /* backwards compatibility */
515 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
516 "instead\n");
517 val = 4;
518 }
519 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (val == 3)
521 data->sensor |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200522 else if (val == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 data->sensor |= 8 << nr;
524 else if (val != 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100525 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -EINVAL;
527 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200528 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100529 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return count;
531}
532#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200533static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
534 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536show_sensor_offset(1);
537show_sensor_offset(2);
538show_sensor_offset(3);
539
540/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100541
542static int pwm_mode(const struct it87_data *data, int nr)
543{
544 int ctrl = data->fan_main_ctrl & (1 << nr);
545
546 if (ctrl == 0) /* Full speed */
547 return 0;
548 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
549 return 2;
550 else /* Manual mode */
551 return 1;
552}
553
Jean Delvare20ad93d2005-06-05 11:53:25 +0200554static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
555 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200557 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
558 int nr = sensor_attr->index;
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct it87_data *data = it87_update_device(dev);
561 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
562 DIV_FROM_REG(data->fan_div[nr])));
563}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200564static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
565 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200567 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
568 int nr = sensor_attr->index;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct it87_data *data = it87_update_device(dev);
571 return sprintf(buf,"%d\n",
572 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
573}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200574static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
575 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200577 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
578 int nr = sensor_attr->index;
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct it87_data *data = it87_update_device(dev);
581 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
582}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200583static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
584 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200586 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
587 int nr = sensor_attr->index;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100590 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200592static ssize_t show_pwm(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 Delvareb99883d2010-03-05 22:17:15 +0100599 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100601static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
602 char *buf)
603{
604 struct it87_data *data = it87_update_device(dev);
605 int index = (data->fan_ctl >> 4) & 0x07;
606
607 return sprintf(buf, "%u\n", pwm_freq[index]);
608}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200609static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
610 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200612 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
613 int nr = sensor_attr->index;
614
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200615 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 int val = simple_strtol(buf, NULL, 10);
Jean Delvare7f999aa2007-02-14 21:15:03 +0100617 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100619 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200620 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800621 switch (nr) {
622 case 0: data->fan_div[nr] = reg & 0x07; break;
623 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
624 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
625 }
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200628 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100629 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return count;
631}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200632static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
633 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200635 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
636 int nr = sensor_attr->index;
637
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200638 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvareb9e349f2006-08-28 14:26:22 +0200639 unsigned long val = simple_strtoul(buf, NULL, 10);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200640 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 u8 old;
642
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100643 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200644 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200646 /* Save fan min limit */
647 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 switch (nr) {
650 case 0:
651 case 1:
652 data->fan_div[nr] = DIV_TO_REG(val);
653 break;
654 case 2:
655 if (val < 8)
656 data->fan_div[nr] = 1;
657 else
658 data->fan_div[nr] = 3;
659 }
660 val = old & 0x80;
661 val |= (data->fan_div[0] & 0x07);
662 val |= (data->fan_div[1] & 0x07) << 3;
663 if (data->fan_div[2] == 3)
664 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200665 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200667 /* Restore fan min limit */
668 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200669 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200670
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100671 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return count;
673}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200674static ssize_t set_pwm_enable(struct device *dev,
675 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200677 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
678 int nr = sensor_attr->index;
679
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200680 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int val = simple_strtol(buf, NULL, 10);
682
Jean Delvareb99883d2010-03-05 22:17:15 +0100683 if (val < 0 || val > 2)
684 return -EINVAL;
685
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100686 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (val == 0) {
689 int tmp;
690 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200691 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
692 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* set on/off mode */
694 data->fan_main_ctrl &= ~(1 << nr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200695 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100696 } else {
697 if (val == 1) /* Manual mode */
698 data->pwm_ctrl[nr] = data->pwm_duty[nr];
699 else /* Automatic mode */
700 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
701 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* set SmartGuardian mode */
703 data->fan_main_ctrl |= (1 << nr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200704 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100707 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return count;
709}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200710static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200713 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
714 int nr = sensor_attr->index;
715
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200716 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 int val = simple_strtol(buf, NULL, 10);
718
719 if (val < 0 || val > 255)
720 return -EINVAL;
721
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100722 mutex_lock(&data->update_lock);
Jean Delvareb99883d2010-03-05 22:17:15 +0100723 data->pwm_duty[nr] = PWM_TO_REG(val);
724 /* If we are in manual mode, write the duty cycle immediately;
725 * otherwise, just store it for later use. */
726 if (!(data->pwm_ctrl[nr] & 0x80)) {
727 data->pwm_ctrl[nr] = data->pwm_duty[nr];
728 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
729 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100730 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return count;
732}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100733static ssize_t set_pwm_freq(struct device *dev,
734 struct device_attribute *attr, const char *buf, size_t count)
735{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200736 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100737 unsigned long val = simple_strtoul(buf, NULL, 10);
738 int i;
739
740 /* Search for the nearest available frequency */
741 for (i = 0; i < 7; i++) {
742 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
743 break;
744 }
745
746 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200747 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100748 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200749 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100750 mutex_unlock(&data->update_lock);
751
752 return count;
753}
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100754static ssize_t show_pwm_temp_map(struct device *dev,
755 struct device_attribute *attr, char *buf)
756{
757 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
758 int nr = sensor_attr->index;
759
760 struct it87_data *data = it87_update_device(dev);
761 int map;
762
763 if (data->pwm_temp_map[nr] < 3)
764 map = 1 << data->pwm_temp_map[nr];
765 else
766 map = 0; /* Should never happen */
767 return sprintf(buf, "%d\n", map);
768}
769static ssize_t set_pwm_temp_map(struct device *dev,
770 struct device_attribute *attr, const char *buf, size_t count)
771{
772 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
773 int nr = sensor_attr->index;
774
775 struct it87_data *data = dev_get_drvdata(dev);
776 long val;
777 u8 reg;
778
779 if (strict_strtol(buf, 10, &val) < 0)
780 return -EINVAL;
781
782 switch (val) {
783 case (1 << 0):
784 reg = 0x00;
785 break;
786 case (1 << 1):
787 reg = 0x01;
788 break;
789 case (1 << 2):
790 reg = 0x02;
791 break;
792 default:
793 return -EINVAL;
794 }
795
796 mutex_lock(&data->update_lock);
797 data->pwm_temp_map[nr] = reg;
798 /* If we are in automatic mode, write the temp mapping immediately;
799 * otherwise, just store it for later use. */
800 if (data->pwm_ctrl[nr] & 0x80) {
801 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
802 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
803 }
804 mutex_unlock(&data->update_lock);
805 return count;
806}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Jean Delvare20ad93d2005-06-05 11:53:25 +0200808#define show_fan_offset(offset) \
809static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
810 show_fan, NULL, offset - 1); \
811static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
812 show_fan_min, set_fan_min, offset - 1); \
813static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
814 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816show_fan_offset(1);
817show_fan_offset(2);
818show_fan_offset(3);
819
820#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200821static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
822 show_pwm_enable, set_pwm_enable, offset - 1); \
823static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +0100824 show_pwm, set_pwm, offset - 1); \
825static DEVICE_ATTR(pwm##offset##_freq, \
826 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100827 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
828static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
829 S_IRUGO, show_pwm_temp_map, set_pwm_temp_map, \
830 offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832show_pwm_offset(1);
833show_pwm_offset(2);
834show_pwm_offset(3);
835
Jean Delvare17d648b2006-08-28 14:23:46 +0200836/* A different set of callbacks for 16-bit fans */
837static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
838 char *buf)
839{
840 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
841 int nr = sensor_attr->index;
842 struct it87_data *data = it87_update_device(dev);
843 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
844}
845
846static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
847 char *buf)
848{
849 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
850 int nr = sensor_attr->index;
851 struct it87_data *data = it87_update_device(dev);
852 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
853}
854
855static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
856 const char *buf, size_t count)
857{
858 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
859 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200860 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare17d648b2006-08-28 14:23:46 +0200861 int val = simple_strtol(buf, NULL, 10);
862
863 mutex_lock(&data->update_lock);
864 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200865 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +0200866 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200867 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +0200868 data->fan_min[nr] >> 8);
869 mutex_unlock(&data->update_lock);
870 return count;
871}
872
873/* We want to use the same sysfs file names as 8-bit fans, but we need
874 different variable names, so we have to use SENSOR_ATTR instead of
875 SENSOR_DEVICE_ATTR. */
876#define show_fan16_offset(offset) \
877static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
878 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
879 show_fan16, NULL, offset - 1); \
880static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
881 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
882 show_fan16_min, set_fan16_min, offset - 1)
883
884show_fan16_offset(1);
885show_fan16_offset(2);
886show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200887show_fan16_offset(4);
888show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +0200889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400891static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200894 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
Jean Delvare1d66c642005-04-18 21:16:59 -0700896static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Jean Delvare0124dd72007-11-25 16:16:41 +0100898static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
899 char *buf)
900{
901 int bitnr = to_sensor_dev_attr(attr)->index;
902 struct it87_data *data = it87_update_device(dev);
903 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
904}
905static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
906static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
907static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
908static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
909static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
910static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
911static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
912static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
913static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
914static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
915static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
916static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
917static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
918static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
919static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
920static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400923show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Jean Delvare90d66192007-10-08 18:24:35 +0200925 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +0100926 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400929store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200931 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 u32 val;
933
934 val = simple_strtoul(buf, NULL, 10);
935 data->vrm = val;
936
937 return count;
938}
939static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400942show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 struct it87_data *data = it87_update_device(dev);
945 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
946}
947static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +0200948
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200949static ssize_t show_name(struct device *dev, struct device_attribute
950 *devattr, char *buf)
951{
952 struct it87_data *data = dev_get_drvdata(dev);
953 return sprintf(buf, "%s\n", data->name);
954}
955static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
956
Jean Delvare87808be2006-09-24 21:17:13 +0200957static struct attribute *it87_attributes[] = {
958 &sensor_dev_attr_in0_input.dev_attr.attr,
959 &sensor_dev_attr_in1_input.dev_attr.attr,
960 &sensor_dev_attr_in2_input.dev_attr.attr,
961 &sensor_dev_attr_in3_input.dev_attr.attr,
962 &sensor_dev_attr_in4_input.dev_attr.attr,
963 &sensor_dev_attr_in5_input.dev_attr.attr,
964 &sensor_dev_attr_in6_input.dev_attr.attr,
965 &sensor_dev_attr_in7_input.dev_attr.attr,
966 &sensor_dev_attr_in8_input.dev_attr.attr,
967 &sensor_dev_attr_in0_min.dev_attr.attr,
968 &sensor_dev_attr_in1_min.dev_attr.attr,
969 &sensor_dev_attr_in2_min.dev_attr.attr,
970 &sensor_dev_attr_in3_min.dev_attr.attr,
971 &sensor_dev_attr_in4_min.dev_attr.attr,
972 &sensor_dev_attr_in5_min.dev_attr.attr,
973 &sensor_dev_attr_in6_min.dev_attr.attr,
974 &sensor_dev_attr_in7_min.dev_attr.attr,
975 &sensor_dev_attr_in0_max.dev_attr.attr,
976 &sensor_dev_attr_in1_max.dev_attr.attr,
977 &sensor_dev_attr_in2_max.dev_attr.attr,
978 &sensor_dev_attr_in3_max.dev_attr.attr,
979 &sensor_dev_attr_in4_max.dev_attr.attr,
980 &sensor_dev_attr_in5_max.dev_attr.attr,
981 &sensor_dev_attr_in6_max.dev_attr.attr,
982 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +0100983 &sensor_dev_attr_in0_alarm.dev_attr.attr,
984 &sensor_dev_attr_in1_alarm.dev_attr.attr,
985 &sensor_dev_attr_in2_alarm.dev_attr.attr,
986 &sensor_dev_attr_in3_alarm.dev_attr.attr,
987 &sensor_dev_attr_in4_alarm.dev_attr.attr,
988 &sensor_dev_attr_in5_alarm.dev_attr.attr,
989 &sensor_dev_attr_in6_alarm.dev_attr.attr,
990 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200991
992 &sensor_dev_attr_temp1_input.dev_attr.attr,
993 &sensor_dev_attr_temp2_input.dev_attr.attr,
994 &sensor_dev_attr_temp3_input.dev_attr.attr,
995 &sensor_dev_attr_temp1_max.dev_attr.attr,
996 &sensor_dev_attr_temp2_max.dev_attr.attr,
997 &sensor_dev_attr_temp3_max.dev_attr.attr,
998 &sensor_dev_attr_temp1_min.dev_attr.attr,
999 &sensor_dev_attr_temp2_min.dev_attr.attr,
1000 &sensor_dev_attr_temp3_min.dev_attr.attr,
1001 &sensor_dev_attr_temp1_type.dev_attr.attr,
1002 &sensor_dev_attr_temp2_type.dev_attr.attr,
1003 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001004 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1005 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1006 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001007
1008 &dev_attr_alarms.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001009 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001010 NULL
1011};
1012
1013static const struct attribute_group it87_group = {
1014 .attrs = it87_attributes,
1015};
1016
1017static struct attribute *it87_attributes_opt[] = {
1018 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1019 &sensor_dev_attr_fan1_min16.dev_attr.attr,
1020 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1021 &sensor_dev_attr_fan2_min16.dev_attr.attr,
1022 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1023 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001024 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1025 &sensor_dev_attr_fan4_min16.dev_attr.attr,
1026 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1027 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001028
1029 &sensor_dev_attr_fan1_input.dev_attr.attr,
1030 &sensor_dev_attr_fan1_min.dev_attr.attr,
1031 &sensor_dev_attr_fan1_div.dev_attr.attr,
1032 &sensor_dev_attr_fan2_input.dev_attr.attr,
1033 &sensor_dev_attr_fan2_min.dev_attr.attr,
1034 &sensor_dev_attr_fan2_div.dev_attr.attr,
1035 &sensor_dev_attr_fan3_input.dev_attr.attr,
1036 &sensor_dev_attr_fan3_min.dev_attr.attr,
1037 &sensor_dev_attr_fan3_div.dev_attr.attr,
1038
Jean Delvare0124dd72007-11-25 16:16:41 +01001039 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1040 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1041 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1042 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1043 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1044
Jean Delvare87808be2006-09-24 21:17:13 +02001045 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1046 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1047 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1048 &sensor_dev_attr_pwm1.dev_attr.attr,
1049 &sensor_dev_attr_pwm2.dev_attr.attr,
1050 &sensor_dev_attr_pwm3.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001051 &dev_attr_pwm1_freq.attr,
1052 &dev_attr_pwm2_freq.attr,
1053 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001054 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1055 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1056 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001057
1058 &dev_attr_vrm.attr,
1059 &dev_attr_cpu0_vid.attr,
1060 NULL
1061};
1062
1063static const struct attribute_group it87_group_opt = {
1064 .attrs = it87_attributes_opt,
1065};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Jean Delvare2d8672c2005-07-19 23:56:35 +02001067/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001068static int __init it87_find(unsigned short *address,
1069 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 int err = -ENODEV;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001072 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001073 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 superio_enter();
Jean Delvare67b671b2007-12-06 23:13:42 +01001076 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001077
1078 switch (chip_type) {
1079 case IT8705F_DEVID:
1080 sio_data->type = it87;
1081 break;
1082 case IT8712F_DEVID:
1083 sio_data->type = it8712;
1084 break;
1085 case IT8716F_DEVID:
1086 case IT8726F_DEVID:
1087 sio_data->type = it8716;
1088 break;
1089 case IT8718F_DEVID:
1090 sio_data->type = it8718;
1091 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001092 case IT8720F_DEVID:
1093 sio_data->type = it8720;
1094 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001095 case 0xffff: /* No device at all */
1096 goto exit;
1097 default:
1098 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
1099 chip_type);
1100 goto exit;
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Jean Delvare87673dd2006-08-28 14:37:19 +02001103 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1105 pr_info("it87: Device not activated, skipping\n");
1106 goto exit;
1107 }
1108
1109 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1110 if (*address == 0) {
1111 pr_info("it87: Base address not set, skipping\n");
1112 goto exit;
1113 }
1114
1115 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001116 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001118 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Jean Delvare87673dd2006-08-28 14:37:19 +02001120 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001121 if (sio_data->type == it87) {
1122 /* The IT8705F doesn't have VID pins at all */
1123 sio_data->skip_vid = 1;
1124 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001125 int reg;
1126
1127 superio_select(GPIO);
Jean Delvare895ff262009-12-09 20:35:47 +01001128 /* We need at least 4 VID pins */
1129 reg = superio_inb(IT87_SIO_GPIO3_REG);
1130 if (reg & 0x0f) {
1131 pr_info("it87: VID is disabled (pins used for GPIO)\n");
1132 sio_data->skip_vid = 1;
1133 }
1134
Jean Delvare591ec652009-12-09 20:35:48 +01001135 /* Check if fan3 is there or not */
1136 if (reg & (1 << 6))
1137 sio_data->skip_pwm |= (1 << 2);
1138 if (reg & (1 << 7))
1139 sio_data->skip_fan |= (1 << 2);
1140
1141 /* Check if fan2 is there or not */
1142 reg = superio_inb(IT87_SIO_GPIO5_REG);
1143 if (reg & (1 << 1))
1144 sio_data->skip_pwm |= (1 << 1);
1145 if (reg & (1 << 2))
1146 sio_data->skip_fan |= (1 << 1);
1147
Jean Delvare895ff262009-12-09 20:35:47 +01001148 if ((sio_data->type == it8718 || sio_data->type == it8720)
1149 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001150 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001151
1152 reg = superio_inb(IT87_SIO_PINX2_REG);
1153 if (reg & (1 << 0))
1154 pr_info("it87: in3 is VCC (+5V)\n");
1155 if (reg & (1 << 1))
1156 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
1157 }
1158
Jean Delvare98dd22c2008-10-09 15:33:58 +02001159 /* Disable specific features based on DMI strings */
1160 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1161 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1162 if (board_vendor && board_name) {
1163 if (strcmp(board_vendor, "nVIDIA") == 0
1164 && strcmp(board_name, "FN68PT") == 0) {
1165 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1166 connected to a fan, but to something else. One user
1167 has reported instant system power-off when changing
1168 the PWM2 duty cycle, so we disable it.
1169 I use the board name string as the trigger in case
1170 the same board is ever used in other systems. */
1171 pr_info("it87: Disabling pwm2 due to "
1172 "hardware constraints\n");
1173 sio_data->skip_pwm = (1 << 1);
1174 }
1175 }
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177exit:
1178 superio_exit();
1179 return err;
1180}
1181
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001182static int __devinit it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001185 struct resource *res;
1186 struct device *dev = &pdev->dev;
1187 struct it87_sio_data *sio_data = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int enable_pwm_interface;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001190 static const char *names[] = {
1191 "it87",
1192 "it8712",
1193 "it8716",
1194 "it8718",
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001195 "it8720",
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001196 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001198 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001199 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001200 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1201 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001202 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001203 err = -EBUSY;
1204 goto ERROR0;
1205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001207 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 err = -ENOMEM;
1209 goto ERROR1;
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001212 data->addr = res->start;
1213 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001214 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001215 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001218 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1219 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001220 err = -ENODEV;
1221 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001224 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001226 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001229 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001232 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /* Register sysfs hooks */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001235 if ((err = sysfs_create_group(&dev->kobj, &it87_group)))
1236 goto ERROR2;
Jean Delvare17d648b2006-08-28 14:23:46 +02001237
Jean Delvare9060f8b2006-08-28 14:24:17 +02001238 /* Do not create fan files for disabled fans */
Andrew Paprocki04751692008-08-06 22:41:06 +02001239 if (has_16bit_fans(data)) {
Jean Delvare87673dd2006-08-28 14:37:19 +02001240 /* 16-bit tachometers */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001241 if (data->has_fan & (1 << 0)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001242 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001243 &sensor_dev_attr_fan1_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001244 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001245 &sensor_dev_attr_fan1_min16.dev_attr))
1246 || (err = device_create_file(dev,
1247 &sensor_dev_attr_fan1_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001248 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001249 }
1250 if (data->has_fan & (1 << 1)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001251 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001252 &sensor_dev_attr_fan2_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001253 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001254 &sensor_dev_attr_fan2_min16.dev_attr))
1255 || (err = device_create_file(dev,
1256 &sensor_dev_attr_fan2_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001257 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001258 }
1259 if (data->has_fan & (1 << 2)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001260 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001261 &sensor_dev_attr_fan3_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001262 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001263 &sensor_dev_attr_fan3_min16.dev_attr))
1264 || (err = device_create_file(dev,
1265 &sensor_dev_attr_fan3_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001266 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001267 }
Jean Delvarec7f1f712007-09-03 17:11:46 +02001268 if (data->has_fan & (1 << 3)) {
1269 if ((err = device_create_file(dev,
1270 &sensor_dev_attr_fan4_input16.dev_attr))
1271 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001272 &sensor_dev_attr_fan4_min16.dev_attr))
1273 || (err = device_create_file(dev,
1274 &sensor_dev_attr_fan4_alarm.dev_attr)))
Jean Delvarec7f1f712007-09-03 17:11:46 +02001275 goto ERROR4;
1276 }
1277 if (data->has_fan & (1 << 4)) {
1278 if ((err = device_create_file(dev,
1279 &sensor_dev_attr_fan5_input16.dev_attr))
1280 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001281 &sensor_dev_attr_fan5_min16.dev_attr))
1282 || (err = device_create_file(dev,
1283 &sensor_dev_attr_fan5_alarm.dev_attr)))
Jean Delvarec7f1f712007-09-03 17:11:46 +02001284 goto ERROR4;
1285 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001286 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001287 /* 8-bit tachometers with clock divider */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001288 if (data->has_fan & (1 << 0)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001289 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001290 &sensor_dev_attr_fan1_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001291 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001292 &sensor_dev_attr_fan1_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001293 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001294 &sensor_dev_attr_fan1_div.dev_attr))
1295 || (err = device_create_file(dev,
1296 &sensor_dev_attr_fan1_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001297 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001298 }
1299 if (data->has_fan & (1 << 1)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001300 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001301 &sensor_dev_attr_fan2_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001302 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001303 &sensor_dev_attr_fan2_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001304 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001305 &sensor_dev_attr_fan2_div.dev_attr))
1306 || (err = device_create_file(dev,
1307 &sensor_dev_attr_fan2_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001308 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001309 }
1310 if (data->has_fan & (1 << 2)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001311 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001312 &sensor_dev_attr_fan3_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001313 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001314 &sensor_dev_attr_fan3_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001315 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001316 &sensor_dev_attr_fan3_div.dev_attr))
1317 || (err = device_create_file(dev,
1318 &sensor_dev_attr_fan3_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001319 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001320 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001321 }
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (enable_pwm_interface) {
Jean Delvare98dd22c2008-10-09 15:33:58 +02001324 if (!(sio_data->skip_pwm & (1 << 0))) {
1325 if ((err = device_create_file(dev,
1326 &sensor_dev_attr_pwm1_enable.dev_attr))
1327 || (err = device_create_file(dev,
1328 &sensor_dev_attr_pwm1.dev_attr))
1329 || (err = device_create_file(dev,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001330 &dev_attr_pwm1_freq))
1331 || (err = device_create_file(dev,
1332 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr)))
Jean Delvare98dd22c2008-10-09 15:33:58 +02001333 goto ERROR4;
1334 }
1335 if (!(sio_data->skip_pwm & (1 << 1))) {
1336 if ((err = device_create_file(dev,
1337 &sensor_dev_attr_pwm2_enable.dev_attr))
1338 || (err = device_create_file(dev,
1339 &sensor_dev_attr_pwm2.dev_attr))
1340 || (err = device_create_file(dev,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001341 &dev_attr_pwm2_freq))
1342 || (err = device_create_file(dev,
1343 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr)))
Jean Delvare98dd22c2008-10-09 15:33:58 +02001344 goto ERROR4;
1345 }
1346 if (!(sio_data->skip_pwm & (1 << 2))) {
1347 if ((err = device_create_file(dev,
1348 &sensor_dev_attr_pwm3_enable.dev_attr))
1349 || (err = device_create_file(dev,
1350 &sensor_dev_attr_pwm3.dev_attr))
1351 || (err = device_create_file(dev,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001352 &dev_attr_pwm3_freq))
1353 || (err = device_create_file(dev,
1354 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr)))
Jean Delvare98dd22c2008-10-09 15:33:58 +02001355 goto ERROR4;
1356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358
Jean Delvare895ff262009-12-09 20:35:47 +01001359 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02001360 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001361 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001362 data->vid = sio_data->vid_value;
1363 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001364 &dev_attr_vrm))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001365 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001366 &dev_attr_cpu0_vid)))
1367 goto ERROR4;
1368 }
1369
Tony Jones1beeffe2007-08-20 13:46:20 -07001370 data->hwmon_dev = hwmon_device_register(dev);
1371 if (IS_ERR(data->hwmon_dev)) {
1372 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001373 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375
1376 return 0;
1377
Jean Delvare87808be2006-09-24 21:17:13 +02001378ERROR4:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001379 sysfs_remove_group(&dev->kobj, &it87_group);
1380 sysfs_remove_group(&dev->kobj, &it87_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381ERROR2:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001382 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 kfree(data);
1384ERROR1:
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001385 release_region(res->start, IT87_EC_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386ERROR0:
1387 return err;
1388}
1389
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001390static int __devexit it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001392 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Tony Jones1beeffe2007-08-20 13:46:20 -07001394 hwmon_device_unregister(data->hwmon_dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001395 sysfs_remove_group(&pdev->dev.kobj, &it87_group);
1396 sysfs_remove_group(&pdev->dev.kobj, &it87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001397
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001398 release_region(data->addr, IT87_EC_EXTENT);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001399 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001400 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 return 0;
1403}
1404
Jean Delvare7f999aa2007-02-14 21:15:03 +01001405/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1407 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001408static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001410 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1411 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Jean Delvare7f999aa2007-02-14 21:15:03 +01001414/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1416 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001417static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001419 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1420 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
1423/* Return 1 if and only if the PWM interface is safe to use */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001424static int __devinit it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001426 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1428 * and polarity set to active low is sign that this is the case so we
1429 * disable pwm control to protect the user. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001430 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if ((tmp & 0x87) == 0) {
1432 if (fix_pwm_polarity) {
1433 /* The user asks us to attempt a chip reconfiguration.
1434 * This means switching to active high polarity and
1435 * inverting all fan speed values. */
1436 int i;
1437 u8 pwm[3];
1438
1439 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001440 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 IT87_REG_PWM(i));
1442
1443 /* If any fan is in automatic pwm mode, the polarity
1444 * might be correct, as suspicious as it seems, so we
1445 * better don't change anything (but still disable the
1446 * PWM interface). */
1447 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001448 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001450 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 tmp | 0x87);
1452 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001453 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 IT87_REG_PWM(i),
1455 0x7f & ~pwm[i]);
1456 return 1;
1457 }
1458
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001459 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 "too broken to be fixed\n");
1461 }
1462
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001463 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 "defaults, disabling PWM interface\n");
1465 return 0;
1466 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001467 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 "sane, won't touch\n");
1469 }
1470
1471 return 1;
1472}
1473
1474/* Called when we have found a new IT87. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001475static void __devinit it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Jean Delvare591ec652009-12-09 20:35:48 +01001477 struct it87_sio_data *sio_data = pdev->dev.platform_data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001478 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01001480 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Jean Delvareb99883d2010-03-05 22:17:15 +01001482 /* For each PWM channel:
1483 * - If it is in automatic mode, setting to manual mode should set
1484 * the fan to full speed by default.
1485 * - If it is in manual mode, we need a mapping to temperature
1486 * channels to use when later setting to automatic mode later.
1487 * Use a 1:1 mapping by default (we are clueless.)
1488 * In both cases, the value can (and should) be changed by the user
1489 * prior to switching to a different mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01001491 data->pwm_temp_map[i] = i;
1492 data->pwm_duty[i] = 0x7f; /* Full speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494
Jean Delvarec5df9b72006-08-28 14:37:54 +02001495 /* Some chips seem to have default value 0xff for all limit
1496 * registers. For low voltage limits it makes no sense and triggers
1497 * alarms, so change to 0 instead. For high temperature limits, it
1498 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1499 * but is still confusing, so change to 127 degrees C. */
1500 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001501 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001502 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001503 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001504 }
1505 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001506 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001507 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001508 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001509 }
1510
Jean Delvare77fa49d2009-01-07 16:37:35 +01001511 /* Check if temperature channels are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001512 tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if ((tmp & 0x3f) == 0) {
1514 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1515 tmp = (tmp & 0xc0) | 0x2a;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001516 it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518 data->sensor = tmp;
1519
1520 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001521 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if ((tmp & 0xff) == 0) {
1523 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001524 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526
1527 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01001528 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001529 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01001530 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01001532 data->fan_main_ctrl |= mask;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001533 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001535 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Jean Delvare17d648b2006-08-28 14:23:46 +02001537 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02001538 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001539 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001540 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001541 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02001542 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001543 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02001544 tmp | 0x07);
1545 }
Andrew Paprocki816d8c62008-08-06 22:41:06 +02001546 /* IT8705F only supports three fans. */
1547 if (data->type != it87) {
1548 if (tmp & (1 << 4))
1549 data->has_fan |= (1 << 3); /* fan4 enabled */
1550 if (tmp & (1 << 5))
1551 data->has_fan |= (1 << 4); /* fan5 enabled */
1552 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001553 }
1554
Jean Delvare591ec652009-12-09 20:35:48 +01001555 /* Fan input pins may be used for alternative functions */
1556 data->has_fan &= ~sio_data->skip_fan;
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001559 it87_write_value(data, IT87_REG_CONFIG,
1560 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 | (update_vbat ? 0x41 : 0x01));
1562}
1563
Jean Delvareb99883d2010-03-05 22:17:15 +01001564static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
1565{
1566 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
1567 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
1568 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
1569 else /* Manual mode */
1570 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
1571}
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573static struct it87_data *it87_update_device(struct device *dev)
1574{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001575 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 int i;
1577
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001578 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1581 || !data->valid) {
1582
1583 if (update_vbat) {
1584 /* Cleared after each update, so reenable. Value
1585 returned by this read will be previous value */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001586 it87_write_value(data, IT87_REG_CONFIG,
1587 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589 for (i = 0; i <= 7; i++) {
1590 data->in[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001591 it87_read_value(data, IT87_REG_VIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 data->in_min[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001593 it87_read_value(data, IT87_REG_VIN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 data->in_max[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001595 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Jean Delvare3543a532006-08-28 14:27:25 +02001597 /* in8 (battery) has no limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 data->in[8] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001599 it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Jean Delvarec7f1f712007-09-03 17:11:46 +02001601 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02001602 /* Skip disabled fans */
1603 if (!(data->has_fan & (1 << i)))
1604 continue;
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 data->fan_min[i] =
Jean Delvarec7f1f712007-09-03 17:11:46 +02001607 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001608 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001609 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02001610 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02001611 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001612 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001613 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001614 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001615 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02001616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618 for (i = 0; i < 3; i++) {
1619 data->temp[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001620 it87_read_value(data, IT87_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 data->temp_high[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001622 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 data->temp_low[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001624 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626
Jean Delvare17d648b2006-08-28 14:23:46 +02001627 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02001628 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001629 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02001630 data->fan_div[0] = i & 0x07;
1631 data->fan_div[1] = (i >> 3) & 0x07;
1632 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001636 it87_read_value(data, IT87_REG_ALARM1) |
1637 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1638 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvareb99883d2010-03-05 22:17:15 +01001639
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001640 data->fan_main_ctrl = it87_read_value(data,
1641 IT87_REG_FAN_MAIN_CTRL);
1642 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01001643 for (i = 0; i < 3; i++)
1644 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001646 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Andrew Paprocki04751692008-08-06 22:41:06 +02001647 /* The 8705 does not have VID capability.
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001648 The 8718 and the 8720 don't use IT87_REG_VID for the
1649 same purpose. */
Jean Delvare17d648b2006-08-28 14:23:46 +02001650 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001651 data->vid = it87_read_value(data, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02001652 /* The older IT8712F revisions had only 5 VID pins,
1653 but we assume it is always safe to read 6 bits. */
1654 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
1656 data->last_updated = jiffies;
1657 data->valid = 1;
1658 }
1659
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001660 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 return data;
1663}
1664
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001665static int __init it87_device_add(unsigned short address,
1666 const struct it87_sio_data *sio_data)
1667{
1668 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001669 .start = address + IT87_EC_OFFSET,
1670 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001671 .name = DRVNAME,
1672 .flags = IORESOURCE_IO,
1673 };
1674 int err;
1675
Jean Delvareb9acb642009-01-07 16:37:35 +01001676 err = acpi_check_resource_conflict(&res);
1677 if (err)
1678 goto exit;
1679
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001680 pdev = platform_device_alloc(DRVNAME, address);
1681 if (!pdev) {
1682 err = -ENOMEM;
1683 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1684 goto exit;
1685 }
1686
1687 err = platform_device_add_resources(pdev, &res, 1);
1688 if (err) {
1689 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1690 "(%d)\n", err);
1691 goto exit_device_put;
1692 }
1693
1694 err = platform_device_add_data(pdev, sio_data,
1695 sizeof(struct it87_sio_data));
1696 if (err) {
1697 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1698 goto exit_device_put;
1699 }
1700
1701 err = platform_device_add(pdev);
1702 if (err) {
1703 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1704 err);
1705 goto exit_device_put;
1706 }
1707
1708 return 0;
1709
1710exit_device_put:
1711 platform_device_put(pdev);
1712exit:
1713 return err;
1714}
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716static int __init sm_it87_init(void)
1717{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001718 int err;
1719 unsigned short isa_address=0;
1720 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02001721
Jean Delvare98dd22c2008-10-09 15:33:58 +02001722 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001723 err = it87_find(&isa_address, &sio_data);
1724 if (err)
1725 return err;
1726 err = platform_driver_register(&it87_driver);
1727 if (err)
1728 return err;
1729
1730 err = it87_device_add(isa_address, &sio_data);
1731 if (err){
1732 platform_driver_unregister(&it87_driver);
1733 return err;
1734 }
1735
1736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
1739static void __exit sm_it87_exit(void)
1740{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001741 platform_device_unregister(pdev);
1742 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
1745
Jean Delvaref1d8e332007-11-25 16:14:44 +01001746MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02001747 "Jean Delvare <khali@linux-fr.org>");
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001748MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8720F/8726F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749module_param(update_vbat, bool, 0);
1750MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1751module_param(fix_pwm_polarity, bool, 0);
1752MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1753MODULE_LICENSE("GPL");
1754
1755module_init(sm_it87_init);
1756module_exit(sm_it87_exit);