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 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/err.h>
29 #include <linux/slab.h>
30 #include <linux/kdev_t.h>
31 #include <linux/idr.h>
32 #include <linux/thermal.h>
33 #include <linux/spinlock.h>
34 #include <linux/reboot.h>
35 #include <net/netlink.h>
36 #include <net/genetlink.h>
38 MODULE_AUTHOR("Zhang Rui");
39 MODULE_DESCRIPTION("Generic thermal management sysfs support");
40 MODULE_LICENSE("GPL");
42 #define PREFIX "Thermal: "
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")))
154 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
155 else if (!strncmp(buf, "disabled", sizeof("disabled")))
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_show(struct device *dev, struct device_attribute *attr,
202 struct thermal_zone_device *tz = to_thermal_zone(dev);
206 if (!tz->ops->get_trip_temp)
209 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
212 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
217 return sprintf(buf, "%ld\n", temperature);
221 passive_store(struct device *dev, struct device_attribute *attr,
222 const char *buf, size_t count)
224 struct thermal_zone_device *tz = to_thermal_zone(dev);
225 struct thermal_cooling_device *cdev = NULL;
228 if (!sscanf(buf, "%d\n", &state))
231 /* sanity check: values below 1000 millicelcius don't make sense
232 * and can cause the system to go into a thermal heart attack
234 if (state && state < 1000)
237 if (state && !tz->forced_passive) {
238 mutex_lock(&thermal_list_lock);
239 list_for_each_entry(cdev, &thermal_cdev_list, node) {
240 if (!strncmp("Processor", cdev->type,
241 sizeof("Processor")))
242 thermal_zone_bind_cooling_device(tz,
246 mutex_unlock(&thermal_list_lock);
247 if (!tz->passive_delay)
248 tz->passive_delay = 1000;
249 } else if (!state && tz->forced_passive) {
250 mutex_lock(&thermal_list_lock);
251 list_for_each_entry(cdev, &thermal_cdev_list, node) {
252 if (!strncmp("Processor", cdev->type,
253 sizeof("Processor")))
254 thermal_zone_unbind_cooling_device(tz,
258 mutex_unlock(&thermal_list_lock);
259 tz->passive_delay = 0;
265 tz->forced_passive = state;
267 thermal_zone_device_update(tz);
273 passive_show(struct device *dev, struct device_attribute *attr,
276 struct thermal_zone_device *tz = to_thermal_zone(dev);
278 return sprintf(buf, "%d\n", tz->forced_passive);
281 static DEVICE_ATTR(type, 0444, type_show, NULL);
282 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
283 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
284 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
286 static struct device_attribute trip_point_attrs[] = {
287 __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL),
288 __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL),
289 __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL),
290 __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL),
291 __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL),
292 __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL),
293 __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL),
294 __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL),
295 __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL),
296 __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL),
297 __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL),
298 __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL),
299 __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL),
300 __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL),
301 __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL),
302 __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL),
303 __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL),
304 __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL),
305 __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL),
306 __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL),
307 __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL),
308 __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL),
309 __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL),
310 __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL),
313 #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
315 result = device_create_file(_dev, \
316 &trip_point_attrs[_index * 2]); \
319 result = device_create_file(_dev, \
320 &trip_point_attrs[_index * 2 + 1]); \
323 #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
325 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
326 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
329 /* sys I/F for cooling device */
330 #define to_cooling_device(_dev) \
331 container_of(_dev, struct thermal_cooling_device, device)
334 thermal_cooling_device_type_show(struct device *dev,
335 struct device_attribute *attr, char *buf)
337 struct thermal_cooling_device *cdev = to_cooling_device(dev);
339 return sprintf(buf, "%s\n", cdev->type);
343 thermal_cooling_device_max_state_show(struct device *dev,
344 struct device_attribute *attr, char *buf)
346 struct thermal_cooling_device *cdev = to_cooling_device(dev);
350 ret = cdev->ops->get_max_state(cdev, &state);
353 return sprintf(buf, "%ld\n", state);
357 thermal_cooling_device_cur_state_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
360 struct thermal_cooling_device *cdev = to_cooling_device(dev);
364 ret = cdev->ops->get_cur_state(cdev, &state);
367 return sprintf(buf, "%ld\n", state);
371 thermal_cooling_device_cur_state_store(struct device *dev,
372 struct device_attribute *attr,
373 const char *buf, size_t count)
375 struct thermal_cooling_device *cdev = to_cooling_device(dev);
379 if (!sscanf(buf, "%ld\n", &state))
385 result = cdev->ops->set_cur_state(cdev, state);
391 static struct device_attribute dev_attr_cdev_type =
392 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
393 static DEVICE_ATTR(max_state, 0444,
394 thermal_cooling_device_max_state_show, NULL);
395 static DEVICE_ATTR(cur_state, 0644,
396 thermal_cooling_device_cur_state_show,
397 thermal_cooling_device_cur_state_store);
400 thermal_cooling_device_trip_point_show(struct device *dev,
401 struct device_attribute *attr, char *buf)
403 struct thermal_cooling_device_instance *instance;
406 container_of(attr, struct thermal_cooling_device_instance, attr);
408 if (instance->trip == THERMAL_TRIPS_NONE)
409 return sprintf(buf, "-1\n");
411 return sprintf(buf, "%d\n", instance->trip);
414 /* Device management */
416 #if defined(CONFIG_THERMAL_HWMON)
419 #include <linux/hwmon.h>
421 /* thermal zone devices with the same type share one hwmon device */
422 struct thermal_hwmon_device {
423 char type[THERMAL_NAME_LENGTH];
424 struct device *device;
426 struct list_head tz_list;
427 struct list_head node;
430 struct thermal_hwmon_attr {
431 struct device_attribute attr;
435 /* one temperature input for each thermal zone */
436 struct thermal_hwmon_temp {
437 struct list_head hwmon_node;
438 struct thermal_zone_device *tz;
439 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
440 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
443 static LIST_HEAD(thermal_hwmon_list);
446 name_show(struct device *dev, struct device_attribute *attr, char *buf)
448 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
449 return sprintf(buf, "%s\n", hwmon->type);
451 static DEVICE_ATTR(name, 0444, name_show, NULL);
454 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
458 struct thermal_hwmon_attr *hwmon_attr
459 = container_of(attr, struct thermal_hwmon_attr, attr);
460 struct thermal_hwmon_temp *temp
461 = container_of(hwmon_attr, struct thermal_hwmon_temp,
463 struct thermal_zone_device *tz = temp->tz;
465 ret = tz->ops->get_temp(tz, &temperature);
470 return sprintf(buf, "%ld\n", temperature);
474 temp_crit_show(struct device *dev, struct device_attribute *attr,
477 struct thermal_hwmon_attr *hwmon_attr
478 = container_of(attr, struct thermal_hwmon_attr, attr);
479 struct thermal_hwmon_temp *temp
480 = container_of(hwmon_attr, struct thermal_hwmon_temp,
482 struct thermal_zone_device *tz = temp->tz;
486 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
490 return sprintf(buf, "%ld\n", temperature);
494 static struct thermal_hwmon_device *
495 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
497 struct thermal_hwmon_device *hwmon;
499 mutex_lock(&thermal_list_lock);
500 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
501 if (!strcmp(hwmon->type, tz->type)) {
502 mutex_unlock(&thermal_list_lock);
505 mutex_unlock(&thermal_list_lock);
510 /* Find the temperature input matching a given thermal zone */
511 static struct thermal_hwmon_temp *
512 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
513 const struct thermal_zone_device *tz)
515 struct thermal_hwmon_temp *temp;
517 mutex_lock(&thermal_list_lock);
518 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
519 if (temp->tz == tz) {
520 mutex_unlock(&thermal_list_lock);
523 mutex_unlock(&thermal_list_lock);
529 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
531 struct thermal_hwmon_device *hwmon;
532 struct thermal_hwmon_temp *temp;
533 int new_hwmon_device = 1;
536 hwmon = thermal_hwmon_lookup_by_type(tz);
538 new_hwmon_device = 0;
539 goto register_sys_interface;
542 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
546 INIT_LIST_HEAD(&hwmon->tz_list);
547 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
548 hwmon->device = hwmon_device_register(NULL);
549 if (IS_ERR(hwmon->device)) {
550 result = PTR_ERR(hwmon->device);
553 dev_set_drvdata(hwmon->device, hwmon);
554 result = device_create_file(hwmon->device, &dev_attr_name);
558 register_sys_interface:
559 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
562 goto unregister_name;
568 snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH,
569 "temp%d_input", hwmon->count);
570 temp->temp_input.attr.attr.name = temp->temp_input.name;
571 temp->temp_input.attr.attr.mode = 0444;
572 temp->temp_input.attr.show = temp_input_show;
573 sysfs_attr_init(&temp->temp_input.attr.attr);
574 result = device_create_file(hwmon->device, &temp->temp_input.attr);
578 if (tz->ops->get_crit_temp) {
579 unsigned long temperature;
580 if (!tz->ops->get_crit_temp(tz, &temperature)) {
581 snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH,
582 "temp%d_crit", hwmon->count);
583 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
584 temp->temp_crit.attr.attr.mode = 0444;
585 temp->temp_crit.attr.show = temp_crit_show;
586 sysfs_attr_init(&temp->temp_crit.attr.attr);
587 result = device_create_file(hwmon->device,
588 &temp->temp_crit.attr);
590 goto unregister_input;
594 mutex_lock(&thermal_list_lock);
595 if (new_hwmon_device)
596 list_add_tail(&hwmon->node, &thermal_hwmon_list);
597 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
598 mutex_unlock(&thermal_list_lock);
603 device_remove_file(hwmon->device, &temp->temp_input.attr);
607 if (new_hwmon_device) {
608 device_remove_file(hwmon->device, &dev_attr_name);
609 hwmon_device_unregister(hwmon->device);
612 if (new_hwmon_device)
619 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
621 struct thermal_hwmon_device *hwmon;
622 struct thermal_hwmon_temp *temp;
624 hwmon = thermal_hwmon_lookup_by_type(tz);
625 if (unlikely(!hwmon)) {
626 /* Should never happen... */
627 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
631 temp = thermal_hwmon_lookup_temp(hwmon, tz);
632 if (unlikely(!temp)) {
633 /* Should never happen... */
634 dev_dbg(&tz->device, "temperature input lookup failed!\n");
638 device_remove_file(hwmon->device, &temp->temp_input.attr);
639 if (tz->ops->get_crit_temp)
640 device_remove_file(hwmon->device, &temp->temp_crit.attr);
642 mutex_lock(&thermal_list_lock);
643 list_del(&temp->hwmon_node);
645 if (!list_empty(&hwmon->tz_list)) {
646 mutex_unlock(&thermal_list_lock);
649 list_del(&hwmon->node);
650 mutex_unlock(&thermal_list_lock);
652 device_remove_file(hwmon->device, &dev_attr_name);
653 hwmon_device_unregister(hwmon->device);
658 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
664 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
669 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
672 cancel_delayed_work(&(tz->poll_queue));
678 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
679 round_jiffies(msecs_to_jiffies(delay)));
681 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
682 msecs_to_jiffies(delay));
685 static void thermal_zone_device_passive(struct thermal_zone_device *tz,
686 int temp, int trip_temp, int trip)
689 struct thermal_cooling_device_instance *instance;
690 struct thermal_cooling_device *cdev;
691 long state, max_state;
696 * Calculate the thermal trend (using the passive cooling equation)
697 * and modify the performance limit for all passive cooling devices
698 * accordingly. Note that we assume symmetry.
700 if (temp >= trip_temp) {
703 trend = (tz->tc1 * (temp - tz->last_temperature)) +
704 (tz->tc2 * (temp - trip_temp));
708 list_for_each_entry(instance, &tz->cooling_devices,
710 if (instance->trip != trip)
712 cdev = instance->cdev;
713 cdev->ops->get_cur_state(cdev, &state);
714 cdev->ops->get_max_state(cdev, &max_state);
715 if (state++ < max_state)
716 cdev->ops->set_cur_state(cdev, state);
718 } else if (trend < 0) { /* Cooling off? */
719 list_for_each_entry(instance, &tz->cooling_devices,
721 if (instance->trip != trip)
723 cdev = instance->cdev;
724 cdev->ops->get_cur_state(cdev, &state);
725 cdev->ops->get_max_state(cdev, &max_state);
727 cdev->ops->set_cur_state(cdev, --state);
736 * Implement passive cooling hysteresis to slowly increase performance
737 * and avoid thrashing around the passive trip point. Note that we
740 list_for_each_entry(instance, &tz->cooling_devices, node) {
741 if (instance->trip != trip)
743 cdev = instance->cdev;
744 cdev->ops->get_cur_state(cdev, &state);
745 cdev->ops->get_max_state(cdev, &max_state);
747 cdev->ops->set_cur_state(cdev, --state);
753 static void thermal_zone_device_check(struct work_struct *work)
755 struct thermal_zone_device *tz = container_of(work, struct
758 thermal_zone_device_update(tz);
762 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
763 * @tz: thermal zone device
764 * @trip: indicates which trip point the cooling devices is
765 * associated with in this thermal zone.
766 * @cdev: thermal cooling device
768 * This function is usually called in the thermal zone device .bind callback.
770 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
772 struct thermal_cooling_device *cdev)
774 struct thermal_cooling_device_instance *dev;
775 struct thermal_cooling_device_instance *pos;
776 struct thermal_zone_device *pos1;
777 struct thermal_cooling_device *pos2;
780 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
783 list_for_each_entry(pos1, &thermal_tz_list, node) {
787 list_for_each_entry(pos2, &thermal_cdev_list, node) {
792 if (tz != pos1 || cdev != pos2)
796 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
802 result = get_idr(&tz->idr, &tz->lock, &dev->id);
806 sprintf(dev->name, "cdev%d", dev->id);
808 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
812 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
813 sysfs_attr_init(&dev->attr.attr);
814 dev->attr.attr.name = dev->attr_name;
815 dev->attr.attr.mode = 0444;
816 dev->attr.show = thermal_cooling_device_trip_point_show;
817 result = device_create_file(&tz->device, &dev->attr);
819 goto remove_symbol_link;
821 mutex_lock(&tz->lock);
822 list_for_each_entry(pos, &tz->cooling_devices, node)
823 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
828 list_add_tail(&dev->node, &tz->cooling_devices);
829 mutex_unlock(&tz->lock);
834 device_remove_file(&tz->device, &dev->attr);
836 sysfs_remove_link(&tz->device.kobj, dev->name);
838 release_idr(&tz->idr, &tz->lock, dev->id);
844 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
847 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
848 * @tz: thermal zone device
849 * @trip: indicates which trip point the cooling devices is
850 * associated with in this thermal zone.
851 * @cdev: thermal cooling device
853 * This function is usually called in the thermal zone device .unbind callback.
855 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
857 struct thermal_cooling_device *cdev)
859 struct thermal_cooling_device_instance *pos, *next;
861 mutex_lock(&tz->lock);
862 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
863 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
864 list_del(&pos->node);
865 mutex_unlock(&tz->lock);
869 mutex_unlock(&tz->lock);
874 device_remove_file(&tz->device, &pos->attr);
875 sysfs_remove_link(&tz->device.kobj, pos->name);
876 release_idr(&tz->idr, &tz->lock, pos->id);
881 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
883 static void thermal_release(struct device *dev)
885 struct thermal_zone_device *tz;
886 struct thermal_cooling_device *cdev;
888 if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) {
889 tz = to_thermal_zone(dev);
892 cdev = to_cooling_device(dev);
897 static struct class thermal_class = {
899 .dev_release = thermal_release,
903 * thermal_cooling_device_register - register a new thermal cooling device
904 * @type: the thermal cooling device type.
905 * @devdata: device private data.
906 * @ops: standard thermal cooling devices callbacks.
908 struct thermal_cooling_device *thermal_cooling_device_register(
909 char *type, void *devdata, const struct thermal_cooling_device_ops *ops)
911 struct thermal_cooling_device *cdev;
912 struct thermal_zone_device *pos;
915 if (strlen(type) >= THERMAL_NAME_LENGTH)
916 return ERR_PTR(-EINVAL);
918 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
920 return ERR_PTR(-EINVAL);
922 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
924 return ERR_PTR(-ENOMEM);
926 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
929 return ERR_PTR(result);
932 strcpy(cdev->type, type);
934 cdev->device.class = &thermal_class;
935 cdev->devdata = devdata;
936 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
937 result = device_register(&cdev->device);
939 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
941 return ERR_PTR(result);
946 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
951 result = device_create_file(&cdev->device, &dev_attr_max_state);
955 result = device_create_file(&cdev->device, &dev_attr_cur_state);
959 mutex_lock(&thermal_list_lock);
960 list_add(&cdev->node, &thermal_cdev_list);
961 list_for_each_entry(pos, &thermal_tz_list, node) {
964 result = pos->ops->bind(pos, cdev);
969 mutex_unlock(&thermal_list_lock);
975 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
976 device_unregister(&cdev->device);
977 return ERR_PTR(result);
980 EXPORT_SYMBOL(thermal_cooling_device_register);
983 * thermal_cooling_device_unregister - removes the registered thermal cooling device
984 * @cdev: the thermal cooling device to remove.
986 * thermal_cooling_device_unregister() must be called when the device is no
989 void thermal_cooling_device_unregister(struct
990 thermal_cooling_device
993 struct thermal_zone_device *tz;
994 struct thermal_cooling_device *pos = NULL;
999 mutex_lock(&thermal_list_lock);
1000 list_for_each_entry(pos, &thermal_cdev_list, node)
1004 /* thermal cooling device not found */
1005 mutex_unlock(&thermal_list_lock);
1008 list_del(&cdev->node);
1009 list_for_each_entry(tz, &thermal_tz_list, node) {
1010 if (!tz->ops->unbind)
1012 tz->ops->unbind(tz, cdev);
1014 mutex_unlock(&thermal_list_lock);
1016 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1017 device_remove_file(&cdev->device, &dev_attr_max_state);
1018 device_remove_file(&cdev->device, &dev_attr_cur_state);
1020 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1021 device_unregister(&cdev->device);
1025 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1028 * thermal_zone_device_update - force an update of a thermal zone's state
1029 * @ttz: the thermal zone to update
1032 void thermal_zone_device_update(struct thermal_zone_device *tz)
1035 long temp, trip_temp;
1036 enum thermal_trip_type trip_type;
1037 struct thermal_cooling_device_instance *instance;
1038 struct thermal_cooling_device *cdev;
1040 mutex_lock(&tz->lock);
1042 if (tz->ops->get_temp(tz, &temp)) {
1043 /* get_temp failed - retry it later */
1044 printk(KERN_WARNING PREFIX "failed to read out thermal zone "
1049 for (count = 0; count < tz->trips; count++) {
1050 tz->ops->get_trip_type(tz, count, &trip_type);
1051 tz->ops->get_trip_temp(tz, count, &trip_temp);
1053 switch (trip_type) {
1054 case THERMAL_TRIP_CRITICAL:
1055 if (temp >= trip_temp) {
1056 if (tz->ops->notify)
1057 ret = tz->ops->notify(tz, count,
1061 "Critical temperature reached (%ld C), shutting down.\n",
1063 orderly_poweroff(true);
1067 case THERMAL_TRIP_HOT:
1068 if (temp >= trip_temp)
1069 if (tz->ops->notify)
1070 tz->ops->notify(tz, count, trip_type);
1072 case THERMAL_TRIP_ACTIVE:
1073 list_for_each_entry(instance, &tz->cooling_devices,
1075 if (instance->trip != count)
1078 cdev = instance->cdev;
1080 if (temp >= trip_temp)
1081 cdev->ops->set_cur_state(cdev, 1);
1083 cdev->ops->set_cur_state(cdev, 0);
1086 case THERMAL_TRIP_PASSIVE:
1087 if (temp >= trip_temp || tz->passive)
1088 thermal_zone_device_passive(tz, temp,
1094 if (tz->forced_passive)
1095 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1096 THERMAL_TRIPS_NONE);
1098 tz->last_temperature = temp;
1102 thermal_zone_device_set_polling(tz, tz->passive_delay);
1103 else if (tz->polling_delay)
1104 thermal_zone_device_set_polling(tz, tz->polling_delay);
1106 thermal_zone_device_set_polling(tz, 0);
1107 mutex_unlock(&tz->lock);
1109 EXPORT_SYMBOL(thermal_zone_device_update);
1112 * thermal_zone_device_register - register a new thermal zone device
1113 * @type: the thermal zone device type
1114 * @trips: the number of trip points the thermal zone support
1115 * @devdata: private device data
1116 * @ops: standard thermal zone device callbacks
1117 * @tc1: thermal coefficient 1 for passive calculations
1118 * @tc2: thermal coefficient 2 for passive calculations
1119 * @passive_delay: number of milliseconds to wait between polls when
1120 * performing passive cooling
1121 * @polling_delay: number of milliseconds to wait between polls when checking
1122 * whether trip points have been crossed (0 for interrupt
1125 * thermal_zone_device_unregister() must be called when the device is no
1126 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1127 * section 11.1.5.1 of the ACPI specification 3.0.
1129 struct thermal_zone_device *thermal_zone_device_register(char *type,
1130 int trips, void *devdata,
1131 const struct thermal_zone_device_ops *ops,
1132 int tc1, int tc2, int passive_delay, int polling_delay)
1134 struct thermal_zone_device *tz;
1135 struct thermal_cooling_device *pos;
1136 enum thermal_trip_type trip_type;
1141 if (strlen(type) >= THERMAL_NAME_LENGTH)
1142 return ERR_PTR(-EINVAL);
1144 if (trips > THERMAL_MAX_TRIPS || trips < 0)
1145 return ERR_PTR(-EINVAL);
1147 if (!ops || !ops->get_temp)
1148 return ERR_PTR(-EINVAL);
1150 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1152 return ERR_PTR(-ENOMEM);
1154 INIT_LIST_HEAD(&tz->cooling_devices);
1156 mutex_init(&tz->lock);
1157 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1160 return ERR_PTR(result);
1163 strcpy(tz->type, type);
1165 tz->device.class = &thermal_class;
1166 tz->devdata = devdata;
1170 tz->passive_delay = passive_delay;
1171 tz->polling_delay = polling_delay;
1173 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1174 result = device_register(&tz->device);
1176 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1178 return ERR_PTR(result);
1183 result = device_create_file(&tz->device, &dev_attr_type);
1188 result = device_create_file(&tz->device, &dev_attr_temp);
1192 if (ops->get_mode) {
1193 result = device_create_file(&tz->device, &dev_attr_mode);
1198 for (count = 0; count < trips; count++) {
1199 TRIP_POINT_ATTR_ADD(&tz->device, count, result);
1202 tz->ops->get_trip_type(tz, count, &trip_type);
1203 if (trip_type == THERMAL_TRIP_PASSIVE)
1208 result = device_create_file(&tz->device,
1214 result = thermal_add_hwmon_sysfs(tz);
1218 mutex_lock(&thermal_list_lock);
1219 list_add_tail(&tz->node, &thermal_tz_list);
1221 list_for_each_entry(pos, &thermal_cdev_list, node) {
1222 result = ops->bind(tz, pos);
1226 mutex_unlock(&thermal_list_lock);
1228 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1230 thermal_zone_device_update(tz);
1236 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1237 device_unregister(&tz->device);
1238 return ERR_PTR(result);
1241 EXPORT_SYMBOL(thermal_zone_device_register);
1244 * thermal_device_unregister - removes the registered thermal zone device
1245 * @tz: the thermal zone device to remove
1247 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1249 struct thermal_cooling_device *cdev;
1250 struct thermal_zone_device *pos = NULL;
1256 mutex_lock(&thermal_list_lock);
1257 list_for_each_entry(pos, &thermal_tz_list, node)
1261 /* thermal zone device not found */
1262 mutex_unlock(&thermal_list_lock);
1265 list_del(&tz->node);
1266 if (tz->ops->unbind)
1267 list_for_each_entry(cdev, &thermal_cdev_list, node)
1268 tz->ops->unbind(tz, cdev);
1269 mutex_unlock(&thermal_list_lock);
1271 thermal_zone_device_set_polling(tz, 0);
1274 device_remove_file(&tz->device, &dev_attr_type);
1275 device_remove_file(&tz->device, &dev_attr_temp);
1276 if (tz->ops->get_mode)
1277 device_remove_file(&tz->device, &dev_attr_mode);
1279 for (count = 0; count < tz->trips; count++)
1280 TRIP_POINT_ATTR_REMOVE(&tz->device, count);
1282 thermal_remove_hwmon_sysfs(tz);
1283 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1284 idr_destroy(&tz->idr);
1285 mutex_destroy(&tz->lock);
1286 device_unregister(&tz->device);
1290 EXPORT_SYMBOL(thermal_zone_device_unregister);
1293 static struct genl_family thermal_event_genl_family = {
1294 .id = GENL_ID_GENERATE,
1295 .name = THERMAL_GENL_FAMILY_NAME,
1296 .version = THERMAL_GENL_VERSION,
1297 .maxattr = THERMAL_GENL_ATTR_MAX,
1300 static struct genl_multicast_group thermal_event_mcgrp = {
1301 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1304 int thermal_generate_netlink_event(u32 orig, enum events event)
1306 struct sk_buff *skb;
1307 struct nlattr *attr;
1308 struct thermal_genl_event *thermal_event;
1312 static unsigned int thermal_event_seqnum;
1314 /* allocate memory */
1315 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1318 skb = genlmsg_new(size, GFP_ATOMIC);
1322 /* add the genetlink message header */
1323 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1324 &thermal_event_genl_family, 0,
1325 THERMAL_GENL_CMD_EVENT);
1332 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1333 sizeof(struct thermal_genl_event));
1340 thermal_event = nla_data(attr);
1341 if (!thermal_event) {
1346 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1348 thermal_event->orig = orig;
1349 thermal_event->event = event;
1351 /* send multicast genetlink message */
1352 result = genlmsg_end(skb, msg_header);
1358 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1360 printk(KERN_INFO "failed to send netlink event:%d", result);
1364 EXPORT_SYMBOL(thermal_generate_netlink_event);
1366 static int genetlink_init(void)
1370 result = genl_register_family(&thermal_event_genl_family);
1374 result = genl_register_mc_group(&thermal_event_genl_family,
1375 &thermal_event_mcgrp);
1377 genl_unregister_family(&thermal_event_genl_family);
1381 static void genetlink_exit(void)
1383 genl_unregister_family(&thermal_event_genl_family);
1385 #else /* !CONFIG_NET */
1386 static inline int genetlink_init(void) { return 0; }
1387 static inline void genetlink_exit(void) {}
1388 #endif /* !CONFIG_NET */
1390 static int __init thermal_init(void)
1394 result = class_register(&thermal_class);
1396 idr_destroy(&thermal_tz_idr);
1397 idr_destroy(&thermal_cdev_idr);
1398 mutex_destroy(&thermal_idr_lock);
1399 mutex_destroy(&thermal_list_lock);
1401 result = genetlink_init();
1405 static void __exit thermal_exit(void)
1407 class_unregister(&thermal_class);
1408 idr_destroy(&thermal_tz_idr);
1409 idr_destroy(&thermal_cdev_idr);
1410 mutex_destroy(&thermal_idr_lock);
1411 mutex_destroy(&thermal_list_lock);
1415 fs_initcall(thermal_init);
1416 module_exit(thermal_exit);