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 *__find_governor(const char *name)
61 struct thermal_governor *pos;
63 list_for_each_entry(pos, &thermal_governor_list, governor_list)
64 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
70 int thermal_register_governor(struct thermal_governor *governor)
74 struct thermal_zone_device *pos;
79 mutex_lock(&thermal_governor_lock);
82 if (__find_governor(governor->name) == NULL) {
84 list_add(&governor->governor_list, &thermal_governor_list);
87 mutex_lock(&thermal_list_lock);
89 list_for_each_entry(pos, &thermal_tz_list, node) {
93 name = pos->tzp->governor_name;
95 name = DEFAULT_THERMAL_GOVERNOR;
96 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
97 pos->governor = governor;
100 mutex_unlock(&thermal_list_lock);
101 mutex_unlock(&thermal_governor_lock);
106 void thermal_unregister_governor(struct thermal_governor *governor)
108 struct thermal_zone_device *pos;
113 mutex_lock(&thermal_governor_lock);
115 if (__find_governor(governor->name) == NULL)
118 mutex_lock(&thermal_list_lock);
120 list_for_each_entry(pos, &thermal_tz_list, node) {
121 if (!strnicmp(pos->governor->name, governor->name,
122 THERMAL_NAME_LENGTH))
123 pos->governor = NULL;
126 mutex_unlock(&thermal_list_lock);
127 list_del(&governor->governor_list);
129 mutex_unlock(&thermal_governor_lock);
133 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
139 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
142 if (unlikely(ret < 0))
148 static void release_idr(struct idr *idr, struct mutex *lock, int id)
157 int get_tz_trend(struct thermal_zone_device *tz, int trip)
159 enum thermal_trend trend;
161 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
162 if (tz->temperature > tz->last_temperature)
163 trend = THERMAL_TREND_RAISING;
164 else if (tz->temperature < tz->last_temperature)
165 trend = THERMAL_TREND_DROPPING;
167 trend = THERMAL_TREND_STABLE;
172 EXPORT_SYMBOL(get_tz_trend);
174 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
175 struct thermal_cooling_device *cdev, int trip)
177 struct thermal_instance *pos = NULL;
178 struct thermal_instance *target_instance = NULL;
180 mutex_lock(&tz->lock);
181 mutex_lock(&cdev->lock);
183 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
184 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
185 target_instance = pos;
190 mutex_unlock(&cdev->lock);
191 mutex_unlock(&tz->lock);
193 return target_instance;
195 EXPORT_SYMBOL(get_thermal_instance);
197 static void print_bind_err_msg(struct thermal_zone_device *tz,
198 struct thermal_cooling_device *cdev, int ret)
200 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
201 tz->type, cdev->type, ret);
204 static void __bind(struct thermal_zone_device *tz, int mask,
205 struct thermal_cooling_device *cdev)
209 for (i = 0; i < tz->trips; i++) {
210 if (mask & (1 << i)) {
211 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
212 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
214 print_bind_err_msg(tz, cdev, ret);
219 static void __unbind(struct thermal_zone_device *tz, int mask,
220 struct thermal_cooling_device *cdev)
224 for (i = 0; i < tz->trips; i++)
226 thermal_zone_unbind_cooling_device(tz, i, cdev);
229 static void bind_cdev(struct thermal_cooling_device *cdev)
232 const struct thermal_zone_params *tzp;
233 struct thermal_zone_device *pos = NULL;
235 mutex_lock(&thermal_list_lock);
237 list_for_each_entry(pos, &thermal_tz_list, node) {
238 if (!pos->tzp && !pos->ops->bind)
241 if (!pos->tzp && pos->ops->bind) {
242 ret = pos->ops->bind(pos, cdev);
244 print_bind_err_msg(pos, cdev, ret);
248 if (!tzp || !tzp->tbp)
251 for (i = 0; i < tzp->num_tbps; i++) {
252 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
254 if (tzp->tbp[i].match(pos, cdev))
256 tzp->tbp[i].cdev = cdev;
257 __bind(pos, tzp->tbp[i].trip_mask, cdev);
261 mutex_unlock(&thermal_list_lock);
264 static void bind_tz(struct thermal_zone_device *tz)
267 struct thermal_cooling_device *pos = NULL;
268 const struct thermal_zone_params *tzp = tz->tzp;
270 if (!tzp && !tz->ops->bind)
273 mutex_lock(&thermal_list_lock);
275 /* If there is no platform data, try to use ops->bind */
276 if (!tzp && tz->ops->bind) {
277 list_for_each_entry(pos, &thermal_cdev_list, node) {
278 ret = tz->ops->bind(tz, pos);
280 print_bind_err_msg(tz, pos, ret);
285 if (!tzp || !tzp->tbp)
288 list_for_each_entry(pos, &thermal_cdev_list, node) {
289 for (i = 0; i < tzp->num_tbps; i++) {
290 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
292 if (tzp->tbp[i].match(tz, pos))
294 tzp->tbp[i].cdev = pos;
295 __bind(tz, tzp->tbp[i].trip_mask, pos);
299 mutex_unlock(&thermal_list_lock);
302 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
306 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
307 round_jiffies(msecs_to_jiffies(delay)));
309 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
310 msecs_to_jiffies(delay));
312 cancel_delayed_work(&tz->poll_queue);
315 static void monitor_thermal_zone(struct thermal_zone_device *tz)
317 mutex_lock(&tz->lock);
320 thermal_zone_device_set_polling(tz, tz->passive_delay);
321 else if (tz->polling_delay)
322 thermal_zone_device_set_polling(tz, tz->polling_delay);
324 thermal_zone_device_set_polling(tz, 0);
326 mutex_unlock(&tz->lock);
329 static void handle_non_critical_trips(struct thermal_zone_device *tz,
330 int trip, enum thermal_trip_type trip_type)
333 tz->governor->throttle(tz, trip);
336 static void handle_critical_trips(struct thermal_zone_device *tz,
337 int trip, enum thermal_trip_type trip_type)
341 tz->ops->get_trip_temp(tz, trip, &trip_temp);
343 /* If we have not crossed the trip_temp, we do not care. */
344 if (tz->temperature < trip_temp)
348 tz->ops->notify(tz, trip, trip_type);
350 if (trip_type == THERMAL_TRIP_CRITICAL) {
351 dev_emerg(&tz->device,
352 "critical temperature reached(%d C),shutting down\n",
353 tz->temperature / 1000);
354 orderly_poweroff(true);
358 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
360 enum thermal_trip_type type;
362 tz->ops->get_trip_type(tz, trip, &type);
364 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
365 handle_critical_trips(tz, trip, type);
367 handle_non_critical_trips(tz, trip, type);
369 * Alright, we handled this trip successfully.
370 * So, start monitoring again.
372 monitor_thermal_zone(tz);
376 * thermal_zone_get_temp() - returns its the temperature of thermal zone
377 * @tz: a valid pointer to a struct thermal_zone_device
378 * @temp: a valid pointer to where to store the resulting temperature.
380 * When a valid thermal zone reference is passed, it will fetch its
381 * temperature and fill @temp.
383 * Return: On success returns 0, an error code otherwise
385 int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
388 #ifdef CONFIG_THERMAL_EMULATION
390 unsigned long crit_temp = -1UL;
391 enum thermal_trip_type type;
394 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
397 mutex_lock(&tz->lock);
399 ret = tz->ops->get_temp(tz, temp);
400 #ifdef CONFIG_THERMAL_EMULATION
401 if (!tz->emul_temperature)
404 for (count = 0; count < tz->trips; count++) {
405 ret = tz->ops->get_trip_type(tz, count, &type);
406 if (!ret && type == THERMAL_TRIP_CRITICAL) {
407 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
415 if (*temp < crit_temp)
416 *temp = tz->emul_temperature;
419 mutex_unlock(&tz->lock);
423 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
425 static void update_temperature(struct thermal_zone_device *tz)
430 ret = thermal_zone_get_temp(tz, &temp);
432 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
437 mutex_lock(&tz->lock);
438 tz->last_temperature = tz->temperature;
439 tz->temperature = temp;
440 mutex_unlock(&tz->lock);
442 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
443 tz->last_temperature, tz->temperature);
446 void thermal_zone_device_update(struct thermal_zone_device *tz)
450 if (!tz->ops->get_temp)
453 update_temperature(tz);
455 for (count = 0; count < tz->trips; count++)
456 handle_thermal_trip(tz, count);
458 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
460 static void thermal_zone_device_check(struct work_struct *work)
462 struct thermal_zone_device *tz = container_of(work, struct
465 thermal_zone_device_update(tz);
468 /* sys I/F for thermal zone */
470 #define to_thermal_zone(_dev) \
471 container_of(_dev, struct thermal_zone_device, device)
474 type_show(struct device *dev, struct device_attribute *attr, char *buf)
476 struct thermal_zone_device *tz = to_thermal_zone(dev);
478 return sprintf(buf, "%s\n", tz->type);
482 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
484 struct thermal_zone_device *tz = to_thermal_zone(dev);
488 ret = thermal_zone_get_temp(tz, &temperature);
493 return sprintf(buf, "%ld\n", temperature);
497 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
499 struct thermal_zone_device *tz = to_thermal_zone(dev);
500 enum thermal_device_mode mode;
503 if (!tz->ops->get_mode)
506 result = tz->ops->get_mode(tz, &mode);
510 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
515 mode_store(struct device *dev, struct device_attribute *attr,
516 const char *buf, size_t count)
518 struct thermal_zone_device *tz = to_thermal_zone(dev);
521 if (!tz->ops->set_mode)
524 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
525 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
526 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
527 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
538 trip_point_type_show(struct device *dev, struct device_attribute *attr,
541 struct thermal_zone_device *tz = to_thermal_zone(dev);
542 enum thermal_trip_type type;
545 if (!tz->ops->get_trip_type)
548 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
551 result = tz->ops->get_trip_type(tz, trip, &type);
556 case THERMAL_TRIP_CRITICAL:
557 return sprintf(buf, "critical\n");
558 case THERMAL_TRIP_HOT:
559 return sprintf(buf, "hot\n");
560 case THERMAL_TRIP_PASSIVE:
561 return sprintf(buf, "passive\n");
562 case THERMAL_TRIP_ACTIVE:
563 return sprintf(buf, "active\n");
565 return sprintf(buf, "unknown\n");
570 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
571 const char *buf, size_t count)
573 struct thermal_zone_device *tz = to_thermal_zone(dev);
575 unsigned long temperature;
577 if (!tz->ops->set_trip_temp)
580 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
583 if (kstrtoul(buf, 10, &temperature))
586 ret = tz->ops->set_trip_temp(tz, trip, temperature);
588 return ret ? ret : count;
592 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
595 struct thermal_zone_device *tz = to_thermal_zone(dev);
599 if (!tz->ops->get_trip_temp)
602 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
605 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
610 return sprintf(buf, "%ld\n", temperature);
614 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
615 const char *buf, size_t count)
617 struct thermal_zone_device *tz = to_thermal_zone(dev);
619 unsigned long temperature;
621 if (!tz->ops->set_trip_hyst)
624 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
627 if (kstrtoul(buf, 10, &temperature))
631 * We are not doing any check on the 'temperature' value
632 * here. The driver implementing 'set_trip_hyst' has to
635 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
637 return ret ? ret : count;
641 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
644 struct thermal_zone_device *tz = to_thermal_zone(dev);
646 unsigned long temperature;
648 if (!tz->ops->get_trip_hyst)
651 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
654 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
656 return ret ? ret : sprintf(buf, "%ld\n", temperature);
660 passive_store(struct device *dev, struct device_attribute *attr,
661 const char *buf, size_t count)
663 struct thermal_zone_device *tz = to_thermal_zone(dev);
664 struct thermal_cooling_device *cdev = NULL;
667 if (!sscanf(buf, "%d\n", &state))
670 /* sanity check: values below 1000 millicelcius don't make sense
671 * and can cause the system to go into a thermal heart attack
673 if (state && state < 1000)
676 if (state && !tz->forced_passive) {
677 mutex_lock(&thermal_list_lock);
678 list_for_each_entry(cdev, &thermal_cdev_list, node) {
679 if (!strncmp("Processor", cdev->type,
680 sizeof("Processor")))
681 thermal_zone_bind_cooling_device(tz,
682 THERMAL_TRIPS_NONE, cdev,
686 mutex_unlock(&thermal_list_lock);
687 if (!tz->passive_delay)
688 tz->passive_delay = 1000;
689 } else if (!state && tz->forced_passive) {
690 mutex_lock(&thermal_list_lock);
691 list_for_each_entry(cdev, &thermal_cdev_list, node) {
692 if (!strncmp("Processor", cdev->type,
693 sizeof("Processor")))
694 thermal_zone_unbind_cooling_device(tz,
698 mutex_unlock(&thermal_list_lock);
699 tz->passive_delay = 0;
702 tz->forced_passive = state;
704 thermal_zone_device_update(tz);
710 passive_show(struct device *dev, struct device_attribute *attr,
713 struct thermal_zone_device *tz = to_thermal_zone(dev);
715 return sprintf(buf, "%d\n", tz->forced_passive);
719 policy_store(struct device *dev, struct device_attribute *attr,
720 const char *buf, size_t count)
723 struct thermal_zone_device *tz = to_thermal_zone(dev);
724 struct thermal_governor *gov;
726 mutex_lock(&thermal_governor_lock);
728 gov = __find_governor(buf);
736 mutex_unlock(&thermal_governor_lock);
741 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
743 struct thermal_zone_device *tz = to_thermal_zone(dev);
745 return sprintf(buf, "%s\n", tz->governor->name);
748 #ifdef CONFIG_THERMAL_EMULATION
750 emul_temp_store(struct device *dev, struct device_attribute *attr,
751 const char *buf, size_t count)
753 struct thermal_zone_device *tz = to_thermal_zone(dev);
755 unsigned long temperature;
757 if (kstrtoul(buf, 10, &temperature))
760 if (!tz->ops->set_emul_temp) {
761 mutex_lock(&tz->lock);
762 tz->emul_temperature = temperature;
763 mutex_unlock(&tz->lock);
765 ret = tz->ops->set_emul_temp(tz, temperature);
768 return ret ? ret : count;
770 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
771 #endif/*CONFIG_THERMAL_EMULATION*/
773 static DEVICE_ATTR(type, 0444, type_show, NULL);
774 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
775 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
776 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
777 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
779 /* sys I/F for cooling device */
780 #define to_cooling_device(_dev) \
781 container_of(_dev, struct thermal_cooling_device, device)
784 thermal_cooling_device_type_show(struct device *dev,
785 struct device_attribute *attr, char *buf)
787 struct thermal_cooling_device *cdev = to_cooling_device(dev);
789 return sprintf(buf, "%s\n", cdev->type);
793 thermal_cooling_device_max_state_show(struct device *dev,
794 struct device_attribute *attr, char *buf)
796 struct thermal_cooling_device *cdev = to_cooling_device(dev);
800 ret = cdev->ops->get_max_state(cdev, &state);
803 return sprintf(buf, "%ld\n", state);
807 thermal_cooling_device_cur_state_show(struct device *dev,
808 struct device_attribute *attr, char *buf)
810 struct thermal_cooling_device *cdev = to_cooling_device(dev);
814 ret = cdev->ops->get_cur_state(cdev, &state);
817 return sprintf(buf, "%ld\n", state);
821 thermal_cooling_device_cur_state_store(struct device *dev,
822 struct device_attribute *attr,
823 const char *buf, size_t count)
825 struct thermal_cooling_device *cdev = to_cooling_device(dev);
829 if (!sscanf(buf, "%ld\n", &state))
835 result = cdev->ops->set_cur_state(cdev, state);
841 static struct device_attribute dev_attr_cdev_type =
842 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
843 static DEVICE_ATTR(max_state, 0444,
844 thermal_cooling_device_max_state_show, NULL);
845 static DEVICE_ATTR(cur_state, 0644,
846 thermal_cooling_device_cur_state_show,
847 thermal_cooling_device_cur_state_store);
850 thermal_cooling_device_trip_point_show(struct device *dev,
851 struct device_attribute *attr, char *buf)
853 struct thermal_instance *instance;
856 container_of(attr, struct thermal_instance, attr);
858 if (instance->trip == THERMAL_TRIPS_NONE)
859 return sprintf(buf, "-1\n");
861 return sprintf(buf, "%d\n", instance->trip);
864 /* Device management */
867 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
868 * @tz: pointer to struct thermal_zone_device
869 * @trip: indicates which trip point the cooling devices is
870 * associated with in this thermal zone.
871 * @cdev: pointer to struct thermal_cooling_device
872 * @upper: the Maximum cooling state for this trip point.
873 * THERMAL_NO_LIMIT means no upper limit,
874 * and the cooling device can be in max_state.
875 * @lower: the Minimum cooling state can be used for this trip point.
876 * THERMAL_NO_LIMIT means no lower limit,
877 * and the cooling device can be in cooling state 0.
879 * This interface function bind a thermal cooling device to the certain trip
880 * point of a thermal zone device.
881 * This function is usually called in the thermal zone device .bind callback.
883 * Return: 0 on success, the proper error value otherwise.
885 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
887 struct thermal_cooling_device *cdev,
888 unsigned long upper, unsigned long lower)
890 struct thermal_instance *dev;
891 struct thermal_instance *pos;
892 struct thermal_zone_device *pos1;
893 struct thermal_cooling_device *pos2;
894 unsigned long max_state;
897 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
900 list_for_each_entry(pos1, &thermal_tz_list, node) {
904 list_for_each_entry(pos2, &thermal_cdev_list, node) {
909 if (tz != pos1 || cdev != pos2)
912 cdev->ops->get_max_state(cdev, &max_state);
914 /* lower default 0, upper default max_state */
915 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
916 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
918 if (lower > upper || upper > max_state)
922 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
930 dev->target = THERMAL_NO_TARGET;
932 result = get_idr(&tz->idr, &tz->lock, &dev->id);
936 sprintf(dev->name, "cdev%d", dev->id);
938 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
942 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
943 sysfs_attr_init(&dev->attr.attr);
944 dev->attr.attr.name = dev->attr_name;
945 dev->attr.attr.mode = 0444;
946 dev->attr.show = thermal_cooling_device_trip_point_show;
947 result = device_create_file(&tz->device, &dev->attr);
949 goto remove_symbol_link;
951 mutex_lock(&tz->lock);
952 mutex_lock(&cdev->lock);
953 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
954 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
959 list_add_tail(&dev->tz_node, &tz->thermal_instances);
960 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
962 mutex_unlock(&cdev->lock);
963 mutex_unlock(&tz->lock);
968 device_remove_file(&tz->device, &dev->attr);
970 sysfs_remove_link(&tz->device.kobj, dev->name);
972 release_idr(&tz->idr, &tz->lock, dev->id);
977 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
980 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
982 * @tz: pointer to a struct thermal_zone_device.
983 * @trip: indicates which trip point the cooling devices is
984 * associated with in this thermal zone.
985 * @cdev: pointer to a struct thermal_cooling_device.
987 * This interface function unbind a thermal cooling device from the certain
988 * trip point of a thermal zone device.
989 * This function is usually called in the thermal zone device .unbind callback.
991 * Return: 0 on success, the proper error value otherwise.
993 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
995 struct thermal_cooling_device *cdev)
997 struct thermal_instance *pos, *next;
999 mutex_lock(&tz->lock);
1000 mutex_lock(&cdev->lock);
1001 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1002 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1003 list_del(&pos->tz_node);
1004 list_del(&pos->cdev_node);
1005 mutex_unlock(&cdev->lock);
1006 mutex_unlock(&tz->lock);
1010 mutex_unlock(&cdev->lock);
1011 mutex_unlock(&tz->lock);
1016 device_remove_file(&tz->device, &pos->attr);
1017 sysfs_remove_link(&tz->device.kobj, pos->name);
1018 release_idr(&tz->idr, &tz->lock, pos->id);
1022 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1024 static void thermal_release(struct device *dev)
1026 struct thermal_zone_device *tz;
1027 struct thermal_cooling_device *cdev;
1029 if (!strncmp(dev_name(dev), "thermal_zone",
1030 sizeof("thermal_zone") - 1)) {
1031 tz = to_thermal_zone(dev);
1034 cdev = to_cooling_device(dev);
1039 static struct class thermal_class = {
1041 .dev_release = thermal_release,
1045 * __thermal_cooling_device_register() - register a new thermal cooling device
1046 * @np: a pointer to a device tree node.
1047 * @type: the thermal cooling device type.
1048 * @devdata: device private data.
1049 * @ops: standard thermal cooling devices callbacks.
1051 * This interface function adds a new thermal cooling device (fan/processor/...)
1052 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1053 * to all the thermal zone devices registered at the same time.
1054 * It also gives the opportunity to link the cooling device to a device tree
1055 * node, so that it can be bound to a thermal zone created out of device tree.
1057 * Return: a pointer to the created struct thermal_cooling_device or an
1058 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1060 static struct thermal_cooling_device *
1061 __thermal_cooling_device_register(struct device_node *np,
1062 char *type, void *devdata,
1063 const struct thermal_cooling_device_ops *ops)
1065 struct thermal_cooling_device *cdev;
1068 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1069 return ERR_PTR(-EINVAL);
1071 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1072 !ops->set_cur_state)
1073 return ERR_PTR(-EINVAL);
1075 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1077 return ERR_PTR(-ENOMEM);
1079 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1082 return ERR_PTR(result);
1085 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1086 mutex_init(&cdev->lock);
1087 INIT_LIST_HEAD(&cdev->thermal_instances);
1090 cdev->updated = true;
1091 cdev->device.class = &thermal_class;
1092 cdev->devdata = devdata;
1093 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1094 result = device_register(&cdev->device);
1096 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1098 return ERR_PTR(result);
1103 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1108 result = device_create_file(&cdev->device, &dev_attr_max_state);
1112 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1116 /* Add 'this' new cdev to the global cdev list */
1117 mutex_lock(&thermal_list_lock);
1118 list_add(&cdev->node, &thermal_cdev_list);
1119 mutex_unlock(&thermal_list_lock);
1121 /* Update binding information for 'this' new cdev */
1127 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1128 device_unregister(&cdev->device);
1129 return ERR_PTR(result);
1133 * thermal_cooling_device_register() - register a new thermal cooling device
1134 * @type: the thermal cooling device type.
1135 * @devdata: device private data.
1136 * @ops: standard thermal cooling devices callbacks.
1138 * This interface function adds a new thermal cooling device (fan/processor/...)
1139 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1140 * to all the thermal zone devices registered at the same time.
1142 * Return: a pointer to the created struct thermal_cooling_device or an
1143 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1145 struct thermal_cooling_device *
1146 thermal_cooling_device_register(char *type, void *devdata,
1147 const struct thermal_cooling_device_ops *ops)
1149 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1151 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1154 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1155 * @np: a pointer to a device tree node.
1156 * @type: the thermal cooling device type.
1157 * @devdata: device private data.
1158 * @ops: standard thermal cooling devices callbacks.
1160 * This function will register a cooling device with device tree node reference.
1161 * This interface function adds a new thermal cooling device (fan/processor/...)
1162 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1163 * to all the thermal zone devices registered at the same time.
1165 * Return: a pointer to the created struct thermal_cooling_device or an
1166 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1168 struct thermal_cooling_device *
1169 thermal_of_cooling_device_register(struct device_node *np,
1170 char *type, void *devdata,
1171 const struct thermal_cooling_device_ops *ops)
1173 return __thermal_cooling_device_register(np, type, devdata, ops);
1175 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1178 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1179 * @cdev: the thermal cooling device to remove.
1181 * thermal_cooling_device_unregister() must be called when the device is no
1184 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1187 const struct thermal_zone_params *tzp;
1188 struct thermal_zone_device *tz;
1189 struct thermal_cooling_device *pos = NULL;
1194 mutex_lock(&thermal_list_lock);
1195 list_for_each_entry(pos, &thermal_cdev_list, node)
1199 /* thermal cooling device not found */
1200 mutex_unlock(&thermal_list_lock);
1203 list_del(&cdev->node);
1205 /* Unbind all thermal zones associated with 'this' cdev */
1206 list_for_each_entry(tz, &thermal_tz_list, node) {
1207 if (tz->ops->unbind) {
1208 tz->ops->unbind(tz, cdev);
1212 if (!tz->tzp || !tz->tzp->tbp)
1216 for (i = 0; i < tzp->num_tbps; i++) {
1217 if (tzp->tbp[i].cdev == cdev) {
1218 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1219 tzp->tbp[i].cdev = NULL;
1224 mutex_unlock(&thermal_list_lock);
1227 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1228 device_remove_file(&cdev->device, &dev_attr_max_state);
1229 device_remove_file(&cdev->device, &dev_attr_cur_state);
1231 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1232 device_unregister(&cdev->device);
1235 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1237 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1239 struct thermal_instance *instance;
1240 unsigned long target = 0;
1242 /* cooling device is updated*/
1246 mutex_lock(&cdev->lock);
1247 /* Make sure cdev enters the deepest cooling state */
1248 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1249 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1250 instance->tz->id, instance->target);
1251 if (instance->target == THERMAL_NO_TARGET)
1253 if (instance->target > target)
1254 target = instance->target;
1256 mutex_unlock(&cdev->lock);
1257 cdev->ops->set_cur_state(cdev, target);
1258 cdev->updated = true;
1259 dev_dbg(&cdev->device, "set to state %lu\n", target);
1261 EXPORT_SYMBOL(thermal_cdev_update);
1264 * thermal_notify_framework - Sensor drivers use this API to notify framework
1265 * @tz: thermal zone device
1266 * @trip: indicates which trip point has been crossed
1268 * This function handles the trip events from sensor drivers. It starts
1269 * throttling the cooling devices according to the policy configured.
1270 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1271 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1272 * The throttling policy is based on the configured platform data; if no
1273 * platform data is provided, this uses the step_wise throttling policy.
1275 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1277 handle_thermal_trip(tz, trip);
1279 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1282 * create_trip_attrs() - create attributes for trip points
1283 * @tz: the thermal zone device
1284 * @mask: Writeable trip point bitmap.
1286 * helper function to instantiate sysfs entries for every trip
1287 * point and its properties of a struct thermal_zone_device.
1289 * Return: 0 on success, the proper error value otherwise.
1291 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1294 int size = sizeof(struct thermal_attr) * tz->trips;
1296 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1297 if (!tz->trip_type_attrs)
1300 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1301 if (!tz->trip_temp_attrs) {
1302 kfree(tz->trip_type_attrs);
1306 if (tz->ops->get_trip_hyst) {
1307 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1308 if (!tz->trip_hyst_attrs) {
1309 kfree(tz->trip_type_attrs);
1310 kfree(tz->trip_temp_attrs);
1316 for (indx = 0; indx < tz->trips; indx++) {
1317 /* create trip type attribute */
1318 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1319 "trip_point_%d_type", indx);
1321 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1322 tz->trip_type_attrs[indx].attr.attr.name =
1323 tz->trip_type_attrs[indx].name;
1324 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1325 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1327 device_create_file(&tz->device,
1328 &tz->trip_type_attrs[indx].attr);
1330 /* create trip temp attribute */
1331 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1332 "trip_point_%d_temp", indx);
1334 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1335 tz->trip_temp_attrs[indx].attr.attr.name =
1336 tz->trip_temp_attrs[indx].name;
1337 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1338 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1339 if (mask & (1 << indx)) {
1340 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1341 tz->trip_temp_attrs[indx].attr.store =
1342 trip_point_temp_store;
1345 device_create_file(&tz->device,
1346 &tz->trip_temp_attrs[indx].attr);
1348 /* create Optional trip hyst attribute */
1349 if (!tz->ops->get_trip_hyst)
1351 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1352 "trip_point_%d_hyst", indx);
1354 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1355 tz->trip_hyst_attrs[indx].attr.attr.name =
1356 tz->trip_hyst_attrs[indx].name;
1357 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1358 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1359 if (tz->ops->set_trip_hyst) {
1360 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1361 tz->trip_hyst_attrs[indx].attr.store =
1362 trip_point_hyst_store;
1365 device_create_file(&tz->device,
1366 &tz->trip_hyst_attrs[indx].attr);
1371 static void remove_trip_attrs(struct thermal_zone_device *tz)
1375 for (indx = 0; indx < tz->trips; indx++) {
1376 device_remove_file(&tz->device,
1377 &tz->trip_type_attrs[indx].attr);
1378 device_remove_file(&tz->device,
1379 &tz->trip_temp_attrs[indx].attr);
1380 if (tz->ops->get_trip_hyst)
1381 device_remove_file(&tz->device,
1382 &tz->trip_hyst_attrs[indx].attr);
1384 kfree(tz->trip_type_attrs);
1385 kfree(tz->trip_temp_attrs);
1386 kfree(tz->trip_hyst_attrs);
1390 * thermal_zone_device_register() - register a new thermal zone device
1391 * @type: the thermal zone device type
1392 * @trips: the number of trip points the thermal zone support
1393 * @mask: a bit string indicating the writeablility of trip points
1394 * @devdata: private device data
1395 * @ops: standard thermal zone device callbacks
1396 * @tzp: thermal zone platform parameters
1397 * @passive_delay: number of milliseconds to wait between polls when
1398 * performing passive cooling
1399 * @polling_delay: number of milliseconds to wait between polls when checking
1400 * whether trip points have been crossed (0 for interrupt
1403 * This interface function adds a new thermal zone device (sensor) to
1404 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1405 * thermal cooling devices registered at the same time.
1406 * thermal_zone_device_unregister() must be called when the device is no
1407 * longer needed. The passive cooling depends on the .get_trend() return value.
1409 * Return: a pointer to the created struct thermal_zone_device or an
1410 * in case of error, an ERR_PTR. Caller must check return value with
1411 * IS_ERR*() helpers.
1413 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1414 int trips, int mask, void *devdata,
1415 struct thermal_zone_device_ops *ops,
1416 const struct thermal_zone_params *tzp,
1417 int passive_delay, int polling_delay)
1419 struct thermal_zone_device *tz;
1420 enum thermal_trip_type trip_type;
1425 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1426 return ERR_PTR(-EINVAL);
1428 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1429 return ERR_PTR(-EINVAL);
1432 return ERR_PTR(-EINVAL);
1434 if (trips > 0 && !ops->get_trip_type)
1435 return ERR_PTR(-EINVAL);
1437 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1439 return ERR_PTR(-ENOMEM);
1441 INIT_LIST_HEAD(&tz->thermal_instances);
1443 mutex_init(&tz->lock);
1444 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1447 return ERR_PTR(result);
1450 strlcpy(tz->type, type ? : "", sizeof(tz->type));
1453 tz->device.class = &thermal_class;
1454 tz->devdata = devdata;
1456 tz->passive_delay = passive_delay;
1457 tz->polling_delay = polling_delay;
1459 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1460 result = device_register(&tz->device);
1462 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1464 return ERR_PTR(result);
1469 result = device_create_file(&tz->device, &dev_attr_type);
1474 result = device_create_file(&tz->device, &dev_attr_temp);
1478 if (ops->get_mode) {
1479 result = device_create_file(&tz->device, &dev_attr_mode);
1484 result = create_trip_attrs(tz, mask);
1488 for (count = 0; count < trips; count++) {
1489 tz->ops->get_trip_type(tz, count, &trip_type);
1490 if (trip_type == THERMAL_TRIP_PASSIVE)
1495 result = device_create_file(&tz->device, &dev_attr_passive);
1500 #ifdef CONFIG_THERMAL_EMULATION
1501 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1505 /* Create policy attribute */
1506 result = device_create_file(&tz->device, &dev_attr_policy);
1510 /* Update 'this' zone's governor information */
1511 mutex_lock(&thermal_governor_lock);
1514 tz->governor = __find_governor(tz->tzp->governor_name);
1516 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1518 mutex_unlock(&thermal_governor_lock);
1520 if (!tz->tzp || !tz->tzp->no_hwmon) {
1521 result = thermal_add_hwmon_sysfs(tz);
1526 mutex_lock(&thermal_list_lock);
1527 list_add_tail(&tz->node, &thermal_tz_list);
1528 mutex_unlock(&thermal_list_lock);
1530 /* Bind cooling devices for this zone */
1533 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1535 if (!tz->ops->get_temp)
1536 thermal_zone_device_set_polling(tz, 0);
1538 thermal_zone_device_update(tz);
1544 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1545 device_unregister(&tz->device);
1546 return ERR_PTR(result);
1548 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1551 * thermal_device_unregister - removes the registered thermal zone device
1552 * @tz: the thermal zone device to remove
1554 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1557 const struct thermal_zone_params *tzp;
1558 struct thermal_cooling_device *cdev;
1559 struct thermal_zone_device *pos = NULL;
1566 mutex_lock(&thermal_list_lock);
1567 list_for_each_entry(pos, &thermal_tz_list, node)
1571 /* thermal zone device not found */
1572 mutex_unlock(&thermal_list_lock);
1575 list_del(&tz->node);
1577 /* Unbind all cdevs associated with 'this' thermal zone */
1578 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1579 if (tz->ops->unbind) {
1580 tz->ops->unbind(tz, cdev);
1584 if (!tzp || !tzp->tbp)
1587 for (i = 0; i < tzp->num_tbps; i++) {
1588 if (tzp->tbp[i].cdev == cdev) {
1589 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1590 tzp->tbp[i].cdev = NULL;
1595 mutex_unlock(&thermal_list_lock);
1597 thermal_zone_device_set_polling(tz, 0);
1600 device_remove_file(&tz->device, &dev_attr_type);
1601 device_remove_file(&tz->device, &dev_attr_temp);
1602 if (tz->ops->get_mode)
1603 device_remove_file(&tz->device, &dev_attr_mode);
1604 device_remove_file(&tz->device, &dev_attr_policy);
1605 remove_trip_attrs(tz);
1606 tz->governor = NULL;
1608 thermal_remove_hwmon_sysfs(tz);
1609 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1610 idr_destroy(&tz->idr);
1611 mutex_destroy(&tz->lock);
1612 device_unregister(&tz->device);
1615 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1618 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1619 * @name: thermal zone name to fetch the temperature
1621 * When only one zone is found with the passed name, returns a reference to it.
1623 * Return: On success returns a reference to an unique thermal zone with
1624 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1625 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1627 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1629 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1630 unsigned int found = 0;
1635 mutex_lock(&thermal_list_lock);
1636 list_for_each_entry(pos, &thermal_tz_list, node)
1637 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1641 mutex_unlock(&thermal_list_lock);
1643 /* nothing has been found, thus an error code for it */
1645 ref = ERR_PTR(-ENODEV);
1647 /* Success only when an unique zone is found */
1648 ref = ERR_PTR(-EEXIST);
1653 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1656 static struct genl_family thermal_event_genl_family = {
1657 .id = GENL_ID_GENERATE,
1658 .name = THERMAL_GENL_FAMILY_NAME,
1659 .version = THERMAL_GENL_VERSION,
1660 .maxattr = THERMAL_GENL_ATTR_MAX,
1663 static struct genl_multicast_group thermal_event_mcgrp = {
1664 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1667 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1670 struct sk_buff *skb;
1671 struct nlattr *attr;
1672 struct thermal_genl_event *thermal_event;
1676 static unsigned int thermal_event_seqnum;
1681 /* allocate memory */
1682 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1685 skb = genlmsg_new(size, GFP_ATOMIC);
1689 /* add the genetlink message header */
1690 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1691 &thermal_event_genl_family, 0,
1692 THERMAL_GENL_CMD_EVENT);
1699 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1700 sizeof(struct thermal_genl_event));
1707 thermal_event = nla_data(attr);
1708 if (!thermal_event) {
1713 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1715 thermal_event->orig = tz->id;
1716 thermal_event->event = event;
1718 /* send multicast genetlink message */
1719 result = genlmsg_end(skb, msg_header);
1725 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1727 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1731 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1733 static int genetlink_init(void)
1737 result = genl_register_family(&thermal_event_genl_family);
1741 result = genl_register_mc_group(&thermal_event_genl_family,
1742 &thermal_event_mcgrp);
1744 genl_unregister_family(&thermal_event_genl_family);
1748 static void genetlink_exit(void)
1750 genl_unregister_family(&thermal_event_genl_family);
1752 #else /* !CONFIG_NET */
1753 static inline int genetlink_init(void) { return 0; }
1754 static inline void genetlink_exit(void) {}
1755 #endif /* !CONFIG_NET */
1757 static int __init thermal_register_governors(void)
1761 result = thermal_gov_step_wise_register();
1765 result = thermal_gov_fair_share_register();
1769 return thermal_gov_user_space_register();
1772 static void thermal_unregister_governors(void)
1774 thermal_gov_step_wise_unregister();
1775 thermal_gov_fair_share_unregister();
1776 thermal_gov_user_space_unregister();
1779 static int __init thermal_init(void)
1783 result = thermal_register_governors();
1787 result = class_register(&thermal_class);
1789 goto unregister_governors;
1791 result = genetlink_init();
1793 goto unregister_class;
1795 result = of_parse_thermal_zones();
1803 unregister_governors:
1804 thermal_unregister_governors();
1806 class_unregister(&thermal_class);
1808 idr_destroy(&thermal_tz_idr);
1809 idr_destroy(&thermal_cdev_idr);
1810 mutex_destroy(&thermal_idr_lock);
1811 mutex_destroy(&thermal_list_lock);
1812 mutex_destroy(&thermal_governor_lock);
1816 static void __exit thermal_exit(void)
1818 of_thermal_destroy_zones();
1820 class_unregister(&thermal_class);
1821 thermal_unregister_governors();
1822 idr_destroy(&thermal_tz_idr);
1823 idr_destroy(&thermal_cdev_idr);
1824 mutex_destroy(&thermal_idr_lock);
1825 mutex_destroy(&thermal_list_lock);
1826 mutex_destroy(&thermal_governor_lock);
1829 fs_initcall(thermal_init);
1830 module_exit(thermal_exit);