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