2 * thermal.c - Generic Thermal Management Sysfs support.
4 * Copyright (C) 2008 Intel Corp
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 #define CREATE_TRACE_POINTS
42 #include <trace/events/thermal.h>
44 #include "thermal_core.h"
45 #include "thermal_hwmon.h"
47 MODULE_AUTHOR("Zhang Rui");
48 MODULE_DESCRIPTION("Generic thermal management sysfs support");
49 MODULE_LICENSE("GPL v2");
51 static DEFINE_IDR(thermal_tz_idr);
52 static DEFINE_IDR(thermal_cdev_idr);
53 static DEFINE_MUTEX(thermal_idr_lock);
55 static LIST_HEAD(thermal_tz_list);
56 static LIST_HEAD(thermal_cdev_list);
57 static LIST_HEAD(thermal_governor_list);
59 static DEFINE_MUTEX(thermal_list_lock);
60 static DEFINE_MUTEX(thermal_governor_lock);
62 static struct thermal_governor *def_governor;
64 static struct thermal_governor *__find_governor(const char *name)
66 struct thermal_governor *pos;
68 if (!name || !name[0])
71 list_for_each_entry(pos, &thermal_governor_list, governor_list)
72 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
78 int thermal_register_governor(struct thermal_governor *governor)
82 struct thermal_zone_device *pos;
87 mutex_lock(&thermal_governor_lock);
90 if (__find_governor(governor->name) == NULL) {
92 list_add(&governor->governor_list, &thermal_governor_list);
93 if (!def_governor && !strncmp(governor->name,
94 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
95 def_governor = governor;
98 mutex_lock(&thermal_list_lock);
100 list_for_each_entry(pos, &thermal_tz_list, node) {
102 * only thermal zones with specified tz->tzp->governor_name
103 * may run with tz->govenor unset
108 name = pos->tzp->governor_name;
110 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH))
111 pos->governor = governor;
114 mutex_unlock(&thermal_list_lock);
115 mutex_unlock(&thermal_governor_lock);
120 void thermal_unregister_governor(struct thermal_governor *governor)
122 struct thermal_zone_device *pos;
127 mutex_lock(&thermal_governor_lock);
129 if (__find_governor(governor->name) == NULL)
132 mutex_lock(&thermal_list_lock);
134 list_for_each_entry(pos, &thermal_tz_list, node) {
135 if (!strncasecmp(pos->governor->name, governor->name,
136 THERMAL_NAME_LENGTH))
137 pos->governor = NULL;
140 mutex_unlock(&thermal_list_lock);
141 list_del(&governor->governor_list);
143 mutex_unlock(&thermal_governor_lock);
147 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
153 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
156 if (unlikely(ret < 0))
162 static void release_idr(struct idr *idr, struct mutex *lock, int id)
171 int get_tz_trend(struct thermal_zone_device *tz, int trip)
173 enum thermal_trend trend;
175 if (tz->emul_temperature || !tz->ops->get_trend ||
176 tz->ops->get_trend(tz, trip, &trend)) {
177 if (tz->temperature > tz->last_temperature)
178 trend = THERMAL_TREND_RAISING;
179 else if (tz->temperature < tz->last_temperature)
180 trend = THERMAL_TREND_DROPPING;
182 trend = THERMAL_TREND_STABLE;
187 EXPORT_SYMBOL(get_tz_trend);
189 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
190 struct thermal_cooling_device *cdev, int trip)
192 struct thermal_instance *pos = NULL;
193 struct thermal_instance *target_instance = NULL;
195 mutex_lock(&tz->lock);
196 mutex_lock(&cdev->lock);
198 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
199 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
200 target_instance = pos;
205 mutex_unlock(&cdev->lock);
206 mutex_unlock(&tz->lock);
208 return target_instance;
210 EXPORT_SYMBOL(get_thermal_instance);
212 static void print_bind_err_msg(struct thermal_zone_device *tz,
213 struct thermal_cooling_device *cdev, int ret)
215 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
216 tz->type, cdev->type, ret);
219 static void __bind(struct thermal_zone_device *tz, int mask,
220 struct thermal_cooling_device *cdev,
221 unsigned long *limits)
225 for (i = 0; i < tz->trips; i++) {
226 if (mask & (1 << i)) {
227 unsigned long upper, lower;
229 upper = THERMAL_NO_LIMIT;
230 lower = THERMAL_NO_LIMIT;
232 lower = limits[i * 2];
233 upper = limits[i * 2 + 1];
235 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
238 print_bind_err_msg(tz, cdev, ret);
243 static void __unbind(struct thermal_zone_device *tz, int mask,
244 struct thermal_cooling_device *cdev)
248 for (i = 0; i < tz->trips; i++)
250 thermal_zone_unbind_cooling_device(tz, i, cdev);
253 static void bind_cdev(struct thermal_cooling_device *cdev)
256 const struct thermal_zone_params *tzp;
257 struct thermal_zone_device *pos = NULL;
259 mutex_lock(&thermal_list_lock);
261 list_for_each_entry(pos, &thermal_tz_list, node) {
262 if (!pos->tzp && !pos->ops->bind)
265 if (pos->ops->bind) {
266 ret = pos->ops->bind(pos, cdev);
268 print_bind_err_msg(pos, cdev, ret);
273 if (!tzp || !tzp->tbp)
276 for (i = 0; i < tzp->num_tbps; i++) {
277 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
279 if (tzp->tbp[i].match(pos, cdev))
281 tzp->tbp[i].cdev = cdev;
282 __bind(pos, tzp->tbp[i].trip_mask, cdev,
283 tzp->tbp[i].binding_limits);
287 mutex_unlock(&thermal_list_lock);
290 static void bind_tz(struct thermal_zone_device *tz)
293 struct thermal_cooling_device *pos = NULL;
294 const struct thermal_zone_params *tzp = tz->tzp;
296 if (!tzp && !tz->ops->bind)
299 mutex_lock(&thermal_list_lock);
301 /* If there is ops->bind, try to use ops->bind */
303 list_for_each_entry(pos, &thermal_cdev_list, node) {
304 ret = tz->ops->bind(tz, pos);
306 print_bind_err_msg(tz, pos, ret);
311 if (!tzp || !tzp->tbp)
314 list_for_each_entry(pos, &thermal_cdev_list, node) {
315 for (i = 0; i < tzp->num_tbps; i++) {
316 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
318 if (tzp->tbp[i].match(tz, pos))
320 tzp->tbp[i].cdev = pos;
321 __bind(tz, tzp->tbp[i].trip_mask, pos,
322 tzp->tbp[i].binding_limits);
326 mutex_unlock(&thermal_list_lock);
329 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
333 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
334 round_jiffies(msecs_to_jiffies(delay)));
336 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
337 msecs_to_jiffies(delay));
339 cancel_delayed_work(&tz->poll_queue);
342 static void monitor_thermal_zone(struct thermal_zone_device *tz)
344 mutex_lock(&tz->lock);
347 thermal_zone_device_set_polling(tz, tz->passive_delay);
348 else if (tz->polling_delay)
349 thermal_zone_device_set_polling(tz, tz->polling_delay);
351 thermal_zone_device_set_polling(tz, 0);
353 mutex_unlock(&tz->lock);
356 static void handle_non_critical_trips(struct thermal_zone_device *tz,
357 int trip, enum thermal_trip_type trip_type)
359 tz->governor ? tz->governor->throttle(tz, trip) :
360 def_governor->throttle(tz, trip);
363 static void handle_critical_trips(struct thermal_zone_device *tz,
364 int trip, enum thermal_trip_type trip_type)
368 tz->ops->get_trip_temp(tz, trip, &trip_temp);
370 /* If we have not crossed the trip_temp, we do not care. */
371 if (trip_temp <= 0 || tz->temperature < trip_temp)
374 trace_thermal_zone_trip(tz, trip, trip_type);
377 tz->ops->notify(tz, trip, trip_type);
379 if (trip_type == THERMAL_TRIP_CRITICAL) {
380 dev_emerg(&tz->device,
381 "critical temperature reached(%d C),shutting down\n",
382 tz->temperature / 1000);
383 orderly_poweroff(true);
387 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
389 enum thermal_trip_type type;
391 tz->ops->get_trip_type(tz, trip, &type);
393 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
394 handle_critical_trips(tz, trip, type);
396 handle_non_critical_trips(tz, trip, type);
398 * Alright, we handled this trip successfully.
399 * So, start monitoring again.
401 monitor_thermal_zone(tz);
405 * thermal_zone_get_temp() - returns its the temperature of thermal zone
406 * @tz: a valid pointer to a struct thermal_zone_device
407 * @temp: a valid pointer to where to store the resulting temperature.
409 * When a valid thermal zone reference is passed, it will fetch its
410 * temperature and fill @temp.
412 * Return: On success returns 0, an error code otherwise
414 int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
417 #ifdef CONFIG_THERMAL_EMULATION
419 unsigned long crit_temp = -1UL;
420 enum thermal_trip_type type;
423 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
426 mutex_lock(&tz->lock);
428 ret = tz->ops->get_temp(tz, temp);
429 #ifdef CONFIG_THERMAL_EMULATION
430 if (!tz->emul_temperature)
433 for (count = 0; count < tz->trips; count++) {
434 ret = tz->ops->get_trip_type(tz, count, &type);
435 if (!ret && type == THERMAL_TRIP_CRITICAL) {
436 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
444 if (*temp < crit_temp)
445 *temp = tz->emul_temperature;
448 mutex_unlock(&tz->lock);
452 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
454 static void update_temperature(struct thermal_zone_device *tz)
459 ret = thermal_zone_get_temp(tz, &temp);
461 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
466 mutex_lock(&tz->lock);
467 tz->last_temperature = tz->temperature;
468 tz->temperature = temp;
469 mutex_unlock(&tz->lock);
471 trace_thermal_temperature(tz);
472 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
473 tz->last_temperature, tz->temperature);
476 void thermal_zone_device_update(struct thermal_zone_device *tz)
480 if (!tz->ops->get_temp)
483 update_temperature(tz);
485 for (count = 0; count < tz->trips; count++)
486 handle_thermal_trip(tz, count);
488 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
490 static void thermal_zone_device_check(struct work_struct *work)
492 struct thermal_zone_device *tz = container_of(work, struct
495 thermal_zone_device_update(tz);
498 /* sys I/F for thermal zone */
500 #define to_thermal_zone(_dev) \
501 container_of(_dev, struct thermal_zone_device, device)
504 type_show(struct device *dev, struct device_attribute *attr, char *buf)
506 struct thermal_zone_device *tz = to_thermal_zone(dev);
508 return sprintf(buf, "%s\n", tz->type);
512 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
514 struct thermal_zone_device *tz = to_thermal_zone(dev);
518 ret = thermal_zone_get_temp(tz, &temperature);
523 return sprintf(buf, "%ld\n", temperature);
527 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
529 struct thermal_zone_device *tz = to_thermal_zone(dev);
530 enum thermal_device_mode mode;
533 if (!tz->ops->get_mode)
536 result = tz->ops->get_mode(tz, &mode);
540 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
545 mode_store(struct device *dev, struct device_attribute *attr,
546 const char *buf, size_t count)
548 struct thermal_zone_device *tz = to_thermal_zone(dev);
551 if (!tz->ops->set_mode)
554 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
555 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
556 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
557 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
568 trip_point_type_show(struct device *dev, struct device_attribute *attr,
571 struct thermal_zone_device *tz = to_thermal_zone(dev);
572 enum thermal_trip_type type;
575 if (!tz->ops->get_trip_type)
578 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
581 result = tz->ops->get_trip_type(tz, trip, &type);
586 case THERMAL_TRIP_CRITICAL:
587 return sprintf(buf, "critical\n");
588 case THERMAL_TRIP_HOT:
589 return sprintf(buf, "hot\n");
590 case THERMAL_TRIP_PASSIVE:
591 return sprintf(buf, "passive\n");
592 case THERMAL_TRIP_ACTIVE:
593 return sprintf(buf, "active\n");
595 return sprintf(buf, "unknown\n");
600 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
601 const char *buf, size_t count)
603 struct thermal_zone_device *tz = to_thermal_zone(dev);
605 unsigned long temperature;
607 if (!tz->ops->set_trip_temp)
610 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
613 if (kstrtoul(buf, 10, &temperature))
616 ret = tz->ops->set_trip_temp(tz, trip, temperature);
618 return ret ? ret : count;
622 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
625 struct thermal_zone_device *tz = to_thermal_zone(dev);
629 if (!tz->ops->get_trip_temp)
632 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
635 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
640 return sprintf(buf, "%ld\n", temperature);
644 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
645 const char *buf, size_t count)
647 struct thermal_zone_device *tz = to_thermal_zone(dev);
649 unsigned long temperature;
651 if (!tz->ops->set_trip_hyst)
654 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
657 if (kstrtoul(buf, 10, &temperature))
661 * We are not doing any check on the 'temperature' value
662 * here. The driver implementing 'set_trip_hyst' has to
665 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
667 return ret ? ret : count;
671 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
674 struct thermal_zone_device *tz = to_thermal_zone(dev);
676 unsigned long temperature;
678 if (!tz->ops->get_trip_hyst)
681 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
684 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
686 return ret ? ret : sprintf(buf, "%ld\n", temperature);
690 passive_store(struct device *dev, struct device_attribute *attr,
691 const char *buf, size_t count)
693 struct thermal_zone_device *tz = to_thermal_zone(dev);
694 struct thermal_cooling_device *cdev = NULL;
697 if (!sscanf(buf, "%d\n", &state))
700 /* sanity check: values below 1000 millicelcius don't make sense
701 * and can cause the system to go into a thermal heart attack
703 if (state && state < 1000)
706 if (state && !tz->forced_passive) {
707 mutex_lock(&thermal_list_lock);
708 list_for_each_entry(cdev, &thermal_cdev_list, node) {
709 if (!strncmp("Processor", cdev->type,
710 sizeof("Processor")))
711 thermal_zone_bind_cooling_device(tz,
712 THERMAL_TRIPS_NONE, cdev,
716 mutex_unlock(&thermal_list_lock);
717 if (!tz->passive_delay)
718 tz->passive_delay = 1000;
719 } else if (!state && tz->forced_passive) {
720 mutex_lock(&thermal_list_lock);
721 list_for_each_entry(cdev, &thermal_cdev_list, node) {
722 if (!strncmp("Processor", cdev->type,
723 sizeof("Processor")))
724 thermal_zone_unbind_cooling_device(tz,
728 mutex_unlock(&thermal_list_lock);
729 tz->passive_delay = 0;
732 tz->forced_passive = state;
734 thermal_zone_device_update(tz);
740 passive_show(struct device *dev, struct device_attribute *attr,
743 struct thermal_zone_device *tz = to_thermal_zone(dev);
745 return sprintf(buf, "%d\n", tz->forced_passive);
749 policy_store(struct device *dev, struct device_attribute *attr,
750 const char *buf, size_t count)
753 struct thermal_zone_device *tz = to_thermal_zone(dev);
754 struct thermal_governor *gov;
755 char name[THERMAL_NAME_LENGTH];
757 snprintf(name, sizeof(name), "%s", buf);
759 mutex_lock(&thermal_governor_lock);
760 mutex_lock(&tz->lock);
762 gov = __find_governor(strim(name));
770 mutex_unlock(&tz->lock);
771 mutex_unlock(&thermal_governor_lock);
776 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
778 struct thermal_zone_device *tz = to_thermal_zone(dev);
780 return sprintf(buf, "%s\n", tz->governor->name);
783 #ifdef CONFIG_THERMAL_EMULATION
785 emul_temp_store(struct device *dev, struct device_attribute *attr,
786 const char *buf, size_t count)
788 struct thermal_zone_device *tz = to_thermal_zone(dev);
790 unsigned long temperature;
792 if (kstrtoul(buf, 10, &temperature))
795 if (!tz->ops->set_emul_temp) {
796 mutex_lock(&tz->lock);
797 tz->emul_temperature = temperature;
798 mutex_unlock(&tz->lock);
800 ret = tz->ops->set_emul_temp(tz, temperature);
804 thermal_zone_device_update(tz);
806 return ret ? ret : count;
808 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
809 #endif/*CONFIG_THERMAL_EMULATION*/
811 static DEVICE_ATTR(type, 0444, type_show, NULL);
812 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
813 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
814 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
815 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
817 /* sys I/F for cooling device */
818 #define to_cooling_device(_dev) \
819 container_of(_dev, struct thermal_cooling_device, device)
822 thermal_cooling_device_type_show(struct device *dev,
823 struct device_attribute *attr, char *buf)
825 struct thermal_cooling_device *cdev = to_cooling_device(dev);
827 return sprintf(buf, "%s\n", cdev->type);
831 thermal_cooling_device_max_state_show(struct device *dev,
832 struct device_attribute *attr, char *buf)
834 struct thermal_cooling_device *cdev = to_cooling_device(dev);
838 ret = cdev->ops->get_max_state(cdev, &state);
841 return sprintf(buf, "%ld\n", state);
845 thermal_cooling_device_cur_state_show(struct device *dev,
846 struct device_attribute *attr, char *buf)
848 struct thermal_cooling_device *cdev = to_cooling_device(dev);
852 ret = cdev->ops->get_cur_state(cdev, &state);
855 return sprintf(buf, "%ld\n", state);
859 thermal_cooling_device_cur_state_store(struct device *dev,
860 struct device_attribute *attr,
861 const char *buf, size_t count)
863 struct thermal_cooling_device *cdev = to_cooling_device(dev);
867 if (!sscanf(buf, "%ld\n", &state))
873 result = cdev->ops->set_cur_state(cdev, state);
879 static struct device_attribute dev_attr_cdev_type =
880 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
881 static DEVICE_ATTR(max_state, 0444,
882 thermal_cooling_device_max_state_show, NULL);
883 static DEVICE_ATTR(cur_state, 0644,
884 thermal_cooling_device_cur_state_show,
885 thermal_cooling_device_cur_state_store);
888 thermal_cooling_device_trip_point_show(struct device *dev,
889 struct device_attribute *attr, char *buf)
891 struct thermal_instance *instance;
894 container_of(attr, struct thermal_instance, attr);
896 if (instance->trip == THERMAL_TRIPS_NONE)
897 return sprintf(buf, "-1\n");
899 return sprintf(buf, "%d\n", instance->trip);
902 static struct attribute *cooling_device_attrs[] = {
903 &dev_attr_cdev_type.attr,
904 &dev_attr_max_state.attr,
905 &dev_attr_cur_state.attr,
909 static const struct attribute_group cooling_device_attr_group = {
910 .attrs = cooling_device_attrs,
913 static const struct attribute_group *cooling_device_attr_groups[] = {
914 &cooling_device_attr_group,
918 /* Device management */
921 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
922 * @tz: pointer to struct thermal_zone_device
923 * @trip: indicates which trip point the cooling devices is
924 * associated with in this thermal zone.
925 * @cdev: pointer to struct thermal_cooling_device
926 * @upper: the Maximum cooling state for this trip point.
927 * THERMAL_NO_LIMIT means no upper limit,
928 * and the cooling device can be in max_state.
929 * @lower: the Minimum cooling state can be used for this trip point.
930 * THERMAL_NO_LIMIT means no lower limit,
931 * and the cooling device can be in cooling state 0.
933 * This interface function bind a thermal cooling device to the certain trip
934 * point of a thermal zone device.
935 * This function is usually called in the thermal zone device .bind callback.
937 * Return: 0 on success, the proper error value otherwise.
939 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
941 struct thermal_cooling_device *cdev,
942 unsigned long upper, unsigned long lower)
944 struct thermal_instance *dev;
945 struct thermal_instance *pos;
946 struct thermal_zone_device *pos1;
947 struct thermal_cooling_device *pos2;
948 unsigned long max_state;
951 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
954 list_for_each_entry(pos1, &thermal_tz_list, node) {
958 list_for_each_entry(pos2, &thermal_cdev_list, node) {
963 if (tz != pos1 || cdev != pos2)
966 ret = cdev->ops->get_max_state(cdev, &max_state);
970 /* lower default 0, upper default max_state */
971 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
972 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
974 if (lower > upper || upper > max_state)
978 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
986 dev->target = THERMAL_NO_TARGET;
988 result = get_idr(&tz->idr, &tz->lock, &dev->id);
992 sprintf(dev->name, "cdev%d", dev->id);
994 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
998 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
999 sysfs_attr_init(&dev->attr.attr);
1000 dev->attr.attr.name = dev->attr_name;
1001 dev->attr.attr.mode = 0444;
1002 dev->attr.show = thermal_cooling_device_trip_point_show;
1003 result = device_create_file(&tz->device, &dev->attr);
1005 goto remove_symbol_link;
1007 mutex_lock(&tz->lock);
1008 mutex_lock(&cdev->lock);
1009 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1010 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1015 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1016 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1018 mutex_unlock(&cdev->lock);
1019 mutex_unlock(&tz->lock);
1024 device_remove_file(&tz->device, &dev->attr);
1026 sysfs_remove_link(&tz->device.kobj, dev->name);
1028 release_idr(&tz->idr, &tz->lock, dev->id);
1033 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
1036 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1038 * @tz: pointer to a struct thermal_zone_device.
1039 * @trip: indicates which trip point the cooling devices is
1040 * associated with in this thermal zone.
1041 * @cdev: pointer to a struct thermal_cooling_device.
1043 * This interface function unbind a thermal cooling device from the certain
1044 * trip point of a thermal zone device.
1045 * This function is usually called in the thermal zone device .unbind callback.
1047 * Return: 0 on success, the proper error value otherwise.
1049 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1051 struct thermal_cooling_device *cdev)
1053 struct thermal_instance *pos, *next;
1055 mutex_lock(&tz->lock);
1056 mutex_lock(&cdev->lock);
1057 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1058 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1059 list_del(&pos->tz_node);
1060 list_del(&pos->cdev_node);
1061 mutex_unlock(&cdev->lock);
1062 mutex_unlock(&tz->lock);
1066 mutex_unlock(&cdev->lock);
1067 mutex_unlock(&tz->lock);
1072 device_remove_file(&tz->device, &pos->attr);
1073 sysfs_remove_link(&tz->device.kobj, pos->name);
1074 release_idr(&tz->idr, &tz->lock, pos->id);
1078 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1080 static void thermal_release(struct device *dev)
1082 struct thermal_zone_device *tz;
1083 struct thermal_cooling_device *cdev;
1085 if (!strncmp(dev_name(dev), "thermal_zone",
1086 sizeof("thermal_zone") - 1)) {
1087 tz = to_thermal_zone(dev);
1089 } else if(!strncmp(dev_name(dev), "cooling_device",
1090 sizeof("cooling_device") - 1)){
1091 cdev = to_cooling_device(dev);
1096 static struct class thermal_class = {
1098 .dev_release = thermal_release,
1102 * __thermal_cooling_device_register() - register a new thermal cooling device
1103 * @np: a pointer to a device tree node.
1104 * @type: the thermal cooling device type.
1105 * @devdata: device private data.
1106 * @ops: standard thermal cooling devices callbacks.
1108 * This interface function adds a new thermal cooling device (fan/processor/...)
1109 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1110 * to all the thermal zone devices registered at the same time.
1111 * It also gives the opportunity to link the cooling device to a device tree
1112 * node, so that it can be bound to a thermal zone created out of device tree.
1114 * Return: a pointer to the created struct thermal_cooling_device or an
1115 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1117 static struct thermal_cooling_device *
1118 __thermal_cooling_device_register(struct device_node *np,
1119 char *type, void *devdata,
1120 const struct thermal_cooling_device_ops *ops)
1122 struct thermal_cooling_device *cdev;
1125 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1126 return ERR_PTR(-EINVAL);
1128 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1129 !ops->set_cur_state)
1130 return ERR_PTR(-EINVAL);
1132 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1134 return ERR_PTR(-ENOMEM);
1136 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1139 return ERR_PTR(result);
1142 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1143 mutex_init(&cdev->lock);
1144 INIT_LIST_HEAD(&cdev->thermal_instances);
1147 cdev->updated = false;
1148 cdev->device.class = &thermal_class;
1149 cdev->device.groups = cooling_device_attr_groups;
1150 cdev->devdata = devdata;
1151 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1152 result = device_register(&cdev->device);
1154 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1156 return ERR_PTR(result);
1159 /* Add 'this' new cdev to the global cdev list */
1160 mutex_lock(&thermal_list_lock);
1161 list_add(&cdev->node, &thermal_cdev_list);
1162 mutex_unlock(&thermal_list_lock);
1164 /* Update binding information for 'this' new cdev */
1171 * thermal_cooling_device_register() - register a new thermal cooling device
1172 * @type: the thermal cooling device type.
1173 * @devdata: device private data.
1174 * @ops: standard thermal cooling devices callbacks.
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_cooling_device_register(char *type, void *devdata,
1185 const struct thermal_cooling_device_ops *ops)
1187 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1189 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1192 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1193 * @np: a pointer to a device tree node.
1194 * @type: the thermal cooling device type.
1195 * @devdata: device private data.
1196 * @ops: standard thermal cooling devices callbacks.
1198 * This function will register a cooling device with device tree node reference.
1199 * This interface function adds a new thermal cooling device (fan/processor/...)
1200 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1201 * to all the thermal zone devices registered at the same time.
1203 * Return: a pointer to the created struct thermal_cooling_device or an
1204 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1206 struct thermal_cooling_device *
1207 thermal_of_cooling_device_register(struct device_node *np,
1208 char *type, void *devdata,
1209 const struct thermal_cooling_device_ops *ops)
1211 return __thermal_cooling_device_register(np, type, devdata, ops);
1213 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1216 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1217 * @cdev: the thermal cooling device to remove.
1219 * thermal_cooling_device_unregister() must be called when the device is no
1222 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1225 const struct thermal_zone_params *tzp;
1226 struct thermal_zone_device *tz;
1227 struct thermal_cooling_device *pos = NULL;
1232 mutex_lock(&thermal_list_lock);
1233 list_for_each_entry(pos, &thermal_cdev_list, node)
1237 /* thermal cooling device not found */
1238 mutex_unlock(&thermal_list_lock);
1241 list_del(&cdev->node);
1243 /* Unbind all thermal zones associated with 'this' cdev */
1244 list_for_each_entry(tz, &thermal_tz_list, node) {
1245 if (tz->ops->unbind) {
1246 tz->ops->unbind(tz, cdev);
1250 if (!tz->tzp || !tz->tzp->tbp)
1254 for (i = 0; i < tzp->num_tbps; i++) {
1255 if (tzp->tbp[i].cdev == cdev) {
1256 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1257 tzp->tbp[i].cdev = NULL;
1262 mutex_unlock(&thermal_list_lock);
1265 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1266 device_remove_file(&cdev->device, &dev_attr_max_state);
1267 device_remove_file(&cdev->device, &dev_attr_cur_state);
1269 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1270 device_unregister(&cdev->device);
1273 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1275 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1277 struct thermal_instance *instance;
1278 unsigned long target = 0;
1280 /* cooling device is updated*/
1284 mutex_lock(&cdev->lock);
1285 /* Make sure cdev enters the deepest cooling state */
1286 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1287 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1288 instance->tz->id, instance->target);
1289 if (instance->target == THERMAL_NO_TARGET)
1291 if (instance->target > target)
1292 target = instance->target;
1294 mutex_unlock(&cdev->lock);
1295 cdev->ops->set_cur_state(cdev, target);
1296 cdev->updated = true;
1297 trace_cdev_update(cdev, target);
1298 dev_dbg(&cdev->device, "set to state %lu\n", target);
1300 EXPORT_SYMBOL(thermal_cdev_update);
1303 * thermal_notify_framework - Sensor drivers use this API to notify framework
1304 * @tz: thermal zone device
1305 * @trip: indicates which trip point has been crossed
1307 * This function handles the trip events from sensor drivers. It starts
1308 * throttling the cooling devices according to the policy configured.
1309 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1310 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1311 * The throttling policy is based on the configured platform data; if no
1312 * platform data is provided, this uses the step_wise throttling policy.
1314 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1316 handle_thermal_trip(tz, trip);
1318 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1321 * create_trip_attrs() - create attributes for trip points
1322 * @tz: the thermal zone device
1323 * @mask: Writeable trip point bitmap.
1325 * helper function to instantiate sysfs entries for every trip
1326 * point and its properties of a struct thermal_zone_device.
1328 * Return: 0 on success, the proper error value otherwise.
1330 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1333 int size = sizeof(struct thermal_attr) * tz->trips;
1335 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1336 if (!tz->trip_type_attrs)
1339 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1340 if (!tz->trip_temp_attrs) {
1341 kfree(tz->trip_type_attrs);
1345 if (tz->ops->get_trip_hyst) {
1346 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1347 if (!tz->trip_hyst_attrs) {
1348 kfree(tz->trip_type_attrs);
1349 kfree(tz->trip_temp_attrs);
1355 for (indx = 0; indx < tz->trips; indx++) {
1356 /* create trip type attribute */
1357 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1358 "trip_point_%d_type", indx);
1360 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1361 tz->trip_type_attrs[indx].attr.attr.name =
1362 tz->trip_type_attrs[indx].name;
1363 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1364 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1366 device_create_file(&tz->device,
1367 &tz->trip_type_attrs[indx].attr);
1369 /* create trip temp attribute */
1370 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1371 "trip_point_%d_temp", indx);
1373 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1374 tz->trip_temp_attrs[indx].attr.attr.name =
1375 tz->trip_temp_attrs[indx].name;
1376 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1377 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1378 if (mask & (1 << indx)) {
1379 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1380 tz->trip_temp_attrs[indx].attr.store =
1381 trip_point_temp_store;
1384 device_create_file(&tz->device,
1385 &tz->trip_temp_attrs[indx].attr);
1387 /* create Optional trip hyst attribute */
1388 if (!tz->ops->get_trip_hyst)
1390 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1391 "trip_point_%d_hyst", indx);
1393 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1394 tz->trip_hyst_attrs[indx].attr.attr.name =
1395 tz->trip_hyst_attrs[indx].name;
1396 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1397 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1398 if (tz->ops->set_trip_hyst) {
1399 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1400 tz->trip_hyst_attrs[indx].attr.store =
1401 trip_point_hyst_store;
1404 device_create_file(&tz->device,
1405 &tz->trip_hyst_attrs[indx].attr);
1410 static void remove_trip_attrs(struct thermal_zone_device *tz)
1414 for (indx = 0; indx < tz->trips; indx++) {
1415 device_remove_file(&tz->device,
1416 &tz->trip_type_attrs[indx].attr);
1417 device_remove_file(&tz->device,
1418 &tz->trip_temp_attrs[indx].attr);
1419 if (tz->ops->get_trip_hyst)
1420 device_remove_file(&tz->device,
1421 &tz->trip_hyst_attrs[indx].attr);
1423 kfree(tz->trip_type_attrs);
1424 kfree(tz->trip_temp_attrs);
1425 kfree(tz->trip_hyst_attrs);
1429 * thermal_zone_device_register() - register a new thermal zone device
1430 * @type: the thermal zone device type
1431 * @trips: the number of trip points the thermal zone support
1432 * @mask: a bit string indicating the writeablility of trip points
1433 * @devdata: private device data
1434 * @ops: standard thermal zone device callbacks
1435 * @tzp: thermal zone platform parameters
1436 * @passive_delay: number of milliseconds to wait between polls when
1437 * performing passive cooling
1438 * @polling_delay: number of milliseconds to wait between polls when checking
1439 * whether trip points have been crossed (0 for interrupt
1442 * This interface function adds a new thermal zone device (sensor) to
1443 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1444 * thermal cooling devices registered at the same time.
1445 * thermal_zone_device_unregister() must be called when the device is no
1446 * longer needed. The passive cooling depends on the .get_trend() return value.
1448 * Return: a pointer to the created struct thermal_zone_device or an
1449 * in case of error, an ERR_PTR. Caller must check return value with
1450 * IS_ERR*() helpers.
1452 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1453 int trips, int mask, void *devdata,
1454 struct thermal_zone_device_ops *ops,
1455 const struct thermal_zone_params *tzp,
1456 int passive_delay, int polling_delay)
1458 struct thermal_zone_device *tz;
1459 enum thermal_trip_type trip_type;
1464 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1465 return ERR_PTR(-EINVAL);
1467 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1468 return ERR_PTR(-EINVAL);
1471 return ERR_PTR(-EINVAL);
1473 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1474 return ERR_PTR(-EINVAL);
1476 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1478 return ERR_PTR(-ENOMEM);
1480 INIT_LIST_HEAD(&tz->thermal_instances);
1482 mutex_init(&tz->lock);
1483 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1486 return ERR_PTR(result);
1489 strlcpy(tz->type, type ? : "", sizeof(tz->type));
1492 tz->device.class = &thermal_class;
1493 tz->devdata = devdata;
1495 tz->passive_delay = passive_delay;
1496 tz->polling_delay = polling_delay;
1498 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1499 result = device_register(&tz->device);
1501 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1503 return ERR_PTR(result);
1508 result = device_create_file(&tz->device, &dev_attr_type);
1513 result = device_create_file(&tz->device, &dev_attr_temp);
1517 if (ops->get_mode) {
1518 result = device_create_file(&tz->device, &dev_attr_mode);
1523 result = create_trip_attrs(tz, mask);
1527 for (count = 0; count < trips; count++) {
1528 tz->ops->get_trip_type(tz, count, &trip_type);
1529 if (trip_type == THERMAL_TRIP_PASSIVE)
1534 result = device_create_file(&tz->device, &dev_attr_passive);
1539 #ifdef CONFIG_THERMAL_EMULATION
1540 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1544 /* Create policy attribute */
1545 result = device_create_file(&tz->device, &dev_attr_policy);
1549 /* Update 'this' zone's governor information */
1550 mutex_lock(&thermal_governor_lock);
1553 tz->governor = __find_governor(tz->tzp->governor_name);
1555 tz->governor = def_governor;
1557 mutex_unlock(&thermal_governor_lock);
1559 if (!tz->tzp || !tz->tzp->no_hwmon) {
1560 result = thermal_add_hwmon_sysfs(tz);
1565 mutex_lock(&thermal_list_lock);
1566 list_add_tail(&tz->node, &thermal_tz_list);
1567 mutex_unlock(&thermal_list_lock);
1569 /* Bind cooling devices for this zone */
1572 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1574 if (!tz->ops->get_temp)
1575 thermal_zone_device_set_polling(tz, 0);
1577 thermal_zone_device_update(tz);
1582 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1583 device_unregister(&tz->device);
1584 return ERR_PTR(result);
1586 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1589 * thermal_device_unregister - removes the registered thermal zone device
1590 * @tz: the thermal zone device to remove
1592 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1595 const struct thermal_zone_params *tzp;
1596 struct thermal_cooling_device *cdev;
1597 struct thermal_zone_device *pos = NULL;
1604 mutex_lock(&thermal_list_lock);
1605 list_for_each_entry(pos, &thermal_tz_list, node)
1609 /* thermal zone device not found */
1610 mutex_unlock(&thermal_list_lock);
1613 list_del(&tz->node);
1615 /* Unbind all cdevs associated with 'this' thermal zone */
1616 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1617 if (tz->ops->unbind) {
1618 tz->ops->unbind(tz, cdev);
1622 if (!tzp || !tzp->tbp)
1625 for (i = 0; i < tzp->num_tbps; i++) {
1626 if (tzp->tbp[i].cdev == cdev) {
1627 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1628 tzp->tbp[i].cdev = NULL;
1633 mutex_unlock(&thermal_list_lock);
1635 thermal_zone_device_set_polling(tz, 0);
1638 device_remove_file(&tz->device, &dev_attr_type);
1639 device_remove_file(&tz->device, &dev_attr_temp);
1640 if (tz->ops->get_mode)
1641 device_remove_file(&tz->device, &dev_attr_mode);
1642 device_remove_file(&tz->device, &dev_attr_policy);
1643 remove_trip_attrs(tz);
1644 tz->governor = NULL;
1646 thermal_remove_hwmon_sysfs(tz);
1647 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1648 idr_destroy(&tz->idr);
1649 mutex_destroy(&tz->lock);
1650 device_unregister(&tz->device);
1653 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1656 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1657 * @name: thermal zone name to fetch the temperature
1659 * When only one zone is found with the passed name, returns a reference to it.
1661 * Return: On success returns a reference to an unique thermal zone with
1662 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1663 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1665 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1667 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1668 unsigned int found = 0;
1673 mutex_lock(&thermal_list_lock);
1674 list_for_each_entry(pos, &thermal_tz_list, node)
1675 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1679 mutex_unlock(&thermal_list_lock);
1681 /* nothing has been found, thus an error code for it */
1683 ref = ERR_PTR(-ENODEV);
1685 /* Success only when an unique zone is found */
1686 ref = ERR_PTR(-EEXIST);
1691 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1694 static const struct genl_multicast_group thermal_event_mcgrps[] = {
1695 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1698 static struct genl_family thermal_event_genl_family = {
1699 .id = GENL_ID_GENERATE,
1700 .name = THERMAL_GENL_FAMILY_NAME,
1701 .version = THERMAL_GENL_VERSION,
1702 .maxattr = THERMAL_GENL_ATTR_MAX,
1703 .mcgrps = thermal_event_mcgrps,
1704 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
1707 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1710 struct sk_buff *skb;
1711 struct nlattr *attr;
1712 struct thermal_genl_event *thermal_event;
1716 static unsigned int thermal_event_seqnum;
1721 /* allocate memory */
1722 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1725 skb = genlmsg_new(size, GFP_ATOMIC);
1729 /* add the genetlink message header */
1730 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1731 &thermal_event_genl_family, 0,
1732 THERMAL_GENL_CMD_EVENT);
1739 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1740 sizeof(struct thermal_genl_event));
1747 thermal_event = nla_data(attr);
1748 if (!thermal_event) {
1753 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1755 thermal_event->orig = tz->id;
1756 thermal_event->event = event;
1758 /* send multicast genetlink message */
1759 genlmsg_end(skb, msg_header);
1761 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
1764 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1768 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1770 static int genetlink_init(void)
1772 return genl_register_family(&thermal_event_genl_family);
1775 static void genetlink_exit(void)
1777 genl_unregister_family(&thermal_event_genl_family);
1779 #else /* !CONFIG_NET */
1780 static inline int genetlink_init(void) { return 0; }
1781 static inline void genetlink_exit(void) {}
1782 #endif /* !CONFIG_NET */
1784 static int __init thermal_register_governors(void)
1788 result = thermal_gov_step_wise_register();
1792 result = thermal_gov_fair_share_register();
1796 result = thermal_gov_bang_bang_register();
1800 return thermal_gov_user_space_register();
1803 static void thermal_unregister_governors(void)
1805 thermal_gov_step_wise_unregister();
1806 thermal_gov_fair_share_unregister();
1807 thermal_gov_bang_bang_unregister();
1808 thermal_gov_user_space_unregister();
1811 static int __init thermal_init(void)
1815 result = thermal_register_governors();
1819 result = class_register(&thermal_class);
1821 goto unregister_governors;
1823 result = genetlink_init();
1825 goto unregister_class;
1827 result = of_parse_thermal_zones();
1836 class_unregister(&thermal_class);
1837 unregister_governors:
1838 thermal_unregister_governors();
1840 idr_destroy(&thermal_tz_idr);
1841 idr_destroy(&thermal_cdev_idr);
1842 mutex_destroy(&thermal_idr_lock);
1843 mutex_destroy(&thermal_list_lock);
1844 mutex_destroy(&thermal_governor_lock);
1848 static void __exit thermal_exit(void)
1850 of_thermal_destroy_zones();
1852 class_unregister(&thermal_class);
1853 thermal_unregister_governors();
1854 idr_destroy(&thermal_tz_idr);
1855 idr_destroy(&thermal_cdev_idr);
1856 mutex_destroy(&thermal_idr_lock);
1857 mutex_destroy(&thermal_list_lock);
1858 mutex_destroy(&thermal_governor_lock);
1861 fs_initcall(thermal_init);
1862 module_exit(thermal_exit);