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>
7 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #include <linux/module.h>
30 #include <linux/device.h>
31 #include <linux/err.h>
32 #include <linux/slab.h>
33 #include <linux/kdev_t.h>
34 #include <linux/idr.h>
35 #include <linux/thermal.h>
36 #include <linux/reboot.h>
37 #include <linux/string.h>
39 #include <net/netlink.h>
40 #include <net/genetlink.h>
41 #include <linux/suspend.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/thermal.h>
46 #include "thermal_core.h"
47 #include "thermal_hwmon.h"
49 MODULE_AUTHOR("Zhang Rui");
50 MODULE_DESCRIPTION("Generic thermal management sysfs support");
51 MODULE_LICENSE("GPL v2");
53 static DEFINE_IDR(thermal_tz_idr);
54 static DEFINE_IDR(thermal_cdev_idr);
55 static DEFINE_MUTEX(thermal_idr_lock);
57 static LIST_HEAD(thermal_tz_list);
58 static LIST_HEAD(thermal_cdev_list);
59 static LIST_HEAD(thermal_governor_list);
61 static DEFINE_MUTEX(thermal_list_lock);
62 static DEFINE_MUTEX(thermal_governor_lock);
64 static atomic_t in_suspend;
66 static struct thermal_governor *def_governor;
68 struct thermal_governor *thermal_find_governor(const char *name)
70 struct thermal_governor *pos;
72 if (!name || !name[0])
75 list_for_each_entry(pos, &thermal_governor_list, governor_list)
76 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
83 * bind_previous_governor() - bind the previous governor of the thermal zone
84 * @tz: a valid pointer to a struct thermal_zone_device
85 * @failed_gov_name: the name of the governor that failed to register
87 * Register the previous governor of the thermal zone after a new
88 * governor has failed to be bound.
90 static void bind_previous_governor(struct thermal_zone_device *tz,
91 const char *failed_gov_name)
93 if (tz->governor && tz->governor->bind_to_tz) {
94 if (tz->governor->bind_to_tz(tz)) {
96 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
97 failed_gov_name, tz->governor->name, tz->type);
104 * thermal_set_governor() - Switch to another governor
105 * @tz: a valid pointer to a struct thermal_zone_device
106 * @new_gov: pointer to the new governor
108 * Change the governor of thermal zone @tz.
110 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
112 static int thermal_set_governor(struct thermal_zone_device *tz,
113 struct thermal_governor *new_gov)
117 if (tz->governor && tz->governor->unbind_from_tz)
118 tz->governor->unbind_from_tz(tz);
120 if (new_gov && new_gov->bind_to_tz) {
121 ret = new_gov->bind_to_tz(tz);
123 bind_previous_governor(tz, new_gov->name);
129 tz->governor = new_gov;
134 int thermal_register_governor(struct thermal_governor *governor)
138 struct thermal_zone_device *pos;
143 mutex_lock(&thermal_governor_lock);
146 if (thermal_find_governor(governor->name) == NULL) {
148 list_add(&governor->governor_list, &thermal_governor_list);
149 if (!def_governor && !strncmp(governor->name,
150 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
151 def_governor = governor;
154 mutex_lock(&thermal_list_lock);
156 list_for_each_entry(pos, &thermal_tz_list, node) {
158 * only thermal zones with specified tz->tzp->governor_name
159 * may run with tz->govenor unset
164 name = pos->tzp->governor_name;
166 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
169 ret = thermal_set_governor(pos, governor);
171 dev_err(&pos->device,
172 "Failed to set governor %s for thermal zone %s: %d\n",
173 governor->name, pos->type, ret);
177 mutex_unlock(&thermal_list_lock);
178 mutex_unlock(&thermal_governor_lock);
183 void thermal_unregister_governor(struct thermal_governor *governor)
185 struct thermal_zone_device *pos;
190 mutex_lock(&thermal_governor_lock);
192 if (thermal_find_governor(governor->name) == NULL)
195 mutex_lock(&thermal_list_lock);
197 list_for_each_entry(pos, &thermal_tz_list, node) {
198 if (!strncasecmp(pos->governor->name, governor->name,
199 THERMAL_NAME_LENGTH))
200 thermal_set_governor(pos, NULL);
203 mutex_unlock(&thermal_list_lock);
204 list_del(&governor->governor_list);
206 mutex_unlock(&thermal_governor_lock);
210 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
216 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
219 if (unlikely(ret < 0))
225 static void release_idr(struct idr *idr, struct mutex *lock, int id)
234 int get_tz_trend(struct thermal_zone_device *tz, int trip)
236 enum thermal_trend trend;
238 if (tz->emul_temperature || !tz->ops->get_trend ||
239 tz->ops->get_trend(tz, trip, &trend)) {
240 if (tz->temperature > tz->last_temperature)
241 trend = THERMAL_TREND_RAISING;
242 else if (tz->temperature < tz->last_temperature)
243 trend = THERMAL_TREND_DROPPING;
245 trend = THERMAL_TREND_STABLE;
250 EXPORT_SYMBOL(get_tz_trend);
252 struct thermal_instance *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, u64 mask,
283 struct thermal_cooling_device *cdev,
284 unsigned long *limits,
289 for (i = 0; i < tz->trips; i++) {
290 if (mask & (1ULL << 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, u64 mask,
309 struct thermal_cooling_device *cdev)
313 for (i = 0; i < tz->trips; i++)
314 if (mask & (1ULL << 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 tz->passive_delay = 0;
451 tz->polling_delay = 0;
452 orderly_poweroff(true);
456 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
458 enum thermal_trip_type type;
460 /* Ignore disabled trip points */
461 if (test_bit(trip, &tz->trips_disabled))
464 tz->ops->get_trip_type(tz, trip, &type);
466 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
467 handle_critical_trips(tz, trip, type);
469 handle_non_critical_trips(tz, trip, type);
471 * Alright, we handled this trip successfully.
472 * So, start monitoring again.
474 monitor_thermal_zone(tz);
478 * thermal_zone_get_temp() - returns the temperature of a thermal zone
479 * @tz: a valid pointer to a struct thermal_zone_device
480 * @temp: a valid pointer to where to store the resulting temperature.
482 * When a valid thermal zone reference is passed, it will fetch its
483 * temperature and fill @temp.
485 * Return: On success returns 0, an error code otherwise
487 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
491 int crit_temp = INT_MAX;
492 enum thermal_trip_type type;
494 if (!tz || IS_ERR(tz) || !tz->ops->get_temp ||
495 !device_is_registered(&tz->device))
498 mutex_lock(&tz->lock);
500 ret = tz->ops->get_temp(tz, temp);
502 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
503 for (count = 0; count < tz->trips; count++) {
504 ret = tz->ops->get_trip_type(tz, count, &type);
505 if (!ret && type == THERMAL_TRIP_CRITICAL) {
506 ret = tz->ops->get_trip_temp(tz, count,
513 * Only allow emulating a temperature when the real temperature
514 * is below the critical temperature so that the emulation code
515 * cannot hide critical conditions.
517 if (!ret && *temp < crit_temp)
518 *temp = tz->emul_temperature;
521 mutex_unlock(&tz->lock);
525 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
527 void thermal_zone_set_trips(struct thermal_zone_device *tz)
531 int trip_temp, hysteresis;
534 mutex_lock(&tz->lock);
536 if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
539 for (i = 0; i < tz->trips; i++) {
542 tz->ops->get_trip_temp(tz, i, &trip_temp);
543 tz->ops->get_trip_hyst(tz, i, &hysteresis);
545 trip_low = trip_temp - hysteresis;
547 if (trip_low < tz->temperature && trip_low > low)
550 if (trip_temp > tz->temperature && trip_temp < high)
554 /* No need to change trip points */
555 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
558 tz->prev_low_trip = low;
559 tz->prev_high_trip = high;
562 "new temperature boundaries: %d < x < %d\n", low, high);
565 * Set a temperature window. When this window is left the driver
566 * must inform the thermal core via thermal_zone_device_update.
568 ret = tz->ops->set_trips(tz, low, high);
570 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
573 mutex_unlock(&tz->lock);
575 EXPORT_SYMBOL_GPL(thermal_zone_set_trips);
577 static int update_temperature(struct thermal_zone_device *tz)
581 ret = thermal_zone_get_temp(tz, &temp);
584 dev_warn(&tz->device,
585 "failed to read out thermal zone (%d)\n",
590 mutex_lock(&tz->lock);
591 tz->last_temperature = tz->temperature;
592 tz->temperature = temp;
593 mutex_unlock(&tz->lock);
595 trace_thermal_temperature(tz);
596 if (tz->last_temperature == THERMAL_TEMP_INVALID)
597 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
600 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
601 tz->last_temperature, tz->temperature);
606 static void thermal_zone_device_reset(struct thermal_zone_device *tz)
608 struct thermal_instance *pos;
610 tz->temperature = THERMAL_TEMP_INVALID;
612 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
613 pos->initialized = false;
616 void thermal_zone_device_update(struct thermal_zone_device *tz)
620 if (atomic_read(&in_suspend))
623 if (!tz->ops->get_temp)
626 if (atomic_read(&in_suspend))
629 if (update_temperature(tz))
632 thermal_zone_set_trips(tz);
634 for (count = 0; count < tz->trips; count++)
635 handle_thermal_trip(tz, count);
637 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
639 static void thermal_zone_device_check(struct work_struct *work)
641 struct thermal_zone_device *tz = container_of(work, struct
644 thermal_zone_device_update(tz);
647 /* sys I/F for thermal zone */
649 #define to_thermal_zone(_dev) \
650 container_of(_dev, struct thermal_zone_device, device)
653 type_show(struct device *dev, struct device_attribute *attr, char *buf)
655 struct thermal_zone_device *tz = to_thermal_zone(dev);
657 return sprintf(buf, "%s\n", tz->type);
661 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
663 struct thermal_zone_device *tz = to_thermal_zone(dev);
664 int temperature, ret;
666 ret = thermal_zone_get_temp(tz, &temperature);
671 return sprintf(buf, "%d\n", temperature);
675 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
677 struct thermal_zone_device *tz = to_thermal_zone(dev);
678 enum thermal_device_mode mode;
681 if (!tz->ops->get_mode)
684 result = tz->ops->get_mode(tz, &mode);
688 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
693 mode_store(struct device *dev, struct device_attribute *attr,
694 const char *buf, size_t count)
696 struct thermal_zone_device *tz = to_thermal_zone(dev);
699 if (!tz->ops->set_mode)
702 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
703 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
704 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
705 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
716 trip_point_type_show(struct device *dev, struct device_attribute *attr,
719 struct thermal_zone_device *tz = to_thermal_zone(dev);
720 enum thermal_trip_type type;
723 if (!tz->ops->get_trip_type)
726 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
729 result = tz->ops->get_trip_type(tz, trip, &type);
734 case THERMAL_TRIP_CRITICAL:
735 return sprintf(buf, "critical\n");
736 case THERMAL_TRIP_HOT:
737 return sprintf(buf, "hot\n");
738 case THERMAL_TRIP_PASSIVE:
739 return sprintf(buf, "passive\n");
740 case THERMAL_TRIP_ACTIVE:
741 return sprintf(buf, "active\n");
743 return sprintf(buf, "unknown\n");
748 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
749 const char *buf, size_t count)
751 struct thermal_zone_device *tz = to_thermal_zone(dev);
753 unsigned long temperature;
755 if (!tz->ops->set_trip_temp)
758 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
761 if (kstrtol(buf, 10, &temperature))
764 ret = tz->ops->set_trip_temp(tz, trip, temperature);
768 thermal_zone_device_update(tz);
774 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
777 struct thermal_zone_device *tz = to_thermal_zone(dev);
781 if (!tz->ops->get_trip_temp)
784 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
787 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
792 return sprintf(buf, "%d\n", temperature);
796 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
797 const char *buf, size_t count)
799 struct thermal_zone_device *tz = to_thermal_zone(dev);
803 if (!tz->ops->set_trip_hyst)
806 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
809 if (kstrtoint(buf, 10, &temperature))
813 * We are not doing any check on the 'temperature' value
814 * here. The driver implementing 'set_trip_hyst' has to
817 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
820 thermal_zone_set_trips(tz);
822 return ret ? ret : count;
826 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
829 struct thermal_zone_device *tz = to_thermal_zone(dev);
833 if (!tz->ops->get_trip_hyst)
836 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
839 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
841 return ret ? ret : sprintf(buf, "%d\n", temperature);
845 passive_store(struct device *dev, struct device_attribute *attr,
846 const char *buf, size_t count)
848 struct thermal_zone_device *tz = to_thermal_zone(dev);
849 struct thermal_cooling_device *cdev = NULL;
852 if (!sscanf(buf, "%d\n", &state))
855 /* sanity check: values below 1000 millicelcius don't make sense
856 * and can cause the system to go into a thermal heart attack
858 if (state && state < 1000)
861 if (state && !tz->forced_passive) {
862 mutex_lock(&thermal_list_lock);
863 list_for_each_entry(cdev, &thermal_cdev_list, node) {
864 if (!strncmp("Processor", cdev->type,
865 sizeof("Processor")))
866 thermal_zone_bind_cooling_device(tz,
867 THERMAL_TRIPS_NONE, cdev,
870 THERMAL_WEIGHT_DEFAULT);
872 mutex_unlock(&thermal_list_lock);
873 if (!tz->passive_delay)
874 tz->passive_delay = 1000;
875 } else if (!state && tz->forced_passive) {
876 mutex_lock(&thermal_list_lock);
877 list_for_each_entry(cdev, &thermal_cdev_list, node) {
878 if (!strncmp("Processor", cdev->type,
879 sizeof("Processor")))
880 thermal_zone_unbind_cooling_device(tz,
884 mutex_unlock(&thermal_list_lock);
885 tz->passive_delay = 0;
888 tz->forced_passive = state;
890 thermal_zone_device_update(tz);
896 passive_show(struct device *dev, struct device_attribute *attr,
899 struct thermal_zone_device *tz = to_thermal_zone(dev);
901 return sprintf(buf, "%d\n", tz->forced_passive);
905 passive_delay_store(struct device *dev, struct device_attribute *attr,
906 const char *buf, size_t count)
908 struct thermal_zone_device *tz = to_thermal_zone(dev);
911 if (!sscanf(buf, "%d\n", &passive_delay))
914 tz->passive_delay = passive_delay;
915 thermal_zone_device_update(tz);
920 passive_delay_show(struct device *dev, struct device_attribute *attr,
923 struct thermal_zone_device *tz = to_thermal_zone(dev);
925 return sprintf(buf, "%d\n", tz->passive_delay);
929 polling_delay_store(struct device *dev, struct device_attribute *attr,
930 const char *buf, size_t count)
932 struct thermal_zone_device *tz = to_thermal_zone(dev);
935 if (!sscanf(buf, "%d\n", &polling_delay))
938 tz->polling_delay = polling_delay;
939 thermal_zone_device_update(tz);
944 polling_delay_show(struct device *dev, struct device_attribute *attr,
947 struct thermal_zone_device *tz = to_thermal_zone(dev);
949 return sprintf(buf, "%d\n", tz->polling_delay);
953 policy_store(struct device *dev, struct device_attribute *attr,
954 const char *buf, size_t count)
957 struct thermal_zone_device *tz = to_thermal_zone(dev);
958 struct thermal_governor *gov;
959 char name[THERMAL_NAME_LENGTH];
961 snprintf(name, sizeof(name), "%s", buf);
963 mutex_lock(&thermal_governor_lock);
964 mutex_lock(&tz->lock);
966 gov = thermal_find_governor((const char *)strim(name));
970 ret = thermal_set_governor(tz, gov);
975 mutex_unlock(&tz->lock);
976 mutex_unlock(&thermal_governor_lock);
981 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
983 struct thermal_zone_device *tz = to_thermal_zone(dev);
985 return sprintf(buf, "%s\n", tz->governor->name);
989 available_policies_show(struct device *dev, struct device_attribute *devattr,
992 struct thermal_governor *pos;
994 ssize_t size = PAGE_SIZE;
996 mutex_lock(&thermal_governor_lock);
998 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
999 size = PAGE_SIZE - count;
1000 count += scnprintf(buf + count, size, "%s ", pos->name);
1002 count += scnprintf(buf + count, size, "\n");
1004 mutex_unlock(&thermal_governor_lock);
1010 emul_temp_store(struct device *dev, struct device_attribute *attr,
1011 const char *buf, size_t count)
1013 struct thermal_zone_device *tz = to_thermal_zone(dev);
1015 unsigned long temperature;
1017 if (kstrtoul(buf, 10, &temperature))
1020 if (!tz->ops->set_emul_temp) {
1021 mutex_lock(&tz->lock);
1022 tz->emul_temperature = temperature;
1023 mutex_unlock(&tz->lock);
1025 ret = tz->ops->set_emul_temp(tz, temperature);
1029 thermal_zone_device_update(tz);
1031 return ret ? ret : count;
1033 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
1036 sustainable_power_show(struct device *dev, struct device_attribute *devattr,
1039 struct thermal_zone_device *tz = to_thermal_zone(dev);
1042 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
1048 sustainable_power_store(struct device *dev, struct device_attribute *devattr,
1049 const char *buf, size_t count)
1051 struct thermal_zone_device *tz = to_thermal_zone(dev);
1052 u32 sustainable_power;
1057 if (kstrtou32(buf, 10, &sustainable_power))
1060 tz->tzp->sustainable_power = sustainable_power;
1064 static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
1065 sustainable_power_store);
1067 #define create_s32_tzp_attr(name) \
1069 name##_show(struct device *dev, struct device_attribute *devattr, \
1072 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1075 return sprintf(buf, "%u\n", tz->tzp->name); \
1081 name##_store(struct device *dev, struct device_attribute *devattr, \
1082 const char *buf, size_t count) \
1084 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1090 if (kstrtos32(buf, 10, &value)) \
1093 tz->tzp->name = value; \
1097 static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
1099 create_s32_tzp_attr(k_po);
1100 create_s32_tzp_attr(k_pu);
1101 create_s32_tzp_attr(k_i);
1102 create_s32_tzp_attr(k_d);
1103 create_s32_tzp_attr(integral_cutoff);
1104 create_s32_tzp_attr(slope);
1105 create_s32_tzp_attr(offset);
1106 #undef create_s32_tzp_attr
1108 static struct device_attribute *dev_tzp_attrs[] = {
1109 &dev_attr_sustainable_power,
1114 &dev_attr_integral_cutoff,
1119 static int create_tzp_attrs(struct device *dev)
1123 for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
1125 struct device_attribute *dev_attr = dev_tzp_attrs[i];
1127 ret = device_create_file(dev, dev_attr);
1136 * power_actor_get_max_power() - get the maximum power that a cdev can consume
1137 * @cdev: pointer to &thermal_cooling_device
1138 * @tz: a valid thermal zone device pointer
1139 * @max_power: pointer in which to store the maximum power
1141 * Calculate the maximum power consumption in milliwats that the
1142 * cooling device can currently consume and store it in @max_power.
1144 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1145 * power_actor API or -E* on other error.
1147 int power_actor_get_max_power(struct thermal_cooling_device *cdev,
1148 struct thermal_zone_device *tz, u32 *max_power)
1150 if (!cdev_is_power_actor(cdev))
1153 return cdev->ops->state2power(cdev, tz, 0, max_power);
1157 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
1158 * @cdev: pointer to &thermal_cooling_device
1159 * @tz: a valid thermal zone device pointer
1160 * @min_power: pointer in which to store the minimum power
1162 * Calculate the minimum power consumption in milliwatts that the
1163 * cooling device can currently consume and store it in @min_power.
1165 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1166 * power_actor API or -E* on other error.
1168 int power_actor_get_min_power(struct thermal_cooling_device *cdev,
1169 struct thermal_zone_device *tz, u32 *min_power)
1171 unsigned long max_state;
1174 if (!cdev_is_power_actor(cdev))
1177 ret = cdev->ops->get_max_state(cdev, &max_state);
1181 return cdev->ops->state2power(cdev, tz, max_state, min_power);
1185 * power_actor_set_power() - limit the maximum power that a cooling device can consume
1186 * @cdev: pointer to &thermal_cooling_device
1187 * @instance: thermal instance to update
1188 * @power: the power in milliwatts
1190 * Set the cooling device to consume at most @power milliwatts.
1192 * Return: 0 on success, -EINVAL if the cooling device does not
1193 * implement the power actor API or -E* for other failures.
1195 int power_actor_set_power(struct thermal_cooling_device *cdev,
1196 struct thermal_instance *instance, u32 power)
1198 unsigned long state;
1201 if (!cdev_is_power_actor(cdev))
1204 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
1208 instance->target = state;
1209 cdev->updated = false;
1210 thermal_cdev_update(cdev);
1215 static DEVICE_ATTR(type, 0444, type_show, NULL);
1216 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
1217 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
1218 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
1219 static DEVICE_ATTR(passive_delay, S_IRUGO | S_IWUSR,
1220 passive_delay_show, passive_delay_store);
1221 static DEVICE_ATTR(polling_delay, S_IRUGO | S_IWUSR,
1222 polling_delay_show, polling_delay_store);
1223 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
1224 static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
1226 /* sys I/F for cooling device */
1227 #define to_cooling_device(_dev) \
1228 container_of(_dev, struct thermal_cooling_device, device)
1231 thermal_cooling_device_type_show(struct device *dev,
1232 struct device_attribute *attr, char *buf)
1234 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1236 return sprintf(buf, "%s\n", cdev->type);
1240 thermal_cooling_device_max_state_show(struct device *dev,
1241 struct device_attribute *attr, char *buf)
1243 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1244 unsigned long state;
1247 ret = cdev->ops->get_max_state(cdev, &state);
1250 return sprintf(buf, "%ld\n", state);
1254 thermal_cooling_device_cur_state_show(struct device *dev,
1255 struct device_attribute *attr, char *buf)
1257 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1258 unsigned long state;
1261 ret = cdev->ops->get_cur_state(cdev, &state);
1264 return sprintf(buf, "%ld\n", state);
1268 thermal_cooling_device_cur_state_store(struct device *dev,
1269 struct device_attribute *attr,
1270 const char *buf, size_t count)
1272 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1273 unsigned long state;
1276 if (!sscanf(buf, "%ld\n", &state))
1279 if ((long)state < 0)
1282 result = cdev->ops->set_cur_state(cdev, state);
1288 static struct device_attribute dev_attr_cdev_type =
1289 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
1290 static DEVICE_ATTR(max_state, 0444,
1291 thermal_cooling_device_max_state_show, NULL);
1292 static DEVICE_ATTR(cur_state, 0644,
1293 thermal_cooling_device_cur_state_show,
1294 thermal_cooling_device_cur_state_store);
1297 thermal_cooling_device_trip_point_show(struct device *dev,
1298 struct device_attribute *attr, char *buf)
1300 struct thermal_instance *instance;
1303 container_of(attr, struct thermal_instance, attr);
1305 if (instance->trip == THERMAL_TRIPS_NONE)
1306 return sprintf(buf, "-1\n");
1308 return sprintf(buf, "%d\n", instance->trip);
1311 static struct attribute *cooling_device_attrs[] = {
1312 &dev_attr_cdev_type.attr,
1313 &dev_attr_max_state.attr,
1314 &dev_attr_cur_state.attr,
1318 static const struct attribute_group cooling_device_attr_group = {
1319 .attrs = cooling_device_attrs,
1322 static const struct attribute_group *cooling_device_attr_groups[] = {
1323 &cooling_device_attr_group,
1328 thermal_cooling_device_weight_show(struct device *dev,
1329 struct device_attribute *attr, char *buf)
1331 struct thermal_instance *instance;
1333 instance = container_of(attr, struct thermal_instance, weight_attr);
1335 return sprintf(buf, "%d\n", instance->weight);
1339 thermal_cooling_device_weight_store(struct device *dev,
1340 struct device_attribute *attr,
1341 const char *buf, size_t count)
1343 struct thermal_instance *instance;
1346 ret = kstrtoint(buf, 0, &weight);
1350 instance = container_of(attr, struct thermal_instance, weight_attr);
1351 instance->weight = weight;
1355 /* Device management */
1358 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1359 * @tz: pointer to struct thermal_zone_device
1360 * @trip: indicates which trip point the cooling devices is
1361 * associated with in this thermal zone.
1362 * @cdev: pointer to struct thermal_cooling_device
1363 * @upper: the Maximum cooling state for this trip point.
1364 * THERMAL_NO_LIMIT means no upper limit,
1365 * and the cooling device can be in max_state.
1366 * @lower: the Minimum cooling state can be used for this trip point.
1367 * THERMAL_NO_LIMIT means no lower limit,
1368 * and the cooling device can be in cooling state 0.
1369 * @weight: The weight of the cooling device to be bound to the
1370 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1373 * This interface function bind a thermal cooling device to the certain trip
1374 * point of a thermal zone device.
1375 * This function is usually called in the thermal zone device .bind callback.
1377 * Return: 0 on success, the proper error value otherwise.
1379 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1381 struct thermal_cooling_device *cdev,
1382 unsigned long upper, unsigned long lower,
1383 unsigned int weight)
1385 struct thermal_instance *dev;
1386 struct thermal_instance *pos;
1387 struct thermal_zone_device *pos1;
1388 struct thermal_cooling_device *pos2;
1389 unsigned long max_state;
1392 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1395 list_for_each_entry(pos1, &thermal_tz_list, node) {
1399 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1404 if (tz != pos1 || cdev != pos2)
1407 ret = cdev->ops->get_max_state(cdev, &max_state);
1411 /* lower default 0, upper default max_state */
1412 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1413 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1415 if (lower > upper || upper > max_state)
1419 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1427 dev->target = THERMAL_NO_TARGET;
1428 dev->weight = weight;
1430 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1434 sprintf(dev->name, "cdev%d", dev->id);
1436 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1440 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1441 sysfs_attr_init(&dev->attr.attr);
1442 dev->attr.attr.name = dev->attr_name;
1443 dev->attr.attr.mode = 0444;
1444 dev->attr.show = thermal_cooling_device_trip_point_show;
1445 result = device_create_file(&tz->device, &dev->attr);
1447 goto remove_symbol_link;
1449 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1450 sysfs_attr_init(&dev->weight_attr.attr);
1451 dev->weight_attr.attr.name = dev->weight_attr_name;
1452 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1453 dev->weight_attr.show = thermal_cooling_device_weight_show;
1454 dev->weight_attr.store = thermal_cooling_device_weight_store;
1455 result = device_create_file(&tz->device, &dev->weight_attr);
1457 goto remove_trip_file;
1459 mutex_lock(&tz->lock);
1460 mutex_lock(&cdev->lock);
1461 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1462 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1467 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1468 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1469 atomic_set(&tz->need_update, 1);
1471 mutex_unlock(&cdev->lock);
1472 mutex_unlock(&tz->lock);
1477 device_remove_file(&tz->device, &dev->weight_attr);
1479 device_remove_file(&tz->device, &dev->attr);
1481 sysfs_remove_link(&tz->device.kobj, dev->name);
1483 release_idr(&tz->idr, &tz->lock, dev->id);
1488 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
1491 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1493 * @tz: pointer to a struct thermal_zone_device.
1494 * @trip: indicates which trip point the cooling devices is
1495 * associated with in this thermal zone.
1496 * @cdev: pointer to a struct thermal_cooling_device.
1498 * This interface function unbind a thermal cooling device from the certain
1499 * trip point of a thermal zone device.
1500 * This function is usually called in the thermal zone device .unbind callback.
1502 * Return: 0 on success, the proper error value otherwise.
1504 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1506 struct thermal_cooling_device *cdev)
1508 struct thermal_instance *pos, *next;
1510 mutex_lock(&tz->lock);
1511 mutex_lock(&cdev->lock);
1512 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1513 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1514 list_del(&pos->tz_node);
1515 list_del(&pos->cdev_node);
1516 mutex_unlock(&cdev->lock);
1517 mutex_unlock(&tz->lock);
1521 mutex_unlock(&cdev->lock);
1522 mutex_unlock(&tz->lock);
1527 device_remove_file(&tz->device, &pos->weight_attr);
1528 device_remove_file(&tz->device, &pos->attr);
1529 sysfs_remove_link(&tz->device.kobj, pos->name);
1530 release_idr(&tz->idr, &tz->lock, pos->id);
1534 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1536 static void thermal_release(struct device *dev)
1538 struct thermal_zone_device *tz;
1539 struct thermal_cooling_device *cdev;
1541 if (!strncmp(dev_name(dev), "thermal_zone",
1542 sizeof("thermal_zone") - 1)) {
1543 tz = to_thermal_zone(dev);
1545 } else if(!strncmp(dev_name(dev), "cooling_device",
1546 sizeof("cooling_device") - 1)){
1547 cdev = to_cooling_device(dev);
1552 static struct class thermal_class = {
1554 .dev_release = thermal_release,
1558 * __thermal_cooling_device_register() - register a new thermal cooling device
1559 * @np: a pointer to a device tree node.
1560 * @type: the thermal cooling device type.
1561 * @devdata: device private data.
1562 * @ops: standard thermal cooling devices callbacks.
1564 * This interface function adds a new thermal cooling device (fan/processor/...)
1565 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1566 * to all the thermal zone devices registered at the same time.
1567 * It also gives the opportunity to link the cooling device to a device tree
1568 * node, so that it can be bound to a thermal zone created out of device tree.
1570 * Return: a pointer to the created struct thermal_cooling_device or an
1571 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1573 static struct thermal_cooling_device *
1574 __thermal_cooling_device_register(struct device_node *np,
1575 char *type, void *devdata,
1576 const struct thermal_cooling_device_ops *ops)
1578 struct thermal_cooling_device *cdev;
1579 struct thermal_zone_device *pos = NULL;
1582 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1583 return ERR_PTR(-EINVAL);
1585 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1586 !ops->set_cur_state)
1587 return ERR_PTR(-EINVAL);
1589 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1591 return ERR_PTR(-ENOMEM);
1593 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1596 return ERR_PTR(result);
1599 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1600 mutex_init(&cdev->lock);
1601 INIT_LIST_HEAD(&cdev->thermal_instances);
1604 cdev->updated = false;
1605 cdev->device.class = &thermal_class;
1606 cdev->device.groups = cooling_device_attr_groups;
1607 cdev->devdata = devdata;
1608 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1609 result = device_register(&cdev->device);
1611 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1613 return ERR_PTR(result);
1616 /* Add 'this' new cdev to the global cdev list */
1617 mutex_lock(&thermal_list_lock);
1618 list_add(&cdev->node, &thermal_cdev_list);
1619 mutex_unlock(&thermal_list_lock);
1621 /* Update binding information for 'this' new cdev */
1624 mutex_lock(&thermal_list_lock);
1625 list_for_each_entry(pos, &thermal_tz_list, node)
1626 if (atomic_cmpxchg(&pos->need_update, 1, 0))
1627 thermal_zone_device_update(pos);
1628 mutex_unlock(&thermal_list_lock);
1634 * thermal_cooling_device_register() - register a new thermal cooling device
1635 * @type: the thermal cooling device type.
1636 * @devdata: device private data.
1637 * @ops: standard thermal cooling devices callbacks.
1639 * This interface function adds a new thermal cooling device (fan/processor/...)
1640 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1641 * to all the thermal zone devices registered at the same time.
1643 * Return: a pointer to the created struct thermal_cooling_device or an
1644 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1646 struct thermal_cooling_device *
1647 thermal_cooling_device_register(char *type, void *devdata,
1648 const struct thermal_cooling_device_ops *ops)
1650 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1652 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1655 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1656 * @np: a pointer to a device tree node.
1657 * @type: the thermal cooling device type.
1658 * @devdata: device private data.
1659 * @ops: standard thermal cooling devices callbacks.
1661 * This function will register a cooling device with device tree node reference.
1662 * This interface function adds a new thermal cooling device (fan/processor/...)
1663 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1664 * to all the thermal zone devices registered at the same time.
1666 * Return: a pointer to the created struct thermal_cooling_device or an
1667 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1669 struct thermal_cooling_device *
1670 thermal_of_cooling_device_register(struct device_node *np,
1671 char *type, void *devdata,
1672 const struct thermal_cooling_device_ops *ops)
1674 return __thermal_cooling_device_register(np, type, devdata, ops);
1676 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1679 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1680 * @cdev: the thermal cooling device to remove.
1682 * thermal_cooling_device_unregister() must be called when the device is no
1685 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1688 const struct thermal_zone_params *tzp;
1689 struct thermal_zone_device *tz;
1690 struct thermal_cooling_device *pos = NULL;
1695 mutex_lock(&thermal_list_lock);
1696 list_for_each_entry(pos, &thermal_cdev_list, node)
1700 /* thermal cooling device not found */
1701 mutex_unlock(&thermal_list_lock);
1704 list_del(&cdev->node);
1706 /* Unbind all thermal zones associated with 'this' cdev */
1707 list_for_each_entry(tz, &thermal_tz_list, node) {
1708 if (tz->ops->unbind) {
1709 tz->ops->unbind(tz, cdev);
1713 if (!tz->tzp || !tz->tzp->tbp)
1717 for (i = 0; i < tzp->num_tbps; i++) {
1718 if (tzp->tbp[i].cdev == cdev) {
1719 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1720 tzp->tbp[i].cdev = NULL;
1725 mutex_unlock(&thermal_list_lock);
1728 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1729 device_remove_file(&cdev->device, &dev_attr_max_state);
1730 device_remove_file(&cdev->device, &dev_attr_cur_state);
1732 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1733 device_unregister(&cdev->device);
1736 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1738 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1740 struct thermal_instance *instance;
1741 unsigned long target = 0;
1743 /* cooling device is updated*/
1747 mutex_lock(&cdev->lock);
1748 /* Make sure cdev enters the deepest cooling state */
1749 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1750 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1751 instance->tz->id, instance->target);
1752 if (instance->target == THERMAL_NO_TARGET)
1754 if (instance->target > target)
1755 target = instance->target;
1757 mutex_unlock(&cdev->lock);
1758 cdev->ops->set_cur_state(cdev, target);
1759 cdev->updated = true;
1760 trace_cdev_update(cdev, target);
1761 dev_dbg(&cdev->device, "set to state %lu\n", target);
1763 EXPORT_SYMBOL(thermal_cdev_update);
1766 * thermal_notify_framework - Sensor drivers use this API to notify framework
1767 * @tz: thermal zone device
1768 * @trip: indicates which trip point has been crossed
1770 * This function handles the trip events from sensor drivers. It starts
1771 * throttling the cooling devices according to the policy configured.
1772 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1773 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1774 * The throttling policy is based on the configured platform data; if no
1775 * platform data is provided, this uses the step_wise throttling policy.
1777 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1779 handle_thermal_trip(tz, trip);
1781 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1783 struct thermal_zone_device *thermal_zone_device_find(void *data,
1784 int (*match)(struct thermal_zone_device *, void *))
1786 struct thermal_zone_device *thz;
1788 mutex_lock(&thermal_list_lock);
1789 list_for_each_entry(thz, &thermal_tz_list, node)
1790 if (match(thz, data)) {
1791 mutex_unlock(&thermal_list_lock);
1795 mutex_unlock(&thermal_list_lock);
1798 EXPORT_SYMBOL(thermal_zone_device_find);
1801 * create_trip_attrs() - create attributes for trip points
1802 * @tz: the thermal zone device
1803 * @mask: Writeable trip point bitmap.
1805 * helper function to instantiate sysfs entries for every trip
1806 * point and its properties of a struct thermal_zone_device.
1808 * Return: 0 on success, the proper error value otherwise.
1810 static int create_trip_attrs(struct thermal_zone_device *tz, u64 mask)
1813 int size = sizeof(struct thermal_attr) * tz->trips;
1815 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1816 if (!tz->trip_type_attrs)
1819 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1820 if (!tz->trip_temp_attrs) {
1821 kfree(tz->trip_type_attrs);
1825 if (tz->ops->get_trip_hyst) {
1826 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1827 if (!tz->trip_hyst_attrs) {
1828 kfree(tz->trip_type_attrs);
1829 kfree(tz->trip_temp_attrs);
1835 for (indx = 0; indx < tz->trips; indx++) {
1836 /* create trip type attribute */
1837 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1838 "trip_point_%d_type", indx);
1840 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1841 tz->trip_type_attrs[indx].attr.attr.name =
1842 tz->trip_type_attrs[indx].name;
1843 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1844 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1846 device_create_file(&tz->device,
1847 &tz->trip_type_attrs[indx].attr);
1849 /* create trip temp attribute */
1850 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1851 "trip_point_%d_temp", indx);
1853 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1854 tz->trip_temp_attrs[indx].attr.attr.name =
1855 tz->trip_temp_attrs[indx].name;
1856 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1857 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1858 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
1859 mask & (1ULL << indx)) {
1860 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1861 tz->trip_temp_attrs[indx].attr.store =
1862 trip_point_temp_store;
1865 device_create_file(&tz->device,
1866 &tz->trip_temp_attrs[indx].attr);
1868 /* create Optional trip hyst attribute */
1869 if (!tz->ops->get_trip_hyst)
1871 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1872 "trip_point_%d_hyst", indx);
1874 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1875 tz->trip_hyst_attrs[indx].attr.attr.name =
1876 tz->trip_hyst_attrs[indx].name;
1877 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1878 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1879 if (tz->ops->set_trip_hyst) {
1880 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1881 tz->trip_hyst_attrs[indx].attr.store =
1882 trip_point_hyst_store;
1885 device_create_file(&tz->device,
1886 &tz->trip_hyst_attrs[indx].attr);
1891 static void remove_trip_attrs(struct thermal_zone_device *tz)
1895 for (indx = 0; indx < tz->trips; indx++) {
1896 device_remove_file(&tz->device,
1897 &tz->trip_type_attrs[indx].attr);
1898 device_remove_file(&tz->device,
1899 &tz->trip_temp_attrs[indx].attr);
1900 if (tz->ops->get_trip_hyst)
1901 device_remove_file(&tz->device,
1902 &tz->trip_hyst_attrs[indx].attr);
1904 kfree(tz->trip_type_attrs);
1905 kfree(tz->trip_temp_attrs);
1906 kfree(tz->trip_hyst_attrs);
1910 * thermal_zone_device_register() - register a new thermal zone device
1911 * @type: the thermal zone device type
1912 * @trips: the number of trip points the thermal zone support
1913 * @mask: a bit string indicating the writeablility of trip points
1914 * @devdata: private device data
1915 * @ops: standard thermal zone device callbacks
1916 * @tzp: thermal zone platform parameters
1917 * @passive_delay: number of milliseconds to wait between polls when
1918 * performing passive cooling
1919 * @polling_delay: number of milliseconds to wait between polls when checking
1920 * whether trip points have been crossed (0 for interrupt
1923 * This interface function adds a new thermal zone device (sensor) to
1924 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1925 * thermal cooling devices registered at the same time.
1926 * thermal_zone_device_unregister() must be called when the device is no
1927 * longer needed. The passive cooling depends on the .get_trend() return value.
1929 * Return: a pointer to the created struct thermal_zone_device or an
1930 * in case of error, an ERR_PTR. Caller must check return value with
1931 * IS_ERR*() helpers.
1933 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1934 int trips, u64 mask, void *devdata,
1935 struct thermal_zone_device_ops *ops,
1936 struct thermal_zone_params *tzp,
1937 int passive_delay, int polling_delay)
1939 struct thermal_zone_device *tz;
1940 enum thermal_trip_type trip_type;
1945 struct thermal_governor *governor;
1947 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1948 return ERR_PTR(-EINVAL);
1950 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1951 return ERR_PTR(-EINVAL);
1954 return ERR_PTR(-EINVAL);
1956 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1957 return ERR_PTR(-EINVAL);
1959 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1961 return ERR_PTR(-ENOMEM);
1963 INIT_LIST_HEAD(&tz->thermal_instances);
1965 mutex_init(&tz->lock);
1966 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1969 return ERR_PTR(result);
1972 strlcpy(tz->type, type ? : "", sizeof(tz->type));
1975 tz->device.class = &thermal_class;
1976 tz->devdata = devdata;
1978 tz->passive_delay = passive_delay;
1979 tz->polling_delay = polling_delay;
1980 /* A new thermal zone needs to be updated anyway. */
1981 atomic_set(&tz->need_update, 1);
1983 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1984 result = device_register(&tz->device);
1986 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1988 return ERR_PTR(result);
1993 result = device_create_file(&tz->device, &dev_attr_type);
1998 result = device_create_file(&tz->device, &dev_attr_temp);
2002 if (ops->get_mode) {
2003 result = device_create_file(&tz->device, &dev_attr_mode);
2008 result = create_trip_attrs(tz, mask);
2012 for (count = 0; count < trips; count++) {
2013 if (tz->ops->get_trip_type(tz, count, &trip_type))
2014 set_bit(count, &tz->trips_disabled);
2015 if (trip_type == THERMAL_TRIP_PASSIVE)
2017 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
2018 set_bit(count, &tz->trips_disabled);
2019 /* Check for bogus trip points */
2021 set_bit(count, &tz->trips_disabled);
2025 result = device_create_file(&tz->device, &dev_attr_passive);
2030 if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
2031 result = device_create_file(&tz->device, &dev_attr_emul_temp);
2036 /* Create policy attribute */
2037 result = device_create_file(&tz->device, &dev_attr_policy);
2041 /* Add thermal zone params */
2042 result = create_tzp_attrs(&tz->device);
2046 /* Create passive_delay attribute */
2047 result = device_create_file(&tz->device, &dev_attr_passive_delay);
2051 /* Create polling_delay attribute */
2052 result = device_create_file(&tz->device, &dev_attr_polling_delay);
2056 /* Create available_policies attribute */
2057 result = device_create_file(&tz->device, &dev_attr_available_policies);
2061 /* Update 'this' zone's governor information */
2062 mutex_lock(&thermal_governor_lock);
2065 governor = thermal_find_governor(tz->tzp->governor_name);
2067 * This situation can arise if a governor is not enabled from a config
2068 * file but is used in the device tree
2071 dev_info(&tz->device,
2072 "%s gov not in list. using default\n",
2073 tz->tzp->governor_name);
2074 governor = def_governor;
2078 governor = def_governor;
2080 result = thermal_set_governor(tz, governor);
2082 mutex_unlock(&thermal_governor_lock);
2086 mutex_unlock(&thermal_governor_lock);
2088 if (!tz->tzp || !tz->tzp->no_hwmon) {
2089 result = thermal_add_hwmon_sysfs(tz);
2094 mutex_lock(&thermal_list_lock);
2095 list_add_tail(&tz->node, &thermal_tz_list);
2096 mutex_unlock(&thermal_list_lock);
2098 /* Bind cooling devices for this zone */
2101 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
2103 thermal_zone_device_reset(tz);
2104 /* Update the new thermal zone and mark it as already updated. */
2105 if (atomic_cmpxchg(&tz->need_update, 1, 0))
2106 thermal_zone_device_update(tz);
2108 dev_info(&tz->device, "Registering thermal zone %s for type %s\n",
2109 dev_name(&tz->device), type);
2113 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2114 device_unregister(&tz->device);
2115 return ERR_PTR(result);
2117 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
2120 * thermal_device_unregister - removes the registered thermal zone device
2121 * @tz: the thermal zone device to remove
2123 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
2126 const struct thermal_zone_params *tzp;
2127 struct thermal_cooling_device *cdev;
2128 struct thermal_zone_device *pos = NULL;
2135 mutex_lock(&thermal_list_lock);
2136 list_for_each_entry(pos, &thermal_tz_list, node)
2140 /* thermal zone device not found */
2141 mutex_unlock(&thermal_list_lock);
2144 list_del(&tz->node);
2146 /* Unbind all cdevs associated with 'this' thermal zone */
2147 list_for_each_entry(cdev, &thermal_cdev_list, node) {
2148 if (tz->ops->unbind) {
2149 tz->ops->unbind(tz, cdev);
2153 if (!tzp || !tzp->tbp)
2156 for (i = 0; i < tzp->num_tbps; i++) {
2157 if (tzp->tbp[i].cdev == cdev) {
2158 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
2159 tzp->tbp[i].cdev = NULL;
2164 mutex_unlock(&thermal_list_lock);
2166 thermal_zone_device_set_polling(tz, 0);
2169 device_remove_file(&tz->device, &dev_attr_type);
2170 device_remove_file(&tz->device, &dev_attr_temp);
2171 if (tz->ops->get_mode)
2172 device_remove_file(&tz->device, &dev_attr_mode);
2173 device_remove_file(&tz->device, &dev_attr_passive_delay);
2174 device_remove_file(&tz->device, &dev_attr_polling_delay);
2175 device_remove_file(&tz->device, &dev_attr_policy);
2176 device_remove_file(&tz->device, &dev_attr_available_policies);
2177 remove_trip_attrs(tz);
2178 thermal_set_governor(tz, NULL);
2180 thermal_remove_hwmon_sysfs(tz);
2181 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2182 idr_destroy(&tz->idr);
2183 mutex_destroy(&tz->lock);
2184 device_unregister(&tz->device);
2187 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
2190 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
2191 * @name: thermal zone name to fetch the temperature
2193 * When only one zone is found with the passed name, returns a reference to it.
2195 * Return: On success returns a reference to an unique thermal zone with
2196 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
2197 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
2199 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
2201 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
2202 unsigned int found = 0;
2207 mutex_lock(&thermal_list_lock);
2208 list_for_each_entry(pos, &thermal_tz_list, node)
2209 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
2213 mutex_unlock(&thermal_list_lock);
2215 /* nothing has been found, thus an error code for it */
2217 ref = ERR_PTR(-ENODEV);
2219 /* Success only when an unique zone is found */
2220 ref = ERR_PTR(-EEXIST);
2225 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
2228 * thermal_zone_get_zone_by_node() - search for a zone and returns its ref
2229 * @node: device node of the thermal zone
2231 * When thermal zone is found with the passed device node, returns a reference
2234 * Return: On success returns a reference to an unique thermal zone with
2235 * matching device node, an ERR_PTR otherwise (-EINVAL for invalid
2236 * paramenters, -ENODEV for not found).
2238 struct thermal_zone_device *
2239 thermal_zone_get_zone_by_node(struct device_node *node)
2241 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-ENODEV);
2244 return ERR_PTR(-EINVAL);
2246 mutex_lock(&thermal_list_lock);
2247 list_for_each_entry(pos, &thermal_tz_list, node)
2248 if (node == pos->np) {
2252 mutex_unlock(&thermal_list_lock);
2256 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_node);
2259 static const struct genl_multicast_group thermal_event_mcgrps[] = {
2260 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
2263 static struct genl_family thermal_event_genl_family = {
2264 .id = GENL_ID_GENERATE,
2265 .name = THERMAL_GENL_FAMILY_NAME,
2266 .version = THERMAL_GENL_VERSION,
2267 .maxattr = THERMAL_GENL_ATTR_MAX,
2268 .mcgrps = thermal_event_mcgrps,
2269 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
2272 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2275 struct sk_buff *skb;
2276 struct nlattr *attr;
2277 struct thermal_genl_event *thermal_event;
2281 static unsigned int thermal_event_seqnum;
2286 /* allocate memory */
2287 size = nla_total_size(sizeof(struct thermal_genl_event)) +
2290 skb = genlmsg_new(size, GFP_ATOMIC);
2294 /* add the genetlink message header */
2295 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2296 &thermal_event_genl_family, 0,
2297 THERMAL_GENL_CMD_EVENT);
2304 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2305 sizeof(struct thermal_genl_event));
2312 thermal_event = nla_data(attr);
2313 if (!thermal_event) {
2318 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2320 thermal_event->orig = tz->id;
2321 thermal_event->event = event;
2323 /* send multicast genetlink message */
2324 genlmsg_end(skb, msg_header);
2326 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
2329 dev_err(&tz->device, "Failed to send netlink event:%d", result);
2333 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
2335 static int genetlink_init(void)
2337 return genl_register_family(&thermal_event_genl_family);
2340 static void genetlink_exit(void)
2342 genl_unregister_family(&thermal_event_genl_family);
2344 #else /* !CONFIG_NET */
2345 static inline int genetlink_init(void) { return 0; }
2346 static inline void genetlink_exit(void) {}
2347 #endif /* !CONFIG_NET */
2349 static int __init thermal_register_governors(void)
2353 result = thermal_gov_step_wise_register();
2357 result = thermal_gov_fair_share_register();
2361 result = thermal_gov_bang_bang_register();
2365 result = pid_thermal_gov_register();
2369 result = thermal_gov_user_space_register();
2373 return thermal_gov_power_allocator_register();
2376 static void thermal_unregister_governors(void)
2378 thermal_gov_step_wise_unregister();
2379 thermal_gov_fair_share_unregister();
2380 thermal_gov_bang_bang_unregister();
2381 pid_thermal_gov_unregister();
2382 thermal_gov_user_space_unregister();
2383 thermal_gov_power_allocator_unregister();
2386 static int thermal_pm_notify(struct notifier_block *nb,
2387 unsigned long mode, void *_unused)
2389 struct thermal_zone_device *tz;
2392 case PM_HIBERNATION_PREPARE:
2393 case PM_RESTORE_PREPARE:
2394 case PM_SUSPEND_PREPARE:
2395 atomic_set(&in_suspend, 1);
2397 case PM_POST_HIBERNATION:
2398 case PM_POST_RESTORE:
2399 case PM_POST_SUSPEND:
2400 atomic_set(&in_suspend, 0);
2401 list_for_each_entry(tz, &thermal_tz_list, node) {
2402 thermal_zone_device_reset(tz);
2403 thermal_zone_device_update(tz);
2412 static struct notifier_block thermal_pm_nb = {
2413 .notifier_call = thermal_pm_notify,
2416 static int __init thermal_init(void)
2420 result = thermal_register_governors();
2424 result = class_register(&thermal_class);
2426 goto unregister_governors;
2428 result = genetlink_init();
2430 goto unregister_class;
2432 result = of_parse_thermal_zones();
2436 result = register_pm_notifier(&thermal_pm_nb);
2438 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
2446 class_unregister(&thermal_class);
2447 unregister_governors:
2448 thermal_unregister_governors();
2450 idr_destroy(&thermal_tz_idr);
2451 idr_destroy(&thermal_cdev_idr);
2452 mutex_destroy(&thermal_idr_lock);
2453 mutex_destroy(&thermal_list_lock);
2454 mutex_destroy(&thermal_governor_lock);
2458 static void __exit thermal_exit(void)
2460 unregister_pm_notifier(&thermal_pm_nb);
2461 of_thermal_destroy_zones();
2463 class_unregister(&thermal_class);
2464 thermal_unregister_governors();
2465 idr_destroy(&thermal_tz_idr);
2466 idr_destroy(&thermal_cdev_idr);
2467 mutex_destroy(&thermal_idr_lock);
2468 mutex_destroy(&thermal_list_lock);
2469 mutex_destroy(&thermal_governor_lock);
2472 fs_initcall(thermal_init);
2473 module_exit(thermal_exit);