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>
38 #include <net/netlink.h>
39 #include <net/genetlink.h>
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/thermal.h>
44 #include "thermal_core.h"
45 #include "thermal_hwmon.h"
47 MODULE_AUTHOR("Zhang Rui");
48 MODULE_DESCRIPTION("Generic thermal management sysfs support");
49 MODULE_LICENSE("GPL v2");
51 static DEFINE_IDR(thermal_tz_idr
);
52 static DEFINE_IDR(thermal_cdev_idr
);
53 static DEFINE_MUTEX(thermal_idr_lock
);
55 static LIST_HEAD(thermal_tz_list
);
56 static LIST_HEAD(thermal_cdev_list
);
57 static LIST_HEAD(thermal_governor_list
);
59 static DEFINE_MUTEX(thermal_list_lock
);
60 static DEFINE_MUTEX(thermal_governor_lock
);
62 static struct thermal_governor
*def_governor
;
64 static struct thermal_governor
*__find_governor(const char *name
)
66 struct thermal_governor
*pos
;
68 if (!name
|| !name
[0])
71 list_for_each_entry(pos
, &thermal_governor_list
, governor_list
)
72 if (!strncasecmp(name
, pos
->name
, THERMAL_NAME_LENGTH
))
79 * bind_previous_governor() - bind the previous governor of the thermal zone
80 * @tz: a valid pointer to a struct thermal_zone_device
81 * @failed_gov_name: the name of the governor that failed to register
83 * Register the previous governor of the thermal zone after a new
84 * governor has failed to be bound.
86 static void bind_previous_governor(struct thermal_zone_device
*tz
,
87 const char *failed_gov_name
)
89 if (tz
->governor
&& tz
->governor
->bind_to_tz
) {
90 if (tz
->governor
->bind_to_tz(tz
)) {
92 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
93 failed_gov_name
, tz
->governor
->name
, tz
->type
);
100 * thermal_set_governor() - Switch to another governor
101 * @tz: a valid pointer to a struct thermal_zone_device
102 * @new_gov: pointer to the new governor
104 * Change the governor of thermal zone @tz.
106 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
108 static int thermal_set_governor(struct thermal_zone_device
*tz
,
109 struct thermal_governor
*new_gov
)
113 if (tz
->governor
&& tz
->governor
->unbind_from_tz
)
114 tz
->governor
->unbind_from_tz(tz
);
116 if (new_gov
&& new_gov
->bind_to_tz
) {
117 ret
= new_gov
->bind_to_tz(tz
);
119 bind_previous_governor(tz
, new_gov
->name
);
125 tz
->governor
= new_gov
;
130 int thermal_register_governor(struct thermal_governor
*governor
)
134 struct thermal_zone_device
*pos
;
139 mutex_lock(&thermal_governor_lock
);
142 if (__find_governor(governor
->name
) == NULL
) {
144 list_add(&governor
->governor_list
, &thermal_governor_list
);
145 if (!def_governor
&& !strncmp(governor
->name
,
146 DEFAULT_THERMAL_GOVERNOR
, THERMAL_NAME_LENGTH
))
147 def_governor
= governor
;
150 mutex_lock(&thermal_list_lock
);
152 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
154 * only thermal zones with specified tz->tzp->governor_name
155 * may run with tz->govenor unset
160 name
= pos
->tzp
->governor_name
;
162 if (!strncasecmp(name
, governor
->name
, THERMAL_NAME_LENGTH
)) {
165 ret
= thermal_set_governor(pos
, governor
);
167 dev_err(&pos
->device
,
168 "Failed to set governor %s for thermal zone %s: %d\n",
169 governor
->name
, pos
->type
, ret
);
173 mutex_unlock(&thermal_list_lock
);
174 mutex_unlock(&thermal_governor_lock
);
179 void thermal_unregister_governor(struct thermal_governor
*governor
)
181 struct thermal_zone_device
*pos
;
186 mutex_lock(&thermal_governor_lock
);
188 if (__find_governor(governor
->name
) == NULL
)
191 mutex_lock(&thermal_list_lock
);
193 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
194 if (!strncasecmp(pos
->governor
->name
, governor
->name
,
195 THERMAL_NAME_LENGTH
))
196 thermal_set_governor(pos
, NULL
);
199 mutex_unlock(&thermal_list_lock
);
200 list_del(&governor
->governor_list
);
202 mutex_unlock(&thermal_governor_lock
);
206 static int get_idr(struct idr
*idr
, struct mutex
*lock
, int *id
)
212 ret
= idr_alloc(idr
, NULL
, 0, 0, GFP_KERNEL
);
215 if (unlikely(ret
< 0))
221 static void release_idr(struct idr
*idr
, struct mutex
*lock
, int id
)
230 int get_tz_trend(struct thermal_zone_device
*tz
, int trip
)
232 enum thermal_trend trend
;
234 if (tz
->emul_temperature
|| !tz
->ops
->get_trend
||
235 tz
->ops
->get_trend(tz
, trip
, &trend
)) {
236 if (tz
->temperature
> tz
->last_temperature
)
237 trend
= THERMAL_TREND_RAISING
;
238 else if (tz
->temperature
< tz
->last_temperature
)
239 trend
= THERMAL_TREND_DROPPING
;
241 trend
= THERMAL_TREND_STABLE
;
246 EXPORT_SYMBOL(get_tz_trend
);
248 struct thermal_instance
*get_thermal_instance(struct thermal_zone_device
*tz
,
249 struct thermal_cooling_device
*cdev
, int trip
)
251 struct thermal_instance
*pos
= NULL
;
252 struct thermal_instance
*target_instance
= NULL
;
254 mutex_lock(&tz
->lock
);
255 mutex_lock(&cdev
->lock
);
257 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
) {
258 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
259 target_instance
= pos
;
264 mutex_unlock(&cdev
->lock
);
265 mutex_unlock(&tz
->lock
);
267 return target_instance
;
269 EXPORT_SYMBOL(get_thermal_instance
);
271 static void print_bind_err_msg(struct thermal_zone_device
*tz
,
272 struct thermal_cooling_device
*cdev
, int ret
)
274 dev_err(&tz
->device
, "binding zone %s with cdev %s failed:%d\n",
275 tz
->type
, cdev
->type
, ret
);
278 static void __bind(struct thermal_zone_device
*tz
, int mask
,
279 struct thermal_cooling_device
*cdev
,
280 unsigned long *limits
,
285 for (i
= 0; i
< tz
->trips
; i
++) {
286 if (mask
& (1 << i
)) {
287 unsigned long upper
, lower
;
289 upper
= THERMAL_NO_LIMIT
;
290 lower
= THERMAL_NO_LIMIT
;
292 lower
= limits
[i
* 2];
293 upper
= limits
[i
* 2 + 1];
295 ret
= thermal_zone_bind_cooling_device(tz
, i
, cdev
,
299 print_bind_err_msg(tz
, cdev
, ret
);
304 static void __unbind(struct thermal_zone_device
*tz
, int mask
,
305 struct thermal_cooling_device
*cdev
)
309 for (i
= 0; i
< tz
->trips
; i
++)
311 thermal_zone_unbind_cooling_device(tz
, i
, cdev
);
314 static void bind_cdev(struct thermal_cooling_device
*cdev
)
317 const struct thermal_zone_params
*tzp
;
318 struct thermal_zone_device
*pos
= NULL
;
320 mutex_lock(&thermal_list_lock
);
322 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
323 if (!pos
->tzp
&& !pos
->ops
->bind
)
326 if (pos
->ops
->bind
) {
327 ret
= pos
->ops
->bind(pos
, cdev
);
329 print_bind_err_msg(pos
, cdev
, ret
);
334 if (!tzp
|| !tzp
->tbp
)
337 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
338 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
340 if (tzp
->tbp
[i
].match(pos
, cdev
))
342 tzp
->tbp
[i
].cdev
= cdev
;
343 __bind(pos
, tzp
->tbp
[i
].trip_mask
, cdev
,
344 tzp
->tbp
[i
].binding_limits
,
349 mutex_unlock(&thermal_list_lock
);
352 static void bind_tz(struct thermal_zone_device
*tz
)
355 struct thermal_cooling_device
*pos
= NULL
;
356 const struct thermal_zone_params
*tzp
= tz
->tzp
;
358 if (!tzp
&& !tz
->ops
->bind
)
361 mutex_lock(&thermal_list_lock
);
363 /* If there is ops->bind, try to use ops->bind */
365 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
366 ret
= tz
->ops
->bind(tz
, pos
);
368 print_bind_err_msg(tz
, pos
, ret
);
373 if (!tzp
|| !tzp
->tbp
)
376 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
377 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
378 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
380 if (tzp
->tbp
[i
].match(tz
, pos
))
382 tzp
->tbp
[i
].cdev
= pos
;
383 __bind(tz
, tzp
->tbp
[i
].trip_mask
, pos
,
384 tzp
->tbp
[i
].binding_limits
,
389 mutex_unlock(&thermal_list_lock
);
392 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
396 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
397 round_jiffies(msecs_to_jiffies(delay
)));
399 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
400 msecs_to_jiffies(delay
));
402 cancel_delayed_work(&tz
->poll_queue
);
405 static void monitor_thermal_zone(struct thermal_zone_device
*tz
)
407 mutex_lock(&tz
->lock
);
410 thermal_zone_device_set_polling(tz
, tz
->passive_delay
);
411 else if (tz
->polling_delay
)
412 thermal_zone_device_set_polling(tz
, tz
->polling_delay
);
414 thermal_zone_device_set_polling(tz
, 0);
416 mutex_unlock(&tz
->lock
);
419 static void handle_non_critical_trips(struct thermal_zone_device
*tz
,
420 int trip
, enum thermal_trip_type trip_type
)
422 tz
->governor
? tz
->governor
->throttle(tz
, trip
) :
423 def_governor
->throttle(tz
, trip
);
426 static void handle_critical_trips(struct thermal_zone_device
*tz
,
427 int trip
, enum thermal_trip_type trip_type
)
431 tz
->ops
->get_trip_temp(tz
, trip
, &trip_temp
);
433 /* If we have not crossed the trip_temp, we do not care. */
434 if (trip_temp
<= 0 || tz
->temperature
< trip_temp
)
437 trace_thermal_zone_trip(tz
, trip
, trip_type
);
440 tz
->ops
->notify(tz
, trip
, trip_type
);
442 if (trip_type
== THERMAL_TRIP_CRITICAL
) {
443 dev_emerg(&tz
->device
,
444 "critical temperature reached(%d C),shutting down\n",
445 tz
->temperature
/ 1000);
446 orderly_poweroff(true);
450 static void handle_thermal_trip(struct thermal_zone_device
*tz
, int trip
)
452 enum thermal_trip_type type
;
454 tz
->ops
->get_trip_type(tz
, trip
, &type
);
456 if (type
== THERMAL_TRIP_CRITICAL
|| type
== THERMAL_TRIP_HOT
)
457 handle_critical_trips(tz
, trip
, type
);
459 handle_non_critical_trips(tz
, trip
, type
);
461 * Alright, we handled this trip successfully.
462 * So, start monitoring again.
464 monitor_thermal_zone(tz
);
468 * thermal_zone_get_temp() - returns its the temperature of thermal zone
469 * @tz: a valid pointer to a struct thermal_zone_device
470 * @temp: a valid pointer to where to store the resulting temperature.
472 * When a valid thermal zone reference is passed, it will fetch its
473 * temperature and fill @temp.
475 * Return: On success returns 0, an error code otherwise
477 int thermal_zone_get_temp(struct thermal_zone_device
*tz
, unsigned long *temp
)
480 #ifdef CONFIG_THERMAL_EMULATION
482 unsigned long crit_temp
= -1UL;
483 enum thermal_trip_type type
;
486 if (!tz
|| IS_ERR(tz
) || !tz
->ops
->get_temp
)
489 mutex_lock(&tz
->lock
);
491 ret
= tz
->ops
->get_temp(tz
, temp
);
492 #ifdef CONFIG_THERMAL_EMULATION
493 if (!tz
->emul_temperature
)
496 for (count
= 0; count
< tz
->trips
; count
++) {
497 ret
= tz
->ops
->get_trip_type(tz
, count
, &type
);
498 if (!ret
&& type
== THERMAL_TRIP_CRITICAL
) {
499 ret
= tz
->ops
->get_trip_temp(tz
, count
, &crit_temp
);
507 if (*temp
< crit_temp
)
508 *temp
= tz
->emul_temperature
;
511 mutex_unlock(&tz
->lock
);
515 EXPORT_SYMBOL_GPL(thermal_zone_get_temp
);
517 static void update_temperature(struct thermal_zone_device
*tz
)
522 ret
= thermal_zone_get_temp(tz
, &temp
);
525 dev_warn(&tz
->device
,
526 "failed to read out thermal zone (%d)\n",
531 mutex_lock(&tz
->lock
);
532 tz
->last_temperature
= tz
->temperature
;
533 tz
->temperature
= temp
;
534 mutex_unlock(&tz
->lock
);
536 trace_thermal_temperature(tz
);
537 dev_dbg(&tz
->device
, "last_temperature=%d, current_temperature=%d\n",
538 tz
->last_temperature
, tz
->temperature
);
541 void thermal_zone_device_update(struct thermal_zone_device
*tz
)
545 if (!tz
->ops
->get_temp
)
548 update_temperature(tz
);
550 for (count
= 0; count
< tz
->trips
; count
++)
551 handle_thermal_trip(tz
, count
);
553 EXPORT_SYMBOL_GPL(thermal_zone_device_update
);
555 static void thermal_zone_device_check(struct work_struct
*work
)
557 struct thermal_zone_device
*tz
= container_of(work
, struct
560 thermal_zone_device_update(tz
);
563 /* sys I/F for thermal zone */
565 #define to_thermal_zone(_dev) \
566 container_of(_dev, struct thermal_zone_device, device)
569 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
571 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
573 return sprintf(buf
, "%s\n", tz
->type
);
577 temp_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
579 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
583 ret
= thermal_zone_get_temp(tz
, &temperature
);
588 return sprintf(buf
, "%ld\n", temperature
);
592 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
594 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
595 enum thermal_device_mode mode
;
598 if (!tz
->ops
->get_mode
)
601 result
= tz
->ops
->get_mode(tz
, &mode
);
605 return sprintf(buf
, "%s\n", mode
== THERMAL_DEVICE_ENABLED
? "enabled"
610 mode_store(struct device
*dev
, struct device_attribute
*attr
,
611 const char *buf
, size_t count
)
613 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
616 if (!tz
->ops
->set_mode
)
619 if (!strncmp(buf
, "enabled", sizeof("enabled") - 1))
620 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_ENABLED
);
621 else if (!strncmp(buf
, "disabled", sizeof("disabled") - 1))
622 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_DISABLED
);
633 trip_point_type_show(struct device
*dev
, struct device_attribute
*attr
,
636 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
637 enum thermal_trip_type type
;
640 if (!tz
->ops
->get_trip_type
)
643 if (!sscanf(attr
->attr
.name
, "trip_point_%d_type", &trip
))
646 result
= tz
->ops
->get_trip_type(tz
, trip
, &type
);
651 case THERMAL_TRIP_CRITICAL
:
652 return sprintf(buf
, "critical\n");
653 case THERMAL_TRIP_HOT
:
654 return sprintf(buf
, "hot\n");
655 case THERMAL_TRIP_PASSIVE
:
656 return sprintf(buf
, "passive\n");
657 case THERMAL_TRIP_ACTIVE
:
658 return sprintf(buf
, "active\n");
660 return sprintf(buf
, "unknown\n");
665 trip_point_temp_store(struct device
*dev
, struct device_attribute
*attr
,
666 const char *buf
, size_t count
)
668 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
670 unsigned long temperature
;
672 if (!tz
->ops
->set_trip_temp
)
675 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
678 if (kstrtoul(buf
, 10, &temperature
))
681 ret
= tz
->ops
->set_trip_temp(tz
, trip
, temperature
);
683 return ret
? ret
: count
;
687 trip_point_temp_show(struct device
*dev
, struct device_attribute
*attr
,
690 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
694 if (!tz
->ops
->get_trip_temp
)
697 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
700 ret
= tz
->ops
->get_trip_temp(tz
, trip
, &temperature
);
705 return sprintf(buf
, "%ld\n", temperature
);
709 trip_point_hyst_store(struct device
*dev
, struct device_attribute
*attr
,
710 const char *buf
, size_t count
)
712 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
714 unsigned long temperature
;
716 if (!tz
->ops
->set_trip_hyst
)
719 if (!sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
))
722 if (kstrtoul(buf
, 10, &temperature
))
726 * We are not doing any check on the 'temperature' value
727 * here. The driver implementing 'set_trip_hyst' has to
730 ret
= tz
->ops
->set_trip_hyst(tz
, trip
, temperature
);
732 return ret
? ret
: count
;
736 trip_point_hyst_show(struct device
*dev
, struct device_attribute
*attr
,
739 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
741 unsigned long temperature
;
743 if (!tz
->ops
->get_trip_hyst
)
746 if (!sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
))
749 ret
= tz
->ops
->get_trip_hyst(tz
, trip
, &temperature
);
751 return ret
? ret
: sprintf(buf
, "%ld\n", temperature
);
755 passive_store(struct device
*dev
, struct device_attribute
*attr
,
756 const char *buf
, size_t count
)
758 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
759 struct thermal_cooling_device
*cdev
= NULL
;
762 if (!sscanf(buf
, "%d\n", &state
))
765 /* sanity check: values below 1000 millicelcius don't make sense
766 * and can cause the system to go into a thermal heart attack
768 if (state
&& state
< 1000)
771 if (state
&& !tz
->forced_passive
) {
772 mutex_lock(&thermal_list_lock
);
773 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
774 if (!strncmp("Processor", cdev
->type
,
775 sizeof("Processor")))
776 thermal_zone_bind_cooling_device(tz
,
777 THERMAL_TRIPS_NONE
, cdev
,
780 THERMAL_WEIGHT_DEFAULT
);
782 mutex_unlock(&thermal_list_lock
);
783 if (!tz
->passive_delay
)
784 tz
->passive_delay
= 1000;
785 } else if (!state
&& tz
->forced_passive
) {
786 mutex_lock(&thermal_list_lock
);
787 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
788 if (!strncmp("Processor", cdev
->type
,
789 sizeof("Processor")))
790 thermal_zone_unbind_cooling_device(tz
,
794 mutex_unlock(&thermal_list_lock
);
795 tz
->passive_delay
= 0;
798 tz
->forced_passive
= state
;
800 thermal_zone_device_update(tz
);
806 passive_show(struct device
*dev
, struct device_attribute
*attr
,
809 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
811 return sprintf(buf
, "%d\n", tz
->forced_passive
);
815 policy_store(struct device
*dev
, struct device_attribute
*attr
,
816 const char *buf
, size_t count
)
819 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
820 struct thermal_governor
*gov
;
821 char name
[THERMAL_NAME_LENGTH
];
823 snprintf(name
, sizeof(name
), "%s", buf
);
825 mutex_lock(&thermal_governor_lock
);
826 mutex_lock(&tz
->lock
);
828 gov
= __find_governor(strim(name
));
832 ret
= thermal_set_governor(tz
, gov
);
837 mutex_unlock(&tz
->lock
);
838 mutex_unlock(&thermal_governor_lock
);
843 policy_show(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
845 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
847 return sprintf(buf
, "%s\n", tz
->governor
->name
);
850 #ifdef CONFIG_THERMAL_EMULATION
852 emul_temp_store(struct device
*dev
, struct device_attribute
*attr
,
853 const char *buf
, size_t count
)
855 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
857 unsigned long temperature
;
859 if (kstrtoul(buf
, 10, &temperature
))
862 if (!tz
->ops
->set_emul_temp
) {
863 mutex_lock(&tz
->lock
);
864 tz
->emul_temperature
= temperature
;
865 mutex_unlock(&tz
->lock
);
867 ret
= tz
->ops
->set_emul_temp(tz
, temperature
);
871 thermal_zone_device_update(tz
);
873 return ret
? ret
: count
;
875 static DEVICE_ATTR(emul_temp
, S_IWUSR
, NULL
, emul_temp_store
);
876 #endif/*CONFIG_THERMAL_EMULATION*/
878 static DEVICE_ATTR(type
, 0444, type_show
, NULL
);
879 static DEVICE_ATTR(temp
, 0444, temp_show
, NULL
);
880 static DEVICE_ATTR(mode
, 0644, mode_show
, mode_store
);
881 static DEVICE_ATTR(passive
, S_IRUGO
| S_IWUSR
, passive_show
, passive_store
);
882 static DEVICE_ATTR(policy
, S_IRUGO
| S_IWUSR
, policy_show
, policy_store
);
884 /* sys I/F for cooling device */
885 #define to_cooling_device(_dev) \
886 container_of(_dev, struct thermal_cooling_device, device)
889 thermal_cooling_device_type_show(struct device
*dev
,
890 struct device_attribute
*attr
, char *buf
)
892 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
894 return sprintf(buf
, "%s\n", cdev
->type
);
898 thermal_cooling_device_max_state_show(struct device
*dev
,
899 struct device_attribute
*attr
, char *buf
)
901 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
905 ret
= cdev
->ops
->get_max_state(cdev
, &state
);
908 return sprintf(buf
, "%ld\n", state
);
912 thermal_cooling_device_cur_state_show(struct device
*dev
,
913 struct device_attribute
*attr
, char *buf
)
915 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
919 ret
= cdev
->ops
->get_cur_state(cdev
, &state
);
922 return sprintf(buf
, "%ld\n", state
);
926 thermal_cooling_device_cur_state_store(struct device
*dev
,
927 struct device_attribute
*attr
,
928 const char *buf
, size_t count
)
930 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
934 if (!sscanf(buf
, "%ld\n", &state
))
940 result
= cdev
->ops
->set_cur_state(cdev
, state
);
946 static struct device_attribute dev_attr_cdev_type
=
947 __ATTR(type
, 0444, thermal_cooling_device_type_show
, NULL
);
948 static DEVICE_ATTR(max_state
, 0444,
949 thermal_cooling_device_max_state_show
, NULL
);
950 static DEVICE_ATTR(cur_state
, 0644,
951 thermal_cooling_device_cur_state_show
,
952 thermal_cooling_device_cur_state_store
);
955 thermal_cooling_device_trip_point_show(struct device
*dev
,
956 struct device_attribute
*attr
, char *buf
)
958 struct thermal_instance
*instance
;
961 container_of(attr
, struct thermal_instance
, attr
);
963 if (instance
->trip
== THERMAL_TRIPS_NONE
)
964 return sprintf(buf
, "-1\n");
966 return sprintf(buf
, "%d\n", instance
->trip
);
969 static struct attribute
*cooling_device_attrs
[] = {
970 &dev_attr_cdev_type
.attr
,
971 &dev_attr_max_state
.attr
,
972 &dev_attr_cur_state
.attr
,
976 static const struct attribute_group cooling_device_attr_group
= {
977 .attrs
= cooling_device_attrs
,
980 static const struct attribute_group
*cooling_device_attr_groups
[] = {
981 &cooling_device_attr_group
,
986 thermal_cooling_device_weight_show(struct device
*dev
,
987 struct device_attribute
*attr
, char *buf
)
989 struct thermal_instance
*instance
;
991 instance
= container_of(attr
, struct thermal_instance
, weight_attr
);
993 return sprintf(buf
, "%d\n", instance
->weight
);
997 thermal_cooling_device_weight_store(struct device
*dev
,
998 struct device_attribute
*attr
,
999 const char *buf
, size_t count
)
1001 struct thermal_instance
*instance
;
1004 ret
= kstrtoint(buf
, 0, &weight
);
1008 instance
= container_of(attr
, struct thermal_instance
, weight_attr
);
1009 instance
->weight
= weight
;
1013 /* Device management */
1016 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1017 * @tz: pointer to struct thermal_zone_device
1018 * @trip: indicates which trip point the cooling devices is
1019 * associated with in this thermal zone.
1020 * @cdev: pointer to struct thermal_cooling_device
1021 * @upper: the Maximum cooling state for this trip point.
1022 * THERMAL_NO_LIMIT means no upper limit,
1023 * and the cooling device can be in max_state.
1024 * @lower: the Minimum cooling state can be used for this trip point.
1025 * THERMAL_NO_LIMIT means no lower limit,
1026 * and the cooling device can be in cooling state 0.
1027 * @weight: The weight of the cooling device to be bound to the
1028 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1031 * This interface function bind a thermal cooling device to the certain trip
1032 * point of a thermal zone device.
1033 * This function is usually called in the thermal zone device .bind callback.
1035 * Return: 0 on success, the proper error value otherwise.
1037 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
1039 struct thermal_cooling_device
*cdev
,
1040 unsigned long upper
, unsigned long lower
,
1041 unsigned int weight
)
1043 struct thermal_instance
*dev
;
1044 struct thermal_instance
*pos
;
1045 struct thermal_zone_device
*pos1
;
1046 struct thermal_cooling_device
*pos2
;
1047 unsigned long max_state
;
1050 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
1053 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
1057 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
1062 if (tz
!= pos1
|| cdev
!= pos2
)
1065 ret
= cdev
->ops
->get_max_state(cdev
, &max_state
);
1069 /* lower default 0, upper default max_state */
1070 lower
= lower
== THERMAL_NO_LIMIT
? 0 : lower
;
1071 upper
= upper
== THERMAL_NO_LIMIT
? max_state
: upper
;
1073 if (lower
> upper
|| upper
> max_state
)
1077 kzalloc(sizeof(struct thermal_instance
), GFP_KERNEL
);
1085 dev
->target
= THERMAL_NO_TARGET
;
1086 dev
->weight
= weight
;
1088 result
= get_idr(&tz
->idr
, &tz
->lock
, &dev
->id
);
1092 sprintf(dev
->name
, "cdev%d", dev
->id
);
1094 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
1098 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
1099 sysfs_attr_init(&dev
->attr
.attr
);
1100 dev
->attr
.attr
.name
= dev
->attr_name
;
1101 dev
->attr
.attr
.mode
= 0444;
1102 dev
->attr
.show
= thermal_cooling_device_trip_point_show
;
1103 result
= device_create_file(&tz
->device
, &dev
->attr
);
1105 goto remove_symbol_link
;
1107 sprintf(dev
->weight_attr_name
, "cdev%d_weight", dev
->id
);
1108 sysfs_attr_init(&dev
->weight_attr
.attr
);
1109 dev
->weight_attr
.attr
.name
= dev
->weight_attr_name
;
1110 dev
->weight_attr
.attr
.mode
= S_IWUSR
| S_IRUGO
;
1111 dev
->weight_attr
.show
= thermal_cooling_device_weight_show
;
1112 dev
->weight_attr
.store
= thermal_cooling_device_weight_store
;
1113 result
= device_create_file(&tz
->device
, &dev
->weight_attr
);
1115 goto remove_trip_file
;
1117 mutex_lock(&tz
->lock
);
1118 mutex_lock(&cdev
->lock
);
1119 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
)
1120 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
1125 list_add_tail(&dev
->tz_node
, &tz
->thermal_instances
);
1126 list_add_tail(&dev
->cdev_node
, &cdev
->thermal_instances
);
1128 mutex_unlock(&cdev
->lock
);
1129 mutex_unlock(&tz
->lock
);
1134 device_remove_file(&tz
->device
, &dev
->weight_attr
);
1136 device_remove_file(&tz
->device
, &dev
->attr
);
1138 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
1140 release_idr(&tz
->idr
, &tz
->lock
, dev
->id
);
1145 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device
);
1148 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1150 * @tz: pointer to a struct thermal_zone_device.
1151 * @trip: indicates which trip point the cooling devices is
1152 * associated with in this thermal zone.
1153 * @cdev: pointer to a struct thermal_cooling_device.
1155 * This interface function unbind a thermal cooling device from the certain
1156 * trip point of a thermal zone device.
1157 * This function is usually called in the thermal zone device .unbind callback.
1159 * Return: 0 on success, the proper error value otherwise.
1161 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
1163 struct thermal_cooling_device
*cdev
)
1165 struct thermal_instance
*pos
, *next
;
1167 mutex_lock(&tz
->lock
);
1168 mutex_lock(&cdev
->lock
);
1169 list_for_each_entry_safe(pos
, next
, &tz
->thermal_instances
, tz_node
) {
1170 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
1171 list_del(&pos
->tz_node
);
1172 list_del(&pos
->cdev_node
);
1173 mutex_unlock(&cdev
->lock
);
1174 mutex_unlock(&tz
->lock
);
1178 mutex_unlock(&cdev
->lock
);
1179 mutex_unlock(&tz
->lock
);
1184 device_remove_file(&tz
->device
, &pos
->attr
);
1185 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
1186 release_idr(&tz
->idr
, &tz
->lock
, pos
->id
);
1190 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device
);
1192 static void thermal_release(struct device
*dev
)
1194 struct thermal_zone_device
*tz
;
1195 struct thermal_cooling_device
*cdev
;
1197 if (!strncmp(dev_name(dev
), "thermal_zone",
1198 sizeof("thermal_zone") - 1)) {
1199 tz
= to_thermal_zone(dev
);
1201 } else if(!strncmp(dev_name(dev
), "cooling_device",
1202 sizeof("cooling_device") - 1)){
1203 cdev
= to_cooling_device(dev
);
1208 static struct class thermal_class
= {
1210 .dev_release
= thermal_release
,
1214 * __thermal_cooling_device_register() - register a new thermal cooling device
1215 * @np: a pointer to a device tree node.
1216 * @type: the thermal cooling device type.
1217 * @devdata: device private data.
1218 * @ops: standard thermal cooling devices callbacks.
1220 * This interface function adds a new thermal cooling device (fan/processor/...)
1221 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1222 * to all the thermal zone devices registered at the same time.
1223 * It also gives the opportunity to link the cooling device to a device tree
1224 * node, so that it can be bound to a thermal zone created out of device tree.
1226 * Return: a pointer to the created struct thermal_cooling_device or an
1227 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1229 static struct thermal_cooling_device
*
1230 __thermal_cooling_device_register(struct device_node
*np
,
1231 char *type
, void *devdata
,
1232 const struct thermal_cooling_device_ops
*ops
)
1234 struct thermal_cooling_device
*cdev
;
1237 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
1238 return ERR_PTR(-EINVAL
);
1240 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
1241 !ops
->set_cur_state
)
1242 return ERR_PTR(-EINVAL
);
1244 cdev
= kzalloc(sizeof(struct thermal_cooling_device
), GFP_KERNEL
);
1246 return ERR_PTR(-ENOMEM
);
1248 result
= get_idr(&thermal_cdev_idr
, &thermal_idr_lock
, &cdev
->id
);
1251 return ERR_PTR(result
);
1254 strlcpy(cdev
->type
, type
? : "", sizeof(cdev
->type
));
1255 mutex_init(&cdev
->lock
);
1256 INIT_LIST_HEAD(&cdev
->thermal_instances
);
1259 cdev
->updated
= false;
1260 cdev
->device
.class = &thermal_class
;
1261 cdev
->device
.groups
= cooling_device_attr_groups
;
1262 cdev
->devdata
= devdata
;
1263 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
1264 result
= device_register(&cdev
->device
);
1266 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1268 return ERR_PTR(result
);
1271 /* Add 'this' new cdev to the global cdev list */
1272 mutex_lock(&thermal_list_lock
);
1273 list_add(&cdev
->node
, &thermal_cdev_list
);
1274 mutex_unlock(&thermal_list_lock
);
1276 /* Update binding information for 'this' new cdev */
1283 * thermal_cooling_device_register() - register a new thermal cooling device
1284 * @type: the thermal cooling device type.
1285 * @devdata: device private data.
1286 * @ops: standard thermal cooling devices callbacks.
1288 * This interface function adds a new thermal cooling device (fan/processor/...)
1289 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1290 * to all the thermal zone devices registered at the same time.
1292 * Return: a pointer to the created struct thermal_cooling_device or an
1293 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1295 struct thermal_cooling_device
*
1296 thermal_cooling_device_register(char *type
, void *devdata
,
1297 const struct thermal_cooling_device_ops
*ops
)
1299 return __thermal_cooling_device_register(NULL
, type
, devdata
, ops
);
1301 EXPORT_SYMBOL_GPL(thermal_cooling_device_register
);
1304 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1305 * @np: a pointer to a device tree node.
1306 * @type: the thermal cooling device type.
1307 * @devdata: device private data.
1308 * @ops: standard thermal cooling devices callbacks.
1310 * This function will register a cooling device with device tree node reference.
1311 * This interface function adds a new thermal cooling device (fan/processor/...)
1312 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1313 * to all the thermal zone devices registered at the same time.
1315 * Return: a pointer to the created struct thermal_cooling_device or an
1316 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1318 struct thermal_cooling_device
*
1319 thermal_of_cooling_device_register(struct device_node
*np
,
1320 char *type
, void *devdata
,
1321 const struct thermal_cooling_device_ops
*ops
)
1323 return __thermal_cooling_device_register(np
, type
, devdata
, ops
);
1325 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register
);
1328 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1329 * @cdev: the thermal cooling device to remove.
1331 * thermal_cooling_device_unregister() must be called when the device is no
1334 void thermal_cooling_device_unregister(struct thermal_cooling_device
*cdev
)
1337 const struct thermal_zone_params
*tzp
;
1338 struct thermal_zone_device
*tz
;
1339 struct thermal_cooling_device
*pos
= NULL
;
1344 mutex_lock(&thermal_list_lock
);
1345 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
1349 /* thermal cooling device not found */
1350 mutex_unlock(&thermal_list_lock
);
1353 list_del(&cdev
->node
);
1355 /* Unbind all thermal zones associated with 'this' cdev */
1356 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1357 if (tz
->ops
->unbind
) {
1358 tz
->ops
->unbind(tz
, cdev
);
1362 if (!tz
->tzp
|| !tz
->tzp
->tbp
)
1366 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1367 if (tzp
->tbp
[i
].cdev
== cdev
) {
1368 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1369 tzp
->tbp
[i
].cdev
= NULL
;
1374 mutex_unlock(&thermal_list_lock
);
1377 device_remove_file(&cdev
->device
, &dev_attr_cdev_type
);
1378 device_remove_file(&cdev
->device
, &dev_attr_max_state
);
1379 device_remove_file(&cdev
->device
, &dev_attr_cur_state
);
1381 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1382 device_unregister(&cdev
->device
);
1385 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister
);
1387 void thermal_cdev_update(struct thermal_cooling_device
*cdev
)
1389 struct thermal_instance
*instance
;
1390 unsigned long target
= 0;
1392 /* cooling device is updated*/
1396 mutex_lock(&cdev
->lock
);
1397 /* Make sure cdev enters the deepest cooling state */
1398 list_for_each_entry(instance
, &cdev
->thermal_instances
, cdev_node
) {
1399 dev_dbg(&cdev
->device
, "zone%d->target=%lu\n",
1400 instance
->tz
->id
, instance
->target
);
1401 if (instance
->target
== THERMAL_NO_TARGET
)
1403 if (instance
->target
> target
)
1404 target
= instance
->target
;
1406 mutex_unlock(&cdev
->lock
);
1407 cdev
->ops
->set_cur_state(cdev
, target
);
1408 cdev
->updated
= true;
1409 trace_cdev_update(cdev
, target
);
1410 dev_dbg(&cdev
->device
, "set to state %lu\n", target
);
1412 EXPORT_SYMBOL(thermal_cdev_update
);
1415 * thermal_notify_framework - Sensor drivers use this API to notify framework
1416 * @tz: thermal zone device
1417 * @trip: indicates which trip point has been crossed
1419 * This function handles the trip events from sensor drivers. It starts
1420 * throttling the cooling devices according to the policy configured.
1421 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1422 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1423 * The throttling policy is based on the configured platform data; if no
1424 * platform data is provided, this uses the step_wise throttling policy.
1426 void thermal_notify_framework(struct thermal_zone_device
*tz
, int trip
)
1428 handle_thermal_trip(tz
, trip
);
1430 EXPORT_SYMBOL_GPL(thermal_notify_framework
);
1433 * create_trip_attrs() - create attributes for trip points
1434 * @tz: the thermal zone device
1435 * @mask: Writeable trip point bitmap.
1437 * helper function to instantiate sysfs entries for every trip
1438 * point and its properties of a struct thermal_zone_device.
1440 * Return: 0 on success, the proper error value otherwise.
1442 static int create_trip_attrs(struct thermal_zone_device
*tz
, int mask
)
1445 int size
= sizeof(struct thermal_attr
) * tz
->trips
;
1447 tz
->trip_type_attrs
= kzalloc(size
, GFP_KERNEL
);
1448 if (!tz
->trip_type_attrs
)
1451 tz
->trip_temp_attrs
= kzalloc(size
, GFP_KERNEL
);
1452 if (!tz
->trip_temp_attrs
) {
1453 kfree(tz
->trip_type_attrs
);
1457 if (tz
->ops
->get_trip_hyst
) {
1458 tz
->trip_hyst_attrs
= kzalloc(size
, GFP_KERNEL
);
1459 if (!tz
->trip_hyst_attrs
) {
1460 kfree(tz
->trip_type_attrs
);
1461 kfree(tz
->trip_temp_attrs
);
1467 for (indx
= 0; indx
< tz
->trips
; indx
++) {
1468 /* create trip type attribute */
1469 snprintf(tz
->trip_type_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1470 "trip_point_%d_type", indx
);
1472 sysfs_attr_init(&tz
->trip_type_attrs
[indx
].attr
.attr
);
1473 tz
->trip_type_attrs
[indx
].attr
.attr
.name
=
1474 tz
->trip_type_attrs
[indx
].name
;
1475 tz
->trip_type_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1476 tz
->trip_type_attrs
[indx
].attr
.show
= trip_point_type_show
;
1478 device_create_file(&tz
->device
,
1479 &tz
->trip_type_attrs
[indx
].attr
);
1481 /* create trip temp attribute */
1482 snprintf(tz
->trip_temp_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1483 "trip_point_%d_temp", indx
);
1485 sysfs_attr_init(&tz
->trip_temp_attrs
[indx
].attr
.attr
);
1486 tz
->trip_temp_attrs
[indx
].attr
.attr
.name
=
1487 tz
->trip_temp_attrs
[indx
].name
;
1488 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1489 tz
->trip_temp_attrs
[indx
].attr
.show
= trip_point_temp_show
;
1490 if (mask
& (1 << indx
)) {
1491 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
1492 tz
->trip_temp_attrs
[indx
].attr
.store
=
1493 trip_point_temp_store
;
1496 device_create_file(&tz
->device
,
1497 &tz
->trip_temp_attrs
[indx
].attr
);
1499 /* create Optional trip hyst attribute */
1500 if (!tz
->ops
->get_trip_hyst
)
1502 snprintf(tz
->trip_hyst_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1503 "trip_point_%d_hyst", indx
);
1505 sysfs_attr_init(&tz
->trip_hyst_attrs
[indx
].attr
.attr
);
1506 tz
->trip_hyst_attrs
[indx
].attr
.attr
.name
=
1507 tz
->trip_hyst_attrs
[indx
].name
;
1508 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1509 tz
->trip_hyst_attrs
[indx
].attr
.show
= trip_point_hyst_show
;
1510 if (tz
->ops
->set_trip_hyst
) {
1511 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
1512 tz
->trip_hyst_attrs
[indx
].attr
.store
=
1513 trip_point_hyst_store
;
1516 device_create_file(&tz
->device
,
1517 &tz
->trip_hyst_attrs
[indx
].attr
);
1522 static void remove_trip_attrs(struct thermal_zone_device
*tz
)
1526 for (indx
= 0; indx
< tz
->trips
; indx
++) {
1527 device_remove_file(&tz
->device
,
1528 &tz
->trip_type_attrs
[indx
].attr
);
1529 device_remove_file(&tz
->device
,
1530 &tz
->trip_temp_attrs
[indx
].attr
);
1531 if (tz
->ops
->get_trip_hyst
)
1532 device_remove_file(&tz
->device
,
1533 &tz
->trip_hyst_attrs
[indx
].attr
);
1535 kfree(tz
->trip_type_attrs
);
1536 kfree(tz
->trip_temp_attrs
);
1537 kfree(tz
->trip_hyst_attrs
);
1541 * thermal_zone_device_register() - register a new thermal zone device
1542 * @type: the thermal zone device type
1543 * @trips: the number of trip points the thermal zone support
1544 * @mask: a bit string indicating the writeablility of trip points
1545 * @devdata: private device data
1546 * @ops: standard thermal zone device callbacks
1547 * @tzp: thermal zone platform parameters
1548 * @passive_delay: number of milliseconds to wait between polls when
1549 * performing passive cooling
1550 * @polling_delay: number of milliseconds to wait between polls when checking
1551 * whether trip points have been crossed (0 for interrupt
1554 * This interface function adds a new thermal zone device (sensor) to
1555 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1556 * thermal cooling devices registered at the same time.
1557 * thermal_zone_device_unregister() must be called when the device is no
1558 * longer needed. The passive cooling depends on the .get_trend() return value.
1560 * Return: a pointer to the created struct thermal_zone_device or an
1561 * in case of error, an ERR_PTR. Caller must check return value with
1562 * IS_ERR*() helpers.
1564 struct thermal_zone_device
*thermal_zone_device_register(const char *type
,
1565 int trips
, int mask
, void *devdata
,
1566 struct thermal_zone_device_ops
*ops
,
1567 const struct thermal_zone_params
*tzp
,
1568 int passive_delay
, int polling_delay
)
1570 struct thermal_zone_device
*tz
;
1571 enum thermal_trip_type trip_type
;
1575 struct thermal_governor
*governor
;
1577 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
1578 return ERR_PTR(-EINVAL
);
1580 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0 || mask
>> trips
)
1581 return ERR_PTR(-EINVAL
);
1584 return ERR_PTR(-EINVAL
);
1586 if (trips
> 0 && (!ops
->get_trip_type
|| !ops
->get_trip_temp
))
1587 return ERR_PTR(-EINVAL
);
1589 tz
= kzalloc(sizeof(struct thermal_zone_device
), GFP_KERNEL
);
1591 return ERR_PTR(-ENOMEM
);
1593 INIT_LIST_HEAD(&tz
->thermal_instances
);
1595 mutex_init(&tz
->lock
);
1596 result
= get_idr(&thermal_tz_idr
, &thermal_idr_lock
, &tz
->id
);
1599 return ERR_PTR(result
);
1602 strlcpy(tz
->type
, type
? : "", sizeof(tz
->type
));
1605 tz
->device
.class = &thermal_class
;
1606 tz
->devdata
= devdata
;
1608 tz
->passive_delay
= passive_delay
;
1609 tz
->polling_delay
= polling_delay
;
1611 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1612 result
= device_register(&tz
->device
);
1614 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1616 return ERR_PTR(result
);
1621 result
= device_create_file(&tz
->device
, &dev_attr_type
);
1626 result
= device_create_file(&tz
->device
, &dev_attr_temp
);
1630 if (ops
->get_mode
) {
1631 result
= device_create_file(&tz
->device
, &dev_attr_mode
);
1636 result
= create_trip_attrs(tz
, mask
);
1640 for (count
= 0; count
< trips
; count
++) {
1641 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
1642 if (trip_type
== THERMAL_TRIP_PASSIVE
)
1647 result
= device_create_file(&tz
->device
, &dev_attr_passive
);
1652 #ifdef CONFIG_THERMAL_EMULATION
1653 result
= device_create_file(&tz
->device
, &dev_attr_emul_temp
);
1657 /* Create policy attribute */
1658 result
= device_create_file(&tz
->device
, &dev_attr_policy
);
1662 /* Update 'this' zone's governor information */
1663 mutex_lock(&thermal_governor_lock
);
1666 governor
= __find_governor(tz
->tzp
->governor_name
);
1668 governor
= def_governor
;
1670 result
= thermal_set_governor(tz
, governor
);
1672 mutex_unlock(&thermal_governor_lock
);
1676 mutex_unlock(&thermal_governor_lock
);
1678 if (!tz
->tzp
|| !tz
->tzp
->no_hwmon
) {
1679 result
= thermal_add_hwmon_sysfs(tz
);
1684 mutex_lock(&thermal_list_lock
);
1685 list_add_tail(&tz
->node
, &thermal_tz_list
);
1686 mutex_unlock(&thermal_list_lock
);
1688 /* Bind cooling devices for this zone */
1691 INIT_DELAYED_WORK(&(tz
->poll_queue
), thermal_zone_device_check
);
1693 if (!tz
->ops
->get_temp
)
1694 thermal_zone_device_set_polling(tz
, 0);
1696 thermal_zone_device_update(tz
);
1701 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1702 device_unregister(&tz
->device
);
1703 return ERR_PTR(result
);
1705 EXPORT_SYMBOL_GPL(thermal_zone_device_register
);
1708 * thermal_device_unregister - removes the registered thermal zone device
1709 * @tz: the thermal zone device to remove
1711 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1714 const struct thermal_zone_params
*tzp
;
1715 struct thermal_cooling_device
*cdev
;
1716 struct thermal_zone_device
*pos
= NULL
;
1723 mutex_lock(&thermal_list_lock
);
1724 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1728 /* thermal zone device not found */
1729 mutex_unlock(&thermal_list_lock
);
1732 list_del(&tz
->node
);
1734 /* Unbind all cdevs associated with 'this' thermal zone */
1735 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
1736 if (tz
->ops
->unbind
) {
1737 tz
->ops
->unbind(tz
, cdev
);
1741 if (!tzp
|| !tzp
->tbp
)
1744 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1745 if (tzp
->tbp
[i
].cdev
== cdev
) {
1746 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1747 tzp
->tbp
[i
].cdev
= NULL
;
1752 mutex_unlock(&thermal_list_lock
);
1754 thermal_zone_device_set_polling(tz
, 0);
1757 device_remove_file(&tz
->device
, &dev_attr_type
);
1758 device_remove_file(&tz
->device
, &dev_attr_temp
);
1759 if (tz
->ops
->get_mode
)
1760 device_remove_file(&tz
->device
, &dev_attr_mode
);
1761 device_remove_file(&tz
->device
, &dev_attr_policy
);
1762 remove_trip_attrs(tz
);
1763 thermal_set_governor(tz
, NULL
);
1765 thermal_remove_hwmon_sysfs(tz
);
1766 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1767 idr_destroy(&tz
->idr
);
1768 mutex_destroy(&tz
->lock
);
1769 device_unregister(&tz
->device
);
1772 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister
);
1775 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1776 * @name: thermal zone name to fetch the temperature
1778 * When only one zone is found with the passed name, returns a reference to it.
1780 * Return: On success returns a reference to an unique thermal zone with
1781 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1782 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1784 struct thermal_zone_device
*thermal_zone_get_zone_by_name(const char *name
)
1786 struct thermal_zone_device
*pos
= NULL
, *ref
= ERR_PTR(-EINVAL
);
1787 unsigned int found
= 0;
1792 mutex_lock(&thermal_list_lock
);
1793 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1794 if (!strncasecmp(name
, pos
->type
, THERMAL_NAME_LENGTH
)) {
1798 mutex_unlock(&thermal_list_lock
);
1800 /* nothing has been found, thus an error code for it */
1802 ref
= ERR_PTR(-ENODEV
);
1804 /* Success only when an unique zone is found */
1805 ref
= ERR_PTR(-EEXIST
);
1810 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name
);
1813 static const struct genl_multicast_group thermal_event_mcgrps
[] = {
1814 { .name
= THERMAL_GENL_MCAST_GROUP_NAME
, },
1817 static struct genl_family thermal_event_genl_family
= {
1818 .id
= GENL_ID_GENERATE
,
1819 .name
= THERMAL_GENL_FAMILY_NAME
,
1820 .version
= THERMAL_GENL_VERSION
,
1821 .maxattr
= THERMAL_GENL_ATTR_MAX
,
1822 .mcgrps
= thermal_event_mcgrps
,
1823 .n_mcgrps
= ARRAY_SIZE(thermal_event_mcgrps
),
1826 int thermal_generate_netlink_event(struct thermal_zone_device
*tz
,
1829 struct sk_buff
*skb
;
1830 struct nlattr
*attr
;
1831 struct thermal_genl_event
*thermal_event
;
1835 static unsigned int thermal_event_seqnum
;
1840 /* allocate memory */
1841 size
= nla_total_size(sizeof(struct thermal_genl_event
)) +
1844 skb
= genlmsg_new(size
, GFP_ATOMIC
);
1848 /* add the genetlink message header */
1849 msg_header
= genlmsg_put(skb
, 0, thermal_event_seqnum
++,
1850 &thermal_event_genl_family
, 0,
1851 THERMAL_GENL_CMD_EVENT
);
1858 attr
= nla_reserve(skb
, THERMAL_GENL_ATTR_EVENT
,
1859 sizeof(struct thermal_genl_event
));
1866 thermal_event
= nla_data(attr
);
1867 if (!thermal_event
) {
1872 memset(thermal_event
, 0, sizeof(struct thermal_genl_event
));
1874 thermal_event
->orig
= tz
->id
;
1875 thermal_event
->event
= event
;
1877 /* send multicast genetlink message */
1878 genlmsg_end(skb
, msg_header
);
1880 result
= genlmsg_multicast(&thermal_event_genl_family
, skb
, 0,
1883 dev_err(&tz
->device
, "Failed to send netlink event:%d", result
);
1887 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event
);
1889 static int genetlink_init(void)
1891 return genl_register_family(&thermal_event_genl_family
);
1894 static void genetlink_exit(void)
1896 genl_unregister_family(&thermal_event_genl_family
);
1898 #else /* !CONFIG_NET */
1899 static inline int genetlink_init(void) { return 0; }
1900 static inline void genetlink_exit(void) {}
1901 #endif /* !CONFIG_NET */
1903 static int __init
thermal_register_governors(void)
1907 result
= thermal_gov_step_wise_register();
1911 result
= thermal_gov_fair_share_register();
1915 result
= thermal_gov_bang_bang_register();
1919 return thermal_gov_user_space_register();
1922 static void thermal_unregister_governors(void)
1924 thermal_gov_step_wise_unregister();
1925 thermal_gov_fair_share_unregister();
1926 thermal_gov_bang_bang_unregister();
1927 thermal_gov_user_space_unregister();
1930 static int __init
thermal_init(void)
1934 result
= thermal_register_governors();
1938 result
= class_register(&thermal_class
);
1940 goto unregister_governors
;
1942 result
= genetlink_init();
1944 goto unregister_class
;
1946 result
= of_parse_thermal_zones();
1955 class_unregister(&thermal_class
);
1956 unregister_governors
:
1957 thermal_unregister_governors();
1959 idr_destroy(&thermal_tz_idr
);
1960 idr_destroy(&thermal_cdev_idr
);
1961 mutex_destroy(&thermal_idr_lock
);
1962 mutex_destroy(&thermal_list_lock
);
1963 mutex_destroy(&thermal_governor_lock
);
1967 static void __exit
thermal_exit(void)
1969 of_thermal_destroy_zones();
1971 class_unregister(&thermal_class
);
1972 thermal_unregister_governors();
1973 idr_destroy(&thermal_tz_idr
);
1974 idr_destroy(&thermal_cdev_idr
);
1975 mutex_destroy(&thermal_idr_lock
);
1976 mutex_destroy(&thermal_list_lock
);
1977 mutex_destroy(&thermal_governor_lock
);
1980 fs_initcall(thermal_init
);
1981 module_exit(thermal_exit
);