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