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