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