0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011
0012 #include <linux/device.h>
0013 #include <linux/err.h>
0014 #include <linux/export.h>
0015 #include <linux/slab.h>
0016 #include <linux/kdev_t.h>
0017 #include <linux/idr.h>
0018 #include <linux/thermal.h>
0019 #include <linux/reboot.h>
0020 #include <linux/string.h>
0021 #include <linux/of.h>
0022 #include <linux/suspend.h>
0023
0024 #define CREATE_TRACE_POINTS
0025 #include <trace/events/thermal.h>
0026
0027 #include "thermal_core.h"
0028 #include "thermal_hwmon.h"
0029
0030 static DEFINE_IDA(thermal_tz_ida);
0031 static DEFINE_IDA(thermal_cdev_ida);
0032
0033 static LIST_HEAD(thermal_tz_list);
0034 static LIST_HEAD(thermal_cdev_list);
0035 static LIST_HEAD(thermal_governor_list);
0036
0037 static DEFINE_MUTEX(thermal_list_lock);
0038 static DEFINE_MUTEX(thermal_governor_lock);
0039
0040 static atomic_t in_suspend;
0041
0042 static struct thermal_governor *def_governor;
0043
0044
0045
0046
0047
0048
0049
0050
0051 static struct thermal_governor *__find_governor(const char *name)
0052 {
0053 struct thermal_governor *pos;
0054
0055 if (!name || !name[0])
0056 return def_governor;
0057
0058 list_for_each_entry(pos, &thermal_governor_list, governor_list)
0059 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
0060 return pos;
0061
0062 return NULL;
0063 }
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073 static void bind_previous_governor(struct thermal_zone_device *tz,
0074 const char *failed_gov_name)
0075 {
0076 if (tz->governor && tz->governor->bind_to_tz) {
0077 if (tz->governor->bind_to_tz(tz)) {
0078 dev_err(&tz->device,
0079 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
0080 failed_gov_name, tz->governor->name, tz->type);
0081 tz->governor = NULL;
0082 }
0083 }
0084 }
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095 static int thermal_set_governor(struct thermal_zone_device *tz,
0096 struct thermal_governor *new_gov)
0097 {
0098 int ret = 0;
0099
0100 if (tz->governor && tz->governor->unbind_from_tz)
0101 tz->governor->unbind_from_tz(tz);
0102
0103 if (new_gov && new_gov->bind_to_tz) {
0104 ret = new_gov->bind_to_tz(tz);
0105 if (ret) {
0106 bind_previous_governor(tz, new_gov->name);
0107
0108 return ret;
0109 }
0110 }
0111
0112 tz->governor = new_gov;
0113
0114 return ret;
0115 }
0116
0117 int thermal_register_governor(struct thermal_governor *governor)
0118 {
0119 int err;
0120 const char *name;
0121 struct thermal_zone_device *pos;
0122
0123 if (!governor)
0124 return -EINVAL;
0125
0126 mutex_lock(&thermal_governor_lock);
0127
0128 err = -EBUSY;
0129 if (!__find_governor(governor->name)) {
0130 bool match_default;
0131
0132 err = 0;
0133 list_add(&governor->governor_list, &thermal_governor_list);
0134 match_default = !strncmp(governor->name,
0135 DEFAULT_THERMAL_GOVERNOR,
0136 THERMAL_NAME_LENGTH);
0137
0138 if (!def_governor && match_default)
0139 def_governor = governor;
0140 }
0141
0142 mutex_lock(&thermal_list_lock);
0143
0144 list_for_each_entry(pos, &thermal_tz_list, node) {
0145
0146
0147
0148
0149 if (pos->governor)
0150 continue;
0151
0152 name = pos->tzp->governor_name;
0153
0154 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
0155 int ret;
0156
0157 ret = thermal_set_governor(pos, governor);
0158 if (ret)
0159 dev_err(&pos->device,
0160 "Failed to set governor %s for thermal zone %s: %d\n",
0161 governor->name, pos->type, ret);
0162 }
0163 }
0164
0165 mutex_unlock(&thermal_list_lock);
0166 mutex_unlock(&thermal_governor_lock);
0167
0168 return err;
0169 }
0170
0171 void thermal_unregister_governor(struct thermal_governor *governor)
0172 {
0173 struct thermal_zone_device *pos;
0174
0175 if (!governor)
0176 return;
0177
0178 mutex_lock(&thermal_governor_lock);
0179
0180 if (!__find_governor(governor->name))
0181 goto exit;
0182
0183 mutex_lock(&thermal_list_lock);
0184
0185 list_for_each_entry(pos, &thermal_tz_list, node) {
0186 if (!strncasecmp(pos->governor->name, governor->name,
0187 THERMAL_NAME_LENGTH))
0188 thermal_set_governor(pos, NULL);
0189 }
0190
0191 mutex_unlock(&thermal_list_lock);
0192 list_del(&governor->governor_list);
0193 exit:
0194 mutex_unlock(&thermal_governor_lock);
0195 }
0196
0197 int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
0198 char *policy)
0199 {
0200 struct thermal_governor *gov;
0201 int ret = -EINVAL;
0202
0203 mutex_lock(&thermal_governor_lock);
0204 mutex_lock(&tz->lock);
0205
0206 gov = __find_governor(strim(policy));
0207 if (!gov)
0208 goto exit;
0209
0210 ret = thermal_set_governor(tz, gov);
0211
0212 exit:
0213 mutex_unlock(&tz->lock);
0214 mutex_unlock(&thermal_governor_lock);
0215
0216 thermal_notify_tz_gov_change(tz->id, policy);
0217
0218 return ret;
0219 }
0220
0221 int thermal_build_list_of_policies(char *buf)
0222 {
0223 struct thermal_governor *pos;
0224 ssize_t count = 0;
0225
0226 mutex_lock(&thermal_governor_lock);
0227
0228 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
0229 count += scnprintf(buf + count, PAGE_SIZE - count, "%s ",
0230 pos->name);
0231 }
0232 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
0233
0234 mutex_unlock(&thermal_governor_lock);
0235
0236 return count;
0237 }
0238
0239 static void __init thermal_unregister_governors(void)
0240 {
0241 struct thermal_governor **governor;
0242
0243 for_each_governor_table(governor)
0244 thermal_unregister_governor(*governor);
0245 }
0246
0247 static int __init thermal_register_governors(void)
0248 {
0249 int ret = 0;
0250 struct thermal_governor **governor;
0251
0252 for_each_governor_table(governor) {
0253 ret = thermal_register_governor(*governor);
0254 if (ret) {
0255 pr_err("Failed to register governor: '%s'",
0256 (*governor)->name);
0257 break;
0258 }
0259
0260 pr_info("Registered thermal governor '%s'",
0261 (*governor)->name);
0262 }
0263
0264 if (ret) {
0265 struct thermal_governor **gov;
0266
0267 for_each_governor_table(gov) {
0268 if (gov == governor)
0269 break;
0270 thermal_unregister_governor(*gov);
0271 }
0272 }
0273
0274 return ret;
0275 }
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
0289 unsigned long delay)
0290 {
0291 if (delay)
0292 mod_delayed_work(system_freezable_power_efficient_wq,
0293 &tz->poll_queue, delay);
0294 else
0295 cancel_delayed_work(&tz->poll_queue);
0296 }
0297
0298 static inline bool should_stop_polling(struct thermal_zone_device *tz)
0299 {
0300 return !thermal_zone_device_is_enabled(tz);
0301 }
0302
0303 static void monitor_thermal_zone(struct thermal_zone_device *tz)
0304 {
0305 bool stop;
0306
0307 stop = should_stop_polling(tz);
0308
0309 mutex_lock(&tz->lock);
0310
0311 if (!stop && tz->passive)
0312 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
0313 else if (!stop && tz->polling_delay_jiffies)
0314 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
0315 else
0316 thermal_zone_device_set_polling(tz, 0);
0317
0318 mutex_unlock(&tz->lock);
0319 }
0320
0321 static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
0322 {
0323 tz->governor ? tz->governor->throttle(tz, trip) :
0324 def_governor->throttle(tz, trip);
0325 }
0326
0327 void thermal_zone_device_critical(struct thermal_zone_device *tz)
0328 {
0329
0330
0331
0332
0333 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
0334
0335 dev_emerg(&tz->device, "%s: critical temperature reached, "
0336 "shutting down\n", tz->type);
0337
0338 hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
0339 }
0340 EXPORT_SYMBOL(thermal_zone_device_critical);
0341
0342 static void handle_critical_trips(struct thermal_zone_device *tz,
0343 int trip, int trip_temp, enum thermal_trip_type trip_type)
0344 {
0345
0346 if (trip_temp <= 0 || tz->temperature < trip_temp)
0347 return;
0348
0349 trace_thermal_zone_trip(tz, trip, trip_type);
0350
0351 if (trip_type == THERMAL_TRIP_HOT && tz->ops->hot)
0352 tz->ops->hot(tz);
0353 else if (trip_type == THERMAL_TRIP_CRITICAL)
0354 tz->ops->critical(tz);
0355 }
0356
0357 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
0358 {
0359 enum thermal_trip_type type;
0360 int trip_temp, hyst = 0;
0361
0362
0363 if (test_bit(trip, &tz->trips_disabled))
0364 return;
0365
0366 tz->ops->get_trip_temp(tz, trip, &trip_temp);
0367 tz->ops->get_trip_type(tz, trip, &type);
0368 if (tz->ops->get_trip_hyst)
0369 tz->ops->get_trip_hyst(tz, trip, &hyst);
0370
0371 if (tz->last_temperature != THERMAL_TEMP_INVALID) {
0372 if (tz->last_temperature < trip_temp &&
0373 tz->temperature >= trip_temp)
0374 thermal_notify_tz_trip_up(tz->id, trip,
0375 tz->temperature);
0376 if (tz->last_temperature >= trip_temp &&
0377 tz->temperature < (trip_temp - hyst))
0378 thermal_notify_tz_trip_down(tz->id, trip,
0379 tz->temperature);
0380 }
0381
0382 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
0383 handle_critical_trips(tz, trip, trip_temp, type);
0384 else
0385 handle_non_critical_trips(tz, trip);
0386
0387
0388
0389
0390 monitor_thermal_zone(tz);
0391 }
0392
0393 static void update_temperature(struct thermal_zone_device *tz)
0394 {
0395 int temp, ret;
0396
0397 ret = thermal_zone_get_temp(tz, &temp);
0398 if (ret) {
0399 if (ret != -EAGAIN)
0400 dev_warn(&tz->device,
0401 "failed to read out thermal zone (%d)\n",
0402 ret);
0403 return;
0404 }
0405
0406 mutex_lock(&tz->lock);
0407 tz->last_temperature = tz->temperature;
0408 tz->temperature = temp;
0409 mutex_unlock(&tz->lock);
0410
0411 trace_thermal_temperature(tz);
0412
0413 thermal_genl_sampling_temp(tz->id, temp);
0414 }
0415
0416 static void thermal_zone_device_init(struct thermal_zone_device *tz)
0417 {
0418 struct thermal_instance *pos;
0419 tz->temperature = THERMAL_TEMP_INVALID;
0420 tz->prev_low_trip = -INT_MAX;
0421 tz->prev_high_trip = INT_MAX;
0422 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
0423 pos->initialized = false;
0424 }
0425
0426 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
0427 enum thermal_device_mode mode)
0428 {
0429 int ret = 0;
0430
0431 mutex_lock(&tz->lock);
0432
0433
0434 if (mode == tz->mode) {
0435 mutex_unlock(&tz->lock);
0436
0437 return ret;
0438 }
0439
0440 if (tz->ops->change_mode)
0441 ret = tz->ops->change_mode(tz, mode);
0442
0443 if (!ret)
0444 tz->mode = mode;
0445
0446 mutex_unlock(&tz->lock);
0447
0448 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
0449
0450 if (mode == THERMAL_DEVICE_ENABLED)
0451 thermal_notify_tz_enable(tz->id);
0452 else
0453 thermal_notify_tz_disable(tz->id);
0454
0455 return ret;
0456 }
0457
0458 int thermal_zone_device_enable(struct thermal_zone_device *tz)
0459 {
0460 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
0461 }
0462 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
0463
0464 int thermal_zone_device_disable(struct thermal_zone_device *tz)
0465 {
0466 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
0467 }
0468 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
0469
0470 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
0471 {
0472 enum thermal_device_mode mode;
0473
0474 mutex_lock(&tz->lock);
0475
0476 mode = tz->mode;
0477
0478 mutex_unlock(&tz->lock);
0479
0480 return mode == THERMAL_DEVICE_ENABLED;
0481 }
0482
0483 void thermal_zone_device_update(struct thermal_zone_device *tz,
0484 enum thermal_notify_event event)
0485 {
0486 int count;
0487
0488 if (should_stop_polling(tz))
0489 return;
0490
0491 if (atomic_read(&in_suspend))
0492 return;
0493
0494 if (WARN_ONCE(!tz->ops->get_temp, "'%s' must not be called without "
0495 "'get_temp' ops set\n", __func__))
0496 return;
0497
0498 update_temperature(tz);
0499
0500 thermal_zone_set_trips(tz);
0501
0502 tz->notify_event = event;
0503
0504 for (count = 0; count < tz->num_trips; count++)
0505 handle_thermal_trip(tz, count);
0506 }
0507 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
0508
0509 static void thermal_zone_device_check(struct work_struct *work)
0510 {
0511 struct thermal_zone_device *tz = container_of(work, struct
0512 thermal_zone_device,
0513 poll_queue.work);
0514 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
0515 }
0516
0517 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
0518 void *data)
0519 {
0520 struct thermal_governor *gov;
0521 int ret = 0;
0522
0523 mutex_lock(&thermal_governor_lock);
0524 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
0525 ret = cb(gov, data);
0526 if (ret)
0527 break;
0528 }
0529 mutex_unlock(&thermal_governor_lock);
0530
0531 return ret;
0532 }
0533
0534 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
0535 void *), void *data)
0536 {
0537 struct thermal_cooling_device *cdev;
0538 int ret = 0;
0539
0540 mutex_lock(&thermal_list_lock);
0541 list_for_each_entry(cdev, &thermal_cdev_list, node) {
0542 ret = cb(cdev, data);
0543 if (ret)
0544 break;
0545 }
0546 mutex_unlock(&thermal_list_lock);
0547
0548 return ret;
0549 }
0550
0551 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
0552 void *data)
0553 {
0554 struct thermal_zone_device *tz;
0555 int ret = 0;
0556
0557 mutex_lock(&thermal_list_lock);
0558 list_for_each_entry(tz, &thermal_tz_list, node) {
0559 ret = cb(tz, data);
0560 if (ret)
0561 break;
0562 }
0563 mutex_unlock(&thermal_list_lock);
0564
0565 return ret;
0566 }
0567
0568 struct thermal_zone_device *thermal_zone_get_by_id(int id)
0569 {
0570 struct thermal_zone_device *tz, *match = NULL;
0571
0572 mutex_lock(&thermal_list_lock);
0573 list_for_each_entry(tz, &thermal_tz_list, node) {
0574 if (tz->id == id) {
0575 match = tz;
0576 break;
0577 }
0578 }
0579 mutex_unlock(&thermal_list_lock);
0580
0581 return match;
0582 }
0583
0584
0585
0586
0587
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
0617 int trip,
0618 struct thermal_cooling_device *cdev,
0619 unsigned long upper, unsigned long lower,
0620 unsigned int weight)
0621 {
0622 struct thermal_instance *dev;
0623 struct thermal_instance *pos;
0624 struct thermal_zone_device *pos1;
0625 struct thermal_cooling_device *pos2;
0626 unsigned long max_state;
0627 int result, ret;
0628
0629 if (trip >= tz->num_trips || trip < 0)
0630 return -EINVAL;
0631
0632 list_for_each_entry(pos1, &thermal_tz_list, node) {
0633 if (pos1 == tz)
0634 break;
0635 }
0636 list_for_each_entry(pos2, &thermal_cdev_list, node) {
0637 if (pos2 == cdev)
0638 break;
0639 }
0640
0641 if (tz != pos1 || cdev != pos2)
0642 return -EINVAL;
0643
0644 ret = cdev->ops->get_max_state(cdev, &max_state);
0645 if (ret)
0646 return ret;
0647
0648
0649 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
0650 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
0651
0652 if (lower > upper || upper > max_state)
0653 return -EINVAL;
0654
0655 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
0656 if (!dev)
0657 return -ENOMEM;
0658 dev->tz = tz;
0659 dev->cdev = cdev;
0660 dev->trip = trip;
0661 dev->upper = upper;
0662 dev->lower = lower;
0663 dev->target = THERMAL_NO_TARGET;
0664 dev->weight = weight;
0665
0666 result = ida_alloc(&tz->ida, GFP_KERNEL);
0667 if (result < 0)
0668 goto free_mem;
0669
0670 dev->id = result;
0671 sprintf(dev->name, "cdev%d", dev->id);
0672 result =
0673 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
0674 if (result)
0675 goto release_ida;
0676
0677 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
0678 sysfs_attr_init(&dev->attr.attr);
0679 dev->attr.attr.name = dev->attr_name;
0680 dev->attr.attr.mode = 0444;
0681 dev->attr.show = trip_point_show;
0682 result = device_create_file(&tz->device, &dev->attr);
0683 if (result)
0684 goto remove_symbol_link;
0685
0686 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
0687 sysfs_attr_init(&dev->weight_attr.attr);
0688 dev->weight_attr.attr.name = dev->weight_attr_name;
0689 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
0690 dev->weight_attr.show = weight_show;
0691 dev->weight_attr.store = weight_store;
0692 result = device_create_file(&tz->device, &dev->weight_attr);
0693 if (result)
0694 goto remove_trip_file;
0695
0696 mutex_lock(&tz->lock);
0697 mutex_lock(&cdev->lock);
0698 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
0699 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
0700 result = -EEXIST;
0701 break;
0702 }
0703 if (!result) {
0704 list_add_tail(&dev->tz_node, &tz->thermal_instances);
0705 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
0706 atomic_set(&tz->need_update, 1);
0707 }
0708 mutex_unlock(&cdev->lock);
0709 mutex_unlock(&tz->lock);
0710
0711 if (!result)
0712 return 0;
0713
0714 device_remove_file(&tz->device, &dev->weight_attr);
0715 remove_trip_file:
0716 device_remove_file(&tz->device, &dev->attr);
0717 remove_symbol_link:
0718 sysfs_remove_link(&tz->device.kobj, dev->name);
0719 release_ida:
0720 ida_free(&tz->ida, dev->id);
0721 free_mem:
0722 kfree(dev);
0723 return result;
0724 }
0725 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
0742 int trip,
0743 struct thermal_cooling_device *cdev)
0744 {
0745 struct thermal_instance *pos, *next;
0746
0747 mutex_lock(&tz->lock);
0748 mutex_lock(&cdev->lock);
0749 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
0750 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
0751 list_del(&pos->tz_node);
0752 list_del(&pos->cdev_node);
0753 mutex_unlock(&cdev->lock);
0754 mutex_unlock(&tz->lock);
0755 goto unbind;
0756 }
0757 }
0758 mutex_unlock(&cdev->lock);
0759 mutex_unlock(&tz->lock);
0760
0761 return -ENODEV;
0762
0763 unbind:
0764 device_remove_file(&tz->device, &pos->weight_attr);
0765 device_remove_file(&tz->device, &pos->attr);
0766 sysfs_remove_link(&tz->device.kobj, pos->name);
0767 ida_free(&tz->ida, pos->id);
0768 kfree(pos);
0769 return 0;
0770 }
0771 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
0772
0773 static void thermal_release(struct device *dev)
0774 {
0775 struct thermal_zone_device *tz;
0776 struct thermal_cooling_device *cdev;
0777
0778 if (!strncmp(dev_name(dev), "thermal_zone",
0779 sizeof("thermal_zone") - 1)) {
0780 tz = to_thermal_zone(dev);
0781 thermal_zone_destroy_device_groups(tz);
0782 kfree(tz);
0783 } else if (!strncmp(dev_name(dev), "cooling_device",
0784 sizeof("cooling_device") - 1)) {
0785 cdev = to_cooling_device(dev);
0786 kfree(cdev);
0787 }
0788 }
0789
0790 static struct class thermal_class = {
0791 .name = "thermal",
0792 .dev_release = thermal_release,
0793 };
0794
0795 static inline
0796 void print_bind_err_msg(struct thermal_zone_device *tz,
0797 struct thermal_cooling_device *cdev, int ret)
0798 {
0799 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
0800 tz->type, cdev->type, ret);
0801 }
0802
0803 static void __bind(struct thermal_zone_device *tz, int mask,
0804 struct thermal_cooling_device *cdev,
0805 unsigned long *limits,
0806 unsigned int weight)
0807 {
0808 int i, ret;
0809
0810 for (i = 0; i < tz->num_trips; i++) {
0811 if (mask & (1 << i)) {
0812 unsigned long upper, lower;
0813
0814 upper = THERMAL_NO_LIMIT;
0815 lower = THERMAL_NO_LIMIT;
0816 if (limits) {
0817 lower = limits[i * 2];
0818 upper = limits[i * 2 + 1];
0819 }
0820 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
0821 upper, lower,
0822 weight);
0823 if (ret)
0824 print_bind_err_msg(tz, cdev, ret);
0825 }
0826 }
0827 }
0828
0829 static void bind_cdev(struct thermal_cooling_device *cdev)
0830 {
0831 int i, ret;
0832 const struct thermal_zone_params *tzp;
0833 struct thermal_zone_device *pos = NULL;
0834
0835 mutex_lock(&thermal_list_lock);
0836
0837 list_for_each_entry(pos, &thermal_tz_list, node) {
0838 if (!pos->tzp && !pos->ops->bind)
0839 continue;
0840
0841 if (pos->ops->bind) {
0842 ret = pos->ops->bind(pos, cdev);
0843 if (ret)
0844 print_bind_err_msg(pos, cdev, ret);
0845 continue;
0846 }
0847
0848 tzp = pos->tzp;
0849 if (!tzp || !tzp->tbp)
0850 continue;
0851
0852 for (i = 0; i < tzp->num_tbps; i++) {
0853 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
0854 continue;
0855 if (tzp->tbp[i].match(pos, cdev))
0856 continue;
0857 tzp->tbp[i].cdev = cdev;
0858 __bind(pos, tzp->tbp[i].trip_mask, cdev,
0859 tzp->tbp[i].binding_limits,
0860 tzp->tbp[i].weight);
0861 }
0862 }
0863
0864 mutex_unlock(&thermal_list_lock);
0865 }
0866
0867
0868
0869
0870
0871
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881
0882
0883 static struct thermal_cooling_device *
0884 __thermal_cooling_device_register(struct device_node *np,
0885 const char *type, void *devdata,
0886 const struct thermal_cooling_device_ops *ops)
0887 {
0888 struct thermal_cooling_device *cdev;
0889 struct thermal_zone_device *pos = NULL;
0890 int id, ret;
0891
0892 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
0893 !ops->set_cur_state)
0894 return ERR_PTR(-EINVAL);
0895
0896 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
0897 if (!cdev)
0898 return ERR_PTR(-ENOMEM);
0899
0900 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
0901 if (ret < 0)
0902 goto out_kfree_cdev;
0903 cdev->id = ret;
0904 id = ret;
0905
0906 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
0907 if (ret)
0908 goto out_ida_remove;
0909
0910 cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
0911 if (!cdev->type) {
0912 ret = -ENOMEM;
0913 goto out_ida_remove;
0914 }
0915
0916 mutex_init(&cdev->lock);
0917 INIT_LIST_HEAD(&cdev->thermal_instances);
0918 cdev->np = np;
0919 cdev->ops = ops;
0920 cdev->updated = false;
0921 cdev->device.class = &thermal_class;
0922 cdev->devdata = devdata;
0923 thermal_cooling_device_setup_sysfs(cdev);
0924 ret = device_register(&cdev->device);
0925 if (ret)
0926 goto out_kfree_type;
0927
0928
0929 mutex_lock(&thermal_list_lock);
0930 list_add(&cdev->node, &thermal_cdev_list);
0931 mutex_unlock(&thermal_list_lock);
0932
0933
0934 bind_cdev(cdev);
0935
0936 mutex_lock(&thermal_list_lock);
0937 list_for_each_entry(pos, &thermal_tz_list, node)
0938 if (atomic_cmpxchg(&pos->need_update, 1, 0))
0939 thermal_zone_device_update(pos,
0940 THERMAL_EVENT_UNSPECIFIED);
0941 mutex_unlock(&thermal_list_lock);
0942
0943 return cdev;
0944
0945 out_kfree_type:
0946 thermal_cooling_device_destroy_sysfs(cdev);
0947 kfree(cdev->type);
0948 put_device(&cdev->device);
0949 cdev = NULL;
0950 out_ida_remove:
0951 ida_free(&thermal_cdev_ida, id);
0952 out_kfree_cdev:
0953 kfree(cdev);
0954 return ERR_PTR(ret);
0955 }
0956
0957
0958
0959
0960
0961
0962
0963
0964
0965
0966
0967
0968
0969
0970 struct thermal_cooling_device *
0971 thermal_cooling_device_register(const char *type, void *devdata,
0972 const struct thermal_cooling_device_ops *ops)
0973 {
0974 return __thermal_cooling_device_register(NULL, type, devdata, ops);
0975 }
0976 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988
0989
0990
0991
0992
0993 struct thermal_cooling_device *
0994 thermal_of_cooling_device_register(struct device_node *np,
0995 const char *type, void *devdata,
0996 const struct thermal_cooling_device_ops *ops)
0997 {
0998 return __thermal_cooling_device_register(np, type, devdata, ops);
0999 }
1000 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1001
1002 static void thermal_cooling_device_release(struct device *dev, void *res)
1003 {
1004 thermal_cooling_device_unregister(
1005 *(struct thermal_cooling_device **)res);
1006 }
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025 struct thermal_cooling_device *
1026 devm_thermal_of_cooling_device_register(struct device *dev,
1027 struct device_node *np,
1028 char *type, void *devdata,
1029 const struct thermal_cooling_device_ops *ops)
1030 {
1031 struct thermal_cooling_device **ptr, *tcd;
1032
1033 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1034 GFP_KERNEL);
1035 if (!ptr)
1036 return ERR_PTR(-ENOMEM);
1037
1038 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1039 if (IS_ERR(tcd)) {
1040 devres_free(ptr);
1041 return tcd;
1042 }
1043
1044 *ptr = tcd;
1045 devres_add(dev, ptr);
1046
1047 return tcd;
1048 }
1049 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1050
1051 static void __unbind(struct thermal_zone_device *tz, int mask,
1052 struct thermal_cooling_device *cdev)
1053 {
1054 int i;
1055
1056 for (i = 0; i < tz->num_trips; i++)
1057 if (mask & (1 << i))
1058 thermal_zone_unbind_cooling_device(tz, i, cdev);
1059 }
1060
1061
1062
1063
1064
1065
1066
1067
1068 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1069 {
1070 int i;
1071 const struct thermal_zone_params *tzp;
1072 struct thermal_zone_device *tz;
1073 struct thermal_cooling_device *pos = NULL;
1074
1075 if (!cdev)
1076 return;
1077
1078 mutex_lock(&thermal_list_lock);
1079 list_for_each_entry(pos, &thermal_cdev_list, node)
1080 if (pos == cdev)
1081 break;
1082 if (pos != cdev) {
1083
1084 mutex_unlock(&thermal_list_lock);
1085 return;
1086 }
1087 list_del(&cdev->node);
1088
1089
1090 list_for_each_entry(tz, &thermal_tz_list, node) {
1091 if (tz->ops->unbind) {
1092 tz->ops->unbind(tz, cdev);
1093 continue;
1094 }
1095
1096 if (!tz->tzp || !tz->tzp->tbp)
1097 continue;
1098
1099 tzp = tz->tzp;
1100 for (i = 0; i < tzp->num_tbps; i++) {
1101 if (tzp->tbp[i].cdev == cdev) {
1102 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1103 tzp->tbp[i].cdev = NULL;
1104 }
1105 }
1106 }
1107
1108 mutex_unlock(&thermal_list_lock);
1109
1110 ida_free(&thermal_cdev_ida, cdev->id);
1111 device_del(&cdev->device);
1112 thermal_cooling_device_destroy_sysfs(cdev);
1113 kfree(cdev->type);
1114 put_device(&cdev->device);
1115 }
1116 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1117
1118 static void bind_tz(struct thermal_zone_device *tz)
1119 {
1120 int i, ret;
1121 struct thermal_cooling_device *pos = NULL;
1122 const struct thermal_zone_params *tzp = tz->tzp;
1123
1124 if (!tzp && !tz->ops->bind)
1125 return;
1126
1127 mutex_lock(&thermal_list_lock);
1128
1129
1130 if (tz->ops->bind) {
1131 list_for_each_entry(pos, &thermal_cdev_list, node) {
1132 ret = tz->ops->bind(tz, pos);
1133 if (ret)
1134 print_bind_err_msg(tz, pos, ret);
1135 }
1136 goto exit;
1137 }
1138
1139 if (!tzp || !tzp->tbp)
1140 goto exit;
1141
1142 list_for_each_entry(pos, &thermal_cdev_list, node) {
1143 for (i = 0; i < tzp->num_tbps; i++) {
1144 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1145 continue;
1146 if (tzp->tbp[i].match(tz, pos))
1147 continue;
1148 tzp->tbp[i].cdev = pos;
1149 __bind(tz, tzp->tbp[i].trip_mask, pos,
1150 tzp->tbp[i].binding_limits,
1151 tzp->tbp[i].weight);
1152 }
1153 }
1154 exit:
1155 mutex_unlock(&thermal_list_lock);
1156 }
1157
1158 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1159 {
1160 *delay_jiffies = msecs_to_jiffies(delay_ms);
1161 if (delay_ms > 1000)
1162 *delay_jiffies = round_jiffies(*delay_jiffies);
1163 }
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190 struct thermal_zone_device *
1191 thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask,
1192 void *devdata, struct thermal_zone_device_ops *ops,
1193 struct thermal_zone_params *tzp, int passive_delay,
1194 int polling_delay)
1195 {
1196 struct thermal_zone_device *tz;
1197 enum thermal_trip_type trip_type;
1198 int trip_temp;
1199 int id;
1200 int result;
1201 int count;
1202 struct thermal_governor *governor;
1203
1204 if (!type || strlen(type) == 0) {
1205 pr_err("No thermal zone type defined\n");
1206 return ERR_PTR(-EINVAL);
1207 }
1208
1209 if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1210 pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1211 type, THERMAL_NAME_LENGTH);
1212 return ERR_PTR(-EINVAL);
1213 }
1214
1215 if (num_trips > THERMAL_MAX_TRIPS || num_trips < 0 || mask >> num_trips) {
1216 pr_err("Incorrect number of thermal trips\n");
1217 return ERR_PTR(-EINVAL);
1218 }
1219
1220 if (!ops) {
1221 pr_err("Thermal zone device ops not defined\n");
1222 return ERR_PTR(-EINVAL);
1223 }
1224
1225 if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1226 return ERR_PTR(-EINVAL);
1227
1228 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1229 if (!tz)
1230 return ERR_PTR(-ENOMEM);
1231
1232 INIT_LIST_HEAD(&tz->thermal_instances);
1233 ida_init(&tz->ida);
1234 mutex_init(&tz->lock);
1235 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1236 if (id < 0) {
1237 result = id;
1238 goto free_tz;
1239 }
1240
1241 tz->id = id;
1242 strlcpy(tz->type, type, sizeof(tz->type));
1243
1244 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1245 if (result)
1246 goto remove_id;
1247
1248 if (!ops->critical)
1249 ops->critical = thermal_zone_device_critical;
1250
1251 tz->ops = ops;
1252 tz->tzp = tzp;
1253 tz->device.class = &thermal_class;
1254 tz->devdata = devdata;
1255 tz->trips = trips;
1256 tz->num_trips = num_trips;
1257
1258 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1259 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1260
1261
1262
1263 result = thermal_zone_create_device_groups(tz, mask);
1264 if (result)
1265 goto remove_id;
1266
1267
1268 atomic_set(&tz->need_update, 1);
1269
1270 result = device_register(&tz->device);
1271 if (result)
1272 goto release_device;
1273
1274 for (count = 0; count < num_trips; count++) {
1275 if (tz->ops->get_trip_type(tz, count, &trip_type) ||
1276 tz->ops->get_trip_temp(tz, count, &trip_temp) ||
1277 !trip_temp)
1278 set_bit(count, &tz->trips_disabled);
1279 }
1280
1281
1282 mutex_lock(&thermal_governor_lock);
1283
1284 if (tz->tzp)
1285 governor = __find_governor(tz->tzp->governor_name);
1286 else
1287 governor = def_governor;
1288
1289 result = thermal_set_governor(tz, governor);
1290 if (result) {
1291 mutex_unlock(&thermal_governor_lock);
1292 goto unregister;
1293 }
1294
1295 mutex_unlock(&thermal_governor_lock);
1296
1297 if (!tz->tzp || !tz->tzp->no_hwmon) {
1298 result = thermal_add_hwmon_sysfs(tz);
1299 if (result)
1300 goto unregister;
1301 }
1302
1303 mutex_lock(&thermal_list_lock);
1304 list_add_tail(&tz->node, &thermal_tz_list);
1305 mutex_unlock(&thermal_list_lock);
1306
1307
1308 bind_tz(tz);
1309
1310 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1311
1312 thermal_zone_device_init(tz);
1313
1314 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1315 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1316
1317 thermal_notify_tz_create(tz->id, tz->type);
1318
1319 return tz;
1320
1321 unregister:
1322 device_del(&tz->device);
1323 release_device:
1324 put_device(&tz->device);
1325 tz = NULL;
1326 remove_id:
1327 ida_free(&thermal_tz_ida, id);
1328 free_tz:
1329 kfree(tz);
1330 return ERR_PTR(result);
1331 }
1332 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1333
1334 struct thermal_zone_device *thermal_zone_device_register(const char *type, int ntrips, int mask,
1335 void *devdata, struct thermal_zone_device_ops *ops,
1336 struct thermal_zone_params *tzp, int passive_delay,
1337 int polling_delay)
1338 {
1339 return thermal_zone_device_register_with_trips(type, NULL, ntrips, mask,
1340 devdata, ops, tzp,
1341 passive_delay, polling_delay);
1342 }
1343 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1344
1345
1346
1347
1348
1349 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1350 {
1351 int i, tz_id;
1352 const struct thermal_zone_params *tzp;
1353 struct thermal_cooling_device *cdev;
1354 struct thermal_zone_device *pos = NULL;
1355
1356 if (!tz)
1357 return;
1358
1359 tzp = tz->tzp;
1360 tz_id = tz->id;
1361
1362 mutex_lock(&thermal_list_lock);
1363 list_for_each_entry(pos, &thermal_tz_list, node)
1364 if (pos == tz)
1365 break;
1366 if (pos != tz) {
1367
1368 mutex_unlock(&thermal_list_lock);
1369 return;
1370 }
1371 list_del(&tz->node);
1372
1373
1374 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1375 if (tz->ops->unbind) {
1376 tz->ops->unbind(tz, cdev);
1377 continue;
1378 }
1379
1380 if (!tzp || !tzp->tbp)
1381 break;
1382
1383 for (i = 0; i < tzp->num_tbps; i++) {
1384 if (tzp->tbp[i].cdev == cdev) {
1385 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1386 tzp->tbp[i].cdev = NULL;
1387 }
1388 }
1389 }
1390
1391 mutex_unlock(&thermal_list_lock);
1392
1393 cancel_delayed_work_sync(&tz->poll_queue);
1394
1395 thermal_set_governor(tz, NULL);
1396
1397 thermal_remove_hwmon_sysfs(tz);
1398 ida_free(&thermal_tz_ida, tz->id);
1399 ida_destroy(&tz->ida);
1400 mutex_destroy(&tz->lock);
1401 device_unregister(&tz->device);
1402
1403 thermal_notify_tz_delete(tz_id);
1404 }
1405 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1418 {
1419 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1420 unsigned int found = 0;
1421
1422 if (!name)
1423 goto exit;
1424
1425 mutex_lock(&thermal_list_lock);
1426 list_for_each_entry(pos, &thermal_tz_list, node)
1427 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1428 found++;
1429 ref = pos;
1430 }
1431 mutex_unlock(&thermal_list_lock);
1432
1433
1434 if (found == 0)
1435 ref = ERR_PTR(-ENODEV);
1436 else if (found > 1)
1437
1438 ref = ERR_PTR(-EEXIST);
1439
1440 exit:
1441 return ref;
1442 }
1443 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1444
1445 static int thermal_pm_notify(struct notifier_block *nb,
1446 unsigned long mode, void *_unused)
1447 {
1448 struct thermal_zone_device *tz;
1449
1450 switch (mode) {
1451 case PM_HIBERNATION_PREPARE:
1452 case PM_RESTORE_PREPARE:
1453 case PM_SUSPEND_PREPARE:
1454 atomic_set(&in_suspend, 1);
1455 break;
1456 case PM_POST_HIBERNATION:
1457 case PM_POST_RESTORE:
1458 case PM_POST_SUSPEND:
1459 atomic_set(&in_suspend, 0);
1460 list_for_each_entry(tz, &thermal_tz_list, node) {
1461 if (!thermal_zone_device_is_enabled(tz))
1462 continue;
1463
1464 thermal_zone_device_init(tz);
1465 thermal_zone_device_update(tz,
1466 THERMAL_EVENT_UNSPECIFIED);
1467 }
1468 break;
1469 default:
1470 break;
1471 }
1472 return 0;
1473 }
1474
1475 static struct notifier_block thermal_pm_nb = {
1476 .notifier_call = thermal_pm_notify,
1477 };
1478
1479 static int __init thermal_init(void)
1480 {
1481 int result;
1482
1483 result = thermal_netlink_init();
1484 if (result)
1485 goto error;
1486
1487 result = thermal_register_governors();
1488 if (result)
1489 goto error;
1490
1491 result = class_register(&thermal_class);
1492 if (result)
1493 goto unregister_governors;
1494
1495 result = of_parse_thermal_zones();
1496 if (result)
1497 goto unregister_class;
1498
1499 result = register_pm_notifier(&thermal_pm_nb);
1500 if (result)
1501 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1502 result);
1503
1504 return 0;
1505
1506 unregister_class:
1507 class_unregister(&thermal_class);
1508 unregister_governors:
1509 thermal_unregister_governors();
1510 error:
1511 ida_destroy(&thermal_tz_ida);
1512 ida_destroy(&thermal_cdev_ida);
1513 mutex_destroy(&thermal_list_lock);
1514 mutex_destroy(&thermal_governor_lock);
1515 return result;
1516 }
1517 postcore_initcall(thermal_init);