blob: 06df92b3ee492ccbfe3db607e1bd221e65a48b87 [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
Jean Delvare91749992005-10-08 00:10:00 +02005 Supports: IT8705F Super I/O chip w/LPC interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 IT8712F Super I/O chip w/LPC interface & SMBus
7 Sis950 A clone of the IT8705F
8
9 Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
10 Largely inspired by lm78.c of the same package
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
27/*
28 djg@pdp8.net David Gesswein 7/18/01
29 Modified to fix bug with not all alarms enabled.
30 Added ability to read battery voltage and select temperature sensor
31 type at module load time.
32*/
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/jiffies.h>
38#include <linux/i2c.h>
Jean Delvarefde09502005-07-19 23:51:07 +020039#include <linux/i2c-isa.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040040#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020041#include <linux/hwmon-sysfs.h>
42#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040043#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010044#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/io.h>
46
47
48/* Addresses to scan */
Jean Delvarec5e3fbf2006-01-18 22:39:48 +010049static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
Jean Delvare91749992005-10-08 00:10:00 +020050static unsigned short isa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020053I2C_CLIENT_INSMOD_2(it87, it8712);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define REG 0x2e /* The register to read/write */
56#define DEV 0x07 /* Register: Logical device select */
57#define VAL 0x2f /* The value to read/write */
58#define PME 0x04 /* The device with the fan registers in it */
59#define DEVID 0x20 /* Register: Device ID */
60#define DEVREV 0x22 /* Register: Device Revision */
61
62static inline int
63superio_inb(int reg)
64{
65 outb(reg, REG);
66 return inb(VAL);
67}
68
69static int superio_inw(int reg)
70{
71 int val;
72 outb(reg++, REG);
73 val = inb(VAL) << 8;
74 outb(reg, REG);
75 val |= inb(VAL);
76 return val;
77}
78
79static inline void
80superio_select(void)
81{
82 outb(DEV, REG);
83 outb(PME, VAL);
84}
85
86static inline void
87superio_enter(void)
88{
89 outb(0x87, REG);
90 outb(0x01, REG);
91 outb(0x55, REG);
92 outb(0x55, REG);
93}
94
95static inline void
96superio_exit(void)
97{
98 outb(0x02, REG);
99 outb(0x02, VAL);
100}
101
102#define IT8712F_DEVID 0x8712
103#define IT8705F_DEVID 0x8705
104#define IT87_ACT_REG 0x30
105#define IT87_BASE_REG 0x60
106
107/* Update battery voltage after every reading if true */
108static int update_vbat;
109
110/* Not all BIOSes properly configure the PWM registers */
111static int fix_pwm_polarity;
112
113/* Chip Type */
114
115static u16 chip_type;
116
117/* Many IT87 constants specified below */
118
119/* Length of ISA address segment */
120#define IT87_EXTENT 8
121
122/* Where are the ISA address/data registers relative to the base address */
123#define IT87_ADDR_REG_OFFSET 5
124#define IT87_DATA_REG_OFFSET 6
125
126/*----- The IT87 registers -----*/
127
128#define IT87_REG_CONFIG 0x00
129
130#define IT87_REG_ALARM1 0x01
131#define IT87_REG_ALARM2 0x02
132#define IT87_REG_ALARM3 0x03
133
134#define IT87_REG_VID 0x0a
135#define IT87_REG_FAN_DIV 0x0b
136
137/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
138
139#define IT87_REG_FAN(nr) (0x0d + (nr))
140#define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
141#define IT87_REG_FAN_MAIN_CTRL 0x13
142#define IT87_REG_FAN_CTL 0x14
143#define IT87_REG_PWM(nr) (0x15 + (nr))
144
145#define IT87_REG_VIN(nr) (0x20 + (nr))
146#define IT87_REG_TEMP(nr) (0x29 + (nr))
147
148#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
149#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
150#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
151#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
152
153#define IT87_REG_I2C_ADDR 0x48
154
155#define IT87_REG_VIN_ENABLE 0x50
156#define IT87_REG_TEMP_ENABLE 0x51
157
158#define IT87_REG_CHIPID 0x58
159
160#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
161#define IN_FROM_REG(val) ((val) * 16)
162
163static inline u8 FAN_TO_REG(long rpm, int div)
164{
165 if (rpm == 0)
166 return 255;
167 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
168 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
169 254);
170}
171
172#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
173
174#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
175 ((val)+500)/1000),-128,127))
176#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#define PWM_TO_REG(val) ((val) >> 1)
179#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
180
181static int DIV_TO_REG(int val)
182{
183 int answer = 0;
184 while ((val >>= 1) != 0)
185 answer++;
186 return answer;
187}
188#define DIV_FROM_REG(val) (1 << (val))
189
190
191/* For each registered IT87, we need to keep some data in memory. That
192 data is pointed to by it87_list[NR]->data. The structure itself is
193 dynamically allocated, at the same time when a new it87 client is
194 allocated. */
195struct it87_data {
196 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400197 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100198 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 enum chips type;
200
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100201 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 char valid; /* !=0 if following fields are valid */
203 unsigned long last_updated; /* In jiffies */
204
205 u8 in[9]; /* Register value */
206 u8 in_max[9]; /* Register value */
207 u8 in_min[9]; /* Register value */
208 u8 fan[3]; /* Register value */
209 u8 fan_min[3]; /* Register value */
210 u8 temp[3]; /* Register value */
211 u8 temp_high[3]; /* Register value */
212 u8 temp_low[3]; /* Register value */
213 u8 sensor; /* Register value */
214 u8 fan_div[3]; /* Register encoding, shifted right */
215 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100216 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 u32 alarms; /* Register encoding, combined */
218 u8 fan_main_ctrl; /* Register value */
219 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
220};
221
222
223static int it87_attach_adapter(struct i2c_adapter *adapter);
Jean Delvare2d8672c2005-07-19 23:56:35 +0200224static int it87_isa_attach_adapter(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
226static int it87_detach_client(struct i2c_client *client);
227
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100228static int it87_read_value(struct i2c_client *client, u8 reg);
229static int it87_write_value(struct i2c_client *client, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static struct it87_data *it87_update_device(struct device *dev);
231static int it87_check_pwm(struct i2c_client *client);
232static void it87_init_client(struct i2c_client *client, struct it87_data *data);
233
234
235static struct i2c_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100236 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100237 .name = "it87",
238 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 .id = I2C_DRIVERID_IT87,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 .attach_adapter = it87_attach_adapter,
241 .detach_client = it87_detach_client,
242};
243
Jean Delvarefde09502005-07-19 23:51:07 +0200244static struct i2c_driver it87_isa_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100245 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100246 .name = "it87-isa",
247 },
Jean Delvare2d8672c2005-07-19 23:56:35 +0200248 .attach_adapter = it87_isa_attach_adapter,
Jean Delvarefde09502005-07-19 23:51:07 +0200249 .detach_client = it87_detach_client,
250};
251
252
Jean Delvare20ad93d2005-06-05 11:53:25 +0200253static ssize_t show_in(struct device *dev, struct device_attribute *attr,
254 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200256 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
257 int nr = sensor_attr->index;
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct it87_data *data = it87_update_device(dev);
260 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
261}
262
Jean Delvare20ad93d2005-06-05 11:53:25 +0200263static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
264 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200266 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
267 int nr = sensor_attr->index;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct it87_data *data = it87_update_device(dev);
270 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
271}
272
Jean Delvare20ad93d2005-06-05 11:53:25 +0200273static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
274 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200276 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
277 int nr = sensor_attr->index;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct it87_data *data = it87_update_device(dev);
280 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
281}
282
Jean Delvare20ad93d2005-06-05 11:53:25 +0200283static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
284 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200286 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
287 int nr = sensor_attr->index;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct i2c_client *client = to_i2c_client(dev);
290 struct it87_data *data = i2c_get_clientdata(client);
291 unsigned long val = simple_strtoul(buf, NULL, 10);
292
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100293 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 data->in_min[nr] = IN_TO_REG(val);
295 it87_write_value(client, IT87_REG_VIN_MIN(nr),
296 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100297 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return count;
299}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200300static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
301 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200303 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
304 int nr = sensor_attr->index;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct i2c_client *client = to_i2c_client(dev);
307 struct it87_data *data = i2c_get_clientdata(client);
308 unsigned long val = simple_strtoul(buf, NULL, 10);
309
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100310 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 data->in_max[nr] = IN_TO_REG(val);
312 it87_write_value(client, IT87_REG_VIN_MAX(nr),
313 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100314 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return count;
316}
317
318#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200319static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
320 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200323static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
324 show_in_min, set_in_min, offset); \
325static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
326 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328show_in_offset(0);
329limit_in_offset(0);
330show_in_offset(1);
331limit_in_offset(1);
332show_in_offset(2);
333limit_in_offset(2);
334show_in_offset(3);
335limit_in_offset(3);
336show_in_offset(4);
337limit_in_offset(4);
338show_in_offset(5);
339limit_in_offset(5);
340show_in_offset(6);
341limit_in_offset(6);
342show_in_offset(7);
343limit_in_offset(7);
344show_in_offset(8);
345
346/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200347static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
348 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200350 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
351 int nr = sensor_attr->index;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct it87_data *data = it87_update_device(dev);
354 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
355}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200356static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
357 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200359 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
360 int nr = sensor_attr->index;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct it87_data *data = it87_update_device(dev);
363 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
364}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200365static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
366 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200368 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
369 int nr = sensor_attr->index;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct it87_data *data = it87_update_device(dev);
372 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
373}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200374static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
375 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200377 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
378 int nr = sensor_attr->index;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 struct i2c_client *client = to_i2c_client(dev);
381 struct it87_data *data = i2c_get_clientdata(client);
382 int val = simple_strtol(buf, NULL, 10);
383
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100384 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 data->temp_high[nr] = TEMP_TO_REG(val);
386 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100387 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return count;
389}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200390static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
391 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200393 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
394 int nr = sensor_attr->index;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct i2c_client *client = to_i2c_client(dev);
397 struct it87_data *data = i2c_get_clientdata(client);
398 int val = simple_strtol(buf, NULL, 10);
399
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100400 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 data->temp_low[nr] = TEMP_TO_REG(val);
402 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100403 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return count;
405}
406#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200407static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
408 show_temp, NULL, offset - 1); \
409static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
410 show_temp_max, set_temp_max, offset - 1); \
411static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
412 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414show_temp_offset(1);
415show_temp_offset(2);
416show_temp_offset(3);
417
Jean Delvare20ad93d2005-06-05 11:53:25 +0200418static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
419 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200421 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
422 int nr = sensor_attr->index;
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct it87_data *data = it87_update_device(dev);
425 u8 reg = data->sensor; /* In case the value is updated while we use it */
426
427 if (reg & (1 << nr))
428 return sprintf(buf, "3\n"); /* thermal diode */
429 if (reg & (8 << nr))
430 return sprintf(buf, "2\n"); /* thermistor */
431 return sprintf(buf, "0\n"); /* disabled */
432}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200433static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200436 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
437 int nr = sensor_attr->index;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct i2c_client *client = to_i2c_client(dev);
440 struct it87_data *data = i2c_get_clientdata(client);
441 int val = simple_strtol(buf, NULL, 10);
442
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100443 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 data->sensor &= ~(1 << nr);
446 data->sensor &= ~(8 << nr);
447 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
448 if (val == 3)
449 data->sensor |= 1 << nr;
450 else if (val == 2)
451 data->sensor |= 8 << nr;
452 else if (val != 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100453 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return -EINVAL;
455 }
456 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100457 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return count;
459}
460#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200461static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
462 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464show_sensor_offset(1);
465show_sensor_offset(2);
466show_sensor_offset(3);
467
468/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200469static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
470 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200472 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
473 int nr = sensor_attr->index;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct it87_data *data = it87_update_device(dev);
476 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
477 DIV_FROM_REG(data->fan_div[nr])));
478}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200479static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
480 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200482 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
483 int nr = sensor_attr->index;
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct it87_data *data = it87_update_device(dev);
486 return sprintf(buf,"%d\n",
487 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
488}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200489static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
490 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200492 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
493 int nr = sensor_attr->index;
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct it87_data *data = it87_update_device(dev);
496 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
497}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200498static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
499 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200501 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
502 int nr = sensor_attr->index;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct it87_data *data = it87_update_device(dev);
505 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
506}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200507static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
508 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200510 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
511 int nr = sensor_attr->index;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct it87_data *data = it87_update_device(dev);
514 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
515}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200516static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
517 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200519 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
520 int nr = sensor_attr->index;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct i2c_client *client = to_i2c_client(dev);
523 struct it87_data *data = i2c_get_clientdata(client);
524 int val = simple_strtol(buf, NULL, 10);
Jean Delvare07eab462005-11-23 15:44:31 -0800525 u8 reg = it87_read_value(client, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100527 mutex_lock(&data->update_lock);
Jean Delvare07eab462005-11-23 15:44:31 -0800528 switch (nr) {
529 case 0: data->fan_div[nr] = reg & 0x07; break;
530 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
531 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
532 }
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
535 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100536 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return count;
538}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200539static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
540 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200542 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
543 int nr = sensor_attr->index;
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct i2c_client *client = to_i2c_client(dev);
546 struct it87_data *data = i2c_get_clientdata(client);
547 int val = simple_strtol(buf, NULL, 10);
548 int i, min[3];
549 u8 old;
550
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100551 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 old = it87_read_value(client, IT87_REG_FAN_DIV);
553
554 for (i = 0; i < 3; i++)
555 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
556
557 switch (nr) {
558 case 0:
559 case 1:
560 data->fan_div[nr] = DIV_TO_REG(val);
561 break;
562 case 2:
563 if (val < 8)
564 data->fan_div[nr] = 1;
565 else
566 data->fan_div[nr] = 3;
567 }
568 val = old & 0x80;
569 val |= (data->fan_div[0] & 0x07);
570 val |= (data->fan_div[1] & 0x07) << 3;
571 if (data->fan_div[2] == 3)
572 val |= 0x1 << 6;
573 it87_write_value(client, IT87_REG_FAN_DIV, val);
574
575 for (i = 0; i < 3; i++) {
576 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
577 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
578 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100579 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return count;
581}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200582static ssize_t set_pwm_enable(struct device *dev,
583 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200585 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
586 int nr = sensor_attr->index;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct i2c_client *client = to_i2c_client(dev);
589 struct it87_data *data = i2c_get_clientdata(client);
590 int val = simple_strtol(buf, NULL, 10);
591
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100592 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (val == 0) {
595 int tmp;
596 /* make sure the fan is on when in on/off mode */
597 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
598 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
599 /* set on/off mode */
600 data->fan_main_ctrl &= ~(1 << nr);
601 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
602 } else if (val == 1) {
603 /* set SmartGuardian mode */
604 data->fan_main_ctrl |= (1 << nr);
605 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
606 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
607 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
608 } else {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100609 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return -EINVAL;
611 }
612
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100613 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return count;
615}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200616static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
617 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200619 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
620 int nr = sensor_attr->index;
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct i2c_client *client = to_i2c_client(dev);
623 struct it87_data *data = i2c_get_clientdata(client);
624 int val = simple_strtol(buf, NULL, 10);
625
626 if (val < 0 || val > 255)
627 return -EINVAL;
628
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100629 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 data->manual_pwm_ctl[nr] = val;
631 if (data->fan_main_ctrl & (1 << nr))
632 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100633 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return count;
635}
636
Jean Delvare20ad93d2005-06-05 11:53:25 +0200637#define show_fan_offset(offset) \
638static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
639 show_fan, NULL, offset - 1); \
640static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
641 show_fan_min, set_fan_min, offset - 1); \
642static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
643 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645show_fan_offset(1);
646show_fan_offset(2);
647show_fan_offset(3);
648
649#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200650static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
651 show_pwm_enable, set_pwm_enable, offset - 1); \
652static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
653 show_pwm, set_pwm, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655show_pwm_offset(1);
656show_pwm_offset(2);
657show_pwm_offset(3);
658
659/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400660static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200663 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
Jean Delvare1d66c642005-04-18 21:16:59 -0700665static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400668show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct it87_data *data = it87_update_device(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +0100671 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400674store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct i2c_client *client = to_i2c_client(dev);
677 struct it87_data *data = i2c_get_clientdata(client);
678 u32 val;
679
680 val = simple_strtoul(buf, NULL, 10);
681 data->vrm = val;
682
683 return count;
684}
685static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
686#define device_create_file_vrm(client) \
687device_create_file(&client->dev, &dev_attr_vrm)
688
689static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400690show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 struct it87_data *data = it87_update_device(dev);
693 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
694}
695static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
696#define device_create_file_vid(client) \
697device_create_file(&client->dev, &dev_attr_cpu0_vid)
698
699/* This function is called when:
700 * it87_driver is inserted (when this module is loaded), for each
701 available adapter
702 * when a new adapter is inserted (and it87_driver is still present) */
703static int it87_attach_adapter(struct i2c_adapter *adapter)
704{
705 if (!(adapter->class & I2C_CLASS_HWMON))
706 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200707 return i2c_probe(adapter, &addr_data, it87_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Jean Delvare2d8672c2005-07-19 23:56:35 +0200710static int it87_isa_attach_adapter(struct i2c_adapter *adapter)
711{
712 return it87_detect(adapter, isa_address, -1);
713}
714
715/* SuperIO detection - will change isa_address if a chip is found */
Jean Delvare91749992005-10-08 00:10:00 +0200716static int __init it87_find(unsigned short *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 int err = -ENODEV;
719
720 superio_enter();
721 chip_type = superio_inw(DEVID);
722 if (chip_type != IT8712F_DEVID
723 && chip_type != IT8705F_DEVID)
724 goto exit;
725
726 superio_select();
727 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
728 pr_info("it87: Device not activated, skipping\n");
729 goto exit;
730 }
731
732 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
733 if (*address == 0) {
734 pr_info("it87: Base address not set, skipping\n");
735 goto exit;
736 }
737
738 err = 0;
739 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
740 chip_type, *address, superio_inb(DEVREV) & 0x0f);
741
742exit:
743 superio_exit();
744 return err;
745}
746
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200747/* This function is called by i2c_probe */
Ben Dooksc49efce2005-10-26 21:07:25 +0200748static int it87_detect(struct i2c_adapter *adapter, int address, int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 int i;
751 struct i2c_client *new_client;
752 struct it87_data *data;
753 int err = 0;
754 const char *name = "";
755 int is_isa = i2c_is_isa_adapter(adapter);
756 int enable_pwm_interface;
757
758 if (!is_isa &&
759 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
760 goto ERROR0;
761
762 /* Reserve the ISA region */
763 if (is_isa)
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100764 if (!request_region(address, IT87_EXTENT,
765 it87_isa_driver.driver.name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 goto ERROR0;
767
Jean Delvare91749992005-10-08 00:10:00 +0200768 /* For now, we presume we have a valid client. We create the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 client structure, even though we cannot fill it completely yet.
770 But it allows us to access it87_{read,write}_value. */
771
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200772 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 err = -ENOMEM;
774 goto ERROR1;
775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 new_client = &data->client;
778 if (is_isa)
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100779 mutex_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 i2c_set_clientdata(new_client, data);
781 new_client->addr = address;
782 new_client->adapter = adapter;
Jean Delvarefde09502005-07-19 23:51:07 +0200783 new_client->driver = is_isa ? &it87_isa_driver : &it87_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 new_client->flags = 0;
785
786 /* Now, we do the remaining detection. */
787
788 if (kind < 0) {
789 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
790 || (!is_isa
791 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
792 err = -ENODEV;
793 goto ERROR2;
794 }
795 }
796
797 /* Determine the chip type. */
798 if (kind <= 0) {
799 i = it87_read_value(new_client, IT87_REG_CHIPID);
800 if (i == 0x90) {
801 kind = it87;
802 if ((is_isa) && (chip_type == IT8712F_DEVID))
803 kind = it8712;
804 }
805 else {
806 if (kind == 0)
807 dev_info(&adapter->dev,
808 "Ignoring 'force' parameter for unknown chip at "
809 "adapter %d, address 0x%02x\n",
810 i2c_adapter_id(adapter), address);
811 err = -ENODEV;
812 goto ERROR2;
813 }
814 }
815
816 if (kind == it87) {
817 name = "it87";
818 } else if (kind == it8712) {
819 name = "it8712";
820 }
821
822 /* Fill in the remaining client fields and put it into the global list */
823 strlcpy(new_client->name, name, I2C_NAME_SIZE);
824 data->type = kind;
825 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100826 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 /* Tell the I2C layer a new client has arrived */
829 if ((err = i2c_attach_client(new_client)))
830 goto ERROR2;
831
Jean Delvarec5e3fbf2006-01-18 22:39:48 +0100832 if (!is_isa)
833 dev_info(&new_client->dev, "The I2C interface to IT87xxF "
834 "hardware monitoring chips is deprecated. Please "
835 "report if you still rely on it.\n");
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* Check PWM configuration */
838 enable_pwm_interface = it87_check_pwm(new_client);
839
840 /* Initialize the IT87 chip */
841 it87_init_client(new_client, data);
842
843 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400844 data->class_dev = hwmon_device_register(&new_client->dev);
845 if (IS_ERR(data->class_dev)) {
846 err = PTR_ERR(data->class_dev);
847 goto ERROR3;
848 }
849
Jean Delvare20ad93d2005-06-05 11:53:25 +0200850 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
851 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
852 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
853 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
854 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
855 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
856 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
857 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
858 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
859 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
860 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
861 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
862 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
863 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
864 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
865 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
866 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
889 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
890 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
891 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 device_create_file(&new_client->dev, &dev_attr_alarms);
897 if (enable_pwm_interface) {
Jean Delvare20ad93d2005-06-05 11:53:25 +0200898 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
899 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
900 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
901 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
902 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
903 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
906 if (data->type == it8712) {
Jean Delvare303760b2005-07-31 21:52:01 +0200907 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 device_create_file_vrm(new_client);
909 device_create_file_vid(new_client);
910 }
911
912 return 0;
913
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400914ERROR3:
915 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916ERROR2:
917 kfree(data);
918ERROR1:
919 if (is_isa)
920 release_region(address, IT87_EXTENT);
921ERROR0:
922 return err;
923}
924
925static int it87_detach_client(struct i2c_client *client)
926{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400927 struct it87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int err;
929
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400930 hwmon_device_unregister(data->class_dev);
931
Jean Delvare7bef5592005-07-27 22:14:49 +0200932 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 if(i2c_is_isa_client(client))
936 release_region(client->addr, IT87_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400937 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 return 0;
940}
941
Steven Cole44bbe872005-05-03 18:21:25 -0600942/* The SMBus locks itself, but ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 We don't want to lock the whole ISA bus, so we lock each client
944 separately.
945 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
946 would slow down the IT87 access and should not be necessary. */
947static int it87_read_value(struct i2c_client *client, u8 reg)
948{
949 struct it87_data *data = i2c_get_clientdata(client);
950
951 int res;
952 if (i2c_is_isa_client(client)) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100953 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
955 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100956 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return res;
958 } else
959 return i2c_smbus_read_byte_data(client, reg);
960}
961
Steven Cole44bbe872005-05-03 18:21:25 -0600962/* The SMBus locks itself, but ISA access muse be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 We don't want to lock the whole ISA bus, so we lock each client
964 separately.
965 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
966 would slow down the IT87 access and should not be necessary. */
967static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
968{
969 struct it87_data *data = i2c_get_clientdata(client);
970
971 if (i2c_is_isa_client(client)) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100972 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
974 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100975 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return 0;
977 } else
978 return i2c_smbus_write_byte_data(client, reg, value);
979}
980
981/* Return 1 if and only if the PWM interface is safe to use */
982static int it87_check_pwm(struct i2c_client *client)
983{
984 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
985 * and polarity set to active low is sign that this is the case so we
986 * disable pwm control to protect the user. */
987 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
988 if ((tmp & 0x87) == 0) {
989 if (fix_pwm_polarity) {
990 /* The user asks us to attempt a chip reconfiguration.
991 * This means switching to active high polarity and
992 * inverting all fan speed values. */
993 int i;
994 u8 pwm[3];
995
996 for (i = 0; i < 3; i++)
997 pwm[i] = it87_read_value(client,
998 IT87_REG_PWM(i));
999
1000 /* If any fan is in automatic pwm mode, the polarity
1001 * might be correct, as suspicious as it seems, so we
1002 * better don't change anything (but still disable the
1003 * PWM interface). */
1004 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1005 dev_info(&client->dev, "Reconfiguring PWM to "
1006 "active high polarity\n");
1007 it87_write_value(client, IT87_REG_FAN_CTL,
1008 tmp | 0x87);
1009 for (i = 0; i < 3; i++)
1010 it87_write_value(client,
1011 IT87_REG_PWM(i),
1012 0x7f & ~pwm[i]);
1013 return 1;
1014 }
1015
1016 dev_info(&client->dev, "PWM configuration is "
1017 "too broken to be fixed\n");
1018 }
1019
1020 dev_info(&client->dev, "Detected broken BIOS "
1021 "defaults, disabling PWM interface\n");
1022 return 0;
1023 } else if (fix_pwm_polarity) {
1024 dev_info(&client->dev, "PWM configuration looks "
1025 "sane, won't touch\n");
1026 }
1027
1028 return 1;
1029}
1030
1031/* Called when we have found a new IT87. */
1032static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1033{
1034 int tmp, i;
1035
1036 /* initialize to sane defaults:
1037 * - if the chip is in manual pwm mode, this will be overwritten with
1038 * the actual settings on the chip (so in this case, initialization
1039 * is not needed)
1040 * - if in automatic or on/off mode, we could switch to manual mode,
1041 * read the registers and set manual_pwm_ctl accordingly, but currently
1042 * this is not implemented, so we initialize to something sane */
1043 for (i = 0; i < 3; i++) {
1044 data->manual_pwm_ctl[i] = 0xff;
1045 }
1046
1047 /* Check if temperature channnels are reset manually or by some reason */
1048 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1049 if ((tmp & 0x3f) == 0) {
1050 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1051 tmp = (tmp & 0xc0) | 0x2a;
1052 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1053 }
1054 data->sensor = tmp;
1055
1056 /* Check if voltage monitors are reset manually or by some reason */
1057 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1058 if ((tmp & 0xff) == 0) {
1059 /* Enable all voltage monitors */
1060 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1061 }
1062
1063 /* Check if tachometers are reset manually or by some reason */
1064 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1065 if ((data->fan_main_ctrl & 0x70) == 0) {
1066 /* Enable all fan tachometers */
1067 data->fan_main_ctrl |= 0x70;
1068 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1069 }
1070
1071 /* Set current fan mode registers and the default settings for the
1072 * other mode registers */
1073 for (i = 0; i < 3; i++) {
1074 if (data->fan_main_ctrl & (1 << i)) {
1075 /* pwm mode */
1076 tmp = it87_read_value(client, IT87_REG_PWM(i));
1077 if (tmp & 0x80) {
1078 /* automatic pwm - not yet implemented, but
1079 * leave the settings made by the BIOS alone
1080 * until a change is requested via the sysfs
1081 * interface */
1082 } else {
1083 /* manual pwm */
1084 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1085 }
1086 }
1087 }
1088
1089 /* Start monitoring */
1090 it87_write_value(client, IT87_REG_CONFIG,
1091 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1092 | (update_vbat ? 0x41 : 0x01));
1093}
1094
1095static struct it87_data *it87_update_device(struct device *dev)
1096{
1097 struct i2c_client *client = to_i2c_client(dev);
1098 struct it87_data *data = i2c_get_clientdata(client);
1099 int i;
1100
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001101 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1104 || !data->valid) {
1105
1106 if (update_vbat) {
1107 /* Cleared after each update, so reenable. Value
1108 returned by this read will be previous value */
1109 it87_write_value(client, IT87_REG_CONFIG,
1110 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1111 }
1112 for (i = 0; i <= 7; i++) {
1113 data->in[i] =
1114 it87_read_value(client, IT87_REG_VIN(i));
1115 data->in_min[i] =
1116 it87_read_value(client, IT87_REG_VIN_MIN(i));
1117 data->in_max[i] =
1118 it87_read_value(client, IT87_REG_VIN_MAX(i));
1119 }
1120 data->in[8] =
1121 it87_read_value(client, IT87_REG_VIN(8));
1122 /* Temperature sensor doesn't have limit registers, set
1123 to min and max value */
1124 data->in_min[8] = 0;
1125 data->in_max[8] = 255;
1126
1127 for (i = 0; i < 3; i++) {
1128 data->fan[i] =
1129 it87_read_value(client, IT87_REG_FAN(i));
1130 data->fan_min[i] =
1131 it87_read_value(client, IT87_REG_FAN_MIN(i));
1132 }
1133 for (i = 0; i < 3; i++) {
1134 data->temp[i] =
1135 it87_read_value(client, IT87_REG_TEMP(i));
1136 data->temp_high[i] =
1137 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1138 data->temp_low[i] =
1139 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1140 }
1141
1142 i = it87_read_value(client, IT87_REG_FAN_DIV);
1143 data->fan_div[0] = i & 0x07;
1144 data->fan_div[1] = (i >> 3) & 0x07;
1145 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1146
1147 data->alarms =
1148 it87_read_value(client, IT87_REG_ALARM1) |
1149 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1150 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1151 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1152
1153 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1154 /* The 8705 does not have VID capability */
1155 if (data->type == it8712) {
1156 data->vid = it87_read_value(client, IT87_REG_VID);
1157 data->vid &= 0x1f;
1158 }
1159 data->last_updated = jiffies;
1160 data->valid = 1;
1161 }
1162
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001163 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 return data;
1166}
1167
1168static int __init sm_it87_init(void)
1169{
Jean Delvare91749992005-10-08 00:10:00 +02001170 int res;
Jean Delvarefde09502005-07-19 23:51:07 +02001171
1172 res = i2c_add_driver(&it87_driver);
1173 if (res)
1174 return res;
1175
Jean Delvare91749992005-10-08 00:10:00 +02001176 if (!it87_find(&isa_address)) {
1177 res = i2c_isa_add_driver(&it87_isa_driver);
1178 if (res) {
1179 i2c_del_driver(&it87_driver);
1180 return res;
1181 }
Jean Delvarefde09502005-07-19 23:51:07 +02001182 }
1183
1184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
1187static void __exit sm_it87_exit(void)
1188{
Jean Delvarebe79c382006-02-07 17:53:32 +01001189 if (isa_address)
1190 i2c_isa_del_driver(&it87_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 i2c_del_driver(&it87_driver);
1192}
1193
1194
1195MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1196MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1197module_param(update_vbat, bool, 0);
1198MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1199module_param(fix_pwm_polarity, bool, 0);
1200MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1201MODULE_LICENSE("GPL");
1202
1203module_init(sm_it87_init);
1204module_exit(sm_it87_exit);