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>
46 #include <linux/acpi.h>
50 static struct platform_device
*pdev
;
51 static unsigned short extra_isa
[3];
55 module_param(init
, int, 0);
56 MODULE_PARM_DESC(init
,
57 "Chip initialization level:\n"
59 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
60 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
61 " 3: Forcibly enable all voltage and temperature channels, including in9");
64 * Super-I/O registers and operations
67 #define DEV 0x07 /* Register: Logical device select */
68 #define DEVID 0x20 /* Register: Device ID */
69 #define ACT 0x30 /* Register: Device activation */
70 #define BASE 0x60 /* Register: Base address */
72 #define FSCM 0x09 /* Logical device: fans */
73 #define VLM 0x0d /* Logical device: voltages */
74 #define TMS 0x0e /* Logical device: temperatures */
75 static const u8 logdev
[3] = { FSCM
, VLM
, TMS
};
81 static inline void superio_outb(int sioaddr
, int reg
, int val
)
87 static inline int superio_inb(int sioaddr
, int reg
)
90 return inb(sioaddr
+1);
93 static inline void superio_exit(int sioaddr
)
96 outb(0x02, sioaddr
+1);
103 #define PC87360_EXTENT 0x10
104 #define PC87365_REG_BANK 0x09
108 * Fan registers and conversions
111 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
112 #define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
113 #define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
114 #define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
115 #define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
116 #define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
118 #define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
119 480000 / ((val)*(div)))
120 #define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
121 480000 / ((val)*(div)))
122 #define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
123 #define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
125 #define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
126 #define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
127 #define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
129 #define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
130 static inline u8
PWM_TO_REG(int val
, int inv
)
142 * Voltage registers and conversions
145 #define PC87365_REG_IN_CONVRATE 0x07
146 #define PC87365_REG_IN_CONFIG 0x08
147 #define PC87365_REG_IN 0x0B
148 #define PC87365_REG_IN_MIN 0x0D
149 #define PC87365_REG_IN_MAX 0x0C
150 #define PC87365_REG_IN_STATUS 0x0A
151 #define PC87365_REG_IN_ALARMS1 0x00
152 #define PC87365_REG_IN_ALARMS2 0x01
153 #define PC87365_REG_VID 0x06
155 #define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
156 #define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
157 (val)*256 >= (ref)*255 ? 255: \
158 ((val) * 256 + (ref)/2) / (ref))
161 * Temperature registers and conversions
164 #define PC87365_REG_TEMP_CONFIG 0x08
165 #define PC87365_REG_TEMP 0x0B
166 #define PC87365_REG_TEMP_MIN 0x0D
167 #define PC87365_REG_TEMP_MAX 0x0C
168 #define PC87365_REG_TEMP_CRIT 0x0E
169 #define PC87365_REG_TEMP_STATUS 0x0A
170 #define PC87365_REG_TEMP_ALARMS 0x00
172 #define TEMP_FROM_REG(val) ((val) * 1000)
173 #define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
174 (val) > 127000 ? 127 : \
175 (val) < 0 ? ((val) - 500) / 1000 : \
176 ((val) + 500) / 1000)
182 struct pc87360_data
{
184 struct device
*hwmon_dev
;
186 struct mutex update_lock
;
187 char valid
; /* !=0 if following fields are valid */
188 unsigned long last_updated
; /* In jiffies */
192 u8 fannr
, innr
, tempnr
;
194 u8 fan
[3]; /* Register value */
195 u8 fan_min
[3]; /* Register value */
196 u8 fan_status
[3]; /* Register value */
197 u8 pwm
[3]; /* Register value */
198 u16 fan_conf
; /* Configuration register values, combined */
200 u16 in_vref
; /* 1 mV/bit */
201 u8 in
[14]; /* Register value */
202 u8 in_min
[14]; /* Register value */
203 u8 in_max
[14]; /* Register value */
204 u8 in_crit
[3]; /* Register value */
205 u8 in_status
[14]; /* Register value */
206 u16 in_alarms
; /* Register values, combined, masked */
207 u8 vid_conf
; /* Configuration register value */
209 u8 vid
; /* Register value */
211 s8 temp
[3]; /* Register value */
212 s8 temp_min
[3]; /* Register value */
213 s8 temp_max
[3]; /* Register value */
214 s8 temp_crit
[3]; /* Register value */
215 u8 temp_status
[3]; /* Register value */
216 u8 temp_alarms
; /* Register value, masked */
220 * Functions declaration
223 static int pc87360_probe(struct platform_device
*pdev
);
224 static int __devexit
pc87360_remove(struct platform_device
*pdev
);
226 static int pc87360_read_value(struct pc87360_data
*data
, u8 ldi
, u8 bank
,
228 static void pc87360_write_value(struct pc87360_data
*data
, u8 ldi
, u8 bank
,
230 static void pc87360_init_device(struct platform_device
*pdev
,
231 int use_thermistors
);
232 static struct pc87360_data
*pc87360_update_device(struct device
*dev
);
238 static struct platform_driver pc87360_driver
= {
240 .owner
= THIS_MODULE
,
243 .probe
= pc87360_probe
,
244 .remove
= __devexit_p(pc87360_remove
),
251 static ssize_t
show_fan_input(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
253 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
254 struct pc87360_data
*data
= pc87360_update_device(dev
);
255 return sprintf(buf
, "%u\n", FAN_FROM_REG(data
->fan
[attr
->index
],
256 FAN_DIV_FROM_REG(data
->fan_status
[attr
->index
])));
258 static ssize_t
show_fan_min(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
260 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
261 struct pc87360_data
*data
= pc87360_update_device(dev
);
262 return sprintf(buf
, "%u\n", FAN_FROM_REG(data
->fan_min
[attr
->index
],
263 FAN_DIV_FROM_REG(data
->fan_status
[attr
->index
])));
265 static ssize_t
show_fan_div(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
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",
270 FAN_DIV_FROM_REG(data
->fan_status
[attr
->index
]));
272 static ssize_t
show_fan_status(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
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",
277 FAN_STATUS_FROM_REG(data
->fan_status
[attr
->index
]));
279 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
282 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
283 struct pc87360_data
*data
= dev_get_drvdata(dev
);
284 long fan_min
= simple_strtol(buf
, NULL
, 10);
286 mutex_lock(&data
->update_lock
);
287 fan_min
= FAN_TO_REG(fan_min
, FAN_DIV_FROM_REG(data
->fan_status
[attr
->index
]));
289 /* If it wouldn't fit, change clock divisor */
291 && (data
->fan_status
[attr
->index
] & 0x60) != 0x60) {
293 data
->fan
[attr
->index
] >>= 1;
294 data
->fan_status
[attr
->index
] += 0x20;
296 data
->fan_min
[attr
->index
] = fan_min
> 255 ? 255 : fan_min
;
297 pc87360_write_value(data
, LD_FAN
, NO_BANK
, PC87360_REG_FAN_MIN(attr
->index
),
298 data
->fan_min
[attr
->index
]);
300 /* Write new divider, preserve alarm bits */
301 pc87360_write_value(data
, LD_FAN
, NO_BANK
, PC87360_REG_FAN_STATUS(attr
->index
),
302 data
->fan_status
[attr
->index
] & 0xF9);
303 mutex_unlock(&data
->update_lock
);
308 static struct sensor_device_attribute fan_input
[] = {
309 SENSOR_ATTR(fan1_input
, S_IRUGO
, show_fan_input
, NULL
, 0),
310 SENSOR_ATTR(fan2_input
, S_IRUGO
, show_fan_input
, NULL
, 1),
311 SENSOR_ATTR(fan3_input
, S_IRUGO
, show_fan_input
, NULL
, 2),
313 static struct sensor_device_attribute fan_status
[] = {
314 SENSOR_ATTR(fan1_status
, S_IRUGO
, show_fan_status
, NULL
, 0),
315 SENSOR_ATTR(fan2_status
, S_IRUGO
, show_fan_status
, NULL
, 1),
316 SENSOR_ATTR(fan3_status
, S_IRUGO
, show_fan_status
, NULL
, 2),
318 static struct sensor_device_attribute fan_div
[] = {
319 SENSOR_ATTR(fan1_div
, S_IRUGO
, show_fan_div
, NULL
, 0),
320 SENSOR_ATTR(fan2_div
, S_IRUGO
, show_fan_div
, NULL
, 1),
321 SENSOR_ATTR(fan3_div
, S_IRUGO
, show_fan_div
, NULL
, 2),
323 static struct sensor_device_attribute fan_min
[] = {
324 SENSOR_ATTR(fan1_min
, S_IWUSR
| S_IRUGO
, show_fan_min
, set_fan_min
, 0),
325 SENSOR_ATTR(fan2_min
, S_IWUSR
| S_IRUGO
, show_fan_min
, set_fan_min
, 1),
326 SENSOR_ATTR(fan3_min
, S_IWUSR
| S_IRUGO
, show_fan_min
, set_fan_min
, 2),
329 #define FAN_UNIT_ATTRS(X) \
330 &fan_input[X].dev_attr.attr, \
331 &fan_status[X].dev_attr.attr, \
332 &fan_div[X].dev_attr.attr, \
333 &fan_min[X].dev_attr.attr
335 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
337 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
338 struct pc87360_data
*data
= pc87360_update_device(dev
);
339 return sprintf(buf
, "%u\n",
340 PWM_FROM_REG(data
->pwm
[attr
->index
],
341 FAN_CONFIG_INVERT(data
->fan_conf
,
344 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
347 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
348 struct pc87360_data
*data
= dev_get_drvdata(dev
);
349 long val
= simple_strtol(buf
, NULL
, 10);
351 mutex_lock(&data
->update_lock
);
352 data
->pwm
[attr
->index
] = PWM_TO_REG(val
,
353 FAN_CONFIG_INVERT(data
->fan_conf
, attr
->index
));
354 pc87360_write_value(data
, LD_FAN
, NO_BANK
, PC87360_REG_PWM(attr
->index
),
355 data
->pwm
[attr
->index
]);
356 mutex_unlock(&data
->update_lock
);
360 static struct sensor_device_attribute pwm
[] = {
361 SENSOR_ATTR(pwm1
, S_IWUSR
| S_IRUGO
, show_pwm
, set_pwm
, 0),
362 SENSOR_ATTR(pwm2
, S_IWUSR
| S_IRUGO
, show_pwm
, set_pwm
, 1),
363 SENSOR_ATTR(pwm3
, S_IWUSR
| S_IRUGO
, show_pwm
, set_pwm
, 2),
366 static struct attribute
* pc8736x_fan_attr_array
[] = {
370 &pwm
[0].dev_attr
.attr
,
371 &pwm
[1].dev_attr
.attr
,
372 &pwm
[2].dev_attr
.attr
,
375 static const struct attribute_group pc8736x_fan_group
= {
376 .attrs
= pc8736x_fan_attr_array
,
379 static ssize_t
show_in_input(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
381 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
382 struct pc87360_data
*data
= pc87360_update_device(dev
);
383 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in
[attr
->index
],
386 static ssize_t
show_in_min(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
388 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
389 struct pc87360_data
*data
= pc87360_update_device(dev
);
390 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in_min
[attr
->index
],
393 static ssize_t
show_in_max(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
395 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
396 struct pc87360_data
*data
= pc87360_update_device(dev
);
397 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in_max
[attr
->index
],
400 static ssize_t
show_in_status(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
402 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
403 struct pc87360_data
*data
= pc87360_update_device(dev
);
404 return sprintf(buf
, "%u\n", data
->in_status
[attr
->index
]);
406 static ssize_t
set_in_min(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
409 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
410 struct pc87360_data
*data
= dev_get_drvdata(dev
);
411 long val
= simple_strtol(buf
, NULL
, 10);
413 mutex_lock(&data
->update_lock
);
414 data
->in_min
[attr
->index
] = IN_TO_REG(val
, data
->in_vref
);
415 pc87360_write_value(data
, LD_IN
, attr
->index
, PC87365_REG_IN_MIN
,
416 data
->in_min
[attr
->index
]);
417 mutex_unlock(&data
->update_lock
);
420 static ssize_t
set_in_max(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
423 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
424 struct pc87360_data
*data
= dev_get_drvdata(dev
);
425 long val
= simple_strtol(buf
, NULL
, 10);
427 mutex_lock(&data
->update_lock
);
428 data
->in_max
[attr
->index
] = IN_TO_REG(val
,
430 pc87360_write_value(data
, LD_IN
, attr
->index
, PC87365_REG_IN_MAX
,
431 data
->in_max
[attr
->index
]);
432 mutex_unlock(&data
->update_lock
);
436 static struct sensor_device_attribute in_input
[] = {
437 SENSOR_ATTR(in0_input
, S_IRUGO
, show_in_input
, NULL
, 0),
438 SENSOR_ATTR(in1_input
, S_IRUGO
, show_in_input
, NULL
, 1),
439 SENSOR_ATTR(in2_input
, S_IRUGO
, show_in_input
, NULL
, 2),
440 SENSOR_ATTR(in3_input
, S_IRUGO
, show_in_input
, NULL
, 3),
441 SENSOR_ATTR(in4_input
, S_IRUGO
, show_in_input
, NULL
, 4),
442 SENSOR_ATTR(in5_input
, S_IRUGO
, show_in_input
, NULL
, 5),
443 SENSOR_ATTR(in6_input
, S_IRUGO
, show_in_input
, NULL
, 6),
444 SENSOR_ATTR(in7_input
, S_IRUGO
, show_in_input
, NULL
, 7),
445 SENSOR_ATTR(in8_input
, S_IRUGO
, show_in_input
, NULL
, 8),
446 SENSOR_ATTR(in9_input
, S_IRUGO
, show_in_input
, NULL
, 9),
447 SENSOR_ATTR(in10_input
, S_IRUGO
, show_in_input
, NULL
, 10),
449 static struct sensor_device_attribute in_status
[] = {
450 SENSOR_ATTR(in0_status
, S_IRUGO
, show_in_status
, NULL
, 0),
451 SENSOR_ATTR(in1_status
, S_IRUGO
, show_in_status
, NULL
, 1),
452 SENSOR_ATTR(in2_status
, S_IRUGO
, show_in_status
, NULL
, 2),
453 SENSOR_ATTR(in3_status
, S_IRUGO
, show_in_status
, NULL
, 3),
454 SENSOR_ATTR(in4_status
, S_IRUGO
, show_in_status
, NULL
, 4),
455 SENSOR_ATTR(in5_status
, S_IRUGO
, show_in_status
, NULL
, 5),
456 SENSOR_ATTR(in6_status
, S_IRUGO
, show_in_status
, NULL
, 6),
457 SENSOR_ATTR(in7_status
, S_IRUGO
, show_in_status
, NULL
, 7),
458 SENSOR_ATTR(in8_status
, S_IRUGO
, show_in_status
, NULL
, 8),
459 SENSOR_ATTR(in9_status
, S_IRUGO
, show_in_status
, NULL
, 9),
460 SENSOR_ATTR(in10_status
, S_IRUGO
, show_in_status
, NULL
, 10),
462 static struct sensor_device_attribute in_min
[] = {
463 SENSOR_ATTR(in0_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 0),
464 SENSOR_ATTR(in1_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 1),
465 SENSOR_ATTR(in2_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 2),
466 SENSOR_ATTR(in3_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 3),
467 SENSOR_ATTR(in4_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 4),
468 SENSOR_ATTR(in5_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 5),
469 SENSOR_ATTR(in6_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 6),
470 SENSOR_ATTR(in7_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 7),
471 SENSOR_ATTR(in8_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 8),
472 SENSOR_ATTR(in9_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 9),
473 SENSOR_ATTR(in10_min
, S_IWUSR
| S_IRUGO
, show_in_min
, set_in_min
, 10),
475 static struct sensor_device_attribute in_max
[] = {
476 SENSOR_ATTR(in0_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 0),
477 SENSOR_ATTR(in1_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 1),
478 SENSOR_ATTR(in2_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 2),
479 SENSOR_ATTR(in3_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 3),
480 SENSOR_ATTR(in4_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 4),
481 SENSOR_ATTR(in5_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 5),
482 SENSOR_ATTR(in6_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 6),
483 SENSOR_ATTR(in7_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 7),
484 SENSOR_ATTR(in8_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 8),
485 SENSOR_ATTR(in9_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 9),
486 SENSOR_ATTR(in10_max
, S_IWUSR
| S_IRUGO
, show_in_max
, set_in_max
, 10),
489 #define VIN_UNIT_ATTRS(X) \
490 &in_input[X].dev_attr.attr, \
491 &in_status[X].dev_attr.attr, \
492 &in_min[X].dev_attr.attr, \
493 &in_max[X].dev_attr.attr
495 static ssize_t
show_vid(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
497 struct pc87360_data
*data
= pc87360_update_device(dev
);
498 return sprintf(buf
, "%u\n", vid_from_reg(data
->vid
, data
->vrm
));
500 static DEVICE_ATTR(cpu0_vid
, S_IRUGO
, show_vid
, NULL
);
502 static ssize_t
show_vrm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
504 struct pc87360_data
*data
= dev_get_drvdata(dev
);
505 return sprintf(buf
, "%u\n", data
->vrm
);
507 static ssize_t
set_vrm(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
509 struct pc87360_data
*data
= dev_get_drvdata(dev
);
510 data
->vrm
= simple_strtoul(buf
, NULL
, 10);
513 static DEVICE_ATTR(vrm
, S_IRUGO
| S_IWUSR
, show_vrm
, set_vrm
);
515 static ssize_t
show_in_alarms(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
517 struct pc87360_data
*data
= pc87360_update_device(dev
);
518 return sprintf(buf
, "%u\n", data
->in_alarms
);
520 static DEVICE_ATTR(alarms_in
, S_IRUGO
, show_in_alarms
, NULL
);
522 static struct attribute
*pc8736x_vin_attr_array
[] = {
534 &dev_attr_cpu0_vid
.attr
,
536 &dev_attr_alarms_in
.attr
,
539 static const struct attribute_group pc8736x_vin_group
= {
540 .attrs
= pc8736x_vin_attr_array
,
543 static ssize_t
show_therm_input(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
545 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
546 struct pc87360_data
*data
= pc87360_update_device(dev
);
547 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in
[attr
->index
],
550 static ssize_t
show_therm_min(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
552 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
553 struct pc87360_data
*data
= pc87360_update_device(dev
);
554 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in_min
[attr
->index
],
557 static ssize_t
show_therm_max(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
559 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
560 struct pc87360_data
*data
= pc87360_update_device(dev
);
561 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in_max
[attr
->index
],
564 static ssize_t
show_therm_crit(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
566 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
567 struct pc87360_data
*data
= pc87360_update_device(dev
);
568 return sprintf(buf
, "%u\n", IN_FROM_REG(data
->in_crit
[attr
->index
-11],
571 static ssize_t
show_therm_status(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
573 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
574 struct pc87360_data
*data
= pc87360_update_device(dev
);
575 return sprintf(buf
, "%u\n", data
->in_status
[attr
->index
]);
577 static ssize_t
set_therm_min(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
580 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
581 struct pc87360_data
*data
= dev_get_drvdata(dev
);
582 long val
= simple_strtol(buf
, NULL
, 10);
584 mutex_lock(&data
->update_lock
);
585 data
->in_min
[attr
->index
] = IN_TO_REG(val
, data
->in_vref
);
586 pc87360_write_value(data
, LD_IN
, attr
->index
, PC87365_REG_TEMP_MIN
,
587 data
->in_min
[attr
->index
]);
588 mutex_unlock(&data
->update_lock
);
591 static ssize_t
set_therm_max(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
594 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
595 struct pc87360_data
*data
= dev_get_drvdata(dev
);
596 long val
= simple_strtol(buf
, NULL
, 10);
598 mutex_lock(&data
->update_lock
);
599 data
->in_max
[attr
->index
] = IN_TO_REG(val
, data
->in_vref
);
600 pc87360_write_value(data
, LD_IN
, attr
->index
, PC87365_REG_TEMP_MAX
,
601 data
->in_max
[attr
->index
]);
602 mutex_unlock(&data
->update_lock
);
605 static ssize_t
set_therm_crit(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
608 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
609 struct pc87360_data
*data
= dev_get_drvdata(dev
);
610 long val
= simple_strtol(buf
, NULL
, 10);
612 mutex_lock(&data
->update_lock
);
613 data
->in_crit
[attr
->index
-11] = IN_TO_REG(val
, data
->in_vref
);
614 pc87360_write_value(data
, LD_IN
, attr
->index
, PC87365_REG_TEMP_CRIT
,
615 data
->in_crit
[attr
->index
-11]);
616 mutex_unlock(&data
->update_lock
);
620 /* the +11 term below reflects the fact that VLM units 11,12,13 are
621 used in the chip to measure voltage across the thermistors
623 static struct sensor_device_attribute therm_input
[] = {
624 SENSOR_ATTR(temp4_input
, S_IRUGO
, show_therm_input
, NULL
, 0+11),
625 SENSOR_ATTR(temp5_input
, S_IRUGO
, show_therm_input
, NULL
, 1+11),
626 SENSOR_ATTR(temp6_input
, S_IRUGO
, show_therm_input
, NULL
, 2+11),
628 static struct sensor_device_attribute therm_status
[] = {
629 SENSOR_ATTR(temp4_status
, S_IRUGO
, show_therm_status
, NULL
, 0+11),
630 SENSOR_ATTR(temp5_status
, S_IRUGO
, show_therm_status
, NULL
, 1+11),
631 SENSOR_ATTR(temp6_status
, S_IRUGO
, show_therm_status
, NULL
, 2+11),
633 static struct sensor_device_attribute therm_min
[] = {
634 SENSOR_ATTR(temp4_min
, S_IRUGO
| S_IWUSR
,
635 show_therm_min
, set_therm_min
, 0+11),
636 SENSOR_ATTR(temp5_min
, S_IRUGO
| S_IWUSR
,
637 show_therm_min
, set_therm_min
, 1+11),
638 SENSOR_ATTR(temp6_min
, S_IRUGO
| S_IWUSR
,
639 show_therm_min
, set_therm_min
, 2+11),
641 static struct sensor_device_attribute therm_max
[] = {
642 SENSOR_ATTR(temp4_max
, S_IRUGO
| S_IWUSR
,
643 show_therm_max
, set_therm_max
, 0+11),
644 SENSOR_ATTR(temp5_max
, S_IRUGO
| S_IWUSR
,
645 show_therm_max
, set_therm_max
, 1+11),
646 SENSOR_ATTR(temp6_max
, S_IRUGO
| S_IWUSR
,
647 show_therm_max
, set_therm_max
, 2+11),
649 static struct sensor_device_attribute therm_crit
[] = {
650 SENSOR_ATTR(temp4_crit
, S_IRUGO
| S_IWUSR
,
651 show_therm_crit
, set_therm_crit
, 0+11),
652 SENSOR_ATTR(temp5_crit
, S_IRUGO
| S_IWUSR
,
653 show_therm_crit
, set_therm_crit
, 1+11),
654 SENSOR_ATTR(temp6_crit
, S_IRUGO
| S_IWUSR
,
655 show_therm_crit
, set_therm_crit
, 2+11),
658 #define THERM_UNIT_ATTRS(X) \
659 &therm_input[X].dev_attr.attr, \
660 &therm_status[X].dev_attr.attr, \
661 &therm_min[X].dev_attr.attr, \
662 &therm_max[X].dev_attr.attr, \
663 &therm_crit[X].dev_attr.attr
665 static struct attribute
* pc8736x_therm_attr_array
[] = {
671 static const struct attribute_group pc8736x_therm_group
= {
672 .attrs
= pc8736x_therm_attr_array
,
675 static ssize_t
show_temp_input(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
677 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
678 struct pc87360_data
*data
= pc87360_update_device(dev
);
679 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp
[attr
->index
]));
681 static ssize_t
show_temp_min(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
683 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
684 struct pc87360_data
*data
= pc87360_update_device(dev
);
685 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[attr
->index
]));
687 static ssize_t
show_temp_max(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
689 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
690 struct pc87360_data
*data
= pc87360_update_device(dev
);
691 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max
[attr
->index
]));
693 static ssize_t
show_temp_crit(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
695 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
696 struct pc87360_data
*data
= pc87360_update_device(dev
);
697 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_crit
[attr
->index
]));
699 static ssize_t
show_temp_status(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
701 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
702 struct pc87360_data
*data
= pc87360_update_device(dev
);
703 return sprintf(buf
, "%d\n", data
->temp_status
[attr
->index
]);
705 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
708 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
709 struct pc87360_data
*data
= dev_get_drvdata(dev
);
710 long val
= simple_strtol(buf
, NULL
, 10);
712 mutex_lock(&data
->update_lock
);
713 data
->temp_min
[attr
->index
] = TEMP_TO_REG(val
);
714 pc87360_write_value(data
, LD_TEMP
, attr
->index
, PC87365_REG_TEMP_MIN
,
715 data
->temp_min
[attr
->index
]);
716 mutex_unlock(&data
->update_lock
);
719 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
722 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
723 struct pc87360_data
*data
= dev_get_drvdata(dev
);
724 long val
= simple_strtol(buf
, NULL
, 10);
726 mutex_lock(&data
->update_lock
);
727 data
->temp_max
[attr
->index
] = TEMP_TO_REG(val
);
728 pc87360_write_value(data
, LD_TEMP
, attr
->index
, PC87365_REG_TEMP_MAX
,
729 data
->temp_max
[attr
->index
]);
730 mutex_unlock(&data
->update_lock
);
733 static ssize_t
set_temp_crit(struct device
*dev
, struct device_attribute
*devattr
, const char *buf
,
736 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
737 struct pc87360_data
*data
= dev_get_drvdata(dev
);
738 long val
= simple_strtol(buf
, NULL
, 10);
740 mutex_lock(&data
->update_lock
);
741 data
->temp_crit
[attr
->index
] = TEMP_TO_REG(val
);
742 pc87360_write_value(data
, LD_TEMP
, attr
->index
, PC87365_REG_TEMP_CRIT
,
743 data
->temp_crit
[attr
->index
]);
744 mutex_unlock(&data
->update_lock
);
748 static struct sensor_device_attribute temp_input
[] = {
749 SENSOR_ATTR(temp1_input
, S_IRUGO
, show_temp_input
, NULL
, 0),
750 SENSOR_ATTR(temp2_input
, S_IRUGO
, show_temp_input
, NULL
, 1),
751 SENSOR_ATTR(temp3_input
, S_IRUGO
, show_temp_input
, NULL
, 2),
753 static struct sensor_device_attribute temp_status
[] = {
754 SENSOR_ATTR(temp1_status
, S_IRUGO
, show_temp_status
, NULL
, 0),
755 SENSOR_ATTR(temp2_status
, S_IRUGO
, show_temp_status
, NULL
, 1),
756 SENSOR_ATTR(temp3_status
, S_IRUGO
, show_temp_status
, NULL
, 2),
758 static struct sensor_device_attribute temp_min
[] = {
759 SENSOR_ATTR(temp1_min
, S_IRUGO
| S_IWUSR
,
760 show_temp_min
, set_temp_min
, 0),
761 SENSOR_ATTR(temp2_min
, S_IRUGO
| S_IWUSR
,
762 show_temp_min
, set_temp_min
, 1),
763 SENSOR_ATTR(temp3_min
, S_IRUGO
| S_IWUSR
,
764 show_temp_min
, set_temp_min
, 2),
766 static struct sensor_device_attribute temp_max
[] = {
767 SENSOR_ATTR(temp1_max
, S_IRUGO
| S_IWUSR
,
768 show_temp_max
, set_temp_max
, 0),
769 SENSOR_ATTR(temp2_max
, S_IRUGO
| S_IWUSR
,
770 show_temp_max
, set_temp_max
, 1),
771 SENSOR_ATTR(temp3_max
, S_IRUGO
| S_IWUSR
,
772 show_temp_max
, set_temp_max
, 2),
774 static struct sensor_device_attribute temp_crit
[] = {
775 SENSOR_ATTR(temp1_crit
, S_IRUGO
| S_IWUSR
,
776 show_temp_crit
, set_temp_crit
, 0),
777 SENSOR_ATTR(temp2_crit
, S_IRUGO
| S_IWUSR
,
778 show_temp_crit
, set_temp_crit
, 1),
779 SENSOR_ATTR(temp3_crit
, S_IRUGO
| S_IWUSR
,
780 show_temp_crit
, set_temp_crit
, 2),
783 static ssize_t
show_temp_alarms(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
785 struct pc87360_data
*data
= pc87360_update_device(dev
);
786 return sprintf(buf
, "%u\n", data
->temp_alarms
);
788 static DEVICE_ATTR(alarms_temp
, S_IRUGO
, show_temp_alarms
, NULL
);
790 #define TEMP_UNIT_ATTRS(X) \
791 &temp_input[X].dev_attr.attr, \
792 &temp_status[X].dev_attr.attr, \
793 &temp_min[X].dev_attr.attr, \
794 &temp_max[X].dev_attr.attr, \
795 &temp_crit[X].dev_attr.attr
797 static struct attribute
* pc8736x_temp_attr_array
[] = {
801 /* include the few miscellaneous atts here */
802 &dev_attr_alarms_temp
.attr
,
805 static const struct attribute_group pc8736x_temp_group
= {
806 .attrs
= pc8736x_temp_attr_array
,
809 static ssize_t
show_name(struct device
*dev
, struct device_attribute
812 struct pc87360_data
*data
= dev_get_drvdata(dev
);
813 return sprintf(buf
, "%s\n", data
->name
);
815 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
818 * Device detection, registration and update
821 static int __init
pc87360_find(int sioaddr
, u8
*devid
, unsigned short *addresses
)
825 int nrdev
; /* logical device count */
827 /* No superio_enter */
829 /* Identify device */
830 val
= superio_inb(sioaddr
, DEVID
);
832 case 0xE1: /* PC87360 */
833 case 0xE8: /* PC87363 */
834 case 0xE4: /* PC87364 */
837 case 0xE5: /* PC87365 */
838 case 0xE9: /* PC87366 */
842 superio_exit(sioaddr
);
845 /* Remember the device id */
848 for (i
= 0; i
< nrdev
; i
++) {
849 /* select logical device */
850 superio_outb(sioaddr
, DEV
, logdev
[i
]);
852 val
= superio_inb(sioaddr
, ACT
);
854 printk(KERN_INFO
"pc87360: Device 0x%02x not "
855 "activated\n", logdev
[i
]);
859 val
= (superio_inb(sioaddr
, BASE
) << 8)
860 | superio_inb(sioaddr
, BASE
+ 1);
862 printk(KERN_INFO
"pc87360: Base address not set for "
863 "device 0x%02x\n", logdev
[i
]);
869 if (i
==0) { /* Fans */
870 confreg
[0] = superio_inb(sioaddr
, 0xF0);
871 confreg
[1] = superio_inb(sioaddr
, 0xF1);
874 printk(KERN_DEBUG
"pc87360: Fan 1: mon=%d "
875 "ctrl=%d inv=%d\n", (confreg
[0]>>2)&1,
876 (confreg
[0]>>3)&1, (confreg
[0]>>4)&1);
877 printk(KERN_DEBUG
"pc87360: Fan 2: mon=%d "
878 "ctrl=%d inv=%d\n", (confreg
[0]>>5)&1,
879 (confreg
[0]>>6)&1, (confreg
[0]>>7)&1);
880 printk(KERN_DEBUG
"pc87360: Fan 3: mon=%d "
881 "ctrl=%d inv=%d\n", confreg
[1]&1,
882 (confreg
[1]>>1)&1, (confreg
[1]>>2)&1);
884 } else if (i
==1) { /* Voltages */
885 /* Are we using thermistors? */
886 if (*devid
== 0xE9) { /* PC87366 */
887 /* These registers are not logical-device
888 specific, just that we won't need them if
889 we don't use the VLM device */
890 confreg
[2] = superio_inb(sioaddr
, 0x2B);
891 confreg
[3] = superio_inb(sioaddr
, 0x25);
893 if (confreg
[2] & 0x40) {
894 printk(KERN_INFO
"pc87360: Using "
895 "thermistors for temperature "
898 if (confreg
[3] & 0xE0) {
899 printk(KERN_INFO
"pc87360: VID "
900 "inputs routed (mode %u)\n",
907 superio_exit(sioaddr
);
911 static int __devinit
pc87360_probe(struct platform_device
*pdev
)
914 struct pc87360_data
*data
;
916 const char *name
= "pc87360";
917 int use_thermistors
= 0;
918 struct device
*dev
= &pdev
->dev
;
920 if (!(data
= kzalloc(sizeof(struct pc87360_data
), GFP_KERNEL
)))
937 data
->fannr
= extra_isa
[0] ? 3 : 0;
938 data
->innr
= extra_isa
[1] ? 11 : 0;
939 data
->tempnr
= extra_isa
[2] ? 2 : 0;
943 data
->fannr
= extra_isa
[0] ? 3 : 0;
944 data
->innr
= extra_isa
[1] ? 14 : 0;
945 data
->tempnr
= extra_isa
[2] ? 3 : 0;
951 mutex_init(&data
->lock
);
952 mutex_init(&data
->update_lock
);
953 platform_set_drvdata(pdev
, data
);
955 for (i
= 0; i
< 3; i
++) {
956 if (((data
->address
[i
] = extra_isa
[i
]))
957 && !request_region(extra_isa
[i
], PC87360_EXTENT
,
958 pc87360_driver
.driver
.name
)) {
959 dev_err(dev
, "Region 0x%x-0x%x already "
960 "in use!\n", extra_isa
[i
],
961 extra_isa
[i
]+PC87360_EXTENT
-1);
962 for (i
--; i
>= 0; i
--)
963 release_region(extra_isa
[i
], PC87360_EXTENT
);
969 /* Retrieve the fans configuration from Super-I/O space */
971 data
->fan_conf
= confreg
[0] | (confreg
[1] << 8);
973 /* Use the correct reference voltage
974 Unless both the VLM and the TMS logical devices agree to
975 use an external Vref, the internal one is used. */
977 i
= pc87360_read_value(data
, LD_IN
, NO_BANK
,
978 PC87365_REG_IN_CONFIG
);
980 i
&= pc87360_read_value(data
, LD_TEMP
, NO_BANK
,
981 PC87365_REG_TEMP_CONFIG
);
983 data
->in_vref
= (i
&0x02) ? 3025 : 2966;
984 dev_dbg(dev
, "Using %s reference voltage\n",
985 (i
&0x02) ? "external" : "internal");
987 data
->vid_conf
= confreg
[3];
988 data
->vrm
= vid_which_vrm();
991 /* Fan clock dividers may be needed before any data is read */
992 for (i
= 0; i
< data
->fannr
; i
++) {
993 if (FAN_CONFIG_MONITOR(data
->fan_conf
, i
))
994 data
->fan_status
[i
] = pc87360_read_value(data
,
996 PC87360_REG_FAN_STATUS(i
));
1000 if (devid
== 0xe9 && data
->address
[1]) /* PC87366 */
1001 use_thermistors
= confreg
[2] & 0x40;
1003 pc87360_init_device(pdev
, use_thermistors
);
1006 /* Register all-or-nothing sysfs groups */
1009 (err
= sysfs_create_group(&dev
->kobj
,
1010 &pc8736x_vin_group
)))
1013 if (data
->innr
== 14 &&
1014 (err
= sysfs_create_group(&dev
->kobj
,
1015 &pc8736x_therm_group
)))
1018 /* create device attr-files for varying sysfs groups */
1021 for (i
= 0; i
< data
->tempnr
; i
++) {
1022 if ((err
= device_create_file(dev
,
1023 &temp_input
[i
].dev_attr
))
1024 || (err
= device_create_file(dev
,
1025 &temp_min
[i
].dev_attr
))
1026 || (err
= device_create_file(dev
,
1027 &temp_max
[i
].dev_attr
))
1028 || (err
= device_create_file(dev
,
1029 &temp_crit
[i
].dev_attr
))
1030 || (err
= device_create_file(dev
,
1031 &temp_status
[i
].dev_attr
)))
1034 if ((err
= device_create_file(dev
, &dev_attr_alarms_temp
)))
1038 for (i
= 0; i
< data
->fannr
; i
++) {
1039 if (FAN_CONFIG_MONITOR(data
->fan_conf
, i
)
1040 && ((err
= device_create_file(dev
,
1041 &fan_input
[i
].dev_attr
))
1042 || (err
= device_create_file(dev
,
1043 &fan_min
[i
].dev_attr
))
1044 || (err
= device_create_file(dev
,
1045 &fan_div
[i
].dev_attr
))
1046 || (err
= device_create_file(dev
,
1047 &fan_status
[i
].dev_attr
))))
1050 if (FAN_CONFIG_CONTROL(data
->fan_conf
, i
)
1051 && (err
= device_create_file(dev
, &pwm
[i
].dev_attr
)))
1055 if ((err
= device_create_file(dev
, &dev_attr_name
)))
1058 data
->hwmon_dev
= hwmon_device_register(dev
);
1059 if (IS_ERR(data
->hwmon_dev
)) {
1060 err
= PTR_ERR(data
->hwmon_dev
);
1066 device_remove_file(dev
, &dev_attr_name
);
1067 /* can still remove groups whose members were added individually */
1068 sysfs_remove_group(&dev
->kobj
, &pc8736x_temp_group
);
1069 sysfs_remove_group(&dev
->kobj
, &pc8736x_fan_group
);
1070 sysfs_remove_group(&dev
->kobj
, &pc8736x_therm_group
);
1071 sysfs_remove_group(&dev
->kobj
, &pc8736x_vin_group
);
1072 for (i
= 0; i
< 3; i
++) {
1073 if (data
->address
[i
]) {
1074 release_region(data
->address
[i
], PC87360_EXTENT
);
1082 static int __devexit
pc87360_remove(struct platform_device
*pdev
)
1084 struct pc87360_data
*data
= platform_get_drvdata(pdev
);
1087 hwmon_device_unregister(data
->hwmon_dev
);
1089 device_remove_file(&pdev
->dev
, &dev_attr_name
);
1090 sysfs_remove_group(&pdev
->dev
.kobj
, &pc8736x_temp_group
);
1091 sysfs_remove_group(&pdev
->dev
.kobj
, &pc8736x_fan_group
);
1092 sysfs_remove_group(&pdev
->dev
.kobj
, &pc8736x_therm_group
);
1093 sysfs_remove_group(&pdev
->dev
.kobj
, &pc8736x_vin_group
);
1095 for (i
= 0; i
< 3; i
++) {
1096 if (data
->address
[i
]) {
1097 release_region(data
->address
[i
], PC87360_EXTENT
);
1105 /* ldi is the logical device index
1106 bank is for voltages and temperatures only */
1107 static int pc87360_read_value(struct pc87360_data
*data
, u8 ldi
, u8 bank
,
1112 mutex_lock(&(data
->lock
));
1113 if (bank
!= NO_BANK
)
1114 outb_p(bank
, data
->address
[ldi
] + PC87365_REG_BANK
);
1115 res
= inb_p(data
->address
[ldi
] + reg
);
1116 mutex_unlock(&(data
->lock
));
1121 static void pc87360_write_value(struct pc87360_data
*data
, u8 ldi
, u8 bank
,
1124 mutex_lock(&(data
->lock
));
1125 if (bank
!= NO_BANK
)
1126 outb_p(bank
, data
->address
[ldi
] + PC87365_REG_BANK
);
1127 outb_p(value
, data
->address
[ldi
] + reg
);
1128 mutex_unlock(&(data
->lock
));
1131 static void pc87360_init_device(struct platform_device
*pdev
,
1132 int use_thermistors
)
1134 struct pc87360_data
*data
= platform_get_drvdata(pdev
);
1136 const u8 init_in
[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1137 const u8 init_temp
[3] = { 2, 2, 1 };
1140 if (init
>= 2 && data
->innr
) {
1141 reg
= pc87360_read_value(data
, LD_IN
, NO_BANK
,
1142 PC87365_REG_IN_CONVRATE
);
1143 dev_info(&pdev
->dev
, "VLM conversion set to "
1144 "1s period, 160us delay\n");
1145 pc87360_write_value(data
, LD_IN
, NO_BANK
,
1146 PC87365_REG_IN_CONVRATE
,
1147 (reg
& 0xC0) | 0x11);
1150 nr
= data
->innr
< 11 ? data
->innr
: 11;
1151 for (i
= 0; i
< nr
; i
++) {
1152 if (init
>= init_in
[i
]) {
1153 /* Forcibly enable voltage channel */
1154 reg
= pc87360_read_value(data
, LD_IN
, i
,
1155 PC87365_REG_IN_STATUS
);
1156 if (!(reg
& 0x01)) {
1157 dev_dbg(&pdev
->dev
, "Forcibly "
1158 "enabling in%d\n", i
);
1159 pc87360_write_value(data
, LD_IN
, i
,
1160 PC87365_REG_IN_STATUS
,
1161 (reg
& 0x68) | 0x87);
1166 /* We can't blindly trust the Super-I/O space configuration bit,
1167 most BIOS won't set it properly */
1168 for (i
= 11; i
< data
->innr
; i
++) {
1169 reg
= pc87360_read_value(data
, LD_IN
, i
,
1170 PC87365_REG_TEMP_STATUS
);
1171 use_thermistors
= use_thermistors
|| (reg
& 0x01);
1174 i
= use_thermistors
? 2 : 0;
1175 for (; i
< data
->tempnr
; i
++) {
1176 if (init
>= init_temp
[i
]) {
1177 /* Forcibly enable temperature channel */
1178 reg
= pc87360_read_value(data
, LD_TEMP
, i
,
1179 PC87365_REG_TEMP_STATUS
);
1180 if (!(reg
& 0x01)) {
1181 dev_dbg(&pdev
->dev
, "Forcibly "
1182 "enabling temp%d\n", i
+1);
1183 pc87360_write_value(data
, LD_TEMP
, i
,
1184 PC87365_REG_TEMP_STATUS
,
1190 if (use_thermistors
) {
1191 for (i
= 11; i
< data
->innr
; i
++) {
1192 if (init
>= init_in
[i
]) {
1193 /* The pin may already be used by thermal
1195 reg
= pc87360_read_value(data
, LD_TEMP
,
1196 (i
-11)/2, PC87365_REG_TEMP_STATUS
);
1198 dev_dbg(&pdev
->dev
, "Skipping "
1199 "temp%d, pin already in use "
1200 "by temp%d\n", i
-7, (i
-11)/2);
1204 /* Forcibly enable thermistor channel */
1205 reg
= pc87360_read_value(data
, LD_IN
, i
,
1206 PC87365_REG_IN_STATUS
);
1207 if (!(reg
& 0x01)) {
1208 dev_dbg(&pdev
->dev
, "Forcibly "
1209 "enabling temp%d\n", i
-7);
1210 pc87360_write_value(data
, LD_IN
, i
,
1211 PC87365_REG_TEMP_STATUS
,
1212 (reg
& 0x60) | 0x8F);
1219 reg
= pc87360_read_value(data
, LD_IN
, NO_BANK
,
1220 PC87365_REG_IN_CONFIG
);
1222 dev_dbg(&pdev
->dev
, "Forcibly "
1223 "enabling monitoring (VLM)\n");
1224 pc87360_write_value(data
, LD_IN
, NO_BANK
,
1225 PC87365_REG_IN_CONFIG
,
1231 reg
= pc87360_read_value(data
, LD_TEMP
, NO_BANK
,
1232 PC87365_REG_TEMP_CONFIG
);
1234 dev_dbg(&pdev
->dev
, "Forcibly enabling "
1235 "monitoring (TMS)\n");
1236 pc87360_write_value(data
, LD_TEMP
, NO_BANK
,
1237 PC87365_REG_TEMP_CONFIG
,
1242 /* Chip config as documented by National Semi. */
1243 pc87360_write_value(data
, LD_TEMP
, 0xF, 0xA, 0x08);
1244 /* We voluntarily omit the bank here, in case the
1245 sequence itself matters. It shouldn't be a problem,
1246 since nobody else is supposed to access the
1247 device at that point. */
1248 pc87360_write_value(data
, LD_TEMP
, NO_BANK
, 0xB, 0x04);
1249 pc87360_write_value(data
, LD_TEMP
, NO_BANK
, 0xC, 0x35);
1250 pc87360_write_value(data
, LD_TEMP
, NO_BANK
, 0xD, 0x05);
1251 pc87360_write_value(data
, LD_TEMP
, NO_BANK
, 0xE, 0x05);
1256 static void pc87360_autodiv(struct device
*dev
, int nr
)
1258 struct pc87360_data
*data
= dev_get_drvdata(dev
);
1259 u8 old_min
= data
->fan_min
[nr
];
1261 /* Increase clock divider if needed and possible */
1262 if ((data
->fan_status
[nr
] & 0x04) /* overflow flag */
1263 || (data
->fan
[nr
] >= 224)) { /* next to overflow */
1264 if ((data
->fan_status
[nr
] & 0x60) != 0x60) {
1265 data
->fan_status
[nr
] += 0x20;
1266 data
->fan_min
[nr
] >>= 1;
1267 data
->fan
[nr
] >>= 1;
1268 dev_dbg(dev
, "Increasing "
1269 "clock divider to %d for fan %d\n",
1270 FAN_DIV_FROM_REG(data
->fan_status
[nr
]), nr
+1);
1273 /* Decrease clock divider if possible */
1274 while (!(data
->fan_min
[nr
] & 0x80) /* min "nails" divider */
1275 && data
->fan
[nr
] < 85 /* bad accuracy */
1276 && (data
->fan_status
[nr
] & 0x60) != 0x00) {
1277 data
->fan_status
[nr
] -= 0x20;
1278 data
->fan_min
[nr
] <<= 1;
1279 data
->fan
[nr
] <<= 1;
1280 dev_dbg(dev
, "Decreasing "
1281 "clock divider to %d for fan %d\n",
1282 FAN_DIV_FROM_REG(data
->fan_status
[nr
]),
1287 /* Write new fan min if it changed */
1288 if (old_min
!= data
->fan_min
[nr
]) {
1289 pc87360_write_value(data
, LD_FAN
, NO_BANK
,
1290 PC87360_REG_FAN_MIN(nr
),
1295 static struct pc87360_data
*pc87360_update_device(struct device
*dev
)
1297 struct pc87360_data
*data
= dev_get_drvdata(dev
);
1300 mutex_lock(&data
->update_lock
);
1302 if (time_after(jiffies
, data
->last_updated
+ HZ
* 2) || !data
->valid
) {
1303 dev_dbg(dev
, "Data update\n");
1306 for (i
= 0; i
< data
->fannr
; i
++) {
1307 if (FAN_CONFIG_MONITOR(data
->fan_conf
, i
)) {
1308 data
->fan_status
[i
] =
1309 pc87360_read_value(data
, LD_FAN
,
1310 NO_BANK
, PC87360_REG_FAN_STATUS(i
));
1311 data
->fan
[i
] = pc87360_read_value(data
, LD_FAN
,
1312 NO_BANK
, PC87360_REG_FAN(i
));
1313 data
->fan_min
[i
] = pc87360_read_value(data
,
1315 PC87360_REG_FAN_MIN(i
));
1316 /* Change clock divider if needed */
1317 pc87360_autodiv(dev
, i
);
1318 /* Clear bits and write new divider */
1319 pc87360_write_value(data
, LD_FAN
, NO_BANK
,
1320 PC87360_REG_FAN_STATUS(i
),
1321 data
->fan_status
[i
]);
1323 if (FAN_CONFIG_CONTROL(data
->fan_conf
, i
))
1324 data
->pwm
[i
] = pc87360_read_value(data
, LD_FAN
,
1325 NO_BANK
, PC87360_REG_PWM(i
));
1329 for (i
= 0; i
< data
->innr
; i
++) {
1330 data
->in_status
[i
] = pc87360_read_value(data
, LD_IN
, i
,
1331 PC87365_REG_IN_STATUS
);
1333 pc87360_write_value(data
, LD_IN
, i
,
1334 PC87365_REG_IN_STATUS
,
1335 data
->in_status
[i
]);
1336 if ((data
->in_status
[i
] & 0x81) == 0x81) {
1337 data
->in
[i
] = pc87360_read_value(data
, LD_IN
,
1340 if (data
->in_status
[i
] & 0x01) {
1341 data
->in_min
[i
] = pc87360_read_value(data
,
1343 PC87365_REG_IN_MIN
);
1344 data
->in_max
[i
] = pc87360_read_value(data
,
1346 PC87365_REG_IN_MAX
);
1348 data
->in_crit
[i
-11] =
1349 pc87360_read_value(data
, LD_IN
,
1350 i
, PC87365_REG_TEMP_CRIT
);
1354 data
->in_alarms
= pc87360_read_value(data
, LD_IN
,
1355 NO_BANK
, PC87365_REG_IN_ALARMS1
)
1356 | ((pc87360_read_value(data
, LD_IN
,
1357 NO_BANK
, PC87365_REG_IN_ALARMS2
)
1359 data
->vid
= (data
->vid_conf
& 0xE0) ?
1360 pc87360_read_value(data
, LD_IN
,
1361 NO_BANK
, PC87365_REG_VID
) : 0x1F;
1365 for (i
= 0; i
< data
->tempnr
; i
++) {
1366 data
->temp_status
[i
] = pc87360_read_value(data
,
1368 PC87365_REG_TEMP_STATUS
);
1370 pc87360_write_value(data
, LD_TEMP
, i
,
1371 PC87365_REG_TEMP_STATUS
,
1372 data
->temp_status
[i
]);
1373 if ((data
->temp_status
[i
] & 0x81) == 0x81) {
1374 data
->temp
[i
] = pc87360_read_value(data
,
1378 if (data
->temp_status
[i
] & 0x01) {
1379 data
->temp_min
[i
] = pc87360_read_value(data
,
1381 PC87365_REG_TEMP_MIN
);
1382 data
->temp_max
[i
] = pc87360_read_value(data
,
1384 PC87365_REG_TEMP_MAX
);
1385 data
->temp_crit
[i
] = pc87360_read_value(data
,
1387 PC87365_REG_TEMP_CRIT
);
1391 data
->temp_alarms
= pc87360_read_value(data
, LD_TEMP
,
1392 NO_BANK
, PC87365_REG_TEMP_ALARMS
)
1396 data
->last_updated
= jiffies
;
1400 mutex_unlock(&data
->update_lock
);
1405 static int __init
pc87360_device_add(unsigned short address
)
1407 struct resource res
= {
1409 .flags
= IORESOURCE_IO
,
1413 pdev
= platform_device_alloc("pc87360", address
);
1416 printk(KERN_ERR
"pc87360: Device allocation failed\n");
1420 for (i
= 0; i
< 3; i
++) {
1423 res
.start
= extra_isa
[i
];
1424 res
.end
= extra_isa
[i
] + PC87360_EXTENT
- 1;
1426 err
= acpi_check_resource_conflict(&res
);
1428 goto exit_device_put
;
1430 err
= platform_device_add_resources(pdev
, &res
, 1);
1432 printk(KERN_ERR
"pc87360: Device resource[%d] "
1433 "addition failed (%d)\n", i
, err
);
1434 goto exit_device_put
;
1438 err
= platform_device_add(pdev
);
1440 printk(KERN_ERR
"pc87360: Device addition failed (%d)\n",
1442 goto exit_device_put
;
1448 platform_device_put(pdev
);
1453 static int __init
pc87360_init(void)
1456 unsigned short address
= 0;
1458 if (pc87360_find(0x2e, &devid
, extra_isa
)
1459 && pc87360_find(0x4e, &devid
, extra_isa
)) {
1460 printk(KERN_WARNING
"pc87360: PC8736x not detected, "
1461 "module not inserted.\n");
1465 /* Arbitrarily pick one of the addresses */
1466 for (i
= 0; i
< 3; i
++) {
1467 if (extra_isa
[i
] != 0x0000) {
1468 address
= extra_isa
[i
];
1473 if (address
== 0x0000) {
1474 printk(KERN_WARNING
"pc87360: No active logical device, "
1475 "module not inserted.\n");
1479 err
= platform_driver_register(&pc87360_driver
);
1483 /* Sets global pdev as a side effect */
1484 err
= pc87360_device_add(address
);
1491 platform_driver_unregister(&pc87360_driver
);
1496 static void __exit
pc87360_exit(void)
1498 platform_device_unregister(pdev
);
1499 platform_driver_unregister(&pc87360_driver
);
1503 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1504 MODULE_DESCRIPTION("PC8736x hardware monitor");
1505 MODULE_LICENSE("GPL");
1507 module_init(pc87360_init
);
1508 module_exit(pc87360_exit
);