2 * thermal.c - Generic Thermal Management Sysfs support.
4 * Copyright (C) 2008 Intel Corp
5 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
6 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/slab.h>
32 #include <linux/kdev_t.h>
33 #include <linux/idr.h>
34 #include <linux/thermal.h>
35 #include <linux/reboot.h>
36 #include <linux/string.h>
38 #include <net/netlink.h>
39 #include <net/genetlink.h>
40 #define CREATE_TRACE_POINTS
41 #include <trace/events/thermal.h>
43 #include "thermal_core.h"
44 #include "thermal_hwmon.h"
46 MODULE_AUTHOR("Zhang Rui");
47 MODULE_DESCRIPTION("Generic thermal management sysfs support");
48 MODULE_LICENSE("GPL v2");
50 static DEFINE_IDR(thermal_tz_idr);
51 static DEFINE_IDR(thermal_cdev_idr);
52 static DEFINE_MUTEX(thermal_idr_lock);
54 static LIST_HEAD(thermal_tz_list);
55 static LIST_HEAD(thermal_cdev_list);
56 static LIST_HEAD(thermal_governor_list);
58 static DEFINE_MUTEX(thermal_list_lock);
59 static DEFINE_MUTEX(thermal_governor_lock);
61 static struct thermal_governor *__find_governor(const char *name)
63 struct thermal_governor *pos;
65 list_for_each_entry(pos, &thermal_governor_list, governor_list)
66 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
72 int thermal_register_governor(struct thermal_governor *governor)
76 struct thermal_zone_device *pos;
81 mutex_lock(&thermal_governor_lock);
84 if (__find_governor(governor->name) == NULL) {
86 list_add(&governor->governor_list, &thermal_governor_list);
89 mutex_lock(&thermal_list_lock);
91 list_for_each_entry(pos, &thermal_tz_list, node) {
95 name = pos->tzp->governor_name;
97 name = DEFAULT_THERMAL_GOVERNOR;
98 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH)) {
99 if (governor->start) {
100 err = governor->start(pos);
104 pos->governor = governor;
109 mutex_unlock(&thermal_list_lock);
110 mutex_unlock(&thermal_governor_lock);
115 void thermal_unregister_governor(struct thermal_governor *governor)
117 struct thermal_zone_device *pos;
122 mutex_lock(&thermal_governor_lock);
124 if (__find_governor(governor->name) == NULL)
127 mutex_lock(&thermal_list_lock);
129 list_for_each_entry(pos, &thermal_tz_list, node) {
130 if (!strnicmp(pos->governor->name, governor->name,
131 THERMAL_NAME_LENGTH)) {
132 if (pos->governor->stop)
133 pos->governor->stop(pos);
134 pos->governor = NULL;
138 mutex_unlock(&thermal_list_lock);
139 list_del(&governor->governor_list);
141 mutex_unlock(&thermal_governor_lock);
145 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
151 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
154 if (unlikely(ret < 0))
160 static void release_idr(struct idr *idr, struct mutex *lock, int id)
169 int get_tz_trend(struct thermal_zone_device *tz, int trip)
171 enum thermal_trend trend;
173 if (tz->emul_temperature || !tz->ops->get_trend ||
174 tz->ops->get_trend(tz, trip, &trend)) {
175 if (tz->temperature > tz->last_temperature)
176 trend = THERMAL_TREND_RAISING;
177 else if (tz->temperature < tz->last_temperature)
178 trend = THERMAL_TREND_DROPPING;
180 trend = THERMAL_TREND_STABLE;
185 EXPORT_SYMBOL(get_tz_trend);
187 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
188 struct thermal_cooling_device *cdev, int trip)
190 struct thermal_instance *pos = NULL;
191 struct thermal_instance *target_instance = NULL;
193 mutex_lock(&tz->lock);
194 mutex_lock(&cdev->lock);
196 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
197 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
198 target_instance = pos;
203 mutex_unlock(&cdev->lock);
204 mutex_unlock(&tz->lock);
206 return target_instance;
208 EXPORT_SYMBOL(get_thermal_instance);
210 static void print_bind_err_msg(struct thermal_zone_device *tz,
211 struct thermal_cooling_device *cdev, int ret)
213 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
214 tz->type, cdev->type, ret);
217 static void __bind(struct thermal_zone_device *tz, u64 mask,
218 struct thermal_cooling_device *cdev,
219 unsigned long *limits)
223 for (i = 0; i < tz->trips; i++) {
224 if (mask & (1ULL << i)) {
225 unsigned long upper, lower;
227 upper = THERMAL_NO_LIMIT;
228 lower = THERMAL_NO_LIMIT;
230 lower = limits[i * 2];
231 upper = limits[i * 2 + 1];
233 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
236 print_bind_err_msg(tz, cdev, ret);
241 static void __unbind(struct thermal_zone_device *tz, u64 mask,
242 struct thermal_cooling_device *cdev)
246 for (i = 0; i < tz->trips; i++)
247 if (mask & (1ULL << i))
248 thermal_zone_unbind_cooling_device(tz, i, cdev);
251 static void bind_cdev(struct thermal_cooling_device *cdev)
254 const struct thermal_zone_params *tzp;
255 struct thermal_zone_device *pos = NULL;
257 mutex_lock(&thermal_list_lock);
259 list_for_each_entry(pos, &thermal_tz_list, node) {
260 if (!pos->tzp && !pos->ops->bind)
263 if (pos->ops->bind) {
264 ret = pos->ops->bind(pos, cdev);
266 print_bind_err_msg(pos, cdev, ret);
271 if (!tzp || !tzp->tbp)
274 for (i = 0; i < tzp->num_tbps; i++) {
275 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
277 if (tzp->tbp[i].match(pos, cdev))
279 tzp->tbp[i].cdev = cdev;
280 __bind(pos, tzp->tbp[i].trip_mask, cdev,
281 tzp->tbp[i].binding_limits);
285 mutex_unlock(&thermal_list_lock);
288 static void bind_tz(struct thermal_zone_device *tz)
291 struct thermal_cooling_device *pos = NULL;
292 const struct thermal_zone_params *tzp = tz->tzp;
294 if (!tzp && !tz->ops->bind)
297 mutex_lock(&thermal_list_lock);
299 /* If there is ops->bind, try to use ops->bind */
301 list_for_each_entry(pos, &thermal_cdev_list, node) {
302 ret = tz->ops->bind(tz, pos);
304 print_bind_err_msg(tz, pos, ret);
309 if (!tzp || !tzp->tbp)
312 list_for_each_entry(pos, &thermal_cdev_list, node) {
313 for (i = 0; i < tzp->num_tbps; i++) {
314 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
316 if (tzp->tbp[i].match(tz, pos))
318 tzp->tbp[i].cdev = pos;
319 __bind(tz, tzp->tbp[i].trip_mask, pos,
320 tzp->tbp[i].binding_limits);
324 mutex_unlock(&thermal_list_lock);
327 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
331 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
332 round_jiffies(msecs_to_jiffies(delay)));
334 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
335 msecs_to_jiffies(delay));
337 cancel_delayed_work(&tz->poll_queue);
340 static void monitor_thermal_zone(struct thermal_zone_device *tz)
342 mutex_lock(&tz->lock);
345 thermal_zone_device_set_polling(tz, tz->passive_delay);
346 else if (tz->polling_delay)
347 thermal_zone_device_set_polling(tz, tz->polling_delay);
349 thermal_zone_device_set_polling(tz, 0);
351 mutex_unlock(&tz->lock);
354 static void handle_non_critical_trips(struct thermal_zone_device *tz,
355 int trip, enum thermal_trip_type trip_type)
358 tz->governor->throttle(tz, trip);
361 static void handle_critical_trips(struct thermal_zone_device *tz,
362 int trip, enum thermal_trip_type trip_type)
366 tz->ops->get_trip_temp(tz, trip, &trip_temp);
368 /* If we have not crossed the trip_temp, we do not care. */
369 if (tz->temperature < trip_temp)
373 tz->ops->notify(tz, trip, trip_type);
375 if (trip_type == THERMAL_TRIP_CRITICAL) {
376 dev_emerg(&tz->device,
377 "critical temperature reached(%d C),shutting down\n",
378 tz->temperature / 1000);
379 orderly_poweroff(true);
383 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
385 enum thermal_trip_type type;
387 trace_thermal_trip(tz->type, tz->temperature/1000);
389 tz->ops->get_trip_type(tz, trip, &type);
391 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
392 handle_critical_trips(tz, trip, type);
394 handle_non_critical_trips(tz, trip, type);
396 * Alright, we handled this trip successfully.
397 * So, start monitoring again.
399 monitor_thermal_zone(tz);
403 * thermal_zone_get_temp() - returns its the temperature of thermal zone
404 * @tz: a valid pointer to a struct thermal_zone_device
405 * @temp: a valid pointer to where to store the resulting temperature.
407 * When a valid thermal zone reference is passed, it will fetch its
408 * temperature and fill @temp.
410 * Return: On success returns 0, an error code otherwise
412 int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
415 #ifdef CONFIG_THERMAL_EMULATION
417 unsigned long crit_temp = -1UL;
418 enum thermal_trip_type type;
421 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
424 mutex_lock(&tz->lock);
426 ret = tz->ops->get_temp(tz, temp);
427 #ifdef CONFIG_THERMAL_EMULATION
428 if (!tz->emul_temperature)
431 for (count = 0; count < tz->trips; count++) {
432 ret = tz->ops->get_trip_type(tz, count, &type);
433 if (!ret && type == THERMAL_TRIP_CRITICAL) {
434 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
442 if (*temp < crit_temp)
443 *temp = tz->emul_temperature;
446 mutex_unlock(&tz->lock);
450 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
452 static void update_temperature(struct thermal_zone_device *tz)
457 ret = thermal_zone_get_temp(tz, &temp);
459 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
464 mutex_lock(&tz->lock);
465 tz->last_temperature = tz->temperature;
466 tz->temperature = temp;
467 mutex_unlock(&tz->lock);
470 void thermal_zone_device_update(struct thermal_zone_device *tz)
474 if (!tz->ops->get_temp)
477 update_temperature(tz);
479 for (count = 0; count < tz->trips; count++)
480 handle_thermal_trip(tz, count);
482 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
484 static void thermal_zone_device_check(struct work_struct *work)
486 struct thermal_zone_device *tz = container_of(work, struct
489 thermal_zone_device_update(tz);
492 /* sys I/F for thermal zone */
494 #define to_thermal_zone(_dev) \
495 container_of(_dev, struct thermal_zone_device, device)
498 type_show(struct device *dev, struct device_attribute *attr, char *buf)
500 struct thermal_zone_device *tz = to_thermal_zone(dev);
502 return sprintf(buf, "%s\n", tz->type);
506 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
508 struct thermal_zone_device *tz = to_thermal_zone(dev);
512 ret = thermal_zone_get_temp(tz, &temperature);
517 return sprintf(buf, "%ld\n", temperature);
521 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
523 struct thermal_zone_device *tz = to_thermal_zone(dev);
524 enum thermal_device_mode mode;
527 if (!tz->ops->get_mode)
530 result = tz->ops->get_mode(tz, &mode);
534 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
539 mode_store(struct device *dev, struct device_attribute *attr,
540 const char *buf, size_t count)
542 struct thermal_zone_device *tz = to_thermal_zone(dev);
545 if (!tz->ops->set_mode)
548 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
549 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
550 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
551 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
562 trip_point_type_show(struct device *dev, struct device_attribute *attr,
565 struct thermal_zone_device *tz = to_thermal_zone(dev);
566 enum thermal_trip_type type;
569 if (!tz->ops->get_trip_type)
572 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
575 result = tz->ops->get_trip_type(tz, trip, &type);
580 case THERMAL_TRIP_CRITICAL:
581 return sprintf(buf, "critical\n");
582 case THERMAL_TRIP_HOT:
583 return sprintf(buf, "hot\n");
584 case THERMAL_TRIP_PASSIVE:
585 return sprintf(buf, "passive\n");
586 case THERMAL_TRIP_ACTIVE:
587 return sprintf(buf, "active\n");
589 return sprintf(buf, "unknown\n");
594 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
595 const char *buf, size_t count)
597 struct thermal_zone_device *tz = to_thermal_zone(dev);
599 unsigned long temperature;
601 if (!tz->ops->set_trip_temp)
604 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
607 if (kstrtoul(buf, 10, &temperature))
610 ret = tz->ops->set_trip_temp(tz, trip, temperature);
612 return ret ? ret : count;
616 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
619 struct thermal_zone_device *tz = to_thermal_zone(dev);
623 if (!tz->ops->get_trip_temp)
626 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
629 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
634 return sprintf(buf, "%ld\n", temperature);
638 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
639 const char *buf, size_t count)
641 struct thermal_zone_device *tz = to_thermal_zone(dev);
643 unsigned long temperature;
645 if (!tz->ops->set_trip_hyst)
648 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
651 if (kstrtoul(buf, 10, &temperature))
655 * We are not doing any check on the 'temperature' value
656 * here. The driver implementing 'set_trip_hyst' has to
659 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
661 return ret ? ret : count;
665 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
668 struct thermal_zone_device *tz = to_thermal_zone(dev);
670 unsigned long temperature;
672 if (!tz->ops->get_trip_hyst)
675 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
678 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
680 return ret ? ret : sprintf(buf, "%ld\n", temperature);
684 passive_store(struct device *dev, struct device_attribute *attr,
685 const char *buf, size_t count)
687 struct thermal_zone_device *tz = to_thermal_zone(dev);
688 struct thermal_cooling_device *cdev = NULL;
691 if (!sscanf(buf, "%d\n", &state))
694 /* sanity check: values below 1000 millicelcius don't make sense
695 * and can cause the system to go into a thermal heart attack
697 if (state && state < 1000)
700 if (state && !tz->forced_passive) {
701 mutex_lock(&thermal_list_lock);
702 list_for_each_entry(cdev, &thermal_cdev_list, node) {
703 if (!strncmp("Processor", cdev->type,
704 sizeof("Processor")))
705 thermal_zone_bind_cooling_device(tz,
706 THERMAL_TRIPS_NONE, cdev,
710 mutex_unlock(&thermal_list_lock);
711 if (!tz->passive_delay)
712 tz->passive_delay = 1000;
713 } else if (!state && tz->forced_passive) {
714 mutex_lock(&thermal_list_lock);
715 list_for_each_entry(cdev, &thermal_cdev_list, node) {
716 if (!strncmp("Processor", cdev->type,
717 sizeof("Processor")))
718 thermal_zone_unbind_cooling_device(tz,
722 mutex_unlock(&thermal_list_lock);
723 tz->passive_delay = 0;
726 tz->forced_passive = state;
728 thermal_zone_device_update(tz);
734 passive_show(struct device *dev, struct device_attribute *attr,
737 struct thermal_zone_device *tz = to_thermal_zone(dev);
739 return sprintf(buf, "%d\n", tz->forced_passive);
743 passive_delay_store(struct device *dev, struct device_attribute *attr,
744 const char *buf, size_t count)
746 struct thermal_zone_device *tz = to_thermal_zone(dev);
749 if (!sscanf(buf, "%d\n", &passive_delay))
752 tz->passive_delay = passive_delay;
753 thermal_zone_device_update(tz);
758 passive_delay_show(struct device *dev, struct device_attribute *attr,
761 struct thermal_zone_device *tz = to_thermal_zone(dev);
763 return sprintf(buf, "%d\n", tz->passive_delay);
767 polling_delay_store(struct device *dev, struct device_attribute *attr,
768 const char *buf, size_t count)
770 struct thermal_zone_device *tz = to_thermal_zone(dev);
773 if (!sscanf(buf, "%d\n", &polling_delay))
776 tz->polling_delay = polling_delay;
777 thermal_zone_device_update(tz);
782 polling_delay_show(struct device *dev, struct device_attribute *attr,
785 struct thermal_zone_device *tz = to_thermal_zone(dev);
787 return sprintf(buf, "%d\n", tz->polling_delay);
791 policy_store(struct device *dev, struct device_attribute *attr,
792 const char *buf, size_t count)
795 struct thermal_zone_device *tz = to_thermal_zone(dev);
796 struct thermal_governor *gov;
797 char name[THERMAL_NAME_LENGTH];
799 snprintf(name, sizeof(name), "%s", buf);
801 mutex_lock(&thermal_governor_lock);
803 if ((strlen(buf) >= THERMAL_NAME_LENGTH) || !sscanf(buf, "%s\n", name))
806 gov = __find_governor((const char *)strim(name));
810 if (gov == tz->governor) {
815 if (tz->governor && tz->governor->stop)
816 tz->governor->stop(tz);
819 ret = gov->start(tz);
821 if (tz->governor && tz->governor->start)
822 tz->governor->start(tz);
831 mutex_unlock(&thermal_governor_lock);
836 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
838 struct thermal_zone_device *tz = to_thermal_zone(dev);
840 return sprintf(buf, "%s\n", tz->governor->name);
844 available_policies_show(struct device *dev, struct device_attribute *devattr,
847 struct thermal_governor *pos;
850 mutex_lock(&thermal_governor_lock);
852 list_for_each_entry(pos, &thermal_governor_list, governor_list)
853 count += sprintf(buf + count, "%s ", pos->name);
854 count += sprintf(buf + count, "\n");
856 mutex_unlock(&thermal_governor_lock);
860 #ifdef CONFIG_THERMAL_EMULATION
862 emul_temp_store(struct device *dev, struct device_attribute *attr,
863 const char *buf, size_t count)
865 struct thermal_zone_device *tz = to_thermal_zone(dev);
867 unsigned long temperature;
869 if (kstrtoul(buf, 10, &temperature))
872 if (!tz->ops->set_emul_temp) {
873 mutex_lock(&tz->lock);
874 tz->emul_temperature = temperature;
875 mutex_unlock(&tz->lock);
877 ret = tz->ops->set_emul_temp(tz, temperature);
880 return ret ? ret : count;
882 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
883 #endif/*CONFIG_THERMAL_EMULATION*/
885 static DEVICE_ATTR(type, 0444, type_show, NULL);
886 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
887 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
888 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
889 static DEVICE_ATTR(passive_delay, S_IRUGO | S_IWUSR,
890 passive_delay_show, passive_delay_store);
891 static DEVICE_ATTR(polling_delay, S_IRUGO | S_IWUSR,
892 polling_delay_show, polling_delay_store);
893 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
894 static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
896 /* sys I/F for cooling device */
897 #define to_cooling_device(_dev) \
898 container_of(_dev, struct thermal_cooling_device, device)
901 thermal_cooling_device_type_show(struct device *dev,
902 struct device_attribute *attr, char *buf)
904 struct thermal_cooling_device *cdev = to_cooling_device(dev);
906 return sprintf(buf, "%s\n", cdev->type);
910 thermal_cooling_device_max_state_show(struct device *dev,
911 struct device_attribute *attr, char *buf)
913 struct thermal_cooling_device *cdev = to_cooling_device(dev);
917 ret = cdev->ops->get_max_state(cdev, &state);
920 return sprintf(buf, "%ld\n", state);
924 thermal_cooling_device_cur_state_show(struct device *dev,
925 struct device_attribute *attr, char *buf)
927 struct thermal_cooling_device *cdev = to_cooling_device(dev);
931 ret = cdev->ops->get_cur_state(cdev, &state);
934 return sprintf(buf, "%ld\n", state);
938 thermal_cooling_device_cur_state_store(struct device *dev,
939 struct device_attribute *attr,
940 const char *buf, size_t count)
942 struct thermal_cooling_device *cdev = to_cooling_device(dev);
946 if (!sscanf(buf, "%ld\n", &state))
952 result = cdev->ops->set_cur_state(cdev, state);
958 static struct device_attribute dev_attr_cdev_type =
959 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
960 static DEVICE_ATTR(max_state, 0444,
961 thermal_cooling_device_max_state_show, NULL);
962 static DEVICE_ATTR(cur_state, 0644,
963 thermal_cooling_device_cur_state_show,
964 thermal_cooling_device_cur_state_store);
967 thermal_cooling_device_trip_point_show(struct device *dev,
968 struct device_attribute *attr, char *buf)
970 struct thermal_instance *instance;
973 container_of(attr, struct thermal_instance, attr);
975 if (instance->trip == THERMAL_TRIPS_NONE)
976 return sprintf(buf, "-1\n");
978 return sprintf(buf, "%d\n", instance->trip);
981 /* Device management */
984 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
985 * @tz: pointer to struct thermal_zone_device
986 * @trip: indicates which trip point the cooling devices is
987 * associated with in this thermal zone.
988 * @cdev: pointer to struct thermal_cooling_device
989 * @upper: the Maximum cooling state for this trip point.
990 * THERMAL_NO_LIMIT means no upper limit,
991 * and the cooling device can be in max_state.
992 * @lower: the Minimum cooling state can be used for this trip point.
993 * THERMAL_NO_LIMIT means no lower limit,
994 * and the cooling device can be in cooling state 0.
996 * This interface function bind a thermal cooling device to the certain trip
997 * point of a thermal zone device.
998 * This function is usually called in the thermal zone device .bind callback.
1000 * Return: 0 on success, the proper error value otherwise.
1002 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1004 struct thermal_cooling_device *cdev,
1005 unsigned long upper, unsigned long lower)
1007 struct thermal_instance *dev;
1008 struct thermal_instance *pos;
1009 struct thermal_zone_device *pos1;
1010 struct thermal_cooling_device *pos2;
1011 unsigned long max_state;
1014 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1017 list_for_each_entry(pos1, &thermal_tz_list, node) {
1021 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1026 if (tz != pos1 || cdev != pos2)
1029 cdev->ops->get_max_state(cdev, &max_state);
1031 /* lower default 0, upper default max_state */
1032 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1033 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1035 if (lower > upper || upper > max_state)
1039 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1047 dev->target = THERMAL_NO_TARGET;
1049 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1053 sprintf(dev->name, "cdev%d", dev->id);
1055 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1059 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1060 sysfs_attr_init(&dev->attr.attr);
1061 dev->attr.attr.name = dev->attr_name;
1062 dev->attr.attr.mode = 0444;
1063 dev->attr.show = thermal_cooling_device_trip_point_show;
1064 result = device_create_file(&tz->device, &dev->attr);
1066 goto remove_symbol_link;
1068 mutex_lock(&tz->lock);
1069 mutex_lock(&cdev->lock);
1070 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1071 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1076 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1077 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1079 mutex_unlock(&cdev->lock);
1080 mutex_unlock(&tz->lock);
1085 device_remove_file(&tz->device, &dev->attr);
1087 sysfs_remove_link(&tz->device.kobj, dev->name);
1089 release_idr(&tz->idr, &tz->lock, dev->id);
1094 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
1097 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1099 * @tz: pointer to a struct thermal_zone_device.
1100 * @trip: indicates which trip point the cooling devices is
1101 * associated with in this thermal zone.
1102 * @cdev: pointer to a struct thermal_cooling_device.
1104 * This interface function unbind a thermal cooling device from the certain
1105 * trip point of a thermal zone device.
1106 * This function is usually called in the thermal zone device .unbind callback.
1108 * Return: 0 on success, the proper error value otherwise.
1110 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1112 struct thermal_cooling_device *cdev)
1114 struct thermal_instance *pos, *next;
1116 mutex_lock(&tz->lock);
1117 mutex_lock(&cdev->lock);
1118 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1119 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1120 list_del(&pos->tz_node);
1121 list_del(&pos->cdev_node);
1122 mutex_unlock(&cdev->lock);
1123 mutex_unlock(&tz->lock);
1127 mutex_unlock(&cdev->lock);
1128 mutex_unlock(&tz->lock);
1133 device_remove_file(&tz->device, &pos->attr);
1134 sysfs_remove_link(&tz->device.kobj, pos->name);
1135 release_idr(&tz->idr, &tz->lock, pos->id);
1139 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1141 static void thermal_release(struct device *dev)
1143 struct thermal_zone_device *tz;
1144 struct thermal_cooling_device *cdev;
1146 if (!strncmp(dev_name(dev), "thermal_zone",
1147 sizeof("thermal_zone") - 1)) {
1148 tz = to_thermal_zone(dev);
1150 } else if(!strncmp(dev_name(dev), "cooling_device",
1151 sizeof("cooling_device") - 1)){
1152 cdev = to_cooling_device(dev);
1157 static struct class thermal_class = {
1159 .dev_release = thermal_release,
1163 * __thermal_cooling_device_register() - register a new thermal cooling device
1164 * @np: a pointer to a device tree node.
1165 * @type: the thermal cooling device type.
1166 * @devdata: device private data.
1167 * @ops: standard thermal cooling devices callbacks.
1169 * This interface function adds a new thermal cooling device (fan/processor/...)
1170 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1171 * to all the thermal zone devices registered at the same time.
1172 * It also gives the opportunity to link the cooling device to a device tree
1173 * node, so that it can be bound to a thermal zone created out of device tree.
1175 * Return: a pointer to the created struct thermal_cooling_device or an
1176 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1178 static struct thermal_cooling_device *
1179 __thermal_cooling_device_register(struct device_node *np,
1180 char *type, void *devdata,
1181 const struct thermal_cooling_device_ops *ops)
1183 struct thermal_cooling_device *cdev;
1186 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1187 return ERR_PTR(-EINVAL);
1189 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1190 !ops->set_cur_state)
1191 return ERR_PTR(-EINVAL);
1193 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1195 return ERR_PTR(-ENOMEM);
1197 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1200 return ERR_PTR(result);
1203 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1204 mutex_init(&cdev->lock);
1205 INIT_LIST_HEAD(&cdev->thermal_instances);
1208 cdev->updated = false;
1209 cdev->device.class = &thermal_class;
1210 cdev->devdata = devdata;
1211 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1212 result = device_register(&cdev->device);
1214 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1216 return ERR_PTR(result);
1221 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1226 result = device_create_file(&cdev->device, &dev_attr_max_state);
1230 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1234 /* Add 'this' new cdev to the global cdev list */
1235 mutex_lock(&thermal_list_lock);
1236 list_add(&cdev->node, &thermal_cdev_list);
1237 mutex_unlock(&thermal_list_lock);
1239 /* Update binding information for 'this' new cdev */
1245 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1246 device_unregister(&cdev->device);
1247 return ERR_PTR(result);
1251 * thermal_cooling_device_register() - register a new thermal cooling device
1252 * @type: the thermal cooling device type.
1253 * @devdata: device private data.
1254 * @ops: standard thermal cooling devices callbacks.
1256 * This interface function adds a new thermal cooling device (fan/processor/...)
1257 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1258 * to all the thermal zone devices registered at the same time.
1260 * Return: a pointer to the created struct thermal_cooling_device or an
1261 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1263 struct thermal_cooling_device *
1264 thermal_cooling_device_register(char *type, void *devdata,
1265 const struct thermal_cooling_device_ops *ops)
1267 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1269 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1272 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1273 * @np: a pointer to a device tree node.
1274 * @type: the thermal cooling device type.
1275 * @devdata: device private data.
1276 * @ops: standard thermal cooling devices callbacks.
1278 * This function will register a cooling device with device tree node reference.
1279 * This interface function adds a new thermal cooling device (fan/processor/...)
1280 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1281 * to all the thermal zone devices registered at the same time.
1283 * Return: a pointer to the created struct thermal_cooling_device or an
1284 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1286 struct thermal_cooling_device *
1287 thermal_of_cooling_device_register(struct device_node *np,
1288 char *type, void *devdata,
1289 const struct thermal_cooling_device_ops *ops)
1291 return __thermal_cooling_device_register(np, type, devdata, ops);
1293 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1296 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1297 * @cdev: the thermal cooling device to remove.
1299 * thermal_cooling_device_unregister() must be called when the device is no
1302 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1305 const struct thermal_zone_params *tzp;
1306 struct thermal_zone_device *tz;
1307 struct thermal_cooling_device *pos = NULL;
1312 mutex_lock(&thermal_list_lock);
1313 list_for_each_entry(pos, &thermal_cdev_list, node)
1317 /* thermal cooling device not found */
1318 mutex_unlock(&thermal_list_lock);
1321 list_del(&cdev->node);
1323 /* Unbind all thermal zones associated with 'this' cdev */
1324 list_for_each_entry(tz, &thermal_tz_list, node) {
1325 if (tz->ops->unbind) {
1326 tz->ops->unbind(tz, cdev);
1330 if (!tz->tzp || !tz->tzp->tbp)
1334 for (i = 0; i < tzp->num_tbps; i++) {
1335 if (tzp->tbp[i].cdev == cdev) {
1336 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1337 tzp->tbp[i].cdev = NULL;
1342 mutex_unlock(&thermal_list_lock);
1345 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1346 device_remove_file(&cdev->device, &dev_attr_max_state);
1347 device_remove_file(&cdev->device, &dev_attr_cur_state);
1349 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1350 device_unregister(&cdev->device);
1353 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1355 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1357 struct thermal_instance *instance;
1358 unsigned long target = 0;
1360 /* cooling device is updated*/
1364 mutex_lock(&cdev->lock);
1365 /* Make sure cdev enters the deepest cooling state */
1366 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1367 if (instance->target == THERMAL_NO_TARGET)
1369 if (instance->target > target)
1370 target = instance->target;
1372 mutex_unlock(&cdev->lock);
1373 cdev->ops->set_cur_state(cdev, target);
1374 trace_cooling_device_update(cdev->type, target);
1375 cdev->updated = true;
1377 EXPORT_SYMBOL(thermal_cdev_update);
1380 * thermal_notify_framework - Sensor drivers use this API to notify framework
1381 * @tz: thermal zone device
1382 * @trip: indicates which trip point has been crossed
1384 * This function handles the trip events from sensor drivers. It starts
1385 * throttling the cooling devices according to the policy configured.
1386 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1387 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1388 * The throttling policy is based on the configured platform data; if no
1389 * platform data is provided, this uses the step_wise throttling policy.
1391 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1393 handle_thermal_trip(tz, trip);
1395 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1397 struct thermal_zone_device *thermal_zone_device_find(void *data,
1398 int (*match)(struct thermal_zone_device *, void *))
1400 struct thermal_zone_device *thz;
1402 mutex_lock(&thermal_list_lock);
1403 list_for_each_entry(thz, &thermal_tz_list, node)
1404 if (match(thz, data)) {
1405 mutex_unlock(&thermal_list_lock);
1409 mutex_unlock(&thermal_list_lock);
1412 EXPORT_SYMBOL(thermal_zone_device_find);
1414 struct thermal_zone_device *thermal_zone_device_find_by_name(const char *name)
1416 struct thermal_zone_device *thz;
1421 mutex_lock(&thermal_list_lock);
1422 list_for_each_entry(thz, &thermal_tz_list, node) {
1423 if (!strncmp(name, thz->type, strlen(name))) {
1424 mutex_unlock(&thermal_list_lock);
1428 mutex_unlock(&thermal_list_lock);
1431 EXPORT_SYMBOL(thermal_zone_device_find_by_name);
1434 * create_trip_attrs() - create attributes for trip points
1435 * @tz: the thermal zone device
1436 * @mask: Writeable trip point bitmap.
1438 * helper function to instantiate sysfs entries for every trip
1439 * point and its properties of a struct thermal_zone_device.
1441 * Return: 0 on success, the proper error value otherwise.
1443 static int create_trip_attrs(struct thermal_zone_device *tz, u64 mask)
1446 int size = sizeof(struct thermal_attr) * tz->trips;
1448 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1449 if (!tz->trip_type_attrs)
1452 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1453 if (!tz->trip_temp_attrs) {
1454 kfree(tz->trip_type_attrs);
1458 if (tz->ops->get_trip_hyst) {
1459 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1460 if (!tz->trip_hyst_attrs) {
1461 kfree(tz->trip_type_attrs);
1462 kfree(tz->trip_temp_attrs);
1468 for (indx = 0; indx < tz->trips; indx++) {
1469 /* create trip type attribute */
1470 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1471 "trip_point_%d_type", indx);
1473 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1474 tz->trip_type_attrs[indx].attr.attr.name =
1475 tz->trip_type_attrs[indx].name;
1476 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1477 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1479 device_create_file(&tz->device,
1480 &tz->trip_type_attrs[indx].attr);
1482 /* create trip temp attribute */
1483 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1484 "trip_point_%d_temp", indx);
1486 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1487 tz->trip_temp_attrs[indx].attr.attr.name =
1488 tz->trip_temp_attrs[indx].name;
1489 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1490 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1491 if (mask & (1ULL << indx)) {
1492 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1493 tz->trip_temp_attrs[indx].attr.store =
1494 trip_point_temp_store;
1497 device_create_file(&tz->device,
1498 &tz->trip_temp_attrs[indx].attr);
1500 /* create Optional trip hyst attribute */
1501 if (!tz->ops->get_trip_hyst)
1503 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1504 "trip_point_%d_hyst", indx);
1506 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1507 tz->trip_hyst_attrs[indx].attr.attr.name =
1508 tz->trip_hyst_attrs[indx].name;
1509 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1510 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1511 if (tz->ops->set_trip_hyst) {
1512 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1513 tz->trip_hyst_attrs[indx].attr.store =
1514 trip_point_hyst_store;
1517 device_create_file(&tz->device,
1518 &tz->trip_hyst_attrs[indx].attr);
1523 static void remove_trip_attrs(struct thermal_zone_device *tz)
1527 for (indx = 0; indx < tz->trips; indx++) {
1528 device_remove_file(&tz->device,
1529 &tz->trip_type_attrs[indx].attr);
1530 device_remove_file(&tz->device,
1531 &tz->trip_temp_attrs[indx].attr);
1532 if (tz->ops->get_trip_hyst)
1533 device_remove_file(&tz->device,
1534 &tz->trip_hyst_attrs[indx].attr);
1536 kfree(tz->trip_type_attrs);
1537 kfree(tz->trip_temp_attrs);
1538 kfree(tz->trip_hyst_attrs);
1542 * thermal_zone_device_register() - register a new thermal zone device
1543 * @type: the thermal zone device type
1544 * @trips: the number of trip points the thermal zone support
1545 * @mask: a bit string indicating the writeablility of trip points
1546 * @devdata: private device data
1547 * @ops: standard thermal zone device callbacks
1548 * @tzp: thermal zone platform parameters
1549 * @passive_delay: number of milliseconds to wait between polls when
1550 * performing passive cooling
1551 * @polling_delay: number of milliseconds to wait between polls when checking
1552 * whether trip points have been crossed (0 for interrupt
1555 * This interface function adds a new thermal zone device (sensor) to
1556 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1557 * thermal cooling devices registered at the same time.
1558 * thermal_zone_device_unregister() must be called when the device is no
1559 * longer needed. The passive cooling depends on the .get_trend() return value.
1561 * Return: a pointer to the created struct thermal_zone_device or an
1562 * in case of error, an ERR_PTR. Caller must check return value with
1563 * IS_ERR*() helpers.
1565 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1566 int trips, u64 mask, void *devdata,
1567 struct thermal_zone_device_ops *ops,
1568 const struct thermal_zone_params *tzp,
1569 int passive_delay, int polling_delay)
1571 struct thermal_zone_device *tz;
1572 enum thermal_trip_type trip_type;
1577 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1578 return ERR_PTR(-EINVAL);
1580 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1581 return ERR_PTR(-EINVAL);
1584 return ERR_PTR(-EINVAL);
1586 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1587 return ERR_PTR(-EINVAL);
1589 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1591 return ERR_PTR(-ENOMEM);
1593 INIT_LIST_HEAD(&tz->thermal_instances);
1595 mutex_init(&tz->lock);
1596 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1599 return ERR_PTR(result);
1602 strlcpy(tz->type, type ? : "", sizeof(tz->type));
1605 tz->device.class = &thermal_class;
1606 tz->devdata = devdata;
1608 tz->passive_delay = passive_delay;
1609 tz->polling_delay = polling_delay;
1611 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1612 result = device_register(&tz->device);
1614 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1616 return ERR_PTR(result);
1621 result = device_create_file(&tz->device, &dev_attr_type);
1626 result = device_create_file(&tz->device, &dev_attr_temp);
1630 if (ops->get_mode) {
1631 result = device_create_file(&tz->device, &dev_attr_mode);
1636 result = create_trip_attrs(tz, mask);
1640 for (count = 0; count < trips; count++) {
1641 tz->ops->get_trip_type(tz, count, &trip_type);
1642 if (trip_type == THERMAL_TRIP_PASSIVE)
1647 result = device_create_file(&tz->device, &dev_attr_passive);
1652 #ifdef CONFIG_THERMAL_EMULATION
1653 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1657 /* Create policy attribute */
1658 result = device_create_file(&tz->device, &dev_attr_policy);
1662 /* Create passive_delay attribute */
1663 result = device_create_file(&tz->device, &dev_attr_passive_delay);
1667 /* Create polling_delay attribute */
1668 result = device_create_file(&tz->device, &dev_attr_polling_delay);
1672 /* Create available_policies attribute */
1673 result = device_create_file(&tz->device, &dev_attr_available_policies);
1677 /* Update 'this' zone's governor information */
1678 mutex_lock(&thermal_governor_lock);
1681 tz->governor = __find_governor(tz->tzp->governor_name);
1683 if (!tz->governor) {
1684 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1685 if (!tz->governor) {
1686 mutex_unlock(&thermal_governor_lock);
1691 if (tz->governor->start) {
1692 result = tz->governor->start(tz);
1694 mutex_unlock(&thermal_governor_lock);
1699 mutex_unlock(&thermal_governor_lock);
1701 if (!tz->tzp || !tz->tzp->no_hwmon) {
1702 result = thermal_add_hwmon_sysfs(tz);
1707 mutex_lock(&thermal_list_lock);
1708 list_add_tail(&tz->node, &thermal_tz_list);
1709 mutex_unlock(&thermal_list_lock);
1711 /* Bind cooling devices for this zone */
1714 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1716 if (!tz->ops->get_temp)
1717 thermal_zone_device_set_polling(tz, 0);
1719 thermal_zone_device_update(tz);
1721 dev_info(&tz->device, "Registering thermal zone %s for type %s\n",
1722 dev_name(&tz->device), type);
1727 if (tz->governor && tz->governor->stop)
1728 tz->governor->stop(tz);
1729 tz->governor = NULL;
1730 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1731 device_unregister(&tz->device);
1732 return ERR_PTR(result);
1734 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1737 * thermal_device_unregister - removes the registered thermal zone device
1738 * @tz: the thermal zone device to remove
1740 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1743 const struct thermal_zone_params *tzp;
1744 struct thermal_cooling_device *cdev;
1745 struct thermal_zone_device *pos = NULL;
1752 mutex_lock(&thermal_list_lock);
1753 list_for_each_entry(pos, &thermal_tz_list, node)
1757 /* thermal zone device not found */
1758 mutex_unlock(&thermal_list_lock);
1761 list_del(&tz->node);
1763 /* Unbind all cdevs associated with 'this' thermal zone */
1764 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1765 if (tz->ops->unbind) {
1766 tz->ops->unbind(tz, cdev);
1770 if (!tzp || !tzp->tbp)
1773 for (i = 0; i < tzp->num_tbps; i++) {
1774 if (tzp->tbp[i].cdev == cdev) {
1775 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1776 tzp->tbp[i].cdev = NULL;
1781 mutex_unlock(&thermal_list_lock);
1783 thermal_zone_device_set_polling(tz, 0);
1786 device_remove_file(&tz->device, &dev_attr_type);
1787 device_remove_file(&tz->device, &dev_attr_temp);
1788 if (tz->ops->get_mode)
1789 device_remove_file(&tz->device, &dev_attr_mode);
1790 device_remove_file(&tz->device, &dev_attr_passive_delay);
1791 device_remove_file(&tz->device, &dev_attr_polling_delay);
1792 device_remove_file(&tz->device, &dev_attr_policy);
1793 device_remove_file(&tz->device, &dev_attr_available_policies);
1794 remove_trip_attrs(tz);
1796 if (tz->governor && tz->governor->stop)
1797 tz->governor->stop(tz);
1798 tz->governor = NULL;
1800 thermal_remove_hwmon_sysfs(tz);
1801 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1802 idr_destroy(&tz->idr);
1803 mutex_destroy(&tz->lock);
1804 device_unregister(&tz->device);
1807 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1810 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1811 * @name: thermal zone name to fetch the temperature
1813 * When only one zone is found with the passed name, returns a reference to it.
1815 * Return: On success returns a reference to an unique thermal zone with
1816 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1817 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1819 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1821 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1822 unsigned int found = 0;
1827 mutex_lock(&thermal_list_lock);
1828 list_for_each_entry(pos, &thermal_tz_list, node)
1829 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1833 mutex_unlock(&thermal_list_lock);
1835 /* nothing has been found, thus an error code for it */
1837 ref = ERR_PTR(-ENODEV);
1839 /* Success only when an unique zone is found */
1840 ref = ERR_PTR(-EEXIST);
1845 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1848 static struct genl_family thermal_event_genl_family = {
1849 .id = GENL_ID_GENERATE,
1850 .name = THERMAL_GENL_FAMILY_NAME,
1851 .version = THERMAL_GENL_VERSION,
1852 .maxattr = THERMAL_GENL_ATTR_MAX,
1855 static struct genl_multicast_group thermal_event_mcgrp = {
1856 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1859 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1860 enum events event, int temp)
1862 struct sk_buff *skb;
1863 struct nlattr *attr;
1864 struct thermal_genl_event *thermal_event;
1868 static unsigned int thermal_event_seqnum;
1873 /* allocate memory */
1874 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1877 skb = genlmsg_new(size, GFP_ATOMIC);
1881 /* add the genetlink message header */
1882 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1883 &thermal_event_genl_family, 0,
1884 THERMAL_GENL_CMD_EVENT);
1891 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1892 sizeof(struct thermal_genl_event));
1899 thermal_event = nla_data(attr);
1900 if (!thermal_event) {
1905 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1907 thermal_event->orig = tz->id;
1908 thermal_event->event = event;
1909 thermal_event->temp = temp;
1911 /* send multicast genetlink message */
1912 result = genlmsg_end(skb, msg_header);
1918 genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1921 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1923 static int genetlink_init(void)
1927 result = genl_register_family(&thermal_event_genl_family);
1931 result = genl_register_mc_group(&thermal_event_genl_family,
1932 &thermal_event_mcgrp);
1934 genl_unregister_family(&thermal_event_genl_family);
1938 static void genetlink_exit(void)
1940 genl_unregister_family(&thermal_event_genl_family);
1942 #else /* !CONFIG_NET */
1943 static inline int genetlink_init(void) { return 0; }
1944 static inline void genetlink_exit(void) {}
1945 #endif /* !CONFIG_NET */
1947 static int __init thermal_register_governors(void)
1951 result = thermal_gov_step_wise_register();
1955 result = thermal_gov_fair_share_register();
1959 result = pid_thermal_gov_register();
1963 return thermal_gov_user_space_register();
1966 static void thermal_unregister_governors(void)
1968 thermal_gov_step_wise_unregister();
1969 thermal_gov_fair_share_unregister();
1970 pid_thermal_gov_unregister();
1971 thermal_gov_user_space_unregister();
1974 static int __init thermal_init(void)
1978 result = thermal_register_governors();
1982 result = class_register(&thermal_class);
1984 goto unregister_governors;
1986 result = genetlink_init();
1988 goto unregister_class;
1990 result = of_parse_thermal_zones();
1998 unregister_governors:
1999 thermal_unregister_governors();
2001 class_unregister(&thermal_class);
2003 idr_destroy(&thermal_tz_idr);
2004 idr_destroy(&thermal_cdev_idr);
2005 mutex_destroy(&thermal_idr_lock);
2006 mutex_destroy(&thermal_list_lock);
2007 mutex_destroy(&thermal_governor_lock);
2011 static void __exit thermal_exit(void)
2013 of_thermal_destroy_zones();
2015 class_unregister(&thermal_class);
2016 thermal_unregister_governors();
2017 idr_destroy(&thermal_tz_idr);
2018 idr_destroy(&thermal_cdev_idr);
2019 mutex_destroy(&thermal_idr_lock);
2020 mutex_destroy(&thermal_list_lock);
2021 mutex_destroy(&thermal_governor_lock);
2024 fs_initcall(thermal_init);
2025 module_exit(thermal_exit);