2 * nct6683 - Driver for the hardware monitoring functionality of
3 * Nuvoton NCT6683D eSIO
5 * Copyright (C) 2013 Guenter Roeck <linux@roeck-us.net>
7 * Derived from nct6775 driver
8 * Copyright (C) 2012, 2013 Guenter Roeck <linux@roeck-us.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * Supports the following chips:
22 * Chip #vin #fan #pwm #temp chip ID
23 * nct6683d 21(1) 16 8 32(1) 0xc730
26 * (1) Total number of vin and temp inputs is 32.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/acpi.h>
32 #include <linux/dmi.h>
33 #include <linux/err.h>
34 #include <linux/init.h>
36 #include <linux/jiffies.h>
37 #include <linux/hwmon.h>
38 #include <linux/hwmon-sysfs.h>
39 #include <linux/module.h>
40 #include <linux/mutex.h>
41 #include <linux/platform_device.h>
42 #include <linux/slab.h>
44 enum kinds { nct6683 };
47 module_param(force, bool, 0);
48 MODULE_PARM_DESC(force, "Set to one to enable detection on non-Intel boards");
50 static const char * const nct6683_device_names[] = {
54 static const char * const nct6683_chip_names[] = {
58 #define DRVNAME "nct6683"
61 * Super-I/O constants and functions
64 #define NCT6683_LD_ACPI 0x0a
65 #define NCT6683_LD_HWM 0x0b
66 #define NCT6683_LD_VID 0x0d
68 #define SIO_REG_LDSEL 0x07 /* Logical device select */
69 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
70 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
71 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
73 #define SIO_NCT6681_ID 0xb270 /* for later */
74 #define SIO_NCT6683_ID 0xc730
75 #define SIO_ID_MASK 0xFFF0
78 superio_outb(int ioreg, int reg, int val)
85 superio_inb(int ioreg, int reg)
88 return inb(ioreg + 1);
92 superio_select(int ioreg, int ld)
94 outb(SIO_REG_LDSEL, ioreg);
99 superio_enter(int ioreg)
102 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
104 if (!request_muxed_region(ioreg, 2, DRVNAME))
114 superio_exit(int ioreg)
118 outb(0x02, ioreg + 1);
119 release_region(ioreg, 2);
126 #define IOREGION_ALIGNMENT (~7)
127 #define IOREGION_OFFSET 4 /* Use EC port 1 */
128 #define IOREGION_LENGTH 4
130 #define EC_PAGE_REG 0
131 #define EC_INDEX_REG 1
132 #define EC_DATA_REG 2
133 #define EC_EVENT_REG 3
135 /* Common and NCT6683 specific data */
137 #define NCT6683_NUM_REG_MON 32
138 #define NCT6683_NUM_REG_FAN 16
139 #define NCT6683_NUM_REG_PWM 8
141 #define NCT6683_REG_MON(x) (0x100 + (x) * 2)
142 #define NCT6683_REG_FAN_RPM(x) (0x140 + (x) * 2)
143 #define NCT6683_REG_PWM(x) (0x160 + (x))
145 #define NCT6683_REG_MON_STS(x) (0x174 + (x))
146 #define NCT6683_REG_IDLE(x) (0x178 + (x))
148 #define NCT6683_REG_FAN_STS(x) (0x17c + (x))
149 #define NCT6683_REG_FAN_ERRSTS 0x17e
150 #define NCT6683_REG_FAN_INITSTS 0x17f
152 #define NCT6683_HWM_CFG 0x180
154 #define NCT6683_REG_MON_CFG(x) (0x1a0 + (x))
155 #define NCT6683_REG_FANIN_CFG(x) (0x1c0 + (x))
156 #define NCT6683_REG_FANOUT_CFG(x) (0x1d0 + (x))
158 #define NCT6683_REG_INTEL_TEMP_MAX(x) (0x901 + (x) * 16)
159 #define NCT6683_REG_INTEL_TEMP_CRIT(x) (0x90d + (x) * 16)
161 #define NCT6683_REG_TEMP_HYST(x) (0x330 + (x)) /* 8 bit */
162 #define NCT6683_REG_TEMP_MAX(x) (0x350 + (x)) /* 8 bit */
163 #define NCT6683_REG_MON_HIGH(x) (0x370 + (x) * 2) /* 8 bit */
164 #define NCT6683_REG_MON_LOW(x) (0x371 + (x) * 2) /* 8 bit */
166 #define NCT6683_REG_FAN_MIN(x) (0x3b8 + (x) * 2) /* 16 bit */
168 #define NCT6683_REG_CUSTOMER_ID 0x602
169 #define NCT6683_CUSTOMER_ID_INTEL 0x805
171 #define NCT6683_REG_BUILD_YEAR 0x604
172 #define NCT6683_REG_BUILD_MONTH 0x605
173 #define NCT6683_REG_BUILD_DAY 0x606
174 #define NCT6683_REG_SERIAL 0x607
175 #define NCT6683_REG_VERSION_HI 0x608
176 #define NCT6683_REG_VERSION_LO 0x609
178 #define NCT6683_REG_CR_CASEOPEN 0xe8
179 #define NCT6683_CR_CASEOPEN_MASK (1 << 7)
181 #define NCT6683_REG_CR_BEEP 0xe0
182 #define NCT6683_CR_BEEP_MASK (1 << 6)
184 static const char *const nct6683_mon_label[] = {
201 "Thermistor 5", /* 0x10 */
210 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
211 "PECI 0.0", /* 0x20 */
223 NULL, NULL, NULL, NULL,
224 "PCH CPU", /* 0x30 */
250 NULL, NULL, NULL, NULL, NULL, NULL,
251 "Virtual 0", /* 0x50 */
259 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
260 "VCC", /* 0x60 voltage sensors */
285 #define NUM_MON_LABELS ARRAY_SIZE(nct6683_mon_label)
286 #define MON_VOLTAGE_START 0x60
288 /* ------------------------------------------------------- */
290 struct nct6683_data {
291 int addr; /* IO base of EC space */
292 int sioreg; /* SIO register */
296 struct device *hwmon_dev;
297 const struct attribute_group *groups[6];
299 int temp_num; /* number of temperature attributes */
300 u8 temp_index[NCT6683_NUM_REG_MON];
301 u8 temp_src[NCT6683_NUM_REG_MON];
303 u8 in_num; /* number of voltage attributes */
304 u8 in_index[NCT6683_NUM_REG_MON];
305 u8 in_src[NCT6683_NUM_REG_MON];
307 struct mutex update_lock; /* used to protect sensor updates */
308 bool valid; /* true if following fields are valid */
309 unsigned long last_updated; /* In jiffies */
311 /* Voltage attribute values */
312 u8 in[3][NCT6683_NUM_REG_MON]; /* [0]=in, [1]=in_max, [2]=in_min */
314 /* Temperature attribute values */
315 s16 temp_in[NCT6683_NUM_REG_MON];
316 s8 temp[4][NCT6683_NUM_REG_MON];/* [0]=min, [1]=max, [2]=hyst,
320 /* Fan attribute values */
321 unsigned int rpm[NCT6683_NUM_REG_FAN];
322 u16 fan_min[NCT6683_NUM_REG_FAN];
323 u8 fanin_cfg[NCT6683_NUM_REG_FAN];
324 u8 fanout_cfg[NCT6683_NUM_REG_FAN];
325 u16 have_fan; /* some fan inputs can be disabled */
328 u8 pwm[NCT6683_NUM_REG_PWM];
331 /* Remember extra register values over suspend/resume */
336 struct nct6683_sio_data {
341 struct sensor_device_template {
342 struct device_attribute dev_attr;
350 bool s2; /* true if both index and nr are used */
353 struct sensor_device_attr_u {
355 struct sensor_device_attribute a1;
356 struct sensor_device_attribute_2 a2;
361 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
362 .attr = {.name = _template, .mode = _mode }, \
367 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
368 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
372 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
374 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
375 .u.s.index = _index, \
379 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
380 static struct sensor_device_template sensor_dev_template_##_name \
381 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
384 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
386 static struct sensor_device_template sensor_dev_template_##_name \
387 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
390 struct sensor_template_group {
391 struct sensor_device_template **templates;
392 umode_t (*is_visible)(struct kobject *, struct attribute *, int);
396 static struct attribute_group *
397 nct6683_create_attr_group(struct device *dev, struct sensor_template_group *tg,
400 struct sensor_device_attribute_2 *a2;
401 struct sensor_device_attribute *a;
402 struct sensor_device_template **t;
403 struct sensor_device_attr_u *su;
404 struct attribute_group *group;
405 struct attribute **attrs;
409 return ERR_PTR(-EINVAL);
412 for (count = 0; *t; t++, count++)
416 return ERR_PTR(-EINVAL);
418 group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
420 return ERR_PTR(-ENOMEM);
422 attrs = devm_kzalloc(dev, sizeof(*attrs) * (repeat * count + 1),
425 return ERR_PTR(-ENOMEM);
427 su = devm_kzalloc(dev, sizeof(*su) * repeat * count,
430 return ERR_PTR(-ENOMEM);
432 group->attrs = attrs;
433 group->is_visible = tg->is_visible;
435 for (i = 0; i < repeat; i++) {
437 for (j = 0; *t != NULL; j++) {
438 snprintf(su->name, sizeof(su->name),
439 (*t)->dev_attr.attr.name, tg->base + i);
442 sysfs_attr_init(&a2->dev_attr.attr);
443 a2->dev_attr.attr.name = su->name;
444 a2->nr = (*t)->u.s.nr + i;
445 a2->index = (*t)->u.s.index;
446 a2->dev_attr.attr.mode =
447 (*t)->dev_attr.attr.mode;
448 a2->dev_attr.show = (*t)->dev_attr.show;
449 a2->dev_attr.store = (*t)->dev_attr.store;
450 *attrs = &a2->dev_attr.attr;
453 sysfs_attr_init(&a->dev_attr.attr);
454 a->dev_attr.attr.name = su->name;
455 a->index = (*t)->u.index + i;
456 a->dev_attr.attr.mode =
457 (*t)->dev_attr.attr.mode;
458 a->dev_attr.show = (*t)->dev_attr.show;
459 a->dev_attr.store = (*t)->dev_attr.store;
460 *attrs = &a->dev_attr.attr;
471 /* LSB is 16 mV, except for the following sources, where it is 32 mV */
472 #define MON_SRC_VCC 0x60
473 #define MON_SRC_VSB 0x61
474 #define MON_SRC_AVSB 0x62
475 #define MON_SRC_VBAT 0x64
477 static inline long in_from_reg(u16 reg, u8 src)
481 if (src == MON_SRC_VCC || src == MON_SRC_VSB || src == MON_SRC_AVSB ||
487 static inline u16 in_to_reg(u32 val, u8 src)
491 if (src == MON_SRC_VCC || src == MON_SRC_VSB || src == MON_SRC_AVSB ||
495 return clamp_val(DIV_ROUND_CLOSEST(val, scale), 0, 127);
498 static u16 nct6683_read(struct nct6683_data *data, u16 reg)
502 outb_p(0xff, data->addr + EC_PAGE_REG); /* unlock */
503 outb_p(reg >> 8, data->addr + EC_PAGE_REG);
504 outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
505 res = inb_p(data->addr + EC_DATA_REG);
509 static u16 nct6683_read16(struct nct6683_data *data, u16 reg)
511 return (nct6683_read(data, reg) << 8) | nct6683_read(data, reg + 1);
514 static void nct6683_write(struct nct6683_data *data, u16 reg, u16 value)
516 outb_p(0xff, data->addr + EC_PAGE_REG); /* unlock */
517 outb_p(reg >> 8, data->addr + EC_PAGE_REG);
518 outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
519 outb_p(value & 0xff, data->addr + EC_DATA_REG);
522 static int get_in_reg(struct nct6683_data *data, int nr, int index)
524 int ch = data->in_index[index];
529 reg = NCT6683_REG_MON(ch);
532 if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
533 reg = NCT6683_REG_MON_LOW(ch);
536 if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
537 reg = NCT6683_REG_MON_HIGH(ch);
545 static int get_temp_reg(struct nct6683_data *data, int nr, int index)
547 int ch = data->temp_index[index];
550 switch (data->customer_id) {
551 case NCT6683_CUSTOMER_ID_INTEL:
555 reg = NCT6683_REG_INTEL_TEMP_MAX(ch);
558 reg = NCT6683_REG_INTEL_TEMP_CRIT(ch);
566 reg = NCT6683_REG_MON_LOW(ch);
569 reg = NCT6683_REG_TEMP_MAX(ch);
572 reg = NCT6683_REG_TEMP_HYST(ch);
575 reg = NCT6683_REG_MON_HIGH(ch);
583 static void nct6683_update_pwm(struct device *dev)
585 struct nct6683_data *data = dev_get_drvdata(dev);
588 for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
589 if (!(data->have_pwm & (1 << i)))
591 data->pwm[i] = nct6683_read(data, NCT6683_REG_PWM(i));
595 static struct nct6683_data *nct6683_update_device(struct device *dev)
597 struct nct6683_data *data = dev_get_drvdata(dev);
600 mutex_lock(&data->update_lock);
602 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
603 /* Measured voltages and limits */
604 for (i = 0; i < data->in_num; i++) {
605 for (j = 0; j < 3; j++) {
606 int reg = get_in_reg(data, j, i);
610 nct6683_read(data, reg);
614 /* Measured temperatures and limits */
615 for (i = 0; i < data->temp_num; i++) {
616 u8 ch = data->temp_index[i];
618 data->temp_in[i] = nct6683_read16(data,
619 NCT6683_REG_MON(ch));
620 for (j = 0; j < 4; j++) {
621 int reg = get_temp_reg(data, j, i);
625 nct6683_read(data, reg);
629 /* Measured fan speeds and limits */
630 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
631 if (!(data->have_fan & (1 << i)))
634 data->rpm[i] = nct6683_read16(data,
635 NCT6683_REG_FAN_RPM(i));
636 data->fan_min[i] = nct6683_read16(data,
637 NCT6683_REG_FAN_MIN(i));
640 nct6683_update_pwm(dev);
642 data->last_updated = jiffies;
646 mutex_unlock(&data->update_lock);
651 * Sysfs callback functions
654 show_in_label(struct device *dev, struct device_attribute *attr, char *buf)
656 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
657 struct nct6683_data *data = nct6683_update_device(dev);
658 int nr = sattr->index;
660 return sprintf(buf, "%s\n", nct6683_mon_label[data->in_src[nr]]);
664 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
666 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
667 struct nct6683_data *data = nct6683_update_device(dev);
668 int index = sattr->index;
671 return sprintf(buf, "%ld\n",
672 in_from_reg(data->in[index][nr], data->in_index[index]));
675 static umode_t nct6683_in_is_visible(struct kobject *kobj,
676 struct attribute *attr, int index)
678 struct device *dev = container_of(kobj, struct device, kobj);
679 struct nct6683_data *data = dev_get_drvdata(dev);
680 int nr = index % 4; /* attribute */
683 * Voltage limits exist for Intel boards,
684 * but register location and encoding is unknown
686 if ((nr == 2 || nr == 3) &&
687 data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
693 SENSOR_TEMPLATE(in_label, "in%d_label", S_IRUGO, show_in_label, NULL, 0);
694 SENSOR_TEMPLATE_2(in_input, "in%d_input", S_IRUGO, show_in_reg, NULL, 0, 0);
695 SENSOR_TEMPLATE_2(in_min, "in%d_min", S_IRUGO, show_in_reg, NULL, 0, 1);
696 SENSOR_TEMPLATE_2(in_max, "in%d_max", S_IRUGO, show_in_reg, NULL, 0, 2);
698 static struct sensor_device_template *nct6683_attributes_in_template[] = {
699 &sensor_dev_template_in_label,
700 &sensor_dev_template_in_input,
701 &sensor_dev_template_in_min,
702 &sensor_dev_template_in_max,
706 static struct sensor_template_group nct6683_in_template_group = {
707 .templates = nct6683_attributes_in_template,
708 .is_visible = nct6683_in_is_visible,
712 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
714 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
715 struct nct6683_data *data = nct6683_update_device(dev);
717 return sprintf(buf, "%d\n", data->rpm[sattr->index]);
721 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
723 struct nct6683_data *data = nct6683_update_device(dev);
724 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
725 int nr = sattr->index;
727 return sprintf(buf, "%d\n", data->fan_min[nr]);
731 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
733 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
734 struct nct6683_data *data = nct6683_update_device(dev);
736 return sprintf(buf, "%d\n",
737 ((data->fanin_cfg[sattr->index] >> 5) & 0x03) + 1);
740 static umode_t nct6683_fan_is_visible(struct kobject *kobj,
741 struct attribute *attr, int index)
743 struct device *dev = container_of(kobj, struct device, kobj);
744 struct nct6683_data *data = dev_get_drvdata(dev);
745 int fan = index / 3; /* fan index */
746 int nr = index % 3; /* attribute index */
748 if (!(data->have_fan & (1 << fan)))
752 * Intel may have minimum fan speed limits,
753 * but register location and encoding are unknown.
755 if (nr == 2 && data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
761 SENSOR_TEMPLATE(fan_input, "fan%d_input", S_IRUGO, show_fan, NULL, 0);
762 SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", S_IRUGO, show_fan_pulses, NULL, 0);
763 SENSOR_TEMPLATE(fan_min, "fan%d_min", S_IRUGO, show_fan_min, NULL, 0);
766 * nct6683_fan_is_visible uses the index into the following array
767 * to determine if attributes should be created or not.
768 * Any change in order or content must be matched.
770 static struct sensor_device_template *nct6683_attributes_fan_template[] = {
771 &sensor_dev_template_fan_input,
772 &sensor_dev_template_fan_pulses,
773 &sensor_dev_template_fan_min,
777 static struct sensor_template_group nct6683_fan_template_group = {
778 .templates = nct6683_attributes_fan_template,
779 .is_visible = nct6683_fan_is_visible,
784 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
786 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
787 struct nct6683_data *data = nct6683_update_device(dev);
788 int nr = sattr->index;
790 return sprintf(buf, "%s\n", nct6683_mon_label[data->temp_src[nr]]);
794 show_temp8(struct device *dev, struct device_attribute *attr, char *buf)
796 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
797 struct nct6683_data *data = nct6683_update_device(dev);
798 int index = sattr->index;
801 return sprintf(buf, "%d\n", data->temp[index][nr] * 1000);
805 show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
807 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
808 struct nct6683_data *data = nct6683_update_device(dev);
809 int nr = sattr->index;
810 int temp = data->temp[1][nr] - data->temp[2][nr];
812 return sprintf(buf, "%d\n", temp * 1000);
816 show_temp16(struct device *dev, struct device_attribute *attr, char *buf)
818 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
819 struct nct6683_data *data = nct6683_update_device(dev);
820 int index = sattr->index;
822 return sprintf(buf, "%d\n", (data->temp_in[index] / 128) * 500);
826 * Temperature sensor type is determined by temperature source
827 * and can not be modified.
828 * 0x02..0x07: Thermal diode
829 * 0x08..0x18: Thermistor
830 * 0x20..0x2b: Intel PECI
831 * 0x42..0x49: AMD TSI
832 * Others are unspecified (not visible)
835 static int get_temp_type(u8 src)
837 if (src >= 0x02 && src <= 0x07)
838 return 3; /* thermal diode */
839 else if (src >= 0x08 && src <= 0x18)
840 return 4; /* thermistor */
841 else if (src >= 0x20 && src <= 0x2b)
843 else if (src >= 0x42 && src <= 0x49)
850 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
852 struct nct6683_data *data = nct6683_update_device(dev);
853 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
854 int nr = sattr->index;
855 return sprintf(buf, "%d\n", get_temp_type(data->temp_src[nr]));
858 static umode_t nct6683_temp_is_visible(struct kobject *kobj,
859 struct attribute *attr, int index)
861 struct device *dev = container_of(kobj, struct device, kobj);
862 struct nct6683_data *data = dev_get_drvdata(dev);
863 int temp = index / 7; /* temp index */
864 int nr = index % 7; /* attribute index */
867 * Intel does not have low temperature limits or temperature hysteresis
868 * registers, or at least register location and encoding is unknown.
870 if ((nr == 2 || nr == 4) &&
871 data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
874 if (nr == 6 && get_temp_type(data->temp_src[temp]) == 0)
880 SENSOR_TEMPLATE(temp_input, "temp%d_input", S_IRUGO, show_temp16, NULL, 0);
881 SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temp_label, NULL, 0);
882 SENSOR_TEMPLATE_2(temp_min, "temp%d_min", S_IRUGO, show_temp8, NULL, 0, 0);
883 SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO, show_temp8, NULL, 0, 1);
884 SENSOR_TEMPLATE(temp_max_hyst, "temp%d_max_hyst", S_IRUGO, show_temp_hyst, NULL,
886 SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", S_IRUGO, show_temp8, NULL, 0, 3);
887 SENSOR_TEMPLATE(temp_type, "temp%d_type", S_IRUGO, show_temp_type, NULL, 0);
890 * nct6683_temp_is_visible uses the index into the following array
891 * to determine if attributes should be created or not.
892 * Any change in order or content must be matched.
894 static struct sensor_device_template *nct6683_attributes_temp_template[] = {
895 &sensor_dev_template_temp_input,
896 &sensor_dev_template_temp_label,
897 &sensor_dev_template_temp_min, /* 2 */
898 &sensor_dev_template_temp_max, /* 3 */
899 &sensor_dev_template_temp_max_hyst, /* 4 */
900 &sensor_dev_template_temp_crit, /* 5 */
901 &sensor_dev_template_temp_type, /* 6 */
905 static struct sensor_template_group nct6683_temp_template_group = {
906 .templates = nct6683_attributes_temp_template,
907 .is_visible = nct6683_temp_is_visible,
912 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
914 struct nct6683_data *data = nct6683_update_device(dev);
915 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
916 int index = sattr->index;
918 return sprintf(buf, "%d\n", data->pwm[index]);
921 SENSOR_TEMPLATE(pwm, "pwm%d", S_IRUGO, show_pwm, NULL, 0);
923 static umode_t nct6683_pwm_is_visible(struct kobject *kobj,
924 struct attribute *attr, int index)
926 struct device *dev = container_of(kobj, struct device, kobj);
927 struct nct6683_data *data = dev_get_drvdata(dev);
928 int pwm = index; /* pwm index */
930 if (!(data->have_pwm & (1 << pwm)))
936 static struct sensor_device_template *nct6683_attributes_pwm_template[] = {
937 &sensor_dev_template_pwm,
941 static struct sensor_template_group nct6683_pwm_template_group = {
942 .templates = nct6683_attributes_pwm_template,
943 .is_visible = nct6683_pwm_is_visible,
948 show_global_beep(struct device *dev, struct device_attribute *attr, char *buf)
950 struct nct6683_data *data = dev_get_drvdata(dev);
954 mutex_lock(&data->update_lock);
956 ret = superio_enter(data->sioreg);
959 superio_select(data->sioreg, NCT6683_LD_HWM);
960 reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
961 superio_exit(data->sioreg);
963 mutex_unlock(&data->update_lock);
965 return sprintf(buf, "%u\n", !!(reg & NCT6683_CR_BEEP_MASK));
968 mutex_unlock(&data->update_lock);
973 store_global_beep(struct device *dev, struct device_attribute *attr,
974 const char *buf, size_t count)
976 struct nct6683_data *data = dev_get_drvdata(dev);
981 if (kstrtoul(buf, 10, &val) || (val != 0 && val != 1))
984 mutex_lock(&data->update_lock);
986 ret = superio_enter(data->sioreg);
992 superio_select(data->sioreg, NCT6683_LD_HWM);
993 reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
995 reg |= NCT6683_CR_BEEP_MASK;
997 reg &= ~NCT6683_CR_BEEP_MASK;
998 superio_outb(data->sioreg, NCT6683_REG_CR_BEEP, reg);
999 superio_exit(data->sioreg);
1001 mutex_unlock(&data->update_lock);
1005 /* Case open detection */
1008 show_caseopen(struct device *dev, struct device_attribute *attr, char *buf)
1010 struct nct6683_data *data = dev_get_drvdata(dev);
1014 mutex_lock(&data->update_lock);
1016 ret = superio_enter(data->sioreg);
1019 superio_select(data->sioreg, NCT6683_LD_ACPI);
1020 reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
1021 superio_exit(data->sioreg);
1023 mutex_unlock(&data->update_lock);
1025 return sprintf(buf, "%u\n", !(reg & NCT6683_CR_CASEOPEN_MASK));
1028 mutex_unlock(&data->update_lock);
1033 clear_caseopen(struct device *dev, struct device_attribute *attr,
1034 const char *buf, size_t count)
1036 struct nct6683_data *data = dev_get_drvdata(dev);
1041 if (kstrtoul(buf, 10, &val) || val != 0)
1044 mutex_lock(&data->update_lock);
1047 * Use CR registers to clear caseopen status.
1048 * Caseopen is activ low, clear by writing 1 into the register.
1051 ret = superio_enter(data->sioreg);
1057 superio_select(data->sioreg, NCT6683_LD_ACPI);
1058 reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
1059 reg |= NCT6683_CR_CASEOPEN_MASK;
1060 superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
1061 reg &= ~NCT6683_CR_CASEOPEN_MASK;
1062 superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
1063 superio_exit(data->sioreg);
1065 data->valid = false; /* Force cache refresh */
1067 mutex_unlock(&data->update_lock);
1071 static DEVICE_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_caseopen,
1073 static DEVICE_ATTR(beep_enable, S_IWUSR | S_IRUGO, show_global_beep,
1076 static struct attribute *nct6683_attributes_other[] = {
1077 &dev_attr_intrusion0_alarm.attr,
1078 &dev_attr_beep_enable.attr,
1082 static const struct attribute_group nct6683_group_other = {
1083 .attrs = nct6683_attributes_other,
1086 /* Get the monitoring functions started */
1087 static inline void nct6683_init_device(struct nct6683_data *data)
1091 /* Start hardware monitoring if needed */
1092 tmp = nct6683_read(data, NCT6683_HWM_CFG);
1094 nct6683_write(data, NCT6683_HWM_CFG, tmp | 0x80);
1098 * There are a total of 24 fan inputs. Each can be configured as input
1099 * or as output. A maximum of 16 inputs and 8 outputs is configurable.
1102 nct6683_setup_fans(struct nct6683_data *data)
1107 for (i = 0; i < NCT6683_NUM_REG_FAN; i++) {
1108 reg = nct6683_read(data, NCT6683_REG_FANIN_CFG(i));
1110 data->have_fan |= 1 << i;
1111 data->fanin_cfg[i] = reg;
1113 for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
1114 reg = nct6683_read(data, NCT6683_REG_FANOUT_CFG(i));
1116 data->have_pwm |= 1 << i;
1117 data->fanout_cfg[i] = reg;
1122 * Translation from monitoring register to temperature and voltage attributes
1123 * ==========================================================================
1125 * There are a total of 32 monitoring registers. Each can be assigned to either
1126 * a temperature or voltage monitoring source.
1127 * NCT6683_REG_MON_CFG(x) defines assignment for each monitoring source.
1129 * Temperature and voltage attribute mapping is determined by walking through
1130 * the NCT6683_REG_MON_CFG registers. If the assigned source is
1131 * a temperature, temp_index[n] is set to the monitor register index, and
1132 * temp_src[n] is set to the temperature source. If the assigned source is
1133 * a voltage, the respective values are stored in in_index[] and in_src[],
1137 static void nct6683_setup_sensors(struct nct6683_data *data)
1144 for (i = 0; i < NCT6683_NUM_REG_MON; i++) {
1145 reg = nct6683_read(data, NCT6683_REG_MON_CFG(i)) & 0x7f;
1146 /* Ignore invalid assignments */
1147 if (reg >= NUM_MON_LABELS)
1149 /* Skip if disabled or reserved */
1150 if (nct6683_mon_label[reg] == NULL)
1152 if (reg < MON_VOLTAGE_START) {
1153 data->temp_index[data->temp_num] = i;
1154 data->temp_src[data->temp_num] = reg;
1157 data->in_index[data->in_num] = i;
1158 data->in_src[data->in_num] = reg;
1164 static int nct6683_probe(struct platform_device *pdev)
1166 struct device *dev = &pdev->dev;
1167 struct nct6683_sio_data *sio_data = dev->platform_data;
1168 struct attribute_group *group;
1169 struct nct6683_data *data;
1170 struct device *hwmon_dev;
1171 struct resource *res;
1174 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1175 if (!devm_request_region(dev, res->start, IOREGION_LENGTH, DRVNAME))
1178 data = devm_kzalloc(dev, sizeof(struct nct6683_data), GFP_KERNEL);
1182 data->kind = sio_data->kind;
1183 data->sioreg = sio_data->sioreg;
1184 data->addr = res->start;
1185 mutex_init(&data->update_lock);
1186 platform_set_drvdata(pdev, data);
1188 data->customer_id = nct6683_read16(data, NCT6683_REG_CUSTOMER_ID);
1190 nct6683_init_device(data);
1191 nct6683_setup_fans(data);
1192 nct6683_setup_sensors(data);
1194 /* Register sysfs hooks */
1196 if (data->have_pwm) {
1197 group = nct6683_create_attr_group(dev,
1198 &nct6683_pwm_template_group,
1199 fls(data->have_pwm));
1201 return PTR_ERR(group);
1202 data->groups[groups++] = group;
1206 group = nct6683_create_attr_group(dev,
1207 &nct6683_in_template_group,
1210 return PTR_ERR(group);
1211 data->groups[groups++] = group;
1214 if (data->have_fan) {
1215 group = nct6683_create_attr_group(dev,
1216 &nct6683_fan_template_group,
1217 fls(data->have_fan));
1219 return PTR_ERR(group);
1220 data->groups[groups++] = group;
1223 if (data->temp_num) {
1224 group = nct6683_create_attr_group(dev,
1225 &nct6683_temp_template_group,
1228 return PTR_ERR(group);
1229 data->groups[groups++] = group;
1231 data->groups[groups++] = &nct6683_group_other;
1233 dev_info(dev, "%s EC firmware version %d.%d build %02x/%02x/%02x\n",
1234 nct6683_chip_names[data->kind],
1235 nct6683_read(data, NCT6683_REG_VERSION_HI),
1236 nct6683_read(data, NCT6683_REG_VERSION_LO),
1237 nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1238 nct6683_read(data, NCT6683_REG_BUILD_DAY),
1239 nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1241 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
1242 nct6683_device_names[data->kind], data, data->groups);
1243 return PTR_ERR_OR_ZERO(hwmon_dev);
1247 static int nct6683_suspend(struct device *dev)
1249 struct nct6683_data *data = nct6683_update_device(dev);
1251 mutex_lock(&data->update_lock);
1252 data->hwm_cfg = nct6683_read(data, NCT6683_HWM_CFG);
1253 mutex_unlock(&data->update_lock);
1258 static int nct6683_resume(struct device *dev)
1260 struct nct6683_data *data = dev_get_drvdata(dev);
1262 mutex_lock(&data->update_lock);
1264 nct6683_write(data, NCT6683_HWM_CFG, data->hwm_cfg);
1266 /* Force re-reading all values */
1267 data->valid = false;
1268 mutex_unlock(&data->update_lock);
1273 static const struct dev_pm_ops nct6683_dev_pm_ops = {
1274 .suspend = nct6683_suspend,
1275 .resume = nct6683_resume,
1276 .freeze = nct6683_suspend,
1277 .restore = nct6683_resume,
1280 #define NCT6683_DEV_PM_OPS (&nct6683_dev_pm_ops)
1282 #define NCT6683_DEV_PM_OPS NULL
1283 #endif /* CONFIG_PM */
1285 static struct platform_driver nct6683_driver = {
1288 .pm = NCT6683_DEV_PM_OPS,
1290 .probe = nct6683_probe,
1293 static int __init nct6683_find(int sioaddr, struct nct6683_sio_data *sio_data)
1295 const char *board_vendor;
1301 * Only run on Intel boards unless the 'force' module parameter is set
1304 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1305 if (!board_vendor || strcmp(board_vendor, "Intel Corporation"))
1309 err = superio_enter(sioaddr);
1313 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
1314 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
1316 switch (val & SIO_ID_MASK) {
1317 case SIO_NCT6683_ID:
1318 sio_data->kind = nct6683;
1322 pr_debug("unsupported chip ID: 0x%04x\n", val);
1326 /* We have a known chip, find the HWM I/O address */
1327 superio_select(sioaddr, NCT6683_LD_HWM);
1328 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
1329 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
1330 addr = val & IOREGION_ALIGNMENT;
1332 pr_err("EC base I/O port unconfigured\n");
1336 /* Activate logical device if needed */
1337 val = superio_inb(sioaddr, SIO_REG_ENABLE);
1338 if (!(val & 0x01)) {
1339 pr_err("EC is disabled\n");
1343 superio_exit(sioaddr);
1344 pr_info("Found %s or compatible chip at %#x:%#x\n",
1345 nct6683_chip_names[sio_data->kind], sioaddr, addr);
1346 sio_data->sioreg = sioaddr;
1351 superio_exit(sioaddr);
1356 * when Super-I/O functions move to a separate file, the Super-I/O
1357 * bus will manage the lifetime of the device and this module will only keep
1358 * track of the nct6683 driver. But since we use platform_device_alloc(), we
1359 * must keep track of the device
1361 static struct platform_device *pdev[2];
1363 static int __init sensors_nct6683_init(void)
1365 struct nct6683_sio_data sio_data;
1366 int sioaddr[2] = { 0x2e, 0x4e };
1367 struct resource res;
1372 err = platform_driver_register(&nct6683_driver);
1377 * initialize sio_data->kind and sio_data->sioreg.
1379 * when Super-I/O functions move to a separate file, the Super-I/O
1380 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1381 * nct6683 hardware monitor, and call probe()
1383 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1384 address = nct6683_find(sioaddr[i], &sio_data);
1390 pdev[i] = platform_device_alloc(DRVNAME, address);
1393 goto exit_device_unregister;
1396 err = platform_device_add_data(pdev[i], &sio_data,
1397 sizeof(struct nct6683_sio_data));
1399 goto exit_device_put;
1401 memset(&res, 0, sizeof(res));
1403 res.start = address + IOREGION_OFFSET;
1404 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
1405 res.flags = IORESOURCE_IO;
1407 err = acpi_check_resource_conflict(&res);
1409 platform_device_put(pdev[i]);
1414 err = platform_device_add_resources(pdev[i], &res, 1);
1416 goto exit_device_put;
1418 /* platform_device_add calls probe() */
1419 err = platform_device_add(pdev[i]);
1421 goto exit_device_put;
1425 goto exit_unregister;
1431 platform_device_put(pdev[i]);
1432 exit_device_unregister:
1435 platform_device_unregister(pdev[i]);
1438 platform_driver_unregister(&nct6683_driver);
1442 static void __exit sensors_nct6683_exit(void)
1446 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1448 platform_device_unregister(pdev[i]);
1450 platform_driver_unregister(&nct6683_driver);
1453 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1454 MODULE_DESCRIPTION("NCT6683D driver");
1455 MODULE_LICENSE("GPL");
1457 module_init(sensors_nct6683_init);
1458 module_exit(sensors_nct6683_exit);