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_ASROCK 0xe2c
177 #define NCT6683_CUSTOMER_ID_ASROCK2 0xe1b
179 #define NCT6683_REG_BUILD_YEAR 0x604
180 #define NCT6683_REG_BUILD_MONTH 0x605
181 #define NCT6683_REG_BUILD_DAY 0x606
182 #define NCT6683_REG_SERIAL 0x607
183 #define NCT6683_REG_VERSION_HI 0x608
184 #define NCT6683_REG_VERSION_LO 0x609
186 #define NCT6683_REG_CR_CASEOPEN 0xe8
187 #define NCT6683_CR_CASEOPEN_MASK (1 << 7)
189 #define NCT6683_REG_CR_BEEP 0xe0
190 #define NCT6683_CR_BEEP_MASK (1 << 6)
192 static const char *const nct6683_mon_label[] = {
209 "Thermistor 5", /* 0x10 */
218 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
219 "PECI 0.0", /* 0x20 */
231 NULL, NULL, NULL, NULL,
232 "PCH CPU", /* 0x30 */
258 NULL, NULL, NULL, NULL, NULL, NULL,
259 "Virtual 0", /* 0x50 */
267 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
268 "VCC", /* 0x60 voltage sensors */
293 #define NUM_MON_LABELS ARRAY_SIZE(nct6683_mon_label)
294 #define MON_VOLTAGE_START 0x60
296 /* ------------------------------------------------------- */
298 struct nct6683_data {
299 int addr; /* IO base of EC space */
300 int sioreg; /* SIO register */
304 struct device *hwmon_dev;
305 const struct attribute_group *groups[6];
307 int temp_num; /* number of temperature attributes */
308 u8 temp_index[NCT6683_NUM_REG_MON];
309 u8 temp_src[NCT6683_NUM_REG_MON];
311 u8 in_num; /* number of voltage attributes */
312 u8 in_index[NCT6683_NUM_REG_MON];
313 u8 in_src[NCT6683_NUM_REG_MON];
315 struct mutex update_lock; /* used to protect sensor updates */
316 bool valid; /* true if following fields are valid */
317 unsigned long last_updated; /* In jiffies */
319 /* Voltage attribute values */
320 u8 in[3][NCT6683_NUM_REG_MON]; /* [0]=in, [1]=in_max, [2]=in_min */
322 /* Temperature attribute values */
323 s16 temp_in[NCT6683_NUM_REG_MON];
324 s8 temp[4][NCT6683_NUM_REG_MON];/* [0]=min, [1]=max, [2]=hyst,
328 /* Fan attribute values */
329 unsigned int rpm[NCT6683_NUM_REG_FAN];
330 u16 fan_min[NCT6683_NUM_REG_FAN];
331 u8 fanin_cfg[NCT6683_NUM_REG_FAN];
332 u8 fanout_cfg[NCT6683_NUM_REG_FAN];
333 u16 have_fan; /* some fan inputs can be disabled */
336 u8 pwm[NCT6683_NUM_REG_PWM];
339 /* Remember extra register values over suspend/resume */
344 struct nct6683_sio_data {
349 struct sensor_device_template {
350 struct device_attribute dev_attr;
358 bool s2; /* true if both index and nr are used */
361 struct sensor_device_attr_u {
363 struct sensor_device_attribute a1;
364 struct sensor_device_attribute_2 a2;
369 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
370 .attr = {.name = _template, .mode = _mode }, \
375 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
376 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
380 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
382 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
383 .u.s.index = _index, \
387 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
388 static struct sensor_device_template sensor_dev_template_##_name \
389 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
392 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
394 static struct sensor_device_template sensor_dev_template_##_name \
395 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
398 struct sensor_template_group {
399 struct sensor_device_template **templates;
400 umode_t (*is_visible)(struct kobject *, struct attribute *, int);
404 static struct attribute_group *
405 nct6683_create_attr_group(struct device *dev,
406 const struct sensor_template_group *tg,
409 struct sensor_device_attribute_2 *a2;
410 struct sensor_device_attribute *a;
411 struct sensor_device_template **t;
412 struct sensor_device_attr_u *su;
413 struct attribute_group *group;
414 struct attribute **attrs;
418 return ERR_PTR(-EINVAL);
421 for (count = 0; *t; t++, count++)
425 return ERR_PTR(-EINVAL);
427 group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
429 return ERR_PTR(-ENOMEM);
431 attrs = devm_kcalloc(dev, repeat * count + 1, sizeof(*attrs),
434 return ERR_PTR(-ENOMEM);
436 su = devm_kzalloc(dev, array3_size(repeat, count, sizeof(*su)),
439 return ERR_PTR(-ENOMEM);
441 group->attrs = attrs;
442 group->is_visible = tg->is_visible;
444 for (i = 0; i < repeat; i++) {
447 snprintf(su->name, sizeof(su->name),
448 (*t)->dev_attr.attr.name, tg->base + i);
451 sysfs_attr_init(&a2->dev_attr.attr);
452 a2->dev_attr.attr.name = su->name;
453 a2->nr = (*t)->u.s.nr + i;
454 a2->index = (*t)->u.s.index;
455 a2->dev_attr.attr.mode =
456 (*t)->dev_attr.attr.mode;
457 a2->dev_attr.show = (*t)->dev_attr.show;
458 a2->dev_attr.store = (*t)->dev_attr.store;
459 *attrs = &a2->dev_attr.attr;
462 sysfs_attr_init(&a->dev_attr.attr);
463 a->dev_attr.attr.name = su->name;
464 a->index = (*t)->u.index + i;
465 a->dev_attr.attr.mode =
466 (*t)->dev_attr.attr.mode;
467 a->dev_attr.show = (*t)->dev_attr.show;
468 a->dev_attr.store = (*t)->dev_attr.store;
469 *attrs = &a->dev_attr.attr;
480 /* LSB is 16 mV, except for the following sources, where it is 32 mV */
481 #define MON_SRC_VCC 0x60
482 #define MON_SRC_VSB 0x61
483 #define MON_SRC_AVSB 0x62
484 #define MON_SRC_VBAT 0x64
486 static inline long in_from_reg(u16 reg, u8 src)
490 if (src == MON_SRC_VCC || src == MON_SRC_VSB || src == MON_SRC_AVSB ||
496 static u16 nct6683_read(struct nct6683_data *data, u16 reg)
500 outb_p(0xff, data->addr + EC_PAGE_REG); /* unlock */
501 outb_p(reg >> 8, data->addr + EC_PAGE_REG);
502 outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
503 res = inb_p(data->addr + EC_DATA_REG);
507 static u16 nct6683_read16(struct nct6683_data *data, u16 reg)
509 return (nct6683_read(data, reg) << 8) | nct6683_read(data, reg + 1);
512 static void nct6683_write(struct nct6683_data *data, u16 reg, u16 value)
514 outb_p(0xff, data->addr + EC_PAGE_REG); /* unlock */
515 outb_p(reg >> 8, data->addr + EC_PAGE_REG);
516 outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
517 outb_p(value & 0xff, data->addr + EC_DATA_REG);
520 static int get_in_reg(struct nct6683_data *data, int nr, int index)
522 int ch = data->in_index[index];
527 reg = NCT6683_REG_MON(ch);
530 if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
531 reg = NCT6683_REG_MON_LOW(ch);
534 if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
535 reg = NCT6683_REG_MON_HIGH(ch);
543 static int get_temp_reg(struct nct6683_data *data, int nr, int index)
545 int ch = data->temp_index[index];
548 switch (data->customer_id) {
549 case NCT6683_CUSTOMER_ID_INTEL:
553 reg = NCT6683_REG_INTEL_TEMP_MAX(ch);
556 reg = NCT6683_REG_INTEL_TEMP_CRIT(ch);
560 case NCT6683_CUSTOMER_ID_MITAC:
565 reg = NCT6683_REG_MON_LOW(ch);
568 reg = NCT6683_REG_TEMP_MAX(ch);
571 reg = NCT6683_REG_TEMP_HYST(ch);
574 reg = NCT6683_REG_MON_HIGH(ch);
582 static void nct6683_update_pwm(struct device *dev)
584 struct nct6683_data *data = dev_get_drvdata(dev);
587 for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
588 if (!(data->have_pwm & (1 << i)))
590 data->pwm[i] = nct6683_read(data, NCT6683_REG_PWM(i));
594 static struct nct6683_data *nct6683_update_device(struct device *dev)
596 struct nct6683_data *data = dev_get_drvdata(dev);
599 mutex_lock(&data->update_lock);
601 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
602 /* Measured voltages and limits */
603 for (i = 0; i < data->in_num; i++) {
604 for (j = 0; j < 3; j++) {
605 int reg = get_in_reg(data, j, i);
609 nct6683_read(data, reg);
613 /* Measured temperatures and limits */
614 for (i = 0; i < data->temp_num; i++) {
615 u8 ch = data->temp_index[i];
617 data->temp_in[i] = nct6683_read16(data,
618 NCT6683_REG_MON(ch));
619 for (j = 0; j < 4; j++) {
620 int reg = get_temp_reg(data, j, i);
624 nct6683_read(data, reg);
628 /* Measured fan speeds and limits */
629 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
630 if (!(data->have_fan & (1 << i)))
633 data->rpm[i] = nct6683_read16(data,
634 NCT6683_REG_FAN_RPM(i));
635 data->fan_min[i] = nct6683_read16(data,
636 NCT6683_REG_FAN_MIN(i));
639 nct6683_update_pwm(dev);
641 data->last_updated = jiffies;
645 mutex_unlock(&data->update_lock);
650 * Sysfs callback functions
653 show_in_label(struct device *dev, struct device_attribute *attr, char *buf)
655 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
656 struct nct6683_data *data = nct6683_update_device(dev);
657 int nr = sattr->index;
659 return sprintf(buf, "%s\n", nct6683_mon_label[data->in_src[nr]]);
663 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
665 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
666 struct nct6683_data *data = nct6683_update_device(dev);
667 int index = sattr->index;
670 return sprintf(buf, "%ld\n",
671 in_from_reg(data->in[index][nr], data->in_index[index]));
674 static umode_t nct6683_in_is_visible(struct kobject *kobj,
675 struct attribute *attr, int index)
677 struct device *dev = kobj_to_dev(kobj);
678 struct nct6683_data *data = dev_get_drvdata(dev);
679 int nr = index % 4; /* attribute */
682 * Voltage limits exist for Intel boards,
683 * but register location and encoding is unknown
685 if ((nr == 2 || nr == 3) &&
686 data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
692 SENSOR_TEMPLATE(in_label, "in%d_label", S_IRUGO, show_in_label, NULL, 0);
693 SENSOR_TEMPLATE_2(in_input, "in%d_input", S_IRUGO, show_in_reg, NULL, 0, 0);
694 SENSOR_TEMPLATE_2(in_min, "in%d_min", S_IRUGO, show_in_reg, NULL, 0, 1);
695 SENSOR_TEMPLATE_2(in_max, "in%d_max", S_IRUGO, show_in_reg, NULL, 0, 2);
697 static struct sensor_device_template *nct6683_attributes_in_template[] = {
698 &sensor_dev_template_in_label,
699 &sensor_dev_template_in_input,
700 &sensor_dev_template_in_min,
701 &sensor_dev_template_in_max,
705 static const struct sensor_template_group nct6683_in_template_group = {
706 .templates = nct6683_attributes_in_template,
707 .is_visible = nct6683_in_is_visible,
711 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
713 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
714 struct nct6683_data *data = nct6683_update_device(dev);
716 return sprintf(buf, "%d\n", data->rpm[sattr->index]);
720 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
722 struct nct6683_data *data = nct6683_update_device(dev);
723 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
724 int nr = sattr->index;
726 return sprintf(buf, "%d\n", data->fan_min[nr]);
730 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
732 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
733 struct nct6683_data *data = nct6683_update_device(dev);
735 return sprintf(buf, "%d\n",
736 ((data->fanin_cfg[sattr->index] >> 5) & 0x03) + 1);
739 static umode_t nct6683_fan_is_visible(struct kobject *kobj,
740 struct attribute *attr, int index)
742 struct device *dev = kobj_to_dev(kobj);
743 struct nct6683_data *data = dev_get_drvdata(dev);
744 int fan = index / 3; /* fan index */
745 int nr = index % 3; /* attribute index */
747 if (!(data->have_fan & (1 << fan)))
751 * Intel may have minimum fan speed limits,
752 * but register location and encoding are unknown.
754 if (nr == 2 && data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
760 SENSOR_TEMPLATE(fan_input, "fan%d_input", S_IRUGO, show_fan, NULL, 0);
761 SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", S_IRUGO, show_fan_pulses, NULL, 0);
762 SENSOR_TEMPLATE(fan_min, "fan%d_min", S_IRUGO, show_fan_min, NULL, 0);
765 * nct6683_fan_is_visible uses the index into the following array
766 * to determine if attributes should be created or not.
767 * Any change in order or content must be matched.
769 static struct sensor_device_template *nct6683_attributes_fan_template[] = {
770 &sensor_dev_template_fan_input,
771 &sensor_dev_template_fan_pulses,
772 &sensor_dev_template_fan_min,
776 static const struct sensor_template_group nct6683_fan_template_group = {
777 .templates = nct6683_attributes_fan_template,
778 .is_visible = nct6683_fan_is_visible,
783 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
785 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
786 struct nct6683_data *data = nct6683_update_device(dev);
787 int nr = sattr->index;
789 return sprintf(buf, "%s\n", nct6683_mon_label[data->temp_src[nr]]);
793 show_temp8(struct device *dev, struct device_attribute *attr, char *buf)
795 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
796 struct nct6683_data *data = nct6683_update_device(dev);
797 int index = sattr->index;
800 return sprintf(buf, "%d\n", data->temp[index][nr] * 1000);
804 show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
806 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
807 struct nct6683_data *data = nct6683_update_device(dev);
808 int nr = sattr->index;
809 int temp = data->temp[1][nr] - data->temp[2][nr];
811 return sprintf(buf, "%d\n", temp * 1000);
815 show_temp16(struct device *dev, struct device_attribute *attr, char *buf)
817 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
818 struct nct6683_data *data = nct6683_update_device(dev);
819 int index = sattr->index;
821 return sprintf(buf, "%d\n", (data->temp_in[index] / 128) * 500);
825 * Temperature sensor type is determined by temperature source
826 * and can not be modified.
827 * 0x02..0x07: Thermal diode
828 * 0x08..0x18: Thermistor
829 * 0x20..0x2b: Intel PECI
830 * 0x42..0x49: AMD TSI
831 * Others are unspecified (not visible)
834 static int get_temp_type(u8 src)
836 if (src >= 0x02 && src <= 0x07)
837 return 3; /* thermal diode */
838 else if (src >= 0x08 && src <= 0x18)
839 return 4; /* thermistor */
840 else if (src >= 0x20 && src <= 0x2b)
842 else if (src >= 0x42 && src <= 0x49)
849 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
851 struct nct6683_data *data = nct6683_update_device(dev);
852 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
853 int nr = sattr->index;
854 return sprintf(buf, "%d\n", get_temp_type(data->temp_src[nr]));
857 static umode_t nct6683_temp_is_visible(struct kobject *kobj,
858 struct attribute *attr, int index)
860 struct device *dev = kobj_to_dev(kobj);
861 struct nct6683_data *data = dev_get_drvdata(dev);
862 int temp = index / 7; /* temp index */
863 int nr = index % 7; /* attribute index */
866 * Intel does not have low temperature limits or temperature hysteresis
867 * registers, or at least register location and encoding is unknown.
869 if ((nr == 2 || nr == 4) &&
870 data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
873 if (nr == 6 && get_temp_type(data->temp_src[temp]) == 0)
879 SENSOR_TEMPLATE(temp_input, "temp%d_input", S_IRUGO, show_temp16, NULL, 0);
880 SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temp_label, NULL, 0);
881 SENSOR_TEMPLATE_2(temp_min, "temp%d_min", S_IRUGO, show_temp8, NULL, 0, 0);
882 SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO, show_temp8, NULL, 0, 1);
883 SENSOR_TEMPLATE(temp_max_hyst, "temp%d_max_hyst", S_IRUGO, show_temp_hyst, NULL,
885 SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", S_IRUGO, show_temp8, NULL, 0, 3);
886 SENSOR_TEMPLATE(temp_type, "temp%d_type", S_IRUGO, show_temp_type, NULL, 0);
889 * nct6683_temp_is_visible uses the index into the following array
890 * to determine if attributes should be created or not.
891 * Any change in order or content must be matched.
893 static struct sensor_device_template *nct6683_attributes_temp_template[] = {
894 &sensor_dev_template_temp_input,
895 &sensor_dev_template_temp_label,
896 &sensor_dev_template_temp_min, /* 2 */
897 &sensor_dev_template_temp_max, /* 3 */
898 &sensor_dev_template_temp_max_hyst, /* 4 */
899 &sensor_dev_template_temp_crit, /* 5 */
900 &sensor_dev_template_temp_type, /* 6 */
904 static const struct sensor_template_group nct6683_temp_template_group = {
905 .templates = nct6683_attributes_temp_template,
906 .is_visible = nct6683_temp_is_visible,
911 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
913 struct nct6683_data *data = nct6683_update_device(dev);
914 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
915 int index = sattr->index;
917 return sprintf(buf, "%d\n", data->pwm[index]);
921 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
924 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
925 struct nct6683_data *data = dev_get_drvdata(dev);
926 int index = sattr->index;
929 if (kstrtoul(buf, 10, &val) || val > 255)
932 mutex_lock(&data->update_lock);
933 nct6683_write(data, NCT6683_REG_FAN_CFG_CTRL, NCT6683_FAN_CFG_REQ);
934 usleep_range(1000, 2000);
935 nct6683_write(data, NCT6683_REG_PWM_WRITE(index), val);
936 nct6683_write(data, NCT6683_REG_FAN_CFG_CTRL, NCT6683_FAN_CFG_DONE);
937 mutex_unlock(&data->update_lock);
942 SENSOR_TEMPLATE(pwm, "pwm%d", S_IRUGO, show_pwm, store_pwm, 0);
944 static umode_t nct6683_pwm_is_visible(struct kobject *kobj,
945 struct attribute *attr, int index)
947 struct device *dev = kobj_to_dev(kobj);
948 struct nct6683_data *data = dev_get_drvdata(dev);
949 int pwm = index; /* pwm index */
951 if (!(data->have_pwm & (1 << pwm)))
954 /* Only update pwm values for Mitac boards */
955 if (data->customer_id == NCT6683_CUSTOMER_ID_MITAC)
956 return attr->mode | S_IWUSR;
961 static struct sensor_device_template *nct6683_attributes_pwm_template[] = {
962 &sensor_dev_template_pwm,
966 static const struct sensor_template_group nct6683_pwm_template_group = {
967 .templates = nct6683_attributes_pwm_template,
968 .is_visible = nct6683_pwm_is_visible,
973 beep_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
975 struct nct6683_data *data = dev_get_drvdata(dev);
979 mutex_lock(&data->update_lock);
981 ret = superio_enter(data->sioreg);
984 superio_select(data->sioreg, NCT6683_LD_HWM);
985 reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
986 superio_exit(data->sioreg);
988 mutex_unlock(&data->update_lock);
990 return sprintf(buf, "%u\n", !!(reg & NCT6683_CR_BEEP_MASK));
993 mutex_unlock(&data->update_lock);
998 beep_enable_store(struct device *dev, struct device_attribute *attr,
999 const char *buf, size_t count)
1001 struct nct6683_data *data = dev_get_drvdata(dev);
1006 if (kstrtoul(buf, 10, &val) || (val != 0 && val != 1))
1009 mutex_lock(&data->update_lock);
1011 ret = superio_enter(data->sioreg);
1017 superio_select(data->sioreg, NCT6683_LD_HWM);
1018 reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
1020 reg |= NCT6683_CR_BEEP_MASK;
1022 reg &= ~NCT6683_CR_BEEP_MASK;
1023 superio_outb(data->sioreg, NCT6683_REG_CR_BEEP, reg);
1024 superio_exit(data->sioreg);
1026 mutex_unlock(&data->update_lock);
1030 /* Case open detection */
1033 intrusion0_alarm_show(struct device *dev, struct device_attribute *attr,
1036 struct nct6683_data *data = dev_get_drvdata(dev);
1040 mutex_lock(&data->update_lock);
1042 ret = superio_enter(data->sioreg);
1045 superio_select(data->sioreg, NCT6683_LD_ACPI);
1046 reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
1047 superio_exit(data->sioreg);
1049 mutex_unlock(&data->update_lock);
1051 return sprintf(buf, "%u\n", !(reg & NCT6683_CR_CASEOPEN_MASK));
1054 mutex_unlock(&data->update_lock);
1059 intrusion0_alarm_store(struct device *dev, struct device_attribute *attr,
1060 const char *buf, size_t count)
1062 struct nct6683_data *data = dev_get_drvdata(dev);
1067 if (kstrtoul(buf, 10, &val) || val != 0)
1070 mutex_lock(&data->update_lock);
1073 * Use CR registers to clear caseopen status.
1074 * Caseopen is activ low, clear by writing 1 into the register.
1077 ret = superio_enter(data->sioreg);
1083 superio_select(data->sioreg, NCT6683_LD_ACPI);
1084 reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
1085 reg |= NCT6683_CR_CASEOPEN_MASK;
1086 superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
1087 reg &= ~NCT6683_CR_CASEOPEN_MASK;
1088 superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
1089 superio_exit(data->sioreg);
1091 data->valid = false; /* Force cache refresh */
1093 mutex_unlock(&data->update_lock);
1097 static DEVICE_ATTR_RW(intrusion0_alarm);
1098 static DEVICE_ATTR_RW(beep_enable);
1100 static struct attribute *nct6683_attributes_other[] = {
1101 &dev_attr_intrusion0_alarm.attr,
1102 &dev_attr_beep_enable.attr,
1106 static const struct attribute_group nct6683_group_other = {
1107 .attrs = nct6683_attributes_other,
1110 /* Get the monitoring functions started */
1111 static inline void nct6683_init_device(struct nct6683_data *data)
1115 /* Start hardware monitoring if needed */
1116 tmp = nct6683_read(data, NCT6683_HWM_CFG);
1118 nct6683_write(data, NCT6683_HWM_CFG, tmp | 0x80);
1122 * There are a total of 24 fan inputs. Each can be configured as input
1123 * or as output. A maximum of 16 inputs and 8 outputs is configurable.
1126 nct6683_setup_fans(struct nct6683_data *data)
1131 for (i = 0; i < NCT6683_NUM_REG_FAN; i++) {
1132 reg = nct6683_read(data, NCT6683_REG_FANIN_CFG(i));
1134 data->have_fan |= 1 << i;
1135 data->fanin_cfg[i] = reg;
1137 for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
1138 reg = nct6683_read(data, NCT6683_REG_FANOUT_CFG(i));
1140 data->have_pwm |= 1 << i;
1141 data->fanout_cfg[i] = reg;
1146 * Translation from monitoring register to temperature and voltage attributes
1147 * ==========================================================================
1149 * There are a total of 32 monitoring registers. Each can be assigned to either
1150 * a temperature or voltage monitoring source.
1151 * NCT6683_REG_MON_CFG(x) defines assignment for each monitoring source.
1153 * Temperature and voltage attribute mapping is determined by walking through
1154 * the NCT6683_REG_MON_CFG registers. If the assigned source is
1155 * a temperature, temp_index[n] is set to the monitor register index, and
1156 * temp_src[n] is set to the temperature source. If the assigned source is
1157 * a voltage, the respective values are stored in in_index[] and in_src[],
1161 static void nct6683_setup_sensors(struct nct6683_data *data)
1168 for (i = 0; i < NCT6683_NUM_REG_MON; i++) {
1169 reg = nct6683_read(data, NCT6683_REG_MON_CFG(i)) & 0x7f;
1170 /* Ignore invalid assignments */
1171 if (reg >= NUM_MON_LABELS)
1173 /* Skip if disabled or reserved */
1174 if (nct6683_mon_label[reg] == NULL)
1176 if (reg < MON_VOLTAGE_START) {
1177 data->temp_index[data->temp_num] = i;
1178 data->temp_src[data->temp_num] = reg;
1181 data->in_index[data->in_num] = i;
1182 data->in_src[data->in_num] = reg;
1188 static int nct6683_probe(struct platform_device *pdev)
1190 struct device *dev = &pdev->dev;
1191 struct nct6683_sio_data *sio_data = dev->platform_data;
1192 struct attribute_group *group;
1193 struct nct6683_data *data;
1194 struct device *hwmon_dev;
1195 struct resource *res;
1199 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1200 if (!devm_request_region(dev, res->start, IOREGION_LENGTH, DRVNAME))
1203 data = devm_kzalloc(dev, sizeof(struct nct6683_data), GFP_KERNEL);
1207 data->kind = sio_data->kind;
1208 data->sioreg = sio_data->sioreg;
1209 data->addr = res->start;
1210 mutex_init(&data->update_lock);
1211 platform_set_drvdata(pdev, data);
1213 data->customer_id = nct6683_read16(data, NCT6683_REG_CUSTOMER_ID);
1215 /* By default only instantiate driver if the customer ID is known */
1216 switch (data->customer_id) {
1217 case NCT6683_CUSTOMER_ID_INTEL:
1219 case NCT6683_CUSTOMER_ID_MITAC:
1221 case NCT6683_CUSTOMER_ID_MSI:
1223 case NCT6683_CUSTOMER_ID_ASROCK:
1225 case NCT6683_CUSTOMER_ID_ASROCK2:
1232 nct6683_init_device(data);
1233 nct6683_setup_fans(data);
1234 nct6683_setup_sensors(data);
1236 /* Register sysfs hooks */
1238 if (data->have_pwm) {
1239 group = nct6683_create_attr_group(dev,
1240 &nct6683_pwm_template_group,
1241 fls(data->have_pwm));
1243 return PTR_ERR(group);
1244 data->groups[groups++] = group;
1248 group = nct6683_create_attr_group(dev,
1249 &nct6683_in_template_group,
1252 return PTR_ERR(group);
1253 data->groups[groups++] = group;
1256 if (data->have_fan) {
1257 group = nct6683_create_attr_group(dev,
1258 &nct6683_fan_template_group,
1259 fls(data->have_fan));
1261 return PTR_ERR(group);
1262 data->groups[groups++] = group;
1265 if (data->temp_num) {
1266 group = nct6683_create_attr_group(dev,
1267 &nct6683_temp_template_group,
1270 return PTR_ERR(group);
1271 data->groups[groups++] = group;
1273 data->groups[groups++] = &nct6683_group_other;
1275 if (data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
1276 scnprintf(build, sizeof(build), "%02x/%02x/%02x",
1277 nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1278 nct6683_read(data, NCT6683_REG_BUILD_DAY),
1279 nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1281 scnprintf(build, sizeof(build), "%02d/%02d/%02d",
1282 nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1283 nct6683_read(data, NCT6683_REG_BUILD_DAY),
1284 nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1286 dev_info(dev, "%s EC firmware version %d.%d build %s\n",
1287 nct6683_chip_names[data->kind],
1288 nct6683_read(data, NCT6683_REG_VERSION_HI),
1289 nct6683_read(data, NCT6683_REG_VERSION_LO),
1292 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
1293 nct6683_device_names[data->kind], data, data->groups);
1294 return PTR_ERR_OR_ZERO(hwmon_dev);
1298 static int nct6683_suspend(struct device *dev)
1300 struct nct6683_data *data = nct6683_update_device(dev);
1302 mutex_lock(&data->update_lock);
1303 data->hwm_cfg = nct6683_read(data, NCT6683_HWM_CFG);
1304 mutex_unlock(&data->update_lock);
1309 static int nct6683_resume(struct device *dev)
1311 struct nct6683_data *data = dev_get_drvdata(dev);
1313 mutex_lock(&data->update_lock);
1315 nct6683_write(data, NCT6683_HWM_CFG, data->hwm_cfg);
1317 /* Force re-reading all values */
1318 data->valid = false;
1319 mutex_unlock(&data->update_lock);
1324 static const struct dev_pm_ops nct6683_dev_pm_ops = {
1325 .suspend = nct6683_suspend,
1326 .resume = nct6683_resume,
1327 .freeze = nct6683_suspend,
1328 .restore = nct6683_resume,
1331 #define NCT6683_DEV_PM_OPS (&nct6683_dev_pm_ops)
1333 #define NCT6683_DEV_PM_OPS NULL
1334 #endif /* CONFIG_PM */
1336 static struct platform_driver nct6683_driver = {
1339 .pm = NCT6683_DEV_PM_OPS,
1341 .probe = nct6683_probe,
1344 static int __init nct6683_find(int sioaddr, struct nct6683_sio_data *sio_data)
1350 err = superio_enter(sioaddr);
1354 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
1355 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
1357 switch (val & SIO_ID_MASK) {
1358 case SIO_NCT6683_ID:
1359 sio_data->kind = nct6683;
1361 case SIO_NCT6686_ID:
1362 sio_data->kind = nct6686;
1364 case SIO_NCT6687_ID:
1365 sio_data->kind = nct6687;
1369 pr_debug("unsupported chip ID: 0x%04x\n", val);
1373 /* We have a known chip, find the HWM I/O address */
1374 superio_select(sioaddr, NCT6683_LD_HWM);
1375 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
1376 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
1377 addr = val & IOREGION_ALIGNMENT;
1379 pr_err("EC base I/O port unconfigured\n");
1383 /* Activate logical device if needed */
1384 val = superio_inb(sioaddr, SIO_REG_ENABLE);
1385 if (!(val & 0x01)) {
1386 pr_warn("Forcibly enabling EC access. Data may be unusable.\n");
1387 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
1390 superio_exit(sioaddr);
1391 pr_info("Found %s or compatible chip at %#x:%#x\n",
1392 nct6683_chip_names[sio_data->kind], sioaddr, addr);
1393 sio_data->sioreg = sioaddr;
1398 superio_exit(sioaddr);
1403 * when Super-I/O functions move to a separate file, the Super-I/O
1404 * bus will manage the lifetime of the device and this module will only keep
1405 * track of the nct6683 driver. But since we use platform_device_alloc(), we
1406 * must keep track of the device
1408 static struct platform_device *pdev[2];
1410 static int __init sensors_nct6683_init(void)
1412 struct nct6683_sio_data sio_data;
1413 int sioaddr[2] = { 0x2e, 0x4e };
1414 struct resource res;
1419 err = platform_driver_register(&nct6683_driver);
1424 * initialize sio_data->kind and sio_data->sioreg.
1426 * when Super-I/O functions move to a separate file, the Super-I/O
1427 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1428 * nct6683 hardware monitor, and call probe()
1430 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1431 address = nct6683_find(sioaddr[i], &sio_data);
1437 pdev[i] = platform_device_alloc(DRVNAME, address);
1440 goto exit_device_unregister;
1443 err = platform_device_add_data(pdev[i], &sio_data,
1444 sizeof(struct nct6683_sio_data));
1446 goto exit_device_put;
1448 memset(&res, 0, sizeof(res));
1450 res.start = address + IOREGION_OFFSET;
1451 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
1452 res.flags = IORESOURCE_IO;
1454 err = acpi_check_resource_conflict(&res);
1456 platform_device_put(pdev[i]);
1461 err = platform_device_add_resources(pdev[i], &res, 1);
1463 goto exit_device_put;
1465 /* platform_device_add calls probe() */
1466 err = platform_device_add(pdev[i]);
1468 goto exit_device_put;
1472 goto exit_unregister;
1478 platform_device_put(pdev[i]);
1479 exit_device_unregister:
1482 platform_device_unregister(pdev[i]);
1485 platform_driver_unregister(&nct6683_driver);
1489 static void __exit sensors_nct6683_exit(void)
1493 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1495 platform_device_unregister(pdev[i]);
1497 platform_driver_unregister(&nct6683_driver);
1500 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1501 MODULE_DESCRIPTION("NCT6683D driver");
1502 MODULE_LICENSE("GPL");
1504 module_init(sensors_nct6683_init);
1505 module_exit(sensors_nct6683_exit);