1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * nct6683 - Driver for the hardware monitoring functionality of 4 * Nuvoton NCT6683D/NCT6686D/NCT6687D eSIO 5 * 6 * Copyright (C) 2013 Guenter Roeck <linux@roeck-us.net> 7 * 8 * Derived from nct6775 driver 9 * Copyright (C) 2012, 2013 Guenter Roeck <linux@roeck-us.net> 10 * 11 * Supports the following chips: 12 * 13 * Chip #vin #fan #pwm #temp chip ID 14 * nct6683d 21(1) 16 8 32(1) 0xc730 15 * nct6686d 21(1) 16 8 32(1) 0xd440 16 * nct6687d 21(1) 16 8 32(1) 0xd590 17 * 18 * Notes: 19 * (1) Total number of vin and temp inputs is 32. 20 */ 21 22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 23 24 #include <linux/acpi.h> 25 #include <linux/delay.h> 26 #include <linux/err.h> 27 #include <linux/init.h> 28 #include <linux/io.h> 29 #include <linux/jiffies.h> 30 #include <linux/hwmon.h> 31 #include <linux/hwmon-sysfs.h> 32 #include <linux/module.h> 33 #include <linux/mutex.h> 34 #include <linux/platform_device.h> 35 #include <linux/slab.h> 36 37 enum kinds { nct6683, nct6686, nct6687 }; 38 39 static bool force; 40 module_param(force, bool, 0); 41 MODULE_PARM_DESC(force, "Set to one to enable support for unknown vendors"); 42 43 static const char * const nct6683_device_names[] = { 44 "nct6683", 45 "nct6686", 46 "nct6687", 47 }; 48 49 static const char * const nct6683_chip_names[] = { 50 "NCT6683D", 51 "NCT6686D", 52 "NCT6687D", 53 }; 54 55 #define DRVNAME "nct6683" 56 57 /* 58 * Super-I/O constants and functions 59 */ 60 61 #define NCT6683_LD_ACPI 0x0a 62 #define NCT6683_LD_HWM 0x0b 63 #define NCT6683_LD_VID 0x0d 64 65 #define SIO_REG_LDSEL 0x07 /* Logical device select */ 66 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */ 67 #define SIO_REG_ENABLE 0x30 /* Logical device enable */ 68 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */ 69 70 #define SIO_NCT6681_ID 0xb270 /* for later */ 71 #define SIO_NCT6683_ID 0xc730 72 #define SIO_NCT6686_ID 0xd440 73 #define SIO_NCT6687_ID 0xd590 74 #define SIO_ID_MASK 0xFFF0 75 76 static inline void 77 superio_outb(int ioreg, int reg, int val) 78 { 79 outb(reg, ioreg); 80 outb(val, ioreg + 1); 81 } 82 83 static inline int 84 superio_inb(int ioreg, int reg) 85 { 86 outb(reg, ioreg); 87 return inb(ioreg + 1); 88 } 89 90 static inline void 91 superio_select(int ioreg, int ld) 92 { 93 outb(SIO_REG_LDSEL, ioreg); 94 outb(ld, ioreg + 1); 95 } 96 97 static inline int 98 superio_enter(int ioreg) 99 { 100 /* 101 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access. 102 */ 103 if (!request_muxed_region(ioreg, 2, DRVNAME)) 104 return -EBUSY; 105 106 outb(0x87, ioreg); 107 outb(0x87, ioreg); 108 109 return 0; 110 } 111 112 static inline void 113 superio_exit(int ioreg) 114 { 115 outb(0xaa, ioreg); 116 outb(0x02, ioreg); 117 outb(0x02, ioreg + 1); 118 release_region(ioreg, 2); 119 } 120 121 /* 122 * ISA constants 123 */ 124 125 #define IOREGION_ALIGNMENT (~7) 126 #define IOREGION_OFFSET 4 /* Use EC port 1 */ 127 #define IOREGION_LENGTH 4 128 129 #define EC_PAGE_REG 0 130 #define EC_INDEX_REG 1 131 #define EC_DATA_REG 2 132 #define EC_EVENT_REG 3 133 134 /* Common and NCT6683 specific data */ 135 136 #define NCT6683_NUM_REG_MON 32 137 #define NCT6683_NUM_REG_FAN 16 138 #define NCT6683_NUM_REG_PWM 8 139 140 #define NCT6683_REG_MON(x) (0x100 + (x) * 2) 141 #define NCT6683_REG_FAN_RPM(x) (0x140 + (x) * 2) 142 #define NCT6683_REG_PWM(x) (0x160 + (x)) 143 #define NCT6683_REG_PWM_WRITE(x) (0xa28 + (x)) 144 145 #define NCT6683_REG_MON_STS(x) (0x174 + (x)) 146 #define NCT6683_REG_IDLE(x) (0x178 + (x)) 147 148 #define NCT6683_REG_FAN_STS(x) (0x17c + (x)) 149 #define NCT6683_REG_FAN_ERRSTS 0x17e 150 #define NCT6683_REG_FAN_INITSTS 0x17f 151 152 #define NCT6683_HWM_CFG 0x180 153 154 #define NCT6683_REG_MON_CFG(x) (0x1a0 + (x)) 155 #define NCT6683_REG_FANIN_CFG(x) (0x1c0 + (x)) 156 #define NCT6683_REG_FANOUT_CFG(x) (0x1d0 + (x)) 157 158 #define NCT6683_REG_INTEL_TEMP_MAX(x) (0x901 + (x) * 16) 159 #define NCT6683_REG_INTEL_TEMP_CRIT(x) (0x90d + (x) * 16) 160 161 #define NCT6683_REG_TEMP_HYST(x) (0x330 + (x)) /* 8 bit */ 162 #define NCT6683_REG_TEMP_MAX(x) (0x350 + (x)) /* 8 bit */ 163 #define NCT6683_REG_MON_HIGH(x) (0x370 + (x) * 2) /* 8 bit */ 164 #define NCT6683_REG_MON_LOW(x) (0x371 + (x) * 2) /* 8 bit */ 165 166 #define NCT6683_REG_FAN_MIN(x) (0x3b8 + (x) * 2) /* 16 bit */ 167 168 #define NCT6683_REG_FAN_CFG_CTRL 0xa01 169 #define NCT6683_FAN_CFG_REQ 0x80 170 #define NCT6683_FAN_CFG_DONE 0x40 171 172 #define NCT6683_REG_CUSTOMER_ID 0x602 173 #define NCT6683_CUSTOMER_ID_INTEL 0x805 174 #define NCT6683_CUSTOMER_ID_MITAC 0xa0e 175 #define NCT6683_CUSTOMER_ID_MSI 0x201 176 #define NCT6683_CUSTOMER_ID_MSI2 0x200 177 #define NCT6683_CUSTOMER_ID_MSI3 0x207 178 #define NCT6683_CUSTOMER_ID_MSI4 0x20d 179 #define NCT6683_CUSTOMER_ID_AMD 0x162b 180 #define NCT6683_CUSTOMER_ID_ASROCK 0xe2c 181 #define NCT6683_CUSTOMER_ID_ASROCK2 0xe1b 182 #define NCT6683_CUSTOMER_ID_ASROCK3 0x1631 183 #define NCT6683_CUSTOMER_ID_ASROCK4 0x163e 184 #define NCT6683_CUSTOMER_ID_ASROCK5 0x1621 185 #define NCT6683_CUSTOMER_ID_ASROCK6 0x1633 186 187 #define NCT6683_REG_BUILD_YEAR 0x604 188 #define NCT6683_REG_BUILD_MONTH 0x605 189 #define NCT6683_REG_BUILD_DAY 0x606 190 #define NCT6683_REG_SERIAL 0x607 191 #define NCT6683_REG_VERSION_HI 0x608 192 #define NCT6683_REG_VERSION_LO 0x609 193 194 #define NCT6683_REG_CR_CASEOPEN 0xe8 195 #define NCT6683_CR_CASEOPEN_MASK (1 << 7) 196 197 #define NCT6683_REG_CR_BEEP 0xe0 198 #define NCT6683_CR_BEEP_MASK (1 << 6) 199 200 static const char *const nct6683_mon_label[] = { 201 NULL, /* disabled */ 202 "Local", 203 "Diode 0 (curr)", 204 "Diode 1 (curr)", 205 "Diode 2 (curr)", 206 "Diode 0 (volt)", 207 "Diode 1 (volt)", 208 "Diode 2 (volt)", 209 "Thermistor 14", 210 "Thermistor 15", 211 "Thermistor 16", 212 "Thermistor 0", 213 "Thermistor 1", 214 "Thermistor 2", 215 "Thermistor 3", 216 "Thermistor 4", 217 "Thermistor 5", /* 0x10 */ 218 "Thermistor 6", 219 "Thermistor 7", 220 "Thermistor 8", 221 "Thermistor 9", 222 "Thermistor 10", 223 "Thermistor 11", 224 "Thermistor 12", 225 "Thermistor 13", 226 NULL, NULL, NULL, NULL, NULL, NULL, NULL, 227 "PECI 0.0", /* 0x20 */ 228 "PECI 1.0", 229 "PECI 2.0", 230 "PECI 3.0", 231 "PECI 0.1", 232 "PECI 1.1", 233 "PECI 2.1", 234 "PECI 3.1", 235 "PECI DIMM 0", 236 "PECI DIMM 1", 237 "PECI DIMM 2", 238 "PECI DIMM 3", 239 NULL, NULL, NULL, NULL, 240 "PCH CPU", /* 0x30 */ 241 "PCH CHIP", 242 "PCH CHIP CPU MAX", 243 "PCH MCH", 244 "PCH DIMM 0", 245 "PCH DIMM 1", 246 "PCH DIMM 2", 247 "PCH DIMM 3", 248 "SMBus 0", 249 "SMBus 1", 250 "SMBus 2", 251 "SMBus 3", 252 "SMBus 4", 253 "SMBus 5", 254 "DIMM 0", 255 "DIMM 1", 256 "DIMM 2", /* 0x40 */ 257 "DIMM 3", 258 "AMD TSI Addr 90h", 259 "AMD TSI Addr 92h", 260 "AMD TSI Addr 94h", 261 "AMD TSI Addr 96h", 262 "AMD TSI Addr 98h", 263 "AMD TSI Addr 9ah", 264 "AMD TSI Addr 9ch", 265 "AMD TSI Addr 9dh", 266 NULL, NULL, NULL, NULL, NULL, NULL, 267 "Virtual 0", /* 0x50 */ 268 "Virtual 1", 269 "Virtual 2", 270 "Virtual 3", 271 "Virtual 4", 272 "Virtual 5", 273 "Virtual 6", 274 "Virtual 7", 275 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 276 "VCC", /* 0x60 voltage sensors */ 277 "VSB", 278 "AVSB", 279 "VTT", 280 "VBAT", 281 "VREF", 282 "VIN0", 283 "VIN1", 284 "VIN2", 285 "VIN3", 286 "VIN4", 287 "VIN5", 288 "VIN6", 289 "VIN7", 290 "VIN8", 291 "VIN9", 292 "VIN10", 293 "VIN11", 294 "VIN12", 295 "VIN13", 296 "VIN14", 297 "VIN15", 298 "VIN16", 299 }; 300 301 #define NUM_MON_LABELS ARRAY_SIZE(nct6683_mon_label) 302 #define MON_VOLTAGE_START 0x60 303 304 /* ------------------------------------------------------- */ 305 306 struct nct6683_data { 307 int addr; /* IO base of EC space */ 308 int sioreg; /* SIO register */ 309 enum kinds kind; 310 u16 customer_id; 311 312 struct device *hwmon_dev; 313 const struct attribute_group *groups[6]; 314 315 int temp_num; /* number of temperature attributes */ 316 u8 temp_index[NCT6683_NUM_REG_MON]; 317 u8 temp_src[NCT6683_NUM_REG_MON]; 318 319 u8 in_num; /* number of voltage attributes */ 320 u8 in_index[NCT6683_NUM_REG_MON]; 321 u8 in_src[NCT6683_NUM_REG_MON]; 322 323 struct mutex update_lock; /* used to protect sensor updates */ 324 bool valid; /* true if following fields are valid */ 325 unsigned long last_updated; /* In jiffies */ 326 327 /* Voltage attribute values */ 328 u8 in[3][NCT6683_NUM_REG_MON]; /* [0]=in, [1]=in_max, [2]=in_min */ 329 330 /* Temperature attribute values */ 331 s16 temp_in[NCT6683_NUM_REG_MON]; 332 s8 temp[4][NCT6683_NUM_REG_MON];/* [0]=min, [1]=max, [2]=hyst, 333 * [3]=crit 334 */ 335 336 /* Fan attribute values */ 337 unsigned int rpm[NCT6683_NUM_REG_FAN]; 338 u16 fan_min[NCT6683_NUM_REG_FAN]; 339 u8 fanin_cfg[NCT6683_NUM_REG_FAN]; 340 u8 fanout_cfg[NCT6683_NUM_REG_FAN]; 341 u16 have_fan; /* some fan inputs can be disabled */ 342 343 u8 have_pwm; 344 u8 pwm[NCT6683_NUM_REG_PWM]; 345 346 #ifdef CONFIG_PM 347 /* Remember extra register values over suspend/resume */ 348 u8 hwm_cfg; 349 #endif 350 }; 351 352 struct nct6683_sio_data { 353 int sioreg; 354 enum kinds kind; 355 }; 356 357 struct sensor_device_template { 358 struct device_attribute dev_attr; 359 union { 360 struct { 361 u8 nr; 362 u8 index; 363 } s; 364 int index; 365 } u; 366 bool s2; /* true if both index and nr are used */ 367 }; 368 369 struct sensor_device_attr_u { 370 union { 371 struct sensor_device_attribute a1; 372 struct sensor_device_attribute_2 a2; 373 } u; 374 char name[32]; 375 }; 376 377 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \ 378 .attr = {.name = _template, .mode = _mode }, \ 379 .show = _show, \ 380 .store = _store, \ 381 } 382 383 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \ 384 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \ 385 .u.index = _index, \ 386 .s2 = false } 387 388 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \ 389 _nr, _index) \ 390 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \ 391 .u.s.index = _index, \ 392 .u.s.nr = _nr, \ 393 .s2 = true } 394 395 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \ 396 static struct sensor_device_template sensor_dev_template_##_name \ 397 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \ 398 _index) 399 400 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \ 401 _nr, _index) \ 402 static struct sensor_device_template sensor_dev_template_##_name \ 403 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \ 404 _nr, _index) 405 406 struct sensor_template_group { 407 struct sensor_device_template **templates; 408 umode_t (*is_visible)(struct kobject *, struct attribute *, int); 409 int base; 410 }; 411 412 static struct attribute_group * 413 nct6683_create_attr_group(struct device *dev, 414 const struct sensor_template_group *tg, 415 int repeat) 416 { 417 struct sensor_device_attribute_2 *a2; 418 struct sensor_device_attribute *a; 419 struct sensor_device_template **t; 420 struct sensor_device_attr_u *su; 421 struct attribute_group *group; 422 struct attribute **attrs; 423 int i, count; 424 425 if (repeat <= 0) 426 return ERR_PTR(-EINVAL); 427 428 t = tg->templates; 429 for (count = 0; *t; t++, count++) 430 ; 431 432 if (count == 0) 433 return ERR_PTR(-EINVAL); 434 435 group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL); 436 if (group == NULL) 437 return ERR_PTR(-ENOMEM); 438 439 attrs = devm_kcalloc(dev, repeat * count + 1, sizeof(*attrs), 440 GFP_KERNEL); 441 if (attrs == NULL) 442 return ERR_PTR(-ENOMEM); 443 444 su = devm_kzalloc(dev, array3_size(repeat, count, sizeof(*su)), 445 GFP_KERNEL); 446 if (su == NULL) 447 return ERR_PTR(-ENOMEM); 448 449 group->attrs = attrs; 450 group->is_visible = tg->is_visible; 451 452 for (i = 0; i < repeat; i++) { 453 t = tg->templates; 454 while (*t) { 455 snprintf(su->name, sizeof(su->name), 456 (*t)->dev_attr.attr.name, tg->base + i); 457 if ((*t)->s2) { 458 a2 = &su->u.a2; 459 sysfs_attr_init(&a2->dev_attr.attr); 460 a2->dev_attr.attr.name = su->name; 461 a2->nr = (*t)->u.s.nr + i; 462 a2->index = (*t)->u.s.index; 463 a2->dev_attr.attr.mode = 464 (*t)->dev_attr.attr.mode; 465 a2->dev_attr.show = (*t)->dev_attr.show; 466 a2->dev_attr.store = (*t)->dev_attr.store; 467 *attrs = &a2->dev_attr.attr; 468 } else { 469 a = &su->u.a1; 470 sysfs_attr_init(&a->dev_attr.attr); 471 a->dev_attr.attr.name = su->name; 472 a->index = (*t)->u.index + i; 473 a->dev_attr.attr.mode = 474 (*t)->dev_attr.attr.mode; 475 a->dev_attr.show = (*t)->dev_attr.show; 476 a->dev_attr.store = (*t)->dev_attr.store; 477 *attrs = &a->dev_attr.attr; 478 } 479 attrs++; 480 su++; 481 t++; 482 } 483 } 484 485 return group; 486 } 487 488 /* LSB is 16 mV, except for the following sources, where it is 32 mV */ 489 #define MON_SRC_VCC 0x60 490 #define MON_SRC_VSB 0x61 491 #define MON_SRC_AVSB 0x62 492 #define MON_SRC_VBAT 0x64 493 494 static inline long in_from_reg(u16 reg, u8 src) 495 { 496 int scale = 16; 497 498 if (src == MON_SRC_VCC || src == MON_SRC_VSB || src == MON_SRC_AVSB || 499 src == MON_SRC_VBAT) 500 scale <<= 1; 501 return reg * scale; 502 } 503 504 static u16 nct6683_read(struct nct6683_data *data, u16 reg) 505 { 506 int res; 507 508 outb_p(0xff, data->addr + EC_PAGE_REG); /* unlock */ 509 outb_p(reg >> 8, data->addr + EC_PAGE_REG); 510 outb_p(reg & 0xff, data->addr + EC_INDEX_REG); 511 res = inb_p(data->addr + EC_DATA_REG); 512 return res; 513 } 514 515 static u16 nct6683_read16(struct nct6683_data *data, u16 reg) 516 { 517 return (nct6683_read(data, reg) << 8) | nct6683_read(data, reg + 1); 518 } 519 520 static void nct6683_write(struct nct6683_data *data, u16 reg, u16 value) 521 { 522 outb_p(0xff, data->addr + EC_PAGE_REG); /* unlock */ 523 outb_p(reg >> 8, data->addr + EC_PAGE_REG); 524 outb_p(reg & 0xff, data->addr + EC_INDEX_REG); 525 outb_p(value & 0xff, data->addr + EC_DATA_REG); 526 } 527 528 static int get_in_reg(struct nct6683_data *data, int nr, int index) 529 { 530 int ch = data->in_index[index]; 531 int reg = -EINVAL; 532 533 switch (nr) { 534 case 0: 535 reg = NCT6683_REG_MON(ch); 536 break; 537 case 1: 538 if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL) 539 reg = NCT6683_REG_MON_LOW(ch); 540 break; 541 case 2: 542 if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL) 543 reg = NCT6683_REG_MON_HIGH(ch); 544 break; 545 default: 546 break; 547 } 548 return reg; 549 } 550 551 static int get_temp_reg(struct nct6683_data *data, int nr, int index) 552 { 553 int ch = data->temp_index[index]; 554 int reg = -EINVAL; 555 556 switch (data->customer_id) { 557 case NCT6683_CUSTOMER_ID_INTEL: 558 switch (nr) { 559 default: 560 case 1: /* max */ 561 reg = NCT6683_REG_INTEL_TEMP_MAX(ch); 562 break; 563 case 3: /* crit */ 564 reg = NCT6683_REG_INTEL_TEMP_CRIT(ch); 565 break; 566 } 567 break; 568 case NCT6683_CUSTOMER_ID_MITAC: 569 default: 570 switch (nr) { 571 default: 572 case 0: /* min */ 573 reg = NCT6683_REG_MON_LOW(ch); 574 break; 575 case 1: /* max */ 576 reg = NCT6683_REG_TEMP_MAX(ch); 577 break; 578 case 2: /* hyst */ 579 reg = NCT6683_REG_TEMP_HYST(ch); 580 break; 581 case 3: /* crit */ 582 reg = NCT6683_REG_MON_HIGH(ch); 583 break; 584 } 585 break; 586 } 587 return reg; 588 } 589 590 static void nct6683_update_pwm(struct device *dev) 591 { 592 struct nct6683_data *data = dev_get_drvdata(dev); 593 int i; 594 595 for (i = 0; i < NCT6683_NUM_REG_PWM; i++) { 596 if (!(data->have_pwm & (1 << i))) 597 continue; 598 data->pwm[i] = nct6683_read(data, NCT6683_REG_PWM(i)); 599 } 600 } 601 602 static struct nct6683_data *nct6683_update_device(struct device *dev) 603 { 604 struct nct6683_data *data = dev_get_drvdata(dev); 605 int i, j; 606 607 mutex_lock(&data->update_lock); 608 609 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 610 /* Measured voltages and limits */ 611 for (i = 0; i < data->in_num; i++) { 612 for (j = 0; j < 3; j++) { 613 int reg = get_in_reg(data, j, i); 614 615 if (reg >= 0) 616 data->in[j][i] = 617 nct6683_read(data, reg); 618 } 619 } 620 621 /* Measured temperatures and limits */ 622 for (i = 0; i < data->temp_num; i++) { 623 u8 ch = data->temp_index[i]; 624 625 data->temp_in[i] = nct6683_read16(data, 626 NCT6683_REG_MON(ch)); 627 for (j = 0; j < 4; j++) { 628 int reg = get_temp_reg(data, j, i); 629 630 if (reg >= 0) 631 data->temp[j][i] = 632 nct6683_read(data, reg); 633 } 634 } 635 636 /* Measured fan speeds and limits */ 637 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) { 638 if (!(data->have_fan & (1 << i))) 639 continue; 640 641 data->rpm[i] = nct6683_read16(data, 642 NCT6683_REG_FAN_RPM(i)); 643 data->fan_min[i] = nct6683_read16(data, 644 NCT6683_REG_FAN_MIN(i)); 645 } 646 647 nct6683_update_pwm(dev); 648 649 data->last_updated = jiffies; 650 data->valid = true; 651 } 652 653 mutex_unlock(&data->update_lock); 654 return data; 655 } 656 657 /* 658 * Sysfs callback functions 659 */ 660 static ssize_t 661 show_in_label(struct device *dev, struct device_attribute *attr, char *buf) 662 { 663 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 664 struct nct6683_data *data = nct6683_update_device(dev); 665 int nr = sattr->index; 666 667 return sprintf(buf, "%s\n", nct6683_mon_label[data->in_src[nr]]); 668 } 669 670 static ssize_t 671 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf) 672 { 673 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 674 struct nct6683_data *data = nct6683_update_device(dev); 675 int index = sattr->index; 676 int nr = sattr->nr; 677 678 return sprintf(buf, "%ld\n", 679 in_from_reg(data->in[index][nr], data->in_index[index])); 680 } 681 682 static umode_t nct6683_in_is_visible(struct kobject *kobj, 683 struct attribute *attr, int index) 684 { 685 struct device *dev = kobj_to_dev(kobj); 686 struct nct6683_data *data = dev_get_drvdata(dev); 687 int nr = index % 4; /* attribute */ 688 689 /* 690 * Voltage limits exist for Intel boards, 691 * but register location and encoding is unknown 692 */ 693 if ((nr == 2 || nr == 3) && 694 data->customer_id == NCT6683_CUSTOMER_ID_INTEL) 695 return 0; 696 697 return attr->mode; 698 } 699 700 SENSOR_TEMPLATE(in_label, "in%d_label", S_IRUGO, show_in_label, NULL, 0); 701 SENSOR_TEMPLATE_2(in_input, "in%d_input", S_IRUGO, show_in_reg, NULL, 0, 0); 702 SENSOR_TEMPLATE_2(in_min, "in%d_min", S_IRUGO, show_in_reg, NULL, 0, 1); 703 SENSOR_TEMPLATE_2(in_max, "in%d_max", S_IRUGO, show_in_reg, NULL, 0, 2); 704 705 static struct sensor_device_template *nct6683_attributes_in_template[] = { 706 &sensor_dev_template_in_label, 707 &sensor_dev_template_in_input, 708 &sensor_dev_template_in_min, 709 &sensor_dev_template_in_max, 710 NULL 711 }; 712 713 static const struct sensor_template_group nct6683_in_template_group = { 714 .templates = nct6683_attributes_in_template, 715 .is_visible = nct6683_in_is_visible, 716 }; 717 718 static ssize_t 719 show_fan(struct device *dev, struct device_attribute *attr, char *buf) 720 { 721 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 722 struct nct6683_data *data = nct6683_update_device(dev); 723 724 return sprintf(buf, "%d\n", data->rpm[sattr->index]); 725 } 726 727 static ssize_t 728 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf) 729 { 730 struct nct6683_data *data = nct6683_update_device(dev); 731 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 732 int nr = sattr->index; 733 734 return sprintf(buf, "%d\n", data->fan_min[nr]); 735 } 736 737 static ssize_t 738 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf) 739 { 740 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 741 struct nct6683_data *data = nct6683_update_device(dev); 742 743 return sprintf(buf, "%d\n", 744 ((data->fanin_cfg[sattr->index] >> 5) & 0x03) + 1); 745 } 746 747 static umode_t nct6683_fan_is_visible(struct kobject *kobj, 748 struct attribute *attr, int index) 749 { 750 struct device *dev = kobj_to_dev(kobj); 751 struct nct6683_data *data = dev_get_drvdata(dev); 752 int fan = index / 3; /* fan index */ 753 int nr = index % 3; /* attribute index */ 754 755 if (!(data->have_fan & (1 << fan))) 756 return 0; 757 758 /* 759 * Intel may have minimum fan speed limits, 760 * but register location and encoding are unknown. 761 */ 762 if (nr == 2 && data->customer_id == NCT6683_CUSTOMER_ID_INTEL) 763 return 0; 764 765 return attr->mode; 766 } 767 768 SENSOR_TEMPLATE(fan_input, "fan%d_input", S_IRUGO, show_fan, NULL, 0); 769 SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", S_IRUGO, show_fan_pulses, NULL, 0); 770 SENSOR_TEMPLATE(fan_min, "fan%d_min", S_IRUGO, show_fan_min, NULL, 0); 771 772 /* 773 * nct6683_fan_is_visible uses the index into the following array 774 * to determine if attributes should be created or not. 775 * Any change in order or content must be matched. 776 */ 777 static struct sensor_device_template *nct6683_attributes_fan_template[] = { 778 &sensor_dev_template_fan_input, 779 &sensor_dev_template_fan_pulses, 780 &sensor_dev_template_fan_min, 781 NULL 782 }; 783 784 static const struct sensor_template_group nct6683_fan_template_group = { 785 .templates = nct6683_attributes_fan_template, 786 .is_visible = nct6683_fan_is_visible, 787 .base = 1, 788 }; 789 790 static ssize_t 791 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf) 792 { 793 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 794 struct nct6683_data *data = nct6683_update_device(dev); 795 int nr = sattr->index; 796 797 return sprintf(buf, "%s\n", nct6683_mon_label[data->temp_src[nr]]); 798 } 799 800 static ssize_t 801 show_temp8(struct device *dev, struct device_attribute *attr, char *buf) 802 { 803 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 804 struct nct6683_data *data = nct6683_update_device(dev); 805 int index = sattr->index; 806 int nr = sattr->nr; 807 808 return sprintf(buf, "%d\n", data->temp[index][nr] * 1000); 809 } 810 811 static ssize_t 812 show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf) 813 { 814 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 815 struct nct6683_data *data = nct6683_update_device(dev); 816 int nr = sattr->index; 817 int temp = data->temp[1][nr] - data->temp[2][nr]; 818 819 return sprintf(buf, "%d\n", temp * 1000); 820 } 821 822 static ssize_t 823 show_temp16(struct device *dev, struct device_attribute *attr, char *buf) 824 { 825 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 826 struct nct6683_data *data = nct6683_update_device(dev); 827 int index = sattr->index; 828 829 return sprintf(buf, "%d\n", (data->temp_in[index] / 128) * 500); 830 } 831 832 /* 833 * Temperature sensor type is determined by temperature source 834 * and can not be modified. 835 * 0x02..0x07: Thermal diode 836 * 0x08..0x18: Thermistor 837 * 0x20..0x2b: Intel PECI 838 * 0x42..0x49: AMD TSI 839 * Others are unspecified (not visible) 840 */ 841 842 static int get_temp_type(u8 src) 843 { 844 if (src >= 0x02 && src <= 0x07) 845 return 3; /* thermal diode */ 846 else if (src >= 0x08 && src <= 0x18) 847 return 4; /* thermistor */ 848 else if (src >= 0x20 && src <= 0x2b) 849 return 6; /* PECI */ 850 else if (src >= 0x42 && src <= 0x49) 851 return 5; 852 853 return 0; 854 } 855 856 static ssize_t 857 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf) 858 { 859 struct nct6683_data *data = nct6683_update_device(dev); 860 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 861 int nr = sattr->index; 862 return sprintf(buf, "%d\n", get_temp_type(data->temp_src[nr])); 863 } 864 865 static umode_t nct6683_temp_is_visible(struct kobject *kobj, 866 struct attribute *attr, int index) 867 { 868 struct device *dev = kobj_to_dev(kobj); 869 struct nct6683_data *data = dev_get_drvdata(dev); 870 int temp = index / 7; /* temp index */ 871 int nr = index % 7; /* attribute index */ 872 873 /* 874 * Intel does not have low temperature limits or temperature hysteresis 875 * registers, or at least register location and encoding is unknown. 876 */ 877 if ((nr == 2 || nr == 4) && 878 data->customer_id == NCT6683_CUSTOMER_ID_INTEL) 879 return 0; 880 881 if (nr == 6 && get_temp_type(data->temp_src[temp]) == 0) 882 return 0; /* type */ 883 884 return attr->mode; 885 } 886 887 SENSOR_TEMPLATE(temp_input, "temp%d_input", S_IRUGO, show_temp16, NULL, 0); 888 SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temp_label, NULL, 0); 889 SENSOR_TEMPLATE_2(temp_min, "temp%d_min", S_IRUGO, show_temp8, NULL, 0, 0); 890 SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO, show_temp8, NULL, 0, 1); 891 SENSOR_TEMPLATE(temp_max_hyst, "temp%d_max_hyst", S_IRUGO, show_temp_hyst, NULL, 892 0); 893 SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", S_IRUGO, show_temp8, NULL, 0, 3); 894 SENSOR_TEMPLATE(temp_type, "temp%d_type", S_IRUGO, show_temp_type, NULL, 0); 895 896 /* 897 * nct6683_temp_is_visible uses the index into the following array 898 * to determine if attributes should be created or not. 899 * Any change in order or content must be matched. 900 */ 901 static struct sensor_device_template *nct6683_attributes_temp_template[] = { 902 &sensor_dev_template_temp_input, 903 &sensor_dev_template_temp_label, 904 &sensor_dev_template_temp_min, /* 2 */ 905 &sensor_dev_template_temp_max, /* 3 */ 906 &sensor_dev_template_temp_max_hyst, /* 4 */ 907 &sensor_dev_template_temp_crit, /* 5 */ 908 &sensor_dev_template_temp_type, /* 6 */ 909 NULL 910 }; 911 912 static const struct sensor_template_group nct6683_temp_template_group = { 913 .templates = nct6683_attributes_temp_template, 914 .is_visible = nct6683_temp_is_visible, 915 .base = 1, 916 }; 917 918 static ssize_t 919 show_pwm(struct device *dev, struct device_attribute *attr, char *buf) 920 { 921 struct nct6683_data *data = nct6683_update_device(dev); 922 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 923 int index = sattr->index; 924 925 return sprintf(buf, "%d\n", data->pwm[index]); 926 } 927 928 static ssize_t 929 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf, 930 size_t count) 931 { 932 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 933 struct nct6683_data *data = dev_get_drvdata(dev); 934 int index = sattr->index; 935 unsigned long val; 936 937 if (kstrtoul(buf, 10, &val) || val > 255) 938 return -EINVAL; 939 940 mutex_lock(&data->update_lock); 941 nct6683_write(data, NCT6683_REG_FAN_CFG_CTRL, NCT6683_FAN_CFG_REQ); 942 usleep_range(1000, 2000); 943 nct6683_write(data, NCT6683_REG_PWM_WRITE(index), val); 944 nct6683_write(data, NCT6683_REG_FAN_CFG_CTRL, NCT6683_FAN_CFG_DONE); 945 mutex_unlock(&data->update_lock); 946 947 return count; 948 } 949 950 SENSOR_TEMPLATE(pwm, "pwm%d", S_IRUGO, show_pwm, store_pwm, 0); 951 952 static umode_t nct6683_pwm_is_visible(struct kobject *kobj, 953 struct attribute *attr, int index) 954 { 955 struct device *dev = kobj_to_dev(kobj); 956 struct nct6683_data *data = dev_get_drvdata(dev); 957 int pwm = index; /* pwm index */ 958 959 if (!(data->have_pwm & (1 << pwm))) 960 return 0; 961 962 /* Only update pwm values for Mitac boards */ 963 if (data->customer_id == NCT6683_CUSTOMER_ID_MITAC) 964 return attr->mode | S_IWUSR; 965 966 return attr->mode; 967 } 968 969 static struct sensor_device_template *nct6683_attributes_pwm_template[] = { 970 &sensor_dev_template_pwm, 971 NULL 972 }; 973 974 static const struct sensor_template_group nct6683_pwm_template_group = { 975 .templates = nct6683_attributes_pwm_template, 976 .is_visible = nct6683_pwm_is_visible, 977 .base = 1, 978 }; 979 980 static ssize_t 981 beep_enable_show(struct device *dev, struct device_attribute *attr, char *buf) 982 { 983 struct nct6683_data *data = dev_get_drvdata(dev); 984 int ret; 985 u8 reg; 986 987 mutex_lock(&data->update_lock); 988 989 ret = superio_enter(data->sioreg); 990 if (ret) 991 goto error; 992 superio_select(data->sioreg, NCT6683_LD_HWM); 993 reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP); 994 superio_exit(data->sioreg); 995 996 mutex_unlock(&data->update_lock); 997 998 return sprintf(buf, "%u\n", !!(reg & NCT6683_CR_BEEP_MASK)); 999 1000 error: 1001 mutex_unlock(&data->update_lock); 1002 return ret; 1003 } 1004 1005 static ssize_t 1006 beep_enable_store(struct device *dev, struct device_attribute *attr, 1007 const char *buf, size_t count) 1008 { 1009 struct nct6683_data *data = dev_get_drvdata(dev); 1010 unsigned long val; 1011 u8 reg; 1012 int ret; 1013 1014 if (kstrtoul(buf, 10, &val) || (val != 0 && val != 1)) 1015 return -EINVAL; 1016 1017 mutex_lock(&data->update_lock); 1018 1019 ret = superio_enter(data->sioreg); 1020 if (ret) { 1021 count = ret; 1022 goto error; 1023 } 1024 1025 superio_select(data->sioreg, NCT6683_LD_HWM); 1026 reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP); 1027 if (val) 1028 reg |= NCT6683_CR_BEEP_MASK; 1029 else 1030 reg &= ~NCT6683_CR_BEEP_MASK; 1031 superio_outb(data->sioreg, NCT6683_REG_CR_BEEP, reg); 1032 superio_exit(data->sioreg); 1033 error: 1034 mutex_unlock(&data->update_lock); 1035 return count; 1036 } 1037 1038 /* Case open detection */ 1039 1040 static ssize_t 1041 intrusion0_alarm_show(struct device *dev, struct device_attribute *attr, 1042 char *buf) 1043 { 1044 struct nct6683_data *data = dev_get_drvdata(dev); 1045 int ret; 1046 u8 reg; 1047 1048 mutex_lock(&data->update_lock); 1049 1050 ret = superio_enter(data->sioreg); 1051 if (ret) 1052 goto error; 1053 superio_select(data->sioreg, NCT6683_LD_ACPI); 1054 reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN); 1055 superio_exit(data->sioreg); 1056 1057 mutex_unlock(&data->update_lock); 1058 1059 return sprintf(buf, "%u\n", !(reg & NCT6683_CR_CASEOPEN_MASK)); 1060 1061 error: 1062 mutex_unlock(&data->update_lock); 1063 return ret; 1064 } 1065 1066 static ssize_t 1067 intrusion0_alarm_store(struct device *dev, struct device_attribute *attr, 1068 const char *buf, size_t count) 1069 { 1070 struct nct6683_data *data = dev_get_drvdata(dev); 1071 unsigned long val; 1072 u8 reg; 1073 int ret; 1074 1075 if (kstrtoul(buf, 10, &val) || val != 0) 1076 return -EINVAL; 1077 1078 mutex_lock(&data->update_lock); 1079 1080 /* 1081 * Use CR registers to clear caseopen status. 1082 * Caseopen is activ low, clear by writing 1 into the register. 1083 */ 1084 1085 ret = superio_enter(data->sioreg); 1086 if (ret) { 1087 count = ret; 1088 goto error; 1089 } 1090 1091 superio_select(data->sioreg, NCT6683_LD_ACPI); 1092 reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN); 1093 reg |= NCT6683_CR_CASEOPEN_MASK; 1094 superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg); 1095 reg &= ~NCT6683_CR_CASEOPEN_MASK; 1096 superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg); 1097 superio_exit(data->sioreg); 1098 1099 data->valid = false; /* Force cache refresh */ 1100 error: 1101 mutex_unlock(&data->update_lock); 1102 return count; 1103 } 1104 1105 static DEVICE_ATTR_RW(intrusion0_alarm); 1106 static DEVICE_ATTR_RW(beep_enable); 1107 1108 static struct attribute *nct6683_attributes_other[] = { 1109 &dev_attr_intrusion0_alarm.attr, 1110 &dev_attr_beep_enable.attr, 1111 NULL 1112 }; 1113 1114 static const struct attribute_group nct6683_group_other = { 1115 .attrs = nct6683_attributes_other, 1116 }; 1117 1118 /* Get the monitoring functions started */ 1119 static inline void nct6683_init_device(struct nct6683_data *data) 1120 { 1121 u8 tmp; 1122 1123 /* Start hardware monitoring if needed */ 1124 tmp = nct6683_read(data, NCT6683_HWM_CFG); 1125 if (!(tmp & 0x80)) 1126 nct6683_write(data, NCT6683_HWM_CFG, tmp | 0x80); 1127 } 1128 1129 /* 1130 * There are a total of 24 fan inputs. Each can be configured as input 1131 * or as output. A maximum of 16 inputs and 8 outputs is configurable. 1132 */ 1133 static void 1134 nct6683_setup_fans(struct nct6683_data *data) 1135 { 1136 int i; 1137 u8 reg; 1138 1139 for (i = 0; i < NCT6683_NUM_REG_FAN; i++) { 1140 reg = nct6683_read(data, NCT6683_REG_FANIN_CFG(i)); 1141 if (reg & 0x80) 1142 data->have_fan |= 1 << i; 1143 data->fanin_cfg[i] = reg; 1144 } 1145 for (i = 0; i < NCT6683_NUM_REG_PWM; i++) { 1146 reg = nct6683_read(data, NCT6683_REG_FANOUT_CFG(i)); 1147 if (reg & 0x80) 1148 data->have_pwm |= 1 << i; 1149 data->fanout_cfg[i] = reg; 1150 } 1151 } 1152 1153 /* 1154 * Translation from monitoring register to temperature and voltage attributes 1155 * ========================================================================== 1156 * 1157 * There are a total of 32 monitoring registers. Each can be assigned to either 1158 * a temperature or voltage monitoring source. 1159 * NCT6683_REG_MON_CFG(x) defines assignment for each monitoring source. 1160 * 1161 * Temperature and voltage attribute mapping is determined by walking through 1162 * the NCT6683_REG_MON_CFG registers. If the assigned source is 1163 * a temperature, temp_index[n] is set to the monitor register index, and 1164 * temp_src[n] is set to the temperature source. If the assigned source is 1165 * a voltage, the respective values are stored in in_index[] and in_src[], 1166 * respectively. 1167 */ 1168 1169 static void nct6683_setup_sensors(struct nct6683_data *data) 1170 { 1171 u8 reg; 1172 int i; 1173 1174 data->temp_num = 0; 1175 data->in_num = 0; 1176 for (i = 0; i < NCT6683_NUM_REG_MON; i++) { 1177 reg = nct6683_read(data, NCT6683_REG_MON_CFG(i)) & 0x7f; 1178 /* Ignore invalid assignments */ 1179 if (reg >= NUM_MON_LABELS) 1180 continue; 1181 /* Skip if disabled or reserved */ 1182 if (nct6683_mon_label[reg] == NULL) 1183 continue; 1184 if (reg < MON_VOLTAGE_START) { 1185 data->temp_index[data->temp_num] = i; 1186 data->temp_src[data->temp_num] = reg; 1187 data->temp_num++; 1188 } else { 1189 data->in_index[data->in_num] = i; 1190 data->in_src[data->in_num] = reg; 1191 data->in_num++; 1192 } 1193 } 1194 } 1195 1196 static int nct6683_probe(struct platform_device *pdev) 1197 { 1198 struct device *dev = &pdev->dev; 1199 struct nct6683_sio_data *sio_data = dev->platform_data; 1200 struct attribute_group *group; 1201 struct nct6683_data *data; 1202 struct device *hwmon_dev; 1203 struct resource *res; 1204 int groups = 0; 1205 char build[16]; 1206 1207 res = platform_get_resource(pdev, IORESOURCE_IO, 0); 1208 if (!devm_request_region(dev, res->start, IOREGION_LENGTH, DRVNAME)) 1209 return -EBUSY; 1210 1211 data = devm_kzalloc(dev, sizeof(struct nct6683_data), GFP_KERNEL); 1212 if (!data) 1213 return -ENOMEM; 1214 1215 data->kind = sio_data->kind; 1216 data->sioreg = sio_data->sioreg; 1217 data->addr = res->start; 1218 mutex_init(&data->update_lock); 1219 platform_set_drvdata(pdev, data); 1220 1221 data->customer_id = nct6683_read16(data, NCT6683_REG_CUSTOMER_ID); 1222 1223 /* By default only instantiate driver if the customer ID is known */ 1224 switch (data->customer_id) { 1225 case NCT6683_CUSTOMER_ID_INTEL: 1226 break; 1227 case NCT6683_CUSTOMER_ID_MITAC: 1228 break; 1229 case NCT6683_CUSTOMER_ID_MSI: 1230 break; 1231 case NCT6683_CUSTOMER_ID_MSI2: 1232 break; 1233 case NCT6683_CUSTOMER_ID_MSI3: 1234 break; 1235 case NCT6683_CUSTOMER_ID_MSI4: 1236 break; 1237 case NCT6683_CUSTOMER_ID_AMD: 1238 break; 1239 case NCT6683_CUSTOMER_ID_ASROCK: 1240 break; 1241 case NCT6683_CUSTOMER_ID_ASROCK2: 1242 break; 1243 case NCT6683_CUSTOMER_ID_ASROCK3: 1244 break; 1245 case NCT6683_CUSTOMER_ID_ASROCK4: 1246 break; 1247 case NCT6683_CUSTOMER_ID_ASROCK5: 1248 break; 1249 case NCT6683_CUSTOMER_ID_ASROCK6: 1250 break; 1251 default: 1252 if (!force) 1253 return -ENODEV; 1254 dev_warn(dev, "Enabling support for unknown customer ID 0x%04x\n", data->customer_id); 1255 break; 1256 } 1257 1258 nct6683_init_device(data); 1259 nct6683_setup_fans(data); 1260 nct6683_setup_sensors(data); 1261 1262 /* Register sysfs hooks */ 1263 1264 if (data->have_pwm) { 1265 group = nct6683_create_attr_group(dev, 1266 &nct6683_pwm_template_group, 1267 fls(data->have_pwm)); 1268 if (IS_ERR(group)) 1269 return PTR_ERR(group); 1270 data->groups[groups++] = group; 1271 } 1272 1273 if (data->in_num) { 1274 group = nct6683_create_attr_group(dev, 1275 &nct6683_in_template_group, 1276 data->in_num); 1277 if (IS_ERR(group)) 1278 return PTR_ERR(group); 1279 data->groups[groups++] = group; 1280 } 1281 1282 if (data->have_fan) { 1283 group = nct6683_create_attr_group(dev, 1284 &nct6683_fan_template_group, 1285 fls(data->have_fan)); 1286 if (IS_ERR(group)) 1287 return PTR_ERR(group); 1288 data->groups[groups++] = group; 1289 } 1290 1291 if (data->temp_num) { 1292 group = nct6683_create_attr_group(dev, 1293 &nct6683_temp_template_group, 1294 data->temp_num); 1295 if (IS_ERR(group)) 1296 return PTR_ERR(group); 1297 data->groups[groups++] = group; 1298 } 1299 data->groups[groups++] = &nct6683_group_other; 1300 1301 if (data->customer_id == NCT6683_CUSTOMER_ID_INTEL) 1302 scnprintf(build, sizeof(build), "%02x/%02x/%02x", 1303 nct6683_read(data, NCT6683_REG_BUILD_MONTH), 1304 nct6683_read(data, NCT6683_REG_BUILD_DAY), 1305 nct6683_read(data, NCT6683_REG_BUILD_YEAR)); 1306 else 1307 scnprintf(build, sizeof(build), "%02d/%02d/%02d", 1308 nct6683_read(data, NCT6683_REG_BUILD_MONTH), 1309 nct6683_read(data, NCT6683_REG_BUILD_DAY), 1310 nct6683_read(data, NCT6683_REG_BUILD_YEAR)); 1311 1312 dev_info(dev, "%s EC firmware version %d.%d build %s\n", 1313 nct6683_chip_names[data->kind], 1314 nct6683_read(data, NCT6683_REG_VERSION_HI), 1315 nct6683_read(data, NCT6683_REG_VERSION_LO), 1316 build); 1317 1318 hwmon_dev = devm_hwmon_device_register_with_groups(dev, 1319 nct6683_device_names[data->kind], data, data->groups); 1320 return PTR_ERR_OR_ZERO(hwmon_dev); 1321 } 1322 1323 #ifdef CONFIG_PM 1324 static int nct6683_suspend(struct device *dev) 1325 { 1326 struct nct6683_data *data = nct6683_update_device(dev); 1327 1328 mutex_lock(&data->update_lock); 1329 data->hwm_cfg = nct6683_read(data, NCT6683_HWM_CFG); 1330 mutex_unlock(&data->update_lock); 1331 1332 return 0; 1333 } 1334 1335 static int nct6683_resume(struct device *dev) 1336 { 1337 struct nct6683_data *data = dev_get_drvdata(dev); 1338 1339 mutex_lock(&data->update_lock); 1340 1341 nct6683_write(data, NCT6683_HWM_CFG, data->hwm_cfg); 1342 1343 /* Force re-reading all values */ 1344 data->valid = false; 1345 mutex_unlock(&data->update_lock); 1346 1347 return 0; 1348 } 1349 1350 static const struct dev_pm_ops nct6683_dev_pm_ops = { 1351 .suspend = nct6683_suspend, 1352 .resume = nct6683_resume, 1353 .freeze = nct6683_suspend, 1354 .restore = nct6683_resume, 1355 }; 1356 1357 #define NCT6683_DEV_PM_OPS (&nct6683_dev_pm_ops) 1358 #else 1359 #define NCT6683_DEV_PM_OPS NULL 1360 #endif /* CONFIG_PM */ 1361 1362 static struct platform_driver nct6683_driver = { 1363 .driver = { 1364 .name = DRVNAME, 1365 .pm = NCT6683_DEV_PM_OPS, 1366 }, 1367 .probe = nct6683_probe, 1368 }; 1369 1370 static int __init nct6683_find(int sioaddr, struct nct6683_sio_data *sio_data) 1371 { 1372 int addr; 1373 u16 val; 1374 int err; 1375 1376 err = superio_enter(sioaddr); 1377 if (err) 1378 return err; 1379 1380 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8) 1381 | superio_inb(sioaddr, SIO_REG_DEVID + 1); 1382 1383 switch (val & SIO_ID_MASK) { 1384 case SIO_NCT6683_ID: 1385 sio_data->kind = nct6683; 1386 break; 1387 case SIO_NCT6686_ID: 1388 sio_data->kind = nct6686; 1389 break; 1390 case SIO_NCT6687_ID: 1391 sio_data->kind = nct6687; 1392 break; 1393 default: 1394 if (val != 0xffff) 1395 pr_debug("unsupported chip ID: 0x%04x\n", val); 1396 goto fail; 1397 } 1398 1399 /* We have a known chip, find the HWM I/O address */ 1400 superio_select(sioaddr, NCT6683_LD_HWM); 1401 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8) 1402 | superio_inb(sioaddr, SIO_REG_ADDR + 1); 1403 addr = val & IOREGION_ALIGNMENT; 1404 if (addr == 0) { 1405 pr_err("EC base I/O port unconfigured\n"); 1406 goto fail; 1407 } 1408 1409 /* Activate logical device if needed */ 1410 val = superio_inb(sioaddr, SIO_REG_ENABLE); 1411 if (!(val & 0x01)) { 1412 pr_warn("Forcibly enabling EC access. Data may be unusable.\n"); 1413 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01); 1414 } 1415 1416 superio_exit(sioaddr); 1417 pr_info("Found %s or compatible chip at %#x:%#x\n", 1418 nct6683_chip_names[sio_data->kind], sioaddr, addr); 1419 sio_data->sioreg = sioaddr; 1420 1421 return addr; 1422 1423 fail: 1424 superio_exit(sioaddr); 1425 return -ENODEV; 1426 } 1427 1428 /* 1429 * when Super-I/O functions move to a separate file, the Super-I/O 1430 * bus will manage the lifetime of the device and this module will only keep 1431 * track of the nct6683 driver. But since we use platform_device_alloc(), we 1432 * must keep track of the device 1433 */ 1434 static struct platform_device *pdev[2]; 1435 1436 static int __init sensors_nct6683_init(void) 1437 { 1438 struct nct6683_sio_data sio_data; 1439 int sioaddr[2] = { 0x2e, 0x4e }; 1440 struct resource res; 1441 bool found = false; 1442 int address; 1443 int i, err; 1444 1445 err = platform_driver_register(&nct6683_driver); 1446 if (err) 1447 return err; 1448 1449 /* 1450 * initialize sio_data->kind and sio_data->sioreg. 1451 * 1452 * when Super-I/O functions move to a separate file, the Super-I/O 1453 * driver will probe 0x2e and 0x4e and auto-detect the presence of a 1454 * nct6683 hardware monitor, and call probe() 1455 */ 1456 for (i = 0; i < ARRAY_SIZE(pdev); i++) { 1457 address = nct6683_find(sioaddr[i], &sio_data); 1458 if (address <= 0) 1459 continue; 1460 1461 found = true; 1462 1463 pdev[i] = platform_device_alloc(DRVNAME, address); 1464 if (!pdev[i]) { 1465 err = -ENOMEM; 1466 goto exit_device_unregister; 1467 } 1468 1469 err = platform_device_add_data(pdev[i], &sio_data, 1470 sizeof(struct nct6683_sio_data)); 1471 if (err) 1472 goto exit_device_put; 1473 1474 memset(&res, 0, sizeof(res)); 1475 res.name = DRVNAME; 1476 res.start = address + IOREGION_OFFSET; 1477 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1; 1478 res.flags = IORESOURCE_IO; 1479 1480 err = acpi_check_resource_conflict(&res); 1481 if (err) { 1482 platform_device_put(pdev[i]); 1483 pdev[i] = NULL; 1484 continue; 1485 } 1486 1487 err = platform_device_add_resources(pdev[i], &res, 1); 1488 if (err) 1489 goto exit_device_put; 1490 1491 /* platform_device_add calls probe() */ 1492 err = platform_device_add(pdev[i]); 1493 if (err) 1494 goto exit_device_put; 1495 } 1496 if (!found) { 1497 err = -ENODEV; 1498 goto exit_unregister; 1499 } 1500 1501 return 0; 1502 1503 exit_device_put: 1504 platform_device_put(pdev[i]); 1505 exit_device_unregister: 1506 while (--i >= 0) { 1507 if (pdev[i]) 1508 platform_device_unregister(pdev[i]); 1509 } 1510 exit_unregister: 1511 platform_driver_unregister(&nct6683_driver); 1512 return err; 1513 } 1514 1515 static void __exit sensors_nct6683_exit(void) 1516 { 1517 int i; 1518 1519 for (i = 0; i < ARRAY_SIZE(pdev); i++) { 1520 if (pdev[i]) 1521 platform_device_unregister(pdev[i]); 1522 } 1523 platform_driver_unregister(&nct6683_driver); 1524 } 1525 1526 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); 1527 MODULE_DESCRIPTION("NCT6683D driver"); 1528 MODULE_LICENSE("GPL"); 1529 1530 module_init(sensors_nct6683_init); 1531 module_exit(sensors_nct6683_exit); 1532