1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * nct6683 - Driver for the hardware monitoring functionality of
4 * Nuvoton NCT6683D/NCT6686D/NCT6687D eSIO
6 * Copyright (C) 2013 Guenter Roeck <linux@roeck-us.net>
8 * Derived from nct6775 driver
9 * Copyright (C) 2012, 2013 Guenter Roeck <linux@roeck-us.net>
11 * Supports the following chips:
13 * Chip #vin #fan #pwm #temp chip ID
14 * nct6683d 21(1) 16 8 32(1) 0xc730
15 * nct6686d 21(1) 16 8 32(1) 0xd440
16 * nct6687d 21(1) 16 8 32(1) 0xd590
19 * (1) Total number of vin and temp inputs is 32.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/acpi.h>
25 #include <linux/delay.h>
26 #include <linux/err.h>
27 #include <linux/init.h>
29 #include <linux/jiffies.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/module.h>
33 #include <linux/mutex.h>
34 #include <linux/platform_device.h>
35 #include <linux/slab.h>
37 enum kinds { nct6683, nct6686, nct6687 };
40 module_param(force, bool, 0);
41 MODULE_PARM_DESC(force, "Set to one to enable support for unknown vendors");
43 static const char * const nct6683_device_names[] = {
49 static const char * const nct6683_chip_names[] = {
55 #define DRVNAME "nct6683"
58 * Super-I/O constants and functions
61 #define NCT6683_LD_ACPI 0x0a
62 #define NCT6683_LD_HWM 0x0b
63 #define NCT6683_LD_VID 0x0d
65 #define SIO_REG_LDSEL 0x07 /* Logical device select */
66 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
67 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
68 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
70 #define SIO_NCT6681_ID 0xb270 /* for later */
71 #define SIO_NCT6683_ID 0xc730
72 #define SIO_NCT6686_ID 0xd440
73 #define SIO_NCT6687_ID 0xd590
74 #define SIO_ID_MASK 0xFFF0
77 superio_outb(int ioreg, int reg, int val)
84 superio_inb(int ioreg, int reg)
87 return inb(ioreg + 1);
91 superio_select(int ioreg, int ld)
93 outb(SIO_REG_LDSEL, ioreg);
98 superio_enter(int ioreg)
101 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
103 if (!request_muxed_region(ioreg, 2, DRVNAME))
113 superio_exit(int ioreg)
117 outb(0x02, ioreg + 1);
118 release_region(ioreg, 2);
125 #define IOREGION_ALIGNMENT (~7)
126 #define IOREGION_OFFSET 4 /* Use EC port 1 */
127 #define IOREGION_LENGTH 4
129 #define EC_PAGE_REG 0
130 #define EC_INDEX_REG 1
131 #define EC_DATA_REG 2
132 #define EC_EVENT_REG 3
134 /* Common and NCT6683 specific data */
136 #define NCT6683_NUM_REG_MON 32
137 #define NCT6683_NUM_REG_FAN 16
138 #define NCT6683_NUM_REG_PWM 8
140 #define NCT6683_REG_MON(x) (0x100 + (x) * 2)
141 #define NCT6683_REG_FAN_RPM(x) (0x140 + (x) * 2)
142 #define NCT6683_REG_PWM(x) (0x160 + (x))
143 #define NCT6683_REG_PWM_WRITE(x) (0xa28 + (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_FAN_CFG_CTRL 0xa01
169 #define NCT6683_FAN_CFG_REQ 0x80
170 #define NCT6683_FAN_CFG_DONE 0x40
172 #define NCT6683_REG_CUSTOMER_ID 0x602
173 #define NCT6683_CUSTOMER_ID_INTEL 0x805
174 #define NCT6683_CUSTOMER_ID_MITAC 0xa0e
175 #define NCT6683_CUSTOMER_ID_MSI 0x201
176 #define NCT6683_CUSTOMER_ID_MSI2 0x200
177 #define NCT6683_CUSTOMER_ID_MSI3 0x207
178 #define NCT6683_CUSTOMER_ID_ASROCK 0xe2c
179 #define NCT6683_CUSTOMER_ID_ASROCK2 0xe1b
180 #define NCT6683_CUSTOMER_ID_ASROCK3 0x1631
182 #define NCT6683_REG_BUILD_YEAR 0x604
183 #define NCT6683_REG_BUILD_MONTH 0x605
184 #define NCT6683_REG_BUILD_DAY 0x606
185 #define NCT6683_REG_SERIAL 0x607
186 #define NCT6683_REG_VERSION_HI 0x608
187 #define NCT6683_REG_VERSION_LO 0x609
189 #define NCT6683_REG_CR_CASEOPEN 0xe8
190 #define NCT6683_CR_CASEOPEN_MASK (1 << 7)
192 #define NCT6683_REG_CR_BEEP 0xe0
193 #define NCT6683_CR_BEEP_MASK (1 << 6)
195 static const char *const nct6683_mon_label[] = {
212 "Thermistor 5", /* 0x10 */
221 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
222 "PECI 0.0", /* 0x20 */
234 NULL, NULL, NULL, NULL,
235 "PCH CPU", /* 0x30 */
261 NULL, NULL, NULL, NULL, NULL, NULL,
262 "Virtual 0", /* 0x50 */
270 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
271 "VCC", /* 0x60 voltage sensors */
296 #define NUM_MON_LABELS ARRAY_SIZE(nct6683_mon_label)
297 #define MON_VOLTAGE_START 0x60
299 /* ------------------------------------------------------- */
301 struct nct6683_data {
302 int addr; /* IO base of EC space */
303 int sioreg; /* SIO register */
307 struct device *hwmon_dev;
308 const struct attribute_group *groups[6];
310 int temp_num; /* number of temperature attributes */
311 u8 temp_index[NCT6683_NUM_REG_MON];
312 u8 temp_src[NCT6683_NUM_REG_MON];
314 u8 in_num; /* number of voltage attributes */
315 u8 in_index[NCT6683_NUM_REG_MON];
316 u8 in_src[NCT6683_NUM_REG_MON];
318 struct mutex update_lock; /* used to protect sensor updates */
319 bool valid; /* true if following fields are valid */
320 unsigned long last_updated; /* In jiffies */
322 /* Voltage attribute values */
323 u8 in[3][NCT6683_NUM_REG_MON]; /* [0]=in, [1]=in_max, [2]=in_min */
325 /* Temperature attribute values */
326 s16 temp_in[NCT6683_NUM_REG_MON];
327 s8 temp[4][NCT6683_NUM_REG_MON];/* [0]=min, [1]=max, [2]=hyst,
331 /* Fan attribute values */
332 unsigned int rpm[NCT6683_NUM_REG_FAN];
333 u16 fan_min[NCT6683_NUM_REG_FAN];
334 u8 fanin_cfg[NCT6683_NUM_REG_FAN];
335 u8 fanout_cfg[NCT6683_NUM_REG_FAN];
336 u16 have_fan; /* some fan inputs can be disabled */
339 u8 pwm[NCT6683_NUM_REG_PWM];
342 /* Remember extra register values over suspend/resume */
347 struct nct6683_sio_data {
352 struct sensor_device_template {
353 struct device_attribute dev_attr;
361 bool s2; /* true if both index and nr are used */
364 struct sensor_device_attr_u {
366 struct sensor_device_attribute a1;
367 struct sensor_device_attribute_2 a2;
372 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
373 .attr = {.name = _template, .mode = _mode }, \
378 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
379 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
383 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
385 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
386 .u.s.index = _index, \
390 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
391 static struct sensor_device_template sensor_dev_template_##_name \
392 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
395 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
397 static struct sensor_device_template sensor_dev_template_##_name \
398 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
401 struct sensor_template_group {
402 struct sensor_device_template **templates;
403 umode_t (*is_visible)(struct kobject *, struct attribute *, int);
407 static struct attribute_group *
408 nct6683_create_attr_group(struct device *dev,
409 const struct sensor_template_group *tg,
412 struct sensor_device_attribute_2 *a2;
413 struct sensor_device_attribute *a;
414 struct sensor_device_template **t;
415 struct sensor_device_attr_u *su;
416 struct attribute_group *group;
417 struct attribute **attrs;
421 return ERR_PTR(-EINVAL);
424 for (count = 0; *t; t++, count++)
428 return ERR_PTR(-EINVAL);
430 group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
432 return ERR_PTR(-ENOMEM);
434 attrs = devm_kcalloc(dev, repeat * count + 1, sizeof(*attrs),
437 return ERR_PTR(-ENOMEM);
439 su = devm_kzalloc(dev, array3_size(repeat, count, sizeof(*su)),
442 return ERR_PTR(-ENOMEM);
444 group->attrs = attrs;
445 group->is_visible = tg->is_visible;
447 for (i = 0; i < repeat; i++) {
450 snprintf(su->name, sizeof(su->name),
451 (*t)->dev_attr.attr.name, tg->base + i);
454 sysfs_attr_init(&a2->dev_attr.attr);
455 a2->dev_attr.attr.name = su->name;
456 a2->nr = (*t)->u.s.nr + i;
457 a2->index = (*t)->u.s.index;
458 a2->dev_attr.attr.mode =
459 (*t)->dev_attr.attr.mode;
460 a2->dev_attr.show = (*t)->dev_attr.show;
461 a2->dev_attr.store = (*t)->dev_attr.store;
462 *attrs = &a2->dev_attr.attr;
465 sysfs_attr_init(&a->dev_attr.attr);
466 a->dev_attr.attr.name = su->name;
467 a->index = (*t)->u.index + i;
468 a->dev_attr.attr.mode =
469 (*t)->dev_attr.attr.mode;
470 a->dev_attr.show = (*t)->dev_attr.show;
471 a->dev_attr.store = (*t)->dev_attr.store;
472 *attrs = &a->dev_attr.attr;
483 /* LSB is 16 mV, except for the following sources, where it is 32 mV */
484 #define MON_SRC_VCC 0x60
485 #define MON_SRC_VSB 0x61
486 #define MON_SRC_AVSB 0x62
487 #define MON_SRC_VBAT 0x64
489 static inline long in_from_reg(u16 reg, u8 src)
493 if (src == MON_SRC_VCC || src == MON_SRC_VSB || src == MON_SRC_AVSB ||
499 static u16 nct6683_read(struct nct6683_data *data, u16 reg)
503 outb_p(0xff, data->addr + EC_PAGE_REG); /* unlock */
504 outb_p(reg >> 8, data->addr + EC_PAGE_REG);
505 outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
506 res = inb_p(data->addr + EC_DATA_REG);
510 static u16 nct6683_read16(struct nct6683_data *data, u16 reg)
512 return (nct6683_read(data, reg) << 8) | nct6683_read(data, reg + 1);
515 static void nct6683_write(struct nct6683_data *data, u16 reg, u16 value)
517 outb_p(0xff, data->addr + EC_PAGE_REG); /* unlock */
518 outb_p(reg >> 8, data->addr + EC_PAGE_REG);
519 outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
520 outb_p(value & 0xff, data->addr + EC_DATA_REG);
523 static int get_in_reg(struct nct6683_data *data, int nr, int index)
525 int ch = data->in_index[index];
530 reg = NCT6683_REG_MON(ch);
533 if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
534 reg = NCT6683_REG_MON_LOW(ch);
537 if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
538 reg = NCT6683_REG_MON_HIGH(ch);
546 static int get_temp_reg(struct nct6683_data *data, int nr, int index)
548 int ch = data->temp_index[index];
551 switch (data->customer_id) {
552 case NCT6683_CUSTOMER_ID_INTEL:
556 reg = NCT6683_REG_INTEL_TEMP_MAX(ch);
559 reg = NCT6683_REG_INTEL_TEMP_CRIT(ch);
563 case NCT6683_CUSTOMER_ID_MITAC:
568 reg = NCT6683_REG_MON_LOW(ch);
571 reg = NCT6683_REG_TEMP_MAX(ch);
574 reg = NCT6683_REG_TEMP_HYST(ch);
577 reg = NCT6683_REG_MON_HIGH(ch);
585 static void nct6683_update_pwm(struct device *dev)
587 struct nct6683_data *data = dev_get_drvdata(dev);
590 for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
591 if (!(data->have_pwm & (1 << i)))
593 data->pwm[i] = nct6683_read(data, NCT6683_REG_PWM(i));
597 static struct nct6683_data *nct6683_update_device(struct device *dev)
599 struct nct6683_data *data = dev_get_drvdata(dev);
602 mutex_lock(&data->update_lock);
604 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
605 /* Measured voltages and limits */
606 for (i = 0; i < data->in_num; i++) {
607 for (j = 0; j < 3; j++) {
608 int reg = get_in_reg(data, j, i);
612 nct6683_read(data, reg);
616 /* Measured temperatures and limits */
617 for (i = 0; i < data->temp_num; i++) {
618 u8 ch = data->temp_index[i];
620 data->temp_in[i] = nct6683_read16(data,
621 NCT6683_REG_MON(ch));
622 for (j = 0; j < 4; j++) {
623 int reg = get_temp_reg(data, j, i);
627 nct6683_read(data, reg);
631 /* Measured fan speeds and limits */
632 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
633 if (!(data->have_fan & (1 << i)))
636 data->rpm[i] = nct6683_read16(data,
637 NCT6683_REG_FAN_RPM(i));
638 data->fan_min[i] = nct6683_read16(data,
639 NCT6683_REG_FAN_MIN(i));
642 nct6683_update_pwm(dev);
644 data->last_updated = jiffies;
648 mutex_unlock(&data->update_lock);
653 * Sysfs callback functions
656 show_in_label(struct device *dev, struct device_attribute *attr, char *buf)
658 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
659 struct nct6683_data *data = nct6683_update_device(dev);
660 int nr = sattr->index;
662 return sprintf(buf, "%s\n", nct6683_mon_label[data->in_src[nr]]);
666 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
668 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
669 struct nct6683_data *data = nct6683_update_device(dev);
670 int index = sattr->index;
673 return sprintf(buf, "%ld\n",
674 in_from_reg(data->in[index][nr], data->in_index[index]));
677 static umode_t nct6683_in_is_visible(struct kobject *kobj,
678 struct attribute *attr, int index)
680 struct device *dev = kobj_to_dev(kobj);
681 struct nct6683_data *data = dev_get_drvdata(dev);
682 int nr = index % 4; /* attribute */
685 * Voltage limits exist for Intel boards,
686 * but register location and encoding is unknown
688 if ((nr == 2 || nr == 3) &&
689 data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
695 SENSOR_TEMPLATE(in_label, "in%d_label", S_IRUGO, show_in_label, NULL, 0);
696 SENSOR_TEMPLATE_2(in_input, "in%d_input", S_IRUGO, show_in_reg, NULL, 0, 0);
697 SENSOR_TEMPLATE_2(in_min, "in%d_min", S_IRUGO, show_in_reg, NULL, 0, 1);
698 SENSOR_TEMPLATE_2(in_max, "in%d_max", S_IRUGO, show_in_reg, NULL, 0, 2);
700 static struct sensor_device_template *nct6683_attributes_in_template[] = {
701 &sensor_dev_template_in_label,
702 &sensor_dev_template_in_input,
703 &sensor_dev_template_in_min,
704 &sensor_dev_template_in_max,
708 static const struct sensor_template_group nct6683_in_template_group = {
709 .templates = nct6683_attributes_in_template,
710 .is_visible = nct6683_in_is_visible,
714 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
716 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
717 struct nct6683_data *data = nct6683_update_device(dev);
719 return sprintf(buf, "%d\n", data->rpm[sattr->index]);
723 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
725 struct nct6683_data *data = nct6683_update_device(dev);
726 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
727 int nr = sattr->index;
729 return sprintf(buf, "%d\n", data->fan_min[nr]);
733 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
735 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
736 struct nct6683_data *data = nct6683_update_device(dev);
738 return sprintf(buf, "%d\n",
739 ((data->fanin_cfg[sattr->index] >> 5) & 0x03) + 1);
742 static umode_t nct6683_fan_is_visible(struct kobject *kobj,
743 struct attribute *attr, int index)
745 struct device *dev = kobj_to_dev(kobj);
746 struct nct6683_data *data = dev_get_drvdata(dev);
747 int fan = index / 3; /* fan index */
748 int nr = index % 3; /* attribute index */
750 if (!(data->have_fan & (1 << fan)))
754 * Intel may have minimum fan speed limits,
755 * but register location and encoding are unknown.
757 if (nr == 2 && data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
763 SENSOR_TEMPLATE(fan_input, "fan%d_input", S_IRUGO, show_fan, NULL, 0);
764 SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", S_IRUGO, show_fan_pulses, NULL, 0);
765 SENSOR_TEMPLATE(fan_min, "fan%d_min", S_IRUGO, show_fan_min, NULL, 0);
768 * nct6683_fan_is_visible uses the index into the following array
769 * to determine if attributes should be created or not.
770 * Any change in order or content must be matched.
772 static struct sensor_device_template *nct6683_attributes_fan_template[] = {
773 &sensor_dev_template_fan_input,
774 &sensor_dev_template_fan_pulses,
775 &sensor_dev_template_fan_min,
779 static const struct sensor_template_group nct6683_fan_template_group = {
780 .templates = nct6683_attributes_fan_template,
781 .is_visible = nct6683_fan_is_visible,
786 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
788 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
789 struct nct6683_data *data = nct6683_update_device(dev);
790 int nr = sattr->index;
792 return sprintf(buf, "%s\n", nct6683_mon_label[data->temp_src[nr]]);
796 show_temp8(struct device *dev, struct device_attribute *attr, char *buf)
798 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
799 struct nct6683_data *data = nct6683_update_device(dev);
800 int index = sattr->index;
803 return sprintf(buf, "%d\n", data->temp[index][nr] * 1000);
807 show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
809 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
810 struct nct6683_data *data = nct6683_update_device(dev);
811 int nr = sattr->index;
812 int temp = data->temp[1][nr] - data->temp[2][nr];
814 return sprintf(buf, "%d\n", temp * 1000);
818 show_temp16(struct device *dev, struct device_attribute *attr, char *buf)
820 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
821 struct nct6683_data *data = nct6683_update_device(dev);
822 int index = sattr->index;
824 return sprintf(buf, "%d\n", (data->temp_in[index] / 128) * 500);
828 * Temperature sensor type is determined by temperature source
829 * and can not be modified.
830 * 0x02..0x07: Thermal diode
831 * 0x08..0x18: Thermistor
832 * 0x20..0x2b: Intel PECI
833 * 0x42..0x49: AMD TSI
834 * Others are unspecified (not visible)
837 static int get_temp_type(u8 src)
839 if (src >= 0x02 && src <= 0x07)
840 return 3; /* thermal diode */
841 else if (src >= 0x08 && src <= 0x18)
842 return 4; /* thermistor */
843 else if (src >= 0x20 && src <= 0x2b)
845 else if (src >= 0x42 && src <= 0x49)
852 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
854 struct nct6683_data *data = nct6683_update_device(dev);
855 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
856 int nr = sattr->index;
857 return sprintf(buf, "%d\n", get_temp_type(data->temp_src[nr]));
860 static umode_t nct6683_temp_is_visible(struct kobject *kobj,
861 struct attribute *attr, int index)
863 struct device *dev = kobj_to_dev(kobj);
864 struct nct6683_data *data = dev_get_drvdata(dev);
865 int temp = index / 7; /* temp index */
866 int nr = index % 7; /* attribute index */
869 * Intel does not have low temperature limits or temperature hysteresis
870 * registers, or at least register location and encoding is unknown.
872 if ((nr == 2 || nr == 4) &&
873 data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
876 if (nr == 6 && get_temp_type(data->temp_src[temp]) == 0)
882 SENSOR_TEMPLATE(temp_input, "temp%d_input", S_IRUGO, show_temp16, NULL, 0);
883 SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temp_label, NULL, 0);
884 SENSOR_TEMPLATE_2(temp_min, "temp%d_min", S_IRUGO, show_temp8, NULL, 0, 0);
885 SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO, show_temp8, NULL, 0, 1);
886 SENSOR_TEMPLATE(temp_max_hyst, "temp%d_max_hyst", S_IRUGO, show_temp_hyst, NULL,
888 SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", S_IRUGO, show_temp8, NULL, 0, 3);
889 SENSOR_TEMPLATE(temp_type, "temp%d_type", S_IRUGO, show_temp_type, NULL, 0);
892 * nct6683_temp_is_visible uses the index into the following array
893 * to determine if attributes should be created or not.
894 * Any change in order or content must be matched.
896 static struct sensor_device_template *nct6683_attributes_temp_template[] = {
897 &sensor_dev_template_temp_input,
898 &sensor_dev_template_temp_label,
899 &sensor_dev_template_temp_min, /* 2 */
900 &sensor_dev_template_temp_max, /* 3 */
901 &sensor_dev_template_temp_max_hyst, /* 4 */
902 &sensor_dev_template_temp_crit, /* 5 */
903 &sensor_dev_template_temp_type, /* 6 */
907 static const struct sensor_template_group nct6683_temp_template_group = {
908 .templates = nct6683_attributes_temp_template,
909 .is_visible = nct6683_temp_is_visible,
914 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
916 struct nct6683_data *data = nct6683_update_device(dev);
917 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
918 int index = sattr->index;
920 return sprintf(buf, "%d\n", data->pwm[index]);
924 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
927 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
928 struct nct6683_data *data = dev_get_drvdata(dev);
929 int index = sattr->index;
932 if (kstrtoul(buf, 10, &val) || val > 255)
935 mutex_lock(&data->update_lock);
936 nct6683_write(data, NCT6683_REG_FAN_CFG_CTRL, NCT6683_FAN_CFG_REQ);
937 usleep_range(1000, 2000);
938 nct6683_write(data, NCT6683_REG_PWM_WRITE(index), val);
939 nct6683_write(data, NCT6683_REG_FAN_CFG_CTRL, NCT6683_FAN_CFG_DONE);
940 mutex_unlock(&data->update_lock);
945 SENSOR_TEMPLATE(pwm, "pwm%d", S_IRUGO, show_pwm, store_pwm, 0);
947 static umode_t nct6683_pwm_is_visible(struct kobject *kobj,
948 struct attribute *attr, int index)
950 struct device *dev = kobj_to_dev(kobj);
951 struct nct6683_data *data = dev_get_drvdata(dev);
952 int pwm = index; /* pwm index */
954 if (!(data->have_pwm & (1 << pwm)))
957 /* Only update pwm values for Mitac boards */
958 if (data->customer_id == NCT6683_CUSTOMER_ID_MITAC)
959 return attr->mode | S_IWUSR;
964 static struct sensor_device_template *nct6683_attributes_pwm_template[] = {
965 &sensor_dev_template_pwm,
969 static const struct sensor_template_group nct6683_pwm_template_group = {
970 .templates = nct6683_attributes_pwm_template,
971 .is_visible = nct6683_pwm_is_visible,
976 beep_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
978 struct nct6683_data *data = dev_get_drvdata(dev);
982 mutex_lock(&data->update_lock);
984 ret = superio_enter(data->sioreg);
987 superio_select(data->sioreg, NCT6683_LD_HWM);
988 reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
989 superio_exit(data->sioreg);
991 mutex_unlock(&data->update_lock);
993 return sprintf(buf, "%u\n", !!(reg & NCT6683_CR_BEEP_MASK));
996 mutex_unlock(&data->update_lock);
1001 beep_enable_store(struct device *dev, struct device_attribute *attr,
1002 const char *buf, size_t count)
1004 struct nct6683_data *data = dev_get_drvdata(dev);
1009 if (kstrtoul(buf, 10, &val) || (val != 0 && val != 1))
1012 mutex_lock(&data->update_lock);
1014 ret = superio_enter(data->sioreg);
1020 superio_select(data->sioreg, NCT6683_LD_HWM);
1021 reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
1023 reg |= NCT6683_CR_BEEP_MASK;
1025 reg &= ~NCT6683_CR_BEEP_MASK;
1026 superio_outb(data->sioreg, NCT6683_REG_CR_BEEP, reg);
1027 superio_exit(data->sioreg);
1029 mutex_unlock(&data->update_lock);
1033 /* Case open detection */
1036 intrusion0_alarm_show(struct device *dev, struct device_attribute *attr,
1039 struct nct6683_data *data = dev_get_drvdata(dev);
1043 mutex_lock(&data->update_lock);
1045 ret = superio_enter(data->sioreg);
1048 superio_select(data->sioreg, NCT6683_LD_ACPI);
1049 reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
1050 superio_exit(data->sioreg);
1052 mutex_unlock(&data->update_lock);
1054 return sprintf(buf, "%u\n", !(reg & NCT6683_CR_CASEOPEN_MASK));
1057 mutex_unlock(&data->update_lock);
1062 intrusion0_alarm_store(struct device *dev, struct device_attribute *attr,
1063 const char *buf, size_t count)
1065 struct nct6683_data *data = dev_get_drvdata(dev);
1070 if (kstrtoul(buf, 10, &val) || val != 0)
1073 mutex_lock(&data->update_lock);
1076 * Use CR registers to clear caseopen status.
1077 * Caseopen is activ low, clear by writing 1 into the register.
1080 ret = superio_enter(data->sioreg);
1086 superio_select(data->sioreg, NCT6683_LD_ACPI);
1087 reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
1088 reg |= NCT6683_CR_CASEOPEN_MASK;
1089 superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
1090 reg &= ~NCT6683_CR_CASEOPEN_MASK;
1091 superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
1092 superio_exit(data->sioreg);
1094 data->valid = false; /* Force cache refresh */
1096 mutex_unlock(&data->update_lock);
1100 static DEVICE_ATTR_RW(intrusion0_alarm);
1101 static DEVICE_ATTR_RW(beep_enable);
1103 static struct attribute *nct6683_attributes_other[] = {
1104 &dev_attr_intrusion0_alarm.attr,
1105 &dev_attr_beep_enable.attr,
1109 static const struct attribute_group nct6683_group_other = {
1110 .attrs = nct6683_attributes_other,
1113 /* Get the monitoring functions started */
1114 static inline void nct6683_init_device(struct nct6683_data *data)
1118 /* Start hardware monitoring if needed */
1119 tmp = nct6683_read(data, NCT6683_HWM_CFG);
1121 nct6683_write(data, NCT6683_HWM_CFG, tmp | 0x80);
1125 * There are a total of 24 fan inputs. Each can be configured as input
1126 * or as output. A maximum of 16 inputs and 8 outputs is configurable.
1129 nct6683_setup_fans(struct nct6683_data *data)
1134 for (i = 0; i < NCT6683_NUM_REG_FAN; i++) {
1135 reg = nct6683_read(data, NCT6683_REG_FANIN_CFG(i));
1137 data->have_fan |= 1 << i;
1138 data->fanin_cfg[i] = reg;
1140 for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
1141 reg = nct6683_read(data, NCT6683_REG_FANOUT_CFG(i));
1143 data->have_pwm |= 1 << i;
1144 data->fanout_cfg[i] = reg;
1149 * Translation from monitoring register to temperature and voltage attributes
1150 * ==========================================================================
1152 * There are a total of 32 monitoring registers. Each can be assigned to either
1153 * a temperature or voltage monitoring source.
1154 * NCT6683_REG_MON_CFG(x) defines assignment for each monitoring source.
1156 * Temperature and voltage attribute mapping is determined by walking through
1157 * the NCT6683_REG_MON_CFG registers. If the assigned source is
1158 * a temperature, temp_index[n] is set to the monitor register index, and
1159 * temp_src[n] is set to the temperature source. If the assigned source is
1160 * a voltage, the respective values are stored in in_index[] and in_src[],
1164 static void nct6683_setup_sensors(struct nct6683_data *data)
1171 for (i = 0; i < NCT6683_NUM_REG_MON; i++) {
1172 reg = nct6683_read(data, NCT6683_REG_MON_CFG(i)) & 0x7f;
1173 /* Ignore invalid assignments */
1174 if (reg >= NUM_MON_LABELS)
1176 /* Skip if disabled or reserved */
1177 if (nct6683_mon_label[reg] == NULL)
1179 if (reg < MON_VOLTAGE_START) {
1180 data->temp_index[data->temp_num] = i;
1181 data->temp_src[data->temp_num] = reg;
1184 data->in_index[data->in_num] = i;
1185 data->in_src[data->in_num] = reg;
1191 static int nct6683_probe(struct platform_device *pdev)
1193 struct device *dev = &pdev->dev;
1194 struct nct6683_sio_data *sio_data = dev->platform_data;
1195 struct attribute_group *group;
1196 struct nct6683_data *data;
1197 struct device *hwmon_dev;
1198 struct resource *res;
1202 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1203 if (!devm_request_region(dev, res->start, IOREGION_LENGTH, DRVNAME))
1206 data = devm_kzalloc(dev, sizeof(struct nct6683_data), GFP_KERNEL);
1210 data->kind = sio_data->kind;
1211 data->sioreg = sio_data->sioreg;
1212 data->addr = res->start;
1213 mutex_init(&data->update_lock);
1214 platform_set_drvdata(pdev, data);
1216 data->customer_id = nct6683_read16(data, NCT6683_REG_CUSTOMER_ID);
1218 /* By default only instantiate driver if the customer ID is known */
1219 switch (data->customer_id) {
1220 case NCT6683_CUSTOMER_ID_INTEL:
1222 case NCT6683_CUSTOMER_ID_MITAC:
1224 case NCT6683_CUSTOMER_ID_MSI:
1226 case NCT6683_CUSTOMER_ID_MSI2:
1228 case NCT6683_CUSTOMER_ID_MSI3:
1230 case NCT6683_CUSTOMER_ID_ASROCK:
1232 case NCT6683_CUSTOMER_ID_ASROCK2:
1234 case NCT6683_CUSTOMER_ID_ASROCK3:
1239 dev_warn(dev, "Enabling support for unknown customer ID 0x%04x\n", data->customer_id);
1243 nct6683_init_device(data);
1244 nct6683_setup_fans(data);
1245 nct6683_setup_sensors(data);
1247 /* Register sysfs hooks */
1249 if (data->have_pwm) {
1250 group = nct6683_create_attr_group(dev,
1251 &nct6683_pwm_template_group,
1252 fls(data->have_pwm));
1254 return PTR_ERR(group);
1255 data->groups[groups++] = group;
1259 group = nct6683_create_attr_group(dev,
1260 &nct6683_in_template_group,
1263 return PTR_ERR(group);
1264 data->groups[groups++] = group;
1267 if (data->have_fan) {
1268 group = nct6683_create_attr_group(dev,
1269 &nct6683_fan_template_group,
1270 fls(data->have_fan));
1272 return PTR_ERR(group);
1273 data->groups[groups++] = group;
1276 if (data->temp_num) {
1277 group = nct6683_create_attr_group(dev,
1278 &nct6683_temp_template_group,
1281 return PTR_ERR(group);
1282 data->groups[groups++] = group;
1284 data->groups[groups++] = &nct6683_group_other;
1286 if (data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
1287 scnprintf(build, sizeof(build), "%02x/%02x/%02x",
1288 nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1289 nct6683_read(data, NCT6683_REG_BUILD_DAY),
1290 nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1292 scnprintf(build, sizeof(build), "%02d/%02d/%02d",
1293 nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1294 nct6683_read(data, NCT6683_REG_BUILD_DAY),
1295 nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1297 dev_info(dev, "%s EC firmware version %d.%d build %s\n",
1298 nct6683_chip_names[data->kind],
1299 nct6683_read(data, NCT6683_REG_VERSION_HI),
1300 nct6683_read(data, NCT6683_REG_VERSION_LO),
1303 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
1304 nct6683_device_names[data->kind], data, data->groups);
1305 return PTR_ERR_OR_ZERO(hwmon_dev);
1309 static int nct6683_suspend(struct device *dev)
1311 struct nct6683_data *data = nct6683_update_device(dev);
1313 mutex_lock(&data->update_lock);
1314 data->hwm_cfg = nct6683_read(data, NCT6683_HWM_CFG);
1315 mutex_unlock(&data->update_lock);
1320 static int nct6683_resume(struct device *dev)
1322 struct nct6683_data *data = dev_get_drvdata(dev);
1324 mutex_lock(&data->update_lock);
1326 nct6683_write(data, NCT6683_HWM_CFG, data->hwm_cfg);
1328 /* Force re-reading all values */
1329 data->valid = false;
1330 mutex_unlock(&data->update_lock);
1335 static const struct dev_pm_ops nct6683_dev_pm_ops = {
1336 .suspend = nct6683_suspend,
1337 .resume = nct6683_resume,
1338 .freeze = nct6683_suspend,
1339 .restore = nct6683_resume,
1342 #define NCT6683_DEV_PM_OPS (&nct6683_dev_pm_ops)
1344 #define NCT6683_DEV_PM_OPS NULL
1345 #endif /* CONFIG_PM */
1347 static struct platform_driver nct6683_driver = {
1350 .pm = NCT6683_DEV_PM_OPS,
1352 .probe = nct6683_probe,
1355 static int __init nct6683_find(int sioaddr, struct nct6683_sio_data *sio_data)
1361 err = superio_enter(sioaddr);
1365 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
1366 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
1368 switch (val & SIO_ID_MASK) {
1369 case SIO_NCT6683_ID:
1370 sio_data->kind = nct6683;
1372 case SIO_NCT6686_ID:
1373 sio_data->kind = nct6686;
1375 case SIO_NCT6687_ID:
1376 sio_data->kind = nct6687;
1380 pr_debug("unsupported chip ID: 0x%04x\n", val);
1384 /* We have a known chip, find the HWM I/O address */
1385 superio_select(sioaddr, NCT6683_LD_HWM);
1386 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
1387 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
1388 addr = val & IOREGION_ALIGNMENT;
1390 pr_err("EC base I/O port unconfigured\n");
1394 /* Activate logical device if needed */
1395 val = superio_inb(sioaddr, SIO_REG_ENABLE);
1396 if (!(val & 0x01)) {
1397 pr_warn("Forcibly enabling EC access. Data may be unusable.\n");
1398 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
1401 superio_exit(sioaddr);
1402 pr_info("Found %s or compatible chip at %#x:%#x\n",
1403 nct6683_chip_names[sio_data->kind], sioaddr, addr);
1404 sio_data->sioreg = sioaddr;
1409 superio_exit(sioaddr);
1414 * when Super-I/O functions move to a separate file, the Super-I/O
1415 * bus will manage the lifetime of the device and this module will only keep
1416 * track of the nct6683 driver. But since we use platform_device_alloc(), we
1417 * must keep track of the device
1419 static struct platform_device *pdev[2];
1421 static int __init sensors_nct6683_init(void)
1423 struct nct6683_sio_data sio_data;
1424 int sioaddr[2] = { 0x2e, 0x4e };
1425 struct resource res;
1430 err = platform_driver_register(&nct6683_driver);
1435 * initialize sio_data->kind and sio_data->sioreg.
1437 * when Super-I/O functions move to a separate file, the Super-I/O
1438 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1439 * nct6683 hardware monitor, and call probe()
1441 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1442 address = nct6683_find(sioaddr[i], &sio_data);
1448 pdev[i] = platform_device_alloc(DRVNAME, address);
1451 goto exit_device_unregister;
1454 err = platform_device_add_data(pdev[i], &sio_data,
1455 sizeof(struct nct6683_sio_data));
1457 goto exit_device_put;
1459 memset(&res, 0, sizeof(res));
1461 res.start = address + IOREGION_OFFSET;
1462 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
1463 res.flags = IORESOURCE_IO;
1465 err = acpi_check_resource_conflict(&res);
1467 platform_device_put(pdev[i]);
1472 err = platform_device_add_resources(pdev[i], &res, 1);
1474 goto exit_device_put;
1476 /* platform_device_add calls probe() */
1477 err = platform_device_add(pdev[i]);
1479 goto exit_device_put;
1483 goto exit_unregister;
1489 platform_device_put(pdev[i]);
1490 exit_device_unregister:
1493 platform_device_unregister(pdev[i]);
1496 platform_driver_unregister(&nct6683_driver);
1500 static void __exit sensors_nct6683_exit(void)
1504 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1506 platform_device_unregister(pdev[i]);
1508 platform_driver_unregister(&nct6683_driver);
1511 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1512 MODULE_DESCRIPTION("NCT6683D driver");
1513 MODULE_LICENSE("GPL");
1515 module_init(sensors_nct6683_init);
1516 module_exit(sensors_nct6683_exit);