blob: d793cc0119908abd5494ed9f531a27f3dbbb478b [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
Rudolf Marek08a8f6e2007-06-09 10:11:16 -040017 IT8726F Super I/O chip w/LPC interface
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 Sis950 A clone of the IT8705F
19
Jean Delvaref1d8e332007-11-25 16:14:44 +010020 Copyright (C) 2001 Chris Gauthron
Jean Delvare0124dd72007-11-25 16:16:41 +010021 Copyright (C) 2005-2007 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23 This program is free software; you can redistribute it and/or modify
24 it under the terms of the GNU General Public License as published by
25 the Free Software Foundation; either version 2 of the License, or
26 (at your option) any later version.
27
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
32
33 You should have received a copy of the GNU General Public License
34 along with this program; if not, write to the Free Software
35 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36*/
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/slab.h>
41#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020042#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040043#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020044#include <linux/hwmon-sysfs.h>
45#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040046#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010047#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020048#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020049#include <linux/string.h>
50#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/io.h>
52
corentin.labbeb74f3fd2007-06-13 20:27:36 +020053#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jean Delvare8e9afcb2006-12-12 18:18:28 +010055enum chips { it87, it8712, it8716, it8718 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jean Delvare67b671b2007-12-06 23:13:42 +010057static unsigned short force_id;
58module_param(force_id, ushort, 0);
59MODULE_PARM_DESC(force_id, "Override the detected device ID");
60
corentin.labbeb74f3fd2007-06-13 20:27:36 +020061static struct platform_device *pdev;
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define REG 0x2e /* The register to read/write */
64#define DEV 0x07 /* Register: Logical device select */
65#define VAL 0x2f /* The value to read/write */
66#define PME 0x04 /* The device with the fan registers in it */
Jean Delvare87673dd2006-08-28 14:37:19 +020067#define GPIO 0x07 /* The device with the IT8718F VID value in it */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define DEVID 0x20 /* Register: Device ID */
69#define DEVREV 0x22 /* Register: Device Revision */
70
71static inline int
72superio_inb(int reg)
73{
74 outb(reg, REG);
75 return inb(VAL);
76}
77
78static int superio_inw(int reg)
79{
80 int val;
81 outb(reg++, REG);
82 val = inb(VAL) << 8;
83 outb(reg, REG);
84 val |= inb(VAL);
85 return val;
86}
87
88static inline void
Jean Delvare87673dd2006-08-28 14:37:19 +020089superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +020092 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95static inline void
96superio_enter(void)
97{
98 outb(0x87, REG);
99 outb(0x01, REG);
100 outb(0x55, REG);
101 outb(0x55, REG);
102}
103
104static inline void
105superio_exit(void)
106{
107 outb(0x02, REG);
108 outb(0x02, VAL);
109}
110
Jean Delvare87673dd2006-08-28 14:37:19 +0200111/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define IT8712F_DEVID 0x8712
113#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200114#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200115#define IT8718F_DEVID 0x8718
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400116#define IT8726F_DEVID 0x8726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define IT87_ACT_REG 0x30
118#define IT87_BASE_REG 0x60
119
Jean Delvare87673dd2006-08-28 14:37:19 +0200120/* Logical device 7 registers (IT8712F and later) */
121#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
122#define IT87_SIO_VID_REG 0xfc /* VID value */
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/* Update battery voltage after every reading if true */
125static int update_vbat;
126
127/* Not all BIOSes properly configure the PWM registers */
128static int fix_pwm_polarity;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/* Many IT87 constants specified below */
131
132/* Length of ISA address segment */
133#define IT87_EXTENT 8
134
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500135/* Length of ISA address segment for Environmental Controller */
136#define IT87_EC_EXTENT 2
137
138/* Offset of EC registers from ISA base address */
139#define IT87_EC_OFFSET 5
140
141/* Where are the ISA address/data registers relative to the EC base address */
142#define IT87_ADDR_REG_OFFSET 0
143#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145/*----- The IT87 registers -----*/
146
147#define IT87_REG_CONFIG 0x00
148
149#define IT87_REG_ALARM1 0x01
150#define IT87_REG_ALARM2 0x02
151#define IT87_REG_ALARM3 0x03
152
Jean Delvare87673dd2006-08-28 14:37:19 +0200153/* The IT8718F has the VID value in a different register, in Super-I/O
154 configuration space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define IT87_REG_VID 0x0a
Andrew Paprocki04751692008-08-06 22:41:06 +0200156/* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
157 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
158 mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200160#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
163
Jean Delvarec7f1f712007-09-03 17:11:46 +0200164static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
165static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
166static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
167static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define IT87_REG_FAN_MAIN_CTRL 0x13
169#define IT87_REG_FAN_CTL 0x14
170#define IT87_REG_PWM(nr) (0x15 + (nr))
171
172#define IT87_REG_VIN(nr) (0x20 + (nr))
173#define IT87_REG_TEMP(nr) (0x29 + (nr))
174
175#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
176#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
177#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
178#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#define IT87_REG_VIN_ENABLE 0x50
181#define IT87_REG_TEMP_ENABLE 0x51
182
183#define IT87_REG_CHIPID 0x58
184
185#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
186#define IN_FROM_REG(val) ((val) * 16)
187
188static inline u8 FAN_TO_REG(long rpm, int div)
189{
190 if (rpm == 0)
191 return 255;
192 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
193 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
194 254);
195}
196
Jean Delvare17d648b2006-08-28 14:23:46 +0200197static inline u16 FAN16_TO_REG(long rpm)
198{
199 if (rpm == 0)
200 return 0xffff;
201 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
Jean Delvare17d648b2006-08-28 14:23:46 +0200205/* The divider is fixed to 2 in 16-bit mode */
206#define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
209 ((val)+500)/1000),-128,127))
210#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#define PWM_TO_REG(val) ((val) >> 1)
213#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
214
215static int DIV_TO_REG(int val)
216{
217 int answer = 0;
Jean Delvareb9e349f2006-08-28 14:26:22 +0200218 while (answer < 7 && (val >>= 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 answer++;
220 return answer;
221}
222#define DIV_FROM_REG(val) (1 << (val))
223
Jean Delvaref8d0c192007-02-14 21:15:02 +0100224static const unsigned int pwm_freq[8] = {
225 48000000 / 128,
226 24000000 / 128,
227 12000000 / 128,
228 8000000 / 128,
229 6000000 / 128,
230 3000000 / 128,
231 1500000 / 128,
232 750000 / 128,
233};
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200236struct it87_sio_data {
237 enum chips type;
238 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200239 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200240 u8 vid_value;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200241 /* Values set based on DMI strings */
242 u8 skip_pwm;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200243};
244
Jean Delvareed6bafb2007-02-14 21:15:03 +0100245/* For each registered chip, we need to keep some data in memory.
246 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700248 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 enum chips type;
Andrew Paprocki04751692008-08-06 22:41:06 +0200250 u8 revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200252 unsigned short addr;
253 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100254 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 char valid; /* !=0 if following fields are valid */
256 unsigned long last_updated; /* In jiffies */
257
258 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200259 u8 in_max[8]; /* Register value */
260 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200261 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvarec7f1f712007-09-03 17:11:46 +0200262 u16 fan[5]; /* Register values, possibly combined */
263 u16 fan_min[5]; /* Register values, possibly combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 u8 temp[3]; /* Register value */
265 u8 temp_high[3]; /* Register value */
266 u8 temp_low[3]; /* Register value */
267 u8 sensor; /* Register value */
268 u8 fan_div[3]; /* Register encoding, shifted right */
269 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100270 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 u32 alarms; /* Register encoding, combined */
272 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100273 u8 fan_ctl; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
275};
276
Andrew Paprocki04751692008-08-06 22:41:06 +0200277static inline int has_16bit_fans(const struct it87_data *data)
278{
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200279 /* IT8705F Datasheet 0.4.1, 3h == Version G.
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200280 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200281 These are the first revisions with 16bit tachometer support. */
282 return (data->type == it87 && data->revision >= 0x03)
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200283 || (data->type == it8712 && data->revision >= 0x08)
Andrew Paprocki04751692008-08-06 22:41:06 +0200284 || data->type == it8716
285 || data->type == it8718;
286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200288static int it87_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200289static int __devexit it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200291static int it87_read_value(struct it87_data *data, u8 reg);
292static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200294static int it87_check_pwm(struct device *dev);
295static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200298static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100299 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200300 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200301 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100302 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200303 .probe = it87_probe,
304 .remove = __devexit_p(it87_remove),
Jean Delvarefde09502005-07-19 23:51:07 +0200305};
306
Jean Delvare20ad93d2005-06-05 11:53:25 +0200307static ssize_t show_in(struct device *dev, struct device_attribute *attr,
308 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200310 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
311 int nr = sensor_attr->index;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct it87_data *data = it87_update_device(dev);
314 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
315}
316
Jean Delvare20ad93d2005-06-05 11:53:25 +0200317static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
318 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200320 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
321 int nr = sensor_attr->index;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct it87_data *data = it87_update_device(dev);
324 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
325}
326
Jean Delvare20ad93d2005-06-05 11:53:25 +0200327static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
328 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200330 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
331 int nr = sensor_attr->index;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct it87_data *data = it87_update_device(dev);
334 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
335}
336
Jean Delvare20ad93d2005-06-05 11:53:25 +0200337static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
338 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200340 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
341 int nr = sensor_attr->index;
342
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200343 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 unsigned long val = simple_strtoul(buf, NULL, 10);
345
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100346 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 data->in_min[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200348 it87_write_value(data, IT87_REG_VIN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100350 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return count;
352}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200353static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
354 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200356 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
357 int nr = sensor_attr->index;
358
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200359 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 unsigned long val = simple_strtoul(buf, NULL, 10);
361
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100362 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 data->in_max[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200364 it87_write_value(data, IT87_REG_VIN_MAX(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100366 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return count;
368}
369
370#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200371static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
372 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200375static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
376 show_in_min, set_in_min, offset); \
377static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
378 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380show_in_offset(0);
381limit_in_offset(0);
382show_in_offset(1);
383limit_in_offset(1);
384show_in_offset(2);
385limit_in_offset(2);
386show_in_offset(3);
387limit_in_offset(3);
388show_in_offset(4);
389limit_in_offset(4);
390show_in_offset(5);
391limit_in_offset(5);
392show_in_offset(6);
393limit_in_offset(6);
394show_in_offset(7);
395limit_in_offset(7);
396show_in_offset(8);
397
398/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200399static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
400 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200402 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
403 int nr = sensor_attr->index;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct it87_data *data = it87_update_device(dev);
406 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
407}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200408static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
409 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200411 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
412 int nr = sensor_attr->index;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct it87_data *data = it87_update_device(dev);
415 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
416}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200417static ssize_t show_temp_min(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_low[nr]));
425}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200426static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
427 const char *buf, size_t count)
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
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200432 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int val = simple_strtol(buf, NULL, 10);
434
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100435 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 data->temp_high[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200437 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100438 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return count;
440}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200441static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
442 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200444 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
445 int nr = sensor_attr->index;
446
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200447 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int val = simple_strtol(buf, NULL, 10);
449
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100450 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 data->temp_low[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200452 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100453 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return count;
455}
456#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200457static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
458 show_temp, NULL, offset - 1); \
459static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
460 show_temp_max, set_temp_max, offset - 1); \
461static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
462 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464show_temp_offset(1);
465show_temp_offset(2);
466show_temp_offset(3);
467
Jean Delvare20ad93d2005-06-05 11:53:25 +0200468static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
469 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200471 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
472 int nr = sensor_attr->index;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct it87_data *data = it87_update_device(dev);
475 u8 reg = data->sensor; /* In case the value is updated while we use it */
476
477 if (reg & (1 << nr))
478 return sprintf(buf, "3\n"); /* thermal diode */
479 if (reg & (8 << nr))
480 return sprintf(buf, "2\n"); /* thermistor */
481 return sprintf(buf, "0\n"); /* disabled */
482}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200483static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
484 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200486 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
487 int nr = sensor_attr->index;
488
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200489 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int val = simple_strtol(buf, NULL, 10);
491
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100492 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 data->sensor &= ~(1 << nr);
495 data->sensor &= ~(8 << nr);
496 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
497 if (val == 3)
498 data->sensor |= 1 << nr;
499 else if (val == 2)
500 data->sensor |= 8 << nr;
501 else if (val != 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100502 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return -EINVAL;
504 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200505 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100506 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return count;
508}
509#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200510static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
511 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513show_sensor_offset(1);
514show_sensor_offset(2);
515show_sensor_offset(3);
516
517/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200518static ssize_t show_fan(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);
525 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
526 DIV_FROM_REG(data->fan_div[nr])));
527}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200528static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
529 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200531 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
532 int nr = sensor_attr->index;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct it87_data *data = it87_update_device(dev);
535 return sprintf(buf,"%d\n",
536 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
537}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200538static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
539 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200541 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
542 int nr = sensor_attr->index;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct it87_data *data = it87_update_device(dev);
545 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
546}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200547static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
548 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200550 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
551 int nr = sensor_attr->index;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct it87_data *data = it87_update_device(dev);
554 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
555}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200556static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
557 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200559 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
560 int nr = sensor_attr->index;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct it87_data *data = it87_update_device(dev);
563 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
564}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100565static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
566 char *buf)
567{
568 struct it87_data *data = it87_update_device(dev);
569 int index = (data->fan_ctl >> 4) & 0x07;
570
571 return sprintf(buf, "%u\n", pwm_freq[index]);
572}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200573static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
574 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200576 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
577 int nr = sensor_attr->index;
578
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200579 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int val = simple_strtol(buf, NULL, 10);
Jean Delvare7f999aa2007-02-14 21:15:03 +0100581 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100583 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200584 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800585 switch (nr) {
586 case 0: data->fan_div[nr] = reg & 0x07; break;
587 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
588 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
589 }
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200592 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100593 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return count;
595}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200596static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
597 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200599 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
600 int nr = sensor_attr->index;
601
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200602 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvareb9e349f2006-08-28 14:26:22 +0200603 unsigned long val = simple_strtoul(buf, NULL, 10);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200604 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 u8 old;
606
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100607 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200608 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200610 /* Save fan min limit */
611 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 switch (nr) {
614 case 0:
615 case 1:
616 data->fan_div[nr] = DIV_TO_REG(val);
617 break;
618 case 2:
619 if (val < 8)
620 data->fan_div[nr] = 1;
621 else
622 data->fan_div[nr] = 3;
623 }
624 val = old & 0x80;
625 val |= (data->fan_div[0] & 0x07);
626 val |= (data->fan_div[1] & 0x07) << 3;
627 if (data->fan_div[2] == 3)
628 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200629 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200631 /* Restore fan min limit */
632 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200633 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200634
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100635 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return count;
637}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200638static ssize_t set_pwm_enable(struct device *dev,
639 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200641 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
642 int nr = sensor_attr->index;
643
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200644 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 int val = simple_strtol(buf, NULL, 10);
646
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100647 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 if (val == 0) {
650 int tmp;
651 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200652 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
653 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* set on/off mode */
655 data->fan_main_ctrl &= ~(1 << nr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200656 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 } else if (val == 1) {
658 /* set SmartGuardian mode */
659 data->fan_main_ctrl |= (1 << nr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200660 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200662 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 } else {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100664 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -EINVAL;
666 }
667
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100668 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return count;
670}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200671static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
672 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200674 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
675 int nr = sensor_attr->index;
676
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200677 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int val = simple_strtol(buf, NULL, 10);
679
680 if (val < 0 || val > 255)
681 return -EINVAL;
682
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100683 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 data->manual_pwm_ctl[nr] = val;
685 if (data->fan_main_ctrl & (1 << nr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200686 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100687 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return count;
689}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100690static ssize_t set_pwm_freq(struct device *dev,
691 struct device_attribute *attr, const char *buf, size_t count)
692{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200693 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100694 unsigned long val = simple_strtoul(buf, NULL, 10);
695 int i;
696
697 /* Search for the nearest available frequency */
698 for (i = 0; i < 7; i++) {
699 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
700 break;
701 }
702
703 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200704 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100705 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200706 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100707 mutex_unlock(&data->update_lock);
708
709 return count;
710}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Jean Delvare20ad93d2005-06-05 11:53:25 +0200712#define show_fan_offset(offset) \
713static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
714 show_fan, NULL, offset - 1); \
715static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
716 show_fan_min, set_fan_min, offset - 1); \
717static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
718 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720show_fan_offset(1);
721show_fan_offset(2);
722show_fan_offset(3);
723
724#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200725static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
726 show_pwm_enable, set_pwm_enable, offset - 1); \
727static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +0100728 show_pwm, set_pwm, offset - 1); \
729static DEVICE_ATTR(pwm##offset##_freq, \
730 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
731 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733show_pwm_offset(1);
734show_pwm_offset(2);
735show_pwm_offset(3);
736
Jean Delvare17d648b2006-08-28 14:23:46 +0200737/* A different set of callbacks for 16-bit fans */
738static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
739 char *buf)
740{
741 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
742 int nr = sensor_attr->index;
743 struct it87_data *data = it87_update_device(dev);
744 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
745}
746
747static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
748 char *buf)
749{
750 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
751 int nr = sensor_attr->index;
752 struct it87_data *data = it87_update_device(dev);
753 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
754}
755
756static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
757 const char *buf, size_t count)
758{
759 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
760 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200761 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare17d648b2006-08-28 14:23:46 +0200762 int val = simple_strtol(buf, NULL, 10);
763
764 mutex_lock(&data->update_lock);
765 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200766 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +0200767 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200768 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +0200769 data->fan_min[nr] >> 8);
770 mutex_unlock(&data->update_lock);
771 return count;
772}
773
774/* We want to use the same sysfs file names as 8-bit fans, but we need
775 different variable names, so we have to use SENSOR_ATTR instead of
776 SENSOR_DEVICE_ATTR. */
777#define show_fan16_offset(offset) \
778static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
779 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
780 show_fan16, NULL, offset - 1); \
781static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
782 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
783 show_fan16_min, set_fan16_min, offset - 1)
784
785show_fan16_offset(1);
786show_fan16_offset(2);
787show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200788show_fan16_offset(4);
789show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +0200790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400792static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200795 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
Jean Delvare1d66c642005-04-18 21:16:59 -0700797static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Jean Delvare0124dd72007-11-25 16:16:41 +0100799static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
800 char *buf)
801{
802 int bitnr = to_sensor_dev_attr(attr)->index;
803 struct it87_data *data = it87_update_device(dev);
804 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
805}
806static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
807static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
808static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
809static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
810static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
811static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
812static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
813static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
814static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
815static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
816static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
817static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
818static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
819static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
820static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
821static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400824show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Jean Delvare90d66192007-10-08 18:24:35 +0200826 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +0100827 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400830store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200832 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 u32 val;
834
835 val = simple_strtoul(buf, NULL, 10);
836 data->vrm = val;
837
838 return count;
839}
840static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400843show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 struct it87_data *data = it87_update_device(dev);
846 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
847}
848static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +0200849
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200850static ssize_t show_name(struct device *dev, struct device_attribute
851 *devattr, char *buf)
852{
853 struct it87_data *data = dev_get_drvdata(dev);
854 return sprintf(buf, "%s\n", data->name);
855}
856static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
857
Jean Delvare87808be2006-09-24 21:17:13 +0200858static struct attribute *it87_attributes[] = {
859 &sensor_dev_attr_in0_input.dev_attr.attr,
860 &sensor_dev_attr_in1_input.dev_attr.attr,
861 &sensor_dev_attr_in2_input.dev_attr.attr,
862 &sensor_dev_attr_in3_input.dev_attr.attr,
863 &sensor_dev_attr_in4_input.dev_attr.attr,
864 &sensor_dev_attr_in5_input.dev_attr.attr,
865 &sensor_dev_attr_in6_input.dev_attr.attr,
866 &sensor_dev_attr_in7_input.dev_attr.attr,
867 &sensor_dev_attr_in8_input.dev_attr.attr,
868 &sensor_dev_attr_in0_min.dev_attr.attr,
869 &sensor_dev_attr_in1_min.dev_attr.attr,
870 &sensor_dev_attr_in2_min.dev_attr.attr,
871 &sensor_dev_attr_in3_min.dev_attr.attr,
872 &sensor_dev_attr_in4_min.dev_attr.attr,
873 &sensor_dev_attr_in5_min.dev_attr.attr,
874 &sensor_dev_attr_in6_min.dev_attr.attr,
875 &sensor_dev_attr_in7_min.dev_attr.attr,
876 &sensor_dev_attr_in0_max.dev_attr.attr,
877 &sensor_dev_attr_in1_max.dev_attr.attr,
878 &sensor_dev_attr_in2_max.dev_attr.attr,
879 &sensor_dev_attr_in3_max.dev_attr.attr,
880 &sensor_dev_attr_in4_max.dev_attr.attr,
881 &sensor_dev_attr_in5_max.dev_attr.attr,
882 &sensor_dev_attr_in6_max.dev_attr.attr,
883 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +0100884 &sensor_dev_attr_in0_alarm.dev_attr.attr,
885 &sensor_dev_attr_in1_alarm.dev_attr.attr,
886 &sensor_dev_attr_in2_alarm.dev_attr.attr,
887 &sensor_dev_attr_in3_alarm.dev_attr.attr,
888 &sensor_dev_attr_in4_alarm.dev_attr.attr,
889 &sensor_dev_attr_in5_alarm.dev_attr.attr,
890 &sensor_dev_attr_in6_alarm.dev_attr.attr,
891 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200892
893 &sensor_dev_attr_temp1_input.dev_attr.attr,
894 &sensor_dev_attr_temp2_input.dev_attr.attr,
895 &sensor_dev_attr_temp3_input.dev_attr.attr,
896 &sensor_dev_attr_temp1_max.dev_attr.attr,
897 &sensor_dev_attr_temp2_max.dev_attr.attr,
898 &sensor_dev_attr_temp3_max.dev_attr.attr,
899 &sensor_dev_attr_temp1_min.dev_attr.attr,
900 &sensor_dev_attr_temp2_min.dev_attr.attr,
901 &sensor_dev_attr_temp3_min.dev_attr.attr,
902 &sensor_dev_attr_temp1_type.dev_attr.attr,
903 &sensor_dev_attr_temp2_type.dev_attr.attr,
904 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +0100905 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
906 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
907 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200908
909 &dev_attr_alarms.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200910 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200911 NULL
912};
913
914static const struct attribute_group it87_group = {
915 .attrs = it87_attributes,
916};
917
918static struct attribute *it87_attributes_opt[] = {
919 &sensor_dev_attr_fan1_input16.dev_attr.attr,
920 &sensor_dev_attr_fan1_min16.dev_attr.attr,
921 &sensor_dev_attr_fan2_input16.dev_attr.attr,
922 &sensor_dev_attr_fan2_min16.dev_attr.attr,
923 &sensor_dev_attr_fan3_input16.dev_attr.attr,
924 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvarec7f1f712007-09-03 17:11:46 +0200925 &sensor_dev_attr_fan4_input16.dev_attr.attr,
926 &sensor_dev_attr_fan4_min16.dev_attr.attr,
927 &sensor_dev_attr_fan5_input16.dev_attr.attr,
928 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200929
930 &sensor_dev_attr_fan1_input.dev_attr.attr,
931 &sensor_dev_attr_fan1_min.dev_attr.attr,
932 &sensor_dev_attr_fan1_div.dev_attr.attr,
933 &sensor_dev_attr_fan2_input.dev_attr.attr,
934 &sensor_dev_attr_fan2_min.dev_attr.attr,
935 &sensor_dev_attr_fan2_div.dev_attr.attr,
936 &sensor_dev_attr_fan3_input.dev_attr.attr,
937 &sensor_dev_attr_fan3_min.dev_attr.attr,
938 &sensor_dev_attr_fan3_div.dev_attr.attr,
939
Jean Delvare0124dd72007-11-25 16:16:41 +0100940 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
941 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
942 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
943 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
944 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
945
Jean Delvare87808be2006-09-24 21:17:13 +0200946 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
947 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
948 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
949 &sensor_dev_attr_pwm1.dev_attr.attr,
950 &sensor_dev_attr_pwm2.dev_attr.attr,
951 &sensor_dev_attr_pwm3.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +0100952 &dev_attr_pwm1_freq.attr,
953 &dev_attr_pwm2_freq.attr,
954 &dev_attr_pwm3_freq.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200955
956 &dev_attr_vrm.attr,
957 &dev_attr_cpu0_vid.attr,
958 NULL
959};
960
961static const struct attribute_group it87_group_opt = {
962 .attrs = it87_attributes_opt,
963};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Jean Delvare2d8672c2005-07-19 23:56:35 +0200965/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200966static int __init it87_find(unsigned short *address,
967 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 int err = -ENODEV;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200970 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200971 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 superio_enter();
Jean Delvare67b671b2007-12-06 23:13:42 +0100974 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200975
976 switch (chip_type) {
977 case IT8705F_DEVID:
978 sio_data->type = it87;
979 break;
980 case IT8712F_DEVID:
981 sio_data->type = it8712;
982 break;
983 case IT8716F_DEVID:
984 case IT8726F_DEVID:
985 sio_data->type = it8716;
986 break;
987 case IT8718F_DEVID:
988 sio_data->type = it8718;
989 break;
990 case 0xffff: /* No device at all */
991 goto exit;
992 default:
993 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
994 chip_type);
995 goto exit;
996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Jean Delvare87673dd2006-08-28 14:37:19 +0200998 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1000 pr_info("it87: Device not activated, skipping\n");
1001 goto exit;
1002 }
1003
1004 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1005 if (*address == 0) {
1006 pr_info("it87: Base address not set, skipping\n");
1007 goto exit;
1008 }
1009
1010 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001011 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001013 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Jean Delvare87673dd2006-08-28 14:37:19 +02001015 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1016 if (chip_type != IT8705F_DEVID) {
1017 int reg;
1018
1019 superio_select(GPIO);
1020 if (chip_type == it8718)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001021 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001022
1023 reg = superio_inb(IT87_SIO_PINX2_REG);
1024 if (reg & (1 << 0))
1025 pr_info("it87: in3 is VCC (+5V)\n");
1026 if (reg & (1 << 1))
1027 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
1028 }
1029
Jean Delvare98dd22c2008-10-09 15:33:58 +02001030 /* Disable specific features based on DMI strings */
1031 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1032 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1033 if (board_vendor && board_name) {
1034 if (strcmp(board_vendor, "nVIDIA") == 0
1035 && strcmp(board_name, "FN68PT") == 0) {
1036 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1037 connected to a fan, but to something else. One user
1038 has reported instant system power-off when changing
1039 the PWM2 duty cycle, so we disable it.
1040 I use the board name string as the trigger in case
1041 the same board is ever used in other systems. */
1042 pr_info("it87: Disabling pwm2 due to "
1043 "hardware constraints\n");
1044 sio_data->skip_pwm = (1 << 1);
1045 }
1046 }
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048exit:
1049 superio_exit();
1050 return err;
1051}
1052
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001053static int __devinit it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001056 struct resource *res;
1057 struct device *dev = &pdev->dev;
1058 struct it87_sio_data *sio_data = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 int enable_pwm_interface;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001061 static const char *names[] = {
1062 "it87",
1063 "it8712",
1064 "it8716",
1065 "it8718",
1066 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001068 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001069 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001070 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1071 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001072 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001073 err = -EBUSY;
1074 goto ERROR0;
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001077 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 err = -ENOMEM;
1079 goto ERROR1;
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001082 data->addr = res->start;
1083 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001084 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001085 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001088 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1089 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001090 err = -ENODEV;
1091 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001094 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001096 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001099 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001102 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 /* Register sysfs hooks */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001105 if ((err = sysfs_create_group(&dev->kobj, &it87_group)))
1106 goto ERROR2;
Jean Delvare17d648b2006-08-28 14:23:46 +02001107
Jean Delvare9060f8b2006-08-28 14:24:17 +02001108 /* Do not create fan files for disabled fans */
Andrew Paprocki04751692008-08-06 22:41:06 +02001109 if (has_16bit_fans(data)) {
Jean Delvare87673dd2006-08-28 14:37:19 +02001110 /* 16-bit tachometers */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001111 if (data->has_fan & (1 << 0)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001112 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001113 &sensor_dev_attr_fan1_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001114 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001115 &sensor_dev_attr_fan1_min16.dev_attr))
1116 || (err = device_create_file(dev,
1117 &sensor_dev_attr_fan1_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001118 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001119 }
1120 if (data->has_fan & (1 << 1)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001121 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001122 &sensor_dev_attr_fan2_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001123 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001124 &sensor_dev_attr_fan2_min16.dev_attr))
1125 || (err = device_create_file(dev,
1126 &sensor_dev_attr_fan2_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001127 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001128 }
1129 if (data->has_fan & (1 << 2)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001130 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001131 &sensor_dev_attr_fan3_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001132 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001133 &sensor_dev_attr_fan3_min16.dev_attr))
1134 || (err = device_create_file(dev,
1135 &sensor_dev_attr_fan3_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001136 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001137 }
Jean Delvarec7f1f712007-09-03 17:11:46 +02001138 if (data->has_fan & (1 << 3)) {
1139 if ((err = device_create_file(dev,
1140 &sensor_dev_attr_fan4_input16.dev_attr))
1141 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001142 &sensor_dev_attr_fan4_min16.dev_attr))
1143 || (err = device_create_file(dev,
1144 &sensor_dev_attr_fan4_alarm.dev_attr)))
Jean Delvarec7f1f712007-09-03 17:11:46 +02001145 goto ERROR4;
1146 }
1147 if (data->has_fan & (1 << 4)) {
1148 if ((err = device_create_file(dev,
1149 &sensor_dev_attr_fan5_input16.dev_attr))
1150 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001151 &sensor_dev_attr_fan5_min16.dev_attr))
1152 || (err = device_create_file(dev,
1153 &sensor_dev_attr_fan5_alarm.dev_attr)))
Jean Delvarec7f1f712007-09-03 17:11:46 +02001154 goto ERROR4;
1155 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001156 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001157 /* 8-bit tachometers with clock divider */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001158 if (data->has_fan & (1 << 0)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001159 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001160 &sensor_dev_attr_fan1_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001161 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001162 &sensor_dev_attr_fan1_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001163 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001164 &sensor_dev_attr_fan1_div.dev_attr))
1165 || (err = device_create_file(dev,
1166 &sensor_dev_attr_fan1_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001167 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001168 }
1169 if (data->has_fan & (1 << 1)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001170 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001171 &sensor_dev_attr_fan2_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001172 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001173 &sensor_dev_attr_fan2_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001174 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001175 &sensor_dev_attr_fan2_div.dev_attr))
1176 || (err = device_create_file(dev,
1177 &sensor_dev_attr_fan2_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001178 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001179 }
1180 if (data->has_fan & (1 << 2)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001181 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001182 &sensor_dev_attr_fan3_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001183 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001184 &sensor_dev_attr_fan3_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001185 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001186 &sensor_dev_attr_fan3_div.dev_attr))
1187 || (err = device_create_file(dev,
1188 &sensor_dev_attr_fan3_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001189 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001190 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001191 }
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (enable_pwm_interface) {
Jean Delvare98dd22c2008-10-09 15:33:58 +02001194 if (!(sio_data->skip_pwm & (1 << 0))) {
1195 if ((err = device_create_file(dev,
1196 &sensor_dev_attr_pwm1_enable.dev_attr))
1197 || (err = device_create_file(dev,
1198 &sensor_dev_attr_pwm1.dev_attr))
1199 || (err = device_create_file(dev,
1200 &dev_attr_pwm1_freq)))
1201 goto ERROR4;
1202 }
1203 if (!(sio_data->skip_pwm & (1 << 1))) {
1204 if ((err = device_create_file(dev,
1205 &sensor_dev_attr_pwm2_enable.dev_attr))
1206 || (err = device_create_file(dev,
1207 &sensor_dev_attr_pwm2.dev_attr))
1208 || (err = device_create_file(dev,
1209 &dev_attr_pwm2_freq)))
1210 goto ERROR4;
1211 }
1212 if (!(sio_data->skip_pwm & (1 << 2))) {
1213 if ((err = device_create_file(dev,
1214 &sensor_dev_attr_pwm3_enable.dev_attr))
1215 || (err = device_create_file(dev,
1216 &sensor_dev_attr_pwm3.dev_attr))
1217 || (err = device_create_file(dev,
1218 &dev_attr_pwm3_freq)))
1219 goto ERROR4;
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
Jean Delvare87673dd2006-08-28 14:37:19 +02001223 if (data->type == it8712 || data->type == it8716
1224 || data->type == it8718) {
Jean Delvare303760b2005-07-31 21:52:01 +02001225 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001226 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001227 data->vid = sio_data->vid_value;
1228 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001229 &dev_attr_vrm))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001230 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001231 &dev_attr_cpu0_vid)))
1232 goto ERROR4;
1233 }
1234
Tony Jones1beeffe2007-08-20 13:46:20 -07001235 data->hwmon_dev = hwmon_device_register(dev);
1236 if (IS_ERR(data->hwmon_dev)) {
1237 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001238 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240
1241 return 0;
1242
Jean Delvare87808be2006-09-24 21:17:13 +02001243ERROR4:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001244 sysfs_remove_group(&dev->kobj, &it87_group);
1245 sysfs_remove_group(&dev->kobj, &it87_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246ERROR2:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001247 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 kfree(data);
1249ERROR1:
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001250 release_region(res->start, IT87_EC_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251ERROR0:
1252 return err;
1253}
1254
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001255static int __devexit it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001257 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Tony Jones1beeffe2007-08-20 13:46:20 -07001259 hwmon_device_unregister(data->hwmon_dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001260 sysfs_remove_group(&pdev->dev.kobj, &it87_group);
1261 sysfs_remove_group(&pdev->dev.kobj, &it87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001262
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001263 release_region(data->addr, IT87_EC_EXTENT);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001264 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001265 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 return 0;
1268}
1269
Jean Delvare7f999aa2007-02-14 21:15:03 +01001270/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1272 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001273static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001275 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1276 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Jean Delvare7f999aa2007-02-14 21:15:03 +01001279/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1281 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001282static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001284 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1285 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
1288/* Return 1 if and only if the PWM interface is safe to use */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001289static int __devinit it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001291 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1293 * and polarity set to active low is sign that this is the case so we
1294 * disable pwm control to protect the user. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001295 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if ((tmp & 0x87) == 0) {
1297 if (fix_pwm_polarity) {
1298 /* The user asks us to attempt a chip reconfiguration.
1299 * This means switching to active high polarity and
1300 * inverting all fan speed values. */
1301 int i;
1302 u8 pwm[3];
1303
1304 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001305 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 IT87_REG_PWM(i));
1307
1308 /* If any fan is in automatic pwm mode, the polarity
1309 * might be correct, as suspicious as it seems, so we
1310 * better don't change anything (but still disable the
1311 * PWM interface). */
1312 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001313 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001315 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 tmp | 0x87);
1317 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001318 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 IT87_REG_PWM(i),
1320 0x7f & ~pwm[i]);
1321 return 1;
1322 }
1323
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001324 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 "too broken to be fixed\n");
1326 }
1327
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001328 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 "defaults, disabling PWM interface\n");
1330 return 0;
1331 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001332 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 "sane, won't touch\n");
1334 }
1335
1336 return 1;
1337}
1338
1339/* Called when we have found a new IT87. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001340static void __devinit it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001342 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int tmp, i;
1344
1345 /* initialize to sane defaults:
1346 * - if the chip is in manual pwm mode, this will be overwritten with
1347 * the actual settings on the chip (so in this case, initialization
1348 * is not needed)
1349 * - if in automatic or on/off mode, we could switch to manual mode,
1350 * read the registers and set manual_pwm_ctl accordingly, but currently
1351 * this is not implemented, so we initialize to something sane */
1352 for (i = 0; i < 3; i++) {
1353 data->manual_pwm_ctl[i] = 0xff;
1354 }
1355
Jean Delvarec5df9b72006-08-28 14:37:54 +02001356 /* Some chips seem to have default value 0xff for all limit
1357 * registers. For low voltage limits it makes no sense and triggers
1358 * alarms, so change to 0 instead. For high temperature limits, it
1359 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1360 * but is still confusing, so change to 127 degrees C. */
1361 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001362 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001363 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001364 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001365 }
1366 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001367 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001368 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001369 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001370 }
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 /* Check if temperature channnels are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001373 tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if ((tmp & 0x3f) == 0) {
1375 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1376 tmp = (tmp & 0xc0) | 0x2a;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001377 it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379 data->sensor = tmp;
1380
1381 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001382 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if ((tmp & 0xff) == 0) {
1384 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001385 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
1388 /* Check if tachometers are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001389 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if ((data->fan_main_ctrl & 0x70) == 0) {
1391 /* Enable all fan tachometers */
1392 data->fan_main_ctrl |= 0x70;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001393 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001395 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Jean Delvare17d648b2006-08-28 14:23:46 +02001397 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02001398 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001399 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001400 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001401 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02001402 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001403 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02001404 tmp | 0x07);
1405 }
Andrew Paprocki816d8c62008-08-06 22:41:06 +02001406 /* IT8705F only supports three fans. */
1407 if (data->type != it87) {
1408 if (tmp & (1 << 4))
1409 data->has_fan |= (1 << 3); /* fan4 enabled */
1410 if (tmp & (1 << 5))
1411 data->has_fan |= (1 << 4); /* fan5 enabled */
1412 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001413 }
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 /* Set current fan mode registers and the default settings for the
1416 * other mode registers */
1417 for (i = 0; i < 3; i++) {
1418 if (data->fan_main_ctrl & (1 << i)) {
1419 /* pwm mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001420 tmp = it87_read_value(data, IT87_REG_PWM(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (tmp & 0x80) {
1422 /* automatic pwm - not yet implemented, but
1423 * leave the settings made by the BIOS alone
1424 * until a change is requested via the sysfs
1425 * interface */
1426 } else {
1427 /* manual pwm */
1428 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1429 }
1430 }
1431 }
1432
1433 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001434 it87_write_value(data, IT87_REG_CONFIG,
1435 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 | (update_vbat ? 0x41 : 0x01));
1437}
1438
1439static struct it87_data *it87_update_device(struct device *dev)
1440{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001441 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int i;
1443
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001444 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1447 || !data->valid) {
1448
1449 if (update_vbat) {
1450 /* Cleared after each update, so reenable. Value
1451 returned by this read will be previous value */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001452 it87_write_value(data, IT87_REG_CONFIG,
1453 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455 for (i = 0; i <= 7; i++) {
1456 data->in[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001457 it87_read_value(data, IT87_REG_VIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 data->in_min[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001459 it87_read_value(data, IT87_REG_VIN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 data->in_max[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001461 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Jean Delvare3543a532006-08-28 14:27:25 +02001463 /* in8 (battery) has no limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 data->in[8] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001465 it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Jean Delvarec7f1f712007-09-03 17:11:46 +02001467 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02001468 /* Skip disabled fans */
1469 if (!(data->has_fan & (1 << i)))
1470 continue;
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 data->fan_min[i] =
Jean Delvarec7f1f712007-09-03 17:11:46 +02001473 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001474 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001475 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02001476 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02001477 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001478 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001479 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001480 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001481 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02001482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484 for (i = 0; i < 3; i++) {
1485 data->temp[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001486 it87_read_value(data, IT87_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 data->temp_high[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001488 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 data->temp_low[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001490 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
1492
Jean Delvare17d648b2006-08-28 14:23:46 +02001493 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02001494 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001495 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02001496 data->fan_div[0] = i & 0x07;
1497 data->fan_div[1] = (i >> 3) & 0x07;
1498 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001502 it87_read_value(data, IT87_REG_ALARM1) |
1503 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1504 (it87_read_value(data, IT87_REG_ALARM3) << 16);
1505 data->fan_main_ctrl = it87_read_value(data,
1506 IT87_REG_FAN_MAIN_CTRL);
1507 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001509 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Andrew Paprocki04751692008-08-06 22:41:06 +02001510 /* The 8705 does not have VID capability.
1511 The 8718 does not use IT87_REG_VID for the same purpose. */
Jean Delvare17d648b2006-08-28 14:23:46 +02001512 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001513 data->vid = it87_read_value(data, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02001514 /* The older IT8712F revisions had only 5 VID pins,
1515 but we assume it is always safe to read 6 bits. */
1516 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518 data->last_updated = jiffies;
1519 data->valid = 1;
1520 }
1521
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001522 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 return data;
1525}
1526
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001527static int __init it87_device_add(unsigned short address,
1528 const struct it87_sio_data *sio_data)
1529{
1530 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001531 .start = address + IT87_EC_OFFSET,
1532 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001533 .name = DRVNAME,
1534 .flags = IORESOURCE_IO,
1535 };
1536 int err;
1537
1538 pdev = platform_device_alloc(DRVNAME, address);
1539 if (!pdev) {
1540 err = -ENOMEM;
1541 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1542 goto exit;
1543 }
1544
1545 err = platform_device_add_resources(pdev, &res, 1);
1546 if (err) {
1547 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1548 "(%d)\n", err);
1549 goto exit_device_put;
1550 }
1551
1552 err = platform_device_add_data(pdev, sio_data,
1553 sizeof(struct it87_sio_data));
1554 if (err) {
1555 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1556 goto exit_device_put;
1557 }
1558
1559 err = platform_device_add(pdev);
1560 if (err) {
1561 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1562 err);
1563 goto exit_device_put;
1564 }
1565
1566 return 0;
1567
1568exit_device_put:
1569 platform_device_put(pdev);
1570exit:
1571 return err;
1572}
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574static int __init sm_it87_init(void)
1575{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001576 int err;
1577 unsigned short isa_address=0;
1578 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02001579
Jean Delvare98dd22c2008-10-09 15:33:58 +02001580 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001581 err = it87_find(&isa_address, &sio_data);
1582 if (err)
1583 return err;
1584 err = platform_driver_register(&it87_driver);
1585 if (err)
1586 return err;
1587
1588 err = it87_device_add(isa_address, &sio_data);
1589 if (err){
1590 platform_driver_unregister(&it87_driver);
1591 return err;
1592 }
1593
1594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
1597static void __exit sm_it87_exit(void)
1598{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001599 platform_device_unregister(pdev);
1600 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
1603
Jean Delvaref1d8e332007-11-25 16:14:44 +01001604MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02001605 "Jean Delvare <khali@linux-fr.org>");
Rudolf Marek08a8f6e2007-06-09 10:11:16 -04001606MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8726F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607module_param(update_vbat, bool, 0);
1608MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1609module_param(fix_pwm_polarity, bool, 0);
1610MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1611MODULE_LICENSE("GPL");
1612
1613module_init(sm_it87_init);
1614module_exit(sm_it87_exit);