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;
67 static struct thermal_governor *__find_governor(const char *name)
69 struct thermal_governor *pos;
71 if (!name || !name[0])
74 list_for_each_entry(pos, &thermal_governor_list, governor_list)
75 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
82 * bind_previous_governor() - bind the previous governor of the thermal zone
83 * @tz: a valid pointer to a struct thermal_zone_device
84 * @failed_gov_name: the name of the governor that failed to register
86 * Register the previous governor of the thermal zone after a new
87 * governor has failed to be bound.
89 static void bind_previous_governor(struct thermal_zone_device *tz,
90 const char *failed_gov_name)
92 if (tz->governor && tz->governor->bind_to_tz) {
93 if (tz->governor->bind_to_tz(tz)) {
95 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
96 failed_gov_name, tz->governor->name, tz->type);
103 * thermal_set_governor() - Switch to another governor
104 * @tz: a valid pointer to a struct thermal_zone_device
105 * @new_gov: pointer to the new governor
107 * Change the governor of thermal zone @tz.
109 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
111 static int thermal_set_governor(struct thermal_zone_device *tz,
112 struct thermal_governor *new_gov)
116 if (tz->governor && tz->governor->unbind_from_tz)
117 tz->governor->unbind_from_tz(tz);
119 if (new_gov && new_gov->bind_to_tz) {
120 ret = new_gov->bind_to_tz(tz);
122 bind_previous_governor(tz, new_gov->name);
128 tz->governor = new_gov;
133 int thermal_register_governor(struct thermal_governor *governor)
137 struct thermal_zone_device *pos;
142 mutex_lock(&thermal_governor_lock);
145 if (__find_governor(governor->name) == NULL) {
147 list_add(&governor->governor_list, &thermal_governor_list);
148 if (!def_governor && !strncmp(governor->name,
149 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
150 def_governor = governor;
153 mutex_lock(&thermal_list_lock);
155 list_for_each_entry(pos, &thermal_tz_list, node) {
157 * only thermal zones with specified tz->tzp->governor_name
158 * may run with tz->govenor unset
163 name = pos->tzp->governor_name;
165 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
168 ret = thermal_set_governor(pos, governor);
170 dev_err(&pos->device,
171 "Failed to set governor %s for thermal zone %s: %d\n",
172 governor->name, pos->type, ret);
176 mutex_unlock(&thermal_list_lock);
177 mutex_unlock(&thermal_governor_lock);
182 void thermal_unregister_governor(struct thermal_governor *governor)
184 struct thermal_zone_device *pos;
189 mutex_lock(&thermal_governor_lock);
191 if (__find_governor(governor->name) == NULL)
194 mutex_lock(&thermal_list_lock);
196 list_for_each_entry(pos, &thermal_tz_list, node) {
197 if (!strncasecmp(pos->governor->name, governor->name,
198 THERMAL_NAME_LENGTH))
199 thermal_set_governor(pos, NULL);
202 mutex_unlock(&thermal_list_lock);
203 list_del(&governor->governor_list);
205 mutex_unlock(&thermal_governor_lock);
209 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
215 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
218 if (unlikely(ret < 0))
224 static void release_idr(struct idr *idr, struct mutex *lock, int id)
233 int get_tz_trend(struct thermal_zone_device *tz, int trip)
235 enum thermal_trend trend;
237 if (tz->emul_temperature || !tz->ops->get_trend ||
238 tz->ops->get_trend(tz, trip, &trend)) {
239 if (tz->temperature > tz->last_temperature)
240 trend = THERMAL_TREND_RAISING;
241 else if (tz->temperature < tz->last_temperature)
242 trend = THERMAL_TREND_DROPPING;
244 trend = THERMAL_TREND_STABLE;
249 EXPORT_SYMBOL(get_tz_trend);
251 struct thermal_instance *
252 get_thermal_instance(struct thermal_zone_device *tz,
253 struct thermal_cooling_device *cdev, int trip)
255 struct thermal_instance *pos = NULL;
256 struct thermal_instance *target_instance = NULL;
258 mutex_lock(&tz->lock);
259 mutex_lock(&cdev->lock);
261 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
262 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
263 target_instance = pos;
268 mutex_unlock(&cdev->lock);
269 mutex_unlock(&tz->lock);
271 return target_instance;
273 EXPORT_SYMBOL(get_thermal_instance);
275 static void print_bind_err_msg(struct thermal_zone_device *tz,
276 struct thermal_cooling_device *cdev, int ret)
278 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
279 tz->type, cdev->type, ret);
282 static void __bind(struct thermal_zone_device *tz, int mask,
283 struct thermal_cooling_device *cdev,
284 unsigned long *limits,
289 for (i = 0; i < tz->trips; i++) {
290 if (mask & (1 << i)) {
291 unsigned long upper, lower;
293 upper = THERMAL_NO_LIMIT;
294 lower = THERMAL_NO_LIMIT;
296 lower = limits[i * 2];
297 upper = limits[i * 2 + 1];
299 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
303 print_bind_err_msg(tz, cdev, ret);
308 static void __unbind(struct thermal_zone_device *tz, int mask,
309 struct thermal_cooling_device *cdev)
313 for (i = 0; i < tz->trips; i++)
315 thermal_zone_unbind_cooling_device(tz, i, cdev);
318 static void bind_cdev(struct thermal_cooling_device *cdev)
321 const struct thermal_zone_params *tzp;
322 struct thermal_zone_device *pos = NULL;
324 mutex_lock(&thermal_list_lock);
326 list_for_each_entry(pos, &thermal_tz_list, node) {
327 if (!pos->tzp && !pos->ops->bind)
330 if (pos->ops->bind) {
331 ret = pos->ops->bind(pos, cdev);
333 print_bind_err_msg(pos, cdev, ret);
338 if (!tzp || !tzp->tbp)
341 for (i = 0; i < tzp->num_tbps; i++) {
342 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
344 if (tzp->tbp[i].match(pos, cdev))
346 tzp->tbp[i].cdev = cdev;
347 __bind(pos, tzp->tbp[i].trip_mask, cdev,
348 tzp->tbp[i].binding_limits,
353 mutex_unlock(&thermal_list_lock);
356 static void bind_tz(struct thermal_zone_device *tz)
359 struct thermal_cooling_device *pos = NULL;
360 const struct thermal_zone_params *tzp = tz->tzp;
362 if (!tzp && !tz->ops->bind)
365 mutex_lock(&thermal_list_lock);
367 /* If there is ops->bind, try to use ops->bind */
369 list_for_each_entry(pos, &thermal_cdev_list, node) {
370 ret = tz->ops->bind(tz, pos);
372 print_bind_err_msg(tz, pos, ret);
377 if (!tzp || !tzp->tbp)
380 list_for_each_entry(pos, &thermal_cdev_list, node) {
381 for (i = 0; i < tzp->num_tbps; i++) {
382 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
384 if (tzp->tbp[i].match(tz, pos))
386 tzp->tbp[i].cdev = pos;
387 __bind(tz, tzp->tbp[i].trip_mask, pos,
388 tzp->tbp[i].binding_limits,
393 mutex_unlock(&thermal_list_lock);
396 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
400 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
401 round_jiffies(msecs_to_jiffies(delay)));
403 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
404 msecs_to_jiffies(delay));
406 cancel_delayed_work(&tz->poll_queue);
409 static void monitor_thermal_zone(struct thermal_zone_device *tz)
411 mutex_lock(&tz->lock);
414 thermal_zone_device_set_polling(tz, tz->passive_delay);
415 else if (tz->polling_delay)
416 thermal_zone_device_set_polling(tz, tz->polling_delay);
418 thermal_zone_device_set_polling(tz, 0);
420 mutex_unlock(&tz->lock);
423 static void handle_non_critical_trips(struct thermal_zone_device *tz,
424 int trip, enum thermal_trip_type trip_type)
426 tz->governor ? tz->governor->throttle(tz, trip) :
427 def_governor->throttle(tz, trip);
430 static void handle_critical_trips(struct thermal_zone_device *tz,
431 int trip, enum thermal_trip_type trip_type)
435 tz->ops->get_trip_temp(tz, trip, &trip_temp);
437 /* If we have not crossed the trip_temp, we do not care. */
438 if (trip_temp <= 0 || tz->temperature < trip_temp)
441 trace_thermal_zone_trip(tz, trip, trip_type);
444 tz->ops->notify(tz, trip, trip_type);
446 if (trip_type == THERMAL_TRIP_CRITICAL) {
447 dev_emerg(&tz->device,
448 "critical temperature reached(%d C),shutting down\n",
449 tz->temperature / 1000);
450 orderly_poweroff(true);
454 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
456 enum thermal_trip_type type;
458 /* Ignore disabled trip points */
459 if (test_bit(trip, &tz->trips_disabled))
462 tz->ops->get_trip_type(tz, trip, &type);
464 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
465 handle_critical_trips(tz, trip, type);
467 handle_non_critical_trips(tz, trip, type);
469 * Alright, we handled this trip successfully.
470 * So, start monitoring again.
472 monitor_thermal_zone(tz);
476 * thermal_zone_get_temp() - returns the temperature of a thermal zone
477 * @tz: a valid pointer to a struct thermal_zone_device
478 * @temp: a valid pointer to where to store the resulting temperature.
480 * When a valid thermal zone reference is passed, it will fetch its
481 * temperature and fill @temp.
483 * Return: On success returns 0, an error code otherwise
485 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
489 int crit_temp = INT_MAX;
490 enum thermal_trip_type type;
492 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
495 mutex_lock(&tz->lock);
497 ret = tz->ops->get_temp(tz, temp);
499 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
500 for (count = 0; count < tz->trips; count++) {
501 ret = tz->ops->get_trip_type(tz, count, &type);
502 if (!ret && type == THERMAL_TRIP_CRITICAL) {
503 ret = tz->ops->get_trip_temp(tz, count,
510 * Only allow emulating a temperature when the real temperature
511 * is below the critical temperature so that the emulation code
512 * cannot hide critical conditions.
514 if (!ret && *temp < crit_temp)
515 *temp = tz->emul_temperature;
518 mutex_unlock(&tz->lock);
522 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
524 void thermal_zone_set_trips(struct thermal_zone_device *tz)
528 int trip_temp, hysteresis;
531 mutex_lock(&tz->lock);
533 if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
536 for (i = 0; i < tz->trips; i++) {
539 tz->ops->get_trip_temp(tz, i, &trip_temp);
540 tz->ops->get_trip_hyst(tz, i, &hysteresis);
542 trip_low = trip_temp - hysteresis;
544 if (trip_low < tz->temperature && trip_low > low)
547 if (trip_temp > tz->temperature && trip_temp < high)
551 /* No need to change trip points */
552 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
555 tz->prev_low_trip = low;
556 tz->prev_high_trip = high;
559 "new temperature boundaries: %d < x < %d\n", low, high);
562 * Set a temperature window. When this window is left the driver
563 * must inform the thermal core via thermal_zone_device_update.
565 ret = tz->ops->set_trips(tz, low, high);
567 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
570 mutex_unlock(&tz->lock);
572 EXPORT_SYMBOL_GPL(thermal_zone_set_trips);
574 static void update_temperature(struct thermal_zone_device *tz)
578 ret = thermal_zone_get_temp(tz, &temp);
581 dev_warn(&tz->device,
582 "failed to read out thermal zone (%d)\n",
587 mutex_lock(&tz->lock);
588 tz->last_temperature = tz->temperature;
589 tz->temperature = temp;
590 mutex_unlock(&tz->lock);
592 trace_thermal_temperature(tz);
593 if (tz->last_temperature == THERMAL_TEMP_INVALID)
594 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
597 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
598 tz->last_temperature, tz->temperature);
601 static void thermal_zone_device_reset(struct thermal_zone_device *tz)
603 struct thermal_instance *pos;
605 tz->temperature = THERMAL_TEMP_INVALID;
607 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
608 pos->initialized = false;
611 void thermal_zone_device_update(struct thermal_zone_device *tz,
612 enum thermal_notify_event event)
616 if (atomic_read(&in_suspend))
619 if (!tz->ops->get_temp)
622 update_temperature(tz);
624 thermal_zone_set_trips(tz);
626 tz->notify_event = event;
628 for (count = 0; count < tz->trips; count++)
629 handle_thermal_trip(tz, count);
631 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
633 static void thermal_zone_device_check(struct work_struct *work)
635 struct thermal_zone_device *tz = container_of(work, struct
638 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
642 * power_actor_get_max_power() - get the maximum power that a cdev can consume
643 * @cdev: pointer to &thermal_cooling_device
644 * @tz: a valid thermal zone device pointer
645 * @max_power: pointer in which to store the maximum power
647 * Calculate the maximum power consumption in milliwats that the
648 * cooling device can currently consume and store it in @max_power.
650 * Return: 0 on success, -EINVAL if @cdev doesn't support the
651 * power_actor API or -E* on other error.
653 int power_actor_get_max_power(struct thermal_cooling_device *cdev,
654 struct thermal_zone_device *tz, u32 *max_power)
656 if (!cdev_is_power_actor(cdev))
659 return cdev->ops->state2power(cdev, tz, 0, max_power);
663 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
664 * @cdev: pointer to &thermal_cooling_device
665 * @tz: a valid thermal zone device pointer
666 * @min_power: pointer in which to store the minimum power
668 * Calculate the minimum power consumption in milliwatts that the
669 * cooling device can currently consume and store it in @min_power.
671 * Return: 0 on success, -EINVAL if @cdev doesn't support the
672 * power_actor API or -E* on other error.
674 int power_actor_get_min_power(struct thermal_cooling_device *cdev,
675 struct thermal_zone_device *tz, u32 *min_power)
677 unsigned long max_state;
680 if (!cdev_is_power_actor(cdev))
683 ret = cdev->ops->get_max_state(cdev, &max_state);
687 return cdev->ops->state2power(cdev, tz, max_state, min_power);
691 * power_actor_set_power() - limit the maximum power a cooling device consumes
692 * @cdev: pointer to &thermal_cooling_device
693 * @instance: thermal instance to update
694 * @power: the power in milliwatts
696 * Set the cooling device to consume at most @power milliwatts. The limit is
697 * expected to be a cap at the maximum power consumption.
699 * Return: 0 on success, -EINVAL if the cooling device does not
700 * implement the power actor API or -E* for other failures.
702 int power_actor_set_power(struct thermal_cooling_device *cdev,
703 struct thermal_instance *instance, u32 power)
708 if (!cdev_is_power_actor(cdev))
711 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
715 instance->target = state;
716 mutex_lock(&cdev->lock);
717 cdev->updated = false;
718 mutex_unlock(&cdev->lock);
719 thermal_cdev_update(cdev);
724 void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
725 const char *cdev_type, size_t size)
727 struct thermal_cooling_device *cdev = NULL;
729 mutex_lock(&thermal_list_lock);
730 list_for_each_entry(cdev, &thermal_cdev_list, node) {
731 /* skip non matching cdevs */
732 if (strncmp(cdev_type, cdev->type, size))
735 /* re binding the exception matching the type pattern */
736 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
739 THERMAL_WEIGHT_DEFAULT);
741 mutex_unlock(&thermal_list_lock);
744 void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
745 const char *cdev_type, size_t size)
747 struct thermal_cooling_device *cdev = NULL;
749 mutex_lock(&thermal_list_lock);
750 list_for_each_entry(cdev, &thermal_cdev_list, node) {
751 /* skip non matching cdevs */
752 if (strncmp(cdev_type, cdev->type, size))
754 /* unbinding the exception matching the type pattern */
755 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
758 mutex_unlock(&thermal_list_lock);
761 int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
764 struct thermal_governor *gov;
767 mutex_lock(&thermal_governor_lock);
768 mutex_lock(&tz->lock);
770 gov = __find_governor(strim(policy));
774 ret = thermal_set_governor(tz, gov);
777 mutex_unlock(&tz->lock);
778 mutex_unlock(&thermal_governor_lock);
783 int thermal_build_list_of_policies(char *buf)
785 struct thermal_governor *pos;
787 ssize_t size = PAGE_SIZE;
789 mutex_lock(&thermal_governor_lock);
791 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
792 size = PAGE_SIZE - count;
793 count += scnprintf(buf + count, size, "%s ", pos->name);
795 count += scnprintf(buf + count, size, "\n");
797 mutex_unlock(&thermal_governor_lock);
802 /* Device management */
805 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
806 * @tz: pointer to struct thermal_zone_device
807 * @trip: indicates which trip point the cooling devices is
808 * associated with in this thermal zone.
809 * @cdev: pointer to struct thermal_cooling_device
810 * @upper: the Maximum cooling state for this trip point.
811 * THERMAL_NO_LIMIT means no upper limit,
812 * and the cooling device can be in max_state.
813 * @lower: the Minimum cooling state can be used for this trip point.
814 * THERMAL_NO_LIMIT means no lower limit,
815 * and the cooling device can be in cooling state 0.
816 * @weight: The weight of the cooling device to be bound to the
817 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
820 * This interface function bind a thermal cooling device to the certain trip
821 * point of a thermal zone device.
822 * This function is usually called in the thermal zone device .bind callback.
824 * Return: 0 on success, the proper error value otherwise.
826 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
828 struct thermal_cooling_device *cdev,
829 unsigned long upper, unsigned long lower,
832 struct thermal_instance *dev;
833 struct thermal_instance *pos;
834 struct thermal_zone_device *pos1;
835 struct thermal_cooling_device *pos2;
836 unsigned long max_state;
839 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
842 list_for_each_entry(pos1, &thermal_tz_list, node) {
846 list_for_each_entry(pos2, &thermal_cdev_list, node) {
851 if (tz != pos1 || cdev != pos2)
854 ret = cdev->ops->get_max_state(cdev, &max_state);
858 /* lower default 0, upper default max_state */
859 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
860 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
862 if (lower > upper || upper > max_state)
866 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
874 dev->target = THERMAL_NO_TARGET;
875 dev->weight = weight;
877 result = get_idr(&tz->idr, &tz->lock, &dev->id);
881 sprintf(dev->name, "cdev%d", dev->id);
883 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
887 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
888 sysfs_attr_init(&dev->attr.attr);
889 dev->attr.attr.name = dev->attr_name;
890 dev->attr.attr.mode = 0444;
891 dev->attr.show = thermal_cooling_device_trip_point_show;
892 result = device_create_file(&tz->device, &dev->attr);
894 goto remove_symbol_link;
896 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
897 sysfs_attr_init(&dev->weight_attr.attr);
898 dev->weight_attr.attr.name = dev->weight_attr_name;
899 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
900 dev->weight_attr.show = thermal_cooling_device_weight_show;
901 dev->weight_attr.store = thermal_cooling_device_weight_store;
902 result = device_create_file(&tz->device, &dev->weight_attr);
904 goto remove_trip_file;
906 mutex_lock(&tz->lock);
907 mutex_lock(&cdev->lock);
908 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
909 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
914 list_add_tail(&dev->tz_node, &tz->thermal_instances);
915 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
916 atomic_set(&tz->need_update, 1);
918 mutex_unlock(&cdev->lock);
919 mutex_unlock(&tz->lock);
924 device_remove_file(&tz->device, &dev->weight_attr);
926 device_remove_file(&tz->device, &dev->attr);
928 sysfs_remove_link(&tz->device.kobj, dev->name);
930 release_idr(&tz->idr, &tz->lock, dev->id);
935 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
938 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
940 * @tz: pointer to a struct thermal_zone_device.
941 * @trip: indicates which trip point the cooling devices is
942 * associated with in this thermal zone.
943 * @cdev: pointer to a struct thermal_cooling_device.
945 * This interface function unbind a thermal cooling device from the certain
946 * trip point of a thermal zone device.
947 * This function is usually called in the thermal zone device .unbind callback.
949 * Return: 0 on success, the proper error value otherwise.
951 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
953 struct thermal_cooling_device *cdev)
955 struct thermal_instance *pos, *next;
957 mutex_lock(&tz->lock);
958 mutex_lock(&cdev->lock);
959 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
960 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
961 list_del(&pos->tz_node);
962 list_del(&pos->cdev_node);
963 mutex_unlock(&cdev->lock);
964 mutex_unlock(&tz->lock);
968 mutex_unlock(&cdev->lock);
969 mutex_unlock(&tz->lock);
974 device_remove_file(&tz->device, &pos->weight_attr);
975 device_remove_file(&tz->device, &pos->attr);
976 sysfs_remove_link(&tz->device.kobj, pos->name);
977 release_idr(&tz->idr, &tz->lock, pos->id);
981 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
983 static void thermal_release(struct device *dev)
985 struct thermal_zone_device *tz;
986 struct thermal_cooling_device *cdev;
988 if (!strncmp(dev_name(dev), "thermal_zone",
989 sizeof("thermal_zone") - 1)) {
990 tz = to_thermal_zone(dev);
992 } else if(!strncmp(dev_name(dev), "cooling_device",
993 sizeof("cooling_device") - 1)){
994 cdev = to_cooling_device(dev);
999 static struct class thermal_class = {
1001 .dev_release = thermal_release,
1005 * __thermal_cooling_device_register() - register a new thermal cooling device
1006 * @np: a pointer to a device tree node.
1007 * @type: the thermal cooling device type.
1008 * @devdata: device private data.
1009 * @ops: standard thermal cooling devices callbacks.
1011 * This interface function adds a new thermal cooling device (fan/processor/...)
1012 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1013 * to all the thermal zone devices registered at the same time.
1014 * It also gives the opportunity to link the cooling device to a device tree
1015 * node, so that it can be bound to a thermal zone created out of device tree.
1017 * Return: a pointer to the created struct thermal_cooling_device or an
1018 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1020 static struct thermal_cooling_device *
1021 __thermal_cooling_device_register(struct device_node *np,
1022 char *type, void *devdata,
1023 const struct thermal_cooling_device_ops *ops)
1025 struct thermal_cooling_device *cdev;
1026 struct thermal_zone_device *pos = NULL;
1029 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1030 return ERR_PTR(-EINVAL);
1032 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1033 !ops->set_cur_state)
1034 return ERR_PTR(-EINVAL);
1036 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1038 return ERR_PTR(-ENOMEM);
1040 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1043 return ERR_PTR(result);
1046 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1047 mutex_init(&cdev->lock);
1048 INIT_LIST_HEAD(&cdev->thermal_instances);
1051 cdev->updated = false;
1052 cdev->device.class = &thermal_class;
1053 thermal_cooling_device_setup_sysfs(cdev);
1054 cdev->devdata = devdata;
1055 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1056 result = device_register(&cdev->device);
1058 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1060 return ERR_PTR(result);
1063 /* Add 'this' new cdev to the global cdev list */
1064 mutex_lock(&thermal_list_lock);
1065 list_add(&cdev->node, &thermal_cdev_list);
1066 mutex_unlock(&thermal_list_lock);
1068 /* Update binding information for 'this' new cdev */
1071 mutex_lock(&thermal_list_lock);
1072 list_for_each_entry(pos, &thermal_tz_list, node)
1073 if (atomic_cmpxchg(&pos->need_update, 1, 0))
1074 thermal_zone_device_update(pos,
1075 THERMAL_EVENT_UNSPECIFIED);
1076 mutex_unlock(&thermal_list_lock);
1082 * thermal_cooling_device_register() - register a new thermal cooling device
1083 * @type: the thermal cooling device type.
1084 * @devdata: device private data.
1085 * @ops: standard thermal cooling devices callbacks.
1087 * This interface function adds a new thermal cooling device (fan/processor/...)
1088 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1089 * to all the thermal zone devices registered at the same time.
1091 * Return: a pointer to the created struct thermal_cooling_device or an
1092 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1094 struct thermal_cooling_device *
1095 thermal_cooling_device_register(char *type, void *devdata,
1096 const struct thermal_cooling_device_ops *ops)
1098 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1100 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1103 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1104 * @np: a pointer to a device tree node.
1105 * @type: the thermal cooling device type.
1106 * @devdata: device private data.
1107 * @ops: standard thermal cooling devices callbacks.
1109 * This function will register a cooling device with device tree node reference.
1110 * This interface function adds a new thermal cooling device (fan/processor/...)
1111 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1112 * to all the thermal zone devices registered at the same time.
1114 * Return: a pointer to the created struct thermal_cooling_device or an
1115 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1117 struct thermal_cooling_device *
1118 thermal_of_cooling_device_register(struct device_node *np,
1119 char *type, void *devdata,
1120 const struct thermal_cooling_device_ops *ops)
1122 return __thermal_cooling_device_register(np, type, devdata, ops);
1124 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1127 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1128 * @cdev: the thermal cooling device to remove.
1130 * thermal_cooling_device_unregister() must be called when the device is no
1133 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1136 const struct thermal_zone_params *tzp;
1137 struct thermal_zone_device *tz;
1138 struct thermal_cooling_device *pos = NULL;
1143 mutex_lock(&thermal_list_lock);
1144 list_for_each_entry(pos, &thermal_cdev_list, node)
1148 /* thermal cooling device not found */
1149 mutex_unlock(&thermal_list_lock);
1152 list_del(&cdev->node);
1154 /* Unbind all thermal zones associated with 'this' cdev */
1155 list_for_each_entry(tz, &thermal_tz_list, node) {
1156 if (tz->ops->unbind) {
1157 tz->ops->unbind(tz, cdev);
1161 if (!tz->tzp || !tz->tzp->tbp)
1165 for (i = 0; i < tzp->num_tbps; i++) {
1166 if (tzp->tbp[i].cdev == cdev) {
1167 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1168 tzp->tbp[i].cdev = NULL;
1173 mutex_unlock(&thermal_list_lock);
1175 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1176 device_unregister(&cdev->device);
1179 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1181 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1183 struct thermal_instance *instance;
1184 unsigned long target = 0;
1186 mutex_lock(&cdev->lock);
1187 /* cooling device is updated*/
1188 if (cdev->updated) {
1189 mutex_unlock(&cdev->lock);
1193 /* Make sure cdev enters the deepest cooling state */
1194 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1195 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1196 instance->tz->id, instance->target);
1197 if (instance->target == THERMAL_NO_TARGET)
1199 if (instance->target > target)
1200 target = instance->target;
1202 cdev->ops->set_cur_state(cdev, target);
1203 cdev->updated = true;
1204 mutex_unlock(&cdev->lock);
1205 trace_cdev_update(cdev, target);
1206 dev_dbg(&cdev->device, "set to state %lu\n", target);
1208 EXPORT_SYMBOL(thermal_cdev_update);
1211 * thermal_notify_framework - Sensor drivers use this API to notify framework
1212 * @tz: thermal zone device
1213 * @trip: indicates which trip point has been crossed
1215 * This function handles the trip events from sensor drivers. It starts
1216 * throttling the cooling devices according to the policy configured.
1217 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1218 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1219 * The throttling policy is based on the configured platform data; if no
1220 * platform data is provided, this uses the step_wise throttling policy.
1222 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1224 handle_thermal_trip(tz, trip);
1226 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1229 * thermal_zone_device_register() - register a new thermal zone device
1230 * @type: the thermal zone device type
1231 * @trips: the number of trip points the thermal zone support
1232 * @mask: a bit string indicating the writeablility of trip points
1233 * @devdata: private device data
1234 * @ops: standard thermal zone device callbacks
1235 * @tzp: thermal zone platform parameters
1236 * @passive_delay: number of milliseconds to wait between polls when
1237 * performing passive cooling
1238 * @polling_delay: number of milliseconds to wait between polls when checking
1239 * whether trip points have been crossed (0 for interrupt
1242 * This interface function adds a new thermal zone device (sensor) to
1243 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1244 * thermal cooling devices registered at the same time.
1245 * thermal_zone_device_unregister() must be called when the device is no
1246 * longer needed. The passive cooling depends on the .get_trend() return value.
1248 * Return: a pointer to the created struct thermal_zone_device or an
1249 * in case of error, an ERR_PTR. Caller must check return value with
1250 * IS_ERR*() helpers.
1252 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1253 int trips, int mask, void *devdata,
1254 struct thermal_zone_device_ops *ops,
1255 struct thermal_zone_params *tzp,
1256 int passive_delay, int polling_delay)
1258 struct thermal_zone_device *tz;
1259 enum thermal_trip_type trip_type;
1263 struct thermal_governor *governor;
1265 if (!type || strlen(type) == 0)
1266 return ERR_PTR(-EINVAL);
1268 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1269 return ERR_PTR(-EINVAL);
1271 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1272 return ERR_PTR(-EINVAL);
1275 return ERR_PTR(-EINVAL);
1277 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1278 return ERR_PTR(-EINVAL);
1280 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1282 return ERR_PTR(-ENOMEM);
1284 INIT_LIST_HEAD(&tz->thermal_instances);
1286 mutex_init(&tz->lock);
1287 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1290 return ERR_PTR(result);
1293 strlcpy(tz->type, type, sizeof(tz->type));
1296 tz->device.class = &thermal_class;
1297 tz->devdata = devdata;
1299 tz->passive_delay = passive_delay;
1300 tz->polling_delay = polling_delay;
1303 /* Add nodes that are always present via .groups */
1304 result = thermal_zone_create_device_groups(tz, mask);
1308 /* A new thermal zone needs to be updated anyway. */
1309 atomic_set(&tz->need_update, 1);
1311 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1312 result = device_register(&tz->device);
1314 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1316 return ERR_PTR(result);
1319 for (count = 0; count < trips; count++) {
1320 if (tz->ops->get_trip_type(tz, count, &trip_type))
1321 set_bit(count, &tz->trips_disabled);
1322 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1323 set_bit(count, &tz->trips_disabled);
1324 /* Check for bogus trip points */
1326 set_bit(count, &tz->trips_disabled);
1329 /* Update 'this' zone's governor information */
1330 mutex_lock(&thermal_governor_lock);
1333 governor = __find_governor(tz->tzp->governor_name);
1335 governor = def_governor;
1337 result = thermal_set_governor(tz, governor);
1339 mutex_unlock(&thermal_governor_lock);
1343 mutex_unlock(&thermal_governor_lock);
1345 if (!tz->tzp || !tz->tzp->no_hwmon) {
1346 result = thermal_add_hwmon_sysfs(tz);
1351 mutex_lock(&thermal_list_lock);
1352 list_add_tail(&tz->node, &thermal_tz_list);
1353 mutex_unlock(&thermal_list_lock);
1355 /* Bind cooling devices for this zone */
1358 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1360 thermal_zone_device_reset(tz);
1361 /* Update the new thermal zone and mark it as already updated. */
1362 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1363 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1368 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1369 device_unregister(&tz->device);
1370 return ERR_PTR(result);
1372 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1375 * thermal_device_unregister - removes the registered thermal zone device
1376 * @tz: the thermal zone device to remove
1378 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1381 const struct thermal_zone_params *tzp;
1382 struct thermal_cooling_device *cdev;
1383 struct thermal_zone_device *pos = NULL;
1390 mutex_lock(&thermal_list_lock);
1391 list_for_each_entry(pos, &thermal_tz_list, node)
1395 /* thermal zone device not found */
1396 mutex_unlock(&thermal_list_lock);
1399 list_del(&tz->node);
1401 /* Unbind all cdevs associated with 'this' thermal zone */
1402 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1403 if (tz->ops->unbind) {
1404 tz->ops->unbind(tz, cdev);
1408 if (!tzp || !tzp->tbp)
1411 for (i = 0; i < tzp->num_tbps; i++) {
1412 if (tzp->tbp[i].cdev == cdev) {
1413 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1414 tzp->tbp[i].cdev = NULL;
1419 mutex_unlock(&thermal_list_lock);
1421 thermal_zone_device_set_polling(tz, 0);
1423 kfree(tz->trip_type_attrs);
1424 kfree(tz->trip_temp_attrs);
1425 kfree(tz->trip_hyst_attrs);
1426 kfree(tz->trips_attribute_group.attrs);
1427 thermal_set_governor(tz, NULL);
1429 thermal_remove_hwmon_sysfs(tz);
1430 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1431 idr_destroy(&tz->idr);
1432 mutex_destroy(&tz->lock);
1433 device_unregister(&tz->device);
1434 kfree(tz->device.groups);
1436 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1439 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1440 * @name: thermal zone name to fetch the temperature
1442 * When only one zone is found with the passed name, returns a reference to it.
1444 * Return: On success returns a reference to an unique thermal zone with
1445 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1446 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1448 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1450 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1451 unsigned int found = 0;
1456 mutex_lock(&thermal_list_lock);
1457 list_for_each_entry(pos, &thermal_tz_list, node)
1458 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1462 mutex_unlock(&thermal_list_lock);
1464 /* nothing has been found, thus an error code for it */
1466 ref = ERR_PTR(-ENODEV);
1468 /* Success only when an unique zone is found */
1469 ref = ERR_PTR(-EEXIST);
1474 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1477 * thermal_zone_get_slope - return the slope attribute of the thermal zone
1478 * @tz: thermal zone device with the slope attribute
1480 * Return: If the thermal zone device has a slope attribute, return it, else
1483 int thermal_zone_get_slope(struct thermal_zone_device *tz)
1486 return tz->tzp->slope;
1489 EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
1492 * thermal_zone_get_offset - return the offset attribute of the thermal zone
1493 * @tz: thermal zone device with the offset attribute
1495 * Return: If the thermal zone device has a offset attribute, return it, else
1498 int thermal_zone_get_offset(struct thermal_zone_device *tz)
1501 return tz->tzp->offset;
1504 EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
1507 static const struct genl_multicast_group thermal_event_mcgrps[] = {
1508 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1511 static struct genl_family thermal_event_genl_family = {
1512 .id = GENL_ID_GENERATE,
1513 .name = THERMAL_GENL_FAMILY_NAME,
1514 .version = THERMAL_GENL_VERSION,
1515 .maxattr = THERMAL_GENL_ATTR_MAX,
1516 .mcgrps = thermal_event_mcgrps,
1517 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
1520 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1523 struct sk_buff *skb;
1524 struct nlattr *attr;
1525 struct thermal_genl_event *thermal_event;
1529 static unsigned int thermal_event_seqnum;
1534 /* allocate memory */
1535 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1538 skb = genlmsg_new(size, GFP_ATOMIC);
1542 /* add the genetlink message header */
1543 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1544 &thermal_event_genl_family, 0,
1545 THERMAL_GENL_CMD_EVENT);
1552 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1553 sizeof(struct thermal_genl_event));
1560 thermal_event = nla_data(attr);
1561 if (!thermal_event) {
1566 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1568 thermal_event->orig = tz->id;
1569 thermal_event->event = event;
1571 /* send multicast genetlink message */
1572 genlmsg_end(skb, msg_header);
1574 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
1577 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1581 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1583 static int genetlink_init(void)
1585 return genl_register_family(&thermal_event_genl_family);
1588 static void genetlink_exit(void)
1590 genl_unregister_family(&thermal_event_genl_family);
1592 #else /* !CONFIG_NET */
1593 static inline int genetlink_init(void) { return 0; }
1594 static inline void genetlink_exit(void) {}
1595 #endif /* !CONFIG_NET */
1597 static int __init thermal_register_governors(void)
1601 result = thermal_gov_step_wise_register();
1605 result = thermal_gov_fair_share_register();
1609 result = thermal_gov_bang_bang_register();
1613 result = thermal_gov_user_space_register();
1617 return thermal_gov_power_allocator_register();
1620 static void thermal_unregister_governors(void)
1622 thermal_gov_step_wise_unregister();
1623 thermal_gov_fair_share_unregister();
1624 thermal_gov_bang_bang_unregister();
1625 thermal_gov_user_space_unregister();
1626 thermal_gov_power_allocator_unregister();
1629 static int thermal_pm_notify(struct notifier_block *nb,
1630 unsigned long mode, void *_unused)
1632 struct thermal_zone_device *tz;
1635 case PM_HIBERNATION_PREPARE:
1636 case PM_RESTORE_PREPARE:
1637 case PM_SUSPEND_PREPARE:
1638 atomic_set(&in_suspend, 1);
1640 case PM_POST_HIBERNATION:
1641 case PM_POST_RESTORE:
1642 case PM_POST_SUSPEND:
1643 atomic_set(&in_suspend, 0);
1644 list_for_each_entry(tz, &thermal_tz_list, node) {
1645 thermal_zone_device_reset(tz);
1646 thermal_zone_device_update(tz,
1647 THERMAL_EVENT_UNSPECIFIED);
1656 static struct notifier_block thermal_pm_nb = {
1657 .notifier_call = thermal_pm_notify,
1660 static int __init thermal_init(void)
1664 result = thermal_register_governors();
1668 result = class_register(&thermal_class);
1670 goto unregister_governors;
1672 result = genetlink_init();
1674 goto unregister_class;
1676 result = of_parse_thermal_zones();
1680 result = register_pm_notifier(&thermal_pm_nb);
1682 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1690 class_unregister(&thermal_class);
1691 unregister_governors:
1692 thermal_unregister_governors();
1694 idr_destroy(&thermal_tz_idr);
1695 idr_destroy(&thermal_cdev_idr);
1696 mutex_destroy(&thermal_idr_lock);
1697 mutex_destroy(&thermal_list_lock);
1698 mutex_destroy(&thermal_governor_lock);
1702 static void __exit thermal_exit(void)
1704 unregister_pm_notifier(&thermal_pm_nb);
1705 of_thermal_destroy_zones();
1707 class_unregister(&thermal_class);
1708 thermal_unregister_governors();
1709 idr_destroy(&thermal_tz_idr);
1710 idr_destroy(&thermal_cdev_idr);
1711 mutex_destroy(&thermal_idr_lock);
1712 mutex_destroy(&thermal_list_lock);
1713 mutex_destroy(&thermal_governor_lock);
1716 fs_initcall(thermal_init);
1717 module_exit(thermal_exit);