1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * pc87360.c - Part of lm_sensors, Linux kernel modules
4 * for hardware monitoring
5 * Copyright (C) 2004, 2007 Jean Delvare <jdelvare@suse.de>
7 * Copied from smsc47m1.c:
8 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
10 * Supports the following chips:
12 * Chip #vin #fan #pwm #temp devid
13 * PC87360 - 2 2 - 0xE1
14 * PC87363 - 2 2 - 0xE8
15 * PC87364 - 3 3 - 0xE4
16 * PC87365 11 3 3 2 0xE5
17 * PC87366 11 3 3 3-4 0xE9
19 * This driver assumes that no more than one chip is present, and one of
20 * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/platform_device.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/hwmon-vid.h>
33 #include <linux/err.h>
34 #include <linux/mutex.h>
35 #include <linux/acpi.h>
38 #define DRIVER_NAME "pc87360"
40 /* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
41 #define CHAN_CNVRTD 0x80 /* new data ready */
42 #define CHAN_ENA 0x01 /* enabled channel (temp or vin) */
43 #define CHAN_ALM_ENA 0x10 /* propagate to alarms-reg ?? (chk val!) */
44 #define CHAN_READY (CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
46 #define TEMP_OTS_OE 0x20 /* OTS Output Enable */
47 #define VIN_RW1C_MASK (CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN) /* 0x87 */
48 #define TEMP_RW1C_MASK (VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
51 static struct platform_device *pdev;
52 static unsigned short extra_isa[3];
56 module_param(init, int, 0);
57 MODULE_PARM_DESC(init,
58 "Chip initialization level:\n"
60 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
61 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
62 " 3: Forcibly enable all voltage and temperature channels, including in9");
64 static unsigned short force_id;
65 module_param(force_id, ushort, 0);
66 MODULE_PARM_DESC(force_id, "Override the detected device ID");
69 * Super-I/O registers and operations
72 #define DEV 0x07 /* Register: Logical device select */
73 #define DEVID 0x20 /* Register: Device ID */
74 #define ACT 0x30 /* Register: Device activation */
75 #define BASE 0x60 /* Register: Base address */
77 #define FSCM 0x09 /* Logical device: fans */
78 #define VLM 0x0d /* Logical device: voltages */
79 #define TMS 0x0e /* Logical device: temperatures */
81 static const u8 logdev[LDNI_MAX] = { FSCM, VLM, TMS };
87 static inline void superio_outb(int sioaddr, int reg, int val)
90 outb(val, sioaddr + 1);
93 static inline int superio_inb(int sioaddr, int reg)
96 return inb(sioaddr + 1);
99 static inline void superio_exit(int sioaddr)
102 outb(0x02, sioaddr + 1);
109 #define PC87360_EXTENT 0x10
110 #define PC87365_REG_BANK 0x09
114 * Fan registers and conversions
117 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
118 #define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
119 #define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
120 #define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
121 #define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
122 #define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
124 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : \
125 480000 / ((val) * (div)))
126 #define FAN_TO_REG(val, div) ((val) <= 100 ? 0 : \
127 480000 / ((val) * (div)))
128 #define FAN_DIV_FROM_REG(val) (1 << (((val) >> 5) & 0x03))
129 #define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
131 #define FAN_CONFIG_MONITOR(val, nr) (((val) >> (2 + (nr) * 3)) & 1)
132 #define FAN_CONFIG_CONTROL(val, nr) (((val) >> (3 + (nr) * 3)) & 1)
133 #define FAN_CONFIG_INVERT(val, nr) (((val) >> (4 + (nr) * 3)) & 1)
135 #define PWM_FROM_REG(val, inv) ((inv) ? 255 - (val) : (val))
136 static inline u8 PWM_TO_REG(int val, int inv)
148 * Voltage registers and conversions
151 #define PC87365_REG_IN_CONVRATE 0x07
152 #define PC87365_REG_IN_CONFIG 0x08
153 #define PC87365_REG_IN 0x0B
154 #define PC87365_REG_IN_MIN 0x0D
155 #define PC87365_REG_IN_MAX 0x0C
156 #define PC87365_REG_IN_STATUS 0x0A
157 #define PC87365_REG_IN_ALARMS1 0x00
158 #define PC87365_REG_IN_ALARMS2 0x01
159 #define PC87365_REG_VID 0x06
161 #define IN_FROM_REG(val, ref) (((val) * (ref) + 128) / 256)
162 #define IN_TO_REG(val, ref) ((val) < 0 ? 0 : \
163 (val) * 256 >= (ref) * 255 ? 255 : \
164 ((val) * 256 + (ref) / 2) / (ref))
167 * Temperature registers and conversions
170 #define PC87365_REG_TEMP_CONFIG 0x08
171 #define PC87365_REG_TEMP 0x0B
172 #define PC87365_REG_TEMP_MIN 0x0D
173 #define PC87365_REG_TEMP_MAX 0x0C
174 #define PC87365_REG_TEMP_CRIT 0x0E
175 #define PC87365_REG_TEMP_STATUS 0x0A
176 #define PC87365_REG_TEMP_ALARMS 0x00
178 #define TEMP_FROM_REG(val) ((val) * 1000)
179 #define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
180 (val) > 127000 ? 127 : \
181 (val) < 0 ? ((val) - 500) / 1000 : \
182 ((val) + 500) / 1000)
188 struct pc87360_data {
190 struct device *hwmon_dev;
192 struct mutex update_lock;
193 bool valid; /* true if following fields are valid */
194 unsigned long last_updated; /* In jiffies */
198 u8 fannr, innr, tempnr;
200 u8 fan[3]; /* Register value */
201 u8 fan_min[3]; /* Register value */
202 u8 fan_status[3]; /* Register value */
203 u8 pwm[3]; /* Register value */
204 u16 fan_conf; /* Configuration register values, combined */
206 u16 in_vref; /* 1 mV/bit */
207 u8 in[14]; /* Register value */
208 u8 in_min[14]; /* Register value */
209 u8 in_max[14]; /* Register value */
210 u8 in_crit[3]; /* Register value */
211 u8 in_status[14]; /* Register value */
212 u16 in_alarms; /* Register values, combined, masked */
213 u8 vid_conf; /* Configuration register value */
215 u8 vid; /* Register value */
217 s8 temp[3]; /* Register value */
218 s8 temp_min[3]; /* Register value */
219 s8 temp_max[3]; /* Register value */
220 s8 temp_crit[3]; /* Register value */
221 u8 temp_status[3]; /* Register value */
222 u8 temp_alarms; /* Register value, masked */
226 * ldi is the logical device index
227 * bank is for voltages and temperatures only
229 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
234 mutex_lock(&(data->lock));
236 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
237 res = inb_p(data->address[ldi] + reg);
238 mutex_unlock(&(data->lock));
243 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
246 mutex_lock(&(data->lock));
248 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
249 outb_p(value, data->address[ldi] + reg);
250 mutex_unlock(&(data->lock));
253 static void pc87360_autodiv(struct device *dev, int nr)
255 struct pc87360_data *data = dev_get_drvdata(dev);
256 u8 old_min = data->fan_min[nr];
258 /* Increase clock divider if needed and possible */
259 if ((data->fan_status[nr] & 0x04) /* overflow flag */
260 || (data->fan[nr] >= 224)) { /* next to overflow */
261 if ((data->fan_status[nr] & 0x60) != 0x60) {
262 data->fan_status[nr] += 0x20;
263 data->fan_min[nr] >>= 1;
266 "Increasing clock divider to %d for fan %d\n",
267 FAN_DIV_FROM_REG(data->fan_status[nr]), nr + 1);
270 /* Decrease clock divider if possible */
271 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
272 && data->fan[nr] < 85 /* bad accuracy */
273 && (data->fan_status[nr] & 0x60) != 0x00) {
274 data->fan_status[nr] -= 0x20;
275 data->fan_min[nr] <<= 1;
278 "Decreasing clock divider to %d for fan %d\n",
279 FAN_DIV_FROM_REG(data->fan_status[nr]),
284 /* Write new fan min if it changed */
285 if (old_min != data->fan_min[nr]) {
286 pc87360_write_value(data, LD_FAN, NO_BANK,
287 PC87360_REG_FAN_MIN(nr),
292 static struct pc87360_data *pc87360_update_device(struct device *dev)
294 struct pc87360_data *data = dev_get_drvdata(dev);
297 mutex_lock(&data->update_lock);
299 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
300 dev_dbg(dev, "Data update\n");
303 for (i = 0; i < data->fannr; i++) {
304 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
305 data->fan_status[i] =
306 pc87360_read_value(data, LD_FAN,
307 NO_BANK, PC87360_REG_FAN_STATUS(i));
308 data->fan[i] = pc87360_read_value(data, LD_FAN,
309 NO_BANK, PC87360_REG_FAN(i));
310 data->fan_min[i] = pc87360_read_value(data,
312 PC87360_REG_FAN_MIN(i));
313 /* Change clock divider if needed */
314 pc87360_autodiv(dev, i);
315 /* Clear bits and write new divider */
316 pc87360_write_value(data, LD_FAN, NO_BANK,
317 PC87360_REG_FAN_STATUS(i),
318 data->fan_status[i]);
320 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
321 data->pwm[i] = pc87360_read_value(data, LD_FAN,
322 NO_BANK, PC87360_REG_PWM(i));
326 for (i = 0; i < data->innr; i++) {
327 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
328 PC87365_REG_IN_STATUS);
330 pc87360_write_value(data, LD_IN, i,
331 PC87365_REG_IN_STATUS,
333 if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
334 data->in[i] = pc87360_read_value(data, LD_IN,
337 if (data->in_status[i] & CHAN_ENA) {
338 data->in_min[i] = pc87360_read_value(data,
341 data->in_max[i] = pc87360_read_value(data,
345 data->in_crit[i-11] =
346 pc87360_read_value(data, LD_IN,
347 i, PC87365_REG_TEMP_CRIT);
351 data->in_alarms = pc87360_read_value(data, LD_IN,
352 NO_BANK, PC87365_REG_IN_ALARMS1)
353 | ((pc87360_read_value(data, LD_IN,
354 NO_BANK, PC87365_REG_IN_ALARMS2)
356 data->vid = (data->vid_conf & 0xE0) ?
357 pc87360_read_value(data, LD_IN,
358 NO_BANK, PC87365_REG_VID) : 0x1F;
362 for (i = 0; i < data->tempnr; i++) {
363 data->temp_status[i] = pc87360_read_value(data,
365 PC87365_REG_TEMP_STATUS);
367 pc87360_write_value(data, LD_TEMP, i,
368 PC87365_REG_TEMP_STATUS,
369 data->temp_status[i]);
370 if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
371 data->temp[i] = pc87360_read_value(data,
375 if (data->temp_status[i] & CHAN_ENA) {
376 data->temp_min[i] = pc87360_read_value(data,
378 PC87365_REG_TEMP_MIN);
379 data->temp_max[i] = pc87360_read_value(data,
381 PC87365_REG_TEMP_MAX);
382 data->temp_crit[i] = pc87360_read_value(data,
384 PC87365_REG_TEMP_CRIT);
388 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
389 NO_BANK, PC87365_REG_TEMP_ALARMS)
393 data->last_updated = jiffies;
397 mutex_unlock(&data->update_lock);
402 static ssize_t in_input_show(struct device *dev,
403 struct device_attribute *devattr, char *buf)
405 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
406 struct pc87360_data *data = pc87360_update_device(dev);
407 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
411 static struct sensor_device_attribute in_input[] = {
412 SENSOR_ATTR_RO(in0_input, in_input, 0),
413 SENSOR_ATTR_RO(in1_input, in_input, 1),
414 SENSOR_ATTR_RO(in2_input, in_input, 2),
415 SENSOR_ATTR_RO(in3_input, in_input, 3),
416 SENSOR_ATTR_RO(in4_input, in_input, 4),
417 SENSOR_ATTR_RO(in5_input, in_input, 5),
418 SENSOR_ATTR_RO(in6_input, in_input, 6),
419 SENSOR_ATTR_RO(in7_input, in_input, 7),
420 SENSOR_ATTR_RO(in8_input, in_input, 8),
421 SENSOR_ATTR_RO(in9_input, in_input, 9),
422 SENSOR_ATTR_RO(in10_input, in_input, 10),
425 static ssize_t in_status_show(struct device *dev,
426 struct device_attribute *devattr, char *buf)
428 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
429 struct pc87360_data *data = pc87360_update_device(dev);
430 return sprintf(buf, "%u\n", data->in_status[attr->index]);
433 static struct sensor_device_attribute in_status[] = {
434 SENSOR_ATTR_RO(in0_status, in_status, 0),
435 SENSOR_ATTR_RO(in1_status, in_status, 1),
436 SENSOR_ATTR_RO(in2_status, in_status, 2),
437 SENSOR_ATTR_RO(in3_status, in_status, 3),
438 SENSOR_ATTR_RO(in4_status, in_status, 4),
439 SENSOR_ATTR_RO(in5_status, in_status, 5),
440 SENSOR_ATTR_RO(in6_status, in_status, 6),
441 SENSOR_ATTR_RO(in7_status, in_status, 7),
442 SENSOR_ATTR_RO(in8_status, in_status, 8),
443 SENSOR_ATTR_RO(in9_status, in_status, 9),
444 SENSOR_ATTR_RO(in10_status, in_status, 10),
447 static ssize_t in_min_show(struct device *dev,
448 struct device_attribute *devattr, char *buf)
450 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
451 struct pc87360_data *data = pc87360_update_device(dev);
452 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
456 static ssize_t in_min_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_min[attr->index] = IN_TO_REG(val, data->in_vref);
471 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
472 data->in_min[attr->index]);
473 mutex_unlock(&data->update_lock);
477 static struct sensor_device_attribute in_min[] = {
478 SENSOR_ATTR_RW(in0_min, in_min, 0),
479 SENSOR_ATTR_RW(in1_min, in_min, 1),
480 SENSOR_ATTR_RW(in2_min, in_min, 2),
481 SENSOR_ATTR_RW(in3_min, in_min, 3),
482 SENSOR_ATTR_RW(in4_min, in_min, 4),
483 SENSOR_ATTR_RW(in5_min, in_min, 5),
484 SENSOR_ATTR_RW(in6_min, in_min, 6),
485 SENSOR_ATTR_RW(in7_min, in_min, 7),
486 SENSOR_ATTR_RW(in8_min, in_min, 8),
487 SENSOR_ATTR_RW(in9_min, in_min, 9),
488 SENSOR_ATTR_RW(in10_min, in_min, 10),
491 static ssize_t in_max_show(struct device *dev,
492 struct device_attribute *devattr, char *buf)
494 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
495 struct pc87360_data *data = pc87360_update_device(dev);
496 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
500 static ssize_t in_max_store(struct device *dev,
501 struct device_attribute *devattr, const char *buf,
504 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
505 struct pc87360_data *data = dev_get_drvdata(dev);
509 err = kstrtol(buf, 10, &val);
513 mutex_lock(&data->update_lock);
514 data->in_max[attr->index] = IN_TO_REG(val,
516 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
517 data->in_max[attr->index]);
518 mutex_unlock(&data->update_lock);
522 static struct sensor_device_attribute in_max[] = {
523 SENSOR_ATTR_RW(in0_max, in_max, 0),
524 SENSOR_ATTR_RW(in1_max, in_max, 1),
525 SENSOR_ATTR_RW(in2_max, in_max, 2),
526 SENSOR_ATTR_RW(in3_max, in_max, 3),
527 SENSOR_ATTR_RW(in4_max, in_max, 4),
528 SENSOR_ATTR_RW(in5_max, in_max, 5),
529 SENSOR_ATTR_RW(in6_max, in_max, 6),
530 SENSOR_ATTR_RW(in7_max, in_max, 7),
531 SENSOR_ATTR_RW(in8_max, in_max, 8),
532 SENSOR_ATTR_RW(in9_max, in_max, 9),
533 SENSOR_ATTR_RW(in10_max, in_max, 10),
536 /* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
537 #define CHAN_ALM_MIN 0x02 /* min limit crossed */
538 #define CHAN_ALM_MAX 0x04 /* max limit exceeded */
539 #define TEMP_ALM_CRIT 0x08 /* temp crit exceeded (temp only) */
542 * show_in_min/max_alarm() reads data from the per-channel status
543 * register (sec 11.5.12), not the vin event status registers (sec
544 * 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms)
547 static ssize_t in_min_alarm_show(struct device *dev,
548 struct device_attribute *devattr, char *buf)
550 struct pc87360_data *data = pc87360_update_device(dev);
551 unsigned nr = to_sensor_dev_attr(devattr)->index;
553 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
556 static struct sensor_device_attribute in_min_alarm[] = {
557 SENSOR_ATTR_RO(in0_min_alarm, in_min_alarm, 0),
558 SENSOR_ATTR_RO(in1_min_alarm, in_min_alarm, 1),
559 SENSOR_ATTR_RO(in2_min_alarm, in_min_alarm, 2),
560 SENSOR_ATTR_RO(in3_min_alarm, in_min_alarm, 3),
561 SENSOR_ATTR_RO(in4_min_alarm, in_min_alarm, 4),
562 SENSOR_ATTR_RO(in5_min_alarm, in_min_alarm, 5),
563 SENSOR_ATTR_RO(in6_min_alarm, in_min_alarm, 6),
564 SENSOR_ATTR_RO(in7_min_alarm, in_min_alarm, 7),
565 SENSOR_ATTR_RO(in8_min_alarm, in_min_alarm, 8),
566 SENSOR_ATTR_RO(in9_min_alarm, in_min_alarm, 9),
567 SENSOR_ATTR_RO(in10_min_alarm, in_min_alarm, 10),
570 static ssize_t in_max_alarm_show(struct device *dev,
571 struct device_attribute *devattr, char *buf)
573 struct pc87360_data *data = pc87360_update_device(dev);
574 unsigned nr = to_sensor_dev_attr(devattr)->index;
576 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
579 static struct sensor_device_attribute in_max_alarm[] = {
580 SENSOR_ATTR_RO(in0_max_alarm, in_max_alarm, 0),
581 SENSOR_ATTR_RO(in1_max_alarm, in_max_alarm, 1),
582 SENSOR_ATTR_RO(in2_max_alarm, in_max_alarm, 2),
583 SENSOR_ATTR_RO(in3_max_alarm, in_max_alarm, 3),
584 SENSOR_ATTR_RO(in4_max_alarm, in_max_alarm, 4),
585 SENSOR_ATTR_RO(in5_max_alarm, in_max_alarm, 5),
586 SENSOR_ATTR_RO(in6_max_alarm, in_max_alarm, 6),
587 SENSOR_ATTR_RO(in7_max_alarm, in_max_alarm, 7),
588 SENSOR_ATTR_RO(in8_max_alarm, in_max_alarm, 8),
589 SENSOR_ATTR_RO(in9_max_alarm, in_max_alarm, 9),
590 SENSOR_ATTR_RO(in10_max_alarm, in_max_alarm, 10),
593 #define VIN_UNIT_ATTRS(X) \
594 &in_input[X].dev_attr.attr, \
595 &in_status[X].dev_attr.attr, \
596 &in_min[X].dev_attr.attr, \
597 &in_max[X].dev_attr.attr, \
598 &in_min_alarm[X].dev_attr.attr, \
599 &in_max_alarm[X].dev_attr.attr
601 static ssize_t cpu0_vid_show(struct device *dev,
602 struct device_attribute *attr, char *buf)
604 struct pc87360_data *data = pc87360_update_device(dev);
605 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
607 static DEVICE_ATTR_RO(cpu0_vid);
609 static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
612 struct pc87360_data *data = dev_get_drvdata(dev);
613 return sprintf(buf, "%u\n", data->vrm);
616 static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
617 const char *buf, size_t count)
619 struct pc87360_data *data = dev_get_drvdata(dev);
623 err = kstrtoul(buf, 10, &val);
633 static DEVICE_ATTR_RW(vrm);
635 static ssize_t alarms_in_show(struct device *dev,
636 struct device_attribute *attr, char *buf)
638 struct pc87360_data *data = pc87360_update_device(dev);
639 return sprintf(buf, "%u\n", data->in_alarms);
641 static DEVICE_ATTR_RO(alarms_in);
643 static struct attribute *pc8736x_vin_attr_array[] = {
655 &dev_attr_cpu0_vid.attr,
657 &dev_attr_alarms_in.attr,
660 static const struct attribute_group pc8736x_vin_group = {
661 .attrs = pc8736x_vin_attr_array,
664 static ssize_t therm_input_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[attr->index],
674 * the +11 term below reflects the fact that VLM units 11,12,13 are
675 * used in the chip to measure voltage across the thermistors
677 static struct sensor_device_attribute therm_input[] = {
678 SENSOR_ATTR_RO(temp4_input, therm_input, 0 + 11),
679 SENSOR_ATTR_RO(temp5_input, therm_input, 1 + 11),
680 SENSOR_ATTR_RO(temp6_input, therm_input, 2 + 11),
683 static ssize_t therm_status_show(struct device *dev,
684 struct device_attribute *devattr, char *buf)
686 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
687 struct pc87360_data *data = pc87360_update_device(dev);
688 return sprintf(buf, "%u\n", data->in_status[attr->index]);
691 static struct sensor_device_attribute therm_status[] = {
692 SENSOR_ATTR_RO(temp4_status, therm_status, 0 + 11),
693 SENSOR_ATTR_RO(temp5_status, therm_status, 1 + 11),
694 SENSOR_ATTR_RO(temp6_status, therm_status, 2 + 11),
697 static ssize_t therm_min_show(struct device *dev,
698 struct device_attribute *devattr, char *buf)
700 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
701 struct pc87360_data *data = pc87360_update_device(dev);
702 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
706 static ssize_t therm_min_store(struct device *dev,
707 struct device_attribute *devattr,
708 const char *buf, size_t count)
710 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
711 struct pc87360_data *data = dev_get_drvdata(dev);
715 err = kstrtol(buf, 10, &val);
719 mutex_lock(&data->update_lock);
720 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
721 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
722 data->in_min[attr->index]);
723 mutex_unlock(&data->update_lock);
727 static struct sensor_device_attribute therm_min[] = {
728 SENSOR_ATTR_RW(temp4_min, therm_min, 0 + 11),
729 SENSOR_ATTR_RW(temp5_min, therm_min, 1 + 11),
730 SENSOR_ATTR_RW(temp6_min, therm_min, 2 + 11),
733 static ssize_t therm_max_show(struct device *dev,
734 struct device_attribute *devattr, char *buf)
736 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
737 struct pc87360_data *data = pc87360_update_device(dev);
738 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
742 static ssize_t therm_max_store(struct device *dev,
743 struct device_attribute *devattr,
744 const char *buf, size_t count)
746 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
747 struct pc87360_data *data = dev_get_drvdata(dev);
751 err = kstrtol(buf, 10, &val);
755 mutex_lock(&data->update_lock);
756 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
757 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
758 data->in_max[attr->index]);
759 mutex_unlock(&data->update_lock);
763 static struct sensor_device_attribute therm_max[] = {
764 SENSOR_ATTR_RW(temp4_max, therm_max, 0 + 11),
765 SENSOR_ATTR_RW(temp5_max, therm_max, 1 + 11),
766 SENSOR_ATTR_RW(temp6_max, therm_max, 2 + 11),
769 static ssize_t therm_crit_show(struct device *dev,
770 struct device_attribute *devattr, char *buf)
772 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
773 struct pc87360_data *data = pc87360_update_device(dev);
774 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
778 static ssize_t therm_crit_store(struct device *dev,
779 struct device_attribute *devattr,
780 const char *buf, size_t count)
782 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
783 struct pc87360_data *data = dev_get_drvdata(dev);
787 err = kstrtol(buf, 10, &val);
791 mutex_lock(&data->update_lock);
792 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
793 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
794 data->in_crit[attr->index-11]);
795 mutex_unlock(&data->update_lock);
799 static struct sensor_device_attribute therm_crit[] = {
800 SENSOR_ATTR_RW(temp4_crit, therm_crit, 0 + 11),
801 SENSOR_ATTR_RW(temp5_crit, therm_crit, 1 + 11),
802 SENSOR_ATTR_RW(temp6_crit, therm_crit, 2 + 11),
806 * show_therm_min/max_alarm() reads data from the per-channel voltage
807 * status register (sec 11.5.12)
809 static ssize_t therm_min_alarm_show(struct device *dev,
810 struct device_attribute *devattr,
813 struct pc87360_data *data = pc87360_update_device(dev);
814 unsigned nr = to_sensor_dev_attr(devattr)->index;
816 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
819 static struct sensor_device_attribute therm_min_alarm[] = {
820 SENSOR_ATTR_RO(temp4_min_alarm, therm_min_alarm, 0 + 11),
821 SENSOR_ATTR_RO(temp5_min_alarm, therm_min_alarm, 1 + 11),
822 SENSOR_ATTR_RO(temp6_min_alarm, therm_min_alarm, 2 + 11),
825 static ssize_t therm_max_alarm_show(struct device *dev,
826 struct device_attribute *devattr,
829 struct pc87360_data *data = pc87360_update_device(dev);
830 unsigned nr = to_sensor_dev_attr(devattr)->index;
832 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
835 static struct sensor_device_attribute therm_max_alarm[] = {
836 SENSOR_ATTR_RO(temp4_max_alarm, therm_max_alarm, 0 + 11),
837 SENSOR_ATTR_RO(temp5_max_alarm, therm_max_alarm, 1 + 11),
838 SENSOR_ATTR_RO(temp6_max_alarm, therm_max_alarm, 2 + 11),
841 static ssize_t therm_crit_alarm_show(struct device *dev,
842 struct device_attribute *devattr,
845 struct pc87360_data *data = pc87360_update_device(dev);
846 unsigned nr = to_sensor_dev_attr(devattr)->index;
848 return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT));
851 static struct sensor_device_attribute therm_crit_alarm[] = {
852 SENSOR_ATTR_RO(temp4_crit_alarm, therm_crit_alarm, 0 + 11),
853 SENSOR_ATTR_RO(temp5_crit_alarm, therm_crit_alarm, 1 + 11),
854 SENSOR_ATTR_RO(temp6_crit_alarm, therm_crit_alarm, 2 + 11),
857 #define THERM_UNIT_ATTRS(X) \
858 &therm_input[X].dev_attr.attr, \
859 &therm_status[X].dev_attr.attr, \
860 &therm_min[X].dev_attr.attr, \
861 &therm_max[X].dev_attr.attr, \
862 &therm_crit[X].dev_attr.attr, \
863 &therm_min_alarm[X].dev_attr.attr, \
864 &therm_max_alarm[X].dev_attr.attr, \
865 &therm_crit_alarm[X].dev_attr.attr
867 static struct attribute *pc8736x_therm_attr_array[] = {
873 static const struct attribute_group pc8736x_therm_group = {
874 .attrs = pc8736x_therm_attr_array,
877 static ssize_t temp_input_show(struct device *dev,
878 struct device_attribute *devattr, char *buf)
880 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
881 struct pc87360_data *data = pc87360_update_device(dev);
882 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
885 static struct sensor_device_attribute temp_input[] = {
886 SENSOR_ATTR_RO(temp1_input, temp_input, 0),
887 SENSOR_ATTR_RO(temp2_input, temp_input, 1),
888 SENSOR_ATTR_RO(temp3_input, temp_input, 2),
891 static ssize_t temp_status_show(struct device *dev,
892 struct device_attribute *devattr, char *buf)
894 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
895 struct pc87360_data *data = pc87360_update_device(dev);
896 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
899 static struct sensor_device_attribute temp_status[] = {
900 SENSOR_ATTR_RO(temp1_status, temp_status, 0),
901 SENSOR_ATTR_RO(temp2_status, temp_status, 1),
902 SENSOR_ATTR_RO(temp3_status, temp_status, 2),
905 static ssize_t temp_min_show(struct device *dev,
906 struct device_attribute *devattr, char *buf)
908 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
909 struct pc87360_data *data = pc87360_update_device(dev);
910 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
913 static ssize_t temp_min_store(struct device *dev,
914 struct device_attribute *devattr,
915 const char *buf, size_t count)
917 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
918 struct pc87360_data *data = dev_get_drvdata(dev);
922 err = kstrtol(buf, 10, &val);
926 mutex_lock(&data->update_lock);
927 data->temp_min[attr->index] = TEMP_TO_REG(val);
928 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
929 data->temp_min[attr->index]);
930 mutex_unlock(&data->update_lock);
934 static struct sensor_device_attribute temp_min[] = {
935 SENSOR_ATTR_RW(temp1_min, temp_min, 0),
936 SENSOR_ATTR_RW(temp2_min, temp_min, 1),
937 SENSOR_ATTR_RW(temp3_min, temp_min, 2),
940 static ssize_t temp_max_show(struct device *dev,
941 struct device_attribute *devattr, char *buf)
943 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
944 struct pc87360_data *data = pc87360_update_device(dev);
945 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
948 static ssize_t temp_max_store(struct device *dev,
949 struct device_attribute *devattr,
950 const char *buf, size_t count)
952 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
953 struct pc87360_data *data = dev_get_drvdata(dev);
957 err = kstrtol(buf, 10, &val);
961 mutex_lock(&data->update_lock);
962 data->temp_max[attr->index] = TEMP_TO_REG(val);
963 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
964 data->temp_max[attr->index]);
965 mutex_unlock(&data->update_lock);
969 static struct sensor_device_attribute temp_max[] = {
970 SENSOR_ATTR_RW(temp1_max, temp_max, 0),
971 SENSOR_ATTR_RW(temp2_max, temp_max, 1),
972 SENSOR_ATTR_RW(temp3_max, temp_max, 2),
975 static ssize_t temp_crit_show(struct device *dev,
976 struct device_attribute *devattr, char *buf)
978 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
979 struct pc87360_data *data = pc87360_update_device(dev);
980 return sprintf(buf, "%d\n",
981 TEMP_FROM_REG(data->temp_crit[attr->index]));
984 static ssize_t temp_crit_store(struct device *dev,
985 struct device_attribute *devattr,
986 const char *buf, size_t count)
988 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
989 struct pc87360_data *data = dev_get_drvdata(dev);
993 err = kstrtol(buf, 10, &val);
997 mutex_lock(&data->update_lock);
998 data->temp_crit[attr->index] = TEMP_TO_REG(val);
999 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
1000 data->temp_crit[attr->index]);
1001 mutex_unlock(&data->update_lock);
1005 static struct sensor_device_attribute temp_crit[] = {
1006 SENSOR_ATTR_RW(temp1_crit, temp_crit, 0),
1007 SENSOR_ATTR_RW(temp2_crit, temp_crit, 1),
1008 SENSOR_ATTR_RW(temp3_crit, temp_crit, 2),
1012 * temp_min/max_alarm_show() reads data from the per-channel status
1013 * register (sec 12.3.7), not the temp event status registers (sec
1014 * 12.3.2) that show_temp_alarm() reads (via data->temp_alarms)
1016 static ssize_t temp_min_alarm_show(struct device *dev,
1017 struct device_attribute *devattr,
1020 struct pc87360_data *data = pc87360_update_device(dev);
1021 unsigned nr = to_sensor_dev_attr(devattr)->index;
1023 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
1026 static struct sensor_device_attribute temp_min_alarm[] = {
1027 SENSOR_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0),
1028 SENSOR_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1),
1029 SENSOR_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2),
1032 static ssize_t temp_max_alarm_show(struct device *dev,
1033 struct device_attribute *devattr,
1036 struct pc87360_data *data = pc87360_update_device(dev);
1037 unsigned nr = to_sensor_dev_attr(devattr)->index;
1039 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
1042 static struct sensor_device_attribute temp_max_alarm[] = {
1043 SENSOR_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0),
1044 SENSOR_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1),
1045 SENSOR_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2),
1048 static ssize_t temp_crit_alarm_show(struct device *dev,
1049 struct device_attribute *devattr,
1052 struct pc87360_data *data = pc87360_update_device(dev);
1053 unsigned nr = to_sensor_dev_attr(devattr)->index;
1055 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
1058 static struct sensor_device_attribute temp_crit_alarm[] = {
1059 SENSOR_ATTR_RO(temp1_crit_alarm, temp_crit_alarm, 0),
1060 SENSOR_ATTR_RO(temp2_crit_alarm, temp_crit_alarm, 1),
1061 SENSOR_ATTR_RO(temp3_crit_alarm, temp_crit_alarm, 2),
1064 #define TEMP_FAULT 0x40 /* open diode */
1065 static ssize_t temp_fault_show(struct device *dev,
1066 struct device_attribute *devattr, char *buf)
1068 struct pc87360_data *data = pc87360_update_device(dev);
1069 unsigned nr = to_sensor_dev_attr(devattr)->index;
1071 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
1074 static struct sensor_device_attribute temp_fault[] = {
1075 SENSOR_ATTR_RO(temp1_fault, temp_fault, 0),
1076 SENSOR_ATTR_RO(temp2_fault, temp_fault, 1),
1077 SENSOR_ATTR_RO(temp3_fault, temp_fault, 2),
1080 #define TEMP_UNIT_ATTRS(X) \
1081 { &temp_input[X].dev_attr.attr, \
1082 &temp_status[X].dev_attr.attr, \
1083 &temp_min[X].dev_attr.attr, \
1084 &temp_max[X].dev_attr.attr, \
1085 &temp_crit[X].dev_attr.attr, \
1086 &temp_min_alarm[X].dev_attr.attr, \
1087 &temp_max_alarm[X].dev_attr.attr, \
1088 &temp_crit_alarm[X].dev_attr.attr, \
1089 &temp_fault[X].dev_attr.attr, \
1093 static struct attribute *pc8736x_temp_attr[][10] = {
1099 static const struct attribute_group pc8736x_temp_attr_group[] = {
1100 { .attrs = pc8736x_temp_attr[0] },
1101 { .attrs = pc8736x_temp_attr[1] },
1102 { .attrs = pc8736x_temp_attr[2] }
1105 static ssize_t alarms_temp_show(struct device *dev,
1106 struct device_attribute *attr, char *buf)
1108 struct pc87360_data *data = pc87360_update_device(dev);
1109 return sprintf(buf, "%u\n", data->temp_alarms);
1112 static DEVICE_ATTR_RO(alarms_temp);
1114 static ssize_t fan_input_show(struct device *dev,
1115 struct device_attribute *devattr, char *buf)
1117 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1118 struct pc87360_data *data = pc87360_update_device(dev);
1119 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
1120 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
1123 static struct sensor_device_attribute fan_input[] = {
1124 SENSOR_ATTR_RO(fan1_input, fan_input, 0),
1125 SENSOR_ATTR_RO(fan2_input, fan_input, 1),
1126 SENSOR_ATTR_RO(fan3_input, fan_input, 2),
1129 static ssize_t fan_status_show(struct device *dev,
1130 struct device_attribute *devattr, char *buf)
1132 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1133 struct pc87360_data *data = pc87360_update_device(dev);
1134 return sprintf(buf, "%u\n",
1135 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
1138 static struct sensor_device_attribute fan_status[] = {
1139 SENSOR_ATTR_RO(fan1_status, fan_status, 0),
1140 SENSOR_ATTR_RO(fan2_status, fan_status, 1),
1141 SENSOR_ATTR_RO(fan3_status, fan_status, 2),
1144 static ssize_t fan_div_show(struct device *dev,
1145 struct device_attribute *devattr, char *buf)
1147 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1148 struct pc87360_data *data = pc87360_update_device(dev);
1149 return sprintf(buf, "%u\n",
1150 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
1153 static struct sensor_device_attribute fan_div[] = {
1154 SENSOR_ATTR_RO(fan1_div, fan_div, 0),
1155 SENSOR_ATTR_RO(fan2_div, fan_div, 1),
1156 SENSOR_ATTR_RO(fan3_div, fan_div, 2),
1159 static ssize_t fan_min_show(struct device *dev,
1160 struct device_attribute *devattr, char *buf)
1162 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1163 struct pc87360_data *data = pc87360_update_device(dev);
1164 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
1165 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
1168 static ssize_t fan_min_store(struct device *dev,
1169 struct device_attribute *devattr,
1170 const char *buf, size_t count)
1172 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1173 struct pc87360_data *data = dev_get_drvdata(dev);
1177 err = kstrtol(buf, 10, &fan_min);
1181 mutex_lock(&data->update_lock);
1182 fan_min = FAN_TO_REG(fan_min,
1183 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
1185 /* If it wouldn't fit, change clock divisor */
1186 while (fan_min > 255
1187 && (data->fan_status[attr->index] & 0x60) != 0x60) {
1189 data->fan[attr->index] >>= 1;
1190 data->fan_status[attr->index] += 0x20;
1192 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
1193 pc87360_write_value(data, LD_FAN, NO_BANK,
1194 PC87360_REG_FAN_MIN(attr->index),
1195 data->fan_min[attr->index]);
1197 /* Write new divider, preserve alarm bits */
1198 pc87360_write_value(data, LD_FAN, NO_BANK,
1199 PC87360_REG_FAN_STATUS(attr->index),
1200 data->fan_status[attr->index] & 0xF9);
1201 mutex_unlock(&data->update_lock);
1206 static struct sensor_device_attribute fan_min[] = {
1207 SENSOR_ATTR_RW(fan1_min, fan_min, 0),
1208 SENSOR_ATTR_RW(fan2_min, fan_min, 1),
1209 SENSOR_ATTR_RW(fan3_min, fan_min, 2),
1212 #define FAN_UNIT_ATTRS(X) \
1213 { &fan_input[X].dev_attr.attr, \
1214 &fan_status[X].dev_attr.attr, \
1215 &fan_div[X].dev_attr.attr, \
1216 &fan_min[X].dev_attr.attr, \
1220 static struct attribute *pc8736x_fan_attr[][5] = {
1226 static const struct attribute_group pc8736x_fan_attr_group[] = {
1227 { .attrs = pc8736x_fan_attr[0], },
1228 { .attrs = pc8736x_fan_attr[1], },
1229 { .attrs = pc8736x_fan_attr[2], },
1232 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
1235 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1236 struct pc87360_data *data = pc87360_update_device(dev);
1237 return sprintf(buf, "%u\n",
1238 PWM_FROM_REG(data->pwm[attr->index],
1239 FAN_CONFIG_INVERT(data->fan_conf,
1243 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
1244 const char *buf, size_t count)
1246 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1247 struct pc87360_data *data = dev_get_drvdata(dev);
1251 err = kstrtol(buf, 10, &val);
1255 mutex_lock(&data->update_lock);
1256 data->pwm[attr->index] = PWM_TO_REG(val,
1257 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
1258 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
1259 data->pwm[attr->index]);
1260 mutex_unlock(&data->update_lock);
1264 static struct sensor_device_attribute pwm[] = {
1265 SENSOR_ATTR_RW(pwm1, pwm, 0),
1266 SENSOR_ATTR_RW(pwm2, pwm, 1),
1267 SENSOR_ATTR_RW(pwm3, pwm, 2),
1270 static ssize_t name_show(struct device *dev,
1271 struct device_attribute *devattr, char *buf)
1273 struct pc87360_data *data = dev_get_drvdata(dev);
1274 return sprintf(buf, "%s\n", data->name);
1277 static DEVICE_ATTR_RO(name);
1279 static void pc87360_remove_files(struct device *dev)
1283 device_remove_file(dev, &dev_attr_name);
1284 device_remove_file(dev, &dev_attr_alarms_temp);
1285 for (i = 0; i < ARRAY_SIZE(pc8736x_temp_attr_group); i++)
1286 sysfs_remove_group(&dev->kobj, &pc8736x_temp_attr_group[i]);
1287 for (i = 0; i < ARRAY_SIZE(pc8736x_fan_attr_group); i++) {
1288 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_attr_group[i]);
1289 device_remove_file(dev, &pwm[i].dev_attr);
1291 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1292 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1295 static void pc87360_init_device(struct platform_device *pdev,
1296 int use_thermistors)
1298 struct pc87360_data *data = platform_get_drvdata(pdev);
1300 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1301 const u8 init_temp[3] = { 2, 2, 1 };
1304 if (init >= 2 && data->innr) {
1305 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1306 PC87365_REG_IN_CONVRATE);
1307 dev_info(&pdev->dev,
1308 "VLM conversion set to 1s period, 160us delay\n");
1309 pc87360_write_value(data, LD_IN, NO_BANK,
1310 PC87365_REG_IN_CONVRATE,
1311 (reg & 0xC0) | 0x11);
1314 nr = data->innr < 11 ? data->innr : 11;
1315 for (i = 0; i < nr; i++) {
1316 reg = pc87360_read_value(data, LD_IN, i,
1317 PC87365_REG_IN_STATUS);
1318 dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
1319 if (init >= init_in[i]) {
1320 /* Forcibly enable voltage channel */
1321 if (!(reg & CHAN_ENA)) {
1322 dev_dbg(&pdev->dev, "Forcibly enabling in%d\n",
1324 pc87360_write_value(data, LD_IN, i,
1325 PC87365_REG_IN_STATUS,
1326 (reg & 0x68) | 0x87);
1332 * We can't blindly trust the Super-I/O space configuration bit,
1333 * most BIOS won't set it properly
1335 dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
1336 for (i = 11; i < data->innr; i++) {
1337 reg = pc87360_read_value(data, LD_IN, i,
1338 PC87365_REG_TEMP_STATUS);
1339 use_thermistors = use_thermistors || (reg & CHAN_ENA);
1340 /* thermistors are temp[4-6], measured on vin[11-14] */
1341 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
1343 dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
1345 i = use_thermistors ? 2 : 0;
1346 for (; i < data->tempnr; i++) {
1347 reg = pc87360_read_value(data, LD_TEMP, i,
1348 PC87365_REG_TEMP_STATUS);
1349 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i + 1, reg);
1350 if (init >= init_temp[i]) {
1351 /* Forcibly enable temperature channel */
1352 if (!(reg & CHAN_ENA)) {
1354 "Forcibly enabling temp%d\n", i + 1);
1355 pc87360_write_value(data, LD_TEMP, i,
1356 PC87365_REG_TEMP_STATUS,
1362 if (use_thermistors) {
1363 for (i = 11; i < data->innr; i++) {
1364 if (init >= init_in[i]) {
1366 * The pin may already be used by thermal
1369 reg = pc87360_read_value(data, LD_TEMP,
1370 (i - 11) / 2, PC87365_REG_TEMP_STATUS);
1371 if (reg & CHAN_ENA) {
1373 "Skipping temp%d, pin already in use by temp%d\n",
1374 i - 7, (i - 11) / 2);
1378 /* Forcibly enable thermistor channel */
1379 reg = pc87360_read_value(data, LD_IN, i,
1380 PC87365_REG_IN_STATUS);
1381 if (!(reg & CHAN_ENA)) {
1383 "Forcibly enabling temp%d\n",
1385 pc87360_write_value(data, LD_IN, i,
1386 PC87365_REG_TEMP_STATUS,
1387 (reg & 0x60) | 0x8F);
1394 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1395 PC87365_REG_IN_CONFIG);
1396 dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
1397 if (reg & CHAN_ENA) {
1399 "Forcibly enabling monitoring (VLM)\n");
1400 pc87360_write_value(data, LD_IN, NO_BANK,
1401 PC87365_REG_IN_CONFIG,
1407 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1408 PC87365_REG_TEMP_CONFIG);
1409 dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
1410 if (reg & CHAN_ENA) {
1412 "Forcibly enabling monitoring (TMS)\n");
1413 pc87360_write_value(data, LD_TEMP, NO_BANK,
1414 PC87365_REG_TEMP_CONFIG,
1419 /* Chip config as documented by National Semi. */
1420 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1422 * We voluntarily omit the bank here, in case the
1423 * sequence itself matters. It shouldn't be a problem,
1424 * since nobody else is supposed to access the
1425 * device at that point.
1427 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1428 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1429 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1430 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1435 static int pc87360_probe(struct platform_device *pdev)
1438 struct pc87360_data *data;
1441 int use_thermistors = 0;
1442 struct device *dev = &pdev->dev;
1444 data = devm_kzalloc(dev, sizeof(struct pc87360_data), GFP_KERNEL);
1463 data->fannr = extra_isa[0] ? 3 : 0;
1464 data->innr = extra_isa[1] ? 11 : 0;
1465 data->tempnr = extra_isa[2] ? 2 : 0;
1469 data->fannr = extra_isa[0] ? 3 : 0;
1470 data->innr = extra_isa[1] ? 14 : 0;
1471 data->tempnr = extra_isa[2] ? 3 : 0;
1476 mutex_init(&data->lock);
1477 mutex_init(&data->update_lock);
1478 platform_set_drvdata(pdev, data);
1480 for (i = 0; i < LDNI_MAX; i++) {
1481 data->address[i] = extra_isa[i];
1482 if (data->address[i]
1483 && !devm_request_region(dev, extra_isa[i], PC87360_EXTENT,
1486 "Region 0x%x-0x%x already in use!\n",
1487 extra_isa[i], extra_isa[i]+PC87360_EXTENT-1);
1492 /* Retrieve the fans configuration from Super-I/O space */
1494 data->fan_conf = confreg[0] | (confreg[1] << 8);
1497 * Use the correct reference voltage
1498 * Unless both the VLM and the TMS logical devices agree to
1499 * use an external Vref, the internal one is used.
1502 i = pc87360_read_value(data, LD_IN, NO_BANK,
1503 PC87365_REG_IN_CONFIG);
1505 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1506 PC87365_REG_TEMP_CONFIG);
1508 data->in_vref = (i&0x02) ? 3025 : 2966;
1509 dev_dbg(dev, "Using %s reference voltage\n",
1510 (i&0x02) ? "external" : "internal");
1512 data->vid_conf = confreg[3];
1513 data->vrm = vid_which_vrm();
1516 /* Fan clock dividers may be needed before any data is read */
1517 for (i = 0; i < data->fannr; i++) {
1518 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1519 data->fan_status[i] = pc87360_read_value(data,
1521 PC87360_REG_FAN_STATUS(i));
1525 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1526 use_thermistors = confreg[2] & 0x40;
1528 pc87360_init_device(pdev, use_thermistors);
1531 /* Register all-or-nothing sysfs groups */
1534 err = sysfs_create_group(&dev->kobj, &pc8736x_vin_group);
1539 if (data->innr == 14) {
1540 err = sysfs_create_group(&dev->kobj, &pc8736x_therm_group);
1545 /* create device attr-files for varying sysfs groups */
1548 for (i = 0; i < data->tempnr; i++) {
1549 err = sysfs_create_group(&dev->kobj,
1550 &pc8736x_temp_attr_group[i]);
1554 err = device_create_file(dev, &dev_attr_alarms_temp);
1559 for (i = 0; i < data->fannr; i++) {
1560 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1561 err = sysfs_create_group(&dev->kobj,
1562 &pc8736x_fan_attr_group[i]);
1566 if (FAN_CONFIG_CONTROL(data->fan_conf, i)) {
1567 err = device_create_file(dev, &pwm[i].dev_attr);
1573 err = device_create_file(dev, &dev_attr_name);
1577 data->hwmon_dev = hwmon_device_register(dev);
1578 if (IS_ERR(data->hwmon_dev)) {
1579 err = PTR_ERR(data->hwmon_dev);
1585 pc87360_remove_files(dev);
1589 static int pc87360_remove(struct platform_device *pdev)
1591 struct pc87360_data *data = platform_get_drvdata(pdev);
1593 hwmon_device_unregister(data->hwmon_dev);
1594 pc87360_remove_files(&pdev->dev);
1602 static struct platform_driver pc87360_driver = {
1604 .name = DRIVER_NAME,
1606 .probe = pc87360_probe,
1607 .remove = pc87360_remove,
1611 * Device detection, registration and update
1614 static int __init pc87360_find(int sioaddr, u8 *devid,
1615 unsigned short *addresses)
1619 int nrdev; /* logical device count */
1621 /* No superio_enter */
1623 /* Identify device */
1624 val = force_id ? force_id : superio_inb(sioaddr, DEVID);
1626 case 0xE1: /* PC87360 */
1627 case 0xE8: /* PC87363 */
1628 case 0xE4: /* PC87364 */
1631 case 0xE5: /* PC87365 */
1632 case 0xE9: /* PC87366 */
1636 superio_exit(sioaddr);
1639 /* Remember the device id */
1642 for (i = 0; i < nrdev; i++) {
1643 /* select logical device */
1644 superio_outb(sioaddr, DEV, logdev[i]);
1646 val = superio_inb(sioaddr, ACT);
1647 if (!(val & 0x01)) {
1648 pr_info("Device 0x%02x not activated\n", logdev[i]);
1652 val = (superio_inb(sioaddr, BASE) << 8)
1653 | superio_inb(sioaddr, BASE + 1);
1655 pr_info("Base address not set for device 0x%02x\n",
1662 if (i == 0) { /* Fans */
1663 confreg[0] = superio_inb(sioaddr, 0xF0);
1664 confreg[1] = superio_inb(sioaddr, 0xF1);
1666 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 1,
1667 (confreg[0] >> 2) & 1, (confreg[0] >> 3) & 1,
1668 (confreg[0] >> 4) & 1);
1669 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 2,
1670 (confreg[0] >> 5) & 1, (confreg[0] >> 6) & 1,
1671 (confreg[0] >> 7) & 1);
1672 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 3,
1673 confreg[1] & 1, (confreg[1] >> 1) & 1,
1674 (confreg[1] >> 2) & 1);
1675 } else if (i == 1) { /* Voltages */
1676 /* Are we using thermistors? */
1677 if (*devid == 0xE9) { /* PC87366 */
1679 * These registers are not logical-device
1680 * specific, just that we won't need them if
1681 * we don't use the VLM device
1683 confreg[2] = superio_inb(sioaddr, 0x2B);
1684 confreg[3] = superio_inb(sioaddr, 0x25);
1686 if (confreg[2] & 0x40) {
1687 pr_info("Using thermistors for temperature monitoring\n");
1689 if (confreg[3] & 0xE0) {
1690 pr_info("VID inputs routed (mode %u)\n",
1697 superio_exit(sioaddr);
1701 static int __init pc87360_device_add(unsigned short address)
1703 struct resource res[3];
1704 int err, i, res_count;
1706 pdev = platform_device_alloc("pc87360", address);
1709 pr_err("Device allocation failed\n");
1713 memset(res, 0, 3 * sizeof(struct resource));
1715 for (i = 0; i < 3; i++) {
1718 res[res_count].start = extra_isa[i];
1719 res[res_count].end = extra_isa[i] + PC87360_EXTENT - 1;
1720 res[res_count].name = "pc87360";
1721 res[res_count].flags = IORESOURCE_IO;
1723 err = acpi_check_resource_conflict(&res[res_count]);
1725 goto exit_device_put;
1730 err = platform_device_add_resources(pdev, res, res_count);
1732 pr_err("Device resources addition failed (%d)\n", err);
1733 goto exit_device_put;
1736 err = platform_device_add(pdev);
1738 pr_err("Device addition failed (%d)\n", err);
1739 goto exit_device_put;
1745 platform_device_put(pdev);
1750 static int __init pc87360_init(void)
1753 unsigned short address = 0;
1755 if (pc87360_find(0x2e, &devid, extra_isa)
1756 && pc87360_find(0x4e, &devid, extra_isa)) {
1757 pr_warn("PC8736x not detected, module not inserted\n");
1761 /* Arbitrarily pick one of the addresses */
1762 for (i = 0; i < 3; i++) {
1763 if (extra_isa[i] != 0x0000) {
1764 address = extra_isa[i];
1769 if (address == 0x0000) {
1770 pr_warn("No active logical device, module not inserted\n");
1774 err = platform_driver_register(&pc87360_driver);
1778 /* Sets global pdev as a side effect */
1779 err = pc87360_device_add(address);
1786 platform_driver_unregister(&pc87360_driver);
1791 static void __exit pc87360_exit(void)
1793 platform_device_unregister(pdev);
1794 platform_driver_unregister(&pc87360_driver);
1797 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1798 MODULE_DESCRIPTION("PC8736x hardware monitor");
1799 MODULE_LICENSE("GPL");
1800 MODULE_ALIAS("platform:" DRIVER_NAME);
1802 module_init(pc87360_init);
1803 module_exit(pc87360_exit);