1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * INT3400 thermal driver 4 * 5 * Copyright (C) 2014, Intel Corporation 6 * Authors: Zhang Rui <rui.zhang@intel.com> 7 */ 8 9 #include <linux/module.h> 10 #include <linux/platform_device.h> 11 #include <linux/acpi.h> 12 #include <linux/thermal.h> 13 #include "acpi_thermal_rel.h" 14 15 #define INT3400_THERMAL_TABLE_CHANGED 0x83 16 #define INT3400_ODVP_CHANGED 0x88 17 #define INT3400_KEEP_ALIVE 0xA0 18 #define INT3400_FAKE_TEMP (20 * 1000) /* faked temp sensor with 20C */ 19 20 enum int3400_thermal_uuid { 21 INT3400_THERMAL_ACTIVE = 0, 22 INT3400_THERMAL_PASSIVE_1, 23 INT3400_THERMAL_CRITICAL, 24 INT3400_THERMAL_ADAPTIVE_PERFORMANCE, 25 INT3400_THERMAL_EMERGENCY_CALL_MODE, 26 INT3400_THERMAL_PASSIVE_2, 27 INT3400_THERMAL_POWER_BOSS, 28 INT3400_THERMAL_VIRTUAL_SENSOR, 29 INT3400_THERMAL_COOLING_MODE, 30 INT3400_THERMAL_HARDWARE_DUTY_CYCLING, 31 INT3400_THERMAL_MAXIMUM_UUID, 32 }; 33 34 static char *int3400_thermal_uuids[INT3400_THERMAL_MAXIMUM_UUID] = { 35 "3A95C389-E4B8-4629-A526-C52C88626BAE", 36 "42A441D6-AE6A-462b-A84B-4A8CE79027D3", 37 "97C68AE7-15FA-499c-B8C9-5DA81D606E0A", 38 "63BE270F-1C11-48FD-A6F7-3AF253FF3E2D", 39 "5349962F-71E6-431D-9AE8-0A635B710AEE", 40 "9E04115A-AE87-4D1C-9500-0F3E340BFE75", 41 "F5A35014-C209-46A4-993A-EB56DE7530A1", 42 "6ED722A7-9240-48A5-B479-31EEF723D7CF", 43 "16CAF1B7-DD38-40ED-B1C1-1B8A1913D531", 44 "BE84BABF-C4D4-403D-B495-3128FD44dAC1", 45 }; 46 47 struct odvp_attr; 48 49 struct int3400_thermal_priv { 50 struct acpi_device *adev; 51 struct platform_device *pdev; 52 struct thermal_zone_device *thermal; 53 int art_count; 54 struct art *arts; 55 int trt_count; 56 struct trt *trts; 57 u32 uuid_bitmap; 58 int rel_misc_dev_res; 59 int current_uuid_index; 60 char *data_vault; 61 int odvp_count; 62 int *odvp; 63 u32 os_uuid_mask; 64 int production_mode; 65 struct odvp_attr *odvp_attrs; 66 }; 67 68 static int evaluate_odvp(struct int3400_thermal_priv *priv); 69 70 struct odvp_attr { 71 int odvp; 72 struct int3400_thermal_priv *priv; 73 struct device_attribute attr; 74 }; 75 76 static BIN_ATTR_SIMPLE_RO(data_vault); 77 78 static ssize_t imok_store(struct device *dev, struct device_attribute *attr, 79 const char *buf, size_t count) 80 { 81 struct int3400_thermal_priv *priv = dev_get_drvdata(dev); 82 acpi_status status; 83 int input, ret; 84 85 ret = kstrtouint(buf, 10, &input); 86 if (ret) 87 return ret; 88 status = acpi_execute_simple_method(priv->adev->handle, "IMOK", input); 89 if (ACPI_FAILURE(status)) 90 return -EIO; 91 92 return count; 93 } 94 95 static DEVICE_ATTR_WO(imok); 96 97 static struct attribute *imok_attr[] = { 98 &dev_attr_imok.attr, 99 NULL 100 }; 101 102 static const struct attribute_group imok_attribute_group = { 103 .attrs = imok_attr, 104 }; 105 106 static ssize_t available_uuids_show(struct device *dev, 107 struct device_attribute *attr, 108 char *buf) 109 { 110 struct int3400_thermal_priv *priv = dev_get_drvdata(dev); 111 int i; 112 int length = 0; 113 114 if (!priv->uuid_bitmap) 115 return sprintf(buf, "UNKNOWN\n"); 116 117 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; i++) { 118 if (priv->uuid_bitmap & (1 << i)) 119 length += sysfs_emit_at(buf, length, "%s\n", int3400_thermal_uuids[i]); 120 } 121 122 return length; 123 } 124 125 static ssize_t current_uuid_show(struct device *dev, 126 struct device_attribute *devattr, char *buf) 127 { 128 struct int3400_thermal_priv *priv = dev_get_drvdata(dev); 129 int i, length = 0; 130 131 if (priv->current_uuid_index >= 0) 132 return sprintf(buf, "%s\n", 133 int3400_thermal_uuids[priv->current_uuid_index]); 134 135 for (i = 0; i <= INT3400_THERMAL_CRITICAL; i++) { 136 if (priv->os_uuid_mask & BIT(i)) 137 length += sysfs_emit_at(buf, length, "%s\n", int3400_thermal_uuids[i]); 138 } 139 140 if (length) 141 return length; 142 143 return sprintf(buf, "INVALID\n"); 144 } 145 146 static int int3400_thermal_run_osc(acpi_handle handle, char *uuid_str, int *enable) 147 { 148 u32 ret, buf[2]; 149 acpi_status status; 150 int result = 0; 151 struct acpi_osc_context context = { 152 .uuid_str = uuid_str, 153 .rev = 1, 154 .cap.length = 8, 155 .cap.pointer = buf, 156 }; 157 158 buf[OSC_QUERY_DWORD] = 0; 159 buf[OSC_SUPPORT_DWORD] = *enable; 160 161 status = acpi_run_osc(handle, &context); 162 if (ACPI_SUCCESS(status)) { 163 ret = *((u32 *)(context.ret.pointer + 4)); 164 if (ret != *enable) 165 result = -EPERM; 166 167 kfree(context.ret.pointer); 168 } else 169 result = -EPERM; 170 171 return result; 172 } 173 174 static int set_os_uuid_mask(struct int3400_thermal_priv *priv, u32 mask) 175 { 176 int cap = 0; 177 178 /* 179 * Capability bits: 180 * Bit 0: set to 1 to indicate DPTF is active 181 * Bi1 1: set to 1 to active cooling is supported by user space daemon 182 * Bit 2: set to 1 to passive cooling is supported by user space daemon 183 * Bit 3: set to 1 to critical trip is handled by user space daemon 184 */ 185 if (mask) 186 cap = (priv->os_uuid_mask << 1) | 0x01; 187 188 return int3400_thermal_run_osc(priv->adev->handle, 189 "b23ba85d-c8b7-3542-88de-8de2ffcfd698", 190 &cap); 191 } 192 193 static ssize_t current_uuid_store(struct device *dev, 194 struct device_attribute *attr, 195 const char *buf, size_t count) 196 { 197 struct int3400_thermal_priv *priv = dev_get_drvdata(dev); 198 int ret, i; 199 200 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; ++i) { 201 if (!strncmp(buf, int3400_thermal_uuids[i], 202 sizeof(int3400_thermal_uuids[i]) - 1)) { 203 /* 204 * If we have a list of supported UUIDs, make sure 205 * this one is supported. 206 */ 207 if (priv->uuid_bitmap & BIT(i)) { 208 priv->current_uuid_index = i; 209 return count; 210 } 211 212 /* 213 * There is support of only 3 policies via the new 214 * _OSC to inform OS capability: 215 * INT3400_THERMAL_ACTIVE 216 * INT3400_THERMAL_PASSIVE_1 217 * INT3400_THERMAL_CRITICAL 218 */ 219 220 if (i > INT3400_THERMAL_CRITICAL) 221 return -EINVAL; 222 223 priv->os_uuid_mask |= BIT(i); 224 225 break; 226 } 227 } 228 229 if (priv->os_uuid_mask) { 230 ret = set_os_uuid_mask(priv, priv->os_uuid_mask); 231 if (ret) 232 return ret; 233 } 234 235 return count; 236 } 237 238 static DEVICE_ATTR_RW(current_uuid); 239 static DEVICE_ATTR_RO(available_uuids); 240 static struct attribute *uuid_attrs[] = { 241 &dev_attr_available_uuids.attr, 242 &dev_attr_current_uuid.attr, 243 NULL 244 }; 245 246 static const struct attribute_group uuid_attribute_group = { 247 .attrs = uuid_attrs, 248 .name = "uuids" 249 }; 250 251 static int int3400_thermal_get_uuids(struct int3400_thermal_priv *priv) 252 { 253 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL}; 254 union acpi_object *obja, *objb; 255 int i, j; 256 int result = 0; 257 acpi_status status; 258 259 status = acpi_evaluate_object(priv->adev->handle, "IDSP", NULL, &buf); 260 if (ACPI_FAILURE(status)) 261 return -ENODEV; 262 263 obja = (union acpi_object *)buf.pointer; 264 if (obja->type != ACPI_TYPE_PACKAGE) { 265 result = -EINVAL; 266 goto end; 267 } 268 269 for (i = 0; i < obja->package.count; i++) { 270 objb = &obja->package.elements[i]; 271 if (objb->type != ACPI_TYPE_BUFFER) { 272 result = -EINVAL; 273 goto end; 274 } 275 276 /* UUID must be 16 bytes */ 277 if (objb->buffer.length != 16) { 278 result = -EINVAL; 279 goto end; 280 } 281 282 for (j = 0; j < INT3400_THERMAL_MAXIMUM_UUID; j++) { 283 guid_t guid; 284 285 guid_parse(int3400_thermal_uuids[j], &guid); 286 if (guid_equal((guid_t *)objb->buffer.pointer, &guid)) { 287 priv->uuid_bitmap |= (1 << j); 288 break; 289 } 290 } 291 } 292 293 end: 294 kfree(buf.pointer); 295 return result; 296 } 297 298 static ssize_t production_mode_show(struct device *dev, struct device_attribute *attr, 299 char *buf) 300 { 301 struct int3400_thermal_priv *priv = dev_get_drvdata(dev); 302 303 return sysfs_emit(buf, "%d\n", priv->production_mode); 304 } 305 306 static DEVICE_ATTR_RO(production_mode); 307 308 static int production_mode_init(struct int3400_thermal_priv *priv) 309 { 310 unsigned long long mode; 311 acpi_status status; 312 int ret; 313 314 priv->production_mode = -1; 315 316 status = acpi_evaluate_integer(priv->adev->handle, "DCFG", NULL, &mode); 317 /* If the method is not present, this is not an error */ 318 if (ACPI_FAILURE(status)) 319 return 0; 320 321 ret = sysfs_create_file(&priv->pdev->dev.kobj, &dev_attr_production_mode.attr); 322 if (ret) 323 return ret; 324 325 priv->production_mode = mode; 326 327 return 0; 328 } 329 330 static void production_mode_exit(struct int3400_thermal_priv *priv) 331 { 332 if (priv->production_mode >= 0) 333 sysfs_remove_file(&priv->pdev->dev.kobj, &dev_attr_production_mode.attr); 334 } 335 336 static ssize_t odvp_show(struct device *dev, struct device_attribute *attr, 337 char *buf) 338 { 339 struct odvp_attr *odvp_attr; 340 341 odvp_attr = container_of(attr, struct odvp_attr, attr); 342 343 return sprintf(buf, "%d\n", odvp_attr->priv->odvp[odvp_attr->odvp]); 344 } 345 346 static void cleanup_odvp(struct int3400_thermal_priv *priv) 347 { 348 int i; 349 350 if (priv->odvp_attrs) { 351 for (i = 0; i < priv->odvp_count; i++) { 352 sysfs_remove_file(&priv->pdev->dev.kobj, 353 &priv->odvp_attrs[i].attr.attr); 354 kfree(priv->odvp_attrs[i].attr.attr.name); 355 } 356 kfree(priv->odvp_attrs); 357 } 358 kfree(priv->odvp); 359 priv->odvp_count = 0; 360 } 361 362 static int evaluate_odvp(struct int3400_thermal_priv *priv) 363 { 364 struct acpi_buffer odvp = { ACPI_ALLOCATE_BUFFER, NULL }; 365 union acpi_object *obj = NULL; 366 acpi_status status; 367 int i, ret; 368 369 status = acpi_evaluate_object(priv->adev->handle, "ODVP", NULL, &odvp); 370 if (ACPI_FAILURE(status)) { 371 ret = -EINVAL; 372 goto out_err; 373 } 374 375 obj = odvp.pointer; 376 if (obj->type != ACPI_TYPE_PACKAGE) { 377 ret = -EINVAL; 378 goto out_err; 379 } 380 381 if (priv->odvp == NULL) { 382 priv->odvp_count = obj->package.count; 383 priv->odvp = kmalloc_array(priv->odvp_count, sizeof(int), 384 GFP_KERNEL); 385 if (!priv->odvp) { 386 ret = -ENOMEM; 387 goto out_err; 388 } 389 } 390 391 if (priv->odvp_attrs == NULL) { 392 priv->odvp_attrs = kcalloc(priv->odvp_count, 393 sizeof(struct odvp_attr), 394 GFP_KERNEL); 395 if (!priv->odvp_attrs) { 396 ret = -ENOMEM; 397 goto out_err; 398 } 399 for (i = 0; i < priv->odvp_count; i++) { 400 struct odvp_attr *odvp = &priv->odvp_attrs[i]; 401 402 sysfs_attr_init(&odvp->attr.attr); 403 odvp->priv = priv; 404 odvp->odvp = i; 405 odvp->attr.attr.name = kasprintf(GFP_KERNEL, 406 "odvp%d", i); 407 408 if (!odvp->attr.attr.name) { 409 ret = -ENOMEM; 410 goto out_err; 411 } 412 odvp->attr.attr.mode = 0444; 413 odvp->attr.show = odvp_show; 414 odvp->attr.store = NULL; 415 ret = sysfs_create_file(&priv->pdev->dev.kobj, 416 &odvp->attr.attr); 417 if (ret) 418 goto out_err; 419 } 420 } 421 422 for (i = 0; i < obj->package.count; i++) { 423 if (obj->package.elements[i].type == ACPI_TYPE_INTEGER) 424 priv->odvp[i] = obj->package.elements[i].integer.value; 425 } 426 427 kfree(obj); 428 return 0; 429 430 out_err: 431 cleanup_odvp(priv); 432 kfree(obj); 433 return ret; 434 } 435 436 static void int3400_notify(acpi_handle handle, 437 u32 event, 438 void *data) 439 { 440 struct int3400_thermal_priv *priv = data; 441 struct device *dev; 442 char *thermal_prop[5]; 443 int therm_event; 444 445 if (!priv) 446 return; 447 448 switch (event) { 449 case INT3400_THERMAL_TABLE_CHANGED: 450 therm_event = THERMAL_TABLE_CHANGED; 451 break; 452 case INT3400_KEEP_ALIVE: 453 therm_event = THERMAL_EVENT_KEEP_ALIVE; 454 break; 455 case INT3400_ODVP_CHANGED: 456 evaluate_odvp(priv); 457 therm_event = THERMAL_DEVICE_POWER_CAPABILITY_CHANGED; 458 break; 459 default: 460 /* Ignore unknown notification codes sent to INT3400 device */ 461 return; 462 } 463 464 dev = thermal_zone_device(priv->thermal); 465 466 thermal_prop[0] = kasprintf(GFP_KERNEL, "NAME=%s", thermal_zone_device_type(priv->thermal)); 467 thermal_prop[1] = kasprintf(GFP_KERNEL, "TEMP=%d", INT3400_FAKE_TEMP); 468 thermal_prop[2] = kasprintf(GFP_KERNEL, "TRIP="); 469 thermal_prop[3] = kasprintf(GFP_KERNEL, "EVENT=%d", therm_event); 470 thermal_prop[4] = NULL; 471 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, thermal_prop); 472 kfree(thermal_prop[0]); 473 kfree(thermal_prop[1]); 474 kfree(thermal_prop[2]); 475 kfree(thermal_prop[3]); 476 } 477 478 static int int3400_thermal_get_temp(struct thermal_zone_device *thermal, 479 int *temp) 480 { 481 *temp = INT3400_FAKE_TEMP; 482 return 0; 483 } 484 485 static int int3400_thermal_change_mode(struct thermal_zone_device *thermal, 486 enum thermal_device_mode mode) 487 { 488 struct int3400_thermal_priv *priv = thermal_zone_device_priv(thermal); 489 int result = 0; 490 int enabled; 491 492 if (!priv) 493 return -EINVAL; 494 495 enabled = mode == THERMAL_DEVICE_ENABLED; 496 497 if (priv->os_uuid_mask) { 498 if (!enabled) { 499 priv->os_uuid_mask = 0; 500 result = set_os_uuid_mask(priv, priv->os_uuid_mask); 501 } 502 goto eval_odvp; 503 } 504 505 if (priv->current_uuid_index < 0 || 506 priv->current_uuid_index >= INT3400_THERMAL_MAXIMUM_UUID) 507 return -EINVAL; 508 509 result = int3400_thermal_run_osc(priv->adev->handle, 510 int3400_thermal_uuids[priv->current_uuid_index], 511 &enabled); 512 eval_odvp: 513 evaluate_odvp(priv); 514 515 return result; 516 } 517 518 static struct thermal_zone_device_ops int3400_thermal_ops = { 519 .get_temp = int3400_thermal_get_temp, 520 .change_mode = int3400_thermal_change_mode, 521 }; 522 523 static struct thermal_zone_params int3400_thermal_params = { 524 .no_hwmon = true, 525 }; 526 527 static void int3400_setup_gddv(struct int3400_thermal_priv *priv) 528 { 529 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 530 union acpi_object *obj; 531 acpi_status status; 532 533 status = acpi_evaluate_object(priv->adev->handle, "GDDV", NULL, 534 &buffer); 535 if (ACPI_FAILURE(status) || !buffer.length) 536 return; 537 538 obj = buffer.pointer; 539 if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 1 540 || obj->package.elements[0].type != ACPI_TYPE_BUFFER) 541 goto out_free; 542 543 priv->data_vault = kmemdup(obj->package.elements[0].buffer.pointer, 544 obj->package.elements[0].buffer.length, 545 GFP_KERNEL); 546 if (ZERO_OR_NULL_PTR(priv->data_vault)) 547 goto out_free; 548 549 bin_attr_data_vault.private = priv->data_vault; 550 bin_attr_data_vault.size = obj->package.elements[0].buffer.length; 551 out_free: 552 kfree(buffer.pointer); 553 } 554 555 static int int3400_thermal_probe(struct platform_device *pdev) 556 { 557 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev); 558 struct int3400_thermal_priv *priv; 559 int result; 560 561 if (!adev) 562 return -ENODEV; 563 564 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 565 if (!priv) 566 return -ENOMEM; 567 568 priv->pdev = pdev; 569 priv->adev = adev; 570 571 result = int3400_thermal_get_uuids(priv); 572 573 /* Missing IDSP isn't fatal */ 574 if (result && result != -ENODEV) 575 goto free_priv; 576 577 priv->current_uuid_index = -1; 578 579 result = acpi_parse_art(priv->adev->handle, &priv->art_count, 580 &priv->arts, true); 581 if (result) 582 dev_dbg(&pdev->dev, "_ART table parsing error\n"); 583 584 result = acpi_parse_trt(priv->adev->handle, &priv->trt_count, 585 &priv->trts, true); 586 if (result) 587 dev_dbg(&pdev->dev, "_TRT table parsing error\n"); 588 589 platform_set_drvdata(pdev, priv); 590 591 int3400_setup_gddv(priv); 592 593 evaluate_odvp(priv); 594 595 priv->thermal = thermal_tripless_zone_device_register("INT3400 Thermal", priv, 596 &int3400_thermal_ops, 597 &int3400_thermal_params); 598 if (IS_ERR(priv->thermal)) { 599 result = PTR_ERR(priv->thermal); 600 goto free_art_trt; 601 } 602 603 priv->rel_misc_dev_res = acpi_thermal_rel_misc_device_add( 604 priv->adev->handle); 605 606 result = sysfs_create_group(&pdev->dev.kobj, &uuid_attribute_group); 607 if (result) 608 goto free_rel_misc; 609 610 if (acpi_has_method(priv->adev->handle, "IMOK")) { 611 result = sysfs_create_group(&pdev->dev.kobj, &imok_attribute_group); 612 if (result) 613 goto free_imok; 614 } 615 616 if (!ZERO_OR_NULL_PTR(priv->data_vault)) { 617 result = device_create_bin_file(&pdev->dev, &bin_attr_data_vault); 618 if (result) 619 goto free_uuid; 620 } 621 622 result = acpi_install_notify_handler( 623 priv->adev->handle, ACPI_DEVICE_NOTIFY, int3400_notify, 624 (void *)priv); 625 if (result) 626 goto free_sysfs; 627 628 result = production_mode_init(priv); 629 if (result) 630 goto free_notify; 631 632 return 0; 633 634 free_notify: 635 acpi_remove_notify_handler(priv->adev->handle, ACPI_DEVICE_NOTIFY, 636 int3400_notify); 637 free_sysfs: 638 cleanup_odvp(priv); 639 if (!ZERO_OR_NULL_PTR(priv->data_vault)) { 640 device_remove_bin_file(&pdev->dev, &bin_attr_data_vault); 641 kfree(priv->data_vault); 642 } 643 free_uuid: 644 sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group); 645 free_imok: 646 sysfs_remove_group(&pdev->dev.kobj, &imok_attribute_group); 647 free_rel_misc: 648 if (!priv->rel_misc_dev_res) 649 acpi_thermal_rel_misc_device_remove(priv->adev->handle); 650 thermal_zone_device_unregister(priv->thermal); 651 free_art_trt: 652 kfree(priv->trts); 653 kfree(priv->arts); 654 free_priv: 655 kfree(priv); 656 return result; 657 } 658 659 static void int3400_thermal_remove(struct platform_device *pdev) 660 { 661 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev); 662 663 production_mode_exit(priv); 664 665 acpi_remove_notify_handler( 666 priv->adev->handle, ACPI_DEVICE_NOTIFY, 667 int3400_notify); 668 669 cleanup_odvp(priv); 670 671 if (!priv->rel_misc_dev_res) 672 acpi_thermal_rel_misc_device_remove(priv->adev->handle); 673 674 if (!ZERO_OR_NULL_PTR(priv->data_vault)) 675 device_remove_bin_file(&pdev->dev, &bin_attr_data_vault); 676 sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group); 677 sysfs_remove_group(&pdev->dev.kobj, &imok_attribute_group); 678 thermal_zone_device_unregister(priv->thermal); 679 kfree(priv->data_vault); 680 kfree(priv->trts); 681 kfree(priv->arts); 682 kfree(priv); 683 } 684 685 static const struct acpi_device_id int3400_thermal_match[] = { 686 {"INT3400", 0}, 687 {"INTC1040", 0}, 688 {"INTC1041", 0}, 689 {"INTC1042", 0}, 690 {"INTC1068", 0}, 691 {"INTC10A0", 0}, 692 {"INTC10D4", 0}, 693 {} 694 }; 695 696 MODULE_DEVICE_TABLE(acpi, int3400_thermal_match); 697 698 static struct platform_driver int3400_thermal_driver = { 699 .probe = int3400_thermal_probe, 700 .remove = int3400_thermal_remove, 701 .driver = { 702 .name = "int3400 thermal", 703 .acpi_match_table = ACPI_PTR(int3400_thermal_match), 704 }, 705 }; 706 707 module_platform_driver(int3400_thermal_driver); 708 709 MODULE_DESCRIPTION("INT3400 Thermal driver"); 710 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>"); 711 MODULE_LICENSE("GPL"); 712