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/spinlock.h>
36 #include <linux/reboot.h>
37 #include <net/netlink.h>
38 #include <net/genetlink.h>
40 #include "thermal_core.h"
42 MODULE_AUTHOR("Zhang Rui");
43 MODULE_DESCRIPTION("Generic thermal management sysfs support");
44 MODULE_LICENSE("GPL");
46 static DEFINE_IDR(thermal_tz_idr);
47 static DEFINE_IDR(thermal_cdev_idr);
48 static DEFINE_MUTEX(thermal_idr_lock);
50 static LIST_HEAD(thermal_tz_list);
51 static LIST_HEAD(thermal_cdev_list);
52 static LIST_HEAD(thermal_governor_list);
54 static DEFINE_MUTEX(thermal_list_lock);
55 static DEFINE_MUTEX(thermal_governor_lock);
57 static struct thermal_governor *__find_governor(const char *name)
59 struct thermal_governor *pos;
61 list_for_each_entry(pos, &thermal_governor_list, governor_list)
62 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
68 int thermal_register_governor(struct thermal_governor *governor)
72 struct thermal_zone_device *pos;
77 mutex_lock(&thermal_governor_lock);
80 if (__find_governor(governor->name) == NULL) {
82 list_add(&governor->governor_list, &thermal_governor_list);
85 mutex_lock(&thermal_list_lock);
87 list_for_each_entry(pos, &thermal_tz_list, node) {
91 name = pos->tzp->governor_name;
93 name = DEFAULT_THERMAL_GOVERNOR;
94 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
95 pos->governor = governor;
98 mutex_unlock(&thermal_list_lock);
99 mutex_unlock(&thermal_governor_lock);
103 EXPORT_SYMBOL_GPL(thermal_register_governor);
105 void thermal_unregister_governor(struct thermal_governor *governor)
107 struct thermal_zone_device *pos;
112 mutex_lock(&thermal_governor_lock);
114 if (__find_governor(governor->name) == NULL)
117 mutex_lock(&thermal_list_lock);
119 list_for_each_entry(pos, &thermal_tz_list, node) {
120 if (!strnicmp(pos->governor->name, governor->name,
121 THERMAL_NAME_LENGTH))
122 pos->governor = NULL;
125 mutex_unlock(&thermal_list_lock);
126 list_del(&governor->governor_list);
128 mutex_unlock(&thermal_governor_lock);
131 EXPORT_SYMBOL_GPL(thermal_unregister_governor);
133 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
138 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
143 err = idr_get_new(idr, NULL, id);
146 if (unlikely(err == -EAGAIN))
148 else if (unlikely(err))
151 *id = *id & MAX_IDR_MASK;
155 static void release_idr(struct idr *idr, struct mutex *lock, int id)
164 int get_tz_trend(struct thermal_zone_device *tz, int trip)
166 enum thermal_trend trend;
168 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
169 if (tz->temperature > tz->last_temperature)
170 trend = THERMAL_TREND_RAISING;
171 else if (tz->temperature < tz->last_temperature)
172 trend = THERMAL_TREND_DROPPING;
174 trend = THERMAL_TREND_STABLE;
179 EXPORT_SYMBOL(get_tz_trend);
181 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
182 struct thermal_cooling_device *cdev, int trip)
184 struct thermal_instance *pos = NULL;
185 struct thermal_instance *target_instance = NULL;
187 mutex_lock(&tz->lock);
188 mutex_lock(&cdev->lock);
190 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
191 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
192 target_instance = pos;
197 mutex_unlock(&cdev->lock);
198 mutex_unlock(&tz->lock);
200 return target_instance;
202 EXPORT_SYMBOL(get_thermal_instance);
204 /* sys I/F for thermal zone */
206 #define to_thermal_zone(_dev) \
207 container_of(_dev, struct thermal_zone_device, device)
210 type_show(struct device *dev, struct device_attribute *attr, char *buf)
212 struct thermal_zone_device *tz = to_thermal_zone(dev);
214 return sprintf(buf, "%s\n", tz->type);
218 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
220 struct thermal_zone_device *tz = to_thermal_zone(dev);
224 if (!tz->ops->get_temp)
227 ret = tz->ops->get_temp(tz, &temperature);
232 return sprintf(buf, "%ld\n", temperature);
236 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
238 struct thermal_zone_device *tz = to_thermal_zone(dev);
239 enum thermal_device_mode mode;
242 if (!tz->ops->get_mode)
245 result = tz->ops->get_mode(tz, &mode);
249 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
254 mode_store(struct device *dev, struct device_attribute *attr,
255 const char *buf, size_t count)
257 struct thermal_zone_device *tz = to_thermal_zone(dev);
260 if (!tz->ops->set_mode)
263 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
264 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
265 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
266 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
277 trip_point_type_show(struct device *dev, struct device_attribute *attr,
280 struct thermal_zone_device *tz = to_thermal_zone(dev);
281 enum thermal_trip_type type;
284 if (!tz->ops->get_trip_type)
287 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
290 result = tz->ops->get_trip_type(tz, trip, &type);
295 case THERMAL_TRIP_CRITICAL:
296 return sprintf(buf, "critical\n");
297 case THERMAL_TRIP_HOT:
298 return sprintf(buf, "hot\n");
299 case THERMAL_TRIP_PASSIVE:
300 return sprintf(buf, "passive\n");
301 case THERMAL_TRIP_ACTIVE:
302 return sprintf(buf, "active\n");
304 return sprintf(buf, "unknown\n");
309 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
310 const char *buf, size_t count)
312 struct thermal_zone_device *tz = to_thermal_zone(dev);
314 unsigned long temperature;
316 if (!tz->ops->set_trip_temp)
319 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
322 if (kstrtoul(buf, 10, &temperature))
325 ret = tz->ops->set_trip_temp(tz, trip, temperature);
327 return ret ? ret : count;
331 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
334 struct thermal_zone_device *tz = to_thermal_zone(dev);
338 if (!tz->ops->get_trip_temp)
341 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
344 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
349 return sprintf(buf, "%ld\n", temperature);
353 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
354 const char *buf, size_t count)
356 struct thermal_zone_device *tz = to_thermal_zone(dev);
358 unsigned long temperature;
360 if (!tz->ops->set_trip_hyst)
363 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
366 if (kstrtoul(buf, 10, &temperature))
370 * We are not doing any check on the 'temperature' value
371 * here. The driver implementing 'set_trip_hyst' has to
374 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
376 return ret ? ret : count;
380 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
383 struct thermal_zone_device *tz = to_thermal_zone(dev);
385 unsigned long temperature;
387 if (!tz->ops->get_trip_hyst)
390 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
393 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
395 return ret ? ret : sprintf(buf, "%ld\n", temperature);
399 passive_store(struct device *dev, struct device_attribute *attr,
400 const char *buf, size_t count)
402 struct thermal_zone_device *tz = to_thermal_zone(dev);
403 struct thermal_cooling_device *cdev = NULL;
406 if (!sscanf(buf, "%d\n", &state))
409 /* sanity check: values below 1000 millicelcius don't make sense
410 * and can cause the system to go into a thermal heart attack
412 if (state && state < 1000)
415 if (state && !tz->forced_passive) {
416 mutex_lock(&thermal_list_lock);
417 list_for_each_entry(cdev, &thermal_cdev_list, node) {
418 if (!strncmp("Processor", cdev->type,
419 sizeof("Processor")))
420 thermal_zone_bind_cooling_device(tz,
421 THERMAL_TRIPS_NONE, cdev,
425 mutex_unlock(&thermal_list_lock);
426 if (!tz->passive_delay)
427 tz->passive_delay = 1000;
428 } else if (!state && tz->forced_passive) {
429 mutex_lock(&thermal_list_lock);
430 list_for_each_entry(cdev, &thermal_cdev_list, node) {
431 if (!strncmp("Processor", cdev->type,
432 sizeof("Processor")))
433 thermal_zone_unbind_cooling_device(tz,
437 mutex_unlock(&thermal_list_lock);
438 tz->passive_delay = 0;
441 tz->forced_passive = state;
443 thermal_zone_device_update(tz);
449 passive_show(struct device *dev, struct device_attribute *attr,
452 struct thermal_zone_device *tz = to_thermal_zone(dev);
454 return sprintf(buf, "%d\n", tz->forced_passive);
457 static DEVICE_ATTR(type, 0444, type_show, NULL);
458 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
459 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
460 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
462 /* sys I/F for cooling device */
463 #define to_cooling_device(_dev) \
464 container_of(_dev, struct thermal_cooling_device, device)
467 thermal_cooling_device_type_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
470 struct thermal_cooling_device *cdev = to_cooling_device(dev);
472 return sprintf(buf, "%s\n", cdev->type);
476 thermal_cooling_device_max_state_show(struct device *dev,
477 struct device_attribute *attr, char *buf)
479 struct thermal_cooling_device *cdev = to_cooling_device(dev);
483 ret = cdev->ops->get_max_state(cdev, &state);
486 return sprintf(buf, "%ld\n", state);
490 thermal_cooling_device_cur_state_show(struct device *dev,
491 struct device_attribute *attr, char *buf)
493 struct thermal_cooling_device *cdev = to_cooling_device(dev);
497 ret = cdev->ops->get_cur_state(cdev, &state);
500 return sprintf(buf, "%ld\n", state);
504 thermal_cooling_device_cur_state_store(struct device *dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
508 struct thermal_cooling_device *cdev = to_cooling_device(dev);
512 if (!sscanf(buf, "%ld\n", &state))
518 result = cdev->ops->set_cur_state(cdev, state);
524 static struct device_attribute dev_attr_cdev_type =
525 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
526 static DEVICE_ATTR(max_state, 0444,
527 thermal_cooling_device_max_state_show, NULL);
528 static DEVICE_ATTR(cur_state, 0644,
529 thermal_cooling_device_cur_state_show,
530 thermal_cooling_device_cur_state_store);
533 thermal_cooling_device_trip_point_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
536 struct thermal_instance *instance;
539 container_of(attr, struct thermal_instance, attr);
541 if (instance->trip == THERMAL_TRIPS_NONE)
542 return sprintf(buf, "-1\n");
544 return sprintf(buf, "%d\n", instance->trip);
547 /* Device management */
549 #if defined(CONFIG_THERMAL_HWMON)
552 #include <linux/hwmon.h>
554 /* thermal zone devices with the same type share one hwmon device */
555 struct thermal_hwmon_device {
556 char type[THERMAL_NAME_LENGTH];
557 struct device *device;
559 struct list_head tz_list;
560 struct list_head node;
563 struct thermal_hwmon_attr {
564 struct device_attribute attr;
568 /* one temperature input for each thermal zone */
569 struct thermal_hwmon_temp {
570 struct list_head hwmon_node;
571 struct thermal_zone_device *tz;
572 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
573 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
576 static LIST_HEAD(thermal_hwmon_list);
579 name_show(struct device *dev, struct device_attribute *attr, char *buf)
581 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
582 return sprintf(buf, "%s\n", hwmon->type);
584 static DEVICE_ATTR(name, 0444, name_show, NULL);
587 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
591 struct thermal_hwmon_attr *hwmon_attr
592 = container_of(attr, struct thermal_hwmon_attr, attr);
593 struct thermal_hwmon_temp *temp
594 = container_of(hwmon_attr, struct thermal_hwmon_temp,
596 struct thermal_zone_device *tz = temp->tz;
598 ret = tz->ops->get_temp(tz, &temperature);
603 return sprintf(buf, "%ld\n", temperature);
607 temp_crit_show(struct device *dev, struct device_attribute *attr,
610 struct thermal_hwmon_attr *hwmon_attr
611 = container_of(attr, struct thermal_hwmon_attr, attr);
612 struct thermal_hwmon_temp *temp
613 = container_of(hwmon_attr, struct thermal_hwmon_temp,
615 struct thermal_zone_device *tz = temp->tz;
619 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
623 return sprintf(buf, "%ld\n", temperature);
627 static struct thermal_hwmon_device *
628 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
630 struct thermal_hwmon_device *hwmon;
632 mutex_lock(&thermal_list_lock);
633 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
634 if (!strcmp(hwmon->type, tz->type)) {
635 mutex_unlock(&thermal_list_lock);
638 mutex_unlock(&thermal_list_lock);
643 /* Find the temperature input matching a given thermal zone */
644 static struct thermal_hwmon_temp *
645 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
646 const struct thermal_zone_device *tz)
648 struct thermal_hwmon_temp *temp;
650 mutex_lock(&thermal_list_lock);
651 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
652 if (temp->tz == tz) {
653 mutex_unlock(&thermal_list_lock);
656 mutex_unlock(&thermal_list_lock);
662 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
664 struct thermal_hwmon_device *hwmon;
665 struct thermal_hwmon_temp *temp;
666 int new_hwmon_device = 1;
669 hwmon = thermal_hwmon_lookup_by_type(tz);
671 new_hwmon_device = 0;
672 goto register_sys_interface;
675 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
679 INIT_LIST_HEAD(&hwmon->tz_list);
680 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
681 hwmon->device = hwmon_device_register(NULL);
682 if (IS_ERR(hwmon->device)) {
683 result = PTR_ERR(hwmon->device);
686 dev_set_drvdata(hwmon->device, hwmon);
687 result = device_create_file(hwmon->device, &dev_attr_name);
691 register_sys_interface:
692 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
695 goto unregister_name;
701 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
702 "temp%d_input", hwmon->count);
703 temp->temp_input.attr.attr.name = temp->temp_input.name;
704 temp->temp_input.attr.attr.mode = 0444;
705 temp->temp_input.attr.show = temp_input_show;
706 sysfs_attr_init(&temp->temp_input.attr.attr);
707 result = device_create_file(hwmon->device, &temp->temp_input.attr);
711 if (tz->ops->get_crit_temp) {
712 unsigned long temperature;
713 if (!tz->ops->get_crit_temp(tz, &temperature)) {
714 snprintf(temp->temp_crit.name,
715 sizeof(temp->temp_crit.name),
716 "temp%d_crit", hwmon->count);
717 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
718 temp->temp_crit.attr.attr.mode = 0444;
719 temp->temp_crit.attr.show = temp_crit_show;
720 sysfs_attr_init(&temp->temp_crit.attr.attr);
721 result = device_create_file(hwmon->device,
722 &temp->temp_crit.attr);
724 goto unregister_input;
728 mutex_lock(&thermal_list_lock);
729 if (new_hwmon_device)
730 list_add_tail(&hwmon->node, &thermal_hwmon_list);
731 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
732 mutex_unlock(&thermal_list_lock);
737 device_remove_file(hwmon->device, &temp->temp_input.attr);
741 if (new_hwmon_device) {
742 device_remove_file(hwmon->device, &dev_attr_name);
743 hwmon_device_unregister(hwmon->device);
746 if (new_hwmon_device)
753 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
755 struct thermal_hwmon_device *hwmon;
756 struct thermal_hwmon_temp *temp;
758 hwmon = thermal_hwmon_lookup_by_type(tz);
759 if (unlikely(!hwmon)) {
760 /* Should never happen... */
761 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
765 temp = thermal_hwmon_lookup_temp(hwmon, tz);
766 if (unlikely(!temp)) {
767 /* Should never happen... */
768 dev_dbg(&tz->device, "temperature input lookup failed!\n");
772 device_remove_file(hwmon->device, &temp->temp_input.attr);
773 if (tz->ops->get_crit_temp)
774 device_remove_file(hwmon->device, &temp->temp_crit.attr);
776 mutex_lock(&thermal_list_lock);
777 list_del(&temp->hwmon_node);
779 if (!list_empty(&hwmon->tz_list)) {
780 mutex_unlock(&thermal_list_lock);
783 list_del(&hwmon->node);
784 mutex_unlock(&thermal_list_lock);
786 device_remove_file(hwmon->device, &dev_attr_name);
787 hwmon_device_unregister(hwmon->device);
792 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
798 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
803 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
807 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
808 round_jiffies(msecs_to_jiffies(delay)));
810 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
811 msecs_to_jiffies(delay));
813 cancel_delayed_work(&tz->poll_queue);
816 static void thermal_zone_device_check(struct work_struct *work)
818 struct thermal_zone_device *tz = container_of(work, struct
821 thermal_zone_device_update(tz);
825 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
826 * @tz: thermal zone device
827 * @trip: indicates which trip point the cooling devices is
828 * associated with in this thermal zone.
829 * @cdev: thermal cooling device
831 * This function is usually called in the thermal zone device .bind callback.
833 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
835 struct thermal_cooling_device *cdev,
836 unsigned long upper, unsigned long lower)
838 struct thermal_instance *dev;
839 struct thermal_instance *pos;
840 struct thermal_zone_device *pos1;
841 struct thermal_cooling_device *pos2;
842 unsigned long max_state;
845 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
848 list_for_each_entry(pos1, &thermal_tz_list, node) {
852 list_for_each_entry(pos2, &thermal_cdev_list, node) {
857 if (tz != pos1 || cdev != pos2)
860 cdev->ops->get_max_state(cdev, &max_state);
862 /* lower default 0, upper default max_state */
863 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
864 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
866 if (lower > upper || upper > max_state)
870 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
878 dev->target = THERMAL_NO_TARGET;
880 result = get_idr(&tz->idr, &tz->lock, &dev->id);
884 sprintf(dev->name, "cdev%d", dev->id);
886 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
890 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
891 sysfs_attr_init(&dev->attr.attr);
892 dev->attr.attr.name = dev->attr_name;
893 dev->attr.attr.mode = 0444;
894 dev->attr.show = thermal_cooling_device_trip_point_show;
895 result = device_create_file(&tz->device, &dev->attr);
897 goto remove_symbol_link;
899 mutex_lock(&tz->lock);
900 mutex_lock(&cdev->lock);
901 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
902 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
907 list_add_tail(&dev->tz_node, &tz->thermal_instances);
908 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
910 mutex_unlock(&cdev->lock);
911 mutex_unlock(&tz->lock);
916 device_remove_file(&tz->device, &dev->attr);
918 sysfs_remove_link(&tz->device.kobj, dev->name);
920 release_idr(&tz->idr, &tz->lock, dev->id);
925 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
928 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
929 * @tz: thermal zone device
930 * @trip: indicates which trip point the cooling devices is
931 * associated with in this thermal zone.
932 * @cdev: thermal cooling device
934 * This function is usually called in the thermal zone device .unbind callback.
936 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
938 struct thermal_cooling_device *cdev)
940 struct thermal_instance *pos, *next;
942 mutex_lock(&tz->lock);
943 mutex_lock(&cdev->lock);
944 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
945 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
946 list_del(&pos->tz_node);
947 list_del(&pos->cdev_node);
948 mutex_unlock(&cdev->lock);
949 mutex_unlock(&tz->lock);
953 mutex_unlock(&cdev->lock);
954 mutex_unlock(&tz->lock);
959 device_remove_file(&tz->device, &pos->attr);
960 sysfs_remove_link(&tz->device.kobj, pos->name);
961 release_idr(&tz->idr, &tz->lock, pos->id);
965 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
967 static void thermal_release(struct device *dev)
969 struct thermal_zone_device *tz;
970 struct thermal_cooling_device *cdev;
972 if (!strncmp(dev_name(dev), "thermal_zone",
973 sizeof("thermal_zone") - 1)) {
974 tz = to_thermal_zone(dev);
977 cdev = to_cooling_device(dev);
982 static struct class thermal_class = {
984 .dev_release = thermal_release,
988 * thermal_cooling_device_register - register a new thermal cooling device
989 * @type: the thermal cooling device type.
990 * @devdata: device private data.
991 * @ops: standard thermal cooling devices callbacks.
993 struct thermal_cooling_device *
994 thermal_cooling_device_register(char *type, void *devdata,
995 const struct thermal_cooling_device_ops *ops)
997 struct thermal_cooling_device *cdev;
998 struct thermal_zone_device *pos;
1001 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1002 return ERR_PTR(-EINVAL);
1004 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1005 !ops->set_cur_state)
1006 return ERR_PTR(-EINVAL);
1008 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1010 return ERR_PTR(-ENOMEM);
1012 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1015 return ERR_PTR(result);
1018 strcpy(cdev->type, type ? : "");
1019 mutex_init(&cdev->lock);
1020 INIT_LIST_HEAD(&cdev->thermal_instances);
1022 cdev->updated = true;
1023 cdev->device.class = &thermal_class;
1024 cdev->devdata = devdata;
1025 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1026 result = device_register(&cdev->device);
1028 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1030 return ERR_PTR(result);
1035 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1040 result = device_create_file(&cdev->device, &dev_attr_max_state);
1044 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1048 mutex_lock(&thermal_list_lock);
1049 list_add(&cdev->node, &thermal_cdev_list);
1050 list_for_each_entry(pos, &thermal_tz_list, node) {
1051 if (!pos->ops->bind)
1053 result = pos->ops->bind(pos, cdev);
1058 mutex_unlock(&thermal_list_lock);
1064 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1065 device_unregister(&cdev->device);
1066 return ERR_PTR(result);
1068 EXPORT_SYMBOL(thermal_cooling_device_register);
1071 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1072 * @cdev: the thermal cooling device to remove.
1074 * thermal_cooling_device_unregister() must be called when the device is no
1077 void thermal_cooling_device_unregister(struct
1078 thermal_cooling_device
1081 struct thermal_zone_device *tz;
1082 struct thermal_cooling_device *pos = NULL;
1087 mutex_lock(&thermal_list_lock);
1088 list_for_each_entry(pos, &thermal_cdev_list, node)
1092 /* thermal cooling device not found */
1093 mutex_unlock(&thermal_list_lock);
1096 list_del(&cdev->node);
1097 list_for_each_entry(tz, &thermal_tz_list, node) {
1098 if (!tz->ops->unbind)
1100 tz->ops->unbind(tz, cdev);
1102 mutex_unlock(&thermal_list_lock);
1104 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1105 device_remove_file(&cdev->device, &dev_attr_max_state);
1106 device_remove_file(&cdev->device, &dev_attr_cur_state);
1108 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1109 device_unregister(&cdev->device);
1112 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1114 static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
1116 struct thermal_instance *instance;
1117 unsigned long target = 0;
1119 /* cooling device is updated*/
1123 mutex_lock(&cdev->lock);
1124 /* Make sure cdev enters the deepest cooling state */
1125 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1126 if (instance->target == THERMAL_NO_TARGET)
1128 if (instance->target > target)
1129 target = instance->target;
1131 mutex_unlock(&cdev->lock);
1132 cdev->ops->set_cur_state(cdev, target);
1133 cdev->updated = true;
1136 static void thermal_zone_do_update(struct thermal_zone_device *tz)
1138 struct thermal_instance *instance;
1140 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
1141 thermal_cdev_do_update(instance->cdev);
1145 * Cooling algorithm for both active and passive cooling
1147 * 1. if the temperature is higher than a trip point,
1148 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1149 * state for this trip point
1150 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1151 * state for this trip point
1153 * 2. if the temperature is lower than a trip point, use lower
1154 * cooling state for this trip point
1156 * Note that this behaves the same as the previous passive cooling
1160 static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1161 int trip, long temp)
1163 struct thermal_instance *instance;
1164 struct thermal_cooling_device *cdev = NULL;
1165 unsigned long cur_state, max_state;
1167 enum thermal_trip_type trip_type;
1168 enum thermal_trend trend;
1170 if (trip == THERMAL_TRIPS_NONE) {
1171 trip_temp = tz->forced_passive;
1172 trip_type = THERMAL_TRIPS_NONE;
1174 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1175 tz->ops->get_trip_type(tz, trip, &trip_type);
1178 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1180 * compare the current temperature and previous temperature
1181 * to get the thermal trend, if no special requirement
1183 if (tz->temperature > tz->last_temperature)
1184 trend = THERMAL_TREND_RAISING;
1185 else if (tz->temperature < tz->last_temperature)
1186 trend = THERMAL_TREND_DROPPING;
1188 trend = THERMAL_TREND_STABLE;
1191 if (temp >= trip_temp) {
1192 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1193 if (instance->trip != trip)
1196 cdev = instance->cdev;
1198 cdev->ops->get_cur_state(cdev, &cur_state);
1199 cdev->ops->get_max_state(cdev, &max_state);
1201 if (trend == THERMAL_TREND_RAISING) {
1202 cur_state = cur_state < instance->upper ?
1203 (cur_state + 1) : instance->upper;
1204 } else if (trend == THERMAL_TREND_DROPPING) {
1205 cur_state = cur_state > instance->lower ?
1206 (cur_state - 1) : instance->lower;
1209 /* activate a passive thermal instance */
1210 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1211 trip_type == THERMAL_TRIPS_NONE) &&
1212 instance->target == THERMAL_NO_TARGET)
1215 instance->target = cur_state;
1216 cdev->updated = false; /* cooling device needs update */
1218 } else { /* below trip */
1219 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1220 if (instance->trip != trip)
1223 /* Do not use the inactive thermal instance */
1224 if (instance->target == THERMAL_NO_TARGET)
1226 cdev = instance->cdev;
1227 cdev->ops->get_cur_state(cdev, &cur_state);
1229 cur_state = cur_state > instance->lower ?
1230 (cur_state - 1) : THERMAL_NO_TARGET;
1232 /* deactivate a passive thermal instance */
1233 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1234 trip_type == THERMAL_TRIPS_NONE) &&
1235 cur_state == THERMAL_NO_TARGET)
1237 instance->target = cur_state;
1238 cdev->updated = false; /* cooling device needs update */
1245 * thermal_zone_device_update - force an update of a thermal zone's state
1246 * @ttz: the thermal zone to update
1249 void thermal_zone_device_update(struct thermal_zone_device *tz)
1252 long temp, trip_temp;
1253 enum thermal_trip_type trip_type;
1255 mutex_lock(&tz->lock);
1257 if (tz->ops->get_temp(tz, &temp)) {
1258 /* get_temp failed - retry it later */
1259 pr_warn("failed to read out thermal zone %d\n", tz->id);
1263 tz->last_temperature = tz->temperature;
1264 tz->temperature = temp;
1266 for (count = 0; count < tz->trips; count++) {
1267 tz->ops->get_trip_type(tz, count, &trip_type);
1268 tz->ops->get_trip_temp(tz, count, &trip_temp);
1270 switch (trip_type) {
1271 case THERMAL_TRIP_CRITICAL:
1272 if (temp >= trip_temp) {
1273 if (tz->ops->notify)
1274 ret = tz->ops->notify(tz, count,
1277 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1279 orderly_poweroff(true);
1283 case THERMAL_TRIP_HOT:
1284 if (temp >= trip_temp)
1285 if (tz->ops->notify)
1286 tz->ops->notify(tz, count, trip_type);
1288 case THERMAL_TRIP_ACTIVE:
1289 thermal_zone_trip_update(tz, count, temp);
1291 case THERMAL_TRIP_PASSIVE:
1292 if (temp >= trip_temp || tz->passive)
1293 thermal_zone_trip_update(tz, count, temp);
1298 if (tz->forced_passive)
1299 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
1300 thermal_zone_do_update(tz);
1304 thermal_zone_device_set_polling(tz, tz->passive_delay);
1305 else if (tz->polling_delay)
1306 thermal_zone_device_set_polling(tz, tz->polling_delay);
1308 thermal_zone_device_set_polling(tz, 0);
1309 mutex_unlock(&tz->lock);
1311 EXPORT_SYMBOL(thermal_zone_device_update);
1314 * create_trip_attrs - create attributes for trip points
1315 * @tz: the thermal zone device
1316 * @mask: Writeable trip point bitmap.
1318 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1321 int size = sizeof(struct thermal_attr) * tz->trips;
1323 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1324 if (!tz->trip_type_attrs)
1327 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1328 if (!tz->trip_temp_attrs) {
1329 kfree(tz->trip_type_attrs);
1333 if (tz->ops->get_trip_hyst) {
1334 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1335 if (!tz->trip_hyst_attrs) {
1336 kfree(tz->trip_type_attrs);
1337 kfree(tz->trip_temp_attrs);
1343 for (indx = 0; indx < tz->trips; indx++) {
1344 /* create trip type attribute */
1345 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1346 "trip_point_%d_type", indx);
1348 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1349 tz->trip_type_attrs[indx].attr.attr.name =
1350 tz->trip_type_attrs[indx].name;
1351 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1352 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1354 device_create_file(&tz->device,
1355 &tz->trip_type_attrs[indx].attr);
1357 /* create trip temp attribute */
1358 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1359 "trip_point_%d_temp", indx);
1361 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1362 tz->trip_temp_attrs[indx].attr.attr.name =
1363 tz->trip_temp_attrs[indx].name;
1364 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1365 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1366 if (mask & (1 << indx)) {
1367 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1368 tz->trip_temp_attrs[indx].attr.store =
1369 trip_point_temp_store;
1372 device_create_file(&tz->device,
1373 &tz->trip_temp_attrs[indx].attr);
1375 /* create Optional trip hyst attribute */
1376 if (!tz->ops->get_trip_hyst)
1378 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1379 "trip_point_%d_hyst", indx);
1381 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1382 tz->trip_hyst_attrs[indx].attr.attr.name =
1383 tz->trip_hyst_attrs[indx].name;
1384 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1385 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1386 if (tz->ops->set_trip_hyst) {
1387 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1388 tz->trip_hyst_attrs[indx].attr.store =
1389 trip_point_hyst_store;
1392 device_create_file(&tz->device,
1393 &tz->trip_hyst_attrs[indx].attr);
1398 static void remove_trip_attrs(struct thermal_zone_device *tz)
1402 for (indx = 0; indx < tz->trips; indx++) {
1403 device_remove_file(&tz->device,
1404 &tz->trip_type_attrs[indx].attr);
1405 device_remove_file(&tz->device,
1406 &tz->trip_temp_attrs[indx].attr);
1407 if (tz->ops->get_trip_hyst)
1408 device_remove_file(&tz->device,
1409 &tz->trip_hyst_attrs[indx].attr);
1411 kfree(tz->trip_type_attrs);
1412 kfree(tz->trip_temp_attrs);
1413 kfree(tz->trip_hyst_attrs);
1417 * thermal_zone_device_register - register a new thermal zone device
1418 * @type: the thermal zone device type
1419 * @trips: the number of trip points the thermal zone support
1420 * @mask: a bit string indicating the writeablility of trip points
1421 * @devdata: private device data
1422 * @ops: standard thermal zone device callbacks
1423 * @tzp: thermal zone platform parameters
1424 * @passive_delay: number of milliseconds to wait between polls when
1425 * performing passive cooling
1426 * @polling_delay: number of milliseconds to wait between polls when checking
1427 * whether trip points have been crossed (0 for interrupt
1430 * thermal_zone_device_unregister() must be called when the device is no
1431 * longer needed. The passive cooling depends on the .get_trend() return value.
1433 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1434 int trips, int mask, void *devdata,
1435 const struct thermal_zone_device_ops *ops,
1436 const struct thermal_zone_params *tzp,
1437 int passive_delay, int polling_delay)
1439 struct thermal_zone_device *tz;
1440 struct thermal_cooling_device *pos;
1441 enum thermal_trip_type trip_type;
1446 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1447 return ERR_PTR(-EINVAL);
1449 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1450 return ERR_PTR(-EINVAL);
1452 if (!ops || !ops->get_temp)
1453 return ERR_PTR(-EINVAL);
1455 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1457 return ERR_PTR(-ENOMEM);
1459 INIT_LIST_HEAD(&tz->thermal_instances);
1461 mutex_init(&tz->lock);
1462 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1465 return ERR_PTR(result);
1468 strcpy(tz->type, type ? : "");
1471 tz->device.class = &thermal_class;
1472 tz->devdata = devdata;
1474 tz->passive_delay = passive_delay;
1475 tz->polling_delay = polling_delay;
1477 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1478 result = device_register(&tz->device);
1480 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1482 return ERR_PTR(result);
1487 result = device_create_file(&tz->device, &dev_attr_type);
1492 result = device_create_file(&tz->device, &dev_attr_temp);
1496 if (ops->get_mode) {
1497 result = device_create_file(&tz->device, &dev_attr_mode);
1502 result = create_trip_attrs(tz, mask);
1506 for (count = 0; count < trips; count++) {
1507 tz->ops->get_trip_type(tz, count, &trip_type);
1508 if (trip_type == THERMAL_TRIP_PASSIVE)
1513 result = device_create_file(&tz->device,
1519 /* Update 'this' zone's governor information */
1520 mutex_lock(&thermal_governor_lock);
1523 tz->governor = __find_governor(tz->tzp->governor_name);
1525 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1527 mutex_unlock(&thermal_governor_lock);
1529 result = thermal_add_hwmon_sysfs(tz);
1533 mutex_lock(&thermal_list_lock);
1534 list_add_tail(&tz->node, &thermal_tz_list);
1536 list_for_each_entry(pos, &thermal_cdev_list, node) {
1537 result = ops->bind(tz, pos);
1541 mutex_unlock(&thermal_list_lock);
1543 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1545 thermal_zone_device_update(tz);
1551 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1552 device_unregister(&tz->device);
1553 return ERR_PTR(result);
1555 EXPORT_SYMBOL(thermal_zone_device_register);
1558 * thermal_device_unregister - removes the registered thermal zone device
1559 * @tz: the thermal zone device to remove
1561 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1563 struct thermal_cooling_device *cdev;
1564 struct thermal_zone_device *pos = NULL;
1569 mutex_lock(&thermal_list_lock);
1570 list_for_each_entry(pos, &thermal_tz_list, node)
1574 /* thermal zone device not found */
1575 mutex_unlock(&thermal_list_lock);
1578 list_del(&tz->node);
1579 if (tz->ops->unbind)
1580 list_for_each_entry(cdev, &thermal_cdev_list, node)
1581 tz->ops->unbind(tz, cdev);
1582 mutex_unlock(&thermal_list_lock);
1584 thermal_zone_device_set_polling(tz, 0);
1587 device_remove_file(&tz->device, &dev_attr_type);
1588 device_remove_file(&tz->device, &dev_attr_temp);
1589 if (tz->ops->get_mode)
1590 device_remove_file(&tz->device, &dev_attr_mode);
1591 remove_trip_attrs(tz);
1592 tz->governor = NULL;
1594 thermal_remove_hwmon_sysfs(tz);
1595 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1596 idr_destroy(&tz->idr);
1597 mutex_destroy(&tz->lock);
1598 device_unregister(&tz->device);
1601 EXPORT_SYMBOL(thermal_zone_device_unregister);
1604 static struct genl_family thermal_event_genl_family = {
1605 .id = GENL_ID_GENERATE,
1606 .name = THERMAL_GENL_FAMILY_NAME,
1607 .version = THERMAL_GENL_VERSION,
1608 .maxattr = THERMAL_GENL_ATTR_MAX,
1611 static struct genl_multicast_group thermal_event_mcgrp = {
1612 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1615 int thermal_generate_netlink_event(u32 orig, enum events event)
1617 struct sk_buff *skb;
1618 struct nlattr *attr;
1619 struct thermal_genl_event *thermal_event;
1623 static unsigned int thermal_event_seqnum;
1625 /* allocate memory */
1626 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1629 skb = genlmsg_new(size, GFP_ATOMIC);
1633 /* add the genetlink message header */
1634 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1635 &thermal_event_genl_family, 0,
1636 THERMAL_GENL_CMD_EVENT);
1643 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1644 sizeof(struct thermal_genl_event));
1651 thermal_event = nla_data(attr);
1652 if (!thermal_event) {
1657 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1659 thermal_event->orig = orig;
1660 thermal_event->event = event;
1662 /* send multicast genetlink message */
1663 result = genlmsg_end(skb, msg_header);
1669 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1671 pr_info("failed to send netlink event:%d\n", result);
1675 EXPORT_SYMBOL(thermal_generate_netlink_event);
1677 static int genetlink_init(void)
1681 result = genl_register_family(&thermal_event_genl_family);
1685 result = genl_register_mc_group(&thermal_event_genl_family,
1686 &thermal_event_mcgrp);
1688 genl_unregister_family(&thermal_event_genl_family);
1692 static void genetlink_exit(void)
1694 genl_unregister_family(&thermal_event_genl_family);
1696 #else /* !CONFIG_NET */
1697 static inline int genetlink_init(void) { return 0; }
1698 static inline void genetlink_exit(void) {}
1699 #endif /* !CONFIG_NET */
1701 static int __init thermal_init(void)
1705 result = class_register(&thermal_class);
1707 idr_destroy(&thermal_tz_idr);
1708 idr_destroy(&thermal_cdev_idr);
1709 mutex_destroy(&thermal_idr_lock);
1710 mutex_destroy(&thermal_list_lock);
1712 result = genetlink_init();
1716 static void __exit thermal_exit(void)
1718 class_unregister(&thermal_class);
1719 idr_destroy(&thermal_tz_idr);
1720 idr_destroy(&thermal_cdev_idr);
1721 mutex_destroy(&thermal_idr_lock);
1722 mutex_destroy(&thermal_list_lock);
1726 fs_initcall(thermal_init);
1727 module_exit(thermal_exit);