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 v2");
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);
373 * thermal_zone_get_temp() - returns its the temperature of thermal zone
374 * @tz: a valid pointer to a struct thermal_zone_device
375 * @temp: a valid pointer to where to store the resulting temperature.
377 * When a valid thermal zone reference is passed, it will fetch its
378 * temperature and fill @temp.
380 * Return: On success returns 0, an error code otherwise
382 int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
385 #ifdef CONFIG_THERMAL_EMULATION
387 unsigned long crit_temp = -1UL;
388 enum thermal_trip_type type;
391 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
394 mutex_lock(&tz->lock);
396 ret = tz->ops->get_temp(tz, temp);
397 #ifdef CONFIG_THERMAL_EMULATION
398 if (!tz->emul_temperature)
401 for (count = 0; count < tz->trips; count++) {
402 ret = tz->ops->get_trip_type(tz, count, &type);
403 if (!ret && type == THERMAL_TRIP_CRITICAL) {
404 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
412 if (*temp < crit_temp)
413 *temp = tz->emul_temperature;
416 mutex_unlock(&tz->lock);
420 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
422 static void update_temperature(struct thermal_zone_device *tz)
427 ret = thermal_zone_get_temp(tz, &temp);
429 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
434 mutex_lock(&tz->lock);
435 tz->last_temperature = tz->temperature;
436 tz->temperature = temp;
437 mutex_unlock(&tz->lock);
440 void thermal_zone_device_update(struct thermal_zone_device *tz)
444 if (!tz->ops->get_temp)
447 update_temperature(tz);
449 for (count = 0; count < tz->trips; count++)
450 handle_thermal_trip(tz, count);
452 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
454 static void thermal_zone_device_check(struct work_struct *work)
456 struct thermal_zone_device *tz = container_of(work, struct
459 thermal_zone_device_update(tz);
462 /* sys I/F for thermal zone */
464 #define to_thermal_zone(_dev) \
465 container_of(_dev, struct thermal_zone_device, device)
468 type_show(struct device *dev, struct device_attribute *attr, char *buf)
470 struct thermal_zone_device *tz = to_thermal_zone(dev);
472 return sprintf(buf, "%s\n", tz->type);
476 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
478 struct thermal_zone_device *tz = to_thermal_zone(dev);
482 ret = thermal_zone_get_temp(tz, &temperature);
487 return sprintf(buf, "%ld\n", temperature);
491 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
493 struct thermal_zone_device *tz = to_thermal_zone(dev);
494 enum thermal_device_mode mode;
497 if (!tz->ops->get_mode)
500 result = tz->ops->get_mode(tz, &mode);
504 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
509 mode_store(struct device *dev, struct device_attribute *attr,
510 const char *buf, size_t count)
512 struct thermal_zone_device *tz = to_thermal_zone(dev);
515 if (!tz->ops->set_mode)
518 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
519 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
520 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
521 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
532 trip_point_type_show(struct device *dev, struct device_attribute *attr,
535 struct thermal_zone_device *tz = to_thermal_zone(dev);
536 enum thermal_trip_type type;
539 if (!tz->ops->get_trip_type)
542 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
545 result = tz->ops->get_trip_type(tz, trip, &type);
550 case THERMAL_TRIP_CRITICAL:
551 return sprintf(buf, "critical\n");
552 case THERMAL_TRIP_HOT:
553 return sprintf(buf, "hot\n");
554 case THERMAL_TRIP_PASSIVE:
555 return sprintf(buf, "passive\n");
556 case THERMAL_TRIP_ACTIVE:
557 return sprintf(buf, "active\n");
559 return sprintf(buf, "unknown\n");
564 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
565 const char *buf, size_t count)
567 struct thermal_zone_device *tz = to_thermal_zone(dev);
569 unsigned long temperature;
571 if (!tz->ops->set_trip_temp)
574 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
577 if (kstrtoul(buf, 10, &temperature))
580 ret = tz->ops->set_trip_temp(tz, trip, temperature);
582 return ret ? ret : count;
586 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
589 struct thermal_zone_device *tz = to_thermal_zone(dev);
593 if (!tz->ops->get_trip_temp)
596 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
599 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
604 return sprintf(buf, "%ld\n", temperature);
608 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
609 const char *buf, size_t count)
611 struct thermal_zone_device *tz = to_thermal_zone(dev);
613 unsigned long temperature;
615 if (!tz->ops->set_trip_hyst)
618 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
621 if (kstrtoul(buf, 10, &temperature))
625 * We are not doing any check on the 'temperature' value
626 * here. The driver implementing 'set_trip_hyst' has to
629 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
631 return ret ? ret : count;
635 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
638 struct thermal_zone_device *tz = to_thermal_zone(dev);
640 unsigned long temperature;
642 if (!tz->ops->get_trip_hyst)
645 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
648 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
650 return ret ? ret : sprintf(buf, "%ld\n", temperature);
654 passive_store(struct device *dev, struct device_attribute *attr,
655 const char *buf, size_t count)
657 struct thermal_zone_device *tz = to_thermal_zone(dev);
658 struct thermal_cooling_device *cdev = NULL;
661 if (!sscanf(buf, "%d\n", &state))
664 /* sanity check: values below 1000 millicelcius don't make sense
665 * and can cause the system to go into a thermal heart attack
667 if (state && state < 1000)
670 if (state && !tz->forced_passive) {
671 mutex_lock(&thermal_list_lock);
672 list_for_each_entry(cdev, &thermal_cdev_list, node) {
673 if (!strncmp("Processor", cdev->type,
674 sizeof("Processor")))
675 thermal_zone_bind_cooling_device(tz,
676 THERMAL_TRIPS_NONE, cdev,
680 mutex_unlock(&thermal_list_lock);
681 if (!tz->passive_delay)
682 tz->passive_delay = 1000;
683 } else if (!state && tz->forced_passive) {
684 mutex_lock(&thermal_list_lock);
685 list_for_each_entry(cdev, &thermal_cdev_list, node) {
686 if (!strncmp("Processor", cdev->type,
687 sizeof("Processor")))
688 thermal_zone_unbind_cooling_device(tz,
692 mutex_unlock(&thermal_list_lock);
693 tz->passive_delay = 0;
696 tz->forced_passive = state;
698 thermal_zone_device_update(tz);
704 passive_show(struct device *dev, struct device_attribute *attr,
707 struct thermal_zone_device *tz = to_thermal_zone(dev);
709 return sprintf(buf, "%d\n", tz->forced_passive);
713 policy_store(struct device *dev, struct device_attribute *attr,
714 const char *buf, size_t count)
717 struct thermal_zone_device *tz = to_thermal_zone(dev);
718 struct thermal_governor *gov;
720 mutex_lock(&thermal_governor_lock);
722 gov = __find_governor(buf);
730 mutex_unlock(&thermal_governor_lock);
735 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
737 struct thermal_zone_device *tz = to_thermal_zone(dev);
739 return sprintf(buf, "%s\n", tz->governor->name);
742 #ifdef CONFIG_THERMAL_EMULATION
744 emul_temp_store(struct device *dev, struct device_attribute *attr,
745 const char *buf, size_t count)
747 struct thermal_zone_device *tz = to_thermal_zone(dev);
749 unsigned long temperature;
751 if (kstrtoul(buf, 10, &temperature))
754 if (!tz->ops->set_emul_temp) {
755 mutex_lock(&tz->lock);
756 tz->emul_temperature = temperature;
757 mutex_unlock(&tz->lock);
759 ret = tz->ops->set_emul_temp(tz, temperature);
762 return ret ? ret : count;
764 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
765 #endif/*CONFIG_THERMAL_EMULATION*/
767 static DEVICE_ATTR(type, 0444, type_show, NULL);
768 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
769 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
770 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
771 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
773 /* sys I/F for cooling device */
774 #define to_cooling_device(_dev) \
775 container_of(_dev, struct thermal_cooling_device, device)
778 thermal_cooling_device_type_show(struct device *dev,
779 struct device_attribute *attr, char *buf)
781 struct thermal_cooling_device *cdev = to_cooling_device(dev);
783 return sprintf(buf, "%s\n", cdev->type);
787 thermal_cooling_device_max_state_show(struct device *dev,
788 struct device_attribute *attr, char *buf)
790 struct thermal_cooling_device *cdev = to_cooling_device(dev);
794 ret = cdev->ops->get_max_state(cdev, &state);
797 return sprintf(buf, "%ld\n", state);
801 thermal_cooling_device_cur_state_show(struct device *dev,
802 struct device_attribute *attr, char *buf)
804 struct thermal_cooling_device *cdev = to_cooling_device(dev);
808 ret = cdev->ops->get_cur_state(cdev, &state);
811 return sprintf(buf, "%ld\n", state);
815 thermal_cooling_device_cur_state_store(struct device *dev,
816 struct device_attribute *attr,
817 const char *buf, size_t count)
819 struct thermal_cooling_device *cdev = to_cooling_device(dev);
823 if (!sscanf(buf, "%ld\n", &state))
829 result = cdev->ops->set_cur_state(cdev, state);
835 static struct device_attribute dev_attr_cdev_type =
836 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
837 static DEVICE_ATTR(max_state, 0444,
838 thermal_cooling_device_max_state_show, NULL);
839 static DEVICE_ATTR(cur_state, 0644,
840 thermal_cooling_device_cur_state_show,
841 thermal_cooling_device_cur_state_store);
844 thermal_cooling_device_trip_point_show(struct device *dev,
845 struct device_attribute *attr, char *buf)
847 struct thermal_instance *instance;
850 container_of(attr, struct thermal_instance, attr);
852 if (instance->trip == THERMAL_TRIPS_NONE)
853 return sprintf(buf, "-1\n");
855 return sprintf(buf, "%d\n", instance->trip);
858 /* Device management */
860 #if defined(CONFIG_THERMAL_HWMON)
863 #include <linux/hwmon.h>
865 /* thermal zone devices with the same type share one hwmon device */
866 struct thermal_hwmon_device {
867 char type[THERMAL_NAME_LENGTH];
868 struct device *device;
870 struct list_head tz_list;
871 struct list_head node;
874 struct thermal_hwmon_attr {
875 struct device_attribute attr;
879 /* one temperature input for each thermal zone */
880 struct thermal_hwmon_temp {
881 struct list_head hwmon_node;
882 struct thermal_zone_device *tz;
883 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
884 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
887 static LIST_HEAD(thermal_hwmon_list);
890 name_show(struct device *dev, struct device_attribute *attr, char *buf)
892 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
893 return sprintf(buf, "%s\n", hwmon->type);
895 static DEVICE_ATTR(name, 0444, name_show, NULL);
898 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
902 struct thermal_hwmon_attr *hwmon_attr
903 = container_of(attr, struct thermal_hwmon_attr, attr);
904 struct thermal_hwmon_temp *temp
905 = container_of(hwmon_attr, struct thermal_hwmon_temp,
907 struct thermal_zone_device *tz = temp->tz;
909 ret = thermal_zone_get_temp(tz, &temperature);
914 return sprintf(buf, "%ld\n", temperature);
918 temp_crit_show(struct device *dev, struct device_attribute *attr,
921 struct thermal_hwmon_attr *hwmon_attr
922 = container_of(attr, struct thermal_hwmon_attr, attr);
923 struct thermal_hwmon_temp *temp
924 = container_of(hwmon_attr, struct thermal_hwmon_temp,
926 struct thermal_zone_device *tz = temp->tz;
930 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
934 return sprintf(buf, "%ld\n", temperature);
938 static struct thermal_hwmon_device *
939 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
941 struct thermal_hwmon_device *hwmon;
943 mutex_lock(&thermal_list_lock);
944 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
945 if (!strcmp(hwmon->type, tz->type)) {
946 mutex_unlock(&thermal_list_lock);
949 mutex_unlock(&thermal_list_lock);
954 /* Find the temperature input matching a given thermal zone */
955 static struct thermal_hwmon_temp *
956 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
957 const struct thermal_zone_device *tz)
959 struct thermal_hwmon_temp *temp;
961 mutex_lock(&thermal_list_lock);
962 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
963 if (temp->tz == tz) {
964 mutex_unlock(&thermal_list_lock);
967 mutex_unlock(&thermal_list_lock);
973 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
975 struct thermal_hwmon_device *hwmon;
976 struct thermal_hwmon_temp *temp;
977 int new_hwmon_device = 1;
980 hwmon = thermal_hwmon_lookup_by_type(tz);
982 new_hwmon_device = 0;
983 goto register_sys_interface;
986 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
990 INIT_LIST_HEAD(&hwmon->tz_list);
991 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
992 hwmon->device = hwmon_device_register(NULL);
993 if (IS_ERR(hwmon->device)) {
994 result = PTR_ERR(hwmon->device);
997 dev_set_drvdata(hwmon->device, hwmon);
998 result = device_create_file(hwmon->device, &dev_attr_name);
1002 register_sys_interface:
1003 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
1006 goto unregister_name;
1012 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
1013 "temp%d_input", hwmon->count);
1014 temp->temp_input.attr.attr.name = temp->temp_input.name;
1015 temp->temp_input.attr.attr.mode = 0444;
1016 temp->temp_input.attr.show = temp_input_show;
1017 sysfs_attr_init(&temp->temp_input.attr.attr);
1018 result = device_create_file(hwmon->device, &temp->temp_input.attr);
1022 if (tz->ops->get_crit_temp) {
1023 unsigned long temperature;
1024 if (!tz->ops->get_crit_temp(tz, &temperature)) {
1025 snprintf(temp->temp_crit.name,
1026 sizeof(temp->temp_crit.name),
1027 "temp%d_crit", hwmon->count);
1028 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1029 temp->temp_crit.attr.attr.mode = 0444;
1030 temp->temp_crit.attr.show = temp_crit_show;
1031 sysfs_attr_init(&temp->temp_crit.attr.attr);
1032 result = device_create_file(hwmon->device,
1033 &temp->temp_crit.attr);
1035 goto unregister_input;
1039 mutex_lock(&thermal_list_lock);
1040 if (new_hwmon_device)
1041 list_add_tail(&hwmon->node, &thermal_hwmon_list);
1042 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
1043 mutex_unlock(&thermal_list_lock);
1048 device_remove_file(hwmon->device, &temp->temp_input.attr);
1052 if (new_hwmon_device) {
1053 device_remove_file(hwmon->device, &dev_attr_name);
1054 hwmon_device_unregister(hwmon->device);
1057 if (new_hwmon_device)
1064 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1066 struct thermal_hwmon_device *hwmon;
1067 struct thermal_hwmon_temp *temp;
1069 hwmon = thermal_hwmon_lookup_by_type(tz);
1070 if (unlikely(!hwmon)) {
1071 /* Should never happen... */
1072 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1076 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1077 if (unlikely(!temp)) {
1078 /* Should never happen... */
1079 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1083 device_remove_file(hwmon->device, &temp->temp_input.attr);
1084 if (tz->ops->get_crit_temp)
1085 device_remove_file(hwmon->device, &temp->temp_crit.attr);
1087 mutex_lock(&thermal_list_lock);
1088 list_del(&temp->hwmon_node);
1090 if (!list_empty(&hwmon->tz_list)) {
1091 mutex_unlock(&thermal_list_lock);
1094 list_del(&hwmon->node);
1095 mutex_unlock(&thermal_list_lock);
1097 device_remove_file(hwmon->device, &dev_attr_name);
1098 hwmon_device_unregister(hwmon->device);
1103 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1109 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1115 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1116 * @tz: pointer to struct thermal_zone_device
1117 * @trip: indicates which trip point the cooling devices is
1118 * associated with in this thermal zone.
1119 * @cdev: pointer to struct thermal_cooling_device
1120 * @upper: the Maximum cooling state for this trip point.
1121 * THERMAL_NO_LIMIT means no upper limit,
1122 * and the cooling device can be in max_state.
1123 * @lower: the Minimum cooling state can be used for this trip point.
1124 * THERMAL_NO_LIMIT means no lower limit,
1125 * and the cooling device can be in cooling state 0.
1127 * This interface function bind a thermal cooling device to the certain trip
1128 * point of a thermal zone device.
1129 * This function is usually called in the thermal zone device .bind callback.
1131 * Return: 0 on success, the proper error value otherwise.
1133 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1135 struct thermal_cooling_device *cdev,
1136 unsigned long upper, unsigned long lower)
1138 struct thermal_instance *dev;
1139 struct thermal_instance *pos;
1140 struct thermal_zone_device *pos1;
1141 struct thermal_cooling_device *pos2;
1142 unsigned long max_state;
1145 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1148 list_for_each_entry(pos1, &thermal_tz_list, node) {
1152 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1157 if (tz != pos1 || cdev != pos2)
1160 cdev->ops->get_max_state(cdev, &max_state);
1162 /* lower default 0, upper default max_state */
1163 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1164 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1166 if (lower > upper || upper > max_state)
1170 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1178 dev->target = THERMAL_NO_TARGET;
1180 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1184 sprintf(dev->name, "cdev%d", dev->id);
1186 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1190 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1191 sysfs_attr_init(&dev->attr.attr);
1192 dev->attr.attr.name = dev->attr_name;
1193 dev->attr.attr.mode = 0444;
1194 dev->attr.show = thermal_cooling_device_trip_point_show;
1195 result = device_create_file(&tz->device, &dev->attr);
1197 goto remove_symbol_link;
1199 mutex_lock(&tz->lock);
1200 mutex_lock(&cdev->lock);
1201 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1202 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1207 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1208 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1210 mutex_unlock(&cdev->lock);
1211 mutex_unlock(&tz->lock);
1216 device_remove_file(&tz->device, &dev->attr);
1218 sysfs_remove_link(&tz->device.kobj, dev->name);
1220 release_idr(&tz->idr, &tz->lock, dev->id);
1225 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
1228 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1230 * @tz: pointer to a struct thermal_zone_device.
1231 * @trip: indicates which trip point the cooling devices is
1232 * associated with in this thermal zone.
1233 * @cdev: pointer to a struct thermal_cooling_device.
1235 * This interface function unbind a thermal cooling device from the certain
1236 * trip point of a thermal zone device.
1237 * This function is usually called in the thermal zone device .unbind callback.
1239 * Return: 0 on success, the proper error value otherwise.
1241 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1243 struct thermal_cooling_device *cdev)
1245 struct thermal_instance *pos, *next;
1247 mutex_lock(&tz->lock);
1248 mutex_lock(&cdev->lock);
1249 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1250 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1251 list_del(&pos->tz_node);
1252 list_del(&pos->cdev_node);
1253 mutex_unlock(&cdev->lock);
1254 mutex_unlock(&tz->lock);
1258 mutex_unlock(&cdev->lock);
1259 mutex_unlock(&tz->lock);
1264 device_remove_file(&tz->device, &pos->attr);
1265 sysfs_remove_link(&tz->device.kobj, pos->name);
1266 release_idr(&tz->idr, &tz->lock, pos->id);
1270 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1272 static void thermal_release(struct device *dev)
1274 struct thermal_zone_device *tz;
1275 struct thermal_cooling_device *cdev;
1277 if (!strncmp(dev_name(dev), "thermal_zone",
1278 sizeof("thermal_zone") - 1)) {
1279 tz = to_thermal_zone(dev);
1282 cdev = to_cooling_device(dev);
1287 static struct class thermal_class = {
1289 .dev_release = thermal_release,
1293 * thermal_cooling_device_register() - register a new thermal cooling device
1294 * @type: the thermal cooling device type.
1295 * @devdata: device private data.
1296 * @ops: standard thermal cooling devices callbacks.
1298 * This interface function adds a new thermal cooling device (fan/processor/...)
1299 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1300 * to all the thermal zone devices registered at the same time.
1302 * Return: a pointer to the created struct thermal_cooling_device or an
1303 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1305 struct thermal_cooling_device *
1306 thermal_cooling_device_register(char *type, void *devdata,
1307 const struct thermal_cooling_device_ops *ops)
1309 struct thermal_cooling_device *cdev;
1312 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1313 return ERR_PTR(-EINVAL);
1315 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1316 !ops->set_cur_state)
1317 return ERR_PTR(-EINVAL);
1319 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1321 return ERR_PTR(-ENOMEM);
1323 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1326 return ERR_PTR(result);
1329 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1330 mutex_init(&cdev->lock);
1331 INIT_LIST_HEAD(&cdev->thermal_instances);
1333 cdev->updated = true;
1334 cdev->device.class = &thermal_class;
1335 cdev->devdata = devdata;
1336 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1337 result = device_register(&cdev->device);
1339 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1341 return ERR_PTR(result);
1346 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1351 result = device_create_file(&cdev->device, &dev_attr_max_state);
1355 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1359 /* Add 'this' new cdev to the global cdev list */
1360 mutex_lock(&thermal_list_lock);
1361 list_add(&cdev->node, &thermal_cdev_list);
1362 mutex_unlock(&thermal_list_lock);
1364 /* Update binding information for 'this' new cdev */
1370 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1371 device_unregister(&cdev->device);
1372 return ERR_PTR(result);
1374 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1377 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1378 * @cdev: the thermal cooling device to remove.
1380 * thermal_cooling_device_unregister() must be called when the device is no
1383 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1386 const struct thermal_zone_params *tzp;
1387 struct thermal_zone_device *tz;
1388 struct thermal_cooling_device *pos = NULL;
1393 mutex_lock(&thermal_list_lock);
1394 list_for_each_entry(pos, &thermal_cdev_list, node)
1398 /* thermal cooling device not found */
1399 mutex_unlock(&thermal_list_lock);
1402 list_del(&cdev->node);
1404 /* Unbind all thermal zones associated with 'this' cdev */
1405 list_for_each_entry(tz, &thermal_tz_list, node) {
1406 if (tz->ops->unbind) {
1407 tz->ops->unbind(tz, cdev);
1411 if (!tz->tzp || !tz->tzp->tbp)
1415 for (i = 0; i < tzp->num_tbps; i++) {
1416 if (tzp->tbp[i].cdev == cdev) {
1417 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1418 tzp->tbp[i].cdev = NULL;
1423 mutex_unlock(&thermal_list_lock);
1426 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1427 device_remove_file(&cdev->device, &dev_attr_max_state);
1428 device_remove_file(&cdev->device, &dev_attr_cur_state);
1430 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1431 device_unregister(&cdev->device);
1434 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1436 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1438 struct thermal_instance *instance;
1439 unsigned long target = 0;
1441 /* cooling device is updated*/
1445 mutex_lock(&cdev->lock);
1446 /* Make sure cdev enters the deepest cooling state */
1447 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1448 if (instance->target == THERMAL_NO_TARGET)
1450 if (instance->target > target)
1451 target = instance->target;
1453 mutex_unlock(&cdev->lock);
1454 cdev->ops->set_cur_state(cdev, target);
1455 cdev->updated = true;
1457 EXPORT_SYMBOL(thermal_cdev_update);
1460 * thermal_notify_framework - Sensor drivers use this API to notify framework
1461 * @tz: thermal zone device
1462 * @trip: indicates which trip point has been crossed
1464 * This function handles the trip events from sensor drivers. It starts
1465 * throttling the cooling devices according to the policy configured.
1466 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1467 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1468 * The throttling policy is based on the configured platform data; if no
1469 * platform data is provided, this uses the step_wise throttling policy.
1471 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1473 handle_thermal_trip(tz, trip);
1475 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1478 * create_trip_attrs() - create attributes for trip points
1479 * @tz: the thermal zone device
1480 * @mask: Writeable trip point bitmap.
1482 * helper function to instantiate sysfs entries for every trip
1483 * point and its properties of a struct thermal_zone_device.
1485 * Return: 0 on success, the proper error value otherwise.
1487 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1490 int size = sizeof(struct thermal_attr) * tz->trips;
1492 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1493 if (!tz->trip_type_attrs)
1496 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1497 if (!tz->trip_temp_attrs) {
1498 kfree(tz->trip_type_attrs);
1502 if (tz->ops->get_trip_hyst) {
1503 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1504 if (!tz->trip_hyst_attrs) {
1505 kfree(tz->trip_type_attrs);
1506 kfree(tz->trip_temp_attrs);
1512 for (indx = 0; indx < tz->trips; indx++) {
1513 /* create trip type attribute */
1514 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1515 "trip_point_%d_type", indx);
1517 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1518 tz->trip_type_attrs[indx].attr.attr.name =
1519 tz->trip_type_attrs[indx].name;
1520 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1521 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1523 device_create_file(&tz->device,
1524 &tz->trip_type_attrs[indx].attr);
1526 /* create trip temp attribute */
1527 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1528 "trip_point_%d_temp", indx);
1530 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1531 tz->trip_temp_attrs[indx].attr.attr.name =
1532 tz->trip_temp_attrs[indx].name;
1533 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1534 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1535 if (mask & (1 << indx)) {
1536 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1537 tz->trip_temp_attrs[indx].attr.store =
1538 trip_point_temp_store;
1541 device_create_file(&tz->device,
1542 &tz->trip_temp_attrs[indx].attr);
1544 /* create Optional trip hyst attribute */
1545 if (!tz->ops->get_trip_hyst)
1547 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1548 "trip_point_%d_hyst", indx);
1550 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1551 tz->trip_hyst_attrs[indx].attr.attr.name =
1552 tz->trip_hyst_attrs[indx].name;
1553 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1554 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1555 if (tz->ops->set_trip_hyst) {
1556 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1557 tz->trip_hyst_attrs[indx].attr.store =
1558 trip_point_hyst_store;
1561 device_create_file(&tz->device,
1562 &tz->trip_hyst_attrs[indx].attr);
1567 static void remove_trip_attrs(struct thermal_zone_device *tz)
1571 for (indx = 0; indx < tz->trips; indx++) {
1572 device_remove_file(&tz->device,
1573 &tz->trip_type_attrs[indx].attr);
1574 device_remove_file(&tz->device,
1575 &tz->trip_temp_attrs[indx].attr);
1576 if (tz->ops->get_trip_hyst)
1577 device_remove_file(&tz->device,
1578 &tz->trip_hyst_attrs[indx].attr);
1580 kfree(tz->trip_type_attrs);
1581 kfree(tz->trip_temp_attrs);
1582 kfree(tz->trip_hyst_attrs);
1586 * thermal_zone_device_register() - register a new thermal zone device
1587 * @type: the thermal zone device type
1588 * @trips: the number of trip points the thermal zone support
1589 * @mask: a bit string indicating the writeablility of trip points
1590 * @devdata: private device data
1591 * @ops: standard thermal zone device callbacks
1592 * @tzp: thermal zone platform parameters
1593 * @passive_delay: number of milliseconds to wait between polls when
1594 * performing passive cooling
1595 * @polling_delay: number of milliseconds to wait between polls when checking
1596 * whether trip points have been crossed (0 for interrupt
1599 * This interface function adds a new thermal zone device (sensor) to
1600 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1601 * thermal cooling devices registered at the same time.
1602 * thermal_zone_device_unregister() must be called when the device is no
1603 * longer needed. The passive cooling depends on the .get_trend() return value.
1605 * Return: a pointer to the created struct thermal_zone_device or an
1606 * in case of error, an ERR_PTR. Caller must check return value with
1607 * IS_ERR*() helpers.
1609 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1610 int trips, int mask, void *devdata,
1611 const struct thermal_zone_device_ops *ops,
1612 const struct thermal_zone_params *tzp,
1613 int passive_delay, int polling_delay)
1615 struct thermal_zone_device *tz;
1616 enum thermal_trip_type trip_type;
1621 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1622 return ERR_PTR(-EINVAL);
1624 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1625 return ERR_PTR(-EINVAL);
1628 return ERR_PTR(-EINVAL);
1630 if (trips > 0 && !ops->get_trip_type)
1631 return ERR_PTR(-EINVAL);
1633 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1635 return ERR_PTR(-ENOMEM);
1637 INIT_LIST_HEAD(&tz->thermal_instances);
1639 mutex_init(&tz->lock);
1640 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1643 return ERR_PTR(result);
1646 strlcpy(tz->type, type ? : "", sizeof(tz->type));
1649 tz->device.class = &thermal_class;
1650 tz->devdata = devdata;
1652 tz->passive_delay = passive_delay;
1653 tz->polling_delay = polling_delay;
1655 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1656 result = device_register(&tz->device);
1658 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1660 return ERR_PTR(result);
1665 result = device_create_file(&tz->device, &dev_attr_type);
1670 result = device_create_file(&tz->device, &dev_attr_temp);
1674 if (ops->get_mode) {
1675 result = device_create_file(&tz->device, &dev_attr_mode);
1680 result = create_trip_attrs(tz, mask);
1684 for (count = 0; count < trips; count++) {
1685 tz->ops->get_trip_type(tz, count, &trip_type);
1686 if (trip_type == THERMAL_TRIP_PASSIVE)
1691 result = device_create_file(&tz->device, &dev_attr_passive);
1696 #ifdef CONFIG_THERMAL_EMULATION
1697 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1701 /* Create policy attribute */
1702 result = device_create_file(&tz->device, &dev_attr_policy);
1706 /* Update 'this' zone's governor information */
1707 mutex_lock(&thermal_governor_lock);
1710 tz->governor = __find_governor(tz->tzp->governor_name);
1712 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1714 mutex_unlock(&thermal_governor_lock);
1716 result = thermal_add_hwmon_sysfs(tz);
1720 mutex_lock(&thermal_list_lock);
1721 list_add_tail(&tz->node, &thermal_tz_list);
1722 mutex_unlock(&thermal_list_lock);
1724 /* Bind cooling devices for this zone */
1727 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1729 if (!tz->ops->get_temp)
1730 thermal_zone_device_set_polling(tz, 0);
1732 thermal_zone_device_update(tz);
1738 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1739 device_unregister(&tz->device);
1740 return ERR_PTR(result);
1742 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1745 * thermal_device_unregister - removes the registered thermal zone device
1746 * @tz: the thermal zone device to remove
1748 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1751 const struct thermal_zone_params *tzp;
1752 struct thermal_cooling_device *cdev;
1753 struct thermal_zone_device *pos = NULL;
1760 mutex_lock(&thermal_list_lock);
1761 list_for_each_entry(pos, &thermal_tz_list, node)
1765 /* thermal zone device not found */
1766 mutex_unlock(&thermal_list_lock);
1769 list_del(&tz->node);
1771 /* Unbind all cdevs associated with 'this' thermal zone */
1772 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1773 if (tz->ops->unbind) {
1774 tz->ops->unbind(tz, cdev);
1778 if (!tzp || !tzp->tbp)
1781 for (i = 0; i < tzp->num_tbps; i++) {
1782 if (tzp->tbp[i].cdev == cdev) {
1783 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1784 tzp->tbp[i].cdev = NULL;
1789 mutex_unlock(&thermal_list_lock);
1791 thermal_zone_device_set_polling(tz, 0);
1794 device_remove_file(&tz->device, &dev_attr_type);
1795 device_remove_file(&tz->device, &dev_attr_temp);
1796 if (tz->ops->get_mode)
1797 device_remove_file(&tz->device, &dev_attr_mode);
1798 device_remove_file(&tz->device, &dev_attr_policy);
1799 remove_trip_attrs(tz);
1800 tz->governor = NULL;
1802 thermal_remove_hwmon_sysfs(tz);
1803 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1804 idr_destroy(&tz->idr);
1805 mutex_destroy(&tz->lock);
1806 device_unregister(&tz->device);
1809 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1812 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1813 * @name: thermal zone name to fetch the temperature
1815 * When only one zone is found with the passed name, returns a reference to it.
1817 * Return: On success returns a reference to an unique thermal zone with
1818 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1819 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1821 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1823 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1824 unsigned int found = 0;
1829 mutex_lock(&thermal_list_lock);
1830 list_for_each_entry(pos, &thermal_tz_list, node)
1831 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1835 mutex_unlock(&thermal_list_lock);
1837 /* nothing has been found, thus an error code for it */
1839 ref = ERR_PTR(-ENODEV);
1841 /* Success only when an unique zone is found */
1842 ref = ERR_PTR(-EEXIST);
1847 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1850 static struct genl_family thermal_event_genl_family = {
1851 .id = GENL_ID_GENERATE,
1852 .name = THERMAL_GENL_FAMILY_NAME,
1853 .version = THERMAL_GENL_VERSION,
1854 .maxattr = THERMAL_GENL_ATTR_MAX,
1857 static struct genl_multicast_group thermal_event_mcgrp = {
1858 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1861 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1864 struct sk_buff *skb;
1865 struct nlattr *attr;
1866 struct thermal_genl_event *thermal_event;
1870 static unsigned int thermal_event_seqnum;
1875 /* allocate memory */
1876 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1879 skb = genlmsg_new(size, GFP_ATOMIC);
1883 /* add the genetlink message header */
1884 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1885 &thermal_event_genl_family, 0,
1886 THERMAL_GENL_CMD_EVENT);
1893 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1894 sizeof(struct thermal_genl_event));
1901 thermal_event = nla_data(attr);
1902 if (!thermal_event) {
1907 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1909 thermal_event->orig = tz->id;
1910 thermal_event->event = event;
1912 /* send multicast genetlink message */
1913 result = genlmsg_end(skb, msg_header);
1919 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1921 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1925 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1927 static int genetlink_init(void)
1931 result = genl_register_family(&thermal_event_genl_family);
1935 result = genl_register_mc_group(&thermal_event_genl_family,
1936 &thermal_event_mcgrp);
1938 genl_unregister_family(&thermal_event_genl_family);
1942 static void genetlink_exit(void)
1944 genl_unregister_family(&thermal_event_genl_family);
1946 #else /* !CONFIG_NET */
1947 static inline int genetlink_init(void) { return 0; }
1948 static inline void genetlink_exit(void) {}
1949 #endif /* !CONFIG_NET */
1951 static int __init thermal_register_governors(void)
1955 result = thermal_gov_step_wise_register();
1959 result = thermal_gov_fair_share_register();
1963 return thermal_gov_user_space_register();
1966 static void thermal_unregister_governors(void)
1968 thermal_gov_step_wise_unregister();
1969 thermal_gov_fair_share_unregister();
1970 thermal_gov_user_space_unregister();
1973 static int __init thermal_init(void)
1977 result = thermal_register_governors();
1981 result = class_register(&thermal_class);
1983 goto unregister_governors;
1985 result = genetlink_init();
1987 goto unregister_class;
1991 unregister_governors:
1992 thermal_unregister_governors();
1994 class_unregister(&thermal_class);
1996 idr_destroy(&thermal_tz_idr);
1997 idr_destroy(&thermal_cdev_idr);
1998 mutex_destroy(&thermal_idr_lock);
1999 mutex_destroy(&thermal_list_lock);
2000 mutex_destroy(&thermal_governor_lock);
2004 static void __exit thermal_exit(void)
2007 class_unregister(&thermal_class);
2008 thermal_unregister_governors();
2009 idr_destroy(&thermal_tz_idr);
2010 idr_destroy(&thermal_cdev_idr);
2011 mutex_destroy(&thermal_idr_lock);
2012 mutex_destroy(&thermal_list_lock);
2013 mutex_destroy(&thermal_governor_lock);
2016 fs_initcall(thermal_init);
2017 module_exit(thermal_exit);