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>
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/thermal.h>
44 #include "thermal_core.h"
45 #include "thermal_hwmon.h"
47 MODULE_AUTHOR("Zhang Rui");
48 MODULE_DESCRIPTION("Generic thermal management sysfs support");
49 MODULE_LICENSE("GPL v2");
51 static DEFINE_IDR(thermal_tz_idr);
52 static DEFINE_IDR(thermal_cdev_idr);
53 static DEFINE_MUTEX(thermal_idr_lock);
55 static LIST_HEAD(thermal_tz_list);
56 static LIST_HEAD(thermal_cdev_list);
57 static LIST_HEAD(thermal_governor_list);
59 static DEFINE_MUTEX(thermal_list_lock);
60 static DEFINE_MUTEX(thermal_governor_lock);
62 static struct thermal_governor *def_governor;
64 static struct thermal_governor *__find_governor(const char *name)
66 struct thermal_governor *pos;
68 if (!name || !name[0])
71 list_for_each_entry(pos, &thermal_governor_list, governor_list)
72 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
79 * bind_previous_governor() - bind the previous governor of the thermal zone
80 * @tz: a valid pointer to a struct thermal_zone_device
81 * @failed_gov_name: the name of the governor that failed to register
83 * Register the previous governor of the thermal zone after a new
84 * governor has failed to be bound.
86 static void bind_previous_governor(struct thermal_zone_device *tz,
87 const char *failed_gov_name)
89 if (tz->governor && tz->governor->bind_to_tz) {
90 if (tz->governor->bind_to_tz(tz)) {
92 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
93 failed_gov_name, tz->governor->name, tz->type);
100 * thermal_set_governor() - Switch to another governor
101 * @tz: a valid pointer to a struct thermal_zone_device
102 * @new_gov: pointer to the new governor
104 * Change the governor of thermal zone @tz.
106 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
108 static int thermal_set_governor(struct thermal_zone_device *tz,
109 struct thermal_governor *new_gov)
113 if (tz->governor && tz->governor->unbind_from_tz)
114 tz->governor->unbind_from_tz(tz);
116 if (new_gov && new_gov->bind_to_tz) {
117 ret = new_gov->bind_to_tz(tz);
119 bind_previous_governor(tz, new_gov->name);
125 tz->governor = new_gov;
130 int thermal_register_governor(struct thermal_governor *governor)
134 struct thermal_zone_device *pos;
139 mutex_lock(&thermal_governor_lock);
142 if (__find_governor(governor->name) == NULL) {
144 list_add(&governor->governor_list, &thermal_governor_list);
145 if (!def_governor && !strncmp(governor->name,
146 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
147 def_governor = governor;
150 mutex_lock(&thermal_list_lock);
152 list_for_each_entry(pos, &thermal_tz_list, node) {
154 * only thermal zones with specified tz->tzp->governor_name
155 * may run with tz->govenor unset
160 name = pos->tzp->governor_name;
162 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
165 ret = thermal_set_governor(pos, governor);
167 dev_err(&pos->device,
168 "Failed to set governor %s for thermal zone %s: %d\n",
169 governor->name, pos->type, ret);
173 mutex_unlock(&thermal_list_lock);
174 mutex_unlock(&thermal_governor_lock);
179 void thermal_unregister_governor(struct thermal_governor *governor)
181 struct thermal_zone_device *pos;
186 mutex_lock(&thermal_governor_lock);
188 if (__find_governor(governor->name) == NULL)
191 mutex_lock(&thermal_list_lock);
193 list_for_each_entry(pos, &thermal_tz_list, node) {
194 if (!strncasecmp(pos->governor->name, governor->name,
195 THERMAL_NAME_LENGTH))
196 thermal_set_governor(pos, NULL);
199 mutex_unlock(&thermal_list_lock);
200 list_del(&governor->governor_list);
202 mutex_unlock(&thermal_governor_lock);
206 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
212 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
215 if (unlikely(ret < 0))
221 static void release_idr(struct idr *idr, struct mutex *lock, int id)
230 int get_tz_trend(struct thermal_zone_device *tz, int trip)
232 enum thermal_trend trend;
234 if (tz->emul_temperature || !tz->ops->get_trend ||
235 tz->ops->get_trend(tz, trip, &trend)) {
236 if (tz->temperature > tz->last_temperature)
237 trend = THERMAL_TREND_RAISING;
238 else if (tz->temperature < tz->last_temperature)
239 trend = THERMAL_TREND_DROPPING;
241 trend = THERMAL_TREND_STABLE;
246 EXPORT_SYMBOL(get_tz_trend);
248 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
249 struct thermal_cooling_device *cdev, int trip)
251 struct thermal_instance *pos = NULL;
252 struct thermal_instance *target_instance = NULL;
254 mutex_lock(&tz->lock);
255 mutex_lock(&cdev->lock);
257 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
258 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
259 target_instance = pos;
264 mutex_unlock(&cdev->lock);
265 mutex_unlock(&tz->lock);
267 return target_instance;
269 EXPORT_SYMBOL(get_thermal_instance);
271 static void print_bind_err_msg(struct thermal_zone_device *tz,
272 struct thermal_cooling_device *cdev, int ret)
274 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
275 tz->type, cdev->type, ret);
278 static void __bind(struct thermal_zone_device *tz, int mask,
279 struct thermal_cooling_device *cdev,
280 unsigned long *limits,
285 for (i = 0; i < tz->trips; i++) {
286 if (mask & (1 << i)) {
287 unsigned long upper, lower;
289 upper = THERMAL_NO_LIMIT;
290 lower = THERMAL_NO_LIMIT;
292 lower = limits[i * 2];
293 upper = limits[i * 2 + 1];
295 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
299 print_bind_err_msg(tz, cdev, ret);
304 static void __unbind(struct thermal_zone_device *tz, int mask,
305 struct thermal_cooling_device *cdev)
309 for (i = 0; i < tz->trips; i++)
311 thermal_zone_unbind_cooling_device(tz, i, cdev);
314 static void bind_cdev(struct thermal_cooling_device *cdev)
317 const struct thermal_zone_params *tzp;
318 struct thermal_zone_device *pos = NULL;
320 mutex_lock(&thermal_list_lock);
322 list_for_each_entry(pos, &thermal_tz_list, node) {
323 if (!pos->tzp && !pos->ops->bind)
326 if (pos->ops->bind) {
327 ret = pos->ops->bind(pos, cdev);
329 print_bind_err_msg(pos, cdev, ret);
334 if (!tzp || !tzp->tbp)
337 for (i = 0; i < tzp->num_tbps; i++) {
338 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
340 if (tzp->tbp[i].match(pos, cdev))
342 tzp->tbp[i].cdev = cdev;
343 __bind(pos, tzp->tbp[i].trip_mask, cdev,
344 tzp->tbp[i].binding_limits,
349 mutex_unlock(&thermal_list_lock);
352 static void bind_tz(struct thermal_zone_device *tz)
355 struct thermal_cooling_device *pos = NULL;
356 const struct thermal_zone_params *tzp = tz->tzp;
358 if (!tzp && !tz->ops->bind)
361 mutex_lock(&thermal_list_lock);
363 /* If there is ops->bind, try to use ops->bind */
365 list_for_each_entry(pos, &thermal_cdev_list, node) {
366 ret = tz->ops->bind(tz, pos);
368 print_bind_err_msg(tz, pos, ret);
373 if (!tzp || !tzp->tbp)
376 list_for_each_entry(pos, &thermal_cdev_list, node) {
377 for (i = 0; i < tzp->num_tbps; i++) {
378 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
380 if (tzp->tbp[i].match(tz, pos))
382 tzp->tbp[i].cdev = pos;
383 __bind(tz, tzp->tbp[i].trip_mask, pos,
384 tzp->tbp[i].binding_limits,
389 mutex_unlock(&thermal_list_lock);
392 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
396 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
397 round_jiffies(msecs_to_jiffies(delay)));
399 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
400 msecs_to_jiffies(delay));
402 cancel_delayed_work(&tz->poll_queue);
405 static void monitor_thermal_zone(struct thermal_zone_device *tz)
407 mutex_lock(&tz->lock);
410 thermal_zone_device_set_polling(tz, tz->passive_delay);
411 else if (tz->polling_delay)
412 thermal_zone_device_set_polling(tz, tz->polling_delay);
414 thermal_zone_device_set_polling(tz, 0);
416 mutex_unlock(&tz->lock);
419 static void handle_non_critical_trips(struct thermal_zone_device *tz,
420 int trip, enum thermal_trip_type trip_type)
422 tz->governor ? tz->governor->throttle(tz, trip) :
423 def_governor->throttle(tz, trip);
426 static void handle_critical_trips(struct thermal_zone_device *tz,
427 int trip, enum thermal_trip_type trip_type)
431 tz->ops->get_trip_temp(tz, trip, &trip_temp);
433 /* If we have not crossed the trip_temp, we do not care. */
434 if (trip_temp <= 0 || tz->temperature < trip_temp)
437 trace_thermal_zone_trip(tz, trip, trip_type);
440 tz->ops->notify(tz, trip, trip_type);
442 if (trip_type == THERMAL_TRIP_CRITICAL) {
443 dev_emerg(&tz->device,
444 "critical temperature reached(%d C),shutting down\n",
445 tz->temperature / 1000);
446 orderly_poweroff(true);
450 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
452 enum thermal_trip_type type;
454 tz->ops->get_trip_type(tz, trip, &type);
456 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
457 handle_critical_trips(tz, trip, type);
459 handle_non_critical_trips(tz, trip, type);
461 * Alright, we handled this trip successfully.
462 * So, start monitoring again.
464 monitor_thermal_zone(tz);
468 * thermal_zone_get_temp() - returns the temperature of a thermal zone
469 * @tz: a valid pointer to a struct thermal_zone_device
470 * @temp: a valid pointer to where to store the resulting temperature.
472 * When a valid thermal zone reference is passed, it will fetch its
473 * temperature and fill @temp.
475 * Return: On success returns 0, an error code otherwise
477 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
481 int crit_temp = INT_MAX;
482 enum thermal_trip_type type;
484 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
487 mutex_lock(&tz->lock);
489 ret = tz->ops->get_temp(tz, temp);
491 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
492 for (count = 0; count < tz->trips; count++) {
493 ret = tz->ops->get_trip_type(tz, count, &type);
494 if (!ret && type == THERMAL_TRIP_CRITICAL) {
495 ret = tz->ops->get_trip_temp(tz, count,
502 * Only allow emulating a temperature when the real temperature
503 * is below the critical temperature so that the emulation code
504 * cannot hide critical conditions.
506 if (!ret && *temp < crit_temp)
507 *temp = tz->emul_temperature;
510 mutex_unlock(&tz->lock);
514 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
516 static void update_temperature(struct thermal_zone_device *tz)
520 ret = thermal_zone_get_temp(tz, &temp);
523 dev_warn(&tz->device,
524 "failed to read out thermal zone (%d)\n",
529 mutex_lock(&tz->lock);
530 tz->last_temperature = tz->temperature;
531 tz->temperature = temp;
532 mutex_unlock(&tz->lock);
534 trace_thermal_temperature(tz);
535 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
536 tz->last_temperature, tz->temperature);
539 void thermal_zone_device_update(struct thermal_zone_device *tz)
543 if (!tz->ops->get_temp)
546 update_temperature(tz);
548 for (count = 0; count < tz->trips; count++)
549 handle_thermal_trip(tz, count);
551 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
553 static void thermal_zone_device_check(struct work_struct *work)
555 struct thermal_zone_device *tz = container_of(work, struct
558 thermal_zone_device_update(tz);
561 /* sys I/F for thermal zone */
563 #define to_thermal_zone(_dev) \
564 container_of(_dev, struct thermal_zone_device, device)
567 type_show(struct device *dev, struct device_attribute *attr, char *buf)
569 struct thermal_zone_device *tz = to_thermal_zone(dev);
571 return sprintf(buf, "%s\n", tz->type);
575 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
577 struct thermal_zone_device *tz = to_thermal_zone(dev);
578 int temperature, ret;
580 ret = thermal_zone_get_temp(tz, &temperature);
585 return sprintf(buf, "%d\n", temperature);
589 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
591 struct thermal_zone_device *tz = to_thermal_zone(dev);
592 enum thermal_device_mode mode;
595 if (!tz->ops->get_mode)
598 result = tz->ops->get_mode(tz, &mode);
602 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
607 mode_store(struct device *dev, struct device_attribute *attr,
608 const char *buf, size_t count)
610 struct thermal_zone_device *tz = to_thermal_zone(dev);
613 if (!tz->ops->set_mode)
616 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
617 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
618 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
619 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
630 trip_point_type_show(struct device *dev, struct device_attribute *attr,
633 struct thermal_zone_device *tz = to_thermal_zone(dev);
634 enum thermal_trip_type type;
637 if (!tz->ops->get_trip_type)
640 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
643 result = tz->ops->get_trip_type(tz, trip, &type);
648 case THERMAL_TRIP_CRITICAL:
649 return sprintf(buf, "critical\n");
650 case THERMAL_TRIP_HOT:
651 return sprintf(buf, "hot\n");
652 case THERMAL_TRIP_PASSIVE:
653 return sprintf(buf, "passive\n");
654 case THERMAL_TRIP_ACTIVE:
655 return sprintf(buf, "active\n");
657 return sprintf(buf, "unknown\n");
662 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
663 const char *buf, size_t count)
665 struct thermal_zone_device *tz = to_thermal_zone(dev);
667 unsigned long temperature;
669 if (!tz->ops->set_trip_temp)
672 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
675 if (kstrtoul(buf, 10, &temperature))
678 ret = tz->ops->set_trip_temp(tz, trip, temperature);
682 thermal_zone_device_update(tz);
688 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
691 struct thermal_zone_device *tz = to_thermal_zone(dev);
695 if (!tz->ops->get_trip_temp)
698 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
701 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
706 return sprintf(buf, "%d\n", temperature);
710 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
713 struct thermal_zone_device *tz = to_thermal_zone(dev);
717 if (!tz->ops->set_trip_hyst)
720 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
723 if (kstrtoint(buf, 10, &temperature))
727 * We are not doing any check on the 'temperature' value
728 * here. The driver implementing 'set_trip_hyst' has to
731 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
733 return ret ? ret : count;
737 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
740 struct thermal_zone_device *tz = to_thermal_zone(dev);
744 if (!tz->ops->get_trip_hyst)
747 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
750 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
752 return ret ? ret : sprintf(buf, "%d\n", temperature);
756 passive_store(struct device *dev, struct device_attribute *attr,
757 const char *buf, size_t count)
759 struct thermal_zone_device *tz = to_thermal_zone(dev);
760 struct thermal_cooling_device *cdev = NULL;
763 if (!sscanf(buf, "%d\n", &state))
766 /* sanity check: values below 1000 millicelcius don't make sense
767 * and can cause the system to go into a thermal heart attack
769 if (state && state < 1000)
772 if (state && !tz->forced_passive) {
773 mutex_lock(&thermal_list_lock);
774 list_for_each_entry(cdev, &thermal_cdev_list, node) {
775 if (!strncmp("Processor", cdev->type,
776 sizeof("Processor")))
777 thermal_zone_bind_cooling_device(tz,
778 THERMAL_TRIPS_NONE, cdev,
781 THERMAL_WEIGHT_DEFAULT);
783 mutex_unlock(&thermal_list_lock);
784 if (!tz->passive_delay)
785 tz->passive_delay = 1000;
786 } else if (!state && tz->forced_passive) {
787 mutex_lock(&thermal_list_lock);
788 list_for_each_entry(cdev, &thermal_cdev_list, node) {
789 if (!strncmp("Processor", cdev->type,
790 sizeof("Processor")))
791 thermal_zone_unbind_cooling_device(tz,
795 mutex_unlock(&thermal_list_lock);
796 tz->passive_delay = 0;
799 tz->forced_passive = state;
801 thermal_zone_device_update(tz);
807 passive_show(struct device *dev, struct device_attribute *attr,
810 struct thermal_zone_device *tz = to_thermal_zone(dev);
812 return sprintf(buf, "%d\n", tz->forced_passive);
816 policy_store(struct device *dev, struct device_attribute *attr,
817 const char *buf, size_t count)
820 struct thermal_zone_device *tz = to_thermal_zone(dev);
821 struct thermal_governor *gov;
822 char name[THERMAL_NAME_LENGTH];
824 snprintf(name, sizeof(name), "%s", buf);
826 mutex_lock(&thermal_governor_lock);
827 mutex_lock(&tz->lock);
829 gov = __find_governor(strim(name));
833 ret = thermal_set_governor(tz, gov);
838 mutex_unlock(&tz->lock);
839 mutex_unlock(&thermal_governor_lock);
844 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
846 struct thermal_zone_device *tz = to_thermal_zone(dev);
848 return sprintf(buf, "%s\n", tz->governor->name);
852 available_policies_show(struct device *dev, struct device_attribute *devattr,
855 struct thermal_governor *pos;
857 ssize_t size = PAGE_SIZE;
859 mutex_lock(&thermal_governor_lock);
861 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
862 size = PAGE_SIZE - count;
863 count += scnprintf(buf + count, size, "%s ", pos->name);
865 count += scnprintf(buf + count, size, "\n");
867 mutex_unlock(&thermal_governor_lock);
873 emul_temp_store(struct device *dev, struct device_attribute *attr,
874 const char *buf, size_t count)
876 struct thermal_zone_device *tz = to_thermal_zone(dev);
878 unsigned long temperature;
880 if (kstrtoul(buf, 10, &temperature))
883 if (!tz->ops->set_emul_temp) {
884 mutex_lock(&tz->lock);
885 tz->emul_temperature = temperature;
886 mutex_unlock(&tz->lock);
888 ret = tz->ops->set_emul_temp(tz, temperature);
892 thermal_zone_device_update(tz);
894 return ret ? ret : count;
896 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
899 sustainable_power_show(struct device *dev, struct device_attribute *devattr,
902 struct thermal_zone_device *tz = to_thermal_zone(dev);
905 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
911 sustainable_power_store(struct device *dev, struct device_attribute *devattr,
912 const char *buf, size_t count)
914 struct thermal_zone_device *tz = to_thermal_zone(dev);
915 u32 sustainable_power;
920 if (kstrtou32(buf, 10, &sustainable_power))
923 tz->tzp->sustainable_power = sustainable_power;
927 static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
928 sustainable_power_store);
930 #define create_s32_tzp_attr(name) \
932 name##_show(struct device *dev, struct device_attribute *devattr, \
935 struct thermal_zone_device *tz = to_thermal_zone(dev); \
938 return sprintf(buf, "%u\n", tz->tzp->name); \
944 name##_store(struct device *dev, struct device_attribute *devattr, \
945 const char *buf, size_t count) \
947 struct thermal_zone_device *tz = to_thermal_zone(dev); \
953 if (kstrtos32(buf, 10, &value)) \
956 tz->tzp->name = value; \
960 static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
962 create_s32_tzp_attr(k_po);
963 create_s32_tzp_attr(k_pu);
964 create_s32_tzp_attr(k_i);
965 create_s32_tzp_attr(k_d);
966 create_s32_tzp_attr(integral_cutoff);
967 create_s32_tzp_attr(slope);
968 create_s32_tzp_attr(offset);
969 #undef create_s32_tzp_attr
971 static struct device_attribute *dev_tzp_attrs[] = {
972 &dev_attr_sustainable_power,
977 &dev_attr_integral_cutoff,
982 static int create_tzp_attrs(struct device *dev)
986 for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
988 struct device_attribute *dev_attr = dev_tzp_attrs[i];
990 ret = device_create_file(dev, dev_attr);
999 * power_actor_get_max_power() - get the maximum power that a cdev can consume
1000 * @cdev: pointer to &thermal_cooling_device
1001 * @tz: a valid thermal zone device pointer
1002 * @max_power: pointer in which to store the maximum power
1004 * Calculate the maximum power consumption in milliwats that the
1005 * cooling device can currently consume and store it in @max_power.
1007 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1008 * power_actor API or -E* on other error.
1010 int power_actor_get_max_power(struct thermal_cooling_device *cdev,
1011 struct thermal_zone_device *tz, u32 *max_power)
1013 if (!cdev_is_power_actor(cdev))
1016 return cdev->ops->state2power(cdev, tz, 0, max_power);
1020 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
1021 * @cdev: pointer to &thermal_cooling_device
1022 * @tz: a valid thermal zone device pointer
1023 * @min_power: pointer in which to store the minimum power
1025 * Calculate the minimum power consumption in milliwatts that the
1026 * cooling device can currently consume and store it in @min_power.
1028 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1029 * power_actor API or -E* on other error.
1031 int power_actor_get_min_power(struct thermal_cooling_device *cdev,
1032 struct thermal_zone_device *tz, u32 *min_power)
1034 unsigned long max_state;
1037 if (!cdev_is_power_actor(cdev))
1040 ret = cdev->ops->get_max_state(cdev, &max_state);
1044 return cdev->ops->state2power(cdev, tz, max_state, min_power);
1048 * power_actor_set_power() - limit the maximum power that a cooling device can consume
1049 * @cdev: pointer to &thermal_cooling_device
1050 * @instance: thermal instance to update
1051 * @power: the power in milliwatts
1053 * Set the cooling device to consume at most @power milliwatts.
1055 * Return: 0 on success, -EINVAL if the cooling device does not
1056 * implement the power actor API or -E* for other failures.
1058 int power_actor_set_power(struct thermal_cooling_device *cdev,
1059 struct thermal_instance *instance, u32 power)
1061 unsigned long state;
1064 if (!cdev_is_power_actor(cdev))
1067 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
1071 instance->target = state;
1072 cdev->updated = false;
1073 thermal_cdev_update(cdev);
1078 static DEVICE_ATTR(type, 0444, type_show, NULL);
1079 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
1080 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
1081 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
1082 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
1083 static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
1085 /* sys I/F for cooling device */
1086 #define to_cooling_device(_dev) \
1087 container_of(_dev, struct thermal_cooling_device, device)
1090 thermal_cooling_device_type_show(struct device *dev,
1091 struct device_attribute *attr, char *buf)
1093 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1095 return sprintf(buf, "%s\n", cdev->type);
1099 thermal_cooling_device_max_state_show(struct device *dev,
1100 struct device_attribute *attr, char *buf)
1102 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1103 unsigned long state;
1106 ret = cdev->ops->get_max_state(cdev, &state);
1109 return sprintf(buf, "%ld\n", state);
1113 thermal_cooling_device_cur_state_show(struct device *dev,
1114 struct device_attribute *attr, char *buf)
1116 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1117 unsigned long state;
1120 ret = cdev->ops->get_cur_state(cdev, &state);
1123 return sprintf(buf, "%ld\n", state);
1127 thermal_cooling_device_cur_state_store(struct device *dev,
1128 struct device_attribute *attr,
1129 const char *buf, size_t count)
1131 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1132 unsigned long state;
1135 if (!sscanf(buf, "%ld\n", &state))
1138 if ((long)state < 0)
1141 result = cdev->ops->set_cur_state(cdev, state);
1147 static struct device_attribute dev_attr_cdev_type =
1148 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
1149 static DEVICE_ATTR(max_state, 0444,
1150 thermal_cooling_device_max_state_show, NULL);
1151 static DEVICE_ATTR(cur_state, 0644,
1152 thermal_cooling_device_cur_state_show,
1153 thermal_cooling_device_cur_state_store);
1156 thermal_cooling_device_trip_point_show(struct device *dev,
1157 struct device_attribute *attr, char *buf)
1159 struct thermal_instance *instance;
1162 container_of(attr, struct thermal_instance, attr);
1164 if (instance->trip == THERMAL_TRIPS_NONE)
1165 return sprintf(buf, "-1\n");
1167 return sprintf(buf, "%d\n", instance->trip);
1170 static struct attribute *cooling_device_attrs[] = {
1171 &dev_attr_cdev_type.attr,
1172 &dev_attr_max_state.attr,
1173 &dev_attr_cur_state.attr,
1177 static const struct attribute_group cooling_device_attr_group = {
1178 .attrs = cooling_device_attrs,
1181 static const struct attribute_group *cooling_device_attr_groups[] = {
1182 &cooling_device_attr_group,
1187 thermal_cooling_device_weight_show(struct device *dev,
1188 struct device_attribute *attr, char *buf)
1190 struct thermal_instance *instance;
1192 instance = container_of(attr, struct thermal_instance, weight_attr);
1194 return sprintf(buf, "%d\n", instance->weight);
1198 thermal_cooling_device_weight_store(struct device *dev,
1199 struct device_attribute *attr,
1200 const char *buf, size_t count)
1202 struct thermal_instance *instance;
1205 ret = kstrtoint(buf, 0, &weight);
1209 instance = container_of(attr, struct thermal_instance, weight_attr);
1210 instance->weight = weight;
1214 /* Device management */
1217 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1218 * @tz: pointer to struct thermal_zone_device
1219 * @trip: indicates which trip point the cooling devices is
1220 * associated with in this thermal zone.
1221 * @cdev: pointer to struct thermal_cooling_device
1222 * @upper: the Maximum cooling state for this trip point.
1223 * THERMAL_NO_LIMIT means no upper limit,
1224 * and the cooling device can be in max_state.
1225 * @lower: the Minimum cooling state can be used for this trip point.
1226 * THERMAL_NO_LIMIT means no lower limit,
1227 * and the cooling device can be in cooling state 0.
1228 * @weight: The weight of the cooling device to be bound to the
1229 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1232 * This interface function bind a thermal cooling device to the certain trip
1233 * point of a thermal zone device.
1234 * This function is usually called in the thermal zone device .bind callback.
1236 * Return: 0 on success, the proper error value otherwise.
1238 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1240 struct thermal_cooling_device *cdev,
1241 unsigned long upper, unsigned long lower,
1242 unsigned int weight)
1244 struct thermal_instance *dev;
1245 struct thermal_instance *pos;
1246 struct thermal_zone_device *pos1;
1247 struct thermal_cooling_device *pos2;
1248 unsigned long max_state;
1251 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1254 list_for_each_entry(pos1, &thermal_tz_list, node) {
1258 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1263 if (tz != pos1 || cdev != pos2)
1266 ret = cdev->ops->get_max_state(cdev, &max_state);
1270 /* lower default 0, upper default max_state */
1271 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1272 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1274 if (lower > upper || upper > max_state)
1278 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1286 dev->target = THERMAL_NO_TARGET;
1287 dev->weight = weight;
1289 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1293 sprintf(dev->name, "cdev%d", dev->id);
1295 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1299 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1300 sysfs_attr_init(&dev->attr.attr);
1301 dev->attr.attr.name = dev->attr_name;
1302 dev->attr.attr.mode = 0444;
1303 dev->attr.show = thermal_cooling_device_trip_point_show;
1304 result = device_create_file(&tz->device, &dev->attr);
1306 goto remove_symbol_link;
1308 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1309 sysfs_attr_init(&dev->weight_attr.attr);
1310 dev->weight_attr.attr.name = dev->weight_attr_name;
1311 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1312 dev->weight_attr.show = thermal_cooling_device_weight_show;
1313 dev->weight_attr.store = thermal_cooling_device_weight_store;
1314 result = device_create_file(&tz->device, &dev->weight_attr);
1316 goto remove_trip_file;
1318 mutex_lock(&tz->lock);
1319 mutex_lock(&cdev->lock);
1320 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1321 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1326 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1327 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1329 mutex_unlock(&cdev->lock);
1330 mutex_unlock(&tz->lock);
1335 device_remove_file(&tz->device, &dev->weight_attr);
1337 device_remove_file(&tz->device, &dev->attr);
1339 sysfs_remove_link(&tz->device.kobj, dev->name);
1341 release_idr(&tz->idr, &tz->lock, dev->id);
1346 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
1349 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1351 * @tz: pointer to a struct thermal_zone_device.
1352 * @trip: indicates which trip point the cooling devices is
1353 * associated with in this thermal zone.
1354 * @cdev: pointer to a struct thermal_cooling_device.
1356 * This interface function unbind a thermal cooling device from the certain
1357 * trip point of a thermal zone device.
1358 * This function is usually called in the thermal zone device .unbind callback.
1360 * Return: 0 on success, the proper error value otherwise.
1362 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1364 struct thermal_cooling_device *cdev)
1366 struct thermal_instance *pos, *next;
1368 mutex_lock(&tz->lock);
1369 mutex_lock(&cdev->lock);
1370 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1371 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1372 list_del(&pos->tz_node);
1373 list_del(&pos->cdev_node);
1374 mutex_unlock(&cdev->lock);
1375 mutex_unlock(&tz->lock);
1379 mutex_unlock(&cdev->lock);
1380 mutex_unlock(&tz->lock);
1385 device_remove_file(&tz->device, &pos->weight_attr);
1386 device_remove_file(&tz->device, &pos->attr);
1387 sysfs_remove_link(&tz->device.kobj, pos->name);
1388 release_idr(&tz->idr, &tz->lock, pos->id);
1392 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1394 static void thermal_release(struct device *dev)
1396 struct thermal_zone_device *tz;
1397 struct thermal_cooling_device *cdev;
1399 if (!strncmp(dev_name(dev), "thermal_zone",
1400 sizeof("thermal_zone") - 1)) {
1401 tz = to_thermal_zone(dev);
1403 } else if(!strncmp(dev_name(dev), "cooling_device",
1404 sizeof("cooling_device") - 1)){
1405 cdev = to_cooling_device(dev);
1410 static struct class thermal_class = {
1412 .dev_release = thermal_release,
1416 * __thermal_cooling_device_register() - register a new thermal cooling device
1417 * @np: a pointer to a device tree node.
1418 * @type: the thermal cooling device type.
1419 * @devdata: device private data.
1420 * @ops: standard thermal cooling devices callbacks.
1422 * This interface function adds a new thermal cooling device (fan/processor/...)
1423 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1424 * to all the thermal zone devices registered at the same time.
1425 * It also gives the opportunity to link the cooling device to a device tree
1426 * node, so that it can be bound to a thermal zone created out of device tree.
1428 * Return: a pointer to the created struct thermal_cooling_device or an
1429 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1431 static struct thermal_cooling_device *
1432 __thermal_cooling_device_register(struct device_node *np,
1433 char *type, void *devdata,
1434 const struct thermal_cooling_device_ops *ops)
1436 struct thermal_cooling_device *cdev;
1439 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1440 return ERR_PTR(-EINVAL);
1442 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1443 !ops->set_cur_state)
1444 return ERR_PTR(-EINVAL);
1446 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1448 return ERR_PTR(-ENOMEM);
1450 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1453 return ERR_PTR(result);
1456 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1457 mutex_init(&cdev->lock);
1458 INIT_LIST_HEAD(&cdev->thermal_instances);
1461 cdev->updated = false;
1462 cdev->device.class = &thermal_class;
1463 cdev->device.groups = cooling_device_attr_groups;
1464 cdev->devdata = devdata;
1465 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1466 result = device_register(&cdev->device);
1468 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1470 return ERR_PTR(result);
1473 /* Add 'this' new cdev to the global cdev list */
1474 mutex_lock(&thermal_list_lock);
1475 list_add(&cdev->node, &thermal_cdev_list);
1476 mutex_unlock(&thermal_list_lock);
1478 /* Update binding information for 'this' new cdev */
1485 * thermal_cooling_device_register() - register a new thermal cooling device
1486 * @type: the thermal cooling device type.
1487 * @devdata: device private data.
1488 * @ops: standard thermal cooling devices callbacks.
1490 * This interface function adds a new thermal cooling device (fan/processor/...)
1491 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1492 * to all the thermal zone devices registered at the same time.
1494 * Return: a pointer to the created struct thermal_cooling_device or an
1495 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1497 struct thermal_cooling_device *
1498 thermal_cooling_device_register(char *type, void *devdata,
1499 const struct thermal_cooling_device_ops *ops)
1501 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1503 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1506 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1507 * @np: a pointer to a device tree node.
1508 * @type: the thermal cooling device type.
1509 * @devdata: device private data.
1510 * @ops: standard thermal cooling devices callbacks.
1512 * This function will register a cooling device with device tree node reference.
1513 * This interface function adds a new thermal cooling device (fan/processor/...)
1514 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1515 * to all the thermal zone devices registered at the same time.
1517 * Return: a pointer to the created struct thermal_cooling_device or an
1518 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1520 struct thermal_cooling_device *
1521 thermal_of_cooling_device_register(struct device_node *np,
1522 char *type, void *devdata,
1523 const struct thermal_cooling_device_ops *ops)
1525 return __thermal_cooling_device_register(np, type, devdata, ops);
1527 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1530 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1531 * @cdev: the thermal cooling device to remove.
1533 * thermal_cooling_device_unregister() must be called when the device is no
1536 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1539 const struct thermal_zone_params *tzp;
1540 struct thermal_zone_device *tz;
1541 struct thermal_cooling_device *pos = NULL;
1546 mutex_lock(&thermal_list_lock);
1547 list_for_each_entry(pos, &thermal_cdev_list, node)
1551 /* thermal cooling device not found */
1552 mutex_unlock(&thermal_list_lock);
1555 list_del(&cdev->node);
1557 /* Unbind all thermal zones associated with 'this' cdev */
1558 list_for_each_entry(tz, &thermal_tz_list, node) {
1559 if (tz->ops->unbind) {
1560 tz->ops->unbind(tz, cdev);
1564 if (!tz->tzp || !tz->tzp->tbp)
1568 for (i = 0; i < tzp->num_tbps; i++) {
1569 if (tzp->tbp[i].cdev == cdev) {
1570 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1571 tzp->tbp[i].cdev = NULL;
1576 mutex_unlock(&thermal_list_lock);
1579 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1580 device_remove_file(&cdev->device, &dev_attr_max_state);
1581 device_remove_file(&cdev->device, &dev_attr_cur_state);
1583 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1584 device_unregister(&cdev->device);
1587 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1589 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1591 struct thermal_instance *instance;
1592 unsigned long target = 0;
1594 /* cooling device is updated*/
1598 mutex_lock(&cdev->lock);
1599 /* Make sure cdev enters the deepest cooling state */
1600 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1601 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1602 instance->tz->id, instance->target);
1603 if (instance->target == THERMAL_NO_TARGET)
1605 if (instance->target > target)
1606 target = instance->target;
1608 mutex_unlock(&cdev->lock);
1609 cdev->ops->set_cur_state(cdev, target);
1610 cdev->updated = true;
1611 trace_cdev_update(cdev, target);
1612 dev_dbg(&cdev->device, "set to state %lu\n", target);
1614 EXPORT_SYMBOL(thermal_cdev_update);
1617 * thermal_notify_framework - Sensor drivers use this API to notify framework
1618 * @tz: thermal zone device
1619 * @trip: indicates which trip point has been crossed
1621 * This function handles the trip events from sensor drivers. It starts
1622 * throttling the cooling devices according to the policy configured.
1623 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1624 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1625 * The throttling policy is based on the configured platform data; if no
1626 * platform data is provided, this uses the step_wise throttling policy.
1628 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1630 handle_thermal_trip(tz, trip);
1632 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1635 * create_trip_attrs() - create attributes for trip points
1636 * @tz: the thermal zone device
1637 * @mask: Writeable trip point bitmap.
1639 * helper function to instantiate sysfs entries for every trip
1640 * point and its properties of a struct thermal_zone_device.
1642 * Return: 0 on success, the proper error value otherwise.
1644 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1647 int size = sizeof(struct thermal_attr) * tz->trips;
1649 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1650 if (!tz->trip_type_attrs)
1653 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1654 if (!tz->trip_temp_attrs) {
1655 kfree(tz->trip_type_attrs);
1659 if (tz->ops->get_trip_hyst) {
1660 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1661 if (!tz->trip_hyst_attrs) {
1662 kfree(tz->trip_type_attrs);
1663 kfree(tz->trip_temp_attrs);
1669 for (indx = 0; indx < tz->trips; indx++) {
1670 /* create trip type attribute */
1671 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1672 "trip_point_%d_type", indx);
1674 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1675 tz->trip_type_attrs[indx].attr.attr.name =
1676 tz->trip_type_attrs[indx].name;
1677 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1678 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1680 device_create_file(&tz->device,
1681 &tz->trip_type_attrs[indx].attr);
1683 /* create trip temp attribute */
1684 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1685 "trip_point_%d_temp", indx);
1687 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1688 tz->trip_temp_attrs[indx].attr.attr.name =
1689 tz->trip_temp_attrs[indx].name;
1690 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1691 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1692 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
1693 mask & (1 << indx)) {
1694 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1695 tz->trip_temp_attrs[indx].attr.store =
1696 trip_point_temp_store;
1699 device_create_file(&tz->device,
1700 &tz->trip_temp_attrs[indx].attr);
1702 /* create Optional trip hyst attribute */
1703 if (!tz->ops->get_trip_hyst)
1705 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1706 "trip_point_%d_hyst", indx);
1708 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1709 tz->trip_hyst_attrs[indx].attr.attr.name =
1710 tz->trip_hyst_attrs[indx].name;
1711 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1712 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1713 if (tz->ops->set_trip_hyst) {
1714 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1715 tz->trip_hyst_attrs[indx].attr.store =
1716 trip_point_hyst_store;
1719 device_create_file(&tz->device,
1720 &tz->trip_hyst_attrs[indx].attr);
1725 static void remove_trip_attrs(struct thermal_zone_device *tz)
1729 for (indx = 0; indx < tz->trips; indx++) {
1730 device_remove_file(&tz->device,
1731 &tz->trip_type_attrs[indx].attr);
1732 device_remove_file(&tz->device,
1733 &tz->trip_temp_attrs[indx].attr);
1734 if (tz->ops->get_trip_hyst)
1735 device_remove_file(&tz->device,
1736 &tz->trip_hyst_attrs[indx].attr);
1738 kfree(tz->trip_type_attrs);
1739 kfree(tz->trip_temp_attrs);
1740 kfree(tz->trip_hyst_attrs);
1744 * thermal_zone_device_register() - register a new thermal zone device
1745 * @type: the thermal zone device type
1746 * @trips: the number of trip points the thermal zone support
1747 * @mask: a bit string indicating the writeablility of trip points
1748 * @devdata: private device data
1749 * @ops: standard thermal zone device callbacks
1750 * @tzp: thermal zone platform parameters
1751 * @passive_delay: number of milliseconds to wait between polls when
1752 * performing passive cooling
1753 * @polling_delay: number of milliseconds to wait between polls when checking
1754 * whether trip points have been crossed (0 for interrupt
1757 * This interface function adds a new thermal zone device (sensor) to
1758 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1759 * thermal cooling devices registered at the same time.
1760 * thermal_zone_device_unregister() must be called when the device is no
1761 * longer needed. The passive cooling depends on the .get_trend() return value.
1763 * Return: a pointer to the created struct thermal_zone_device or an
1764 * in case of error, an ERR_PTR. Caller must check return value with
1765 * IS_ERR*() helpers.
1767 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1768 int trips, int mask, void *devdata,
1769 struct thermal_zone_device_ops *ops,
1770 struct thermal_zone_params *tzp,
1771 int passive_delay, int polling_delay)
1773 struct thermal_zone_device *tz;
1774 enum thermal_trip_type trip_type;
1778 struct thermal_governor *governor;
1780 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1781 return ERR_PTR(-EINVAL);
1783 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1784 return ERR_PTR(-EINVAL);
1787 return ERR_PTR(-EINVAL);
1789 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1790 return ERR_PTR(-EINVAL);
1792 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1794 return ERR_PTR(-ENOMEM);
1796 INIT_LIST_HEAD(&tz->thermal_instances);
1798 mutex_init(&tz->lock);
1799 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1802 return ERR_PTR(result);
1805 strlcpy(tz->type, type ? : "", sizeof(tz->type));
1808 tz->device.class = &thermal_class;
1809 tz->devdata = devdata;
1811 tz->passive_delay = passive_delay;
1812 tz->polling_delay = polling_delay;
1814 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1815 result = device_register(&tz->device);
1817 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1819 return ERR_PTR(result);
1824 result = device_create_file(&tz->device, &dev_attr_type);
1829 result = device_create_file(&tz->device, &dev_attr_temp);
1833 if (ops->get_mode) {
1834 result = device_create_file(&tz->device, &dev_attr_mode);
1839 result = create_trip_attrs(tz, mask);
1843 for (count = 0; count < trips; count++) {
1844 tz->ops->get_trip_type(tz, count, &trip_type);
1845 if (trip_type == THERMAL_TRIP_PASSIVE)
1850 result = device_create_file(&tz->device, &dev_attr_passive);
1855 if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
1856 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1861 /* Create policy attribute */
1862 result = device_create_file(&tz->device, &dev_attr_policy);
1866 /* Add thermal zone params */
1867 result = create_tzp_attrs(&tz->device);
1871 /* Create available_policies attribute */
1872 result = device_create_file(&tz->device, &dev_attr_available_policies);
1876 /* Update 'this' zone's governor information */
1877 mutex_lock(&thermal_governor_lock);
1880 governor = __find_governor(tz->tzp->governor_name);
1882 governor = def_governor;
1884 result = thermal_set_governor(tz, governor);
1886 mutex_unlock(&thermal_governor_lock);
1890 mutex_unlock(&thermal_governor_lock);
1892 if (!tz->tzp || !tz->tzp->no_hwmon) {
1893 result = thermal_add_hwmon_sysfs(tz);
1898 mutex_lock(&thermal_list_lock);
1899 list_add_tail(&tz->node, &thermal_tz_list);
1900 mutex_unlock(&thermal_list_lock);
1902 /* Bind cooling devices for this zone */
1905 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1907 thermal_zone_device_update(tz);
1912 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1913 device_unregister(&tz->device);
1914 return ERR_PTR(result);
1916 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1919 * thermal_device_unregister - removes the registered thermal zone device
1920 * @tz: the thermal zone device to remove
1922 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1925 const struct thermal_zone_params *tzp;
1926 struct thermal_cooling_device *cdev;
1927 struct thermal_zone_device *pos = NULL;
1934 mutex_lock(&thermal_list_lock);
1935 list_for_each_entry(pos, &thermal_tz_list, node)
1939 /* thermal zone device not found */
1940 mutex_unlock(&thermal_list_lock);
1943 list_del(&tz->node);
1945 /* Unbind all cdevs associated with 'this' thermal zone */
1946 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1947 if (tz->ops->unbind) {
1948 tz->ops->unbind(tz, cdev);
1952 if (!tzp || !tzp->tbp)
1955 for (i = 0; i < tzp->num_tbps; i++) {
1956 if (tzp->tbp[i].cdev == cdev) {
1957 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1958 tzp->tbp[i].cdev = NULL;
1963 mutex_unlock(&thermal_list_lock);
1965 thermal_zone_device_set_polling(tz, 0);
1968 device_remove_file(&tz->device, &dev_attr_type);
1969 device_remove_file(&tz->device, &dev_attr_temp);
1970 if (tz->ops->get_mode)
1971 device_remove_file(&tz->device, &dev_attr_mode);
1972 device_remove_file(&tz->device, &dev_attr_policy);
1973 device_remove_file(&tz->device, &dev_attr_available_policies);
1974 remove_trip_attrs(tz);
1975 thermal_set_governor(tz, NULL);
1977 thermal_remove_hwmon_sysfs(tz);
1978 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1979 idr_destroy(&tz->idr);
1980 mutex_destroy(&tz->lock);
1981 device_unregister(&tz->device);
1984 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1987 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1988 * @name: thermal zone name to fetch the temperature
1990 * When only one zone is found with the passed name, returns a reference to it.
1992 * Return: On success returns a reference to an unique thermal zone with
1993 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1994 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1996 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1998 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1999 unsigned int found = 0;
2004 mutex_lock(&thermal_list_lock);
2005 list_for_each_entry(pos, &thermal_tz_list, node)
2006 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
2010 mutex_unlock(&thermal_list_lock);
2012 /* nothing has been found, thus an error code for it */
2014 ref = ERR_PTR(-ENODEV);
2016 /* Success only when an unique zone is found */
2017 ref = ERR_PTR(-EEXIST);
2022 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
2025 static const struct genl_multicast_group thermal_event_mcgrps[] = {
2026 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
2029 static struct genl_family thermal_event_genl_family = {
2030 .id = GENL_ID_GENERATE,
2031 .name = THERMAL_GENL_FAMILY_NAME,
2032 .version = THERMAL_GENL_VERSION,
2033 .maxattr = THERMAL_GENL_ATTR_MAX,
2034 .mcgrps = thermal_event_mcgrps,
2035 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
2038 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2041 struct sk_buff *skb;
2042 struct nlattr *attr;
2043 struct thermal_genl_event *thermal_event;
2047 static unsigned int thermal_event_seqnum;
2052 /* allocate memory */
2053 size = nla_total_size(sizeof(struct thermal_genl_event)) +
2056 skb = genlmsg_new(size, GFP_ATOMIC);
2060 /* add the genetlink message header */
2061 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2062 &thermal_event_genl_family, 0,
2063 THERMAL_GENL_CMD_EVENT);
2070 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2071 sizeof(struct thermal_genl_event));
2078 thermal_event = nla_data(attr);
2079 if (!thermal_event) {
2084 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2086 thermal_event->orig = tz->id;
2087 thermal_event->event = event;
2089 /* send multicast genetlink message */
2090 genlmsg_end(skb, msg_header);
2092 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
2095 dev_err(&tz->device, "Failed to send netlink event:%d", result);
2099 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
2101 static int genetlink_init(void)
2103 return genl_register_family(&thermal_event_genl_family);
2106 static void genetlink_exit(void)
2108 genl_unregister_family(&thermal_event_genl_family);
2110 #else /* !CONFIG_NET */
2111 static inline int genetlink_init(void) { return 0; }
2112 static inline void genetlink_exit(void) {}
2113 #endif /* !CONFIG_NET */
2115 static int __init thermal_register_governors(void)
2119 result = thermal_gov_step_wise_register();
2123 result = thermal_gov_fair_share_register();
2127 result = thermal_gov_bang_bang_register();
2131 result = thermal_gov_user_space_register();
2135 return thermal_gov_power_allocator_register();
2138 static void thermal_unregister_governors(void)
2140 thermal_gov_step_wise_unregister();
2141 thermal_gov_fair_share_unregister();
2142 thermal_gov_bang_bang_unregister();
2143 thermal_gov_user_space_unregister();
2144 thermal_gov_power_allocator_unregister();
2147 static int __init thermal_init(void)
2151 result = thermal_register_governors();
2155 result = class_register(&thermal_class);
2157 goto unregister_governors;
2159 result = genetlink_init();
2161 goto unregister_class;
2163 result = of_parse_thermal_zones();
2172 class_unregister(&thermal_class);
2173 unregister_governors:
2174 thermal_unregister_governors();
2176 idr_destroy(&thermal_tz_idr);
2177 idr_destroy(&thermal_cdev_idr);
2178 mutex_destroy(&thermal_idr_lock);
2179 mutex_destroy(&thermal_list_lock);
2180 mutex_destroy(&thermal_governor_lock);
2184 static void __exit thermal_exit(void)
2186 of_thermal_destroy_zones();
2188 class_unregister(&thermal_class);
2189 thermal_unregister_governors();
2190 idr_destroy(&thermal_tz_idr);
2191 idr_destroy(&thermal_cdev_idr);
2192 mutex_destroy(&thermal_idr_lock);
2193 mutex_destroy(&thermal_list_lock);
2194 mutex_destroy(&thermal_governor_lock);
2197 fs_initcall(thermal_init);
2198 module_exit(thermal_exit);