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 DEFINE_MUTEX(thermal_list_lock);
54 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
59 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
64 err = idr_get_new(idr, NULL, id);
67 if (unlikely(err == -EAGAIN))
69 else if (unlikely(err))
72 *id = *id & MAX_IDR_MASK;
76 static void release_idr(struct idr *idr, struct mutex *lock, int id)
85 /* sys I/F for thermal zone */
87 #define to_thermal_zone(_dev) \
88 container_of(_dev, struct thermal_zone_device, device)
91 type_show(struct device *dev, struct device_attribute *attr, char *buf)
93 struct thermal_zone_device *tz = to_thermal_zone(dev);
95 return sprintf(buf, "%s\n", tz->type);
99 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
101 struct thermal_zone_device *tz = to_thermal_zone(dev);
105 if (!tz->ops->get_temp)
108 ret = tz->ops->get_temp(tz, &temperature);
113 return sprintf(buf, "%ld\n", temperature);
117 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
119 struct thermal_zone_device *tz = to_thermal_zone(dev);
120 enum thermal_device_mode mode;
123 if (!tz->ops->get_mode)
126 result = tz->ops->get_mode(tz, &mode);
130 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
135 mode_store(struct device *dev, struct device_attribute *attr,
136 const char *buf, size_t count)
138 struct thermal_zone_device *tz = to_thermal_zone(dev);
141 if (!tz->ops->set_mode)
144 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
145 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
146 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
147 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
158 trip_point_type_show(struct device *dev, struct device_attribute *attr,
161 struct thermal_zone_device *tz = to_thermal_zone(dev);
162 enum thermal_trip_type type;
165 if (!tz->ops->get_trip_type)
168 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
171 result = tz->ops->get_trip_type(tz, trip, &type);
176 case THERMAL_TRIP_CRITICAL:
177 return sprintf(buf, "critical\n");
178 case THERMAL_TRIP_HOT:
179 return sprintf(buf, "hot\n");
180 case THERMAL_TRIP_PASSIVE:
181 return sprintf(buf, "passive\n");
182 case THERMAL_TRIP_ACTIVE:
183 return sprintf(buf, "active\n");
185 return sprintf(buf, "unknown\n");
190 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
191 const char *buf, size_t count)
193 struct thermal_zone_device *tz = to_thermal_zone(dev);
195 unsigned long temperature;
197 if (!tz->ops->set_trip_temp)
200 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
203 if (kstrtoul(buf, 10, &temperature))
206 ret = tz->ops->set_trip_temp(tz, trip, temperature);
208 return ret ? ret : count;
212 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
215 struct thermal_zone_device *tz = to_thermal_zone(dev);
219 if (!tz->ops->get_trip_temp)
222 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
225 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
230 return sprintf(buf, "%ld\n", temperature);
234 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
235 const char *buf, size_t count)
237 struct thermal_zone_device *tz = to_thermal_zone(dev);
239 unsigned long temperature;
241 if (!tz->ops->set_trip_hyst)
244 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
247 if (kstrtoul(buf, 10, &temperature))
251 * We are not doing any check on the 'temperature' value
252 * here. The driver implementing 'set_trip_hyst' has to
255 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
257 return ret ? ret : count;
261 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
264 struct thermal_zone_device *tz = to_thermal_zone(dev);
266 unsigned long temperature;
268 if (!tz->ops->get_trip_hyst)
271 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
274 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
276 return ret ? ret : sprintf(buf, "%ld\n", temperature);
280 passive_store(struct device *dev, struct device_attribute *attr,
281 const char *buf, size_t count)
283 struct thermal_zone_device *tz = to_thermal_zone(dev);
284 struct thermal_cooling_device *cdev = NULL;
287 if (!sscanf(buf, "%d\n", &state))
290 /* sanity check: values below 1000 millicelcius don't make sense
291 * and can cause the system to go into a thermal heart attack
293 if (state && state < 1000)
296 if (state && !tz->forced_passive) {
297 mutex_lock(&thermal_list_lock);
298 list_for_each_entry(cdev, &thermal_cdev_list, node) {
299 if (!strncmp("Processor", cdev->type,
300 sizeof("Processor")))
301 thermal_zone_bind_cooling_device(tz,
302 THERMAL_TRIPS_NONE, cdev,
306 mutex_unlock(&thermal_list_lock);
307 if (!tz->passive_delay)
308 tz->passive_delay = 1000;
309 } else if (!state && tz->forced_passive) {
310 mutex_lock(&thermal_list_lock);
311 list_for_each_entry(cdev, &thermal_cdev_list, node) {
312 if (!strncmp("Processor", cdev->type,
313 sizeof("Processor")))
314 thermal_zone_unbind_cooling_device(tz,
318 mutex_unlock(&thermal_list_lock);
319 tz->passive_delay = 0;
322 tz->forced_passive = state;
324 thermal_zone_device_update(tz);
330 passive_show(struct device *dev, struct device_attribute *attr,
333 struct thermal_zone_device *tz = to_thermal_zone(dev);
335 return sprintf(buf, "%d\n", tz->forced_passive);
338 static DEVICE_ATTR(type, 0444, type_show, NULL);
339 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
340 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
341 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
343 /* sys I/F for cooling device */
344 #define to_cooling_device(_dev) \
345 container_of(_dev, struct thermal_cooling_device, device)
348 thermal_cooling_device_type_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
351 struct thermal_cooling_device *cdev = to_cooling_device(dev);
353 return sprintf(buf, "%s\n", cdev->type);
357 thermal_cooling_device_max_state_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
360 struct thermal_cooling_device *cdev = to_cooling_device(dev);
364 ret = cdev->ops->get_max_state(cdev, &state);
367 return sprintf(buf, "%ld\n", state);
371 thermal_cooling_device_cur_state_show(struct device *dev,
372 struct device_attribute *attr, char *buf)
374 struct thermal_cooling_device *cdev = to_cooling_device(dev);
378 ret = cdev->ops->get_cur_state(cdev, &state);
381 return sprintf(buf, "%ld\n", state);
385 thermal_cooling_device_cur_state_store(struct device *dev,
386 struct device_attribute *attr,
387 const char *buf, size_t count)
389 struct thermal_cooling_device *cdev = to_cooling_device(dev);
393 if (!sscanf(buf, "%ld\n", &state))
399 result = cdev->ops->set_cur_state(cdev, state);
405 static struct device_attribute dev_attr_cdev_type =
406 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
407 static DEVICE_ATTR(max_state, 0444,
408 thermal_cooling_device_max_state_show, NULL);
409 static DEVICE_ATTR(cur_state, 0644,
410 thermal_cooling_device_cur_state_show,
411 thermal_cooling_device_cur_state_store);
414 thermal_cooling_device_trip_point_show(struct device *dev,
415 struct device_attribute *attr, char *buf)
417 struct thermal_instance *instance;
420 container_of(attr, struct thermal_instance, attr);
422 if (instance->trip == THERMAL_TRIPS_NONE)
423 return sprintf(buf, "-1\n");
425 return sprintf(buf, "%d\n", instance->trip);
428 /* Device management */
430 #if defined(CONFIG_THERMAL_HWMON)
433 #include <linux/hwmon.h>
435 /* thermal zone devices with the same type share one hwmon device */
436 struct thermal_hwmon_device {
437 char type[THERMAL_NAME_LENGTH];
438 struct device *device;
440 struct list_head tz_list;
441 struct list_head node;
444 struct thermal_hwmon_attr {
445 struct device_attribute attr;
449 /* one temperature input for each thermal zone */
450 struct thermal_hwmon_temp {
451 struct list_head hwmon_node;
452 struct thermal_zone_device *tz;
453 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
454 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
457 static LIST_HEAD(thermal_hwmon_list);
460 name_show(struct device *dev, struct device_attribute *attr, char *buf)
462 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
463 return sprintf(buf, "%s\n", hwmon->type);
465 static DEVICE_ATTR(name, 0444, name_show, NULL);
468 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
472 struct thermal_hwmon_attr *hwmon_attr
473 = container_of(attr, struct thermal_hwmon_attr, attr);
474 struct thermal_hwmon_temp *temp
475 = container_of(hwmon_attr, struct thermal_hwmon_temp,
477 struct thermal_zone_device *tz = temp->tz;
479 ret = tz->ops->get_temp(tz, &temperature);
484 return sprintf(buf, "%ld\n", temperature);
488 temp_crit_show(struct device *dev, struct device_attribute *attr,
491 struct thermal_hwmon_attr *hwmon_attr
492 = container_of(attr, struct thermal_hwmon_attr, attr);
493 struct thermal_hwmon_temp *temp
494 = container_of(hwmon_attr, struct thermal_hwmon_temp,
496 struct thermal_zone_device *tz = temp->tz;
500 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
504 return sprintf(buf, "%ld\n", temperature);
508 static struct thermal_hwmon_device *
509 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
511 struct thermal_hwmon_device *hwmon;
513 mutex_lock(&thermal_list_lock);
514 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
515 if (!strcmp(hwmon->type, tz->type)) {
516 mutex_unlock(&thermal_list_lock);
519 mutex_unlock(&thermal_list_lock);
524 /* Find the temperature input matching a given thermal zone */
525 static struct thermal_hwmon_temp *
526 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
527 const struct thermal_zone_device *tz)
529 struct thermal_hwmon_temp *temp;
531 mutex_lock(&thermal_list_lock);
532 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
533 if (temp->tz == tz) {
534 mutex_unlock(&thermal_list_lock);
537 mutex_unlock(&thermal_list_lock);
543 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
545 struct thermal_hwmon_device *hwmon;
546 struct thermal_hwmon_temp *temp;
547 int new_hwmon_device = 1;
550 hwmon = thermal_hwmon_lookup_by_type(tz);
552 new_hwmon_device = 0;
553 goto register_sys_interface;
556 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
560 INIT_LIST_HEAD(&hwmon->tz_list);
561 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
562 hwmon->device = hwmon_device_register(NULL);
563 if (IS_ERR(hwmon->device)) {
564 result = PTR_ERR(hwmon->device);
567 dev_set_drvdata(hwmon->device, hwmon);
568 result = device_create_file(hwmon->device, &dev_attr_name);
572 register_sys_interface:
573 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
576 goto unregister_name;
582 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
583 "temp%d_input", hwmon->count);
584 temp->temp_input.attr.attr.name = temp->temp_input.name;
585 temp->temp_input.attr.attr.mode = 0444;
586 temp->temp_input.attr.show = temp_input_show;
587 sysfs_attr_init(&temp->temp_input.attr.attr);
588 result = device_create_file(hwmon->device, &temp->temp_input.attr);
592 if (tz->ops->get_crit_temp) {
593 unsigned long temperature;
594 if (!tz->ops->get_crit_temp(tz, &temperature)) {
595 snprintf(temp->temp_crit.name,
596 sizeof(temp->temp_crit.name),
597 "temp%d_crit", hwmon->count);
598 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
599 temp->temp_crit.attr.attr.mode = 0444;
600 temp->temp_crit.attr.show = temp_crit_show;
601 sysfs_attr_init(&temp->temp_crit.attr.attr);
602 result = device_create_file(hwmon->device,
603 &temp->temp_crit.attr);
605 goto unregister_input;
609 mutex_lock(&thermal_list_lock);
610 if (new_hwmon_device)
611 list_add_tail(&hwmon->node, &thermal_hwmon_list);
612 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
613 mutex_unlock(&thermal_list_lock);
618 device_remove_file(hwmon->device, &temp->temp_input.attr);
622 if (new_hwmon_device) {
623 device_remove_file(hwmon->device, &dev_attr_name);
624 hwmon_device_unregister(hwmon->device);
627 if (new_hwmon_device)
634 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
636 struct thermal_hwmon_device *hwmon;
637 struct thermal_hwmon_temp *temp;
639 hwmon = thermal_hwmon_lookup_by_type(tz);
640 if (unlikely(!hwmon)) {
641 /* Should never happen... */
642 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
646 temp = thermal_hwmon_lookup_temp(hwmon, tz);
647 if (unlikely(!temp)) {
648 /* Should never happen... */
649 dev_dbg(&tz->device, "temperature input lookup failed!\n");
653 device_remove_file(hwmon->device, &temp->temp_input.attr);
654 if (tz->ops->get_crit_temp)
655 device_remove_file(hwmon->device, &temp->temp_crit.attr);
657 mutex_lock(&thermal_list_lock);
658 list_del(&temp->hwmon_node);
660 if (!list_empty(&hwmon->tz_list)) {
661 mutex_unlock(&thermal_list_lock);
664 list_del(&hwmon->node);
665 mutex_unlock(&thermal_list_lock);
667 device_remove_file(hwmon->device, &dev_attr_name);
668 hwmon_device_unregister(hwmon->device);
673 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
679 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
684 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
688 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
689 round_jiffies(msecs_to_jiffies(delay)));
691 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
692 msecs_to_jiffies(delay));
694 cancel_delayed_work(&tz->poll_queue);
697 static void thermal_zone_device_check(struct work_struct *work)
699 struct thermal_zone_device *tz = container_of(work, struct
702 thermal_zone_device_update(tz);
706 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
707 * @tz: thermal zone device
708 * @trip: indicates which trip point the cooling devices is
709 * associated with in this thermal zone.
710 * @cdev: thermal cooling device
712 * This function is usually called in the thermal zone device .bind callback.
714 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
716 struct thermal_cooling_device *cdev,
717 unsigned long upper, unsigned long lower)
719 struct thermal_instance *dev;
720 struct thermal_instance *pos;
721 struct thermal_zone_device *pos1;
722 struct thermal_cooling_device *pos2;
723 unsigned long max_state;
726 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
729 list_for_each_entry(pos1, &thermal_tz_list, node) {
733 list_for_each_entry(pos2, &thermal_cdev_list, node) {
738 if (tz != pos1 || cdev != pos2)
741 cdev->ops->get_max_state(cdev, &max_state);
743 /* lower default 0, upper default max_state */
744 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
745 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
747 if (lower > upper || upper > max_state)
751 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
759 dev->target = THERMAL_NO_TARGET;
761 result = get_idr(&tz->idr, &tz->lock, &dev->id);
765 sprintf(dev->name, "cdev%d", dev->id);
767 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
771 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
772 sysfs_attr_init(&dev->attr.attr);
773 dev->attr.attr.name = dev->attr_name;
774 dev->attr.attr.mode = 0444;
775 dev->attr.show = thermal_cooling_device_trip_point_show;
776 result = device_create_file(&tz->device, &dev->attr);
778 goto remove_symbol_link;
780 mutex_lock(&tz->lock);
781 mutex_lock(&cdev->lock);
782 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
783 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
788 list_add_tail(&dev->tz_node, &tz->thermal_instances);
789 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
791 mutex_unlock(&cdev->lock);
792 mutex_unlock(&tz->lock);
797 device_remove_file(&tz->device, &dev->attr);
799 sysfs_remove_link(&tz->device.kobj, dev->name);
801 release_idr(&tz->idr, &tz->lock, dev->id);
806 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
809 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
810 * @tz: thermal zone device
811 * @trip: indicates which trip point the cooling devices is
812 * associated with in this thermal zone.
813 * @cdev: thermal cooling device
815 * This function is usually called in the thermal zone device .unbind callback.
817 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
819 struct thermal_cooling_device *cdev)
821 struct thermal_instance *pos, *next;
823 mutex_lock(&tz->lock);
824 mutex_lock(&cdev->lock);
825 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
826 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
827 list_del(&pos->tz_node);
828 list_del(&pos->cdev_node);
829 mutex_unlock(&cdev->lock);
830 mutex_unlock(&tz->lock);
834 mutex_unlock(&cdev->lock);
835 mutex_unlock(&tz->lock);
840 device_remove_file(&tz->device, &pos->attr);
841 sysfs_remove_link(&tz->device.kobj, pos->name);
842 release_idr(&tz->idr, &tz->lock, pos->id);
846 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
848 static void thermal_release(struct device *dev)
850 struct thermal_zone_device *tz;
851 struct thermal_cooling_device *cdev;
853 if (!strncmp(dev_name(dev), "thermal_zone",
854 sizeof("thermal_zone") - 1)) {
855 tz = to_thermal_zone(dev);
858 cdev = to_cooling_device(dev);
863 static struct class thermal_class = {
865 .dev_release = thermal_release,
869 * thermal_cooling_device_register - register a new thermal cooling device
870 * @type: the thermal cooling device type.
871 * @devdata: device private data.
872 * @ops: standard thermal cooling devices callbacks.
874 struct thermal_cooling_device *
875 thermal_cooling_device_register(char *type, void *devdata,
876 const struct thermal_cooling_device_ops *ops)
878 struct thermal_cooling_device *cdev;
879 struct thermal_zone_device *pos;
882 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
883 return ERR_PTR(-EINVAL);
885 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
887 return ERR_PTR(-EINVAL);
889 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
891 return ERR_PTR(-ENOMEM);
893 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
896 return ERR_PTR(result);
899 strcpy(cdev->type, type ? : "");
900 mutex_init(&cdev->lock);
901 INIT_LIST_HEAD(&cdev->thermal_instances);
903 cdev->updated = true;
904 cdev->device.class = &thermal_class;
905 cdev->devdata = devdata;
906 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
907 result = device_register(&cdev->device);
909 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
911 return ERR_PTR(result);
916 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
921 result = device_create_file(&cdev->device, &dev_attr_max_state);
925 result = device_create_file(&cdev->device, &dev_attr_cur_state);
929 mutex_lock(&thermal_list_lock);
930 list_add(&cdev->node, &thermal_cdev_list);
931 list_for_each_entry(pos, &thermal_tz_list, node) {
934 result = pos->ops->bind(pos, cdev);
939 mutex_unlock(&thermal_list_lock);
945 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
946 device_unregister(&cdev->device);
947 return ERR_PTR(result);
949 EXPORT_SYMBOL(thermal_cooling_device_register);
952 * thermal_cooling_device_unregister - removes the registered thermal cooling device
953 * @cdev: the thermal cooling device to remove.
955 * thermal_cooling_device_unregister() must be called when the device is no
958 void thermal_cooling_device_unregister(struct
959 thermal_cooling_device
962 struct thermal_zone_device *tz;
963 struct thermal_cooling_device *pos = NULL;
968 mutex_lock(&thermal_list_lock);
969 list_for_each_entry(pos, &thermal_cdev_list, node)
973 /* thermal cooling device not found */
974 mutex_unlock(&thermal_list_lock);
977 list_del(&cdev->node);
978 list_for_each_entry(tz, &thermal_tz_list, node) {
979 if (!tz->ops->unbind)
981 tz->ops->unbind(tz, cdev);
983 mutex_unlock(&thermal_list_lock);
985 device_remove_file(&cdev->device, &dev_attr_cdev_type);
986 device_remove_file(&cdev->device, &dev_attr_max_state);
987 device_remove_file(&cdev->device, &dev_attr_cur_state);
989 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
990 device_unregister(&cdev->device);
993 EXPORT_SYMBOL(thermal_cooling_device_unregister);
995 static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
997 struct thermal_instance *instance;
998 unsigned long target = 0;
1000 /* cooling device is updated*/
1004 mutex_lock(&cdev->lock);
1005 /* Make sure cdev enters the deepest cooling state */
1006 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1007 if (instance->target == THERMAL_NO_TARGET)
1009 if (instance->target > target)
1010 target = instance->target;
1012 mutex_unlock(&cdev->lock);
1013 cdev->ops->set_cur_state(cdev, target);
1014 cdev->updated = true;
1017 static void thermal_zone_do_update(struct thermal_zone_device *tz)
1019 struct thermal_instance *instance;
1021 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
1022 thermal_cdev_do_update(instance->cdev);
1026 * Cooling algorithm for both active and passive cooling
1028 * 1. if the temperature is higher than a trip point,
1029 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1030 * state for this trip point
1031 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1032 * state for this trip point
1034 * 2. if the temperature is lower than a trip point, use lower
1035 * cooling state for this trip point
1037 * Note that this behaves the same as the previous passive cooling
1041 static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1042 int trip, long temp)
1044 struct thermal_instance *instance;
1045 struct thermal_cooling_device *cdev = NULL;
1046 unsigned long cur_state, max_state;
1048 enum thermal_trip_type trip_type;
1049 enum thermal_trend trend;
1051 if (trip == THERMAL_TRIPS_NONE) {
1052 trip_temp = tz->forced_passive;
1053 trip_type = THERMAL_TRIPS_NONE;
1055 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1056 tz->ops->get_trip_type(tz, trip, &trip_type);
1059 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1061 * compare the current temperature and previous temperature
1062 * to get the thermal trend, if no special requirement
1064 if (tz->temperature > tz->last_temperature)
1065 trend = THERMAL_TREND_RAISING;
1066 else if (tz->temperature < tz->last_temperature)
1067 trend = THERMAL_TREND_DROPPING;
1069 trend = THERMAL_TREND_STABLE;
1072 if (temp >= trip_temp) {
1073 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1074 if (instance->trip != trip)
1077 cdev = instance->cdev;
1079 cdev->ops->get_cur_state(cdev, &cur_state);
1080 cdev->ops->get_max_state(cdev, &max_state);
1082 if (trend == THERMAL_TREND_RAISING) {
1083 cur_state = cur_state < instance->upper ?
1084 (cur_state + 1) : instance->upper;
1085 } else if (trend == THERMAL_TREND_DROPPING) {
1086 cur_state = cur_state > instance->lower ?
1087 (cur_state - 1) : instance->lower;
1090 /* activate a passive thermal instance */
1091 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1092 trip_type == THERMAL_TRIPS_NONE) &&
1093 instance->target == THERMAL_NO_TARGET)
1096 instance->target = cur_state;
1097 cdev->updated = false; /* cooling device needs update */
1099 } else { /* below trip */
1100 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1101 if (instance->trip != trip)
1104 /* Do not use the inactive thermal instance */
1105 if (instance->target == THERMAL_NO_TARGET)
1107 cdev = instance->cdev;
1108 cdev->ops->get_cur_state(cdev, &cur_state);
1110 cur_state = cur_state > instance->lower ?
1111 (cur_state - 1) : THERMAL_NO_TARGET;
1113 /* deactivate a passive thermal instance */
1114 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1115 trip_type == THERMAL_TRIPS_NONE) &&
1116 cur_state == THERMAL_NO_TARGET)
1118 instance->target = cur_state;
1119 cdev->updated = false; /* cooling device needs update */
1126 * thermal_zone_device_update - force an update of a thermal zone's state
1127 * @ttz: the thermal zone to update
1130 void thermal_zone_device_update(struct thermal_zone_device *tz)
1133 long temp, trip_temp;
1134 enum thermal_trip_type trip_type;
1136 mutex_lock(&tz->lock);
1138 if (tz->ops->get_temp(tz, &temp)) {
1139 /* get_temp failed - retry it later */
1140 pr_warn("failed to read out thermal zone %d\n", tz->id);
1144 tz->last_temperature = tz->temperature;
1145 tz->temperature = temp;
1147 for (count = 0; count < tz->trips; count++) {
1148 tz->ops->get_trip_type(tz, count, &trip_type);
1149 tz->ops->get_trip_temp(tz, count, &trip_temp);
1151 switch (trip_type) {
1152 case THERMAL_TRIP_CRITICAL:
1153 if (temp >= trip_temp) {
1154 if (tz->ops->notify)
1155 ret = tz->ops->notify(tz, count,
1158 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1160 orderly_poweroff(true);
1164 case THERMAL_TRIP_HOT:
1165 if (temp >= trip_temp)
1166 if (tz->ops->notify)
1167 tz->ops->notify(tz, count, trip_type);
1169 case THERMAL_TRIP_ACTIVE:
1170 thermal_zone_trip_update(tz, count, temp);
1172 case THERMAL_TRIP_PASSIVE:
1173 if (temp >= trip_temp || tz->passive)
1174 thermal_zone_trip_update(tz, count, temp);
1179 if (tz->forced_passive)
1180 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
1181 thermal_zone_do_update(tz);
1185 thermal_zone_device_set_polling(tz, tz->passive_delay);
1186 else if (tz->polling_delay)
1187 thermal_zone_device_set_polling(tz, tz->polling_delay);
1189 thermal_zone_device_set_polling(tz, 0);
1190 mutex_unlock(&tz->lock);
1192 EXPORT_SYMBOL(thermal_zone_device_update);
1195 * create_trip_attrs - create attributes for trip points
1196 * @tz: the thermal zone device
1197 * @mask: Writeable trip point bitmap.
1199 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1202 int size = sizeof(struct thermal_attr) * tz->trips;
1204 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1205 if (!tz->trip_type_attrs)
1208 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1209 if (!tz->trip_temp_attrs) {
1210 kfree(tz->trip_type_attrs);
1214 if (tz->ops->get_trip_hyst) {
1215 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1216 if (!tz->trip_hyst_attrs) {
1217 kfree(tz->trip_type_attrs);
1218 kfree(tz->trip_temp_attrs);
1224 for (indx = 0; indx < tz->trips; indx++) {
1225 /* create trip type attribute */
1226 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1227 "trip_point_%d_type", indx);
1229 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1230 tz->trip_type_attrs[indx].attr.attr.name =
1231 tz->trip_type_attrs[indx].name;
1232 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1233 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1235 device_create_file(&tz->device,
1236 &tz->trip_type_attrs[indx].attr);
1238 /* create trip temp attribute */
1239 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1240 "trip_point_%d_temp", indx);
1242 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1243 tz->trip_temp_attrs[indx].attr.attr.name =
1244 tz->trip_temp_attrs[indx].name;
1245 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1246 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1247 if (mask & (1 << indx)) {
1248 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1249 tz->trip_temp_attrs[indx].attr.store =
1250 trip_point_temp_store;
1253 device_create_file(&tz->device,
1254 &tz->trip_temp_attrs[indx].attr);
1256 /* create Optional trip hyst attribute */
1257 if (!tz->ops->get_trip_hyst)
1259 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1260 "trip_point_%d_hyst", indx);
1262 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1263 tz->trip_hyst_attrs[indx].attr.attr.name =
1264 tz->trip_hyst_attrs[indx].name;
1265 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1266 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1267 if (tz->ops->set_trip_hyst) {
1268 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1269 tz->trip_hyst_attrs[indx].attr.store =
1270 trip_point_hyst_store;
1273 device_create_file(&tz->device,
1274 &tz->trip_hyst_attrs[indx].attr);
1279 static void remove_trip_attrs(struct thermal_zone_device *tz)
1283 for (indx = 0; indx < tz->trips; indx++) {
1284 device_remove_file(&tz->device,
1285 &tz->trip_type_attrs[indx].attr);
1286 device_remove_file(&tz->device,
1287 &tz->trip_temp_attrs[indx].attr);
1288 if (tz->ops->get_trip_hyst)
1289 device_remove_file(&tz->device,
1290 &tz->trip_hyst_attrs[indx].attr);
1292 kfree(tz->trip_type_attrs);
1293 kfree(tz->trip_temp_attrs);
1294 kfree(tz->trip_hyst_attrs);
1298 * thermal_zone_device_register - register a new thermal zone device
1299 * @type: the thermal zone device type
1300 * @trips: the number of trip points the thermal zone support
1301 * @mask: a bit string indicating the writeablility of trip points
1302 * @devdata: private device data
1303 * @ops: standard thermal zone device callbacks
1304 * @passive_delay: number of milliseconds to wait between polls when
1305 * performing passive cooling
1306 * @polling_delay: number of milliseconds to wait between polls when checking
1307 * whether trip points have been crossed (0 for interrupt
1310 * thermal_zone_device_unregister() must be called when the device is no
1311 * longer needed. The passive cooling depends on the .get_trend() return value.
1313 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1314 int trips, int mask, void *devdata,
1315 const struct thermal_zone_device_ops *ops,
1316 int passive_delay, int polling_delay)
1318 struct thermal_zone_device *tz;
1319 struct thermal_cooling_device *pos;
1320 enum thermal_trip_type trip_type;
1325 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1326 return ERR_PTR(-EINVAL);
1328 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1329 return ERR_PTR(-EINVAL);
1331 if (!ops || !ops->get_temp)
1332 return ERR_PTR(-EINVAL);
1334 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1336 return ERR_PTR(-ENOMEM);
1338 INIT_LIST_HEAD(&tz->thermal_instances);
1340 mutex_init(&tz->lock);
1341 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1344 return ERR_PTR(result);
1347 strcpy(tz->type, type ? : "");
1349 tz->device.class = &thermal_class;
1350 tz->devdata = devdata;
1352 tz->passive_delay = passive_delay;
1353 tz->polling_delay = polling_delay;
1355 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1356 result = device_register(&tz->device);
1358 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1360 return ERR_PTR(result);
1365 result = device_create_file(&tz->device, &dev_attr_type);
1370 result = device_create_file(&tz->device, &dev_attr_temp);
1374 if (ops->get_mode) {
1375 result = device_create_file(&tz->device, &dev_attr_mode);
1380 result = create_trip_attrs(tz, mask);
1384 for (count = 0; count < trips; count++) {
1385 tz->ops->get_trip_type(tz, count, &trip_type);
1386 if (trip_type == THERMAL_TRIP_PASSIVE)
1391 result = device_create_file(&tz->device,
1397 result = thermal_add_hwmon_sysfs(tz);
1401 mutex_lock(&thermal_list_lock);
1402 list_add_tail(&tz->node, &thermal_tz_list);
1404 list_for_each_entry(pos, &thermal_cdev_list, node) {
1405 result = ops->bind(tz, pos);
1409 mutex_unlock(&thermal_list_lock);
1411 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1413 thermal_zone_device_update(tz);
1419 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1420 device_unregister(&tz->device);
1421 return ERR_PTR(result);
1423 EXPORT_SYMBOL(thermal_zone_device_register);
1426 * thermal_device_unregister - removes the registered thermal zone device
1427 * @tz: the thermal zone device to remove
1429 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1431 struct thermal_cooling_device *cdev;
1432 struct thermal_zone_device *pos = NULL;
1437 mutex_lock(&thermal_list_lock);
1438 list_for_each_entry(pos, &thermal_tz_list, node)
1442 /* thermal zone device not found */
1443 mutex_unlock(&thermal_list_lock);
1446 list_del(&tz->node);
1447 if (tz->ops->unbind)
1448 list_for_each_entry(cdev, &thermal_cdev_list, node)
1449 tz->ops->unbind(tz, cdev);
1450 mutex_unlock(&thermal_list_lock);
1452 thermal_zone_device_set_polling(tz, 0);
1455 device_remove_file(&tz->device, &dev_attr_type);
1456 device_remove_file(&tz->device, &dev_attr_temp);
1457 if (tz->ops->get_mode)
1458 device_remove_file(&tz->device, &dev_attr_mode);
1459 remove_trip_attrs(tz);
1461 thermal_remove_hwmon_sysfs(tz);
1462 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1463 idr_destroy(&tz->idr);
1464 mutex_destroy(&tz->lock);
1465 device_unregister(&tz->device);
1468 EXPORT_SYMBOL(thermal_zone_device_unregister);
1471 static struct genl_family thermal_event_genl_family = {
1472 .id = GENL_ID_GENERATE,
1473 .name = THERMAL_GENL_FAMILY_NAME,
1474 .version = THERMAL_GENL_VERSION,
1475 .maxattr = THERMAL_GENL_ATTR_MAX,
1478 static struct genl_multicast_group thermal_event_mcgrp = {
1479 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1482 int thermal_generate_netlink_event(u32 orig, enum events event)
1484 struct sk_buff *skb;
1485 struct nlattr *attr;
1486 struct thermal_genl_event *thermal_event;
1490 static unsigned int thermal_event_seqnum;
1492 /* allocate memory */
1493 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1496 skb = genlmsg_new(size, GFP_ATOMIC);
1500 /* add the genetlink message header */
1501 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1502 &thermal_event_genl_family, 0,
1503 THERMAL_GENL_CMD_EVENT);
1510 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1511 sizeof(struct thermal_genl_event));
1518 thermal_event = nla_data(attr);
1519 if (!thermal_event) {
1524 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1526 thermal_event->orig = orig;
1527 thermal_event->event = event;
1529 /* send multicast genetlink message */
1530 result = genlmsg_end(skb, msg_header);
1536 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1538 pr_info("failed to send netlink event:%d\n", result);
1542 EXPORT_SYMBOL(thermal_generate_netlink_event);
1544 static int genetlink_init(void)
1548 result = genl_register_family(&thermal_event_genl_family);
1552 result = genl_register_mc_group(&thermal_event_genl_family,
1553 &thermal_event_mcgrp);
1555 genl_unregister_family(&thermal_event_genl_family);
1559 static void genetlink_exit(void)
1561 genl_unregister_family(&thermal_event_genl_family);
1563 #else /* !CONFIG_NET */
1564 static inline int genetlink_init(void) { return 0; }
1565 static inline void genetlink_exit(void) {}
1566 #endif /* !CONFIG_NET */
1568 static int __init thermal_init(void)
1572 result = class_register(&thermal_class);
1574 idr_destroy(&thermal_tz_idr);
1575 idr_destroy(&thermal_cdev_idr);
1576 mutex_destroy(&thermal_idr_lock);
1577 mutex_destroy(&thermal_list_lock);
1579 result = genetlink_init();
1583 static void __exit thermal_exit(void)
1585 class_unregister(&thermal_class);
1586 idr_destroy(&thermal_tz_idr);
1587 idr_destroy(&thermal_cdev_idr);
1588 mutex_destroy(&thermal_idr_lock);
1589 mutex_destroy(&thermal_list_lock);
1593 fs_initcall(thermal_init);
1594 module_exit(thermal_exit);