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);
103 void thermal_unregister_governor(struct thermal_governor *governor)
105 struct thermal_zone_device *pos;
110 mutex_lock(&thermal_governor_lock);
112 if (__find_governor(governor->name) == NULL)
115 mutex_lock(&thermal_list_lock);
117 list_for_each_entry(pos, &thermal_tz_list, node) {
118 if (!strnicmp(pos->governor->name, governor->name,
119 THERMAL_NAME_LENGTH))
120 pos->governor = NULL;
123 mutex_unlock(&thermal_list_lock);
124 list_del(&governor->governor_list);
126 mutex_unlock(&thermal_governor_lock);
130 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
136 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
139 if (unlikely(ret < 0))
145 static void release_idr(struct idr *idr, struct mutex *lock, int id)
154 int get_tz_trend(struct thermal_zone_device *tz, int trip)
156 enum thermal_trend trend;
158 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
159 if (tz->temperature > tz->last_temperature)
160 trend = THERMAL_TREND_RAISING;
161 else if (tz->temperature < tz->last_temperature)
162 trend = THERMAL_TREND_DROPPING;
164 trend = THERMAL_TREND_STABLE;
169 EXPORT_SYMBOL(get_tz_trend);
171 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
172 struct thermal_cooling_device *cdev, int trip)
174 struct thermal_instance *pos = NULL;
175 struct thermal_instance *target_instance = NULL;
177 mutex_lock(&tz->lock);
178 mutex_lock(&cdev->lock);
180 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
181 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
182 target_instance = pos;
187 mutex_unlock(&cdev->lock);
188 mutex_unlock(&tz->lock);
190 return target_instance;
192 EXPORT_SYMBOL(get_thermal_instance);
194 static void print_bind_err_msg(struct thermal_zone_device *tz,
195 struct thermal_cooling_device *cdev, int ret)
197 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
198 tz->type, cdev->type, ret);
201 static void __bind(struct thermal_zone_device *tz, int mask,
202 struct thermal_cooling_device *cdev)
206 for (i = 0; i < tz->trips; i++) {
207 if (mask & (1 << i)) {
208 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
209 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
211 print_bind_err_msg(tz, cdev, ret);
216 static void __unbind(struct thermal_zone_device *tz, int mask,
217 struct thermal_cooling_device *cdev)
221 for (i = 0; i < tz->trips; i++)
223 thermal_zone_unbind_cooling_device(tz, i, cdev);
226 static void bind_cdev(struct thermal_cooling_device *cdev)
229 const struct thermal_zone_params *tzp;
230 struct thermal_zone_device *pos = NULL;
232 mutex_lock(&thermal_list_lock);
234 list_for_each_entry(pos, &thermal_tz_list, node) {
235 if (!pos->tzp && !pos->ops->bind)
238 if (!pos->tzp && pos->ops->bind) {
239 ret = pos->ops->bind(pos, cdev);
241 print_bind_err_msg(pos, cdev, ret);
245 if (!tzp || !tzp->tbp)
248 for (i = 0; i < tzp->num_tbps; i++) {
249 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
251 if (tzp->tbp[i].match(pos, cdev))
253 tzp->tbp[i].cdev = cdev;
254 __bind(pos, tzp->tbp[i].trip_mask, cdev);
258 mutex_unlock(&thermal_list_lock);
261 static void bind_tz(struct thermal_zone_device *tz)
264 struct thermal_cooling_device *pos = NULL;
265 const struct thermal_zone_params *tzp = tz->tzp;
267 if (!tzp && !tz->ops->bind)
270 mutex_lock(&thermal_list_lock);
272 /* If there is no platform data, try to use ops->bind */
273 if (!tzp && tz->ops->bind) {
274 list_for_each_entry(pos, &thermal_cdev_list, node) {
275 ret = tz->ops->bind(tz, pos);
277 print_bind_err_msg(tz, pos, ret);
282 if (!tzp || !tzp->tbp)
285 list_for_each_entry(pos, &thermal_cdev_list, node) {
286 for (i = 0; i < tzp->num_tbps; i++) {
287 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
289 if (tzp->tbp[i].match(tz, pos))
291 tzp->tbp[i].cdev = pos;
292 __bind(tz, tzp->tbp[i].trip_mask, pos);
296 mutex_unlock(&thermal_list_lock);
299 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
303 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
304 round_jiffies(msecs_to_jiffies(delay)));
306 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
307 msecs_to_jiffies(delay));
309 cancel_delayed_work(&tz->poll_queue);
312 static void monitor_thermal_zone(struct thermal_zone_device *tz)
314 mutex_lock(&tz->lock);
317 thermal_zone_device_set_polling(tz, tz->passive_delay);
318 else if (tz->polling_delay)
319 thermal_zone_device_set_polling(tz, tz->polling_delay);
321 thermal_zone_device_set_polling(tz, 0);
323 mutex_unlock(&tz->lock);
326 static void handle_non_critical_trips(struct thermal_zone_device *tz,
327 int trip, enum thermal_trip_type trip_type)
330 tz->governor->throttle(tz, trip);
333 static void handle_critical_trips(struct thermal_zone_device *tz,
334 int trip, enum thermal_trip_type trip_type)
338 tz->ops->get_trip_temp(tz, trip, &trip_temp);
340 /* If we have not crossed the trip_temp, we do not care. */
341 if (tz->temperature < trip_temp)
345 tz->ops->notify(tz, trip, trip_type);
347 if (trip_type == THERMAL_TRIP_CRITICAL) {
348 dev_emerg(&tz->device,
349 "critical temperature reached(%d C),shutting down\n",
350 tz->temperature / 1000);
351 orderly_poweroff(true);
355 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
357 enum thermal_trip_type type;
359 tz->ops->get_trip_type(tz, trip, &type);
361 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
362 handle_critical_trips(tz, trip, type);
364 handle_non_critical_trips(tz, trip, type);
366 * Alright, we handled this trip successfully.
367 * So, start monitoring again.
369 monitor_thermal_zone(tz);
372 static int thermal_zone_get_temp(struct thermal_zone_device *tz,
376 #ifdef CONFIG_THERMAL_EMULATION
378 unsigned long crit_temp = -1UL;
379 enum thermal_trip_type type;
382 mutex_lock(&tz->lock);
384 ret = tz->ops->get_temp(tz, temp);
385 #ifdef CONFIG_THERMAL_EMULATION
386 if (!tz->emul_temperature)
389 for (count = 0; count < tz->trips; count++) {
390 ret = tz->ops->get_trip_type(tz, count, &type);
391 if (!ret && type == THERMAL_TRIP_CRITICAL) {
392 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
400 if (*temp < crit_temp)
401 *temp = tz->emul_temperature;
404 mutex_unlock(&tz->lock);
408 static void update_temperature(struct thermal_zone_device *tz)
413 ret = thermal_zone_get_temp(tz, &temp);
415 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
420 mutex_lock(&tz->lock);
421 tz->last_temperature = tz->temperature;
422 tz->temperature = temp;
423 mutex_unlock(&tz->lock);
426 void thermal_zone_device_update(struct thermal_zone_device *tz)
430 update_temperature(tz);
432 for (count = 0; count < tz->trips; count++)
433 handle_thermal_trip(tz, count);
435 EXPORT_SYMBOL(thermal_zone_device_update);
437 static void thermal_zone_device_check(struct work_struct *work)
439 struct thermal_zone_device *tz = container_of(work, struct
442 thermal_zone_device_update(tz);
445 /* sys I/F for thermal zone */
447 #define to_thermal_zone(_dev) \
448 container_of(_dev, struct thermal_zone_device, device)
451 type_show(struct device *dev, struct device_attribute *attr, char *buf)
453 struct thermal_zone_device *tz = to_thermal_zone(dev);
455 return sprintf(buf, "%s\n", tz->type);
459 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
461 struct thermal_zone_device *tz = to_thermal_zone(dev);
465 ret = thermal_zone_get_temp(tz, &temperature);
470 return sprintf(buf, "%ld\n", temperature);
474 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
476 struct thermal_zone_device *tz = to_thermal_zone(dev);
477 enum thermal_device_mode mode;
480 if (!tz->ops->get_mode)
483 result = tz->ops->get_mode(tz, &mode);
487 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
492 mode_store(struct device *dev, struct device_attribute *attr,
493 const char *buf, size_t count)
495 struct thermal_zone_device *tz = to_thermal_zone(dev);
498 if (!tz->ops->set_mode)
501 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
502 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
503 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
504 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
515 trip_point_type_show(struct device *dev, struct device_attribute *attr,
518 struct thermal_zone_device *tz = to_thermal_zone(dev);
519 enum thermal_trip_type type;
522 if (!tz->ops->get_trip_type)
525 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
528 result = tz->ops->get_trip_type(tz, trip, &type);
533 case THERMAL_TRIP_CRITICAL:
534 return sprintf(buf, "critical\n");
535 case THERMAL_TRIP_HOT:
536 return sprintf(buf, "hot\n");
537 case THERMAL_TRIP_PASSIVE:
538 return sprintf(buf, "passive\n");
539 case THERMAL_TRIP_ACTIVE:
540 return sprintf(buf, "active\n");
542 return sprintf(buf, "unknown\n");
547 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
548 const char *buf, size_t count)
550 struct thermal_zone_device *tz = to_thermal_zone(dev);
552 unsigned long temperature;
554 if (!tz->ops->set_trip_temp)
557 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
560 if (kstrtoul(buf, 10, &temperature))
563 ret = tz->ops->set_trip_temp(tz, trip, temperature);
565 return ret ? ret : count;
569 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
572 struct thermal_zone_device *tz = to_thermal_zone(dev);
576 if (!tz->ops->get_trip_temp)
579 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
582 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
587 return sprintf(buf, "%ld\n", temperature);
591 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
592 const char *buf, size_t count)
594 struct thermal_zone_device *tz = to_thermal_zone(dev);
596 unsigned long temperature;
598 if (!tz->ops->set_trip_hyst)
601 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
604 if (kstrtoul(buf, 10, &temperature))
608 * We are not doing any check on the 'temperature' value
609 * here. The driver implementing 'set_trip_hyst' has to
612 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
614 return ret ? ret : count;
618 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
621 struct thermal_zone_device *tz = to_thermal_zone(dev);
623 unsigned long temperature;
625 if (!tz->ops->get_trip_hyst)
628 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
631 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
633 return ret ? ret : sprintf(buf, "%ld\n", temperature);
637 passive_store(struct device *dev, struct device_attribute *attr,
638 const char *buf, size_t count)
640 struct thermal_zone_device *tz = to_thermal_zone(dev);
641 struct thermal_cooling_device *cdev = NULL;
644 if (!sscanf(buf, "%d\n", &state))
647 /* sanity check: values below 1000 millicelcius don't make sense
648 * and can cause the system to go into a thermal heart attack
650 if (state && state < 1000)
653 if (state && !tz->forced_passive) {
654 mutex_lock(&thermal_list_lock);
655 list_for_each_entry(cdev, &thermal_cdev_list, node) {
656 if (!strncmp("Processor", cdev->type,
657 sizeof("Processor")))
658 thermal_zone_bind_cooling_device(tz,
659 THERMAL_TRIPS_NONE, cdev,
663 mutex_unlock(&thermal_list_lock);
664 if (!tz->passive_delay)
665 tz->passive_delay = 1000;
666 } else if (!state && tz->forced_passive) {
667 mutex_lock(&thermal_list_lock);
668 list_for_each_entry(cdev, &thermal_cdev_list, node) {
669 if (!strncmp("Processor", cdev->type,
670 sizeof("Processor")))
671 thermal_zone_unbind_cooling_device(tz,
675 mutex_unlock(&thermal_list_lock);
676 tz->passive_delay = 0;
679 tz->forced_passive = state;
681 thermal_zone_device_update(tz);
687 passive_show(struct device *dev, struct device_attribute *attr,
690 struct thermal_zone_device *tz = to_thermal_zone(dev);
692 return sprintf(buf, "%d\n", tz->forced_passive);
696 policy_store(struct device *dev, struct device_attribute *attr,
697 const char *buf, size_t count)
700 struct thermal_zone_device *tz = to_thermal_zone(dev);
701 struct thermal_governor *gov;
703 mutex_lock(&thermal_governor_lock);
705 gov = __find_governor(buf);
713 mutex_unlock(&thermal_governor_lock);
718 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
720 struct thermal_zone_device *tz = to_thermal_zone(dev);
722 return sprintf(buf, "%s\n", tz->governor->name);
725 #ifdef CONFIG_THERMAL_EMULATION
727 emul_temp_store(struct device *dev, struct device_attribute *attr,
728 const char *buf, size_t count)
730 struct thermal_zone_device *tz = to_thermal_zone(dev);
732 unsigned long temperature;
734 if (kstrtoul(buf, 10, &temperature))
737 if (!tz->ops->set_emul_temp) {
738 mutex_lock(&tz->lock);
739 tz->emul_temperature = temperature;
740 mutex_unlock(&tz->lock);
742 ret = tz->ops->set_emul_temp(tz, temperature);
745 return ret ? ret : count;
747 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
748 #endif/*CONFIG_THERMAL_EMULATION*/
750 static DEVICE_ATTR(type, 0444, type_show, NULL);
751 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
752 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
753 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
754 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
756 /* sys I/F for cooling device */
757 #define to_cooling_device(_dev) \
758 container_of(_dev, struct thermal_cooling_device, device)
761 thermal_cooling_device_type_show(struct device *dev,
762 struct device_attribute *attr, char *buf)
764 struct thermal_cooling_device *cdev = to_cooling_device(dev);
766 return sprintf(buf, "%s\n", cdev->type);
770 thermal_cooling_device_max_state_show(struct device *dev,
771 struct device_attribute *attr, char *buf)
773 struct thermal_cooling_device *cdev = to_cooling_device(dev);
777 ret = cdev->ops->get_max_state(cdev, &state);
780 return sprintf(buf, "%ld\n", state);
784 thermal_cooling_device_cur_state_show(struct device *dev,
785 struct device_attribute *attr, char *buf)
787 struct thermal_cooling_device *cdev = to_cooling_device(dev);
791 ret = cdev->ops->get_cur_state(cdev, &state);
794 return sprintf(buf, "%ld\n", state);
798 thermal_cooling_device_cur_state_store(struct device *dev,
799 struct device_attribute *attr,
800 const char *buf, size_t count)
802 struct thermal_cooling_device *cdev = to_cooling_device(dev);
806 if (!sscanf(buf, "%ld\n", &state))
812 result = cdev->ops->set_cur_state(cdev, state);
818 static struct device_attribute dev_attr_cdev_type =
819 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
820 static DEVICE_ATTR(max_state, 0444,
821 thermal_cooling_device_max_state_show, NULL);
822 static DEVICE_ATTR(cur_state, 0644,
823 thermal_cooling_device_cur_state_show,
824 thermal_cooling_device_cur_state_store);
827 thermal_cooling_device_trip_point_show(struct device *dev,
828 struct device_attribute *attr, char *buf)
830 struct thermal_instance *instance;
833 container_of(attr, struct thermal_instance, attr);
835 if (instance->trip == THERMAL_TRIPS_NONE)
836 return sprintf(buf, "-1\n");
838 return sprintf(buf, "%d\n", instance->trip);
841 /* Device management */
843 #if defined(CONFIG_THERMAL_HWMON)
846 #include <linux/hwmon.h>
848 /* thermal zone devices with the same type share one hwmon device */
849 struct thermal_hwmon_device {
850 char type[THERMAL_NAME_LENGTH];
851 struct device *device;
853 struct list_head tz_list;
854 struct list_head node;
857 struct thermal_hwmon_attr {
858 struct device_attribute attr;
862 /* one temperature input for each thermal zone */
863 struct thermal_hwmon_temp {
864 struct list_head hwmon_node;
865 struct thermal_zone_device *tz;
866 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
867 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
870 static LIST_HEAD(thermal_hwmon_list);
873 name_show(struct device *dev, struct device_attribute *attr, char *buf)
875 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
876 return sprintf(buf, "%s\n", hwmon->type);
878 static DEVICE_ATTR(name, 0444, name_show, NULL);
881 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
885 struct thermal_hwmon_attr *hwmon_attr
886 = container_of(attr, struct thermal_hwmon_attr, attr);
887 struct thermal_hwmon_temp *temp
888 = container_of(hwmon_attr, struct thermal_hwmon_temp,
890 struct thermal_zone_device *tz = temp->tz;
892 ret = thermal_zone_get_temp(tz, &temperature);
897 return sprintf(buf, "%ld\n", temperature);
901 temp_crit_show(struct device *dev, struct device_attribute *attr,
904 struct thermal_hwmon_attr *hwmon_attr
905 = container_of(attr, struct thermal_hwmon_attr, attr);
906 struct thermal_hwmon_temp *temp
907 = container_of(hwmon_attr, struct thermal_hwmon_temp,
909 struct thermal_zone_device *tz = temp->tz;
913 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
917 return sprintf(buf, "%ld\n", temperature);
921 static struct thermal_hwmon_device *
922 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
924 struct thermal_hwmon_device *hwmon;
926 mutex_lock(&thermal_list_lock);
927 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
928 if (!strcmp(hwmon->type, tz->type)) {
929 mutex_unlock(&thermal_list_lock);
932 mutex_unlock(&thermal_list_lock);
937 /* Find the temperature input matching a given thermal zone */
938 static struct thermal_hwmon_temp *
939 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
940 const struct thermal_zone_device *tz)
942 struct thermal_hwmon_temp *temp;
944 mutex_lock(&thermal_list_lock);
945 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
946 if (temp->tz == tz) {
947 mutex_unlock(&thermal_list_lock);
950 mutex_unlock(&thermal_list_lock);
956 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
958 struct thermal_hwmon_device *hwmon;
959 struct thermal_hwmon_temp *temp;
960 int new_hwmon_device = 1;
963 hwmon = thermal_hwmon_lookup_by_type(tz);
965 new_hwmon_device = 0;
966 goto register_sys_interface;
969 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
973 INIT_LIST_HEAD(&hwmon->tz_list);
974 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
975 hwmon->device = hwmon_device_register(NULL);
976 if (IS_ERR(hwmon->device)) {
977 result = PTR_ERR(hwmon->device);
980 dev_set_drvdata(hwmon->device, hwmon);
981 result = device_create_file(hwmon->device, &dev_attr_name);
985 register_sys_interface:
986 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
989 goto unregister_name;
995 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
996 "temp%d_input", hwmon->count);
997 temp->temp_input.attr.attr.name = temp->temp_input.name;
998 temp->temp_input.attr.attr.mode = 0444;
999 temp->temp_input.attr.show = temp_input_show;
1000 sysfs_attr_init(&temp->temp_input.attr.attr);
1001 result = device_create_file(hwmon->device, &temp->temp_input.attr);
1005 if (tz->ops->get_crit_temp) {
1006 unsigned long temperature;
1007 if (!tz->ops->get_crit_temp(tz, &temperature)) {
1008 snprintf(temp->temp_crit.name,
1009 sizeof(temp->temp_crit.name),
1010 "temp%d_crit", hwmon->count);
1011 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1012 temp->temp_crit.attr.attr.mode = 0444;
1013 temp->temp_crit.attr.show = temp_crit_show;
1014 sysfs_attr_init(&temp->temp_crit.attr.attr);
1015 result = device_create_file(hwmon->device,
1016 &temp->temp_crit.attr);
1018 goto unregister_input;
1022 mutex_lock(&thermal_list_lock);
1023 if (new_hwmon_device)
1024 list_add_tail(&hwmon->node, &thermal_hwmon_list);
1025 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
1026 mutex_unlock(&thermal_list_lock);
1031 device_remove_file(hwmon->device, &temp->temp_input.attr);
1035 if (new_hwmon_device) {
1036 device_remove_file(hwmon->device, &dev_attr_name);
1037 hwmon_device_unregister(hwmon->device);
1040 if (new_hwmon_device)
1047 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1049 struct thermal_hwmon_device *hwmon;
1050 struct thermal_hwmon_temp *temp;
1052 hwmon = thermal_hwmon_lookup_by_type(tz);
1053 if (unlikely(!hwmon)) {
1054 /* Should never happen... */
1055 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1059 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1060 if (unlikely(!temp)) {
1061 /* Should never happen... */
1062 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1066 device_remove_file(hwmon->device, &temp->temp_input.attr);
1067 if (tz->ops->get_crit_temp)
1068 device_remove_file(hwmon->device, &temp->temp_crit.attr);
1070 mutex_lock(&thermal_list_lock);
1071 list_del(&temp->hwmon_node);
1073 if (!list_empty(&hwmon->tz_list)) {
1074 mutex_unlock(&thermal_list_lock);
1077 list_del(&hwmon->node);
1078 mutex_unlock(&thermal_list_lock);
1080 device_remove_file(hwmon->device, &dev_attr_name);
1081 hwmon_device_unregister(hwmon->device);
1086 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1092 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1098 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
1099 * @tz: thermal zone device
1100 * @trip: indicates which trip point the cooling devices is
1101 * associated with in this thermal zone.
1102 * @cdev: thermal cooling device
1104 * This function is usually called in the thermal zone device .bind callback.
1106 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1108 struct thermal_cooling_device *cdev,
1109 unsigned long upper, unsigned long lower)
1111 struct thermal_instance *dev;
1112 struct thermal_instance *pos;
1113 struct thermal_zone_device *pos1;
1114 struct thermal_cooling_device *pos2;
1115 unsigned long max_state;
1118 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1121 list_for_each_entry(pos1, &thermal_tz_list, node) {
1125 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1130 if (tz != pos1 || cdev != pos2)
1133 cdev->ops->get_max_state(cdev, &max_state);
1135 /* lower default 0, upper default max_state */
1136 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1137 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1139 if (lower > upper || upper > max_state)
1143 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1151 dev->target = THERMAL_NO_TARGET;
1153 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1157 sprintf(dev->name, "cdev%d", dev->id);
1159 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1163 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1164 sysfs_attr_init(&dev->attr.attr);
1165 dev->attr.attr.name = dev->attr_name;
1166 dev->attr.attr.mode = 0444;
1167 dev->attr.show = thermal_cooling_device_trip_point_show;
1168 result = device_create_file(&tz->device, &dev->attr);
1170 goto remove_symbol_link;
1172 mutex_lock(&tz->lock);
1173 mutex_lock(&cdev->lock);
1174 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1175 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1180 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1181 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1183 mutex_unlock(&cdev->lock);
1184 mutex_unlock(&tz->lock);
1189 device_remove_file(&tz->device, &dev->attr);
1191 sysfs_remove_link(&tz->device.kobj, dev->name);
1193 release_idr(&tz->idr, &tz->lock, dev->id);
1198 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1201 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
1202 * @tz: thermal zone device
1203 * @trip: indicates which trip point the cooling devices is
1204 * associated with in this thermal zone.
1205 * @cdev: thermal cooling device
1207 * This function is usually called in the thermal zone device .unbind callback.
1209 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1211 struct thermal_cooling_device *cdev)
1213 struct thermal_instance *pos, *next;
1215 mutex_lock(&tz->lock);
1216 mutex_lock(&cdev->lock);
1217 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1218 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1219 list_del(&pos->tz_node);
1220 list_del(&pos->cdev_node);
1221 mutex_unlock(&cdev->lock);
1222 mutex_unlock(&tz->lock);
1226 mutex_unlock(&cdev->lock);
1227 mutex_unlock(&tz->lock);
1232 device_remove_file(&tz->device, &pos->attr);
1233 sysfs_remove_link(&tz->device.kobj, pos->name);
1234 release_idr(&tz->idr, &tz->lock, pos->id);
1238 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1240 static void thermal_release(struct device *dev)
1242 struct thermal_zone_device *tz;
1243 struct thermal_cooling_device *cdev;
1245 if (!strncmp(dev_name(dev), "thermal_zone",
1246 sizeof("thermal_zone") - 1)) {
1247 tz = to_thermal_zone(dev);
1250 cdev = to_cooling_device(dev);
1255 static struct class thermal_class = {
1257 .dev_release = thermal_release,
1261 * thermal_cooling_device_register - register a new thermal cooling device
1262 * @type: the thermal cooling device type.
1263 * @devdata: device private data.
1264 * @ops: standard thermal cooling devices callbacks.
1266 struct thermal_cooling_device *
1267 thermal_cooling_device_register(char *type, void *devdata,
1268 const struct thermal_cooling_device_ops *ops)
1270 struct thermal_cooling_device *cdev;
1273 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1274 return ERR_PTR(-EINVAL);
1276 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1277 !ops->set_cur_state)
1278 return ERR_PTR(-EINVAL);
1280 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1282 return ERR_PTR(-ENOMEM);
1284 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1287 return ERR_PTR(result);
1290 strcpy(cdev->type, type ? : "");
1291 mutex_init(&cdev->lock);
1292 INIT_LIST_HEAD(&cdev->thermal_instances);
1294 cdev->updated = true;
1295 cdev->device.class = &thermal_class;
1296 cdev->devdata = devdata;
1297 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1298 result = device_register(&cdev->device);
1300 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1302 return ERR_PTR(result);
1307 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1312 result = device_create_file(&cdev->device, &dev_attr_max_state);
1316 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1320 /* Add 'this' new cdev to the global cdev list */
1321 mutex_lock(&thermal_list_lock);
1322 list_add(&cdev->node, &thermal_cdev_list);
1323 mutex_unlock(&thermal_list_lock);
1325 /* Update binding information for 'this' new cdev */
1331 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1332 device_unregister(&cdev->device);
1333 return ERR_PTR(result);
1335 EXPORT_SYMBOL(thermal_cooling_device_register);
1338 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1339 * @cdev: the thermal cooling device to remove.
1341 * thermal_cooling_device_unregister() must be called when the device is no
1344 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1347 const struct thermal_zone_params *tzp;
1348 struct thermal_zone_device *tz;
1349 struct thermal_cooling_device *pos = NULL;
1354 mutex_lock(&thermal_list_lock);
1355 list_for_each_entry(pos, &thermal_cdev_list, node)
1359 /* thermal cooling device not found */
1360 mutex_unlock(&thermal_list_lock);
1363 list_del(&cdev->node);
1365 /* Unbind all thermal zones associated with 'this' cdev */
1366 list_for_each_entry(tz, &thermal_tz_list, node) {
1367 if (tz->ops->unbind) {
1368 tz->ops->unbind(tz, cdev);
1372 if (!tz->tzp || !tz->tzp->tbp)
1376 for (i = 0; i < tzp->num_tbps; i++) {
1377 if (tzp->tbp[i].cdev == cdev) {
1378 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1379 tzp->tbp[i].cdev = NULL;
1384 mutex_unlock(&thermal_list_lock);
1387 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1388 device_remove_file(&cdev->device, &dev_attr_max_state);
1389 device_remove_file(&cdev->device, &dev_attr_cur_state);
1391 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1392 device_unregister(&cdev->device);
1395 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1397 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1399 struct thermal_instance *instance;
1400 unsigned long target = 0;
1402 /* cooling device is updated*/
1406 mutex_lock(&cdev->lock);
1407 /* Make sure cdev enters the deepest cooling state */
1408 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1409 if (instance->target == THERMAL_NO_TARGET)
1411 if (instance->target > target)
1412 target = instance->target;
1414 mutex_unlock(&cdev->lock);
1415 cdev->ops->set_cur_state(cdev, target);
1416 cdev->updated = true;
1418 EXPORT_SYMBOL(thermal_cdev_update);
1421 * notify_thermal_framework - Sensor drivers use this API to notify framework
1422 * @tz: thermal zone device
1423 * @trip: indicates which trip point has been crossed
1425 * This function handles the trip events from sensor drivers. It starts
1426 * throttling the cooling devices according to the policy configured.
1427 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1428 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1429 * The throttling policy is based on the configured platform data; if no
1430 * platform data is provided, this uses the step_wise throttling policy.
1432 void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
1434 handle_thermal_trip(tz, trip);
1436 EXPORT_SYMBOL(notify_thermal_framework);
1439 * create_trip_attrs - create attributes for trip points
1440 * @tz: the thermal zone device
1441 * @mask: Writeable trip point bitmap.
1443 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1446 int size = sizeof(struct thermal_attr) * tz->trips;
1448 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1449 if (!tz->trip_type_attrs)
1452 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1453 if (!tz->trip_temp_attrs) {
1454 kfree(tz->trip_type_attrs);
1458 if (tz->ops->get_trip_hyst) {
1459 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1460 if (!tz->trip_hyst_attrs) {
1461 kfree(tz->trip_type_attrs);
1462 kfree(tz->trip_temp_attrs);
1468 for (indx = 0; indx < tz->trips; indx++) {
1469 /* create trip type attribute */
1470 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1471 "trip_point_%d_type", indx);
1473 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1474 tz->trip_type_attrs[indx].attr.attr.name =
1475 tz->trip_type_attrs[indx].name;
1476 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1477 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1479 device_create_file(&tz->device,
1480 &tz->trip_type_attrs[indx].attr);
1482 /* create trip temp attribute */
1483 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1484 "trip_point_%d_temp", indx);
1486 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1487 tz->trip_temp_attrs[indx].attr.attr.name =
1488 tz->trip_temp_attrs[indx].name;
1489 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1490 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1491 if (mask & (1 << indx)) {
1492 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1493 tz->trip_temp_attrs[indx].attr.store =
1494 trip_point_temp_store;
1497 device_create_file(&tz->device,
1498 &tz->trip_temp_attrs[indx].attr);
1500 /* create Optional trip hyst attribute */
1501 if (!tz->ops->get_trip_hyst)
1503 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1504 "trip_point_%d_hyst", indx);
1506 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1507 tz->trip_hyst_attrs[indx].attr.attr.name =
1508 tz->trip_hyst_attrs[indx].name;
1509 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1510 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1511 if (tz->ops->set_trip_hyst) {
1512 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1513 tz->trip_hyst_attrs[indx].attr.store =
1514 trip_point_hyst_store;
1517 device_create_file(&tz->device,
1518 &tz->trip_hyst_attrs[indx].attr);
1523 static void remove_trip_attrs(struct thermal_zone_device *tz)
1527 for (indx = 0; indx < tz->trips; indx++) {
1528 device_remove_file(&tz->device,
1529 &tz->trip_type_attrs[indx].attr);
1530 device_remove_file(&tz->device,
1531 &tz->trip_temp_attrs[indx].attr);
1532 if (tz->ops->get_trip_hyst)
1533 device_remove_file(&tz->device,
1534 &tz->trip_hyst_attrs[indx].attr);
1536 kfree(tz->trip_type_attrs);
1537 kfree(tz->trip_temp_attrs);
1538 kfree(tz->trip_hyst_attrs);
1542 * thermal_zone_device_register - register a new thermal zone device
1543 * @type: the thermal zone device type
1544 * @trips: the number of trip points the thermal zone support
1545 * @mask: a bit string indicating the writeablility of trip points
1546 * @devdata: private device data
1547 * @ops: standard thermal zone device callbacks
1548 * @tzp: thermal zone platform parameters
1549 * @passive_delay: number of milliseconds to wait between polls when
1550 * performing passive cooling
1551 * @polling_delay: number of milliseconds to wait between polls when checking
1552 * whether trip points have been crossed (0 for interrupt
1555 * thermal_zone_device_unregister() must be called when the device is no
1556 * longer needed. The passive cooling depends on the .get_trend() return value.
1558 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1559 int trips, int mask, void *devdata,
1560 const struct thermal_zone_device_ops *ops,
1561 const struct thermal_zone_params *tzp,
1562 int passive_delay, int polling_delay)
1564 struct thermal_zone_device *tz;
1565 enum thermal_trip_type trip_type;
1570 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1571 return ERR_PTR(-EINVAL);
1573 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1574 return ERR_PTR(-EINVAL);
1576 if (!ops || !ops->get_temp)
1577 return ERR_PTR(-EINVAL);
1579 if (trips > 0 && !ops->get_trip_type)
1580 return ERR_PTR(-EINVAL);
1582 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1584 return ERR_PTR(-ENOMEM);
1586 INIT_LIST_HEAD(&tz->thermal_instances);
1588 mutex_init(&tz->lock);
1589 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1592 return ERR_PTR(result);
1595 strcpy(tz->type, type ? : "");
1598 tz->device.class = &thermal_class;
1599 tz->devdata = devdata;
1601 tz->passive_delay = passive_delay;
1602 tz->polling_delay = polling_delay;
1604 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1605 result = device_register(&tz->device);
1607 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1609 return ERR_PTR(result);
1614 result = device_create_file(&tz->device, &dev_attr_type);
1619 result = device_create_file(&tz->device, &dev_attr_temp);
1623 if (ops->get_mode) {
1624 result = device_create_file(&tz->device, &dev_attr_mode);
1629 result = create_trip_attrs(tz, mask);
1633 for (count = 0; count < trips; count++) {
1634 tz->ops->get_trip_type(tz, count, &trip_type);
1635 if (trip_type == THERMAL_TRIP_PASSIVE)
1640 result = device_create_file(&tz->device, &dev_attr_passive);
1645 #ifdef CONFIG_THERMAL_EMULATION
1646 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1650 /* Create policy attribute */
1651 result = device_create_file(&tz->device, &dev_attr_policy);
1655 /* Update 'this' zone's governor information */
1656 mutex_lock(&thermal_governor_lock);
1659 tz->governor = __find_governor(tz->tzp->governor_name);
1661 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1663 mutex_unlock(&thermal_governor_lock);
1665 result = thermal_add_hwmon_sysfs(tz);
1669 mutex_lock(&thermal_list_lock);
1670 list_add_tail(&tz->node, &thermal_tz_list);
1671 mutex_unlock(&thermal_list_lock);
1673 /* Bind cooling devices for this zone */
1676 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1678 thermal_zone_device_update(tz);
1684 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1685 device_unregister(&tz->device);
1686 return ERR_PTR(result);
1688 EXPORT_SYMBOL(thermal_zone_device_register);
1691 * thermal_device_unregister - removes the registered thermal zone device
1692 * @tz: the thermal zone device to remove
1694 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1697 const struct thermal_zone_params *tzp;
1698 struct thermal_cooling_device *cdev;
1699 struct thermal_zone_device *pos = NULL;
1706 mutex_lock(&thermal_list_lock);
1707 list_for_each_entry(pos, &thermal_tz_list, node)
1711 /* thermal zone device not found */
1712 mutex_unlock(&thermal_list_lock);
1715 list_del(&tz->node);
1717 /* Unbind all cdevs associated with 'this' thermal zone */
1718 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1719 if (tz->ops->unbind) {
1720 tz->ops->unbind(tz, cdev);
1724 if (!tzp || !tzp->tbp)
1727 for (i = 0; i < tzp->num_tbps; i++) {
1728 if (tzp->tbp[i].cdev == cdev) {
1729 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1730 tzp->tbp[i].cdev = NULL;
1735 mutex_unlock(&thermal_list_lock);
1737 thermal_zone_device_set_polling(tz, 0);
1740 device_remove_file(&tz->device, &dev_attr_type);
1741 device_remove_file(&tz->device, &dev_attr_temp);
1742 if (tz->ops->get_mode)
1743 device_remove_file(&tz->device, &dev_attr_mode);
1744 device_remove_file(&tz->device, &dev_attr_policy);
1745 remove_trip_attrs(tz);
1746 tz->governor = NULL;
1748 thermal_remove_hwmon_sysfs(tz);
1749 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1750 idr_destroy(&tz->idr);
1751 mutex_destroy(&tz->lock);
1752 device_unregister(&tz->device);
1755 EXPORT_SYMBOL(thermal_zone_device_unregister);
1758 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1759 * @name: thermal zone name to fetch the temperature
1761 * When only one zone is found with the passed name, returns a reference to it.
1763 * Return: On success returns a reference to an unique thermal zone with
1764 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1765 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1767 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1769 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1770 unsigned int found = 0;
1775 mutex_lock(&thermal_list_lock);
1776 list_for_each_entry(pos, &thermal_tz_list, node)
1777 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1781 mutex_unlock(&thermal_list_lock);
1783 /* nothing has been found, thus an error code for it */
1785 ref = ERR_PTR(-ENODEV);
1787 /* Success only when an unique zone is found */
1788 ref = ERR_PTR(-EEXIST);
1793 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1796 static struct genl_family thermal_event_genl_family = {
1797 .id = GENL_ID_GENERATE,
1798 .name = THERMAL_GENL_FAMILY_NAME,
1799 .version = THERMAL_GENL_VERSION,
1800 .maxattr = THERMAL_GENL_ATTR_MAX,
1803 static struct genl_multicast_group thermal_event_mcgrp = {
1804 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1807 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1810 struct sk_buff *skb;
1811 struct nlattr *attr;
1812 struct thermal_genl_event *thermal_event;
1816 static unsigned int thermal_event_seqnum;
1821 /* allocate memory */
1822 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1825 skb = genlmsg_new(size, GFP_ATOMIC);
1829 /* add the genetlink message header */
1830 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1831 &thermal_event_genl_family, 0,
1832 THERMAL_GENL_CMD_EVENT);
1839 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1840 sizeof(struct thermal_genl_event));
1847 thermal_event = nla_data(attr);
1848 if (!thermal_event) {
1853 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1855 thermal_event->orig = tz->id;
1856 thermal_event->event = event;
1858 /* send multicast genetlink message */
1859 result = genlmsg_end(skb, msg_header);
1865 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1867 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1871 EXPORT_SYMBOL(thermal_generate_netlink_event);
1873 static int genetlink_init(void)
1877 result = genl_register_family(&thermal_event_genl_family);
1881 result = genl_register_mc_group(&thermal_event_genl_family,
1882 &thermal_event_mcgrp);
1884 genl_unregister_family(&thermal_event_genl_family);
1888 static void genetlink_exit(void)
1890 genl_unregister_family(&thermal_event_genl_family);
1892 #else /* !CONFIG_NET */
1893 static inline int genetlink_init(void) { return 0; }
1894 static inline void genetlink_exit(void) {}
1895 #endif /* !CONFIG_NET */
1897 static int __init thermal_register_governors(void)
1901 result = thermal_gov_step_wise_register();
1905 result = thermal_gov_fair_share_register();
1909 return thermal_gov_user_space_register();
1912 static void thermal_unregister_governors(void)
1914 thermal_gov_step_wise_unregister();
1915 thermal_gov_fair_share_unregister();
1916 thermal_gov_user_space_unregister();
1919 static int __init thermal_init(void)
1923 result = thermal_register_governors();
1927 result = class_register(&thermal_class);
1929 goto unregister_governors;
1931 result = genetlink_init();
1933 goto unregister_class;
1937 unregister_governors:
1938 thermal_unregister_governors();
1940 class_unregister(&thermal_class);
1942 idr_destroy(&thermal_tz_idr);
1943 idr_destroy(&thermal_cdev_idr);
1944 mutex_destroy(&thermal_idr_lock);
1945 mutex_destroy(&thermal_list_lock);
1946 mutex_destroy(&thermal_governor_lock);
1950 static void __exit thermal_exit(void)
1953 class_unregister(&thermal_class);
1954 thermal_unregister_governors();
1955 idr_destroy(&thermal_tz_idr);
1956 idr_destroy(&thermal_cdev_idr);
1957 mutex_destroy(&thermal_idr_lock);
1958 mutex_destroy(&thermal_list_lock);
1959 mutex_destroy(&thermal_governor_lock);
1962 fs_initcall(thermal_init);
1963 module_exit(thermal_exit);