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 struct thermal_cooling_device_instance {
46 char name[THERMAL_NAME_LENGTH];
47 struct thermal_zone_device *tz;
48 struct thermal_cooling_device *cdev;
50 char attr_name[THERMAL_NAME_LENGTH];
51 struct device_attribute attr;
52 struct list_head node;
55 static DEFINE_IDR(thermal_tz_idr);
56 static DEFINE_IDR(thermal_cdev_idr);
57 static DEFINE_MUTEX(thermal_idr_lock);
59 static LIST_HEAD(thermal_tz_list);
60 static LIST_HEAD(thermal_cdev_list);
61 static DEFINE_MUTEX(thermal_list_lock);
63 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
68 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
73 err = idr_get_new(idr, NULL, id);
76 if (unlikely(err == -EAGAIN))
78 else if (unlikely(err))
81 *id = *id & MAX_ID_MASK;
85 static void release_idr(struct idr *idr, struct mutex *lock, int id)
94 /* sys I/F for thermal zone */
96 #define to_thermal_zone(_dev) \
97 container_of(_dev, struct thermal_zone_device, device)
100 type_show(struct device *dev, struct device_attribute *attr, char *buf)
102 struct thermal_zone_device *tz = to_thermal_zone(dev);
104 return sprintf(buf, "%s\n", tz->type);
108 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
110 struct thermal_zone_device *tz = to_thermal_zone(dev);
114 if (!tz->ops->get_temp)
117 ret = tz->ops->get_temp(tz, &temperature);
122 return sprintf(buf, "%ld\n", temperature);
126 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
128 struct thermal_zone_device *tz = to_thermal_zone(dev);
129 enum thermal_device_mode mode;
132 if (!tz->ops->get_mode)
135 result = tz->ops->get_mode(tz, &mode);
139 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
144 mode_store(struct device *dev, struct device_attribute *attr,
145 const char *buf, size_t count)
147 struct thermal_zone_device *tz = to_thermal_zone(dev);
150 if (!tz->ops->set_mode)
153 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
154 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
155 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
156 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
167 trip_point_type_show(struct device *dev, struct device_attribute *attr,
170 struct thermal_zone_device *tz = to_thermal_zone(dev);
171 enum thermal_trip_type type;
174 if (!tz->ops->get_trip_type)
177 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
180 result = tz->ops->get_trip_type(tz, trip, &type);
185 case THERMAL_TRIP_CRITICAL:
186 return sprintf(buf, "critical\n");
187 case THERMAL_TRIP_HOT:
188 return sprintf(buf, "hot\n");
189 case THERMAL_TRIP_PASSIVE:
190 return sprintf(buf, "passive\n");
191 case THERMAL_TRIP_ACTIVE:
192 return sprintf(buf, "active\n");
194 return sprintf(buf, "unknown\n");
199 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
200 const char *buf, size_t count)
202 struct thermal_zone_device *tz = to_thermal_zone(dev);
204 unsigned long temperature;
206 if (!tz->ops->set_trip_temp)
209 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
212 if (kstrtoul(buf, 10, &temperature))
215 ret = tz->ops->set_trip_temp(tz, trip, temperature);
217 return ret ? ret : count;
221 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
224 struct thermal_zone_device *tz = to_thermal_zone(dev);
228 if (!tz->ops->get_trip_temp)
231 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
234 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
239 return sprintf(buf, "%ld\n", temperature);
243 passive_store(struct device *dev, struct device_attribute *attr,
244 const char *buf, size_t count)
246 struct thermal_zone_device *tz = to_thermal_zone(dev);
247 struct thermal_cooling_device *cdev = NULL;
250 if (!sscanf(buf, "%d\n", &state))
253 /* sanity check: values below 1000 millicelcius don't make sense
254 * and can cause the system to go into a thermal heart attack
256 if (state && state < 1000)
259 if (state && !tz->forced_passive) {
260 mutex_lock(&thermal_list_lock);
261 list_for_each_entry(cdev, &thermal_cdev_list, node) {
262 if (!strncmp("Processor", cdev->type,
263 sizeof("Processor")))
264 thermal_zone_bind_cooling_device(tz,
268 mutex_unlock(&thermal_list_lock);
269 if (!tz->passive_delay)
270 tz->passive_delay = 1000;
271 } else if (!state && tz->forced_passive) {
272 mutex_lock(&thermal_list_lock);
273 list_for_each_entry(cdev, &thermal_cdev_list, node) {
274 if (!strncmp("Processor", cdev->type,
275 sizeof("Processor")))
276 thermal_zone_unbind_cooling_device(tz,
280 mutex_unlock(&thermal_list_lock);
281 tz->passive_delay = 0;
287 tz->forced_passive = state;
289 thermal_zone_device_update(tz);
295 passive_show(struct device *dev, struct device_attribute *attr,
298 struct thermal_zone_device *tz = to_thermal_zone(dev);
300 return sprintf(buf, "%d\n", tz->forced_passive);
303 static DEVICE_ATTR(type, 0444, type_show, NULL);
304 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
305 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
306 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
308 /* sys I/F for cooling device */
309 #define to_cooling_device(_dev) \
310 container_of(_dev, struct thermal_cooling_device, device)
313 thermal_cooling_device_type_show(struct device *dev,
314 struct device_attribute *attr, char *buf)
316 struct thermal_cooling_device *cdev = to_cooling_device(dev);
318 return sprintf(buf, "%s\n", cdev->type);
322 thermal_cooling_device_max_state_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
325 struct thermal_cooling_device *cdev = to_cooling_device(dev);
329 ret = cdev->ops->get_max_state(cdev, &state);
332 return sprintf(buf, "%ld\n", state);
336 thermal_cooling_device_cur_state_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
339 struct thermal_cooling_device *cdev = to_cooling_device(dev);
343 ret = cdev->ops->get_cur_state(cdev, &state);
346 return sprintf(buf, "%ld\n", state);
350 thermal_cooling_device_cur_state_store(struct device *dev,
351 struct device_attribute *attr,
352 const char *buf, size_t count)
354 struct thermal_cooling_device *cdev = to_cooling_device(dev);
358 if (!sscanf(buf, "%ld\n", &state))
364 result = cdev->ops->set_cur_state(cdev, state);
370 static struct device_attribute dev_attr_cdev_type =
371 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
372 static DEVICE_ATTR(max_state, 0444,
373 thermal_cooling_device_max_state_show, NULL);
374 static DEVICE_ATTR(cur_state, 0644,
375 thermal_cooling_device_cur_state_show,
376 thermal_cooling_device_cur_state_store);
379 thermal_cooling_device_trip_point_show(struct device *dev,
380 struct device_attribute *attr, char *buf)
382 struct thermal_cooling_device_instance *instance;
385 container_of(attr, struct thermal_cooling_device_instance, attr);
387 if (instance->trip == THERMAL_TRIPS_NONE)
388 return sprintf(buf, "-1\n");
390 return sprintf(buf, "%d\n", instance->trip);
393 /* Device management */
395 #if defined(CONFIG_THERMAL_HWMON)
398 #include <linux/hwmon.h>
400 /* thermal zone devices with the same type share one hwmon device */
401 struct thermal_hwmon_device {
402 char type[THERMAL_NAME_LENGTH];
403 struct device *device;
405 struct list_head tz_list;
406 struct list_head node;
409 struct thermal_hwmon_attr {
410 struct device_attribute attr;
414 /* one temperature input for each thermal zone */
415 struct thermal_hwmon_temp {
416 struct list_head hwmon_node;
417 struct thermal_zone_device *tz;
418 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
419 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
422 static LIST_HEAD(thermal_hwmon_list);
425 name_show(struct device *dev, struct device_attribute *attr, char *buf)
427 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
428 return sprintf(buf, "%s\n", hwmon->type);
430 static DEVICE_ATTR(name, 0444, name_show, NULL);
433 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
437 struct thermal_hwmon_attr *hwmon_attr
438 = container_of(attr, struct thermal_hwmon_attr, attr);
439 struct thermal_hwmon_temp *temp
440 = container_of(hwmon_attr, struct thermal_hwmon_temp,
442 struct thermal_zone_device *tz = temp->tz;
444 ret = tz->ops->get_temp(tz, &temperature);
449 return sprintf(buf, "%ld\n", temperature);
453 temp_crit_show(struct device *dev, struct device_attribute *attr,
456 struct thermal_hwmon_attr *hwmon_attr
457 = container_of(attr, struct thermal_hwmon_attr, attr);
458 struct thermal_hwmon_temp *temp
459 = container_of(hwmon_attr, struct thermal_hwmon_temp,
461 struct thermal_zone_device *tz = temp->tz;
465 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
469 return sprintf(buf, "%ld\n", temperature);
473 static struct thermal_hwmon_device *
474 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
476 struct thermal_hwmon_device *hwmon;
478 mutex_lock(&thermal_list_lock);
479 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
480 if (!strcmp(hwmon->type, tz->type)) {
481 mutex_unlock(&thermal_list_lock);
484 mutex_unlock(&thermal_list_lock);
489 /* Find the temperature input matching a given thermal zone */
490 static struct thermal_hwmon_temp *
491 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
492 const struct thermal_zone_device *tz)
494 struct thermal_hwmon_temp *temp;
496 mutex_lock(&thermal_list_lock);
497 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
498 if (temp->tz == tz) {
499 mutex_unlock(&thermal_list_lock);
502 mutex_unlock(&thermal_list_lock);
508 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
510 struct thermal_hwmon_device *hwmon;
511 struct thermal_hwmon_temp *temp;
512 int new_hwmon_device = 1;
515 hwmon = thermal_hwmon_lookup_by_type(tz);
517 new_hwmon_device = 0;
518 goto register_sys_interface;
521 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
525 INIT_LIST_HEAD(&hwmon->tz_list);
526 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
527 hwmon->device = hwmon_device_register(NULL);
528 if (IS_ERR(hwmon->device)) {
529 result = PTR_ERR(hwmon->device);
532 dev_set_drvdata(hwmon->device, hwmon);
533 result = device_create_file(hwmon->device, &dev_attr_name);
537 register_sys_interface:
538 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
541 goto unregister_name;
547 snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH,
548 "temp%d_input", hwmon->count);
549 temp->temp_input.attr.attr.name = temp->temp_input.name;
550 temp->temp_input.attr.attr.mode = 0444;
551 temp->temp_input.attr.show = temp_input_show;
552 sysfs_attr_init(&temp->temp_input.attr.attr);
553 result = device_create_file(hwmon->device, &temp->temp_input.attr);
557 if (tz->ops->get_crit_temp) {
558 unsigned long temperature;
559 if (!tz->ops->get_crit_temp(tz, &temperature)) {
560 snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH,
561 "temp%d_crit", hwmon->count);
562 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
563 temp->temp_crit.attr.attr.mode = 0444;
564 temp->temp_crit.attr.show = temp_crit_show;
565 sysfs_attr_init(&temp->temp_crit.attr.attr);
566 result = device_create_file(hwmon->device,
567 &temp->temp_crit.attr);
569 goto unregister_input;
573 mutex_lock(&thermal_list_lock);
574 if (new_hwmon_device)
575 list_add_tail(&hwmon->node, &thermal_hwmon_list);
576 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
577 mutex_unlock(&thermal_list_lock);
582 device_remove_file(hwmon->device, &temp->temp_input.attr);
586 if (new_hwmon_device) {
587 device_remove_file(hwmon->device, &dev_attr_name);
588 hwmon_device_unregister(hwmon->device);
591 if (new_hwmon_device)
598 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
600 struct thermal_hwmon_device *hwmon;
601 struct thermal_hwmon_temp *temp;
603 hwmon = thermal_hwmon_lookup_by_type(tz);
604 if (unlikely(!hwmon)) {
605 /* Should never happen... */
606 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
610 temp = thermal_hwmon_lookup_temp(hwmon, tz);
611 if (unlikely(!temp)) {
612 /* Should never happen... */
613 dev_dbg(&tz->device, "temperature input lookup failed!\n");
617 device_remove_file(hwmon->device, &temp->temp_input.attr);
618 if (tz->ops->get_crit_temp)
619 device_remove_file(hwmon->device, &temp->temp_crit.attr);
621 mutex_lock(&thermal_list_lock);
622 list_del(&temp->hwmon_node);
624 if (!list_empty(&hwmon->tz_list)) {
625 mutex_unlock(&thermal_list_lock);
628 list_del(&hwmon->node);
629 mutex_unlock(&thermal_list_lock);
631 device_remove_file(hwmon->device, &dev_attr_name);
632 hwmon_device_unregister(hwmon->device);
637 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
643 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
648 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
651 cancel_delayed_work(&(tz->poll_queue));
657 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
658 round_jiffies(msecs_to_jiffies(delay)));
660 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
661 msecs_to_jiffies(delay));
664 static void thermal_zone_device_passive(struct thermal_zone_device *tz,
665 int temp, int trip_temp, int trip)
668 struct thermal_cooling_device_instance *instance;
669 struct thermal_cooling_device *cdev;
670 long state, max_state;
675 * Calculate the thermal trend (using the passive cooling equation)
676 * and modify the performance limit for all passive cooling devices
677 * accordingly. Note that we assume symmetry.
679 if (temp >= trip_temp) {
682 trend = (tz->tc1 * (temp - tz->last_temperature)) +
683 (tz->tc2 * (temp - trip_temp));
687 list_for_each_entry(instance, &tz->cooling_devices,
689 if (instance->trip != trip)
691 cdev = instance->cdev;
692 cdev->ops->get_cur_state(cdev, &state);
693 cdev->ops->get_max_state(cdev, &max_state);
694 if (state++ < max_state)
695 cdev->ops->set_cur_state(cdev, state);
697 } else if (trend < 0) { /* Cooling off? */
698 list_for_each_entry(instance, &tz->cooling_devices,
700 if (instance->trip != trip)
702 cdev = instance->cdev;
703 cdev->ops->get_cur_state(cdev, &state);
704 cdev->ops->get_max_state(cdev, &max_state);
706 cdev->ops->set_cur_state(cdev, --state);
715 * Implement passive cooling hysteresis to slowly increase performance
716 * and avoid thrashing around the passive trip point. Note that we
719 list_for_each_entry(instance, &tz->cooling_devices, node) {
720 if (instance->trip != trip)
722 cdev = instance->cdev;
723 cdev->ops->get_cur_state(cdev, &state);
724 cdev->ops->get_max_state(cdev, &max_state);
726 cdev->ops->set_cur_state(cdev, --state);
732 static void thermal_zone_device_check(struct work_struct *work)
734 struct thermal_zone_device *tz = container_of(work, struct
737 thermal_zone_device_update(tz);
741 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
742 * @tz: thermal zone device
743 * @trip: indicates which trip point the cooling devices is
744 * associated with in this thermal zone.
745 * @cdev: thermal cooling device
747 * This function is usually called in the thermal zone device .bind callback.
749 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
751 struct thermal_cooling_device *cdev)
753 struct thermal_cooling_device_instance *dev;
754 struct thermal_cooling_device_instance *pos;
755 struct thermal_zone_device *pos1;
756 struct thermal_cooling_device *pos2;
759 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
762 list_for_each_entry(pos1, &thermal_tz_list, node) {
766 list_for_each_entry(pos2, &thermal_cdev_list, node) {
771 if (tz != pos1 || cdev != pos2)
775 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
781 result = get_idr(&tz->idr, &tz->lock, &dev->id);
785 sprintf(dev->name, "cdev%d", dev->id);
787 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
791 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
792 sysfs_attr_init(&dev->attr.attr);
793 dev->attr.attr.name = dev->attr_name;
794 dev->attr.attr.mode = 0444;
795 dev->attr.show = thermal_cooling_device_trip_point_show;
796 result = device_create_file(&tz->device, &dev->attr);
798 goto remove_symbol_link;
800 mutex_lock(&tz->lock);
801 list_for_each_entry(pos, &tz->cooling_devices, node)
802 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
807 list_add_tail(&dev->node, &tz->cooling_devices);
808 mutex_unlock(&tz->lock);
813 device_remove_file(&tz->device, &dev->attr);
815 sysfs_remove_link(&tz->device.kobj, dev->name);
817 release_idr(&tz->idr, &tz->lock, dev->id);
822 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
825 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
826 * @tz: thermal zone device
827 * @trip: indicates which trip point the cooling devices is
828 * associated with in this thermal zone.
829 * @cdev: thermal cooling device
831 * This function is usually called in the thermal zone device .unbind callback.
833 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
835 struct thermal_cooling_device *cdev)
837 struct thermal_cooling_device_instance *pos, *next;
839 mutex_lock(&tz->lock);
840 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
841 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
842 list_del(&pos->node);
843 mutex_unlock(&tz->lock);
847 mutex_unlock(&tz->lock);
852 device_remove_file(&tz->device, &pos->attr);
853 sysfs_remove_link(&tz->device.kobj, pos->name);
854 release_idr(&tz->idr, &tz->lock, pos->id);
858 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
860 static void thermal_release(struct device *dev)
862 struct thermal_zone_device *tz;
863 struct thermal_cooling_device *cdev;
865 if (!strncmp(dev_name(dev), "thermal_zone",
866 sizeof("thermal_zone") - 1)) {
867 tz = to_thermal_zone(dev);
870 cdev = to_cooling_device(dev);
875 static struct class thermal_class = {
877 .dev_release = thermal_release,
881 * thermal_cooling_device_register - register a new thermal cooling device
882 * @type: the thermal cooling device type.
883 * @devdata: device private data.
884 * @ops: standard thermal cooling devices callbacks.
886 struct thermal_cooling_device *
887 thermal_cooling_device_register(char *type, void *devdata,
888 const struct thermal_cooling_device_ops *ops)
890 struct thermal_cooling_device *cdev;
891 struct thermal_zone_device *pos;
894 if (strlen(type) >= THERMAL_NAME_LENGTH)
895 return ERR_PTR(-EINVAL);
897 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
899 return ERR_PTR(-EINVAL);
901 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
903 return ERR_PTR(-ENOMEM);
905 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
908 return ERR_PTR(result);
911 strcpy(cdev->type, type);
913 cdev->device.class = &thermal_class;
914 cdev->devdata = devdata;
915 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
916 result = device_register(&cdev->device);
918 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
920 return ERR_PTR(result);
925 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
930 result = device_create_file(&cdev->device, &dev_attr_max_state);
934 result = device_create_file(&cdev->device, &dev_attr_cur_state);
938 mutex_lock(&thermal_list_lock);
939 list_add(&cdev->node, &thermal_cdev_list);
940 list_for_each_entry(pos, &thermal_tz_list, node) {
943 result = pos->ops->bind(pos, cdev);
948 mutex_unlock(&thermal_list_lock);
954 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
955 device_unregister(&cdev->device);
956 return ERR_PTR(result);
958 EXPORT_SYMBOL(thermal_cooling_device_register);
961 * thermal_cooling_device_unregister - removes the registered thermal cooling device
962 * @cdev: the thermal cooling device to remove.
964 * thermal_cooling_device_unregister() must be called when the device is no
967 void thermal_cooling_device_unregister(struct
968 thermal_cooling_device
971 struct thermal_zone_device *tz;
972 struct thermal_cooling_device *pos = NULL;
977 mutex_lock(&thermal_list_lock);
978 list_for_each_entry(pos, &thermal_cdev_list, node)
982 /* thermal cooling device not found */
983 mutex_unlock(&thermal_list_lock);
986 list_del(&cdev->node);
987 list_for_each_entry(tz, &thermal_tz_list, node) {
988 if (!tz->ops->unbind)
990 tz->ops->unbind(tz, cdev);
992 mutex_unlock(&thermal_list_lock);
994 device_remove_file(&cdev->device, &dev_attr_cdev_type);
995 device_remove_file(&cdev->device, &dev_attr_max_state);
996 device_remove_file(&cdev->device, &dev_attr_cur_state);
998 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
999 device_unregister(&cdev->device);
1002 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1005 * thermal_zone_device_update - force an update of a thermal zone's state
1006 * @ttz: the thermal zone to update
1009 void thermal_zone_device_update(struct thermal_zone_device *tz)
1012 long temp, trip_temp;
1013 enum thermal_trip_type trip_type;
1014 struct thermal_cooling_device_instance *instance;
1015 struct thermal_cooling_device *cdev;
1017 mutex_lock(&tz->lock);
1019 if (tz->ops->get_temp(tz, &temp)) {
1020 /* get_temp failed - retry it later */
1021 pr_warn("failed to read out thermal zone %d\n", tz->id);
1025 for (count = 0; count < tz->trips; count++) {
1026 tz->ops->get_trip_type(tz, count, &trip_type);
1027 tz->ops->get_trip_temp(tz, count, &trip_temp);
1029 switch (trip_type) {
1030 case THERMAL_TRIP_CRITICAL:
1031 if (temp >= trip_temp) {
1032 if (tz->ops->notify)
1033 ret = tz->ops->notify(tz, count,
1036 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1038 orderly_poweroff(true);
1042 case THERMAL_TRIP_HOT:
1043 if (temp >= trip_temp)
1044 if (tz->ops->notify)
1045 tz->ops->notify(tz, count, trip_type);
1047 case THERMAL_TRIP_ACTIVE:
1048 list_for_each_entry(instance, &tz->cooling_devices,
1050 if (instance->trip != count)
1053 cdev = instance->cdev;
1055 if (temp >= trip_temp)
1056 cdev->ops->set_cur_state(cdev, 1);
1058 cdev->ops->set_cur_state(cdev, 0);
1061 case THERMAL_TRIP_PASSIVE:
1062 if (temp >= trip_temp || tz->passive)
1063 thermal_zone_device_passive(tz, temp,
1069 if (tz->forced_passive)
1070 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1071 THERMAL_TRIPS_NONE);
1073 tz->last_temperature = temp;
1077 thermal_zone_device_set_polling(tz, tz->passive_delay);
1078 else if (tz->polling_delay)
1079 thermal_zone_device_set_polling(tz, tz->polling_delay);
1081 thermal_zone_device_set_polling(tz, 0);
1082 mutex_unlock(&tz->lock);
1084 EXPORT_SYMBOL(thermal_zone_device_update);
1087 * create_trip_attrs - create attributes for trip points
1088 * @tz: the thermal zone device
1089 * @mask: Writeable trip point bitmap.
1091 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1095 tz->trip_type_attrs =
1096 kzalloc(sizeof(struct thermal_attr) * tz->trips, GFP_KERNEL);
1097 if (!tz->trip_type_attrs)
1100 tz->trip_temp_attrs =
1101 kzalloc(sizeof(struct thermal_attr) * tz->trips, GFP_KERNEL);
1102 if (!tz->trip_temp_attrs) {
1103 kfree(tz->trip_type_attrs);
1107 for (indx = 0; indx < tz->trips; indx++) {
1109 /* create trip type attribute */
1110 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1111 "trip_point_%d_type", indx);
1113 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1114 tz->trip_type_attrs[indx].attr.attr.name =
1115 tz->trip_type_attrs[indx].name;
1116 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1117 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1119 device_create_file(&tz->device,
1120 &tz->trip_type_attrs[indx].attr);
1122 /* create trip temp attribute */
1123 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1124 "trip_point_%d_temp", indx);
1126 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1127 tz->trip_temp_attrs[indx].attr.attr.name =
1128 tz->trip_temp_attrs[indx].name;
1129 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1130 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1131 if (mask & (1 << indx)) {
1132 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1133 tz->trip_temp_attrs[indx].attr.store =
1134 trip_point_temp_store;
1137 device_create_file(&tz->device,
1138 &tz->trip_temp_attrs[indx].attr);
1143 static void remove_trip_attrs(struct thermal_zone_device *tz)
1147 for (indx = 0; indx < tz->trips; indx++) {
1148 device_remove_file(&tz->device,
1149 &tz->trip_type_attrs[indx].attr);
1150 device_remove_file(&tz->device,
1151 &tz->trip_temp_attrs[indx].attr);
1153 kfree(tz->trip_type_attrs);
1154 kfree(tz->trip_temp_attrs);
1158 * thermal_zone_device_register - register a new thermal zone device
1159 * @type: the thermal zone device type
1160 * @trips: the number of trip points the thermal zone support
1161 * @mask: a bit string indicating the writeablility of trip points
1162 * @devdata: private device data
1163 * @ops: standard thermal zone device callbacks
1164 * @tc1: thermal coefficient 1 for passive calculations
1165 * @tc2: thermal coefficient 2 for passive calculations
1166 * @passive_delay: number of milliseconds to wait between polls when
1167 * performing passive cooling
1168 * @polling_delay: number of milliseconds to wait between polls when checking
1169 * whether trip points have been crossed (0 for interrupt
1172 * thermal_zone_device_unregister() must be called when the device is no
1173 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1174 * section 11.1.5.1 of the ACPI specification 3.0.
1176 struct thermal_zone_device *thermal_zone_device_register(char *type,
1177 int trips, int mask, void *devdata,
1178 const struct thermal_zone_device_ops *ops,
1179 int tc1, int tc2, int passive_delay, int polling_delay)
1181 struct thermal_zone_device *tz;
1182 struct thermal_cooling_device *pos;
1183 enum thermal_trip_type trip_type;
1188 if (strlen(type) >= THERMAL_NAME_LENGTH)
1189 return ERR_PTR(-EINVAL);
1191 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1192 return ERR_PTR(-EINVAL);
1194 if (!ops || !ops->get_temp)
1195 return ERR_PTR(-EINVAL);
1197 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1199 return ERR_PTR(-ENOMEM);
1201 INIT_LIST_HEAD(&tz->cooling_devices);
1203 mutex_init(&tz->lock);
1204 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1207 return ERR_PTR(result);
1210 strcpy(tz->type, type);
1212 tz->device.class = &thermal_class;
1213 tz->devdata = devdata;
1217 tz->passive_delay = passive_delay;
1218 tz->polling_delay = polling_delay;
1220 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1221 result = device_register(&tz->device);
1223 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1225 return ERR_PTR(result);
1230 result = device_create_file(&tz->device, &dev_attr_type);
1235 result = device_create_file(&tz->device, &dev_attr_temp);
1239 if (ops->get_mode) {
1240 result = device_create_file(&tz->device, &dev_attr_mode);
1245 result = create_trip_attrs(tz, mask);
1249 for (count = 0; count < trips; count++) {
1250 tz->ops->get_trip_type(tz, count, &trip_type);
1251 if (trip_type == THERMAL_TRIP_PASSIVE)
1256 result = device_create_file(&tz->device,
1262 result = thermal_add_hwmon_sysfs(tz);
1266 mutex_lock(&thermal_list_lock);
1267 list_add_tail(&tz->node, &thermal_tz_list);
1269 list_for_each_entry(pos, &thermal_cdev_list, node) {
1270 result = ops->bind(tz, pos);
1274 mutex_unlock(&thermal_list_lock);
1276 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1278 thermal_zone_device_update(tz);
1284 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1285 device_unregister(&tz->device);
1286 return ERR_PTR(result);
1288 EXPORT_SYMBOL(thermal_zone_device_register);
1291 * thermal_device_unregister - removes the registered thermal zone device
1292 * @tz: the thermal zone device to remove
1294 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1296 struct thermal_cooling_device *cdev;
1297 struct thermal_zone_device *pos = NULL;
1302 mutex_lock(&thermal_list_lock);
1303 list_for_each_entry(pos, &thermal_tz_list, node)
1307 /* thermal zone device not found */
1308 mutex_unlock(&thermal_list_lock);
1311 list_del(&tz->node);
1312 if (tz->ops->unbind)
1313 list_for_each_entry(cdev, &thermal_cdev_list, node)
1314 tz->ops->unbind(tz, cdev);
1315 mutex_unlock(&thermal_list_lock);
1317 thermal_zone_device_set_polling(tz, 0);
1320 device_remove_file(&tz->device, &dev_attr_type);
1321 device_remove_file(&tz->device, &dev_attr_temp);
1322 if (tz->ops->get_mode)
1323 device_remove_file(&tz->device, &dev_attr_mode);
1324 remove_trip_attrs(tz);
1326 thermal_remove_hwmon_sysfs(tz);
1327 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1328 idr_destroy(&tz->idr);
1329 mutex_destroy(&tz->lock);
1330 device_unregister(&tz->device);
1333 EXPORT_SYMBOL(thermal_zone_device_unregister);
1336 static struct genl_family thermal_event_genl_family = {
1337 .id = GENL_ID_GENERATE,
1338 .name = THERMAL_GENL_FAMILY_NAME,
1339 .version = THERMAL_GENL_VERSION,
1340 .maxattr = THERMAL_GENL_ATTR_MAX,
1343 static struct genl_multicast_group thermal_event_mcgrp = {
1344 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1347 int thermal_generate_netlink_event(u32 orig, enum events event)
1349 struct sk_buff *skb;
1350 struct nlattr *attr;
1351 struct thermal_genl_event *thermal_event;
1355 static unsigned int thermal_event_seqnum;
1357 /* allocate memory */
1358 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1361 skb = genlmsg_new(size, GFP_ATOMIC);
1365 /* add the genetlink message header */
1366 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1367 &thermal_event_genl_family, 0,
1368 THERMAL_GENL_CMD_EVENT);
1375 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1376 sizeof(struct thermal_genl_event));
1383 thermal_event = nla_data(attr);
1384 if (!thermal_event) {
1389 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1391 thermal_event->orig = orig;
1392 thermal_event->event = event;
1394 /* send multicast genetlink message */
1395 result = genlmsg_end(skb, msg_header);
1401 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1403 pr_info("failed to send netlink event:%d\n", result);
1407 EXPORT_SYMBOL(thermal_generate_netlink_event);
1409 static int genetlink_init(void)
1413 result = genl_register_family(&thermal_event_genl_family);
1417 result = genl_register_mc_group(&thermal_event_genl_family,
1418 &thermal_event_mcgrp);
1420 genl_unregister_family(&thermal_event_genl_family);
1424 static void genetlink_exit(void)
1426 genl_unregister_family(&thermal_event_genl_family);
1428 #else /* !CONFIG_NET */
1429 static inline int genetlink_init(void) { return 0; }
1430 static inline void genetlink_exit(void) {}
1431 #endif /* !CONFIG_NET */
1433 static int __init thermal_init(void)
1437 result = class_register(&thermal_class);
1439 idr_destroy(&thermal_tz_idr);
1440 idr_destroy(&thermal_cdev_idr);
1441 mutex_destroy(&thermal_idr_lock);
1442 mutex_destroy(&thermal_list_lock);
1444 result = genetlink_init();
1448 static void __exit thermal_exit(void)
1450 class_unregister(&thermal_class);
1451 idr_destroy(&thermal_tz_idr);
1452 idr_destroy(&thermal_cdev_idr);
1453 mutex_destroy(&thermal_idr_lock);
1454 mutex_destroy(&thermal_list_lock);
1458 fs_initcall(thermal_init);
1459 module_exit(thermal_exit);