1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * nct6775 - Platform driver for the hardware monitoring
4 * functionality of Nuvoton NCT677x Super-I/O chips
6 * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/acpi.h>
12 #include <linux/dmi.h>
13 #include <linux/hwmon-sysfs.h>
14 #include <linux/hwmon-vid.h>
15 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/wmi.h>
24 enum sensor_access { access_direct, access_asuswmi };
26 static const char * const nct6775_sio_names[] __initconst = {
41 static unsigned short force_id;
42 module_param(force_id, ushort, 0);
43 MODULE_PARM_DESC(force_id, "Override the detected device ID");
45 static unsigned short fan_debounce;
46 module_param(fan_debounce, ushort, 0);
47 MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
49 #define DRVNAME "nct6775"
51 #define NCT6775_PORT_CHIPID 0x58
57 #define IOREGION_ALIGNMENT (~7)
58 #define IOREGION_OFFSET 5
59 #define IOREGION_LENGTH 2
60 #define ADDR_REG_OFFSET 0
61 #define DATA_REG_OFFSET 1
64 * Super-I/O constants and functions
67 #define NCT6775_LD_ACPI 0x0a
68 #define NCT6775_LD_HWM 0x0b
69 #define NCT6775_LD_VID 0x0d
70 #define NCT6775_LD_12 0x12
72 #define SIO_REG_LDSEL 0x07 /* Logical device select */
73 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
74 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
75 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
77 #define SIO_NCT6106_ID 0xc450
78 #define SIO_NCT6116_ID 0xd280
79 #define SIO_NCT6775_ID 0xb470
80 #define SIO_NCT6776_ID 0xc330
81 #define SIO_NCT6779_ID 0xc560
82 #define SIO_NCT6791_ID 0xc800
83 #define SIO_NCT6792_ID 0xc910
84 #define SIO_NCT6793_ID 0xd120
85 #define SIO_NCT6795_ID 0xd350
86 #define SIO_NCT6796_ID 0xd420
87 #define SIO_NCT6797_ID 0xd450
88 #define SIO_NCT6798_ID 0xd428
89 #define SIO_ID_MASK 0xFFF8
94 #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0
96 struct nct6775_sio_data {
100 enum sensor_access access;
102 /* superio_() callbacks */
103 void (*sio_outb)(struct nct6775_sio_data *sio_data, int reg, int val);
104 int (*sio_inb)(struct nct6775_sio_data *sio_data, int reg);
105 void (*sio_select)(struct nct6775_sio_data *sio_data, int ld);
106 int (*sio_enter)(struct nct6775_sio_data *sio_data);
107 void (*sio_exit)(struct nct6775_sio_data *sio_data);
110 #define ASUSWMI_MONITORING_GUID "466747A0-70EC-11DE-8A39-0800200C9A66"
111 #define ASUSWMI_METHODID_RSIO 0x5253494F
112 #define ASUSWMI_METHODID_WSIO 0x5753494F
113 #define ASUSWMI_METHODID_RHWM 0x5248574D
114 #define ASUSWMI_METHODID_WHWM 0x5748574D
115 #define ASUSWMI_UNSUPPORTED_METHOD 0xFFFFFFFE
117 static int nct6775_asuswmi_evaluate_method(u32 method_id, u8 bank, u8 reg, u8 val, u32 *retval)
119 #if IS_ENABLED(CONFIG_ACPI_WMI)
120 u32 args = bank | (reg << 8) | (val << 16);
121 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
122 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
124 union acpi_object *obj;
125 u32 tmp = ASUSWMI_UNSUPPORTED_METHOD;
127 status = wmi_evaluate_method(ASUSWMI_MONITORING_GUID, 0,
128 method_id, &input, &output);
130 if (ACPI_FAILURE(status))
133 obj = output.pointer;
134 if (obj && obj->type == ACPI_TYPE_INTEGER)
135 tmp = obj->integer.value;
142 if (tmp == ASUSWMI_UNSUPPORTED_METHOD)
150 static inline int nct6775_asuswmi_write(u8 bank, u8 reg, u8 val)
152 return nct6775_asuswmi_evaluate_method(ASUSWMI_METHODID_WHWM, bank,
156 static inline int nct6775_asuswmi_read(u8 bank, u8 reg, u8 *val)
160 ret = nct6775_asuswmi_evaluate_method(ASUSWMI_METHODID_RHWM, bank,
166 static int superio_wmi_inb(struct nct6775_sio_data *sio_data, int reg)
170 nct6775_asuswmi_evaluate_method(ASUSWMI_METHODID_RSIO, sio_data->ld,
175 static void superio_wmi_outb(struct nct6775_sio_data *sio_data, int reg, int val)
177 nct6775_asuswmi_evaluate_method(ASUSWMI_METHODID_WSIO, sio_data->ld,
181 static void superio_wmi_select(struct nct6775_sio_data *sio_data, int ld)
186 static int superio_wmi_enter(struct nct6775_sio_data *sio_data)
191 static void superio_wmi_exit(struct nct6775_sio_data *sio_data)
195 static void superio_outb(struct nct6775_sio_data *sio_data, int reg, int val)
197 int ioreg = sio_data->sioreg;
200 outb(val, ioreg + 1);
203 static int superio_inb(struct nct6775_sio_data *sio_data, int reg)
205 int ioreg = sio_data->sioreg;
208 return inb(ioreg + 1);
211 static void superio_select(struct nct6775_sio_data *sio_data, int ld)
213 int ioreg = sio_data->sioreg;
215 outb(SIO_REG_LDSEL, ioreg);
219 static int superio_enter(struct nct6775_sio_data *sio_data)
221 int ioreg = sio_data->sioreg;
224 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
226 if (!request_muxed_region(ioreg, 2, DRVNAME))
235 static void superio_exit(struct nct6775_sio_data *sio_data)
237 int ioreg = sio_data->sioreg;
241 outb(0x02, ioreg + 1);
242 release_region(ioreg, 2);
245 static inline void nct6775_wmi_set_bank(struct nct6775_data *data, u16 reg)
252 static int nct6775_wmi_reg_read(void *ctx, unsigned int reg, unsigned int *val)
254 struct nct6775_data *data = ctx;
255 int err, word_sized = nct6775_reg_is_word_sized(data, reg);
259 nct6775_wmi_set_bank(data, reg);
261 err = nct6775_asuswmi_read(data->bank, reg & 0xff, &tmp);
267 err = nct6775_asuswmi_read(data->bank, (reg & 0xff) + 1, &tmp);
271 res = (res << 8) + tmp;
277 static int nct6775_wmi_reg_write(void *ctx, unsigned int reg, unsigned int value)
279 struct nct6775_data *data = ctx;
280 int res, word_sized = nct6775_reg_is_word_sized(data, reg);
282 nct6775_wmi_set_bank(data, reg);
285 res = nct6775_asuswmi_write(data->bank, reg & 0xff, value >> 8);
289 res = nct6775_asuswmi_write(data->bank, (reg & 0xff) + 1, value);
291 res = nct6775_asuswmi_write(data->bank, reg & 0xff, value);
298 * On older chips, only registers 0x50-0x5f are banked.
299 * On more recent chips, all registers are banked.
300 * Assume that is the case and set the bank number for each access.
301 * Cache the bank number so it only needs to be set if it changes.
303 static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
307 if (data->bank != bank) {
308 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
309 outb_p(bank, data->addr + DATA_REG_OFFSET);
314 static int nct6775_reg_read(void *ctx, unsigned int reg, unsigned int *val)
316 struct nct6775_data *data = ctx;
317 int word_sized = nct6775_reg_is_word_sized(data, reg);
319 nct6775_set_bank(data, reg);
320 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
321 *val = inb_p(data->addr + DATA_REG_OFFSET);
323 outb_p((reg & 0xff) + 1,
324 data->addr + ADDR_REG_OFFSET);
325 *val = (*val << 8) + inb_p(data->addr + DATA_REG_OFFSET);
330 static int nct6775_reg_write(void *ctx, unsigned int reg, unsigned int value)
332 struct nct6775_data *data = ctx;
333 int word_sized = nct6775_reg_is_word_sized(data, reg);
335 nct6775_set_bank(data, reg);
336 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
338 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
339 outb_p((reg & 0xff) + 1,
340 data->addr + ADDR_REG_OFFSET);
342 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
346 static void nct6791_enable_io_mapping(struct nct6775_sio_data *sio_data)
350 val = sio_data->sio_inb(sio_data, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE);
352 pr_info("Enabling hardware monitor logical device mappings.\n");
353 sio_data->sio_outb(sio_data, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE,
358 static int nct6775_suspend(struct device *dev)
362 struct nct6775_data *data = nct6775_update_device(dev);
365 return PTR_ERR(data);
367 mutex_lock(&data->update_lock);
368 err = nct6775_read_value(data, data->REG_VBAT, &tmp);
372 if (data->kind == nct6775) {
373 err = nct6775_read_value(data, NCT6775_REG_FANDIV1, &tmp);
378 err = nct6775_read_value(data, NCT6775_REG_FANDIV2, &tmp);
384 mutex_unlock(&data->update_lock);
389 static int nct6775_resume(struct device *dev)
391 struct nct6775_data *data = dev_get_drvdata(dev);
392 struct nct6775_sio_data *sio_data = dev_get_platdata(dev);
396 mutex_lock(&data->update_lock);
397 data->bank = 0xff; /* Force initial bank selection */
399 err = sio_data->sio_enter(sio_data);
403 sio_data->sio_select(sio_data, NCT6775_LD_HWM);
404 reg = sio_data->sio_inb(sio_data, SIO_REG_ENABLE);
405 if (reg != data->sio_reg_enable)
406 sio_data->sio_outb(sio_data, SIO_REG_ENABLE, data->sio_reg_enable);
408 if (data->kind == nct6791 || data->kind == nct6792 ||
409 data->kind == nct6793 || data->kind == nct6795 ||
410 data->kind == nct6796 || data->kind == nct6797 ||
411 data->kind == nct6798)
412 nct6791_enable_io_mapping(sio_data);
414 sio_data->sio_exit(sio_data);
417 for (i = 0; i < data->in_num; i++) {
418 if (!(data->have_in & BIT(i)))
421 err = nct6775_write_value(data, data->REG_IN_MINMAX[0][i], data->in[i][1]);
424 err = nct6775_write_value(data, data->REG_IN_MINMAX[1][i], data->in[i][2]);
429 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
430 if (!(data->has_fan_min & BIT(i)))
433 err = nct6775_write_value(data, data->REG_FAN_MIN[i], data->fan_min[i]);
438 for (i = 0; i < NUM_TEMP; i++) {
439 if (!(data->have_temp & BIT(i)))
442 for (j = 1; j < ARRAY_SIZE(data->reg_temp); j++)
443 if (data->reg_temp[j][i]) {
444 err = nct6775_write_temp(data, data->reg_temp[j][i],
451 /* Restore other settings */
452 err = nct6775_write_value(data, data->REG_VBAT, data->vbat);
455 if (data->kind == nct6775) {
456 err = nct6775_write_value(data, NCT6775_REG_FANDIV1, data->fandiv1);
459 err = nct6775_write_value(data, NCT6775_REG_FANDIV2, data->fandiv2);
463 /* Force re-reading all values */
465 mutex_unlock(&data->update_lock);
470 static DEFINE_SIMPLE_DEV_PM_OPS(nct6775_dev_pm_ops, nct6775_suspend, nct6775_resume);
473 nct6775_check_fan_inputs(struct nct6775_data *data, struct nct6775_sio_data *sio_data)
475 bool fan3pin = false, fan4pin = false, fan4min = false;
476 bool fan5pin = false, fan6pin = false, fan7pin = false;
477 bool pwm3pin = false, pwm4pin = false, pwm5pin = false;
478 bool pwm6pin = false, pwm7pin = false;
480 /* Store SIO_REG_ENABLE for use during resume */
481 sio_data->sio_select(sio_data, NCT6775_LD_HWM);
482 data->sio_reg_enable = sio_data->sio_inb(sio_data, SIO_REG_ENABLE);
484 /* fan4 and fan5 share some pins with the GPIO and serial flash */
485 if (data->kind == nct6775) {
486 int cr2c = sio_data->sio_inb(sio_data, 0x2c);
488 fan3pin = cr2c & BIT(6);
489 pwm3pin = cr2c & BIT(7);
491 /* On NCT6775, fan4 shares pins with the fdc interface */
492 fan4pin = !(sio_data->sio_inb(sio_data, 0x2A) & 0x80);
493 } else if (data->kind == nct6776) {
494 bool gpok = sio_data->sio_inb(sio_data, 0x27) & 0x80;
495 const char *board_vendor, *board_name;
497 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
498 board_name = dmi_get_system_info(DMI_BOARD_NAME);
500 if (board_name && board_vendor &&
501 !strcmp(board_vendor, "ASRock")) {
503 * Auxiliary fan monitoring is not enabled on ASRock
504 * Z77 Pro4-M if booted in UEFI Ultra-FastBoot mode.
505 * Observed with BIOS version 2.00.
507 if (!strcmp(board_name, "Z77 Pro4-M")) {
508 if ((data->sio_reg_enable & 0xe0) != 0xe0) {
509 data->sio_reg_enable |= 0xe0;
510 sio_data->sio_outb(sio_data, SIO_REG_ENABLE,
511 data->sio_reg_enable);
516 if (data->sio_reg_enable & 0x80)
519 fan3pin = !(sio_data->sio_inb(sio_data, 0x24) & 0x40);
521 if (data->sio_reg_enable & 0x40)
524 fan4pin = sio_data->sio_inb(sio_data, 0x1C) & 0x01;
526 if (data->sio_reg_enable & 0x20)
529 fan5pin = sio_data->sio_inb(sio_data, 0x1C) & 0x02;
533 } else if (data->kind == nct6106) {
534 int cr24 = sio_data->sio_inb(sio_data, 0x24);
536 fan3pin = !(cr24 & 0x80);
537 pwm3pin = cr24 & 0x08;
538 } else if (data->kind == nct6116) {
539 int cr1a = sio_data->sio_inb(sio_data, 0x1a);
540 int cr1b = sio_data->sio_inb(sio_data, 0x1b);
541 int cr24 = sio_data->sio_inb(sio_data, 0x24);
542 int cr2a = sio_data->sio_inb(sio_data, 0x2a);
543 int cr2b = sio_data->sio_inb(sio_data, 0x2b);
544 int cr2f = sio_data->sio_inb(sio_data, 0x2f);
546 fan3pin = !(cr2b & 0x10);
547 fan4pin = (cr2b & 0x80) || // pin 1(2)
548 (!(cr2f & 0x10) && (cr1a & 0x04)); // pin 65(66)
549 fan5pin = (cr2b & 0x80) || // pin 126(127)
550 (!(cr1b & 0x03) && (cr2a & 0x02)); // pin 94(96)
552 pwm3pin = fan3pin && (cr24 & 0x08);
557 * NCT6779D, NCT6791D, NCT6792D, NCT6793D, NCT6795D, NCT6796D,
560 int cr1a = sio_data->sio_inb(sio_data, 0x1a);
561 int cr1b = sio_data->sio_inb(sio_data, 0x1b);
562 int cr1c = sio_data->sio_inb(sio_data, 0x1c);
563 int cr1d = sio_data->sio_inb(sio_data, 0x1d);
564 int cr2a = sio_data->sio_inb(sio_data, 0x2a);
565 int cr2b = sio_data->sio_inb(sio_data, 0x2b);
566 int cr2d = sio_data->sio_inb(sio_data, 0x2d);
567 int cr2f = sio_data->sio_inb(sio_data, 0x2f);
568 bool dsw_en = cr2f & BIT(3);
569 bool ddr4_en = cr2f & BIT(4);
574 sio_data->sio_select(sio_data, NCT6775_LD_12);
575 cre0 = sio_data->sio_inb(sio_data, 0xe0);
576 creb = sio_data->sio_inb(sio_data, 0xeb);
577 cred = sio_data->sio_inb(sio_data, 0xed);
579 fan3pin = !(cr1c & BIT(5));
580 fan4pin = !(cr1c & BIT(6));
581 fan5pin = !(cr1c & BIT(7));
583 pwm3pin = !(cr1c & BIT(0));
584 pwm4pin = !(cr1c & BIT(1));
585 pwm5pin = !(cr1c & BIT(2));
587 switch (data->kind) {
589 fan6pin = cr2d & BIT(1);
590 pwm6pin = cr2d & BIT(0);
593 fan6pin = !dsw_en && (cr2d & BIT(1));
594 pwm6pin = !dsw_en && (cr2d & BIT(0));
597 fan5pin |= cr1b & BIT(5);
598 fan5pin |= creb & BIT(5);
600 fan6pin = !dsw_en && (cr2d & BIT(1));
601 fan6pin |= creb & BIT(3);
603 pwm5pin |= cr2d & BIT(7);
604 pwm5pin |= (creb & BIT(4)) && !(cr2a & BIT(0));
606 pwm6pin = !dsw_en && (cr2d & BIT(0));
607 pwm6pin |= creb & BIT(2);
610 fan5pin |= cr1b & BIT(5);
611 fan5pin |= creb & BIT(5);
613 fan6pin = (cr2a & BIT(4)) &&
614 (!dsw_en || (cred & BIT(4)));
615 fan6pin |= creb & BIT(3);
617 pwm5pin |= cr2d & BIT(7);
618 pwm5pin |= (creb & BIT(4)) && !(cr2a & BIT(0));
620 pwm6pin = (cr2a & BIT(3)) && (cred & BIT(2));
621 pwm6pin |= creb & BIT(2);
624 fan5pin |= cr1b & BIT(5);
625 fan5pin |= (cre0 & BIT(3)) && !(cr1b & BIT(0));
626 fan5pin |= creb & BIT(5);
628 fan6pin = (cr2a & BIT(4)) &&
629 (!dsw_en || (cred & BIT(4)));
630 fan6pin |= creb & BIT(3);
632 fan7pin = !(cr2b & BIT(2));
634 pwm5pin |= cr2d & BIT(7);
635 pwm5pin |= (cre0 & BIT(4)) && !(cr1b & BIT(0));
636 pwm5pin |= (creb & BIT(4)) && !(cr2a & BIT(0));
638 pwm6pin = (cr2a & BIT(3)) && (cred & BIT(2));
639 pwm6pin |= creb & BIT(2);
641 pwm7pin = !(cr1d & (BIT(2) | BIT(3)));
644 fan5pin |= !ddr4_en && (cr1b & BIT(5));
645 fan5pin |= creb & BIT(5);
647 fan6pin = cr2a & BIT(4);
648 fan6pin |= creb & BIT(3);
650 fan7pin = cr1a & BIT(1);
652 pwm5pin |= (creb & BIT(4)) && !(cr2a & BIT(0));
653 pwm5pin |= !ddr4_en && (cr2d & BIT(7));
655 pwm6pin = creb & BIT(2);
656 pwm6pin |= cred & BIT(2);
658 pwm7pin = cr1d & BIT(4);
661 fan6pin = !(cr1b & BIT(0)) && (cre0 & BIT(3));
662 fan6pin |= cr2a & BIT(4);
663 fan6pin |= creb & BIT(5);
665 fan7pin = cr1b & BIT(5);
666 fan7pin |= !(cr2b & BIT(2));
667 fan7pin |= creb & BIT(3);
669 pwm6pin = !(cr1b & BIT(0)) && (cre0 & BIT(4));
670 pwm6pin |= !(cred & BIT(2)) && (cr2a & BIT(3));
671 pwm6pin |= (creb & BIT(4)) && !(cr2a & BIT(0));
673 pwm7pin = !(cr1d & (BIT(2) | BIT(3)));
674 pwm7pin |= cr2d & BIT(7);
675 pwm7pin |= creb & BIT(2);
677 default: /* NCT6779D */
684 /* fan 1 and 2 (0x03) are always present */
685 data->has_fan = 0x03 | (fan3pin << 2) | (fan4pin << 3) |
686 (fan5pin << 4) | (fan6pin << 5) | (fan7pin << 6);
687 data->has_fan_min = 0x03 | (fan3pin << 2) | (fan4min << 3) |
688 (fan5pin << 4) | (fan6pin << 5) | (fan7pin << 6);
689 data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) |
690 (pwm5pin << 4) | (pwm6pin << 5) | (pwm7pin << 6);
694 cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf)
696 struct nct6775_data *data = dev_get_drvdata(dev);
698 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
701 static DEVICE_ATTR_RO(cpu0_vid);
703 /* Case open detection */
705 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
706 static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
709 clear_caseopen(struct device *dev, struct device_attribute *attr,
710 const char *buf, size_t count)
712 struct nct6775_data *data = dev_get_drvdata(dev);
713 struct nct6775_sio_data *sio_data = data->driver_data;
714 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
719 if (kstrtoul(buf, 10, &val) || val != 0)
722 mutex_lock(&data->update_lock);
725 * Use CR registers to clear caseopen status.
726 * The CR registers are the same for all chips, and not all chips
727 * support clearing the caseopen status through "regular" registers.
729 ret = sio_data->sio_enter(sio_data);
735 sio_data->sio_select(sio_data, NCT6775_LD_ACPI);
736 reg = sio_data->sio_inb(sio_data, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
737 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
738 sio_data->sio_outb(sio_data, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
739 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
740 sio_data->sio_outb(sio_data, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
741 sio_data->sio_exit(sio_data);
743 data->valid = false; /* Force cache refresh */
745 mutex_unlock(&data->update_lock);
749 static SENSOR_DEVICE_ATTR(intrusion0_alarm, 0644, nct6775_show_alarm,
750 clear_caseopen, INTRUSION_ALARM_BASE);
751 static SENSOR_DEVICE_ATTR(intrusion1_alarm, 0644, nct6775_show_alarm,
752 clear_caseopen, INTRUSION_ALARM_BASE + 1);
753 static SENSOR_DEVICE_ATTR(intrusion0_beep, 0644, nct6775_show_beep,
754 nct6775_store_beep, INTRUSION_ALARM_BASE);
755 static SENSOR_DEVICE_ATTR(intrusion1_beep, 0644, nct6775_show_beep,
756 nct6775_store_beep, INTRUSION_ALARM_BASE + 1);
757 static SENSOR_DEVICE_ATTR(beep_enable, 0644, nct6775_show_beep,
758 nct6775_store_beep, BEEP_ENABLE_BASE);
760 static umode_t nct6775_other_is_visible(struct kobject *kobj,
761 struct attribute *attr, int index)
763 struct device *dev = kobj_to_dev(kobj);
764 struct nct6775_data *data = dev_get_drvdata(dev);
766 if (index == 0 && !data->have_vid)
769 if (index == 1 || index == 2) {
770 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + index - 1] < 0)
774 if (index == 3 || index == 4) {
775 if (data->BEEP_BITS[INTRUSION_ALARM_BASE + index - 3] < 0)
779 return nct6775_attr_mode(data, attr);
783 * nct6775_other_is_visible uses the index into the following array
784 * to determine if attributes should be created or not.
785 * Any change in order or content must be matched.
787 static struct attribute *nct6775_attributes_other[] = {
788 &dev_attr_cpu0_vid.attr, /* 0 */
789 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr, /* 1 */
790 &sensor_dev_attr_intrusion1_alarm.dev_attr.attr, /* 2 */
791 &sensor_dev_attr_intrusion0_beep.dev_attr.attr, /* 3 */
792 &sensor_dev_attr_intrusion1_beep.dev_attr.attr, /* 4 */
793 &sensor_dev_attr_beep_enable.dev_attr.attr, /* 5 */
798 static const struct attribute_group nct6775_group_other = {
799 .attrs = nct6775_attributes_other,
800 .is_visible = nct6775_other_is_visible,
803 static int nct6775_platform_probe_init(struct nct6775_data *data)
807 struct nct6775_sio_data *sio_data = data->driver_data;
809 err = sio_data->sio_enter(sio_data);
813 cr2a = sio_data->sio_inb(sio_data, 0x2a);
814 switch (data->kind) {
816 data->have_vid = (cr2a & 0x40);
819 data->have_vid = (cr2a & 0x60) == 0x40;
836 * We can get the VID input values directly at logical device D 0xe3.
838 if (data->have_vid) {
839 sio_data->sio_select(sio_data, NCT6775_LD_VID);
840 data->vid = sio_data->sio_inb(sio_data, 0xe3);
841 data->vrm = vid_which_vrm();
847 sio_data->sio_select(sio_data, NCT6775_LD_HWM);
848 tmp = sio_data->sio_inb(sio_data,
849 NCT6775_REG_CR_FAN_DEBOUNCE);
850 switch (data->kind) {
872 sio_data->sio_outb(sio_data, NCT6775_REG_CR_FAN_DEBOUNCE,
874 pr_info("Enabled fan debounce for chip %s\n", data->name);
877 nct6775_check_fan_inputs(data, sio_data);
879 sio_data->sio_exit(sio_data);
881 return nct6775_add_attr_group(data, &nct6775_group_other);
884 static const struct regmap_config nct6775_regmap_config = {
887 .reg_read = nct6775_reg_read,
888 .reg_write = nct6775_reg_write,
891 static const struct regmap_config nct6775_wmi_regmap_config = {
894 .reg_read = nct6775_wmi_reg_read,
895 .reg_write = nct6775_wmi_reg_write,
898 static int nct6775_platform_probe(struct platform_device *pdev)
900 struct device *dev = &pdev->dev;
901 struct nct6775_sio_data *sio_data = dev_get_platdata(dev);
902 struct nct6775_data *data;
903 struct resource *res;
904 const struct regmap_config *regmapcfg;
906 if (sio_data->access == access_direct) {
907 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
908 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH, DRVNAME))
912 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
916 data->kind = sio_data->kind;
917 data->sioreg = sio_data->sioreg;
919 if (sio_data->access == access_direct) {
920 data->addr = res->start;
921 regmapcfg = &nct6775_regmap_config;
923 regmapcfg = &nct6775_wmi_regmap_config;
926 platform_set_drvdata(pdev, data);
928 data->driver_data = sio_data;
929 data->driver_init = nct6775_platform_probe_init;
931 return nct6775_probe(&pdev->dev, data, regmapcfg);
934 static struct platform_driver nct6775_driver = {
937 .pm = pm_sleep_ptr(&nct6775_dev_pm_ops),
939 .probe = nct6775_platform_probe,
942 /* nct6775_find() looks for a '627 in the Super-I/O config space */
943 static int __init nct6775_find(int sioaddr, struct nct6775_sio_data *sio_data)
949 sio_data->access = access_direct;
950 sio_data->sioreg = sioaddr;
952 err = sio_data->sio_enter(sio_data);
956 val = (sio_data->sio_inb(sio_data, SIO_REG_DEVID) << 8) |
957 sio_data->sio_inb(sio_data, SIO_REG_DEVID + 1);
958 if (force_id && val != 0xffff)
961 switch (val & SIO_ID_MASK) {
963 sio_data->kind = nct6106;
966 sio_data->kind = nct6116;
969 sio_data->kind = nct6775;
972 sio_data->kind = nct6776;
975 sio_data->kind = nct6779;
978 sio_data->kind = nct6791;
981 sio_data->kind = nct6792;
984 sio_data->kind = nct6793;
987 sio_data->kind = nct6795;
990 sio_data->kind = nct6796;
993 sio_data->kind = nct6797;
996 sio_data->kind = nct6798;
1000 pr_debug("unsupported chip ID: 0x%04x\n", val);
1001 sio_data->sio_exit(sio_data);
1005 /* We have a known chip, find the HWM I/O address */
1006 sio_data->sio_select(sio_data, NCT6775_LD_HWM);
1007 val = (sio_data->sio_inb(sio_data, SIO_REG_ADDR) << 8)
1008 | sio_data->sio_inb(sio_data, SIO_REG_ADDR + 1);
1009 addr = val & IOREGION_ALIGNMENT;
1011 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
1012 sio_data->sio_exit(sio_data);
1016 /* Activate logical device if needed */
1017 val = sio_data->sio_inb(sio_data, SIO_REG_ENABLE);
1018 if (!(val & 0x01)) {
1019 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
1020 sio_data->sio_outb(sio_data, SIO_REG_ENABLE, val | 0x01);
1023 if (sio_data->kind == nct6791 || sio_data->kind == nct6792 ||
1024 sio_data->kind == nct6793 || sio_data->kind == nct6795 ||
1025 sio_data->kind == nct6796 || sio_data->kind == nct6797 ||
1026 sio_data->kind == nct6798)
1027 nct6791_enable_io_mapping(sio_data);
1029 sio_data->sio_exit(sio_data);
1030 pr_info("Found %s or compatible chip at %#x:%#x\n",
1031 nct6775_sio_names[sio_data->kind], sioaddr, addr);
1037 * when Super-I/O functions move to a separate file, the Super-I/O
1038 * bus will manage the lifetime of the device and this module will only keep
1039 * track of the nct6775 driver. But since we use platform_device_alloc(), we
1040 * must keep track of the device
1042 static struct platform_device *pdev[2];
1044 static const char * const asus_wmi_boards[] = {
1046 "ProArt X570-CREATOR WIFI",
1053 "PRIME B550M-A (WI-FI)",
1057 "ROG CROSSHAIR VIII DARK HERO",
1058 "ROG CROSSHAIR VIII FORMULA",
1059 "ROG CROSSHAIR VIII HERO",
1060 "ROG CROSSHAIR VIII IMPACT",
1061 "ROG STRIX B550-A GAMING",
1062 "ROG STRIX B550-E GAMING",
1063 "ROG STRIX B550-F GAMING",
1064 "ROG STRIX B550-F GAMING (WI-FI)",
1065 "ROG STRIX B550-F GAMING WIFI II",
1066 "ROG STRIX B550-I GAMING",
1067 "ROG STRIX B550-XE GAMING (WI-FI)",
1068 "ROG STRIX X570-E GAMING",
1069 "ROG STRIX X570-E GAMING WIFI II",
1070 "ROG STRIX X570-F GAMING",
1071 "ROG STRIX X570-I GAMING",
1072 "ROG STRIX Z390-E GAMING",
1073 "ROG STRIX Z390-F GAMING",
1074 "ROG STRIX Z390-H GAMING",
1075 "ROG STRIX Z390-I GAMING",
1076 "ROG STRIX Z490-A GAMING",
1077 "ROG STRIX Z490-E GAMING",
1078 "ROG STRIX Z490-F GAMING",
1079 "ROG STRIX Z490-G GAMING",
1080 "ROG STRIX Z490-G GAMING (WI-FI)",
1081 "ROG STRIX Z490-H GAMING",
1082 "ROG STRIX Z490-I GAMING",
1083 "TUF GAMING B550M-PLUS",
1084 "TUF GAMING B550M-PLUS (WI-FI)",
1085 "TUF GAMING B550-PLUS",
1086 "TUF GAMING B550-PLUS WIFI II",
1087 "TUF GAMING B550-PRO",
1088 "TUF GAMING X570-PLUS",
1089 "TUF GAMING X570-PLUS (WI-FI)",
1090 "TUF GAMING X570-PRO (WI-FI)",
1091 "TUF GAMING Z490-PLUS",
1092 "TUF GAMING Z490-PLUS (WI-FI)",
1095 static int __init sensors_nct6775_platform_init(void)
1100 struct resource res;
1101 struct nct6775_sio_data sio_data;
1102 int sioaddr[2] = { 0x2e, 0x4e };
1103 enum sensor_access access = access_direct;
1104 const char *board_vendor, *board_name;
1107 err = platform_driver_register(&nct6775_driver);
1111 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1112 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1114 if (board_name && board_vendor &&
1115 !strcmp(board_vendor, "ASUSTeK COMPUTER INC.")) {
1116 err = match_string(asus_wmi_boards, ARRAY_SIZE(asus_wmi_boards),
1119 /* if reading chip id via WMI succeeds, use WMI */
1120 if (!nct6775_asuswmi_read(0, NCT6775_PORT_CHIPID, &tmp) && tmp) {
1121 pr_info("Using Asus WMI to access %#x chip.\n", tmp);
1122 access = access_asuswmi;
1124 pr_err("Can't read ChipID by Asus WMI.\n");
1130 * initialize sio_data->kind and sio_data->sioreg.
1132 * when Super-I/O functions move to a separate file, the Super-I/O
1133 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1134 * nct6775 hardware monitor, and call probe()
1136 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1137 sio_data.sio_outb = superio_outb;
1138 sio_data.sio_inb = superio_inb;
1139 sio_data.sio_select = superio_select;
1140 sio_data.sio_enter = superio_enter;
1141 sio_data.sio_exit = superio_exit;
1143 address = nct6775_find(sioaddr[i], &sio_data);
1149 sio_data.access = access;
1151 if (access == access_asuswmi) {
1152 sio_data.sio_outb = superio_wmi_outb;
1153 sio_data.sio_inb = superio_wmi_inb;
1154 sio_data.sio_select = superio_wmi_select;
1155 sio_data.sio_enter = superio_wmi_enter;
1156 sio_data.sio_exit = superio_wmi_exit;
1159 pdev[i] = platform_device_alloc(DRVNAME, address);
1162 goto exit_device_unregister;
1165 err = platform_device_add_data(pdev[i], &sio_data,
1166 sizeof(struct nct6775_sio_data));
1168 goto exit_device_put;
1170 if (sio_data.access == access_direct) {
1171 memset(&res, 0, sizeof(res));
1173 res.start = address + IOREGION_OFFSET;
1174 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
1175 res.flags = IORESOURCE_IO;
1177 err = acpi_check_resource_conflict(&res);
1179 platform_device_put(pdev[i]);
1184 err = platform_device_add_resources(pdev[i], &res, 1);
1186 goto exit_device_put;
1189 /* platform_device_add calls probe() */
1190 err = platform_device_add(pdev[i]);
1192 goto exit_device_put;
1196 goto exit_unregister;
1202 platform_device_put(pdev[i]);
1203 exit_device_unregister:
1205 platform_device_unregister(pdev[i]);
1207 platform_driver_unregister(&nct6775_driver);
1211 static void __exit sensors_nct6775_platform_exit(void)
1215 for (i = 0; i < ARRAY_SIZE(pdev); i++)
1216 platform_device_unregister(pdev[i]);
1217 platform_driver_unregister(&nct6775_driver);
1220 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1221 MODULE_DESCRIPTION("Platform driver for NCT6775F and compatible chips");
1222 MODULE_LICENSE("GPL");
1223 MODULE_IMPORT_NS(HWMON_NCT6775);
1225 module_init(sensors_nct6775_platform_init);
1226 module_exit(sensors_nct6775_platform_exit);