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