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 <net/netlink.h>
37 #include <net/genetlink.h>
39 #include "thermal_core.h"
41 MODULE_AUTHOR("Zhang Rui");
42 MODULE_DESCRIPTION("Generic thermal management sysfs support");
43 MODULE_LICENSE("GPL v2");
45 static DEFINE_IDR(thermal_tz_idr);
46 static DEFINE_IDR(thermal_cdev_idr);
47 static DEFINE_MUTEX(thermal_idr_lock);
49 static LIST_HEAD(thermal_tz_list);
50 static LIST_HEAD(thermal_cdev_list);
51 static LIST_HEAD(thermal_governor_list);
53 static DEFINE_MUTEX(thermal_list_lock);
54 static DEFINE_MUTEX(thermal_governor_lock);
56 static struct thermal_governor *__find_governor(const char *name)
58 struct thermal_governor *pos;
60 list_for_each_entry(pos, &thermal_governor_list, governor_list)
61 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
67 int thermal_register_governor(struct thermal_governor *governor)
71 struct thermal_zone_device *pos;
76 mutex_lock(&thermal_governor_lock);
79 if (__find_governor(governor->name) == NULL) {
81 list_add(&governor->governor_list, &thermal_governor_list);
84 mutex_lock(&thermal_list_lock);
86 list_for_each_entry(pos, &thermal_tz_list, node) {
90 name = pos->tzp->governor_name;
92 name = DEFAULT_THERMAL_GOVERNOR;
93 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
94 pos->governor = governor;
97 mutex_unlock(&thermal_list_lock);
98 mutex_unlock(&thermal_governor_lock);
103 void thermal_unregister_governor(struct thermal_governor *governor)
105 struct thermal_zone_device *pos;
110 mutex_lock(&thermal_governor_lock);
112 if (__find_governor(governor->name) == NULL)
115 mutex_lock(&thermal_list_lock);
117 list_for_each_entry(pos, &thermal_tz_list, node) {
118 if (!strnicmp(pos->governor->name, governor->name,
119 THERMAL_NAME_LENGTH))
120 pos->governor = NULL;
123 mutex_unlock(&thermal_list_lock);
124 list_del(&governor->governor_list);
126 mutex_unlock(&thermal_governor_lock);
130 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
136 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
139 if (unlikely(ret < 0))
145 static void release_idr(struct idr *idr, struct mutex *lock, int id)
154 int get_tz_trend(struct thermal_zone_device *tz, int trip)
156 enum thermal_trend trend;
158 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
159 if (tz->temperature > tz->last_temperature)
160 trend = THERMAL_TREND_RAISING;
161 else if (tz->temperature < tz->last_temperature)
162 trend = THERMAL_TREND_DROPPING;
164 trend = THERMAL_TREND_STABLE;
169 EXPORT_SYMBOL(get_tz_trend);
171 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
172 struct thermal_cooling_device *cdev, int trip)
174 struct thermal_instance *pos = NULL;
175 struct thermal_instance *target_instance = NULL;
177 mutex_lock(&tz->lock);
178 mutex_lock(&cdev->lock);
180 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
181 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
182 target_instance = pos;
187 mutex_unlock(&cdev->lock);
188 mutex_unlock(&tz->lock);
190 return target_instance;
192 EXPORT_SYMBOL(get_thermal_instance);
194 static void print_bind_err_msg(struct thermal_zone_device *tz,
195 struct thermal_cooling_device *cdev, int ret)
197 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
198 tz->type, cdev->type, ret);
201 static void __bind(struct thermal_zone_device *tz, int mask,
202 struct thermal_cooling_device *cdev)
206 for (i = 0; i < tz->trips; i++) {
207 if (mask & (1 << i)) {
208 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
209 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
211 print_bind_err_msg(tz, cdev, ret);
216 static void __unbind(struct thermal_zone_device *tz, int mask,
217 struct thermal_cooling_device *cdev)
221 for (i = 0; i < tz->trips; i++)
223 thermal_zone_unbind_cooling_device(tz, i, cdev);
226 static void bind_cdev(struct thermal_cooling_device *cdev)
229 const struct thermal_zone_params *tzp;
230 struct thermal_zone_device *pos = NULL;
232 mutex_lock(&thermal_list_lock);
234 list_for_each_entry(pos, &thermal_tz_list, node) {
235 if (!pos->tzp && !pos->ops->bind)
238 if (!pos->tzp && pos->ops->bind) {
239 ret = pos->ops->bind(pos, cdev);
241 print_bind_err_msg(pos, cdev, ret);
245 if (!tzp || !tzp->tbp)
248 for (i = 0; i < tzp->num_tbps; i++) {
249 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
251 if (tzp->tbp[i].match(pos, cdev))
253 tzp->tbp[i].cdev = cdev;
254 __bind(pos, tzp->tbp[i].trip_mask, cdev);
258 mutex_unlock(&thermal_list_lock);
261 static void bind_tz(struct thermal_zone_device *tz)
264 struct thermal_cooling_device *pos = NULL;
265 const struct thermal_zone_params *tzp = tz->tzp;
267 if (!tzp && !tz->ops->bind)
270 mutex_lock(&thermal_list_lock);
272 /* If there is no platform data, try to use ops->bind */
273 if (!tzp && tz->ops->bind) {
274 list_for_each_entry(pos, &thermal_cdev_list, node) {
275 ret = tz->ops->bind(tz, pos);
277 print_bind_err_msg(tz, pos, ret);
282 if (!tzp || !tzp->tbp)
285 list_for_each_entry(pos, &thermal_cdev_list, node) {
286 for (i = 0; i < tzp->num_tbps; i++) {
287 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
289 if (tzp->tbp[i].match(tz, pos))
291 tzp->tbp[i].cdev = pos;
292 __bind(tz, tzp->tbp[i].trip_mask, pos);
296 mutex_unlock(&thermal_list_lock);
299 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
303 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
304 round_jiffies(msecs_to_jiffies(delay)));
306 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
307 msecs_to_jiffies(delay));
309 cancel_delayed_work(&tz->poll_queue);
312 static void monitor_thermal_zone(struct thermal_zone_device *tz)
314 mutex_lock(&tz->lock);
317 thermal_zone_device_set_polling(tz, tz->passive_delay);
318 else if (tz->polling_delay)
319 thermal_zone_device_set_polling(tz, tz->polling_delay);
321 thermal_zone_device_set_polling(tz, 0);
323 mutex_unlock(&tz->lock);
326 static void handle_non_critical_trips(struct thermal_zone_device *tz,
327 int trip, enum thermal_trip_type trip_type)
330 tz->governor->throttle(tz, trip);
333 static void handle_critical_trips(struct thermal_zone_device *tz,
334 int trip, enum thermal_trip_type trip_type)
338 tz->ops->get_trip_temp(tz, trip, &trip_temp);
340 /* If we have not crossed the trip_temp, we do not care. */
341 if (tz->temperature < trip_temp)
345 tz->ops->notify(tz, trip, trip_type);
347 if (trip_type == THERMAL_TRIP_CRITICAL) {
348 dev_emerg(&tz->device,
349 "critical temperature reached(%d C),shutting down\n",
350 tz->temperature / 1000);
351 orderly_poweroff(true);
355 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
357 enum thermal_trip_type type;
359 tz->ops->get_trip_type(tz, trip, &type);
361 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
362 handle_critical_trips(tz, trip, type);
364 handle_non_critical_trips(tz, trip, type);
366 * Alright, we handled this trip successfully.
367 * So, start monitoring again.
369 monitor_thermal_zone(tz);
373 * thermal_zone_get_temp() - returns its the temperature of thermal zone
374 * @tz: a valid pointer to a struct thermal_zone_device
375 * @temp: a valid pointer to where to store the resulting temperature.
377 * When a valid thermal zone reference is passed, it will fetch its
378 * temperature and fill @temp.
380 * Return: On success returns 0, an error code otherwise
382 int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
385 #ifdef CONFIG_THERMAL_EMULATION
387 unsigned long crit_temp = -1UL;
388 enum thermal_trip_type type;
391 if (!tz || IS_ERR(tz))
394 mutex_lock(&tz->lock);
396 ret = tz->ops->get_temp(tz, temp);
397 #ifdef CONFIG_THERMAL_EMULATION
398 if (!tz->emul_temperature)
401 for (count = 0; count < tz->trips; count++) {
402 ret = tz->ops->get_trip_type(tz, count, &type);
403 if (!ret && type == THERMAL_TRIP_CRITICAL) {
404 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
412 if (*temp < crit_temp)
413 *temp = tz->emul_temperature;
416 mutex_unlock(&tz->lock);
420 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
422 static void update_temperature(struct thermal_zone_device *tz)
427 ret = thermal_zone_get_temp(tz, &temp);
429 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
434 mutex_lock(&tz->lock);
435 tz->last_temperature = tz->temperature;
436 tz->temperature = temp;
437 mutex_unlock(&tz->lock);
440 void thermal_zone_device_update(struct thermal_zone_device *tz)
444 update_temperature(tz);
446 for (count = 0; count < tz->trips; count++)
447 handle_thermal_trip(tz, count);
449 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
451 static void thermal_zone_device_check(struct work_struct *work)
453 struct thermal_zone_device *tz = container_of(work, struct
456 thermal_zone_device_update(tz);
459 /* sys I/F for thermal zone */
461 #define to_thermal_zone(_dev) \
462 container_of(_dev, struct thermal_zone_device, device)
465 type_show(struct device *dev, struct device_attribute *attr, char *buf)
467 struct thermal_zone_device *tz = to_thermal_zone(dev);
469 return sprintf(buf, "%s\n", tz->type);
473 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
475 struct thermal_zone_device *tz = to_thermal_zone(dev);
479 ret = thermal_zone_get_temp(tz, &temperature);
484 return sprintf(buf, "%ld\n", temperature);
488 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
490 struct thermal_zone_device *tz = to_thermal_zone(dev);
491 enum thermal_device_mode mode;
494 if (!tz->ops->get_mode)
497 result = tz->ops->get_mode(tz, &mode);
501 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
506 mode_store(struct device *dev, struct device_attribute *attr,
507 const char *buf, size_t count)
509 struct thermal_zone_device *tz = to_thermal_zone(dev);
512 if (!tz->ops->set_mode)
515 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
516 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
517 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
518 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
529 trip_point_type_show(struct device *dev, struct device_attribute *attr,
532 struct thermal_zone_device *tz = to_thermal_zone(dev);
533 enum thermal_trip_type type;
536 if (!tz->ops->get_trip_type)
539 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
542 result = tz->ops->get_trip_type(tz, trip, &type);
547 case THERMAL_TRIP_CRITICAL:
548 return sprintf(buf, "critical\n");
549 case THERMAL_TRIP_HOT:
550 return sprintf(buf, "hot\n");
551 case THERMAL_TRIP_PASSIVE:
552 return sprintf(buf, "passive\n");
553 case THERMAL_TRIP_ACTIVE:
554 return sprintf(buf, "active\n");
556 return sprintf(buf, "unknown\n");
561 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
562 const char *buf, size_t count)
564 struct thermal_zone_device *tz = to_thermal_zone(dev);
566 unsigned long temperature;
568 if (!tz->ops->set_trip_temp)
571 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
574 if (kstrtoul(buf, 10, &temperature))
577 ret = tz->ops->set_trip_temp(tz, trip, temperature);
579 return ret ? ret : count;
583 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
586 struct thermal_zone_device *tz = to_thermal_zone(dev);
590 if (!tz->ops->get_trip_temp)
593 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
596 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
601 return sprintf(buf, "%ld\n", temperature);
605 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
606 const char *buf, size_t count)
608 struct thermal_zone_device *tz = to_thermal_zone(dev);
610 unsigned long temperature;
612 if (!tz->ops->set_trip_hyst)
615 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
618 if (kstrtoul(buf, 10, &temperature))
622 * We are not doing any check on the 'temperature' value
623 * here. The driver implementing 'set_trip_hyst' has to
626 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
628 return ret ? ret : count;
632 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
635 struct thermal_zone_device *tz = to_thermal_zone(dev);
637 unsigned long temperature;
639 if (!tz->ops->get_trip_hyst)
642 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
645 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
647 return ret ? ret : sprintf(buf, "%ld\n", temperature);
651 passive_store(struct device *dev, struct device_attribute *attr,
652 const char *buf, size_t count)
654 struct thermal_zone_device *tz = to_thermal_zone(dev);
655 struct thermal_cooling_device *cdev = NULL;
658 if (!sscanf(buf, "%d\n", &state))
661 /* sanity check: values below 1000 millicelcius don't make sense
662 * and can cause the system to go into a thermal heart attack
664 if (state && state < 1000)
667 if (state && !tz->forced_passive) {
668 mutex_lock(&thermal_list_lock);
669 list_for_each_entry(cdev, &thermal_cdev_list, node) {
670 if (!strncmp("Processor", cdev->type,
671 sizeof("Processor")))
672 thermal_zone_bind_cooling_device(tz,
673 THERMAL_TRIPS_NONE, cdev,
677 mutex_unlock(&thermal_list_lock);
678 if (!tz->passive_delay)
679 tz->passive_delay = 1000;
680 } else if (!state && tz->forced_passive) {
681 mutex_lock(&thermal_list_lock);
682 list_for_each_entry(cdev, &thermal_cdev_list, node) {
683 if (!strncmp("Processor", cdev->type,
684 sizeof("Processor")))
685 thermal_zone_unbind_cooling_device(tz,
689 mutex_unlock(&thermal_list_lock);
690 tz->passive_delay = 0;
693 tz->forced_passive = state;
695 thermal_zone_device_update(tz);
701 passive_show(struct device *dev, struct device_attribute *attr,
704 struct thermal_zone_device *tz = to_thermal_zone(dev);
706 return sprintf(buf, "%d\n", tz->forced_passive);
710 policy_store(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
714 struct thermal_zone_device *tz = to_thermal_zone(dev);
715 struct thermal_governor *gov;
717 mutex_lock(&thermal_governor_lock);
719 gov = __find_governor(buf);
727 mutex_unlock(&thermal_governor_lock);
732 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
734 struct thermal_zone_device *tz = to_thermal_zone(dev);
736 return sprintf(buf, "%s\n", tz->governor->name);
739 #ifdef CONFIG_THERMAL_EMULATION
741 emul_temp_store(struct device *dev, struct device_attribute *attr,
742 const char *buf, size_t count)
744 struct thermal_zone_device *tz = to_thermal_zone(dev);
746 unsigned long temperature;
748 if (kstrtoul(buf, 10, &temperature))
751 if (!tz->ops->set_emul_temp) {
752 mutex_lock(&tz->lock);
753 tz->emul_temperature = temperature;
754 mutex_unlock(&tz->lock);
756 ret = tz->ops->set_emul_temp(tz, temperature);
759 return ret ? ret : count;
761 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
762 #endif/*CONFIG_THERMAL_EMULATION*/
764 static DEVICE_ATTR(type, 0444, type_show, NULL);
765 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
766 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
767 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
768 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
770 /* sys I/F for cooling device */
771 #define to_cooling_device(_dev) \
772 container_of(_dev, struct thermal_cooling_device, device)
775 thermal_cooling_device_type_show(struct device *dev,
776 struct device_attribute *attr, char *buf)
778 struct thermal_cooling_device *cdev = to_cooling_device(dev);
780 return sprintf(buf, "%s\n", cdev->type);
784 thermal_cooling_device_max_state_show(struct device *dev,
785 struct device_attribute *attr, char *buf)
787 struct thermal_cooling_device *cdev = to_cooling_device(dev);
791 ret = cdev->ops->get_max_state(cdev, &state);
794 return sprintf(buf, "%ld\n", state);
798 thermal_cooling_device_cur_state_show(struct device *dev,
799 struct device_attribute *attr, char *buf)
801 struct thermal_cooling_device *cdev = to_cooling_device(dev);
805 ret = cdev->ops->get_cur_state(cdev, &state);
808 return sprintf(buf, "%ld\n", state);
812 thermal_cooling_device_cur_state_store(struct device *dev,
813 struct device_attribute *attr,
814 const char *buf, size_t count)
816 struct thermal_cooling_device *cdev = to_cooling_device(dev);
820 if (!sscanf(buf, "%ld\n", &state))
826 result = cdev->ops->set_cur_state(cdev, state);
832 static struct device_attribute dev_attr_cdev_type =
833 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
834 static DEVICE_ATTR(max_state, 0444,
835 thermal_cooling_device_max_state_show, NULL);
836 static DEVICE_ATTR(cur_state, 0644,
837 thermal_cooling_device_cur_state_show,
838 thermal_cooling_device_cur_state_store);
841 thermal_cooling_device_trip_point_show(struct device *dev,
842 struct device_attribute *attr, char *buf)
844 struct thermal_instance *instance;
847 container_of(attr, struct thermal_instance, attr);
849 if (instance->trip == THERMAL_TRIPS_NONE)
850 return sprintf(buf, "-1\n");
852 return sprintf(buf, "%d\n", instance->trip);
855 /* Device management */
857 #if defined(CONFIG_THERMAL_HWMON)
860 #include <linux/hwmon.h>
862 /* thermal zone devices with the same type share one hwmon device */
863 struct thermal_hwmon_device {
864 char type[THERMAL_NAME_LENGTH];
865 struct device *device;
867 struct list_head tz_list;
868 struct list_head node;
871 struct thermal_hwmon_attr {
872 struct device_attribute attr;
876 /* one temperature input for each thermal zone */
877 struct thermal_hwmon_temp {
878 struct list_head hwmon_node;
879 struct thermal_zone_device *tz;
880 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
881 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
884 static LIST_HEAD(thermal_hwmon_list);
887 name_show(struct device *dev, struct device_attribute *attr, char *buf)
889 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
890 return sprintf(buf, "%s\n", hwmon->type);
892 static DEVICE_ATTR(name, 0444, name_show, NULL);
895 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
899 struct thermal_hwmon_attr *hwmon_attr
900 = container_of(attr, struct thermal_hwmon_attr, attr);
901 struct thermal_hwmon_temp *temp
902 = container_of(hwmon_attr, struct thermal_hwmon_temp,
904 struct thermal_zone_device *tz = temp->tz;
906 ret = thermal_zone_get_temp(tz, &temperature);
911 return sprintf(buf, "%ld\n", temperature);
915 temp_crit_show(struct device *dev, struct device_attribute *attr,
918 struct thermal_hwmon_attr *hwmon_attr
919 = container_of(attr, struct thermal_hwmon_attr, attr);
920 struct thermal_hwmon_temp *temp
921 = container_of(hwmon_attr, struct thermal_hwmon_temp,
923 struct thermal_zone_device *tz = temp->tz;
927 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
931 return sprintf(buf, "%ld\n", temperature);
935 static struct thermal_hwmon_device *
936 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
938 struct thermal_hwmon_device *hwmon;
940 mutex_lock(&thermal_list_lock);
941 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
942 if (!strcmp(hwmon->type, tz->type)) {
943 mutex_unlock(&thermal_list_lock);
946 mutex_unlock(&thermal_list_lock);
951 /* Find the temperature input matching a given thermal zone */
952 static struct thermal_hwmon_temp *
953 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
954 const struct thermal_zone_device *tz)
956 struct thermal_hwmon_temp *temp;
958 mutex_lock(&thermal_list_lock);
959 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
960 if (temp->tz == tz) {
961 mutex_unlock(&thermal_list_lock);
964 mutex_unlock(&thermal_list_lock);
970 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
972 struct thermal_hwmon_device *hwmon;
973 struct thermal_hwmon_temp *temp;
974 int new_hwmon_device = 1;
977 hwmon = thermal_hwmon_lookup_by_type(tz);
979 new_hwmon_device = 0;
980 goto register_sys_interface;
983 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
987 INIT_LIST_HEAD(&hwmon->tz_list);
988 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
989 hwmon->device = hwmon_device_register(NULL);
990 if (IS_ERR(hwmon->device)) {
991 result = PTR_ERR(hwmon->device);
994 dev_set_drvdata(hwmon->device, hwmon);
995 result = device_create_file(hwmon->device, &dev_attr_name);
999 register_sys_interface:
1000 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
1003 goto unregister_name;
1009 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
1010 "temp%d_input", hwmon->count);
1011 temp->temp_input.attr.attr.name = temp->temp_input.name;
1012 temp->temp_input.attr.attr.mode = 0444;
1013 temp->temp_input.attr.show = temp_input_show;
1014 sysfs_attr_init(&temp->temp_input.attr.attr);
1015 result = device_create_file(hwmon->device, &temp->temp_input.attr);
1019 if (tz->ops->get_crit_temp) {
1020 unsigned long temperature;
1021 if (!tz->ops->get_crit_temp(tz, &temperature)) {
1022 snprintf(temp->temp_crit.name,
1023 sizeof(temp->temp_crit.name),
1024 "temp%d_crit", hwmon->count);
1025 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1026 temp->temp_crit.attr.attr.mode = 0444;
1027 temp->temp_crit.attr.show = temp_crit_show;
1028 sysfs_attr_init(&temp->temp_crit.attr.attr);
1029 result = device_create_file(hwmon->device,
1030 &temp->temp_crit.attr);
1032 goto unregister_input;
1036 mutex_lock(&thermal_list_lock);
1037 if (new_hwmon_device)
1038 list_add_tail(&hwmon->node, &thermal_hwmon_list);
1039 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
1040 mutex_unlock(&thermal_list_lock);
1045 device_remove_file(hwmon->device, &temp->temp_input.attr);
1049 if (new_hwmon_device) {
1050 device_remove_file(hwmon->device, &dev_attr_name);
1051 hwmon_device_unregister(hwmon->device);
1054 if (new_hwmon_device)
1061 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1063 struct thermal_hwmon_device *hwmon;
1064 struct thermal_hwmon_temp *temp;
1066 hwmon = thermal_hwmon_lookup_by_type(tz);
1067 if (unlikely(!hwmon)) {
1068 /* Should never happen... */
1069 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1073 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1074 if (unlikely(!temp)) {
1075 /* Should never happen... */
1076 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1080 device_remove_file(hwmon->device, &temp->temp_input.attr);
1081 if (tz->ops->get_crit_temp)
1082 device_remove_file(hwmon->device, &temp->temp_crit.attr);
1084 mutex_lock(&thermal_list_lock);
1085 list_del(&temp->hwmon_node);
1087 if (!list_empty(&hwmon->tz_list)) {
1088 mutex_unlock(&thermal_list_lock);
1091 list_del(&hwmon->node);
1092 mutex_unlock(&thermal_list_lock);
1094 device_remove_file(hwmon->device, &dev_attr_name);
1095 hwmon_device_unregister(hwmon->device);
1100 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1106 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1112 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1113 * @tz: pointer to struct thermal_zone_device
1114 * @trip: indicates which trip point the cooling devices is
1115 * associated with in this thermal zone.
1116 * @cdev: pointer to struct thermal_cooling_device
1117 * @upper: the Maximum cooling state for this trip point.
1118 * THERMAL_NO_LIMIT means no upper limit,
1119 * and the cooling device can be in max_state.
1120 * @lower: the Minimum cooling state can be used for this trip point.
1121 * THERMAL_NO_LIMIT means no lower limit,
1122 * and the cooling device can be in cooling state 0.
1124 * This interface function bind a thermal cooling device to the certain trip
1125 * point of a thermal zone device.
1126 * This function is usually called in the thermal zone device .bind callback.
1128 * Return: 0 on success, the proper error value otherwise.
1130 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1132 struct thermal_cooling_device *cdev,
1133 unsigned long upper, unsigned long lower)
1135 struct thermal_instance *dev;
1136 struct thermal_instance *pos;
1137 struct thermal_zone_device *pos1;
1138 struct thermal_cooling_device *pos2;
1139 unsigned long max_state;
1142 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1145 list_for_each_entry(pos1, &thermal_tz_list, node) {
1149 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1154 if (tz != pos1 || cdev != pos2)
1157 cdev->ops->get_max_state(cdev, &max_state);
1159 /* lower default 0, upper default max_state */
1160 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1161 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1163 if (lower > upper || upper > max_state)
1167 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1175 dev->target = THERMAL_NO_TARGET;
1177 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1181 sprintf(dev->name, "cdev%d", dev->id);
1183 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1187 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1188 sysfs_attr_init(&dev->attr.attr);
1189 dev->attr.attr.name = dev->attr_name;
1190 dev->attr.attr.mode = 0444;
1191 dev->attr.show = thermal_cooling_device_trip_point_show;
1192 result = device_create_file(&tz->device, &dev->attr);
1194 goto remove_symbol_link;
1196 mutex_lock(&tz->lock);
1197 mutex_lock(&cdev->lock);
1198 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1199 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1204 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1205 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1207 mutex_unlock(&cdev->lock);
1208 mutex_unlock(&tz->lock);
1213 device_remove_file(&tz->device, &dev->attr);
1215 sysfs_remove_link(&tz->device.kobj, dev->name);
1217 release_idr(&tz->idr, &tz->lock, dev->id);
1222 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
1225 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1227 * @tz: pointer to a struct thermal_zone_device.
1228 * @trip: indicates which trip point the cooling devices is
1229 * associated with in this thermal zone.
1230 * @cdev: pointer to a struct thermal_cooling_device.
1232 * This interface function unbind a thermal cooling device from the certain
1233 * trip point of a thermal zone device.
1234 * This function is usually called in the thermal zone device .unbind callback.
1236 * Return: 0 on success, the proper error value otherwise.
1238 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1240 struct thermal_cooling_device *cdev)
1242 struct thermal_instance *pos, *next;
1244 mutex_lock(&tz->lock);
1245 mutex_lock(&cdev->lock);
1246 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1247 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1248 list_del(&pos->tz_node);
1249 list_del(&pos->cdev_node);
1250 mutex_unlock(&cdev->lock);
1251 mutex_unlock(&tz->lock);
1255 mutex_unlock(&cdev->lock);
1256 mutex_unlock(&tz->lock);
1261 device_remove_file(&tz->device, &pos->attr);
1262 sysfs_remove_link(&tz->device.kobj, pos->name);
1263 release_idr(&tz->idr, &tz->lock, pos->id);
1267 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1269 static void thermal_release(struct device *dev)
1271 struct thermal_zone_device *tz;
1272 struct thermal_cooling_device *cdev;
1274 if (!strncmp(dev_name(dev), "thermal_zone",
1275 sizeof("thermal_zone") - 1)) {
1276 tz = to_thermal_zone(dev);
1279 cdev = to_cooling_device(dev);
1284 static struct class thermal_class = {
1286 .dev_release = thermal_release,
1290 * thermal_cooling_device_register() - register a new thermal cooling device
1291 * @type: the thermal cooling device type.
1292 * @devdata: device private data.
1293 * @ops: standard thermal cooling devices callbacks.
1295 * This interface function adds a new thermal cooling device (fan/processor/...)
1296 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1297 * to all the thermal zone devices registered at the same time.
1299 * Return: a pointer to the created struct thermal_cooling_device or an
1300 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1302 struct thermal_cooling_device *
1303 thermal_cooling_device_register(char *type, void *devdata,
1304 const struct thermal_cooling_device_ops *ops)
1306 struct thermal_cooling_device *cdev;
1309 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1310 return ERR_PTR(-EINVAL);
1312 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1313 !ops->set_cur_state)
1314 return ERR_PTR(-EINVAL);
1316 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1318 return ERR_PTR(-ENOMEM);
1320 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1323 return ERR_PTR(result);
1326 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1327 mutex_init(&cdev->lock);
1328 INIT_LIST_HEAD(&cdev->thermal_instances);
1330 cdev->updated = true;
1331 cdev->device.class = &thermal_class;
1332 cdev->devdata = devdata;
1333 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1334 result = device_register(&cdev->device);
1336 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1338 return ERR_PTR(result);
1343 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1348 result = device_create_file(&cdev->device, &dev_attr_max_state);
1352 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1356 /* Add 'this' new cdev to the global cdev list */
1357 mutex_lock(&thermal_list_lock);
1358 list_add(&cdev->node, &thermal_cdev_list);
1359 mutex_unlock(&thermal_list_lock);
1361 /* Update binding information for 'this' new cdev */
1367 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1368 device_unregister(&cdev->device);
1369 return ERR_PTR(result);
1371 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1374 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1375 * @cdev: the thermal cooling device to remove.
1377 * thermal_cooling_device_unregister() must be called when the device is no
1380 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1383 const struct thermal_zone_params *tzp;
1384 struct thermal_zone_device *tz;
1385 struct thermal_cooling_device *pos = NULL;
1390 mutex_lock(&thermal_list_lock);
1391 list_for_each_entry(pos, &thermal_cdev_list, node)
1395 /* thermal cooling device not found */
1396 mutex_unlock(&thermal_list_lock);
1399 list_del(&cdev->node);
1401 /* Unbind all thermal zones associated with 'this' cdev */
1402 list_for_each_entry(tz, &thermal_tz_list, node) {
1403 if (tz->ops->unbind) {
1404 tz->ops->unbind(tz, cdev);
1408 if (!tz->tzp || !tz->tzp->tbp)
1412 for (i = 0; i < tzp->num_tbps; i++) {
1413 if (tzp->tbp[i].cdev == cdev) {
1414 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1415 tzp->tbp[i].cdev = NULL;
1420 mutex_unlock(&thermal_list_lock);
1423 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1424 device_remove_file(&cdev->device, &dev_attr_max_state);
1425 device_remove_file(&cdev->device, &dev_attr_cur_state);
1427 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1428 device_unregister(&cdev->device);
1431 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1433 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1435 struct thermal_instance *instance;
1436 unsigned long target = 0;
1438 /* cooling device is updated*/
1442 mutex_lock(&cdev->lock);
1443 /* Make sure cdev enters the deepest cooling state */
1444 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1445 if (instance->target == THERMAL_NO_TARGET)
1447 if (instance->target > target)
1448 target = instance->target;
1450 mutex_unlock(&cdev->lock);
1451 cdev->ops->set_cur_state(cdev, target);
1452 cdev->updated = true;
1454 EXPORT_SYMBOL(thermal_cdev_update);
1457 * thermal_notify_framework - Sensor drivers use this API to notify framework
1458 * @tz: thermal zone device
1459 * @trip: indicates which trip point has been crossed
1461 * This function handles the trip events from sensor drivers. It starts
1462 * throttling the cooling devices according to the policy configured.
1463 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1464 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1465 * The throttling policy is based on the configured platform data; if no
1466 * platform data is provided, this uses the step_wise throttling policy.
1468 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1470 handle_thermal_trip(tz, trip);
1472 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1475 * create_trip_attrs() - create attributes for trip points
1476 * @tz: the thermal zone device
1477 * @mask: Writeable trip point bitmap.
1479 * helper function to instantiate sysfs entries for every trip
1480 * point and its properties of a struct thermal_zone_device.
1482 * Return: 0 on success, the proper error value otherwise.
1484 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1487 int size = sizeof(struct thermal_attr) * tz->trips;
1489 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1490 if (!tz->trip_type_attrs)
1493 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1494 if (!tz->trip_temp_attrs) {
1495 kfree(tz->trip_type_attrs);
1499 if (tz->ops->get_trip_hyst) {
1500 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1501 if (!tz->trip_hyst_attrs) {
1502 kfree(tz->trip_type_attrs);
1503 kfree(tz->trip_temp_attrs);
1509 for (indx = 0; indx < tz->trips; indx++) {
1510 /* create trip type attribute */
1511 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1512 "trip_point_%d_type", indx);
1514 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1515 tz->trip_type_attrs[indx].attr.attr.name =
1516 tz->trip_type_attrs[indx].name;
1517 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1518 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1520 device_create_file(&tz->device,
1521 &tz->trip_type_attrs[indx].attr);
1523 /* create trip temp attribute */
1524 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1525 "trip_point_%d_temp", indx);
1527 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1528 tz->trip_temp_attrs[indx].attr.attr.name =
1529 tz->trip_temp_attrs[indx].name;
1530 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1531 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1532 if (mask & (1 << indx)) {
1533 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1534 tz->trip_temp_attrs[indx].attr.store =
1535 trip_point_temp_store;
1538 device_create_file(&tz->device,
1539 &tz->trip_temp_attrs[indx].attr);
1541 /* create Optional trip hyst attribute */
1542 if (!tz->ops->get_trip_hyst)
1544 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1545 "trip_point_%d_hyst", indx);
1547 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1548 tz->trip_hyst_attrs[indx].attr.attr.name =
1549 tz->trip_hyst_attrs[indx].name;
1550 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1551 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1552 if (tz->ops->set_trip_hyst) {
1553 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1554 tz->trip_hyst_attrs[indx].attr.store =
1555 trip_point_hyst_store;
1558 device_create_file(&tz->device,
1559 &tz->trip_hyst_attrs[indx].attr);
1564 static void remove_trip_attrs(struct thermal_zone_device *tz)
1568 for (indx = 0; indx < tz->trips; indx++) {
1569 device_remove_file(&tz->device,
1570 &tz->trip_type_attrs[indx].attr);
1571 device_remove_file(&tz->device,
1572 &tz->trip_temp_attrs[indx].attr);
1573 if (tz->ops->get_trip_hyst)
1574 device_remove_file(&tz->device,
1575 &tz->trip_hyst_attrs[indx].attr);
1577 kfree(tz->trip_type_attrs);
1578 kfree(tz->trip_temp_attrs);
1579 kfree(tz->trip_hyst_attrs);
1583 * thermal_zone_device_register() - register a new thermal zone device
1584 * @type: the thermal zone device type
1585 * @trips: the number of trip points the thermal zone support
1586 * @mask: a bit string indicating the writeablility of trip points
1587 * @devdata: private device data
1588 * @ops: standard thermal zone device callbacks
1589 * @tzp: thermal zone platform parameters
1590 * @passive_delay: number of milliseconds to wait between polls when
1591 * performing passive cooling
1592 * @polling_delay: number of milliseconds to wait between polls when checking
1593 * whether trip points have been crossed (0 for interrupt
1596 * This interface function adds a new thermal zone device (sensor) to
1597 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1598 * thermal cooling devices registered at the same time.
1599 * thermal_zone_device_unregister() must be called when the device is no
1600 * longer needed. The passive cooling depends on the .get_trend() return value.
1602 * Return: a pointer to the created struct thermal_zone_device or an
1603 * in case of error, an ERR_PTR. Caller must check return value with
1604 * IS_ERR*() helpers.
1606 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1607 int trips, int mask, void *devdata,
1608 const struct thermal_zone_device_ops *ops,
1609 const struct thermal_zone_params *tzp,
1610 int passive_delay, int polling_delay)
1612 struct thermal_zone_device *tz;
1613 enum thermal_trip_type trip_type;
1618 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1619 return ERR_PTR(-EINVAL);
1621 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1622 return ERR_PTR(-EINVAL);
1624 if (!ops || !ops->get_temp)
1625 return ERR_PTR(-EINVAL);
1627 if (trips > 0 && !ops->get_trip_type)
1628 return ERR_PTR(-EINVAL);
1630 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1632 return ERR_PTR(-ENOMEM);
1634 INIT_LIST_HEAD(&tz->thermal_instances);
1636 mutex_init(&tz->lock);
1637 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1640 return ERR_PTR(result);
1643 strlcpy(tz->type, type ? : "", sizeof(tz->type));
1646 tz->device.class = &thermal_class;
1647 tz->devdata = devdata;
1649 tz->passive_delay = passive_delay;
1650 tz->polling_delay = polling_delay;
1652 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1653 result = device_register(&tz->device);
1655 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1657 return ERR_PTR(result);
1662 result = device_create_file(&tz->device, &dev_attr_type);
1667 result = device_create_file(&tz->device, &dev_attr_temp);
1671 if (ops->get_mode) {
1672 result = device_create_file(&tz->device, &dev_attr_mode);
1677 result = create_trip_attrs(tz, mask);
1681 for (count = 0; count < trips; count++) {
1682 tz->ops->get_trip_type(tz, count, &trip_type);
1683 if (trip_type == THERMAL_TRIP_PASSIVE)
1688 result = device_create_file(&tz->device, &dev_attr_passive);
1693 #ifdef CONFIG_THERMAL_EMULATION
1694 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1698 /* Create policy attribute */
1699 result = device_create_file(&tz->device, &dev_attr_policy);
1703 /* Update 'this' zone's governor information */
1704 mutex_lock(&thermal_governor_lock);
1707 tz->governor = __find_governor(tz->tzp->governor_name);
1709 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1711 mutex_unlock(&thermal_governor_lock);
1713 result = thermal_add_hwmon_sysfs(tz);
1717 mutex_lock(&thermal_list_lock);
1718 list_add_tail(&tz->node, &thermal_tz_list);
1719 mutex_unlock(&thermal_list_lock);
1721 /* Bind cooling devices for this zone */
1724 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1726 thermal_zone_device_update(tz);
1732 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1733 device_unregister(&tz->device);
1734 return ERR_PTR(result);
1736 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1739 * thermal_device_unregister - removes the registered thermal zone device
1740 * @tz: the thermal zone device to remove
1742 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1745 const struct thermal_zone_params *tzp;
1746 struct thermal_cooling_device *cdev;
1747 struct thermal_zone_device *pos = NULL;
1754 mutex_lock(&thermal_list_lock);
1755 list_for_each_entry(pos, &thermal_tz_list, node)
1759 /* thermal zone device not found */
1760 mutex_unlock(&thermal_list_lock);
1763 list_del(&tz->node);
1765 /* Unbind all cdevs associated with 'this' thermal zone */
1766 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1767 if (tz->ops->unbind) {
1768 tz->ops->unbind(tz, cdev);
1772 if (!tzp || !tzp->tbp)
1775 for (i = 0; i < tzp->num_tbps; i++) {
1776 if (tzp->tbp[i].cdev == cdev) {
1777 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1778 tzp->tbp[i].cdev = NULL;
1783 mutex_unlock(&thermal_list_lock);
1785 thermal_zone_device_set_polling(tz, 0);
1788 device_remove_file(&tz->device, &dev_attr_type);
1789 device_remove_file(&tz->device, &dev_attr_temp);
1790 if (tz->ops->get_mode)
1791 device_remove_file(&tz->device, &dev_attr_mode);
1792 device_remove_file(&tz->device, &dev_attr_policy);
1793 remove_trip_attrs(tz);
1794 tz->governor = NULL;
1796 thermal_remove_hwmon_sysfs(tz);
1797 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1798 idr_destroy(&tz->idr);
1799 mutex_destroy(&tz->lock);
1800 device_unregister(&tz->device);
1803 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1806 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1807 * @name: thermal zone name to fetch the temperature
1809 * When only one zone is found with the passed name, returns a reference to it.
1811 * Return: On success returns a reference to an unique thermal zone with
1812 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1813 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1815 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1817 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1818 unsigned int found = 0;
1823 mutex_lock(&thermal_list_lock);
1824 list_for_each_entry(pos, &thermal_tz_list, node)
1825 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1829 mutex_unlock(&thermal_list_lock);
1831 /* nothing has been found, thus an error code for it */
1833 ref = ERR_PTR(-ENODEV);
1835 /* Success only when an unique zone is found */
1836 ref = ERR_PTR(-EEXIST);
1841 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1844 static struct genl_family thermal_event_genl_family = {
1845 .id = GENL_ID_GENERATE,
1846 .name = THERMAL_GENL_FAMILY_NAME,
1847 .version = THERMAL_GENL_VERSION,
1848 .maxattr = THERMAL_GENL_ATTR_MAX,
1851 static struct genl_multicast_group thermal_event_mcgrp = {
1852 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1855 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1858 struct sk_buff *skb;
1859 struct nlattr *attr;
1860 struct thermal_genl_event *thermal_event;
1864 static unsigned int thermal_event_seqnum;
1869 /* allocate memory */
1870 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1873 skb = genlmsg_new(size, GFP_ATOMIC);
1877 /* add the genetlink message header */
1878 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1879 &thermal_event_genl_family, 0,
1880 THERMAL_GENL_CMD_EVENT);
1887 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1888 sizeof(struct thermal_genl_event));
1895 thermal_event = nla_data(attr);
1896 if (!thermal_event) {
1901 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1903 thermal_event->orig = tz->id;
1904 thermal_event->event = event;
1906 /* send multicast genetlink message */
1907 result = genlmsg_end(skb, msg_header);
1913 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1915 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1919 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1921 static int genetlink_init(void)
1925 result = genl_register_family(&thermal_event_genl_family);
1929 result = genl_register_mc_group(&thermal_event_genl_family,
1930 &thermal_event_mcgrp);
1932 genl_unregister_family(&thermal_event_genl_family);
1936 static void genetlink_exit(void)
1938 genl_unregister_family(&thermal_event_genl_family);
1940 #else /* !CONFIG_NET */
1941 static inline int genetlink_init(void) { return 0; }
1942 static inline void genetlink_exit(void) {}
1943 #endif /* !CONFIG_NET */
1945 static int __init thermal_register_governors(void)
1949 result = thermal_gov_step_wise_register();
1953 result = thermal_gov_fair_share_register();
1957 return thermal_gov_user_space_register();
1960 static void thermal_unregister_governors(void)
1962 thermal_gov_step_wise_unregister();
1963 thermal_gov_fair_share_unregister();
1964 thermal_gov_user_space_unregister();
1967 static int __init thermal_init(void)
1971 result = thermal_register_governors();
1975 result = class_register(&thermal_class);
1977 goto unregister_governors;
1979 result = genetlink_init();
1981 goto unregister_class;
1985 unregister_governors:
1986 thermal_unregister_governors();
1988 class_unregister(&thermal_class);
1990 idr_destroy(&thermal_tz_idr);
1991 idr_destroy(&thermal_cdev_idr);
1992 mutex_destroy(&thermal_idr_lock);
1993 mutex_destroy(&thermal_list_lock);
1994 mutex_destroy(&thermal_governor_lock);
1998 static void __exit thermal_exit(void)
2001 class_unregister(&thermal_class);
2002 thermal_unregister_governors();
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);
2010 fs_initcall(thermal_init);
2011 module_exit(thermal_exit);