2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 * Copyright (C) 2004, 2007 Jean Delvare <jdelvare@suse.de>
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/jiffies.h>
42 #include <linux/platform_device.h>
43 #include <linux/hwmon.h>
44 #include <linux/hwmon-sysfs.h>
45 #include <linux/hwmon-vid.h>
46 #include <linux/err.h>
47 #include <linux/mutex.h>
48 #include <linux/acpi.h>
52 static struct platform_device *pdev;
53 static unsigned short extra_isa[3];
57 module_param(init, int, 0);
58 MODULE_PARM_DESC(init,
59 "Chip initialization level:\n"
61 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
62 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
63 " 3: Forcibly enable all voltage and temperature channels, including in9");
65 static unsigned short force_id;
66 module_param(force_id, ushort, 0);
67 MODULE_PARM_DESC(force_id, "Override the detected device ID");
70 * Super-I/O registers and operations
73 #define DEV 0x07 /* Register: Logical device select */
74 #define DEVID 0x20 /* Register: Device ID */
75 #define ACT 0x30 /* Register: Device activation */
76 #define BASE 0x60 /* Register: Base address */
78 #define FSCM 0x09 /* Logical device: fans */
79 #define VLM 0x0d /* Logical device: voltages */
80 #define TMS 0x0e /* Logical device: temperatures */
82 static const u8 logdev[LDNI_MAX] = { FSCM, VLM, TMS };
88 static inline void superio_outb(int sioaddr, int reg, int val)
91 outb(val, sioaddr + 1);
94 static inline int superio_inb(int sioaddr, int reg)
97 return inb(sioaddr + 1);
100 static inline void superio_exit(int sioaddr)
103 outb(0x02, sioaddr + 1);
110 #define PC87360_EXTENT 0x10
111 #define PC87365_REG_BANK 0x09
115 * Fan registers and conversions
118 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
119 #define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
120 #define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
121 #define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
122 #define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
123 #define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
125 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : \
126 480000 / ((val) * (div)))
127 #define FAN_TO_REG(val, div) ((val) <= 100 ? 0 : \
128 480000 / ((val) * (div)))
129 #define FAN_DIV_FROM_REG(val) (1 << (((val) >> 5) & 0x03))
130 #define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
132 #define FAN_CONFIG_MONITOR(val, nr) (((val) >> (2 + (nr) * 3)) & 1)
133 #define FAN_CONFIG_CONTROL(val, nr) (((val) >> (3 + (nr) * 3)) & 1)
134 #define FAN_CONFIG_INVERT(val, nr) (((val) >> (4 + (nr) * 3)) & 1)
136 #define PWM_FROM_REG(val, inv) ((inv) ? 255 - (val) : (val))
137 static inline u8 PWM_TO_REG(int val, int inv)
149 * Voltage registers and conversions
152 #define PC87365_REG_IN_CONVRATE 0x07
153 #define PC87365_REG_IN_CONFIG 0x08
154 #define PC87365_REG_IN 0x0B
155 #define PC87365_REG_IN_MIN 0x0D
156 #define PC87365_REG_IN_MAX 0x0C
157 #define PC87365_REG_IN_STATUS 0x0A
158 #define PC87365_REG_IN_ALARMS1 0x00
159 #define PC87365_REG_IN_ALARMS2 0x01
160 #define PC87365_REG_VID 0x06
162 #define IN_FROM_REG(val, ref) (((val) * (ref) + 128) / 256)
163 #define IN_TO_REG(val, ref) ((val) < 0 ? 0 : \
164 (val) * 256 >= (ref) * 255 ? 255 : \
165 ((val) * 256 + (ref) / 2) / (ref))
168 * Temperature registers and conversions
171 #define PC87365_REG_TEMP_CONFIG 0x08
172 #define PC87365_REG_TEMP 0x0B
173 #define PC87365_REG_TEMP_MIN 0x0D
174 #define PC87365_REG_TEMP_MAX 0x0C
175 #define PC87365_REG_TEMP_CRIT 0x0E
176 #define PC87365_REG_TEMP_STATUS 0x0A
177 #define PC87365_REG_TEMP_ALARMS 0x00
179 #define TEMP_FROM_REG(val) ((val) * 1000)
180 #define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
181 (val) > 127000 ? 127 : \
182 (val) < 0 ? ((val) - 500) / 1000 : \
183 ((val) + 500) / 1000)
189 struct pc87360_data {
191 struct device *hwmon_dev;
193 struct mutex update_lock;
194 char valid; /* !=0 if following fields are valid */
195 unsigned long last_updated; /* In jiffies */
199 u8 fannr, innr, tempnr;
201 u8 fan[3]; /* Register value */
202 u8 fan_min[3]; /* Register value */
203 u8 fan_status[3]; /* Register value */
204 u8 pwm[3]; /* Register value */
205 u16 fan_conf; /* Configuration register values, combined */
207 u16 in_vref; /* 1 mV/bit */
208 u8 in[14]; /* Register value */
209 u8 in_min[14]; /* Register value */
210 u8 in_max[14]; /* Register value */
211 u8 in_crit[3]; /* Register value */
212 u8 in_status[14]; /* Register value */
213 u16 in_alarms; /* Register values, combined, masked */
214 u8 vid_conf; /* Configuration register value */
216 u8 vid; /* Register value */
218 s8 temp[3]; /* Register value */
219 s8 temp_min[3]; /* Register value */
220 s8 temp_max[3]; /* Register value */
221 s8 temp_crit[3]; /* Register value */
222 u8 temp_status[3]; /* Register value */
223 u8 temp_alarms; /* Register value, masked */
227 * Functions declaration
230 static int pc87360_probe(struct platform_device *pdev);
231 static int pc87360_remove(struct platform_device *pdev);
233 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
235 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
237 static void pc87360_init_device(struct platform_device *pdev,
238 int use_thermistors);
239 static struct pc87360_data *pc87360_update_device(struct device *dev);
245 static struct platform_driver pc87360_driver = {
249 .probe = pc87360_probe,
250 .remove = pc87360_remove,
257 static ssize_t fan_input_show(struct device *dev,
258 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[attr->index],
263 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
265 static ssize_t fan_min_show(struct device *dev,
266 struct device_attribute *devattr, char *buf)
268 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
269 struct pc87360_data *data = pc87360_update_device(dev);
270 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
271 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
273 static ssize_t fan_div_show(struct device *dev,
274 struct device_attribute *devattr, char *buf)
276 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
277 struct pc87360_data *data = pc87360_update_device(dev);
278 return sprintf(buf, "%u\n",
279 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
281 static ssize_t fan_status_show(struct device *dev,
282 struct device_attribute *devattr, char *buf)
284 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
285 struct pc87360_data *data = pc87360_update_device(dev);
286 return sprintf(buf, "%u\n",
287 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
289 static ssize_t fan_min_store(struct device *dev,
290 struct device_attribute *devattr,
291 const char *buf, size_t count)
293 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
294 struct pc87360_data *data = dev_get_drvdata(dev);
298 err = kstrtol(buf, 10, &fan_min);
302 mutex_lock(&data->update_lock);
303 fan_min = FAN_TO_REG(fan_min,
304 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
306 /* If it wouldn't fit, change clock divisor */
308 && (data->fan_status[attr->index] & 0x60) != 0x60) {
310 data->fan[attr->index] >>= 1;
311 data->fan_status[attr->index] += 0x20;
313 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
314 pc87360_write_value(data, LD_FAN, NO_BANK,
315 PC87360_REG_FAN_MIN(attr->index),
316 data->fan_min[attr->index]);
318 /* Write new divider, preserve alarm bits */
319 pc87360_write_value(data, LD_FAN, NO_BANK,
320 PC87360_REG_FAN_STATUS(attr->index),
321 data->fan_status[attr->index] & 0xF9);
322 mutex_unlock(&data->update_lock);
327 static struct sensor_device_attribute fan_input[] = {
328 SENSOR_ATTR_RO(fan1_input, fan_input, 0),
329 SENSOR_ATTR_RO(fan2_input, fan_input, 1),
330 SENSOR_ATTR_RO(fan3_input, fan_input, 2),
332 static struct sensor_device_attribute fan_status[] = {
333 SENSOR_ATTR_RO(fan1_status, fan_status, 0),
334 SENSOR_ATTR_RO(fan2_status, fan_status, 1),
335 SENSOR_ATTR_RO(fan3_status, fan_status, 2),
337 static struct sensor_device_attribute fan_div[] = {
338 SENSOR_ATTR_RO(fan1_div, fan_div, 0),
339 SENSOR_ATTR_RO(fan2_div, fan_div, 1),
340 SENSOR_ATTR_RO(fan3_div, fan_div, 2),
342 static struct sensor_device_attribute fan_min[] = {
343 SENSOR_ATTR_RW(fan1_min, fan_min, 0),
344 SENSOR_ATTR_RW(fan2_min, fan_min, 1),
345 SENSOR_ATTR_RW(fan3_min, fan_min, 2),
348 #define FAN_UNIT_ATTRS(X) \
349 { &fan_input[X].dev_attr.attr, \
350 &fan_status[X].dev_attr.attr, \
351 &fan_div[X].dev_attr.attr, \
352 &fan_min[X].dev_attr.attr, \
356 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
359 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
360 struct pc87360_data *data = pc87360_update_device(dev);
361 return sprintf(buf, "%u\n",
362 PWM_FROM_REG(data->pwm[attr->index],
363 FAN_CONFIG_INVERT(data->fan_conf,
366 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
367 const char *buf, size_t count)
369 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
370 struct pc87360_data *data = dev_get_drvdata(dev);
374 err = kstrtol(buf, 10, &val);
378 mutex_lock(&data->update_lock);
379 data->pwm[attr->index] = PWM_TO_REG(val,
380 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
381 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
382 data->pwm[attr->index]);
383 mutex_unlock(&data->update_lock);
387 static struct sensor_device_attribute pwm[] = {
388 SENSOR_ATTR_RW(pwm1, pwm, 0),
389 SENSOR_ATTR_RW(pwm2, pwm, 1),
390 SENSOR_ATTR_RW(pwm3, pwm, 2),
393 static struct attribute *pc8736x_fan_attr[][5] = {
399 static const struct attribute_group pc8736x_fan_attr_group[] = {
400 { .attrs = pc8736x_fan_attr[0], },
401 { .attrs = pc8736x_fan_attr[1], },
402 { .attrs = pc8736x_fan_attr[2], },
405 static ssize_t in_input_show(struct device *dev,
406 struct device_attribute *devattr, char *buf)
408 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
409 struct pc87360_data *data = pc87360_update_device(dev);
410 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
413 static ssize_t in_min_show(struct device *dev,
414 struct device_attribute *devattr, char *buf)
416 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
417 struct pc87360_data *data = pc87360_update_device(dev);
418 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
421 static ssize_t in_max_show(struct device *dev,
422 struct device_attribute *devattr, char *buf)
424 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
425 struct pc87360_data *data = pc87360_update_device(dev);
426 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
429 static ssize_t in_status_show(struct device *dev,
430 struct device_attribute *devattr, char *buf)
432 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
433 struct pc87360_data *data = pc87360_update_device(dev);
434 return sprintf(buf, "%u\n", data->in_status[attr->index]);
436 static ssize_t in_min_store(struct device *dev,
437 struct device_attribute *devattr, const char *buf,
440 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
441 struct pc87360_data *data = dev_get_drvdata(dev);
445 err = kstrtol(buf, 10, &val);
449 mutex_lock(&data->update_lock);
450 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
451 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
452 data->in_min[attr->index]);
453 mutex_unlock(&data->update_lock);
456 static ssize_t in_max_store(struct device *dev,
457 struct device_attribute *devattr, const char *buf,
460 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
461 struct pc87360_data *data = dev_get_drvdata(dev);
465 err = kstrtol(buf, 10, &val);
469 mutex_lock(&data->update_lock);
470 data->in_max[attr->index] = IN_TO_REG(val,
472 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
473 data->in_max[attr->index]);
474 mutex_unlock(&data->update_lock);
478 static struct sensor_device_attribute in_input[] = {
479 SENSOR_ATTR_RO(in0_input, in_input, 0),
480 SENSOR_ATTR_RO(in1_input, in_input, 1),
481 SENSOR_ATTR_RO(in2_input, in_input, 2),
482 SENSOR_ATTR_RO(in3_input, in_input, 3),
483 SENSOR_ATTR_RO(in4_input, in_input, 4),
484 SENSOR_ATTR_RO(in5_input, in_input, 5),
485 SENSOR_ATTR_RO(in6_input, in_input, 6),
486 SENSOR_ATTR_RO(in7_input, in_input, 7),
487 SENSOR_ATTR_RO(in8_input, in_input, 8),
488 SENSOR_ATTR_RO(in9_input, in_input, 9),
489 SENSOR_ATTR_RO(in10_input, in_input, 10),
491 static struct sensor_device_attribute in_status[] = {
492 SENSOR_ATTR_RO(in0_status, in_status, 0),
493 SENSOR_ATTR_RO(in1_status, in_status, 1),
494 SENSOR_ATTR_RO(in2_status, in_status, 2),
495 SENSOR_ATTR_RO(in3_status, in_status, 3),
496 SENSOR_ATTR_RO(in4_status, in_status, 4),
497 SENSOR_ATTR_RO(in5_status, in_status, 5),
498 SENSOR_ATTR_RO(in6_status, in_status, 6),
499 SENSOR_ATTR_RO(in7_status, in_status, 7),
500 SENSOR_ATTR_RO(in8_status, in_status, 8),
501 SENSOR_ATTR_RO(in9_status, in_status, 9),
502 SENSOR_ATTR_RO(in10_status, in_status, 10),
504 static struct sensor_device_attribute in_min[] = {
505 SENSOR_ATTR_RW(in0_min, in_min, 0),
506 SENSOR_ATTR_RW(in1_min, in_min, 1),
507 SENSOR_ATTR_RW(in2_min, in_min, 2),
508 SENSOR_ATTR_RW(in3_min, in_min, 3),
509 SENSOR_ATTR_RW(in4_min, in_min, 4),
510 SENSOR_ATTR_RW(in5_min, in_min, 5),
511 SENSOR_ATTR_RW(in6_min, in_min, 6),
512 SENSOR_ATTR_RW(in7_min, in_min, 7),
513 SENSOR_ATTR_RW(in8_min, in_min, 8),
514 SENSOR_ATTR_RW(in9_min, in_min, 9),
515 SENSOR_ATTR_RW(in10_min, in_min, 10),
517 static struct sensor_device_attribute in_max[] = {
518 SENSOR_ATTR_RW(in0_max, in_max, 0),
519 SENSOR_ATTR_RW(in1_max, in_max, 1),
520 SENSOR_ATTR_RW(in2_max, in_max, 2),
521 SENSOR_ATTR_RW(in3_max, in_max, 3),
522 SENSOR_ATTR_RW(in4_max, in_max, 4),
523 SENSOR_ATTR_RW(in5_max, in_max, 5),
524 SENSOR_ATTR_RW(in6_max, in_max, 6),
525 SENSOR_ATTR_RW(in7_max, in_max, 7),
526 SENSOR_ATTR_RW(in8_max, in_max, 8),
527 SENSOR_ATTR_RW(in9_max, in_max, 9),
528 SENSOR_ATTR_RW(in10_max, in_max, 10),
531 /* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
532 #define CHAN_ALM_MIN 0x02 /* min limit crossed */
533 #define CHAN_ALM_MAX 0x04 /* max limit exceeded */
534 #define TEMP_ALM_CRIT 0x08 /* temp crit exceeded (temp only) */
537 * show_in_min/max_alarm() reads data from the per-channel status
538 * register (sec 11.5.12), not the vin event status registers (sec
539 * 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms)
542 static ssize_t in_min_alarm_show(struct device *dev,
543 struct device_attribute *devattr, char *buf)
545 struct pc87360_data *data = pc87360_update_device(dev);
546 unsigned nr = to_sensor_dev_attr(devattr)->index;
548 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
550 static ssize_t in_max_alarm_show(struct device *dev,
551 struct device_attribute *devattr, char *buf)
553 struct pc87360_data *data = pc87360_update_device(dev);
554 unsigned nr = to_sensor_dev_attr(devattr)->index;
556 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
559 static struct sensor_device_attribute in_min_alarm[] = {
560 SENSOR_ATTR_RO(in0_min_alarm, in_min_alarm, 0),
561 SENSOR_ATTR_RO(in1_min_alarm, in_min_alarm, 1),
562 SENSOR_ATTR_RO(in2_min_alarm, in_min_alarm, 2),
563 SENSOR_ATTR_RO(in3_min_alarm, in_min_alarm, 3),
564 SENSOR_ATTR_RO(in4_min_alarm, in_min_alarm, 4),
565 SENSOR_ATTR_RO(in5_min_alarm, in_min_alarm, 5),
566 SENSOR_ATTR_RO(in6_min_alarm, in_min_alarm, 6),
567 SENSOR_ATTR_RO(in7_min_alarm, in_min_alarm, 7),
568 SENSOR_ATTR_RO(in8_min_alarm, in_min_alarm, 8),
569 SENSOR_ATTR_RO(in9_min_alarm, in_min_alarm, 9),
570 SENSOR_ATTR_RO(in10_min_alarm, in_min_alarm, 10),
572 static struct sensor_device_attribute in_max_alarm[] = {
573 SENSOR_ATTR_RO(in0_max_alarm, in_max_alarm, 0),
574 SENSOR_ATTR_RO(in1_max_alarm, in_max_alarm, 1),
575 SENSOR_ATTR_RO(in2_max_alarm, in_max_alarm, 2),
576 SENSOR_ATTR_RO(in3_max_alarm, in_max_alarm, 3),
577 SENSOR_ATTR_RO(in4_max_alarm, in_max_alarm, 4),
578 SENSOR_ATTR_RO(in5_max_alarm, in_max_alarm, 5),
579 SENSOR_ATTR_RO(in6_max_alarm, in_max_alarm, 6),
580 SENSOR_ATTR_RO(in7_max_alarm, in_max_alarm, 7),
581 SENSOR_ATTR_RO(in8_max_alarm, in_max_alarm, 8),
582 SENSOR_ATTR_RO(in9_max_alarm, in_max_alarm, 9),
583 SENSOR_ATTR_RO(in10_max_alarm, in_max_alarm, 10),
586 #define VIN_UNIT_ATTRS(X) \
587 &in_input[X].dev_attr.attr, \
588 &in_status[X].dev_attr.attr, \
589 &in_min[X].dev_attr.attr, \
590 &in_max[X].dev_attr.attr, \
591 &in_min_alarm[X].dev_attr.attr, \
592 &in_max_alarm[X].dev_attr.attr
594 static ssize_t cpu0_vid_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
597 struct pc87360_data *data = pc87360_update_device(dev);
598 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
600 static DEVICE_ATTR_RO(cpu0_vid);
602 static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
605 struct pc87360_data *data = dev_get_drvdata(dev);
606 return sprintf(buf, "%u\n", data->vrm);
608 static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
609 const char *buf, size_t count)
611 struct pc87360_data *data = dev_get_drvdata(dev);
615 err = kstrtoul(buf, 10, &val);
625 static DEVICE_ATTR_RW(vrm);
627 static ssize_t alarms_in_show(struct device *dev,
628 struct device_attribute *attr, char *buf)
630 struct pc87360_data *data = pc87360_update_device(dev);
631 return sprintf(buf, "%u\n", data->in_alarms);
633 static DEVICE_ATTR_RO(alarms_in);
635 static struct attribute *pc8736x_vin_attr_array[] = {
647 &dev_attr_cpu0_vid.attr,
649 &dev_attr_alarms_in.attr,
652 static const struct attribute_group pc8736x_vin_group = {
653 .attrs = pc8736x_vin_attr_array,
656 static ssize_t therm_input_show(struct device *dev,
657 struct device_attribute *devattr, char *buf)
659 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
660 struct pc87360_data *data = pc87360_update_device(dev);
661 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
664 static ssize_t therm_min_show(struct device *dev,
665 struct device_attribute *devattr, char *buf)
667 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
668 struct pc87360_data *data = pc87360_update_device(dev);
669 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
672 static ssize_t therm_max_show(struct device *dev,
673 struct device_attribute *devattr, char *buf)
675 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
676 struct pc87360_data *data = pc87360_update_device(dev);
677 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
680 static ssize_t therm_crit_show(struct device *dev,
681 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, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
688 static ssize_t therm_status_show(struct device *dev,
689 struct device_attribute *devattr, char *buf)
691 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
692 struct pc87360_data *data = pc87360_update_device(dev);
693 return sprintf(buf, "%u\n", data->in_status[attr->index]);
696 static ssize_t therm_min_store(struct device *dev,
697 struct device_attribute *devattr,
698 const char *buf, size_t count)
700 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
701 struct pc87360_data *data = dev_get_drvdata(dev);
705 err = kstrtol(buf, 10, &val);
709 mutex_lock(&data->update_lock);
710 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
711 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
712 data->in_min[attr->index]);
713 mutex_unlock(&data->update_lock);
717 static ssize_t therm_max_store(struct device *dev,
718 struct device_attribute *devattr,
719 const char *buf, size_t count)
721 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
722 struct pc87360_data *data = dev_get_drvdata(dev);
726 err = kstrtol(buf, 10, &val);
730 mutex_lock(&data->update_lock);
731 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
732 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
733 data->in_max[attr->index]);
734 mutex_unlock(&data->update_lock);
737 static ssize_t therm_crit_store(struct device *dev,
738 struct device_attribute *devattr,
739 const char *buf, size_t count)
741 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
742 struct pc87360_data *data = dev_get_drvdata(dev);
746 err = kstrtol(buf, 10, &val);
750 mutex_lock(&data->update_lock);
751 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
752 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
753 data->in_crit[attr->index-11]);
754 mutex_unlock(&data->update_lock);
759 * the +11 term below reflects the fact that VLM units 11,12,13 are
760 * used in the chip to measure voltage across the thermistors
762 static struct sensor_device_attribute therm_input[] = {
763 SENSOR_ATTR_RO(temp4_input, therm_input, 0 + 11),
764 SENSOR_ATTR_RO(temp5_input, therm_input, 1 + 11),
765 SENSOR_ATTR_RO(temp6_input, therm_input, 2 + 11),
767 static struct sensor_device_attribute therm_status[] = {
768 SENSOR_ATTR_RO(temp4_status, therm_status, 0 + 11),
769 SENSOR_ATTR_RO(temp5_status, therm_status, 1 + 11),
770 SENSOR_ATTR_RO(temp6_status, therm_status, 2 + 11),
772 static struct sensor_device_attribute therm_min[] = {
773 SENSOR_ATTR_RW(temp4_min, therm_min, 0 + 11),
774 SENSOR_ATTR_RW(temp5_min, therm_min, 1 + 11),
775 SENSOR_ATTR_RW(temp6_min, therm_min, 2 + 11),
777 static struct sensor_device_attribute therm_max[] = {
778 SENSOR_ATTR_RW(temp4_max, therm_max, 0 + 11),
779 SENSOR_ATTR_RW(temp5_max, therm_max, 1 + 11),
780 SENSOR_ATTR_RW(temp6_max, therm_max, 2 + 11),
782 static struct sensor_device_attribute therm_crit[] = {
783 SENSOR_ATTR_RW(temp4_crit, therm_crit, 0 + 11),
784 SENSOR_ATTR_RW(temp5_crit, therm_crit, 1 + 11),
785 SENSOR_ATTR_RW(temp6_crit, therm_crit, 2 + 11),
789 * show_therm_min/max_alarm() reads data from the per-channel voltage
790 * status register (sec 11.5.12)
793 static ssize_t therm_min_alarm_show(struct device *dev,
794 struct device_attribute *devattr,
797 struct pc87360_data *data = pc87360_update_device(dev);
798 unsigned nr = to_sensor_dev_attr(devattr)->index;
800 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
802 static ssize_t therm_max_alarm_show(struct device *dev,
803 struct device_attribute *devattr,
806 struct pc87360_data *data = pc87360_update_device(dev);
807 unsigned nr = to_sensor_dev_attr(devattr)->index;
809 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
811 static ssize_t therm_crit_alarm_show(struct device *dev,
812 struct device_attribute *devattr,
815 struct pc87360_data *data = pc87360_update_device(dev);
816 unsigned nr = to_sensor_dev_attr(devattr)->index;
818 return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT));
821 static struct sensor_device_attribute therm_min_alarm[] = {
822 SENSOR_ATTR_RO(temp4_min_alarm, therm_min_alarm, 0 + 11),
823 SENSOR_ATTR_RO(temp5_min_alarm, therm_min_alarm, 1 + 11),
824 SENSOR_ATTR_RO(temp6_min_alarm, therm_min_alarm, 2 + 11),
826 static struct sensor_device_attribute therm_max_alarm[] = {
827 SENSOR_ATTR_RO(temp4_max_alarm, therm_max_alarm, 0 + 11),
828 SENSOR_ATTR_RO(temp5_max_alarm, therm_max_alarm, 1 + 11),
829 SENSOR_ATTR_RO(temp6_max_alarm, therm_max_alarm, 2 + 11),
831 static struct sensor_device_attribute therm_crit_alarm[] = {
832 SENSOR_ATTR_RO(temp4_crit_alarm, therm_crit_alarm, 0 + 11),
833 SENSOR_ATTR_RO(temp5_crit_alarm, therm_crit_alarm, 1 + 11),
834 SENSOR_ATTR_RO(temp6_crit_alarm, therm_crit_alarm, 2 + 11),
837 #define THERM_UNIT_ATTRS(X) \
838 &therm_input[X].dev_attr.attr, \
839 &therm_status[X].dev_attr.attr, \
840 &therm_min[X].dev_attr.attr, \
841 &therm_max[X].dev_attr.attr, \
842 &therm_crit[X].dev_attr.attr, \
843 &therm_min_alarm[X].dev_attr.attr, \
844 &therm_max_alarm[X].dev_attr.attr, \
845 &therm_crit_alarm[X].dev_attr.attr
847 static struct attribute *pc8736x_therm_attr_array[] = {
853 static const struct attribute_group pc8736x_therm_group = {
854 .attrs = pc8736x_therm_attr_array,
857 static ssize_t temp_input_show(struct device *dev,
858 struct device_attribute *devattr, char *buf)
860 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
861 struct pc87360_data *data = pc87360_update_device(dev);
862 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
865 static ssize_t temp_min_show(struct device *dev,
866 struct device_attribute *devattr, char *buf)
868 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
869 struct pc87360_data *data = pc87360_update_device(dev);
870 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
873 static ssize_t temp_max_show(struct device *dev,
874 struct device_attribute *devattr, char *buf)
876 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
877 struct pc87360_data *data = pc87360_update_device(dev);
878 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
881 static ssize_t temp_crit_show(struct device *dev,
882 struct device_attribute *devattr, char *buf)
884 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
885 struct pc87360_data *data = pc87360_update_device(dev);
886 return sprintf(buf, "%d\n",
887 TEMP_FROM_REG(data->temp_crit[attr->index]));
890 static ssize_t temp_status_show(struct device *dev,
891 struct device_attribute *devattr, char *buf)
893 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
894 struct pc87360_data *data = pc87360_update_device(dev);
895 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
898 static ssize_t temp_min_store(struct device *dev,
899 struct device_attribute *devattr,
900 const char *buf, size_t count)
902 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
903 struct pc87360_data *data = dev_get_drvdata(dev);
907 err = kstrtol(buf, 10, &val);
911 mutex_lock(&data->update_lock);
912 data->temp_min[attr->index] = TEMP_TO_REG(val);
913 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
914 data->temp_min[attr->index]);
915 mutex_unlock(&data->update_lock);
919 static ssize_t temp_max_store(struct device *dev,
920 struct device_attribute *devattr,
921 const char *buf, size_t count)
923 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
924 struct pc87360_data *data = dev_get_drvdata(dev);
928 err = kstrtol(buf, 10, &val);
932 mutex_lock(&data->update_lock);
933 data->temp_max[attr->index] = TEMP_TO_REG(val);
934 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
935 data->temp_max[attr->index]);
936 mutex_unlock(&data->update_lock);
940 static ssize_t temp_crit_store(struct device *dev,
941 struct device_attribute *devattr,
942 const char *buf, size_t count)
944 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
945 struct pc87360_data *data = dev_get_drvdata(dev);
949 err = kstrtol(buf, 10, &val);
953 mutex_lock(&data->update_lock);
954 data->temp_crit[attr->index] = TEMP_TO_REG(val);
955 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
956 data->temp_crit[attr->index]);
957 mutex_unlock(&data->update_lock);
961 static struct sensor_device_attribute temp_input[] = {
962 SENSOR_ATTR_RO(temp1_input, temp_input, 0),
963 SENSOR_ATTR_RO(temp2_input, temp_input, 1),
964 SENSOR_ATTR_RO(temp3_input, temp_input, 2),
966 static struct sensor_device_attribute temp_status[] = {
967 SENSOR_ATTR_RO(temp1_status, temp_status, 0),
968 SENSOR_ATTR_RO(temp2_status, temp_status, 1),
969 SENSOR_ATTR_RO(temp3_status, temp_status, 2),
971 static struct sensor_device_attribute temp_min[] = {
972 SENSOR_ATTR_RW(temp1_min, temp_min, 0),
973 SENSOR_ATTR_RW(temp2_min, temp_min, 1),
974 SENSOR_ATTR_RW(temp3_min, temp_min, 2),
976 static struct sensor_device_attribute temp_max[] = {
977 SENSOR_ATTR_RW(temp1_max, temp_max, 0),
978 SENSOR_ATTR_RW(temp2_max, temp_max, 1),
979 SENSOR_ATTR_RW(temp3_max, temp_max, 2),
981 static struct sensor_device_attribute temp_crit[] = {
982 SENSOR_ATTR_RW(temp1_crit, temp_crit, 0),
983 SENSOR_ATTR_RW(temp2_crit, temp_crit, 1),
984 SENSOR_ATTR_RW(temp3_crit, temp_crit, 2),
987 static ssize_t alarms_temp_show(struct device *dev,
988 struct device_attribute *attr, char *buf)
990 struct pc87360_data *data = pc87360_update_device(dev);
991 return sprintf(buf, "%u\n", data->temp_alarms);
994 static DEVICE_ATTR_RO(alarms_temp);
997 * show_temp_min/max_alarm() reads data from the per-channel status
998 * register (sec 12.3.7), not the temp event status registers (sec
999 * 12.3.2) that show_temp_alarm() reads (via data->temp_alarms)
1002 static ssize_t temp_min_alarm_show(struct device *dev,
1003 struct device_attribute *devattr,
1006 struct pc87360_data *data = pc87360_update_device(dev);
1007 unsigned nr = to_sensor_dev_attr(devattr)->index;
1009 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
1012 static ssize_t temp_max_alarm_show(struct device *dev,
1013 struct device_attribute *devattr,
1016 struct pc87360_data *data = pc87360_update_device(dev);
1017 unsigned nr = to_sensor_dev_attr(devattr)->index;
1019 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
1022 static ssize_t temp_crit_alarm_show(struct device *dev,
1023 struct device_attribute *devattr,
1026 struct pc87360_data *data = pc87360_update_device(dev);
1027 unsigned nr = to_sensor_dev_attr(devattr)->index;
1029 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
1032 static struct sensor_device_attribute temp_min_alarm[] = {
1033 SENSOR_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0),
1034 SENSOR_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1),
1035 SENSOR_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2),
1038 static struct sensor_device_attribute temp_max_alarm[] = {
1039 SENSOR_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0),
1040 SENSOR_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1),
1041 SENSOR_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2),
1044 static struct sensor_device_attribute temp_crit_alarm[] = {
1045 SENSOR_ATTR_RO(temp1_crit_alarm, temp_crit_alarm, 0),
1046 SENSOR_ATTR_RO(temp2_crit_alarm, temp_crit_alarm, 1),
1047 SENSOR_ATTR_RO(temp3_crit_alarm, temp_crit_alarm, 2),
1050 #define TEMP_FAULT 0x40 /* open diode */
1051 static ssize_t temp_fault_show(struct device *dev,
1052 struct device_attribute *devattr, char *buf)
1054 struct pc87360_data *data = pc87360_update_device(dev);
1055 unsigned nr = to_sensor_dev_attr(devattr)->index;
1057 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
1059 static struct sensor_device_attribute temp_fault[] = {
1060 SENSOR_ATTR_RO(temp1_fault, temp_fault, 0),
1061 SENSOR_ATTR_RO(temp2_fault, temp_fault, 1),
1062 SENSOR_ATTR_RO(temp3_fault, temp_fault, 2),
1065 #define TEMP_UNIT_ATTRS(X) \
1066 { &temp_input[X].dev_attr.attr, \
1067 &temp_status[X].dev_attr.attr, \
1068 &temp_min[X].dev_attr.attr, \
1069 &temp_max[X].dev_attr.attr, \
1070 &temp_crit[X].dev_attr.attr, \
1071 &temp_min_alarm[X].dev_attr.attr, \
1072 &temp_max_alarm[X].dev_attr.attr, \
1073 &temp_crit_alarm[X].dev_attr.attr, \
1074 &temp_fault[X].dev_attr.attr, \
1078 static struct attribute *pc8736x_temp_attr[][10] = {
1084 static const struct attribute_group pc8736x_temp_attr_group[] = {
1085 { .attrs = pc8736x_temp_attr[0] },
1086 { .attrs = pc8736x_temp_attr[1] },
1087 { .attrs = pc8736x_temp_attr[2] }
1090 static ssize_t name_show(struct device *dev,
1091 struct device_attribute *devattr, char *buf)
1093 struct pc87360_data *data = dev_get_drvdata(dev);
1094 return sprintf(buf, "%s\n", data->name);
1097 static DEVICE_ATTR_RO(name);
1100 * Device detection, registration and update
1103 static int __init pc87360_find(int sioaddr, u8 *devid,
1104 unsigned short *addresses)
1108 int nrdev; /* logical device count */
1110 /* No superio_enter */
1112 /* Identify device */
1113 val = force_id ? force_id : superio_inb(sioaddr, DEVID);
1115 case 0xE1: /* PC87360 */
1116 case 0xE8: /* PC87363 */
1117 case 0xE4: /* PC87364 */
1120 case 0xE5: /* PC87365 */
1121 case 0xE9: /* PC87366 */
1125 superio_exit(sioaddr);
1128 /* Remember the device id */
1131 for (i = 0; i < nrdev; i++) {
1132 /* select logical device */
1133 superio_outb(sioaddr, DEV, logdev[i]);
1135 val = superio_inb(sioaddr, ACT);
1136 if (!(val & 0x01)) {
1137 pr_info("Device 0x%02x not activated\n", logdev[i]);
1141 val = (superio_inb(sioaddr, BASE) << 8)
1142 | superio_inb(sioaddr, BASE + 1);
1144 pr_info("Base address not set for device 0x%02x\n",
1151 if (i == 0) { /* Fans */
1152 confreg[0] = superio_inb(sioaddr, 0xF0);
1153 confreg[1] = superio_inb(sioaddr, 0xF1);
1155 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 1,
1156 (confreg[0] >> 2) & 1, (confreg[0] >> 3) & 1,
1157 (confreg[0] >> 4) & 1);
1158 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 2,
1159 (confreg[0] >> 5) & 1, (confreg[0] >> 6) & 1,
1160 (confreg[0] >> 7) & 1);
1161 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 3,
1162 confreg[1] & 1, (confreg[1] >> 1) & 1,
1163 (confreg[1] >> 2) & 1);
1164 } else if (i == 1) { /* Voltages */
1165 /* Are we using thermistors? */
1166 if (*devid == 0xE9) { /* PC87366 */
1168 * These registers are not logical-device
1169 * specific, just that we won't need them if
1170 * we don't use the VLM device
1172 confreg[2] = superio_inb(sioaddr, 0x2B);
1173 confreg[3] = superio_inb(sioaddr, 0x25);
1175 if (confreg[2] & 0x40) {
1176 pr_info("Using thermistors for temperature monitoring\n");
1178 if (confreg[3] & 0xE0) {
1179 pr_info("VID inputs routed (mode %u)\n",
1186 superio_exit(sioaddr);
1190 static void pc87360_remove_files(struct device *dev)
1194 device_remove_file(dev, &dev_attr_name);
1195 device_remove_file(dev, &dev_attr_alarms_temp);
1196 for (i = 0; i < ARRAY_SIZE(pc8736x_temp_attr_group); i++)
1197 sysfs_remove_group(&dev->kobj, &pc8736x_temp_attr_group[i]);
1198 for (i = 0; i < ARRAY_SIZE(pc8736x_fan_attr_group); i++) {
1199 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_attr_group[i]);
1200 device_remove_file(dev, &pwm[i].dev_attr);
1202 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1203 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1206 static int pc87360_probe(struct platform_device *pdev)
1209 struct pc87360_data *data;
1212 int use_thermistors = 0;
1213 struct device *dev = &pdev->dev;
1215 data = devm_kzalloc(dev, sizeof(struct pc87360_data), GFP_KERNEL);
1234 data->fannr = extra_isa[0] ? 3 : 0;
1235 data->innr = extra_isa[1] ? 11 : 0;
1236 data->tempnr = extra_isa[2] ? 2 : 0;
1240 data->fannr = extra_isa[0] ? 3 : 0;
1241 data->innr = extra_isa[1] ? 14 : 0;
1242 data->tempnr = extra_isa[2] ? 3 : 0;
1247 mutex_init(&data->lock);
1248 mutex_init(&data->update_lock);
1249 platform_set_drvdata(pdev, data);
1251 for (i = 0; i < LDNI_MAX; i++) {
1252 data->address[i] = extra_isa[i];
1253 if (data->address[i]
1254 && !devm_request_region(dev, extra_isa[i], PC87360_EXTENT,
1255 pc87360_driver.driver.name)) {
1257 "Region 0x%x-0x%x already in use!\n",
1258 extra_isa[i], extra_isa[i]+PC87360_EXTENT-1);
1263 /* Retrieve the fans configuration from Super-I/O space */
1265 data->fan_conf = confreg[0] | (confreg[1] << 8);
1268 * Use the correct reference voltage
1269 * Unless both the VLM and the TMS logical devices agree to
1270 * use an external Vref, the internal one is used.
1273 i = pc87360_read_value(data, LD_IN, NO_BANK,
1274 PC87365_REG_IN_CONFIG);
1276 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1277 PC87365_REG_TEMP_CONFIG);
1279 data->in_vref = (i&0x02) ? 3025 : 2966;
1280 dev_dbg(dev, "Using %s reference voltage\n",
1281 (i&0x02) ? "external" : "internal");
1283 data->vid_conf = confreg[3];
1284 data->vrm = vid_which_vrm();
1287 /* Fan clock dividers may be needed before any data is read */
1288 for (i = 0; i < data->fannr; i++) {
1289 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1290 data->fan_status[i] = pc87360_read_value(data,
1292 PC87360_REG_FAN_STATUS(i));
1296 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1297 use_thermistors = confreg[2] & 0x40;
1299 pc87360_init_device(pdev, use_thermistors);
1302 /* Register all-or-nothing sysfs groups */
1305 err = sysfs_create_group(&dev->kobj, &pc8736x_vin_group);
1310 if (data->innr == 14) {
1311 err = sysfs_create_group(&dev->kobj, &pc8736x_therm_group);
1316 /* create device attr-files for varying sysfs groups */
1319 for (i = 0; i < data->tempnr; i++) {
1320 err = sysfs_create_group(&dev->kobj,
1321 &pc8736x_temp_attr_group[i]);
1325 err = device_create_file(dev, &dev_attr_alarms_temp);
1330 for (i = 0; i < data->fannr; i++) {
1331 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1332 err = sysfs_create_group(&dev->kobj,
1333 &pc8736x_fan_attr_group[i]);
1337 if (FAN_CONFIG_CONTROL(data->fan_conf, i)) {
1338 err = device_create_file(dev, &pwm[i].dev_attr);
1344 err = device_create_file(dev, &dev_attr_name);
1348 data->hwmon_dev = hwmon_device_register(dev);
1349 if (IS_ERR(data->hwmon_dev)) {
1350 err = PTR_ERR(data->hwmon_dev);
1356 pc87360_remove_files(dev);
1360 static int pc87360_remove(struct platform_device *pdev)
1362 struct pc87360_data *data = platform_get_drvdata(pdev);
1364 hwmon_device_unregister(data->hwmon_dev);
1365 pc87360_remove_files(&pdev->dev);
1371 * ldi is the logical device index
1372 * bank is for voltages and temperatures only
1374 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1379 mutex_lock(&(data->lock));
1380 if (bank != NO_BANK)
1381 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1382 res = inb_p(data->address[ldi] + reg);
1383 mutex_unlock(&(data->lock));
1388 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1391 mutex_lock(&(data->lock));
1392 if (bank != NO_BANK)
1393 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1394 outb_p(value, data->address[ldi] + reg);
1395 mutex_unlock(&(data->lock));
1398 /* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
1399 #define CHAN_CNVRTD 0x80 /* new data ready */
1400 #define CHAN_ENA 0x01 /* enabled channel (temp or vin) */
1401 #define CHAN_ALM_ENA 0x10 /* propagate to alarms-reg ?? (chk val!) */
1402 #define CHAN_READY (CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
1404 #define TEMP_OTS_OE 0x20 /* OTS Output Enable */
1405 #define VIN_RW1C_MASK (CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN) /* 0x87 */
1406 #define TEMP_RW1C_MASK (VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
1408 static void pc87360_init_device(struct platform_device *pdev,
1409 int use_thermistors)
1411 struct pc87360_data *data = platform_get_drvdata(pdev);
1413 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1414 const u8 init_temp[3] = { 2, 2, 1 };
1417 if (init >= 2 && data->innr) {
1418 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1419 PC87365_REG_IN_CONVRATE);
1420 dev_info(&pdev->dev,
1421 "VLM conversion set to 1s period, 160us delay\n");
1422 pc87360_write_value(data, LD_IN, NO_BANK,
1423 PC87365_REG_IN_CONVRATE,
1424 (reg & 0xC0) | 0x11);
1427 nr = data->innr < 11 ? data->innr : 11;
1428 for (i = 0; i < nr; i++) {
1429 reg = pc87360_read_value(data, LD_IN, i,
1430 PC87365_REG_IN_STATUS);
1431 dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
1432 if (init >= init_in[i]) {
1433 /* Forcibly enable voltage channel */
1434 if (!(reg & CHAN_ENA)) {
1435 dev_dbg(&pdev->dev, "Forcibly enabling in%d\n",
1437 pc87360_write_value(data, LD_IN, i,
1438 PC87365_REG_IN_STATUS,
1439 (reg & 0x68) | 0x87);
1445 * We can't blindly trust the Super-I/O space configuration bit,
1446 * most BIOS won't set it properly
1448 dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
1449 for (i = 11; i < data->innr; i++) {
1450 reg = pc87360_read_value(data, LD_IN, i,
1451 PC87365_REG_TEMP_STATUS);
1452 use_thermistors = use_thermistors || (reg & CHAN_ENA);
1453 /* thermistors are temp[4-6], measured on vin[11-14] */
1454 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
1456 dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
1458 i = use_thermistors ? 2 : 0;
1459 for (; i < data->tempnr; i++) {
1460 reg = pc87360_read_value(data, LD_TEMP, i,
1461 PC87365_REG_TEMP_STATUS);
1462 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i + 1, reg);
1463 if (init >= init_temp[i]) {
1464 /* Forcibly enable temperature channel */
1465 if (!(reg & CHAN_ENA)) {
1467 "Forcibly enabling temp%d\n", i + 1);
1468 pc87360_write_value(data, LD_TEMP, i,
1469 PC87365_REG_TEMP_STATUS,
1475 if (use_thermistors) {
1476 for (i = 11; i < data->innr; i++) {
1477 if (init >= init_in[i]) {
1479 * The pin may already be used by thermal
1482 reg = pc87360_read_value(data, LD_TEMP,
1483 (i - 11) / 2, PC87365_REG_TEMP_STATUS);
1484 if (reg & CHAN_ENA) {
1486 "Skipping temp%d, pin already in use by temp%d\n",
1487 i - 7, (i - 11) / 2);
1491 /* Forcibly enable thermistor channel */
1492 reg = pc87360_read_value(data, LD_IN, i,
1493 PC87365_REG_IN_STATUS);
1494 if (!(reg & CHAN_ENA)) {
1496 "Forcibly enabling temp%d\n",
1498 pc87360_write_value(data, LD_IN, i,
1499 PC87365_REG_TEMP_STATUS,
1500 (reg & 0x60) | 0x8F);
1507 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1508 PC87365_REG_IN_CONFIG);
1509 dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
1510 if (reg & CHAN_ENA) {
1512 "Forcibly enabling monitoring (VLM)\n");
1513 pc87360_write_value(data, LD_IN, NO_BANK,
1514 PC87365_REG_IN_CONFIG,
1520 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1521 PC87365_REG_TEMP_CONFIG);
1522 dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
1523 if (reg & CHAN_ENA) {
1525 "Forcibly enabling monitoring (TMS)\n");
1526 pc87360_write_value(data, LD_TEMP, NO_BANK,
1527 PC87365_REG_TEMP_CONFIG,
1532 /* Chip config as documented by National Semi. */
1533 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1535 * We voluntarily omit the bank here, in case the
1536 * sequence itself matters. It shouldn't be a problem,
1537 * since nobody else is supposed to access the
1538 * device at that point.
1540 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1541 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1542 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1543 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1548 static void pc87360_autodiv(struct device *dev, int nr)
1550 struct pc87360_data *data = dev_get_drvdata(dev);
1551 u8 old_min = data->fan_min[nr];
1553 /* Increase clock divider if needed and possible */
1554 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1555 || (data->fan[nr] >= 224)) { /* next to overflow */
1556 if ((data->fan_status[nr] & 0x60) != 0x60) {
1557 data->fan_status[nr] += 0x20;
1558 data->fan_min[nr] >>= 1;
1559 data->fan[nr] >>= 1;
1561 "Increasing clock divider to %d for fan %d\n",
1562 FAN_DIV_FROM_REG(data->fan_status[nr]), nr + 1);
1565 /* Decrease clock divider if possible */
1566 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1567 && data->fan[nr] < 85 /* bad accuracy */
1568 && (data->fan_status[nr] & 0x60) != 0x00) {
1569 data->fan_status[nr] -= 0x20;
1570 data->fan_min[nr] <<= 1;
1571 data->fan[nr] <<= 1;
1573 "Decreasing clock divider to %d for fan %d\n",
1574 FAN_DIV_FROM_REG(data->fan_status[nr]),
1579 /* Write new fan min if it changed */
1580 if (old_min != data->fan_min[nr]) {
1581 pc87360_write_value(data, LD_FAN, NO_BANK,
1582 PC87360_REG_FAN_MIN(nr),
1587 static struct pc87360_data *pc87360_update_device(struct device *dev)
1589 struct pc87360_data *data = dev_get_drvdata(dev);
1592 mutex_lock(&data->update_lock);
1594 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1595 dev_dbg(dev, "Data update\n");
1598 for (i = 0; i < data->fannr; i++) {
1599 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1600 data->fan_status[i] =
1601 pc87360_read_value(data, LD_FAN,
1602 NO_BANK, PC87360_REG_FAN_STATUS(i));
1603 data->fan[i] = pc87360_read_value(data, LD_FAN,
1604 NO_BANK, PC87360_REG_FAN(i));
1605 data->fan_min[i] = pc87360_read_value(data,
1607 PC87360_REG_FAN_MIN(i));
1608 /* Change clock divider if needed */
1609 pc87360_autodiv(dev, i);
1610 /* Clear bits and write new divider */
1611 pc87360_write_value(data, LD_FAN, NO_BANK,
1612 PC87360_REG_FAN_STATUS(i),
1613 data->fan_status[i]);
1615 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1616 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1617 NO_BANK, PC87360_REG_PWM(i));
1621 for (i = 0; i < data->innr; i++) {
1622 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1623 PC87365_REG_IN_STATUS);
1625 pc87360_write_value(data, LD_IN, i,
1626 PC87365_REG_IN_STATUS,
1627 data->in_status[i]);
1628 if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
1629 data->in[i] = pc87360_read_value(data, LD_IN,
1632 if (data->in_status[i] & CHAN_ENA) {
1633 data->in_min[i] = pc87360_read_value(data,
1635 PC87365_REG_IN_MIN);
1636 data->in_max[i] = pc87360_read_value(data,
1638 PC87365_REG_IN_MAX);
1640 data->in_crit[i-11] =
1641 pc87360_read_value(data, LD_IN,
1642 i, PC87365_REG_TEMP_CRIT);
1646 data->in_alarms = pc87360_read_value(data, LD_IN,
1647 NO_BANK, PC87365_REG_IN_ALARMS1)
1648 | ((pc87360_read_value(data, LD_IN,
1649 NO_BANK, PC87365_REG_IN_ALARMS2)
1651 data->vid = (data->vid_conf & 0xE0) ?
1652 pc87360_read_value(data, LD_IN,
1653 NO_BANK, PC87365_REG_VID) : 0x1F;
1657 for (i = 0; i < data->tempnr; i++) {
1658 data->temp_status[i] = pc87360_read_value(data,
1660 PC87365_REG_TEMP_STATUS);
1662 pc87360_write_value(data, LD_TEMP, i,
1663 PC87365_REG_TEMP_STATUS,
1664 data->temp_status[i]);
1665 if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
1666 data->temp[i] = pc87360_read_value(data,
1670 if (data->temp_status[i] & CHAN_ENA) {
1671 data->temp_min[i] = pc87360_read_value(data,
1673 PC87365_REG_TEMP_MIN);
1674 data->temp_max[i] = pc87360_read_value(data,
1676 PC87365_REG_TEMP_MAX);
1677 data->temp_crit[i] = pc87360_read_value(data,
1679 PC87365_REG_TEMP_CRIT);
1683 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1684 NO_BANK, PC87365_REG_TEMP_ALARMS)
1688 data->last_updated = jiffies;
1692 mutex_unlock(&data->update_lock);
1697 static int __init pc87360_device_add(unsigned short address)
1699 struct resource res[3];
1700 int err, i, res_count;
1702 pdev = platform_device_alloc("pc87360", address);
1705 pr_err("Device allocation failed\n");
1709 memset(res, 0, 3 * sizeof(struct resource));
1711 for (i = 0; i < 3; i++) {
1714 res[res_count].start = extra_isa[i];
1715 res[res_count].end = extra_isa[i] + PC87360_EXTENT - 1;
1716 res[res_count].name = "pc87360",
1717 res[res_count].flags = IORESOURCE_IO,
1719 err = acpi_check_resource_conflict(&res[res_count]);
1721 goto exit_device_put;
1726 err = platform_device_add_resources(pdev, res, res_count);
1728 pr_err("Device resources addition failed (%d)\n", err);
1729 goto exit_device_put;
1732 err = platform_device_add(pdev);
1734 pr_err("Device addition failed (%d)\n", err);
1735 goto exit_device_put;
1741 platform_device_put(pdev);
1746 static int __init pc87360_init(void)
1749 unsigned short address = 0;
1751 if (pc87360_find(0x2e, &devid, extra_isa)
1752 && pc87360_find(0x4e, &devid, extra_isa)) {
1753 pr_warn("PC8736x not detected, module not inserted\n");
1757 /* Arbitrarily pick one of the addresses */
1758 for (i = 0; i < 3; i++) {
1759 if (extra_isa[i] != 0x0000) {
1760 address = extra_isa[i];
1765 if (address == 0x0000) {
1766 pr_warn("No active logical device, module not inserted\n");
1770 err = platform_driver_register(&pc87360_driver);
1774 /* Sets global pdev as a side effect */
1775 err = pc87360_device_add(address);
1782 platform_driver_unregister(&pc87360_driver);
1787 static void __exit pc87360_exit(void)
1789 platform_device_unregister(pdev);
1790 platform_driver_unregister(&pc87360_driver);
1794 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1795 MODULE_DESCRIPTION("PC8736x hardware monitor");
1796 MODULE_LICENSE("GPL");
1798 module_init(pc87360_init);
1799 module_exit(pc87360_exit);