2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 * Copyright (C) 2004, 2007 Jean Delvare <khali@linux-fr.org>
6 * Copied from smsc47m1.c:
7 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
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.
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.
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.
23 * Supports the following chips:
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
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).
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/jiffies.h>
40 #include <linux/platform_device.h>
41 #include <linux/hwmon.h>
42 #include <linux/hwmon-sysfs.h>
43 #include <linux/hwmon-vid.h>
44 #include <linux/err.h>
45 #include <linux/mutex.h>
49 static struct platform_device
*pdev
;
50 static unsigned short extra_isa
[3];
54 module_param(init
, int, 0);
55 MODULE_PARM_DESC(init
,
56 "Chip initialization level:\n"
58 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
59 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
60 " 3: Forcibly enable all voltage and temperature channels, including in9");
62 static unsigned short force_id
;
63 module_param(force_id
, ushort
, 0);
64 MODULE_PARM_DESC(force_id
, "Override the detected device ID");
67 * Super-I/O registers and operations
70 #define DEV 0x07 /* Register: Logical device select */
71 #define DEVID 0x20 /* Register: Device ID */
72 #define ACT 0x30 /* Register: Device activation */
73 #define BASE 0x60 /* Register: Base address */
75 #define FSCM 0x09 /* Logical device: fans */
76 #define VLM 0x0d /* Logical device: voltages */
77 #define TMS 0x0e /* Logical device: temperatures */
78 static const u8 logdev
[3] = { FSCM
, VLM
, TMS
};
84 static inline void superio_outb(int sioaddr
, int reg
, int val
)
90 static inline int superio_inb(int sioaddr
, int reg
)
93 return inb(sioaddr
+1);
96 static inline void superio_exit(int sioaddr
)
99 outb(0x02, sioaddr
+1);
106 #define PC87360_EXTENT 0x10
107 #define PC87365_REG_BANK 0x09
111 * Fan registers and conversions
114 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
115 #define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
116 #define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
117 #define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
118 #define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
119 #define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
121 #define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
122 480000 / ((val)*(div)))
123 #define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
124 480000 / ((val)*(div)))
125 #define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
126 #define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
128 #define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
129 #define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
130 #define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
132 #define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
133 static inline u8
PWM_TO_REG(int val
, int inv
)
145 * Voltage registers and conversions
148 #define PC87365_REG_IN_CONVRATE 0x07
149 #define PC87365_REG_IN_CONFIG 0x08
150 #define PC87365_REG_IN 0x0B
151 #define PC87365_REG_IN_MIN 0x0D
152 #define PC87365_REG_IN_MAX 0x0C
153 #define PC87365_REG_IN_STATUS 0x0A
154 #define PC87365_REG_IN_ALARMS1 0x00
155 #define PC87365_REG_IN_ALARMS2 0x01
156 #define PC87365_REG_VID 0x06
158 #define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
159 #define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
160 (val)*256 >= (ref)*255 ? 255: \
161 ((val) * 256 + (ref)/2) / (ref))
164 * Temperature registers and conversions
167 #define PC87365_REG_TEMP_CONFIG 0x08
168 #define PC87365_REG_TEMP 0x0B
169 #define PC87365_REG_TEMP_MIN 0x0D
170 #define PC87365_REG_TEMP_MAX 0x0C
171 #define PC87365_REG_TEMP_CRIT 0x0E
172 #define PC87365_REG_TEMP_STATUS 0x0A
173 #define PC87365_REG_TEMP_ALARMS 0x00
175 #define TEMP_FROM_REG(val) ((val) * 1000)
176 #define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
177 (val) > 127000 ? 127 : \
178 (val) < 0 ? ((val) - 500) / 1000 : \
179 ((val) + 500) / 1000)
185 struct pc87360_data
{
187 struct device
*hwmon_dev
;
189 struct mutex update_lock
;
190 char valid
; /* !=0 if following fields are valid */
191 unsigned long last_updated
; /* In jiffies */
195 u8 fannr
, innr
, tempnr
;
197 u8 fan
[3]; /* Register value */
198 u8 fan_min
[3]; /* Register value */
199 u8 fan_status
[3]; /* Register value */
200 u8 pwm
[3]; /* Register value */
201 u16 fan_conf
; /* Configuration register values, combined */
203 u16 in_vref
; /* 1 mV/bit */
204 u8 in
[14]; /* Register value */
205 u8 in_min
[14]; /* Register value */
206 u8 in_max
[14]; /* Register value */
207 u8 in_crit
[3]; /* Register value */
208 u8 in_status
[14]; /* Register value */
209 u16 in_alarms
; /* Register values, combined, masked */
210 u8 vid_conf
; /* Configuration register value */
212 u8 vid
; /* Register value */
214 s8 temp
[3]; /* Register value */
215 s8 temp_min
[3]; /* Register value */
216 s8 temp_max
[3]; /* Register value */
217 s8 temp_crit
[3]; /* Register value */
218 u8 temp_status
[3]; /* Register value */
219 u8 temp_alarms
; /* Register value, masked */
223 * Functions declaration
226 static int pc87360_probe(struct platform_device
*pdev
);
227 static int __devexit
pc87360_remove(struct platform_device
*pdev
);
229 static int pc87360_read_value(struct pc87360_data
*data
, u8 ldi
, u8 bank
,
231 static void pc87360_write_value(struct pc87360_data
*data
, u8 ldi
, u8 bank
,
233 static void pc87360_init_device(struct platform_device
*pdev
,
234 int use_thermistors
);
235 static struct pc87360_data
*pc87360_update_device(struct device
*dev
);
241 static struct platform_driver pc87360_driver
= {
243 .owner
= THIS_MODULE
,
246 .probe
= pc87360_probe
,
247 .remove
= __devexit_p(pc87360_remove
),
254 static ssize_t
show_fan_input(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
256 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
257 struct pc87360_data
*data
= pc87360_update_device(dev
);
258 return sprintf(buf
, "%u\n", FAN_FROM_REG(data
->fan
[attr
->index
],
259 FAN_DIV_FROM_REG(data
->fan_status
[attr
->index
])));
261 static ssize_t
show_fan_min(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
263 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
264 struct pc87360_data
*data
= pc87360_update_device(dev
);
265 return sprintf(buf
, "%u\n", FAN_FROM_REG(data
->fan_min
[attr
->index
],
266 FAN_DIV_FROM_REG(data
->fan_status
[attr
->index
])));
268 static ssize_t
show_fan_div(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
270 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
271 struct pc87360_data
*data
= pc87360_update_device(dev
);
272 return sprintf(buf
, "%u\n",
273 FAN_DIV_FROM_REG(data
->fan_status
[attr
->index
]));
275 static ssize_t
show_fan_status(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
277 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
278 struct pc87360_data
*data
= pc87360_update_device(dev
);
279 return sprintf(buf
, "%u\n",
280 FAN_STATUS_FROM_REG(data
->fan_status
[attr
->index
]));
282 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
285 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
286 struct pc87360_data
*data
= dev_get_drvdata(dev
);
287 long fan_min
= simple_strtol(buf
, NULL
, 10);
289 mutex_lock(&data
->update_lock
);
290 fan_min
= FAN_TO_REG(fan_min
, FAN_DIV_FROM_REG(data
->fan_status
[attr
->index
]));
292 /* If it wouldn't fit, change clock divisor */
294 && (data
->fan_status
[attr
->index
] & 0x60) != 0x60) {
296 data
->fan
[attr
->index
] >>= 1;
297 data
->fan_status
[attr
->index
] += 0x20;
299 data
->fan_min
[attr
->index
] = fan_min
> 255 ? 255 : fan_min
;
300 pc87360_write_value(data
, LD_FAN
, NO_BANK
, PC87360_REG_FAN_MIN(attr
->index
),
301 data
->fan_min
[attr
->index
]);
303 /* Write new divider, preserve alarm bits */
304 pc87360_write_value(data
, LD_FAN
, NO_BANK
, PC87360_REG_FAN_STATUS(attr
->index
),
305 data
->fan_status
[attr
->index
] & 0xF9);
306 mutex_unlock(&data
->update_lock
);
311 static struct sensor_device_attribute fan_input
[] = {
312 SENSOR_ATTR(fan1_input
, S_IRUGO
, show_fan_input
, NULL
, 0),
313 SENSOR_ATTR(fan2_input
, S_IRUGO
, show_fan_input
, NULL
, 1),
314 SENSOR_ATTR(fan3_input
, S_IRUGO
, show_fan_input
, NULL
, 2),
316 static struct sensor_device_attribute fan_status
[] = {
317 SENSOR_ATTR(fan1_status
, S_IRUGO
, show_fan_status
, NULL
, 0),
318 SENSOR_ATTR(fan2_status
, S_IRUGO
, show_fan_status
, NULL
, 1),
319 SENSOR_ATTR(fan3_status
, S_IRUGO
, show_fan_status
, NULL
, 2),
321 static struct sensor_device_attribute fan_div
[] = {
322 SENSOR_ATTR(fan1_div
, S_IRUGO
, show_fan_div
, NULL
, 0),
323 SENSOR_ATTR(fan2_div
, S_IRUGO
, show_fan_div
, NULL
, 1),
324 SENSOR_ATTR(fan3_div
, S_IRUGO
, show_fan_div
, NULL
, 2),
326 static struct sensor_device_attribute fan_min
[] = {
327 SENSOR_ATTR(fan1_min
, S_IWUSR
| S_IRUGO
, show_fan_min
, set_fan_min
, 0),
328 SENSOR_ATTR(fan2_min
, S_IWUSR
| S_IRUGO
, show_fan_min
, set_fan_min
, 1),
329 SENSOR_ATTR(fan3_min
, S_IWUSR
| S_IRUGO
, show_fan_min
, set_fan_min
, 2),
332 #define FAN_UNIT_ATTRS(X) \
333 &fan_input[X].dev_attr.attr, \
334 &fan_status[X].dev_attr.attr, \
335 &fan_div[X].dev_attr.attr, \
336 &fan_min[X].dev_attr.attr
338 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
340 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
341 struct pc87360_data
*data
= pc87360_update_device(dev
);
342 return sprintf(buf
, "%u\n",
343 PWM_FROM_REG(data
->pwm
[attr
->index
],
344 FAN_CONFIG_INVERT(data
->fan_conf
,
347 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
350 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
351 struct pc87360_data
*data
= dev_get_drvdata(dev
);
352 long val
= simple_strtol(buf
, NULL
, 10);
354 mutex_lock(&data
->update_lock
);
355 data
->pwm
[attr
->index
] = PWM_TO_REG(val
,
356 FAN_CONFIG_INVERT(data
->fan_conf
, attr
->index
));
357 pc87360_write_value(data
, LD_FAN
, NO_BANK
, PC87360_REG_PWM(attr
->index
),
358 data
->pwm
[attr
->index
]);
359 mutex_unlock(&data
->update_lock
);
363 static struct sensor_device_attribute pwm
[] = {
364 SENSOR_ATTR(pwm1
, S_IWUSR
| S_IRUGO
, show_pwm
, set_pwm
, 0),
365 SENSOR_ATTR(pwm2
, S_IWUSR
| S_IRUGO
, show_pwm
, set_pwm
, 1),
366 SENSOR_ATTR(pwm3
, S_IWUSR
| S_IRUGO
, show_pwm
, set_pwm
, 2),
369 static struct attribute
* pc8736x_fan_attr_array
[] = {
373 &pwm
[0].dev_attr
.attr
,
374 &pwm
[1].dev_attr
.attr
,
375 &pwm
[2].dev_attr
.attr
,
378 static const struct attribute_group pc8736x_fan_group
= {
379 .attrs
= pc8736x_fan_attr_array
,
382 static ssize_t
show_in_input(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
384 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
385 struct pc87360_data
*data
= pc87360_update_device(dev
);
386 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in
[attr
->index
],
389 static ssize_t
show_in_min(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
391 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
392 struct pc87360_data
*data
= pc87360_update_device(dev
);
393 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in_min
[attr
->index
],
396 static ssize_t
show_in_max(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
398 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
399 struct pc87360_data
*data
= pc87360_update_device(dev
);
400 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in_max
[attr
->index
],
403 static ssize_t
show_in_status(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
405 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
406 struct pc87360_data
*data
= pc87360_update_device(dev
);
407 return sprintf(buf
, "%u\n", data
->in_status
[attr
->index
]);
409 static ssize_t
set_in_min(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
412 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
413 struct pc87360_data
*data
= dev_get_drvdata(dev
);
414 long val
= simple_strtol(buf
, NULL
, 10);
416 mutex_lock(&data
->update_lock
);
417 data
->in_min
[attr
->index
] = IN_TO_REG(val
, data
->in_vref
);
418 pc87360_write_value(data
, LD_IN
, attr
->index
, PC87365_REG_IN_MIN
,
419 data
->in_min
[attr
->index
]);
420 mutex_unlock(&data
->update_lock
);
423 static ssize_t
set_in_max(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
426 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
427 struct pc87360_data
*data
= dev_get_drvdata(dev
);
428 long val
= simple_strtol(buf
, NULL
, 10);
430 mutex_lock(&data
->update_lock
);
431 data
->in_max
[attr
->index
] = IN_TO_REG(val
,
433 pc87360_write_value(data
, LD_IN
, attr
->index
, PC87365_REG_IN_MAX
,
434 data
->in_max
[attr
->index
]);
435 mutex_unlock(&data
->update_lock
);
439 static struct sensor_device_attribute in_input
[] = {
440 SENSOR_ATTR(in0_input
, S_IRUGO
, show_in_input
, NULL
, 0),
441 SENSOR_ATTR(in1_input
, S_IRUGO
, show_in_input
, NULL
, 1),
442 SENSOR_ATTR(in2_input
, S_IRUGO
, show_in_input
, NULL
, 2),
443 SENSOR_ATTR(in3_input
, S_IRUGO
, show_in_input
, NULL
, 3),
444 SENSOR_ATTR(in4_input
, S_IRUGO
, show_in_input
, NULL
, 4),
445 SENSOR_ATTR(in5_input
, S_IRUGO
, show_in_input
, NULL
, 5),
446 SENSOR_ATTR(in6_input
, S_IRUGO
, show_in_input
, NULL
, 6),
447 SENSOR_ATTR(in7_input
, S_IRUGO
, show_in_input
, NULL
, 7),
448 SENSOR_ATTR(in8_input
, S_IRUGO
, show_in_input
, NULL
, 8),
449 SENSOR_ATTR(in9_input
, S_IRUGO
, show_in_input
, NULL
, 9),
450 SENSOR_ATTR(in10_input
, S_IRUGO
, show_in_input
, NULL
, 10),
452 static struct sensor_device_attribute in_status
[] = {
453 SENSOR_ATTR(in0_status
, S_IRUGO
, show_in_status
, NULL
, 0),
454 SENSOR_ATTR(in1_status
, S_IRUGO
, show_in_status
, NULL
, 1),
455 SENSOR_ATTR(in2_status
, S_IRUGO
, show_in_status
, NULL
, 2),
456 SENSOR_ATTR(in3_status
, S_IRUGO
, show_in_status
, NULL
, 3),
457 SENSOR_ATTR(in4_status
, S_IRUGO
, show_in_status
, NULL
, 4),
458 SENSOR_ATTR(in5_status
, S_IRUGO
, show_in_status
, NULL
, 5),
459 SENSOR_ATTR(in6_status
, S_IRUGO
, show_in_status
, NULL
, 6),
460 SENSOR_ATTR(in7_status
, S_IRUGO
, show_in_status
, NULL
, 7),
461 SENSOR_ATTR(in8_status
, S_IRUGO
, show_in_status
, NULL
, 8),
462 SENSOR_ATTR(in9_status
, S_IRUGO
, show_in_status
, NULL
, 9),
463 SENSOR_ATTR(in10_status
, S_IRUGO
, show_in_status
, NULL
, 10),
465 static struct sensor_device_attribute in_min
[] = {
466 SENSOR_ATTR(in0_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 0),
467 SENSOR_ATTR(in1_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 1),
468 SENSOR_ATTR(in2_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 2),
469 SENSOR_ATTR(in3_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 3),
470 SENSOR_ATTR(in4_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 4),
471 SENSOR_ATTR(in5_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 5),
472 SENSOR_ATTR(in6_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 6),
473 SENSOR_ATTR(in7_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 7),
474 SENSOR_ATTR(in8_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 8),
475 SENSOR_ATTR(in9_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 9),
476 SENSOR_ATTR(in10_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 10),
478 static struct sensor_device_attribute in_max
[] = {
479 SENSOR_ATTR(in0_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 0),
480 SENSOR_ATTR(in1_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 1),
481 SENSOR_ATTR(in2_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 2),
482 SENSOR_ATTR(in3_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 3),
483 SENSOR_ATTR(in4_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 4),
484 SENSOR_ATTR(in5_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 5),
485 SENSOR_ATTR(in6_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 6),
486 SENSOR_ATTR(in7_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 7),
487 SENSOR_ATTR(in8_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 8),
488 SENSOR_ATTR(in9_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 9),
489 SENSOR_ATTR(in10_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 10),
492 #define VIN_UNIT_ATTRS(X) \
493 &in_input[X].dev_attr.attr, \
494 &in_status[X].dev_attr.attr, \
495 &in_min[X].dev_attr.attr, \
496 &in_max[X].dev_attr.attr
498 static ssize_t
show_vid(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
500 struct pc87360_data
*data
= pc87360_update_device(dev
);
501 return sprintf(buf
, "%u\n", vid_from_reg(data
->vid
, data
->vrm
));
503 static DEVICE_ATTR(cpu0_vid
, S_IRUGO
, show_vid
, NULL
);
505 static ssize_t
show_vrm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
507 struct pc87360_data
*data
= dev_get_drvdata(dev
);
508 return sprintf(buf
, "%u\n", data
->vrm
);
510 static ssize_t
set_vrm(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
512 struct pc87360_data
*data
= dev_get_drvdata(dev
);
513 data
->vrm
= simple_strtoul(buf
, NULL
, 10);
516 static DEVICE_ATTR(vrm
, S_IRUGO
| S_IWUSR
, show_vrm
, set_vrm
);
518 static ssize_t
show_in_alarms(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
520 struct pc87360_data
*data
= pc87360_update_device(dev
);
521 return sprintf(buf
, "%u\n", data
->in_alarms
);
523 static DEVICE_ATTR(alarms_in
, S_IRUGO
, show_in_alarms
, NULL
);
525 static struct attribute
*pc8736x_vin_attr_array
[] = {
537 &dev_attr_cpu0_vid
.attr
,
539 &dev_attr_alarms_in
.attr
,
542 static const struct attribute_group pc8736x_vin_group
= {
543 .attrs
= pc8736x_vin_attr_array
,
546 static ssize_t
show_therm_input(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
548 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
549 struct pc87360_data
*data
= pc87360_update_device(dev
);
550 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in
[attr
->index
],
553 static ssize_t
show_therm_min(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
555 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
556 struct pc87360_data
*data
= pc87360_update_device(dev
);
557 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in_min
[attr
->index
],
560 static ssize_t
show_therm_max(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
562 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
563 struct pc87360_data
*data
= pc87360_update_device(dev
);
564 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in_max
[attr
->index
],
567 static ssize_t
show_therm_crit(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
569 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
570 struct pc87360_data
*data
= pc87360_update_device(dev
);
571 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in_crit
[attr
->index
-11],
574 static ssize_t
show_therm_status(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
576 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
577 struct pc87360_data
*data
= pc87360_update_device(dev
);
578 return sprintf(buf
, "%u\n", data
->in_status
[attr
->index
]);
580 static ssize_t
set_therm_min(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
583 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
584 struct pc87360_data
*data
= dev_get_drvdata(dev
);
585 long val
= simple_strtol(buf
, NULL
, 10);
587 mutex_lock(&data
->update_lock
);
588 data
->in_min
[attr
->index
] = IN_TO_REG(val
, data
->in_vref
);
589 pc87360_write_value(data
, LD_IN
, attr
->index
, PC87365_REG_TEMP_MIN
,
590 data
->in_min
[attr
->index
]);
591 mutex_unlock(&data
->update_lock
);
594 static ssize_t
set_therm_max(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
597 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
598 struct pc87360_data
*data
= dev_get_drvdata(dev
);
599 long val
= simple_strtol(buf
, NULL
, 10);
601 mutex_lock(&data
->update_lock
);
602 data
->in_max
[attr
->index
] = IN_TO_REG(val
, data
->in_vref
);
603 pc87360_write_value(data
, LD_IN
, attr
->index
, PC87365_REG_TEMP_MAX
,
604 data
->in_max
[attr
->index
]);
605 mutex_unlock(&data
->update_lock
);
608 static ssize_t
set_therm_crit(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
611 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
612 struct pc87360_data
*data
= dev_get_drvdata(dev
);
613 long val
= simple_strtol(buf
, NULL
, 10);
615 mutex_lock(&data
->update_lock
);
616 data
->in_crit
[attr
->index
-11] = IN_TO_REG(val
, data
->in_vref
);
617 pc87360_write_value(data
, LD_IN
, attr
->index
, PC87365_REG_TEMP_CRIT
,
618 data
->in_crit
[attr
->index
-11]);
619 mutex_unlock(&data
->update_lock
);
623 /* the +11 term below reflects the fact that VLM units 11,12,13 are
624 used in the chip to measure voltage across the thermistors
626 static struct sensor_device_attribute therm_input
[] = {
627 SENSOR_ATTR(temp4_input
, S_IRUGO
, show_therm_input
, NULL
, 0+11),
628 SENSOR_ATTR(temp5_input
, S_IRUGO
, show_therm_input
, NULL
, 1+11),
629 SENSOR_ATTR(temp6_input
, S_IRUGO
, show_therm_input
, NULL
, 2+11),
631 static struct sensor_device_attribute therm_status
[] = {
632 SENSOR_ATTR(temp4_status
, S_IRUGO
, show_therm_status
, NULL
, 0+11),
633 SENSOR_ATTR(temp5_status
, S_IRUGO
, show_therm_status
, NULL
, 1+11),
634 SENSOR_ATTR(temp6_status
, S_IRUGO
, show_therm_status
, NULL
, 2+11),
636 static struct sensor_device_attribute therm_min
[] = {
637 SENSOR_ATTR(temp4_min
, S_IRUGO
| S_IWUSR
,
638 show_therm_min
, set_therm_min
, 0+11),
639 SENSOR_ATTR(temp5_min
, S_IRUGO
| S_IWUSR
,
640 show_therm_min
, set_therm_min
, 1+11),
641 SENSOR_ATTR(temp6_min
, S_IRUGO
| S_IWUSR
,
642 show_therm_min
, set_therm_min
, 2+11),
644 static struct sensor_device_attribute therm_max
[] = {
645 SENSOR_ATTR(temp4_max
, S_IRUGO
| S_IWUSR
,
646 show_therm_max
, set_therm_max
, 0+11),
647 SENSOR_ATTR(temp5_max
, S_IRUGO
| S_IWUSR
,
648 show_therm_max
, set_therm_max
, 1+11),
649 SENSOR_ATTR(temp6_max
, S_IRUGO
| S_IWUSR
,
650 show_therm_max
, set_therm_max
, 2+11),
652 static struct sensor_device_attribute therm_crit
[] = {
653 SENSOR_ATTR(temp4_crit
, S_IRUGO
| S_IWUSR
,
654 show_therm_crit
, set_therm_crit
, 0+11),
655 SENSOR_ATTR(temp5_crit
, S_IRUGO
| S_IWUSR
,
656 show_therm_crit
, set_therm_crit
, 1+11),
657 SENSOR_ATTR(temp6_crit
, S_IRUGO
| S_IWUSR
,
658 show_therm_crit
, set_therm_crit
, 2+11),
661 #define THERM_UNIT_ATTRS(X) \
662 &therm_input[X].dev_attr.attr, \
663 &therm_status[X].dev_attr.attr, \
664 &therm_min[X].dev_attr.attr, \
665 &therm_max[X].dev_attr.attr, \
666 &therm_crit[X].dev_attr.attr
668 static struct attribute
* pc8736x_therm_attr_array
[] = {
674 static const struct attribute_group pc8736x_therm_group
= {
675 .attrs
= pc8736x_therm_attr_array
,
678 static ssize_t
show_temp_input(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
680 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
681 struct pc87360_data
*data
= pc87360_update_device(dev
);
682 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp
[attr
->index
]));
684 static ssize_t
show_temp_min(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
686 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
687 struct pc87360_data
*data
= pc87360_update_device(dev
);
688 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[attr
->index
]));
690 static ssize_t
show_temp_max(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
692 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
693 struct pc87360_data
*data
= pc87360_update_device(dev
);
694 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max
[attr
->index
]));
696 static ssize_t
show_temp_crit(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
698 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
699 struct pc87360_data
*data
= pc87360_update_device(dev
);
700 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_crit
[attr
->index
]));
702 static ssize_t
show_temp_status(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
704 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
705 struct pc87360_data
*data
= pc87360_update_device(dev
);
706 return sprintf(buf
, "%d\n", data
->temp_status
[attr
->index
]);
708 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
711 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
712 struct pc87360_data
*data
= dev_get_drvdata(dev
);
713 long val
= simple_strtol(buf
, NULL
, 10);
715 mutex_lock(&data
->update_lock
);
716 data
->temp_min
[attr
->index
] = TEMP_TO_REG(val
);
717 pc87360_write_value(data
, LD_TEMP
, attr
->index
, PC87365_REG_TEMP_MIN
,
718 data
->temp_min
[attr
->index
]);
719 mutex_unlock(&data
->update_lock
);
722 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
725 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
726 struct pc87360_data
*data
= dev_get_drvdata(dev
);
727 long val
= simple_strtol(buf
, NULL
, 10);
729 mutex_lock(&data
->update_lock
);
730 data
->temp_max
[attr
->index
] = TEMP_TO_REG(val
);
731 pc87360_write_value(data
, LD_TEMP
, attr
->index
, PC87365_REG_TEMP_MAX
,
732 data
->temp_max
[attr
->index
]);
733 mutex_unlock(&data
->update_lock
);
736 static ssize_t
set_temp_crit(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
739 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
740 struct pc87360_data
*data
= dev_get_drvdata(dev
);
741 long val
= simple_strtol(buf
, NULL
, 10);
743 mutex_lock(&data
->update_lock
);
744 data
->temp_crit
[attr
->index
] = TEMP_TO_REG(val
);
745 pc87360_write_value(data
, LD_TEMP
, attr
->index
, PC87365_REG_TEMP_CRIT
,
746 data
->temp_crit
[attr
->index
]);
747 mutex_unlock(&data
->update_lock
);
751 static struct sensor_device_attribute temp_input
[] = {
752 SENSOR_ATTR(temp1_input
, S_IRUGO
, show_temp_input
, NULL
, 0),
753 SENSOR_ATTR(temp2_input
, S_IRUGO
, show_temp_input
, NULL
, 1),
754 SENSOR_ATTR(temp3_input
, S_IRUGO
, show_temp_input
, NULL
, 2),
756 static struct sensor_device_attribute temp_status
[] = {
757 SENSOR_ATTR(temp1_status
, S_IRUGO
, show_temp_status
, NULL
, 0),
758 SENSOR_ATTR(temp2_status
, S_IRUGO
, show_temp_status
, NULL
, 1),
759 SENSOR_ATTR(temp3_status
, S_IRUGO
, show_temp_status
, NULL
, 2),
761 static struct sensor_device_attribute temp_min
[] = {
762 SENSOR_ATTR(temp1_min
, S_IRUGO
| S_IWUSR
,
763 show_temp_min
, set_temp_min
, 0),
764 SENSOR_ATTR(temp2_min
, S_IRUGO
| S_IWUSR
,
765 show_temp_min
, set_temp_min
, 1),
766 SENSOR_ATTR(temp3_min
, S_IRUGO
| S_IWUSR
,
767 show_temp_min
, set_temp_min
, 2),
769 static struct sensor_device_attribute temp_max
[] = {
770 SENSOR_ATTR(temp1_max
, S_IRUGO
| S_IWUSR
,
771 show_temp_max
, set_temp_max
, 0),
772 SENSOR_ATTR(temp2_max
, S_IRUGO
| S_IWUSR
,
773 show_temp_max
, set_temp_max
, 1),
774 SENSOR_ATTR(temp3_max
, S_IRUGO
| S_IWUSR
,
775 show_temp_max
, set_temp_max
, 2),
777 static struct sensor_device_attribute temp_crit
[] = {
778 SENSOR_ATTR(temp1_crit
, S_IRUGO
| S_IWUSR
,
779 show_temp_crit
, set_temp_crit
, 0),
780 SENSOR_ATTR(temp2_crit
, S_IRUGO
| S_IWUSR
,
781 show_temp_crit
, set_temp_crit
, 1),
782 SENSOR_ATTR(temp3_crit
, S_IRUGO
| S_IWUSR
,
783 show_temp_crit
, set_temp_crit
, 2),
786 static ssize_t
show_temp_alarms(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
788 struct pc87360_data
*data
= pc87360_update_device(dev
);
789 return sprintf(buf
, "%u\n", data
->temp_alarms
);
791 static DEVICE_ATTR(alarms_temp
, S_IRUGO
, show_temp_alarms
, NULL
);
793 #define TEMP_UNIT_ATTRS(X) \
794 &temp_input[X].dev_attr.attr, \
795 &temp_status[X].dev_attr.attr, \
796 &temp_min[X].dev_attr.attr, \
797 &temp_max[X].dev_attr.attr, \
798 &temp_crit[X].dev_attr.attr
800 static struct attribute
* pc8736x_temp_attr_array
[] = {
804 /* include the few miscellaneous atts here */
805 &dev_attr_alarms_temp
.attr
,
808 static const struct attribute_group pc8736x_temp_group
= {
809 .attrs
= pc8736x_temp_attr_array
,
812 static ssize_t
show_name(struct device
*dev
, struct device_attribute
815 struct pc87360_data
*data
= dev_get_drvdata(dev
);
816 return sprintf(buf
, "%s\n", data
->name
);
818 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
821 * Device detection, registration and update
824 static int __init
pc87360_find(int sioaddr
, u8
*devid
, unsigned short *addresses
)
828 int nrdev
; /* logical device count */
830 /* No superio_enter */
832 /* Identify device */
833 val
= force_id
? force_id
: superio_inb(sioaddr
, DEVID
);
835 case 0xE1: /* PC87360 */
836 case 0xE8: /* PC87363 */
837 case 0xE4: /* PC87364 */
840 case 0xE5: /* PC87365 */
841 case 0xE9: /* PC87366 */
845 superio_exit(sioaddr
);
848 /* Remember the device id */
851 for (i
= 0; i
< nrdev
; i
++) {
852 /* select logical device */
853 superio_outb(sioaddr
, DEV
, logdev
[i
]);
855 val
= superio_inb(sioaddr
, ACT
);
857 printk(KERN_INFO
"pc87360: Device 0x%02x not "
858 "activated\n", logdev
[i
]);
862 val
= (superio_inb(sioaddr
, BASE
) << 8)
863 | superio_inb(sioaddr
, BASE
+ 1);
865 printk(KERN_INFO
"pc87360: Base address not set for "
866 "device 0x%02x\n", logdev
[i
]);
872 if (i
==0) { /* Fans */
873 confreg
[0] = superio_inb(sioaddr
, 0xF0);
874 confreg
[1] = superio_inb(sioaddr
, 0xF1);
877 printk(KERN_DEBUG
"pc87360: Fan 1: mon=%d "
878 "ctrl=%d inv=%d\n", (confreg
[0]>>2)&1,
879 (confreg
[0]>>3)&1, (confreg
[0]>>4)&1);
880 printk(KERN_DEBUG
"pc87360: Fan 2: mon=%d "
881 "ctrl=%d inv=%d\n", (confreg
[0]>>5)&1,
882 (confreg
[0]>>6)&1, (confreg
[0]>>7)&1);
883 printk(KERN_DEBUG
"pc87360: Fan 3: mon=%d "
884 "ctrl=%d inv=%d\n", confreg
[1]&1,
885 (confreg
[1]>>1)&1, (confreg
[1]>>2)&1);
887 } else if (i
==1) { /* Voltages */
888 /* Are we using thermistors? */
889 if (*devid
== 0xE9) { /* PC87366 */
890 /* These registers are not logical-device
891 specific, just that we won't need them if
892 we don't use the VLM device */
893 confreg
[2] = superio_inb(sioaddr
, 0x2B);
894 confreg
[3] = superio_inb(sioaddr
, 0x25);
896 if (confreg
[2] & 0x40) {
897 printk(KERN_INFO
"pc87360: Using "
898 "thermistors for temperature "
901 if (confreg
[3] & 0xE0) {
902 printk(KERN_INFO
"pc87360: VID "
903 "inputs routed (mode %u)\n",
910 superio_exit(sioaddr
);
914 static int __devinit
pc87360_probe(struct platform_device
*pdev
)
917 struct pc87360_data
*data
;
919 const char *name
= "pc87360";
920 int use_thermistors
= 0;
921 struct device
*dev
= &pdev
->dev
;
923 if (!(data
= kzalloc(sizeof(struct pc87360_data
), GFP_KERNEL
)))
940 data
->fannr
= extra_isa
[0] ? 3 : 0;
941 data
->innr
= extra_isa
[1] ? 11 : 0;
942 data
->tempnr
= extra_isa
[2] ? 2 : 0;
946 data
->fannr
= extra_isa
[0] ? 3 : 0;
947 data
->innr
= extra_isa
[1] ? 14 : 0;
948 data
->tempnr
= extra_isa
[2] ? 3 : 0;
954 mutex_init(&data
->lock
);
955 mutex_init(&data
->update_lock
);
956 platform_set_drvdata(pdev
, data
);
958 for (i
= 0; i
< 3; i
++) {
959 if (((data
->address
[i
] = extra_isa
[i
]))
960 && !request_region(extra_isa
[i
], PC87360_EXTENT
,
961 pc87360_driver
.driver
.name
)) {
962 dev_err(dev
, "Region 0x%x-0x%x already "
963 "in use!\n", extra_isa
[i
],
964 extra_isa
[i
]+PC87360_EXTENT
-1);
965 for (i
--; i
>= 0; i
--)
966 release_region(extra_isa
[i
], PC87360_EXTENT
);
972 /* Retrieve the fans configuration from Super-I/O space */
974 data
->fan_conf
= confreg
[0] | (confreg
[1] << 8);
976 /* Use the correct reference voltage
977 Unless both the VLM and the TMS logical devices agree to
978 use an external Vref, the internal one is used. */
980 i
= pc87360_read_value(data
, LD_IN
, NO_BANK
,
981 PC87365_REG_IN_CONFIG
);
983 i
&= pc87360_read_value(data
, LD_TEMP
, NO_BANK
,
984 PC87365_REG_TEMP_CONFIG
);
986 data
->in_vref
= (i
&0x02) ? 3025 : 2966;
987 dev_dbg(dev
, "Using %s reference voltage\n",
988 (i
&0x02) ? "external" : "internal");
990 data
->vid_conf
= confreg
[3];
991 data
->vrm
= vid_which_vrm();
994 /* Fan clock dividers may be needed before any data is read */
995 for (i
= 0; i
< data
->fannr
; i
++) {
996 if (FAN_CONFIG_MONITOR(data
->fan_conf
, i
))
997 data
->fan_status
[i
] = pc87360_read_value(data
,
999 PC87360_REG_FAN_STATUS(i
));
1003 if (devid
== 0xe9 && data
->address
[1]) /* PC87366 */
1004 use_thermistors
= confreg
[2] & 0x40;
1006 pc87360_init_device(pdev
, use_thermistors
);
1009 /* Register all-or-nothing sysfs groups */
1012 (err
= sysfs_create_group(&dev
->kobj
,
1013 &pc8736x_vin_group
)))
1016 if (data
->innr
== 14 &&
1017 (err
= sysfs_create_group(&dev
->kobj
,
1018 &pc8736x_therm_group
)))
1021 /* create device attr-files for varying sysfs groups */
1024 for (i
= 0; i
< data
->tempnr
; i
++) {
1025 if ((err
= device_create_file(dev
,
1026 &temp_input
[i
].dev_attr
))
1027 || (err
= device_create_file(dev
,
1028 &temp_min
[i
].dev_attr
))
1029 || (err
= device_create_file(dev
,
1030 &temp_max
[i
].dev_attr
))
1031 || (err
= device_create_file(dev
,
1032 &temp_crit
[i
].dev_attr
))
1033 || (err
= device_create_file(dev
,
1034 &temp_status
[i
].dev_attr
)))
1037 if ((err
= device_create_file(dev
, &dev_attr_alarms_temp
)))
1041 for (i
= 0; i
< data
->fannr
; i
++) {
1042 if (FAN_CONFIG_MONITOR(data
->fan_conf
, i
)
1043 && ((err
= device_create_file(dev
,
1044 &fan_input
[i
].dev_attr
))
1045 || (err
= device_create_file(dev
,
1046 &fan_min
[i
].dev_attr
))
1047 || (err
= device_create_file(dev
,
1048 &fan_div
[i
].dev_attr
))
1049 || (err
= device_create_file(dev
,
1050 &fan_status
[i
].dev_attr
))))
1053 if (FAN_CONFIG_CONTROL(data
->fan_conf
, i
)
1054 && (err
= device_create_file(dev
, &pwm
[i
].dev_attr
)))
1058 if ((err
= device_create_file(dev
, &dev_attr_name
)))
1061 data
->hwmon_dev
= hwmon_device_register(dev
);
1062 if (IS_ERR(data
->hwmon_dev
)) {
1063 err
= PTR_ERR(data
->hwmon_dev
);
1069 device_remove_file(dev
, &dev_attr_name
);
1070 /* can still remove groups whose members were added individually */
1071 sysfs_remove_group(&dev
->kobj
, &pc8736x_temp_group
);
1072 sysfs_remove_group(&dev
->kobj
, &pc8736x_fan_group
);
1073 sysfs_remove_group(&dev
->kobj
, &pc8736x_therm_group
);
1074 sysfs_remove_group(&dev
->kobj
, &pc8736x_vin_group
);
1075 for (i
= 0; i
< 3; i
++) {
1076 if (data
->address
[i
]) {
1077 release_region(data
->address
[i
], PC87360_EXTENT
);
1085 static int __devexit
pc87360_remove(struct platform_device
*pdev
)
1087 struct pc87360_data
*data
= platform_get_drvdata(pdev
);
1090 hwmon_device_unregister(data
->hwmon_dev
);
1092 device_remove_file(&pdev
->dev
, &dev_attr_name
);
1093 sysfs_remove_group(&pdev
->dev
.kobj
, &pc8736x_temp_group
);
1094 sysfs_remove_group(&pdev
->dev
.kobj
, &pc8736x_fan_group
);
1095 sysfs_remove_group(&pdev
->dev
.kobj
, &pc8736x_therm_group
);
1096 sysfs_remove_group(&pdev
->dev
.kobj
, &pc8736x_vin_group
);
1098 for (i
= 0; i
< 3; i
++) {
1099 if (data
->address
[i
]) {
1100 release_region(data
->address
[i
], PC87360_EXTENT
);
1108 /* ldi is the logical device index
1109 bank is for voltages and temperatures only */
1110 static int pc87360_read_value(struct pc87360_data
*data
, u8 ldi
, u8 bank
,
1115 mutex_lock(&(data
->lock
));
1116 if (bank
!= NO_BANK
)
1117 outb_p(bank
, data
->address
[ldi
] + PC87365_REG_BANK
);
1118 res
= inb_p(data
->address
[ldi
] + reg
);
1119 mutex_unlock(&(data
->lock
));
1124 static void pc87360_write_value(struct pc87360_data
*data
, u8 ldi
, u8 bank
,
1127 mutex_lock(&(data
->lock
));
1128 if (bank
!= NO_BANK
)
1129 outb_p(bank
, data
->address
[ldi
] + PC87365_REG_BANK
);
1130 outb_p(value
, data
->address
[ldi
] + reg
);
1131 mutex_unlock(&(data
->lock
));
1134 static void pc87360_init_device(struct platform_device
*pdev
,
1135 int use_thermistors
)
1137 struct pc87360_data
*data
= platform_get_drvdata(pdev
);
1139 const u8 init_in
[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1140 const u8 init_temp
[3] = { 2, 2, 1 };
1143 if (init
>= 2 && data
->innr
) {
1144 reg
= pc87360_read_value(data
, LD_IN
, NO_BANK
,
1145 PC87365_REG_IN_CONVRATE
);
1146 dev_info(&pdev
->dev
, "VLM conversion set to "
1147 "1s period, 160us delay\n");
1148 pc87360_write_value(data
, LD_IN
, NO_BANK
,
1149 PC87365_REG_IN_CONVRATE
,
1150 (reg
& 0xC0) | 0x11);
1153 nr
= data
->innr
< 11 ? data
->innr
: 11;
1154 for (i
= 0; i
< nr
; i
++) {
1155 if (init
>= init_in
[i
]) {
1156 /* Forcibly enable voltage channel */
1157 reg
= pc87360_read_value(data
, LD_IN
, i
,
1158 PC87365_REG_IN_STATUS
);
1159 if (!(reg
& 0x01)) {
1160 dev_dbg(&pdev
->dev
, "Forcibly "
1161 "enabling in%d\n", i
);
1162 pc87360_write_value(data
, LD_IN
, i
,
1163 PC87365_REG_IN_STATUS
,
1164 (reg
& 0x68) | 0x87);
1169 /* We can't blindly trust the Super-I/O space configuration bit,
1170 most BIOS won't set it properly */
1171 for (i
= 11; i
< data
->innr
; i
++) {
1172 reg
= pc87360_read_value(data
, LD_IN
, i
,
1173 PC87365_REG_TEMP_STATUS
);
1174 use_thermistors
= use_thermistors
|| (reg
& 0x01);
1177 i
= use_thermistors
? 2 : 0;
1178 for (; i
< data
->tempnr
; i
++) {
1179 if (init
>= init_temp
[i
]) {
1180 /* Forcibly enable temperature channel */
1181 reg
= pc87360_read_value(data
, LD_TEMP
, i
,
1182 PC87365_REG_TEMP_STATUS
);
1183 if (!(reg
& 0x01)) {
1184 dev_dbg(&pdev
->dev
, "Forcibly "
1185 "enabling temp%d\n", i
+1);
1186 pc87360_write_value(data
, LD_TEMP
, i
,
1187 PC87365_REG_TEMP_STATUS
,
1193 if (use_thermistors
) {
1194 for (i
= 11; i
< data
->innr
; i
++) {
1195 if (init
>= init_in
[i
]) {
1196 /* The pin may already be used by thermal
1198 reg
= pc87360_read_value(data
, LD_TEMP
,
1199 (i
-11)/2, PC87365_REG_TEMP_STATUS
);
1201 dev_dbg(&pdev
->dev
, "Skipping "
1202 "temp%d, pin already in use "
1203 "by temp%d\n", i
-7, (i
-11)/2);
1207 /* Forcibly enable thermistor channel */
1208 reg
= pc87360_read_value(data
, LD_IN
, i
,
1209 PC87365_REG_IN_STATUS
);
1210 if (!(reg
& 0x01)) {
1211 dev_dbg(&pdev
->dev
, "Forcibly "
1212 "enabling temp%d\n", i
-7);
1213 pc87360_write_value(data
, LD_IN
, i
,
1214 PC87365_REG_TEMP_STATUS
,
1215 (reg
& 0x60) | 0x8F);
1222 reg
= pc87360_read_value(data
, LD_IN
, NO_BANK
,
1223 PC87365_REG_IN_CONFIG
);
1225 dev_dbg(&pdev
->dev
, "Forcibly "
1226 "enabling monitoring (VLM)\n");
1227 pc87360_write_value(data
, LD_IN
, NO_BANK
,
1228 PC87365_REG_IN_CONFIG
,
1234 reg
= pc87360_read_value(data
, LD_TEMP
, NO_BANK
,
1235 PC87365_REG_TEMP_CONFIG
);
1237 dev_dbg(&pdev
->dev
, "Forcibly enabling "
1238 "monitoring (TMS)\n");
1239 pc87360_write_value(data
, LD_TEMP
, NO_BANK
,
1240 PC87365_REG_TEMP_CONFIG
,
1245 /* Chip config as documented by National Semi. */
1246 pc87360_write_value(data
, LD_TEMP
, 0xF, 0xA, 0x08);
1247 /* We voluntarily omit the bank here, in case the
1248 sequence itself matters. It shouldn't be a problem,
1249 since nobody else is supposed to access the
1250 device at that point. */
1251 pc87360_write_value(data
, LD_TEMP
, NO_BANK
, 0xB, 0x04);
1252 pc87360_write_value(data
, LD_TEMP
, NO_BANK
, 0xC, 0x35);
1253 pc87360_write_value(data
, LD_TEMP
, NO_BANK
, 0xD, 0x05);
1254 pc87360_write_value(data
, LD_TEMP
, NO_BANK
, 0xE, 0x05);
1259 static void pc87360_autodiv(struct device
*dev
, int nr
)
1261 struct pc87360_data
*data
= dev_get_drvdata(dev
);
1262 u8 old_min
= data
->fan_min
[nr
];
1264 /* Increase clock divider if needed and possible */
1265 if ((data
->fan_status
[nr
] & 0x04) /* overflow flag */
1266 || (data
->fan
[nr
] >= 224)) { /* next to overflow */
1267 if ((data
->fan_status
[nr
] & 0x60) != 0x60) {
1268 data
->fan_status
[nr
] += 0x20;
1269 data
->fan_min
[nr
] >>= 1;
1270 data
->fan
[nr
] >>= 1;
1271 dev_dbg(dev
, "Increasing "
1272 "clock divider to %d for fan %d\n",
1273 FAN_DIV_FROM_REG(data
->fan_status
[nr
]), nr
+1);
1276 /* Decrease clock divider if possible */
1277 while (!(data
->fan_min
[nr
] & 0x80) /* min "nails" divider */
1278 && data
->fan
[nr
] < 85 /* bad accuracy */
1279 && (data
->fan_status
[nr
] & 0x60) != 0x00) {
1280 data
->fan_status
[nr
] -= 0x20;
1281 data
->fan_min
[nr
] <<= 1;
1282 data
->fan
[nr
] <<= 1;
1283 dev_dbg(dev
, "Decreasing "
1284 "clock divider to %d for fan %d\n",
1285 FAN_DIV_FROM_REG(data
->fan_status
[nr
]),
1290 /* Write new fan min if it changed */
1291 if (old_min
!= data
->fan_min
[nr
]) {
1292 pc87360_write_value(data
, LD_FAN
, NO_BANK
,
1293 PC87360_REG_FAN_MIN(nr
),
1298 static struct pc87360_data
*pc87360_update_device(struct device
*dev
)
1300 struct pc87360_data
*data
= dev_get_drvdata(dev
);
1303 mutex_lock(&data
->update_lock
);
1305 if (time_after(jiffies
, data
->last_updated
+ HZ
* 2) || !data
->valid
) {
1306 dev_dbg(dev
, "Data update\n");
1309 for (i
= 0; i
< data
->fannr
; i
++) {
1310 if (FAN_CONFIG_MONITOR(data
->fan_conf
, i
)) {
1311 data
->fan_status
[i
] =
1312 pc87360_read_value(data
, LD_FAN
,
1313 NO_BANK
, PC87360_REG_FAN_STATUS(i
));
1314 data
->fan
[i
] = pc87360_read_value(data
, LD_FAN
,
1315 NO_BANK
, PC87360_REG_FAN(i
));
1316 data
->fan_min
[i
] = pc87360_read_value(data
,
1318 PC87360_REG_FAN_MIN(i
));
1319 /* Change clock divider if needed */
1320 pc87360_autodiv(dev
, i
);
1321 /* Clear bits and write new divider */
1322 pc87360_write_value(data
, LD_FAN
, NO_BANK
,
1323 PC87360_REG_FAN_STATUS(i
),
1324 data
->fan_status
[i
]);
1326 if (FAN_CONFIG_CONTROL(data
->fan_conf
, i
))
1327 data
->pwm
[i
] = pc87360_read_value(data
, LD_FAN
,
1328 NO_BANK
, PC87360_REG_PWM(i
));
1332 for (i
= 0; i
< data
->innr
; i
++) {
1333 data
->in_status
[i
] = pc87360_read_value(data
, LD_IN
, i
,
1334 PC87365_REG_IN_STATUS
);
1336 pc87360_write_value(data
, LD_IN
, i
,
1337 PC87365_REG_IN_STATUS
,
1338 data
->in_status
[i
]);
1339 if ((data
->in_status
[i
] & 0x81) == 0x81) {
1340 data
->in
[i
] = pc87360_read_value(data
, LD_IN
,
1343 if (data
->in_status
[i
] & 0x01) {
1344 data
->in_min
[i
] = pc87360_read_value(data
,
1346 PC87365_REG_IN_MIN
);
1347 data
->in_max
[i
] = pc87360_read_value(data
,
1349 PC87365_REG_IN_MAX
);
1351 data
->in_crit
[i
-11] =
1352 pc87360_read_value(data
, LD_IN
,
1353 i
, PC87365_REG_TEMP_CRIT
);
1357 data
->in_alarms
= pc87360_read_value(data
, LD_IN
,
1358 NO_BANK
, PC87365_REG_IN_ALARMS1
)
1359 | ((pc87360_read_value(data
, LD_IN
,
1360 NO_BANK
, PC87365_REG_IN_ALARMS2
)
1362 data
->vid
= (data
->vid_conf
& 0xE0) ?
1363 pc87360_read_value(data
, LD_IN
,
1364 NO_BANK
, PC87365_REG_VID
) : 0x1F;
1368 for (i
= 0; i
< data
->tempnr
; i
++) {
1369 data
->temp_status
[i
] = pc87360_read_value(data
,
1371 PC87365_REG_TEMP_STATUS
);
1373 pc87360_write_value(data
, LD_TEMP
, i
,
1374 PC87365_REG_TEMP_STATUS
,
1375 data
->temp_status
[i
]);
1376 if ((data
->temp_status
[i
] & 0x81) == 0x81) {
1377 data
->temp
[i
] = pc87360_read_value(data
,
1381 if (data
->temp_status
[i
] & 0x01) {
1382 data
->temp_min
[i
] = pc87360_read_value(data
,
1384 PC87365_REG_TEMP_MIN
);
1385 data
->temp_max
[i
] = pc87360_read_value(data
,
1387 PC87365_REG_TEMP_MAX
);
1388 data
->temp_crit
[i
] = pc87360_read_value(data
,
1390 PC87365_REG_TEMP_CRIT
);
1394 data
->temp_alarms
= pc87360_read_value(data
, LD_TEMP
,
1395 NO_BANK
, PC87365_REG_TEMP_ALARMS
)
1399 data
->last_updated
= jiffies
;
1403 mutex_unlock(&data
->update_lock
);
1408 static int __init
pc87360_device_add(unsigned short address
)
1410 struct resource res
= {
1412 .flags
= IORESOURCE_IO
,
1416 pdev
= platform_device_alloc("pc87360", address
);
1419 printk(KERN_ERR
"pc87360: Device allocation failed\n");
1423 for (i
= 0; i
< 3; i
++) {
1426 res
.start
= extra_isa
[i
];
1427 res
.end
= extra_isa
[i
] + PC87360_EXTENT
- 1;
1428 err
= platform_device_add_resources(pdev
, &res
, 1);
1430 printk(KERN_ERR
"pc87360: Device resource[%d] "
1431 "addition failed (%d)\n", i
, err
);
1432 goto exit_device_put
;
1436 err
= platform_device_add(pdev
);
1438 printk(KERN_ERR
"pc87360: Device addition failed (%d)\n",
1440 goto exit_device_put
;
1446 platform_device_put(pdev
);
1451 static int __init
pc87360_init(void)
1454 unsigned short address
= 0;
1456 if (pc87360_find(0x2e, &devid
, extra_isa
)
1457 && pc87360_find(0x4e, &devid
, extra_isa
)) {
1458 printk(KERN_WARNING
"pc87360: PC8736x not detected, "
1459 "module not inserted.\n");
1463 /* Arbitrarily pick one of the addresses */
1464 for (i
= 0; i
< 3; i
++) {
1465 if (extra_isa
[i
] != 0x0000) {
1466 address
= extra_isa
[i
];
1471 if (address
== 0x0000) {
1472 printk(KERN_WARNING
"pc87360: No active logical device, "
1473 "module not inserted.\n");
1477 err
= platform_driver_register(&pc87360_driver
);
1481 /* Sets global pdev as a side effect */
1482 err
= pc87360_device_add(address
);
1489 platform_driver_unregister(&pc87360_driver
);
1494 static void __exit
pc87360_exit(void)
1496 platform_device_unregister(pdev
);
1497 platform_driver_unregister(&pc87360_driver
);
1501 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1502 MODULE_DESCRIPTION("PC8736x hardware monitor");
1503 MODULE_LICENSE("GPL");
1505 module_init(pc87360_init
);
1506 module_exit(pc87360_exit
);