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/spinlock.h>
36 #include <linux/reboot.h>
37 #include <net/netlink.h>
38 #include <net/genetlink.h>
40 #include "thermal_core.h"
42 MODULE_AUTHOR("Zhang Rui");
43 MODULE_DESCRIPTION("Generic thermal management sysfs support");
44 MODULE_LICENSE("GPL");
46 static DEFINE_IDR(thermal_tz_idr);
47 static DEFINE_IDR(thermal_cdev_idr);
48 static DEFINE_MUTEX(thermal_idr_lock);
50 static LIST_HEAD(thermal_tz_list);
51 static LIST_HEAD(thermal_cdev_list);
52 static LIST_HEAD(thermal_governor_list);
54 static DEFINE_MUTEX(thermal_list_lock);
55 static DEFINE_MUTEX(thermal_governor_lock);
57 static struct thermal_governor *__find_governor(const char *name)
59 struct thermal_governor *pos;
61 list_for_each_entry(pos, &thermal_governor_list, governor_list)
62 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
68 int thermal_register_governor(struct thermal_governor *governor)
72 struct thermal_zone_device *pos;
77 mutex_lock(&thermal_governor_lock);
80 if (__find_governor(governor->name) == NULL) {
82 list_add(&governor->governor_list, &thermal_governor_list);
85 mutex_lock(&thermal_list_lock);
87 list_for_each_entry(pos, &thermal_tz_list, node) {
91 name = pos->tzp->governor_name;
93 name = DEFAULT_THERMAL_GOVERNOR;
94 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
95 pos->governor = governor;
98 mutex_unlock(&thermal_list_lock);
99 mutex_unlock(&thermal_governor_lock);
103 EXPORT_SYMBOL_GPL(thermal_register_governor);
105 void thermal_unregister_governor(struct thermal_governor *governor)
107 struct thermal_zone_device *pos;
112 mutex_lock(&thermal_governor_lock);
114 if (__find_governor(governor->name) == NULL)
117 mutex_lock(&thermal_list_lock);
119 list_for_each_entry(pos, &thermal_tz_list, node) {
120 if (!strnicmp(pos->governor->name, governor->name,
121 THERMAL_NAME_LENGTH))
122 pos->governor = NULL;
125 mutex_unlock(&thermal_list_lock);
126 list_del(&governor->governor_list);
128 mutex_unlock(&thermal_governor_lock);
131 EXPORT_SYMBOL_GPL(thermal_unregister_governor);
133 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
138 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
143 err = idr_get_new(idr, NULL, id);
146 if (unlikely(err == -EAGAIN))
148 else if (unlikely(err))
151 *id = *id & MAX_IDR_MASK;
155 static void release_idr(struct idr *idr, struct mutex *lock, int id)
164 int get_tz_trend(struct thermal_zone_device *tz, int trip)
166 enum thermal_trend trend;
168 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
169 if (tz->temperature > tz->last_temperature)
170 trend = THERMAL_TREND_RAISING;
171 else if (tz->temperature < tz->last_temperature)
172 trend = THERMAL_TREND_DROPPING;
174 trend = THERMAL_TREND_STABLE;
179 EXPORT_SYMBOL(get_tz_trend);
181 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
182 struct thermal_cooling_device *cdev, int trip)
184 struct thermal_instance *pos = NULL;
185 struct thermal_instance *target_instance = NULL;
187 mutex_lock(&tz->lock);
188 mutex_lock(&cdev->lock);
190 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
191 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
192 target_instance = pos;
197 mutex_unlock(&cdev->lock);
198 mutex_unlock(&tz->lock);
200 return target_instance;
202 EXPORT_SYMBOL(get_thermal_instance);
204 static void print_bind_err_msg(struct thermal_zone_device *tz,
205 struct thermal_cooling_device *cdev, int ret)
207 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
208 tz->type, cdev->type, ret);
211 static void __bind(struct thermal_zone_device *tz, int mask,
212 struct thermal_cooling_device *cdev)
216 for (i = 0; i < tz->trips; i++) {
217 if (mask & (1 << i)) {
218 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
219 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
221 print_bind_err_msg(tz, cdev, ret);
226 static void __unbind(struct thermal_zone_device *tz, int mask,
227 struct thermal_cooling_device *cdev)
231 for (i = 0; i < tz->trips; i++)
233 thermal_zone_unbind_cooling_device(tz, i, cdev);
236 static void bind_cdev(struct thermal_cooling_device *cdev)
239 const struct thermal_zone_params *tzp;
240 struct thermal_zone_device *pos = NULL;
242 mutex_lock(&thermal_list_lock);
244 list_for_each_entry(pos, &thermal_tz_list, node) {
245 if (!pos->tzp && !pos->ops->bind)
248 if (!pos->tzp && pos->ops->bind) {
249 ret = pos->ops->bind(pos, cdev);
251 print_bind_err_msg(pos, cdev, ret);
258 for (i = 0; i < tzp->num_tbps; i++) {
259 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
261 if (tzp->tbp[i].match(pos, cdev))
263 tzp->tbp[i].cdev = cdev;
264 __bind(pos, tzp->tbp[i].trip_mask, cdev);
268 mutex_unlock(&thermal_list_lock);
271 static void bind_tz(struct thermal_zone_device *tz)
274 struct thermal_cooling_device *pos = NULL;
275 const struct thermal_zone_params *tzp = tz->tzp;
277 if (!tzp && !tz->ops->bind)
280 mutex_lock(&thermal_list_lock);
282 /* If there is no platform data, try to use ops->bind */
283 if (!tzp && tz->ops->bind) {
284 list_for_each_entry(pos, &thermal_cdev_list, node) {
285 ret = tz->ops->bind(tz, pos);
287 print_bind_err_msg(tz, pos, ret);
295 list_for_each_entry(pos, &thermal_cdev_list, node) {
296 for (i = 0; i < tzp->num_tbps; i++) {
297 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
299 if (tzp->tbp[i].match(tz, pos))
301 tzp->tbp[i].cdev = pos;
302 __bind(tz, tzp->tbp[i].trip_mask, pos);
306 mutex_unlock(&thermal_list_lock);
309 /* sys I/F for thermal zone */
311 #define to_thermal_zone(_dev) \
312 container_of(_dev, struct thermal_zone_device, device)
315 type_show(struct device *dev, struct device_attribute *attr, char *buf)
317 struct thermal_zone_device *tz = to_thermal_zone(dev);
319 return sprintf(buf, "%s\n", tz->type);
323 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
325 struct thermal_zone_device *tz = to_thermal_zone(dev);
329 if (!tz->ops->get_temp)
332 ret = tz->ops->get_temp(tz, &temperature);
337 return sprintf(buf, "%ld\n", temperature);
341 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
343 struct thermal_zone_device *tz = to_thermal_zone(dev);
344 enum thermal_device_mode mode;
347 if (!tz->ops->get_mode)
350 result = tz->ops->get_mode(tz, &mode);
354 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
359 mode_store(struct device *dev, struct device_attribute *attr,
360 const char *buf, size_t count)
362 struct thermal_zone_device *tz = to_thermal_zone(dev);
365 if (!tz->ops->set_mode)
368 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
369 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
370 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
371 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
382 trip_point_type_show(struct device *dev, struct device_attribute *attr,
385 struct thermal_zone_device *tz = to_thermal_zone(dev);
386 enum thermal_trip_type type;
389 if (!tz->ops->get_trip_type)
392 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
395 result = tz->ops->get_trip_type(tz, trip, &type);
400 case THERMAL_TRIP_CRITICAL:
401 return sprintf(buf, "critical\n");
402 case THERMAL_TRIP_HOT:
403 return sprintf(buf, "hot\n");
404 case THERMAL_TRIP_PASSIVE:
405 return sprintf(buf, "passive\n");
406 case THERMAL_TRIP_ACTIVE:
407 return sprintf(buf, "active\n");
409 return sprintf(buf, "unknown\n");
414 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
415 const char *buf, size_t count)
417 struct thermal_zone_device *tz = to_thermal_zone(dev);
419 unsigned long temperature;
421 if (!tz->ops->set_trip_temp)
424 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
427 if (kstrtoul(buf, 10, &temperature))
430 ret = tz->ops->set_trip_temp(tz, trip, temperature);
432 return ret ? ret : count;
436 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
439 struct thermal_zone_device *tz = to_thermal_zone(dev);
443 if (!tz->ops->get_trip_temp)
446 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
449 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
454 return sprintf(buf, "%ld\n", temperature);
458 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
459 const char *buf, size_t count)
461 struct thermal_zone_device *tz = to_thermal_zone(dev);
463 unsigned long temperature;
465 if (!tz->ops->set_trip_hyst)
468 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
471 if (kstrtoul(buf, 10, &temperature))
475 * We are not doing any check on the 'temperature' value
476 * here. The driver implementing 'set_trip_hyst' has to
479 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
481 return ret ? ret : count;
485 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
488 struct thermal_zone_device *tz = to_thermal_zone(dev);
490 unsigned long temperature;
492 if (!tz->ops->get_trip_hyst)
495 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
498 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
500 return ret ? ret : sprintf(buf, "%ld\n", temperature);
504 passive_store(struct device *dev, struct device_attribute *attr,
505 const char *buf, size_t count)
507 struct thermal_zone_device *tz = to_thermal_zone(dev);
508 struct thermal_cooling_device *cdev = NULL;
511 if (!sscanf(buf, "%d\n", &state))
514 /* sanity check: values below 1000 millicelcius don't make sense
515 * and can cause the system to go into a thermal heart attack
517 if (state && state < 1000)
520 if (state && !tz->forced_passive) {
521 mutex_lock(&thermal_list_lock);
522 list_for_each_entry(cdev, &thermal_cdev_list, node) {
523 if (!strncmp("Processor", cdev->type,
524 sizeof("Processor")))
525 thermal_zone_bind_cooling_device(tz,
526 THERMAL_TRIPS_NONE, cdev,
530 mutex_unlock(&thermal_list_lock);
531 if (!tz->passive_delay)
532 tz->passive_delay = 1000;
533 } else if (!state && tz->forced_passive) {
534 mutex_lock(&thermal_list_lock);
535 list_for_each_entry(cdev, &thermal_cdev_list, node) {
536 if (!strncmp("Processor", cdev->type,
537 sizeof("Processor")))
538 thermal_zone_unbind_cooling_device(tz,
542 mutex_unlock(&thermal_list_lock);
543 tz->passive_delay = 0;
546 tz->forced_passive = state;
548 thermal_zone_device_update(tz);
554 passive_show(struct device *dev, struct device_attribute *attr,
557 struct thermal_zone_device *tz = to_thermal_zone(dev);
559 return sprintf(buf, "%d\n", tz->forced_passive);
563 policy_store(struct device *dev, struct device_attribute *attr,
564 const char *buf, size_t count)
567 struct thermal_zone_device *tz = to_thermal_zone(dev);
568 struct thermal_governor *gov;
570 mutex_lock(&thermal_governor_lock);
572 gov = __find_governor(buf);
580 mutex_unlock(&thermal_governor_lock);
585 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
587 struct thermal_zone_device *tz = to_thermal_zone(dev);
589 return sprintf(buf, "%s\n", tz->governor->name);
592 static DEVICE_ATTR(type, 0444, type_show, NULL);
593 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
594 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
595 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
596 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
598 /* sys I/F for cooling device */
599 #define to_cooling_device(_dev) \
600 container_of(_dev, struct thermal_cooling_device, device)
603 thermal_cooling_device_type_show(struct device *dev,
604 struct device_attribute *attr, char *buf)
606 struct thermal_cooling_device *cdev = to_cooling_device(dev);
608 return sprintf(buf, "%s\n", cdev->type);
612 thermal_cooling_device_max_state_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
615 struct thermal_cooling_device *cdev = to_cooling_device(dev);
619 ret = cdev->ops->get_max_state(cdev, &state);
622 return sprintf(buf, "%ld\n", state);
626 thermal_cooling_device_cur_state_show(struct device *dev,
627 struct device_attribute *attr, char *buf)
629 struct thermal_cooling_device *cdev = to_cooling_device(dev);
633 ret = cdev->ops->get_cur_state(cdev, &state);
636 return sprintf(buf, "%ld\n", state);
640 thermal_cooling_device_cur_state_store(struct device *dev,
641 struct device_attribute *attr,
642 const char *buf, size_t count)
644 struct thermal_cooling_device *cdev = to_cooling_device(dev);
648 if (!sscanf(buf, "%ld\n", &state))
654 result = cdev->ops->set_cur_state(cdev, state);
660 static struct device_attribute dev_attr_cdev_type =
661 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
662 static DEVICE_ATTR(max_state, 0444,
663 thermal_cooling_device_max_state_show, NULL);
664 static DEVICE_ATTR(cur_state, 0644,
665 thermal_cooling_device_cur_state_show,
666 thermal_cooling_device_cur_state_store);
669 thermal_cooling_device_trip_point_show(struct device *dev,
670 struct device_attribute *attr, char *buf)
672 struct thermal_instance *instance;
675 container_of(attr, struct thermal_instance, attr);
677 if (instance->trip == THERMAL_TRIPS_NONE)
678 return sprintf(buf, "-1\n");
680 return sprintf(buf, "%d\n", instance->trip);
683 /* Device management */
685 #if defined(CONFIG_THERMAL_HWMON)
688 #include <linux/hwmon.h>
690 /* thermal zone devices with the same type share one hwmon device */
691 struct thermal_hwmon_device {
692 char type[THERMAL_NAME_LENGTH];
693 struct device *device;
695 struct list_head tz_list;
696 struct list_head node;
699 struct thermal_hwmon_attr {
700 struct device_attribute attr;
704 /* one temperature input for each thermal zone */
705 struct thermal_hwmon_temp {
706 struct list_head hwmon_node;
707 struct thermal_zone_device *tz;
708 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
709 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
712 static LIST_HEAD(thermal_hwmon_list);
715 name_show(struct device *dev, struct device_attribute *attr, char *buf)
717 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
718 return sprintf(buf, "%s\n", hwmon->type);
720 static DEVICE_ATTR(name, 0444, name_show, NULL);
723 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
727 struct thermal_hwmon_attr *hwmon_attr
728 = container_of(attr, struct thermal_hwmon_attr, attr);
729 struct thermal_hwmon_temp *temp
730 = container_of(hwmon_attr, struct thermal_hwmon_temp,
732 struct thermal_zone_device *tz = temp->tz;
734 ret = tz->ops->get_temp(tz, &temperature);
739 return sprintf(buf, "%ld\n", temperature);
743 temp_crit_show(struct device *dev, struct device_attribute *attr,
746 struct thermal_hwmon_attr *hwmon_attr
747 = container_of(attr, struct thermal_hwmon_attr, attr);
748 struct thermal_hwmon_temp *temp
749 = container_of(hwmon_attr, struct thermal_hwmon_temp,
751 struct thermal_zone_device *tz = temp->tz;
755 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
759 return sprintf(buf, "%ld\n", temperature);
763 static struct thermal_hwmon_device *
764 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
766 struct thermal_hwmon_device *hwmon;
768 mutex_lock(&thermal_list_lock);
769 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
770 if (!strcmp(hwmon->type, tz->type)) {
771 mutex_unlock(&thermal_list_lock);
774 mutex_unlock(&thermal_list_lock);
779 /* Find the temperature input matching a given thermal zone */
780 static struct thermal_hwmon_temp *
781 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
782 const struct thermal_zone_device *tz)
784 struct thermal_hwmon_temp *temp;
786 mutex_lock(&thermal_list_lock);
787 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
788 if (temp->tz == tz) {
789 mutex_unlock(&thermal_list_lock);
792 mutex_unlock(&thermal_list_lock);
798 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
800 struct thermal_hwmon_device *hwmon;
801 struct thermal_hwmon_temp *temp;
802 int new_hwmon_device = 1;
805 hwmon = thermal_hwmon_lookup_by_type(tz);
807 new_hwmon_device = 0;
808 goto register_sys_interface;
811 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
815 INIT_LIST_HEAD(&hwmon->tz_list);
816 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
817 hwmon->device = hwmon_device_register(NULL);
818 if (IS_ERR(hwmon->device)) {
819 result = PTR_ERR(hwmon->device);
822 dev_set_drvdata(hwmon->device, hwmon);
823 result = device_create_file(hwmon->device, &dev_attr_name);
827 register_sys_interface:
828 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
831 goto unregister_name;
837 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
838 "temp%d_input", hwmon->count);
839 temp->temp_input.attr.attr.name = temp->temp_input.name;
840 temp->temp_input.attr.attr.mode = 0444;
841 temp->temp_input.attr.show = temp_input_show;
842 sysfs_attr_init(&temp->temp_input.attr.attr);
843 result = device_create_file(hwmon->device, &temp->temp_input.attr);
847 if (tz->ops->get_crit_temp) {
848 unsigned long temperature;
849 if (!tz->ops->get_crit_temp(tz, &temperature)) {
850 snprintf(temp->temp_crit.name,
851 sizeof(temp->temp_crit.name),
852 "temp%d_crit", hwmon->count);
853 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
854 temp->temp_crit.attr.attr.mode = 0444;
855 temp->temp_crit.attr.show = temp_crit_show;
856 sysfs_attr_init(&temp->temp_crit.attr.attr);
857 result = device_create_file(hwmon->device,
858 &temp->temp_crit.attr);
860 goto unregister_input;
864 mutex_lock(&thermal_list_lock);
865 if (new_hwmon_device)
866 list_add_tail(&hwmon->node, &thermal_hwmon_list);
867 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
868 mutex_unlock(&thermal_list_lock);
873 device_remove_file(hwmon->device, &temp->temp_input.attr);
877 if (new_hwmon_device) {
878 device_remove_file(hwmon->device, &dev_attr_name);
879 hwmon_device_unregister(hwmon->device);
882 if (new_hwmon_device)
889 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
891 struct thermal_hwmon_device *hwmon;
892 struct thermal_hwmon_temp *temp;
894 hwmon = thermal_hwmon_lookup_by_type(tz);
895 if (unlikely(!hwmon)) {
896 /* Should never happen... */
897 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
901 temp = thermal_hwmon_lookup_temp(hwmon, tz);
902 if (unlikely(!temp)) {
903 /* Should never happen... */
904 dev_dbg(&tz->device, "temperature input lookup failed!\n");
908 device_remove_file(hwmon->device, &temp->temp_input.attr);
909 if (tz->ops->get_crit_temp)
910 device_remove_file(hwmon->device, &temp->temp_crit.attr);
912 mutex_lock(&thermal_list_lock);
913 list_del(&temp->hwmon_node);
915 if (!list_empty(&hwmon->tz_list)) {
916 mutex_unlock(&thermal_list_lock);
919 list_del(&hwmon->node);
920 mutex_unlock(&thermal_list_lock);
922 device_remove_file(hwmon->device, &dev_attr_name);
923 hwmon_device_unregister(hwmon->device);
928 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
934 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
939 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
943 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
944 round_jiffies(msecs_to_jiffies(delay)));
946 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
947 msecs_to_jiffies(delay));
949 cancel_delayed_work(&tz->poll_queue);
952 static void thermal_zone_device_check(struct work_struct *work)
954 struct thermal_zone_device *tz = container_of(work, struct
957 thermal_zone_device_update(tz);
961 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
962 * @tz: thermal zone device
963 * @trip: indicates which trip point the cooling devices is
964 * associated with in this thermal zone.
965 * @cdev: thermal cooling device
967 * This function is usually called in the thermal zone device .bind callback.
969 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
971 struct thermal_cooling_device *cdev,
972 unsigned long upper, unsigned long lower)
974 struct thermal_instance *dev;
975 struct thermal_instance *pos;
976 struct thermal_zone_device *pos1;
977 struct thermal_cooling_device *pos2;
978 unsigned long max_state;
981 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
984 list_for_each_entry(pos1, &thermal_tz_list, node) {
988 list_for_each_entry(pos2, &thermal_cdev_list, node) {
993 if (tz != pos1 || cdev != pos2)
996 cdev->ops->get_max_state(cdev, &max_state);
998 /* lower default 0, upper default max_state */
999 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1000 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1002 if (lower > upper || upper > max_state)
1006 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1014 dev->target = THERMAL_NO_TARGET;
1016 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1020 sprintf(dev->name, "cdev%d", dev->id);
1022 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1026 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1027 sysfs_attr_init(&dev->attr.attr);
1028 dev->attr.attr.name = dev->attr_name;
1029 dev->attr.attr.mode = 0444;
1030 dev->attr.show = thermal_cooling_device_trip_point_show;
1031 result = device_create_file(&tz->device, &dev->attr);
1033 goto remove_symbol_link;
1035 mutex_lock(&tz->lock);
1036 mutex_lock(&cdev->lock);
1037 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1038 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1043 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1044 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1046 mutex_unlock(&cdev->lock);
1047 mutex_unlock(&tz->lock);
1052 device_remove_file(&tz->device, &dev->attr);
1054 sysfs_remove_link(&tz->device.kobj, dev->name);
1056 release_idr(&tz->idr, &tz->lock, dev->id);
1061 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1064 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
1065 * @tz: thermal zone device
1066 * @trip: indicates which trip point the cooling devices is
1067 * associated with in this thermal zone.
1068 * @cdev: thermal cooling device
1070 * This function is usually called in the thermal zone device .unbind callback.
1072 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1074 struct thermal_cooling_device *cdev)
1076 struct thermal_instance *pos, *next;
1078 mutex_lock(&tz->lock);
1079 mutex_lock(&cdev->lock);
1080 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1081 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1082 list_del(&pos->tz_node);
1083 list_del(&pos->cdev_node);
1084 mutex_unlock(&cdev->lock);
1085 mutex_unlock(&tz->lock);
1089 mutex_unlock(&cdev->lock);
1090 mutex_unlock(&tz->lock);
1095 device_remove_file(&tz->device, &pos->attr);
1096 sysfs_remove_link(&tz->device.kobj, pos->name);
1097 release_idr(&tz->idr, &tz->lock, pos->id);
1101 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1103 static void thermal_release(struct device *dev)
1105 struct thermal_zone_device *tz;
1106 struct thermal_cooling_device *cdev;
1108 if (!strncmp(dev_name(dev), "thermal_zone",
1109 sizeof("thermal_zone") - 1)) {
1110 tz = to_thermal_zone(dev);
1113 cdev = to_cooling_device(dev);
1118 static struct class thermal_class = {
1120 .dev_release = thermal_release,
1124 * thermal_cooling_device_register - register a new thermal cooling device
1125 * @type: the thermal cooling device type.
1126 * @devdata: device private data.
1127 * @ops: standard thermal cooling devices callbacks.
1129 struct thermal_cooling_device *
1130 thermal_cooling_device_register(char *type, void *devdata,
1131 const struct thermal_cooling_device_ops *ops)
1133 struct thermal_cooling_device *cdev;
1136 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1137 return ERR_PTR(-EINVAL);
1139 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1140 !ops->set_cur_state)
1141 return ERR_PTR(-EINVAL);
1143 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1145 return ERR_PTR(-ENOMEM);
1147 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1150 return ERR_PTR(result);
1153 strcpy(cdev->type, type ? : "");
1154 mutex_init(&cdev->lock);
1155 INIT_LIST_HEAD(&cdev->thermal_instances);
1157 cdev->updated = true;
1158 cdev->device.class = &thermal_class;
1159 cdev->devdata = devdata;
1160 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1161 result = device_register(&cdev->device);
1163 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1165 return ERR_PTR(result);
1170 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1175 result = device_create_file(&cdev->device, &dev_attr_max_state);
1179 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1183 /* Add 'this' new cdev to the global cdev list */
1184 mutex_lock(&thermal_list_lock);
1185 list_add(&cdev->node, &thermal_cdev_list);
1186 mutex_unlock(&thermal_list_lock);
1188 /* Update binding information for 'this' new cdev */
1194 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1195 device_unregister(&cdev->device);
1196 return ERR_PTR(result);
1198 EXPORT_SYMBOL(thermal_cooling_device_register);
1201 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1202 * @cdev: the thermal cooling device to remove.
1204 * thermal_cooling_device_unregister() must be called when the device is no
1207 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1210 const struct thermal_zone_params *tzp;
1211 struct thermal_zone_device *tz;
1212 struct thermal_cooling_device *pos = NULL;
1217 mutex_lock(&thermal_list_lock);
1218 list_for_each_entry(pos, &thermal_cdev_list, node)
1222 /* thermal cooling device not found */
1223 mutex_unlock(&thermal_list_lock);
1226 list_del(&cdev->node);
1228 /* Unbind all thermal zones associated with 'this' cdev */
1229 list_for_each_entry(tz, &thermal_tz_list, node) {
1230 if (tz->ops->unbind) {
1231 tz->ops->unbind(tz, cdev);
1235 if (!tz->tzp || !tz->tzp->tbp)
1239 for (i = 0; i < tzp->num_tbps; i++) {
1240 if (tzp->tbp[i].cdev == cdev) {
1241 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1242 tzp->tbp[i].cdev = NULL;
1247 mutex_unlock(&thermal_list_lock);
1250 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1251 device_remove_file(&cdev->device, &dev_attr_max_state);
1252 device_remove_file(&cdev->device, &dev_attr_cur_state);
1254 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1255 device_unregister(&cdev->device);
1258 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1260 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1262 struct thermal_instance *instance;
1263 unsigned long target = 0;
1265 /* cooling device is updated*/
1269 mutex_lock(&cdev->lock);
1270 /* Make sure cdev enters the deepest cooling state */
1271 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1272 if (instance->target == THERMAL_NO_TARGET)
1274 if (instance->target > target)
1275 target = instance->target;
1277 mutex_unlock(&cdev->lock);
1278 cdev->ops->set_cur_state(cdev, target);
1279 cdev->updated = true;
1281 EXPORT_SYMBOL(thermal_cdev_update);
1283 static void thermal_zone_do_update(struct thermal_zone_device *tz)
1285 struct thermal_instance *instance;
1287 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
1288 thermal_cdev_update(instance->cdev);
1292 * Cooling algorithm for both active and passive cooling
1294 * 1. if the temperature is higher than a trip point,
1295 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1296 * state for this trip point
1297 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1298 * state for this trip point
1300 * 2. if the temperature is lower than a trip point, use lower
1301 * cooling state for this trip point
1303 * Note that this behaves the same as the previous passive cooling
1307 static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1308 int trip, long temp)
1310 struct thermal_instance *instance;
1311 struct thermal_cooling_device *cdev = NULL;
1312 unsigned long cur_state, max_state;
1314 enum thermal_trip_type trip_type;
1315 enum thermal_trend trend;
1317 if (trip == THERMAL_TRIPS_NONE) {
1318 trip_temp = tz->forced_passive;
1319 trip_type = THERMAL_TRIPS_NONE;
1321 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1322 tz->ops->get_trip_type(tz, trip, &trip_type);
1325 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1327 * compare the current temperature and previous temperature
1328 * to get the thermal trend, if no special requirement
1330 if (tz->temperature > tz->last_temperature)
1331 trend = THERMAL_TREND_RAISING;
1332 else if (tz->temperature < tz->last_temperature)
1333 trend = THERMAL_TREND_DROPPING;
1335 trend = THERMAL_TREND_STABLE;
1338 if (temp >= trip_temp) {
1339 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1340 if (instance->trip != trip)
1343 cdev = instance->cdev;
1345 cdev->ops->get_cur_state(cdev, &cur_state);
1346 cdev->ops->get_max_state(cdev, &max_state);
1348 if (trend == THERMAL_TREND_RAISING) {
1349 cur_state = cur_state < instance->upper ?
1350 (cur_state + 1) : instance->upper;
1351 } else if (trend == THERMAL_TREND_DROPPING) {
1352 cur_state = cur_state > instance->lower ?
1353 (cur_state - 1) : instance->lower;
1356 /* activate a passive thermal instance */
1357 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1358 trip_type == THERMAL_TRIPS_NONE) &&
1359 instance->target == THERMAL_NO_TARGET)
1362 instance->target = cur_state;
1363 cdev->updated = false; /* cooling device needs update */
1365 } else { /* below trip */
1366 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1367 if (instance->trip != trip)
1370 /* Do not use the inactive thermal instance */
1371 if (instance->target == THERMAL_NO_TARGET)
1373 cdev = instance->cdev;
1374 cdev->ops->get_cur_state(cdev, &cur_state);
1376 cur_state = cur_state > instance->lower ?
1377 (cur_state - 1) : THERMAL_NO_TARGET;
1379 /* deactivate a passive thermal instance */
1380 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1381 trip_type == THERMAL_TRIPS_NONE) &&
1382 cur_state == THERMAL_NO_TARGET)
1384 instance->target = cur_state;
1385 cdev->updated = false; /* cooling device needs update */
1392 * thermal_zone_device_update - force an update of a thermal zone's state
1393 * @ttz: the thermal zone to update
1396 void thermal_zone_device_update(struct thermal_zone_device *tz)
1399 long temp, trip_temp;
1400 enum thermal_trip_type trip_type;
1402 mutex_lock(&tz->lock);
1404 if (tz->ops->get_temp(tz, &temp)) {
1405 /* get_temp failed - retry it later */
1406 pr_warn("failed to read out thermal zone %d\n", tz->id);
1410 tz->last_temperature = tz->temperature;
1411 tz->temperature = temp;
1413 for (count = 0; count < tz->trips; count++) {
1414 tz->ops->get_trip_type(tz, count, &trip_type);
1415 tz->ops->get_trip_temp(tz, count, &trip_temp);
1417 switch (trip_type) {
1418 case THERMAL_TRIP_CRITICAL:
1419 if (temp >= trip_temp) {
1420 if (tz->ops->notify)
1421 ret = tz->ops->notify(tz, count,
1424 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1426 orderly_poweroff(true);
1430 case THERMAL_TRIP_HOT:
1431 if (temp >= trip_temp)
1432 if (tz->ops->notify)
1433 tz->ops->notify(tz, count, trip_type);
1435 case THERMAL_TRIP_ACTIVE:
1436 thermal_zone_trip_update(tz, count, temp);
1438 case THERMAL_TRIP_PASSIVE:
1439 if (temp >= trip_temp || tz->passive)
1440 thermal_zone_trip_update(tz, count, temp);
1445 if (tz->forced_passive)
1446 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
1447 thermal_zone_do_update(tz);
1451 thermal_zone_device_set_polling(tz, tz->passive_delay);
1452 else if (tz->polling_delay)
1453 thermal_zone_device_set_polling(tz, tz->polling_delay);
1455 thermal_zone_device_set_polling(tz, 0);
1456 mutex_unlock(&tz->lock);
1458 EXPORT_SYMBOL(thermal_zone_device_update);
1461 * create_trip_attrs - create attributes for trip points
1462 * @tz: the thermal zone device
1463 * @mask: Writeable trip point bitmap.
1465 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1468 int size = sizeof(struct thermal_attr) * tz->trips;
1470 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1471 if (!tz->trip_type_attrs)
1474 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1475 if (!tz->trip_temp_attrs) {
1476 kfree(tz->trip_type_attrs);
1480 if (tz->ops->get_trip_hyst) {
1481 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1482 if (!tz->trip_hyst_attrs) {
1483 kfree(tz->trip_type_attrs);
1484 kfree(tz->trip_temp_attrs);
1490 for (indx = 0; indx < tz->trips; indx++) {
1491 /* create trip type attribute */
1492 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1493 "trip_point_%d_type", indx);
1495 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1496 tz->trip_type_attrs[indx].attr.attr.name =
1497 tz->trip_type_attrs[indx].name;
1498 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1499 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1501 device_create_file(&tz->device,
1502 &tz->trip_type_attrs[indx].attr);
1504 /* create trip temp attribute */
1505 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1506 "trip_point_%d_temp", indx);
1508 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1509 tz->trip_temp_attrs[indx].attr.attr.name =
1510 tz->trip_temp_attrs[indx].name;
1511 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1512 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1513 if (mask & (1 << indx)) {
1514 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1515 tz->trip_temp_attrs[indx].attr.store =
1516 trip_point_temp_store;
1519 device_create_file(&tz->device,
1520 &tz->trip_temp_attrs[indx].attr);
1522 /* create Optional trip hyst attribute */
1523 if (!tz->ops->get_trip_hyst)
1525 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1526 "trip_point_%d_hyst", indx);
1528 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1529 tz->trip_hyst_attrs[indx].attr.attr.name =
1530 tz->trip_hyst_attrs[indx].name;
1531 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1532 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1533 if (tz->ops->set_trip_hyst) {
1534 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1535 tz->trip_hyst_attrs[indx].attr.store =
1536 trip_point_hyst_store;
1539 device_create_file(&tz->device,
1540 &tz->trip_hyst_attrs[indx].attr);
1545 static void remove_trip_attrs(struct thermal_zone_device *tz)
1549 for (indx = 0; indx < tz->trips; indx++) {
1550 device_remove_file(&tz->device,
1551 &tz->trip_type_attrs[indx].attr);
1552 device_remove_file(&tz->device,
1553 &tz->trip_temp_attrs[indx].attr);
1554 if (tz->ops->get_trip_hyst)
1555 device_remove_file(&tz->device,
1556 &tz->trip_hyst_attrs[indx].attr);
1558 kfree(tz->trip_type_attrs);
1559 kfree(tz->trip_temp_attrs);
1560 kfree(tz->trip_hyst_attrs);
1564 * thermal_zone_device_register - register a new thermal zone device
1565 * @type: the thermal zone device type
1566 * @trips: the number of trip points the thermal zone support
1567 * @mask: a bit string indicating the writeablility of trip points
1568 * @devdata: private device data
1569 * @ops: standard thermal zone device callbacks
1570 * @tzp: thermal zone platform parameters
1571 * @passive_delay: number of milliseconds to wait between polls when
1572 * performing passive cooling
1573 * @polling_delay: number of milliseconds to wait between polls when checking
1574 * whether trip points have been crossed (0 for interrupt
1577 * thermal_zone_device_unregister() must be called when the device is no
1578 * longer needed. The passive cooling depends on the .get_trend() return value.
1580 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1581 int trips, int mask, void *devdata,
1582 const struct thermal_zone_device_ops *ops,
1583 const struct thermal_zone_params *tzp,
1584 int passive_delay, int polling_delay)
1586 struct thermal_zone_device *tz;
1587 enum thermal_trip_type trip_type;
1592 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1593 return ERR_PTR(-EINVAL);
1595 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1596 return ERR_PTR(-EINVAL);
1598 if (!ops || !ops->get_temp)
1599 return ERR_PTR(-EINVAL);
1601 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1603 return ERR_PTR(-ENOMEM);
1605 INIT_LIST_HEAD(&tz->thermal_instances);
1607 mutex_init(&tz->lock);
1608 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1611 return ERR_PTR(result);
1614 strcpy(tz->type, type ? : "");
1617 tz->device.class = &thermal_class;
1618 tz->devdata = devdata;
1620 tz->passive_delay = passive_delay;
1621 tz->polling_delay = polling_delay;
1623 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1624 result = device_register(&tz->device);
1626 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1628 return ERR_PTR(result);
1633 result = device_create_file(&tz->device, &dev_attr_type);
1638 result = device_create_file(&tz->device, &dev_attr_temp);
1642 if (ops->get_mode) {
1643 result = device_create_file(&tz->device, &dev_attr_mode);
1648 result = create_trip_attrs(tz, mask);
1652 for (count = 0; count < trips; count++) {
1653 tz->ops->get_trip_type(tz, count, &trip_type);
1654 if (trip_type == THERMAL_TRIP_PASSIVE)
1659 result = device_create_file(&tz->device, &dev_attr_passive);
1664 /* Create policy attribute */
1665 result = device_create_file(&tz->device, &dev_attr_policy);
1669 /* Update 'this' zone's governor information */
1670 mutex_lock(&thermal_governor_lock);
1673 tz->governor = __find_governor(tz->tzp->governor_name);
1675 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1677 mutex_unlock(&thermal_governor_lock);
1679 result = thermal_add_hwmon_sysfs(tz);
1683 mutex_lock(&thermal_list_lock);
1684 list_add_tail(&tz->node, &thermal_tz_list);
1685 mutex_unlock(&thermal_list_lock);
1687 /* Bind cooling devices for this zone */
1690 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1692 thermal_zone_device_update(tz);
1698 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1699 device_unregister(&tz->device);
1700 return ERR_PTR(result);
1702 EXPORT_SYMBOL(thermal_zone_device_register);
1705 * thermal_device_unregister - removes the registered thermal zone device
1706 * @tz: the thermal zone device to remove
1708 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1711 const struct thermal_zone_params *tzp;
1712 struct thermal_cooling_device *cdev;
1713 struct thermal_zone_device *pos = NULL;
1720 mutex_lock(&thermal_list_lock);
1721 list_for_each_entry(pos, &thermal_tz_list, node)
1725 /* thermal zone device not found */
1726 mutex_unlock(&thermal_list_lock);
1729 list_del(&tz->node);
1731 /* Unbind all cdevs associated with 'this' thermal zone */
1732 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1733 if (tz->ops->unbind) {
1734 tz->ops->unbind(tz, cdev);
1738 if (!tzp || !tzp->tbp)
1741 for (i = 0; i < tzp->num_tbps; i++) {
1742 if (tzp->tbp[i].cdev == cdev) {
1743 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1744 tzp->tbp[i].cdev = NULL;
1749 mutex_unlock(&thermal_list_lock);
1751 thermal_zone_device_set_polling(tz, 0);
1754 device_remove_file(&tz->device, &dev_attr_type);
1755 device_remove_file(&tz->device, &dev_attr_temp);
1756 if (tz->ops->get_mode)
1757 device_remove_file(&tz->device, &dev_attr_mode);
1758 device_remove_file(&tz->device, &dev_attr_policy);
1759 remove_trip_attrs(tz);
1760 tz->governor = NULL;
1762 thermal_remove_hwmon_sysfs(tz);
1763 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1764 idr_destroy(&tz->idr);
1765 mutex_destroy(&tz->lock);
1766 device_unregister(&tz->device);
1769 EXPORT_SYMBOL(thermal_zone_device_unregister);
1772 static struct genl_family thermal_event_genl_family = {
1773 .id = GENL_ID_GENERATE,
1774 .name = THERMAL_GENL_FAMILY_NAME,
1775 .version = THERMAL_GENL_VERSION,
1776 .maxattr = THERMAL_GENL_ATTR_MAX,
1779 static struct genl_multicast_group thermal_event_mcgrp = {
1780 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1783 int thermal_generate_netlink_event(u32 orig, enum events event)
1785 struct sk_buff *skb;
1786 struct nlattr *attr;
1787 struct thermal_genl_event *thermal_event;
1791 static unsigned int thermal_event_seqnum;
1793 /* allocate memory */
1794 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1797 skb = genlmsg_new(size, GFP_ATOMIC);
1801 /* add the genetlink message header */
1802 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1803 &thermal_event_genl_family, 0,
1804 THERMAL_GENL_CMD_EVENT);
1811 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1812 sizeof(struct thermal_genl_event));
1819 thermal_event = nla_data(attr);
1820 if (!thermal_event) {
1825 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1827 thermal_event->orig = orig;
1828 thermal_event->event = event;
1830 /* send multicast genetlink message */
1831 result = genlmsg_end(skb, msg_header);
1837 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1839 pr_info("failed to send netlink event:%d\n", result);
1843 EXPORT_SYMBOL(thermal_generate_netlink_event);
1845 static int genetlink_init(void)
1849 result = genl_register_family(&thermal_event_genl_family);
1853 result = genl_register_mc_group(&thermal_event_genl_family,
1854 &thermal_event_mcgrp);
1856 genl_unregister_family(&thermal_event_genl_family);
1860 static void genetlink_exit(void)
1862 genl_unregister_family(&thermal_event_genl_family);
1864 #else /* !CONFIG_NET */
1865 static inline int genetlink_init(void) { return 0; }
1866 static inline void genetlink_exit(void) {}
1867 #endif /* !CONFIG_NET */
1869 static int __init thermal_init(void)
1873 result = class_register(&thermal_class);
1875 idr_destroy(&thermal_tz_idr);
1876 idr_destroy(&thermal_cdev_idr);
1877 mutex_destroy(&thermal_idr_lock);
1878 mutex_destroy(&thermal_list_lock);
1880 result = genetlink_init();
1884 static void __exit thermal_exit(void)
1886 class_unregister(&thermal_class);
1887 idr_destroy(&thermal_tz_idr);
1888 idr_destroy(&thermal_cdev_idr);
1889 mutex_destroy(&thermal_idr_lock);
1890 mutex_destroy(&thermal_list_lock);
1894 fs_initcall(thermal_init);
1895 module_exit(thermal_exit);