1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * thermal.c - Generic Thermal Management Sysfs support. 4 * 5 * Copyright (C) 2008 Intel Corp 6 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com> 7 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com> 8 */ 9 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 12 #include <linux/device.h> 13 #include <linux/err.h> 14 #include <linux/export.h> 15 #include <linux/slab.h> 16 #include <linux/kdev_t.h> 17 #include <linux/idr.h> 18 #include <linux/list_sort.h> 19 #include <linux/thermal.h> 20 #include <linux/reboot.h> 21 #include <linux/string.h> 22 #include <linux/of.h> 23 #include <linux/suspend.h> 24 25 #define CREATE_TRACE_POINTS 26 #include "thermal_trace.h" 27 28 #include "thermal_core.h" 29 #include "thermal_hwmon.h" 30 31 static DEFINE_IDA(thermal_tz_ida); 32 static DEFINE_IDA(thermal_cdev_ida); 33 34 static LIST_HEAD(thermal_tz_list); 35 static LIST_HEAD(thermal_cdev_list); 36 static LIST_HEAD(thermal_governor_list); 37 38 static DEFINE_MUTEX(thermal_list_lock); 39 static DEFINE_MUTEX(thermal_governor_lock); 40 41 static struct thermal_governor *def_governor; 42 43 /* 44 * Governor section: set of functions to handle thermal governors 45 * 46 * Functions to help in the life cycle of thermal governors within 47 * the thermal core and by the thermal governor code. 48 */ 49 50 static struct thermal_governor *__find_governor(const char *name) 51 { 52 struct thermal_governor *pos; 53 54 if (!name || !name[0]) 55 return def_governor; 56 57 list_for_each_entry(pos, &thermal_governor_list, governor_list) 58 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH)) 59 return pos; 60 61 return NULL; 62 } 63 64 /** 65 * bind_previous_governor() - bind the previous governor of the thermal zone 66 * @tz: a valid pointer to a struct thermal_zone_device 67 * @failed_gov_name: the name of the governor that failed to register 68 * 69 * Register the previous governor of the thermal zone after a new 70 * governor has failed to be bound. 71 */ 72 static void bind_previous_governor(struct thermal_zone_device *tz, 73 const char *failed_gov_name) 74 { 75 if (tz->governor && tz->governor->bind_to_tz) { 76 if (tz->governor->bind_to_tz(tz)) { 77 dev_err(&tz->device, 78 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n", 79 failed_gov_name, tz->governor->name, tz->type); 80 tz->governor = NULL; 81 } 82 } 83 } 84 85 /** 86 * thermal_set_governor() - Switch to another governor 87 * @tz: a valid pointer to a struct thermal_zone_device 88 * @new_gov: pointer to the new governor 89 * 90 * Change the governor of thermal zone @tz. 91 * 92 * Return: 0 on success, an error if the new governor's bind_to_tz() failed. 93 */ 94 static int thermal_set_governor(struct thermal_zone_device *tz, 95 struct thermal_governor *new_gov) 96 { 97 int ret = 0; 98 99 if (tz->governor && tz->governor->unbind_from_tz) 100 tz->governor->unbind_from_tz(tz); 101 102 if (new_gov && new_gov->bind_to_tz) { 103 ret = new_gov->bind_to_tz(tz); 104 if (ret) { 105 bind_previous_governor(tz, new_gov->name); 106 107 return ret; 108 } 109 } 110 111 tz->governor = new_gov; 112 113 return ret; 114 } 115 116 int thermal_register_governor(struct thermal_governor *governor) 117 { 118 int err; 119 const char *name; 120 struct thermal_zone_device *pos; 121 122 if (!governor) 123 return -EINVAL; 124 125 mutex_lock(&thermal_governor_lock); 126 127 err = -EBUSY; 128 if (!__find_governor(governor->name)) { 129 bool match_default; 130 131 err = 0; 132 list_add(&governor->governor_list, &thermal_governor_list); 133 match_default = !strncmp(governor->name, 134 DEFAULT_THERMAL_GOVERNOR, 135 THERMAL_NAME_LENGTH); 136 137 if (!def_governor && match_default) 138 def_governor = governor; 139 } 140 141 mutex_lock(&thermal_list_lock); 142 143 list_for_each_entry(pos, &thermal_tz_list, node) { 144 /* 145 * only thermal zones with specified tz->tzp->governor_name 146 * may run with tz->govenor unset 147 */ 148 if (pos->governor) 149 continue; 150 151 name = pos->tzp->governor_name; 152 153 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) { 154 int ret; 155 156 ret = thermal_set_governor(pos, governor); 157 if (ret) 158 dev_err(&pos->device, 159 "Failed to set governor %s for thermal zone %s: %d\n", 160 governor->name, pos->type, ret); 161 } 162 } 163 164 mutex_unlock(&thermal_list_lock); 165 mutex_unlock(&thermal_governor_lock); 166 167 return err; 168 } 169 170 void thermal_unregister_governor(struct thermal_governor *governor) 171 { 172 struct thermal_zone_device *pos; 173 174 if (!governor) 175 return; 176 177 mutex_lock(&thermal_governor_lock); 178 179 if (!__find_governor(governor->name)) 180 goto exit; 181 182 mutex_lock(&thermal_list_lock); 183 184 list_for_each_entry(pos, &thermal_tz_list, node) { 185 if (!strncasecmp(pos->governor->name, governor->name, 186 THERMAL_NAME_LENGTH)) 187 thermal_set_governor(pos, NULL); 188 } 189 190 mutex_unlock(&thermal_list_lock); 191 list_del(&governor->governor_list); 192 exit: 193 mutex_unlock(&thermal_governor_lock); 194 } 195 196 int thermal_zone_device_set_policy(struct thermal_zone_device *tz, 197 char *policy) 198 { 199 struct thermal_governor *gov; 200 int ret = -EINVAL; 201 202 mutex_lock(&thermal_governor_lock); 203 mutex_lock(&tz->lock); 204 205 gov = __find_governor(strim(policy)); 206 if (!gov) 207 goto exit; 208 209 ret = thermal_set_governor(tz, gov); 210 211 exit: 212 mutex_unlock(&tz->lock); 213 mutex_unlock(&thermal_governor_lock); 214 215 thermal_notify_tz_gov_change(tz, policy); 216 217 return ret; 218 } 219 220 int thermal_build_list_of_policies(char *buf) 221 { 222 struct thermal_governor *pos; 223 ssize_t count = 0; 224 225 mutex_lock(&thermal_governor_lock); 226 227 list_for_each_entry(pos, &thermal_governor_list, governor_list) { 228 count += sysfs_emit_at(buf, count, "%s ", pos->name); 229 } 230 count += sysfs_emit_at(buf, count, "\n"); 231 232 mutex_unlock(&thermal_governor_lock); 233 234 return count; 235 } 236 237 static void __init thermal_unregister_governors(void) 238 { 239 struct thermal_governor **governor; 240 241 for_each_governor_table(governor) 242 thermal_unregister_governor(*governor); 243 } 244 245 static int __init thermal_register_governors(void) 246 { 247 int ret = 0; 248 struct thermal_governor **governor; 249 250 for_each_governor_table(governor) { 251 ret = thermal_register_governor(*governor); 252 if (ret) { 253 pr_err("Failed to register governor: '%s'", 254 (*governor)->name); 255 break; 256 } 257 258 pr_info("Registered thermal governor '%s'", 259 (*governor)->name); 260 } 261 262 if (ret) { 263 struct thermal_governor **gov; 264 265 for_each_governor_table(gov) { 266 if (gov == governor) 267 break; 268 thermal_unregister_governor(*gov); 269 } 270 } 271 272 return ret; 273 } 274 275 /* 276 * Zone update section: main control loop applied to each zone while monitoring 277 * in polling mode. The monitoring is done using a workqueue. 278 * Same update may be done on a zone by calling thermal_zone_device_update(). 279 * 280 * An update means: 281 * - Non-critical trips will invoke the governor responsible for that zone; 282 * - Hot trips will produce a notification to userspace; 283 * - Critical trip point will cause a system shutdown. 284 */ 285 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, 286 unsigned long delay) 287 { 288 if (delay) 289 mod_delayed_work(system_freezable_power_efficient_wq, 290 &tz->poll_queue, delay); 291 else 292 cancel_delayed_work(&tz->poll_queue); 293 } 294 295 static void monitor_thermal_zone(struct thermal_zone_device *tz) 296 { 297 if (tz->mode != THERMAL_DEVICE_ENABLED) 298 thermal_zone_device_set_polling(tz, 0); 299 else if (tz->passive > 0) 300 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies); 301 else if (tz->polling_delay_jiffies) 302 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies); 303 } 304 305 static struct thermal_governor *thermal_get_tz_governor(struct thermal_zone_device *tz) 306 { 307 if (tz->governor) 308 return tz->governor; 309 310 return def_governor; 311 } 312 313 void thermal_governor_update_tz(struct thermal_zone_device *tz, 314 enum thermal_notify_event reason) 315 { 316 if (!tz->governor || !tz->governor->update_tz) 317 return; 318 319 tz->governor->update_tz(tz, reason); 320 } 321 322 static void thermal_zone_device_halt(struct thermal_zone_device *tz, bool shutdown) 323 { 324 /* 325 * poweroff_delay_ms must be a carefully profiled positive value. 326 * Its a must for forced_emergency_poweroff_work to be scheduled. 327 */ 328 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS; 329 const char *msg = "Temperature too high"; 330 331 dev_emerg(&tz->device, "%s: critical temperature reached\n", tz->type); 332 333 if (shutdown) 334 hw_protection_shutdown(msg, poweroff_delay_ms); 335 else 336 hw_protection_reboot(msg, poweroff_delay_ms); 337 } 338 339 void thermal_zone_device_critical(struct thermal_zone_device *tz) 340 { 341 thermal_zone_device_halt(tz, true); 342 } 343 EXPORT_SYMBOL(thermal_zone_device_critical); 344 345 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz) 346 { 347 thermal_zone_device_halt(tz, false); 348 } 349 350 static void handle_critical_trips(struct thermal_zone_device *tz, 351 const struct thermal_trip *trip) 352 { 353 trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type); 354 355 if (trip->type == THERMAL_TRIP_CRITICAL) 356 tz->ops.critical(tz); 357 else if (tz->ops.hot) 358 tz->ops.hot(tz); 359 } 360 361 static void handle_thermal_trip(struct thermal_zone_device *tz, 362 struct thermal_trip_desc *td, 363 struct list_head *way_up_list, 364 struct list_head *way_down_list) 365 { 366 const struct thermal_trip *trip = &td->trip; 367 int old_threshold; 368 369 if (trip->temperature == THERMAL_TEMP_INVALID) 370 return; 371 372 /* 373 * If the trip temperature or hysteresis has been updated recently, 374 * the threshold needs to be computed again using the new values. 375 * However, its initial value still reflects the old ones and that 376 * is what needs to be compared with the previous zone temperature 377 * to decide which action to take. 378 */ 379 old_threshold = td->threshold; 380 td->threshold = trip->temperature; 381 382 if (tz->last_temperature >= old_threshold && 383 tz->last_temperature != THERMAL_TEMP_INVALID) { 384 /* 385 * Mitigation is under way, so it needs to stop if the zone 386 * temperature falls below the low temperature of the trip. 387 * In that case, the trip temperature becomes the new threshold. 388 */ 389 if (tz->temperature < trip->temperature - trip->hysteresis) { 390 list_add(&td->notify_list_node, way_down_list); 391 td->notify_temp = trip->temperature - trip->hysteresis; 392 393 if (trip->type == THERMAL_TRIP_PASSIVE) { 394 tz->passive--; 395 WARN_ON(tz->passive < 0); 396 } 397 } else { 398 td->threshold -= trip->hysteresis; 399 } 400 } else if (tz->temperature >= trip->temperature) { 401 /* 402 * There is no mitigation under way, so it needs to be started 403 * if the zone temperature exceeds the trip one. The new 404 * threshold is then set to the low temperature of the trip. 405 */ 406 list_add_tail(&td->notify_list_node, way_up_list); 407 td->notify_temp = trip->temperature; 408 td->threshold -= trip->hysteresis; 409 410 if (trip->type == THERMAL_TRIP_PASSIVE) 411 tz->passive++; 412 else if (trip->type == THERMAL_TRIP_CRITICAL || 413 trip->type == THERMAL_TRIP_HOT) 414 handle_critical_trips(tz, trip); 415 } 416 } 417 418 static void update_temperature(struct thermal_zone_device *tz) 419 { 420 int temp, ret; 421 422 ret = __thermal_zone_get_temp(tz, &temp); 423 if (ret) { 424 if (ret != -EAGAIN) 425 dev_warn(&tz->device, 426 "failed to read out thermal zone (%d)\n", 427 ret); 428 return; 429 } 430 431 tz->last_temperature = tz->temperature; 432 tz->temperature = temp; 433 434 trace_thermal_temperature(tz); 435 436 thermal_genl_sampling_temp(tz->id, temp); 437 } 438 439 static void thermal_zone_device_check(struct work_struct *work) 440 { 441 struct thermal_zone_device *tz = container_of(work, struct 442 thermal_zone_device, 443 poll_queue.work); 444 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 445 } 446 447 static void thermal_zone_device_init(struct thermal_zone_device *tz) 448 { 449 struct thermal_instance *pos; 450 451 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check); 452 453 tz->temperature = THERMAL_TEMP_INVALID; 454 tz->passive = 0; 455 tz->prev_low_trip = -INT_MAX; 456 tz->prev_high_trip = INT_MAX; 457 list_for_each_entry(pos, &tz->thermal_instances, tz_node) 458 pos->initialized = false; 459 } 460 461 static void thermal_governor_trip_crossed(struct thermal_governor *governor, 462 struct thermal_zone_device *tz, 463 const struct thermal_trip *trip, 464 bool crossed_up) 465 { 466 if (governor->trip_crossed) 467 governor->trip_crossed(tz, trip, crossed_up); 468 } 469 470 static void thermal_trip_crossed(struct thermal_zone_device *tz, 471 const struct thermal_trip *trip, 472 struct thermal_governor *governor, 473 bool crossed_up) 474 { 475 if (crossed_up) { 476 thermal_notify_tz_trip_up(tz, trip); 477 thermal_debug_tz_trip_up(tz, trip); 478 } else { 479 thermal_notify_tz_trip_down(tz, trip); 480 thermal_debug_tz_trip_down(tz, trip); 481 } 482 thermal_governor_trip_crossed(governor, tz, trip, crossed_up); 483 } 484 485 static int thermal_trip_notify_cmp(void *ascending, const struct list_head *a, 486 const struct list_head *b) 487 { 488 struct thermal_trip_desc *tda = container_of(a, struct thermal_trip_desc, 489 notify_list_node); 490 struct thermal_trip_desc *tdb = container_of(b, struct thermal_trip_desc, 491 notify_list_node); 492 int ret = tdb->notify_temp - tda->notify_temp; 493 494 return ascending ? ret : -ret; 495 } 496 497 void __thermal_zone_device_update(struct thermal_zone_device *tz, 498 enum thermal_notify_event event) 499 { 500 struct thermal_governor *governor = thermal_get_tz_governor(tz); 501 struct thermal_trip_desc *td; 502 LIST_HEAD(way_down_list); 503 LIST_HEAD(way_up_list); 504 505 if (tz->suspended) 506 return; 507 508 if (!thermal_zone_device_is_enabled(tz)) 509 return; 510 511 update_temperature(tz); 512 513 if (tz->temperature == THERMAL_TEMP_INVALID) 514 return; 515 516 __thermal_zone_set_trips(tz); 517 518 tz->notify_event = event; 519 520 for_each_trip_desc(tz, td) 521 handle_thermal_trip(tz, td, &way_up_list, &way_down_list); 522 523 list_sort(&way_up_list, &way_up_list, thermal_trip_notify_cmp); 524 list_for_each_entry(td, &way_up_list, notify_list_node) 525 thermal_trip_crossed(tz, &td->trip, governor, true); 526 527 list_sort(NULL, &way_down_list, thermal_trip_notify_cmp); 528 list_for_each_entry(td, &way_down_list, notify_list_node) 529 thermal_trip_crossed(tz, &td->trip, governor, false); 530 531 if (governor->manage) 532 governor->manage(tz); 533 534 thermal_debug_update_trip_stats(tz); 535 536 monitor_thermal_zone(tz); 537 } 538 539 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz, 540 enum thermal_device_mode mode) 541 { 542 int ret = 0; 543 544 mutex_lock(&tz->lock); 545 546 /* do nothing if mode isn't changing */ 547 if (mode == tz->mode) { 548 mutex_unlock(&tz->lock); 549 550 return ret; 551 } 552 553 if (tz->ops.change_mode) 554 ret = tz->ops.change_mode(tz, mode); 555 556 if (!ret) 557 tz->mode = mode; 558 559 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 560 561 mutex_unlock(&tz->lock); 562 563 if (mode == THERMAL_DEVICE_ENABLED) 564 thermal_notify_tz_enable(tz); 565 else 566 thermal_notify_tz_disable(tz); 567 568 return ret; 569 } 570 571 int thermal_zone_device_enable(struct thermal_zone_device *tz) 572 { 573 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED); 574 } 575 EXPORT_SYMBOL_GPL(thermal_zone_device_enable); 576 577 int thermal_zone_device_disable(struct thermal_zone_device *tz) 578 { 579 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED); 580 } 581 EXPORT_SYMBOL_GPL(thermal_zone_device_disable); 582 583 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz) 584 { 585 lockdep_assert_held(&tz->lock); 586 587 return tz->mode == THERMAL_DEVICE_ENABLED; 588 } 589 590 static bool thermal_zone_is_present(struct thermal_zone_device *tz) 591 { 592 return !list_empty(&tz->node); 593 } 594 595 void thermal_zone_device_update(struct thermal_zone_device *tz, 596 enum thermal_notify_event event) 597 { 598 mutex_lock(&tz->lock); 599 if (thermal_zone_is_present(tz)) 600 __thermal_zone_device_update(tz, event); 601 mutex_unlock(&tz->lock); 602 } 603 EXPORT_SYMBOL_GPL(thermal_zone_device_update); 604 605 void thermal_zone_trip_down(struct thermal_zone_device *tz, 606 const struct thermal_trip *trip) 607 { 608 thermal_trip_crossed(tz, trip, thermal_get_tz_governor(tz), false); 609 } 610 611 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), 612 void *data) 613 { 614 struct thermal_governor *gov; 615 int ret = 0; 616 617 mutex_lock(&thermal_governor_lock); 618 list_for_each_entry(gov, &thermal_governor_list, governor_list) { 619 ret = cb(gov, data); 620 if (ret) 621 break; 622 } 623 mutex_unlock(&thermal_governor_lock); 624 625 return ret; 626 } 627 628 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *, 629 void *), void *data) 630 { 631 struct thermal_cooling_device *cdev; 632 int ret = 0; 633 634 mutex_lock(&thermal_list_lock); 635 list_for_each_entry(cdev, &thermal_cdev_list, node) { 636 ret = cb(cdev, data); 637 if (ret) 638 break; 639 } 640 mutex_unlock(&thermal_list_lock); 641 642 return ret; 643 } 644 645 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *), 646 void *data) 647 { 648 struct thermal_zone_device *tz; 649 int ret = 0; 650 651 mutex_lock(&thermal_list_lock); 652 list_for_each_entry(tz, &thermal_tz_list, node) { 653 ret = cb(tz, data); 654 if (ret) 655 break; 656 } 657 mutex_unlock(&thermal_list_lock); 658 659 return ret; 660 } 661 662 struct thermal_zone_device *thermal_zone_get_by_id(int id) 663 { 664 struct thermal_zone_device *tz, *match = NULL; 665 666 mutex_lock(&thermal_list_lock); 667 list_for_each_entry(tz, &thermal_tz_list, node) { 668 if (tz->id == id) { 669 match = tz; 670 break; 671 } 672 } 673 mutex_unlock(&thermal_list_lock); 674 675 return match; 676 } 677 678 /* 679 * Device management section: cooling devices, zones devices, and binding 680 * 681 * Set of functions provided by the thermal core for: 682 * - cooling devices lifecycle: registration, unregistration, 683 * binding, and unbinding. 684 * - thermal zone devices lifecycle: registration, unregistration, 685 * binding, and unbinding. 686 */ 687 688 /** 689 * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone 690 * @tz: pointer to struct thermal_zone_device 691 * @trip: trip point the cooling devices is associated with in this zone. 692 * @cdev: pointer to struct thermal_cooling_device 693 * @upper: the Maximum cooling state for this trip point. 694 * THERMAL_NO_LIMIT means no upper limit, 695 * and the cooling device can be in max_state. 696 * @lower: the Minimum cooling state can be used for this trip point. 697 * THERMAL_NO_LIMIT means no lower limit, 698 * and the cooling device can be in cooling state 0. 699 * @weight: The weight of the cooling device to be bound to the 700 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the 701 * default value 702 * 703 * This interface function bind a thermal cooling device to the certain trip 704 * point of a thermal zone device. 705 * This function is usually called in the thermal zone device .bind callback. 706 * 707 * Return: 0 on success, the proper error value otherwise. 708 */ 709 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz, 710 const struct thermal_trip *trip, 711 struct thermal_cooling_device *cdev, 712 unsigned long upper, unsigned long lower, 713 unsigned int weight) 714 { 715 struct thermal_instance *dev; 716 struct thermal_instance *pos; 717 struct thermal_zone_device *pos1; 718 struct thermal_cooling_device *pos2; 719 bool upper_no_limit; 720 int result; 721 722 list_for_each_entry(pos1, &thermal_tz_list, node) { 723 if (pos1 == tz) 724 break; 725 } 726 list_for_each_entry(pos2, &thermal_cdev_list, node) { 727 if (pos2 == cdev) 728 break; 729 } 730 731 if (tz != pos1 || cdev != pos2) 732 return -EINVAL; 733 734 /* lower default 0, upper default max_state */ 735 lower = lower == THERMAL_NO_LIMIT ? 0 : lower; 736 737 if (upper == THERMAL_NO_LIMIT) { 738 upper = cdev->max_state; 739 upper_no_limit = true; 740 } else { 741 upper_no_limit = false; 742 } 743 744 if (lower > upper || upper > cdev->max_state) 745 return -EINVAL; 746 747 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 748 if (!dev) 749 return -ENOMEM; 750 dev->tz = tz; 751 dev->cdev = cdev; 752 dev->trip = trip; 753 dev->upper = upper; 754 dev->upper_no_limit = upper_no_limit; 755 dev->lower = lower; 756 dev->target = THERMAL_NO_TARGET; 757 dev->weight = weight; 758 759 result = ida_alloc(&tz->ida, GFP_KERNEL); 760 if (result < 0) 761 goto free_mem; 762 763 dev->id = result; 764 sprintf(dev->name, "cdev%d", dev->id); 765 result = 766 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); 767 if (result) 768 goto release_ida; 769 770 snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point", 771 dev->id); 772 sysfs_attr_init(&dev->attr.attr); 773 dev->attr.attr.name = dev->attr_name; 774 dev->attr.attr.mode = 0444; 775 dev->attr.show = trip_point_show; 776 result = device_create_file(&tz->device, &dev->attr); 777 if (result) 778 goto remove_symbol_link; 779 780 snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name), 781 "cdev%d_weight", dev->id); 782 sysfs_attr_init(&dev->weight_attr.attr); 783 dev->weight_attr.attr.name = dev->weight_attr_name; 784 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO; 785 dev->weight_attr.show = weight_show; 786 dev->weight_attr.store = weight_store; 787 result = device_create_file(&tz->device, &dev->weight_attr); 788 if (result) 789 goto remove_trip_file; 790 791 mutex_lock(&tz->lock); 792 mutex_lock(&cdev->lock); 793 list_for_each_entry(pos, &tz->thermal_instances, tz_node) 794 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { 795 result = -EEXIST; 796 break; 797 } 798 if (!result) { 799 list_add_tail(&dev->tz_node, &tz->thermal_instances); 800 list_add_tail(&dev->cdev_node, &cdev->thermal_instances); 801 atomic_set(&tz->need_update, 1); 802 803 thermal_governor_update_tz(tz, THERMAL_TZ_BIND_CDEV); 804 } 805 mutex_unlock(&cdev->lock); 806 mutex_unlock(&tz->lock); 807 808 if (!result) 809 return 0; 810 811 device_remove_file(&tz->device, &dev->weight_attr); 812 remove_trip_file: 813 device_remove_file(&tz->device, &dev->attr); 814 remove_symbol_link: 815 sysfs_remove_link(&tz->device.kobj, dev->name); 816 release_ida: 817 ida_free(&tz->ida, dev->id); 818 free_mem: 819 kfree(dev); 820 return result; 821 } 822 EXPORT_SYMBOL_GPL(thermal_bind_cdev_to_trip); 823 824 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, 825 int trip_index, 826 struct thermal_cooling_device *cdev, 827 unsigned long upper, unsigned long lower, 828 unsigned int weight) 829 { 830 if (trip_index < 0 || trip_index >= tz->num_trips) 831 return -EINVAL; 832 833 return thermal_bind_cdev_to_trip(tz, &tz->trips[trip_index].trip, cdev, 834 upper, lower, weight); 835 } 836 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device); 837 838 /** 839 * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone. 840 * @tz: pointer to a struct thermal_zone_device. 841 * @trip: trip point the cooling devices is associated with in this zone. 842 * @cdev: pointer to a struct thermal_cooling_device. 843 * 844 * This interface function unbind a thermal cooling device from the certain 845 * trip point of a thermal zone device. 846 * This function is usually called in the thermal zone device .unbind callback. 847 * 848 * Return: 0 on success, the proper error value otherwise. 849 */ 850 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz, 851 const struct thermal_trip *trip, 852 struct thermal_cooling_device *cdev) 853 { 854 struct thermal_instance *pos, *next; 855 856 mutex_lock(&tz->lock); 857 mutex_lock(&cdev->lock); 858 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) { 859 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { 860 list_del(&pos->tz_node); 861 list_del(&pos->cdev_node); 862 863 thermal_governor_update_tz(tz, THERMAL_TZ_UNBIND_CDEV); 864 865 mutex_unlock(&cdev->lock); 866 mutex_unlock(&tz->lock); 867 goto unbind; 868 } 869 } 870 mutex_unlock(&cdev->lock); 871 mutex_unlock(&tz->lock); 872 873 return -ENODEV; 874 875 unbind: 876 device_remove_file(&tz->device, &pos->weight_attr); 877 device_remove_file(&tz->device, &pos->attr); 878 sysfs_remove_link(&tz->device.kobj, pos->name); 879 ida_free(&tz->ida, pos->id); 880 kfree(pos); 881 return 0; 882 } 883 EXPORT_SYMBOL_GPL(thermal_unbind_cdev_from_trip); 884 885 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, 886 int trip_index, 887 struct thermal_cooling_device *cdev) 888 { 889 if (trip_index < 0 || trip_index >= tz->num_trips) 890 return -EINVAL; 891 892 return thermal_unbind_cdev_from_trip(tz, &tz->trips[trip_index].trip, cdev); 893 } 894 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device); 895 896 static void thermal_release(struct device *dev) 897 { 898 struct thermal_zone_device *tz; 899 struct thermal_cooling_device *cdev; 900 901 if (!strncmp(dev_name(dev), "thermal_zone", 902 sizeof("thermal_zone") - 1)) { 903 tz = to_thermal_zone(dev); 904 thermal_zone_destroy_device_groups(tz); 905 mutex_destroy(&tz->lock); 906 complete(&tz->removal); 907 } else if (!strncmp(dev_name(dev), "cooling_device", 908 sizeof("cooling_device") - 1)) { 909 cdev = to_cooling_device(dev); 910 thermal_cooling_device_destroy_sysfs(cdev); 911 kfree_const(cdev->type); 912 ida_free(&thermal_cdev_ida, cdev->id); 913 kfree(cdev); 914 } 915 } 916 917 static struct class *thermal_class; 918 919 static inline 920 void print_bind_err_msg(struct thermal_zone_device *tz, 921 struct thermal_cooling_device *cdev, int ret) 922 { 923 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n", 924 tz->type, cdev->type, ret); 925 } 926 927 static void bind_cdev(struct thermal_cooling_device *cdev) 928 { 929 int ret; 930 struct thermal_zone_device *pos = NULL; 931 932 list_for_each_entry(pos, &thermal_tz_list, node) { 933 if (pos->ops.bind) { 934 ret = pos->ops.bind(pos, cdev); 935 if (ret) 936 print_bind_err_msg(pos, cdev, ret); 937 } 938 } 939 } 940 941 /** 942 * __thermal_cooling_device_register() - register a new thermal cooling device 943 * @np: a pointer to a device tree node. 944 * @type: the thermal cooling device type. 945 * @devdata: device private data. 946 * @ops: standard thermal cooling devices callbacks. 947 * 948 * This interface function adds a new thermal cooling device (fan/processor/...) 949 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself 950 * to all the thermal zone devices registered at the same time. 951 * It also gives the opportunity to link the cooling device to a device tree 952 * node, so that it can be bound to a thermal zone created out of device tree. 953 * 954 * Return: a pointer to the created struct thermal_cooling_device or an 955 * ERR_PTR. Caller must check return value with IS_ERR*() helpers. 956 */ 957 static struct thermal_cooling_device * 958 __thermal_cooling_device_register(struct device_node *np, 959 const char *type, void *devdata, 960 const struct thermal_cooling_device_ops *ops) 961 { 962 struct thermal_cooling_device *cdev; 963 struct thermal_zone_device *pos = NULL; 964 unsigned long current_state; 965 int id, ret; 966 967 if (!ops || !ops->get_max_state || !ops->get_cur_state || 968 !ops->set_cur_state) 969 return ERR_PTR(-EINVAL); 970 971 if (!thermal_class) 972 return ERR_PTR(-ENODEV); 973 974 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 975 if (!cdev) 976 return ERR_PTR(-ENOMEM); 977 978 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL); 979 if (ret < 0) 980 goto out_kfree_cdev; 981 cdev->id = ret; 982 id = ret; 983 984 cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL); 985 if (!cdev->type) { 986 ret = -ENOMEM; 987 goto out_ida_remove; 988 } 989 990 mutex_init(&cdev->lock); 991 INIT_LIST_HEAD(&cdev->thermal_instances); 992 cdev->np = np; 993 cdev->ops = ops; 994 cdev->updated = false; 995 cdev->device.class = thermal_class; 996 cdev->devdata = devdata; 997 998 ret = cdev->ops->get_max_state(cdev, &cdev->max_state); 999 if (ret) 1000 goto out_cdev_type; 1001 1002 /* 1003 * The cooling device's current state is only needed for debug 1004 * initialization below, so a failure to get it does not cause 1005 * the entire cooling device initialization to fail. However, 1006 * the debug will not work for the device if its initial state 1007 * cannot be determined and drivers are responsible for ensuring 1008 * that this will not happen. 1009 */ 1010 ret = cdev->ops->get_cur_state(cdev, ¤t_state); 1011 if (ret) 1012 current_state = ULONG_MAX; 1013 1014 thermal_cooling_device_setup_sysfs(cdev); 1015 1016 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id); 1017 if (ret) 1018 goto out_cooling_dev; 1019 1020 ret = device_register(&cdev->device); 1021 if (ret) { 1022 /* thermal_release() handles rest of the cleanup */ 1023 put_device(&cdev->device); 1024 return ERR_PTR(ret); 1025 } 1026 1027 if (current_state <= cdev->max_state) 1028 thermal_debug_cdev_add(cdev, current_state); 1029 1030 /* Add 'this' new cdev to the global cdev list */ 1031 mutex_lock(&thermal_list_lock); 1032 1033 list_add(&cdev->node, &thermal_cdev_list); 1034 1035 /* Update binding information for 'this' new cdev */ 1036 bind_cdev(cdev); 1037 1038 list_for_each_entry(pos, &thermal_tz_list, node) 1039 if (atomic_cmpxchg(&pos->need_update, 1, 0)) 1040 thermal_zone_device_update(pos, 1041 THERMAL_EVENT_UNSPECIFIED); 1042 1043 mutex_unlock(&thermal_list_lock); 1044 1045 return cdev; 1046 1047 out_cooling_dev: 1048 thermal_cooling_device_destroy_sysfs(cdev); 1049 out_cdev_type: 1050 kfree_const(cdev->type); 1051 out_ida_remove: 1052 ida_free(&thermal_cdev_ida, id); 1053 out_kfree_cdev: 1054 kfree(cdev); 1055 return ERR_PTR(ret); 1056 } 1057 1058 /** 1059 * thermal_cooling_device_register() - register a new thermal cooling device 1060 * @type: the thermal cooling device type. 1061 * @devdata: device private data. 1062 * @ops: standard thermal cooling devices callbacks. 1063 * 1064 * This interface function adds a new thermal cooling device (fan/processor/...) 1065 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself 1066 * to all the thermal zone devices registered at the same time. 1067 * 1068 * Return: a pointer to the created struct thermal_cooling_device or an 1069 * ERR_PTR. Caller must check return value with IS_ERR*() helpers. 1070 */ 1071 struct thermal_cooling_device * 1072 thermal_cooling_device_register(const char *type, void *devdata, 1073 const struct thermal_cooling_device_ops *ops) 1074 { 1075 return __thermal_cooling_device_register(NULL, type, devdata, ops); 1076 } 1077 EXPORT_SYMBOL_GPL(thermal_cooling_device_register); 1078 1079 /** 1080 * thermal_of_cooling_device_register() - register an OF thermal cooling device 1081 * @np: a pointer to a device tree node. 1082 * @type: the thermal cooling device type. 1083 * @devdata: device private data. 1084 * @ops: standard thermal cooling devices callbacks. 1085 * 1086 * This function will register a cooling device with device tree node reference. 1087 * This interface function adds a new thermal cooling device (fan/processor/...) 1088 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself 1089 * to all the thermal zone devices registered at the same time. 1090 * 1091 * Return: a pointer to the created struct thermal_cooling_device or an 1092 * ERR_PTR. Caller must check return value with IS_ERR*() helpers. 1093 */ 1094 struct thermal_cooling_device * 1095 thermal_of_cooling_device_register(struct device_node *np, 1096 const char *type, void *devdata, 1097 const struct thermal_cooling_device_ops *ops) 1098 { 1099 return __thermal_cooling_device_register(np, type, devdata, ops); 1100 } 1101 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register); 1102 1103 static void thermal_cooling_device_release(struct device *dev, void *res) 1104 { 1105 thermal_cooling_device_unregister( 1106 *(struct thermal_cooling_device **)res); 1107 } 1108 1109 /** 1110 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling 1111 * device 1112 * @dev: a valid struct device pointer of a sensor device. 1113 * @np: a pointer to a device tree node. 1114 * @type: the thermal cooling device type. 1115 * @devdata: device private data. 1116 * @ops: standard thermal cooling devices callbacks. 1117 * 1118 * This function will register a cooling device with device tree node reference. 1119 * This interface function adds a new thermal cooling device (fan/processor/...) 1120 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself 1121 * to all the thermal zone devices registered at the same time. 1122 * 1123 * Return: a pointer to the created struct thermal_cooling_device or an 1124 * ERR_PTR. Caller must check return value with IS_ERR*() helpers. 1125 */ 1126 struct thermal_cooling_device * 1127 devm_thermal_of_cooling_device_register(struct device *dev, 1128 struct device_node *np, 1129 char *type, void *devdata, 1130 const struct thermal_cooling_device_ops *ops) 1131 { 1132 struct thermal_cooling_device **ptr, *tcd; 1133 1134 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr), 1135 GFP_KERNEL); 1136 if (!ptr) 1137 return ERR_PTR(-ENOMEM); 1138 1139 tcd = __thermal_cooling_device_register(np, type, devdata, ops); 1140 if (IS_ERR(tcd)) { 1141 devres_free(ptr); 1142 return tcd; 1143 } 1144 1145 *ptr = tcd; 1146 devres_add(dev, ptr); 1147 1148 return tcd; 1149 } 1150 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register); 1151 1152 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev) 1153 { 1154 struct thermal_cooling_device *pos = NULL; 1155 1156 list_for_each_entry(pos, &thermal_cdev_list, node) { 1157 if (pos == cdev) 1158 return true; 1159 } 1160 1161 return false; 1162 } 1163 1164 /** 1165 * thermal_cooling_device_update - Update a cooling device object 1166 * @cdev: Target cooling device. 1167 * 1168 * Update @cdev to reflect a change of the underlying hardware or platform. 1169 * 1170 * Must be called when the maximum cooling state of @cdev becomes invalid and so 1171 * its .get_max_state() callback needs to be run to produce the new maximum 1172 * cooling state value. 1173 */ 1174 void thermal_cooling_device_update(struct thermal_cooling_device *cdev) 1175 { 1176 struct thermal_instance *ti; 1177 unsigned long state; 1178 1179 if (IS_ERR_OR_NULL(cdev)) 1180 return; 1181 1182 /* 1183 * Hold thermal_list_lock throughout the update to prevent the device 1184 * from going away while being updated. 1185 */ 1186 mutex_lock(&thermal_list_lock); 1187 1188 if (!thermal_cooling_device_present(cdev)) 1189 goto unlock_list; 1190 1191 /* 1192 * Update under the cdev lock to prevent the state from being set beyond 1193 * the new limit concurrently. 1194 */ 1195 mutex_lock(&cdev->lock); 1196 1197 if (cdev->ops->get_max_state(cdev, &cdev->max_state)) 1198 goto unlock; 1199 1200 thermal_cooling_device_stats_reinit(cdev); 1201 1202 list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) { 1203 if (ti->upper == cdev->max_state) 1204 continue; 1205 1206 if (ti->upper < cdev->max_state) { 1207 if (ti->upper_no_limit) 1208 ti->upper = cdev->max_state; 1209 1210 continue; 1211 } 1212 1213 ti->upper = cdev->max_state; 1214 if (ti->lower > ti->upper) 1215 ti->lower = ti->upper; 1216 1217 if (ti->target == THERMAL_NO_TARGET) 1218 continue; 1219 1220 if (ti->target > ti->upper) 1221 ti->target = ti->upper; 1222 } 1223 1224 if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state) 1225 goto unlock; 1226 1227 thermal_cooling_device_stats_update(cdev, state); 1228 1229 unlock: 1230 mutex_unlock(&cdev->lock); 1231 1232 unlock_list: 1233 mutex_unlock(&thermal_list_lock); 1234 } 1235 EXPORT_SYMBOL_GPL(thermal_cooling_device_update); 1236 1237 /** 1238 * thermal_cooling_device_unregister - removes a thermal cooling device 1239 * @cdev: the thermal cooling device to remove. 1240 * 1241 * thermal_cooling_device_unregister() must be called when a registered 1242 * thermal cooling device is no longer needed. 1243 */ 1244 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) 1245 { 1246 struct thermal_zone_device *tz; 1247 1248 if (!cdev) 1249 return; 1250 1251 thermal_debug_cdev_remove(cdev); 1252 1253 mutex_lock(&thermal_list_lock); 1254 1255 if (!thermal_cooling_device_present(cdev)) { 1256 mutex_unlock(&thermal_list_lock); 1257 return; 1258 } 1259 1260 list_del(&cdev->node); 1261 1262 /* Unbind all thermal zones associated with 'this' cdev */ 1263 list_for_each_entry(tz, &thermal_tz_list, node) { 1264 if (tz->ops.unbind) 1265 tz->ops.unbind(tz, cdev); 1266 } 1267 1268 mutex_unlock(&thermal_list_lock); 1269 1270 device_unregister(&cdev->device); 1271 } 1272 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); 1273 1274 static void bind_tz(struct thermal_zone_device *tz) 1275 { 1276 int ret; 1277 struct thermal_cooling_device *pos = NULL; 1278 1279 if (!tz->ops.bind) 1280 return; 1281 1282 mutex_lock(&thermal_list_lock); 1283 1284 list_for_each_entry(pos, &thermal_cdev_list, node) { 1285 ret = tz->ops.bind(tz, pos); 1286 if (ret) 1287 print_bind_err_msg(tz, pos, ret); 1288 } 1289 1290 mutex_unlock(&thermal_list_lock); 1291 } 1292 1293 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms) 1294 { 1295 *delay_jiffies = msecs_to_jiffies(delay_ms); 1296 if (delay_ms > 1000) 1297 *delay_jiffies = round_jiffies(*delay_jiffies); 1298 } 1299 1300 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp) 1301 { 1302 const struct thermal_trip_desc *td; 1303 int ret = -EINVAL; 1304 1305 if (tz->ops.get_crit_temp) 1306 return tz->ops.get_crit_temp(tz, temp); 1307 1308 mutex_lock(&tz->lock); 1309 1310 for_each_trip_desc(tz, td) { 1311 const struct thermal_trip *trip = &td->trip; 1312 1313 if (trip->type == THERMAL_TRIP_CRITICAL) { 1314 *temp = trip->temperature; 1315 ret = 0; 1316 break; 1317 } 1318 } 1319 1320 mutex_unlock(&tz->lock); 1321 1322 return ret; 1323 } 1324 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp); 1325 1326 /** 1327 * thermal_zone_device_register_with_trips() - register a new thermal zone device 1328 * @type: the thermal zone device type 1329 * @trips: a pointer to an array of thermal trips 1330 * @num_trips: the number of trip points the thermal zone support 1331 * @devdata: private device data 1332 * @ops: standard thermal zone device callbacks 1333 * @tzp: thermal zone platform parameters 1334 * @passive_delay: number of milliseconds to wait between polls when 1335 * performing passive cooling 1336 * @polling_delay: number of milliseconds to wait between polls when checking 1337 * whether trip points have been crossed (0 for interrupt 1338 * driven systems) 1339 * 1340 * This interface function adds a new thermal zone device (sensor) to 1341 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the 1342 * thermal cooling devices registered at the same time. 1343 * thermal_zone_device_unregister() must be called when the device is no 1344 * longer needed. The passive cooling depends on the .get_trend() return value. 1345 * 1346 * Return: a pointer to the created struct thermal_zone_device or an 1347 * in case of error, an ERR_PTR. Caller must check return value with 1348 * IS_ERR*() helpers. 1349 */ 1350 struct thermal_zone_device * 1351 thermal_zone_device_register_with_trips(const char *type, 1352 const struct thermal_trip *trips, 1353 int num_trips, void *devdata, 1354 const struct thermal_zone_device_ops *ops, 1355 const struct thermal_zone_params *tzp, 1356 int passive_delay, int polling_delay) 1357 { 1358 const struct thermal_trip *trip = trips; 1359 struct thermal_zone_device *tz; 1360 struct thermal_trip_desc *td; 1361 int id; 1362 int result; 1363 struct thermal_governor *governor; 1364 1365 if (!type || strlen(type) == 0) { 1366 pr_err("No thermal zone type defined\n"); 1367 return ERR_PTR(-EINVAL); 1368 } 1369 1370 if (strlen(type) >= THERMAL_NAME_LENGTH) { 1371 pr_err("Thermal zone name (%s) too long, should be under %d chars\n", 1372 type, THERMAL_NAME_LENGTH); 1373 return ERR_PTR(-EINVAL); 1374 } 1375 1376 if (num_trips < 0) { 1377 pr_err("Incorrect number of thermal trips\n"); 1378 return ERR_PTR(-EINVAL); 1379 } 1380 1381 if (!ops || !ops->get_temp) { 1382 pr_err("Thermal zone device ops not defined\n"); 1383 return ERR_PTR(-EINVAL); 1384 } 1385 1386 if (num_trips > 0 && !trips) 1387 return ERR_PTR(-EINVAL); 1388 1389 if (!thermal_class) 1390 return ERR_PTR(-ENODEV); 1391 1392 tz = kzalloc(struct_size(tz, trips, num_trips), GFP_KERNEL); 1393 if (!tz) 1394 return ERR_PTR(-ENOMEM); 1395 1396 if (tzp) { 1397 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL); 1398 if (!tz->tzp) { 1399 result = -ENOMEM; 1400 goto free_tz; 1401 } 1402 } 1403 1404 INIT_LIST_HEAD(&tz->thermal_instances); 1405 INIT_LIST_HEAD(&tz->node); 1406 ida_init(&tz->ida); 1407 mutex_init(&tz->lock); 1408 init_completion(&tz->removal); 1409 init_completion(&tz->resume); 1410 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL); 1411 if (id < 0) { 1412 result = id; 1413 goto free_tzp; 1414 } 1415 1416 tz->id = id; 1417 strscpy(tz->type, type, sizeof(tz->type)); 1418 1419 tz->ops = *ops; 1420 if (!tz->ops.critical) 1421 tz->ops.critical = thermal_zone_device_critical; 1422 1423 tz->device.class = thermal_class; 1424 tz->devdata = devdata; 1425 tz->num_trips = num_trips; 1426 for_each_trip_desc(tz, td) { 1427 td->trip = *trip++; 1428 /* 1429 * Mark all thresholds as invalid to start with even though 1430 * this only matters for the trips that start as invalid and 1431 * become valid later. 1432 */ 1433 td->threshold = INT_MAX; 1434 } 1435 1436 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay); 1437 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay); 1438 1439 /* sys I/F */ 1440 /* Add nodes that are always present via .groups */ 1441 result = thermal_zone_create_device_groups(tz); 1442 if (result) 1443 goto remove_id; 1444 1445 /* A new thermal zone needs to be updated anyway. */ 1446 atomic_set(&tz->need_update, 1); 1447 1448 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id); 1449 if (result) { 1450 thermal_zone_destroy_device_groups(tz); 1451 goto remove_id; 1452 } 1453 result = device_register(&tz->device); 1454 if (result) 1455 goto release_device; 1456 1457 /* Update 'this' zone's governor information */ 1458 mutex_lock(&thermal_governor_lock); 1459 1460 if (tz->tzp) 1461 governor = __find_governor(tz->tzp->governor_name); 1462 else 1463 governor = def_governor; 1464 1465 result = thermal_set_governor(tz, governor); 1466 if (result) { 1467 mutex_unlock(&thermal_governor_lock); 1468 goto unregister; 1469 } 1470 1471 mutex_unlock(&thermal_governor_lock); 1472 1473 if (!tz->tzp || !tz->tzp->no_hwmon) { 1474 result = thermal_add_hwmon_sysfs(tz); 1475 if (result) 1476 goto unregister; 1477 } 1478 1479 mutex_lock(&thermal_list_lock); 1480 mutex_lock(&tz->lock); 1481 list_add_tail(&tz->node, &thermal_tz_list); 1482 mutex_unlock(&tz->lock); 1483 mutex_unlock(&thermal_list_lock); 1484 1485 /* Bind cooling devices for this zone */ 1486 bind_tz(tz); 1487 1488 thermal_zone_device_init(tz); 1489 /* Update the new thermal zone and mark it as already updated. */ 1490 if (atomic_cmpxchg(&tz->need_update, 1, 0)) 1491 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 1492 1493 thermal_notify_tz_create(tz); 1494 1495 thermal_debug_tz_add(tz); 1496 1497 return tz; 1498 1499 unregister: 1500 device_del(&tz->device); 1501 release_device: 1502 put_device(&tz->device); 1503 remove_id: 1504 ida_free(&thermal_tz_ida, id); 1505 free_tzp: 1506 kfree(tz->tzp); 1507 free_tz: 1508 kfree(tz); 1509 return ERR_PTR(result); 1510 } 1511 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips); 1512 1513 struct thermal_zone_device *thermal_tripless_zone_device_register( 1514 const char *type, 1515 void *devdata, 1516 const struct thermal_zone_device_ops *ops, 1517 const struct thermal_zone_params *tzp) 1518 { 1519 return thermal_zone_device_register_with_trips(type, NULL, 0, devdata, 1520 ops, tzp, 0, 0); 1521 } 1522 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register); 1523 1524 void *thermal_zone_device_priv(struct thermal_zone_device *tzd) 1525 { 1526 return tzd->devdata; 1527 } 1528 EXPORT_SYMBOL_GPL(thermal_zone_device_priv); 1529 1530 const char *thermal_zone_device_type(struct thermal_zone_device *tzd) 1531 { 1532 return tzd->type; 1533 } 1534 EXPORT_SYMBOL_GPL(thermal_zone_device_type); 1535 1536 int thermal_zone_device_id(struct thermal_zone_device *tzd) 1537 { 1538 return tzd->id; 1539 } 1540 EXPORT_SYMBOL_GPL(thermal_zone_device_id); 1541 1542 struct device *thermal_zone_device(struct thermal_zone_device *tzd) 1543 { 1544 return &tzd->device; 1545 } 1546 EXPORT_SYMBOL_GPL(thermal_zone_device); 1547 1548 /** 1549 * thermal_zone_device_unregister - removes the registered thermal zone device 1550 * @tz: the thermal zone device to remove 1551 */ 1552 void thermal_zone_device_unregister(struct thermal_zone_device *tz) 1553 { 1554 struct thermal_cooling_device *cdev; 1555 struct thermal_zone_device *pos = NULL; 1556 1557 if (!tz) 1558 return; 1559 1560 thermal_debug_tz_remove(tz); 1561 1562 mutex_lock(&thermal_list_lock); 1563 list_for_each_entry(pos, &thermal_tz_list, node) 1564 if (pos == tz) 1565 break; 1566 if (pos != tz) { 1567 /* thermal zone device not found */ 1568 mutex_unlock(&thermal_list_lock); 1569 return; 1570 } 1571 1572 mutex_lock(&tz->lock); 1573 list_del(&tz->node); 1574 mutex_unlock(&tz->lock); 1575 1576 /* Unbind all cdevs associated with 'this' thermal zone */ 1577 list_for_each_entry(cdev, &thermal_cdev_list, node) 1578 if (tz->ops.unbind) 1579 tz->ops.unbind(tz, cdev); 1580 1581 mutex_unlock(&thermal_list_lock); 1582 1583 cancel_delayed_work_sync(&tz->poll_queue); 1584 1585 thermal_set_governor(tz, NULL); 1586 1587 thermal_remove_hwmon_sysfs(tz); 1588 ida_free(&thermal_tz_ida, tz->id); 1589 ida_destroy(&tz->ida); 1590 1591 device_del(&tz->device); 1592 1593 kfree(tz->tzp); 1594 1595 put_device(&tz->device); 1596 1597 thermal_notify_tz_delete(tz); 1598 1599 wait_for_completion(&tz->removal); 1600 kfree(tz); 1601 } 1602 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister); 1603 1604 /** 1605 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref 1606 * @name: thermal zone name to fetch the temperature 1607 * 1608 * When only one zone is found with the passed name, returns a reference to it. 1609 * 1610 * Return: On success returns a reference to an unique thermal zone with 1611 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid 1612 * paramenters, -ENODEV for not found and -EEXIST for multiple matches). 1613 */ 1614 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name) 1615 { 1616 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL); 1617 unsigned int found = 0; 1618 1619 if (!name) 1620 goto exit; 1621 1622 mutex_lock(&thermal_list_lock); 1623 list_for_each_entry(pos, &thermal_tz_list, node) 1624 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) { 1625 found++; 1626 ref = pos; 1627 } 1628 mutex_unlock(&thermal_list_lock); 1629 1630 /* nothing has been found, thus an error code for it */ 1631 if (found == 0) 1632 ref = ERR_PTR(-ENODEV); 1633 else if (found > 1) 1634 /* Success only when an unique zone is found */ 1635 ref = ERR_PTR(-EEXIST); 1636 1637 exit: 1638 return ref; 1639 } 1640 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name); 1641 1642 static void thermal_zone_device_resume(struct work_struct *work) 1643 { 1644 struct thermal_zone_device *tz; 1645 1646 tz = container_of(work, struct thermal_zone_device, poll_queue.work); 1647 1648 mutex_lock(&tz->lock); 1649 1650 tz->suspended = false; 1651 1652 thermal_zone_device_init(tz); 1653 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 1654 1655 complete(&tz->resume); 1656 tz->resuming = false; 1657 1658 mutex_unlock(&tz->lock); 1659 } 1660 1661 static int thermal_pm_notify(struct notifier_block *nb, 1662 unsigned long mode, void *_unused) 1663 { 1664 struct thermal_zone_device *tz; 1665 1666 switch (mode) { 1667 case PM_HIBERNATION_PREPARE: 1668 case PM_RESTORE_PREPARE: 1669 case PM_SUSPEND_PREPARE: 1670 mutex_lock(&thermal_list_lock); 1671 1672 list_for_each_entry(tz, &thermal_tz_list, node) { 1673 mutex_lock(&tz->lock); 1674 1675 if (tz->resuming) { 1676 /* 1677 * thermal_zone_device_resume() queued up for 1678 * this zone has not acquired the lock yet, so 1679 * release it to let the function run and wait 1680 * util it has done the work. 1681 */ 1682 mutex_unlock(&tz->lock); 1683 1684 wait_for_completion(&tz->resume); 1685 1686 mutex_lock(&tz->lock); 1687 } 1688 1689 tz->suspended = true; 1690 1691 mutex_unlock(&tz->lock); 1692 } 1693 1694 mutex_unlock(&thermal_list_lock); 1695 break; 1696 case PM_POST_HIBERNATION: 1697 case PM_POST_RESTORE: 1698 case PM_POST_SUSPEND: 1699 mutex_lock(&thermal_list_lock); 1700 1701 list_for_each_entry(tz, &thermal_tz_list, node) { 1702 mutex_lock(&tz->lock); 1703 1704 cancel_delayed_work(&tz->poll_queue); 1705 1706 reinit_completion(&tz->resume); 1707 tz->resuming = true; 1708 1709 /* 1710 * Replace the work function with the resume one, which 1711 * will restore the original work function and schedule 1712 * the polling work if needed. 1713 */ 1714 INIT_DELAYED_WORK(&tz->poll_queue, 1715 thermal_zone_device_resume); 1716 /* Queue up the work without a delay. */ 1717 mod_delayed_work(system_freezable_power_efficient_wq, 1718 &tz->poll_queue, 0); 1719 1720 mutex_unlock(&tz->lock); 1721 } 1722 1723 mutex_unlock(&thermal_list_lock); 1724 break; 1725 default: 1726 break; 1727 } 1728 return 0; 1729 } 1730 1731 static struct notifier_block thermal_pm_nb = { 1732 .notifier_call = thermal_pm_notify, 1733 /* 1734 * Run at the lowest priority to avoid interference between the thermal 1735 * zone resume work items spawned by thermal_pm_notify() and the other 1736 * PM notifiers. 1737 */ 1738 .priority = INT_MIN, 1739 }; 1740 1741 static int __init thermal_init(void) 1742 { 1743 int result; 1744 1745 thermal_debug_init(); 1746 1747 result = thermal_netlink_init(); 1748 if (result) 1749 goto error; 1750 1751 result = thermal_register_governors(); 1752 if (result) 1753 goto unregister_netlink; 1754 1755 thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL); 1756 if (!thermal_class) { 1757 result = -ENOMEM; 1758 goto unregister_governors; 1759 } 1760 1761 thermal_class->name = "thermal"; 1762 thermal_class->dev_release = thermal_release; 1763 1764 result = class_register(thermal_class); 1765 if (result) { 1766 kfree(thermal_class); 1767 thermal_class = NULL; 1768 goto unregister_governors; 1769 } 1770 1771 result = register_pm_notifier(&thermal_pm_nb); 1772 if (result) 1773 pr_warn("Thermal: Can not register suspend notifier, return %d\n", 1774 result); 1775 1776 return 0; 1777 1778 unregister_governors: 1779 thermal_unregister_governors(); 1780 unregister_netlink: 1781 thermal_netlink_exit(); 1782 error: 1783 mutex_destroy(&thermal_list_lock); 1784 mutex_destroy(&thermal_governor_lock); 1785 return result; 1786 } 1787 postcore_initcall(thermal_init); 1788