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