]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hwmon/pc87360.c
[PATCH] hwmon: Fix the Kconfig header
[mirror_ubuntu-artful-kernel.git] / drivers / hwmon / pc87360.c
CommitLineData
1da177e4
LT
1/*
2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
5 *
6 * Copied from smsc47m1.c:
7 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Supports the following chips:
24 *
25 * Chip #vin #fan #pwm #temp devid
26 * PC87360 - 2 2 - 0xE1
27 * PC87363 - 2 2 - 0xE8
28 * PC87364 - 3 3 - 0xE4
29 * PC87365 11 3 3 2 0xE5
30 * PC87366 11 3 3 3-4 0xE9
31 *
32 * This driver assumes that no more than one chip is present, and one of
33 * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
34 */
35
1da177e4
LT
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/jiffies.h>
40#include <linux/i2c.h>
fde09509 41#include <linux/i2c-isa.h>
943b0830 42#include <linux/hwmon.h>
f0986bd8 43#include <linux/hwmon-sysfs.h>
303760b4 44#include <linux/hwmon-vid.h>
943b0830 45#include <linux/err.h>
9a61bf63 46#include <linux/mutex.h>
1da177e4
LT
47#include <asm/io.h>
48
1da177e4 49static u8 devid;
2d8672c5
JD
50static unsigned short address;
51static unsigned short extra_isa[3];
1da177e4
LT
52static u8 confreg[4];
53
54enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
1da177e4
LT
55
56static int init = 1;
57module_param(init, int, 0);
58MODULE_PARM_DESC(init,
59 "Chip initialization level:\n"
60 " 0: None\n"
61 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
62 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
63 " 3: Forcibly enable all voltage and temperature channels, including in9");
64
65/*
66 * Super-I/O registers and operations
67 */
68
69#define DEV 0x07 /* Register: Logical device select */
70#define DEVID 0x20 /* Register: Device ID */
71#define ACT 0x30 /* Register: Device activation */
72#define BASE 0x60 /* Register: Base address */
73
74#define FSCM 0x09 /* Logical device: fans */
75#define VLM 0x0d /* Logical device: voltages */
76#define TMS 0x0e /* Logical device: temperatures */
77static const u8 logdev[3] = { FSCM, VLM, TMS };
78
79#define LD_FAN 0
80#define LD_IN 1
81#define LD_TEMP 2
82
83static inline void superio_outb(int sioaddr, int reg, int val)
84{
85 outb(reg, sioaddr);
86 outb(val, sioaddr+1);
87}
88
89static inline int superio_inb(int sioaddr, int reg)
90{
91 outb(reg, sioaddr);
92 return inb(sioaddr+1);
93}
94
95static inline void superio_exit(int sioaddr)
96{
97 outb(0x02, sioaddr);
98 outb(0x02, sioaddr+1);
99}
100
101/*
102 * Logical devices
103 */
104
105#define PC87360_EXTENT 0x10
106#define PC87365_REG_BANK 0x09
107#define NO_BANK 0xff
108
109/*
110 * Fan registers and conversions
111 */
112
113/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
114#define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
115#define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
116#define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
117#define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
118#define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
119
120#define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
121 480000 / ((val)*(div)))
122#define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
123 480000 / ((val)*(div)))
124#define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
125#define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
126
127#define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
128#define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
129#define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
130
131#define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
132static inline u8 PWM_TO_REG(int val, int inv)
133{
134 if (inv)
135 val = 255 - val;
136 if (val < 0)
137 return 0;
138 if (val > 255)
139 return 255;
140 return val;
141}
142
143/*
144 * Voltage registers and conversions
145 */
146
147#define PC87365_REG_IN_CONVRATE 0x07
148#define PC87365_REG_IN_CONFIG 0x08
149#define PC87365_REG_IN 0x0B
150#define PC87365_REG_IN_MIN 0x0D
151#define PC87365_REG_IN_MAX 0x0C
152#define PC87365_REG_IN_STATUS 0x0A
153#define PC87365_REG_IN_ALARMS1 0x00
154#define PC87365_REG_IN_ALARMS2 0x01
155#define PC87365_REG_VID 0x06
156
157#define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
158#define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
159 (val)*256 >= (ref)*255 ? 255: \
160 ((val) * 256 + (ref)/2) / (ref))
161
162/*
163 * Temperature registers and conversions
164 */
165
166#define PC87365_REG_TEMP_CONFIG 0x08
167#define PC87365_REG_TEMP 0x0B
168#define PC87365_REG_TEMP_MIN 0x0D
169#define PC87365_REG_TEMP_MAX 0x0C
170#define PC87365_REG_TEMP_CRIT 0x0E
171#define PC87365_REG_TEMP_STATUS 0x0A
172#define PC87365_REG_TEMP_ALARMS 0x00
173
174#define TEMP_FROM_REG(val) ((val) * 1000)
175#define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
176 (val) > 127000 ? 127 : \
177 (val) < 0 ? ((val) - 500) / 1000 : \
178 ((val) + 500) / 1000)
179
180/*
181 * Client data (each client gets its own)
182 */
183
184struct pc87360_data {
185 struct i2c_client client;
943b0830 186 struct class_device *class_dev;
9a61bf63
IM
187 struct mutex lock;
188 struct mutex update_lock;
1da177e4
LT
189 char valid; /* !=0 if following fields are valid */
190 unsigned long last_updated; /* In jiffies */
191
192 int address[3];
193
194 u8 fannr, innr, tempnr;
195
196 u8 fan[3]; /* Register value */
197 u8 fan_min[3]; /* Register value */
198 u8 fan_status[3]; /* Register value */
199 u8 pwm[3]; /* Register value */
200 u16 fan_conf; /* Configuration register values, combined */
201
202 u16 in_vref; /* 1 mV/bit */
203 u8 in[14]; /* Register value */
204 u8 in_min[14]; /* Register value */
205 u8 in_max[14]; /* Register value */
206 u8 in_crit[3]; /* Register value */
207 u8 in_status[14]; /* Register value */
208 u16 in_alarms; /* Register values, combined, masked */
209 u8 vid_conf; /* Configuration register value */
210 u8 vrm;
211 u8 vid; /* Register value */
212
213 s8 temp[3]; /* Register value */
214 s8 temp_min[3]; /* Register value */
215 s8 temp_max[3]; /* Register value */
216 s8 temp_crit[3]; /* Register value */
217 u8 temp_status[3]; /* Register value */
218 u8 temp_alarms; /* Register value, masked */
219};
220
221/*
222 * Functions declaration
223 */
224
2d8672c5 225static int pc87360_detect(struct i2c_adapter *adapter);
1da177e4
LT
226static int pc87360_detach_client(struct i2c_client *client);
227
228static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
229 u8 reg);
230static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
231 u8 reg, u8 value);
232static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
233static struct pc87360_data *pc87360_update_device(struct device *dev);
234
235/*
236 * Driver data (common to all clients)
237 */
238
239static struct i2c_driver pc87360_driver = {
cdaf7934 240 .driver = {
cdaf7934
LR
241 .name = "pc87360",
242 },
2d8672c5 243 .attach_adapter = pc87360_detect,
1da177e4
LT
244 .detach_client = pc87360_detach_client,
245};
246
247/*
248 * Sysfs stuff
249 */
250
f0986bd8
JC
251static ssize_t show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
252{
253 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
254 struct pc87360_data *data = pc87360_update_device(dev);
694fa056
JC
255 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
256 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
f0986bd8
JC
257}
258static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
259{
260 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
261 struct pc87360_data *data = pc87360_update_device(dev);
694fa056
JC
262 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
263 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
f0986bd8
JC
264}
265static ssize_t show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
266{
267 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
268 struct pc87360_data *data = pc87360_update_device(dev);
269 return sprintf(buf, "%u\n",
694fa056 270 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
f0986bd8
JC
271}
272static ssize_t show_fan_status(struct device *dev, struct device_attribute *devattr, char *buf)
273{
274 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
275 struct pc87360_data *data = pc87360_update_device(dev);
276 return sprintf(buf, "%u\n",
694fa056 277 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
f0986bd8
JC
278}
279static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
280 size_t count)
281{
282 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
11be27ea
JC
283 struct i2c_client *client = to_i2c_client(dev);
284 struct pc87360_data *data = i2c_get_clientdata(client);
285 long fan_min = simple_strtol(buf, NULL, 10);
286
9a61bf63 287 mutex_lock(&data->update_lock);
11be27ea
JC
288 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
289
290 /* If it wouldn't fit, change clock divisor */
291 while (fan_min > 255
292 && (data->fan_status[attr->index] & 0x60) != 0x60) {
293 fan_min >>= 1;
294 data->fan[attr->index] >>= 1;
295 data->fan_status[attr->index] += 0x20;
296 }
297 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
298 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
299 data->fan_min[attr->index]);
300
301 /* Write new divider, preserve alarm bits */
302 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
303 data->fan_status[attr->index] & 0xF9);
9a61bf63 304 mutex_unlock(&data->update_lock);
11be27ea
JC
305
306 return count;
f0986bd8
JC
307}
308
dedc6a78
JC
309static struct sensor_device_attribute fan_input[] = {
310 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
311 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
312 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
313};
314static struct sensor_device_attribute fan_status[] = {
315 SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
316 SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
317 SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
318};
319static struct sensor_device_attribute fan_div[] = {
320 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
321 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
322 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
323};
324static struct sensor_device_attribute fan_min[] = {
325 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
326 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
327 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
328};
1da177e4 329
f0986bd8
JC
330static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
331{
332 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
333 struct pc87360_data *data = pc87360_update_device(dev);
334 return sprintf(buf, "%u\n",
694fa056 335 PWM_FROM_REG(data->pwm[attr->index],
f0986bd8 336 FAN_CONFIG_INVERT(data->fan_conf,
694fa056 337 attr->index)));
f0986bd8
JC
338}
339static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
340 size_t count)
341{
342 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
343 struct i2c_client *client = to_i2c_client(dev);
344 struct pc87360_data *data = i2c_get_clientdata(client);
345 long val = simple_strtol(buf, NULL, 10);
346
9a61bf63 347 mutex_lock(&data->update_lock);
694fa056
JC
348 data->pwm[attr->index] = PWM_TO_REG(val,
349 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
350 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
351 data->pwm[attr->index]);
9a61bf63 352 mutex_unlock(&data->update_lock);
f0986bd8
JC
353 return count;
354}
355
dedc6a78
JC
356static struct sensor_device_attribute pwm[] = {
357 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
358 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
359 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
360};
1da177e4 361
f0986bd8
JC
362static ssize_t show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
363{
364 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
365 struct pc87360_data *data = pc87360_update_device(dev);
366 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
367 data->in_vref));
368}
369static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
370{
371 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
372 struct pc87360_data *data = pc87360_update_device(dev);
373 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
374 data->in_vref));
375}
376static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
377{
378 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
379 struct pc87360_data *data = pc87360_update_device(dev);
380 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
381 data->in_vref));
382}
383static ssize_t show_in_status(struct device *dev, struct device_attribute *devattr, char *buf)
384{
385 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
386 struct pc87360_data *data = pc87360_update_device(dev);
387 return sprintf(buf, "%u\n", data->in_status[attr->index]);
388}
389static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr, const char *buf,
390 size_t count)
391{
392 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
393 struct i2c_client *client = to_i2c_client(dev);
394 struct pc87360_data *data = i2c_get_clientdata(client);
395 long val = simple_strtol(buf, NULL, 10);
396
9a61bf63 397 mutex_lock(&data->update_lock);
f0986bd8
JC
398 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
399 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
400 data->in_min[attr->index]);
9a61bf63 401 mutex_unlock(&data->update_lock);
f0986bd8
JC
402 return count;
403}
404static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
405 size_t count)
406{
407 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
408 struct i2c_client *client = to_i2c_client(dev);
409 struct pc87360_data *data = i2c_get_clientdata(client);
410 long val = simple_strtol(buf, NULL, 10);
411
9a61bf63 412 mutex_lock(&data->update_lock);
f0986bd8
JC
413 data->in_max[attr->index] = IN_TO_REG(val,
414 data->in_vref);
415 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
416 data->in_max[attr->index]);
9a61bf63 417 mutex_unlock(&data->update_lock);
f0986bd8
JC
418 return count;
419}
420
dedc6a78
JC
421static struct sensor_device_attribute in_input[] = {
422 SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
423 SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
424 SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
425 SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
426 SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
427 SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
428 SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
429 SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
430 SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
431 SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
432 SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
433};
434static struct sensor_device_attribute in_status[] = {
435 SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
436 SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
437 SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
438 SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
439 SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
440 SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
441 SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
442 SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
443 SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
444 SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
445 SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
446};
447static struct sensor_device_attribute in_min[] = {
448 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
449 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
450 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
451 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
452 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
453 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
454 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
455 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
456 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
457 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
458 SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
459};
460static struct sensor_device_attribute in_max[] = {
461 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
462 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
463 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
464 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
465 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
466 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
467 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
468 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
469 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
470 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
471 SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
472};
1da177e4 473
f0986bd8
JC
474static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
475{
476 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
477 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 478 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
f0986bd8
JC
479 data->in_vref));
480}
481static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
482{
483 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
484 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 485 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
f0986bd8
JC
486 data->in_vref));
487}
488static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
489{
490 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
491 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 492 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
f0986bd8
JC
493 data->in_vref));
494}
495static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
496{
497 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
498 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 499 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
f0986bd8
JC
500 data->in_vref));
501}
502static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
503{
504 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
505 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 506 return sprintf(buf, "%u\n", data->in_status[attr->index]);
f0986bd8
JC
507}
508static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
509 size_t count)
510{
511 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
512 struct i2c_client *client = to_i2c_client(dev);
513 struct pc87360_data *data = i2c_get_clientdata(client);
514 long val = simple_strtol(buf, NULL, 10);
515
9a61bf63 516 mutex_lock(&data->update_lock);
694fa056
JC
517 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
518 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
519 data->in_min[attr->index]);
9a61bf63 520 mutex_unlock(&data->update_lock);
f0986bd8
JC
521 return count;
522}
523static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
524 size_t count)
525{
526 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
527 struct i2c_client *client = to_i2c_client(dev);
528 struct pc87360_data *data = i2c_get_clientdata(client);
529 long val = simple_strtol(buf, NULL, 10);
530
9a61bf63 531 mutex_lock(&data->update_lock);
694fa056
JC
532 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
533 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
534 data->in_max[attr->index]);
9a61bf63 535 mutex_unlock(&data->update_lock);
f0986bd8
JC
536 return count;
537}
538static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
539 size_t count)
540{
541 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
542 struct i2c_client *client = to_i2c_client(dev);
543 struct pc87360_data *data = i2c_get_clientdata(client);
544 long val = simple_strtol(buf, NULL, 10);
545
9a61bf63 546 mutex_lock(&data->update_lock);
694fa056
JC
547 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
548 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
549 data->in_crit[attr->index-11]);
9a61bf63 550 mutex_unlock(&data->update_lock);
f0986bd8
JC
551 return count;
552}
553
dedc6a78
JC
554/* the +11 term below reflects the fact that VLM units 11,12,13 are
555 used in the chip to measure voltage across the thermistors
556*/
557static struct sensor_device_attribute therm_input[] = {
558 SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0+11),
559 SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1+11),
560 SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2+11),
561};
562static struct sensor_device_attribute therm_status[] = {
563 SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0+11),
564 SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1+11),
565 SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2+11),
566};
567static struct sensor_device_attribute therm_min[] = {
568 SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
569 show_therm_min, set_therm_min, 0+11),
570 SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
571 show_therm_min, set_therm_min, 1+11),
572 SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
573 show_therm_min, set_therm_min, 2+11),
574};
575static struct sensor_device_attribute therm_max[] = {
576 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
577 show_therm_max, set_therm_max, 0+11),
578 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
579 show_therm_max, set_therm_max, 1+11),
580 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
581 show_therm_max, set_therm_max, 2+11),
582};
583static struct sensor_device_attribute therm_crit[] = {
584 SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
585 show_therm_crit, set_therm_crit, 0+11),
586 SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
587 show_therm_crit, set_therm_crit, 1+11),
588 SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
589 show_therm_crit, set_therm_crit, 2+11),
590};
1da177e4 591
a5099cfc 592static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
593{
594 struct pc87360_data *data = pc87360_update_device(dev);
595 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
596}
597static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
598
a5099cfc 599static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
600{
601 struct pc87360_data *data = pc87360_update_device(dev);
602 return sprintf(buf, "%u\n", data->vrm);
603}
a5099cfc 604static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
605{
606 struct i2c_client *client = to_i2c_client(dev);
607 struct pc87360_data *data = i2c_get_clientdata(client);
608 data->vrm = simple_strtoul(buf, NULL, 10);
609 return count;
610}
611static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
612
a5099cfc 613static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
614{
615 struct pc87360_data *data = pc87360_update_device(dev);
616 return sprintf(buf, "%u\n", data->in_alarms);
617}
618static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
619
f0986bd8
JC
620static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
621{
622 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
623 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 624 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
f0986bd8
JC
625}
626static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
627{
628 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
629 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 630 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
f0986bd8
JC
631}
632static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
633{
634 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
635 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 636 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
f0986bd8
JC
637}
638static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
639{
640 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
641 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 642 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
f0986bd8
JC
643}
644static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
645{
646 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
647 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 648 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
f0986bd8
JC
649}
650static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
651 size_t count)
652{
653 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
654 struct i2c_client *client = to_i2c_client(dev);
655 struct pc87360_data *data = i2c_get_clientdata(client);
656 long val = simple_strtol(buf, NULL, 10);
657
9a61bf63 658 mutex_lock(&data->update_lock);
694fa056
JC
659 data->temp_min[attr->index] = TEMP_TO_REG(val);
660 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
661 data->temp_min[attr->index]);
9a61bf63 662 mutex_unlock(&data->update_lock);
f0986bd8
JC
663 return count;
664}
665static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
666 size_t count)
667{
668 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
669 struct i2c_client *client = to_i2c_client(dev);
670 struct pc87360_data *data = i2c_get_clientdata(client);
671 long val = simple_strtol(buf, NULL, 10);
672
9a61bf63 673 mutex_lock(&data->update_lock);
694fa056
JC
674 data->temp_max[attr->index] = TEMP_TO_REG(val);
675 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
676 data->temp_max[attr->index]);
9a61bf63 677 mutex_unlock(&data->update_lock);
f0986bd8
JC
678 return count;
679}
680static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
681 size_t count)
682{
683 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
684 struct i2c_client *client = to_i2c_client(dev);
685 struct pc87360_data *data = i2c_get_clientdata(client);
686 long val = simple_strtol(buf, NULL, 10);
687
9a61bf63 688 mutex_lock(&data->update_lock);
694fa056
JC
689 data->temp_crit[attr->index] = TEMP_TO_REG(val);
690 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
691 data->temp_crit[attr->index]);
9a61bf63 692 mutex_unlock(&data->update_lock);
f0986bd8
JC
693 return count;
694}
695
dedc6a78
JC
696static struct sensor_device_attribute temp_input[] = {
697 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
698 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
699 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
700};
701static struct sensor_device_attribute temp_status[] = {
702 SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
703 SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
704 SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
705};
706static struct sensor_device_attribute temp_min[] = {
707 SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
708 show_temp_min, set_temp_min, 0),
709 SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
710 show_temp_min, set_temp_min, 1),
711 SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
712 show_temp_min, set_temp_min, 2),
713};
714static struct sensor_device_attribute temp_max[] = {
715 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
716 show_temp_max, set_temp_max, 0),
717 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
718 show_temp_max, set_temp_max, 1),
719 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
720 show_temp_max, set_temp_max, 2),
721};
722static struct sensor_device_attribute temp_crit[] = {
723 SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
724 show_temp_crit, set_temp_crit, 0),
725 SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
726 show_temp_crit, set_temp_crit, 1),
727 SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
728 show_temp_crit, set_temp_crit, 2),
729};
1da177e4 730
a5099cfc 731static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
732{
733 struct pc87360_data *data = pc87360_update_device(dev);
734 return sprintf(buf, "%u\n", data->temp_alarms);
735}
736static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
737
738/*
739 * Device detection, registration and update
740 */
741
e6cfb3ad 742static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
1da177e4
LT
743{
744 u16 val;
745 int i;
746 int nrdev; /* logical device count */
747
748 /* No superio_enter */
749
750 /* Identify device */
751 val = superio_inb(sioaddr, DEVID);
752 switch (val) {
753 case 0xE1: /* PC87360 */
754 case 0xE8: /* PC87363 */
755 case 0xE4: /* PC87364 */
756 nrdev = 1;
757 break;
758 case 0xE5: /* PC87365 */
759 case 0xE9: /* PC87366 */
760 nrdev = 3;
761 break;
762 default:
763 superio_exit(sioaddr);
764 return -ENODEV;
765 }
766 /* Remember the device id */
767 *devid = val;
768
769 for (i = 0; i < nrdev; i++) {
770 /* select logical device */
771 superio_outb(sioaddr, DEV, logdev[i]);
772
773 val = superio_inb(sioaddr, ACT);
774 if (!(val & 0x01)) {
775 printk(KERN_INFO "pc87360: Device 0x%02x not "
776 "activated\n", logdev[i]);
777 continue;
778 }
779
780 val = (superio_inb(sioaddr, BASE) << 8)
781 | superio_inb(sioaddr, BASE + 1);
782 if (!val) {
783 printk(KERN_INFO "pc87360: Base address not set for "
784 "device 0x%02x\n", logdev[i]);
785 continue;
786 }
787
2d8672c5 788 addresses[i] = val;
1da177e4
LT
789
790 if (i==0) { /* Fans */
791 confreg[0] = superio_inb(sioaddr, 0xF0);
792 confreg[1] = superio_inb(sioaddr, 0xF1);
793
794#ifdef DEBUG
795 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
796 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
797 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
798 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
799 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
800 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
801 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
802 "ctrl=%d inv=%d\n", confreg[1]&1,
803 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
804#endif
805 } else if (i==1) { /* Voltages */
806 /* Are we using thermistors? */
807 if (*devid == 0xE9) { /* PC87366 */
808 /* These registers are not logical-device
809 specific, just that we won't need them if
810 we don't use the VLM device */
811 confreg[2] = superio_inb(sioaddr, 0x2B);
812 confreg[3] = superio_inb(sioaddr, 0x25);
813
814 if (confreg[2] & 0x40) {
815 printk(KERN_INFO "pc87360: Using "
816 "thermistors for temperature "
817 "monitoring\n");
818 }
819 if (confreg[3] & 0xE0) {
820 printk(KERN_INFO "pc87360: VID "
821 "inputs routed (mode %u)\n",
822 confreg[3] >> 5);
823 }
824 }
825 }
826 }
827
828 superio_exit(sioaddr);
829 return 0;
830}
831
2d8672c5 832static int pc87360_detect(struct i2c_adapter *adapter)
1da177e4
LT
833{
834 int i;
dedc6a78 835 struct i2c_client *client;
1da177e4
LT
836 struct pc87360_data *data;
837 int err = 0;
838 const char *name = "pc87360";
839 int use_thermistors = 0;
dedc6a78 840 struct device *dev;
1da177e4 841
ba9c2e8d 842 if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
1da177e4 843 return -ENOMEM;
1da177e4 844
dedc6a78
JC
845 client = &data->client;
846 dev = &client->dev;
847 i2c_set_clientdata(client, data);
848 client->addr = address;
9a61bf63 849 mutex_init(&data->lock);
dedc6a78
JC
850 client->adapter = adapter;
851 client->driver = &pc87360_driver;
852 client->flags = 0;
1da177e4
LT
853
854 data->fannr = 2;
855 data->innr = 0;
856 data->tempnr = 0;
857
858 switch (devid) {
859 case 0xe8:
860 name = "pc87363";
861 break;
862 case 0xe4:
863 name = "pc87364";
864 data->fannr = 3;
865 break;
866 case 0xe5:
867 name = "pc87365";
868 data->fannr = extra_isa[0] ? 3 : 0;
869 data->innr = extra_isa[1] ? 11 : 0;
870 data->tempnr = extra_isa[2] ? 2 : 0;
871 break;
872 case 0xe9:
873 name = "pc87366";
874 data->fannr = extra_isa[0] ? 3 : 0;
875 data->innr = extra_isa[1] ? 14 : 0;
876 data->tempnr = extra_isa[2] ? 3 : 0;
877 break;
878 }
879
dedc6a78 880 strlcpy(client->name, name, sizeof(client->name));
1da177e4 881 data->valid = 0;
9a61bf63 882 mutex_init(&data->update_lock);
1da177e4
LT
883
884 for (i = 0; i < 3; i++) {
885 if (((data->address[i] = extra_isa[i]))
886 && !request_region(extra_isa[i], PC87360_EXTENT,
cdaf7934 887 pc87360_driver.driver.name)) {
dedc6a78 888 dev_err(&client->dev, "Region 0x%x-0x%x already "
1da177e4
LT
889 "in use!\n", extra_isa[i],
890 extra_isa[i]+PC87360_EXTENT-1);
891 for (i--; i >= 0; i--)
892 release_region(extra_isa[i], PC87360_EXTENT);
893 err = -EBUSY;
894 goto ERROR1;
895 }
896 }
897
898 /* Retrieve the fans configuration from Super-I/O space */
899 if (data->fannr)
900 data->fan_conf = confreg[0] | (confreg[1] << 8);
901
dedc6a78 902 if ((err = i2c_attach_client(client)))
1da177e4
LT
903 goto ERROR2;
904
905 /* Use the correct reference voltage
906 Unless both the VLM and the TMS logical devices agree to
907 use an external Vref, the internal one is used. */
908 if (data->innr) {
909 i = pc87360_read_value(data, LD_IN, NO_BANK,
910 PC87365_REG_IN_CONFIG);
911 if (data->tempnr) {
912 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
913 PC87365_REG_TEMP_CONFIG);
914 }
915 data->in_vref = (i&0x02) ? 3025 : 2966;
dedc6a78 916 dev_dbg(&client->dev, "Using %s reference voltage\n",
1da177e4
LT
917 (i&0x02) ? "external" : "internal");
918
919 data->vid_conf = confreg[3];
920 data->vrm = 90;
921 }
922
923 /* Fan clock dividers may be needed before any data is read */
924 for (i = 0; i < data->fannr; i++) {
925 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
926 data->fan_status[i] = pc87360_read_value(data,
927 LD_FAN, NO_BANK,
928 PC87360_REG_FAN_STATUS(i));
929 }
930
931 if (init > 0) {
932 if (devid == 0xe9 && data->address[1]) /* PC87366 */
933 use_thermistors = confreg[2] & 0x40;
934
dedc6a78 935 pc87360_init_client(client, use_thermistors);
1da177e4
LT
936 }
937
938 /* Register sysfs hooks */
dedc6a78 939 data->class_dev = hwmon_device_register(&client->dev);
943b0830
MH
940 if (IS_ERR(data->class_dev)) {
941 err = PTR_ERR(data->class_dev);
942 goto ERROR3;
943 }
944
1da177e4 945 if (data->innr) {
dedc6a78
JC
946 for (i = 0; i < 11; i++) {
947 device_create_file(dev, &in_input[i].dev_attr);
948 device_create_file(dev, &in_min[i].dev_attr);
949 device_create_file(dev, &in_max[i].dev_attr);
950 device_create_file(dev, &in_status[i].dev_attr);
951 }
952 device_create_file(dev, &dev_attr_cpu0_vid);
953 device_create_file(dev, &dev_attr_vrm);
954 device_create_file(dev, &dev_attr_alarms_in);
1da177e4
LT
955 }
956
957 if (data->tempnr) {
dedc6a78
JC
958 for (i = 0; i < data->tempnr; i++) {
959 device_create_file(dev, &temp_input[i].dev_attr);
960 device_create_file(dev, &temp_min[i].dev_attr);
961 device_create_file(dev, &temp_max[i].dev_attr);
962 device_create_file(dev, &temp_crit[i].dev_attr);
963 device_create_file(dev, &temp_status[i].dev_attr);
1da177e4 964 }
dedc6a78
JC
965 device_create_file(dev, &dev_attr_alarms_temp);
966 }
1da177e4 967
dedc6a78
JC
968 if (data->innr == 14) {
969 for (i = 0; i < 3; i++) {
970 device_create_file(dev, &therm_input[i].dev_attr);
971 device_create_file(dev, &therm_min[i].dev_attr);
972 device_create_file(dev, &therm_max[i].dev_attr);
973 device_create_file(dev, &therm_crit[i].dev_attr);
974 device_create_file(dev, &therm_status[i].dev_attr);
1da177e4 975 }
1da177e4 976 }
1da177e4 977
dedc6a78
JC
978 for (i = 0; i < data->fannr; i++) {
979 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
980 device_create_file(dev, &fan_input[i].dev_attr);
981 device_create_file(dev, &fan_min[i].dev_attr);
982 device_create_file(dev, &fan_div[i].dev_attr);
983 device_create_file(dev, &fan_status[i].dev_attr);
984 }
985 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
986 device_create_file(dev, &pwm[i].dev_attr);
1da177e4
LT
987 }
988
989 return 0;
990
943b0830 991ERROR3:
dedc6a78 992 i2c_detach_client(client);
1da177e4
LT
993ERROR2:
994 for (i = 0; i < 3; i++) {
995 if (data->address[i]) {
996 release_region(data->address[i], PC87360_EXTENT);
997 }
998 }
999ERROR1:
1000 kfree(data);
1001 return err;
1002}
1003
1004static int pc87360_detach_client(struct i2c_client *client)
1005{
1006 struct pc87360_data *data = i2c_get_clientdata(client);
1007 int i;
1008
943b0830
MH
1009 hwmon_device_unregister(data->class_dev);
1010
7bef5594 1011 if ((i = i2c_detach_client(client)))
1da177e4 1012 return i;
1da177e4
LT
1013
1014 for (i = 0; i < 3; i++) {
1015 if (data->address[i]) {
1016 release_region(data->address[i], PC87360_EXTENT);
1017 }
1018 }
1019 kfree(data);
1020
1021 return 0;
1022}
1023
1024/* ldi is the logical device index
1025 bank is for voltages and temperatures only */
1026static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1027 u8 reg)
1028{
1029 int res;
1030
9a61bf63 1031 mutex_lock(&(data->lock));
1da177e4
LT
1032 if (bank != NO_BANK)
1033 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1034 res = inb_p(data->address[ldi] + reg);
9a61bf63 1035 mutex_unlock(&(data->lock));
1da177e4
LT
1036
1037 return res;
1038}
1039
1040static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1041 u8 reg, u8 value)
1042{
9a61bf63 1043 mutex_lock(&(data->lock));
1da177e4
LT
1044 if (bank != NO_BANK)
1045 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1046 outb_p(value, data->address[ldi] + reg);
9a61bf63 1047 mutex_unlock(&(data->lock));
1da177e4
LT
1048}
1049
1050static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
1051{
1052 struct pc87360_data *data = i2c_get_clientdata(client);
1053 int i, nr;
1054 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1055 const u8 init_temp[3] = { 2, 2, 1 };
1056 u8 reg;
1057
1058 if (init >= 2 && data->innr) {
1059 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1060 PC87365_REG_IN_CONVRATE);
368609c5 1061 dev_info(&client->dev, "VLM conversion set to "
1da177e4
LT
1062 "1s period, 160us delay\n");
1063 pc87360_write_value(data, LD_IN, NO_BANK,
1064 PC87365_REG_IN_CONVRATE,
1065 (reg & 0xC0) | 0x11);
1066 }
1067
1068 nr = data->innr < 11 ? data->innr : 11;
dedc6a78 1069 for (i = 0; i < nr; i++) {
1da177e4
LT
1070 if (init >= init_in[i]) {
1071 /* Forcibly enable voltage channel */
1072 reg = pc87360_read_value(data, LD_IN, i,
1073 PC87365_REG_IN_STATUS);
1074 if (!(reg & 0x01)) {
1075 dev_dbg(&client->dev, "Forcibly "
1076 "enabling in%d\n", i);
1077 pc87360_write_value(data, LD_IN, i,
1078 PC87365_REG_IN_STATUS,
1079 (reg & 0x68) | 0x87);
1080 }
1081 }
1082 }
1083
1084 /* We can't blindly trust the Super-I/O space configuration bit,
1085 most BIOS won't set it properly */
dedc6a78 1086 for (i = 11; i < data->innr; i++) {
1da177e4
LT
1087 reg = pc87360_read_value(data, LD_IN, i,
1088 PC87365_REG_TEMP_STATUS);
1089 use_thermistors = use_thermistors || (reg & 0x01);
1090 }
1091
1092 i = use_thermistors ? 2 : 0;
dedc6a78 1093 for (; i < data->tempnr; i++) {
1da177e4
LT
1094 if (init >= init_temp[i]) {
1095 /* Forcibly enable temperature channel */
1096 reg = pc87360_read_value(data, LD_TEMP, i,
1097 PC87365_REG_TEMP_STATUS);
1098 if (!(reg & 0x01)) {
1099 dev_dbg(&client->dev, "Forcibly "
1100 "enabling temp%d\n", i+1);
1101 pc87360_write_value(data, LD_TEMP, i,
1102 PC87365_REG_TEMP_STATUS,
1103 0xCF);
1104 }
1105 }
1106 }
1107
1108 if (use_thermistors) {
dedc6a78 1109 for (i = 11; i < data->innr; i++) {
1da177e4
LT
1110 if (init >= init_in[i]) {
1111 /* The pin may already be used by thermal
1112 diodes */
1113 reg = pc87360_read_value(data, LD_TEMP,
1114 (i-11)/2, PC87365_REG_TEMP_STATUS);
1115 if (reg & 0x01) {
1116 dev_dbg(&client->dev, "Skipping "
1117 "temp%d, pin already in use "
1118 "by temp%d\n", i-7, (i-11)/2);
1119 continue;
1120 }
1121
1122 /* Forcibly enable thermistor channel */
1123 reg = pc87360_read_value(data, LD_IN, i,
1124 PC87365_REG_IN_STATUS);
1125 if (!(reg & 0x01)) {
1126 dev_dbg(&client->dev, "Forcibly "
1127 "enabling temp%d\n", i-7);
1128 pc87360_write_value(data, LD_IN, i,
1129 PC87365_REG_TEMP_STATUS,
1130 (reg & 0x60) | 0x8F);
1131 }
1132 }
1133 }
1134 }
1135
1136 if (data->innr) {
1137 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1138 PC87365_REG_IN_CONFIG);
1139 if (reg & 0x01) {
1140 dev_dbg(&client->dev, "Forcibly "
1141 "enabling monitoring (VLM)\n");
1142 pc87360_write_value(data, LD_IN, NO_BANK,
1143 PC87365_REG_IN_CONFIG,
1144 reg & 0xFE);
1145 }
1146 }
1147
1148 if (data->tempnr) {
1149 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1150 PC87365_REG_TEMP_CONFIG);
1151 if (reg & 0x01) {
1152 dev_dbg(&client->dev, "Forcibly enabling "
1153 "monitoring (TMS)\n");
1154 pc87360_write_value(data, LD_TEMP, NO_BANK,
1155 PC87365_REG_TEMP_CONFIG,
1156 reg & 0xFE);
1157 }
1158
1159 if (init >= 2) {
1160 /* Chip config as documented by National Semi. */
1161 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1162 /* We voluntarily omit the bank here, in case the
1163 sequence itself matters. It shouldn't be a problem,
1164 since nobody else is supposed to access the
1165 device at that point. */
1166 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1167 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1168 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1169 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1170 }
1171 }
1172}
1173
1174static void pc87360_autodiv(struct i2c_client *client, int nr)
1175{
1176 struct pc87360_data *data = i2c_get_clientdata(client);
1177 u8 old_min = data->fan_min[nr];
1178
1179 /* Increase clock divider if needed and possible */
1180 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1181 || (data->fan[nr] >= 224)) { /* next to overflow */
1182 if ((data->fan_status[nr] & 0x60) != 0x60) {
1183 data->fan_status[nr] += 0x20;
1184 data->fan_min[nr] >>= 1;
1185 data->fan[nr] >>= 1;
1186 dev_dbg(&client->dev, "Increasing "
1187 "clock divider to %d for fan %d\n",
1188 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1189 }
1190 } else {
1191 /* Decrease clock divider if possible */
1192 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1193 && data->fan[nr] < 85 /* bad accuracy */
1194 && (data->fan_status[nr] & 0x60) != 0x00) {
1195 data->fan_status[nr] -= 0x20;
1196 data->fan_min[nr] <<= 1;
1197 data->fan[nr] <<= 1;
1198 dev_dbg(&client->dev, "Decreasing "
1199 "clock divider to %d for fan %d\n",
1200 FAN_DIV_FROM_REG(data->fan_status[nr]),
1201 nr+1);
1202 }
1203 }
1204
1205 /* Write new fan min if it changed */
1206 if (old_min != data->fan_min[nr]) {
1207 pc87360_write_value(data, LD_FAN, NO_BANK,
1208 PC87360_REG_FAN_MIN(nr),
1209 data->fan_min[nr]);
1210 }
1211}
1212
1213static struct pc87360_data *pc87360_update_device(struct device *dev)
1214{
1215 struct i2c_client *client = to_i2c_client(dev);
1216 struct pc87360_data *data = i2c_get_clientdata(client);
1217 u8 i;
1218
9a61bf63 1219 mutex_lock(&data->update_lock);
1da177e4
LT
1220
1221 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1222 dev_dbg(&client->dev, "Data update\n");
1223
1224 /* Fans */
1225 for (i = 0; i < data->fannr; i++) {
1226 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1227 data->fan_status[i] =
1228 pc87360_read_value(data, LD_FAN,
1229 NO_BANK, PC87360_REG_FAN_STATUS(i));
1230 data->fan[i] = pc87360_read_value(data, LD_FAN,
1231 NO_BANK, PC87360_REG_FAN(i));
1232 data->fan_min[i] = pc87360_read_value(data,
1233 LD_FAN, NO_BANK,
1234 PC87360_REG_FAN_MIN(i));
1235 /* Change clock divider if needed */
1236 pc87360_autodiv(client, i);
1237 /* Clear bits and write new divider */
1238 pc87360_write_value(data, LD_FAN, NO_BANK,
1239 PC87360_REG_FAN_STATUS(i),
1240 data->fan_status[i]);
1241 }
1242 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1243 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1244 NO_BANK, PC87360_REG_PWM(i));
1245 }
1246
1247 /* Voltages */
1248 for (i = 0; i < data->innr; i++) {
1249 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1250 PC87365_REG_IN_STATUS);
1251 /* Clear bits */
1252 pc87360_write_value(data, LD_IN, i,
1253 PC87365_REG_IN_STATUS,
1254 data->in_status[i]);
1255 if ((data->in_status[i] & 0x81) == 0x81) {
1256 data->in[i] = pc87360_read_value(data, LD_IN,
1257 i, PC87365_REG_IN);
1258 }
1259 if (data->in_status[i] & 0x01) {
1260 data->in_min[i] = pc87360_read_value(data,
1261 LD_IN, i,
1262 PC87365_REG_IN_MIN);
1263 data->in_max[i] = pc87360_read_value(data,
1264 LD_IN, i,
1265 PC87365_REG_IN_MAX);
1266 if (i >= 11)
1267 data->in_crit[i-11] =
1268 pc87360_read_value(data, LD_IN,
1269 i, PC87365_REG_TEMP_CRIT);
1270 }
1271 }
1272 if (data->innr) {
1273 data->in_alarms = pc87360_read_value(data, LD_IN,
1274 NO_BANK, PC87365_REG_IN_ALARMS1)
1275 | ((pc87360_read_value(data, LD_IN,
1276 NO_BANK, PC87365_REG_IN_ALARMS2)
1277 & 0x07) << 8);
1278 data->vid = (data->vid_conf & 0xE0) ?
1279 pc87360_read_value(data, LD_IN,
1280 NO_BANK, PC87365_REG_VID) : 0x1F;
1281 }
1282
1283 /* Temperatures */
1284 for (i = 0; i < data->tempnr; i++) {
1285 data->temp_status[i] = pc87360_read_value(data,
1286 LD_TEMP, i,
1287 PC87365_REG_TEMP_STATUS);
1288 /* Clear bits */
1289 pc87360_write_value(data, LD_TEMP, i,
1290 PC87365_REG_TEMP_STATUS,
1291 data->temp_status[i]);
1292 if ((data->temp_status[i] & 0x81) == 0x81) {
1293 data->temp[i] = pc87360_read_value(data,
1294 LD_TEMP, i,
1295 PC87365_REG_TEMP);
1296 }
1297 if (data->temp_status[i] & 0x01) {
1298 data->temp_min[i] = pc87360_read_value(data,
1299 LD_TEMP, i,
1300 PC87365_REG_TEMP_MIN);
1301 data->temp_max[i] = pc87360_read_value(data,
1302 LD_TEMP, i,
1303 PC87365_REG_TEMP_MAX);
1304 data->temp_crit[i] = pc87360_read_value(data,
1305 LD_TEMP, i,
1306 PC87365_REG_TEMP_CRIT);
1307 }
1308 }
1309 if (data->tempnr) {
1310 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1311 NO_BANK, PC87365_REG_TEMP_ALARMS)
1312 & 0x3F;
1313 }
1314
1315 data->last_updated = jiffies;
1316 data->valid = 1;
1317 }
1318
9a61bf63 1319 mutex_unlock(&data->update_lock);
1da177e4
LT
1320
1321 return data;
1322}
1323
1324static int __init pc87360_init(void)
1325{
1326 int i;
1327
1328 if (pc87360_find(0x2e, &devid, extra_isa)
1329 && pc87360_find(0x4e, &devid, extra_isa)) {
1330 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1331 "module not inserted.\n");
1332 return -ENODEV;
1333 }
1334
1335 /* Arbitrarily pick one of the addresses */
1336 for (i = 0; i < 3; i++) {
1337 if (extra_isa[i] != 0x0000) {
2d8672c5 1338 address = extra_isa[i];
1da177e4
LT
1339 break;
1340 }
1341 }
1342
2d8672c5 1343 if (address == 0x0000) {
1da177e4
LT
1344 printk(KERN_WARNING "pc87360: No active logical device, "
1345 "module not inserted.\n");
1346 return -ENODEV;
1347 }
1348
fde09509 1349 return i2c_isa_add_driver(&pc87360_driver);
1da177e4
LT
1350}
1351
1352static void __exit pc87360_exit(void)
1353{
fde09509 1354 i2c_isa_del_driver(&pc87360_driver);
1da177e4
LT
1355}
1356
1357
1358MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1359MODULE_DESCRIPTION("PC8736x hardware monitor");
1360MODULE_LICENSE("GPL");
1361
1362module_init(pc87360_init);
1363module_exit(pc87360_exit);