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