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 #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 (!strnicmp(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 (!strnicmp(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 (!strnicmp(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 (tz->temperature < trip_temp)
375 tz->ops->notify(tz, trip, trip_type);
377 if (trip_type == THERMAL_TRIP_CRITICAL) {
378 dev_emerg(&tz->device,
379 "critical temperature reached(%d C),shutting down\n",
380 tz->temperature / 1000);
381 orderly_poweroff(true);
385 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
387 enum thermal_trip_type type;
389 tz->ops->get_trip_type(tz, trip, &type);
391 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
392 handle_critical_trips(tz, trip, type);
394 handle_non_critical_trips(tz, trip, type);
396 * Alright, we handled this trip successfully.
397 * So, start monitoring again.
399 monitor_thermal_zone(tz);
403 * thermal_zone_get_temp() - returns its the temperature of thermal zone
404 * @tz: a valid pointer to a struct thermal_zone_device
405 * @temp: a valid pointer to where to store the resulting temperature.
407 * When a valid thermal zone reference is passed, it will fetch its
408 * temperature and fill @temp.
410 * Return: On success returns 0, an error code otherwise
412 int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
415 #ifdef CONFIG_THERMAL_EMULATION
417 unsigned long crit_temp = -1UL;
418 enum thermal_trip_type type;
421 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
424 mutex_lock(&tz->lock);
426 ret = tz->ops->get_temp(tz, temp);
427 #ifdef CONFIG_THERMAL_EMULATION
428 if (!tz->emul_temperature)
431 for (count = 0; count < tz->trips; count++) {
432 ret = tz->ops->get_trip_type(tz, count, &type);
433 if (!ret && type == THERMAL_TRIP_CRITICAL) {
434 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
442 if (*temp < crit_temp)
443 *temp = tz->emul_temperature;
446 mutex_unlock(&tz->lock);
450 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
452 static void update_temperature(struct thermal_zone_device *tz)
457 ret = thermal_zone_get_temp(tz, &temp);
459 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
464 mutex_lock(&tz->lock);
465 tz->last_temperature = tz->temperature;
466 tz->temperature = temp;
467 mutex_unlock(&tz->lock);
469 trace_thermal_temperature(tz);
470 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
471 tz->last_temperature, tz->temperature);
474 void thermal_zone_device_update(struct thermal_zone_device *tz)
478 if (!tz->ops->get_temp)
481 update_temperature(tz);
483 for (count = 0; count < tz->trips; count++)
484 handle_thermal_trip(tz, count);
486 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
488 static void thermal_zone_device_check(struct work_struct *work)
490 struct thermal_zone_device *tz = container_of(work, struct
493 thermal_zone_device_update(tz);
496 /* sys I/F for thermal zone */
498 #define to_thermal_zone(_dev) \
499 container_of(_dev, struct thermal_zone_device, device)
502 type_show(struct device *dev, struct device_attribute *attr, char *buf)
504 struct thermal_zone_device *tz = to_thermal_zone(dev);
506 return sprintf(buf, "%s\n", tz->type);
510 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
512 struct thermal_zone_device *tz = to_thermal_zone(dev);
516 ret = thermal_zone_get_temp(tz, &temperature);
521 return sprintf(buf, "%ld\n", temperature);
525 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
527 struct thermal_zone_device *tz = to_thermal_zone(dev);
528 enum thermal_device_mode mode;
531 if (!tz->ops->get_mode)
534 result = tz->ops->get_mode(tz, &mode);
538 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
543 mode_store(struct device *dev, struct device_attribute *attr,
544 const char *buf, size_t count)
546 struct thermal_zone_device *tz = to_thermal_zone(dev);
549 if (!tz->ops->set_mode)
552 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
553 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
554 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
555 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
566 trip_point_type_show(struct device *dev, struct device_attribute *attr,
569 struct thermal_zone_device *tz = to_thermal_zone(dev);
570 enum thermal_trip_type type;
573 if (!tz->ops->get_trip_type)
576 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
579 result = tz->ops->get_trip_type(tz, trip, &type);
584 case THERMAL_TRIP_CRITICAL:
585 return sprintf(buf, "critical\n");
586 case THERMAL_TRIP_HOT:
587 return sprintf(buf, "hot\n");
588 case THERMAL_TRIP_PASSIVE:
589 return sprintf(buf, "passive\n");
590 case THERMAL_TRIP_ACTIVE:
591 return sprintf(buf, "active\n");
593 return sprintf(buf, "unknown\n");
598 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
599 const char *buf, size_t count)
601 struct thermal_zone_device *tz = to_thermal_zone(dev);
603 unsigned long temperature;
605 if (!tz->ops->set_trip_temp)
608 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
611 if (kstrtoul(buf, 10, &temperature))
614 ret = tz->ops->set_trip_temp(tz, trip, temperature);
616 return ret ? ret : count;
620 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
623 struct thermal_zone_device *tz = to_thermal_zone(dev);
627 if (!tz->ops->get_trip_temp)
630 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
633 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
638 return sprintf(buf, "%ld\n", temperature);
642 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
643 const char *buf, size_t count)
645 struct thermal_zone_device *tz = to_thermal_zone(dev);
647 unsigned long temperature;
649 if (!tz->ops->set_trip_hyst)
652 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
655 if (kstrtoul(buf, 10, &temperature))
659 * We are not doing any check on the 'temperature' value
660 * here. The driver implementing 'set_trip_hyst' has to
663 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
665 return ret ? ret : count;
669 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
672 struct thermal_zone_device *tz = to_thermal_zone(dev);
674 unsigned long temperature;
676 if (!tz->ops->get_trip_hyst)
679 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
682 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
684 return ret ? ret : sprintf(buf, "%ld\n", temperature);
688 passive_store(struct device *dev, struct device_attribute *attr,
689 const char *buf, size_t count)
691 struct thermal_zone_device *tz = to_thermal_zone(dev);
692 struct thermal_cooling_device *cdev = NULL;
695 if (!sscanf(buf, "%d\n", &state))
698 /* sanity check: values below 1000 millicelcius don't make sense
699 * and can cause the system to go into a thermal heart attack
701 if (state && state < 1000)
704 if (state && !tz->forced_passive) {
705 mutex_lock(&thermal_list_lock);
706 list_for_each_entry(cdev, &thermal_cdev_list, node) {
707 if (!strncmp("Processor", cdev->type,
708 sizeof("Processor")))
709 thermal_zone_bind_cooling_device(tz,
710 THERMAL_TRIPS_NONE, cdev,
714 mutex_unlock(&thermal_list_lock);
715 if (!tz->passive_delay)
716 tz->passive_delay = 1000;
717 } else if (!state && tz->forced_passive) {
718 mutex_lock(&thermal_list_lock);
719 list_for_each_entry(cdev, &thermal_cdev_list, node) {
720 if (!strncmp("Processor", cdev->type,
721 sizeof("Processor")))
722 thermal_zone_unbind_cooling_device(tz,
726 mutex_unlock(&thermal_list_lock);
727 tz->passive_delay = 0;
730 tz->forced_passive = state;
732 thermal_zone_device_update(tz);
738 passive_show(struct device *dev, struct device_attribute *attr,
741 struct thermal_zone_device *tz = to_thermal_zone(dev);
743 return sprintf(buf, "%d\n", tz->forced_passive);
747 policy_store(struct device *dev, struct device_attribute *attr,
748 const char *buf, size_t count)
751 struct thermal_zone_device *tz = to_thermal_zone(dev);
752 struct thermal_governor *gov;
753 char name[THERMAL_NAME_LENGTH];
755 snprintf(name, sizeof(name), "%s", buf);
757 mutex_lock(&thermal_governor_lock);
759 gov = __find_governor(strim(name));
767 mutex_unlock(&thermal_governor_lock);
772 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
774 struct thermal_zone_device *tz = to_thermal_zone(dev);
776 return sprintf(buf, "%s\n", tz->governor->name);
779 #ifdef CONFIG_THERMAL_EMULATION
781 emul_temp_store(struct device *dev, struct device_attribute *attr,
782 const char *buf, size_t count)
784 struct thermal_zone_device *tz = to_thermal_zone(dev);
786 unsigned long temperature;
788 if (kstrtoul(buf, 10, &temperature))
791 if (!tz->ops->set_emul_temp) {
792 mutex_lock(&tz->lock);
793 tz->emul_temperature = temperature;
794 mutex_unlock(&tz->lock);
796 ret = tz->ops->set_emul_temp(tz, temperature);
800 thermal_zone_device_update(tz);
802 return ret ? ret : count;
804 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
805 #endif/*CONFIG_THERMAL_EMULATION*/
807 static DEVICE_ATTR(type, 0444, type_show, NULL);
808 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
809 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
810 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
811 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
813 /* sys I/F for cooling device */
814 #define to_cooling_device(_dev) \
815 container_of(_dev, struct thermal_cooling_device, device)
818 thermal_cooling_device_type_show(struct device *dev,
819 struct device_attribute *attr, char *buf)
821 struct thermal_cooling_device *cdev = to_cooling_device(dev);
823 return sprintf(buf, "%s\n", cdev->type);
827 thermal_cooling_device_max_state_show(struct device *dev,
828 struct device_attribute *attr, char *buf)
830 struct thermal_cooling_device *cdev = to_cooling_device(dev);
834 ret = cdev->ops->get_max_state(cdev, &state);
837 return sprintf(buf, "%ld\n", state);
841 thermal_cooling_device_cur_state_show(struct device *dev,
842 struct device_attribute *attr, char *buf)
844 struct thermal_cooling_device *cdev = to_cooling_device(dev);
848 ret = cdev->ops->get_cur_state(cdev, &state);
851 return sprintf(buf, "%ld\n", state);
855 thermal_cooling_device_cur_state_store(struct device *dev,
856 struct device_attribute *attr,
857 const char *buf, size_t count)
859 struct thermal_cooling_device *cdev = to_cooling_device(dev);
863 if (!sscanf(buf, "%ld\n", &state))
869 result = cdev->ops->set_cur_state(cdev, state);
875 static struct device_attribute dev_attr_cdev_type =
876 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
877 static DEVICE_ATTR(max_state, 0444,
878 thermal_cooling_device_max_state_show, NULL);
879 static DEVICE_ATTR(cur_state, 0644,
880 thermal_cooling_device_cur_state_show,
881 thermal_cooling_device_cur_state_store);
884 thermal_cooling_device_trip_point_show(struct device *dev,
885 struct device_attribute *attr, char *buf)
887 struct thermal_instance *instance;
890 container_of(attr, struct thermal_instance, attr);
892 if (instance->trip == THERMAL_TRIPS_NONE)
893 return sprintf(buf, "-1\n");
895 return sprintf(buf, "%d\n", instance->trip);
898 /* Device management */
901 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
902 * @tz: pointer to struct thermal_zone_device
903 * @trip: indicates which trip point the cooling devices is
904 * associated with in this thermal zone.
905 * @cdev: pointer to struct thermal_cooling_device
906 * @upper: the Maximum cooling state for this trip point.
907 * THERMAL_NO_LIMIT means no upper limit,
908 * and the cooling device can be in max_state.
909 * @lower: the Minimum cooling state can be used for this trip point.
910 * THERMAL_NO_LIMIT means no lower limit,
911 * and the cooling device can be in cooling state 0.
913 * This interface function bind a thermal cooling device to the certain trip
914 * point of a thermal zone device.
915 * This function is usually called in the thermal zone device .bind callback.
917 * Return: 0 on success, the proper error value otherwise.
919 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
921 struct thermal_cooling_device *cdev,
922 unsigned long upper, unsigned long lower)
924 struct thermal_instance *dev;
925 struct thermal_instance *pos;
926 struct thermal_zone_device *pos1;
927 struct thermal_cooling_device *pos2;
928 unsigned long max_state;
931 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
934 list_for_each_entry(pos1, &thermal_tz_list, node) {
938 list_for_each_entry(pos2, &thermal_cdev_list, node) {
943 if (tz != pos1 || cdev != pos2)
946 cdev->ops->get_max_state(cdev, &max_state);
948 /* lower default 0, upper default max_state */
949 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
950 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
952 if (lower > upper || upper > max_state)
956 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
964 dev->target = THERMAL_NO_TARGET;
966 result = get_idr(&tz->idr, &tz->lock, &dev->id);
970 sprintf(dev->name, "cdev%d", dev->id);
972 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
976 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
977 sysfs_attr_init(&dev->attr.attr);
978 dev->attr.attr.name = dev->attr_name;
979 dev->attr.attr.mode = 0444;
980 dev->attr.show = thermal_cooling_device_trip_point_show;
981 result = device_create_file(&tz->device, &dev->attr);
983 goto remove_symbol_link;
985 mutex_lock(&tz->lock);
986 mutex_lock(&cdev->lock);
987 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
988 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
993 list_add_tail(&dev->tz_node, &tz->thermal_instances);
994 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
996 mutex_unlock(&cdev->lock);
997 mutex_unlock(&tz->lock);
1002 device_remove_file(&tz->device, &dev->attr);
1004 sysfs_remove_link(&tz->device.kobj, dev->name);
1006 release_idr(&tz->idr, &tz->lock, dev->id);
1011 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
1014 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1016 * @tz: pointer to a struct thermal_zone_device.
1017 * @trip: indicates which trip point the cooling devices is
1018 * associated with in this thermal zone.
1019 * @cdev: pointer to a struct thermal_cooling_device.
1021 * This interface function unbind a thermal cooling device from the certain
1022 * trip point of a thermal zone device.
1023 * This function is usually called in the thermal zone device .unbind callback.
1025 * Return: 0 on success, the proper error value otherwise.
1027 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1029 struct thermal_cooling_device *cdev)
1031 struct thermal_instance *pos, *next;
1033 mutex_lock(&tz->lock);
1034 mutex_lock(&cdev->lock);
1035 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1036 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1037 list_del(&pos->tz_node);
1038 list_del(&pos->cdev_node);
1039 mutex_unlock(&cdev->lock);
1040 mutex_unlock(&tz->lock);
1044 mutex_unlock(&cdev->lock);
1045 mutex_unlock(&tz->lock);
1050 device_remove_file(&tz->device, &pos->attr);
1051 sysfs_remove_link(&tz->device.kobj, pos->name);
1052 release_idr(&tz->idr, &tz->lock, pos->id);
1056 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1058 static void thermal_release(struct device *dev)
1060 struct thermal_zone_device *tz;
1061 struct thermal_cooling_device *cdev;
1063 if (!strncmp(dev_name(dev), "thermal_zone",
1064 sizeof("thermal_zone") - 1)) {
1065 tz = to_thermal_zone(dev);
1067 } else if(!strncmp(dev_name(dev), "cooling_device",
1068 sizeof("cooling_device") - 1)){
1069 cdev = to_cooling_device(dev);
1074 static struct class thermal_class = {
1076 .dev_release = thermal_release,
1080 * __thermal_cooling_device_register() - register a new thermal cooling device
1081 * @np: a pointer to a device tree node.
1082 * @type: the thermal cooling device type.
1083 * @devdata: device private data.
1084 * @ops: standard thermal cooling devices callbacks.
1086 * This interface function adds a new thermal cooling device (fan/processor/...)
1087 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1088 * to all the thermal zone devices registered at the same time.
1089 * It also gives the opportunity to link the cooling device to a device tree
1090 * node, so that it can be bound to a thermal zone created out of device tree.
1092 * Return: a pointer to the created struct thermal_cooling_device or an
1093 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1095 static struct thermal_cooling_device *
1096 __thermal_cooling_device_register(struct device_node *np,
1097 char *type, void *devdata,
1098 const struct thermal_cooling_device_ops *ops)
1100 struct thermal_cooling_device *cdev;
1103 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1104 return ERR_PTR(-EINVAL);
1106 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1107 !ops->set_cur_state)
1108 return ERR_PTR(-EINVAL);
1110 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1112 return ERR_PTR(-ENOMEM);
1114 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1117 return ERR_PTR(result);
1120 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1121 mutex_init(&cdev->lock);
1122 INIT_LIST_HEAD(&cdev->thermal_instances);
1125 cdev->updated = false;
1126 cdev->device.class = &thermal_class;
1127 cdev->devdata = devdata;
1128 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1129 result = device_register(&cdev->device);
1131 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1133 return ERR_PTR(result);
1138 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1143 result = device_create_file(&cdev->device, &dev_attr_max_state);
1147 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1151 /* Add 'this' new cdev to the global cdev list */
1152 mutex_lock(&thermal_list_lock);
1153 list_add(&cdev->node, &thermal_cdev_list);
1154 mutex_unlock(&thermal_list_lock);
1156 /* Update binding information for 'this' new cdev */
1162 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1163 device_unregister(&cdev->device);
1164 return ERR_PTR(result);
1168 * thermal_cooling_device_register() - register a new thermal cooling device
1169 * @type: the thermal cooling device type.
1170 * @devdata: device private data.
1171 * @ops: standard thermal cooling devices callbacks.
1173 * This interface function adds a new thermal cooling device (fan/processor/...)
1174 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1175 * to all the thermal zone devices registered at the same time.
1177 * Return: a pointer to the created struct thermal_cooling_device or an
1178 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1180 struct thermal_cooling_device *
1181 thermal_cooling_device_register(char *type, void *devdata,
1182 const struct thermal_cooling_device_ops *ops)
1184 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1186 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1189 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1190 * @np: a pointer to a device tree node.
1191 * @type: the thermal cooling device type.
1192 * @devdata: device private data.
1193 * @ops: standard thermal cooling devices callbacks.
1195 * This function will register a cooling device with device tree node reference.
1196 * This interface function adds a new thermal cooling device (fan/processor/...)
1197 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1198 * to all the thermal zone devices registered at the same time.
1200 * Return: a pointer to the created struct thermal_cooling_device or an
1201 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1203 struct thermal_cooling_device *
1204 thermal_of_cooling_device_register(struct device_node *np,
1205 char *type, void *devdata,
1206 const struct thermal_cooling_device_ops *ops)
1208 return __thermal_cooling_device_register(np, type, devdata, ops);
1210 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1213 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1214 * @cdev: the thermal cooling device to remove.
1216 * thermal_cooling_device_unregister() must be called when the device is no
1219 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1222 const struct thermal_zone_params *tzp;
1223 struct thermal_zone_device *tz;
1224 struct thermal_cooling_device *pos = NULL;
1229 mutex_lock(&thermal_list_lock);
1230 list_for_each_entry(pos, &thermal_cdev_list, node)
1234 /* thermal cooling device not found */
1235 mutex_unlock(&thermal_list_lock);
1238 list_del(&cdev->node);
1240 /* Unbind all thermal zones associated with 'this' cdev */
1241 list_for_each_entry(tz, &thermal_tz_list, node) {
1242 if (tz->ops->unbind) {
1243 tz->ops->unbind(tz, cdev);
1247 if (!tz->tzp || !tz->tzp->tbp)
1251 for (i = 0; i < tzp->num_tbps; i++) {
1252 if (tzp->tbp[i].cdev == cdev) {
1253 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1254 tzp->tbp[i].cdev = NULL;
1259 mutex_unlock(&thermal_list_lock);
1262 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1263 device_remove_file(&cdev->device, &dev_attr_max_state);
1264 device_remove_file(&cdev->device, &dev_attr_cur_state);
1266 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1267 device_unregister(&cdev->device);
1270 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1272 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1274 struct thermal_instance *instance;
1275 unsigned long target = 0;
1277 /* cooling device is updated*/
1281 mutex_lock(&cdev->lock);
1282 /* Make sure cdev enters the deepest cooling state */
1283 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1284 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1285 instance->tz->id, instance->target);
1286 if (instance->target == THERMAL_NO_TARGET)
1288 if (instance->target > target)
1289 target = instance->target;
1291 mutex_unlock(&cdev->lock);
1292 cdev->ops->set_cur_state(cdev, target);
1293 cdev->updated = true;
1294 dev_dbg(&cdev->device, "set to state %lu\n", target);
1296 EXPORT_SYMBOL(thermal_cdev_update);
1299 * thermal_notify_framework - Sensor drivers use this API to notify framework
1300 * @tz: thermal zone device
1301 * @trip: indicates which trip point has been crossed
1303 * This function handles the trip events from sensor drivers. It starts
1304 * throttling the cooling devices according to the policy configured.
1305 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1306 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1307 * The throttling policy is based on the configured platform data; if no
1308 * platform data is provided, this uses the step_wise throttling policy.
1310 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1312 handle_thermal_trip(tz, trip);
1314 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1317 * create_trip_attrs() - create attributes for trip points
1318 * @tz: the thermal zone device
1319 * @mask: Writeable trip point bitmap.
1321 * helper function to instantiate sysfs entries for every trip
1322 * point and its properties of a struct thermal_zone_device.
1324 * Return: 0 on success, the proper error value otherwise.
1326 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1329 int size = sizeof(struct thermal_attr) * tz->trips;
1331 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1332 if (!tz->trip_type_attrs)
1335 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1336 if (!tz->trip_temp_attrs) {
1337 kfree(tz->trip_type_attrs);
1341 if (tz->ops->get_trip_hyst) {
1342 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1343 if (!tz->trip_hyst_attrs) {
1344 kfree(tz->trip_type_attrs);
1345 kfree(tz->trip_temp_attrs);
1351 for (indx = 0; indx < tz->trips; indx++) {
1352 /* create trip type attribute */
1353 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1354 "trip_point_%d_type", indx);
1356 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1357 tz->trip_type_attrs[indx].attr.attr.name =
1358 tz->trip_type_attrs[indx].name;
1359 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1360 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1362 device_create_file(&tz->device,
1363 &tz->trip_type_attrs[indx].attr);
1365 /* create trip temp attribute */
1366 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1367 "trip_point_%d_temp", indx);
1369 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1370 tz->trip_temp_attrs[indx].attr.attr.name =
1371 tz->trip_temp_attrs[indx].name;
1372 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1373 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1374 if (mask & (1 << indx)) {
1375 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1376 tz->trip_temp_attrs[indx].attr.store =
1377 trip_point_temp_store;
1380 device_create_file(&tz->device,
1381 &tz->trip_temp_attrs[indx].attr);
1383 /* create Optional trip hyst attribute */
1384 if (!tz->ops->get_trip_hyst)
1386 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1387 "trip_point_%d_hyst", indx);
1389 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1390 tz->trip_hyst_attrs[indx].attr.attr.name =
1391 tz->trip_hyst_attrs[indx].name;
1392 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1393 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1394 if (tz->ops->set_trip_hyst) {
1395 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1396 tz->trip_hyst_attrs[indx].attr.store =
1397 trip_point_hyst_store;
1400 device_create_file(&tz->device,
1401 &tz->trip_hyst_attrs[indx].attr);
1406 static void remove_trip_attrs(struct thermal_zone_device *tz)
1410 for (indx = 0; indx < tz->trips; indx++) {
1411 device_remove_file(&tz->device,
1412 &tz->trip_type_attrs[indx].attr);
1413 device_remove_file(&tz->device,
1414 &tz->trip_temp_attrs[indx].attr);
1415 if (tz->ops->get_trip_hyst)
1416 device_remove_file(&tz->device,
1417 &tz->trip_hyst_attrs[indx].attr);
1419 kfree(tz->trip_type_attrs);
1420 kfree(tz->trip_temp_attrs);
1421 kfree(tz->trip_hyst_attrs);
1425 * thermal_zone_device_register() - register a new thermal zone device
1426 * @type: the thermal zone device type
1427 * @trips: the number of trip points the thermal zone support
1428 * @mask: a bit string indicating the writeablility of trip points
1429 * @devdata: private device data
1430 * @ops: standard thermal zone device callbacks
1431 * @tzp: thermal zone platform parameters
1432 * @passive_delay: number of milliseconds to wait between polls when
1433 * performing passive cooling
1434 * @polling_delay: number of milliseconds to wait between polls when checking
1435 * whether trip points have been crossed (0 for interrupt
1438 * This interface function adds a new thermal zone device (sensor) to
1439 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1440 * thermal cooling devices registered at the same time.
1441 * thermal_zone_device_unregister() must be called when the device is no
1442 * longer needed. The passive cooling depends on the .get_trend() return value.
1444 * Return: a pointer to the created struct thermal_zone_device or an
1445 * in case of error, an ERR_PTR. Caller must check return value with
1446 * IS_ERR*() helpers.
1448 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1449 int trips, int mask, void *devdata,
1450 struct thermal_zone_device_ops *ops,
1451 const struct thermal_zone_params *tzp,
1452 int passive_delay, int polling_delay)
1454 struct thermal_zone_device *tz;
1455 enum thermal_trip_type trip_type;
1460 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1461 return ERR_PTR(-EINVAL);
1463 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1464 return ERR_PTR(-EINVAL);
1467 return ERR_PTR(-EINVAL);
1469 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1470 return ERR_PTR(-EINVAL);
1472 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1474 return ERR_PTR(-ENOMEM);
1476 INIT_LIST_HEAD(&tz->thermal_instances);
1478 mutex_init(&tz->lock);
1479 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1482 return ERR_PTR(result);
1485 strlcpy(tz->type, type ? : "", sizeof(tz->type));
1488 tz->device.class = &thermal_class;
1489 tz->devdata = devdata;
1491 tz->passive_delay = passive_delay;
1492 tz->polling_delay = polling_delay;
1494 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1495 result = device_register(&tz->device);
1497 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1499 return ERR_PTR(result);
1504 result = device_create_file(&tz->device, &dev_attr_type);
1509 result = device_create_file(&tz->device, &dev_attr_temp);
1513 if (ops->get_mode) {
1514 result = device_create_file(&tz->device, &dev_attr_mode);
1519 result = create_trip_attrs(tz, mask);
1523 for (count = 0; count < trips; count++) {
1524 tz->ops->get_trip_type(tz, count, &trip_type);
1525 if (trip_type == THERMAL_TRIP_PASSIVE)
1530 result = device_create_file(&tz->device, &dev_attr_passive);
1535 #ifdef CONFIG_THERMAL_EMULATION
1536 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1540 /* Create policy attribute */
1541 result = device_create_file(&tz->device, &dev_attr_policy);
1545 /* Update 'this' zone's governor information */
1546 mutex_lock(&thermal_governor_lock);
1549 tz->governor = __find_governor(tz->tzp->governor_name);
1551 tz->governor = def_governor;
1553 mutex_unlock(&thermal_governor_lock);
1555 if (!tz->tzp || !tz->tzp->no_hwmon) {
1556 result = thermal_add_hwmon_sysfs(tz);
1561 mutex_lock(&thermal_list_lock);
1562 list_add_tail(&tz->node, &thermal_tz_list);
1563 mutex_unlock(&thermal_list_lock);
1565 /* Bind cooling devices for this zone */
1568 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1570 if (!tz->ops->get_temp)
1571 thermal_zone_device_set_polling(tz, 0);
1573 thermal_zone_device_update(tz);
1579 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1580 device_unregister(&tz->device);
1581 return ERR_PTR(result);
1583 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1586 * thermal_device_unregister - removes the registered thermal zone device
1587 * @tz: the thermal zone device to remove
1589 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1592 const struct thermal_zone_params *tzp;
1593 struct thermal_cooling_device *cdev;
1594 struct thermal_zone_device *pos = NULL;
1601 mutex_lock(&thermal_list_lock);
1602 list_for_each_entry(pos, &thermal_tz_list, node)
1606 /* thermal zone device not found */
1607 mutex_unlock(&thermal_list_lock);
1610 list_del(&tz->node);
1612 /* Unbind all cdevs associated with 'this' thermal zone */
1613 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1614 if (tz->ops->unbind) {
1615 tz->ops->unbind(tz, cdev);
1619 if (!tzp || !tzp->tbp)
1622 for (i = 0; i < tzp->num_tbps; i++) {
1623 if (tzp->tbp[i].cdev == cdev) {
1624 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1625 tzp->tbp[i].cdev = NULL;
1630 mutex_unlock(&thermal_list_lock);
1632 thermal_zone_device_set_polling(tz, 0);
1635 device_remove_file(&tz->device, &dev_attr_type);
1636 device_remove_file(&tz->device, &dev_attr_temp);
1637 if (tz->ops->get_mode)
1638 device_remove_file(&tz->device, &dev_attr_mode);
1639 device_remove_file(&tz->device, &dev_attr_policy);
1640 remove_trip_attrs(tz);
1641 tz->governor = NULL;
1643 thermal_remove_hwmon_sysfs(tz);
1644 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1645 idr_destroy(&tz->idr);
1646 mutex_destroy(&tz->lock);
1647 device_unregister(&tz->device);
1650 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1653 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1654 * @name: thermal zone name to fetch the temperature
1656 * When only one zone is found with the passed name, returns a reference to it.
1658 * Return: On success returns a reference to an unique thermal zone with
1659 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1660 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1662 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1664 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1665 unsigned int found = 0;
1670 mutex_lock(&thermal_list_lock);
1671 list_for_each_entry(pos, &thermal_tz_list, node)
1672 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1676 mutex_unlock(&thermal_list_lock);
1678 /* nothing has been found, thus an error code for it */
1680 ref = ERR_PTR(-ENODEV);
1682 /* Success only when an unique zone is found */
1683 ref = ERR_PTR(-EEXIST);
1688 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1691 static const struct genl_multicast_group thermal_event_mcgrps[] = {
1692 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1695 static struct genl_family thermal_event_genl_family = {
1696 .id = GENL_ID_GENERATE,
1697 .name = THERMAL_GENL_FAMILY_NAME,
1698 .version = THERMAL_GENL_VERSION,
1699 .maxattr = THERMAL_GENL_ATTR_MAX,
1700 .mcgrps = thermal_event_mcgrps,
1701 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
1704 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1707 struct sk_buff *skb;
1708 struct nlattr *attr;
1709 struct thermal_genl_event *thermal_event;
1713 static unsigned int thermal_event_seqnum;
1718 /* allocate memory */
1719 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1722 skb = genlmsg_new(size, GFP_ATOMIC);
1726 /* add the genetlink message header */
1727 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1728 &thermal_event_genl_family, 0,
1729 THERMAL_GENL_CMD_EVENT);
1736 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1737 sizeof(struct thermal_genl_event));
1744 thermal_event = nla_data(attr);
1745 if (!thermal_event) {
1750 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1752 thermal_event->orig = tz->id;
1753 thermal_event->event = event;
1755 /* send multicast genetlink message */
1756 result = genlmsg_end(skb, msg_header);
1762 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
1765 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1769 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1771 static int genetlink_init(void)
1773 return genl_register_family(&thermal_event_genl_family);
1776 static void genetlink_exit(void)
1778 genl_unregister_family(&thermal_event_genl_family);
1780 #else /* !CONFIG_NET */
1781 static inline int genetlink_init(void) { return 0; }
1782 static inline void genetlink_exit(void) {}
1783 #endif /* !CONFIG_NET */
1785 static int __init thermal_register_governors(void)
1789 result = thermal_gov_step_wise_register();
1793 result = thermal_gov_fair_share_register();
1797 return thermal_gov_user_space_register();
1800 static void thermal_unregister_governors(void)
1802 thermal_gov_step_wise_unregister();
1803 thermal_gov_fair_share_unregister();
1804 thermal_gov_user_space_unregister();
1807 static int __init thermal_init(void)
1811 result = thermal_register_governors();
1815 result = class_register(&thermal_class);
1817 goto unregister_governors;
1819 result = genetlink_init();
1821 goto unregister_class;
1823 result = of_parse_thermal_zones();
1831 unregister_governors:
1832 thermal_unregister_governors();
1834 class_unregister(&thermal_class);
1836 idr_destroy(&thermal_tz_idr);
1837 idr_destroy(&thermal_cdev_idr);
1838 mutex_destroy(&thermal_idr_lock);
1839 mutex_destroy(&thermal_list_lock);
1840 mutex_destroy(&thermal_governor_lock);
1844 static void __exit thermal_exit(void)
1846 of_thermal_destroy_zones();
1848 class_unregister(&thermal_class);
1849 thermal_unregister_governors();
1850 idr_destroy(&thermal_tz_idr);
1851 idr_destroy(&thermal_cdev_idr);
1852 mutex_destroy(&thermal_idr_lock);
1853 mutex_destroy(&thermal_list_lock);
1854 mutex_destroy(&thermal_governor_lock);
1857 fs_initcall(thermal_init);
1858 module_exit(thermal_exit);