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");
44 #define THERMAL_NO_TARGET -1UL
46 * This structure is used to describe the behavior of
47 * a certain cooling device on a certain trip point
48 * in a certain thermal zone
50 struct thermal_instance {
52 char name[THERMAL_NAME_LENGTH];
53 struct thermal_zone_device *tz;
54 struct thermal_cooling_device *cdev;
56 unsigned long upper; /* Highest cooling state for this trip point */
57 unsigned long lower; /* Lowest cooling state for this trip point */
58 unsigned long target; /* expected cooling state */
59 char attr_name[THERMAL_NAME_LENGTH];
60 struct device_attribute attr;
61 struct list_head tz_node; /* node in tz->thermal_instances */
62 struct list_head cdev_node; /* node in cdev->thermal_instances */
65 static DEFINE_IDR(thermal_tz_idr);
66 static DEFINE_IDR(thermal_cdev_idr);
67 static DEFINE_MUTEX(thermal_idr_lock);
69 static LIST_HEAD(thermal_tz_list);
70 static LIST_HEAD(thermal_cdev_list);
71 static DEFINE_MUTEX(thermal_list_lock);
73 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
78 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
83 err = idr_get_new(idr, NULL, id);
86 if (unlikely(err == -EAGAIN))
88 else if (unlikely(err))
91 *id = *id & MAX_IDR_MASK;
95 static void release_idr(struct idr *idr, struct mutex *lock, int id)
104 /* sys I/F for thermal zone */
106 #define to_thermal_zone(_dev) \
107 container_of(_dev, struct thermal_zone_device, device)
110 type_show(struct device *dev, struct device_attribute *attr, char *buf)
112 struct thermal_zone_device *tz = to_thermal_zone(dev);
114 return sprintf(buf, "%s\n", tz->type);
118 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
120 struct thermal_zone_device *tz = to_thermal_zone(dev);
124 if (!tz->ops->get_temp)
127 ret = tz->ops->get_temp(tz, &temperature);
132 return sprintf(buf, "%ld\n", temperature);
136 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
138 struct thermal_zone_device *tz = to_thermal_zone(dev);
139 enum thermal_device_mode mode;
142 if (!tz->ops->get_mode)
145 result = tz->ops->get_mode(tz, &mode);
149 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
154 mode_store(struct device *dev, struct device_attribute *attr,
155 const char *buf, size_t count)
157 struct thermal_zone_device *tz = to_thermal_zone(dev);
160 if (!tz->ops->set_mode)
163 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
164 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
165 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
166 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
177 trip_point_type_show(struct device *dev, struct device_attribute *attr,
180 struct thermal_zone_device *tz = to_thermal_zone(dev);
181 enum thermal_trip_type type;
184 if (!tz->ops->get_trip_type)
187 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
190 result = tz->ops->get_trip_type(tz, trip, &type);
195 case THERMAL_TRIP_CRITICAL:
196 return sprintf(buf, "critical\n");
197 case THERMAL_TRIP_HOT:
198 return sprintf(buf, "hot\n");
199 case THERMAL_TRIP_PASSIVE:
200 return sprintf(buf, "passive\n");
201 case THERMAL_TRIP_ACTIVE:
202 return sprintf(buf, "active\n");
204 return sprintf(buf, "unknown\n");
209 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
210 const char *buf, size_t count)
212 struct thermal_zone_device *tz = to_thermal_zone(dev);
214 unsigned long temperature;
216 if (!tz->ops->set_trip_temp)
219 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
222 if (kstrtoul(buf, 10, &temperature))
225 ret = tz->ops->set_trip_temp(tz, trip, temperature);
227 return ret ? ret : count;
231 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
234 struct thermal_zone_device *tz = to_thermal_zone(dev);
238 if (!tz->ops->get_trip_temp)
241 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
244 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
249 return sprintf(buf, "%ld\n", temperature);
253 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
254 const char *buf, size_t count)
256 struct thermal_zone_device *tz = to_thermal_zone(dev);
258 unsigned long temperature;
260 if (!tz->ops->set_trip_hyst)
263 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
266 if (kstrtoul(buf, 10, &temperature))
270 * We are not doing any check on the 'temperature' value
271 * here. The driver implementing 'set_trip_hyst' has to
274 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
276 return ret ? ret : count;
280 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
283 struct thermal_zone_device *tz = to_thermal_zone(dev);
285 unsigned long temperature;
287 if (!tz->ops->get_trip_hyst)
290 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
293 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
295 return ret ? ret : sprintf(buf, "%ld\n", temperature);
299 passive_store(struct device *dev, struct device_attribute *attr,
300 const char *buf, size_t count)
302 struct thermal_zone_device *tz = to_thermal_zone(dev);
303 struct thermal_cooling_device *cdev = NULL;
306 if (!sscanf(buf, "%d\n", &state))
309 /* sanity check: values below 1000 millicelcius don't make sense
310 * and can cause the system to go into a thermal heart attack
312 if (state && state < 1000)
315 if (state && !tz->forced_passive) {
316 mutex_lock(&thermal_list_lock);
317 list_for_each_entry(cdev, &thermal_cdev_list, node) {
318 if (!strncmp("Processor", cdev->type,
319 sizeof("Processor")))
320 thermal_zone_bind_cooling_device(tz,
321 THERMAL_TRIPS_NONE, cdev,
325 mutex_unlock(&thermal_list_lock);
326 if (!tz->passive_delay)
327 tz->passive_delay = 1000;
328 } else if (!state && tz->forced_passive) {
329 mutex_lock(&thermal_list_lock);
330 list_for_each_entry(cdev, &thermal_cdev_list, node) {
331 if (!strncmp("Processor", cdev->type,
332 sizeof("Processor")))
333 thermal_zone_unbind_cooling_device(tz,
337 mutex_unlock(&thermal_list_lock);
338 tz->passive_delay = 0;
341 tz->forced_passive = state;
343 thermal_zone_device_update(tz);
349 passive_show(struct device *dev, struct device_attribute *attr,
352 struct thermal_zone_device *tz = to_thermal_zone(dev);
354 return sprintf(buf, "%d\n", tz->forced_passive);
357 static DEVICE_ATTR(type, 0444, type_show, NULL);
358 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
359 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
360 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
362 /* sys I/F for cooling device */
363 #define to_cooling_device(_dev) \
364 container_of(_dev, struct thermal_cooling_device, device)
367 thermal_cooling_device_type_show(struct device *dev,
368 struct device_attribute *attr, char *buf)
370 struct thermal_cooling_device *cdev = to_cooling_device(dev);
372 return sprintf(buf, "%s\n", cdev->type);
376 thermal_cooling_device_max_state_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
379 struct thermal_cooling_device *cdev = to_cooling_device(dev);
383 ret = cdev->ops->get_max_state(cdev, &state);
386 return sprintf(buf, "%ld\n", state);
390 thermal_cooling_device_cur_state_show(struct device *dev,
391 struct device_attribute *attr, char *buf)
393 struct thermal_cooling_device *cdev = to_cooling_device(dev);
397 ret = cdev->ops->get_cur_state(cdev, &state);
400 return sprintf(buf, "%ld\n", state);
404 thermal_cooling_device_cur_state_store(struct device *dev,
405 struct device_attribute *attr,
406 const char *buf, size_t count)
408 struct thermal_cooling_device *cdev = to_cooling_device(dev);
412 if (!sscanf(buf, "%ld\n", &state))
418 result = cdev->ops->set_cur_state(cdev, state);
424 static struct device_attribute dev_attr_cdev_type =
425 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
426 static DEVICE_ATTR(max_state, 0444,
427 thermal_cooling_device_max_state_show, NULL);
428 static DEVICE_ATTR(cur_state, 0644,
429 thermal_cooling_device_cur_state_show,
430 thermal_cooling_device_cur_state_store);
433 thermal_cooling_device_trip_point_show(struct device *dev,
434 struct device_attribute *attr, char *buf)
436 struct thermal_instance *instance;
439 container_of(attr, struct thermal_instance, attr);
441 if (instance->trip == THERMAL_TRIPS_NONE)
442 return sprintf(buf, "-1\n");
444 return sprintf(buf, "%d\n", instance->trip);
447 /* Device management */
449 #if defined(CONFIG_THERMAL_HWMON)
452 #include <linux/hwmon.h>
454 /* thermal zone devices with the same type share one hwmon device */
455 struct thermal_hwmon_device {
456 char type[THERMAL_NAME_LENGTH];
457 struct device *device;
459 struct list_head tz_list;
460 struct list_head node;
463 struct thermal_hwmon_attr {
464 struct device_attribute attr;
468 /* one temperature input for each thermal zone */
469 struct thermal_hwmon_temp {
470 struct list_head hwmon_node;
471 struct thermal_zone_device *tz;
472 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
473 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
476 static LIST_HEAD(thermal_hwmon_list);
479 name_show(struct device *dev, struct device_attribute *attr, char *buf)
481 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
482 return sprintf(buf, "%s\n", hwmon->type);
484 static DEVICE_ATTR(name, 0444, name_show, NULL);
487 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
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;
498 ret = tz->ops->get_temp(tz, &temperature);
503 return sprintf(buf, "%ld\n", temperature);
507 temp_crit_show(struct device *dev, struct device_attribute *attr,
510 struct thermal_hwmon_attr *hwmon_attr
511 = container_of(attr, struct thermal_hwmon_attr, attr);
512 struct thermal_hwmon_temp *temp
513 = container_of(hwmon_attr, struct thermal_hwmon_temp,
515 struct thermal_zone_device *tz = temp->tz;
519 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
523 return sprintf(buf, "%ld\n", temperature);
527 static struct thermal_hwmon_device *
528 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
530 struct thermal_hwmon_device *hwmon;
532 mutex_lock(&thermal_list_lock);
533 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
534 if (!strcmp(hwmon->type, tz->type)) {
535 mutex_unlock(&thermal_list_lock);
538 mutex_unlock(&thermal_list_lock);
543 /* Find the temperature input matching a given thermal zone */
544 static struct thermal_hwmon_temp *
545 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
546 const struct thermal_zone_device *tz)
548 struct thermal_hwmon_temp *temp;
550 mutex_lock(&thermal_list_lock);
551 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
552 if (temp->tz == tz) {
553 mutex_unlock(&thermal_list_lock);
556 mutex_unlock(&thermal_list_lock);
562 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
564 struct thermal_hwmon_device *hwmon;
565 struct thermal_hwmon_temp *temp;
566 int new_hwmon_device = 1;
569 hwmon = thermal_hwmon_lookup_by_type(tz);
571 new_hwmon_device = 0;
572 goto register_sys_interface;
575 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
579 INIT_LIST_HEAD(&hwmon->tz_list);
580 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
581 hwmon->device = hwmon_device_register(NULL);
582 if (IS_ERR(hwmon->device)) {
583 result = PTR_ERR(hwmon->device);
586 dev_set_drvdata(hwmon->device, hwmon);
587 result = device_create_file(hwmon->device, &dev_attr_name);
591 register_sys_interface:
592 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
595 goto unregister_name;
601 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
602 "temp%d_input", hwmon->count);
603 temp->temp_input.attr.attr.name = temp->temp_input.name;
604 temp->temp_input.attr.attr.mode = 0444;
605 temp->temp_input.attr.show = temp_input_show;
606 sysfs_attr_init(&temp->temp_input.attr.attr);
607 result = device_create_file(hwmon->device, &temp->temp_input.attr);
611 if (tz->ops->get_crit_temp) {
612 unsigned long temperature;
613 if (!tz->ops->get_crit_temp(tz, &temperature)) {
614 snprintf(temp->temp_crit.name,
615 sizeof(temp->temp_crit.name),
616 "temp%d_crit", hwmon->count);
617 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
618 temp->temp_crit.attr.attr.mode = 0444;
619 temp->temp_crit.attr.show = temp_crit_show;
620 sysfs_attr_init(&temp->temp_crit.attr.attr);
621 result = device_create_file(hwmon->device,
622 &temp->temp_crit.attr);
624 goto unregister_input;
628 mutex_lock(&thermal_list_lock);
629 if (new_hwmon_device)
630 list_add_tail(&hwmon->node, &thermal_hwmon_list);
631 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
632 mutex_unlock(&thermal_list_lock);
637 device_remove_file(hwmon->device, &temp->temp_input.attr);
641 if (new_hwmon_device) {
642 device_remove_file(hwmon->device, &dev_attr_name);
643 hwmon_device_unregister(hwmon->device);
646 if (new_hwmon_device)
653 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
655 struct thermal_hwmon_device *hwmon;
656 struct thermal_hwmon_temp *temp;
658 hwmon = thermal_hwmon_lookup_by_type(tz);
659 if (unlikely(!hwmon)) {
660 /* Should never happen... */
661 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
665 temp = thermal_hwmon_lookup_temp(hwmon, tz);
666 if (unlikely(!temp)) {
667 /* Should never happen... */
668 dev_dbg(&tz->device, "temperature input lookup failed!\n");
672 device_remove_file(hwmon->device, &temp->temp_input.attr);
673 if (tz->ops->get_crit_temp)
674 device_remove_file(hwmon->device, &temp->temp_crit.attr);
676 mutex_lock(&thermal_list_lock);
677 list_del(&temp->hwmon_node);
679 if (!list_empty(&hwmon->tz_list)) {
680 mutex_unlock(&thermal_list_lock);
683 list_del(&hwmon->node);
684 mutex_unlock(&thermal_list_lock);
686 device_remove_file(hwmon->device, &dev_attr_name);
687 hwmon_device_unregister(hwmon->device);
692 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
698 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
703 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
707 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
708 round_jiffies(msecs_to_jiffies(delay)));
710 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
711 msecs_to_jiffies(delay));
713 cancel_delayed_work(&tz->poll_queue);
716 static void thermal_zone_device_check(struct work_struct *work)
718 struct thermal_zone_device *tz = container_of(work, struct
721 thermal_zone_device_update(tz);
725 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
726 * @tz: thermal zone device
727 * @trip: indicates which trip point the cooling devices is
728 * associated with in this thermal zone.
729 * @cdev: thermal cooling device
731 * This function is usually called in the thermal zone device .bind callback.
733 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
735 struct thermal_cooling_device *cdev,
736 unsigned long upper, unsigned long lower)
738 struct thermal_instance *dev;
739 struct thermal_instance *pos;
740 struct thermal_zone_device *pos1;
741 struct thermal_cooling_device *pos2;
742 unsigned long max_state;
745 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
748 list_for_each_entry(pos1, &thermal_tz_list, node) {
752 list_for_each_entry(pos2, &thermal_cdev_list, node) {
757 if (tz != pos1 || cdev != pos2)
760 cdev->ops->get_max_state(cdev, &max_state);
762 /* lower default 0, upper default max_state */
763 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
764 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
766 if (lower > upper || upper > max_state)
770 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
778 dev->target = THERMAL_NO_TARGET;
780 result = get_idr(&tz->idr, &tz->lock, &dev->id);
784 sprintf(dev->name, "cdev%d", dev->id);
786 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
790 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
791 sysfs_attr_init(&dev->attr.attr);
792 dev->attr.attr.name = dev->attr_name;
793 dev->attr.attr.mode = 0444;
794 dev->attr.show = thermal_cooling_device_trip_point_show;
795 result = device_create_file(&tz->device, &dev->attr);
797 goto remove_symbol_link;
799 mutex_lock(&tz->lock);
800 mutex_lock(&cdev->lock);
801 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
802 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
807 list_add_tail(&dev->tz_node, &tz->thermal_instances);
808 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
810 mutex_unlock(&cdev->lock);
811 mutex_unlock(&tz->lock);
816 device_remove_file(&tz->device, &dev->attr);
818 sysfs_remove_link(&tz->device.kobj, dev->name);
820 release_idr(&tz->idr, &tz->lock, dev->id);
825 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
828 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
829 * @tz: thermal zone device
830 * @trip: indicates which trip point the cooling devices is
831 * associated with in this thermal zone.
832 * @cdev: thermal cooling device
834 * This function is usually called in the thermal zone device .unbind callback.
836 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
838 struct thermal_cooling_device *cdev)
840 struct thermal_instance *pos, *next;
842 mutex_lock(&tz->lock);
843 mutex_lock(&cdev->lock);
844 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
845 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
846 list_del(&pos->tz_node);
847 list_del(&pos->cdev_node);
848 mutex_unlock(&cdev->lock);
849 mutex_unlock(&tz->lock);
853 mutex_unlock(&cdev->lock);
854 mutex_unlock(&tz->lock);
859 device_remove_file(&tz->device, &pos->attr);
860 sysfs_remove_link(&tz->device.kobj, pos->name);
861 release_idr(&tz->idr, &tz->lock, pos->id);
865 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
867 static void thermal_release(struct device *dev)
869 struct thermal_zone_device *tz;
870 struct thermal_cooling_device *cdev;
872 if (!strncmp(dev_name(dev), "thermal_zone",
873 sizeof("thermal_zone") - 1)) {
874 tz = to_thermal_zone(dev);
877 cdev = to_cooling_device(dev);
882 static struct class thermal_class = {
884 .dev_release = thermal_release,
888 * thermal_cooling_device_register - register a new thermal cooling device
889 * @type: the thermal cooling device type.
890 * @devdata: device private data.
891 * @ops: standard thermal cooling devices callbacks.
893 struct thermal_cooling_device *
894 thermal_cooling_device_register(char *type, void *devdata,
895 const struct thermal_cooling_device_ops *ops)
897 struct thermal_cooling_device *cdev;
898 struct thermal_zone_device *pos;
901 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
902 return ERR_PTR(-EINVAL);
904 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
906 return ERR_PTR(-EINVAL);
908 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
910 return ERR_PTR(-ENOMEM);
912 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
915 return ERR_PTR(result);
918 strcpy(cdev->type, type ? : "");
919 mutex_init(&cdev->lock);
920 INIT_LIST_HEAD(&cdev->thermal_instances);
922 cdev->updated = true;
923 cdev->device.class = &thermal_class;
924 cdev->devdata = devdata;
925 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
926 result = device_register(&cdev->device);
928 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
930 return ERR_PTR(result);
935 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
940 result = device_create_file(&cdev->device, &dev_attr_max_state);
944 result = device_create_file(&cdev->device, &dev_attr_cur_state);
948 mutex_lock(&thermal_list_lock);
949 list_add(&cdev->node, &thermal_cdev_list);
950 list_for_each_entry(pos, &thermal_tz_list, node) {
953 result = pos->ops->bind(pos, cdev);
958 mutex_unlock(&thermal_list_lock);
964 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
965 device_unregister(&cdev->device);
966 return ERR_PTR(result);
968 EXPORT_SYMBOL(thermal_cooling_device_register);
971 * thermal_cooling_device_unregister - removes the registered thermal cooling device
972 * @cdev: the thermal cooling device to remove.
974 * thermal_cooling_device_unregister() must be called when the device is no
977 void thermal_cooling_device_unregister(struct
978 thermal_cooling_device
981 struct thermal_zone_device *tz;
982 struct thermal_cooling_device *pos = NULL;
987 mutex_lock(&thermal_list_lock);
988 list_for_each_entry(pos, &thermal_cdev_list, node)
992 /* thermal cooling device not found */
993 mutex_unlock(&thermal_list_lock);
996 list_del(&cdev->node);
997 list_for_each_entry(tz, &thermal_tz_list, node) {
998 if (!tz->ops->unbind)
1000 tz->ops->unbind(tz, cdev);
1002 mutex_unlock(&thermal_list_lock);
1004 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1005 device_remove_file(&cdev->device, &dev_attr_max_state);
1006 device_remove_file(&cdev->device, &dev_attr_cur_state);
1008 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1009 device_unregister(&cdev->device);
1012 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1014 static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
1016 struct thermal_instance *instance;
1017 unsigned long target = 0;
1019 /* cooling device is updated*/
1023 mutex_lock(&cdev->lock);
1024 /* Make sure cdev enters the deepest cooling state */
1025 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1026 if (instance->target == THERMAL_NO_TARGET)
1028 if (instance->target > target)
1029 target = instance->target;
1031 mutex_unlock(&cdev->lock);
1032 cdev->ops->set_cur_state(cdev, target);
1033 cdev->updated = true;
1036 static void thermal_zone_do_update(struct thermal_zone_device *tz)
1038 struct thermal_instance *instance;
1040 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
1041 thermal_cdev_do_update(instance->cdev);
1045 * Cooling algorithm for both active and passive cooling
1047 * 1. if the temperature is higher than a trip point,
1048 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1049 * state for this trip point
1050 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1051 * state for this trip point
1053 * 2. if the temperature is lower than a trip point, use lower
1054 * cooling state for this trip point
1056 * Note that this behaves the same as the previous passive cooling
1060 static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1061 int trip, long temp)
1063 struct thermal_instance *instance;
1064 struct thermal_cooling_device *cdev = NULL;
1065 unsigned long cur_state, max_state;
1067 enum thermal_trip_type trip_type;
1068 enum thermal_trend trend;
1070 if (trip == THERMAL_TRIPS_NONE) {
1071 trip_temp = tz->forced_passive;
1072 trip_type = THERMAL_TRIPS_NONE;
1074 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1075 tz->ops->get_trip_type(tz, trip, &trip_type);
1078 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1080 * compare the current temperature and previous temperature
1081 * to get the thermal trend, if no special requirement
1083 if (tz->temperature > tz->last_temperature)
1084 trend = THERMAL_TREND_RAISING;
1085 else if (tz->temperature < tz->last_temperature)
1086 trend = THERMAL_TREND_DROPPING;
1088 trend = THERMAL_TREND_STABLE;
1091 if (temp >= trip_temp) {
1092 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1093 if (instance->trip != trip)
1096 cdev = instance->cdev;
1098 cdev->ops->get_cur_state(cdev, &cur_state);
1099 cdev->ops->get_max_state(cdev, &max_state);
1101 if (trend == THERMAL_TREND_RAISING) {
1102 cur_state = cur_state < instance->upper ?
1103 (cur_state + 1) : instance->upper;
1104 } else if (trend == THERMAL_TREND_DROPPING) {
1105 cur_state = cur_state > instance->lower ?
1106 (cur_state - 1) : instance->lower;
1109 /* activate a passive thermal instance */
1110 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1111 trip_type == THERMAL_TRIPS_NONE) &&
1112 instance->target == THERMAL_NO_TARGET)
1115 instance->target = cur_state;
1116 cdev->updated = false; /* cooling device needs update */
1118 } else { /* below trip */
1119 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1120 if (instance->trip != trip)
1123 /* Do not use the inactive thermal instance */
1124 if (instance->target == THERMAL_NO_TARGET)
1126 cdev = instance->cdev;
1127 cdev->ops->get_cur_state(cdev, &cur_state);
1129 cur_state = cur_state > instance->lower ?
1130 (cur_state - 1) : THERMAL_NO_TARGET;
1132 /* deactivate a passive thermal instance */
1133 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1134 trip_type == THERMAL_TRIPS_NONE) &&
1135 cur_state == THERMAL_NO_TARGET)
1137 instance->target = cur_state;
1138 cdev->updated = false; /* cooling device needs update */
1145 * thermal_zone_device_update - force an update of a thermal zone's state
1146 * @ttz: the thermal zone to update
1149 void thermal_zone_device_update(struct thermal_zone_device *tz)
1152 long temp, trip_temp;
1153 enum thermal_trip_type trip_type;
1155 mutex_lock(&tz->lock);
1157 if (tz->ops->get_temp(tz, &temp)) {
1158 /* get_temp failed - retry it later */
1159 pr_warn("failed to read out thermal zone %d\n", tz->id);
1163 tz->last_temperature = tz->temperature;
1164 tz->temperature = temp;
1166 for (count = 0; count < tz->trips; count++) {
1167 tz->ops->get_trip_type(tz, count, &trip_type);
1168 tz->ops->get_trip_temp(tz, count, &trip_temp);
1170 switch (trip_type) {
1171 case THERMAL_TRIP_CRITICAL:
1172 if (temp >= trip_temp) {
1173 if (tz->ops->notify)
1174 ret = tz->ops->notify(tz, count,
1177 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1179 orderly_poweroff(true);
1183 case THERMAL_TRIP_HOT:
1184 if (temp >= trip_temp)
1185 if (tz->ops->notify)
1186 tz->ops->notify(tz, count, trip_type);
1188 case THERMAL_TRIP_ACTIVE:
1189 thermal_zone_trip_update(tz, count, temp);
1191 case THERMAL_TRIP_PASSIVE:
1192 if (temp >= trip_temp || tz->passive)
1193 thermal_zone_trip_update(tz, count, temp);
1198 if (tz->forced_passive)
1199 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
1200 thermal_zone_do_update(tz);
1204 thermal_zone_device_set_polling(tz, tz->passive_delay);
1205 else if (tz->polling_delay)
1206 thermal_zone_device_set_polling(tz, tz->polling_delay);
1208 thermal_zone_device_set_polling(tz, 0);
1209 mutex_unlock(&tz->lock);
1211 EXPORT_SYMBOL(thermal_zone_device_update);
1214 * create_trip_attrs - create attributes for trip points
1215 * @tz: the thermal zone device
1216 * @mask: Writeable trip point bitmap.
1218 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1221 int size = sizeof(struct thermal_attr) * tz->trips;
1223 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1224 if (!tz->trip_type_attrs)
1227 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1228 if (!tz->trip_temp_attrs) {
1229 kfree(tz->trip_type_attrs);
1233 if (tz->ops->get_trip_hyst) {
1234 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1235 if (!tz->trip_hyst_attrs) {
1236 kfree(tz->trip_type_attrs);
1237 kfree(tz->trip_temp_attrs);
1243 for (indx = 0; indx < tz->trips; indx++) {
1244 /* create trip type attribute */
1245 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1246 "trip_point_%d_type", indx);
1248 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1249 tz->trip_type_attrs[indx].attr.attr.name =
1250 tz->trip_type_attrs[indx].name;
1251 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1252 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1254 device_create_file(&tz->device,
1255 &tz->trip_type_attrs[indx].attr);
1257 /* create trip temp attribute */
1258 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1259 "trip_point_%d_temp", indx);
1261 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1262 tz->trip_temp_attrs[indx].attr.attr.name =
1263 tz->trip_temp_attrs[indx].name;
1264 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1265 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1266 if (mask & (1 << indx)) {
1267 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1268 tz->trip_temp_attrs[indx].attr.store =
1269 trip_point_temp_store;
1272 device_create_file(&tz->device,
1273 &tz->trip_temp_attrs[indx].attr);
1275 /* create Optional trip hyst attribute */
1276 if (!tz->ops->get_trip_hyst)
1278 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1279 "trip_point_%d_hyst", indx);
1281 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1282 tz->trip_hyst_attrs[indx].attr.attr.name =
1283 tz->trip_hyst_attrs[indx].name;
1284 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1285 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1286 if (tz->ops->set_trip_hyst) {
1287 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1288 tz->trip_hyst_attrs[indx].attr.store =
1289 trip_point_hyst_store;
1292 device_create_file(&tz->device,
1293 &tz->trip_hyst_attrs[indx].attr);
1298 static void remove_trip_attrs(struct thermal_zone_device *tz)
1302 for (indx = 0; indx < tz->trips; indx++) {
1303 device_remove_file(&tz->device,
1304 &tz->trip_type_attrs[indx].attr);
1305 device_remove_file(&tz->device,
1306 &tz->trip_temp_attrs[indx].attr);
1307 if (tz->ops->get_trip_hyst)
1308 device_remove_file(&tz->device,
1309 &tz->trip_hyst_attrs[indx].attr);
1311 kfree(tz->trip_type_attrs);
1312 kfree(tz->trip_temp_attrs);
1313 kfree(tz->trip_hyst_attrs);
1317 * thermal_zone_device_register - register a new thermal zone device
1318 * @type: the thermal zone device type
1319 * @trips: the number of trip points the thermal zone support
1320 * @mask: a bit string indicating the writeablility of trip points
1321 * @devdata: private device data
1322 * @ops: standard thermal zone device callbacks
1323 * @passive_delay: number of milliseconds to wait between polls when
1324 * performing passive cooling
1325 * @polling_delay: number of milliseconds to wait between polls when checking
1326 * whether trip points have been crossed (0 for interrupt
1329 * thermal_zone_device_unregister() must be called when the device is no
1330 * longer needed. The passive cooling depends on the .get_trend() return value.
1332 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1333 int trips, int mask, void *devdata,
1334 const struct thermal_zone_device_ops *ops,
1335 int passive_delay, int polling_delay)
1337 struct thermal_zone_device *tz;
1338 struct thermal_cooling_device *pos;
1339 enum thermal_trip_type trip_type;
1344 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1345 return ERR_PTR(-EINVAL);
1347 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1348 return ERR_PTR(-EINVAL);
1350 if (!ops || !ops->get_temp)
1351 return ERR_PTR(-EINVAL);
1353 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1355 return ERR_PTR(-ENOMEM);
1357 INIT_LIST_HEAD(&tz->thermal_instances);
1359 mutex_init(&tz->lock);
1360 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1363 return ERR_PTR(result);
1366 strcpy(tz->type, type ? : "");
1368 tz->device.class = &thermal_class;
1369 tz->devdata = devdata;
1371 tz->passive_delay = passive_delay;
1372 tz->polling_delay = polling_delay;
1374 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1375 result = device_register(&tz->device);
1377 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1379 return ERR_PTR(result);
1384 result = device_create_file(&tz->device, &dev_attr_type);
1389 result = device_create_file(&tz->device, &dev_attr_temp);
1393 if (ops->get_mode) {
1394 result = device_create_file(&tz->device, &dev_attr_mode);
1399 result = create_trip_attrs(tz, mask);
1403 for (count = 0; count < trips; count++) {
1404 tz->ops->get_trip_type(tz, count, &trip_type);
1405 if (trip_type == THERMAL_TRIP_PASSIVE)
1410 result = device_create_file(&tz->device,
1416 result = thermal_add_hwmon_sysfs(tz);
1420 mutex_lock(&thermal_list_lock);
1421 list_add_tail(&tz->node, &thermal_tz_list);
1423 list_for_each_entry(pos, &thermal_cdev_list, node) {
1424 result = ops->bind(tz, pos);
1428 mutex_unlock(&thermal_list_lock);
1430 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1432 thermal_zone_device_update(tz);
1438 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1439 device_unregister(&tz->device);
1440 return ERR_PTR(result);
1442 EXPORT_SYMBOL(thermal_zone_device_register);
1445 * thermal_device_unregister - removes the registered thermal zone device
1446 * @tz: the thermal zone device to remove
1448 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1450 struct thermal_cooling_device *cdev;
1451 struct thermal_zone_device *pos = NULL;
1456 mutex_lock(&thermal_list_lock);
1457 list_for_each_entry(pos, &thermal_tz_list, node)
1461 /* thermal zone device not found */
1462 mutex_unlock(&thermal_list_lock);
1465 list_del(&tz->node);
1466 if (tz->ops->unbind)
1467 list_for_each_entry(cdev, &thermal_cdev_list, node)
1468 tz->ops->unbind(tz, cdev);
1469 mutex_unlock(&thermal_list_lock);
1471 thermal_zone_device_set_polling(tz, 0);
1474 device_remove_file(&tz->device, &dev_attr_type);
1475 device_remove_file(&tz->device, &dev_attr_temp);
1476 if (tz->ops->get_mode)
1477 device_remove_file(&tz->device, &dev_attr_mode);
1478 remove_trip_attrs(tz);
1480 thermal_remove_hwmon_sysfs(tz);
1481 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1482 idr_destroy(&tz->idr);
1483 mutex_destroy(&tz->lock);
1484 device_unregister(&tz->device);
1487 EXPORT_SYMBOL(thermal_zone_device_unregister);
1490 static struct genl_family thermal_event_genl_family = {
1491 .id = GENL_ID_GENERATE,
1492 .name = THERMAL_GENL_FAMILY_NAME,
1493 .version = THERMAL_GENL_VERSION,
1494 .maxattr = THERMAL_GENL_ATTR_MAX,
1497 static struct genl_multicast_group thermal_event_mcgrp = {
1498 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1501 int thermal_generate_netlink_event(u32 orig, enum events event)
1503 struct sk_buff *skb;
1504 struct nlattr *attr;
1505 struct thermal_genl_event *thermal_event;
1509 static unsigned int thermal_event_seqnum;
1511 /* allocate memory */
1512 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1515 skb = genlmsg_new(size, GFP_ATOMIC);
1519 /* add the genetlink message header */
1520 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1521 &thermal_event_genl_family, 0,
1522 THERMAL_GENL_CMD_EVENT);
1529 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1530 sizeof(struct thermal_genl_event));
1537 thermal_event = nla_data(attr);
1538 if (!thermal_event) {
1543 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1545 thermal_event->orig = orig;
1546 thermal_event->event = event;
1548 /* send multicast genetlink message */
1549 result = genlmsg_end(skb, msg_header);
1555 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1557 pr_info("failed to send netlink event:%d\n", result);
1561 EXPORT_SYMBOL(thermal_generate_netlink_event);
1563 static int genetlink_init(void)
1567 result = genl_register_family(&thermal_event_genl_family);
1571 result = genl_register_mc_group(&thermal_event_genl_family,
1572 &thermal_event_mcgrp);
1574 genl_unregister_family(&thermal_event_genl_family);
1578 static void genetlink_exit(void)
1580 genl_unregister_family(&thermal_event_genl_family);
1582 #else /* !CONFIG_NET */
1583 static inline int genetlink_init(void) { return 0; }
1584 static inline void genetlink_exit(void) {}
1585 #endif /* !CONFIG_NET */
1587 static int __init thermal_init(void)
1591 result = class_register(&thermal_class);
1593 idr_destroy(&thermal_tz_idr);
1594 idr_destroy(&thermal_cdev_idr);
1595 mutex_destroy(&thermal_idr_lock);
1596 mutex_destroy(&thermal_list_lock);
1598 result = genetlink_init();
1602 static void __exit thermal_exit(void)
1604 class_unregister(&thermal_class);
1605 idr_destroy(&thermal_tz_idr);
1606 idr_destroy(&thermal_cdev_idr);
1607 mutex_destroy(&thermal_idr_lock);
1608 mutex_destroy(&thermal_list_lock);
1612 fs_initcall(thermal_init);
1613 module_exit(thermal_exit);