2 * thermal.c - Generic Thermal Management Sysfs support.
4 * Copyright (C) 2008 Intel Corp
5 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
6 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/slab.h>
32 #include <linux/kdev_t.h>
33 #include <linux/idr.h>
34 #include <linux/thermal.h>
35 #include <linux/reboot.h>
36 #include <linux/string.h>
38 #include <net/netlink.h>
39 #include <net/genetlink.h>
41 #include "thermal_core.h"
42 #include "thermal_hwmon.h"
44 MODULE_AUTHOR("Zhang Rui");
45 MODULE_DESCRIPTION("Generic thermal management sysfs support");
46 MODULE_LICENSE("GPL v2");
48 static DEFINE_IDR(thermal_tz_idr);
49 static DEFINE_IDR(thermal_cdev_idr);
50 static DEFINE_MUTEX(thermal_idr_lock);
52 static LIST_HEAD(thermal_tz_list);
53 static LIST_HEAD(thermal_cdev_list);
54 static LIST_HEAD(thermal_governor_list);
56 static DEFINE_MUTEX(thermal_list_lock);
57 static DEFINE_MUTEX(thermal_governor_lock);
59 static struct thermal_governor *def_governor;
61 static struct thermal_governor *__find_governor(const char *name)
63 struct thermal_governor *pos;
65 if (!name || !name[0])
68 list_for_each_entry(pos, &thermal_governor_list, governor_list)
69 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
75 int thermal_register_governor(struct thermal_governor *governor)
79 struct thermal_zone_device *pos;
84 mutex_lock(&thermal_governor_lock);
87 if (__find_governor(governor->name) == NULL) {
89 list_add(&governor->governor_list, &thermal_governor_list);
90 if (!def_governor && !strncmp(governor->name,
91 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
92 def_governor = governor;
95 mutex_lock(&thermal_list_lock);
97 list_for_each_entry(pos, &thermal_tz_list, node) {
99 * only thermal zones with specified tz->tzp->governor_name
100 * may run with tz->govenor unset
105 name = pos->tzp->governor_name;
107 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
108 pos->governor = governor;
111 mutex_unlock(&thermal_list_lock);
112 mutex_unlock(&thermal_governor_lock);
117 void thermal_unregister_governor(struct thermal_governor *governor)
119 struct thermal_zone_device *pos;
124 mutex_lock(&thermal_governor_lock);
126 if (__find_governor(governor->name) == NULL)
129 mutex_lock(&thermal_list_lock);
131 list_for_each_entry(pos, &thermal_tz_list, node) {
132 if (!strnicmp(pos->governor->name, governor->name,
133 THERMAL_NAME_LENGTH))
134 pos->governor = NULL;
137 mutex_unlock(&thermal_list_lock);
138 list_del(&governor->governor_list);
140 mutex_unlock(&thermal_governor_lock);
144 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
150 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
153 if (unlikely(ret < 0))
159 static void release_idr(struct idr *idr, struct mutex *lock, int id)
168 int get_tz_trend(struct thermal_zone_device *tz, int trip)
170 enum thermal_trend trend;
172 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
173 if (tz->temperature > tz->last_temperature)
174 trend = THERMAL_TREND_RAISING;
175 else if (tz->temperature < tz->last_temperature)
176 trend = THERMAL_TREND_DROPPING;
178 trend = THERMAL_TREND_STABLE;
183 EXPORT_SYMBOL(get_tz_trend);
185 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
186 struct thermal_cooling_device *cdev, int trip)
188 struct thermal_instance *pos = NULL;
189 struct thermal_instance *target_instance = NULL;
191 mutex_lock(&tz->lock);
192 mutex_lock(&cdev->lock);
194 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
195 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
196 target_instance = pos;
201 mutex_unlock(&cdev->lock);
202 mutex_unlock(&tz->lock);
204 return target_instance;
206 EXPORT_SYMBOL(get_thermal_instance);
208 static void print_bind_err_msg(struct thermal_zone_device *tz,
209 struct thermal_cooling_device *cdev, int ret)
211 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
212 tz->type, cdev->type, ret);
215 static void __bind(struct thermal_zone_device *tz, int mask,
216 struct thermal_cooling_device *cdev)
220 for (i = 0; i < tz->trips; i++) {
221 if (mask & (1 << i)) {
222 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
223 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
225 print_bind_err_msg(tz, cdev, ret);
230 static void __unbind(struct thermal_zone_device *tz, int mask,
231 struct thermal_cooling_device *cdev)
235 for (i = 0; i < tz->trips; i++)
237 thermal_zone_unbind_cooling_device(tz, i, cdev);
240 static void bind_cdev(struct thermal_cooling_device *cdev)
243 const struct thermal_zone_params *tzp;
244 struct thermal_zone_device *pos = NULL;
246 mutex_lock(&thermal_list_lock);
248 list_for_each_entry(pos, &thermal_tz_list, node) {
249 if (!pos->tzp && !pos->ops->bind)
252 if (pos->ops->bind) {
253 ret = pos->ops->bind(pos, cdev);
255 print_bind_err_msg(pos, cdev, ret);
260 if (!tzp || !tzp->tbp)
263 for (i = 0; i < tzp->num_tbps; i++) {
264 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
266 if (tzp->tbp[i].match(pos, cdev))
268 tzp->tbp[i].cdev = cdev;
269 __bind(pos, tzp->tbp[i].trip_mask, cdev);
273 mutex_unlock(&thermal_list_lock);
276 static void bind_tz(struct thermal_zone_device *tz)
279 struct thermal_cooling_device *pos = NULL;
280 const struct thermal_zone_params *tzp = tz->tzp;
282 if (!tzp && !tz->ops->bind)
285 mutex_lock(&thermal_list_lock);
287 /* If there is ops->bind, try to use ops->bind */
289 list_for_each_entry(pos, &thermal_cdev_list, node) {
290 ret = tz->ops->bind(tz, pos);
292 print_bind_err_msg(tz, pos, ret);
297 if (!tzp || !tzp->tbp)
300 list_for_each_entry(pos, &thermal_cdev_list, node) {
301 for (i = 0; i < tzp->num_tbps; i++) {
302 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
304 if (tzp->tbp[i].match(tz, pos))
306 tzp->tbp[i].cdev = pos;
307 __bind(tz, tzp->tbp[i].trip_mask, pos);
311 mutex_unlock(&thermal_list_lock);
314 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
318 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
319 round_jiffies(msecs_to_jiffies(delay)));
321 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
322 msecs_to_jiffies(delay));
324 cancel_delayed_work(&tz->poll_queue);
327 static void monitor_thermal_zone(struct thermal_zone_device *tz)
329 mutex_lock(&tz->lock);
332 thermal_zone_device_set_polling(tz, tz->passive_delay);
333 else if (tz->polling_delay)
334 thermal_zone_device_set_polling(tz, tz->polling_delay);
336 thermal_zone_device_set_polling(tz, 0);
338 mutex_unlock(&tz->lock);
341 static void handle_non_critical_trips(struct thermal_zone_device *tz,
342 int trip, enum thermal_trip_type trip_type)
344 tz->governor ? tz->governor->throttle(tz, trip) :
345 def_governor->throttle(tz, trip);
348 static void handle_critical_trips(struct thermal_zone_device *tz,
349 int trip, enum thermal_trip_type trip_type)
353 tz->ops->get_trip_temp(tz, trip, &trip_temp);
355 /* If we have not crossed the trip_temp, we do not care. */
356 if (tz->temperature < trip_temp)
360 tz->ops->notify(tz, trip, trip_type);
362 if (trip_type == THERMAL_TRIP_CRITICAL) {
363 dev_emerg(&tz->device,
364 "critical temperature reached(%d C),shutting down\n",
365 tz->temperature / 1000);
366 orderly_poweroff(true);
370 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
372 enum thermal_trip_type type;
374 tz->ops->get_trip_type(tz, trip, &type);
376 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
377 handle_critical_trips(tz, trip, type);
379 handle_non_critical_trips(tz, trip, type);
381 * Alright, we handled this trip successfully.
382 * So, start monitoring again.
384 monitor_thermal_zone(tz);
388 * thermal_zone_get_temp() - returns its the temperature of thermal zone
389 * @tz: a valid pointer to a struct thermal_zone_device
390 * @temp: a valid pointer to where to store the resulting temperature.
392 * When a valid thermal zone reference is passed, it will fetch its
393 * temperature and fill @temp.
395 * Return: On success returns 0, an error code otherwise
397 int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
400 #ifdef CONFIG_THERMAL_EMULATION
402 unsigned long crit_temp = -1UL;
403 enum thermal_trip_type type;
406 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
409 mutex_lock(&tz->lock);
411 ret = tz->ops->get_temp(tz, temp);
412 #ifdef CONFIG_THERMAL_EMULATION
413 if (!tz->emul_temperature)
416 for (count = 0; count < tz->trips; count++) {
417 ret = tz->ops->get_trip_type(tz, count, &type);
418 if (!ret && type == THERMAL_TRIP_CRITICAL) {
419 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
427 if (*temp < crit_temp)
428 *temp = tz->emul_temperature;
431 mutex_unlock(&tz->lock);
435 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
437 static void update_temperature(struct thermal_zone_device *tz)
442 ret = thermal_zone_get_temp(tz, &temp);
444 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
449 mutex_lock(&tz->lock);
450 tz->last_temperature = tz->temperature;
451 tz->temperature = temp;
452 mutex_unlock(&tz->lock);
454 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
455 tz->last_temperature, tz->temperature);
458 void thermal_zone_device_update(struct thermal_zone_device *tz)
462 if (!tz->ops->get_temp)
465 update_temperature(tz);
467 for (count = 0; count < tz->trips; count++)
468 handle_thermal_trip(tz, count);
470 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
472 static void thermal_zone_device_check(struct work_struct *work)
474 struct thermal_zone_device *tz = container_of(work, struct
477 thermal_zone_device_update(tz);
480 /* sys I/F for thermal zone */
482 #define to_thermal_zone(_dev) \
483 container_of(_dev, struct thermal_zone_device, device)
486 type_show(struct device *dev, struct device_attribute *attr, char *buf)
488 struct thermal_zone_device *tz = to_thermal_zone(dev);
490 return sprintf(buf, "%s\n", tz->type);
494 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
496 struct thermal_zone_device *tz = to_thermal_zone(dev);
500 ret = thermal_zone_get_temp(tz, &temperature);
505 return sprintf(buf, "%ld\n", temperature);
509 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
511 struct thermal_zone_device *tz = to_thermal_zone(dev);
512 enum thermal_device_mode mode;
515 if (!tz->ops->get_mode)
518 result = tz->ops->get_mode(tz, &mode);
522 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
527 mode_store(struct device *dev, struct device_attribute *attr,
528 const char *buf, size_t count)
530 struct thermal_zone_device *tz = to_thermal_zone(dev);
533 if (!tz->ops->set_mode)
536 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
537 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
538 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
539 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
550 trip_point_type_show(struct device *dev, struct device_attribute *attr,
553 struct thermal_zone_device *tz = to_thermal_zone(dev);
554 enum thermal_trip_type type;
557 if (!tz->ops->get_trip_type)
560 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
563 result = tz->ops->get_trip_type(tz, trip, &type);
568 case THERMAL_TRIP_CRITICAL:
569 return sprintf(buf, "critical\n");
570 case THERMAL_TRIP_HOT:
571 return sprintf(buf, "hot\n");
572 case THERMAL_TRIP_PASSIVE:
573 return sprintf(buf, "passive\n");
574 case THERMAL_TRIP_ACTIVE:
575 return sprintf(buf, "active\n");
577 return sprintf(buf, "unknown\n");
582 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
583 const char *buf, size_t count)
585 struct thermal_zone_device *tz = to_thermal_zone(dev);
587 unsigned long temperature;
589 if (!tz->ops->set_trip_temp)
592 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
595 if (kstrtoul(buf, 10, &temperature))
598 ret = tz->ops->set_trip_temp(tz, trip, temperature);
600 return ret ? ret : count;
604 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
607 struct thermal_zone_device *tz = to_thermal_zone(dev);
611 if (!tz->ops->get_trip_temp)
614 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
617 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
622 return sprintf(buf, "%ld\n", temperature);
626 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
627 const char *buf, size_t count)
629 struct thermal_zone_device *tz = to_thermal_zone(dev);
631 unsigned long temperature;
633 if (!tz->ops->set_trip_hyst)
636 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
639 if (kstrtoul(buf, 10, &temperature))
643 * We are not doing any check on the 'temperature' value
644 * here. The driver implementing 'set_trip_hyst' has to
647 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
649 return ret ? ret : count;
653 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
656 struct thermal_zone_device *tz = to_thermal_zone(dev);
658 unsigned long temperature;
660 if (!tz->ops->get_trip_hyst)
663 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
666 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
668 return ret ? ret : sprintf(buf, "%ld\n", temperature);
672 passive_store(struct device *dev, struct device_attribute *attr,
673 const char *buf, size_t count)
675 struct thermal_zone_device *tz = to_thermal_zone(dev);
676 struct thermal_cooling_device *cdev = NULL;
679 if (!sscanf(buf, "%d\n", &state))
682 /* sanity check: values below 1000 millicelcius don't make sense
683 * and can cause the system to go into a thermal heart attack
685 if (state && state < 1000)
688 if (state && !tz->forced_passive) {
689 mutex_lock(&thermal_list_lock);
690 list_for_each_entry(cdev, &thermal_cdev_list, node) {
691 if (!strncmp("Processor", cdev->type,
692 sizeof("Processor")))
693 thermal_zone_bind_cooling_device(tz,
694 THERMAL_TRIPS_NONE, cdev,
698 mutex_unlock(&thermal_list_lock);
699 if (!tz->passive_delay)
700 tz->passive_delay = 1000;
701 } else if (!state && tz->forced_passive) {
702 mutex_lock(&thermal_list_lock);
703 list_for_each_entry(cdev, &thermal_cdev_list, node) {
704 if (!strncmp("Processor", cdev->type,
705 sizeof("Processor")))
706 thermal_zone_unbind_cooling_device(tz,
710 mutex_unlock(&thermal_list_lock);
711 tz->passive_delay = 0;
714 tz->forced_passive = state;
716 thermal_zone_device_update(tz);
722 passive_show(struct device *dev, struct device_attribute *attr,
725 struct thermal_zone_device *tz = to_thermal_zone(dev);
727 return sprintf(buf, "%d\n", tz->forced_passive);
731 policy_store(struct device *dev, struct device_attribute *attr,
732 const char *buf, size_t count)
735 struct thermal_zone_device *tz = to_thermal_zone(dev);
736 struct thermal_governor *gov;
738 mutex_lock(&thermal_governor_lock);
740 gov = __find_governor(buf);
748 mutex_unlock(&thermal_governor_lock);
753 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
755 struct thermal_zone_device *tz = to_thermal_zone(dev);
757 return sprintf(buf, "%s\n", tz->governor->name);
760 #ifdef CONFIG_THERMAL_EMULATION
762 emul_temp_store(struct device *dev, struct device_attribute *attr,
763 const char *buf, size_t count)
765 struct thermal_zone_device *tz = to_thermal_zone(dev);
767 unsigned long temperature;
769 if (kstrtoul(buf, 10, &temperature))
772 if (!tz->ops->set_emul_temp) {
773 mutex_lock(&tz->lock);
774 tz->emul_temperature = temperature;
775 mutex_unlock(&tz->lock);
777 ret = tz->ops->set_emul_temp(tz, temperature);
781 thermal_zone_device_update(tz);
783 return ret ? ret : count;
785 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
786 #endif/*CONFIG_THERMAL_EMULATION*/
788 static DEVICE_ATTR(type, 0444, type_show, NULL);
789 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
790 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
791 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
792 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
794 /* sys I/F for cooling device */
795 #define to_cooling_device(_dev) \
796 container_of(_dev, struct thermal_cooling_device, device)
799 thermal_cooling_device_type_show(struct device *dev,
800 struct device_attribute *attr, char *buf)
802 struct thermal_cooling_device *cdev = to_cooling_device(dev);
804 return sprintf(buf, "%s\n", cdev->type);
808 thermal_cooling_device_max_state_show(struct device *dev,
809 struct device_attribute *attr, char *buf)
811 struct thermal_cooling_device *cdev = to_cooling_device(dev);
815 ret = cdev->ops->get_max_state(cdev, &state);
818 return sprintf(buf, "%ld\n", state);
822 thermal_cooling_device_cur_state_show(struct device *dev,
823 struct device_attribute *attr, char *buf)
825 struct thermal_cooling_device *cdev = to_cooling_device(dev);
829 ret = cdev->ops->get_cur_state(cdev, &state);
832 return sprintf(buf, "%ld\n", state);
836 thermal_cooling_device_cur_state_store(struct device *dev,
837 struct device_attribute *attr,
838 const char *buf, size_t count)
840 struct thermal_cooling_device *cdev = to_cooling_device(dev);
844 if (!sscanf(buf, "%ld\n", &state))
850 result = cdev->ops->set_cur_state(cdev, state);
856 static struct device_attribute dev_attr_cdev_type =
857 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
858 static DEVICE_ATTR(max_state, 0444,
859 thermal_cooling_device_max_state_show, NULL);
860 static DEVICE_ATTR(cur_state, 0644,
861 thermal_cooling_device_cur_state_show,
862 thermal_cooling_device_cur_state_store);
865 thermal_cooling_device_trip_point_show(struct device *dev,
866 struct device_attribute *attr, char *buf)
868 struct thermal_instance *instance;
871 container_of(attr, struct thermal_instance, attr);
873 if (instance->trip == THERMAL_TRIPS_NONE)
874 return sprintf(buf, "-1\n");
876 return sprintf(buf, "%d\n", instance->trip);
879 /* Device management */
882 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
883 * @tz: pointer to struct thermal_zone_device
884 * @trip: indicates which trip point the cooling devices is
885 * associated with in this thermal zone.
886 * @cdev: pointer to struct thermal_cooling_device
887 * @upper: the Maximum cooling state for this trip point.
888 * THERMAL_NO_LIMIT means no upper limit,
889 * and the cooling device can be in max_state.
890 * @lower: the Minimum cooling state can be used for this trip point.
891 * THERMAL_NO_LIMIT means no lower limit,
892 * and the cooling device can be in cooling state 0.
894 * This interface function bind a thermal cooling device to the certain trip
895 * point of a thermal zone device.
896 * This function is usually called in the thermal zone device .bind callback.
898 * Return: 0 on success, the proper error value otherwise.
900 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
902 struct thermal_cooling_device *cdev,
903 unsigned long upper, unsigned long lower)
905 struct thermal_instance *dev;
906 struct thermal_instance *pos;
907 struct thermal_zone_device *pos1;
908 struct thermal_cooling_device *pos2;
909 unsigned long max_state;
912 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
915 list_for_each_entry(pos1, &thermal_tz_list, node) {
919 list_for_each_entry(pos2, &thermal_cdev_list, node) {
924 if (tz != pos1 || cdev != pos2)
927 cdev->ops->get_max_state(cdev, &max_state);
929 /* lower default 0, upper default max_state */
930 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
931 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
933 if (lower > upper || upper > max_state)
937 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
945 dev->target = THERMAL_NO_TARGET;
947 result = get_idr(&tz->idr, &tz->lock, &dev->id);
951 sprintf(dev->name, "cdev%d", dev->id);
953 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
957 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
958 sysfs_attr_init(&dev->attr.attr);
959 dev->attr.attr.name = dev->attr_name;
960 dev->attr.attr.mode = 0444;
961 dev->attr.show = thermal_cooling_device_trip_point_show;
962 result = device_create_file(&tz->device, &dev->attr);
964 goto remove_symbol_link;
966 mutex_lock(&tz->lock);
967 mutex_lock(&cdev->lock);
968 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
969 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
974 list_add_tail(&dev->tz_node, &tz->thermal_instances);
975 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
977 mutex_unlock(&cdev->lock);
978 mutex_unlock(&tz->lock);
983 device_remove_file(&tz->device, &dev->attr);
985 sysfs_remove_link(&tz->device.kobj, dev->name);
987 release_idr(&tz->idr, &tz->lock, dev->id);
992 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
995 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
997 * @tz: pointer to a struct thermal_zone_device.
998 * @trip: indicates which trip point the cooling devices is
999 * associated with in this thermal zone.
1000 * @cdev: pointer to a struct thermal_cooling_device.
1002 * This interface function unbind a thermal cooling device from the certain
1003 * trip point of a thermal zone device.
1004 * This function is usually called in the thermal zone device .unbind callback.
1006 * Return: 0 on success, the proper error value otherwise.
1008 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1010 struct thermal_cooling_device *cdev)
1012 struct thermal_instance *pos, *next;
1014 mutex_lock(&tz->lock);
1015 mutex_lock(&cdev->lock);
1016 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1017 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1018 list_del(&pos->tz_node);
1019 list_del(&pos->cdev_node);
1020 mutex_unlock(&cdev->lock);
1021 mutex_unlock(&tz->lock);
1025 mutex_unlock(&cdev->lock);
1026 mutex_unlock(&tz->lock);
1031 device_remove_file(&tz->device, &pos->attr);
1032 sysfs_remove_link(&tz->device.kobj, pos->name);
1033 release_idr(&tz->idr, &tz->lock, pos->id);
1037 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1039 static void thermal_release(struct device *dev)
1041 struct thermal_zone_device *tz;
1042 struct thermal_cooling_device *cdev;
1044 if (!strncmp(dev_name(dev), "thermal_zone",
1045 sizeof("thermal_zone") - 1)) {
1046 tz = to_thermal_zone(dev);
1049 cdev = to_cooling_device(dev);
1054 static struct class thermal_class = {
1056 .dev_release = thermal_release,
1060 * __thermal_cooling_device_register() - register a new thermal cooling device
1061 * @np: a pointer to a device tree node.
1062 * @type: the thermal cooling device type.
1063 * @devdata: device private data.
1064 * @ops: standard thermal cooling devices callbacks.
1066 * This interface function adds a new thermal cooling device (fan/processor/...)
1067 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1068 * to all the thermal zone devices registered at the same time.
1069 * It also gives the opportunity to link the cooling device to a device tree
1070 * node, so that it can be bound to a thermal zone created out of device tree.
1072 * Return: a pointer to the created struct thermal_cooling_device or an
1073 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1075 static struct thermal_cooling_device *
1076 __thermal_cooling_device_register(struct device_node *np,
1077 char *type, void *devdata,
1078 const struct thermal_cooling_device_ops *ops)
1080 struct thermal_cooling_device *cdev;
1083 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1084 return ERR_PTR(-EINVAL);
1086 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1087 !ops->set_cur_state)
1088 return ERR_PTR(-EINVAL);
1090 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1092 return ERR_PTR(-ENOMEM);
1094 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1097 return ERR_PTR(result);
1100 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1101 mutex_init(&cdev->lock);
1102 INIT_LIST_HEAD(&cdev->thermal_instances);
1105 cdev->updated = false;
1106 cdev->device.class = &thermal_class;
1107 cdev->devdata = devdata;
1108 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1109 result = device_register(&cdev->device);
1111 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1113 return ERR_PTR(result);
1118 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1123 result = device_create_file(&cdev->device, &dev_attr_max_state);
1127 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1131 /* Add 'this' new cdev to the global cdev list */
1132 mutex_lock(&thermal_list_lock);
1133 list_add(&cdev->node, &thermal_cdev_list);
1134 mutex_unlock(&thermal_list_lock);
1136 /* Update binding information for 'this' new cdev */
1142 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1143 device_unregister(&cdev->device);
1144 return ERR_PTR(result);
1148 * thermal_cooling_device_register() - register a new thermal cooling device
1149 * @type: the thermal cooling device type.
1150 * @devdata: device private data.
1151 * @ops: standard thermal cooling devices callbacks.
1153 * This interface function adds a new thermal cooling device (fan/processor/...)
1154 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1155 * to all the thermal zone devices registered at the same time.
1157 * Return: a pointer to the created struct thermal_cooling_device or an
1158 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1160 struct thermal_cooling_device *
1161 thermal_cooling_device_register(char *type, void *devdata,
1162 const struct thermal_cooling_device_ops *ops)
1164 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1166 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1169 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1170 * @np: a pointer to a device tree node.
1171 * @type: the thermal cooling device type.
1172 * @devdata: device private data.
1173 * @ops: standard thermal cooling devices callbacks.
1175 * This function will register a cooling device with device tree node reference.
1176 * This interface function adds a new thermal cooling device (fan/processor/...)
1177 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1178 * to all the thermal zone devices registered at the same time.
1180 * Return: a pointer to the created struct thermal_cooling_device or an
1181 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1183 struct thermal_cooling_device *
1184 thermal_of_cooling_device_register(struct device_node *np,
1185 char *type, void *devdata,
1186 const struct thermal_cooling_device_ops *ops)
1188 return __thermal_cooling_device_register(np, type, devdata, ops);
1190 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1193 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1194 * @cdev: the thermal cooling device to remove.
1196 * thermal_cooling_device_unregister() must be called when the device is no
1199 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1202 const struct thermal_zone_params *tzp;
1203 struct thermal_zone_device *tz;
1204 struct thermal_cooling_device *pos = NULL;
1209 mutex_lock(&thermal_list_lock);
1210 list_for_each_entry(pos, &thermal_cdev_list, node)
1214 /* thermal cooling device not found */
1215 mutex_unlock(&thermal_list_lock);
1218 list_del(&cdev->node);
1220 /* Unbind all thermal zones associated with 'this' cdev */
1221 list_for_each_entry(tz, &thermal_tz_list, node) {
1222 if (tz->ops->unbind) {
1223 tz->ops->unbind(tz, cdev);
1227 if (!tz->tzp || !tz->tzp->tbp)
1231 for (i = 0; i < tzp->num_tbps; i++) {
1232 if (tzp->tbp[i].cdev == cdev) {
1233 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1234 tzp->tbp[i].cdev = NULL;
1239 mutex_unlock(&thermal_list_lock);
1242 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1243 device_remove_file(&cdev->device, &dev_attr_max_state);
1244 device_remove_file(&cdev->device, &dev_attr_cur_state);
1246 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1247 device_unregister(&cdev->device);
1250 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1252 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1254 struct thermal_instance *instance;
1255 unsigned long target = 0;
1257 /* cooling device is updated*/
1261 mutex_lock(&cdev->lock);
1262 /* Make sure cdev enters the deepest cooling state */
1263 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1264 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1265 instance->tz->id, instance->target);
1266 if (instance->target == THERMAL_NO_TARGET)
1268 if (instance->target > target)
1269 target = instance->target;
1271 mutex_unlock(&cdev->lock);
1272 cdev->ops->set_cur_state(cdev, target);
1273 cdev->updated = true;
1274 dev_dbg(&cdev->device, "set to state %lu\n", target);
1276 EXPORT_SYMBOL(thermal_cdev_update);
1279 * thermal_notify_framework - Sensor drivers use this API to notify framework
1280 * @tz: thermal zone device
1281 * @trip: indicates which trip point has been crossed
1283 * This function handles the trip events from sensor drivers. It starts
1284 * throttling the cooling devices according to the policy configured.
1285 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1286 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1287 * The throttling policy is based on the configured platform data; if no
1288 * platform data is provided, this uses the step_wise throttling policy.
1290 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1292 handle_thermal_trip(tz, trip);
1294 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1297 * create_trip_attrs() - create attributes for trip points
1298 * @tz: the thermal zone device
1299 * @mask: Writeable trip point bitmap.
1301 * helper function to instantiate sysfs entries for every trip
1302 * point and its properties of a struct thermal_zone_device.
1304 * Return: 0 on success, the proper error value otherwise.
1306 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1309 int size = sizeof(struct thermal_attr) * tz->trips;
1311 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1312 if (!tz->trip_type_attrs)
1315 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1316 if (!tz->trip_temp_attrs) {
1317 kfree(tz->trip_type_attrs);
1321 if (tz->ops->get_trip_hyst) {
1322 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1323 if (!tz->trip_hyst_attrs) {
1324 kfree(tz->trip_type_attrs);
1325 kfree(tz->trip_temp_attrs);
1331 for (indx = 0; indx < tz->trips; indx++) {
1332 /* create trip type attribute */
1333 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1334 "trip_point_%d_type", indx);
1336 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1337 tz->trip_type_attrs[indx].attr.attr.name =
1338 tz->trip_type_attrs[indx].name;
1339 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1340 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1342 device_create_file(&tz->device,
1343 &tz->trip_type_attrs[indx].attr);
1345 /* create trip temp attribute */
1346 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1347 "trip_point_%d_temp", indx);
1349 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1350 tz->trip_temp_attrs[indx].attr.attr.name =
1351 tz->trip_temp_attrs[indx].name;
1352 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1353 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1354 if (mask & (1 << indx)) {
1355 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1356 tz->trip_temp_attrs[indx].attr.store =
1357 trip_point_temp_store;
1360 device_create_file(&tz->device,
1361 &tz->trip_temp_attrs[indx].attr);
1363 /* create Optional trip hyst attribute */
1364 if (!tz->ops->get_trip_hyst)
1366 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1367 "trip_point_%d_hyst", indx);
1369 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1370 tz->trip_hyst_attrs[indx].attr.attr.name =
1371 tz->trip_hyst_attrs[indx].name;
1372 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1373 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1374 if (tz->ops->set_trip_hyst) {
1375 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1376 tz->trip_hyst_attrs[indx].attr.store =
1377 trip_point_hyst_store;
1380 device_create_file(&tz->device,
1381 &tz->trip_hyst_attrs[indx].attr);
1386 static void remove_trip_attrs(struct thermal_zone_device *tz)
1390 for (indx = 0; indx < tz->trips; indx++) {
1391 device_remove_file(&tz->device,
1392 &tz->trip_type_attrs[indx].attr);
1393 device_remove_file(&tz->device,
1394 &tz->trip_temp_attrs[indx].attr);
1395 if (tz->ops->get_trip_hyst)
1396 device_remove_file(&tz->device,
1397 &tz->trip_hyst_attrs[indx].attr);
1399 kfree(tz->trip_type_attrs);
1400 kfree(tz->trip_temp_attrs);
1401 kfree(tz->trip_hyst_attrs);
1405 * thermal_zone_device_register() - register a new thermal zone device
1406 * @type: the thermal zone device type
1407 * @trips: the number of trip points the thermal zone support
1408 * @mask: a bit string indicating the writeablility of trip points
1409 * @devdata: private device data
1410 * @ops: standard thermal zone device callbacks
1411 * @tzp: thermal zone platform parameters
1412 * @passive_delay: number of milliseconds to wait between polls when
1413 * performing passive cooling
1414 * @polling_delay: number of milliseconds to wait between polls when checking
1415 * whether trip points have been crossed (0 for interrupt
1418 * This interface function adds a new thermal zone device (sensor) to
1419 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1420 * thermal cooling devices registered at the same time.
1421 * thermal_zone_device_unregister() must be called when the device is no
1422 * longer needed. The passive cooling depends on the .get_trend() return value.
1424 * Return: a pointer to the created struct thermal_zone_device or an
1425 * in case of error, an ERR_PTR. Caller must check return value with
1426 * IS_ERR*() helpers.
1428 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1429 int trips, int mask, void *devdata,
1430 struct thermal_zone_device_ops *ops,
1431 const struct thermal_zone_params *tzp,
1432 int passive_delay, int polling_delay)
1434 struct thermal_zone_device *tz;
1435 enum thermal_trip_type trip_type;
1440 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1441 return ERR_PTR(-EINVAL);
1443 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1444 return ERR_PTR(-EINVAL);
1447 return ERR_PTR(-EINVAL);
1449 if (trips > 0 && !ops->get_trip_type)
1450 return ERR_PTR(-EINVAL);
1452 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1454 return ERR_PTR(-ENOMEM);
1456 INIT_LIST_HEAD(&tz->thermal_instances);
1458 mutex_init(&tz->lock);
1459 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1462 return ERR_PTR(result);
1465 strlcpy(tz->type, type ? : "", sizeof(tz->type));
1468 tz->device.class = &thermal_class;
1469 tz->devdata = devdata;
1471 tz->passive_delay = passive_delay;
1472 tz->polling_delay = polling_delay;
1474 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1475 result = device_register(&tz->device);
1477 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1479 return ERR_PTR(result);
1484 result = device_create_file(&tz->device, &dev_attr_type);
1489 result = device_create_file(&tz->device, &dev_attr_temp);
1493 if (ops->get_mode) {
1494 result = device_create_file(&tz->device, &dev_attr_mode);
1499 result = create_trip_attrs(tz, mask);
1503 for (count = 0; count < trips; count++) {
1504 tz->ops->get_trip_type(tz, count, &trip_type);
1505 if (trip_type == THERMAL_TRIP_PASSIVE)
1510 result = device_create_file(&tz->device, &dev_attr_passive);
1515 #ifdef CONFIG_THERMAL_EMULATION
1516 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1520 /* Create policy attribute */
1521 result = device_create_file(&tz->device, &dev_attr_policy);
1525 /* Update 'this' zone's governor information */
1526 mutex_lock(&thermal_governor_lock);
1529 tz->governor = __find_governor(tz->tzp->governor_name);
1531 tz->governor = def_governor;
1533 mutex_unlock(&thermal_governor_lock);
1535 if (!tz->tzp || !tz->tzp->no_hwmon) {
1536 result = thermal_add_hwmon_sysfs(tz);
1541 mutex_lock(&thermal_list_lock);
1542 list_add_tail(&tz->node, &thermal_tz_list);
1543 mutex_unlock(&thermal_list_lock);
1545 /* Bind cooling devices for this zone */
1548 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1550 if (!tz->ops->get_temp)
1551 thermal_zone_device_set_polling(tz, 0);
1553 thermal_zone_device_update(tz);
1559 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1560 device_unregister(&tz->device);
1561 return ERR_PTR(result);
1563 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1566 * thermal_device_unregister - removes the registered thermal zone device
1567 * @tz: the thermal zone device to remove
1569 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1572 const struct thermal_zone_params *tzp;
1573 struct thermal_cooling_device *cdev;
1574 struct thermal_zone_device *pos = NULL;
1581 mutex_lock(&thermal_list_lock);
1582 list_for_each_entry(pos, &thermal_tz_list, node)
1586 /* thermal zone device not found */
1587 mutex_unlock(&thermal_list_lock);
1590 list_del(&tz->node);
1592 /* Unbind all cdevs associated with 'this' thermal zone */
1593 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1594 if (tz->ops->unbind) {
1595 tz->ops->unbind(tz, cdev);
1599 if (!tzp || !tzp->tbp)
1602 for (i = 0; i < tzp->num_tbps; i++) {
1603 if (tzp->tbp[i].cdev == cdev) {
1604 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1605 tzp->tbp[i].cdev = NULL;
1610 mutex_unlock(&thermal_list_lock);
1612 thermal_zone_device_set_polling(tz, 0);
1615 device_remove_file(&tz->device, &dev_attr_type);
1616 device_remove_file(&tz->device, &dev_attr_temp);
1617 if (tz->ops->get_mode)
1618 device_remove_file(&tz->device, &dev_attr_mode);
1619 device_remove_file(&tz->device, &dev_attr_policy);
1620 remove_trip_attrs(tz);
1621 tz->governor = NULL;
1623 thermal_remove_hwmon_sysfs(tz);
1624 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1625 idr_destroy(&tz->idr);
1626 mutex_destroy(&tz->lock);
1627 device_unregister(&tz->device);
1630 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1633 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1634 * @name: thermal zone name to fetch the temperature
1636 * When only one zone is found with the passed name, returns a reference to it.
1638 * Return: On success returns a reference to an unique thermal zone with
1639 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1640 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1642 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1644 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1645 unsigned int found = 0;
1650 mutex_lock(&thermal_list_lock);
1651 list_for_each_entry(pos, &thermal_tz_list, node)
1652 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1656 mutex_unlock(&thermal_list_lock);
1658 /* nothing has been found, thus an error code for it */
1660 ref = ERR_PTR(-ENODEV);
1662 /* Success only when an unique zone is found */
1663 ref = ERR_PTR(-EEXIST);
1668 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1671 static struct genl_family thermal_event_genl_family = {
1672 .id = GENL_ID_GENERATE,
1673 .name = THERMAL_GENL_FAMILY_NAME,
1674 .version = THERMAL_GENL_VERSION,
1675 .maxattr = THERMAL_GENL_ATTR_MAX,
1678 static struct genl_multicast_group thermal_event_mcgrp = {
1679 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1682 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1685 struct sk_buff *skb;
1686 struct nlattr *attr;
1687 struct thermal_genl_event *thermal_event;
1691 static unsigned int thermal_event_seqnum;
1696 /* allocate memory */
1697 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1700 skb = genlmsg_new(size, GFP_ATOMIC);
1704 /* add the genetlink message header */
1705 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1706 &thermal_event_genl_family, 0,
1707 THERMAL_GENL_CMD_EVENT);
1714 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1715 sizeof(struct thermal_genl_event));
1722 thermal_event = nla_data(attr);
1723 if (!thermal_event) {
1728 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1730 thermal_event->orig = tz->id;
1731 thermal_event->event = event;
1733 /* send multicast genetlink message */
1734 result = genlmsg_end(skb, msg_header);
1740 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1742 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1746 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1748 static int genetlink_init(void)
1752 result = genl_register_family(&thermal_event_genl_family);
1756 result = genl_register_mc_group(&thermal_event_genl_family,
1757 &thermal_event_mcgrp);
1759 genl_unregister_family(&thermal_event_genl_family);
1763 static void genetlink_exit(void)
1765 genl_unregister_family(&thermal_event_genl_family);
1767 #else /* !CONFIG_NET */
1768 static inline int genetlink_init(void) { return 0; }
1769 static inline void genetlink_exit(void) {}
1770 #endif /* !CONFIG_NET */
1772 static int __init thermal_register_governors(void)
1776 result = thermal_gov_step_wise_register();
1780 result = thermal_gov_fair_share_register();
1784 return thermal_gov_user_space_register();
1787 static void thermal_unregister_governors(void)
1789 thermal_gov_step_wise_unregister();
1790 thermal_gov_fair_share_unregister();
1791 thermal_gov_user_space_unregister();
1794 static int __init thermal_init(void)
1798 result = thermal_register_governors();
1802 result = class_register(&thermal_class);
1804 goto unregister_governors;
1806 result = genetlink_init();
1808 goto unregister_class;
1810 result = of_parse_thermal_zones();
1818 unregister_governors:
1819 thermal_unregister_governors();
1821 class_unregister(&thermal_class);
1823 idr_destroy(&thermal_tz_idr);
1824 idr_destroy(&thermal_cdev_idr);
1825 mutex_destroy(&thermal_idr_lock);
1826 mutex_destroy(&thermal_list_lock);
1827 mutex_destroy(&thermal_governor_lock);
1831 static void __exit thermal_exit(void)
1833 of_thermal_destroy_zones();
1835 class_unregister(&thermal_class);
1836 thermal_unregister_governors();
1837 idr_destroy(&thermal_tz_idr);
1838 idr_destroy(&thermal_cdev_idr);
1839 mutex_destroy(&thermal_idr_lock);
1840 mutex_destroy(&thermal_list_lock);
1841 mutex_destroy(&thermal_governor_lock);
1844 fs_initcall(thermal_init);
1845 module_exit(thermal_exit);