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 #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])
68 list_for_each_entry(pos
, &thermal_governor_list
, governor_list
)
69 if (!strnicmp(name
, pos
->name
, THERMAL_NAME_LENGTH
))
75 int thermal_register_governor(struct thermal_governor
*governor
)
79 struct thermal_zone_device
*pos
;
84 mutex_lock(&thermal_governor_lock
);
87 if (__find_governor(governor
->name
) == NULL
) {
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
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
);
117 void thermal_unregister_governor(struct thermal_governor
*governor
)
119 struct thermal_zone_device
*pos
;
124 mutex_lock(&thermal_governor_lock
);
126 if (__find_governor(governor
->name
) == NULL
)
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
);
140 mutex_unlock(&thermal_governor_lock
);
144 static int get_idr(struct idr
*idr
, struct mutex
*lock
, int *id
)
150 ret
= idr_alloc(idr
, NULL
, 0, 0, GFP_KERNEL
);
153 if (unlikely(ret
< 0))
159 static void release_idr(struct idr
*idr
, struct mutex
*lock
, int id
)
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
;
179 trend
= THERMAL_TREND_STABLE
;
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
;
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
)
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
;
229 lower
= limits
[i
* 2];
230 upper
= limits
[i
* 2 + 1];
232 ret
= thermal_zone_bind_cooling_device(tz
, i
, cdev
,
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
)
245 for (i
= 0; i
< tz
->trips
; i
++)
247 thermal_zone_unbind_cooling_device(tz
, i
, cdev
);
250 static void bind_cdev(struct thermal_cooling_device
*cdev
)
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
)
262 if (pos
->ops
->bind
) {
263 ret
= pos
->ops
->bind(pos
, cdev
);
265 print_bind_err_msg(pos
, cdev
, ret
);
270 if (!tzp
|| !tzp
->tbp
)
273 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
274 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
276 if (tzp
->tbp
[i
].match(pos
, cdev
))
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
)
290 struct thermal_cooling_device
*pos
= NULL
;
291 const struct thermal_zone_params
*tzp
= tz
->tzp
;
293 if (!tzp
&& !tz
->ops
->bind
)
296 mutex_lock(&thermal_list_lock
);
298 /* If there is ops->bind, try to use ops->bind */
300 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
301 ret
= tz
->ops
->bind(tz
, pos
);
303 print_bind_err_msg(tz
, pos
, ret
);
308 if (!tzp
|| !tzp
->tbp
)
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
)
315 if (tzp
->tbp
[i
].match(tz
, pos
))
317 tzp
->tbp
[i
].cdev
= pos
;
318 __bind(tz
, tzp
->tbp
[i
].trip_mask
, pos
,
319 tzp
->tbp
[i
].binding_limits
);
323 mutex_unlock(&thermal_list_lock
);
326 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
330 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
331 round_jiffies(msecs_to_jiffies(delay
)));
333 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
334 msecs_to_jiffies(delay
));
336 cancel_delayed_work(&tz
->poll_queue
);
339 static void monitor_thermal_zone(struct thermal_zone_device
*tz
)
341 mutex_lock(&tz
->lock
);
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
);
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
)
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
)
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
);
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
)
412 #ifdef CONFIG_THERMAL_EMULATION
414 unsigned long crit_temp
= -1UL;
415 enum thermal_trip_type type
;
418 if (!tz
|| IS_ERR(tz
) || !tz
->ops
->get_temp
)
421 mutex_lock(&tz
->lock
);
423 ret
= tz
->ops
->get_temp(tz
, temp
);
424 #ifdef CONFIG_THERMAL_EMULATION
425 if (!tz
->emul_temperature
)
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
);
439 if (*temp
< crit_temp
)
440 *temp
= tz
->emul_temperature
;
443 mutex_unlock(&tz
->lock
);
447 EXPORT_SYMBOL_GPL(thermal_zone_get_temp
);
449 static void update_temperature(struct thermal_zone_device
*tz
)
454 ret
= thermal_zone_get_temp(tz
, &temp
);
456 dev_warn(&tz
->device
, "failed to read out thermal zone %d\n",
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
)
474 if (!tz
->ops
->get_temp
)
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
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)
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
);
506 temp_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
508 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
512 ret
= thermal_zone_get_temp(tz
, &temperature
);
517 return sprintf(buf
, "%ld\n", temperature
);
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
;
527 if (!tz
->ops
->get_mode
)
530 result
= tz
->ops
->get_mode(tz
, &mode
);
534 return sprintf(buf
, "%s\n", mode
== THERMAL_DEVICE_ENABLED
? "enabled"
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
);
545 if (!tz
->ops
->set_mode
)
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
);
562 trip_point_type_show(struct device
*dev
, struct device_attribute
*attr
,
565 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
566 enum thermal_trip_type type
;
569 if (!tz
->ops
->get_trip_type
)
572 if (!sscanf(attr
->attr
.name
, "trip_point_%d_type", &trip
))
575 result
= tz
->ops
->get_trip_type(tz
, trip
, &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");
589 return sprintf(buf
, "unknown\n");
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
);
599 unsigned long temperature
;
601 if (!tz
->ops
->set_trip_temp
)
604 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
607 if (kstrtoul(buf
, 10, &temperature
))
610 ret
= tz
->ops
->set_trip_temp(tz
, trip
, temperature
);
612 return ret
? ret
: count
;
616 trip_point_temp_show(struct device
*dev
, struct device_attribute
*attr
,
619 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
623 if (!tz
->ops
->get_trip_temp
)
626 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
629 ret
= tz
->ops
->get_trip_temp(tz
, trip
, &temperature
);
634 return sprintf(buf
, "%ld\n", temperature
);
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
);
643 unsigned long temperature
;
645 if (!tz
->ops
->set_trip_hyst
)
648 if (!sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
))
651 if (kstrtoul(buf
, 10, &temperature
))
655 * We are not doing any check on the 'temperature' value
656 * here. The driver implementing 'set_trip_hyst' has to
659 ret
= tz
->ops
->set_trip_hyst(tz
, trip
, temperature
);
661 return ret
? ret
: count
;
665 trip_point_hyst_show(struct device
*dev
, struct device_attribute
*attr
,
668 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
670 unsigned long temperature
;
672 if (!tz
->ops
->get_trip_hyst
)
675 if (!sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
))
678 ret
= tz
->ops
->get_trip_hyst(tz
, trip
, &temperature
);
680 return ret
? ret
: sprintf(buf
, "%ld\n", temperature
);
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
;
691 if (!sscanf(buf
, "%d\n", &state
))
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)
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
,
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
,
722 mutex_unlock(&thermal_list_lock
);
723 tz
->passive_delay
= 0;
726 tz
->forced_passive
= state
;
728 thermal_zone_device_update(tz
);
734 passive_show(struct device
*dev
, struct device_attribute
*attr
,
737 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
739 return sprintf(buf
, "%d\n", tz
->forced_passive
);
743 policy_store(struct device
*dev
, struct device_attribute
*attr
,
744 const char *buf
, size_t count
)
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
));
763 mutex_unlock(&thermal_governor_lock
);
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
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
);
782 unsigned long temperature
;
784 if (kstrtoul(buf
, 10, &temperature
))
787 if (!tz
->ops
->set_emul_temp
) {
788 mutex_lock(&tz
->lock
);
789 tz
->emul_temperature
= temperature
;
790 mutex_unlock(&tz
->lock
);
792 ret
= tz
->ops
->set_emul_temp(tz
, temperature
);
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)
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
);
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
);
830 ret
= cdev
->ops
->get_max_state(cdev
, &state
);
833 return sprintf(buf
, "%ld\n", state
);
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
);
844 ret
= cdev
->ops
->get_cur_state(cdev
, &state
);
847 return sprintf(buf
, "%ld\n", state
);
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
);
859 if (!sscanf(buf
, "%ld\n", &state
))
865 result
= cdev
->ops
->set_cur_state(cdev
, state
);
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
);
880 thermal_cooling_device_trip_point_show(struct device
*dev
,
881 struct device_attribute
*attr
, char *buf
)
883 struct thermal_instance
*instance
;
886 container_of(attr
, struct thermal_instance
, attr
);
888 if (instance
->trip
== THERMAL_TRIPS_NONE
)
889 return sprintf(buf
, "-1\n");
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
,
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
;
927 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
930 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
934 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
939 if (tz
!= pos1
|| cdev
!= pos2
)
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
)
952 kzalloc(sizeof(struct thermal_instance
), GFP_KERNEL
);
960 dev
->target
= THERMAL_NO_TARGET
;
962 result
= get_idr(&tz
->idr
, &tz
->lock
, &dev
->id
);
966 sprintf(dev
->name
, "cdev%d", dev
->id
);
968 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
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
);
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
) {
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
);
998 device_remove_file(&tz
->device
, &dev
->attr
);
1000 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
1002 release_idr(&tz
->idr
, &tz
->lock
, dev
->id
);
1007 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device
);
1010 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
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
,
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
);
1040 mutex_unlock(&cdev
->lock
);
1041 mutex_unlock(&tz
->lock
);
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
);
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
);
1063 } else if(!strncmp(dev_name(dev
), "cooling_device",
1064 sizeof("cooling_device") - 1)){
1065 cdev
= to_cooling_device(dev
);
1070 static struct class thermal_class
= {
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
;
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
);
1108 return ERR_PTR(-ENOMEM
);
1110 result
= get_idr(&thermal_cdev_idr
, &thermal_idr_lock
, &cdev
->id
);
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
);
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
);
1127 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1129 return ERR_PTR(result
);
1134 result
= device_create_file(&cdev
->device
, &dev_attr_cdev_type
);
1139 result
= device_create_file(&cdev
->device
, &dev_attr_max_state
);
1143 result
= device_create_file(&cdev
->device
, &dev_attr_cur_state
);
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 */
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
1215 void thermal_cooling_device_unregister(struct thermal_cooling_device
*cdev
)
1218 const struct thermal_zone_params
*tzp
;
1219 struct thermal_zone_device
*tz
;
1220 struct thermal_cooling_device
*pos
= NULL
;
1225 mutex_lock(&thermal_list_lock
);
1226 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
1230 /* thermal cooling device not found */
1231 mutex_unlock(&thermal_list_lock
);
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
);
1243 if (!tz
->tzp
|| !tz
->tzp
->tbp
)
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
);
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
);
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*/
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
)
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
)
1325 int size
= sizeof(struct thermal_attr
) * tz
->trips
;
1327 tz
->trip_type_attrs
= kzalloc(size
, GFP_KERNEL
);
1328 if (!tz
->trip_type_attrs
)
1331 tz
->trip_temp_attrs
= kzalloc(size
, GFP_KERNEL
);
1332 if (!tz
->trip_temp_attrs
) {
1333 kfree(tz
->trip_type_attrs
);
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
);
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
)
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
);
1402 static void remove_trip_attrs(struct thermal_zone_device
*tz
)
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
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
;
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
);
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
);
1470 return ERR_PTR(-ENOMEM
);
1472 INIT_LIST_HEAD(&tz
->thermal_instances
);
1474 mutex_init(&tz
->lock
);
1475 result
= get_idr(&thermal_tz_idr
, &thermal_idr_lock
, &tz
->id
);
1478 return ERR_PTR(result
);
1481 strlcpy(tz
->type
, type
? : "", sizeof(tz
->type
));
1484 tz
->device
.class = &thermal_class
;
1485 tz
->devdata
= devdata
;
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
);
1493 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1495 return ERR_PTR(result
);
1500 result
= device_create_file(&tz
->device
, &dev_attr_type
);
1505 result
= device_create_file(&tz
->device
, &dev_attr_temp
);
1509 if (ops
->get_mode
) {
1510 result
= device_create_file(&tz
->device
, &dev_attr_mode
);
1515 result
= create_trip_attrs(tz
, mask
);
1519 for (count
= 0; count
< trips
; count
++) {
1520 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
1521 if (trip_type
== THERMAL_TRIP_PASSIVE
)
1526 result
= device_create_file(&tz
->device
, &dev_attr_passive
);
1531 #ifdef CONFIG_THERMAL_EMULATION
1532 result
= device_create_file(&tz
->device
, &dev_attr_emul_temp
);
1536 /* Create policy attribute */
1537 result
= device_create_file(&tz
->device
, &dev_attr_policy
);
1541 /* Update 'this' zone's governor information */
1542 mutex_lock(&thermal_governor_lock
);
1545 tz
->governor
= __find_governor(tz
->tzp
->governor_name
);
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
);
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 */
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
);
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
)
1588 const struct thermal_zone_params
*tzp
;
1589 struct thermal_cooling_device
*cdev
;
1590 struct thermal_zone_device
*pos
= NULL
;
1597 mutex_lock(&thermal_list_lock
);
1598 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1602 /* thermal zone device not found */
1603 mutex_unlock(&thermal_list_lock
);
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
);
1615 if (!tzp
|| !tzp
->tbp
)
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);
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
);
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;
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
)) {
1672 mutex_unlock(&thermal_list_lock
);
1674 /* nothing has been found, thus an error code for it */
1676 ref
= ERR_PTR(-ENODEV
);
1678 /* Success only when an unique zone is found */
1679 ref
= ERR_PTR(-EEXIST
);
1684 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name
);
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
,
1703 struct sk_buff
*skb
;
1704 struct nlattr
*attr
;
1705 struct thermal_genl_event
*thermal_event
;
1709 static unsigned int thermal_event_seqnum
;
1714 /* allocate memory */
1715 size
= nla_total_size(sizeof(struct thermal_genl_event
)) +
1718 skb
= genlmsg_new(size
, GFP_ATOMIC
);
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
);
1732 attr
= nla_reserve(skb
, THERMAL_GENL_ATTR_EVENT
,
1733 sizeof(struct thermal_genl_event
));
1740 thermal_event
= nla_data(attr
);
1741 if (!thermal_event
) {
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
);
1758 result
= genlmsg_multicast(&thermal_event_genl_family
, skb
, 0,
1761 dev_err(&tz
->device
, "Failed to send netlink event:%d", 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)
1785 result
= thermal_gov_step_wise_register();
1789 result
= thermal_gov_fair_share_register();
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)
1807 result
= thermal_register_governors();
1811 result
= class_register(&thermal_class
);
1813 goto unregister_governors
;
1815 result
= genetlink_init();
1817 goto unregister_class
;
1819 result
= of_parse_thermal_zones();
1827 unregister_governors
:
1828 thermal_unregister_governors();
1830 class_unregister(&thermal_class
);
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
);
1840 static void __exit
thermal_exit(void)
1842 of_thermal_destroy_zones();
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
);