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