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->id, 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 * 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) 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 void handle_non_critical_trips(struct thermal_zone_device *tz, 306 const struct thermal_trip *trip) 307 { 308 tz->governor ? tz->governor->throttle(tz, trip) : 309 def_governor->throttle(tz, trip); 310 } 311 312 void thermal_governor_update_tz(struct thermal_zone_device *tz, 313 enum thermal_notify_event reason) 314 { 315 if (!tz->governor || !tz->governor->update_tz) 316 return; 317 318 tz->governor->update_tz(tz, reason); 319 } 320 321 static void thermal_zone_device_halt(struct thermal_zone_device *tz, bool shutdown) 322 { 323 /* 324 * poweroff_delay_ms must be a carefully profiled positive value. 325 * Its a must for forced_emergency_poweroff_work to be scheduled. 326 */ 327 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS; 328 const char *msg = "Temperature too high"; 329 330 dev_emerg(&tz->device, "%s: critical temperature reached\n", tz->type); 331 332 if (shutdown) 333 hw_protection_shutdown(msg, poweroff_delay_ms); 334 else 335 hw_protection_reboot(msg, poweroff_delay_ms); 336 } 337 338 void thermal_zone_device_critical(struct thermal_zone_device *tz) 339 { 340 thermal_zone_device_halt(tz, true); 341 } 342 EXPORT_SYMBOL(thermal_zone_device_critical); 343 344 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz) 345 { 346 thermal_zone_device_halt(tz, false); 347 } 348 349 static void handle_critical_trips(struct thermal_zone_device *tz, 350 const struct thermal_trip *trip) 351 { 352 /* If we have not crossed the trip_temp, we do not care. */ 353 if (trip->temperature <= 0 || tz->temperature < trip->temperature) 354 return; 355 356 trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type); 357 358 if (trip->type == THERMAL_TRIP_CRITICAL) 359 tz->ops->critical(tz); 360 else if (tz->ops->hot) 361 tz->ops->hot(tz); 362 } 363 364 static void handle_thermal_trip(struct thermal_zone_device *tz, 365 struct thermal_trip *trip) 366 { 367 if (trip->temperature == THERMAL_TEMP_INVALID) 368 return; 369 370 if (tz->last_temperature == THERMAL_TEMP_INVALID) { 371 /* Initialization. */ 372 trip->threshold = trip->temperature; 373 if (tz->temperature >= trip->threshold) 374 trip->threshold -= trip->hysteresis; 375 } else if (tz->last_temperature < trip->threshold) { 376 /* 377 * The trip threshold is equal to the trip temperature, unless 378 * the latter has changed in the meantime. In either case, 379 * the trip is crossed if the current zone temperature is at 380 * least equal to its temperature, but otherwise ensure that 381 * the threshold and the trip temperature will be equal. 382 */ 383 if (tz->temperature >= trip->temperature) { 384 thermal_notify_tz_trip_up(tz->id, 385 thermal_zone_trip_id(tz, trip), 386 tz->temperature); 387 trip->threshold = trip->temperature - trip->hysteresis; 388 } else { 389 trip->threshold = trip->temperature; 390 } 391 } else { 392 /* 393 * The previous zone temperature was above or equal to the trip 394 * threshold, which would be equal to the "low temperature" of 395 * the trip (its temperature minus its hysteresis), unless the 396 * trip temperature or hysteresis had changed. In either case, 397 * the trip is crossed if the current zone temperature is below 398 * the low temperature of the trip, but otherwise ensure that 399 * the trip threshold will be equal to the low temperature of 400 * the trip. 401 */ 402 if (tz->temperature < trip->temperature - trip->hysteresis) { 403 thermal_notify_tz_trip_down(tz->id, 404 thermal_zone_trip_id(tz, trip), 405 tz->temperature); 406 trip->threshold = trip->temperature; 407 } else { 408 trip->threshold = trip->temperature - trip->hysteresis; 409 } 410 } 411 412 if (trip->type == THERMAL_TRIP_CRITICAL || trip->type == THERMAL_TRIP_HOT) 413 handle_critical_trips(tz, trip); 414 else 415 handle_non_critical_trips(tz, trip); 416 } 417 418 static void update_temperature(struct thermal_zone_device *tz) 419 { 420 int temp, ret; 421 422 ret = __thermal_zone_get_temp(tz, &temp); 423 if (ret) { 424 if (ret != -EAGAIN) 425 dev_warn(&tz->device, 426 "failed to read out thermal zone (%d)\n", 427 ret); 428 return; 429 } 430 431 tz->last_temperature = tz->temperature; 432 tz->temperature = temp; 433 434 trace_thermal_temperature(tz); 435 436 thermal_genl_sampling_temp(tz->id, temp); 437 } 438 439 static void thermal_zone_device_check(struct work_struct *work) 440 { 441 struct thermal_zone_device *tz = container_of(work, struct 442 thermal_zone_device, 443 poll_queue.work); 444 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 445 } 446 447 static void thermal_zone_device_init(struct thermal_zone_device *tz) 448 { 449 struct thermal_instance *pos; 450 451 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check); 452 453 tz->temperature = THERMAL_TEMP_INVALID; 454 tz->prev_low_trip = -INT_MAX; 455 tz->prev_high_trip = INT_MAX; 456 list_for_each_entry(pos, &tz->thermal_instances, tz_node) 457 pos->initialized = false; 458 } 459 460 void __thermal_zone_device_update(struct thermal_zone_device *tz, 461 enum thermal_notify_event event) 462 { 463 struct thermal_trip *trip; 464 465 if (tz->suspended) 466 return; 467 468 if (!thermal_zone_device_is_enabled(tz)) 469 return; 470 471 update_temperature(tz); 472 473 __thermal_zone_set_trips(tz); 474 475 tz->notify_event = event; 476 477 for_each_trip(tz, trip) 478 handle_thermal_trip(tz, trip); 479 480 monitor_thermal_zone(tz); 481 } 482 483 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz, 484 enum thermal_device_mode mode) 485 { 486 int ret = 0; 487 488 mutex_lock(&tz->lock); 489 490 /* do nothing if mode isn't changing */ 491 if (mode == tz->mode) { 492 mutex_unlock(&tz->lock); 493 494 return ret; 495 } 496 497 if (tz->ops->change_mode) 498 ret = tz->ops->change_mode(tz, mode); 499 500 if (!ret) 501 tz->mode = mode; 502 503 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 504 505 mutex_unlock(&tz->lock); 506 507 if (mode == THERMAL_DEVICE_ENABLED) 508 thermal_notify_tz_enable(tz->id); 509 else 510 thermal_notify_tz_disable(tz->id); 511 512 return ret; 513 } 514 515 int thermal_zone_device_enable(struct thermal_zone_device *tz) 516 { 517 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED); 518 } 519 EXPORT_SYMBOL_GPL(thermal_zone_device_enable); 520 521 int thermal_zone_device_disable(struct thermal_zone_device *tz) 522 { 523 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED); 524 } 525 EXPORT_SYMBOL_GPL(thermal_zone_device_disable); 526 527 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz) 528 { 529 lockdep_assert_held(&tz->lock); 530 531 return tz->mode == THERMAL_DEVICE_ENABLED; 532 } 533 534 static bool thermal_zone_is_present(struct thermal_zone_device *tz) 535 { 536 return !list_empty(&tz->node); 537 } 538 539 void thermal_zone_device_update(struct thermal_zone_device *tz, 540 enum thermal_notify_event event) 541 { 542 mutex_lock(&tz->lock); 543 if (thermal_zone_is_present(tz)) 544 __thermal_zone_device_update(tz, event); 545 mutex_unlock(&tz->lock); 546 } 547 EXPORT_SYMBOL_GPL(thermal_zone_device_update); 548 549 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), 550 void *data) 551 { 552 struct thermal_governor *gov; 553 int ret = 0; 554 555 mutex_lock(&thermal_governor_lock); 556 list_for_each_entry(gov, &thermal_governor_list, governor_list) { 557 ret = cb(gov, data); 558 if (ret) 559 break; 560 } 561 mutex_unlock(&thermal_governor_lock); 562 563 return ret; 564 } 565 566 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *, 567 void *), void *data) 568 { 569 struct thermal_cooling_device *cdev; 570 int ret = 0; 571 572 mutex_lock(&thermal_list_lock); 573 list_for_each_entry(cdev, &thermal_cdev_list, node) { 574 ret = cb(cdev, data); 575 if (ret) 576 break; 577 } 578 mutex_unlock(&thermal_list_lock); 579 580 return ret; 581 } 582 583 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *), 584 void *data) 585 { 586 struct thermal_zone_device *tz; 587 int ret = 0; 588 589 mutex_lock(&thermal_list_lock); 590 list_for_each_entry(tz, &thermal_tz_list, node) { 591 ret = cb(tz, data); 592 if (ret) 593 break; 594 } 595 mutex_unlock(&thermal_list_lock); 596 597 return ret; 598 } 599 600 struct thermal_zone_device *thermal_zone_get_by_id(int id) 601 { 602 struct thermal_zone_device *tz, *match = NULL; 603 604 mutex_lock(&thermal_list_lock); 605 list_for_each_entry(tz, &thermal_tz_list, node) { 606 if (tz->id == id) { 607 match = tz; 608 break; 609 } 610 } 611 mutex_unlock(&thermal_list_lock); 612 613 return match; 614 } 615 616 /* 617 * Device management section: cooling devices, zones devices, and binding 618 * 619 * Set of functions provided by the thermal core for: 620 * - cooling devices lifecycle: registration, unregistration, 621 * binding, and unbinding. 622 * - thermal zone devices lifecycle: registration, unregistration, 623 * binding, and unbinding. 624 */ 625 626 /** 627 * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone 628 * @tz: pointer to struct thermal_zone_device 629 * @trip: trip point the cooling devices is associated with in this zone. 630 * @cdev: pointer to struct thermal_cooling_device 631 * @upper: the Maximum cooling state for this trip point. 632 * THERMAL_NO_LIMIT means no upper limit, 633 * and the cooling device can be in max_state. 634 * @lower: the Minimum cooling state can be used for this trip point. 635 * THERMAL_NO_LIMIT means no lower limit, 636 * and the cooling device can be in cooling state 0. 637 * @weight: The weight of the cooling device to be bound to the 638 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the 639 * default value 640 * 641 * This interface function bind a thermal cooling device to the certain trip 642 * point of a thermal zone device. 643 * This function is usually called in the thermal zone device .bind callback. 644 * 645 * Return: 0 on success, the proper error value otherwise. 646 */ 647 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz, 648 const struct thermal_trip *trip, 649 struct thermal_cooling_device *cdev, 650 unsigned long upper, unsigned long lower, 651 unsigned int weight) 652 { 653 struct thermal_instance *dev; 654 struct thermal_instance *pos; 655 struct thermal_zone_device *pos1; 656 struct thermal_cooling_device *pos2; 657 bool upper_no_limit; 658 int result; 659 660 list_for_each_entry(pos1, &thermal_tz_list, node) { 661 if (pos1 == tz) 662 break; 663 } 664 list_for_each_entry(pos2, &thermal_cdev_list, node) { 665 if (pos2 == cdev) 666 break; 667 } 668 669 if (tz != pos1 || cdev != pos2) 670 return -EINVAL; 671 672 /* lower default 0, upper default max_state */ 673 lower = lower == THERMAL_NO_LIMIT ? 0 : lower; 674 675 if (upper == THERMAL_NO_LIMIT) { 676 upper = cdev->max_state; 677 upper_no_limit = true; 678 } else { 679 upper_no_limit = false; 680 } 681 682 if (lower > upper || upper > cdev->max_state) 683 return -EINVAL; 684 685 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 686 if (!dev) 687 return -ENOMEM; 688 dev->tz = tz; 689 dev->cdev = cdev; 690 dev->trip = trip; 691 dev->upper = upper; 692 dev->upper_no_limit = upper_no_limit; 693 dev->lower = lower; 694 dev->target = THERMAL_NO_TARGET; 695 dev->weight = weight; 696 697 result = ida_alloc(&tz->ida, GFP_KERNEL); 698 if (result < 0) 699 goto free_mem; 700 701 dev->id = result; 702 sprintf(dev->name, "cdev%d", dev->id); 703 result = 704 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); 705 if (result) 706 goto release_ida; 707 708 snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point", 709 dev->id); 710 sysfs_attr_init(&dev->attr.attr); 711 dev->attr.attr.name = dev->attr_name; 712 dev->attr.attr.mode = 0444; 713 dev->attr.show = trip_point_show; 714 result = device_create_file(&tz->device, &dev->attr); 715 if (result) 716 goto remove_symbol_link; 717 718 snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name), 719 "cdev%d_weight", dev->id); 720 sysfs_attr_init(&dev->weight_attr.attr); 721 dev->weight_attr.attr.name = dev->weight_attr_name; 722 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO; 723 dev->weight_attr.show = weight_show; 724 dev->weight_attr.store = weight_store; 725 result = device_create_file(&tz->device, &dev->weight_attr); 726 if (result) 727 goto remove_trip_file; 728 729 mutex_lock(&tz->lock); 730 mutex_lock(&cdev->lock); 731 list_for_each_entry(pos, &tz->thermal_instances, tz_node) 732 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { 733 result = -EEXIST; 734 break; 735 } 736 if (!result) { 737 list_add_tail(&dev->tz_node, &tz->thermal_instances); 738 list_add_tail(&dev->cdev_node, &cdev->thermal_instances); 739 atomic_set(&tz->need_update, 1); 740 741 thermal_governor_update_tz(tz, THERMAL_TZ_BIND_CDEV); 742 } 743 mutex_unlock(&cdev->lock); 744 mutex_unlock(&tz->lock); 745 746 if (!result) 747 return 0; 748 749 device_remove_file(&tz->device, &dev->weight_attr); 750 remove_trip_file: 751 device_remove_file(&tz->device, &dev->attr); 752 remove_symbol_link: 753 sysfs_remove_link(&tz->device.kobj, dev->name); 754 release_ida: 755 ida_free(&tz->ida, dev->id); 756 free_mem: 757 kfree(dev); 758 return result; 759 } 760 EXPORT_SYMBOL_GPL(thermal_bind_cdev_to_trip); 761 762 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, 763 int trip_index, 764 struct thermal_cooling_device *cdev, 765 unsigned long upper, unsigned long lower, 766 unsigned int weight) 767 { 768 if (trip_index < 0 || trip_index >= tz->num_trips) 769 return -EINVAL; 770 771 return thermal_bind_cdev_to_trip(tz, &tz->trips[trip_index], cdev, 772 upper, lower, weight); 773 } 774 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device); 775 776 /** 777 * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone. 778 * @tz: pointer to a struct thermal_zone_device. 779 * @trip: trip point the cooling devices is associated with in this zone. 780 * @cdev: pointer to a struct thermal_cooling_device. 781 * 782 * This interface function unbind a thermal cooling device from the certain 783 * trip point of a thermal zone device. 784 * This function is usually called in the thermal zone device .unbind callback. 785 * 786 * Return: 0 on success, the proper error value otherwise. 787 */ 788 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz, 789 const struct thermal_trip *trip, 790 struct thermal_cooling_device *cdev) 791 { 792 struct thermal_instance *pos, *next; 793 794 mutex_lock(&tz->lock); 795 mutex_lock(&cdev->lock); 796 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) { 797 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { 798 list_del(&pos->tz_node); 799 list_del(&pos->cdev_node); 800 801 thermal_governor_update_tz(tz, THERMAL_TZ_UNBIND_CDEV); 802 803 mutex_unlock(&cdev->lock); 804 mutex_unlock(&tz->lock); 805 goto unbind; 806 } 807 } 808 mutex_unlock(&cdev->lock); 809 mutex_unlock(&tz->lock); 810 811 return -ENODEV; 812 813 unbind: 814 device_remove_file(&tz->device, &pos->weight_attr); 815 device_remove_file(&tz->device, &pos->attr); 816 sysfs_remove_link(&tz->device.kobj, pos->name); 817 ida_free(&tz->ida, pos->id); 818 kfree(pos); 819 return 0; 820 } 821 EXPORT_SYMBOL_GPL(thermal_unbind_cdev_from_trip); 822 823 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, 824 int trip_index, 825 struct thermal_cooling_device *cdev) 826 { 827 if (trip_index < 0 || trip_index >= tz->num_trips) 828 return -EINVAL; 829 830 return thermal_unbind_cdev_from_trip(tz, &tz->trips[trip_index], cdev); 831 } 832 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device); 833 834 static void thermal_release(struct device *dev) 835 { 836 struct thermal_zone_device *tz; 837 struct thermal_cooling_device *cdev; 838 839 if (!strncmp(dev_name(dev), "thermal_zone", 840 sizeof("thermal_zone") - 1)) { 841 tz = to_thermal_zone(dev); 842 thermal_zone_destroy_device_groups(tz); 843 mutex_destroy(&tz->lock); 844 complete(&tz->removal); 845 } else if (!strncmp(dev_name(dev), "cooling_device", 846 sizeof("cooling_device") - 1)) { 847 cdev = to_cooling_device(dev); 848 thermal_cooling_device_destroy_sysfs(cdev); 849 kfree(cdev->type); 850 ida_free(&thermal_cdev_ida, cdev->id); 851 kfree(cdev); 852 } 853 } 854 855 static struct class *thermal_class; 856 857 static inline 858 void print_bind_err_msg(struct thermal_zone_device *tz, 859 struct thermal_cooling_device *cdev, int ret) 860 { 861 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n", 862 tz->type, cdev->type, ret); 863 } 864 865 static void bind_cdev(struct thermal_cooling_device *cdev) 866 { 867 int ret; 868 struct thermal_zone_device *pos = NULL; 869 870 list_for_each_entry(pos, &thermal_tz_list, node) { 871 if (pos->ops->bind) { 872 ret = pos->ops->bind(pos, cdev); 873 if (ret) 874 print_bind_err_msg(pos, cdev, ret); 875 } 876 } 877 } 878 879 /** 880 * __thermal_cooling_device_register() - register a new thermal cooling device 881 * @np: a pointer to a device tree node. 882 * @type: the thermal cooling device type. 883 * @devdata: device private data. 884 * @ops: standard thermal cooling devices callbacks. 885 * 886 * This interface function adds a new thermal cooling device (fan/processor/...) 887 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself 888 * to all the thermal zone devices registered at the same time. 889 * It also gives the opportunity to link the cooling device to a device tree 890 * node, so that it can be bound to a thermal zone created out of device tree. 891 * 892 * Return: a pointer to the created struct thermal_cooling_device or an 893 * ERR_PTR. Caller must check return value with IS_ERR*() helpers. 894 */ 895 static struct thermal_cooling_device * 896 __thermal_cooling_device_register(struct device_node *np, 897 const char *type, void *devdata, 898 const struct thermal_cooling_device_ops *ops) 899 { 900 struct thermal_cooling_device *cdev; 901 struct thermal_zone_device *pos = NULL; 902 int id, ret; 903 904 if (!ops || !ops->get_max_state || !ops->get_cur_state || 905 !ops->set_cur_state) 906 return ERR_PTR(-EINVAL); 907 908 if (!thermal_class) 909 return ERR_PTR(-ENODEV); 910 911 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 912 if (!cdev) 913 return ERR_PTR(-ENOMEM); 914 915 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL); 916 if (ret < 0) 917 goto out_kfree_cdev; 918 cdev->id = ret; 919 id = ret; 920 921 cdev->type = kstrdup(type ? type : "", GFP_KERNEL); 922 if (!cdev->type) { 923 ret = -ENOMEM; 924 goto out_ida_remove; 925 } 926 927 mutex_init(&cdev->lock); 928 INIT_LIST_HEAD(&cdev->thermal_instances); 929 cdev->np = np; 930 cdev->ops = ops; 931 cdev->updated = false; 932 cdev->device.class = thermal_class; 933 cdev->devdata = devdata; 934 935 ret = cdev->ops->get_max_state(cdev, &cdev->max_state); 936 if (ret) 937 goto out_cdev_type; 938 939 thermal_cooling_device_setup_sysfs(cdev); 940 941 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id); 942 if (ret) 943 goto out_cooling_dev; 944 945 ret = device_register(&cdev->device); 946 if (ret) { 947 /* thermal_release() handles rest of the cleanup */ 948 put_device(&cdev->device); 949 return ERR_PTR(ret); 950 } 951 952 /* Add 'this' new cdev to the global cdev list */ 953 mutex_lock(&thermal_list_lock); 954 955 list_add(&cdev->node, &thermal_cdev_list); 956 957 /* Update binding information for 'this' new cdev */ 958 bind_cdev(cdev); 959 960 list_for_each_entry(pos, &thermal_tz_list, node) 961 if (atomic_cmpxchg(&pos->need_update, 1, 0)) 962 thermal_zone_device_update(pos, 963 THERMAL_EVENT_UNSPECIFIED); 964 965 mutex_unlock(&thermal_list_lock); 966 967 return cdev; 968 969 out_cooling_dev: 970 thermal_cooling_device_destroy_sysfs(cdev); 971 out_cdev_type: 972 kfree(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 mutex_lock(&thermal_list_lock); 1174 1175 if (!thermal_cooling_device_present(cdev)) { 1176 mutex_unlock(&thermal_list_lock); 1177 return; 1178 } 1179 1180 list_del(&cdev->node); 1181 1182 /* Unbind all thermal zones associated with 'this' cdev */ 1183 list_for_each_entry(tz, &thermal_tz_list, node) { 1184 if (tz->ops->unbind) 1185 tz->ops->unbind(tz, cdev); 1186 } 1187 1188 mutex_unlock(&thermal_list_lock); 1189 1190 device_unregister(&cdev->device); 1191 } 1192 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); 1193 1194 static void bind_tz(struct thermal_zone_device *tz) 1195 { 1196 int ret; 1197 struct thermal_cooling_device *pos = NULL; 1198 1199 if (!tz->ops->bind) 1200 return; 1201 1202 mutex_lock(&thermal_list_lock); 1203 1204 list_for_each_entry(pos, &thermal_cdev_list, node) { 1205 ret = tz->ops->bind(tz, pos); 1206 if (ret) 1207 print_bind_err_msg(tz, pos, ret); 1208 } 1209 1210 mutex_unlock(&thermal_list_lock); 1211 } 1212 1213 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms) 1214 { 1215 *delay_jiffies = msecs_to_jiffies(delay_ms); 1216 if (delay_ms > 1000) 1217 *delay_jiffies = round_jiffies(*delay_jiffies); 1218 } 1219 1220 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp) 1221 { 1222 int i, ret = -EINVAL; 1223 1224 if (tz->ops->get_crit_temp) 1225 return tz->ops->get_crit_temp(tz, temp); 1226 1227 if (!tz->trips) 1228 return -EINVAL; 1229 1230 mutex_lock(&tz->lock); 1231 1232 for (i = 0; i < tz->num_trips; i++) { 1233 if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) { 1234 *temp = tz->trips[i].temperature; 1235 ret = 0; 1236 break; 1237 } 1238 } 1239 1240 mutex_unlock(&tz->lock); 1241 1242 return ret; 1243 } 1244 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp); 1245 1246 /** 1247 * thermal_zone_device_register_with_trips() - register a new thermal zone device 1248 * @type: the thermal zone device type 1249 * @trips: a pointer to an array of thermal trips 1250 * @num_trips: the number of trip points the thermal zone support 1251 * @mask: a bit string indicating the writeablility of trip points 1252 * @devdata: private device data 1253 * @ops: standard thermal zone device callbacks 1254 * @tzp: thermal zone platform parameters 1255 * @passive_delay: number of milliseconds to wait between polls when 1256 * performing passive cooling 1257 * @polling_delay: number of milliseconds to wait between polls when checking 1258 * whether trip points have been crossed (0 for interrupt 1259 * driven systems) 1260 * 1261 * This interface function adds a new thermal zone device (sensor) to 1262 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the 1263 * thermal cooling devices registered at the same time. 1264 * thermal_zone_device_unregister() must be called when the device is no 1265 * longer needed. The passive cooling depends on the .get_trend() return value. 1266 * 1267 * Return: a pointer to the created struct thermal_zone_device or an 1268 * in case of error, an ERR_PTR. Caller must check return value with 1269 * IS_ERR*() helpers. 1270 */ 1271 struct thermal_zone_device * 1272 thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask, 1273 void *devdata, struct thermal_zone_device_ops *ops, 1274 const struct thermal_zone_params *tzp, int passive_delay, 1275 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 /* 1294 * Max trip count can't exceed 31 as the "mask >> num_trips" condition. 1295 * For example, shifting by 32 will result in compiler warning: 1296 * warning: right shift count >= width of type [-Wshift-count- overflow] 1297 * 1298 * Also "mask >> num_trips" will always be true with 32 bit shift. 1299 * E.g. mask = 0x80000000 for trip id 31 to be RW. Then 1300 * mask >> 32 = 0x80000000 1301 * This will result in failure for the below condition. 1302 * 1303 * Check will be true when the bit 31 of the mask is set. 1304 * 32 bit shift will cause overflow of 4 byte integer. 1305 */ 1306 if (num_trips > (BITS_PER_TYPE(int) - 1) || num_trips < 0 || mask >> num_trips) { 1307 pr_err("Incorrect number of thermal trips\n"); 1308 return ERR_PTR(-EINVAL); 1309 } 1310 1311 if (!ops || !ops->get_temp) { 1312 pr_err("Thermal zone device ops not defined\n"); 1313 return ERR_PTR(-EINVAL); 1314 } 1315 1316 if (num_trips > 0 && !trips) 1317 return ERR_PTR(-EINVAL); 1318 1319 if (!thermal_class) 1320 return ERR_PTR(-ENODEV); 1321 1322 tz = kzalloc(sizeof(*tz), GFP_KERNEL); 1323 if (!tz) 1324 return ERR_PTR(-ENOMEM); 1325 1326 if (tzp) { 1327 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL); 1328 if (!tz->tzp) { 1329 result = -ENOMEM; 1330 goto free_tz; 1331 } 1332 } 1333 1334 INIT_LIST_HEAD(&tz->thermal_instances); 1335 INIT_LIST_HEAD(&tz->node); 1336 ida_init(&tz->ida); 1337 mutex_init(&tz->lock); 1338 init_completion(&tz->removal); 1339 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL); 1340 if (id < 0) { 1341 result = id; 1342 goto free_tzp; 1343 } 1344 1345 tz->id = id; 1346 strscpy(tz->type, type, sizeof(tz->type)); 1347 1348 if (!ops->critical) 1349 ops->critical = thermal_zone_device_critical; 1350 1351 tz->ops = ops; 1352 tz->device.class = thermal_class; 1353 tz->devdata = devdata; 1354 tz->trips = trips; 1355 tz->num_trips = num_trips; 1356 1357 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay); 1358 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay); 1359 1360 /* sys I/F */ 1361 /* Add nodes that are always present via .groups */ 1362 result = thermal_zone_create_device_groups(tz, mask); 1363 if (result) 1364 goto remove_id; 1365 1366 /* A new thermal zone needs to be updated anyway. */ 1367 atomic_set(&tz->need_update, 1); 1368 1369 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id); 1370 if (result) { 1371 thermal_zone_destroy_device_groups(tz); 1372 goto remove_id; 1373 } 1374 result = device_register(&tz->device); 1375 if (result) 1376 goto release_device; 1377 1378 /* Update 'this' zone's governor information */ 1379 mutex_lock(&thermal_governor_lock); 1380 1381 if (tz->tzp) 1382 governor = __find_governor(tz->tzp->governor_name); 1383 else 1384 governor = def_governor; 1385 1386 result = thermal_set_governor(tz, governor); 1387 if (result) { 1388 mutex_unlock(&thermal_governor_lock); 1389 goto unregister; 1390 } 1391 1392 mutex_unlock(&thermal_governor_lock); 1393 1394 if (!tz->tzp || !tz->tzp->no_hwmon) { 1395 result = thermal_add_hwmon_sysfs(tz); 1396 if (result) 1397 goto unregister; 1398 } 1399 1400 mutex_lock(&thermal_list_lock); 1401 mutex_lock(&tz->lock); 1402 list_add_tail(&tz->node, &thermal_tz_list); 1403 mutex_unlock(&tz->lock); 1404 mutex_unlock(&thermal_list_lock); 1405 1406 /* Bind cooling devices for this zone */ 1407 bind_tz(tz); 1408 1409 thermal_zone_device_init(tz); 1410 /* Update the new thermal zone and mark it as already updated. */ 1411 if (atomic_cmpxchg(&tz->need_update, 1, 0)) 1412 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 1413 1414 thermal_notify_tz_create(tz->id, tz->type); 1415 1416 return tz; 1417 1418 unregister: 1419 device_del(&tz->device); 1420 release_device: 1421 put_device(&tz->device); 1422 remove_id: 1423 ida_free(&thermal_tz_ida, id); 1424 free_tzp: 1425 kfree(tz->tzp); 1426 free_tz: 1427 kfree(tz); 1428 return ERR_PTR(result); 1429 } 1430 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips); 1431 1432 struct thermal_zone_device *thermal_tripless_zone_device_register( 1433 const char *type, 1434 void *devdata, 1435 struct thermal_zone_device_ops *ops, 1436 const struct thermal_zone_params *tzp) 1437 { 1438 return thermal_zone_device_register_with_trips(type, NULL, 0, 0, devdata, 1439 ops, tzp, 0, 0); 1440 } 1441 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register); 1442 1443 void *thermal_zone_device_priv(struct thermal_zone_device *tzd) 1444 { 1445 return tzd->devdata; 1446 } 1447 EXPORT_SYMBOL_GPL(thermal_zone_device_priv); 1448 1449 const char *thermal_zone_device_type(struct thermal_zone_device *tzd) 1450 { 1451 return tzd->type; 1452 } 1453 EXPORT_SYMBOL_GPL(thermal_zone_device_type); 1454 1455 int thermal_zone_device_id(struct thermal_zone_device *tzd) 1456 { 1457 return tzd->id; 1458 } 1459 EXPORT_SYMBOL_GPL(thermal_zone_device_id); 1460 1461 struct device *thermal_zone_device(struct thermal_zone_device *tzd) 1462 { 1463 return &tzd->device; 1464 } 1465 EXPORT_SYMBOL_GPL(thermal_zone_device); 1466 1467 /** 1468 * thermal_zone_device_unregister - removes the registered thermal zone device 1469 * @tz: the thermal zone device to remove 1470 */ 1471 void thermal_zone_device_unregister(struct thermal_zone_device *tz) 1472 { 1473 int tz_id; 1474 struct thermal_cooling_device *cdev; 1475 struct thermal_zone_device *pos = NULL; 1476 1477 if (!tz) 1478 return; 1479 1480 tz_id = tz->id; 1481 1482 mutex_lock(&thermal_list_lock); 1483 list_for_each_entry(pos, &thermal_tz_list, node) 1484 if (pos == tz) 1485 break; 1486 if (pos != tz) { 1487 /* thermal zone device not found */ 1488 mutex_unlock(&thermal_list_lock); 1489 return; 1490 } 1491 1492 mutex_lock(&tz->lock); 1493 list_del(&tz->node); 1494 mutex_unlock(&tz->lock); 1495 1496 /* Unbind all cdevs associated with 'this' thermal zone */ 1497 list_for_each_entry(cdev, &thermal_cdev_list, node) 1498 if (tz->ops->unbind) 1499 tz->ops->unbind(tz, cdev); 1500 1501 mutex_unlock(&thermal_list_lock); 1502 1503 cancel_delayed_work_sync(&tz->poll_queue); 1504 1505 thermal_set_governor(tz, NULL); 1506 1507 thermal_remove_hwmon_sysfs(tz); 1508 ida_free(&thermal_tz_ida, tz->id); 1509 ida_destroy(&tz->ida); 1510 1511 device_del(&tz->device); 1512 1513 kfree(tz->tzp); 1514 1515 put_device(&tz->device); 1516 1517 thermal_notify_tz_delete(tz_id); 1518 1519 wait_for_completion(&tz->removal); 1520 kfree(tz); 1521 } 1522 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister); 1523 1524 /** 1525 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref 1526 * @name: thermal zone name to fetch the temperature 1527 * 1528 * When only one zone is found with the passed name, returns a reference to it. 1529 * 1530 * Return: On success returns a reference to an unique thermal zone with 1531 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid 1532 * paramenters, -ENODEV for not found and -EEXIST for multiple matches). 1533 */ 1534 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name) 1535 { 1536 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL); 1537 unsigned int found = 0; 1538 1539 if (!name) 1540 goto exit; 1541 1542 mutex_lock(&thermal_list_lock); 1543 list_for_each_entry(pos, &thermal_tz_list, node) 1544 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) { 1545 found++; 1546 ref = pos; 1547 } 1548 mutex_unlock(&thermal_list_lock); 1549 1550 /* nothing has been found, thus an error code for it */ 1551 if (found == 0) 1552 ref = ERR_PTR(-ENODEV); 1553 else if (found > 1) 1554 /* Success only when an unique zone is found */ 1555 ref = ERR_PTR(-EEXIST); 1556 1557 exit: 1558 return ref; 1559 } 1560 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name); 1561 1562 static void thermal_zone_device_resume(struct work_struct *work) 1563 { 1564 struct thermal_zone_device *tz; 1565 1566 tz = container_of(work, struct thermal_zone_device, poll_queue.work); 1567 1568 mutex_lock(&tz->lock); 1569 1570 tz->suspended = false; 1571 1572 thermal_zone_device_init(tz); 1573 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 1574 1575 mutex_unlock(&tz->lock); 1576 } 1577 1578 static int thermal_pm_notify(struct notifier_block *nb, 1579 unsigned long mode, void *_unused) 1580 { 1581 struct thermal_zone_device *tz; 1582 1583 switch (mode) { 1584 case PM_HIBERNATION_PREPARE: 1585 case PM_RESTORE_PREPARE: 1586 case PM_SUSPEND_PREPARE: 1587 mutex_lock(&thermal_list_lock); 1588 1589 list_for_each_entry(tz, &thermal_tz_list, node) { 1590 mutex_lock(&tz->lock); 1591 1592 tz->suspended = true; 1593 1594 mutex_unlock(&tz->lock); 1595 } 1596 1597 mutex_unlock(&thermal_list_lock); 1598 break; 1599 case PM_POST_HIBERNATION: 1600 case PM_POST_RESTORE: 1601 case PM_POST_SUSPEND: 1602 mutex_lock(&thermal_list_lock); 1603 1604 list_for_each_entry(tz, &thermal_tz_list, node) { 1605 mutex_lock(&tz->lock); 1606 1607 cancel_delayed_work(&tz->poll_queue); 1608 1609 /* 1610 * Replace the work function with the resume one, which 1611 * will restore the original work function and schedule 1612 * the polling work if needed. 1613 */ 1614 INIT_DELAYED_WORK(&tz->poll_queue, 1615 thermal_zone_device_resume); 1616 /* Queue up the work without a delay. */ 1617 mod_delayed_work(system_freezable_power_efficient_wq, 1618 &tz->poll_queue, 0); 1619 1620 mutex_unlock(&tz->lock); 1621 } 1622 1623 mutex_unlock(&thermal_list_lock); 1624 break; 1625 default: 1626 break; 1627 } 1628 return 0; 1629 } 1630 1631 static struct notifier_block thermal_pm_nb = { 1632 .notifier_call = thermal_pm_notify, 1633 }; 1634 1635 static int __init thermal_init(void) 1636 { 1637 int result; 1638 1639 result = thermal_netlink_init(); 1640 if (result) 1641 goto error; 1642 1643 result = thermal_register_governors(); 1644 if (result) 1645 goto unregister_netlink; 1646 1647 thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL); 1648 if (!thermal_class) { 1649 result = -ENOMEM; 1650 goto unregister_governors; 1651 } 1652 1653 thermal_class->name = "thermal"; 1654 thermal_class->dev_release = thermal_release; 1655 1656 result = class_register(thermal_class); 1657 if (result) { 1658 kfree(thermal_class); 1659 thermal_class = NULL; 1660 goto unregister_governors; 1661 } 1662 1663 result = register_pm_notifier(&thermal_pm_nb); 1664 if (result) 1665 pr_warn("Thermal: Can not register suspend notifier, return %d\n", 1666 result); 1667 1668 return 0; 1669 1670 unregister_governors: 1671 thermal_unregister_governors(); 1672 unregister_netlink: 1673 thermal_netlink_exit(); 1674 error: 1675 mutex_destroy(&thermal_list_lock); 1676 mutex_destroy(&thermal_governor_lock); 1677 return result; 1678 } 1679 postcore_initcall(thermal_init); 1680