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