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