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 int get_tz_trend(struct thermal_zone_device *tz, int trip)
87 enum thermal_trend trend;
89 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
90 if (tz->temperature > tz->last_temperature)
91 trend = THERMAL_TREND_RAISING;
92 else if (tz->temperature < tz->last_temperature)
93 trend = THERMAL_TREND_DROPPING;
95 trend = THERMAL_TREND_STABLE;
100 EXPORT_SYMBOL(get_tz_trend);
102 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
103 struct thermal_cooling_device *cdev, int trip)
105 struct thermal_instance *pos = NULL;
106 struct thermal_instance *target_instance = NULL;
108 mutex_lock(&tz->lock);
109 mutex_lock(&cdev->lock);
111 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
112 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
113 target_instance = pos;
118 mutex_unlock(&cdev->lock);
119 mutex_unlock(&tz->lock);
121 return target_instance;
123 EXPORT_SYMBOL(get_thermal_instance);
125 /* sys I/F for thermal zone */
127 #define to_thermal_zone(_dev) \
128 container_of(_dev, struct thermal_zone_device, device)
131 type_show(struct device *dev, struct device_attribute *attr, char *buf)
133 struct thermal_zone_device *tz = to_thermal_zone(dev);
135 return sprintf(buf, "%s\n", tz->type);
139 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
141 struct thermal_zone_device *tz = to_thermal_zone(dev);
145 if (!tz->ops->get_temp)
148 ret = tz->ops->get_temp(tz, &temperature);
153 return sprintf(buf, "%ld\n", temperature);
157 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
159 struct thermal_zone_device *tz = to_thermal_zone(dev);
160 enum thermal_device_mode mode;
163 if (!tz->ops->get_mode)
166 result = tz->ops->get_mode(tz, &mode);
170 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
175 mode_store(struct device *dev, struct device_attribute *attr,
176 const char *buf, size_t count)
178 struct thermal_zone_device *tz = to_thermal_zone(dev);
181 if (!tz->ops->set_mode)
184 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
185 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
186 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
187 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
198 trip_point_type_show(struct device *dev, struct device_attribute *attr,
201 struct thermal_zone_device *tz = to_thermal_zone(dev);
202 enum thermal_trip_type type;
205 if (!tz->ops->get_trip_type)
208 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
211 result = tz->ops->get_trip_type(tz, trip, &type);
216 case THERMAL_TRIP_CRITICAL:
217 return sprintf(buf, "critical\n");
218 case THERMAL_TRIP_HOT:
219 return sprintf(buf, "hot\n");
220 case THERMAL_TRIP_PASSIVE:
221 return sprintf(buf, "passive\n");
222 case THERMAL_TRIP_ACTIVE:
223 return sprintf(buf, "active\n");
225 return sprintf(buf, "unknown\n");
230 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
231 const char *buf, size_t count)
233 struct thermal_zone_device *tz = to_thermal_zone(dev);
235 unsigned long temperature;
237 if (!tz->ops->set_trip_temp)
240 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
243 if (kstrtoul(buf, 10, &temperature))
246 ret = tz->ops->set_trip_temp(tz, trip, temperature);
248 return ret ? ret : count;
252 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
255 struct thermal_zone_device *tz = to_thermal_zone(dev);
259 if (!tz->ops->get_trip_temp)
262 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
265 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
270 return sprintf(buf, "%ld\n", temperature);
274 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
275 const char *buf, size_t count)
277 struct thermal_zone_device *tz = to_thermal_zone(dev);
279 unsigned long temperature;
281 if (!tz->ops->set_trip_hyst)
284 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
287 if (kstrtoul(buf, 10, &temperature))
291 * We are not doing any check on the 'temperature' value
292 * here. The driver implementing 'set_trip_hyst' has to
295 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
297 return ret ? ret : count;
301 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
304 struct thermal_zone_device *tz = to_thermal_zone(dev);
306 unsigned long temperature;
308 if (!tz->ops->get_trip_hyst)
311 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
314 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
316 return ret ? ret : sprintf(buf, "%ld\n", temperature);
320 passive_store(struct device *dev, struct device_attribute *attr,
321 const char *buf, size_t count)
323 struct thermal_zone_device *tz = to_thermal_zone(dev);
324 struct thermal_cooling_device *cdev = NULL;
327 if (!sscanf(buf, "%d\n", &state))
330 /* sanity check: values below 1000 millicelcius don't make sense
331 * and can cause the system to go into a thermal heart attack
333 if (state && state < 1000)
336 if (state && !tz->forced_passive) {
337 mutex_lock(&thermal_list_lock);
338 list_for_each_entry(cdev, &thermal_cdev_list, node) {
339 if (!strncmp("Processor", cdev->type,
340 sizeof("Processor")))
341 thermal_zone_bind_cooling_device(tz,
342 THERMAL_TRIPS_NONE, cdev,
346 mutex_unlock(&thermal_list_lock);
347 if (!tz->passive_delay)
348 tz->passive_delay = 1000;
349 } else if (!state && tz->forced_passive) {
350 mutex_lock(&thermal_list_lock);
351 list_for_each_entry(cdev, &thermal_cdev_list, node) {
352 if (!strncmp("Processor", cdev->type,
353 sizeof("Processor")))
354 thermal_zone_unbind_cooling_device(tz,
358 mutex_unlock(&thermal_list_lock);
359 tz->passive_delay = 0;
362 tz->forced_passive = state;
364 thermal_zone_device_update(tz);
370 passive_show(struct device *dev, struct device_attribute *attr,
373 struct thermal_zone_device *tz = to_thermal_zone(dev);
375 return sprintf(buf, "%d\n", tz->forced_passive);
378 static DEVICE_ATTR(type, 0444, type_show, NULL);
379 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
380 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
381 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
383 /* sys I/F for cooling device */
384 #define to_cooling_device(_dev) \
385 container_of(_dev, struct thermal_cooling_device, device)
388 thermal_cooling_device_type_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
391 struct thermal_cooling_device *cdev = to_cooling_device(dev);
393 return sprintf(buf, "%s\n", cdev->type);
397 thermal_cooling_device_max_state_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
400 struct thermal_cooling_device *cdev = to_cooling_device(dev);
404 ret = cdev->ops->get_max_state(cdev, &state);
407 return sprintf(buf, "%ld\n", state);
411 thermal_cooling_device_cur_state_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
414 struct thermal_cooling_device *cdev = to_cooling_device(dev);
418 ret = cdev->ops->get_cur_state(cdev, &state);
421 return sprintf(buf, "%ld\n", state);
425 thermal_cooling_device_cur_state_store(struct device *dev,
426 struct device_attribute *attr,
427 const char *buf, size_t count)
429 struct thermal_cooling_device *cdev = to_cooling_device(dev);
433 if (!sscanf(buf, "%ld\n", &state))
439 result = cdev->ops->set_cur_state(cdev, state);
445 static struct device_attribute dev_attr_cdev_type =
446 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
447 static DEVICE_ATTR(max_state, 0444,
448 thermal_cooling_device_max_state_show, NULL);
449 static DEVICE_ATTR(cur_state, 0644,
450 thermal_cooling_device_cur_state_show,
451 thermal_cooling_device_cur_state_store);
454 thermal_cooling_device_trip_point_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
457 struct thermal_instance *instance;
460 container_of(attr, struct thermal_instance, attr);
462 if (instance->trip == THERMAL_TRIPS_NONE)
463 return sprintf(buf, "-1\n");
465 return sprintf(buf, "%d\n", instance->trip);
468 /* Device management */
470 #if defined(CONFIG_THERMAL_HWMON)
473 #include <linux/hwmon.h>
475 /* thermal zone devices with the same type share one hwmon device */
476 struct thermal_hwmon_device {
477 char type[THERMAL_NAME_LENGTH];
478 struct device *device;
480 struct list_head tz_list;
481 struct list_head node;
484 struct thermal_hwmon_attr {
485 struct device_attribute attr;
489 /* one temperature input for each thermal zone */
490 struct thermal_hwmon_temp {
491 struct list_head hwmon_node;
492 struct thermal_zone_device *tz;
493 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
494 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
497 static LIST_HEAD(thermal_hwmon_list);
500 name_show(struct device *dev, struct device_attribute *attr, char *buf)
502 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
503 return sprintf(buf, "%s\n", hwmon->type);
505 static DEVICE_ATTR(name, 0444, name_show, NULL);
508 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
512 struct thermal_hwmon_attr *hwmon_attr
513 = container_of(attr, struct thermal_hwmon_attr, attr);
514 struct thermal_hwmon_temp *temp
515 = container_of(hwmon_attr, struct thermal_hwmon_temp,
517 struct thermal_zone_device *tz = temp->tz;
519 ret = tz->ops->get_temp(tz, &temperature);
524 return sprintf(buf, "%ld\n", temperature);
528 temp_crit_show(struct device *dev, struct device_attribute *attr,
531 struct thermal_hwmon_attr *hwmon_attr
532 = container_of(attr, struct thermal_hwmon_attr, attr);
533 struct thermal_hwmon_temp *temp
534 = container_of(hwmon_attr, struct thermal_hwmon_temp,
536 struct thermal_zone_device *tz = temp->tz;
540 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
544 return sprintf(buf, "%ld\n", temperature);
548 static struct thermal_hwmon_device *
549 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
551 struct thermal_hwmon_device *hwmon;
553 mutex_lock(&thermal_list_lock);
554 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
555 if (!strcmp(hwmon->type, tz->type)) {
556 mutex_unlock(&thermal_list_lock);
559 mutex_unlock(&thermal_list_lock);
564 /* Find the temperature input matching a given thermal zone */
565 static struct thermal_hwmon_temp *
566 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
567 const struct thermal_zone_device *tz)
569 struct thermal_hwmon_temp *temp;
571 mutex_lock(&thermal_list_lock);
572 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
573 if (temp->tz == tz) {
574 mutex_unlock(&thermal_list_lock);
577 mutex_unlock(&thermal_list_lock);
583 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
585 struct thermal_hwmon_device *hwmon;
586 struct thermal_hwmon_temp *temp;
587 int new_hwmon_device = 1;
590 hwmon = thermal_hwmon_lookup_by_type(tz);
592 new_hwmon_device = 0;
593 goto register_sys_interface;
596 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
600 INIT_LIST_HEAD(&hwmon->tz_list);
601 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
602 hwmon->device = hwmon_device_register(NULL);
603 if (IS_ERR(hwmon->device)) {
604 result = PTR_ERR(hwmon->device);
607 dev_set_drvdata(hwmon->device, hwmon);
608 result = device_create_file(hwmon->device, &dev_attr_name);
612 register_sys_interface:
613 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
616 goto unregister_name;
622 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
623 "temp%d_input", hwmon->count);
624 temp->temp_input.attr.attr.name = temp->temp_input.name;
625 temp->temp_input.attr.attr.mode = 0444;
626 temp->temp_input.attr.show = temp_input_show;
627 sysfs_attr_init(&temp->temp_input.attr.attr);
628 result = device_create_file(hwmon->device, &temp->temp_input.attr);
632 if (tz->ops->get_crit_temp) {
633 unsigned long temperature;
634 if (!tz->ops->get_crit_temp(tz, &temperature)) {
635 snprintf(temp->temp_crit.name,
636 sizeof(temp->temp_crit.name),
637 "temp%d_crit", hwmon->count);
638 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
639 temp->temp_crit.attr.attr.mode = 0444;
640 temp->temp_crit.attr.show = temp_crit_show;
641 sysfs_attr_init(&temp->temp_crit.attr.attr);
642 result = device_create_file(hwmon->device,
643 &temp->temp_crit.attr);
645 goto unregister_input;
649 mutex_lock(&thermal_list_lock);
650 if (new_hwmon_device)
651 list_add_tail(&hwmon->node, &thermal_hwmon_list);
652 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
653 mutex_unlock(&thermal_list_lock);
658 device_remove_file(hwmon->device, &temp->temp_input.attr);
662 if (new_hwmon_device) {
663 device_remove_file(hwmon->device, &dev_attr_name);
664 hwmon_device_unregister(hwmon->device);
667 if (new_hwmon_device)
674 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
676 struct thermal_hwmon_device *hwmon;
677 struct thermal_hwmon_temp *temp;
679 hwmon = thermal_hwmon_lookup_by_type(tz);
680 if (unlikely(!hwmon)) {
681 /* Should never happen... */
682 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
686 temp = thermal_hwmon_lookup_temp(hwmon, tz);
687 if (unlikely(!temp)) {
688 /* Should never happen... */
689 dev_dbg(&tz->device, "temperature input lookup failed!\n");
693 device_remove_file(hwmon->device, &temp->temp_input.attr);
694 if (tz->ops->get_crit_temp)
695 device_remove_file(hwmon->device, &temp->temp_crit.attr);
697 mutex_lock(&thermal_list_lock);
698 list_del(&temp->hwmon_node);
700 if (!list_empty(&hwmon->tz_list)) {
701 mutex_unlock(&thermal_list_lock);
704 list_del(&hwmon->node);
705 mutex_unlock(&thermal_list_lock);
707 device_remove_file(hwmon->device, &dev_attr_name);
708 hwmon_device_unregister(hwmon->device);
713 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
719 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
724 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
728 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
729 round_jiffies(msecs_to_jiffies(delay)));
731 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
732 msecs_to_jiffies(delay));
734 cancel_delayed_work(&tz->poll_queue);
737 static void thermal_zone_device_check(struct work_struct *work)
739 struct thermal_zone_device *tz = container_of(work, struct
742 thermal_zone_device_update(tz);
746 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
747 * @tz: thermal zone device
748 * @trip: indicates which trip point the cooling devices is
749 * associated with in this thermal zone.
750 * @cdev: thermal cooling device
752 * This function is usually called in the thermal zone device .bind callback.
754 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
756 struct thermal_cooling_device *cdev,
757 unsigned long upper, unsigned long lower)
759 struct thermal_instance *dev;
760 struct thermal_instance *pos;
761 struct thermal_zone_device *pos1;
762 struct thermal_cooling_device *pos2;
763 unsigned long max_state;
766 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
769 list_for_each_entry(pos1, &thermal_tz_list, node) {
773 list_for_each_entry(pos2, &thermal_cdev_list, node) {
778 if (tz != pos1 || cdev != pos2)
781 cdev->ops->get_max_state(cdev, &max_state);
783 /* lower default 0, upper default max_state */
784 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
785 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
787 if (lower > upper || upper > max_state)
791 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
799 dev->target = THERMAL_NO_TARGET;
801 result = get_idr(&tz->idr, &tz->lock, &dev->id);
805 sprintf(dev->name, "cdev%d", dev->id);
807 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
811 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
812 sysfs_attr_init(&dev->attr.attr);
813 dev->attr.attr.name = dev->attr_name;
814 dev->attr.attr.mode = 0444;
815 dev->attr.show = thermal_cooling_device_trip_point_show;
816 result = device_create_file(&tz->device, &dev->attr);
818 goto remove_symbol_link;
820 mutex_lock(&tz->lock);
821 mutex_lock(&cdev->lock);
822 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
823 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
828 list_add_tail(&dev->tz_node, &tz->thermal_instances);
829 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
831 mutex_unlock(&cdev->lock);
832 mutex_unlock(&tz->lock);
837 device_remove_file(&tz->device, &dev->attr);
839 sysfs_remove_link(&tz->device.kobj, dev->name);
841 release_idr(&tz->idr, &tz->lock, dev->id);
846 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
849 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
850 * @tz: thermal zone device
851 * @trip: indicates which trip point the cooling devices is
852 * associated with in this thermal zone.
853 * @cdev: thermal cooling device
855 * This function is usually called in the thermal zone device .unbind callback.
857 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
859 struct thermal_cooling_device *cdev)
861 struct thermal_instance *pos, *next;
863 mutex_lock(&tz->lock);
864 mutex_lock(&cdev->lock);
865 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
866 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
867 list_del(&pos->tz_node);
868 list_del(&pos->cdev_node);
869 mutex_unlock(&cdev->lock);
870 mutex_unlock(&tz->lock);
874 mutex_unlock(&cdev->lock);
875 mutex_unlock(&tz->lock);
880 device_remove_file(&tz->device, &pos->attr);
881 sysfs_remove_link(&tz->device.kobj, pos->name);
882 release_idr(&tz->idr, &tz->lock, pos->id);
886 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
888 static void thermal_release(struct device *dev)
890 struct thermal_zone_device *tz;
891 struct thermal_cooling_device *cdev;
893 if (!strncmp(dev_name(dev), "thermal_zone",
894 sizeof("thermal_zone") - 1)) {
895 tz = to_thermal_zone(dev);
898 cdev = to_cooling_device(dev);
903 static struct class thermal_class = {
905 .dev_release = thermal_release,
909 * thermal_cooling_device_register - register a new thermal cooling device
910 * @type: the thermal cooling device type.
911 * @devdata: device private data.
912 * @ops: standard thermal cooling devices callbacks.
914 struct thermal_cooling_device *
915 thermal_cooling_device_register(char *type, void *devdata,
916 const struct thermal_cooling_device_ops *ops)
918 struct thermal_cooling_device *cdev;
919 struct thermal_zone_device *pos;
922 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
923 return ERR_PTR(-EINVAL);
925 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
927 return ERR_PTR(-EINVAL);
929 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
931 return ERR_PTR(-ENOMEM);
933 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
936 return ERR_PTR(result);
939 strcpy(cdev->type, type ? : "");
940 mutex_init(&cdev->lock);
941 INIT_LIST_HEAD(&cdev->thermal_instances);
943 cdev->updated = true;
944 cdev->device.class = &thermal_class;
945 cdev->devdata = devdata;
946 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
947 result = device_register(&cdev->device);
949 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
951 return ERR_PTR(result);
956 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
961 result = device_create_file(&cdev->device, &dev_attr_max_state);
965 result = device_create_file(&cdev->device, &dev_attr_cur_state);
969 mutex_lock(&thermal_list_lock);
970 list_add(&cdev->node, &thermal_cdev_list);
971 list_for_each_entry(pos, &thermal_tz_list, node) {
974 result = pos->ops->bind(pos, cdev);
979 mutex_unlock(&thermal_list_lock);
985 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
986 device_unregister(&cdev->device);
987 return ERR_PTR(result);
989 EXPORT_SYMBOL(thermal_cooling_device_register);
992 * thermal_cooling_device_unregister - removes the registered thermal cooling device
993 * @cdev: the thermal cooling device to remove.
995 * thermal_cooling_device_unregister() must be called when the device is no
998 void thermal_cooling_device_unregister(struct
999 thermal_cooling_device
1002 struct thermal_zone_device *tz;
1003 struct thermal_cooling_device *pos = NULL;
1008 mutex_lock(&thermal_list_lock);
1009 list_for_each_entry(pos, &thermal_cdev_list, node)
1013 /* thermal cooling device not found */
1014 mutex_unlock(&thermal_list_lock);
1017 list_del(&cdev->node);
1018 list_for_each_entry(tz, &thermal_tz_list, node) {
1019 if (!tz->ops->unbind)
1021 tz->ops->unbind(tz, cdev);
1023 mutex_unlock(&thermal_list_lock);
1025 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1026 device_remove_file(&cdev->device, &dev_attr_max_state);
1027 device_remove_file(&cdev->device, &dev_attr_cur_state);
1029 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1030 device_unregister(&cdev->device);
1033 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1035 static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
1037 struct thermal_instance *instance;
1038 unsigned long target = 0;
1040 /* cooling device is updated*/
1044 mutex_lock(&cdev->lock);
1045 /* Make sure cdev enters the deepest cooling state */
1046 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1047 if (instance->target == THERMAL_NO_TARGET)
1049 if (instance->target > target)
1050 target = instance->target;
1052 mutex_unlock(&cdev->lock);
1053 cdev->ops->set_cur_state(cdev, target);
1054 cdev->updated = true;
1057 static void thermal_zone_do_update(struct thermal_zone_device *tz)
1059 struct thermal_instance *instance;
1061 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
1062 thermal_cdev_do_update(instance->cdev);
1066 * Cooling algorithm for both active and passive cooling
1068 * 1. if the temperature is higher than a trip point,
1069 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1070 * state for this trip point
1071 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1072 * state for this trip point
1074 * 2. if the temperature is lower than a trip point, use lower
1075 * cooling state for this trip point
1077 * Note that this behaves the same as the previous passive cooling
1081 static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1082 int trip, long temp)
1084 struct thermal_instance *instance;
1085 struct thermal_cooling_device *cdev = NULL;
1086 unsigned long cur_state, max_state;
1088 enum thermal_trip_type trip_type;
1089 enum thermal_trend trend;
1091 if (trip == THERMAL_TRIPS_NONE) {
1092 trip_temp = tz->forced_passive;
1093 trip_type = THERMAL_TRIPS_NONE;
1095 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1096 tz->ops->get_trip_type(tz, trip, &trip_type);
1099 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1101 * compare the current temperature and previous temperature
1102 * to get the thermal trend, if no special requirement
1104 if (tz->temperature > tz->last_temperature)
1105 trend = THERMAL_TREND_RAISING;
1106 else if (tz->temperature < tz->last_temperature)
1107 trend = THERMAL_TREND_DROPPING;
1109 trend = THERMAL_TREND_STABLE;
1112 if (temp >= trip_temp) {
1113 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1114 if (instance->trip != trip)
1117 cdev = instance->cdev;
1119 cdev->ops->get_cur_state(cdev, &cur_state);
1120 cdev->ops->get_max_state(cdev, &max_state);
1122 if (trend == THERMAL_TREND_RAISING) {
1123 cur_state = cur_state < instance->upper ?
1124 (cur_state + 1) : instance->upper;
1125 } else if (trend == THERMAL_TREND_DROPPING) {
1126 cur_state = cur_state > instance->lower ?
1127 (cur_state - 1) : instance->lower;
1130 /* activate a passive thermal instance */
1131 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1132 trip_type == THERMAL_TRIPS_NONE) &&
1133 instance->target == THERMAL_NO_TARGET)
1136 instance->target = cur_state;
1137 cdev->updated = false; /* cooling device needs update */
1139 } else { /* below trip */
1140 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1141 if (instance->trip != trip)
1144 /* Do not use the inactive thermal instance */
1145 if (instance->target == THERMAL_NO_TARGET)
1147 cdev = instance->cdev;
1148 cdev->ops->get_cur_state(cdev, &cur_state);
1150 cur_state = cur_state > instance->lower ?
1151 (cur_state - 1) : THERMAL_NO_TARGET;
1153 /* deactivate a passive thermal instance */
1154 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1155 trip_type == THERMAL_TRIPS_NONE) &&
1156 cur_state == THERMAL_NO_TARGET)
1158 instance->target = cur_state;
1159 cdev->updated = false; /* cooling device needs update */
1166 * thermal_zone_device_update - force an update of a thermal zone's state
1167 * @ttz: the thermal zone to update
1170 void thermal_zone_device_update(struct thermal_zone_device *tz)
1173 long temp, trip_temp;
1174 enum thermal_trip_type trip_type;
1176 mutex_lock(&tz->lock);
1178 if (tz->ops->get_temp(tz, &temp)) {
1179 /* get_temp failed - retry it later */
1180 pr_warn("failed to read out thermal zone %d\n", tz->id);
1184 tz->last_temperature = tz->temperature;
1185 tz->temperature = temp;
1187 for (count = 0; count < tz->trips; count++) {
1188 tz->ops->get_trip_type(tz, count, &trip_type);
1189 tz->ops->get_trip_temp(tz, count, &trip_temp);
1191 switch (trip_type) {
1192 case THERMAL_TRIP_CRITICAL:
1193 if (temp >= trip_temp) {
1194 if (tz->ops->notify)
1195 ret = tz->ops->notify(tz, count,
1198 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1200 orderly_poweroff(true);
1204 case THERMAL_TRIP_HOT:
1205 if (temp >= trip_temp)
1206 if (tz->ops->notify)
1207 tz->ops->notify(tz, count, trip_type);
1209 case THERMAL_TRIP_ACTIVE:
1210 thermal_zone_trip_update(tz, count, temp);
1212 case THERMAL_TRIP_PASSIVE:
1213 if (temp >= trip_temp || tz->passive)
1214 thermal_zone_trip_update(tz, count, temp);
1219 if (tz->forced_passive)
1220 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
1221 thermal_zone_do_update(tz);
1225 thermal_zone_device_set_polling(tz, tz->passive_delay);
1226 else if (tz->polling_delay)
1227 thermal_zone_device_set_polling(tz, tz->polling_delay);
1229 thermal_zone_device_set_polling(tz, 0);
1230 mutex_unlock(&tz->lock);
1232 EXPORT_SYMBOL(thermal_zone_device_update);
1235 * create_trip_attrs - create attributes for trip points
1236 * @tz: the thermal zone device
1237 * @mask: Writeable trip point bitmap.
1239 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1242 int size = sizeof(struct thermal_attr) * tz->trips;
1244 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1245 if (!tz->trip_type_attrs)
1248 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1249 if (!tz->trip_temp_attrs) {
1250 kfree(tz->trip_type_attrs);
1254 if (tz->ops->get_trip_hyst) {
1255 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1256 if (!tz->trip_hyst_attrs) {
1257 kfree(tz->trip_type_attrs);
1258 kfree(tz->trip_temp_attrs);
1264 for (indx = 0; indx < tz->trips; indx++) {
1265 /* create trip type attribute */
1266 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1267 "trip_point_%d_type", indx);
1269 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1270 tz->trip_type_attrs[indx].attr.attr.name =
1271 tz->trip_type_attrs[indx].name;
1272 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1273 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1275 device_create_file(&tz->device,
1276 &tz->trip_type_attrs[indx].attr);
1278 /* create trip temp attribute */
1279 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1280 "trip_point_%d_temp", indx);
1282 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1283 tz->trip_temp_attrs[indx].attr.attr.name =
1284 tz->trip_temp_attrs[indx].name;
1285 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1286 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1287 if (mask & (1 << indx)) {
1288 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1289 tz->trip_temp_attrs[indx].attr.store =
1290 trip_point_temp_store;
1293 device_create_file(&tz->device,
1294 &tz->trip_temp_attrs[indx].attr);
1296 /* create Optional trip hyst attribute */
1297 if (!tz->ops->get_trip_hyst)
1299 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1300 "trip_point_%d_hyst", indx);
1302 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1303 tz->trip_hyst_attrs[indx].attr.attr.name =
1304 tz->trip_hyst_attrs[indx].name;
1305 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1306 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1307 if (tz->ops->set_trip_hyst) {
1308 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1309 tz->trip_hyst_attrs[indx].attr.store =
1310 trip_point_hyst_store;
1313 device_create_file(&tz->device,
1314 &tz->trip_hyst_attrs[indx].attr);
1319 static void remove_trip_attrs(struct thermal_zone_device *tz)
1323 for (indx = 0; indx < tz->trips; indx++) {
1324 device_remove_file(&tz->device,
1325 &tz->trip_type_attrs[indx].attr);
1326 device_remove_file(&tz->device,
1327 &tz->trip_temp_attrs[indx].attr);
1328 if (tz->ops->get_trip_hyst)
1329 device_remove_file(&tz->device,
1330 &tz->trip_hyst_attrs[indx].attr);
1332 kfree(tz->trip_type_attrs);
1333 kfree(tz->trip_temp_attrs);
1334 kfree(tz->trip_hyst_attrs);
1338 * thermal_zone_device_register - register a new thermal zone device
1339 * @type: the thermal zone device type
1340 * @trips: the number of trip points the thermal zone support
1341 * @mask: a bit string indicating the writeablility of trip points
1342 * @devdata: private device data
1343 * @ops: standard thermal zone device callbacks
1344 * @passive_delay: number of milliseconds to wait between polls when
1345 * performing passive cooling
1346 * @polling_delay: number of milliseconds to wait between polls when checking
1347 * whether trip points have been crossed (0 for interrupt
1350 * thermal_zone_device_unregister() must be called when the device is no
1351 * longer needed. The passive cooling depends on the .get_trend() return value.
1353 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1354 int trips, int mask, void *devdata,
1355 const struct thermal_zone_device_ops *ops,
1356 int passive_delay, int polling_delay)
1358 struct thermal_zone_device *tz;
1359 struct thermal_cooling_device *pos;
1360 enum thermal_trip_type trip_type;
1365 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1366 return ERR_PTR(-EINVAL);
1368 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1369 return ERR_PTR(-EINVAL);
1371 if (!ops || !ops->get_temp)
1372 return ERR_PTR(-EINVAL);
1374 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1376 return ERR_PTR(-ENOMEM);
1378 INIT_LIST_HEAD(&tz->thermal_instances);
1380 mutex_init(&tz->lock);
1381 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1384 return ERR_PTR(result);
1387 strcpy(tz->type, type ? : "");
1389 tz->device.class = &thermal_class;
1390 tz->devdata = devdata;
1392 tz->passive_delay = passive_delay;
1393 tz->polling_delay = polling_delay;
1395 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1396 result = device_register(&tz->device);
1398 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1400 return ERR_PTR(result);
1405 result = device_create_file(&tz->device, &dev_attr_type);
1410 result = device_create_file(&tz->device, &dev_attr_temp);
1414 if (ops->get_mode) {
1415 result = device_create_file(&tz->device, &dev_attr_mode);
1420 result = create_trip_attrs(tz, mask);
1424 for (count = 0; count < trips; count++) {
1425 tz->ops->get_trip_type(tz, count, &trip_type);
1426 if (trip_type == THERMAL_TRIP_PASSIVE)
1431 result = device_create_file(&tz->device,
1437 result = thermal_add_hwmon_sysfs(tz);
1441 mutex_lock(&thermal_list_lock);
1442 list_add_tail(&tz->node, &thermal_tz_list);
1444 list_for_each_entry(pos, &thermal_cdev_list, node) {
1445 result = ops->bind(tz, pos);
1449 mutex_unlock(&thermal_list_lock);
1451 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1453 thermal_zone_device_update(tz);
1459 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1460 device_unregister(&tz->device);
1461 return ERR_PTR(result);
1463 EXPORT_SYMBOL(thermal_zone_device_register);
1466 * thermal_device_unregister - removes the registered thermal zone device
1467 * @tz: the thermal zone device to remove
1469 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1471 struct thermal_cooling_device *cdev;
1472 struct thermal_zone_device *pos = NULL;
1477 mutex_lock(&thermal_list_lock);
1478 list_for_each_entry(pos, &thermal_tz_list, node)
1482 /* thermal zone device not found */
1483 mutex_unlock(&thermal_list_lock);
1486 list_del(&tz->node);
1487 if (tz->ops->unbind)
1488 list_for_each_entry(cdev, &thermal_cdev_list, node)
1489 tz->ops->unbind(tz, cdev);
1490 mutex_unlock(&thermal_list_lock);
1492 thermal_zone_device_set_polling(tz, 0);
1495 device_remove_file(&tz->device, &dev_attr_type);
1496 device_remove_file(&tz->device, &dev_attr_temp);
1497 if (tz->ops->get_mode)
1498 device_remove_file(&tz->device, &dev_attr_mode);
1499 remove_trip_attrs(tz);
1501 thermal_remove_hwmon_sysfs(tz);
1502 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1503 idr_destroy(&tz->idr);
1504 mutex_destroy(&tz->lock);
1505 device_unregister(&tz->device);
1508 EXPORT_SYMBOL(thermal_zone_device_unregister);
1511 static struct genl_family thermal_event_genl_family = {
1512 .id = GENL_ID_GENERATE,
1513 .name = THERMAL_GENL_FAMILY_NAME,
1514 .version = THERMAL_GENL_VERSION,
1515 .maxattr = THERMAL_GENL_ATTR_MAX,
1518 static struct genl_multicast_group thermal_event_mcgrp = {
1519 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1522 int thermal_generate_netlink_event(u32 orig, enum events event)
1524 struct sk_buff *skb;
1525 struct nlattr *attr;
1526 struct thermal_genl_event *thermal_event;
1530 static unsigned int thermal_event_seqnum;
1532 /* allocate memory */
1533 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1536 skb = genlmsg_new(size, GFP_ATOMIC);
1540 /* add the genetlink message header */
1541 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1542 &thermal_event_genl_family, 0,
1543 THERMAL_GENL_CMD_EVENT);
1550 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1551 sizeof(struct thermal_genl_event));
1558 thermal_event = nla_data(attr);
1559 if (!thermal_event) {
1564 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1566 thermal_event->orig = orig;
1567 thermal_event->event = event;
1569 /* send multicast genetlink message */
1570 result = genlmsg_end(skb, msg_header);
1576 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1578 pr_info("failed to send netlink event:%d\n", result);
1582 EXPORT_SYMBOL(thermal_generate_netlink_event);
1584 static int genetlink_init(void)
1588 result = genl_register_family(&thermal_event_genl_family);
1592 result = genl_register_mc_group(&thermal_event_genl_family,
1593 &thermal_event_mcgrp);
1595 genl_unregister_family(&thermal_event_genl_family);
1599 static void genetlink_exit(void)
1601 genl_unregister_family(&thermal_event_genl_family);
1603 #else /* !CONFIG_NET */
1604 static inline int genetlink_init(void) { return 0; }
1605 static inline void genetlink_exit(void) {}
1606 #endif /* !CONFIG_NET */
1608 static int __init thermal_init(void)
1612 result = class_register(&thermal_class);
1614 idr_destroy(&thermal_tz_idr);
1615 idr_destroy(&thermal_cdev_idr);
1616 mutex_destroy(&thermal_idr_lock);
1617 mutex_destroy(&thermal_list_lock);
1619 result = genetlink_init();
1623 static void __exit thermal_exit(void)
1625 class_unregister(&thermal_class);
1626 idr_destroy(&thermal_tz_idr);
1627 idr_destroy(&thermal_cdev_idr);
1628 mutex_destroy(&thermal_idr_lock);
1629 mutex_destroy(&thermal_list_lock);
1633 fs_initcall(thermal_init);
1634 module_exit(thermal_exit);