2 * thermal.c - Generic Thermal Management Sysfs support.
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>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/slab.h>
32 #include <linux/kdev_t.h>
33 #include <linux/idr.h>
34 #include <linux/thermal.h>
35 #include <linux/reboot.h>
36 #include <linux/string.h>
38 #include <net/netlink.h>
39 #include <net/genetlink.h>
40 #include <linux/suspend.h>
42 #define CREATE_TRACE_POINTS
43 #include <trace/events/thermal.h>
45 #include "thermal_core.h"
46 #include "thermal_hwmon.h"
48 MODULE_AUTHOR("Zhang Rui");
49 MODULE_DESCRIPTION("Generic thermal management sysfs support");
50 MODULE_LICENSE("GPL v2");
52 static DEFINE_IDR(thermal_tz_idr);
53 static DEFINE_IDR(thermal_cdev_idr);
54 static DEFINE_MUTEX(thermal_idr_lock);
56 static LIST_HEAD(thermal_tz_list);
57 static LIST_HEAD(thermal_cdev_list);
58 static LIST_HEAD(thermal_governor_list);
60 static DEFINE_MUTEX(thermal_list_lock);
61 static DEFINE_MUTEX(thermal_governor_lock);
63 static atomic_t in_suspend;
65 static struct thermal_governor *def_governor;
68 * Governor section: set of functions to handle thermal governors
70 * Functions to help in the life cycle of thermal governors within
71 * the thermal core and by the thermal governor code.
74 static struct thermal_governor *__find_governor(const char *name)
76 struct thermal_governor *pos;
78 if (!name || !name[0])
81 list_for_each_entry(pos, &thermal_governor_list, governor_list)
82 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
89 * bind_previous_governor() - bind the previous governor of the thermal zone
90 * @tz: a valid pointer to a struct thermal_zone_device
91 * @failed_gov_name: the name of the governor that failed to register
93 * Register the previous governor of the thermal zone after a new
94 * governor has failed to be bound.
96 static void bind_previous_governor(struct thermal_zone_device *tz,
97 const char *failed_gov_name)
99 if (tz->governor && tz->governor->bind_to_tz) {
100 if (tz->governor->bind_to_tz(tz)) {
102 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
103 failed_gov_name, tz->governor->name, tz->type);
110 * thermal_set_governor() - Switch to another governor
111 * @tz: a valid pointer to a struct thermal_zone_device
112 * @new_gov: pointer to the new governor
114 * Change the governor of thermal zone @tz.
116 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
118 static int thermal_set_governor(struct thermal_zone_device *tz,
119 struct thermal_governor *new_gov)
123 if (tz->governor && tz->governor->unbind_from_tz)
124 tz->governor->unbind_from_tz(tz);
126 if (new_gov && new_gov->bind_to_tz) {
127 ret = new_gov->bind_to_tz(tz);
129 bind_previous_governor(tz, new_gov->name);
135 tz->governor = new_gov;
140 int thermal_register_governor(struct thermal_governor *governor)
144 struct thermal_zone_device *pos;
149 mutex_lock(&thermal_governor_lock);
152 if (__find_governor(governor->name) == NULL) {
154 list_add(&governor->governor_list, &thermal_governor_list);
155 if (!def_governor && !strncmp(governor->name,
156 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
157 def_governor = governor;
160 mutex_lock(&thermal_list_lock);
162 list_for_each_entry(pos, &thermal_tz_list, node) {
164 * only thermal zones with specified tz->tzp->governor_name
165 * may run with tz->govenor unset
170 name = pos->tzp->governor_name;
172 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
175 ret = thermal_set_governor(pos, governor);
177 dev_err(&pos->device,
178 "Failed to set governor %s for thermal zone %s: %d\n",
179 governor->name, pos->type, ret);
183 mutex_unlock(&thermal_list_lock);
184 mutex_unlock(&thermal_governor_lock);
189 void thermal_unregister_governor(struct thermal_governor *governor)
191 struct thermal_zone_device *pos;
196 mutex_lock(&thermal_governor_lock);
198 if (__find_governor(governor->name) == NULL)
201 mutex_lock(&thermal_list_lock);
203 list_for_each_entry(pos, &thermal_tz_list, node) {
204 if (!strncasecmp(pos->governor->name, governor->name,
205 THERMAL_NAME_LENGTH))
206 thermal_set_governor(pos, NULL);
209 mutex_unlock(&thermal_list_lock);
210 list_del(&governor->governor_list);
212 mutex_unlock(&thermal_governor_lock);
216 int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
219 struct thermal_governor *gov;
222 mutex_lock(&thermal_governor_lock);
223 mutex_lock(&tz->lock);
225 gov = __find_governor(strim(policy));
229 ret = thermal_set_governor(tz, gov);
232 mutex_unlock(&tz->lock);
233 mutex_unlock(&thermal_governor_lock);
238 int thermal_build_list_of_policies(char *buf)
240 struct thermal_governor *pos;
242 ssize_t size = PAGE_SIZE;
244 mutex_lock(&thermal_governor_lock);
246 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
247 size = PAGE_SIZE - count;
248 count += scnprintf(buf + count, size, "%s ", pos->name);
250 count += scnprintf(buf + count, size, "\n");
252 mutex_unlock(&thermal_governor_lock);
257 static int __init thermal_register_governors(void)
261 result = thermal_gov_step_wise_register();
265 result = thermal_gov_fair_share_register();
269 result = thermal_gov_bang_bang_register();
273 result = thermal_gov_user_space_register();
277 return thermal_gov_power_allocator_register();
280 static void thermal_unregister_governors(void)
282 thermal_gov_step_wise_unregister();
283 thermal_gov_fair_share_unregister();
284 thermal_gov_bang_bang_unregister();
285 thermal_gov_user_space_unregister();
286 thermal_gov_power_allocator_unregister();
289 static void print_bind_err_msg(struct thermal_zone_device *tz,
290 struct thermal_cooling_device *cdev, int ret)
292 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
293 tz->type, cdev->type, ret);
296 static void __bind(struct thermal_zone_device *tz, int mask,
297 struct thermal_cooling_device *cdev,
298 unsigned long *limits,
303 for (i = 0; i < tz->trips; i++) {
304 if (mask & (1 << i)) {
305 unsigned long upper, lower;
307 upper = THERMAL_NO_LIMIT;
308 lower = THERMAL_NO_LIMIT;
310 lower = limits[i * 2];
311 upper = limits[i * 2 + 1];
313 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
317 print_bind_err_msg(tz, cdev, ret);
322 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
326 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
327 round_jiffies(msecs_to_jiffies(delay)));
329 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
330 msecs_to_jiffies(delay));
332 cancel_delayed_work(&tz->poll_queue);
335 static void monitor_thermal_zone(struct thermal_zone_device *tz)
337 mutex_lock(&tz->lock);
340 thermal_zone_device_set_polling(tz, tz->passive_delay);
341 else if (tz->polling_delay)
342 thermal_zone_device_set_polling(tz, tz->polling_delay);
344 thermal_zone_device_set_polling(tz, 0);
346 mutex_unlock(&tz->lock);
349 static void handle_non_critical_trips(struct thermal_zone_device *tz,
350 int trip, enum thermal_trip_type trip_type)
352 tz->governor ? tz->governor->throttle(tz, trip) :
353 def_governor->throttle(tz, trip);
356 static void handle_critical_trips(struct thermal_zone_device *tz,
357 int trip, enum thermal_trip_type trip_type)
361 tz->ops->get_trip_temp(tz, trip, &trip_temp);
363 /* If we have not crossed the trip_temp, we do not care. */
364 if (trip_temp <= 0 || tz->temperature < trip_temp)
367 trace_thermal_zone_trip(tz, trip, trip_type);
370 tz->ops->notify(tz, trip, trip_type);
372 if (trip_type == THERMAL_TRIP_CRITICAL) {
373 dev_emerg(&tz->device,
374 "critical temperature reached(%d C),shutting down\n",
375 tz->temperature / 1000);
376 orderly_poweroff(true);
380 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
382 enum thermal_trip_type type;
384 /* Ignore disabled trip points */
385 if (test_bit(trip, &tz->trips_disabled))
388 tz->ops->get_trip_type(tz, trip, &type);
390 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
391 handle_critical_trips(tz, trip, type);
393 handle_non_critical_trips(tz, trip, type);
395 * Alright, we handled this trip successfully.
396 * So, start monitoring again.
398 monitor_thermal_zone(tz);
401 static void update_temperature(struct thermal_zone_device *tz)
405 ret = thermal_zone_get_temp(tz, &temp);
408 dev_warn(&tz->device,
409 "failed to read out thermal zone (%d)\n",
414 mutex_lock(&tz->lock);
415 tz->last_temperature = tz->temperature;
416 tz->temperature = temp;
417 mutex_unlock(&tz->lock);
419 trace_thermal_temperature(tz);
420 if (tz->last_temperature == THERMAL_TEMP_INVALID)
421 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
424 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
425 tz->last_temperature, tz->temperature);
428 static void thermal_zone_device_reset(struct thermal_zone_device *tz)
430 struct thermal_instance *pos;
432 tz->temperature = THERMAL_TEMP_INVALID;
434 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
435 pos->initialized = false;
438 void thermal_zone_device_update(struct thermal_zone_device *tz,
439 enum thermal_notify_event event)
443 if (atomic_read(&in_suspend))
446 if (!tz->ops->get_temp)
449 update_temperature(tz);
451 thermal_zone_set_trips(tz);
453 tz->notify_event = event;
455 for (count = 0; count < tz->trips; count++)
456 handle_thermal_trip(tz, count);
458 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
460 static void thermal_zone_device_check(struct work_struct *work)
462 struct thermal_zone_device *tz = container_of(work, struct
465 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
469 * power_actor_get_max_power() - get the maximum power that a cdev can consume
470 * @cdev: pointer to &thermal_cooling_device
471 * @tz: a valid thermal zone device pointer
472 * @max_power: pointer in which to store the maximum power
474 * Calculate the maximum power consumption in milliwats that the
475 * cooling device can currently consume and store it in @max_power.
477 * Return: 0 on success, -EINVAL if @cdev doesn't support the
478 * power_actor API or -E* on other error.
480 int power_actor_get_max_power(struct thermal_cooling_device *cdev,
481 struct thermal_zone_device *tz, u32 *max_power)
483 if (!cdev_is_power_actor(cdev))
486 return cdev->ops->state2power(cdev, tz, 0, max_power);
490 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
491 * @cdev: pointer to &thermal_cooling_device
492 * @tz: a valid thermal zone device pointer
493 * @min_power: pointer in which to store the minimum power
495 * Calculate the minimum power consumption in milliwatts that the
496 * cooling device can currently consume and store it in @min_power.
498 * Return: 0 on success, -EINVAL if @cdev doesn't support the
499 * power_actor API or -E* on other error.
501 int power_actor_get_min_power(struct thermal_cooling_device *cdev,
502 struct thermal_zone_device *tz, u32 *min_power)
504 unsigned long max_state;
507 if (!cdev_is_power_actor(cdev))
510 ret = cdev->ops->get_max_state(cdev, &max_state);
514 return cdev->ops->state2power(cdev, tz, max_state, min_power);
518 * power_actor_set_power() - limit the maximum power a cooling device consumes
519 * @cdev: pointer to &thermal_cooling_device
520 * @instance: thermal instance to update
521 * @power: the power in milliwatts
523 * Set the cooling device to consume at most @power milliwatts. The limit is
524 * expected to be a cap at the maximum power consumption.
526 * Return: 0 on success, -EINVAL if the cooling device does not
527 * implement the power actor API or -E* for other failures.
529 int power_actor_set_power(struct thermal_cooling_device *cdev,
530 struct thermal_instance *instance, u32 power)
535 if (!cdev_is_power_actor(cdev))
538 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
542 instance->target = state;
543 mutex_lock(&cdev->lock);
544 cdev->updated = false;
545 mutex_unlock(&cdev->lock);
546 thermal_cdev_update(cdev);
551 void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
552 const char *cdev_type, size_t size)
554 struct thermal_cooling_device *cdev = NULL;
556 mutex_lock(&thermal_list_lock);
557 list_for_each_entry(cdev, &thermal_cdev_list, node) {
558 /* skip non matching cdevs */
559 if (strncmp(cdev_type, cdev->type, size))
562 /* re binding the exception matching the type pattern */
563 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
566 THERMAL_WEIGHT_DEFAULT);
568 mutex_unlock(&thermal_list_lock);
571 void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
572 const char *cdev_type, size_t size)
574 struct thermal_cooling_device *cdev = NULL;
576 mutex_lock(&thermal_list_lock);
577 list_for_each_entry(cdev, &thermal_cdev_list, node) {
578 /* skip non matching cdevs */
579 if (strncmp(cdev_type, cdev->type, size))
581 /* unbinding the exception matching the type pattern */
582 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
585 mutex_unlock(&thermal_list_lock);
588 /* Device management */
590 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
596 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
599 if (unlikely(ret < 0))
605 static void release_idr(struct idr *idr, struct mutex *lock, int id)
615 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
616 * @tz: pointer to struct thermal_zone_device
617 * @trip: indicates which trip point the cooling devices is
618 * associated with in this thermal zone.
619 * @cdev: pointer to struct thermal_cooling_device
620 * @upper: the Maximum cooling state for this trip point.
621 * THERMAL_NO_LIMIT means no upper limit,
622 * and the cooling device can be in max_state.
623 * @lower: the Minimum cooling state can be used for this trip point.
624 * THERMAL_NO_LIMIT means no lower limit,
625 * and the cooling device can be in cooling state 0.
626 * @weight: The weight of the cooling device to be bound to the
627 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
630 * This interface function bind a thermal cooling device to the certain trip
631 * point of a thermal zone device.
632 * This function is usually called in the thermal zone device .bind callback.
634 * Return: 0 on success, the proper error value otherwise.
636 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
638 struct thermal_cooling_device *cdev,
639 unsigned long upper, unsigned long lower,
642 struct thermal_instance *dev;
643 struct thermal_instance *pos;
644 struct thermal_zone_device *pos1;
645 struct thermal_cooling_device *pos2;
646 unsigned long max_state;
649 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
652 list_for_each_entry(pos1, &thermal_tz_list, node) {
656 list_for_each_entry(pos2, &thermal_cdev_list, node) {
661 if (tz != pos1 || cdev != pos2)
664 ret = cdev->ops->get_max_state(cdev, &max_state);
668 /* lower default 0, upper default max_state */
669 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
670 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
672 if (lower > upper || upper > max_state)
676 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
684 dev->target = THERMAL_NO_TARGET;
685 dev->weight = weight;
687 result = get_idr(&tz->idr, &tz->lock, &dev->id);
691 sprintf(dev->name, "cdev%d", dev->id);
693 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
697 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
698 sysfs_attr_init(&dev->attr.attr);
699 dev->attr.attr.name = dev->attr_name;
700 dev->attr.attr.mode = 0444;
701 dev->attr.show = thermal_cooling_device_trip_point_show;
702 result = device_create_file(&tz->device, &dev->attr);
704 goto remove_symbol_link;
706 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
707 sysfs_attr_init(&dev->weight_attr.attr);
708 dev->weight_attr.attr.name = dev->weight_attr_name;
709 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
710 dev->weight_attr.show = thermal_cooling_device_weight_show;
711 dev->weight_attr.store = thermal_cooling_device_weight_store;
712 result = device_create_file(&tz->device, &dev->weight_attr);
714 goto remove_trip_file;
716 mutex_lock(&tz->lock);
717 mutex_lock(&cdev->lock);
718 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
719 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
724 list_add_tail(&dev->tz_node, &tz->thermal_instances);
725 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
726 atomic_set(&tz->need_update, 1);
728 mutex_unlock(&cdev->lock);
729 mutex_unlock(&tz->lock);
734 device_remove_file(&tz->device, &dev->weight_attr);
736 device_remove_file(&tz->device, &dev->attr);
738 sysfs_remove_link(&tz->device.kobj, dev->name);
740 release_idr(&tz->idr, &tz->lock, dev->id);
745 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
748 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
750 * @tz: pointer to a struct thermal_zone_device.
751 * @trip: indicates which trip point the cooling devices is
752 * associated with in this thermal zone.
753 * @cdev: pointer to a struct thermal_cooling_device.
755 * This interface function unbind a thermal cooling device from the certain
756 * trip point of a thermal zone device.
757 * This function is usually called in the thermal zone device .unbind callback.
759 * Return: 0 on success, the proper error value otherwise.
761 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
763 struct thermal_cooling_device *cdev)
765 struct thermal_instance *pos, *next;
767 mutex_lock(&tz->lock);
768 mutex_lock(&cdev->lock);
769 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
770 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
771 list_del(&pos->tz_node);
772 list_del(&pos->cdev_node);
773 mutex_unlock(&cdev->lock);
774 mutex_unlock(&tz->lock);
778 mutex_unlock(&cdev->lock);
779 mutex_unlock(&tz->lock);
784 device_remove_file(&tz->device, &pos->weight_attr);
785 device_remove_file(&tz->device, &pos->attr);
786 sysfs_remove_link(&tz->device.kobj, pos->name);
787 release_idr(&tz->idr, &tz->lock, pos->id);
791 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
793 static void thermal_release(struct device *dev)
795 struct thermal_zone_device *tz;
796 struct thermal_cooling_device *cdev;
798 if (!strncmp(dev_name(dev), "thermal_zone",
799 sizeof("thermal_zone") - 1)) {
800 tz = to_thermal_zone(dev);
802 } else if(!strncmp(dev_name(dev), "cooling_device",
803 sizeof("cooling_device") - 1)){
804 cdev = to_cooling_device(dev);
809 static struct class thermal_class = {
811 .dev_release = thermal_release,
814 static void bind_cdev(struct thermal_cooling_device *cdev)
817 const struct thermal_zone_params *tzp;
818 struct thermal_zone_device *pos = NULL;
820 mutex_lock(&thermal_list_lock);
822 list_for_each_entry(pos, &thermal_tz_list, node) {
823 if (!pos->tzp && !pos->ops->bind)
826 if (pos->ops->bind) {
827 ret = pos->ops->bind(pos, cdev);
829 print_bind_err_msg(pos, cdev, ret);
834 if (!tzp || !tzp->tbp)
837 for (i = 0; i < tzp->num_tbps; i++) {
838 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
840 if (tzp->tbp[i].match(pos, cdev))
842 tzp->tbp[i].cdev = cdev;
843 __bind(pos, tzp->tbp[i].trip_mask, cdev,
844 tzp->tbp[i].binding_limits,
849 mutex_unlock(&thermal_list_lock);
853 * __thermal_cooling_device_register() - register a new thermal cooling device
854 * @np: a pointer to a device tree node.
855 * @type: the thermal cooling device type.
856 * @devdata: device private data.
857 * @ops: standard thermal cooling devices callbacks.
859 * This interface function adds a new thermal cooling device (fan/processor/...)
860 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
861 * to all the thermal zone devices registered at the same time.
862 * It also gives the opportunity to link the cooling device to a device tree
863 * node, so that it can be bound to a thermal zone created out of device tree.
865 * Return: a pointer to the created struct thermal_cooling_device or an
866 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
868 static struct thermal_cooling_device *
869 __thermal_cooling_device_register(struct device_node *np,
870 char *type, void *devdata,
871 const struct thermal_cooling_device_ops *ops)
873 struct thermal_cooling_device *cdev;
874 struct thermal_zone_device *pos = NULL;
877 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
878 return ERR_PTR(-EINVAL);
880 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
882 return ERR_PTR(-EINVAL);
884 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
886 return ERR_PTR(-ENOMEM);
888 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
891 return ERR_PTR(result);
894 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
895 mutex_init(&cdev->lock);
896 INIT_LIST_HEAD(&cdev->thermal_instances);
899 cdev->updated = false;
900 cdev->device.class = &thermal_class;
901 thermal_cooling_device_setup_sysfs(cdev);
902 cdev->devdata = devdata;
903 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
904 result = device_register(&cdev->device);
906 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
908 return ERR_PTR(result);
911 /* Add 'this' new cdev to the global cdev list */
912 mutex_lock(&thermal_list_lock);
913 list_add(&cdev->node, &thermal_cdev_list);
914 mutex_unlock(&thermal_list_lock);
916 /* Update binding information for 'this' new cdev */
919 mutex_lock(&thermal_list_lock);
920 list_for_each_entry(pos, &thermal_tz_list, node)
921 if (atomic_cmpxchg(&pos->need_update, 1, 0))
922 thermal_zone_device_update(pos,
923 THERMAL_EVENT_UNSPECIFIED);
924 mutex_unlock(&thermal_list_lock);
930 * thermal_cooling_device_register() - register a new thermal cooling device
931 * @type: the thermal cooling device type.
932 * @devdata: device private data.
933 * @ops: standard thermal cooling devices callbacks.
935 * This interface function adds a new thermal cooling device (fan/processor/...)
936 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
937 * to all the thermal zone devices registered at the same time.
939 * Return: a pointer to the created struct thermal_cooling_device or an
940 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
942 struct thermal_cooling_device *
943 thermal_cooling_device_register(char *type, void *devdata,
944 const struct thermal_cooling_device_ops *ops)
946 return __thermal_cooling_device_register(NULL, type, devdata, ops);
948 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
951 * thermal_of_cooling_device_register() - register an OF thermal cooling device
952 * @np: a pointer to a device tree node.
953 * @type: the thermal cooling device type.
954 * @devdata: device private data.
955 * @ops: standard thermal cooling devices callbacks.
957 * This function will register a cooling device with device tree node reference.
958 * This interface function adds a new thermal cooling device (fan/processor/...)
959 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
960 * to all the thermal zone devices registered at the same time.
962 * Return: a pointer to the created struct thermal_cooling_device or an
963 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
965 struct thermal_cooling_device *
966 thermal_of_cooling_device_register(struct device_node *np,
967 char *type, void *devdata,
968 const struct thermal_cooling_device_ops *ops)
970 return __thermal_cooling_device_register(np, type, devdata, ops);
972 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
974 static void __unbind(struct thermal_zone_device *tz, int mask,
975 struct thermal_cooling_device *cdev)
979 for (i = 0; i < tz->trips; i++)
981 thermal_zone_unbind_cooling_device(tz, i, cdev);
985 * thermal_cooling_device_unregister - removes the registered thermal cooling device
986 * @cdev: the thermal cooling device to remove.
988 * thermal_cooling_device_unregister() must be called when the device is no
991 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
994 const struct thermal_zone_params *tzp;
995 struct thermal_zone_device *tz;
996 struct thermal_cooling_device *pos = NULL;
1001 mutex_lock(&thermal_list_lock);
1002 list_for_each_entry(pos, &thermal_cdev_list, node)
1006 /* thermal cooling device not found */
1007 mutex_unlock(&thermal_list_lock);
1010 list_del(&cdev->node);
1012 /* Unbind all thermal zones associated with 'this' cdev */
1013 list_for_each_entry(tz, &thermal_tz_list, node) {
1014 if (tz->ops->unbind) {
1015 tz->ops->unbind(tz, cdev);
1019 if (!tz->tzp || !tz->tzp->tbp)
1023 for (i = 0; i < tzp->num_tbps; i++) {
1024 if (tzp->tbp[i].cdev == cdev) {
1025 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1026 tzp->tbp[i].cdev = NULL;
1031 mutex_unlock(&thermal_list_lock);
1033 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1034 device_unregister(&cdev->device);
1037 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1040 * thermal_notify_framework - Sensor drivers use this API to notify framework
1041 * @tz: thermal zone device
1042 * @trip: indicates which trip point has been crossed
1044 * This function handles the trip events from sensor drivers. It starts
1045 * throttling the cooling devices according to the policy configured.
1046 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1047 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1048 * The throttling policy is based on the configured platform data; if no
1049 * platform data is provided, this uses the step_wise throttling policy.
1051 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1053 handle_thermal_trip(tz, trip);
1055 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1057 static void bind_tz(struct thermal_zone_device *tz)
1060 struct thermal_cooling_device *pos = NULL;
1061 const struct thermal_zone_params *tzp = tz->tzp;
1063 if (!tzp && !tz->ops->bind)
1066 mutex_lock(&thermal_list_lock);
1068 /* If there is ops->bind, try to use ops->bind */
1069 if (tz->ops->bind) {
1070 list_for_each_entry(pos, &thermal_cdev_list, node) {
1071 ret = tz->ops->bind(tz, pos);
1073 print_bind_err_msg(tz, pos, ret);
1078 if (!tzp || !tzp->tbp)
1081 list_for_each_entry(pos, &thermal_cdev_list, node) {
1082 for (i = 0; i < tzp->num_tbps; i++) {
1083 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1085 if (tzp->tbp[i].match(tz, pos))
1087 tzp->tbp[i].cdev = pos;
1088 __bind(tz, tzp->tbp[i].trip_mask, pos,
1089 tzp->tbp[i].binding_limits,
1090 tzp->tbp[i].weight);
1094 mutex_unlock(&thermal_list_lock);
1098 * thermal_zone_device_register() - register a new thermal zone device
1099 * @type: the thermal zone device type
1100 * @trips: the number of trip points the thermal zone support
1101 * @mask: a bit string indicating the writeablility of trip points
1102 * @devdata: private device data
1103 * @ops: standard thermal zone device callbacks
1104 * @tzp: thermal zone platform parameters
1105 * @passive_delay: number of milliseconds to wait between polls when
1106 * performing passive cooling
1107 * @polling_delay: number of milliseconds to wait between polls when checking
1108 * whether trip points have been crossed (0 for interrupt
1111 * This interface function adds a new thermal zone device (sensor) to
1112 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1113 * thermal cooling devices registered at the same time.
1114 * thermal_zone_device_unregister() must be called when the device is no
1115 * longer needed. The passive cooling depends on the .get_trend() return value.
1117 * Return: a pointer to the created struct thermal_zone_device or an
1118 * in case of error, an ERR_PTR. Caller must check return value with
1119 * IS_ERR*() helpers.
1121 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1122 int trips, int mask, void *devdata,
1123 struct thermal_zone_device_ops *ops,
1124 struct thermal_zone_params *tzp,
1125 int passive_delay, int polling_delay)
1127 struct thermal_zone_device *tz;
1128 enum thermal_trip_type trip_type;
1132 struct thermal_governor *governor;
1134 if (!type || strlen(type) == 0)
1135 return ERR_PTR(-EINVAL);
1137 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1138 return ERR_PTR(-EINVAL);
1140 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1141 return ERR_PTR(-EINVAL);
1144 return ERR_PTR(-EINVAL);
1146 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1147 return ERR_PTR(-EINVAL);
1149 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1151 return ERR_PTR(-ENOMEM);
1153 INIT_LIST_HEAD(&tz->thermal_instances);
1155 mutex_init(&tz->lock);
1156 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1159 return ERR_PTR(result);
1162 strlcpy(tz->type, type, sizeof(tz->type));
1165 tz->device.class = &thermal_class;
1166 tz->devdata = devdata;
1168 tz->passive_delay = passive_delay;
1169 tz->polling_delay = polling_delay;
1172 /* Add nodes that are always present via .groups */
1173 result = thermal_zone_create_device_groups(tz, mask);
1177 /* A new thermal zone needs to be updated anyway. */
1178 atomic_set(&tz->need_update, 1);
1180 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1181 result = device_register(&tz->device);
1183 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1185 return ERR_PTR(result);
1188 for (count = 0; count < trips; count++) {
1189 if (tz->ops->get_trip_type(tz, count, &trip_type))
1190 set_bit(count, &tz->trips_disabled);
1191 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1192 set_bit(count, &tz->trips_disabled);
1193 /* Check for bogus trip points */
1195 set_bit(count, &tz->trips_disabled);
1198 /* Update 'this' zone's governor information */
1199 mutex_lock(&thermal_governor_lock);
1202 governor = __find_governor(tz->tzp->governor_name);
1204 governor = def_governor;
1206 result = thermal_set_governor(tz, governor);
1208 mutex_unlock(&thermal_governor_lock);
1212 mutex_unlock(&thermal_governor_lock);
1214 if (!tz->tzp || !tz->tzp->no_hwmon) {
1215 result = thermal_add_hwmon_sysfs(tz);
1220 mutex_lock(&thermal_list_lock);
1221 list_add_tail(&tz->node, &thermal_tz_list);
1222 mutex_unlock(&thermal_list_lock);
1224 /* Bind cooling devices for this zone */
1227 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1229 thermal_zone_device_reset(tz);
1230 /* Update the new thermal zone and mark it as already updated. */
1231 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1232 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1237 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1238 device_unregister(&tz->device);
1239 return ERR_PTR(result);
1241 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1244 * thermal_device_unregister - removes the registered thermal zone device
1245 * @tz: the thermal zone device to remove
1247 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1250 const struct thermal_zone_params *tzp;
1251 struct thermal_cooling_device *cdev;
1252 struct thermal_zone_device *pos = NULL;
1259 mutex_lock(&thermal_list_lock);
1260 list_for_each_entry(pos, &thermal_tz_list, node)
1264 /* thermal zone device not found */
1265 mutex_unlock(&thermal_list_lock);
1268 list_del(&tz->node);
1270 /* Unbind all cdevs associated with 'this' thermal zone */
1271 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1272 if (tz->ops->unbind) {
1273 tz->ops->unbind(tz, cdev);
1277 if (!tzp || !tzp->tbp)
1280 for (i = 0; i < tzp->num_tbps; i++) {
1281 if (tzp->tbp[i].cdev == cdev) {
1282 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1283 tzp->tbp[i].cdev = NULL;
1288 mutex_unlock(&thermal_list_lock);
1290 thermal_zone_device_set_polling(tz, 0);
1292 kfree(tz->trip_type_attrs);
1293 kfree(tz->trip_temp_attrs);
1294 kfree(tz->trip_hyst_attrs);
1295 kfree(tz->trips_attribute_group.attrs);
1296 thermal_set_governor(tz, NULL);
1298 thermal_remove_hwmon_sysfs(tz);
1299 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1300 idr_destroy(&tz->idr);
1301 mutex_destroy(&tz->lock);
1302 device_unregister(&tz->device);
1303 kfree(tz->device.groups);
1305 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1308 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1309 * @name: thermal zone name to fetch the temperature
1311 * When only one zone is found with the passed name, returns a reference to it.
1313 * Return: On success returns a reference to an unique thermal zone with
1314 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1315 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1317 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1319 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1320 unsigned int found = 0;
1325 mutex_lock(&thermal_list_lock);
1326 list_for_each_entry(pos, &thermal_tz_list, node)
1327 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1331 mutex_unlock(&thermal_list_lock);
1333 /* nothing has been found, thus an error code for it */
1335 ref = ERR_PTR(-ENODEV);
1337 /* Success only when an unique zone is found */
1338 ref = ERR_PTR(-EEXIST);
1343 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1346 * thermal_zone_get_slope - return the slope attribute of the thermal zone
1347 * @tz: thermal zone device with the slope attribute
1349 * Return: If the thermal zone device has a slope attribute, return it, else
1352 int thermal_zone_get_slope(struct thermal_zone_device *tz)
1355 return tz->tzp->slope;
1358 EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
1361 * thermal_zone_get_offset - return the offset attribute of the thermal zone
1362 * @tz: thermal zone device with the offset attribute
1364 * Return: If the thermal zone device has a offset attribute, return it, else
1367 int thermal_zone_get_offset(struct thermal_zone_device *tz)
1370 return tz->tzp->offset;
1373 EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
1376 static const struct genl_multicast_group thermal_event_mcgrps[] = {
1377 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1380 static struct genl_family thermal_event_genl_family = {
1381 .id = GENL_ID_GENERATE,
1382 .name = THERMAL_GENL_FAMILY_NAME,
1383 .version = THERMAL_GENL_VERSION,
1384 .maxattr = THERMAL_GENL_ATTR_MAX,
1385 .mcgrps = thermal_event_mcgrps,
1386 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
1389 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1392 struct sk_buff *skb;
1393 struct nlattr *attr;
1394 struct thermal_genl_event *thermal_event;
1398 static unsigned int thermal_event_seqnum;
1403 /* allocate memory */
1404 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1407 skb = genlmsg_new(size, GFP_ATOMIC);
1411 /* add the genetlink message header */
1412 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1413 &thermal_event_genl_family, 0,
1414 THERMAL_GENL_CMD_EVENT);
1421 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1422 sizeof(struct thermal_genl_event));
1429 thermal_event = nla_data(attr);
1430 if (!thermal_event) {
1435 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1437 thermal_event->orig = tz->id;
1438 thermal_event->event = event;
1440 /* send multicast genetlink message */
1441 genlmsg_end(skb, msg_header);
1443 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
1446 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1450 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1452 static int genetlink_init(void)
1454 return genl_register_family(&thermal_event_genl_family);
1457 static void genetlink_exit(void)
1459 genl_unregister_family(&thermal_event_genl_family);
1461 #else /* !CONFIG_NET */
1462 static inline int genetlink_init(void) { return 0; }
1463 static inline void genetlink_exit(void) {}
1464 #endif /* !CONFIG_NET */
1466 static int thermal_pm_notify(struct notifier_block *nb,
1467 unsigned long mode, void *_unused)
1469 struct thermal_zone_device *tz;
1472 case PM_HIBERNATION_PREPARE:
1473 case PM_RESTORE_PREPARE:
1474 case PM_SUSPEND_PREPARE:
1475 atomic_set(&in_suspend, 1);
1477 case PM_POST_HIBERNATION:
1478 case PM_POST_RESTORE:
1479 case PM_POST_SUSPEND:
1480 atomic_set(&in_suspend, 0);
1481 list_for_each_entry(tz, &thermal_tz_list, node) {
1482 thermal_zone_device_reset(tz);
1483 thermal_zone_device_update(tz,
1484 THERMAL_EVENT_UNSPECIFIED);
1493 static struct notifier_block thermal_pm_nb = {
1494 .notifier_call = thermal_pm_notify,
1497 static int __init thermal_init(void)
1501 result = thermal_register_governors();
1505 result = class_register(&thermal_class);
1507 goto unregister_governors;
1509 result = genetlink_init();
1511 goto unregister_class;
1513 result = of_parse_thermal_zones();
1517 result = register_pm_notifier(&thermal_pm_nb);
1519 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1527 class_unregister(&thermal_class);
1528 unregister_governors:
1529 thermal_unregister_governors();
1531 idr_destroy(&thermal_tz_idr);
1532 idr_destroy(&thermal_cdev_idr);
1533 mutex_destroy(&thermal_idr_lock);
1534 mutex_destroy(&thermal_list_lock);
1535 mutex_destroy(&thermal_governor_lock);
1539 static void __exit thermal_exit(void)
1541 unregister_pm_notifier(&thermal_pm_nb);
1542 of_thermal_destroy_zones();
1544 class_unregister(&thermal_class);
1545 thermal_unregister_governors();
1546 idr_destroy(&thermal_tz_idr);
1547 idr_destroy(&thermal_cdev_idr);
1548 mutex_destroy(&thermal_idr_lock);
1549 mutex_destroy(&thermal_list_lock);
1550 mutex_destroy(&thermal_governor_lock);
1553 fs_initcall(thermal_init);
1554 module_exit(thermal_exit);