Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwmon: (nct6775) Split core and platform driver

This splits the nct6775 driver into an interface-independent core and
a separate platform driver that wraps inb/outb port I/O (or asuswmi
methods) around that core.

Signed-off-by: Zev Weiss <zev@bewilderbeest.net>
Tested-by: Renze Nicolai <renze@rnplus.nl>
Link: https://lore.kernel.org/r/20220427010154.29749-7-zev@bewilderbeest.net
Tested-by: Oleksandr Natalenko <oleksandr@natalenko.name>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>

authored by

Zev Weiss and committed by
Guenter Roeck
c3963bc0 ae0d7227

+1526 -1433
+4 -2
MAINTAINERS
··· 13537 13537 S: Maintained 13538 13538 F: net/ncsi/ 13539 13539 13540 - NCT6775 HARDWARE MONITOR DRIVER 13540 + NCT6775 HARDWARE MONITOR DRIVER - CORE & PLATFORM DRIVER 13541 13541 M: Guenter Roeck <linux@roeck-us.net> 13542 13542 L: linux-hwmon@vger.kernel.org 13543 13543 S: Maintained 13544 13544 F: Documentation/hwmon/nct6775.rst 13545 - F: drivers/hwmon/nct6775.c 13545 + F: drivers/hwmon/nct6775-core.c 13546 + F: drivers/hwmon/nct6775-platform.c 13547 + F: drivers/hwmon/nct6775.h 13546 13548 13547 13549 NETDEVSIM 13548 13550 M: Jakub Kicinski <kuba@kernel.org>
+13 -1
drivers/hwmon/Kconfig
··· 1462 1462 This driver can also be built as a module. If so, the module 1463 1463 will be called nct6683. 1464 1464 1465 + config SENSORS_NCT6775_CORE 1466 + tristate 1467 + select REGMAP 1468 + help 1469 + This module contains common code shared by the platform and 1470 + i2c versions of the nct6775 driver; it is not useful on its 1471 + own. 1472 + 1473 + If built as a module, the module will be called 1474 + nct6775-core. 1475 + 1465 1476 config SENSORS_NCT6775 1466 - tristate "Nuvoton NCT6775F and compatibles" 1477 + tristate "Platform driver for Nuvoton NCT6775F and compatibles" 1467 1478 depends on !PPC 1468 1479 depends on ACPI_WMI || ACPI_WMI=n 1469 1480 select HWMON_VID 1481 + select SENSORS_NCT6775_CORE 1470 1482 help 1471 1483 If you say yes here you get support for the hardware monitoring 1472 1484 functionality of the Nuvoton NCT6106D, NCT6775F, NCT6776F, NCT6779D,
+2
drivers/hwmon/Makefile
··· 154 154 obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o 155 155 obj-$(CONFIG_SENSORS_MR75203) += mr75203.o 156 156 obj-$(CONFIG_SENSORS_NCT6683) += nct6683.o 157 + obj-$(CONFIG_SENSORS_NCT6775_CORE) += nct6775-core.o 158 + nct6775-objs := nct6775-platform.o 157 159 obj-$(CONFIG_SENSORS_NCT6775) += nct6775.o 158 160 obj-$(CONFIG_SENSORS_NCT7802) += nct7802.o 159 161 obj-$(CONFIG_SENSORS_NCT7904) += nct7904.o
+1226
drivers/hwmon/nct6775-platform.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * nct6775 - Platform driver for the hardware monitoring 4 + * functionality of Nuvoton NCT677x Super-I/O chips 5 + * 6 + * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net> 7 + */ 8 + 9 + #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 + 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> 16 + #include <linux/io.h> 17 + #include <linux/module.h> 18 + #include <linux/platform_device.h> 19 + #include <linux/regmap.h> 20 + #include <linux/wmi.h> 21 + 22 + #include "nct6775.h" 23 + 24 + enum sensor_access { access_direct, access_asuswmi }; 25 + 26 + static const char * const nct6775_sio_names[] __initconst = { 27 + "NCT6106D", 28 + "NCT6116D", 29 + "NCT6775F", 30 + "NCT6776D/F", 31 + "NCT6779D", 32 + "NCT6791D", 33 + "NCT6792D", 34 + "NCT6793D", 35 + "NCT6795D", 36 + "NCT6796D", 37 + "NCT6797D", 38 + "NCT6798D", 39 + }; 40 + 41 + static unsigned short force_id; 42 + module_param(force_id, ushort, 0); 43 + MODULE_PARM_DESC(force_id, "Override the detected device ID"); 44 + 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"); 48 + 49 + #define DRVNAME "nct6775" 50 + 51 + #define NCT6775_PORT_CHIPID 0x58 52 + 53 + /* 54 + * ISA constants 55 + */ 56 + 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 62 + 63 + /* 64 + * Super-I/O constants and functions 65 + */ 66 + 67 + #define NCT6775_LD_ACPI 0x0a 68 + #define NCT6775_LD_HWM 0x0b 69 + #define NCT6775_LD_VID 0x0d 70 + #define NCT6775_LD_12 0x12 71 + 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) */ 76 + 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 90 + 91 + /* 92 + * Control registers 93 + */ 94 + #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0 95 + 96 + struct nct6775_sio_data { 97 + int sioreg; 98 + int ld; 99 + enum kinds kind; 100 + enum sensor_access access; 101 + 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); 108 + }; 109 + 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 116 + 117 + static int nct6775_asuswmi_evaluate_method(u32 method_id, u8 bank, u8 reg, u8 val, u32 *retval) 118 + { 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 }; 123 + acpi_status status; 124 + union acpi_object *obj; 125 + u32 tmp = ASUSWMI_UNSUPPORTED_METHOD; 126 + 127 + status = wmi_evaluate_method(ASUSWMI_MONITORING_GUID, 0, 128 + method_id, &input, &output); 129 + 130 + if (ACPI_FAILURE(status)) 131 + return -EIO; 132 + 133 + obj = output.pointer; 134 + if (obj && obj->type == ACPI_TYPE_INTEGER) 135 + tmp = obj->integer.value; 136 + 137 + if (retval) 138 + *retval = tmp; 139 + 140 + kfree(obj); 141 + 142 + if (tmp == ASUSWMI_UNSUPPORTED_METHOD) 143 + return -ENODEV; 144 + return 0; 145 + #else 146 + return -EOPNOTSUPP; 147 + #endif 148 + } 149 + 150 + static inline int nct6775_asuswmi_write(u8 bank, u8 reg, u8 val) 151 + { 152 + return nct6775_asuswmi_evaluate_method(ASUSWMI_METHODID_WHWM, bank, 153 + reg, val, NULL); 154 + } 155 + 156 + static inline int nct6775_asuswmi_read(u8 bank, u8 reg, u8 *val) 157 + { 158 + u32 ret, tmp = 0; 159 + 160 + ret = nct6775_asuswmi_evaluate_method(ASUSWMI_METHODID_RHWM, bank, 161 + reg, 0, &tmp); 162 + *val = tmp; 163 + return ret; 164 + } 165 + 166 + static int superio_wmi_inb(struct nct6775_sio_data *sio_data, int reg) 167 + { 168 + int tmp = 0; 169 + 170 + nct6775_asuswmi_evaluate_method(ASUSWMI_METHODID_RSIO, sio_data->ld, 171 + reg, 0, &tmp); 172 + return tmp; 173 + } 174 + 175 + static void superio_wmi_outb(struct nct6775_sio_data *sio_data, int reg, int val) 176 + { 177 + nct6775_asuswmi_evaluate_method(ASUSWMI_METHODID_WSIO, sio_data->ld, 178 + reg, val, NULL); 179 + } 180 + 181 + static void superio_wmi_select(struct nct6775_sio_data *sio_data, int ld) 182 + { 183 + sio_data->ld = ld; 184 + } 185 + 186 + static int superio_wmi_enter(struct nct6775_sio_data *sio_data) 187 + { 188 + return 0; 189 + } 190 + 191 + static void superio_wmi_exit(struct nct6775_sio_data *sio_data) 192 + { 193 + } 194 + 195 + static void superio_outb(struct nct6775_sio_data *sio_data, int reg, int val) 196 + { 197 + int ioreg = sio_data->sioreg; 198 + 199 + outb(reg, ioreg); 200 + outb(val, ioreg + 1); 201 + } 202 + 203 + static int superio_inb(struct nct6775_sio_data *sio_data, int reg) 204 + { 205 + int ioreg = sio_data->sioreg; 206 + 207 + outb(reg, ioreg); 208 + return inb(ioreg + 1); 209 + } 210 + 211 + static void superio_select(struct nct6775_sio_data *sio_data, int ld) 212 + { 213 + int ioreg = sio_data->sioreg; 214 + 215 + outb(SIO_REG_LDSEL, ioreg); 216 + outb(ld, ioreg + 1); 217 + } 218 + 219 + static int superio_enter(struct nct6775_sio_data *sio_data) 220 + { 221 + int ioreg = sio_data->sioreg; 222 + 223 + /* 224 + * Try to reserve <ioreg> and <ioreg + 1> for exclusive access. 225 + */ 226 + if (!request_muxed_region(ioreg, 2, DRVNAME)) 227 + return -EBUSY; 228 + 229 + outb(0x87, ioreg); 230 + outb(0x87, ioreg); 231 + 232 + return 0; 233 + } 234 + 235 + static void superio_exit(struct nct6775_sio_data *sio_data) 236 + { 237 + int ioreg = sio_data->sioreg; 238 + 239 + outb(0xaa, ioreg); 240 + outb(0x02, ioreg); 241 + outb(0x02, ioreg + 1); 242 + release_region(ioreg, 2); 243 + } 244 + 245 + static inline void nct6775_wmi_set_bank(struct nct6775_data *data, u16 reg) 246 + { 247 + u8 bank = reg >> 8; 248 + 249 + data->bank = bank; 250 + } 251 + 252 + static int nct6775_wmi_reg_read(void *ctx, unsigned int reg, unsigned int *val) 253 + { 254 + struct nct6775_data *data = ctx; 255 + int err, word_sized = nct6775_reg_is_word_sized(data, reg); 256 + u8 tmp = 0; 257 + u16 res; 258 + 259 + nct6775_wmi_set_bank(data, reg); 260 + 261 + err = nct6775_asuswmi_read(data->bank, reg & 0xff, &tmp); 262 + if (err) 263 + return err; 264 + 265 + res = tmp; 266 + if (word_sized) { 267 + err = nct6775_asuswmi_read(data->bank, (reg & 0xff) + 1, &tmp); 268 + if (err) 269 + return err; 270 + 271 + res = (res << 8) + tmp; 272 + } 273 + *val = res; 274 + return 0; 275 + } 276 + 277 + static int nct6775_wmi_reg_write(void *ctx, unsigned int reg, unsigned int value) 278 + { 279 + struct nct6775_data *data = ctx; 280 + int res, word_sized = nct6775_reg_is_word_sized(data, reg); 281 + 282 + nct6775_wmi_set_bank(data, reg); 283 + 284 + if (word_sized) { 285 + res = nct6775_asuswmi_write(data->bank, reg & 0xff, value >> 8); 286 + if (res) 287 + return res; 288 + 289 + res = nct6775_asuswmi_write(data->bank, (reg & 0xff) + 1, value); 290 + } else { 291 + res = nct6775_asuswmi_write(data->bank, reg & 0xff, value); 292 + } 293 + 294 + return res; 295 + } 296 + 297 + /* 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. 302 + */ 303 + static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg) 304 + { 305 + u8 bank = reg >> 8; 306 + 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); 310 + data->bank = bank; 311 + } 312 + } 313 + 314 + static int nct6775_reg_read(void *ctx, unsigned int reg, unsigned int *val) 315 + { 316 + struct nct6775_data *data = ctx; 317 + int word_sized = nct6775_reg_is_word_sized(data, reg); 318 + 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); 322 + if (word_sized) { 323 + outb_p((reg & 0xff) + 1, 324 + data->addr + ADDR_REG_OFFSET); 325 + *val = (*val << 8) + inb_p(data->addr + DATA_REG_OFFSET); 326 + } 327 + return 0; 328 + } 329 + 330 + static int nct6775_reg_write(void *ctx, unsigned int reg, unsigned int value) 331 + { 332 + struct nct6775_data *data = ctx; 333 + int word_sized = nct6775_reg_is_word_sized(data, reg); 334 + 335 + nct6775_set_bank(data, reg); 336 + outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET); 337 + if (word_sized) { 338 + outb_p(value >> 8, data->addr + DATA_REG_OFFSET); 339 + outb_p((reg & 0xff) + 1, 340 + data->addr + ADDR_REG_OFFSET); 341 + } 342 + outb_p(value & 0xff, data->addr + DATA_REG_OFFSET); 343 + return 0; 344 + } 345 + 346 + static void nct6791_enable_io_mapping(struct nct6775_sio_data *sio_data) 347 + { 348 + int val; 349 + 350 + val = sio_data->sio_inb(sio_data, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE); 351 + if (val & 0x10) { 352 + pr_info("Enabling hardware monitor logical device mappings.\n"); 353 + sio_data->sio_outb(sio_data, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE, 354 + val & ~0x10); 355 + } 356 + } 357 + 358 + static int __maybe_unused nct6775_suspend(struct device *dev) 359 + { 360 + int err; 361 + u16 tmp; 362 + struct nct6775_data *data = dev_get_drvdata(dev); 363 + 364 + if (IS_ERR(data)) 365 + return PTR_ERR(data); 366 + 367 + mutex_lock(&data->update_lock); 368 + err = nct6775_read_value(data, data->REG_VBAT, &tmp); 369 + if (err) 370 + goto out; 371 + data->vbat = tmp; 372 + if (data->kind == nct6775) { 373 + err = nct6775_read_value(data, NCT6775_REG_FANDIV1, &tmp); 374 + if (err) 375 + goto out; 376 + data->fandiv1 = tmp; 377 + 378 + err = nct6775_read_value(data, NCT6775_REG_FANDIV2, &tmp); 379 + if (err) 380 + goto out; 381 + data->fandiv2 = tmp; 382 + } 383 + out: 384 + mutex_unlock(&data->update_lock); 385 + 386 + return err; 387 + } 388 + 389 + static int __maybe_unused nct6775_resume(struct device *dev) 390 + { 391 + struct nct6775_data *data = dev_get_drvdata(dev); 392 + struct nct6775_sio_data *sio_data = dev_get_platdata(dev); 393 + int i, j, err = 0; 394 + u8 reg; 395 + 396 + mutex_lock(&data->update_lock); 397 + data->bank = 0xff; /* Force initial bank selection */ 398 + 399 + err = sio_data->sio_enter(sio_data); 400 + if (err) 401 + goto abort; 402 + 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); 407 + 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); 413 + 414 + sio_data->sio_exit(sio_data); 415 + 416 + /* Restore limits */ 417 + for (i = 0; i < data->in_num; i++) { 418 + if (!(data->have_in & BIT(i))) 419 + continue; 420 + 421 + err = nct6775_write_value(data, data->REG_IN_MINMAX[0][i], data->in[i][1]); 422 + if (err) 423 + goto abort; 424 + err = nct6775_write_value(data, data->REG_IN_MINMAX[1][i], data->in[i][2]); 425 + if (err) 426 + goto abort; 427 + } 428 + 429 + for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) { 430 + if (!(data->has_fan_min & BIT(i))) 431 + continue; 432 + 433 + err = nct6775_write_value(data, data->REG_FAN_MIN[i], data->fan_min[i]); 434 + if (err) 435 + goto abort; 436 + } 437 + 438 + for (i = 0; i < NUM_TEMP; i++) { 439 + if (!(data->have_temp & BIT(i))) 440 + continue; 441 + 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], 445 + data->temp[j][i]); 446 + if (err) 447 + goto abort; 448 + } 449 + } 450 + 451 + /* Restore other settings */ 452 + err = nct6775_write_value(data, data->REG_VBAT, data->vbat); 453 + if (err) 454 + goto abort; 455 + if (data->kind == nct6775) { 456 + err = nct6775_write_value(data, NCT6775_REG_FANDIV1, data->fandiv1); 457 + if (err) 458 + goto abort; 459 + err = nct6775_write_value(data, NCT6775_REG_FANDIV2, data->fandiv2); 460 + } 461 + 462 + abort: 463 + /* Force re-reading all values */ 464 + data->valid = false; 465 + mutex_unlock(&data->update_lock); 466 + 467 + return err; 468 + } 469 + 470 + static SIMPLE_DEV_PM_OPS(nct6775_dev_pm_ops, nct6775_suspend, nct6775_resume); 471 + 472 + static void 473 + nct6775_check_fan_inputs(struct nct6775_data *data, struct nct6775_sio_data *sio_data) 474 + { 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; 479 + 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); 483 + 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); 487 + 488 + fan3pin = cr2c & BIT(6); 489 + pwm3pin = cr2c & BIT(7); 490 + 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; 496 + 497 + board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR); 498 + board_name = dmi_get_system_info(DMI_BOARD_NAME); 499 + 500 + if (board_name && board_vendor && 501 + !strcmp(board_vendor, "ASRock")) { 502 + /* 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. 506 + */ 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); 512 + } 513 + } 514 + } 515 + 516 + if (data->sio_reg_enable & 0x80) 517 + fan3pin = gpok; 518 + else 519 + fan3pin = !(sio_data->sio_inb(sio_data, 0x24) & 0x40); 520 + 521 + if (data->sio_reg_enable & 0x40) 522 + fan4pin = gpok; 523 + else 524 + fan4pin = sio_data->sio_inb(sio_data, 0x1C) & 0x01; 525 + 526 + if (data->sio_reg_enable & 0x20) 527 + fan5pin = gpok; 528 + else 529 + fan5pin = sio_data->sio_inb(sio_data, 0x1C) & 0x02; 530 + 531 + fan4min = fan4pin; 532 + pwm3pin = fan3pin; 533 + } else if (data->kind == nct6106) { 534 + int cr24 = sio_data->sio_inb(sio_data, 0x24); 535 + 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); 545 + 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) 551 + 552 + pwm3pin = fan3pin && (cr24 & 0x08); 553 + pwm4pin = fan4pin; 554 + pwm5pin = fan5pin; 555 + } else { 556 + /* 557 + * NCT6779D, NCT6791D, NCT6792D, NCT6793D, NCT6795D, NCT6796D, 558 + * NCT6797D, NCT6798D 559 + */ 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); 570 + int cre0; 571 + int creb; 572 + int cred; 573 + 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); 578 + 579 + fan3pin = !(cr1c & BIT(5)); 580 + fan4pin = !(cr1c & BIT(6)); 581 + fan5pin = !(cr1c & BIT(7)); 582 + 583 + pwm3pin = !(cr1c & BIT(0)); 584 + pwm4pin = !(cr1c & BIT(1)); 585 + pwm5pin = !(cr1c & BIT(2)); 586 + 587 + switch (data->kind) { 588 + case nct6791: 589 + fan6pin = cr2d & BIT(1); 590 + pwm6pin = cr2d & BIT(0); 591 + break; 592 + case nct6792: 593 + fan6pin = !dsw_en && (cr2d & BIT(1)); 594 + pwm6pin = !dsw_en && (cr2d & BIT(0)); 595 + break; 596 + case nct6793: 597 + fan5pin |= cr1b & BIT(5); 598 + fan5pin |= creb & BIT(5); 599 + 600 + fan6pin = !dsw_en && (cr2d & BIT(1)); 601 + fan6pin |= creb & BIT(3); 602 + 603 + pwm5pin |= cr2d & BIT(7); 604 + pwm5pin |= (creb & BIT(4)) && !(cr2a & BIT(0)); 605 + 606 + pwm6pin = !dsw_en && (cr2d & BIT(0)); 607 + pwm6pin |= creb & BIT(2); 608 + break; 609 + case nct6795: 610 + fan5pin |= cr1b & BIT(5); 611 + fan5pin |= creb & BIT(5); 612 + 613 + fan6pin = (cr2a & BIT(4)) && 614 + (!dsw_en || (cred & BIT(4))); 615 + fan6pin |= creb & BIT(3); 616 + 617 + pwm5pin |= cr2d & BIT(7); 618 + pwm5pin |= (creb & BIT(4)) && !(cr2a & BIT(0)); 619 + 620 + pwm6pin = (cr2a & BIT(3)) && (cred & BIT(2)); 621 + pwm6pin |= creb & BIT(2); 622 + break; 623 + case nct6796: 624 + fan5pin |= cr1b & BIT(5); 625 + fan5pin |= (cre0 & BIT(3)) && !(cr1b & BIT(0)); 626 + fan5pin |= creb & BIT(5); 627 + 628 + fan6pin = (cr2a & BIT(4)) && 629 + (!dsw_en || (cred & BIT(4))); 630 + fan6pin |= creb & BIT(3); 631 + 632 + fan7pin = !(cr2b & BIT(2)); 633 + 634 + pwm5pin |= cr2d & BIT(7); 635 + pwm5pin |= (cre0 & BIT(4)) && !(cr1b & BIT(0)); 636 + pwm5pin |= (creb & BIT(4)) && !(cr2a & BIT(0)); 637 + 638 + pwm6pin = (cr2a & BIT(3)) && (cred & BIT(2)); 639 + pwm6pin |= creb & BIT(2); 640 + 641 + pwm7pin = !(cr1d & (BIT(2) | BIT(3))); 642 + break; 643 + case nct6797: 644 + fan5pin |= !ddr4_en && (cr1b & BIT(5)); 645 + fan5pin |= creb & BIT(5); 646 + 647 + fan6pin = cr2a & BIT(4); 648 + fan6pin |= creb & BIT(3); 649 + 650 + fan7pin = cr1a & BIT(1); 651 + 652 + pwm5pin |= (creb & BIT(4)) && !(cr2a & BIT(0)); 653 + pwm5pin |= !ddr4_en && (cr2d & BIT(7)); 654 + 655 + pwm6pin = creb & BIT(2); 656 + pwm6pin |= cred & BIT(2); 657 + 658 + pwm7pin = cr1d & BIT(4); 659 + break; 660 + case nct6798: 661 + fan6pin = !(cr1b & BIT(0)) && (cre0 & BIT(3)); 662 + fan6pin |= cr2a & BIT(4); 663 + fan6pin |= creb & BIT(5); 664 + 665 + fan7pin = cr1b & BIT(5); 666 + fan7pin |= !(cr2b & BIT(2)); 667 + fan7pin |= creb & BIT(3); 668 + 669 + pwm6pin = !(cr1b & BIT(0)) && (cre0 & BIT(4)); 670 + pwm6pin |= !(cred & BIT(2)) && (cr2a & BIT(3)); 671 + pwm6pin |= (creb & BIT(4)) && !(cr2a & BIT(0)); 672 + 673 + pwm7pin = !(cr1d & (BIT(2) | BIT(3))); 674 + pwm7pin |= cr2d & BIT(7); 675 + pwm7pin |= creb & BIT(2); 676 + break; 677 + default: /* NCT6779D */ 678 + break; 679 + } 680 + 681 + fan4min = fan4pin; 682 + } 683 + 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); 691 + } 692 + 693 + static ssize_t 694 + cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf) 695 + { 696 + struct nct6775_data *data = dev_get_drvdata(dev); 697 + 698 + return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); 699 + } 700 + 701 + static DEVICE_ATTR_RO(cpu0_vid); 702 + 703 + /* Case open detection */ 704 + 705 + static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee }; 706 + static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 }; 707 + 708 + static ssize_t 709 + clear_caseopen(struct device *dev, struct device_attribute *attr, 710 + const char *buf, size_t count) 711 + { 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; 715 + unsigned long val; 716 + u8 reg; 717 + int ret; 718 + 719 + if (kstrtoul(buf, 10, &val) || val != 0) 720 + return -EINVAL; 721 + 722 + mutex_lock(&data->update_lock); 723 + 724 + /* 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. 728 + */ 729 + ret = sio_data->sio_enter(sio_data); 730 + if (ret) { 731 + count = ret; 732 + goto error; 733 + } 734 + 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); 742 + 743 + data->valid = false; /* Force cache refresh */ 744 + error: 745 + mutex_unlock(&data->update_lock); 746 + return count; 747 + } 748 + 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); 759 + 760 + static umode_t nct6775_other_is_visible(struct kobject *kobj, 761 + struct attribute *attr, int index) 762 + { 763 + struct device *dev = kobj_to_dev(kobj); 764 + struct nct6775_data *data = dev_get_drvdata(dev); 765 + 766 + if (index == 0 && !data->have_vid) 767 + return 0; 768 + 769 + if (index == 1 || index == 2) { 770 + if (data->ALARM_BITS[INTRUSION_ALARM_BASE + index - 1] < 0) 771 + return 0; 772 + } 773 + 774 + if (index == 3 || index == 4) { 775 + if (data->BEEP_BITS[INTRUSION_ALARM_BASE + index - 3] < 0) 776 + return 0; 777 + } 778 + 779 + return nct6775_attr_mode(data, attr); 780 + } 781 + 782 + /* 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. 786 + */ 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 */ 794 + 795 + NULL 796 + }; 797 + 798 + static const struct attribute_group nct6775_group_other = { 799 + .attrs = nct6775_attributes_other, 800 + .is_visible = nct6775_other_is_visible, 801 + }; 802 + 803 + static int nct6775_platform_probe_init(struct nct6775_data *data) 804 + { 805 + int err; 806 + u8 cr2a; 807 + struct nct6775_sio_data *sio_data = data->driver_data; 808 + 809 + err = sio_data->sio_enter(sio_data); 810 + if (err) 811 + return err; 812 + 813 + cr2a = sio_data->sio_inb(sio_data, 0x2a); 814 + switch (data->kind) { 815 + case nct6775: 816 + data->have_vid = (cr2a & 0x40); 817 + break; 818 + case nct6776: 819 + data->have_vid = (cr2a & 0x60) == 0x40; 820 + break; 821 + case nct6106: 822 + case nct6116: 823 + case nct6779: 824 + case nct6791: 825 + case nct6792: 826 + case nct6793: 827 + case nct6795: 828 + case nct6796: 829 + case nct6797: 830 + case nct6798: 831 + break; 832 + } 833 + 834 + /* 835 + * Read VID value 836 + * We can get the VID input values directly at logical device D 0xe3. 837 + */ 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(); 842 + } 843 + 844 + if (fan_debounce) { 845 + u8 tmp; 846 + 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) { 851 + case nct6106: 852 + case nct6116: 853 + tmp |= 0xe0; 854 + break; 855 + case nct6775: 856 + tmp |= 0x1e; 857 + break; 858 + case nct6776: 859 + case nct6779: 860 + tmp |= 0x3e; 861 + break; 862 + case nct6791: 863 + case nct6792: 864 + case nct6793: 865 + case nct6795: 866 + case nct6796: 867 + case nct6797: 868 + case nct6798: 869 + tmp |= 0x7e; 870 + break; 871 + } 872 + sio_data->sio_outb(sio_data, NCT6775_REG_CR_FAN_DEBOUNCE, 873 + tmp); 874 + pr_info("Enabled fan debounce for chip %s\n", data->name); 875 + } 876 + 877 + nct6775_check_fan_inputs(data, sio_data); 878 + 879 + sio_data->sio_exit(sio_data); 880 + 881 + return nct6775_add_attr_group(data, &nct6775_group_other); 882 + } 883 + 884 + static const struct regmap_config nct6775_regmap_config = { 885 + .reg_bits = 16, 886 + .val_bits = 16, 887 + .reg_read = nct6775_reg_read, 888 + .reg_write = nct6775_reg_write, 889 + }; 890 + 891 + static const struct regmap_config nct6775_wmi_regmap_config = { 892 + .reg_bits = 16, 893 + .val_bits = 16, 894 + .reg_read = nct6775_wmi_reg_read, 895 + .reg_write = nct6775_wmi_reg_write, 896 + }; 897 + 898 + static int nct6775_platform_probe(struct platform_device *pdev) 899 + { 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; 905 + 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)) 909 + return -EBUSY; 910 + } 911 + 912 + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 913 + if (!data) 914 + return -ENOMEM; 915 + 916 + data->kind = sio_data->kind; 917 + data->sioreg = sio_data->sioreg; 918 + 919 + if (sio_data->access == access_direct) { 920 + data->addr = res->start; 921 + regmapcfg = &nct6775_regmap_config; 922 + } else { 923 + regmapcfg = &nct6775_wmi_regmap_config; 924 + } 925 + 926 + platform_set_drvdata(pdev, data); 927 + 928 + data->driver_data = sio_data; 929 + data->driver_init = nct6775_platform_probe_init; 930 + 931 + return nct6775_probe(&pdev->dev, data, regmapcfg); 932 + } 933 + 934 + static struct platform_driver nct6775_driver = { 935 + .driver = { 936 + .name = DRVNAME, 937 + .pm = &nct6775_dev_pm_ops, 938 + }, 939 + .probe = nct6775_platform_probe, 940 + }; 941 + 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) 944 + { 945 + u16 val; 946 + int err; 947 + int addr; 948 + 949 + sio_data->access = access_direct; 950 + sio_data->sioreg = sioaddr; 951 + 952 + err = sio_data->sio_enter(sio_data); 953 + if (err) 954 + return err; 955 + 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) 959 + val = force_id; 960 + 961 + switch (val & SIO_ID_MASK) { 962 + case SIO_NCT6106_ID: 963 + sio_data->kind = nct6106; 964 + break; 965 + case SIO_NCT6116_ID: 966 + sio_data->kind = nct6116; 967 + break; 968 + case SIO_NCT6775_ID: 969 + sio_data->kind = nct6775; 970 + break; 971 + case SIO_NCT6776_ID: 972 + sio_data->kind = nct6776; 973 + break; 974 + case SIO_NCT6779_ID: 975 + sio_data->kind = nct6779; 976 + break; 977 + case SIO_NCT6791_ID: 978 + sio_data->kind = nct6791; 979 + break; 980 + case SIO_NCT6792_ID: 981 + sio_data->kind = nct6792; 982 + break; 983 + case SIO_NCT6793_ID: 984 + sio_data->kind = nct6793; 985 + break; 986 + case SIO_NCT6795_ID: 987 + sio_data->kind = nct6795; 988 + break; 989 + case SIO_NCT6796_ID: 990 + sio_data->kind = nct6796; 991 + break; 992 + case SIO_NCT6797_ID: 993 + sio_data->kind = nct6797; 994 + break; 995 + case SIO_NCT6798_ID: 996 + sio_data->kind = nct6798; 997 + break; 998 + default: 999 + if (val != 0xffff) 1000 + pr_debug("unsupported chip ID: 0x%04x\n", val); 1001 + sio_data->sio_exit(sio_data); 1002 + return -ENODEV; 1003 + } 1004 + 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; 1010 + if (addr == 0) { 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); 1013 + return -ENODEV; 1014 + } 1015 + 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); 1021 + } 1022 + 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); 1028 + 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); 1032 + 1033 + return addr; 1034 + } 1035 + 1036 + /* 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 1041 + */ 1042 + static struct platform_device *pdev[2]; 1043 + 1044 + static const char * const asus_wmi_boards[] = { 1045 + "ProArt X570-CREATOR WIFI", 1046 + "Pro B550M-C", 1047 + "Pro WS X570-ACE", 1048 + "PRIME B360-PLUS", 1049 + "PRIME B460-PLUS", 1050 + "PRIME B550-PLUS", 1051 + "PRIME B550M-A", 1052 + "PRIME B550M-A (WI-FI)", 1053 + "PRIME X570-P", 1054 + "PRIME X570-PRO", 1055 + "ROG CROSSHAIR VIII DARK HERO", 1056 + "ROG CROSSHAIR VIII FORMULA", 1057 + "ROG CROSSHAIR VIII HERO", 1058 + "ROG CROSSHAIR VIII IMPACT", 1059 + "ROG STRIX B550-A GAMING", 1060 + "ROG STRIX B550-E GAMING", 1061 + "ROG STRIX B550-F GAMING", 1062 + "ROG STRIX B550-F GAMING (WI-FI)", 1063 + "ROG STRIX B550-F GAMING WIFI II", 1064 + "ROG STRIX B550-I GAMING", 1065 + "ROG STRIX B550-XE GAMING (WI-FI)", 1066 + "ROG STRIX X570-E GAMING", 1067 + "ROG STRIX X570-F GAMING", 1068 + "ROG STRIX X570-I GAMING", 1069 + "ROG STRIX Z390-E GAMING", 1070 + "ROG STRIX Z390-F GAMING", 1071 + "ROG STRIX Z390-H GAMING", 1072 + "ROG STRIX Z390-I GAMING", 1073 + "ROG STRIX Z490-A GAMING", 1074 + "ROG STRIX Z490-E GAMING", 1075 + "ROG STRIX Z490-F GAMING", 1076 + "ROG STRIX Z490-G GAMING", 1077 + "ROG STRIX Z490-G GAMING (WI-FI)", 1078 + "ROG STRIX Z490-H GAMING", 1079 + "ROG STRIX Z490-I GAMING", 1080 + "TUF GAMING B550M-PLUS", 1081 + "TUF GAMING B550M-PLUS (WI-FI)", 1082 + "TUF GAMING B550-PLUS", 1083 + "TUF GAMING B550-PRO", 1084 + "TUF GAMING X570-PLUS", 1085 + "TUF GAMING X570-PLUS (WI-FI)", 1086 + "TUF GAMING X570-PRO (WI-FI)", 1087 + "TUF GAMING Z490-PLUS", 1088 + "TUF GAMING Z490-PLUS (WI-FI)", 1089 + }; 1090 + 1091 + static int __init sensors_nct6775_platform_init(void) 1092 + { 1093 + int i, err; 1094 + bool found = false; 1095 + int address; 1096 + struct resource res; 1097 + struct nct6775_sio_data sio_data; 1098 + int sioaddr[2] = { 0x2e, 0x4e }; 1099 + enum sensor_access access = access_direct; 1100 + const char *board_vendor, *board_name; 1101 + u8 tmp; 1102 + 1103 + err = platform_driver_register(&nct6775_driver); 1104 + if (err) 1105 + return err; 1106 + 1107 + board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR); 1108 + board_name = dmi_get_system_info(DMI_BOARD_NAME); 1109 + 1110 + if (board_name && board_vendor && 1111 + !strcmp(board_vendor, "ASUSTeK COMPUTER INC.")) { 1112 + err = match_string(asus_wmi_boards, ARRAY_SIZE(asus_wmi_boards), 1113 + board_name); 1114 + if (err >= 0) { 1115 + /* if reading chip id via WMI succeeds, use WMI */ 1116 + if (!nct6775_asuswmi_read(0, NCT6775_PORT_CHIPID, &tmp) && tmp) { 1117 + pr_info("Using Asus WMI to access %#x chip.\n", tmp); 1118 + access = access_asuswmi; 1119 + } else { 1120 + pr_err("Can't read ChipID by Asus WMI.\n"); 1121 + } 1122 + } 1123 + } 1124 + 1125 + /* 1126 + * initialize sio_data->kind and sio_data->sioreg. 1127 + * 1128 + * when Super-I/O functions move to a separate file, the Super-I/O 1129 + * driver will probe 0x2e and 0x4e and auto-detect the presence of a 1130 + * nct6775 hardware monitor, and call probe() 1131 + */ 1132 + for (i = 0; i < ARRAY_SIZE(pdev); i++) { 1133 + sio_data.sio_outb = superio_outb; 1134 + sio_data.sio_inb = superio_inb; 1135 + sio_data.sio_select = superio_select; 1136 + sio_data.sio_enter = superio_enter; 1137 + sio_data.sio_exit = superio_exit; 1138 + 1139 + address = nct6775_find(sioaddr[i], &sio_data); 1140 + if (address <= 0) 1141 + continue; 1142 + 1143 + found = true; 1144 + 1145 + sio_data.access = access; 1146 + 1147 + if (access == access_asuswmi) { 1148 + sio_data.sio_outb = superio_wmi_outb; 1149 + sio_data.sio_inb = superio_wmi_inb; 1150 + sio_data.sio_select = superio_wmi_select; 1151 + sio_data.sio_enter = superio_wmi_enter; 1152 + sio_data.sio_exit = superio_wmi_exit; 1153 + } 1154 + 1155 + pdev[i] = platform_device_alloc(DRVNAME, address); 1156 + if (!pdev[i]) { 1157 + err = -ENOMEM; 1158 + goto exit_device_unregister; 1159 + } 1160 + 1161 + err = platform_device_add_data(pdev[i], &sio_data, 1162 + sizeof(struct nct6775_sio_data)); 1163 + if (err) 1164 + goto exit_device_put; 1165 + 1166 + if (sio_data.access == access_direct) { 1167 + memset(&res, 0, sizeof(res)); 1168 + res.name = DRVNAME; 1169 + res.start = address + IOREGION_OFFSET; 1170 + res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1; 1171 + res.flags = IORESOURCE_IO; 1172 + 1173 + err = acpi_check_resource_conflict(&res); 1174 + if (err) { 1175 + platform_device_put(pdev[i]); 1176 + pdev[i] = NULL; 1177 + continue; 1178 + } 1179 + 1180 + err = platform_device_add_resources(pdev[i], &res, 1); 1181 + if (err) 1182 + goto exit_device_put; 1183 + } 1184 + 1185 + /* platform_device_add calls probe() */ 1186 + err = platform_device_add(pdev[i]); 1187 + if (err) 1188 + goto exit_device_put; 1189 + } 1190 + if (!found) { 1191 + err = -ENODEV; 1192 + goto exit_unregister; 1193 + } 1194 + 1195 + return 0; 1196 + 1197 + exit_device_put: 1198 + platform_device_put(pdev[i]); 1199 + exit_device_unregister: 1200 + while (--i >= 0) { 1201 + if (pdev[i]) 1202 + platform_device_unregister(pdev[i]); 1203 + } 1204 + exit_unregister: 1205 + platform_driver_unregister(&nct6775_driver); 1206 + return err; 1207 + } 1208 + 1209 + static void __exit sensors_nct6775_platform_exit(void) 1210 + { 1211 + int i; 1212 + 1213 + for (i = 0; i < ARRAY_SIZE(pdev); i++) { 1214 + if (pdev[i]) 1215 + platform_device_unregister(pdev[i]); 1216 + } 1217 + platform_driver_unregister(&nct6775_driver); 1218 + } 1219 + 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); 1224 + 1225 + module_init(sensors_nct6775_platform_init); 1226 + module_exit(sensors_nct6775_platform_exit);
+29 -1430
drivers/hwmon/nct6775.c drivers/hwmon/nct6775-core.c
··· 44 44 #include <linux/init.h> 45 45 #include <linux/slab.h> 46 46 #include <linux/jiffies.h> 47 - #include <linux/platform_device.h> 48 47 #include <linux/hwmon.h> 49 48 #include <linux/hwmon-sysfs.h> 50 - #include <linux/hwmon-vid.h> 51 49 #include <linux/err.h> 52 50 #include <linux/mutex.h> 53 - #include <linux/acpi.h> 54 51 #include <linux/bitops.h> 55 - #include <linux/dmi.h> 56 - #include <linux/io.h> 57 52 #include <linux/nospec.h> 58 53 #include <linux/regmap.h> 59 - #include <linux/wmi.h> 60 54 #include "lm75.h" 55 + #include "nct6775.h" 56 + 57 + #undef DEFAULT_SYMBOL_NAMESPACE 58 + #define DEFAULT_SYMBOL_NAMESPACE HWMON_NCT6775 61 59 62 60 #define USE_ALTERNATE 63 - 64 - enum kinds { nct6106, nct6116, nct6775, nct6776, nct6779, nct6791, nct6792, 65 - nct6793, nct6795, nct6796, nct6797, nct6798 }; 66 61 67 62 /* used to set data->name = nct6775_device_names[data->sio_kind] */ 68 63 static const char * const nct6775_device_names[] = { ··· 74 79 "nct6797", 75 80 "nct6798", 76 81 }; 77 - 78 - static const char * const nct6775_sio_names[] __initconst = { 79 - "NCT6106D", 80 - "NCT6116D", 81 - "NCT6775F", 82 - "NCT6776D/F", 83 - "NCT6779D", 84 - "NCT6791D", 85 - "NCT6792D", 86 - "NCT6793D", 87 - "NCT6795D", 88 - "NCT6796D", 89 - "NCT6797D", 90 - "NCT6798D", 91 - }; 92 - 93 - static unsigned short force_id; 94 - module_param(force_id, ushort, 0); 95 - MODULE_PARM_DESC(force_id, "Override the detected device ID"); 96 - 97 - static unsigned short fan_debounce; 98 - module_param(fan_debounce, ushort, 0); 99 - MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal"); 100 - 101 - #define DRVNAME "nct6775" 102 - 103 - /* 104 - * Super-I/O constants and functions 105 - */ 106 - 107 - #define NCT6775_LD_ACPI 0x0a 108 - #define NCT6775_LD_HWM 0x0b 109 - #define NCT6775_LD_VID 0x0d 110 - #define NCT6775_LD_12 0x12 111 - 112 - #define SIO_REG_LDSEL 0x07 /* Logical device select */ 113 - #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */ 114 - #define SIO_REG_ENABLE 0x30 /* Logical device enable */ 115 - #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */ 116 - 117 - #define SIO_NCT6106_ID 0xc450 118 - #define SIO_NCT6116_ID 0xd280 119 - #define SIO_NCT6775_ID 0xb470 120 - #define SIO_NCT6776_ID 0xc330 121 - #define SIO_NCT6779_ID 0xc560 122 - #define SIO_NCT6791_ID 0xc800 123 - #define SIO_NCT6792_ID 0xc910 124 - #define SIO_NCT6793_ID 0xd120 125 - #define SIO_NCT6795_ID 0xd350 126 - #define SIO_NCT6796_ID 0xd420 127 - #define SIO_NCT6797_ID 0xd450 128 - #define SIO_NCT6798_ID 0xd428 129 - #define SIO_ID_MASK 0xFFF8 130 - 131 - enum pwm_enable { off, manual, thermal_cruise, speed_cruise, sf3, sf4 }; 132 - enum sensor_access { access_direct, access_asuswmi }; 133 - 134 - struct nct6775_sio_data { 135 - int sioreg; 136 - int ld; 137 - enum kinds kind; 138 - enum sensor_access access; 139 - 140 - /* superio_() callbacks */ 141 - void (*sio_outb)(struct nct6775_sio_data *sio_data, int reg, int val); 142 - int (*sio_inb)(struct nct6775_sio_data *sio_data, int reg); 143 - void (*sio_select)(struct nct6775_sio_data *sio_data, int ld); 144 - int (*sio_enter)(struct nct6775_sio_data *sio_data); 145 - void (*sio_exit)(struct nct6775_sio_data *sio_data); 146 - }; 147 - 148 - #define ASUSWMI_MONITORING_GUID "466747A0-70EC-11DE-8A39-0800200C9A66" 149 - #define ASUSWMI_METHODID_RSIO 0x5253494F 150 - #define ASUSWMI_METHODID_WSIO 0x5753494F 151 - #define ASUSWMI_METHODID_RHWM 0x5248574D 152 - #define ASUSWMI_METHODID_WHWM 0x5748574D 153 - #define ASUSWMI_UNSUPPORTED_METHOD 0xFFFFFFFE 154 - 155 - static int nct6775_asuswmi_evaluate_method(u32 method_id, u8 bank, u8 reg, u8 val, u32 *retval) 156 - { 157 - #if IS_ENABLED(CONFIG_ACPI_WMI) 158 - u32 args = bank | (reg << 8) | (val << 16); 159 - struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; 160 - struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; 161 - acpi_status status; 162 - union acpi_object *obj; 163 - u32 tmp = ASUSWMI_UNSUPPORTED_METHOD; 164 - 165 - status = wmi_evaluate_method(ASUSWMI_MONITORING_GUID, 0, 166 - method_id, &input, &output); 167 - 168 - if (ACPI_FAILURE(status)) 169 - return -EIO; 170 - 171 - obj = output.pointer; 172 - if (obj && obj->type == ACPI_TYPE_INTEGER) 173 - tmp = obj->integer.value; 174 - 175 - if (retval) 176 - *retval = tmp; 177 - 178 - kfree(obj); 179 - 180 - if (tmp == ASUSWMI_UNSUPPORTED_METHOD) 181 - return -ENODEV; 182 - return 0; 183 - #else 184 - return -EOPNOTSUPP; 185 - #endif 186 - } 187 - 188 - static inline int nct6775_asuswmi_write(u8 bank, u8 reg, u8 val) 189 - { 190 - return nct6775_asuswmi_evaluate_method(ASUSWMI_METHODID_WHWM, bank, 191 - reg, val, NULL); 192 - } 193 - 194 - static inline int nct6775_asuswmi_read(u8 bank, u8 reg, u8 *val) 195 - { 196 - u32 ret, tmp = 0; 197 - 198 - ret = nct6775_asuswmi_evaluate_method(ASUSWMI_METHODID_RHWM, bank, 199 - reg, 0, &tmp); 200 - *val = tmp; 201 - return ret; 202 - } 203 - 204 - static int superio_wmi_inb(struct nct6775_sio_data *sio_data, int reg) 205 - { 206 - int tmp = 0; 207 - 208 - nct6775_asuswmi_evaluate_method(ASUSWMI_METHODID_RSIO, sio_data->ld, 209 - reg, 0, &tmp); 210 - return tmp; 211 - } 212 - 213 - static void superio_wmi_outb(struct nct6775_sio_data *sio_data, int reg, int val) 214 - { 215 - nct6775_asuswmi_evaluate_method(ASUSWMI_METHODID_WSIO, sio_data->ld, 216 - reg, val, NULL); 217 - } 218 - 219 - static void superio_wmi_select(struct nct6775_sio_data *sio_data, int ld) 220 - { 221 - sio_data->ld = ld; 222 - } 223 - 224 - static int superio_wmi_enter(struct nct6775_sio_data *sio_data) 225 - { 226 - return 0; 227 - } 228 - 229 - static void superio_wmi_exit(struct nct6775_sio_data *sio_data) 230 - { 231 - } 232 - 233 - static void superio_outb(struct nct6775_sio_data *sio_data, int reg, int val) 234 - { 235 - int ioreg = sio_data->sioreg; 236 - 237 - outb(reg, ioreg); 238 - outb(val, ioreg + 1); 239 - } 240 - 241 - static int superio_inb(struct nct6775_sio_data *sio_data, int reg) 242 - { 243 - int ioreg = sio_data->sioreg; 244 - 245 - outb(reg, ioreg); 246 - return inb(ioreg + 1); 247 - } 248 - 249 - static void superio_select(struct nct6775_sio_data *sio_data, int ld) 250 - { 251 - int ioreg = sio_data->sioreg; 252 - 253 - outb(SIO_REG_LDSEL, ioreg); 254 - outb(ld, ioreg + 1); 255 - } 256 - 257 - static int superio_enter(struct nct6775_sio_data *sio_data) 258 - { 259 - int ioreg = sio_data->sioreg; 260 - 261 - /* 262 - * Try to reserve <ioreg> and <ioreg + 1> for exclusive access. 263 - */ 264 - if (!request_muxed_region(ioreg, 2, DRVNAME)) 265 - return -EBUSY; 266 - 267 - outb(0x87, ioreg); 268 - outb(0x87, ioreg); 269 - 270 - return 0; 271 - } 272 - 273 - static void superio_exit(struct nct6775_sio_data *sio_data) 274 - { 275 - int ioreg = sio_data->sioreg; 276 - 277 - outb(0xaa, ioreg); 278 - outb(0x02, ioreg); 279 - outb(0x02, ioreg + 1); 280 - release_region(ioreg, 2); 281 - } 282 - 283 - /* 284 - * ISA constants 285 - */ 286 - 287 - #define IOREGION_ALIGNMENT (~7) 288 - #define IOREGION_OFFSET 5 289 - #define IOREGION_LENGTH 2 290 - #define ADDR_REG_OFFSET 0 291 - #define DATA_REG_OFFSET 1 292 - 293 - #define NCT6775_REG_BANK 0x4E 294 - #define NCT6775_REG_CONFIG 0x40 295 - #define NCT6775_PORT_CHIPID 0x58 296 - 297 - /* 298 - * Not currently used: 299 - * REG_MAN_ID has the value 0x5ca3 for all supported chips. 300 - * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model. 301 - * REG_MAN_ID is at port 0x4f 302 - * REG_CHIP_ID is at port 0x58 303 - */ 304 - 305 - #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/ 306 - #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */ 307 - #define NUM_TSI_TEMP 8 /* Max number of TSI temp register pairs */ 308 - 309 - #define NUM_REG_ALARM 7 /* Max number of alarm registers */ 310 - #define NUM_REG_BEEP 5 /* Max number of beep registers */ 311 - 312 - #define NUM_FAN 7 313 82 314 83 /* Common and NCT6775 specific data */ 315 84 ··· 93 334 #define NCT6775_REG_DIODE 0x5E 94 335 #define NCT6775_DIODE_MASK 0x02 95 336 96 - #define NCT6775_REG_FANDIV1 0x506 97 - #define NCT6775_REG_FANDIV2 0x507 98 - 99 - #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0 100 - 101 337 static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B }; 102 338 103 339 /* 0..15 voltages, 16..23 fans, 24..29 temperatures, 30..31 intrusion */ ··· 105 351 -1, -1, -1, /* unused */ 106 352 4, 5, 13, -1, -1, -1, /* temp1..temp6 */ 107 353 12, -1 }; /* intrusion0, intrusion1 */ 108 - 109 - #define FAN_ALARM_BASE 16 110 - #define TEMP_ALARM_BASE 24 111 - #define INTRUSION_ALARM_BASE 30 112 354 113 355 static const u16 NCT6775_REG_BEEP[NUM_REG_BEEP] = { 0x56, 0x57, 0x453, 0x4e }; 114 356 ··· 120 370 -1, -1, -1, /* unused */ 121 371 4, 5, 13, -1, -1, -1, /* temp1..temp6 */ 122 372 12, -1 }; /* intrusion0, intrusion1 */ 123 - 124 - #define BEEP_ENABLE_BASE 15 125 - 126 - static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee }; 127 - static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 }; 128 373 129 374 /* DC or PWM output fan configuration */ 130 375 static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 }; ··· 435 690 }; 436 691 437 692 /* NCT6791 specific data */ 438 - 439 - #define NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE 0x28 440 693 441 694 static const u16 NCT6791_REG_WEIGHT_TEMP_SEL[NUM_FAN] = { 0, 0x239 }; 442 695 static const u16 NCT6791_REG_WEIGHT_TEMP_STEP[NUM_FAN] = { 0, 0x23a }; ··· 935 1192 * Data structures and manipulation thereof 936 1193 */ 937 1194 938 - struct nct6775_data { 939 - int addr; /* IO base of hw monitor block */ 940 - struct nct6775_sio_data *sio_data; 941 - enum kinds kind; 942 - const char *name; 943 - 944 - const struct attribute_group *groups[7]; 945 - u8 num_groups; 946 - 947 - u16 reg_temp[5][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst, 948 - * 3=temp_crit, 4=temp_lcrit 949 - */ 950 - u8 temp_src[NUM_TEMP]; 951 - u16 reg_temp_config[NUM_TEMP]; 952 - const char * const *temp_label; 953 - u32 temp_mask; 954 - u32 virt_temp_mask; 955 - 956 - u16 REG_CONFIG; 957 - u16 REG_VBAT; 958 - u16 REG_DIODE; 959 - u8 DIODE_MASK; 960 - 961 - const s8 *ALARM_BITS; 962 - const s8 *BEEP_BITS; 963 - 964 - const u16 *REG_VIN; 965 - const u16 *REG_IN_MINMAX[2]; 966 - 967 - const u16 *REG_TARGET; 968 - const u16 *REG_FAN; 969 - const u16 *REG_FAN_MODE; 970 - const u16 *REG_FAN_MIN; 971 - const u16 *REG_FAN_PULSES; 972 - const u16 *FAN_PULSE_SHIFT; 973 - const u16 *REG_FAN_TIME[3]; 974 - 975 - const u16 *REG_TOLERANCE_H; 976 - 977 - const u8 *REG_PWM_MODE; 978 - const u8 *PWM_MODE_MASK; 979 - 980 - const u16 *REG_PWM[7]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor, 981 - * [3]=pwm_max, [4]=pwm_step, 982 - * [5]=weight_duty_step, [6]=weight_duty_base 983 - */ 984 - const u16 *REG_PWM_READ; 985 - 986 - const u16 *REG_CRITICAL_PWM_ENABLE; 987 - u8 CRITICAL_PWM_ENABLE_MASK; 988 - const u16 *REG_CRITICAL_PWM; 989 - 990 - const u16 *REG_AUTO_TEMP; 991 - const u16 *REG_AUTO_PWM; 992 - 993 - const u16 *REG_CRITICAL_TEMP; 994 - const u16 *REG_CRITICAL_TEMP_TOLERANCE; 995 - 996 - const u16 *REG_TEMP_SOURCE; /* temp register sources */ 997 - const u16 *REG_TEMP_SEL; 998 - const u16 *REG_WEIGHT_TEMP_SEL; 999 - const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=tolerance, 2=step */ 1000 - 1001 - const u16 *REG_TEMP_OFFSET; 1002 - 1003 - const u16 *REG_ALARM; 1004 - const u16 *REG_BEEP; 1005 - 1006 - const u16 *REG_TSI_TEMP; 1007 - 1008 - unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg); 1009 - unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg); 1010 - 1011 - struct mutex update_lock; 1012 - bool valid; /* true if following fields are valid */ 1013 - unsigned long last_updated; /* In jiffies */ 1014 - 1015 - /* Register values */ 1016 - u8 bank; /* current register bank */ 1017 - u8 in_num; /* number of in inputs we have */ 1018 - u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */ 1019 - unsigned int rpm[NUM_FAN]; 1020 - u16 fan_min[NUM_FAN]; 1021 - u8 fan_pulses[NUM_FAN]; 1022 - u8 fan_div[NUM_FAN]; 1023 - u8 has_pwm; 1024 - u8 has_fan; /* some fan inputs can be disabled */ 1025 - u8 has_fan_min; /* some fans don't have min register */ 1026 - bool has_fan_div; 1027 - 1028 - u8 num_temp_alarms; /* 2, 3, or 6 */ 1029 - u8 num_temp_beeps; /* 2, 3, or 6 */ 1030 - u8 temp_fixed_num; /* 3 or 6 */ 1031 - u8 temp_type[NUM_TEMP_FIXED]; 1032 - s8 temp_offset[NUM_TEMP_FIXED]; 1033 - s16 temp[5][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst, 1034 - * 3=temp_crit, 4=temp_lcrit */ 1035 - s16 tsi_temp[NUM_TSI_TEMP]; 1036 - u64 alarms; 1037 - u64 beeps; 1038 - 1039 - u8 pwm_num; /* number of pwm */ 1040 - u8 pwm_mode[NUM_FAN]; /* 0->DC variable voltage, 1041 - * 1->PWM variable duty cycle 1042 - */ 1043 - enum pwm_enable pwm_enable[NUM_FAN]; 1044 - /* 0->off 1045 - * 1->manual 1046 - * 2->thermal cruise mode (also called SmartFan I) 1047 - * 3->fan speed cruise mode 1048 - * 4->SmartFan III 1049 - * 5->enhanced variable thermal cruise (SmartFan IV) 1050 - */ 1051 - u8 pwm[7][NUM_FAN]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor, 1052 - * [3]=pwm_max, [4]=pwm_step, 1053 - * [5]=weight_duty_step, [6]=weight_duty_base 1054 - */ 1055 - 1056 - u8 target_temp[NUM_FAN]; 1057 - u8 target_temp_mask; 1058 - u32 target_speed[NUM_FAN]; 1059 - u32 target_speed_tolerance[NUM_FAN]; 1060 - u8 speed_tolerance_limit; 1061 - 1062 - u8 temp_tolerance[2][NUM_FAN]; 1063 - u8 tolerance_mask; 1064 - 1065 - u8 fan_time[3][NUM_FAN]; /* 0 = stop_time, 1 = step_up, 2 = step_down */ 1066 - 1067 - /* Automatic fan speed control registers */ 1068 - int auto_pwm_num; 1069 - u8 auto_pwm[NUM_FAN][7]; 1070 - u8 auto_temp[NUM_FAN][7]; 1071 - u8 pwm_temp_sel[NUM_FAN]; 1072 - u8 pwm_weight_temp_sel[NUM_FAN]; 1073 - u8 weight_temp[3][NUM_FAN]; /* 0->temp_step, 1->temp_step_tol, 1074 - * 2->temp_base 1075 - */ 1076 - 1077 - u8 vid; 1078 - u8 vrm; 1079 - 1080 - bool have_vid; 1081 - 1082 - u16 have_temp; 1083 - u16 have_temp_fixed; 1084 - u16 have_tsi_temp; 1085 - u16 have_in; 1086 - 1087 - /* Remember extra register values over suspend/resume */ 1088 - u8 vbat; 1089 - u8 fandiv1; 1090 - u8 fandiv2; 1091 - u8 sio_reg_enable; 1092 - 1093 - struct regmap *regmap; 1094 - bool read_only; 1095 - }; 1096 - 1097 1195 struct sensor_device_template { 1098 1196 struct device_attribute dev_attr; 1099 1197 union { ··· 989 1405 umode_t (*is_visible)(struct kobject *, struct attribute *, int); 990 1406 int base; 991 1407 }; 992 - 993 - static inline umode_t nct6775_attr_mode(struct nct6775_data *data, struct attribute *attr) 994 - { 995 - return data->read_only ? (attr->mode & ~0222) : attr->mode; 996 - } 997 - 998 - static int nct6775_add_attr_group(struct nct6775_data *data, const struct attribute_group *group) 999 - { 1000 - /* Need to leave a NULL terminator at the end of data->groups */ 1001 - if (data->num_groups == ARRAY_SIZE(data->groups) - 1) 1002 - return -ENOBUFS; 1003 - 1004 - data->groups[data->num_groups++] = group; 1005 - return 0; 1006 - } 1007 1408 1008 1409 static int nct6775_add_template_attr_group(struct device *dev, struct nct6775_data *data, 1009 1410 const struct sensor_template_group *tg, int repeat) ··· 1064 1495 return nct6775_add_attr_group(data, group); 1065 1496 } 1066 1497 1067 - static bool is_word_sized(struct nct6775_data *data, u16 reg) 1498 + bool nct6775_reg_is_word_sized(struct nct6775_data *data, u16 reg) 1068 1499 { 1069 1500 switch (data->kind) { 1070 1501 case nct6106: ··· 1121 1552 } 1122 1553 return false; 1123 1554 } 1124 - 1125 - static inline void nct6775_wmi_set_bank(struct nct6775_data *data, u16 reg) 1126 - { 1127 - u8 bank = reg >> 8; 1128 - 1129 - data->bank = bank; 1130 - } 1131 - 1132 - static int nct6775_wmi_reg_read(void *ctx, unsigned int reg, unsigned int *val) 1133 - { 1134 - struct nct6775_data *data = ctx; 1135 - int err, word_sized = is_word_sized(data, reg); 1136 - u8 tmp = 0; 1137 - u16 res; 1138 - 1139 - nct6775_wmi_set_bank(data, reg); 1140 - 1141 - err = nct6775_asuswmi_read(data->bank, reg & 0xff, &tmp); 1142 - if (err) 1143 - return err; 1144 - 1145 - res = tmp; 1146 - if (word_sized) { 1147 - err = nct6775_asuswmi_read(data->bank, (reg & 0xff) + 1, &tmp); 1148 - if (err) 1149 - return err; 1150 - 1151 - res = (res << 8) + tmp; 1152 - } 1153 - *val = res; 1154 - return 0; 1155 - } 1156 - 1157 - static inline int nct6775_read_value(struct nct6775_data *data, u16 reg, u16 *value) 1158 - { 1159 - unsigned int tmp; 1160 - int ret = regmap_read(data->regmap, reg, &tmp); 1161 - 1162 - if (!ret) 1163 - *value = tmp; 1164 - return ret; 1165 - } 1166 - 1167 - static int nct6775_wmi_reg_write(void *ctx, unsigned int reg, unsigned int value) 1168 - { 1169 - struct nct6775_data *data = ctx; 1170 - int res, word_sized = is_word_sized(data, reg); 1171 - 1172 - nct6775_wmi_set_bank(data, reg); 1173 - 1174 - if (word_sized) { 1175 - res = nct6775_asuswmi_write(data->bank, reg & 0xff, value >> 8); 1176 - if (res) 1177 - return res; 1178 - 1179 - res = nct6775_asuswmi_write(data->bank, (reg & 0xff) + 1, value); 1180 - } else { 1181 - res = nct6775_asuswmi_write(data->bank, reg & 0xff, value); 1182 - } 1183 - 1184 - return res; 1185 - } 1186 - 1187 - static inline int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value) 1188 - { 1189 - return regmap_write(data->regmap, reg, value); 1190 - } 1191 - 1192 - /* 1193 - * On older chips, only registers 0x50-0x5f are banked. 1194 - * On more recent chips, all registers are banked. 1195 - * Assume that is the case and set the bank number for each access. 1196 - * Cache the bank number so it only needs to be set if it changes. 1197 - */ 1198 - static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg) 1199 - { 1200 - u8 bank = reg >> 8; 1201 - 1202 - if (data->bank != bank) { 1203 - outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET); 1204 - outb_p(bank, data->addr + DATA_REG_OFFSET); 1205 - data->bank = bank; 1206 - } 1207 - } 1208 - 1209 - static int nct6775_reg_read(void *ctx, unsigned int reg, unsigned int *val) 1210 - { 1211 - struct nct6775_data *data = ctx; 1212 - int word_sized = is_word_sized(data, reg); 1213 - 1214 - nct6775_set_bank(data, reg); 1215 - outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET); 1216 - *val = inb_p(data->addr + DATA_REG_OFFSET); 1217 - if (word_sized) { 1218 - outb_p((reg & 0xff) + 1, 1219 - data->addr + ADDR_REG_OFFSET); 1220 - *val = (*val << 8) + inb_p(data->addr + DATA_REG_OFFSET); 1221 - } 1222 - return 0; 1223 - } 1224 - 1225 - static int nct6775_reg_write(void *ctx, unsigned int reg, unsigned int value) 1226 - { 1227 - struct nct6775_data *data = ctx; 1228 - int word_sized = is_word_sized(data, reg); 1229 - 1230 - nct6775_set_bank(data, reg); 1231 - outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET); 1232 - if (word_sized) { 1233 - outb_p(value >> 8, data->addr + DATA_REG_OFFSET); 1234 - outb_p((reg & 0xff) + 1, 1235 - data->addr + ADDR_REG_OFFSET); 1236 - } 1237 - outb_p(value & 0xff, data->addr + DATA_REG_OFFSET); 1238 - return 0; 1239 - } 1555 + EXPORT_SYMBOL_GPL(nct6775_reg_is_word_sized); 1240 1556 1241 1557 /* We left-align 8-bit temperature values to make the code simpler */ 1242 1558 static int nct6775_read_temp(struct nct6775_data *data, u16 reg, u16 *val) ··· 1132 1678 if (err) 1133 1679 return err; 1134 1680 1135 - if (!is_word_sized(data, reg)) 1681 + if (!nct6775_reg_is_word_sized(data, reg)) 1136 1682 *val <<= 8; 1137 1683 1138 1684 return 0; 1139 - } 1140 - 1141 - static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value) 1142 - { 1143 - if (!is_word_sized(data, reg)) 1144 - value >>= 8; 1145 - return nct6775_write_value(data, reg, value); 1146 1685 } 1147 1686 1148 1687 /* This function assumes that the caller holds data->update_lock */ ··· 1654 2207 return err ? : count; 1655 2208 } 1656 2209 1657 - static ssize_t 1658 - show_alarm(struct device *dev, struct device_attribute *attr, char *buf) 2210 + ssize_t 2211 + nct6775_show_alarm(struct device *dev, struct device_attribute *attr, char *buf) 1659 2212 { 1660 2213 struct nct6775_data *data = nct6775_update_device(dev); 1661 2214 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); ··· 1668 2221 return sprintf(buf, "%u\n", 1669 2222 (unsigned int)((data->alarms >> nr) & 0x01)); 1670 2223 } 2224 + EXPORT_SYMBOL_GPL(nct6775_show_alarm); 1671 2225 1672 2226 static int find_temp_source(struct nct6775_data *data, int index, int count) 1673 2227 { ··· 1711 2263 return sprintf(buf, "%u\n", alarm); 1712 2264 } 1713 2265 1714 - static ssize_t 1715 - show_beep(struct device *dev, struct device_attribute *attr, char *buf) 2266 + ssize_t 2267 + nct6775_show_beep(struct device *dev, struct device_attribute *attr, char *buf) 1716 2268 { 1717 2269 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 1718 2270 struct nct6775_data *data = nct6775_update_device(dev); ··· 1726 2278 return sprintf(buf, "%u\n", 1727 2279 (unsigned int)((data->beeps >> nr) & 0x01)); 1728 2280 } 2281 + EXPORT_SYMBOL_GPL(nct6775_show_beep); 1729 2282 1730 - static ssize_t 1731 - store_beep(struct device *dev, struct device_attribute *attr, const char *buf, 1732 - size_t count) 2283 + ssize_t 2284 + nct6775_store_beep(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 1733 2285 { 1734 2286 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 1735 2287 struct nct6775_data *data = dev_get_drvdata(dev); ··· 1754 2306 mutex_unlock(&data->update_lock); 1755 2307 return err ? : count; 1756 2308 } 2309 + EXPORT_SYMBOL_GPL(nct6775_store_beep); 1757 2310 1758 2311 static ssize_t 1759 2312 show_temp_beep(struct device *dev, struct device_attribute *attr, char *buf) ··· 1830 2381 } 1831 2382 1832 2383 SENSOR_TEMPLATE_2(in_input, "in%d_input", 0444, show_in_reg, NULL, 0, 0); 1833 - SENSOR_TEMPLATE(in_alarm, "in%d_alarm", 0444, show_alarm, NULL, 0); 1834 - SENSOR_TEMPLATE(in_beep, "in%d_beep", 0644, show_beep, store_beep, 0); 2384 + SENSOR_TEMPLATE(in_alarm, "in%d_alarm", 0444, nct6775_show_alarm, NULL, 0); 2385 + SENSOR_TEMPLATE(in_beep, "in%d_beep", 0644, nct6775_show_beep, nct6775_store_beep, 0); 1835 2386 SENSOR_TEMPLATE_2(in_min, "in%d_min", 0644, show_in_reg, store_in_reg, 0, 1); 1836 2387 SENSOR_TEMPLATE_2(in_max, "in%d_max", 0644, show_in_reg, store_in_reg, 0, 2); 1837 2388 ··· 2063 2614 } 2064 2615 2065 2616 SENSOR_TEMPLATE(fan_input, "fan%d_input", 0444, show_fan, NULL, 0); 2066 - SENSOR_TEMPLATE(fan_alarm, "fan%d_alarm", 0444, show_alarm, NULL, FAN_ALARM_BASE); 2067 - SENSOR_TEMPLATE(fan_beep, "fan%d_beep", 0644, show_beep, store_beep, FAN_ALARM_BASE); 2617 + SENSOR_TEMPLATE(fan_alarm, "fan%d_alarm", 0444, nct6775_show_alarm, NULL, FAN_ALARM_BASE); 2618 + SENSOR_TEMPLATE(fan_beep, "fan%d_beep", 0644, nct6775_show_beep, 2619 + nct6775_store_beep, FAN_ALARM_BASE); 2068 2620 SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", 0644, show_fan_pulses, store_fan_pulses, 0); 2069 2621 SENSOR_TEMPLATE(fan_min, "fan%d_min", 0644, show_fan_min, store_fan_min, 0); 2070 2622 SENSOR_TEMPLATE(fan_div, "fan%d_div", 0444, show_fan_div, NULL, 0); ··· 3307 3857 .base = 1, 3308 3858 }; 3309 3859 3310 - static ssize_t 3311 - cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf) 3312 - { 3313 - struct nct6775_data *data = dev_get_drvdata(dev); 3314 - 3315 - return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); 3316 - } 3317 - 3318 - static DEVICE_ATTR_RO(cpu0_vid); 3319 - 3320 - /* Case open detection */ 3321 - 3322 - static ssize_t 3323 - clear_caseopen(struct device *dev, struct device_attribute *attr, 3324 - const char *buf, size_t count) 3325 - { 3326 - struct nct6775_data *data = dev_get_drvdata(dev); 3327 - struct nct6775_sio_data *sio_data = data->sio_data; 3328 - int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE; 3329 - unsigned long val; 3330 - u8 reg; 3331 - int ret; 3332 - 3333 - if (kstrtoul(buf, 10, &val) || val != 0) 3334 - return -EINVAL; 3335 - 3336 - mutex_lock(&data->update_lock); 3337 - 3338 - /* 3339 - * Use CR registers to clear caseopen status. 3340 - * The CR registers are the same for all chips, and not all chips 3341 - * support clearing the caseopen status through "regular" registers. 3342 - */ 3343 - ret = sio_data->sio_enter(sio_data); 3344 - if (ret) { 3345 - count = ret; 3346 - goto error; 3347 - } 3348 - 3349 - sio_data->sio_select(sio_data, NCT6775_LD_ACPI); 3350 - reg = sio_data->sio_inb(sio_data, NCT6775_REG_CR_CASEOPEN_CLR[nr]); 3351 - reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr]; 3352 - sio_data->sio_outb(sio_data, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg); 3353 - reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr]; 3354 - sio_data->sio_outb(sio_data, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg); 3355 - sio_data->sio_exit(sio_data); 3356 - 3357 - data->valid = false; /* Force cache refresh */ 3358 - error: 3359 - mutex_unlock(&data->update_lock); 3360 - return count; 3361 - } 3362 - 3363 - static SENSOR_DEVICE_ATTR(intrusion0_alarm, 0644, show_alarm, clear_caseopen, INTRUSION_ALARM_BASE); 3364 - static SENSOR_DEVICE_ATTR(intrusion1_alarm, 0644, show_alarm, 3365 - clear_caseopen, INTRUSION_ALARM_BASE + 1); 3366 - static SENSOR_DEVICE_ATTR(intrusion0_beep, 0644, show_beep, store_beep, INTRUSION_ALARM_BASE); 3367 - static SENSOR_DEVICE_ATTR(intrusion1_beep, 0644, show_beep, store_beep, INTRUSION_ALARM_BASE + 1); 3368 - static SENSOR_DEVICE_ATTR(beep_enable, 0644, show_beep, store_beep, BEEP_ENABLE_BASE); 3369 - 3370 - static umode_t nct6775_other_is_visible(struct kobject *kobj, 3371 - struct attribute *attr, int index) 3372 - { 3373 - struct device *dev = kobj_to_dev(kobj); 3374 - struct nct6775_data *data = dev_get_drvdata(dev); 3375 - 3376 - if (index == 0 && !data->have_vid) 3377 - return 0; 3378 - 3379 - if (index == 1 || index == 2) { 3380 - if (data->ALARM_BITS[INTRUSION_ALARM_BASE + index - 1] < 0) 3381 - return 0; 3382 - } 3383 - 3384 - if (index == 3 || index == 4) { 3385 - if (data->BEEP_BITS[INTRUSION_ALARM_BASE + index - 3] < 0) 3386 - return 0; 3387 - } 3388 - 3389 - return nct6775_attr_mode(data, attr); 3390 - } 3391 - 3392 - /* 3393 - * nct6775_other_is_visible uses the index into the following array 3394 - * to determine if attributes should be created or not. 3395 - * Any change in order or content must be matched. 3396 - */ 3397 - static struct attribute *nct6775_attributes_other[] = { 3398 - &dev_attr_cpu0_vid.attr, /* 0 */ 3399 - &sensor_dev_attr_intrusion0_alarm.dev_attr.attr, /* 1 */ 3400 - &sensor_dev_attr_intrusion1_alarm.dev_attr.attr, /* 2 */ 3401 - &sensor_dev_attr_intrusion0_beep.dev_attr.attr, /* 3 */ 3402 - &sensor_dev_attr_intrusion1_beep.dev_attr.attr, /* 4 */ 3403 - &sensor_dev_attr_beep_enable.dev_attr.attr, /* 5 */ 3404 - 3405 - NULL 3406 - }; 3407 - 3408 - static const struct attribute_group nct6775_group_other = { 3409 - .attrs = nct6775_attributes_other, 3410 - .is_visible = nct6775_other_is_visible, 3411 - }; 3412 - 3413 3860 static inline int nct6775_init_device(struct nct6775_data *data) 3414 3861 { 3415 3862 int i, err; ··· 3367 4020 return 0; 3368 4021 } 3369 4022 3370 - static void 3371 - nct6775_check_fan_inputs(struct nct6775_data *data, struct nct6775_sio_data *sio_data) 3372 - { 3373 - bool fan3pin = false, fan4pin = false, fan4min = false; 3374 - bool fan5pin = false, fan6pin = false, fan7pin = false; 3375 - bool pwm3pin = false, pwm4pin = false, pwm5pin = false; 3376 - bool pwm6pin = false, pwm7pin = false; 3377 - 3378 - /* Store SIO_REG_ENABLE for use during resume */ 3379 - sio_data->sio_select(sio_data, NCT6775_LD_HWM); 3380 - data->sio_reg_enable = sio_data->sio_inb(sio_data, SIO_REG_ENABLE); 3381 - 3382 - /* fan4 and fan5 share some pins with the GPIO and serial flash */ 3383 - if (data->kind == nct6775) { 3384 - int cr2c = sio_data->sio_inb(sio_data, 0x2c); 3385 - 3386 - fan3pin = cr2c & BIT(6); 3387 - pwm3pin = cr2c & BIT(7); 3388 - 3389 - /* On NCT6775, fan4 shares pins with the fdc interface */ 3390 - fan4pin = !(sio_data->sio_inb(sio_data, 0x2A) & 0x80); 3391 - } else if (data->kind == nct6776) { 3392 - bool gpok = sio_data->sio_inb(sio_data, 0x27) & 0x80; 3393 - const char *board_vendor, *board_name; 3394 - 3395 - board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR); 3396 - board_name = dmi_get_system_info(DMI_BOARD_NAME); 3397 - 3398 - if (board_name && board_vendor && 3399 - !strcmp(board_vendor, "ASRock")) { 3400 - /* 3401 - * Auxiliary fan monitoring is not enabled on ASRock 3402 - * Z77 Pro4-M if booted in UEFI Ultra-FastBoot mode. 3403 - * Observed with BIOS version 2.00. 3404 - */ 3405 - if (!strcmp(board_name, "Z77 Pro4-M")) { 3406 - if ((data->sio_reg_enable & 0xe0) != 0xe0) { 3407 - data->sio_reg_enable |= 0xe0; 3408 - sio_data->sio_outb(sio_data, SIO_REG_ENABLE, 3409 - data->sio_reg_enable); 3410 - } 3411 - } 3412 - } 3413 - 3414 - if (data->sio_reg_enable & 0x80) 3415 - fan3pin = gpok; 3416 - else 3417 - fan3pin = !(sio_data->sio_inb(sio_data, 0x24) & 0x40); 3418 - 3419 - if (data->sio_reg_enable & 0x40) 3420 - fan4pin = gpok; 3421 - else 3422 - fan4pin = sio_data->sio_inb(sio_data, 0x1C) & 0x01; 3423 - 3424 - if (data->sio_reg_enable & 0x20) 3425 - fan5pin = gpok; 3426 - else 3427 - fan5pin = sio_data->sio_inb(sio_data, 0x1C) & 0x02; 3428 - 3429 - fan4min = fan4pin; 3430 - pwm3pin = fan3pin; 3431 - } else if (data->kind == nct6106) { 3432 - int cr24 = sio_data->sio_inb(sio_data, 0x24); 3433 - 3434 - fan3pin = !(cr24 & 0x80); 3435 - pwm3pin = cr24 & 0x08; 3436 - } else if (data->kind == nct6116) { 3437 - int cr1a = sio_data->sio_inb(sio_data, 0x1a); 3438 - int cr1b = sio_data->sio_inb(sio_data, 0x1b); 3439 - int cr24 = sio_data->sio_inb(sio_data, 0x24); 3440 - int cr2a = sio_data->sio_inb(sio_data, 0x2a); 3441 - int cr2b = sio_data->sio_inb(sio_data, 0x2b); 3442 - int cr2f = sio_data->sio_inb(sio_data, 0x2f); 3443 - 3444 - fan3pin = !(cr2b & 0x10); 3445 - fan4pin = (cr2b & 0x80) || // pin 1(2) 3446 - (!(cr2f & 0x10) && (cr1a & 0x04)); // pin 65(66) 3447 - fan5pin = (cr2b & 0x80) || // pin 126(127) 3448 - (!(cr1b & 0x03) && (cr2a & 0x02)); // pin 94(96) 3449 - 3450 - pwm3pin = fan3pin && (cr24 & 0x08); 3451 - pwm4pin = fan4pin; 3452 - pwm5pin = fan5pin; 3453 - } else { 3454 - /* 3455 - * NCT6779D, NCT6791D, NCT6792D, NCT6793D, NCT6795D, NCT6796D, 3456 - * NCT6797D, NCT6798D 3457 - */ 3458 - int cr1a = sio_data->sio_inb(sio_data, 0x1a); 3459 - int cr1b = sio_data->sio_inb(sio_data, 0x1b); 3460 - int cr1c = sio_data->sio_inb(sio_data, 0x1c); 3461 - int cr1d = sio_data->sio_inb(sio_data, 0x1d); 3462 - int cr2a = sio_data->sio_inb(sio_data, 0x2a); 3463 - int cr2b = sio_data->sio_inb(sio_data, 0x2b); 3464 - int cr2d = sio_data->sio_inb(sio_data, 0x2d); 3465 - int cr2f = sio_data->sio_inb(sio_data, 0x2f); 3466 - bool dsw_en = cr2f & BIT(3); 3467 - bool ddr4_en = cr2f & BIT(4); 3468 - int cre0; 3469 - int creb; 3470 - int cred; 3471 - 3472 - sio_data->sio_select(sio_data, NCT6775_LD_12); 3473 - cre0 = sio_data->sio_inb(sio_data, 0xe0); 3474 - creb = sio_data->sio_inb(sio_data, 0xeb); 3475 - cred = sio_data->sio_inb(sio_data, 0xed); 3476 - 3477 - fan3pin = !(cr1c & BIT(5)); 3478 - fan4pin = !(cr1c & BIT(6)); 3479 - fan5pin = !(cr1c & BIT(7)); 3480 - 3481 - pwm3pin = !(cr1c & BIT(0)); 3482 - pwm4pin = !(cr1c & BIT(1)); 3483 - pwm5pin = !(cr1c & BIT(2)); 3484 - 3485 - switch (data->kind) { 3486 - case nct6791: 3487 - fan6pin = cr2d & BIT(1); 3488 - pwm6pin = cr2d & BIT(0); 3489 - break; 3490 - case nct6792: 3491 - fan6pin = !dsw_en && (cr2d & BIT(1)); 3492 - pwm6pin = !dsw_en && (cr2d & BIT(0)); 3493 - break; 3494 - case nct6793: 3495 - fan5pin |= cr1b & BIT(5); 3496 - fan5pin |= creb & BIT(5); 3497 - 3498 - fan6pin = !dsw_en && (cr2d & BIT(1)); 3499 - fan6pin |= creb & BIT(3); 3500 - 3501 - pwm5pin |= cr2d & BIT(7); 3502 - pwm5pin |= (creb & BIT(4)) && !(cr2a & BIT(0)); 3503 - 3504 - pwm6pin = !dsw_en && (cr2d & BIT(0)); 3505 - pwm6pin |= creb & BIT(2); 3506 - break; 3507 - case nct6795: 3508 - fan5pin |= cr1b & BIT(5); 3509 - fan5pin |= creb & BIT(5); 3510 - 3511 - fan6pin = (cr2a & BIT(4)) && 3512 - (!dsw_en || (cred & BIT(4))); 3513 - fan6pin |= creb & BIT(3); 3514 - 3515 - pwm5pin |= cr2d & BIT(7); 3516 - pwm5pin |= (creb & BIT(4)) && !(cr2a & BIT(0)); 3517 - 3518 - pwm6pin = (cr2a & BIT(3)) && (cred & BIT(2)); 3519 - pwm6pin |= creb & BIT(2); 3520 - break; 3521 - case nct6796: 3522 - fan5pin |= cr1b & BIT(5); 3523 - fan5pin |= (cre0 & BIT(3)) && !(cr1b & BIT(0)); 3524 - fan5pin |= creb & BIT(5); 3525 - 3526 - fan6pin = (cr2a & BIT(4)) && 3527 - (!dsw_en || (cred & BIT(4))); 3528 - fan6pin |= creb & BIT(3); 3529 - 3530 - fan7pin = !(cr2b & BIT(2)); 3531 - 3532 - pwm5pin |= cr2d & BIT(7); 3533 - pwm5pin |= (cre0 & BIT(4)) && !(cr1b & BIT(0)); 3534 - pwm5pin |= (creb & BIT(4)) && !(cr2a & BIT(0)); 3535 - 3536 - pwm6pin = (cr2a & BIT(3)) && (cred & BIT(2)); 3537 - pwm6pin |= creb & BIT(2); 3538 - 3539 - pwm7pin = !(cr1d & (BIT(2) | BIT(3))); 3540 - break; 3541 - case nct6797: 3542 - fan5pin |= !ddr4_en && (cr1b & BIT(5)); 3543 - fan5pin |= creb & BIT(5); 3544 - 3545 - fan6pin = cr2a & BIT(4); 3546 - fan6pin |= creb & BIT(3); 3547 - 3548 - fan7pin = cr1a & BIT(1); 3549 - 3550 - pwm5pin |= (creb & BIT(4)) && !(cr2a & BIT(0)); 3551 - pwm5pin |= !ddr4_en && (cr2d & BIT(7)); 3552 - 3553 - pwm6pin = creb & BIT(2); 3554 - pwm6pin |= cred & BIT(2); 3555 - 3556 - pwm7pin = cr1d & BIT(4); 3557 - break; 3558 - case nct6798: 3559 - fan6pin = !(cr1b & BIT(0)) && (cre0 & BIT(3)); 3560 - fan6pin |= cr2a & BIT(4); 3561 - fan6pin |= creb & BIT(5); 3562 - 3563 - fan7pin = cr1b & BIT(5); 3564 - fan7pin |= !(cr2b & BIT(2)); 3565 - fan7pin |= creb & BIT(3); 3566 - 3567 - pwm6pin = !(cr1b & BIT(0)) && (cre0 & BIT(4)); 3568 - pwm6pin |= !(cred & BIT(2)) && (cr2a & BIT(3)); 3569 - pwm6pin |= (creb & BIT(4)) && !(cr2a & BIT(0)); 3570 - 3571 - pwm7pin = !(cr1d & (BIT(2) | BIT(3))); 3572 - pwm7pin |= cr2d & BIT(7); 3573 - pwm7pin |= creb & BIT(2); 3574 - break; 3575 - default: /* NCT6779D */ 3576 - break; 3577 - } 3578 - 3579 - fan4min = fan4pin; 3580 - } 3581 - 3582 - /* fan 1 and 2 (0x03) are always present */ 3583 - data->has_fan = 0x03 | (fan3pin << 2) | (fan4pin << 3) | 3584 - (fan5pin << 4) | (fan6pin << 5) | (fan7pin << 6); 3585 - data->has_fan_min = 0x03 | (fan3pin << 2) | (fan4min << 3) | 3586 - (fan5pin << 4) | (fan6pin << 5) | (fan7pin << 6); 3587 - data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | 3588 - (pwm5pin << 4) | (pwm6pin << 5) | (pwm7pin << 6); 3589 - } 3590 - 3591 4023 static int add_temp_sensors(struct nct6775_data *data, const u16 *regp, 3592 4024 int *available, int *mask) 3593 4025 { ··· 3398 4272 return 0; 3399 4273 } 3400 4274 3401 - static const struct regmap_config nct6775_regmap_config = { 3402 - .reg_bits = 16, 3403 - .val_bits = 16, 3404 - .reg_read = nct6775_reg_read, 3405 - .reg_write = nct6775_reg_write, 3406 - }; 3407 - 3408 - static const struct regmap_config nct6775_wmi_regmap_config = { 3409 - .reg_bits = 16, 3410 - .val_bits = 16, 3411 - .reg_read = nct6775_wmi_reg_read, 3412 - .reg_write = nct6775_wmi_reg_write, 3413 - }; 3414 - 3415 - static int nct6775_probe(struct platform_device *pdev) 4275 + int nct6775_probe(struct device *dev, struct nct6775_data *data, 4276 + const struct regmap_config *regmapcfg) 3416 4277 { 3417 - struct device *dev = &pdev->dev; 3418 - struct nct6775_sio_data *sio_data = dev_get_platdata(dev); 3419 - struct nct6775_data *data; 3420 - struct resource *res; 3421 4278 int i, s, err = 0; 3422 4279 int mask, available; 3423 4280 u16 src; ··· 3408 4299 const u16 *reg_temp_mon, *reg_temp_alternate, *reg_temp_crit; 3409 4300 const u16 *reg_temp_crit_l = NULL, *reg_temp_crit_h = NULL; 3410 4301 int num_reg_temp, num_reg_temp_mon, num_reg_tsi_temp; 3411 - u8 cr2a; 3412 4302 struct device *hwmon_dev; 3413 4303 struct sensor_template_group tsi_temp_tg; 3414 - const struct regmap_config *regmapcfg; 3415 - 3416 - if (sio_data->access == access_direct) { 3417 - res = platform_get_resource(pdev, IORESOURCE_IO, 0); 3418 - if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH, 3419 - DRVNAME)) 3420 - return -EBUSY; 3421 - } 3422 - 3423 - data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data), 3424 - GFP_KERNEL); 3425 - if (!data) 3426 - return -ENOMEM; 3427 - 3428 - data->kind = sio_data->kind; 3429 - data->sio_data = sio_data; 3430 - 3431 - if (sio_data->access == access_direct) { 3432 - data->addr = res->start; 3433 - regmapcfg = &nct6775_regmap_config; 3434 - } else { 3435 - regmapcfg = &nct6775_wmi_regmap_config; 3436 - } 3437 4304 3438 4305 data->regmap = devm_regmap_init(dev, NULL, data, regmapcfg); 3439 4306 if (IS_ERR(data->regmap)) ··· 3418 4333 mutex_init(&data->update_lock); 3419 4334 data->name = nct6775_device_names[data->kind]; 3420 4335 data->bank = 0xff; /* Force initial bank selection */ 3421 - platform_set_drvdata(pdev, data); 3422 4336 3423 4337 switch (data->kind) { 3424 4338 case nct6106: ··· 4152 5068 if (err) 4153 5069 return err; 4154 5070 4155 - err = sio_data->sio_enter(sio_data); 4156 - if (err) 4157 - return err; 4158 - 4159 - cr2a = sio_data->sio_inb(sio_data, 0x2a); 4160 - switch (data->kind) { 4161 - case nct6775: 4162 - data->have_vid = (cr2a & 0x40); 4163 - break; 4164 - case nct6776: 4165 - data->have_vid = (cr2a & 0x60) == 0x40; 4166 - break; 4167 - case nct6106: 4168 - case nct6116: 4169 - case nct6779: 4170 - case nct6791: 4171 - case nct6792: 4172 - case nct6793: 4173 - case nct6795: 4174 - case nct6796: 4175 - case nct6797: 4176 - case nct6798: 4177 - break; 5071 + if (data->driver_init) { 5072 + err = data->driver_init(data); 5073 + if (err) 5074 + return err; 4178 5075 } 4179 - 4180 - /* 4181 - * Read VID value 4182 - * We can get the VID input values directly at logical device D 0xe3. 4183 - */ 4184 - if (data->have_vid) { 4185 - sio_data->sio_select(sio_data, NCT6775_LD_VID); 4186 - data->vid = sio_data->sio_inb(sio_data, 0xe3); 4187 - data->vrm = vid_which_vrm(); 4188 - } 4189 - 4190 - if (fan_debounce) { 4191 - u8 tmp; 4192 - 4193 - sio_data->sio_select(sio_data, NCT6775_LD_HWM); 4194 - tmp = sio_data->sio_inb(sio_data, 4195 - NCT6775_REG_CR_FAN_DEBOUNCE); 4196 - switch (data->kind) { 4197 - case nct6106: 4198 - case nct6116: 4199 - tmp |= 0xe0; 4200 - break; 4201 - case nct6775: 4202 - tmp |= 0x1e; 4203 - break; 4204 - case nct6776: 4205 - case nct6779: 4206 - tmp |= 0x3e; 4207 - break; 4208 - case nct6791: 4209 - case nct6792: 4210 - case nct6793: 4211 - case nct6795: 4212 - case nct6796: 4213 - case nct6797: 4214 - case nct6798: 4215 - tmp |= 0x7e; 4216 - break; 4217 - } 4218 - sio_data->sio_outb(sio_data, NCT6775_REG_CR_FAN_DEBOUNCE, 4219 - tmp); 4220 - dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n", 4221 - data->name); 4222 - } 4223 - 4224 - nct6775_check_fan_inputs(data, sio_data); 4225 - 4226 - sio_data->sio_exit(sio_data); 4227 5076 4228 5077 /* Read fan clock dividers immediately */ 4229 5078 err = nct6775_init_fan_common(dev, data); ··· 4194 5177 return err; 4195 5178 } 4196 5179 4197 - err = nct6775_add_attr_group(data, &nct6775_group_other); 4198 - if (err) 4199 - return err; 4200 - 4201 5180 hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name, 4202 5181 data, data->groups); 4203 5182 return PTR_ERR_OR_ZERO(hwmon_dev); 4204 5183 } 4205 - 4206 - static void nct6791_enable_io_mapping(struct nct6775_sio_data *sio_data) 4207 - { 4208 - int val; 4209 - 4210 - val = sio_data->sio_inb(sio_data, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE); 4211 - if (val & 0x10) { 4212 - pr_info("Enabling hardware monitor logical device mappings.\n"); 4213 - sio_data->sio_outb(sio_data, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE, 4214 - val & ~0x10); 4215 - } 4216 - } 4217 - 4218 - static int __maybe_unused nct6775_suspend(struct device *dev) 4219 - { 4220 - int err; 4221 - u16 tmp; 4222 - struct nct6775_data *data = nct6775_update_device(dev); 4223 - 4224 - if (IS_ERR(data)) 4225 - return PTR_ERR(data); 4226 - 4227 - mutex_lock(&data->update_lock); 4228 - err = nct6775_read_value(data, data->REG_VBAT, &tmp); 4229 - if (err) 4230 - goto out; 4231 - data->vbat = tmp; 4232 - if (data->kind == nct6775) { 4233 - err = nct6775_read_value(data, NCT6775_REG_FANDIV1, &tmp); 4234 - if (err) 4235 - goto out; 4236 - data->fandiv1 = tmp; 4237 - 4238 - err = nct6775_read_value(data, NCT6775_REG_FANDIV2, &tmp); 4239 - if (err) 4240 - goto out; 4241 - data->fandiv2 = tmp; 4242 - } 4243 - out: 4244 - mutex_unlock(&data->update_lock); 4245 - 4246 - return err; 4247 - } 4248 - 4249 - static int __maybe_unused nct6775_resume(struct device *dev) 4250 - { 4251 - struct nct6775_data *data = dev_get_drvdata(dev); 4252 - struct nct6775_sio_data *sio_data = dev_get_platdata(dev); 4253 - int i, j, err = 0; 4254 - u8 reg; 4255 - 4256 - mutex_lock(&data->update_lock); 4257 - data->bank = 0xff; /* Force initial bank selection */ 4258 - 4259 - err = sio_data->sio_enter(sio_data); 4260 - if (err) 4261 - goto abort; 4262 - 4263 - sio_data->sio_select(sio_data, NCT6775_LD_HWM); 4264 - reg = sio_data->sio_inb(sio_data, SIO_REG_ENABLE); 4265 - if (reg != data->sio_reg_enable) 4266 - sio_data->sio_outb(sio_data, SIO_REG_ENABLE, data->sio_reg_enable); 4267 - 4268 - if (data->kind == nct6791 || data->kind == nct6792 || 4269 - data->kind == nct6793 || data->kind == nct6795 || 4270 - data->kind == nct6796 || data->kind == nct6797 || 4271 - data->kind == nct6798) 4272 - nct6791_enable_io_mapping(sio_data); 4273 - 4274 - sio_data->sio_exit(sio_data); 4275 - 4276 - /* Restore limits */ 4277 - for (i = 0; i < data->in_num; i++) { 4278 - if (!(data->have_in & BIT(i))) 4279 - continue; 4280 - 4281 - err = nct6775_write_value(data, data->REG_IN_MINMAX[0][i], data->in[i][1]); 4282 - if (err) 4283 - goto abort; 4284 - err = nct6775_write_value(data, data->REG_IN_MINMAX[1][i], data->in[i][2]); 4285 - if (err) 4286 - goto abort; 4287 - } 4288 - 4289 - for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) { 4290 - if (!(data->has_fan_min & BIT(i))) 4291 - continue; 4292 - 4293 - err = nct6775_write_value(data, data->REG_FAN_MIN[i], data->fan_min[i]); 4294 - if (err) 4295 - goto abort; 4296 - } 4297 - 4298 - for (i = 0; i < NUM_TEMP; i++) { 4299 - if (!(data->have_temp & BIT(i))) 4300 - continue; 4301 - 4302 - for (j = 1; j < ARRAY_SIZE(data->reg_temp); j++) 4303 - if (data->reg_temp[j][i]) { 4304 - err = nct6775_write_temp(data, data->reg_temp[j][i], 4305 - data->temp[j][i]); 4306 - if (err) 4307 - goto abort; 4308 - } 4309 - } 4310 - 4311 - /* Restore other settings */ 4312 - err = nct6775_write_value(data, data->REG_VBAT, data->vbat); 4313 - if (err) 4314 - goto abort; 4315 - if (data->kind == nct6775) { 4316 - err = nct6775_write_value(data, NCT6775_REG_FANDIV1, data->fandiv1); 4317 - if (err) 4318 - goto abort; 4319 - err = nct6775_write_value(data, NCT6775_REG_FANDIV2, data->fandiv2); 4320 - } 4321 - 4322 - abort: 4323 - /* Force re-reading all values */ 4324 - data->valid = false; 4325 - mutex_unlock(&data->update_lock); 4326 - 4327 - return err; 4328 - } 4329 - 4330 - static SIMPLE_DEV_PM_OPS(nct6775_dev_pm_ops, nct6775_suspend, nct6775_resume); 4331 - 4332 - static struct platform_driver nct6775_driver = { 4333 - .driver = { 4334 - .name = DRVNAME, 4335 - .pm = &nct6775_dev_pm_ops, 4336 - }, 4337 - .probe = nct6775_probe, 4338 - }; 4339 - 4340 - /* nct6775_find() looks for a '627 in the Super-I/O config space */ 4341 - static int __init nct6775_find(int sioaddr, struct nct6775_sio_data *sio_data) 4342 - { 4343 - u16 val; 4344 - int err; 4345 - int addr; 4346 - 4347 - sio_data->access = access_direct; 4348 - sio_data->sioreg = sioaddr; 4349 - 4350 - err = sio_data->sio_enter(sio_data); 4351 - if (err) 4352 - return err; 4353 - 4354 - val = (sio_data->sio_inb(sio_data, SIO_REG_DEVID) << 8) | 4355 - sio_data->sio_inb(sio_data, SIO_REG_DEVID + 1); 4356 - if (force_id && val != 0xffff) 4357 - val = force_id; 4358 - 4359 - switch (val & SIO_ID_MASK) { 4360 - case SIO_NCT6106_ID: 4361 - sio_data->kind = nct6106; 4362 - break; 4363 - case SIO_NCT6116_ID: 4364 - sio_data->kind = nct6116; 4365 - break; 4366 - case SIO_NCT6775_ID: 4367 - sio_data->kind = nct6775; 4368 - break; 4369 - case SIO_NCT6776_ID: 4370 - sio_data->kind = nct6776; 4371 - break; 4372 - case SIO_NCT6779_ID: 4373 - sio_data->kind = nct6779; 4374 - break; 4375 - case SIO_NCT6791_ID: 4376 - sio_data->kind = nct6791; 4377 - break; 4378 - case SIO_NCT6792_ID: 4379 - sio_data->kind = nct6792; 4380 - break; 4381 - case SIO_NCT6793_ID: 4382 - sio_data->kind = nct6793; 4383 - break; 4384 - case SIO_NCT6795_ID: 4385 - sio_data->kind = nct6795; 4386 - break; 4387 - case SIO_NCT6796_ID: 4388 - sio_data->kind = nct6796; 4389 - break; 4390 - case SIO_NCT6797_ID: 4391 - sio_data->kind = nct6797; 4392 - break; 4393 - case SIO_NCT6798_ID: 4394 - sio_data->kind = nct6798; 4395 - break; 4396 - default: 4397 - if (val != 0xffff) 4398 - pr_debug("unsupported chip ID: 0x%04x\n", val); 4399 - sio_data->sio_exit(sio_data); 4400 - return -ENODEV; 4401 - } 4402 - 4403 - /* We have a known chip, find the HWM I/O address */ 4404 - sio_data->sio_select(sio_data, NCT6775_LD_HWM); 4405 - val = (sio_data->sio_inb(sio_data, SIO_REG_ADDR) << 8) 4406 - | sio_data->sio_inb(sio_data, SIO_REG_ADDR + 1); 4407 - addr = val & IOREGION_ALIGNMENT; 4408 - if (addr == 0) { 4409 - pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n"); 4410 - sio_data->sio_exit(sio_data); 4411 - return -ENODEV; 4412 - } 4413 - 4414 - /* Activate logical device if needed */ 4415 - val = sio_data->sio_inb(sio_data, SIO_REG_ENABLE); 4416 - if (!(val & 0x01)) { 4417 - pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n"); 4418 - sio_data->sio_outb(sio_data, SIO_REG_ENABLE, val | 0x01); 4419 - } 4420 - 4421 - if (sio_data->kind == nct6791 || sio_data->kind == nct6792 || 4422 - sio_data->kind == nct6793 || sio_data->kind == nct6795 || 4423 - sio_data->kind == nct6796 || sio_data->kind == nct6797 || 4424 - sio_data->kind == nct6798) 4425 - nct6791_enable_io_mapping(sio_data); 4426 - 4427 - sio_data->sio_exit(sio_data); 4428 - pr_info("Found %s or compatible chip at %#x:%#x\n", 4429 - nct6775_sio_names[sio_data->kind], sioaddr, addr); 4430 - 4431 - return addr; 4432 - } 4433 - 4434 - /* 4435 - * when Super-I/O functions move to a separate file, the Super-I/O 4436 - * bus will manage the lifetime of the device and this module will only keep 4437 - * track of the nct6775 driver. But since we use platform_device_alloc(), we 4438 - * must keep track of the device 4439 - */ 4440 - static struct platform_device *pdev[2]; 4441 - 4442 - static const char * const asus_wmi_boards[] = { 4443 - "ProArt X570-CREATOR WIFI", 4444 - "Pro B550M-C", 4445 - "Pro WS X570-ACE", 4446 - "PRIME B360-PLUS", 4447 - "PRIME B460-PLUS", 4448 - "PRIME B550-PLUS", 4449 - "PRIME B550M-A", 4450 - "PRIME B550M-A (WI-FI)", 4451 - "PRIME X570-P", 4452 - "PRIME X570-PRO", 4453 - "ROG CROSSHAIR VIII DARK HERO", 4454 - "ROG CROSSHAIR VIII FORMULA", 4455 - "ROG CROSSHAIR VIII HERO", 4456 - "ROG CROSSHAIR VIII IMPACT", 4457 - "ROG STRIX B550-A GAMING", 4458 - "ROG STRIX B550-E GAMING", 4459 - "ROG STRIX B550-F GAMING", 4460 - "ROG STRIX B550-F GAMING (WI-FI)", 4461 - "ROG STRIX B550-F GAMING WIFI II", 4462 - "ROG STRIX B550-I GAMING", 4463 - "ROG STRIX B550-XE GAMING (WI-FI)", 4464 - "ROG STRIX X570-E GAMING", 4465 - "ROG STRIX X570-F GAMING", 4466 - "ROG STRIX X570-I GAMING", 4467 - "ROG STRIX Z390-E GAMING", 4468 - "ROG STRIX Z390-F GAMING", 4469 - "ROG STRIX Z390-H GAMING", 4470 - "ROG STRIX Z390-I GAMING", 4471 - "ROG STRIX Z490-A GAMING", 4472 - "ROG STRIX Z490-E GAMING", 4473 - "ROG STRIX Z490-F GAMING", 4474 - "ROG STRIX Z490-G GAMING", 4475 - "ROG STRIX Z490-G GAMING (WI-FI)", 4476 - "ROG STRIX Z490-H GAMING", 4477 - "ROG STRIX Z490-I GAMING", 4478 - "TUF GAMING B550M-PLUS", 4479 - "TUF GAMING B550M-PLUS (WI-FI)", 4480 - "TUF GAMING B550-PLUS", 4481 - "TUF GAMING B550-PRO", 4482 - "TUF GAMING X570-PLUS", 4483 - "TUF GAMING X570-PLUS (WI-FI)", 4484 - "TUF GAMING X570-PRO (WI-FI)", 4485 - "TUF GAMING Z490-PLUS", 4486 - "TUF GAMING Z490-PLUS (WI-FI)", 4487 - }; 4488 - 4489 - static int __init sensors_nct6775_init(void) 4490 - { 4491 - int i, err; 4492 - bool found = false; 4493 - int address; 4494 - struct resource res; 4495 - struct nct6775_sio_data sio_data; 4496 - int sioaddr[2] = { 0x2e, 0x4e }; 4497 - enum sensor_access access = access_direct; 4498 - const char *board_vendor, *board_name; 4499 - u8 tmp; 4500 - 4501 - err = platform_driver_register(&nct6775_driver); 4502 - if (err) 4503 - return err; 4504 - 4505 - board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR); 4506 - board_name = dmi_get_system_info(DMI_BOARD_NAME); 4507 - 4508 - if (board_name && board_vendor && 4509 - !strcmp(board_vendor, "ASUSTeK COMPUTER INC.")) { 4510 - err = match_string(asus_wmi_boards, ARRAY_SIZE(asus_wmi_boards), 4511 - board_name); 4512 - if (err >= 0) { 4513 - /* if reading chip id via WMI succeeds, use WMI */ 4514 - if (!nct6775_asuswmi_read(0, NCT6775_PORT_CHIPID, &tmp) && tmp) { 4515 - pr_info("Using Asus WMI to access %#x chip.\n", tmp); 4516 - access = access_asuswmi; 4517 - } else { 4518 - pr_err("Can't read ChipID by Asus WMI.\n"); 4519 - } 4520 - } 4521 - } 4522 - 4523 - /* 4524 - * initialize sio_data->kind and sio_data->sioreg. 4525 - * 4526 - * when Super-I/O functions move to a separate file, the Super-I/O 4527 - * driver will probe 0x2e and 0x4e and auto-detect the presence of a 4528 - * nct6775 hardware monitor, and call probe() 4529 - */ 4530 - for (i = 0; i < ARRAY_SIZE(pdev); i++) { 4531 - sio_data.sio_outb = superio_outb; 4532 - sio_data.sio_inb = superio_inb; 4533 - sio_data.sio_select = superio_select; 4534 - sio_data.sio_enter = superio_enter; 4535 - sio_data.sio_exit = superio_exit; 4536 - 4537 - address = nct6775_find(sioaddr[i], &sio_data); 4538 - if (address <= 0) 4539 - continue; 4540 - 4541 - found = true; 4542 - 4543 - sio_data.access = access; 4544 - 4545 - if (access == access_asuswmi) { 4546 - sio_data.sio_outb = superio_wmi_outb; 4547 - sio_data.sio_inb = superio_wmi_inb; 4548 - sio_data.sio_select = superio_wmi_select; 4549 - sio_data.sio_enter = superio_wmi_enter; 4550 - sio_data.sio_exit = superio_wmi_exit; 4551 - } 4552 - 4553 - pdev[i] = platform_device_alloc(DRVNAME, address); 4554 - if (!pdev[i]) { 4555 - err = -ENOMEM; 4556 - goto exit_device_unregister; 4557 - } 4558 - 4559 - err = platform_device_add_data(pdev[i], &sio_data, 4560 - sizeof(struct nct6775_sio_data)); 4561 - if (err) 4562 - goto exit_device_put; 4563 - 4564 - if (sio_data.access == access_direct) { 4565 - memset(&res, 0, sizeof(res)); 4566 - res.name = DRVNAME; 4567 - res.start = address + IOREGION_OFFSET; 4568 - res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1; 4569 - res.flags = IORESOURCE_IO; 4570 - 4571 - err = acpi_check_resource_conflict(&res); 4572 - if (err) { 4573 - platform_device_put(pdev[i]); 4574 - pdev[i] = NULL; 4575 - continue; 4576 - } 4577 - 4578 - err = platform_device_add_resources(pdev[i], &res, 1); 4579 - if (err) 4580 - goto exit_device_put; 4581 - } 4582 - 4583 - /* platform_device_add calls probe() */ 4584 - err = platform_device_add(pdev[i]); 4585 - if (err) 4586 - goto exit_device_put; 4587 - } 4588 - if (!found) { 4589 - err = -ENODEV; 4590 - goto exit_unregister; 4591 - } 4592 - 4593 - return 0; 4594 - 4595 - exit_device_put: 4596 - platform_device_put(pdev[i]); 4597 - exit_device_unregister: 4598 - while (--i >= 0) { 4599 - if (pdev[i]) 4600 - platform_device_unregister(pdev[i]); 4601 - } 4602 - exit_unregister: 4603 - platform_driver_unregister(&nct6775_driver); 4604 - return err; 4605 - } 4606 - 4607 - static void __exit sensors_nct6775_exit(void) 4608 - { 4609 - int i; 4610 - 4611 - for (i = 0; i < ARRAY_SIZE(pdev); i++) { 4612 - if (pdev[i]) 4613 - platform_device_unregister(pdev[i]); 4614 - } 4615 - platform_driver_unregister(&nct6775_driver); 4616 - } 5184 + EXPORT_SYMBOL_GPL(nct6775_probe); 4617 5185 4618 5186 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); 4619 - MODULE_DESCRIPTION("Driver for NCT6775F and compatible chips"); 5187 + MODULE_DESCRIPTION("Core driver for NCT6775F and compatible chips"); 4620 5188 MODULE_LICENSE("GPL"); 4621 - 4622 - module_init(sensors_nct6775_init); 4623 - module_exit(sensors_nct6775_exit);
+252
drivers/hwmon/nct6775.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + #ifndef __HWMON_NCT6775_H__ 3 + #define __HWMON_NCT6775_H__ 4 + 5 + #include <linux/types.h> 6 + 7 + enum kinds { nct6106, nct6116, nct6775, nct6776, nct6779, nct6791, nct6792, 8 + nct6793, nct6795, nct6796, nct6797, nct6798 }; 9 + enum pwm_enable { off, manual, thermal_cruise, speed_cruise, sf3, sf4 }; 10 + 11 + #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/ 12 + #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */ 13 + #define NUM_TSI_TEMP 8 /* Max number of TSI temp register pairs */ 14 + 15 + #define NUM_REG_ALARM 7 /* Max number of alarm registers */ 16 + #define NUM_REG_BEEP 5 /* Max number of beep registers */ 17 + 18 + #define NUM_FAN 7 19 + 20 + struct nct6775_data { 21 + int addr; /* IO base of hw monitor block */ 22 + int sioreg; /* SIO register address */ 23 + enum kinds kind; 24 + const char *name; 25 + 26 + const struct attribute_group *groups[7]; 27 + u8 num_groups; 28 + 29 + u16 reg_temp[5][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst, 30 + * 3=temp_crit, 4=temp_lcrit 31 + */ 32 + u8 temp_src[NUM_TEMP]; 33 + u16 reg_temp_config[NUM_TEMP]; 34 + const char * const *temp_label; 35 + u32 temp_mask; 36 + u32 virt_temp_mask; 37 + 38 + u16 REG_CONFIG; 39 + u16 REG_VBAT; 40 + u16 REG_DIODE; 41 + u8 DIODE_MASK; 42 + 43 + const s8 *ALARM_BITS; 44 + const s8 *BEEP_BITS; 45 + 46 + const u16 *REG_VIN; 47 + const u16 *REG_IN_MINMAX[2]; 48 + 49 + const u16 *REG_TARGET; 50 + const u16 *REG_FAN; 51 + const u16 *REG_FAN_MODE; 52 + const u16 *REG_FAN_MIN; 53 + const u16 *REG_FAN_PULSES; 54 + const u16 *FAN_PULSE_SHIFT; 55 + const u16 *REG_FAN_TIME[3]; 56 + 57 + const u16 *REG_TOLERANCE_H; 58 + 59 + const u8 *REG_PWM_MODE; 60 + const u8 *PWM_MODE_MASK; 61 + 62 + const u16 *REG_PWM[7]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor, 63 + * [3]=pwm_max, [4]=pwm_step, 64 + * [5]=weight_duty_step, [6]=weight_duty_base 65 + */ 66 + const u16 *REG_PWM_READ; 67 + 68 + const u16 *REG_CRITICAL_PWM_ENABLE; 69 + u8 CRITICAL_PWM_ENABLE_MASK; 70 + const u16 *REG_CRITICAL_PWM; 71 + 72 + const u16 *REG_AUTO_TEMP; 73 + const u16 *REG_AUTO_PWM; 74 + 75 + const u16 *REG_CRITICAL_TEMP; 76 + const u16 *REG_CRITICAL_TEMP_TOLERANCE; 77 + 78 + const u16 *REG_TEMP_SOURCE; /* temp register sources */ 79 + const u16 *REG_TEMP_SEL; 80 + const u16 *REG_WEIGHT_TEMP_SEL; 81 + const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=tolerance, 2=step */ 82 + 83 + const u16 *REG_TEMP_OFFSET; 84 + 85 + const u16 *REG_ALARM; 86 + const u16 *REG_BEEP; 87 + 88 + const u16 *REG_TSI_TEMP; 89 + 90 + unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg); 91 + unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg); 92 + 93 + struct mutex update_lock; 94 + bool valid; /* true if following fields are valid */ 95 + unsigned long last_updated; /* In jiffies */ 96 + 97 + /* Register values */ 98 + u8 bank; /* current register bank */ 99 + u8 in_num; /* number of in inputs we have */ 100 + u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */ 101 + unsigned int rpm[NUM_FAN]; 102 + u16 fan_min[NUM_FAN]; 103 + u8 fan_pulses[NUM_FAN]; 104 + u8 fan_div[NUM_FAN]; 105 + u8 has_pwm; 106 + u8 has_fan; /* some fan inputs can be disabled */ 107 + u8 has_fan_min; /* some fans don't have min register */ 108 + bool has_fan_div; 109 + 110 + u8 num_temp_alarms; /* 2, 3, or 6 */ 111 + u8 num_temp_beeps; /* 2, 3, or 6 */ 112 + u8 temp_fixed_num; /* 3 or 6 */ 113 + u8 temp_type[NUM_TEMP_FIXED]; 114 + s8 temp_offset[NUM_TEMP_FIXED]; 115 + s16 temp[5][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst, 116 + * 3=temp_crit, 4=temp_lcrit 117 + */ 118 + s16 tsi_temp[NUM_TSI_TEMP]; 119 + u64 alarms; 120 + u64 beeps; 121 + 122 + u8 pwm_num; /* number of pwm */ 123 + u8 pwm_mode[NUM_FAN]; /* 0->DC variable voltage, 124 + * 1->PWM variable duty cycle 125 + */ 126 + enum pwm_enable pwm_enable[NUM_FAN]; 127 + /* 0->off 128 + * 1->manual 129 + * 2->thermal cruise mode (also called SmartFan I) 130 + * 3->fan speed cruise mode 131 + * 4->SmartFan III 132 + * 5->enhanced variable thermal cruise (SmartFan IV) 133 + */ 134 + u8 pwm[7][NUM_FAN]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor, 135 + * [3]=pwm_max, [4]=pwm_step, 136 + * [5]=weight_duty_step, [6]=weight_duty_base 137 + */ 138 + 139 + u8 target_temp[NUM_FAN]; 140 + u8 target_temp_mask; 141 + u32 target_speed[NUM_FAN]; 142 + u32 target_speed_tolerance[NUM_FAN]; 143 + u8 speed_tolerance_limit; 144 + 145 + u8 temp_tolerance[2][NUM_FAN]; 146 + u8 tolerance_mask; 147 + 148 + u8 fan_time[3][NUM_FAN]; /* 0 = stop_time, 1 = step_up, 2 = step_down */ 149 + 150 + /* Automatic fan speed control registers */ 151 + int auto_pwm_num; 152 + u8 auto_pwm[NUM_FAN][7]; 153 + u8 auto_temp[NUM_FAN][7]; 154 + u8 pwm_temp_sel[NUM_FAN]; 155 + u8 pwm_weight_temp_sel[NUM_FAN]; 156 + u8 weight_temp[3][NUM_FAN]; /* 0->temp_step, 1->temp_step_tol, 157 + * 2->temp_base 158 + */ 159 + 160 + u8 vid; 161 + u8 vrm; 162 + 163 + bool have_vid; 164 + 165 + u16 have_temp; 166 + u16 have_temp_fixed; 167 + u16 have_tsi_temp; 168 + u16 have_in; 169 + 170 + /* Remember extra register values over suspend/resume */ 171 + u8 vbat; 172 + u8 fandiv1; 173 + u8 fandiv2; 174 + u8 sio_reg_enable; 175 + 176 + struct regmap *regmap; 177 + bool read_only; 178 + 179 + /* driver-specific (platform, i2c) initialization hook and data */ 180 + int (*driver_init)(struct nct6775_data *data); 181 + void *driver_data; 182 + }; 183 + 184 + static inline int nct6775_read_value(struct nct6775_data *data, u16 reg, u16 *value) 185 + { 186 + unsigned int tmp; 187 + int ret = regmap_read(data->regmap, reg, &tmp); 188 + 189 + if (!ret) 190 + *value = tmp; 191 + return ret; 192 + } 193 + 194 + static inline int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value) 195 + { 196 + return regmap_write(data->regmap, reg, value); 197 + } 198 + 199 + bool nct6775_reg_is_word_sized(struct nct6775_data *data, u16 reg); 200 + int nct6775_probe(struct device *dev, struct nct6775_data *data, 201 + const struct regmap_config *regmapcfg); 202 + 203 + ssize_t nct6775_show_alarm(struct device *dev, struct device_attribute *attr, char *buf); 204 + ssize_t nct6775_show_beep(struct device *dev, struct device_attribute *attr, char *buf); 205 + ssize_t nct6775_store_beep(struct device *dev, struct device_attribute *attr, const char *buf, 206 + size_t count); 207 + 208 + static inline int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value) 209 + { 210 + if (!nct6775_reg_is_word_sized(data, reg)) 211 + value >>= 8; 212 + return nct6775_write_value(data, reg, value); 213 + } 214 + 215 + static inline umode_t nct6775_attr_mode(struct nct6775_data *data, struct attribute *attr) 216 + { 217 + return data->read_only ? (attr->mode & ~0222) : attr->mode; 218 + } 219 + 220 + static inline int 221 + nct6775_add_attr_group(struct nct6775_data *data, const struct attribute_group *group) 222 + { 223 + /* Need to leave a NULL terminator at the end of data->groups */ 224 + if (data->num_groups == ARRAY_SIZE(data->groups) - 1) 225 + return -ENOBUFS; 226 + 227 + data->groups[data->num_groups++] = group; 228 + return 0; 229 + } 230 + 231 + #define NCT6775_REG_BANK 0x4E 232 + #define NCT6775_REG_CONFIG 0x40 233 + 234 + #define NCT6775_REG_FANDIV1 0x506 235 + #define NCT6775_REG_FANDIV2 0x507 236 + 237 + #define NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE 0x28 238 + 239 + #define FAN_ALARM_BASE 16 240 + #define TEMP_ALARM_BASE 24 241 + #define INTRUSION_ALARM_BASE 30 242 + #define BEEP_ENABLE_BASE 15 243 + 244 + /* 245 + * Not currently used: 246 + * REG_MAN_ID has the value 0x5ca3 for all supported chips. 247 + * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model. 248 + * REG_MAN_ID is at port 0x4f 249 + * REG_CHIP_ID is at port 0x58 250 + */ 251 + 252 + #endif /* __HWMON_NCT6775_H__ */