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