2 * thermal.c - Generic Thermal Management Sysfs support.
4 * Copyright (C) 2008 Intel Corp
5 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
6 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/slab.h>
32 #include <linux/kdev_t.h>
33 #include <linux/idr.h>
34 #include <linux/thermal.h>
35 #include <linux/spinlock.h>
36 #include <linux/reboot.h>
37 #include <net/netlink.h>
38 #include <net/genetlink.h>
40 MODULE_AUTHOR("Zhang Rui");
41 MODULE_DESCRIPTION("Generic thermal management sysfs support");
42 MODULE_LICENSE("GPL");
45 * This structure is used to describe the behavior of
46 * a certain cooling device on a certain trip point
47 * in a certain thermal zone
49 struct thermal_cooling_device_instance {
51 char name[THERMAL_NAME_LENGTH];
52 struct thermal_zone_device *tz;
53 struct thermal_cooling_device *cdev;
55 unsigned long upper; /* Highest cooling state for this trip point */
56 unsigned long lower; /* Lowest cooling state for this trip point */
57 char attr_name[THERMAL_NAME_LENGTH];
58 struct device_attribute attr;
59 struct list_head node;
62 static DEFINE_IDR(thermal_tz_idr);
63 static DEFINE_IDR(thermal_cdev_idr);
64 static DEFINE_MUTEX(thermal_idr_lock);
66 static LIST_HEAD(thermal_tz_list);
67 static LIST_HEAD(thermal_cdev_list);
68 static DEFINE_MUTEX(thermal_list_lock);
70 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
75 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
80 err = idr_get_new(idr, NULL, id);
83 if (unlikely(err == -EAGAIN))
85 else if (unlikely(err))
88 *id = *id & MAX_ID_MASK;
92 static void release_idr(struct idr *idr, struct mutex *lock, int id)
101 /* sys I/F for thermal zone */
103 #define to_thermal_zone(_dev) \
104 container_of(_dev, struct thermal_zone_device, device)
107 type_show(struct device *dev, struct device_attribute *attr, char *buf)
109 struct thermal_zone_device *tz = to_thermal_zone(dev);
111 return sprintf(buf, "%s\n", tz->type);
115 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
117 struct thermal_zone_device *tz = to_thermal_zone(dev);
121 if (!tz->ops->get_temp)
124 ret = tz->ops->get_temp(tz, &temperature);
129 return sprintf(buf, "%ld\n", temperature);
133 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
135 struct thermal_zone_device *tz = to_thermal_zone(dev);
136 enum thermal_device_mode mode;
139 if (!tz->ops->get_mode)
142 result = tz->ops->get_mode(tz, &mode);
146 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
151 mode_store(struct device *dev, struct device_attribute *attr,
152 const char *buf, size_t count)
154 struct thermal_zone_device *tz = to_thermal_zone(dev);
157 if (!tz->ops->set_mode)
160 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
161 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
162 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
163 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
174 trip_point_type_show(struct device *dev, struct device_attribute *attr,
177 struct thermal_zone_device *tz = to_thermal_zone(dev);
178 enum thermal_trip_type type;
181 if (!tz->ops->get_trip_type)
184 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
187 result = tz->ops->get_trip_type(tz, trip, &type);
192 case THERMAL_TRIP_CRITICAL:
193 return sprintf(buf, "critical\n");
194 case THERMAL_TRIP_HOT:
195 return sprintf(buf, "hot\n");
196 case THERMAL_TRIP_PASSIVE:
197 return sprintf(buf, "passive\n");
198 case THERMAL_TRIP_ACTIVE:
199 return sprintf(buf, "active\n");
201 return sprintf(buf, "unknown\n");
206 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
207 const char *buf, size_t count)
209 struct thermal_zone_device *tz = to_thermal_zone(dev);
211 unsigned long temperature;
213 if (!tz->ops->set_trip_temp)
216 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
219 if (kstrtoul(buf, 10, &temperature))
222 ret = tz->ops->set_trip_temp(tz, trip, temperature);
224 return ret ? ret : count;
228 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
231 struct thermal_zone_device *tz = to_thermal_zone(dev);
235 if (!tz->ops->get_trip_temp)
238 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
241 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
246 return sprintf(buf, "%ld\n", temperature);
250 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
251 const char *buf, size_t count)
253 struct thermal_zone_device *tz = to_thermal_zone(dev);
255 unsigned long temperature;
257 if (!tz->ops->set_trip_hyst)
260 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
263 if (kstrtoul(buf, 10, &temperature))
267 * We are not doing any check on the 'temperature' value
268 * here. The driver implementing 'set_trip_hyst' has to
271 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
273 return ret ? ret : count;
277 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
280 struct thermal_zone_device *tz = to_thermal_zone(dev);
282 unsigned long temperature;
284 if (!tz->ops->get_trip_hyst)
287 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
290 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
292 return ret ? ret : sprintf(buf, "%ld\n", temperature);
296 passive_store(struct device *dev, struct device_attribute *attr,
297 const char *buf, size_t count)
299 struct thermal_zone_device *tz = to_thermal_zone(dev);
300 struct thermal_cooling_device *cdev = NULL;
303 if (!sscanf(buf, "%d\n", &state))
306 /* sanity check: values below 1000 millicelcius don't make sense
307 * and can cause the system to go into a thermal heart attack
309 if (state && state < 1000)
312 if (state && !tz->forced_passive) {
313 mutex_lock(&thermal_list_lock);
314 list_for_each_entry(cdev, &thermal_cdev_list, node) {
315 if (!strncmp("Processor", cdev->type,
316 sizeof("Processor")))
317 thermal_zone_bind_cooling_device(tz,
318 THERMAL_TRIPS_NONE, cdev,
322 mutex_unlock(&thermal_list_lock);
323 if (!tz->passive_delay)
324 tz->passive_delay = 1000;
325 } else if (!state && tz->forced_passive) {
326 mutex_lock(&thermal_list_lock);
327 list_for_each_entry(cdev, &thermal_cdev_list, node) {
328 if (!strncmp("Processor", cdev->type,
329 sizeof("Processor")))
330 thermal_zone_unbind_cooling_device(tz,
334 mutex_unlock(&thermal_list_lock);
335 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_cooling_device_instance *instance;
439 container_of(attr, struct thermal_cooling_device_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, THERMAL_NAME_LENGTH,
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, THERMAL_NAME_LENGTH,
615 "temp%d_crit", hwmon->count);
616 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
617 temp->temp_crit.attr.attr.mode = 0444;
618 temp->temp_crit.attr.show = temp_crit_show;
619 sysfs_attr_init(&temp->temp_crit.attr.attr);
620 result = device_create_file(hwmon->device,
621 &temp->temp_crit.attr);
623 goto unregister_input;
627 mutex_lock(&thermal_list_lock);
628 if (new_hwmon_device)
629 list_add_tail(&hwmon->node, &thermal_hwmon_list);
630 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
631 mutex_unlock(&thermal_list_lock);
636 device_remove_file(hwmon->device, &temp->temp_input.attr);
640 if (new_hwmon_device) {
641 device_remove_file(hwmon->device, &dev_attr_name);
642 hwmon_device_unregister(hwmon->device);
645 if (new_hwmon_device)
652 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
654 struct thermal_hwmon_device *hwmon;
655 struct thermal_hwmon_temp *temp;
657 hwmon = thermal_hwmon_lookup_by_type(tz);
658 if (unlikely(!hwmon)) {
659 /* Should never happen... */
660 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
664 temp = thermal_hwmon_lookup_temp(hwmon, tz);
665 if (unlikely(!temp)) {
666 /* Should never happen... */
667 dev_dbg(&tz->device, "temperature input lookup failed!\n");
671 device_remove_file(hwmon->device, &temp->temp_input.attr);
672 if (tz->ops->get_crit_temp)
673 device_remove_file(hwmon->device, &temp->temp_crit.attr);
675 mutex_lock(&thermal_list_lock);
676 list_del(&temp->hwmon_node);
678 if (!list_empty(&hwmon->tz_list)) {
679 mutex_unlock(&thermal_list_lock);
682 list_del(&hwmon->node);
683 mutex_unlock(&thermal_list_lock);
685 device_remove_file(hwmon->device, &dev_attr_name);
686 hwmon_device_unregister(hwmon->device);
691 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
697 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
702 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
705 cancel_delayed_work(&(tz->poll_queue));
711 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
712 round_jiffies(msecs_to_jiffies(delay)));
714 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
715 msecs_to_jiffies(delay));
718 static void thermal_zone_device_passive(struct thermal_zone_device *tz,
719 int temp, int trip_temp, int trip)
722 struct thermal_cooling_device_instance *instance;
723 struct thermal_cooling_device *cdev;
724 long state, max_state;
729 * Calculate the thermal trend (using the passive cooling equation)
730 * and modify the performance limit for all passive cooling devices
731 * accordingly. Note that we assume symmetry.
733 if (temp >= trip_temp) {
736 trend = (tz->tc1 * (temp - tz->last_temperature)) +
737 (tz->tc2 * (temp - trip_temp));
741 list_for_each_entry(instance, &tz->cooling_devices,
743 if (instance->trip != trip)
745 cdev = instance->cdev;
746 cdev->ops->get_cur_state(cdev, &state);
747 cdev->ops->get_max_state(cdev, &max_state);
748 if (state++ < max_state)
749 cdev->ops->set_cur_state(cdev, state);
751 } else if (trend < 0) { /* Cooling off? */
752 list_for_each_entry(instance, &tz->cooling_devices,
754 if (instance->trip != trip)
756 cdev = instance->cdev;
757 cdev->ops->get_cur_state(cdev, &state);
758 cdev->ops->get_max_state(cdev, &max_state);
760 cdev->ops->set_cur_state(cdev, --state);
769 * Implement passive cooling hysteresis to slowly increase performance
770 * and avoid thrashing around the passive trip point. Note that we
773 list_for_each_entry(instance, &tz->cooling_devices, node) {
774 if (instance->trip != trip)
776 cdev = instance->cdev;
777 cdev->ops->get_cur_state(cdev, &state);
778 cdev->ops->get_max_state(cdev, &max_state);
780 cdev->ops->set_cur_state(cdev, --state);
786 static void thermal_zone_device_check(struct work_struct *work)
788 struct thermal_zone_device *tz = container_of(work, struct
791 thermal_zone_device_update(tz);
795 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
796 * @tz: thermal zone device
797 * @trip: indicates which trip point the cooling devices is
798 * associated with in this thermal zone.
799 * @cdev: thermal cooling device
801 * This function is usually called in the thermal zone device .bind callback.
803 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
805 struct thermal_cooling_device *cdev,
806 unsigned long upper, unsigned long lower)
808 struct thermal_cooling_device_instance *dev;
809 struct thermal_cooling_device_instance *pos;
810 struct thermal_zone_device *pos1;
811 struct thermal_cooling_device *pos2;
812 unsigned long max_state;
815 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
818 list_for_each_entry(pos1, &thermal_tz_list, node) {
822 list_for_each_entry(pos2, &thermal_cdev_list, node) {
827 if (tz != pos1 || cdev != pos2)
830 cdev->ops->get_max_state(cdev, &max_state);
832 /* lower default 0, upper default max_state */
833 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
834 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
836 if (lower > upper || upper > max_state)
840 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
849 result = get_idr(&tz->idr, &tz->lock, &dev->id);
853 sprintf(dev->name, "cdev%d", dev->id);
855 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
859 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
860 sysfs_attr_init(&dev->attr.attr);
861 dev->attr.attr.name = dev->attr_name;
862 dev->attr.attr.mode = 0444;
863 dev->attr.show = thermal_cooling_device_trip_point_show;
864 result = device_create_file(&tz->device, &dev->attr);
866 goto remove_symbol_link;
868 mutex_lock(&tz->lock);
869 list_for_each_entry(pos, &tz->cooling_devices, node)
870 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
875 list_add_tail(&dev->node, &tz->cooling_devices);
876 mutex_unlock(&tz->lock);
881 device_remove_file(&tz->device, &dev->attr);
883 sysfs_remove_link(&tz->device.kobj, dev->name);
885 release_idr(&tz->idr, &tz->lock, dev->id);
890 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
893 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
894 * @tz: thermal zone device
895 * @trip: indicates which trip point the cooling devices is
896 * associated with in this thermal zone.
897 * @cdev: thermal cooling device
899 * This function is usually called in the thermal zone device .unbind callback.
901 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
903 struct thermal_cooling_device *cdev)
905 struct thermal_cooling_device_instance *pos, *next;
907 mutex_lock(&tz->lock);
908 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
909 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
910 list_del(&pos->node);
911 mutex_unlock(&tz->lock);
915 mutex_unlock(&tz->lock);
920 device_remove_file(&tz->device, &pos->attr);
921 sysfs_remove_link(&tz->device.kobj, pos->name);
922 release_idr(&tz->idr, &tz->lock, pos->id);
926 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
928 static void thermal_release(struct device *dev)
930 struct thermal_zone_device *tz;
931 struct thermal_cooling_device *cdev;
933 if (!strncmp(dev_name(dev), "thermal_zone",
934 sizeof("thermal_zone") - 1)) {
935 tz = to_thermal_zone(dev);
938 cdev = to_cooling_device(dev);
943 static struct class thermal_class = {
945 .dev_release = thermal_release,
949 * thermal_cooling_device_register - register a new thermal cooling device
950 * @type: the thermal cooling device type.
951 * @devdata: device private data.
952 * @ops: standard thermal cooling devices callbacks.
954 struct thermal_cooling_device *
955 thermal_cooling_device_register(char *type, void *devdata,
956 const struct thermal_cooling_device_ops *ops)
958 struct thermal_cooling_device *cdev;
959 struct thermal_zone_device *pos;
962 if (strlen(type) >= THERMAL_NAME_LENGTH)
963 return ERR_PTR(-EINVAL);
965 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
967 return ERR_PTR(-EINVAL);
969 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
971 return ERR_PTR(-ENOMEM);
973 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
976 return ERR_PTR(result);
979 strcpy(cdev->type, type);
981 cdev->device.class = &thermal_class;
982 cdev->devdata = devdata;
983 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
984 result = device_register(&cdev->device);
986 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
988 return ERR_PTR(result);
993 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
998 result = device_create_file(&cdev->device, &dev_attr_max_state);
1002 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1006 mutex_lock(&thermal_list_lock);
1007 list_add(&cdev->node, &thermal_cdev_list);
1008 list_for_each_entry(pos, &thermal_tz_list, node) {
1009 if (!pos->ops->bind)
1011 result = pos->ops->bind(pos, cdev);
1016 mutex_unlock(&thermal_list_lock);
1022 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1023 device_unregister(&cdev->device);
1024 return ERR_PTR(result);
1026 EXPORT_SYMBOL(thermal_cooling_device_register);
1029 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1030 * @cdev: the thermal cooling device to remove.
1032 * thermal_cooling_device_unregister() must be called when the device is no
1035 void thermal_cooling_device_unregister(struct
1036 thermal_cooling_device
1039 struct thermal_zone_device *tz;
1040 struct thermal_cooling_device *pos = NULL;
1045 mutex_lock(&thermal_list_lock);
1046 list_for_each_entry(pos, &thermal_cdev_list, node)
1050 /* thermal cooling device not found */
1051 mutex_unlock(&thermal_list_lock);
1054 list_del(&cdev->node);
1055 list_for_each_entry(tz, &thermal_tz_list, node) {
1056 if (!tz->ops->unbind)
1058 tz->ops->unbind(tz, cdev);
1060 mutex_unlock(&thermal_list_lock);
1062 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1063 device_remove_file(&cdev->device, &dev_attr_max_state);
1064 device_remove_file(&cdev->device, &dev_attr_cur_state);
1066 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1067 device_unregister(&cdev->device);
1070 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1073 * thermal_zone_device_update - force an update of a thermal zone's state
1074 * @ttz: the thermal zone to update
1077 void thermal_zone_device_update(struct thermal_zone_device *tz)
1080 long temp, trip_temp;
1081 enum thermal_trip_type trip_type;
1082 struct thermal_cooling_device_instance *instance;
1083 struct thermal_cooling_device *cdev;
1084 unsigned long cur_state, max_state;
1086 mutex_lock(&tz->lock);
1088 if (tz->ops->get_temp(tz, &temp)) {
1089 /* get_temp failed - retry it later */
1090 pr_warn("failed to read out thermal zone %d\n", tz->id);
1094 for (count = 0; count < tz->trips; count++) {
1095 tz->ops->get_trip_type(tz, count, &trip_type);
1096 tz->ops->get_trip_temp(tz, count, &trip_temp);
1098 switch (trip_type) {
1099 case THERMAL_TRIP_CRITICAL:
1100 if (temp >= trip_temp) {
1101 if (tz->ops->notify)
1102 ret = tz->ops->notify(tz, count,
1105 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1107 orderly_poweroff(true);
1111 case THERMAL_TRIP_HOT:
1112 if (temp >= trip_temp)
1113 if (tz->ops->notify)
1114 tz->ops->notify(tz, count, trip_type);
1116 case THERMAL_TRIP_ACTIVE:
1117 list_for_each_entry(instance, &tz->cooling_devices,
1119 if (instance->trip != count)
1122 cdev = instance->cdev;
1124 cdev->ops->get_cur_state(cdev, &cur_state);
1125 cdev->ops->get_max_state(cdev, &max_state);
1127 if (temp >= trip_temp)
1129 cur_state < instance->upper ?
1134 cur_state > instance->lower ?
1138 cdev->ops->set_cur_state(cdev, cur_state);
1141 case THERMAL_TRIP_PASSIVE:
1142 if (temp >= trip_temp || tz->passive)
1143 thermal_zone_device_passive(tz, temp,
1149 if (tz->forced_passive)
1150 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1151 THERMAL_TRIPS_NONE);
1153 tz->last_temperature = temp;
1157 thermal_zone_device_set_polling(tz, tz->passive_delay);
1158 else if (tz->polling_delay)
1159 thermal_zone_device_set_polling(tz, tz->polling_delay);
1161 thermal_zone_device_set_polling(tz, 0);
1162 mutex_unlock(&tz->lock);
1164 EXPORT_SYMBOL(thermal_zone_device_update);
1167 * create_trip_attrs - create attributes for trip points
1168 * @tz: the thermal zone device
1169 * @mask: Writeable trip point bitmap.
1171 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1174 int size = sizeof(struct thermal_attr) * tz->trips;
1176 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1177 if (!tz->trip_type_attrs)
1180 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1181 if (!tz->trip_temp_attrs) {
1182 kfree(tz->trip_type_attrs);
1186 if (tz->ops->get_trip_hyst) {
1187 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1188 if (!tz->trip_hyst_attrs) {
1189 kfree(tz->trip_type_attrs);
1190 kfree(tz->trip_temp_attrs);
1196 for (indx = 0; indx < tz->trips; indx++) {
1197 /* create trip type attribute */
1198 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1199 "trip_point_%d_type", indx);
1201 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1202 tz->trip_type_attrs[indx].attr.attr.name =
1203 tz->trip_type_attrs[indx].name;
1204 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1205 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1207 device_create_file(&tz->device,
1208 &tz->trip_type_attrs[indx].attr);
1210 /* create trip temp attribute */
1211 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1212 "trip_point_%d_temp", indx);
1214 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1215 tz->trip_temp_attrs[indx].attr.attr.name =
1216 tz->trip_temp_attrs[indx].name;
1217 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1218 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1219 if (mask & (1 << indx)) {
1220 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1221 tz->trip_temp_attrs[indx].attr.store =
1222 trip_point_temp_store;
1225 device_create_file(&tz->device,
1226 &tz->trip_temp_attrs[indx].attr);
1228 /* create Optional trip hyst attribute */
1229 if (!tz->ops->get_trip_hyst)
1231 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1232 "trip_point_%d_hyst", indx);
1234 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1235 tz->trip_hyst_attrs[indx].attr.attr.name =
1236 tz->trip_hyst_attrs[indx].name;
1237 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1238 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1239 if (tz->ops->set_trip_hyst) {
1240 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1241 tz->trip_hyst_attrs[indx].attr.store =
1242 trip_point_hyst_store;
1245 device_create_file(&tz->device,
1246 &tz->trip_hyst_attrs[indx].attr);
1251 static void remove_trip_attrs(struct thermal_zone_device *tz)
1255 for (indx = 0; indx < tz->trips; indx++) {
1256 device_remove_file(&tz->device,
1257 &tz->trip_type_attrs[indx].attr);
1258 device_remove_file(&tz->device,
1259 &tz->trip_temp_attrs[indx].attr);
1260 if (tz->ops->get_trip_hyst)
1261 device_remove_file(&tz->device,
1262 &tz->trip_hyst_attrs[indx].attr);
1264 kfree(tz->trip_type_attrs);
1265 kfree(tz->trip_temp_attrs);
1266 kfree(tz->trip_hyst_attrs);
1270 * thermal_zone_device_register - register a new thermal zone device
1271 * @type: the thermal zone device type
1272 * @trips: the number of trip points the thermal zone support
1273 * @mask: a bit string indicating the writeablility of trip points
1274 * @devdata: private device data
1275 * @ops: standard thermal zone device callbacks
1276 * @tc1: thermal coefficient 1 for passive calculations
1277 * @tc2: thermal coefficient 2 for passive calculations
1278 * @passive_delay: number of milliseconds to wait between polls when
1279 * performing passive cooling
1280 * @polling_delay: number of milliseconds to wait between polls when checking
1281 * whether trip points have been crossed (0 for interrupt
1284 * thermal_zone_device_unregister() must be called when the device is no
1285 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1286 * section 11.1.5.1 of the ACPI specification 3.0.
1288 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1289 int trips, int mask, void *devdata,
1290 const struct thermal_zone_device_ops *ops,
1291 int tc1, int tc2, int passive_delay, int polling_delay)
1293 struct thermal_zone_device *tz;
1294 struct thermal_cooling_device *pos;
1295 enum thermal_trip_type trip_type;
1300 if (strlen(type) >= THERMAL_NAME_LENGTH)
1301 return ERR_PTR(-EINVAL);
1303 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1304 return ERR_PTR(-EINVAL);
1306 if (!ops || !ops->get_temp)
1307 return ERR_PTR(-EINVAL);
1309 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1311 return ERR_PTR(-ENOMEM);
1313 INIT_LIST_HEAD(&tz->cooling_devices);
1315 mutex_init(&tz->lock);
1316 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1319 return ERR_PTR(result);
1322 strcpy(tz->type, type);
1324 tz->device.class = &thermal_class;
1325 tz->devdata = devdata;
1329 tz->passive_delay = passive_delay;
1330 tz->polling_delay = polling_delay;
1332 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1333 result = device_register(&tz->device);
1335 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1337 return ERR_PTR(result);
1342 result = device_create_file(&tz->device, &dev_attr_type);
1347 result = device_create_file(&tz->device, &dev_attr_temp);
1351 if (ops->get_mode) {
1352 result = device_create_file(&tz->device, &dev_attr_mode);
1357 result = create_trip_attrs(tz, mask);
1361 for (count = 0; count < trips; count++) {
1362 tz->ops->get_trip_type(tz, count, &trip_type);
1363 if (trip_type == THERMAL_TRIP_PASSIVE)
1368 result = device_create_file(&tz->device,
1374 result = thermal_add_hwmon_sysfs(tz);
1378 mutex_lock(&thermal_list_lock);
1379 list_add_tail(&tz->node, &thermal_tz_list);
1381 list_for_each_entry(pos, &thermal_cdev_list, node) {
1382 result = ops->bind(tz, pos);
1386 mutex_unlock(&thermal_list_lock);
1388 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1390 thermal_zone_device_update(tz);
1396 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1397 device_unregister(&tz->device);
1398 return ERR_PTR(result);
1400 EXPORT_SYMBOL(thermal_zone_device_register);
1403 * thermal_device_unregister - removes the registered thermal zone device
1404 * @tz: the thermal zone device to remove
1406 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1408 struct thermal_cooling_device *cdev;
1409 struct thermal_zone_device *pos = NULL;
1414 mutex_lock(&thermal_list_lock);
1415 list_for_each_entry(pos, &thermal_tz_list, node)
1419 /* thermal zone device not found */
1420 mutex_unlock(&thermal_list_lock);
1423 list_del(&tz->node);
1424 if (tz->ops->unbind)
1425 list_for_each_entry(cdev, &thermal_cdev_list, node)
1426 tz->ops->unbind(tz, cdev);
1427 mutex_unlock(&thermal_list_lock);
1429 thermal_zone_device_set_polling(tz, 0);
1432 device_remove_file(&tz->device, &dev_attr_type);
1433 device_remove_file(&tz->device, &dev_attr_temp);
1434 if (tz->ops->get_mode)
1435 device_remove_file(&tz->device, &dev_attr_mode);
1436 remove_trip_attrs(tz);
1438 thermal_remove_hwmon_sysfs(tz);
1439 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1440 idr_destroy(&tz->idr);
1441 mutex_destroy(&tz->lock);
1442 device_unregister(&tz->device);
1445 EXPORT_SYMBOL(thermal_zone_device_unregister);
1448 static struct genl_family thermal_event_genl_family = {
1449 .id = GENL_ID_GENERATE,
1450 .name = THERMAL_GENL_FAMILY_NAME,
1451 .version = THERMAL_GENL_VERSION,
1452 .maxattr = THERMAL_GENL_ATTR_MAX,
1455 static struct genl_multicast_group thermal_event_mcgrp = {
1456 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1459 int thermal_generate_netlink_event(u32 orig, enum events event)
1461 struct sk_buff *skb;
1462 struct nlattr *attr;
1463 struct thermal_genl_event *thermal_event;
1467 static unsigned int thermal_event_seqnum;
1469 /* allocate memory */
1470 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1473 skb = genlmsg_new(size, GFP_ATOMIC);
1477 /* add the genetlink message header */
1478 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1479 &thermal_event_genl_family, 0,
1480 THERMAL_GENL_CMD_EVENT);
1487 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1488 sizeof(struct thermal_genl_event));
1495 thermal_event = nla_data(attr);
1496 if (!thermal_event) {
1501 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1503 thermal_event->orig = orig;
1504 thermal_event->event = event;
1506 /* send multicast genetlink message */
1507 result = genlmsg_end(skb, msg_header);
1513 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1515 pr_info("failed to send netlink event:%d\n", result);
1519 EXPORT_SYMBOL(thermal_generate_netlink_event);
1521 static int genetlink_init(void)
1525 result = genl_register_family(&thermal_event_genl_family);
1529 result = genl_register_mc_group(&thermal_event_genl_family,
1530 &thermal_event_mcgrp);
1532 genl_unregister_family(&thermal_event_genl_family);
1536 static void genetlink_exit(void)
1538 genl_unregister_family(&thermal_event_genl_family);
1540 #else /* !CONFIG_NET */
1541 static inline int genetlink_init(void) { return 0; }
1542 static inline void genetlink_exit(void) {}
1543 #endif /* !CONFIG_NET */
1545 static int __init thermal_init(void)
1549 result = class_register(&thermal_class);
1551 idr_destroy(&thermal_tz_idr);
1552 idr_destroy(&thermal_cdev_idr);
1553 mutex_destroy(&thermal_idr_lock);
1554 mutex_destroy(&thermal_list_lock);
1556 result = genetlink_init();
1560 static void __exit thermal_exit(void)
1562 class_unregister(&thermal_class);
1563 idr_destroy(&thermal_tz_idr);
1564 idr_destroy(&thermal_cdev_idr);
1565 mutex_destroy(&thermal_idr_lock);
1566 mutex_destroy(&thermal_list_lock);
1570 fs_initcall(thermal_init);
1571 module_exit(thermal_exit);