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 <net/netlink.h>
37 #include <net/genetlink.h>
39 #include "thermal_core.h"
41 MODULE_AUTHOR("Zhang Rui");
42 MODULE_DESCRIPTION("Generic thermal management sysfs support");
43 MODULE_LICENSE("GPL");
45 static DEFINE_IDR(thermal_tz_idr);
46 static DEFINE_IDR(thermal_cdev_idr);
47 static DEFINE_MUTEX(thermal_idr_lock);
49 static LIST_HEAD(thermal_tz_list);
50 static LIST_HEAD(thermal_cdev_list);
51 static LIST_HEAD(thermal_governor_list);
53 static DEFINE_MUTEX(thermal_list_lock);
54 static DEFINE_MUTEX(thermal_governor_lock);
56 static struct thermal_governor *__find_governor(const char *name)
58 struct thermal_governor *pos;
60 list_for_each_entry(pos, &thermal_governor_list, governor_list)
61 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
67 int thermal_register_governor(struct thermal_governor *governor)
71 struct thermal_zone_device *pos;
76 mutex_lock(&thermal_governor_lock);
79 if (__find_governor(governor->name) == NULL) {
81 list_add(&governor->governor_list, &thermal_governor_list);
84 mutex_lock(&thermal_list_lock);
86 list_for_each_entry(pos, &thermal_tz_list, node) {
90 name = pos->tzp->governor_name;
92 name = DEFAULT_THERMAL_GOVERNOR;
93 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
94 pos->governor = governor;
97 mutex_unlock(&thermal_list_lock);
98 mutex_unlock(&thermal_governor_lock);
102 EXPORT_SYMBOL_GPL(thermal_register_governor);
104 void thermal_unregister_governor(struct thermal_governor *governor)
106 struct thermal_zone_device *pos;
111 mutex_lock(&thermal_governor_lock);
113 if (__find_governor(governor->name) == NULL)
116 mutex_lock(&thermal_list_lock);
118 list_for_each_entry(pos, &thermal_tz_list, node) {
119 if (!strnicmp(pos->governor->name, governor->name,
120 THERMAL_NAME_LENGTH))
121 pos->governor = NULL;
124 mutex_unlock(&thermal_list_lock);
125 list_del(&governor->governor_list);
127 mutex_unlock(&thermal_governor_lock);
130 EXPORT_SYMBOL_GPL(thermal_unregister_governor);
132 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
137 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
142 err = idr_get_new(idr, NULL, id);
145 if (unlikely(err == -EAGAIN))
147 else if (unlikely(err))
150 *id = *id & MAX_IDR_MASK;
154 static void release_idr(struct idr *idr, struct mutex *lock, int id)
163 int get_tz_trend(struct thermal_zone_device *tz, int trip)
165 enum thermal_trend trend;
167 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
168 if (tz->temperature > tz->last_temperature)
169 trend = THERMAL_TREND_RAISING;
170 else if (tz->temperature < tz->last_temperature)
171 trend = THERMAL_TREND_DROPPING;
173 trend = THERMAL_TREND_STABLE;
178 EXPORT_SYMBOL(get_tz_trend);
180 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
181 struct thermal_cooling_device *cdev, int trip)
183 struct thermal_instance *pos = NULL;
184 struct thermal_instance *target_instance = NULL;
186 mutex_lock(&tz->lock);
187 mutex_lock(&cdev->lock);
189 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
190 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
191 target_instance = pos;
196 mutex_unlock(&cdev->lock);
197 mutex_unlock(&tz->lock);
199 return target_instance;
201 EXPORT_SYMBOL(get_thermal_instance);
203 static void print_bind_err_msg(struct thermal_zone_device *tz,
204 struct thermal_cooling_device *cdev, int ret)
206 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
207 tz->type, cdev->type, ret);
210 static void __bind(struct thermal_zone_device *tz, int mask,
211 struct thermal_cooling_device *cdev)
215 for (i = 0; i < tz->trips; i++) {
216 if (mask & (1 << i)) {
217 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
218 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
220 print_bind_err_msg(tz, cdev, ret);
225 static void __unbind(struct thermal_zone_device *tz, int mask,
226 struct thermal_cooling_device *cdev)
230 for (i = 0; i < tz->trips; i++)
232 thermal_zone_unbind_cooling_device(tz, i, cdev);
235 static void bind_cdev(struct thermal_cooling_device *cdev)
238 const struct thermal_zone_params *tzp;
239 struct thermal_zone_device *pos = NULL;
241 mutex_lock(&thermal_list_lock);
243 list_for_each_entry(pos, &thermal_tz_list, node) {
244 if (!pos->tzp && !pos->ops->bind)
247 if (!pos->tzp && pos->ops->bind) {
248 ret = pos->ops->bind(pos, cdev);
250 print_bind_err_msg(pos, cdev, ret);
254 if (!tzp || !tzp->tbp)
257 for (i = 0; i < tzp->num_tbps; i++) {
258 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
260 if (tzp->tbp[i].match(pos, cdev))
262 tzp->tbp[i].cdev = cdev;
263 __bind(pos, tzp->tbp[i].trip_mask, cdev);
267 mutex_unlock(&thermal_list_lock);
270 static void bind_tz(struct thermal_zone_device *tz)
273 struct thermal_cooling_device *pos = NULL;
274 const struct thermal_zone_params *tzp = tz->tzp;
276 if (!tzp && !tz->ops->bind)
279 mutex_lock(&thermal_list_lock);
281 /* If there is no platform data, try to use ops->bind */
282 if (!tzp && tz->ops->bind) {
283 list_for_each_entry(pos, &thermal_cdev_list, node) {
284 ret = tz->ops->bind(tz, pos);
286 print_bind_err_msg(tz, pos, ret);
291 if (!tzp || !tzp->tbp)
294 list_for_each_entry(pos, &thermal_cdev_list, node) {
295 for (i = 0; i < tzp->num_tbps; i++) {
296 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
298 if (tzp->tbp[i].match(tz, pos))
300 tzp->tbp[i].cdev = pos;
301 __bind(tz, tzp->tbp[i].trip_mask, pos);
305 mutex_unlock(&thermal_list_lock);
308 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
312 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
313 round_jiffies(msecs_to_jiffies(delay)));
315 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
316 msecs_to_jiffies(delay));
318 cancel_delayed_work(&tz->poll_queue);
321 static void monitor_thermal_zone(struct thermal_zone_device *tz)
323 mutex_lock(&tz->lock);
326 thermal_zone_device_set_polling(tz, tz->passive_delay);
327 else if (tz->polling_delay)
328 thermal_zone_device_set_polling(tz, tz->polling_delay);
330 thermal_zone_device_set_polling(tz, 0);
332 mutex_unlock(&tz->lock);
335 static void handle_non_critical_trips(struct thermal_zone_device *tz,
336 int trip, enum thermal_trip_type trip_type)
339 tz->governor->throttle(tz, trip);
342 static void handle_critical_trips(struct thermal_zone_device *tz,
343 int trip, enum thermal_trip_type trip_type)
347 tz->ops->get_trip_temp(tz, trip, &trip_temp);
349 /* If we have not crossed the trip_temp, we do not care. */
350 if (tz->temperature < trip_temp)
354 tz->ops->notify(tz, trip, trip_type);
356 if (trip_type == THERMAL_TRIP_CRITICAL) {
357 dev_emerg(&tz->device,
358 "critical temperature reached(%d C),shutting down\n",
359 tz->temperature / 1000);
360 orderly_poweroff(true);
364 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
366 enum thermal_trip_type type;
368 tz->ops->get_trip_type(tz, trip, &type);
370 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
371 handle_critical_trips(tz, trip, type);
373 handle_non_critical_trips(tz, trip, type);
375 * Alright, we handled this trip successfully.
376 * So, start monitoring again.
378 monitor_thermal_zone(tz);
381 static int thermal_zone_get_temp(struct thermal_zone_device *tz,
385 #ifdef CONFIG_THERMAL_EMULATION
387 unsigned long crit_temp = -1UL;
388 enum thermal_trip_type type;
391 mutex_lock(&tz->lock);
393 ret = tz->ops->get_temp(tz, temp);
394 #ifdef CONFIG_THERMAL_EMULATION
395 if (!tz->emul_temperature)
398 for (count = 0; count < tz->trips; count++) {
399 ret = tz->ops->get_trip_type(tz, count, &type);
400 if (!ret && type == THERMAL_TRIP_CRITICAL) {
401 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
409 if (*temp < crit_temp)
410 *temp = tz->emul_temperature;
413 mutex_unlock(&tz->lock);
417 static void update_temperature(struct thermal_zone_device *tz)
422 ret = thermal_zone_get_temp(tz, &temp);
424 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
429 mutex_lock(&tz->lock);
430 tz->last_temperature = tz->temperature;
431 tz->temperature = temp;
432 mutex_unlock(&tz->lock);
435 void thermal_zone_device_update(struct thermal_zone_device *tz)
439 update_temperature(tz);
441 for (count = 0; count < tz->trips; count++)
442 handle_thermal_trip(tz, count);
444 EXPORT_SYMBOL(thermal_zone_device_update);
446 static void thermal_zone_device_check(struct work_struct *work)
448 struct thermal_zone_device *tz = container_of(work, struct
451 thermal_zone_device_update(tz);
454 /* sys I/F for thermal zone */
456 #define to_thermal_zone(_dev) \
457 container_of(_dev, struct thermal_zone_device, device)
460 type_show(struct device *dev, struct device_attribute *attr, char *buf)
462 struct thermal_zone_device *tz = to_thermal_zone(dev);
464 return sprintf(buf, "%s\n", tz->type);
468 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
470 struct thermal_zone_device *tz = to_thermal_zone(dev);
474 ret = thermal_zone_get_temp(tz, &temperature);
479 return sprintf(buf, "%ld\n", temperature);
483 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
485 struct thermal_zone_device *tz = to_thermal_zone(dev);
486 enum thermal_device_mode mode;
489 if (!tz->ops->get_mode)
492 result = tz->ops->get_mode(tz, &mode);
496 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
501 mode_store(struct device *dev, struct device_attribute *attr,
502 const char *buf, size_t count)
504 struct thermal_zone_device *tz = to_thermal_zone(dev);
507 if (!tz->ops->set_mode)
510 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
511 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
512 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
513 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
524 trip_point_type_show(struct device *dev, struct device_attribute *attr,
527 struct thermal_zone_device *tz = to_thermal_zone(dev);
528 enum thermal_trip_type type;
531 if (!tz->ops->get_trip_type)
534 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
537 result = tz->ops->get_trip_type(tz, trip, &type);
542 case THERMAL_TRIP_CRITICAL:
543 return sprintf(buf, "critical\n");
544 case THERMAL_TRIP_HOT:
545 return sprintf(buf, "hot\n");
546 case THERMAL_TRIP_PASSIVE:
547 return sprintf(buf, "passive\n");
548 case THERMAL_TRIP_ACTIVE:
549 return sprintf(buf, "active\n");
551 return sprintf(buf, "unknown\n");
556 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
557 const char *buf, size_t count)
559 struct thermal_zone_device *tz = to_thermal_zone(dev);
561 unsigned long temperature;
563 if (!tz->ops->set_trip_temp)
566 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
569 if (kstrtoul(buf, 10, &temperature))
572 ret = tz->ops->set_trip_temp(tz, trip, temperature);
574 return ret ? ret : count;
578 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
581 struct thermal_zone_device *tz = to_thermal_zone(dev);
585 if (!tz->ops->get_trip_temp)
588 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
591 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
596 return sprintf(buf, "%ld\n", temperature);
600 trip_point_hyst_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_hyst)
610 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
613 if (kstrtoul(buf, 10, &temperature))
617 * We are not doing any check on the 'temperature' value
618 * here. The driver implementing 'set_trip_hyst' has to
621 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
623 return ret ? ret : count;
627 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
630 struct thermal_zone_device *tz = to_thermal_zone(dev);
632 unsigned long temperature;
634 if (!tz->ops->get_trip_hyst)
637 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
640 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
642 return ret ? ret : sprintf(buf, "%ld\n", temperature);
646 passive_store(struct device *dev, struct device_attribute *attr,
647 const char *buf, size_t count)
649 struct thermal_zone_device *tz = to_thermal_zone(dev);
650 struct thermal_cooling_device *cdev = NULL;
653 if (!sscanf(buf, "%d\n", &state))
656 /* sanity check: values below 1000 millicelcius don't make sense
657 * and can cause the system to go into a thermal heart attack
659 if (state && state < 1000)
662 if (state && !tz->forced_passive) {
663 mutex_lock(&thermal_list_lock);
664 list_for_each_entry(cdev, &thermal_cdev_list, node) {
665 if (!strncmp("Processor", cdev->type,
666 sizeof("Processor")))
667 thermal_zone_bind_cooling_device(tz,
668 THERMAL_TRIPS_NONE, cdev,
672 mutex_unlock(&thermal_list_lock);
673 if (!tz->passive_delay)
674 tz->passive_delay = 1000;
675 } else if (!state && tz->forced_passive) {
676 mutex_lock(&thermal_list_lock);
677 list_for_each_entry(cdev, &thermal_cdev_list, node) {
678 if (!strncmp("Processor", cdev->type,
679 sizeof("Processor")))
680 thermal_zone_unbind_cooling_device(tz,
684 mutex_unlock(&thermal_list_lock);
685 tz->passive_delay = 0;
688 tz->forced_passive = state;
690 thermal_zone_device_update(tz);
696 passive_show(struct device *dev, struct device_attribute *attr,
699 struct thermal_zone_device *tz = to_thermal_zone(dev);
701 return sprintf(buf, "%d\n", tz->forced_passive);
705 policy_store(struct device *dev, struct device_attribute *attr,
706 const char *buf, size_t count)
709 struct thermal_zone_device *tz = to_thermal_zone(dev);
710 struct thermal_governor *gov;
712 mutex_lock(&thermal_governor_lock);
714 gov = __find_governor(buf);
722 mutex_unlock(&thermal_governor_lock);
727 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
729 struct thermal_zone_device *tz = to_thermal_zone(dev);
731 return sprintf(buf, "%s\n", tz->governor->name);
734 #ifdef CONFIG_THERMAL_EMULATION
736 emul_temp_store(struct device *dev, struct device_attribute *attr,
737 const char *buf, size_t count)
739 struct thermal_zone_device *tz = to_thermal_zone(dev);
741 unsigned long temperature;
743 if (kstrtoul(buf, 10, &temperature))
746 if (!tz->ops->set_emul_temp) {
747 mutex_lock(&tz->lock);
748 tz->emul_temperature = temperature;
749 mutex_unlock(&tz->lock);
751 ret = tz->ops->set_emul_temp(tz, temperature);
754 return ret ? ret : count;
756 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
757 #endif/*CONFIG_THERMAL_EMULATION*/
759 static DEVICE_ATTR(type, 0444, type_show, NULL);
760 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
761 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
762 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
763 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
765 /* sys I/F for cooling device */
766 #define to_cooling_device(_dev) \
767 container_of(_dev, struct thermal_cooling_device, device)
770 thermal_cooling_device_type_show(struct device *dev,
771 struct device_attribute *attr, char *buf)
773 struct thermal_cooling_device *cdev = to_cooling_device(dev);
775 return sprintf(buf, "%s\n", cdev->type);
779 thermal_cooling_device_max_state_show(struct device *dev,
780 struct device_attribute *attr, char *buf)
782 struct thermal_cooling_device *cdev = to_cooling_device(dev);
786 ret = cdev->ops->get_max_state(cdev, &state);
789 return sprintf(buf, "%ld\n", state);
793 thermal_cooling_device_cur_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_cur_state(cdev, &state);
803 return sprintf(buf, "%ld\n", state);
807 thermal_cooling_device_cur_state_store(struct device *dev,
808 struct device_attribute *attr,
809 const char *buf, size_t count)
811 struct thermal_cooling_device *cdev = to_cooling_device(dev);
815 if (!sscanf(buf, "%ld\n", &state))
821 result = cdev->ops->set_cur_state(cdev, state);
827 static struct device_attribute dev_attr_cdev_type =
828 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
829 static DEVICE_ATTR(max_state, 0444,
830 thermal_cooling_device_max_state_show, NULL);
831 static DEVICE_ATTR(cur_state, 0644,
832 thermal_cooling_device_cur_state_show,
833 thermal_cooling_device_cur_state_store);
836 thermal_cooling_device_trip_point_show(struct device *dev,
837 struct device_attribute *attr, char *buf)
839 struct thermal_instance *instance;
842 container_of(attr, struct thermal_instance, attr);
844 if (instance->trip == THERMAL_TRIPS_NONE)
845 return sprintf(buf, "-1\n");
847 return sprintf(buf, "%d\n", instance->trip);
850 /* Device management */
852 #if defined(CONFIG_THERMAL_HWMON)
855 #include <linux/hwmon.h>
857 /* thermal zone devices with the same type share one hwmon device */
858 struct thermal_hwmon_device {
859 char type[THERMAL_NAME_LENGTH];
860 struct device *device;
862 struct list_head tz_list;
863 struct list_head node;
866 struct thermal_hwmon_attr {
867 struct device_attribute attr;
871 /* one temperature input for each thermal zone */
872 struct thermal_hwmon_temp {
873 struct list_head hwmon_node;
874 struct thermal_zone_device *tz;
875 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
876 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
879 static LIST_HEAD(thermal_hwmon_list);
882 name_show(struct device *dev, struct device_attribute *attr, char *buf)
884 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
885 return sprintf(buf, "%s\n", hwmon->type);
887 static DEVICE_ATTR(name, 0444, name_show, NULL);
890 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
894 struct thermal_hwmon_attr *hwmon_attr
895 = container_of(attr, struct thermal_hwmon_attr, attr);
896 struct thermal_hwmon_temp *temp
897 = container_of(hwmon_attr, struct thermal_hwmon_temp,
899 struct thermal_zone_device *tz = temp->tz;
901 ret = thermal_zone_get_temp(tz, &temperature);
906 return sprintf(buf, "%ld\n", temperature);
910 temp_crit_show(struct device *dev, struct device_attribute *attr,
913 struct thermal_hwmon_attr *hwmon_attr
914 = container_of(attr, struct thermal_hwmon_attr, attr);
915 struct thermal_hwmon_temp *temp
916 = container_of(hwmon_attr, struct thermal_hwmon_temp,
918 struct thermal_zone_device *tz = temp->tz;
922 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
926 return sprintf(buf, "%ld\n", temperature);
930 static struct thermal_hwmon_device *
931 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
933 struct thermal_hwmon_device *hwmon;
935 mutex_lock(&thermal_list_lock);
936 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
937 if (!strcmp(hwmon->type, tz->type)) {
938 mutex_unlock(&thermal_list_lock);
941 mutex_unlock(&thermal_list_lock);
946 /* Find the temperature input matching a given thermal zone */
947 static struct thermal_hwmon_temp *
948 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
949 const struct thermal_zone_device *tz)
951 struct thermal_hwmon_temp *temp;
953 mutex_lock(&thermal_list_lock);
954 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
955 if (temp->tz == tz) {
956 mutex_unlock(&thermal_list_lock);
959 mutex_unlock(&thermal_list_lock);
965 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
967 struct thermal_hwmon_device *hwmon;
968 struct thermal_hwmon_temp *temp;
969 int new_hwmon_device = 1;
972 hwmon = thermal_hwmon_lookup_by_type(tz);
974 new_hwmon_device = 0;
975 goto register_sys_interface;
978 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
982 INIT_LIST_HEAD(&hwmon->tz_list);
983 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
984 hwmon->device = hwmon_device_register(NULL);
985 if (IS_ERR(hwmon->device)) {
986 result = PTR_ERR(hwmon->device);
989 dev_set_drvdata(hwmon->device, hwmon);
990 result = device_create_file(hwmon->device, &dev_attr_name);
994 register_sys_interface:
995 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
998 goto unregister_name;
1004 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
1005 "temp%d_input", hwmon->count);
1006 temp->temp_input.attr.attr.name = temp->temp_input.name;
1007 temp->temp_input.attr.attr.mode = 0444;
1008 temp->temp_input.attr.show = temp_input_show;
1009 sysfs_attr_init(&temp->temp_input.attr.attr);
1010 result = device_create_file(hwmon->device, &temp->temp_input.attr);
1014 if (tz->ops->get_crit_temp) {
1015 unsigned long temperature;
1016 if (!tz->ops->get_crit_temp(tz, &temperature)) {
1017 snprintf(temp->temp_crit.name,
1018 sizeof(temp->temp_crit.name),
1019 "temp%d_crit", hwmon->count);
1020 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1021 temp->temp_crit.attr.attr.mode = 0444;
1022 temp->temp_crit.attr.show = temp_crit_show;
1023 sysfs_attr_init(&temp->temp_crit.attr.attr);
1024 result = device_create_file(hwmon->device,
1025 &temp->temp_crit.attr);
1027 goto unregister_input;
1031 mutex_lock(&thermal_list_lock);
1032 if (new_hwmon_device)
1033 list_add_tail(&hwmon->node, &thermal_hwmon_list);
1034 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
1035 mutex_unlock(&thermal_list_lock);
1040 device_remove_file(hwmon->device, &temp->temp_input.attr);
1044 if (new_hwmon_device) {
1045 device_remove_file(hwmon->device, &dev_attr_name);
1046 hwmon_device_unregister(hwmon->device);
1049 if (new_hwmon_device)
1056 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1058 struct thermal_hwmon_device *hwmon;
1059 struct thermal_hwmon_temp *temp;
1061 hwmon = thermal_hwmon_lookup_by_type(tz);
1062 if (unlikely(!hwmon)) {
1063 /* Should never happen... */
1064 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1068 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1069 if (unlikely(!temp)) {
1070 /* Should never happen... */
1071 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1075 device_remove_file(hwmon->device, &temp->temp_input.attr);
1076 if (tz->ops->get_crit_temp)
1077 device_remove_file(hwmon->device, &temp->temp_crit.attr);
1079 mutex_lock(&thermal_list_lock);
1080 list_del(&temp->hwmon_node);
1082 if (!list_empty(&hwmon->tz_list)) {
1083 mutex_unlock(&thermal_list_lock);
1086 list_del(&hwmon->node);
1087 mutex_unlock(&thermal_list_lock);
1089 device_remove_file(hwmon->device, &dev_attr_name);
1090 hwmon_device_unregister(hwmon->device);
1095 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1101 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1107 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
1108 * @tz: thermal zone device
1109 * @trip: indicates which trip point the cooling devices is
1110 * associated with in this thermal zone.
1111 * @cdev: thermal cooling device
1113 * This function is usually called in the thermal zone device .bind callback.
1115 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1117 struct thermal_cooling_device *cdev,
1118 unsigned long upper, unsigned long lower)
1120 struct thermal_instance *dev;
1121 struct thermal_instance *pos;
1122 struct thermal_zone_device *pos1;
1123 struct thermal_cooling_device *pos2;
1124 unsigned long max_state;
1127 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1130 list_for_each_entry(pos1, &thermal_tz_list, node) {
1134 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1139 if (tz != pos1 || cdev != pos2)
1142 cdev->ops->get_max_state(cdev, &max_state);
1144 /* lower default 0, upper default max_state */
1145 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1146 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1148 if (lower > upper || upper > max_state)
1152 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1160 dev->target = THERMAL_NO_TARGET;
1162 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1166 sprintf(dev->name, "cdev%d", dev->id);
1168 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1172 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1173 sysfs_attr_init(&dev->attr.attr);
1174 dev->attr.attr.name = dev->attr_name;
1175 dev->attr.attr.mode = 0444;
1176 dev->attr.show = thermal_cooling_device_trip_point_show;
1177 result = device_create_file(&tz->device, &dev->attr);
1179 goto remove_symbol_link;
1181 mutex_lock(&tz->lock);
1182 mutex_lock(&cdev->lock);
1183 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1184 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1189 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1190 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1192 mutex_unlock(&cdev->lock);
1193 mutex_unlock(&tz->lock);
1198 device_remove_file(&tz->device, &dev->attr);
1200 sysfs_remove_link(&tz->device.kobj, dev->name);
1202 release_idr(&tz->idr, &tz->lock, dev->id);
1207 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1210 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
1211 * @tz: thermal zone device
1212 * @trip: indicates which trip point the cooling devices is
1213 * associated with in this thermal zone.
1214 * @cdev: thermal cooling device
1216 * This function is usually called in the thermal zone device .unbind callback.
1218 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1220 struct thermal_cooling_device *cdev)
1222 struct thermal_instance *pos, *next;
1224 mutex_lock(&tz->lock);
1225 mutex_lock(&cdev->lock);
1226 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1227 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1228 list_del(&pos->tz_node);
1229 list_del(&pos->cdev_node);
1230 mutex_unlock(&cdev->lock);
1231 mutex_unlock(&tz->lock);
1235 mutex_unlock(&cdev->lock);
1236 mutex_unlock(&tz->lock);
1241 device_remove_file(&tz->device, &pos->attr);
1242 sysfs_remove_link(&tz->device.kobj, pos->name);
1243 release_idr(&tz->idr, &tz->lock, pos->id);
1247 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1249 static void thermal_release(struct device *dev)
1251 struct thermal_zone_device *tz;
1252 struct thermal_cooling_device *cdev;
1254 if (!strncmp(dev_name(dev), "thermal_zone",
1255 sizeof("thermal_zone") - 1)) {
1256 tz = to_thermal_zone(dev);
1259 cdev = to_cooling_device(dev);
1264 static struct class thermal_class = {
1266 .dev_release = thermal_release,
1270 * thermal_cooling_device_register - register a new thermal cooling device
1271 * @type: the thermal cooling device type.
1272 * @devdata: device private data.
1273 * @ops: standard thermal cooling devices callbacks.
1275 struct thermal_cooling_device *
1276 thermal_cooling_device_register(char *type, void *devdata,
1277 const struct thermal_cooling_device_ops *ops)
1279 struct thermal_cooling_device *cdev;
1282 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1283 return ERR_PTR(-EINVAL);
1285 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1286 !ops->set_cur_state)
1287 return ERR_PTR(-EINVAL);
1289 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1291 return ERR_PTR(-ENOMEM);
1293 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1296 return ERR_PTR(result);
1299 strcpy(cdev->type, type ? : "");
1300 mutex_init(&cdev->lock);
1301 INIT_LIST_HEAD(&cdev->thermal_instances);
1303 cdev->updated = true;
1304 cdev->device.class = &thermal_class;
1305 cdev->devdata = devdata;
1306 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1307 result = device_register(&cdev->device);
1309 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1311 return ERR_PTR(result);
1316 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1321 result = device_create_file(&cdev->device, &dev_attr_max_state);
1325 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1329 /* Add 'this' new cdev to the global cdev list */
1330 mutex_lock(&thermal_list_lock);
1331 list_add(&cdev->node, &thermal_cdev_list);
1332 mutex_unlock(&thermal_list_lock);
1334 /* Update binding information for 'this' new cdev */
1340 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1341 device_unregister(&cdev->device);
1342 return ERR_PTR(result);
1344 EXPORT_SYMBOL(thermal_cooling_device_register);
1347 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1348 * @cdev: the thermal cooling device to remove.
1350 * thermal_cooling_device_unregister() must be called when the device is no
1353 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1356 const struct thermal_zone_params *tzp;
1357 struct thermal_zone_device *tz;
1358 struct thermal_cooling_device *pos = NULL;
1363 mutex_lock(&thermal_list_lock);
1364 list_for_each_entry(pos, &thermal_cdev_list, node)
1368 /* thermal cooling device not found */
1369 mutex_unlock(&thermal_list_lock);
1372 list_del(&cdev->node);
1374 /* Unbind all thermal zones associated with 'this' cdev */
1375 list_for_each_entry(tz, &thermal_tz_list, node) {
1376 if (tz->ops->unbind) {
1377 tz->ops->unbind(tz, cdev);
1381 if (!tz->tzp || !tz->tzp->tbp)
1385 for (i = 0; i < tzp->num_tbps; i++) {
1386 if (tzp->tbp[i].cdev == cdev) {
1387 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1388 tzp->tbp[i].cdev = NULL;
1393 mutex_unlock(&thermal_list_lock);
1396 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1397 device_remove_file(&cdev->device, &dev_attr_max_state);
1398 device_remove_file(&cdev->device, &dev_attr_cur_state);
1400 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1401 device_unregister(&cdev->device);
1404 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1406 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1408 struct thermal_instance *instance;
1409 unsigned long target = 0;
1411 /* cooling device is updated*/
1415 mutex_lock(&cdev->lock);
1416 /* Make sure cdev enters the deepest cooling state */
1417 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1418 if (instance->target == THERMAL_NO_TARGET)
1420 if (instance->target > target)
1421 target = instance->target;
1423 mutex_unlock(&cdev->lock);
1424 cdev->ops->set_cur_state(cdev, target);
1425 cdev->updated = true;
1427 EXPORT_SYMBOL(thermal_cdev_update);
1430 * notify_thermal_framework - Sensor drivers use this API to notify framework
1431 * @tz: thermal zone device
1432 * @trip: indicates which trip point has been crossed
1434 * This function handles the trip events from sensor drivers. It starts
1435 * throttling the cooling devices according to the policy configured.
1436 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1437 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1438 * The throttling policy is based on the configured platform data; if no
1439 * platform data is provided, this uses the step_wise throttling policy.
1441 void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
1443 handle_thermal_trip(tz, trip);
1445 EXPORT_SYMBOL(notify_thermal_framework);
1448 * create_trip_attrs - create attributes for trip points
1449 * @tz: the thermal zone device
1450 * @mask: Writeable trip point bitmap.
1452 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1455 int size = sizeof(struct thermal_attr) * tz->trips;
1457 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1458 if (!tz->trip_type_attrs)
1461 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1462 if (!tz->trip_temp_attrs) {
1463 kfree(tz->trip_type_attrs);
1467 if (tz->ops->get_trip_hyst) {
1468 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1469 if (!tz->trip_hyst_attrs) {
1470 kfree(tz->trip_type_attrs);
1471 kfree(tz->trip_temp_attrs);
1477 for (indx = 0; indx < tz->trips; indx++) {
1478 /* create trip type attribute */
1479 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1480 "trip_point_%d_type", indx);
1482 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1483 tz->trip_type_attrs[indx].attr.attr.name =
1484 tz->trip_type_attrs[indx].name;
1485 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1486 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1488 device_create_file(&tz->device,
1489 &tz->trip_type_attrs[indx].attr);
1491 /* create trip temp attribute */
1492 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1493 "trip_point_%d_temp", indx);
1495 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1496 tz->trip_temp_attrs[indx].attr.attr.name =
1497 tz->trip_temp_attrs[indx].name;
1498 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1499 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1500 if (mask & (1 << indx)) {
1501 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1502 tz->trip_temp_attrs[indx].attr.store =
1503 trip_point_temp_store;
1506 device_create_file(&tz->device,
1507 &tz->trip_temp_attrs[indx].attr);
1509 /* create Optional trip hyst attribute */
1510 if (!tz->ops->get_trip_hyst)
1512 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1513 "trip_point_%d_hyst", indx);
1515 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1516 tz->trip_hyst_attrs[indx].attr.attr.name =
1517 tz->trip_hyst_attrs[indx].name;
1518 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1519 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1520 if (tz->ops->set_trip_hyst) {
1521 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1522 tz->trip_hyst_attrs[indx].attr.store =
1523 trip_point_hyst_store;
1526 device_create_file(&tz->device,
1527 &tz->trip_hyst_attrs[indx].attr);
1532 static void remove_trip_attrs(struct thermal_zone_device *tz)
1536 for (indx = 0; indx < tz->trips; indx++) {
1537 device_remove_file(&tz->device,
1538 &tz->trip_type_attrs[indx].attr);
1539 device_remove_file(&tz->device,
1540 &tz->trip_temp_attrs[indx].attr);
1541 if (tz->ops->get_trip_hyst)
1542 device_remove_file(&tz->device,
1543 &tz->trip_hyst_attrs[indx].attr);
1545 kfree(tz->trip_type_attrs);
1546 kfree(tz->trip_temp_attrs);
1547 kfree(tz->trip_hyst_attrs);
1551 * thermal_zone_device_register - register a new thermal zone device
1552 * @type: the thermal zone device type
1553 * @trips: the number of trip points the thermal zone support
1554 * @mask: a bit string indicating the writeablility of trip points
1555 * @devdata: private device data
1556 * @ops: standard thermal zone device callbacks
1557 * @tzp: thermal zone platform parameters
1558 * @passive_delay: number of milliseconds to wait between polls when
1559 * performing passive cooling
1560 * @polling_delay: number of milliseconds to wait between polls when checking
1561 * whether trip points have been crossed (0 for interrupt
1564 * thermal_zone_device_unregister() must be called when the device is no
1565 * longer needed. The passive cooling depends on the .get_trend() return value.
1567 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1568 int trips, int mask, void *devdata,
1569 const struct thermal_zone_device_ops *ops,
1570 const struct thermal_zone_params *tzp,
1571 int passive_delay, int polling_delay)
1573 struct thermal_zone_device *tz;
1574 enum thermal_trip_type trip_type;
1579 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1580 return ERR_PTR(-EINVAL);
1582 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1583 return ERR_PTR(-EINVAL);
1585 if (!ops || !ops->get_temp)
1586 return ERR_PTR(-EINVAL);
1588 if (trips > 0 && !ops->get_trip_type)
1589 return ERR_PTR(-EINVAL);
1591 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1593 return ERR_PTR(-ENOMEM);
1595 INIT_LIST_HEAD(&tz->thermal_instances);
1597 mutex_init(&tz->lock);
1598 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1601 return ERR_PTR(result);
1604 strcpy(tz->type, type ? : "");
1607 tz->device.class = &thermal_class;
1608 tz->devdata = devdata;
1610 tz->passive_delay = passive_delay;
1611 tz->polling_delay = polling_delay;
1613 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1614 result = device_register(&tz->device);
1616 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1618 return ERR_PTR(result);
1623 result = device_create_file(&tz->device, &dev_attr_type);
1628 result = device_create_file(&tz->device, &dev_attr_temp);
1632 if (ops->get_mode) {
1633 result = device_create_file(&tz->device, &dev_attr_mode);
1638 result = create_trip_attrs(tz, mask);
1642 for (count = 0; count < trips; count++) {
1643 tz->ops->get_trip_type(tz, count, &trip_type);
1644 if (trip_type == THERMAL_TRIP_PASSIVE)
1649 result = device_create_file(&tz->device, &dev_attr_passive);
1654 #ifdef CONFIG_THERMAL_EMULATION
1655 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1659 /* Create policy attribute */
1660 result = device_create_file(&tz->device, &dev_attr_policy);
1664 /* Update 'this' zone's governor information */
1665 mutex_lock(&thermal_governor_lock);
1668 tz->governor = __find_governor(tz->tzp->governor_name);
1670 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1672 mutex_unlock(&thermal_governor_lock);
1674 result = thermal_add_hwmon_sysfs(tz);
1678 mutex_lock(&thermal_list_lock);
1679 list_add_tail(&tz->node, &thermal_tz_list);
1680 mutex_unlock(&thermal_list_lock);
1682 /* Bind cooling devices for this zone */
1685 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1687 thermal_zone_device_update(tz);
1693 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1694 device_unregister(&tz->device);
1695 return ERR_PTR(result);
1697 EXPORT_SYMBOL(thermal_zone_device_register);
1700 * thermal_device_unregister - removes the registered thermal zone device
1701 * @tz: the thermal zone device to remove
1703 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1706 const struct thermal_zone_params *tzp;
1707 struct thermal_cooling_device *cdev;
1708 struct thermal_zone_device *pos = NULL;
1715 mutex_lock(&thermal_list_lock);
1716 list_for_each_entry(pos, &thermal_tz_list, node)
1720 /* thermal zone device not found */
1721 mutex_unlock(&thermal_list_lock);
1724 list_del(&tz->node);
1726 /* Unbind all cdevs associated with 'this' thermal zone */
1727 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1728 if (tz->ops->unbind) {
1729 tz->ops->unbind(tz, cdev);
1733 if (!tzp || !tzp->tbp)
1736 for (i = 0; i < tzp->num_tbps; i++) {
1737 if (tzp->tbp[i].cdev == cdev) {
1738 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1739 tzp->tbp[i].cdev = NULL;
1744 mutex_unlock(&thermal_list_lock);
1746 thermal_zone_device_set_polling(tz, 0);
1749 device_remove_file(&tz->device, &dev_attr_type);
1750 device_remove_file(&tz->device, &dev_attr_temp);
1751 if (tz->ops->get_mode)
1752 device_remove_file(&tz->device, &dev_attr_mode);
1753 device_remove_file(&tz->device, &dev_attr_policy);
1754 remove_trip_attrs(tz);
1755 tz->governor = NULL;
1757 thermal_remove_hwmon_sysfs(tz);
1758 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1759 idr_destroy(&tz->idr);
1760 mutex_destroy(&tz->lock);
1761 device_unregister(&tz->device);
1764 EXPORT_SYMBOL(thermal_zone_device_unregister);
1767 static struct genl_family thermal_event_genl_family = {
1768 .id = GENL_ID_GENERATE,
1769 .name = THERMAL_GENL_FAMILY_NAME,
1770 .version = THERMAL_GENL_VERSION,
1771 .maxattr = THERMAL_GENL_ATTR_MAX,
1774 static struct genl_multicast_group thermal_event_mcgrp = {
1775 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1778 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1781 struct sk_buff *skb;
1782 struct nlattr *attr;
1783 struct thermal_genl_event *thermal_event;
1787 static unsigned int thermal_event_seqnum;
1792 /* allocate memory */
1793 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1796 skb = genlmsg_new(size, GFP_ATOMIC);
1800 /* add the genetlink message header */
1801 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1802 &thermal_event_genl_family, 0,
1803 THERMAL_GENL_CMD_EVENT);
1810 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1811 sizeof(struct thermal_genl_event));
1818 thermal_event = nla_data(attr);
1819 if (!thermal_event) {
1824 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1826 thermal_event->orig = tz->id;
1827 thermal_event->event = event;
1829 /* send multicast genetlink message */
1830 result = genlmsg_end(skb, msg_header);
1836 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1838 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1842 EXPORT_SYMBOL(thermal_generate_netlink_event);
1844 static int genetlink_init(void)
1848 result = genl_register_family(&thermal_event_genl_family);
1852 result = genl_register_mc_group(&thermal_event_genl_family,
1853 &thermal_event_mcgrp);
1855 genl_unregister_family(&thermal_event_genl_family);
1859 static void genetlink_exit(void)
1861 genl_unregister_family(&thermal_event_genl_family);
1863 #else /* !CONFIG_NET */
1864 static inline int genetlink_init(void) { return 0; }
1865 static inline void genetlink_exit(void) {}
1866 #endif /* !CONFIG_NET */
1868 static int __init thermal_init(void)
1872 result = class_register(&thermal_class);
1874 idr_destroy(&thermal_tz_idr);
1875 idr_destroy(&thermal_cdev_idr);
1876 mutex_destroy(&thermal_idr_lock);
1877 mutex_destroy(&thermal_list_lock);
1880 result = genetlink_init();
1884 static void __exit thermal_exit(void)
1886 class_unregister(&thermal_class);
1887 idr_destroy(&thermal_tz_idr);
1888 idr_destroy(&thermal_cdev_idr);
1889 mutex_destroy(&thermal_idr_lock);
1890 mutex_destroy(&thermal_list_lock);
1894 fs_initcall(thermal_init);
1895 module_exit(thermal_exit);