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 MODULE_AUTHOR("Zhang Rui");
41 MODULE_DESCRIPTION("Generic thermal management sysfs support");
42 MODULE_LICENSE("GPL");
45 * This structure is used to describe the behavior of
46 * a certain cooling device on a certain trip point
47 * in a certain thermal zone
49 struct thermal_instance {
51 char name[THERMAL_NAME_LENGTH];
52 struct thermal_zone_device *tz;
53 struct thermal_cooling_device *cdev;
55 unsigned long upper; /* Highest cooling state for this trip point */
56 unsigned long lower; /* Lowest cooling state for this trip point */
57 char attr_name[THERMAL_NAME_LENGTH];
58 struct device_attribute attr;
59 struct list_head tz_node; /* node in tz->thermal_instances */
60 struct list_head cdev_node; /* node in cdev->thermal_instances */
63 static DEFINE_IDR(thermal_tz_idr);
64 static DEFINE_IDR(thermal_cdev_idr);
65 static DEFINE_MUTEX(thermal_idr_lock);
67 static LIST_HEAD(thermal_tz_list);
68 static LIST_HEAD(thermal_cdev_list);
69 static DEFINE_MUTEX(thermal_list_lock);
71 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
76 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
81 err = idr_get_new(idr, NULL, id);
84 if (unlikely(err == -EAGAIN))
86 else if (unlikely(err))
89 *id = *id & MAX_ID_MASK;
93 static void release_idr(struct idr *idr, struct mutex *lock, int id)
102 /* sys I/F for thermal zone */
104 #define to_thermal_zone(_dev) \
105 container_of(_dev, struct thermal_zone_device, device)
108 type_show(struct device *dev, struct device_attribute *attr, char *buf)
110 struct thermal_zone_device *tz = to_thermal_zone(dev);
112 return sprintf(buf, "%s\n", tz->type);
116 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
118 struct thermal_zone_device *tz = to_thermal_zone(dev);
122 if (!tz->ops->get_temp)
125 ret = tz->ops->get_temp(tz, &temperature);
130 return sprintf(buf, "%ld\n", temperature);
134 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
136 struct thermal_zone_device *tz = to_thermal_zone(dev);
137 enum thermal_device_mode mode;
140 if (!tz->ops->get_mode)
143 result = tz->ops->get_mode(tz, &mode);
147 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
152 mode_store(struct device *dev, struct device_attribute *attr,
153 const char *buf, size_t count)
155 struct thermal_zone_device *tz = to_thermal_zone(dev);
158 if (!tz->ops->set_mode)
161 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
162 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
163 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
164 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
175 trip_point_type_show(struct device *dev, struct device_attribute *attr,
178 struct thermal_zone_device *tz = to_thermal_zone(dev);
179 enum thermal_trip_type type;
182 if (!tz->ops->get_trip_type)
185 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
188 result = tz->ops->get_trip_type(tz, trip, &type);
193 case THERMAL_TRIP_CRITICAL:
194 return sprintf(buf, "critical\n");
195 case THERMAL_TRIP_HOT:
196 return sprintf(buf, "hot\n");
197 case THERMAL_TRIP_PASSIVE:
198 return sprintf(buf, "passive\n");
199 case THERMAL_TRIP_ACTIVE:
200 return sprintf(buf, "active\n");
202 return sprintf(buf, "unknown\n");
207 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
208 const char *buf, size_t count)
210 struct thermal_zone_device *tz = to_thermal_zone(dev);
212 unsigned long temperature;
214 if (!tz->ops->set_trip_temp)
217 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
220 if (kstrtoul(buf, 10, &temperature))
223 ret = tz->ops->set_trip_temp(tz, trip, temperature);
225 return ret ? ret : count;
229 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
232 struct thermal_zone_device *tz = to_thermal_zone(dev);
236 if (!tz->ops->get_trip_temp)
239 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
242 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
247 return sprintf(buf, "%ld\n", temperature);
251 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
252 const char *buf, size_t count)
254 struct thermal_zone_device *tz = to_thermal_zone(dev);
256 unsigned long temperature;
258 if (!tz->ops->set_trip_hyst)
261 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
264 if (kstrtoul(buf, 10, &temperature))
268 * We are not doing any check on the 'temperature' value
269 * here. The driver implementing 'set_trip_hyst' has to
272 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
274 return ret ? ret : count;
278 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
281 struct thermal_zone_device *tz = to_thermal_zone(dev);
283 unsigned long temperature;
285 if (!tz->ops->get_trip_hyst)
288 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
291 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
293 return ret ? ret : sprintf(buf, "%ld\n", temperature);
297 passive_store(struct device *dev, struct device_attribute *attr,
298 const char *buf, size_t count)
300 struct thermal_zone_device *tz = to_thermal_zone(dev);
301 struct thermal_cooling_device *cdev = NULL;
304 if (!sscanf(buf, "%d\n", &state))
307 /* sanity check: values below 1000 millicelcius don't make sense
308 * and can cause the system to go into a thermal heart attack
310 if (state && state < 1000)
313 if (state && !tz->forced_passive) {
314 mutex_lock(&thermal_list_lock);
315 list_for_each_entry(cdev, &thermal_cdev_list, node) {
316 if (!strncmp("Processor", cdev->type,
317 sizeof("Processor")))
318 thermal_zone_bind_cooling_device(tz,
319 THERMAL_TRIPS_NONE, cdev,
323 mutex_unlock(&thermal_list_lock);
324 if (!tz->passive_delay)
325 tz->passive_delay = 1000;
326 } else if (!state && tz->forced_passive) {
327 mutex_lock(&thermal_list_lock);
328 list_for_each_entry(cdev, &thermal_cdev_list, node) {
329 if (!strncmp("Processor", cdev->type,
330 sizeof("Processor")))
331 thermal_zone_unbind_cooling_device(tz,
335 mutex_unlock(&thermal_list_lock);
336 tz->passive_delay = 0;
339 tz->forced_passive = state;
341 thermal_zone_device_update(tz);
347 passive_show(struct device *dev, struct device_attribute *attr,
350 struct thermal_zone_device *tz = to_thermal_zone(dev);
352 return sprintf(buf, "%d\n", tz->forced_passive);
355 static DEVICE_ATTR(type, 0444, type_show, NULL);
356 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
357 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
358 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
360 /* sys I/F for cooling device */
361 #define to_cooling_device(_dev) \
362 container_of(_dev, struct thermal_cooling_device, device)
365 thermal_cooling_device_type_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
368 struct thermal_cooling_device *cdev = to_cooling_device(dev);
370 return sprintf(buf, "%s\n", cdev->type);
374 thermal_cooling_device_max_state_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
377 struct thermal_cooling_device *cdev = to_cooling_device(dev);
381 ret = cdev->ops->get_max_state(cdev, &state);
384 return sprintf(buf, "%ld\n", state);
388 thermal_cooling_device_cur_state_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
391 struct thermal_cooling_device *cdev = to_cooling_device(dev);
395 ret = cdev->ops->get_cur_state(cdev, &state);
398 return sprintf(buf, "%ld\n", state);
402 thermal_cooling_device_cur_state_store(struct device *dev,
403 struct device_attribute *attr,
404 const char *buf, size_t count)
406 struct thermal_cooling_device *cdev = to_cooling_device(dev);
410 if (!sscanf(buf, "%ld\n", &state))
416 result = cdev->ops->set_cur_state(cdev, state);
422 static struct device_attribute dev_attr_cdev_type =
423 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
424 static DEVICE_ATTR(max_state, 0444,
425 thermal_cooling_device_max_state_show, NULL);
426 static DEVICE_ATTR(cur_state, 0644,
427 thermal_cooling_device_cur_state_show,
428 thermal_cooling_device_cur_state_store);
431 thermal_cooling_device_trip_point_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
434 struct thermal_instance *instance;
437 container_of(attr, struct thermal_instance, attr);
439 if (instance->trip == THERMAL_TRIPS_NONE)
440 return sprintf(buf, "-1\n");
442 return sprintf(buf, "%d\n", instance->trip);
445 /* Device management */
447 #if defined(CONFIG_THERMAL_HWMON)
450 #include <linux/hwmon.h>
452 /* thermal zone devices with the same type share one hwmon device */
453 struct thermal_hwmon_device {
454 char type[THERMAL_NAME_LENGTH];
455 struct device *device;
457 struct list_head tz_list;
458 struct list_head node;
461 struct thermal_hwmon_attr {
462 struct device_attribute attr;
466 /* one temperature input for each thermal zone */
467 struct thermal_hwmon_temp {
468 struct list_head hwmon_node;
469 struct thermal_zone_device *tz;
470 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
471 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
474 static LIST_HEAD(thermal_hwmon_list);
477 name_show(struct device *dev, struct device_attribute *attr, char *buf)
479 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
480 return sprintf(buf, "%s\n", hwmon->type);
482 static DEVICE_ATTR(name, 0444, name_show, NULL);
485 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
489 struct thermal_hwmon_attr *hwmon_attr
490 = container_of(attr, struct thermal_hwmon_attr, attr);
491 struct thermal_hwmon_temp *temp
492 = container_of(hwmon_attr, struct thermal_hwmon_temp,
494 struct thermal_zone_device *tz = temp->tz;
496 ret = tz->ops->get_temp(tz, &temperature);
501 return sprintf(buf, "%ld\n", temperature);
505 temp_crit_show(struct device *dev, struct device_attribute *attr,
508 struct thermal_hwmon_attr *hwmon_attr
509 = container_of(attr, struct thermal_hwmon_attr, attr);
510 struct thermal_hwmon_temp *temp
511 = container_of(hwmon_attr, struct thermal_hwmon_temp,
513 struct thermal_zone_device *tz = temp->tz;
517 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
521 return sprintf(buf, "%ld\n", temperature);
525 static struct thermal_hwmon_device *
526 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
528 struct thermal_hwmon_device *hwmon;
530 mutex_lock(&thermal_list_lock);
531 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
532 if (!strcmp(hwmon->type, tz->type)) {
533 mutex_unlock(&thermal_list_lock);
536 mutex_unlock(&thermal_list_lock);
541 /* Find the temperature input matching a given thermal zone */
542 static struct thermal_hwmon_temp *
543 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
544 const struct thermal_zone_device *tz)
546 struct thermal_hwmon_temp *temp;
548 mutex_lock(&thermal_list_lock);
549 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
550 if (temp->tz == tz) {
551 mutex_unlock(&thermal_list_lock);
554 mutex_unlock(&thermal_list_lock);
560 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
562 struct thermal_hwmon_device *hwmon;
563 struct thermal_hwmon_temp *temp;
564 int new_hwmon_device = 1;
567 hwmon = thermal_hwmon_lookup_by_type(tz);
569 new_hwmon_device = 0;
570 goto register_sys_interface;
573 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
577 INIT_LIST_HEAD(&hwmon->tz_list);
578 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
579 hwmon->device = hwmon_device_register(NULL);
580 if (IS_ERR(hwmon->device)) {
581 result = PTR_ERR(hwmon->device);
584 dev_set_drvdata(hwmon->device, hwmon);
585 result = device_create_file(hwmon->device, &dev_attr_name);
589 register_sys_interface:
590 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
593 goto unregister_name;
599 snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH,
600 "temp%d_input", hwmon->count);
601 temp->temp_input.attr.attr.name = temp->temp_input.name;
602 temp->temp_input.attr.attr.mode = 0444;
603 temp->temp_input.attr.show = temp_input_show;
604 sysfs_attr_init(&temp->temp_input.attr.attr);
605 result = device_create_file(hwmon->device, &temp->temp_input.attr);
609 if (tz->ops->get_crit_temp) {
610 unsigned long temperature;
611 if (!tz->ops->get_crit_temp(tz, &temperature)) {
612 snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH,
613 "temp%d_crit", hwmon->count);
614 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
615 temp->temp_crit.attr.attr.mode = 0444;
616 temp->temp_crit.attr.show = temp_crit_show;
617 sysfs_attr_init(&temp->temp_crit.attr.attr);
618 result = device_create_file(hwmon->device,
619 &temp->temp_crit.attr);
621 goto unregister_input;
625 mutex_lock(&thermal_list_lock);
626 if (new_hwmon_device)
627 list_add_tail(&hwmon->node, &thermal_hwmon_list);
628 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
629 mutex_unlock(&thermal_list_lock);
634 device_remove_file(hwmon->device, &temp->temp_input.attr);
638 if (new_hwmon_device) {
639 device_remove_file(hwmon->device, &dev_attr_name);
640 hwmon_device_unregister(hwmon->device);
643 if (new_hwmon_device)
650 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
652 struct thermal_hwmon_device *hwmon;
653 struct thermal_hwmon_temp *temp;
655 hwmon = thermal_hwmon_lookup_by_type(tz);
656 if (unlikely(!hwmon)) {
657 /* Should never happen... */
658 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
662 temp = thermal_hwmon_lookup_temp(hwmon, tz);
663 if (unlikely(!temp)) {
664 /* Should never happen... */
665 dev_dbg(&tz->device, "temperature input lookup failed!\n");
669 device_remove_file(hwmon->device, &temp->temp_input.attr);
670 if (tz->ops->get_crit_temp)
671 device_remove_file(hwmon->device, &temp->temp_crit.attr);
673 mutex_lock(&thermal_list_lock);
674 list_del(&temp->hwmon_node);
676 if (!list_empty(&hwmon->tz_list)) {
677 mutex_unlock(&thermal_list_lock);
680 list_del(&hwmon->node);
681 mutex_unlock(&thermal_list_lock);
683 device_remove_file(hwmon->device, &dev_attr_name);
684 hwmon_device_unregister(hwmon->device);
689 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
695 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
700 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
703 cancel_delayed_work(&(tz->poll_queue));
709 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
710 round_jiffies(msecs_to_jiffies(delay)));
712 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
713 msecs_to_jiffies(delay));
716 static void thermal_zone_device_passive(struct thermal_zone_device *tz,
717 int temp, int trip_temp, int trip)
719 enum thermal_trend trend;
720 struct thermal_instance *instance;
721 struct thermal_cooling_device *cdev;
722 long state, max_state;
724 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
726 * compare the current temperature and previous temperature
727 * to get the thermal trend, if no special requirement
729 if (tz->temperature > tz->last_temperature)
730 trend = THERMAL_TREND_RAISING;
731 else if (tz->temperature < tz->last_temperature)
732 trend = THERMAL_TREND_DROPPING;
734 trend = THERMAL_TREND_STABLE;
740 * Calculate the thermal trend (using the passive cooling equation)
741 * and modify the performance limit for all passive cooling devices
742 * accordingly. Note that we assume symmetry.
744 if (temp >= trip_temp) {
748 if (trend == THERMAL_TREND_RAISING) {
749 list_for_each_entry(instance, &tz->thermal_instances,
751 if (instance->trip != trip)
753 cdev = instance->cdev;
754 cdev->ops->get_cur_state(cdev, &state);
755 cdev->ops->get_max_state(cdev, &max_state);
756 if (state++ < max_state)
757 cdev->ops->set_cur_state(cdev, state);
759 } else if (trend == THERMAL_TREND_DROPPING) { /* Cooling off? */
760 list_for_each_entry(instance, &tz->thermal_instances,
762 if (instance->trip != trip)
764 cdev = instance->cdev;
765 cdev->ops->get_cur_state(cdev, &state);
766 cdev->ops->get_max_state(cdev, &max_state);
768 cdev->ops->set_cur_state(cdev, --state);
777 * Implement passive cooling hysteresis to slowly increase performance
778 * and avoid thrashing around the passive trip point. Note that we
781 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
782 if (instance->trip != trip)
784 cdev = instance->cdev;
785 cdev->ops->get_cur_state(cdev, &state);
786 cdev->ops->get_max_state(cdev, &max_state);
788 cdev->ops->set_cur_state(cdev, --state);
794 static void thermal_zone_device_check(struct work_struct *work)
796 struct thermal_zone_device *tz = container_of(work, struct
799 thermal_zone_device_update(tz);
803 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
804 * @tz: thermal zone device
805 * @trip: indicates which trip point the cooling devices is
806 * associated with in this thermal zone.
807 * @cdev: thermal cooling device
809 * This function is usually called in the thermal zone device .bind callback.
811 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
813 struct thermal_cooling_device *cdev,
814 unsigned long upper, unsigned long lower)
816 struct thermal_instance *dev;
817 struct thermal_instance *pos;
818 struct thermal_zone_device *pos1;
819 struct thermal_cooling_device *pos2;
820 unsigned long max_state;
823 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
826 list_for_each_entry(pos1, &thermal_tz_list, node) {
830 list_for_each_entry(pos2, &thermal_cdev_list, node) {
835 if (tz != pos1 || cdev != pos2)
838 cdev->ops->get_max_state(cdev, &max_state);
840 /* lower default 0, upper default max_state */
841 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
842 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
844 if (lower > upper || upper > max_state)
848 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
857 result = get_idr(&tz->idr, &tz->lock, &dev->id);
861 sprintf(dev->name, "cdev%d", dev->id);
863 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
867 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
868 sysfs_attr_init(&dev->attr.attr);
869 dev->attr.attr.name = dev->attr_name;
870 dev->attr.attr.mode = 0444;
871 dev->attr.show = thermal_cooling_device_trip_point_show;
872 result = device_create_file(&tz->device, &dev->attr);
874 goto remove_symbol_link;
876 mutex_lock(&tz->lock);
877 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
878 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
883 list_add_tail(&dev->tz_node, &tz->thermal_instances);
884 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
886 mutex_unlock(&tz->lock);
891 device_remove_file(&tz->device, &dev->attr);
893 sysfs_remove_link(&tz->device.kobj, dev->name);
895 release_idr(&tz->idr, &tz->lock, dev->id);
900 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
903 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
904 * @tz: thermal zone device
905 * @trip: indicates which trip point the cooling devices is
906 * associated with in this thermal zone.
907 * @cdev: thermal cooling device
909 * This function is usually called in the thermal zone device .unbind callback.
911 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
913 struct thermal_cooling_device *cdev)
915 struct thermal_instance *pos, *next;
917 mutex_lock(&tz->lock);
918 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
919 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
920 list_del(&pos->tz_node);
921 list_del(&pos->cdev_node);
922 mutex_unlock(&tz->lock);
926 mutex_unlock(&tz->lock);
931 device_remove_file(&tz->device, &pos->attr);
932 sysfs_remove_link(&tz->device.kobj, pos->name);
933 release_idr(&tz->idr, &tz->lock, pos->id);
937 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
939 static void thermal_release(struct device *dev)
941 struct thermal_zone_device *tz;
942 struct thermal_cooling_device *cdev;
944 if (!strncmp(dev_name(dev), "thermal_zone",
945 sizeof("thermal_zone") - 1)) {
946 tz = to_thermal_zone(dev);
949 cdev = to_cooling_device(dev);
954 static struct class thermal_class = {
956 .dev_release = thermal_release,
960 * thermal_cooling_device_register - register a new thermal cooling device
961 * @type: the thermal cooling device type.
962 * @devdata: device private data.
963 * @ops: standard thermal cooling devices callbacks.
965 struct thermal_cooling_device *
966 thermal_cooling_device_register(char *type, void *devdata,
967 const struct thermal_cooling_device_ops *ops)
969 struct thermal_cooling_device *cdev;
970 struct thermal_zone_device *pos;
973 if (strlen(type) >= THERMAL_NAME_LENGTH)
974 return ERR_PTR(-EINVAL);
976 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
978 return ERR_PTR(-EINVAL);
980 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
982 return ERR_PTR(-ENOMEM);
984 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
987 return ERR_PTR(result);
990 strcpy(cdev->type, type);
991 INIT_LIST_HEAD(&cdev->thermal_instances);
993 cdev->device.class = &thermal_class;
994 cdev->devdata = devdata;
995 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
996 result = device_register(&cdev->device);
998 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1000 return ERR_PTR(result);
1005 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1010 result = device_create_file(&cdev->device, &dev_attr_max_state);
1014 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1018 mutex_lock(&thermal_list_lock);
1019 list_add(&cdev->node, &thermal_cdev_list);
1020 list_for_each_entry(pos, &thermal_tz_list, node) {
1021 if (!pos->ops->bind)
1023 result = pos->ops->bind(pos, cdev);
1028 mutex_unlock(&thermal_list_lock);
1034 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1035 device_unregister(&cdev->device);
1036 return ERR_PTR(result);
1038 EXPORT_SYMBOL(thermal_cooling_device_register);
1041 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1042 * @cdev: the thermal cooling device to remove.
1044 * thermal_cooling_device_unregister() must be called when the device is no
1047 void thermal_cooling_device_unregister(struct
1048 thermal_cooling_device
1051 struct thermal_zone_device *tz;
1052 struct thermal_cooling_device *pos = NULL;
1057 mutex_lock(&thermal_list_lock);
1058 list_for_each_entry(pos, &thermal_cdev_list, node)
1062 /* thermal cooling device not found */
1063 mutex_unlock(&thermal_list_lock);
1066 list_del(&cdev->node);
1067 list_for_each_entry(tz, &thermal_tz_list, node) {
1068 if (!tz->ops->unbind)
1070 tz->ops->unbind(tz, cdev);
1072 mutex_unlock(&thermal_list_lock);
1074 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1075 device_remove_file(&cdev->device, &dev_attr_max_state);
1076 device_remove_file(&cdev->device, &dev_attr_cur_state);
1078 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1079 device_unregister(&cdev->device);
1082 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1085 * Cooling algorithm for active trip points
1087 * 1. if the temperature is higher than a trip point,
1088 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1089 * state for this trip point
1090 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1091 * state for this trip point
1093 * 2. if the temperature is lower than a trip point, use lower
1094 * cooling state for this trip point
1096 * Note that this behaves the same as the previous passive cooling
1100 static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1101 int trip, long temp)
1103 struct thermal_instance *instance;
1104 struct thermal_cooling_device *cdev = NULL;
1105 unsigned long cur_state, max_state;
1107 enum thermal_trend trend;
1109 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1111 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1113 * compare the current temperature and previous temperature
1114 * to get the thermal trend, if no special requirement
1116 if (tz->temperature > tz->last_temperature)
1117 trend = THERMAL_TREND_RAISING;
1118 else if (tz->temperature < tz->last_temperature)
1119 trend = THERMAL_TREND_DROPPING;
1121 trend = THERMAL_TREND_STABLE;
1124 if (temp >= trip_temp) {
1125 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1126 if (instance->trip != trip)
1129 cdev = instance->cdev;
1131 cdev->ops->get_cur_state(cdev, &cur_state);
1132 cdev->ops->get_max_state(cdev, &max_state);
1134 if (trend == THERMAL_TREND_RAISING) {
1135 cur_state = cur_state < instance->upper ?
1136 (cur_state + 1) : instance->upper;
1137 } else if (trend == THERMAL_TREND_DROPPING) {
1138 cur_state = cur_state > instance->lower ?
1139 (cur_state - 1) : instance->lower;
1141 cdev->ops->set_cur_state(cdev, cur_state);
1143 } else { /* below trip */
1144 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1145 if (instance->trip != trip)
1148 cdev = instance->cdev;
1149 cdev->ops->get_cur_state(cdev, &cur_state);
1151 cur_state = cur_state > instance->lower ?
1152 (cur_state - 1) : instance->lower;
1153 cdev->ops->set_cur_state(cdev, cur_state);
1160 * thermal_zone_device_update - force an update of a thermal zone's state
1161 * @ttz: the thermal zone to update
1164 void thermal_zone_device_update(struct thermal_zone_device *tz)
1167 long temp, trip_temp;
1168 enum thermal_trip_type trip_type;
1170 mutex_lock(&tz->lock);
1172 if (tz->ops->get_temp(tz, &temp)) {
1173 /* get_temp failed - retry it later */
1174 pr_warn("failed to read out thermal zone %d\n", tz->id);
1178 tz->last_temperature = tz->temperature;
1179 tz->temperature = temp;
1181 for (count = 0; count < tz->trips; count++) {
1182 tz->ops->get_trip_type(tz, count, &trip_type);
1183 tz->ops->get_trip_temp(tz, count, &trip_temp);
1185 switch (trip_type) {
1186 case THERMAL_TRIP_CRITICAL:
1187 if (temp >= trip_temp) {
1188 if (tz->ops->notify)
1189 ret = tz->ops->notify(tz, count,
1192 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1194 orderly_poweroff(true);
1198 case THERMAL_TRIP_HOT:
1199 if (temp >= trip_temp)
1200 if (tz->ops->notify)
1201 tz->ops->notify(tz, count, trip_type);
1203 case THERMAL_TRIP_ACTIVE:
1204 thermal_zone_trip_update(tz, count, temp);
1206 case THERMAL_TRIP_PASSIVE:
1207 if (temp >= trip_temp || tz->passive)
1208 thermal_zone_device_passive(tz, temp,
1214 if (tz->forced_passive)
1215 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1216 THERMAL_TRIPS_NONE);
1220 thermal_zone_device_set_polling(tz, tz->passive_delay);
1221 else if (tz->polling_delay)
1222 thermal_zone_device_set_polling(tz, tz->polling_delay);
1224 thermal_zone_device_set_polling(tz, 0);
1225 mutex_unlock(&tz->lock);
1227 EXPORT_SYMBOL(thermal_zone_device_update);
1230 * create_trip_attrs - create attributes for trip points
1231 * @tz: the thermal zone device
1232 * @mask: Writeable trip point bitmap.
1234 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1237 int size = sizeof(struct thermal_attr) * tz->trips;
1239 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1240 if (!tz->trip_type_attrs)
1243 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1244 if (!tz->trip_temp_attrs) {
1245 kfree(tz->trip_type_attrs);
1249 if (tz->ops->get_trip_hyst) {
1250 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1251 if (!tz->trip_hyst_attrs) {
1252 kfree(tz->trip_type_attrs);
1253 kfree(tz->trip_temp_attrs);
1259 for (indx = 0; indx < tz->trips; indx++) {
1260 /* create trip type attribute */
1261 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1262 "trip_point_%d_type", indx);
1264 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1265 tz->trip_type_attrs[indx].attr.attr.name =
1266 tz->trip_type_attrs[indx].name;
1267 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1268 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1270 device_create_file(&tz->device,
1271 &tz->trip_type_attrs[indx].attr);
1273 /* create trip temp attribute */
1274 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1275 "trip_point_%d_temp", indx);
1277 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1278 tz->trip_temp_attrs[indx].attr.attr.name =
1279 tz->trip_temp_attrs[indx].name;
1280 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1281 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1282 if (mask & (1 << indx)) {
1283 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1284 tz->trip_temp_attrs[indx].attr.store =
1285 trip_point_temp_store;
1288 device_create_file(&tz->device,
1289 &tz->trip_temp_attrs[indx].attr);
1291 /* create Optional trip hyst attribute */
1292 if (!tz->ops->get_trip_hyst)
1294 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1295 "trip_point_%d_hyst", indx);
1297 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1298 tz->trip_hyst_attrs[indx].attr.attr.name =
1299 tz->trip_hyst_attrs[indx].name;
1300 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1301 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1302 if (tz->ops->set_trip_hyst) {
1303 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1304 tz->trip_hyst_attrs[indx].attr.store =
1305 trip_point_hyst_store;
1308 device_create_file(&tz->device,
1309 &tz->trip_hyst_attrs[indx].attr);
1314 static void remove_trip_attrs(struct thermal_zone_device *tz)
1318 for (indx = 0; indx < tz->trips; indx++) {
1319 device_remove_file(&tz->device,
1320 &tz->trip_type_attrs[indx].attr);
1321 device_remove_file(&tz->device,
1322 &tz->trip_temp_attrs[indx].attr);
1323 if (tz->ops->get_trip_hyst)
1324 device_remove_file(&tz->device,
1325 &tz->trip_hyst_attrs[indx].attr);
1327 kfree(tz->trip_type_attrs);
1328 kfree(tz->trip_temp_attrs);
1329 kfree(tz->trip_hyst_attrs);
1333 * thermal_zone_device_register - register a new thermal zone device
1334 * @type: the thermal zone device type
1335 * @trips: the number of trip points the thermal zone support
1336 * @mask: a bit string indicating the writeablility of trip points
1337 * @devdata: private device data
1338 * @ops: standard thermal zone device callbacks
1339 * @passive_delay: number of milliseconds to wait between polls when
1340 * performing passive cooling
1341 * @polling_delay: number of milliseconds to wait between polls when checking
1342 * whether trip points have been crossed (0 for interrupt
1345 * thermal_zone_device_unregister() must be called when the device is no
1346 * longer needed. The passive cooling depends on the .get_trend() return value.
1348 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1349 int trips, int mask, void *devdata,
1350 const struct thermal_zone_device_ops *ops,
1351 int passive_delay, int polling_delay)
1353 struct thermal_zone_device *tz;
1354 struct thermal_cooling_device *pos;
1355 enum thermal_trip_type trip_type;
1360 if (strlen(type) >= THERMAL_NAME_LENGTH)
1361 return ERR_PTR(-EINVAL);
1363 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1364 return ERR_PTR(-EINVAL);
1366 if (!ops || !ops->get_temp)
1367 return ERR_PTR(-EINVAL);
1369 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1371 return ERR_PTR(-ENOMEM);
1373 INIT_LIST_HEAD(&tz->thermal_instances);
1375 mutex_init(&tz->lock);
1376 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1379 return ERR_PTR(result);
1382 strcpy(tz->type, type);
1384 tz->device.class = &thermal_class;
1385 tz->devdata = devdata;
1387 tz->passive_delay = passive_delay;
1388 tz->polling_delay = polling_delay;
1390 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1391 result = device_register(&tz->device);
1393 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1395 return ERR_PTR(result);
1400 result = device_create_file(&tz->device, &dev_attr_type);
1405 result = device_create_file(&tz->device, &dev_attr_temp);
1409 if (ops->get_mode) {
1410 result = device_create_file(&tz->device, &dev_attr_mode);
1415 result = create_trip_attrs(tz, mask);
1419 for (count = 0; count < trips; count++) {
1420 tz->ops->get_trip_type(tz, count, &trip_type);
1421 if (trip_type == THERMAL_TRIP_PASSIVE)
1426 result = device_create_file(&tz->device,
1432 result = thermal_add_hwmon_sysfs(tz);
1436 mutex_lock(&thermal_list_lock);
1437 list_add_tail(&tz->node, &thermal_tz_list);
1439 list_for_each_entry(pos, &thermal_cdev_list, node) {
1440 result = ops->bind(tz, pos);
1444 mutex_unlock(&thermal_list_lock);
1446 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1448 thermal_zone_device_update(tz);
1454 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1455 device_unregister(&tz->device);
1456 return ERR_PTR(result);
1458 EXPORT_SYMBOL(thermal_zone_device_register);
1461 * thermal_device_unregister - removes the registered thermal zone device
1462 * @tz: the thermal zone device to remove
1464 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1466 struct thermal_cooling_device *cdev;
1467 struct thermal_zone_device *pos = NULL;
1472 mutex_lock(&thermal_list_lock);
1473 list_for_each_entry(pos, &thermal_tz_list, node)
1477 /* thermal zone device not found */
1478 mutex_unlock(&thermal_list_lock);
1481 list_del(&tz->node);
1482 if (tz->ops->unbind)
1483 list_for_each_entry(cdev, &thermal_cdev_list, node)
1484 tz->ops->unbind(tz, cdev);
1485 mutex_unlock(&thermal_list_lock);
1487 thermal_zone_device_set_polling(tz, 0);
1490 device_remove_file(&tz->device, &dev_attr_type);
1491 device_remove_file(&tz->device, &dev_attr_temp);
1492 if (tz->ops->get_mode)
1493 device_remove_file(&tz->device, &dev_attr_mode);
1494 remove_trip_attrs(tz);
1496 thermal_remove_hwmon_sysfs(tz);
1497 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1498 idr_destroy(&tz->idr);
1499 mutex_destroy(&tz->lock);
1500 device_unregister(&tz->device);
1503 EXPORT_SYMBOL(thermal_zone_device_unregister);
1506 static struct genl_family thermal_event_genl_family = {
1507 .id = GENL_ID_GENERATE,
1508 .name = THERMAL_GENL_FAMILY_NAME,
1509 .version = THERMAL_GENL_VERSION,
1510 .maxattr = THERMAL_GENL_ATTR_MAX,
1513 static struct genl_multicast_group thermal_event_mcgrp = {
1514 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1517 int thermal_generate_netlink_event(u32 orig, enum events event)
1519 struct sk_buff *skb;
1520 struct nlattr *attr;
1521 struct thermal_genl_event *thermal_event;
1525 static unsigned int thermal_event_seqnum;
1527 /* allocate memory */
1528 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1531 skb = genlmsg_new(size, GFP_ATOMIC);
1535 /* add the genetlink message header */
1536 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1537 &thermal_event_genl_family, 0,
1538 THERMAL_GENL_CMD_EVENT);
1545 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1546 sizeof(struct thermal_genl_event));
1553 thermal_event = nla_data(attr);
1554 if (!thermal_event) {
1559 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1561 thermal_event->orig = orig;
1562 thermal_event->event = event;
1564 /* send multicast genetlink message */
1565 result = genlmsg_end(skb, msg_header);
1571 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1573 pr_info("failed to send netlink event:%d\n", result);
1577 EXPORT_SYMBOL(thermal_generate_netlink_event);
1579 static int genetlink_init(void)
1583 result = genl_register_family(&thermal_event_genl_family);
1587 result = genl_register_mc_group(&thermal_event_genl_family,
1588 &thermal_event_mcgrp);
1590 genl_unregister_family(&thermal_event_genl_family);
1594 static void genetlink_exit(void)
1596 genl_unregister_family(&thermal_event_genl_family);
1598 #else /* !CONFIG_NET */
1599 static inline int genetlink_init(void) { return 0; }
1600 static inline void genetlink_exit(void) {}
1601 #endif /* !CONFIG_NET */
1603 static int __init thermal_init(void)
1607 result = class_register(&thermal_class);
1609 idr_destroy(&thermal_tz_idr);
1610 idr_destroy(&thermal_cdev_idr);
1611 mutex_destroy(&thermal_idr_lock);
1612 mutex_destroy(&thermal_list_lock);
1614 result = genetlink_init();
1618 static void __exit thermal_exit(void)
1620 class_unregister(&thermal_class);
1621 idr_destroy(&thermal_tz_idr);
1622 idr_destroy(&thermal_cdev_idr);
1623 mutex_destroy(&thermal_idr_lock);
1624 mutex_destroy(&thermal_list_lock);
1628 fs_initcall(thermal_init);
1629 module_exit(thermal_exit);