mwifiex: fix left_len calculation issue
[linux-2.6/btrfs-unstable.git] / drivers / thermal / thermal_core.c
blob71b0ec0c370d73aad18d685118c197735c5b2833
1 /*
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/reboot.h>
36 #include <linux/string.h>
37 #include <linux/of.h>
38 #include <net/netlink.h>
39 #include <net/genetlink.h>
41 #include "thermal_core.h"
42 #include "thermal_hwmon.h"
44 MODULE_AUTHOR("Zhang Rui");
45 MODULE_DESCRIPTION("Generic thermal management sysfs support");
46 MODULE_LICENSE("GPL v2");
48 static DEFINE_IDR(thermal_tz_idr);
49 static DEFINE_IDR(thermal_cdev_idr);
50 static DEFINE_MUTEX(thermal_idr_lock);
52 static LIST_HEAD(thermal_tz_list);
53 static LIST_HEAD(thermal_cdev_list);
54 static LIST_HEAD(thermal_governor_list);
56 static DEFINE_MUTEX(thermal_list_lock);
57 static DEFINE_MUTEX(thermal_governor_lock);
59 static struct thermal_governor *def_governor;
61 static struct thermal_governor *__find_governor(const char *name)
63 struct thermal_governor *pos;
65 if (!name || !name[0])
66 return def_governor;
68 list_for_each_entry(pos, &thermal_governor_list, governor_list)
69 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
70 return pos;
72 return NULL;
75 int thermal_register_governor(struct thermal_governor *governor)
77 int err;
78 const char *name;
79 struct thermal_zone_device *pos;
81 if (!governor)
82 return -EINVAL;
84 mutex_lock(&thermal_governor_lock);
86 err = -EBUSY;
87 if (__find_governor(governor->name) == NULL) {
88 err = 0;
89 list_add(&governor->governor_list, &thermal_governor_list);
90 if (!def_governor && !strncmp(governor->name,
91 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
92 def_governor = governor;
95 mutex_lock(&thermal_list_lock);
97 list_for_each_entry(pos, &thermal_tz_list, node) {
99 * only thermal zones with specified tz->tzp->governor_name
100 * may run with tz->govenor unset
102 if (pos->governor)
103 continue;
105 name = pos->tzp->governor_name;
107 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
108 pos->governor = governor;
111 mutex_unlock(&thermal_list_lock);
112 mutex_unlock(&thermal_governor_lock);
114 return err;
117 void thermal_unregister_governor(struct thermal_governor *governor)
119 struct thermal_zone_device *pos;
121 if (!governor)
122 return;
124 mutex_lock(&thermal_governor_lock);
126 if (__find_governor(governor->name) == NULL)
127 goto exit;
129 mutex_lock(&thermal_list_lock);
131 list_for_each_entry(pos, &thermal_tz_list, node) {
132 if (!strnicmp(pos->governor->name, governor->name,
133 THERMAL_NAME_LENGTH))
134 pos->governor = NULL;
137 mutex_unlock(&thermal_list_lock);
138 list_del(&governor->governor_list);
139 exit:
140 mutex_unlock(&thermal_governor_lock);
141 return;
144 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
146 int ret;
148 if (lock)
149 mutex_lock(lock);
150 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
151 if (lock)
152 mutex_unlock(lock);
153 if (unlikely(ret < 0))
154 return ret;
155 *id = ret;
156 return 0;
159 static void release_idr(struct idr *idr, struct mutex *lock, int id)
161 if (lock)
162 mutex_lock(lock);
163 idr_remove(idr, id);
164 if (lock)
165 mutex_unlock(lock);
168 int get_tz_trend(struct thermal_zone_device *tz, int trip)
170 enum thermal_trend trend;
172 if (tz->emul_temperature || !tz->ops->get_trend ||
173 tz->ops->get_trend(tz, trip, &trend)) {
174 if (tz->temperature > tz->last_temperature)
175 trend = THERMAL_TREND_RAISING;
176 else if (tz->temperature < tz->last_temperature)
177 trend = THERMAL_TREND_DROPPING;
178 else
179 trend = THERMAL_TREND_STABLE;
182 return trend;
184 EXPORT_SYMBOL(get_tz_trend);
186 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
187 struct thermal_cooling_device *cdev, int trip)
189 struct thermal_instance *pos = NULL;
190 struct thermal_instance *target_instance = NULL;
192 mutex_lock(&tz->lock);
193 mutex_lock(&cdev->lock);
195 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
196 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
197 target_instance = pos;
198 break;
202 mutex_unlock(&cdev->lock);
203 mutex_unlock(&tz->lock);
205 return target_instance;
207 EXPORT_SYMBOL(get_thermal_instance);
209 static void print_bind_err_msg(struct thermal_zone_device *tz,
210 struct thermal_cooling_device *cdev, int ret)
212 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
213 tz->type, cdev->type, ret);
216 static void __bind(struct thermal_zone_device *tz, int mask,
217 struct thermal_cooling_device *cdev,
218 unsigned long *limits)
220 int i, ret;
222 for (i = 0; i < tz->trips; i++) {
223 if (mask & (1 << i)) {
224 unsigned long upper, lower;
226 upper = THERMAL_NO_LIMIT;
227 lower = THERMAL_NO_LIMIT;
228 if (limits) {
229 lower = limits[i * 2];
230 upper = limits[i * 2 + 1];
232 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
233 upper, lower);
234 if (ret)
235 print_bind_err_msg(tz, cdev, ret);
240 static void __unbind(struct thermal_zone_device *tz, int mask,
241 struct thermal_cooling_device *cdev)
243 int i;
245 for (i = 0; i < tz->trips; i++)
246 if (mask & (1 << i))
247 thermal_zone_unbind_cooling_device(tz, i, cdev);
250 static void bind_cdev(struct thermal_cooling_device *cdev)
252 int i, ret;
253 const struct thermal_zone_params *tzp;
254 struct thermal_zone_device *pos = NULL;
256 mutex_lock(&thermal_list_lock);
258 list_for_each_entry(pos, &thermal_tz_list, node) {
259 if (!pos->tzp && !pos->ops->bind)
260 continue;
262 if (pos->ops->bind) {
263 ret = pos->ops->bind(pos, cdev);
264 if (ret)
265 print_bind_err_msg(pos, cdev, ret);
266 continue;
269 tzp = pos->tzp;
270 if (!tzp || !tzp->tbp)
271 continue;
273 for (i = 0; i < tzp->num_tbps; i++) {
274 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
275 continue;
276 if (tzp->tbp[i].match(pos, cdev))
277 continue;
278 tzp->tbp[i].cdev = cdev;
279 __bind(pos, tzp->tbp[i].trip_mask, cdev,
280 tzp->tbp[i].binding_limits);
284 mutex_unlock(&thermal_list_lock);
287 static void bind_tz(struct thermal_zone_device *tz)
289 int i, ret;
290 struct thermal_cooling_device *pos = NULL;
291 const struct thermal_zone_params *tzp = tz->tzp;
293 if (!tzp && !tz->ops->bind)
294 return;
296 mutex_lock(&thermal_list_lock);
298 /* If there is ops->bind, try to use ops->bind */
299 if (tz->ops->bind) {
300 list_for_each_entry(pos, &thermal_cdev_list, node) {
301 ret = tz->ops->bind(tz, pos);
302 if (ret)
303 print_bind_err_msg(tz, pos, ret);
305 goto exit;
308 if (!tzp || !tzp->tbp)
309 goto exit;
311 list_for_each_entry(pos, &thermal_cdev_list, node) {
312 for (i = 0; i < tzp->num_tbps; i++) {
313 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
314 continue;
315 if (tzp->tbp[i].match(tz, pos))
316 continue;
317 tzp->tbp[i].cdev = pos;
318 __bind(tz, tzp->tbp[i].trip_mask, pos,
319 tzp->tbp[i].binding_limits);
322 exit:
323 mutex_unlock(&thermal_list_lock);
326 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
327 int delay)
329 if (delay > 1000)
330 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
331 round_jiffies(msecs_to_jiffies(delay)));
332 else if (delay)
333 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
334 msecs_to_jiffies(delay));
335 else
336 cancel_delayed_work(&tz->poll_queue);
339 static void monitor_thermal_zone(struct thermal_zone_device *tz)
341 mutex_lock(&tz->lock);
343 if (tz->passive)
344 thermal_zone_device_set_polling(tz, tz->passive_delay);
345 else if (tz->polling_delay)
346 thermal_zone_device_set_polling(tz, tz->polling_delay);
347 else
348 thermal_zone_device_set_polling(tz, 0);
350 mutex_unlock(&tz->lock);
353 static void handle_non_critical_trips(struct thermal_zone_device *tz,
354 int trip, enum thermal_trip_type trip_type)
356 tz->governor ? tz->governor->throttle(tz, trip) :
357 def_governor->throttle(tz, trip);
360 static void handle_critical_trips(struct thermal_zone_device *tz,
361 int trip, enum thermal_trip_type trip_type)
363 long trip_temp;
365 tz->ops->get_trip_temp(tz, trip, &trip_temp);
367 /* If we have not crossed the trip_temp, we do not care. */
368 if (tz->temperature < trip_temp)
369 return;
371 if (tz->ops->notify)
372 tz->ops->notify(tz, trip, trip_type);
374 if (trip_type == THERMAL_TRIP_CRITICAL) {
375 dev_emerg(&tz->device,
376 "critical temperature reached(%d C),shutting down\n",
377 tz->temperature / 1000);
378 orderly_poweroff(true);
382 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
384 enum thermal_trip_type type;
386 tz->ops->get_trip_type(tz, trip, &type);
388 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
389 handle_critical_trips(tz, trip, type);
390 else
391 handle_non_critical_trips(tz, trip, type);
393 * Alright, we handled this trip successfully.
394 * So, start monitoring again.
396 monitor_thermal_zone(tz);
400 * thermal_zone_get_temp() - returns its the temperature of thermal zone
401 * @tz: a valid pointer to a struct thermal_zone_device
402 * @temp: a valid pointer to where to store the resulting temperature.
404 * When a valid thermal zone reference is passed, it will fetch its
405 * temperature and fill @temp.
407 * Return: On success returns 0, an error code otherwise
409 int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
411 int ret = -EINVAL;
412 #ifdef CONFIG_THERMAL_EMULATION
413 int count;
414 unsigned long crit_temp = -1UL;
415 enum thermal_trip_type type;
416 #endif
418 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
419 goto exit;
421 mutex_lock(&tz->lock);
423 ret = tz->ops->get_temp(tz, temp);
424 #ifdef CONFIG_THERMAL_EMULATION
425 if (!tz->emul_temperature)
426 goto skip_emul;
428 for (count = 0; count < tz->trips; count++) {
429 ret = tz->ops->get_trip_type(tz, count, &type);
430 if (!ret && type == THERMAL_TRIP_CRITICAL) {
431 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
432 break;
436 if (ret)
437 goto skip_emul;
439 if (*temp < crit_temp)
440 *temp = tz->emul_temperature;
441 skip_emul:
442 #endif
443 mutex_unlock(&tz->lock);
444 exit:
445 return ret;
447 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
449 static void update_temperature(struct thermal_zone_device *tz)
451 long temp;
452 int ret;
454 ret = thermal_zone_get_temp(tz, &temp);
455 if (ret) {
456 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
457 tz->id);
458 return;
461 mutex_lock(&tz->lock);
462 tz->last_temperature = tz->temperature;
463 tz->temperature = temp;
464 mutex_unlock(&tz->lock);
466 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
467 tz->last_temperature, tz->temperature);
470 void thermal_zone_device_update(struct thermal_zone_device *tz)
472 int count;
474 if (!tz->ops->get_temp)
475 return;
477 update_temperature(tz);
479 for (count = 0; count < tz->trips; count++)
480 handle_thermal_trip(tz, count);
482 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
484 static void thermal_zone_device_check(struct work_struct *work)
486 struct thermal_zone_device *tz = container_of(work, struct
487 thermal_zone_device,
488 poll_queue.work);
489 thermal_zone_device_update(tz);
492 /* sys I/F for thermal zone */
494 #define to_thermal_zone(_dev) \
495 container_of(_dev, struct thermal_zone_device, device)
497 static ssize_t
498 type_show(struct device *dev, struct device_attribute *attr, char *buf)
500 struct thermal_zone_device *tz = to_thermal_zone(dev);
502 return sprintf(buf, "%s\n", tz->type);
505 static ssize_t
506 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
508 struct thermal_zone_device *tz = to_thermal_zone(dev);
509 long temperature;
510 int ret;
512 ret = thermal_zone_get_temp(tz, &temperature);
514 if (ret)
515 return ret;
517 return sprintf(buf, "%ld\n", temperature);
520 static ssize_t
521 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
523 struct thermal_zone_device *tz = to_thermal_zone(dev);
524 enum thermal_device_mode mode;
525 int result;
527 if (!tz->ops->get_mode)
528 return -EPERM;
530 result = tz->ops->get_mode(tz, &mode);
531 if (result)
532 return result;
534 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
535 : "disabled");
538 static ssize_t
539 mode_store(struct device *dev, struct device_attribute *attr,
540 const char *buf, size_t count)
542 struct thermal_zone_device *tz = to_thermal_zone(dev);
543 int result;
545 if (!tz->ops->set_mode)
546 return -EPERM;
548 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
549 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
550 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
551 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
552 else
553 result = -EINVAL;
555 if (result)
556 return result;
558 return count;
561 static ssize_t
562 trip_point_type_show(struct device *dev, struct device_attribute *attr,
563 char *buf)
565 struct thermal_zone_device *tz = to_thermal_zone(dev);
566 enum thermal_trip_type type;
567 int trip, result;
569 if (!tz->ops->get_trip_type)
570 return -EPERM;
572 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
573 return -EINVAL;
575 result = tz->ops->get_trip_type(tz, trip, &type);
576 if (result)
577 return result;
579 switch (type) {
580 case THERMAL_TRIP_CRITICAL:
581 return sprintf(buf, "critical\n");
582 case THERMAL_TRIP_HOT:
583 return sprintf(buf, "hot\n");
584 case THERMAL_TRIP_PASSIVE:
585 return sprintf(buf, "passive\n");
586 case THERMAL_TRIP_ACTIVE:
587 return sprintf(buf, "active\n");
588 default:
589 return sprintf(buf, "unknown\n");
593 static ssize_t
594 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
595 const char *buf, size_t count)
597 struct thermal_zone_device *tz = to_thermal_zone(dev);
598 int trip, ret;
599 unsigned long temperature;
601 if (!tz->ops->set_trip_temp)
602 return -EPERM;
604 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
605 return -EINVAL;
607 if (kstrtoul(buf, 10, &temperature))
608 return -EINVAL;
610 ret = tz->ops->set_trip_temp(tz, trip, temperature);
612 return ret ? ret : count;
615 static ssize_t
616 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
617 char *buf)
619 struct thermal_zone_device *tz = to_thermal_zone(dev);
620 int trip, ret;
621 long temperature;
623 if (!tz->ops->get_trip_temp)
624 return -EPERM;
626 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
627 return -EINVAL;
629 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
631 if (ret)
632 return ret;
634 return sprintf(buf, "%ld\n", temperature);
637 static ssize_t
638 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
639 const char *buf, size_t count)
641 struct thermal_zone_device *tz = to_thermal_zone(dev);
642 int trip, ret;
643 unsigned long temperature;
645 if (!tz->ops->set_trip_hyst)
646 return -EPERM;
648 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
649 return -EINVAL;
651 if (kstrtoul(buf, 10, &temperature))
652 return -EINVAL;
655 * We are not doing any check on the 'temperature' value
656 * here. The driver implementing 'set_trip_hyst' has to
657 * take care of this.
659 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
661 return ret ? ret : count;
664 static ssize_t
665 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
666 char *buf)
668 struct thermal_zone_device *tz = to_thermal_zone(dev);
669 int trip, ret;
670 unsigned long temperature;
672 if (!tz->ops->get_trip_hyst)
673 return -EPERM;
675 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
676 return -EINVAL;
678 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
680 return ret ? ret : sprintf(buf, "%ld\n", temperature);
683 static ssize_t
684 passive_store(struct device *dev, struct device_attribute *attr,
685 const char *buf, size_t count)
687 struct thermal_zone_device *tz = to_thermal_zone(dev);
688 struct thermal_cooling_device *cdev = NULL;
689 int state;
691 if (!sscanf(buf, "%d\n", &state))
692 return -EINVAL;
694 /* sanity check: values below 1000 millicelcius don't make sense
695 * and can cause the system to go into a thermal heart attack
697 if (state && state < 1000)
698 return -EINVAL;
700 if (state && !tz->forced_passive) {
701 mutex_lock(&thermal_list_lock);
702 list_for_each_entry(cdev, &thermal_cdev_list, node) {
703 if (!strncmp("Processor", cdev->type,
704 sizeof("Processor")))
705 thermal_zone_bind_cooling_device(tz,
706 THERMAL_TRIPS_NONE, cdev,
707 THERMAL_NO_LIMIT,
708 THERMAL_NO_LIMIT);
710 mutex_unlock(&thermal_list_lock);
711 if (!tz->passive_delay)
712 tz->passive_delay = 1000;
713 } else if (!state && tz->forced_passive) {
714 mutex_lock(&thermal_list_lock);
715 list_for_each_entry(cdev, &thermal_cdev_list, node) {
716 if (!strncmp("Processor", cdev->type,
717 sizeof("Processor")))
718 thermal_zone_unbind_cooling_device(tz,
719 THERMAL_TRIPS_NONE,
720 cdev);
722 mutex_unlock(&thermal_list_lock);
723 tz->passive_delay = 0;
726 tz->forced_passive = state;
728 thermal_zone_device_update(tz);
730 return count;
733 static ssize_t
734 passive_show(struct device *dev, struct device_attribute *attr,
735 char *buf)
737 struct thermal_zone_device *tz = to_thermal_zone(dev);
739 return sprintf(buf, "%d\n", tz->forced_passive);
742 static ssize_t
743 policy_store(struct device *dev, struct device_attribute *attr,
744 const char *buf, size_t count)
746 int ret = -EINVAL;
747 struct thermal_zone_device *tz = to_thermal_zone(dev);
748 struct thermal_governor *gov;
749 char name[THERMAL_NAME_LENGTH];
751 snprintf(name, sizeof(name), "%s", buf);
753 mutex_lock(&thermal_governor_lock);
755 gov = __find_governor(strim(name));
756 if (!gov)
757 goto exit;
759 tz->governor = gov;
760 ret = count;
762 exit:
763 mutex_unlock(&thermal_governor_lock);
764 return ret;
767 static ssize_t
768 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
770 struct thermal_zone_device *tz = to_thermal_zone(dev);
772 return sprintf(buf, "%s\n", tz->governor->name);
775 #ifdef CONFIG_THERMAL_EMULATION
776 static ssize_t
777 emul_temp_store(struct device *dev, struct device_attribute *attr,
778 const char *buf, size_t count)
780 struct thermal_zone_device *tz = to_thermal_zone(dev);
781 int ret = 0;
782 unsigned long temperature;
784 if (kstrtoul(buf, 10, &temperature))
785 return -EINVAL;
787 if (!tz->ops->set_emul_temp) {
788 mutex_lock(&tz->lock);
789 tz->emul_temperature = temperature;
790 mutex_unlock(&tz->lock);
791 } else {
792 ret = tz->ops->set_emul_temp(tz, temperature);
795 if (!ret)
796 thermal_zone_device_update(tz);
798 return ret ? ret : count;
800 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
801 #endif/*CONFIG_THERMAL_EMULATION*/
803 static DEVICE_ATTR(type, 0444, type_show, NULL);
804 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
805 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
806 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
807 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
809 /* sys I/F for cooling device */
810 #define to_cooling_device(_dev) \
811 container_of(_dev, struct thermal_cooling_device, device)
813 static ssize_t
814 thermal_cooling_device_type_show(struct device *dev,
815 struct device_attribute *attr, char *buf)
817 struct thermal_cooling_device *cdev = to_cooling_device(dev);
819 return sprintf(buf, "%s\n", cdev->type);
822 static ssize_t
823 thermal_cooling_device_max_state_show(struct device *dev,
824 struct device_attribute *attr, char *buf)
826 struct thermal_cooling_device *cdev = to_cooling_device(dev);
827 unsigned long state;
828 int ret;
830 ret = cdev->ops->get_max_state(cdev, &state);
831 if (ret)
832 return ret;
833 return sprintf(buf, "%ld\n", state);
836 static ssize_t
837 thermal_cooling_device_cur_state_show(struct device *dev,
838 struct device_attribute *attr, char *buf)
840 struct thermal_cooling_device *cdev = to_cooling_device(dev);
841 unsigned long state;
842 int ret;
844 ret = cdev->ops->get_cur_state(cdev, &state);
845 if (ret)
846 return ret;
847 return sprintf(buf, "%ld\n", state);
850 static ssize_t
851 thermal_cooling_device_cur_state_store(struct device *dev,
852 struct device_attribute *attr,
853 const char *buf, size_t count)
855 struct thermal_cooling_device *cdev = to_cooling_device(dev);
856 unsigned long state;
857 int result;
859 if (!sscanf(buf, "%ld\n", &state))
860 return -EINVAL;
862 if ((long)state < 0)
863 return -EINVAL;
865 result = cdev->ops->set_cur_state(cdev, state);
866 if (result)
867 return result;
868 return count;
871 static struct device_attribute dev_attr_cdev_type =
872 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
873 static DEVICE_ATTR(max_state, 0444,
874 thermal_cooling_device_max_state_show, NULL);
875 static DEVICE_ATTR(cur_state, 0644,
876 thermal_cooling_device_cur_state_show,
877 thermal_cooling_device_cur_state_store);
879 static ssize_t
880 thermal_cooling_device_trip_point_show(struct device *dev,
881 struct device_attribute *attr, char *buf)
883 struct thermal_instance *instance;
885 instance =
886 container_of(attr, struct thermal_instance, attr);
888 if (instance->trip == THERMAL_TRIPS_NONE)
889 return sprintf(buf, "-1\n");
890 else
891 return sprintf(buf, "%d\n", instance->trip);
894 /* Device management */
897 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
898 * @tz: pointer to struct thermal_zone_device
899 * @trip: indicates which trip point the cooling devices is
900 * associated with in this thermal zone.
901 * @cdev: pointer to struct thermal_cooling_device
902 * @upper: the Maximum cooling state for this trip point.
903 * THERMAL_NO_LIMIT means no upper limit,
904 * and the cooling device can be in max_state.
905 * @lower: the Minimum cooling state can be used for this trip point.
906 * THERMAL_NO_LIMIT means no lower limit,
907 * and the cooling device can be in cooling state 0.
909 * This interface function bind a thermal cooling device to the certain trip
910 * point of a thermal zone device.
911 * This function is usually called in the thermal zone device .bind callback.
913 * Return: 0 on success, the proper error value otherwise.
915 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
916 int trip,
917 struct thermal_cooling_device *cdev,
918 unsigned long upper, unsigned long lower)
920 struct thermal_instance *dev;
921 struct thermal_instance *pos;
922 struct thermal_zone_device *pos1;
923 struct thermal_cooling_device *pos2;
924 unsigned long max_state;
925 int result;
927 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
928 return -EINVAL;
930 list_for_each_entry(pos1, &thermal_tz_list, node) {
931 if (pos1 == tz)
932 break;
934 list_for_each_entry(pos2, &thermal_cdev_list, node) {
935 if (pos2 == cdev)
936 break;
939 if (tz != pos1 || cdev != pos2)
940 return -EINVAL;
942 cdev->ops->get_max_state(cdev, &max_state);
944 /* lower default 0, upper default max_state */
945 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
946 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
948 if (lower > upper || upper > max_state)
949 return -EINVAL;
951 dev =
952 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
953 if (!dev)
954 return -ENOMEM;
955 dev->tz = tz;
956 dev->cdev = cdev;
957 dev->trip = trip;
958 dev->upper = upper;
959 dev->lower = lower;
960 dev->target = THERMAL_NO_TARGET;
962 result = get_idr(&tz->idr, &tz->lock, &dev->id);
963 if (result)
964 goto free_mem;
966 sprintf(dev->name, "cdev%d", dev->id);
967 result =
968 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
969 if (result)
970 goto release_idr;
972 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
973 sysfs_attr_init(&dev->attr.attr);
974 dev->attr.attr.name = dev->attr_name;
975 dev->attr.attr.mode = 0444;
976 dev->attr.show = thermal_cooling_device_trip_point_show;
977 result = device_create_file(&tz->device, &dev->attr);
978 if (result)
979 goto remove_symbol_link;
981 mutex_lock(&tz->lock);
982 mutex_lock(&cdev->lock);
983 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
984 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
985 result = -EEXIST;
986 break;
988 if (!result) {
989 list_add_tail(&dev->tz_node, &tz->thermal_instances);
990 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
992 mutex_unlock(&cdev->lock);
993 mutex_unlock(&tz->lock);
995 if (!result)
996 return 0;
998 device_remove_file(&tz->device, &dev->attr);
999 remove_symbol_link:
1000 sysfs_remove_link(&tz->device.kobj, dev->name);
1001 release_idr:
1002 release_idr(&tz->idr, &tz->lock, dev->id);
1003 free_mem:
1004 kfree(dev);
1005 return result;
1007 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
1010 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1011 * thermal zone.
1012 * @tz: pointer to a struct thermal_zone_device.
1013 * @trip: indicates which trip point the cooling devices is
1014 * associated with in this thermal zone.
1015 * @cdev: pointer to a struct thermal_cooling_device.
1017 * This interface function unbind a thermal cooling device from the certain
1018 * trip point of a thermal zone device.
1019 * This function is usually called in the thermal zone device .unbind callback.
1021 * Return: 0 on success, the proper error value otherwise.
1023 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1024 int trip,
1025 struct thermal_cooling_device *cdev)
1027 struct thermal_instance *pos, *next;
1029 mutex_lock(&tz->lock);
1030 mutex_lock(&cdev->lock);
1031 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1032 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1033 list_del(&pos->tz_node);
1034 list_del(&pos->cdev_node);
1035 mutex_unlock(&cdev->lock);
1036 mutex_unlock(&tz->lock);
1037 goto unbind;
1040 mutex_unlock(&cdev->lock);
1041 mutex_unlock(&tz->lock);
1043 return -ENODEV;
1045 unbind:
1046 device_remove_file(&tz->device, &pos->attr);
1047 sysfs_remove_link(&tz->device.kobj, pos->name);
1048 release_idr(&tz->idr, &tz->lock, pos->id);
1049 kfree(pos);
1050 return 0;
1052 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1054 static void thermal_release(struct device *dev)
1056 struct thermal_zone_device *tz;
1057 struct thermal_cooling_device *cdev;
1059 if (!strncmp(dev_name(dev), "thermal_zone",
1060 sizeof("thermal_zone") - 1)) {
1061 tz = to_thermal_zone(dev);
1062 kfree(tz);
1063 } else if(!strncmp(dev_name(dev), "cooling_device",
1064 sizeof("cooling_device") - 1)){
1065 cdev = to_cooling_device(dev);
1066 kfree(cdev);
1070 static struct class thermal_class = {
1071 .name = "thermal",
1072 .dev_release = thermal_release,
1076 * __thermal_cooling_device_register() - register a new thermal cooling device
1077 * @np: a pointer to a device tree node.
1078 * @type: the thermal cooling device type.
1079 * @devdata: device private data.
1080 * @ops: standard thermal cooling devices callbacks.
1082 * This interface function adds a new thermal cooling device (fan/processor/...)
1083 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1084 * to all the thermal zone devices registered at the same time.
1085 * It also gives the opportunity to link the cooling device to a device tree
1086 * node, so that it can be bound to a thermal zone created out of device tree.
1088 * Return: a pointer to the created struct thermal_cooling_device or an
1089 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1091 static struct thermal_cooling_device *
1092 __thermal_cooling_device_register(struct device_node *np,
1093 char *type, void *devdata,
1094 const struct thermal_cooling_device_ops *ops)
1096 struct thermal_cooling_device *cdev;
1097 int result;
1099 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1100 return ERR_PTR(-EINVAL);
1102 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1103 !ops->set_cur_state)
1104 return ERR_PTR(-EINVAL);
1106 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1107 if (!cdev)
1108 return ERR_PTR(-ENOMEM);
1110 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1111 if (result) {
1112 kfree(cdev);
1113 return ERR_PTR(result);
1116 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1117 mutex_init(&cdev->lock);
1118 INIT_LIST_HEAD(&cdev->thermal_instances);
1119 cdev->np = np;
1120 cdev->ops = ops;
1121 cdev->updated = false;
1122 cdev->device.class = &thermal_class;
1123 cdev->devdata = devdata;
1124 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1125 result = device_register(&cdev->device);
1126 if (result) {
1127 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1128 kfree(cdev);
1129 return ERR_PTR(result);
1132 /* sys I/F */
1133 if (type) {
1134 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1135 if (result)
1136 goto unregister;
1139 result = device_create_file(&cdev->device, &dev_attr_max_state);
1140 if (result)
1141 goto unregister;
1143 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1144 if (result)
1145 goto unregister;
1147 /* Add 'this' new cdev to the global cdev list */
1148 mutex_lock(&thermal_list_lock);
1149 list_add(&cdev->node, &thermal_cdev_list);
1150 mutex_unlock(&thermal_list_lock);
1152 /* Update binding information for 'this' new cdev */
1153 bind_cdev(cdev);
1155 return cdev;
1157 unregister:
1158 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1159 device_unregister(&cdev->device);
1160 return ERR_PTR(result);
1164 * thermal_cooling_device_register() - register a new thermal cooling device
1165 * @type: the thermal cooling device type.
1166 * @devdata: device private data.
1167 * @ops: standard thermal cooling devices callbacks.
1169 * This interface function adds a new thermal cooling device (fan/processor/...)
1170 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1171 * to all the thermal zone devices registered at the same time.
1173 * Return: a pointer to the created struct thermal_cooling_device or an
1174 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1176 struct thermal_cooling_device *
1177 thermal_cooling_device_register(char *type, void *devdata,
1178 const struct thermal_cooling_device_ops *ops)
1180 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1182 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1185 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1186 * @np: a pointer to a device tree node.
1187 * @type: the thermal cooling device type.
1188 * @devdata: device private data.
1189 * @ops: standard thermal cooling devices callbacks.
1191 * This function will register a cooling device with device tree node reference.
1192 * This interface function adds a new thermal cooling device (fan/processor/...)
1193 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1194 * to all the thermal zone devices registered at the same time.
1196 * Return: a pointer to the created struct thermal_cooling_device or an
1197 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1199 struct thermal_cooling_device *
1200 thermal_of_cooling_device_register(struct device_node *np,
1201 char *type, void *devdata,
1202 const struct thermal_cooling_device_ops *ops)
1204 return __thermal_cooling_device_register(np, type, devdata, ops);
1206 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1209 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1210 * @cdev: the thermal cooling device to remove.
1212 * thermal_cooling_device_unregister() must be called when the device is no
1213 * longer needed.
1215 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1217 int i;
1218 const struct thermal_zone_params *tzp;
1219 struct thermal_zone_device *tz;
1220 struct thermal_cooling_device *pos = NULL;
1222 if (!cdev)
1223 return;
1225 mutex_lock(&thermal_list_lock);
1226 list_for_each_entry(pos, &thermal_cdev_list, node)
1227 if (pos == cdev)
1228 break;
1229 if (pos != cdev) {
1230 /* thermal cooling device not found */
1231 mutex_unlock(&thermal_list_lock);
1232 return;
1234 list_del(&cdev->node);
1236 /* Unbind all thermal zones associated with 'this' cdev */
1237 list_for_each_entry(tz, &thermal_tz_list, node) {
1238 if (tz->ops->unbind) {
1239 tz->ops->unbind(tz, cdev);
1240 continue;
1243 if (!tz->tzp || !tz->tzp->tbp)
1244 continue;
1246 tzp = tz->tzp;
1247 for (i = 0; i < tzp->num_tbps; i++) {
1248 if (tzp->tbp[i].cdev == cdev) {
1249 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1250 tzp->tbp[i].cdev = NULL;
1255 mutex_unlock(&thermal_list_lock);
1257 if (cdev->type[0])
1258 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1259 device_remove_file(&cdev->device, &dev_attr_max_state);
1260 device_remove_file(&cdev->device, &dev_attr_cur_state);
1262 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1263 device_unregister(&cdev->device);
1264 return;
1266 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1268 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1270 struct thermal_instance *instance;
1271 unsigned long target = 0;
1273 /* cooling device is updated*/
1274 if (cdev->updated)
1275 return;
1277 mutex_lock(&cdev->lock);
1278 /* Make sure cdev enters the deepest cooling state */
1279 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1280 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1281 instance->tz->id, instance->target);
1282 if (instance->target == THERMAL_NO_TARGET)
1283 continue;
1284 if (instance->target > target)
1285 target = instance->target;
1287 mutex_unlock(&cdev->lock);
1288 cdev->ops->set_cur_state(cdev, target);
1289 cdev->updated = true;
1290 dev_dbg(&cdev->device, "set to state %lu\n", target);
1292 EXPORT_SYMBOL(thermal_cdev_update);
1295 * thermal_notify_framework - Sensor drivers use this API to notify framework
1296 * @tz: thermal zone device
1297 * @trip: indicates which trip point has been crossed
1299 * This function handles the trip events from sensor drivers. It starts
1300 * throttling the cooling devices according to the policy configured.
1301 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1302 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1303 * The throttling policy is based on the configured platform data; if no
1304 * platform data is provided, this uses the step_wise throttling policy.
1306 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1308 handle_thermal_trip(tz, trip);
1310 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1313 * create_trip_attrs() - create attributes for trip points
1314 * @tz: the thermal zone device
1315 * @mask: Writeable trip point bitmap.
1317 * helper function to instantiate sysfs entries for every trip
1318 * point and its properties of a struct thermal_zone_device.
1320 * Return: 0 on success, the proper error value otherwise.
1322 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1324 int indx;
1325 int size = sizeof(struct thermal_attr) * tz->trips;
1327 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1328 if (!tz->trip_type_attrs)
1329 return -ENOMEM;
1331 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1332 if (!tz->trip_temp_attrs) {
1333 kfree(tz->trip_type_attrs);
1334 return -ENOMEM;
1337 if (tz->ops->get_trip_hyst) {
1338 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1339 if (!tz->trip_hyst_attrs) {
1340 kfree(tz->trip_type_attrs);
1341 kfree(tz->trip_temp_attrs);
1342 return -ENOMEM;
1347 for (indx = 0; indx < tz->trips; indx++) {
1348 /* create trip type attribute */
1349 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1350 "trip_point_%d_type", indx);
1352 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1353 tz->trip_type_attrs[indx].attr.attr.name =
1354 tz->trip_type_attrs[indx].name;
1355 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1356 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1358 device_create_file(&tz->device,
1359 &tz->trip_type_attrs[indx].attr);
1361 /* create trip temp attribute */
1362 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1363 "trip_point_%d_temp", indx);
1365 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1366 tz->trip_temp_attrs[indx].attr.attr.name =
1367 tz->trip_temp_attrs[indx].name;
1368 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1369 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1370 if (mask & (1 << indx)) {
1371 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1372 tz->trip_temp_attrs[indx].attr.store =
1373 trip_point_temp_store;
1376 device_create_file(&tz->device,
1377 &tz->trip_temp_attrs[indx].attr);
1379 /* create Optional trip hyst attribute */
1380 if (!tz->ops->get_trip_hyst)
1381 continue;
1382 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1383 "trip_point_%d_hyst", indx);
1385 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1386 tz->trip_hyst_attrs[indx].attr.attr.name =
1387 tz->trip_hyst_attrs[indx].name;
1388 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1389 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1390 if (tz->ops->set_trip_hyst) {
1391 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1392 tz->trip_hyst_attrs[indx].attr.store =
1393 trip_point_hyst_store;
1396 device_create_file(&tz->device,
1397 &tz->trip_hyst_attrs[indx].attr);
1399 return 0;
1402 static void remove_trip_attrs(struct thermal_zone_device *tz)
1404 int indx;
1406 for (indx = 0; indx < tz->trips; indx++) {
1407 device_remove_file(&tz->device,
1408 &tz->trip_type_attrs[indx].attr);
1409 device_remove_file(&tz->device,
1410 &tz->trip_temp_attrs[indx].attr);
1411 if (tz->ops->get_trip_hyst)
1412 device_remove_file(&tz->device,
1413 &tz->trip_hyst_attrs[indx].attr);
1415 kfree(tz->trip_type_attrs);
1416 kfree(tz->trip_temp_attrs);
1417 kfree(tz->trip_hyst_attrs);
1421 * thermal_zone_device_register() - register a new thermal zone device
1422 * @type: the thermal zone device type
1423 * @trips: the number of trip points the thermal zone support
1424 * @mask: a bit string indicating the writeablility of trip points
1425 * @devdata: private device data
1426 * @ops: standard thermal zone device callbacks
1427 * @tzp: thermal zone platform parameters
1428 * @passive_delay: number of milliseconds to wait between polls when
1429 * performing passive cooling
1430 * @polling_delay: number of milliseconds to wait between polls when checking
1431 * whether trip points have been crossed (0 for interrupt
1432 * driven systems)
1434 * This interface function adds a new thermal zone device (sensor) to
1435 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1436 * thermal cooling devices registered at the same time.
1437 * thermal_zone_device_unregister() must be called when the device is no
1438 * longer needed. The passive cooling depends on the .get_trend() return value.
1440 * Return: a pointer to the created struct thermal_zone_device or an
1441 * in case of error, an ERR_PTR. Caller must check return value with
1442 * IS_ERR*() helpers.
1444 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1445 int trips, int mask, void *devdata,
1446 struct thermal_zone_device_ops *ops,
1447 const struct thermal_zone_params *tzp,
1448 int passive_delay, int polling_delay)
1450 struct thermal_zone_device *tz;
1451 enum thermal_trip_type trip_type;
1452 int result;
1453 int count;
1454 int passive = 0;
1456 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1457 return ERR_PTR(-EINVAL);
1459 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1460 return ERR_PTR(-EINVAL);
1462 if (!ops)
1463 return ERR_PTR(-EINVAL);
1465 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1466 return ERR_PTR(-EINVAL);
1468 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1469 if (!tz)
1470 return ERR_PTR(-ENOMEM);
1472 INIT_LIST_HEAD(&tz->thermal_instances);
1473 idr_init(&tz->idr);
1474 mutex_init(&tz->lock);
1475 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1476 if (result) {
1477 kfree(tz);
1478 return ERR_PTR(result);
1481 strlcpy(tz->type, type ? : "", sizeof(tz->type));
1482 tz->ops = ops;
1483 tz->tzp = tzp;
1484 tz->device.class = &thermal_class;
1485 tz->devdata = devdata;
1486 tz->trips = trips;
1487 tz->passive_delay = passive_delay;
1488 tz->polling_delay = polling_delay;
1490 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1491 result = device_register(&tz->device);
1492 if (result) {
1493 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1494 kfree(tz);
1495 return ERR_PTR(result);
1498 /* sys I/F */
1499 if (type) {
1500 result = device_create_file(&tz->device, &dev_attr_type);
1501 if (result)
1502 goto unregister;
1505 result = device_create_file(&tz->device, &dev_attr_temp);
1506 if (result)
1507 goto unregister;
1509 if (ops->get_mode) {
1510 result = device_create_file(&tz->device, &dev_attr_mode);
1511 if (result)
1512 goto unregister;
1515 result = create_trip_attrs(tz, mask);
1516 if (result)
1517 goto unregister;
1519 for (count = 0; count < trips; count++) {
1520 tz->ops->get_trip_type(tz, count, &trip_type);
1521 if (trip_type == THERMAL_TRIP_PASSIVE)
1522 passive = 1;
1525 if (!passive) {
1526 result = device_create_file(&tz->device, &dev_attr_passive);
1527 if (result)
1528 goto unregister;
1531 #ifdef CONFIG_THERMAL_EMULATION
1532 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1533 if (result)
1534 goto unregister;
1535 #endif
1536 /* Create policy attribute */
1537 result = device_create_file(&tz->device, &dev_attr_policy);
1538 if (result)
1539 goto unregister;
1541 /* Update 'this' zone's governor information */
1542 mutex_lock(&thermal_governor_lock);
1544 if (tz->tzp)
1545 tz->governor = __find_governor(tz->tzp->governor_name);
1546 else
1547 tz->governor = def_governor;
1549 mutex_unlock(&thermal_governor_lock);
1551 if (!tz->tzp || !tz->tzp->no_hwmon) {
1552 result = thermal_add_hwmon_sysfs(tz);
1553 if (result)
1554 goto unregister;
1557 mutex_lock(&thermal_list_lock);
1558 list_add_tail(&tz->node, &thermal_tz_list);
1559 mutex_unlock(&thermal_list_lock);
1561 /* Bind cooling devices for this zone */
1562 bind_tz(tz);
1564 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1566 if (!tz->ops->get_temp)
1567 thermal_zone_device_set_polling(tz, 0);
1569 thermal_zone_device_update(tz);
1571 if (!result)
1572 return tz;
1574 unregister:
1575 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1576 device_unregister(&tz->device);
1577 return ERR_PTR(result);
1579 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1582 * thermal_device_unregister - removes the registered thermal zone device
1583 * @tz: the thermal zone device to remove
1585 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1587 int i;
1588 const struct thermal_zone_params *tzp;
1589 struct thermal_cooling_device *cdev;
1590 struct thermal_zone_device *pos = NULL;
1592 if (!tz)
1593 return;
1595 tzp = tz->tzp;
1597 mutex_lock(&thermal_list_lock);
1598 list_for_each_entry(pos, &thermal_tz_list, node)
1599 if (pos == tz)
1600 break;
1601 if (pos != tz) {
1602 /* thermal zone device not found */
1603 mutex_unlock(&thermal_list_lock);
1604 return;
1606 list_del(&tz->node);
1608 /* Unbind all cdevs associated with 'this' thermal zone */
1609 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1610 if (tz->ops->unbind) {
1611 tz->ops->unbind(tz, cdev);
1612 continue;
1615 if (!tzp || !tzp->tbp)
1616 break;
1618 for (i = 0; i < tzp->num_tbps; i++) {
1619 if (tzp->tbp[i].cdev == cdev) {
1620 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1621 tzp->tbp[i].cdev = NULL;
1626 mutex_unlock(&thermal_list_lock);
1628 thermal_zone_device_set_polling(tz, 0);
1630 if (tz->type[0])
1631 device_remove_file(&tz->device, &dev_attr_type);
1632 device_remove_file(&tz->device, &dev_attr_temp);
1633 if (tz->ops->get_mode)
1634 device_remove_file(&tz->device, &dev_attr_mode);
1635 device_remove_file(&tz->device, &dev_attr_policy);
1636 remove_trip_attrs(tz);
1637 tz->governor = NULL;
1639 thermal_remove_hwmon_sysfs(tz);
1640 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1641 idr_destroy(&tz->idr);
1642 mutex_destroy(&tz->lock);
1643 device_unregister(&tz->device);
1644 return;
1646 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1649 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1650 * @name: thermal zone name to fetch the temperature
1652 * When only one zone is found with the passed name, returns a reference to it.
1654 * Return: On success returns a reference to an unique thermal zone with
1655 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1656 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1658 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1660 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1661 unsigned int found = 0;
1663 if (!name)
1664 goto exit;
1666 mutex_lock(&thermal_list_lock);
1667 list_for_each_entry(pos, &thermal_tz_list, node)
1668 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1669 found++;
1670 ref = pos;
1672 mutex_unlock(&thermal_list_lock);
1674 /* nothing has been found, thus an error code for it */
1675 if (found == 0)
1676 ref = ERR_PTR(-ENODEV);
1677 else if (found > 1)
1678 /* Success only when an unique zone is found */
1679 ref = ERR_PTR(-EEXIST);
1681 exit:
1682 return ref;
1684 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1686 #ifdef CONFIG_NET
1687 static const struct genl_multicast_group thermal_event_mcgrps[] = {
1688 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1691 static struct genl_family thermal_event_genl_family = {
1692 .id = GENL_ID_GENERATE,
1693 .name = THERMAL_GENL_FAMILY_NAME,
1694 .version = THERMAL_GENL_VERSION,
1695 .maxattr = THERMAL_GENL_ATTR_MAX,
1696 .mcgrps = thermal_event_mcgrps,
1697 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
1700 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1701 enum events event)
1703 struct sk_buff *skb;
1704 struct nlattr *attr;
1705 struct thermal_genl_event *thermal_event;
1706 void *msg_header;
1707 int size;
1708 int result;
1709 static unsigned int thermal_event_seqnum;
1711 if (!tz)
1712 return -EINVAL;
1714 /* allocate memory */
1715 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1716 nla_total_size(0);
1718 skb = genlmsg_new(size, GFP_ATOMIC);
1719 if (!skb)
1720 return -ENOMEM;
1722 /* add the genetlink message header */
1723 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1724 &thermal_event_genl_family, 0,
1725 THERMAL_GENL_CMD_EVENT);
1726 if (!msg_header) {
1727 nlmsg_free(skb);
1728 return -ENOMEM;
1731 /* fill the data */
1732 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1733 sizeof(struct thermal_genl_event));
1735 if (!attr) {
1736 nlmsg_free(skb);
1737 return -EINVAL;
1740 thermal_event = nla_data(attr);
1741 if (!thermal_event) {
1742 nlmsg_free(skb);
1743 return -EINVAL;
1746 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1748 thermal_event->orig = tz->id;
1749 thermal_event->event = event;
1751 /* send multicast genetlink message */
1752 result = genlmsg_end(skb, msg_header);
1753 if (result < 0) {
1754 nlmsg_free(skb);
1755 return result;
1758 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
1759 0, GFP_ATOMIC);
1760 if (result)
1761 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1763 return result;
1765 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1767 static int genetlink_init(void)
1769 return genl_register_family(&thermal_event_genl_family);
1772 static void genetlink_exit(void)
1774 genl_unregister_family(&thermal_event_genl_family);
1776 #else /* !CONFIG_NET */
1777 static inline int genetlink_init(void) { return 0; }
1778 static inline void genetlink_exit(void) {}
1779 #endif /* !CONFIG_NET */
1781 static int __init thermal_register_governors(void)
1783 int result;
1785 result = thermal_gov_step_wise_register();
1786 if (result)
1787 return result;
1789 result = thermal_gov_fair_share_register();
1790 if (result)
1791 return result;
1793 return thermal_gov_user_space_register();
1796 static void thermal_unregister_governors(void)
1798 thermal_gov_step_wise_unregister();
1799 thermal_gov_fair_share_unregister();
1800 thermal_gov_user_space_unregister();
1803 static int __init thermal_init(void)
1805 int result;
1807 result = thermal_register_governors();
1808 if (result)
1809 goto error;
1811 result = class_register(&thermal_class);
1812 if (result)
1813 goto unregister_governors;
1815 result = genetlink_init();
1816 if (result)
1817 goto unregister_class;
1819 result = of_parse_thermal_zones();
1820 if (result)
1821 goto exit_netlink;
1823 return 0;
1825 exit_netlink:
1826 genetlink_exit();
1827 unregister_governors:
1828 thermal_unregister_governors();
1829 unregister_class:
1830 class_unregister(&thermal_class);
1831 error:
1832 idr_destroy(&thermal_tz_idr);
1833 idr_destroy(&thermal_cdev_idr);
1834 mutex_destroy(&thermal_idr_lock);
1835 mutex_destroy(&thermal_list_lock);
1836 mutex_destroy(&thermal_governor_lock);
1837 return result;
1840 static void __exit thermal_exit(void)
1842 of_thermal_destroy_zones();
1843 genetlink_exit();
1844 class_unregister(&thermal_class);
1845 thermal_unregister_governors();
1846 idr_destroy(&thermal_tz_idr);
1847 idr_destroy(&thermal_cdev_idr);
1848 mutex_destroy(&thermal_idr_lock);
1849 mutex_destroy(&thermal_list_lock);
1850 mutex_destroy(&thermal_governor_lock);
1853 fs_initcall(thermal_init);
1854 module_exit(thermal_exit);