linux/drivers/thermal/thermal_core.c
<<
>>
Prefs
   1/*
   2 *  thermal.c - Generic Thermal Management Sysfs support.
   3 *
   4 *  Copyright (C) 2008 Intel Corp
   5 *  Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
   6 *  Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
   7 *
   8 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   9 *
  10 *  This program is free software; you can redistribute it and/or modify
  11 *  it under the terms of the GNU General Public License as published by
  12 *  the Free Software Foundation; version 2 of the License.
  13 *
  14 *  This program is distributed in the hope that it will be useful, but
  15 *  WITHOUT ANY WARRANTY; without even the implied warranty of
  16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17 *  General Public License for more details.
  18 *
  19 *  You should have received a copy of the GNU General Public License along
  20 *  with this program; if not, write to the Free Software Foundation, Inc.,
  21 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22 *
  23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24 */
  25
  26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27
  28#include <linux/module.h>
  29#include <linux/device.h>
  30#include <linux/err.h>
  31#include <linux/slab.h>
  32#include <linux/kdev_t.h>
  33#include <linux/idr.h>
  34#include <linux/thermal.h>
  35#include <linux/reboot.h>
  36#include <linux/string.h>
  37#include <net/netlink.h>
  38#include <net/genetlink.h>
  39
  40#include "thermal_core.h"
  41
  42MODULE_AUTHOR("Zhang Rui");
  43MODULE_DESCRIPTION("Generic thermal management sysfs support");
  44MODULE_LICENSE("GPL v2");
  45
  46static DEFINE_IDR(thermal_tz_idr);
  47static DEFINE_IDR(thermal_cdev_idr);
  48static DEFINE_MUTEX(thermal_idr_lock);
  49
  50static LIST_HEAD(thermal_tz_list);
  51static LIST_HEAD(thermal_cdev_list);
  52static LIST_HEAD(thermal_governor_list);
  53
  54static DEFINE_MUTEX(thermal_list_lock);
  55static DEFINE_MUTEX(thermal_governor_lock);
  56
  57static struct thermal_governor *__find_governor(const char *name)
  58{
  59        struct thermal_governor *pos;
  60
  61        list_for_each_entry(pos, &thermal_governor_list, governor_list)
  62                if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
  63                        return pos;
  64
  65        return NULL;
  66}
  67
  68int thermal_register_governor(struct thermal_governor *governor)
  69{
  70        int err;
  71        const char *name;
  72        struct thermal_zone_device *pos;
  73
  74        if (!governor)
  75                return -EINVAL;
  76
  77        mutex_lock(&thermal_governor_lock);
  78
  79        err = -EBUSY;
  80        if (__find_governor(governor->name) == NULL) {
  81                err = 0;
  82                list_add(&governor->governor_list, &thermal_governor_list);
  83        }
  84
  85        mutex_lock(&thermal_list_lock);
  86
  87        list_for_each_entry(pos, &thermal_tz_list, node) {
  88                if (pos->governor)
  89                        continue;
  90                if (pos->tzp)
  91                        name = pos->tzp->governor_name;
  92                else
  93                        name = DEFAULT_THERMAL_GOVERNOR;
  94                if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
  95                        pos->governor = governor;
  96        }
  97
  98        mutex_unlock(&thermal_list_lock);
  99        mutex_unlock(&thermal_governor_lock);
 100
 101        return err;
 102}
 103
 104void thermal_unregister_governor(struct thermal_governor *governor)
 105{
 106        struct thermal_zone_device *pos;
 107
 108        if (!governor)
 109                return;
 110
 111        mutex_lock(&thermal_governor_lock);
 112
 113        if (__find_governor(governor->name) == NULL)
 114                goto exit;
 115
 116        mutex_lock(&thermal_list_lock);
 117
 118        list_for_each_entry(pos, &thermal_tz_list, node) {
 119                if (!strnicmp(pos->governor->name, governor->name,
 120                                                THERMAL_NAME_LENGTH))
 121                        pos->governor = NULL;
 122        }
 123
 124        mutex_unlock(&thermal_list_lock);
 125        list_del(&governor->governor_list);
 126exit:
 127        mutex_unlock(&thermal_governor_lock);
 128        return;
 129}
 130
 131static int get_idr(struct idr *idr, struct mutex *lock, int *id)
 132{
 133        int ret;
 134
 135        if (lock)
 136                mutex_lock(lock);
 137        ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
 138        if (lock)
 139                mutex_unlock(lock);
 140        if (unlikely(ret < 0))
 141                return ret;
 142        *id = ret;
 143        return 0;
 144}
 145
 146static void release_idr(struct idr *idr, struct mutex *lock, int id)
 147{
 148        if (lock)
 149                mutex_lock(lock);
 150        idr_remove(idr, id);
 151        if (lock)
 152                mutex_unlock(lock);
 153}
 154
 155int get_tz_trend(struct thermal_zone_device *tz, int trip)
 156{
 157        enum thermal_trend trend;
 158
 159        if (tz->emul_temperature || !tz->ops->get_trend ||
 160            tz->ops->get_trend(tz, trip, &trend)) {
 161                if (tz->temperature > tz->last_temperature)
 162                        trend = THERMAL_TREND_RAISING;
 163                else if (tz->temperature < tz->last_temperature)
 164                        trend = THERMAL_TREND_DROPPING;
 165                else
 166                        trend = THERMAL_TREND_STABLE;
 167        }
 168
 169        return trend;
 170}
 171EXPORT_SYMBOL(get_tz_trend);
 172
 173struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
 174                        struct thermal_cooling_device *cdev, int trip)
 175{
 176        struct thermal_instance *pos = NULL;
 177        struct thermal_instance *target_instance = NULL;
 178
 179        mutex_lock(&tz->lock);
 180        mutex_lock(&cdev->lock);
 181
 182        list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
 183                if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
 184                        target_instance = pos;
 185                        break;
 186                }
 187        }
 188
 189        mutex_unlock(&cdev->lock);
 190        mutex_unlock(&tz->lock);
 191
 192        return target_instance;
 193}
 194EXPORT_SYMBOL(get_thermal_instance);
 195
 196static void print_bind_err_msg(struct thermal_zone_device *tz,
 197                        struct thermal_cooling_device *cdev, int ret)
 198{
 199        dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
 200                                tz->type, cdev->type, ret);
 201}
 202
 203static void __bind(struct thermal_zone_device *tz, int mask,
 204                        struct thermal_cooling_device *cdev)
 205{
 206        int i, ret;
 207
 208        for (i = 0; i < tz->trips; i++) {
 209                if (mask & (1 << i)) {
 210                        ret = thermal_zone_bind_cooling_device(tz, i, cdev,
 211                                        THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
 212                        if (ret)
 213                                print_bind_err_msg(tz, cdev, ret);
 214                }
 215        }
 216}
 217
 218static void __unbind(struct thermal_zone_device *tz, int mask,
 219                        struct thermal_cooling_device *cdev)
 220{
 221        int i;
 222
 223        for (i = 0; i < tz->trips; i++)
 224                if (mask & (1 << i))
 225                        thermal_zone_unbind_cooling_device(tz, i, cdev);
 226}
 227
 228static void bind_cdev(struct thermal_cooling_device *cdev)
 229{
 230        int i, ret;
 231        const struct thermal_zone_params *tzp;
 232        struct thermal_zone_device *pos = NULL;
 233
 234        mutex_lock(&thermal_list_lock);
 235
 236        list_for_each_entry(pos, &thermal_tz_list, node) {
 237                if (!pos->tzp && !pos->ops->bind)
 238                        continue;
 239
 240                if (!pos->tzp && pos->ops->bind) {
 241                        ret = pos->ops->bind(pos, cdev);
 242                        if (ret)
 243                                print_bind_err_msg(pos, cdev, ret);
 244                }
 245
 246                tzp = pos->tzp;
 247                if (!tzp || !tzp->tbp)
 248                        continue;
 249
 250                for (i = 0; i < tzp->num_tbps; i++) {
 251                        if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
 252                                continue;
 253                        if (tzp->tbp[i].match(pos, cdev))
 254                                continue;
 255                        tzp->tbp[i].cdev = cdev;
 256                        __bind(pos, tzp->tbp[i].trip_mask, cdev);
 257                }
 258        }
 259
 260        mutex_unlock(&thermal_list_lock);
 261}
 262
 263static void bind_tz(struct thermal_zone_device *tz)
 264{
 265        int i, ret;
 266        struct thermal_cooling_device *pos = NULL;
 267        const struct thermal_zone_params *tzp = tz->tzp;
 268
 269        if (!tzp && !tz->ops->bind)
 270                return;
 271
 272        mutex_lock(&thermal_list_lock);
 273
 274        /* If there is no platform data, try to use ops->bind */
 275        if (!tzp && tz->ops->bind) {
 276                list_for_each_entry(pos, &thermal_cdev_list, node) {
 277                        ret = tz->ops->bind(tz, pos);
 278                        if (ret)
 279                                print_bind_err_msg(tz, pos, ret);
 280                }
 281                goto exit;
 282        }
 283
 284        if (!tzp || !tzp->tbp)
 285                goto exit;
 286
 287        list_for_each_entry(pos, &thermal_cdev_list, node) {
 288                for (i = 0; i < tzp->num_tbps; i++) {
 289                        if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
 290                                continue;
 291                        if (tzp->tbp[i].match(tz, pos))
 292                                continue;
 293                        tzp->tbp[i].cdev = pos;
 294                        __bind(tz, tzp->tbp[i].trip_mask, pos);
 295                }
 296        }
 297exit:
 298        mutex_unlock(&thermal_list_lock);
 299}
 300
 301static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
 302                                            int delay)
 303{
 304        if (delay > 1000)
 305                mod_delayed_work(system_freezable_wq, &tz->poll_queue,
 306                                 round_jiffies(msecs_to_jiffies(delay)));
 307        else if (delay)
 308                mod_delayed_work(system_freezable_wq, &tz->poll_queue,
 309                                 msecs_to_jiffies(delay));
 310        else
 311                cancel_delayed_work(&tz->poll_queue);
 312}
 313
 314static void monitor_thermal_zone(struct thermal_zone_device *tz)
 315{
 316        mutex_lock(&tz->lock);
 317
 318        if (tz->passive)
 319                thermal_zone_device_set_polling(tz, tz->passive_delay);
 320        else if (tz->polling_delay)
 321                thermal_zone_device_set_polling(tz, tz->polling_delay);
 322        else
 323                thermal_zone_device_set_polling(tz, 0);
 324
 325        mutex_unlock(&tz->lock);
 326}
 327
 328static void handle_non_critical_trips(struct thermal_zone_device *tz,
 329                        int trip, enum thermal_trip_type trip_type)
 330{
 331        if (tz->governor)
 332                tz->governor->throttle(tz, trip);
 333}
 334
 335static void handle_critical_trips(struct thermal_zone_device *tz,
 336                                int trip, enum thermal_trip_type trip_type)
 337{
 338        long trip_temp;
 339
 340        tz->ops->get_trip_temp(tz, trip, &trip_temp);
 341
 342        /* If we have not crossed the trip_temp, we do not care. */
 343        if (tz->temperature < trip_temp)
 344                return;
 345
 346        if (tz->ops->notify)
 347                tz->ops->notify(tz, trip, trip_type);
 348
 349        if (trip_type == THERMAL_TRIP_CRITICAL) {
 350                dev_emerg(&tz->device,
 351                          "critical temperature reached(%d C),shutting down\n",
 352                          tz->temperature / 1000);
 353                orderly_poweroff(true);
 354        }
 355}
 356
 357static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
 358{
 359        enum thermal_trip_type type;
 360
 361        tz->ops->get_trip_type(tz, trip, &type);
 362
 363        if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
 364                handle_critical_trips(tz, trip, type);
 365        else
 366                handle_non_critical_trips(tz, trip, type);
 367        /*
 368         * Alright, we handled this trip successfully.
 369         * So, start monitoring again.
 370         */
 371        monitor_thermal_zone(tz);
 372}
 373
 374/**
 375 * thermal_zone_get_temp() - returns its the temperature of thermal zone
 376 * @tz: a valid pointer to a struct thermal_zone_device
 377 * @temp: a valid pointer to where to store the resulting temperature.
 378 *
 379 * When a valid thermal zone reference is passed, it will fetch its
 380 * temperature and fill @temp.
 381 *
 382 * Return: On success returns 0, an error code otherwise
 383 */
 384int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
 385{
 386        int ret = -EINVAL;
 387#ifdef CONFIG_THERMAL_EMULATION
 388        int count;
 389        unsigned long crit_temp = -1UL;
 390        enum thermal_trip_type type;
 391#endif
 392
 393        if (!tz || IS_ERR(tz))
 394                goto exit;
 395
 396        mutex_lock(&tz->lock);
 397
 398        ret = tz->ops->get_temp(tz, temp);
 399#ifdef CONFIG_THERMAL_EMULATION
 400        if (!tz->emul_temperature)
 401                goto skip_emul;
 402
 403        for (count = 0; count < tz->trips; count++) {
 404                ret = tz->ops->get_trip_type(tz, count, &type);
 405                if (!ret && type == THERMAL_TRIP_CRITICAL) {
 406                        ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
 407                        break;
 408                }
 409        }
 410
 411        if (ret)
 412                goto skip_emul;
 413
 414        if (*temp < crit_temp)
 415                *temp = tz->emul_temperature;
 416skip_emul:
 417#endif
 418        mutex_unlock(&tz->lock);
 419exit:
 420        return ret;
 421}
 422EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
 423
 424static void update_temperature(struct thermal_zone_device *tz)
 425{
 426        long temp;
 427        int ret;
 428
 429        ret = thermal_zone_get_temp(tz, &temp);
 430        if (ret) {
 431                dev_warn(&tz->device, "failed to read out thermal zone %d\n",
 432                         tz->id);
 433                return;
 434        }
 435
 436        mutex_lock(&tz->lock);
 437        tz->last_temperature = tz->temperature;
 438        tz->temperature = temp;
 439        mutex_unlock(&tz->lock);
 440}
 441
 442void thermal_zone_device_update(struct thermal_zone_device *tz)
 443{
 444        int count;
 445
 446        update_temperature(tz);
 447
 448        for (count = 0; count < tz->trips; count++)
 449                handle_thermal_trip(tz, count);
 450}
 451EXPORT_SYMBOL_GPL(thermal_zone_device_update);
 452
 453static void thermal_zone_device_check(struct work_struct *work)
 454{
 455        struct thermal_zone_device *tz = container_of(work, struct
 456                                                      thermal_zone_device,
 457                                                      poll_queue.work);
 458        thermal_zone_device_update(tz);
 459}
 460
 461/* sys I/F for thermal zone */
 462
 463#define to_thermal_zone(_dev) \
 464        container_of(_dev, struct thermal_zone_device, device)
 465
 466static ssize_t
 467type_show(struct device *dev, struct device_attribute *attr, char *buf)
 468{
 469        struct thermal_zone_device *tz = to_thermal_zone(dev);
 470
 471        return sprintf(buf, "%s\n", tz->type);
 472}
 473
 474static ssize_t
 475temp_show(struct device *dev, struct device_attribute *attr, char *buf)
 476{
 477        struct thermal_zone_device *tz = to_thermal_zone(dev);
 478        long temperature;
 479        int ret;
 480
 481        ret = thermal_zone_get_temp(tz, &temperature);
 482
 483        if (ret)
 484                return ret;
 485
 486        return sprintf(buf, "%ld\n", temperature);
 487}
 488
 489static ssize_t
 490mode_show(struct device *dev, struct device_attribute *attr, char *buf)
 491{
 492        struct thermal_zone_device *tz = to_thermal_zone(dev);
 493        enum thermal_device_mode mode;
 494        int result;
 495
 496        if (!tz->ops->get_mode)
 497                return -EPERM;
 498
 499        result = tz->ops->get_mode(tz, &mode);
 500        if (result)
 501                return result;
 502
 503        return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
 504                       : "disabled");
 505}
 506
 507static ssize_t
 508mode_store(struct device *dev, struct device_attribute *attr,
 509           const char *buf, size_t count)
 510{
 511        struct thermal_zone_device *tz = to_thermal_zone(dev);
 512        int result;
 513
 514        if (!tz->ops->set_mode)
 515                return -EPERM;
 516
 517        if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
 518                result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
 519        else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
 520                result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
 521        else
 522                result = -EINVAL;
 523
 524        if (result)
 525                return result;
 526
 527        return count;
 528}
 529
 530static ssize_t
 531trip_point_type_show(struct device *dev, struct device_attribute *attr,
 532                     char *buf)
 533{
 534        struct thermal_zone_device *tz = to_thermal_zone(dev);
 535        enum thermal_trip_type type;
 536        int trip, result;
 537
 538        if (!tz->ops->get_trip_type)
 539                return -EPERM;
 540
 541        if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
 542                return -EINVAL;
 543
 544        result = tz->ops->get_trip_type(tz, trip, &type);
 545        if (result)
 546                return result;
 547
 548        switch (type) {
 549        case THERMAL_TRIP_CRITICAL:
 550                return sprintf(buf, "critical\n");
 551        case THERMAL_TRIP_HOT:
 552                return sprintf(buf, "hot\n");
 553        case THERMAL_TRIP_PASSIVE:
 554                return sprintf(buf, "passive\n");
 555        case THERMAL_TRIP_ACTIVE:
 556                return sprintf(buf, "active\n");
 557        default:
 558                return sprintf(buf, "unknown\n");
 559        }
 560}
 561
 562static ssize_t
 563trip_point_temp_store(struct device *dev, struct device_attribute *attr,
 564                     const char *buf, size_t count)
 565{
 566        struct thermal_zone_device *tz = to_thermal_zone(dev);
 567        int trip, ret;
 568        unsigned long temperature;
 569
 570        if (!tz->ops->set_trip_temp)
 571                return -EPERM;
 572
 573        if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
 574                return -EINVAL;
 575
 576        if (kstrtoul(buf, 10, &temperature))
 577                return -EINVAL;
 578
 579        ret = tz->ops->set_trip_temp(tz, trip, temperature);
 580
 581        return ret ? ret : count;
 582}
 583
 584static ssize_t
 585trip_point_temp_show(struct device *dev, struct device_attribute *attr,
 586                     char *buf)
 587{
 588        struct thermal_zone_device *tz = to_thermal_zone(dev);
 589        int trip, ret;
 590        long temperature;
 591
 592        if (!tz->ops->get_trip_temp)
 593                return -EPERM;
 594
 595        if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
 596                return -EINVAL;
 597
 598        ret = tz->ops->get_trip_temp(tz, trip, &temperature);
 599
 600        if (ret)
 601                return ret;
 602
 603        return sprintf(buf, "%ld\n", temperature);
 604}
 605
 606static ssize_t
 607trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
 608                        const char *buf, size_t count)
 609{
 610        struct thermal_zone_device *tz = to_thermal_zone(dev);
 611        int trip, ret;
 612        unsigned long temperature;
 613
 614        if (!tz->ops->set_trip_hyst)
 615                return -EPERM;
 616
 617        if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
 618                return -EINVAL;
 619
 620        if (kstrtoul(buf, 10, &temperature))
 621                return -EINVAL;
 622
 623        /*
 624         * We are not doing any check on the 'temperature' value
 625         * here. The driver implementing 'set_trip_hyst' has to
 626         * take care of this.
 627         */
 628        ret = tz->ops->set_trip_hyst(tz, trip, temperature);
 629
 630        return ret ? ret : count;
 631}
 632
 633static ssize_t
 634trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
 635                        char *buf)
 636{
 637        struct thermal_zone_device *tz = to_thermal_zone(dev);
 638        int trip, ret;
 639        unsigned long temperature;
 640
 641        if (!tz->ops->get_trip_hyst)
 642                return -EPERM;
 643
 644        if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
 645                return -EINVAL;
 646
 647        ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
 648
 649        return ret ? ret : sprintf(buf, "%ld\n", temperature);
 650}
 651
 652static ssize_t
 653passive_store(struct device *dev, struct device_attribute *attr,
 654                    const char *buf, size_t count)
 655{
 656        struct thermal_zone_device *tz = to_thermal_zone(dev);
 657        struct thermal_cooling_device *cdev = NULL;
 658        int state;
 659
 660        if (!sscanf(buf, "%d\n", &state))
 661                return -EINVAL;
 662
 663        /* sanity check: values below 1000 millicelcius don't make sense
 664         * and can cause the system to go into a thermal heart attack
 665         */
 666        if (state && state < 1000)
 667                return -EINVAL;
 668
 669        if (state && !tz->forced_passive) {
 670                mutex_lock(&thermal_list_lock);
 671                list_for_each_entry(cdev, &thermal_cdev_list, node) {
 672                        if (!strncmp("Processor", cdev->type,
 673                                     sizeof("Processor")))
 674                                thermal_zone_bind_cooling_device(tz,
 675                                                THERMAL_TRIPS_NONE, cdev,
 676                                                THERMAL_NO_LIMIT,
 677                                                THERMAL_NO_LIMIT);
 678                }
 679                mutex_unlock(&thermal_list_lock);
 680                if (!tz->passive_delay)
 681                        tz->passive_delay = 1000;
 682        } else if (!state && tz->forced_passive) {
 683                mutex_lock(&thermal_list_lock);
 684                list_for_each_entry(cdev, &thermal_cdev_list, node) {
 685                        if (!strncmp("Processor", cdev->type,
 686                                     sizeof("Processor")))
 687                                thermal_zone_unbind_cooling_device(tz,
 688                                                                   THERMAL_TRIPS_NONE,
 689                                                                   cdev);
 690                }
 691                mutex_unlock(&thermal_list_lock);
 692                tz->passive_delay = 0;
 693        }
 694
 695        tz->forced_passive = state;
 696
 697        thermal_zone_device_update(tz);
 698
 699        return count;
 700}
 701
 702static ssize_t
 703passive_show(struct device *dev, struct device_attribute *attr,
 704                   char *buf)
 705{
 706        struct thermal_zone_device *tz = to_thermal_zone(dev);
 707
 708        return sprintf(buf, "%d\n", tz->forced_passive);
 709}
 710
 711static ssize_t
 712policy_store(struct device *dev, struct device_attribute *attr,
 713                    const char *buf, size_t count)
 714{
 715        int ret = -EINVAL;
 716        struct thermal_zone_device *tz = to_thermal_zone(dev);
 717        struct thermal_governor *gov;
 718        char name[THERMAL_NAME_LENGTH];
 719
 720        snprintf(name, sizeof(name), "%s", buf);
 721
 722        mutex_lock(&thermal_governor_lock);
 723
 724        gov = __find_governor(strim(name));
 725        if (!gov)
 726                goto exit;
 727
 728        tz->governor = gov;
 729        ret = count;
 730
 731exit:
 732        mutex_unlock(&thermal_governor_lock);
 733        return ret;
 734}
 735
 736static ssize_t
 737policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
 738{
 739        struct thermal_zone_device *tz = to_thermal_zone(dev);
 740
 741        return sprintf(buf, "%s\n", tz->governor->name);
 742}
 743
 744#ifdef CONFIG_THERMAL_EMULATION
 745static ssize_t
 746emul_temp_store(struct device *dev, struct device_attribute *attr,
 747                     const char *buf, size_t count)
 748{
 749        struct thermal_zone_device *tz = to_thermal_zone(dev);
 750        int ret = 0;
 751        unsigned long temperature;
 752
 753        if (kstrtoul(buf, 10, &temperature))
 754                return -EINVAL;
 755
 756        if (!tz->ops->set_emul_temp) {
 757                mutex_lock(&tz->lock);
 758                tz->emul_temperature = temperature;
 759                mutex_unlock(&tz->lock);
 760        } else {
 761                ret = tz->ops->set_emul_temp(tz, temperature);
 762        }
 763
 764        return ret ? ret : count;
 765}
 766static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
 767#endif/*CONFIG_THERMAL_EMULATION*/
 768
 769static DEVICE_ATTR(type, 0444, type_show, NULL);
 770static DEVICE_ATTR(temp, 0444, temp_show, NULL);
 771static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
 772static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
 773static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
 774
 775/* sys I/F for cooling device */
 776#define to_cooling_device(_dev) \
 777        container_of(_dev, struct thermal_cooling_device, device)
 778
 779static ssize_t
 780thermal_cooling_device_type_show(struct device *dev,
 781                                 struct device_attribute *attr, char *buf)
 782{
 783        struct thermal_cooling_device *cdev = to_cooling_device(dev);
 784
 785        return sprintf(buf, "%s\n", cdev->type);
 786}
 787
 788static ssize_t
 789thermal_cooling_device_max_state_show(struct device *dev,
 790                                      struct device_attribute *attr, char *buf)
 791{
 792        struct thermal_cooling_device *cdev = to_cooling_device(dev);
 793        unsigned long state;
 794        int ret;
 795
 796        ret = cdev->ops->get_max_state(cdev, &state);
 797        if (ret)
 798                return ret;
 799        return sprintf(buf, "%ld\n", state);
 800}
 801
 802static ssize_t
 803thermal_cooling_device_cur_state_show(struct device *dev,
 804                                      struct device_attribute *attr, char *buf)
 805{
 806        struct thermal_cooling_device *cdev = to_cooling_device(dev);
 807        unsigned long state;
 808        int ret;
 809
 810        ret = cdev->ops->get_cur_state(cdev, &state);
 811        if (ret)
 812                return ret;
 813        return sprintf(buf, "%ld\n", state);
 814}
 815
 816static ssize_t
 817thermal_cooling_device_cur_state_store(struct device *dev,
 818                                       struct device_attribute *attr,
 819                                       const char *buf, size_t count)
 820{
 821        struct thermal_cooling_device *cdev = to_cooling_device(dev);
 822        unsigned long state;
 823        int result;
 824
 825        if (!sscanf(buf, "%ld\n", &state))
 826                return -EINVAL;
 827
 828        if ((long)state < 0)
 829                return -EINVAL;
 830
 831        result = cdev->ops->set_cur_state(cdev, state);
 832        if (result)
 833                return result;
 834        return count;
 835}
 836
 837static struct device_attribute dev_attr_cdev_type =
 838__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
 839static DEVICE_ATTR(max_state, 0444,
 840                   thermal_cooling_device_max_state_show, NULL);
 841static DEVICE_ATTR(cur_state, 0644,
 842                   thermal_cooling_device_cur_state_show,
 843                   thermal_cooling_device_cur_state_store);
 844
 845static ssize_t
 846thermal_cooling_device_trip_point_show(struct device *dev,
 847                                       struct device_attribute *attr, char *buf)
 848{
 849        struct thermal_instance *instance;
 850
 851        instance =
 852            container_of(attr, struct thermal_instance, attr);
 853
 854        if (instance->trip == THERMAL_TRIPS_NONE)
 855                return sprintf(buf, "-1\n");
 856        else
 857                return sprintf(buf, "%d\n", instance->trip);
 858}
 859
 860/* Device management */
 861
 862#if defined(CONFIG_THERMAL_HWMON)
 863
 864/* hwmon sys I/F */
 865#include <linux/hwmon.h>
 866
 867/* thermal zone devices with the same type share one hwmon device */
 868struct thermal_hwmon_device {
 869        char type[THERMAL_NAME_LENGTH];
 870        struct device *device;
 871        int count;
 872        struct list_head tz_list;
 873        struct list_head node;
 874};
 875
 876struct thermal_hwmon_attr {
 877        struct device_attribute attr;
 878        char name[16];
 879};
 880
 881/* one temperature input for each thermal zone */
 882struct thermal_hwmon_temp {
 883        struct list_head hwmon_node;
 884        struct thermal_zone_device *tz;
 885        struct thermal_hwmon_attr temp_input;   /* hwmon sys attr */
 886        struct thermal_hwmon_attr temp_crit;    /* hwmon sys attr */
 887};
 888
 889static LIST_HEAD(thermal_hwmon_list);
 890
 891static ssize_t
 892name_show(struct device *dev, struct device_attribute *attr, char *buf)
 893{
 894        struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
 895        return sprintf(buf, "%s\n", hwmon->type);
 896}
 897static DEVICE_ATTR(name, 0444, name_show, NULL);
 898
 899static ssize_t
 900temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
 901{
 902        long temperature;
 903        int ret;
 904        struct thermal_hwmon_attr *hwmon_attr
 905                        = container_of(attr, struct thermal_hwmon_attr, attr);
 906        struct thermal_hwmon_temp *temp
 907                        = container_of(hwmon_attr, struct thermal_hwmon_temp,
 908                                       temp_input);
 909        struct thermal_zone_device *tz = temp->tz;
 910
 911        ret = thermal_zone_get_temp(tz, &temperature);
 912
 913        if (ret)
 914                return ret;
 915
 916        return sprintf(buf, "%ld\n", temperature);
 917}
 918
 919static ssize_t
 920temp_crit_show(struct device *dev, struct device_attribute *attr,
 921                char *buf)
 922{
 923        struct thermal_hwmon_attr *hwmon_attr
 924                        = container_of(attr, struct thermal_hwmon_attr, attr);
 925        struct thermal_hwmon_temp *temp
 926                        = container_of(hwmon_attr, struct thermal_hwmon_temp,
 927                                       temp_crit);
 928        struct thermal_zone_device *tz = temp->tz;
 929        long temperature;
 930        int ret;
 931
 932        ret = tz->ops->get_trip_temp(tz, 0, &temperature);
 933        if (ret)
 934                return ret;
 935
 936        return sprintf(buf, "%ld\n", temperature);
 937}
 938
 939
 940static struct thermal_hwmon_device *
 941thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
 942{
 943        struct thermal_hwmon_device *hwmon;
 944
 945        mutex_lock(&thermal_list_lock);
 946        list_for_each_entry(hwmon, &thermal_hwmon_list, node)
 947                if (!strcmp(hwmon->type, tz->type)) {
 948                        mutex_unlock(&thermal_list_lock);
 949                        return hwmon;
 950                }
 951        mutex_unlock(&thermal_list_lock);
 952
 953        return NULL;
 954}
 955
 956/* Find the temperature input matching a given thermal zone */
 957static struct thermal_hwmon_temp *
 958thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
 959                          const struct thermal_zone_device *tz)
 960{
 961        struct thermal_hwmon_temp *temp;
 962
 963        mutex_lock(&thermal_list_lock);
 964        list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
 965                if (temp->tz == tz) {
 966                        mutex_unlock(&thermal_list_lock);
 967                        return temp;
 968                }
 969        mutex_unlock(&thermal_list_lock);
 970
 971        return NULL;
 972}
 973
 974static int
 975thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
 976{
 977        struct thermal_hwmon_device *hwmon;
 978        struct thermal_hwmon_temp *temp;
 979        int new_hwmon_device = 1;
 980        int result;
 981
 982        hwmon = thermal_hwmon_lookup_by_type(tz);
 983        if (hwmon) {
 984                new_hwmon_device = 0;
 985                goto register_sys_interface;
 986        }
 987
 988        hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
 989        if (!hwmon)
 990                return -ENOMEM;
 991
 992        INIT_LIST_HEAD(&hwmon->tz_list);
 993        strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
 994        hwmon->device = hwmon_device_register(NULL);
 995        if (IS_ERR(hwmon->device)) {
 996                result = PTR_ERR(hwmon->device);
 997                goto free_mem;
 998        }
 999        dev_set_drvdata(hwmon->device, hwmon);
1000        result = device_create_file(hwmon->device, &dev_attr_name);
1001        if (result)
1002                goto free_mem;
1003
1004 register_sys_interface:
1005        temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
1006        if (!temp) {
1007                result = -ENOMEM;
1008                goto unregister_name;
1009        }
1010
1011        temp->tz = tz;
1012        hwmon->count++;
1013
1014        snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
1015                 "temp%d_input", hwmon->count);
1016        temp->temp_input.attr.attr.name = temp->temp_input.name;
1017        temp->temp_input.attr.attr.mode = 0444;
1018        temp->temp_input.attr.show = temp_input_show;
1019        sysfs_attr_init(&temp->temp_input.attr.attr);
1020        result = device_create_file(hwmon->device, &temp->temp_input.attr);
1021        if (result)
1022                goto free_temp_mem;
1023
1024        if (tz->ops->get_crit_temp) {
1025                unsigned long temperature;
1026                if (!tz->ops->get_crit_temp(tz, &temperature)) {
1027                        snprintf(temp->temp_crit.name,
1028                                 sizeof(temp->temp_crit.name),
1029                                "temp%d_crit", hwmon->count);
1030                        temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1031                        temp->temp_crit.attr.attr.mode = 0444;
1032                        temp->temp_crit.attr.show = temp_crit_show;
1033                        sysfs_attr_init(&temp->temp_crit.attr.attr);
1034                        result = device_create_file(hwmon->device,
1035                                                    &temp->temp_crit.attr);
1036                        if (result)
1037                                goto unregister_input;
1038                }
1039        }
1040
1041        mutex_lock(&thermal_list_lock);
1042        if (new_hwmon_device)
1043                list_add_tail(&hwmon->node, &thermal_hwmon_list);
1044        list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
1045        mutex_unlock(&thermal_list_lock);
1046
1047        return 0;
1048
1049 unregister_input:
1050        device_remove_file(hwmon->device, &temp->temp_input.attr);
1051 free_temp_mem:
1052        kfree(temp);
1053 unregister_name:
1054        if (new_hwmon_device) {
1055                device_remove_file(hwmon->device, &dev_attr_name);
1056                hwmon_device_unregister(hwmon->device);
1057        }
1058 free_mem:
1059        if (new_hwmon_device)
1060                kfree(hwmon);
1061
1062        return result;
1063}
1064
1065static void
1066thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1067{
1068        struct thermal_hwmon_device *hwmon;
1069        struct thermal_hwmon_temp *temp;
1070
1071        hwmon = thermal_hwmon_lookup_by_type(tz);
1072        if (unlikely(!hwmon)) {
1073                /* Should never happen... */
1074                dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1075                return;
1076        }
1077
1078        temp = thermal_hwmon_lookup_temp(hwmon, tz);
1079        if (unlikely(!temp)) {
1080                /* Should never happen... */
1081                dev_dbg(&tz->device, "temperature input lookup failed!\n");
1082                return;
1083        }
1084
1085        device_remove_file(hwmon->device, &temp->temp_input.attr);
1086        if (tz->ops->get_crit_temp)
1087                device_remove_file(hwmon->device, &temp->temp_crit.attr);
1088
1089        mutex_lock(&thermal_list_lock);
1090        list_del(&temp->hwmon_node);
1091        kfree(temp);
1092        if (!list_empty(&hwmon->tz_list)) {
1093                mutex_unlock(&thermal_list_lock);
1094                return;
1095        }
1096        list_del(&hwmon->node);
1097        mutex_unlock(&thermal_list_lock);
1098
1099        device_remove_file(hwmon->device, &dev_attr_name);
1100        hwmon_device_unregister(hwmon->device);
1101        kfree(hwmon);
1102}
1103#else
1104static int
1105thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1106{
1107        return 0;
1108}
1109
1110static void
1111thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1112{
1113}
1114#endif
1115
1116/**
1117 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1118 * @tz:         pointer to struct thermal_zone_device
1119 * @trip:       indicates which trip point the cooling devices is
1120 *              associated with in this thermal zone.
1121 * @cdev:       pointer to struct thermal_cooling_device
1122 * @upper:      the Maximum cooling state for this trip point.
1123 *              THERMAL_NO_LIMIT means no upper limit,
1124 *              and the cooling device can be in max_state.
1125 * @lower:      the Minimum cooling state can be used for this trip point.
1126 *              THERMAL_NO_LIMIT means no lower limit,
1127 *              and the cooling device can be in cooling state 0.
1128 *
1129 * This interface function bind a thermal cooling device to the certain trip
1130 * point of a thermal zone device.
1131 * This function is usually called in the thermal zone device .bind callback.
1132 *
1133 * Return: 0 on success, the proper error value otherwise.
1134 */
1135int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1136                                     int trip,
1137                                     struct thermal_cooling_device *cdev,
1138                                     unsigned long upper, unsigned long lower)
1139{
1140        struct thermal_instance *dev;
1141        struct thermal_instance *pos;
1142        struct thermal_zone_device *pos1;
1143        struct thermal_cooling_device *pos2;
1144        unsigned long max_state;
1145        int result;
1146
1147        if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1148                return -EINVAL;
1149
1150        list_for_each_entry(pos1, &thermal_tz_list, node) {
1151                if (pos1 == tz)
1152                        break;
1153        }
1154        list_for_each_entry(pos2, &thermal_cdev_list, node) {
1155                if (pos2 == cdev)
1156                        break;
1157        }
1158
1159        if (tz != pos1 || cdev != pos2)
1160                return -EINVAL;
1161
1162        cdev->ops->get_max_state(cdev, &max_state);
1163
1164        /* lower default 0, upper default max_state */
1165        lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1166        upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1167
1168        if (lower > upper || upper > max_state)
1169                return -EINVAL;
1170
1171        dev =
1172            kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1173        if (!dev)
1174                return -ENOMEM;
1175        dev->tz = tz;
1176        dev->cdev = cdev;
1177        dev->trip = trip;
1178        dev->upper = upper;
1179        dev->lower = lower;
1180        dev->target = THERMAL_NO_TARGET;
1181
1182        result = get_idr(&tz->idr, &tz->lock, &dev->id);
1183        if (result)
1184                goto free_mem;
1185
1186        sprintf(dev->name, "cdev%d", dev->id);
1187        result =
1188            sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1189        if (result)
1190                goto release_idr;
1191
1192        sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1193        sysfs_attr_init(&dev->attr.attr);
1194        dev->attr.attr.name = dev->attr_name;
1195        dev->attr.attr.mode = 0444;
1196        dev->attr.show = thermal_cooling_device_trip_point_show;
1197        result = device_create_file(&tz->device, &dev->attr);
1198        if (result)
1199                goto remove_symbol_link;
1200
1201        mutex_lock(&tz->lock);
1202        mutex_lock(&cdev->lock);
1203        list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1204            if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1205                result = -EEXIST;
1206                break;
1207        }
1208        if (!result) {
1209                list_add_tail(&dev->tz_node, &tz->thermal_instances);
1210                list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1211        }
1212        mutex_unlock(&cdev->lock);
1213        mutex_unlock(&tz->lock);
1214
1215        if (!result)
1216                return 0;
1217
1218        device_remove_file(&tz->device, &dev->attr);
1219remove_symbol_link:
1220        sysfs_remove_link(&tz->device.kobj, dev->name);
1221release_idr:
1222        release_idr(&tz->idr, &tz->lock, dev->id);
1223free_mem:
1224        kfree(dev);
1225        return result;
1226}
1227EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
1228
1229/**
1230 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1231 *                                        thermal zone.
1232 * @tz:         pointer to a struct thermal_zone_device.
1233 * @trip:       indicates which trip point the cooling devices is
1234 *              associated with in this thermal zone.
1235 * @cdev:       pointer to a struct thermal_cooling_device.
1236 *
1237 * This interface function unbind a thermal cooling device from the certain
1238 * trip point of a thermal zone device.
1239 * This function is usually called in the thermal zone device .unbind callback.
1240 *
1241 * Return: 0 on success, the proper error value otherwise.
1242 */
1243int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1244                                       int trip,
1245                                       struct thermal_cooling_device *cdev)
1246{
1247        struct thermal_instance *pos, *next;
1248
1249        mutex_lock(&tz->lock);
1250        mutex_lock(&cdev->lock);
1251        list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1252                if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1253                        list_del(&pos->tz_node);
1254                        list_del(&pos->cdev_node);
1255                        mutex_unlock(&cdev->lock);
1256                        mutex_unlock(&tz->lock);
1257                        goto unbind;
1258                }
1259        }
1260        mutex_unlock(&cdev->lock);
1261        mutex_unlock(&tz->lock);
1262
1263        return -ENODEV;
1264
1265unbind:
1266        device_remove_file(&tz->device, &pos->attr);
1267        sysfs_remove_link(&tz->device.kobj, pos->name);
1268        release_idr(&tz->idr, &tz->lock, pos->id);
1269        kfree(pos);
1270        return 0;
1271}
1272EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1273
1274static void thermal_release(struct device *dev)
1275{
1276        struct thermal_zone_device *tz;
1277        struct thermal_cooling_device *cdev;
1278
1279        if (!strncmp(dev_name(dev), "thermal_zone",
1280                     sizeof("thermal_zone") - 1)) {
1281                tz = to_thermal_zone(dev);
1282                kfree(tz);
1283        } else {
1284                cdev = to_cooling_device(dev);
1285                kfree(cdev);
1286        }
1287}
1288
1289static struct class thermal_class = {
1290        .name = "thermal",
1291        .dev_release = thermal_release,
1292};
1293
1294/**
1295 * thermal_cooling_device_register() - register a new thermal cooling device
1296 * @type:       the thermal cooling device type.
1297 * @devdata:    device private data.
1298 * @ops:                standard thermal cooling devices callbacks.
1299 *
1300 * This interface function adds a new thermal cooling device (fan/processor/...)
1301 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1302 * to all the thermal zone devices registered at the same time.
1303 *
1304 * Return: a pointer to the created struct thermal_cooling_device or an
1305 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1306 */
1307struct thermal_cooling_device *
1308thermal_cooling_device_register(char *type, void *devdata,
1309                                const struct thermal_cooling_device_ops *ops)
1310{
1311        struct thermal_cooling_device *cdev;
1312        int result;
1313
1314        if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1315                return ERR_PTR(-EINVAL);
1316
1317        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1318            !ops->set_cur_state)
1319                return ERR_PTR(-EINVAL);
1320
1321        cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1322        if (!cdev)
1323                return ERR_PTR(-ENOMEM);
1324
1325        result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1326        if (result) {
1327                kfree(cdev);
1328                return ERR_PTR(result);
1329        }
1330
1331        strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1332        mutex_init(&cdev->lock);
1333        INIT_LIST_HEAD(&cdev->thermal_instances);
1334        cdev->ops = ops;
1335        cdev->updated = true;
1336        cdev->device.class = &thermal_class;
1337        cdev->devdata = devdata;
1338        dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1339        result = device_register(&cdev->device);
1340        if (result) {
1341                release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1342                kfree(cdev);
1343                return ERR_PTR(result);
1344        }
1345
1346        /* sys I/F */
1347        if (type) {
1348                result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1349                if (result)
1350                        goto unregister;
1351        }
1352
1353        result = device_create_file(&cdev->device, &dev_attr_max_state);
1354        if (result)
1355                goto unregister;
1356
1357        result = device_create_file(&cdev->device, &dev_attr_cur_state);
1358        if (result)
1359                goto unregister;
1360
1361        /* Add 'this' new cdev to the global cdev list */
1362        mutex_lock(&thermal_list_lock);
1363        list_add(&cdev->node, &thermal_cdev_list);
1364        mutex_unlock(&thermal_list_lock);
1365
1366        /* Update binding information for 'this' new cdev */
1367        bind_cdev(cdev);
1368
1369        return cdev;
1370
1371unregister:
1372        release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1373        device_unregister(&cdev->device);
1374        return ERR_PTR(result);
1375}
1376EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1377
1378/**
1379 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1380 * @cdev:       the thermal cooling device to remove.
1381 *
1382 * thermal_cooling_device_unregister() must be called when the device is no
1383 * longer needed.
1384 */
1385void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1386{
1387        int i;
1388        const struct thermal_zone_params *tzp;
1389        struct thermal_zone_device *tz;
1390        struct thermal_cooling_device *pos = NULL;
1391
1392        if (!cdev)
1393                return;
1394
1395        mutex_lock(&thermal_list_lock);
1396        list_for_each_entry(pos, &thermal_cdev_list, node)
1397            if (pos == cdev)
1398                break;
1399        if (pos != cdev) {
1400                /* thermal cooling device not found */
1401                mutex_unlock(&thermal_list_lock);
1402                return;
1403        }
1404        list_del(&cdev->node);
1405
1406        /* Unbind all thermal zones associated with 'this' cdev */
1407        list_for_each_entry(tz, &thermal_tz_list, node) {
1408                if (tz->ops->unbind) {
1409                        tz->ops->unbind(tz, cdev);
1410                        continue;
1411                }
1412
1413                if (!tz->tzp || !tz->tzp->tbp)
1414                        continue;
1415
1416                tzp = tz->tzp;
1417                for (i = 0; i < tzp->num_tbps; i++) {
1418                        if (tzp->tbp[i].cdev == cdev) {
1419                                __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1420                                tzp->tbp[i].cdev = NULL;
1421                        }
1422                }
1423        }
1424
1425        mutex_unlock(&thermal_list_lock);
1426
1427        if (cdev->type[0])
1428                device_remove_file(&cdev->device, &dev_attr_cdev_type);
1429        device_remove_file(&cdev->device, &dev_attr_max_state);
1430        device_remove_file(&cdev->device, &dev_attr_cur_state);
1431
1432        release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1433        device_unregister(&cdev->device);
1434        return;
1435}
1436EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1437
1438void thermal_cdev_update(struct thermal_cooling_device *cdev)
1439{
1440        struct thermal_instance *instance;
1441        unsigned long target = 0;
1442
1443        /* cooling device is updated*/
1444        if (cdev->updated)
1445                return;
1446
1447        mutex_lock(&cdev->lock);
1448        /* Make sure cdev enters the deepest cooling state */
1449        list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1450                if (instance->target == THERMAL_NO_TARGET)
1451                        continue;
1452                if (instance->target > target)
1453                        target = instance->target;
1454        }
1455        mutex_unlock(&cdev->lock);
1456        cdev->ops->set_cur_state(cdev, target);
1457        cdev->updated = true;
1458}
1459EXPORT_SYMBOL(thermal_cdev_update);
1460
1461/**
1462 * thermal_notify_framework - Sensor drivers use this API to notify framework
1463 * @tz:         thermal zone device
1464 * @trip:       indicates which trip point has been crossed
1465 *
1466 * This function handles the trip events from sensor drivers. It starts
1467 * throttling the cooling devices according to the policy configured.
1468 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1469 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1470 * The throttling policy is based on the configured platform data; if no
1471 * platform data is provided, this uses the step_wise throttling policy.
1472 */
1473void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1474{
1475        handle_thermal_trip(tz, trip);
1476}
1477EXPORT_SYMBOL_GPL(thermal_notify_framework);
1478
1479/**
1480 * create_trip_attrs() - create attributes for trip points
1481 * @tz:         the thermal zone device
1482 * @mask:       Writeable trip point bitmap.
1483 *
1484 * helper function to instantiate sysfs entries for every trip
1485 * point and its properties of a struct thermal_zone_device.
1486 *
1487 * Return: 0 on success, the proper error value otherwise.
1488 */
1489static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1490{
1491        int indx;
1492        int size = sizeof(struct thermal_attr) * tz->trips;
1493
1494        tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1495        if (!tz->trip_type_attrs)
1496                return -ENOMEM;
1497
1498        tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1499        if (!tz->trip_temp_attrs) {
1500                kfree(tz->trip_type_attrs);
1501                return -ENOMEM;
1502        }
1503
1504        if (tz->ops->get_trip_hyst) {
1505                tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1506                if (!tz->trip_hyst_attrs) {
1507                        kfree(tz->trip_type_attrs);
1508                        kfree(tz->trip_temp_attrs);
1509                        return -ENOMEM;
1510                }
1511        }
1512
1513
1514        for (indx = 0; indx < tz->trips; indx++) {
1515                /* create trip type attribute */
1516                snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1517                         "trip_point_%d_type", indx);
1518
1519                sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1520                tz->trip_type_attrs[indx].attr.attr.name =
1521                                                tz->trip_type_attrs[indx].name;
1522                tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1523                tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1524
1525                device_create_file(&tz->device,
1526                                   &tz->trip_type_attrs[indx].attr);
1527
1528                /* create trip temp attribute */
1529                snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1530                         "trip_point_%d_temp", indx);
1531
1532                sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1533                tz->trip_temp_attrs[indx].attr.attr.name =
1534                                                tz->trip_temp_attrs[indx].name;
1535                tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1536                tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1537                if (mask & (1 << indx)) {
1538                        tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1539                        tz->trip_temp_attrs[indx].attr.store =
1540                                                        trip_point_temp_store;
1541                }
1542
1543                device_create_file(&tz->device,
1544                                   &tz->trip_temp_attrs[indx].attr);
1545
1546                /* create Optional trip hyst attribute */
1547                if (!tz->ops->get_trip_hyst)
1548                        continue;
1549                snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1550                         "trip_point_%d_hyst", indx);
1551
1552                sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1553                tz->trip_hyst_attrs[indx].attr.attr.name =
1554                                        tz->trip_hyst_attrs[indx].name;
1555                tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1556                tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1557                if (tz->ops->set_trip_hyst) {
1558                        tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1559                        tz->trip_hyst_attrs[indx].attr.store =
1560                                        trip_point_hyst_store;
1561                }
1562
1563                device_create_file(&tz->device,
1564                                   &tz->trip_hyst_attrs[indx].attr);
1565        }
1566        return 0;
1567}
1568
1569static void remove_trip_attrs(struct thermal_zone_device *tz)
1570{
1571        int indx;
1572
1573        for (indx = 0; indx < tz->trips; indx++) {
1574                device_remove_file(&tz->device,
1575                                   &tz->trip_type_attrs[indx].attr);
1576                device_remove_file(&tz->device,
1577                                   &tz->trip_temp_attrs[indx].attr);
1578                if (tz->ops->get_trip_hyst)
1579                        device_remove_file(&tz->device,
1580                                  &tz->trip_hyst_attrs[indx].attr);
1581        }
1582        kfree(tz->trip_type_attrs);
1583        kfree(tz->trip_temp_attrs);
1584        kfree(tz->trip_hyst_attrs);
1585}
1586
1587/**
1588 * thermal_zone_device_register() - register a new thermal zone device
1589 * @type:       the thermal zone device type
1590 * @trips:      the number of trip points the thermal zone support
1591 * @mask:       a bit string indicating the writeablility of trip points
1592 * @devdata:    private device data
1593 * @ops:        standard thermal zone device callbacks
1594 * @tzp:        thermal zone platform parameters
1595 * @passive_delay: number of milliseconds to wait between polls when
1596 *                 performing passive cooling
1597 * @polling_delay: number of milliseconds to wait between polls when checking
1598 *                 whether trip points have been crossed (0 for interrupt
1599 *                 driven systems)
1600 *
1601 * This interface function adds a new thermal zone device (sensor) to
1602 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1603 * thermal cooling devices registered at the same time.
1604 * thermal_zone_device_unregister() must be called when the device is no
1605 * longer needed. The passive cooling depends on the .get_trend() return value.
1606 *
1607 * Return: a pointer to the created struct thermal_zone_device or an
1608 * in case of error, an ERR_PTR. Caller must check return value with
1609 * IS_ERR*() helpers.
1610 */
1611struct thermal_zone_device *thermal_zone_device_register(const char *type,
1612        int trips, int mask, void *devdata,
1613        const struct thermal_zone_device_ops *ops,
1614        const struct thermal_zone_params *tzp,
1615        int passive_delay, int polling_delay)
1616{
1617        struct thermal_zone_device *tz;
1618        enum thermal_trip_type trip_type;
1619        int result;
1620        int count;
1621        int passive = 0;
1622
1623        if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1624                return ERR_PTR(-EINVAL);
1625
1626        if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1627                return ERR_PTR(-EINVAL);
1628
1629        if (!ops || !ops->get_temp)
1630                return ERR_PTR(-EINVAL);
1631
1632        if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1633                return ERR_PTR(-EINVAL);
1634
1635        tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1636        if (!tz)
1637                return ERR_PTR(-ENOMEM);
1638
1639        INIT_LIST_HEAD(&tz->thermal_instances);
1640        idr_init(&tz->idr);
1641        mutex_init(&tz->lock);
1642        result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1643        if (result) {
1644                kfree(tz);
1645                return ERR_PTR(result);
1646        }
1647
1648        strlcpy(tz->type, type ? : "", sizeof(tz->type));
1649        tz->ops = ops;
1650        tz->tzp = tzp;
1651        tz->device.class = &thermal_class;
1652        tz->devdata = devdata;
1653        tz->trips = trips;
1654        tz->passive_delay = passive_delay;
1655        tz->polling_delay = polling_delay;
1656
1657        dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1658        result = device_register(&tz->device);
1659        if (result) {
1660                release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1661                kfree(tz);
1662                return ERR_PTR(result);
1663        }
1664
1665        /* sys I/F */
1666        if (type) {
1667                result = device_create_file(&tz->device, &dev_attr_type);
1668                if (result)
1669                        goto unregister;
1670        }
1671
1672        result = device_create_file(&tz->device, &dev_attr_temp);
1673        if (result)
1674                goto unregister;
1675
1676        if (ops->get_mode) {
1677                result = device_create_file(&tz->device, &dev_attr_mode);
1678                if (result)
1679                        goto unregister;
1680        }
1681
1682        result = create_trip_attrs(tz, mask);
1683        if (result)
1684                goto unregister;
1685
1686        for (count = 0; count < trips; count++) {
1687                tz->ops->get_trip_type(tz, count, &trip_type);
1688                if (trip_type == THERMAL_TRIP_PASSIVE)
1689                        passive = 1;
1690        }
1691
1692        if (!passive) {
1693                result = device_create_file(&tz->device, &dev_attr_passive);
1694                if (result)
1695                        goto unregister;
1696        }
1697
1698#ifdef CONFIG_THERMAL_EMULATION
1699        result = device_create_file(&tz->device, &dev_attr_emul_temp);
1700        if (result)
1701                goto unregister;
1702#endif
1703        /* Create policy attribute */
1704        result = device_create_file(&tz->device, &dev_attr_policy);
1705        if (result)
1706                goto unregister;
1707
1708        /* Update 'this' zone's governor information */
1709        mutex_lock(&thermal_governor_lock);
1710
1711        if (tz->tzp)
1712                tz->governor = __find_governor(tz->tzp->governor_name);
1713        else
1714                tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1715
1716        mutex_unlock(&thermal_governor_lock);
1717
1718        result = thermal_add_hwmon_sysfs(tz);
1719        if (result)
1720                goto unregister;
1721
1722        mutex_lock(&thermal_list_lock);
1723        list_add_tail(&tz->node, &thermal_tz_list);
1724        mutex_unlock(&thermal_list_lock);
1725
1726        /* Bind cooling devices for this zone */
1727        bind_tz(tz);
1728
1729        INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1730
1731        thermal_zone_device_update(tz);
1732
1733        if (!result)
1734                return tz;
1735
1736unregister:
1737        release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1738        device_unregister(&tz->device);
1739        return ERR_PTR(result);
1740}
1741EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1742
1743/**
1744 * thermal_device_unregister - removes the registered thermal zone device
1745 * @tz: the thermal zone device to remove
1746 */
1747void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1748{
1749        int i;
1750        const struct thermal_zone_params *tzp;
1751        struct thermal_cooling_device *cdev;
1752        struct thermal_zone_device *pos = NULL;
1753
1754        if (!tz)
1755                return;
1756
1757        tzp = tz->tzp;
1758
1759        mutex_lock(&thermal_list_lock);
1760        list_for_each_entry(pos, &thermal_tz_list, node)
1761            if (pos == tz)
1762                break;
1763        if (pos != tz) {
1764                /* thermal zone device not found */
1765                mutex_unlock(&thermal_list_lock);
1766                return;
1767        }
1768        list_del(&tz->node);
1769
1770        /* Unbind all cdevs associated with 'this' thermal zone */
1771        list_for_each_entry(cdev, &thermal_cdev_list, node) {
1772                if (tz->ops->unbind) {
1773                        tz->ops->unbind(tz, cdev);
1774                        continue;
1775                }
1776
1777                if (!tzp || !tzp->tbp)
1778                        break;
1779
1780                for (i = 0; i < tzp->num_tbps; i++) {
1781                        if (tzp->tbp[i].cdev == cdev) {
1782                                __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1783                                tzp->tbp[i].cdev = NULL;
1784                        }
1785                }
1786        }
1787
1788        mutex_unlock(&thermal_list_lock);
1789
1790        thermal_zone_device_set_polling(tz, 0);
1791
1792        if (tz->type[0])
1793                device_remove_file(&tz->device, &dev_attr_type);
1794        device_remove_file(&tz->device, &dev_attr_temp);
1795        if (tz->ops->get_mode)
1796                device_remove_file(&tz->device, &dev_attr_mode);
1797        device_remove_file(&tz->device, &dev_attr_policy);
1798        remove_trip_attrs(tz);
1799        tz->governor = NULL;
1800
1801        thermal_remove_hwmon_sysfs(tz);
1802        release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1803        idr_destroy(&tz->idr);
1804        mutex_destroy(&tz->lock);
1805        device_unregister(&tz->device);
1806        return;
1807}
1808EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1809
1810/**
1811 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1812 * @name: thermal zone name to fetch the temperature
1813 *
1814 * When only one zone is found with the passed name, returns a reference to it.
1815 *
1816 * Return: On success returns a reference to an unique thermal zone with
1817 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1818 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1819 */
1820struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1821{
1822        struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1823        unsigned int found = 0;
1824
1825        if (!name)
1826                goto exit;
1827
1828        mutex_lock(&thermal_list_lock);
1829        list_for_each_entry(pos, &thermal_tz_list, node)
1830                if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1831                        found++;
1832                        ref = pos;
1833                }
1834        mutex_unlock(&thermal_list_lock);
1835
1836        /* nothing has been found, thus an error code for it */
1837        if (found == 0)
1838                ref = ERR_PTR(-ENODEV);
1839        else if (found > 1)
1840        /* Success only when an unique zone is found */
1841                ref = ERR_PTR(-EEXIST);
1842
1843exit:
1844        return ref;
1845}
1846EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1847
1848#ifdef CONFIG_NET
1849static struct genl_family thermal_event_genl_family = {
1850        .id = GENL_ID_GENERATE,
1851        .name = THERMAL_GENL_FAMILY_NAME,
1852        .version = THERMAL_GENL_VERSION,
1853        .maxattr = THERMAL_GENL_ATTR_MAX,
1854};
1855
1856static struct genl_multicast_group thermal_event_mcgrp = {
1857        .name = THERMAL_GENL_MCAST_GROUP_NAME,
1858};
1859
1860int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1861                                        enum events event)
1862{
1863        struct sk_buff *skb;
1864        struct nlattr *attr;
1865        struct thermal_genl_event *thermal_event;
1866        void *msg_header;
1867        int size;
1868        int result;
1869        static unsigned int thermal_event_seqnum;
1870
1871        if (!tz)
1872                return -EINVAL;
1873
1874        /* allocate memory */
1875        size = nla_total_size(sizeof(struct thermal_genl_event)) +
1876               nla_total_size(0);
1877
1878        skb = genlmsg_new(size, GFP_ATOMIC);
1879        if (!skb)
1880                return -ENOMEM;
1881
1882        /* add the genetlink message header */
1883        msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1884                                 &thermal_event_genl_family, 0,
1885                                 THERMAL_GENL_CMD_EVENT);
1886        if (!msg_header) {
1887                nlmsg_free(skb);
1888                return -ENOMEM;
1889        }
1890
1891        /* fill the data */
1892        attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1893                           sizeof(struct thermal_genl_event));
1894
1895        if (!attr) {
1896                nlmsg_free(skb);
1897                return -EINVAL;
1898        }
1899
1900        thermal_event = nla_data(attr);
1901        if (!thermal_event) {
1902                nlmsg_free(skb);
1903                return -EINVAL;
1904        }
1905
1906        memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1907
1908        thermal_event->orig = tz->id;
1909        thermal_event->event = event;
1910
1911        /* send multicast genetlink message */
1912        result = genlmsg_end(skb, msg_header);
1913        if (result < 0) {
1914                nlmsg_free(skb);
1915                return result;
1916        }
1917
1918        result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1919        if (result)
1920                dev_err(&tz->device, "Failed to send netlink event:%d", result);
1921
1922        return result;
1923}
1924EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1925
1926static int genetlink_init(void)
1927{
1928        int result;
1929
1930        result = genl_register_family(&thermal_event_genl_family);
1931        if (result)
1932                return result;
1933
1934        result = genl_register_mc_group(&thermal_event_genl_family,
1935                                        &thermal_event_mcgrp);
1936        if (result)
1937                genl_unregister_family(&thermal_event_genl_family);
1938        return result;
1939}
1940
1941static void genetlink_exit(void)
1942{
1943        genl_unregister_family(&thermal_event_genl_family);
1944}
1945#else /* !CONFIG_NET */
1946static inline int genetlink_init(void) { return 0; }
1947static inline void genetlink_exit(void) {}
1948#endif /* !CONFIG_NET */
1949
1950static int __init thermal_register_governors(void)
1951{
1952        int result;
1953
1954        result = thermal_gov_step_wise_register();
1955        if (result)
1956                return result;
1957
1958        result = thermal_gov_fair_share_register();
1959        if (result)
1960                return result;
1961
1962        return thermal_gov_user_space_register();
1963}
1964
1965static void thermal_unregister_governors(void)
1966{
1967        thermal_gov_step_wise_unregister();
1968        thermal_gov_fair_share_unregister();
1969        thermal_gov_user_space_unregister();
1970}
1971
1972static int __init thermal_init(void)
1973{
1974        int result;
1975
1976        result = thermal_register_governors();
1977        if (result)
1978                goto error;
1979
1980        result = class_register(&thermal_class);
1981        if (result)
1982                goto unregister_governors;
1983
1984        result = genetlink_init();
1985        if (result)
1986                goto unregister_class;
1987
1988        return 0;
1989
1990unregister_governors:
1991        thermal_unregister_governors();
1992unregister_class:
1993        class_unregister(&thermal_class);
1994error:
1995        idr_destroy(&thermal_tz_idr);
1996        idr_destroy(&thermal_cdev_idr);
1997        mutex_destroy(&thermal_idr_lock);
1998        mutex_destroy(&thermal_list_lock);
1999        mutex_destroy(&thermal_governor_lock);
2000        return result;
2001}
2002
2003static void __exit thermal_exit(void)
2004{
2005        genetlink_exit();
2006        class_unregister(&thermal_class);
2007        thermal_unregister_governors();
2008        idr_destroy(&thermal_tz_idr);
2009        idr_destroy(&thermal_cdev_idr);
2010        mutex_destroy(&thermal_idr_lock);
2011        mutex_destroy(&thermal_list_lock);
2012        mutex_destroy(&thermal_governor_lock);
2013}
2014
2015fs_initcall(thermal_init);
2016module_exit(thermal_exit);
2017
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.