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
*__find_governor(const char *name
)
61 struct thermal_governor
*pos
;
63 list_for_each_entry(pos
, &thermal_governor_list
, governor_list
)
64 if (!strnicmp(name
, pos
->name
, THERMAL_NAME_LENGTH
))
70 int thermal_register_governor(struct thermal_governor
*governor
)
74 struct thermal_zone_device
*pos
;
79 mutex_lock(&thermal_governor_lock
);
82 if (__find_governor(governor
->name
) == NULL
) {
84 list_add(&governor
->governor_list
, &thermal_governor_list
);
87 mutex_lock(&thermal_list_lock
);
89 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
93 name
= pos
->tzp
->governor_name
;
95 name
= DEFAULT_THERMAL_GOVERNOR
;
96 if (!strnicmp(name
, governor
->name
, THERMAL_NAME_LENGTH
))
97 pos
->governor
= governor
;
100 mutex_unlock(&thermal_list_lock
);
101 mutex_unlock(&thermal_governor_lock
);
106 void thermal_unregister_governor(struct thermal_governor
*governor
)
108 struct thermal_zone_device
*pos
;
113 mutex_lock(&thermal_governor_lock
);
115 if (__find_governor(governor
->name
) == NULL
)
118 mutex_lock(&thermal_list_lock
);
120 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
121 if (!strnicmp(pos
->governor
->name
, governor
->name
,
122 THERMAL_NAME_LENGTH
))
123 pos
->governor
= NULL
;
126 mutex_unlock(&thermal_list_lock
);
127 list_del(&governor
->governor_list
);
129 mutex_unlock(&thermal_governor_lock
);
133 static int get_idr(struct idr
*idr
, struct mutex
*lock
, int *id
)
139 ret
= idr_alloc(idr
, NULL
, 0, 0, GFP_KERNEL
);
142 if (unlikely(ret
< 0))
148 static void release_idr(struct idr
*idr
, struct mutex
*lock
, int id
)
157 int get_tz_trend(struct thermal_zone_device
*tz
, int trip
)
159 enum thermal_trend trend
;
161 if (tz
->emul_temperature
|| !tz
->ops
->get_trend
||
162 tz
->ops
->get_trend(tz
, trip
, &trend
)) {
163 if (tz
->temperature
> tz
->last_temperature
)
164 trend
= THERMAL_TREND_RAISING
;
165 else if (tz
->temperature
< tz
->last_temperature
)
166 trend
= THERMAL_TREND_DROPPING
;
168 trend
= THERMAL_TREND_STABLE
;
173 EXPORT_SYMBOL(get_tz_trend
);
175 struct thermal_instance
*get_thermal_instance(struct thermal_zone_device
*tz
,
176 struct thermal_cooling_device
*cdev
, int trip
)
178 struct thermal_instance
*pos
= NULL
;
179 struct thermal_instance
*target_instance
= NULL
;
181 mutex_lock(&tz
->lock
);
182 mutex_lock(&cdev
->lock
);
184 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
) {
185 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
186 target_instance
= pos
;
191 mutex_unlock(&cdev
->lock
);
192 mutex_unlock(&tz
->lock
);
194 return target_instance
;
196 EXPORT_SYMBOL(get_thermal_instance
);
198 static void print_bind_err_msg(struct thermal_zone_device
*tz
,
199 struct thermal_cooling_device
*cdev
, int ret
)
201 dev_err(&tz
->device
, "binding zone %s with cdev %s failed:%d\n",
202 tz
->type
, cdev
->type
, ret
);
205 static void __bind(struct thermal_zone_device
*tz
, int mask
,
206 struct thermal_cooling_device
*cdev
,
207 unsigned long *limits
)
211 for (i
= 0; i
< tz
->trips
; i
++) {
212 if (mask
& (1 << i
)) {
213 unsigned long upper
, lower
;
215 upper
= THERMAL_NO_LIMIT
;
216 lower
= THERMAL_NO_LIMIT
;
218 lower
= limits
[i
* 2];
219 upper
= limits
[i
* 2 + 1];
221 ret
= thermal_zone_bind_cooling_device(tz
, i
, cdev
,
224 print_bind_err_msg(tz
, cdev
, ret
);
229 static void __unbind(struct thermal_zone_device
*tz
, int mask
,
230 struct thermal_cooling_device
*cdev
)
234 for (i
= 0; i
< tz
->trips
; i
++)
236 thermal_zone_unbind_cooling_device(tz
, i
, cdev
);
239 static void bind_cdev(struct thermal_cooling_device
*cdev
)
242 const struct thermal_zone_params
*tzp
;
243 struct thermal_zone_device
*pos
= NULL
;
245 mutex_lock(&thermal_list_lock
);
247 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
248 if (!pos
->tzp
&& !pos
->ops
->bind
)
251 if (pos
->ops
->bind
) {
252 ret
= pos
->ops
->bind(pos
, cdev
);
254 print_bind_err_msg(pos
, cdev
, ret
);
259 if (!tzp
|| !tzp
->tbp
)
262 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
263 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
265 if (tzp
->tbp
[i
].match(pos
, cdev
))
267 tzp
->tbp
[i
].cdev
= cdev
;
268 __bind(pos
, tzp
->tbp
[i
].trip_mask
, cdev
,
269 tzp
->tbp
[i
].binding_limits
);
273 mutex_unlock(&thermal_list_lock
);
276 static void bind_tz(struct thermal_zone_device
*tz
)
279 struct thermal_cooling_device
*pos
= NULL
;
280 const struct thermal_zone_params
*tzp
= tz
->tzp
;
282 if (!tzp
&& !tz
->ops
->bind
)
285 mutex_lock(&thermal_list_lock
);
287 /* If there is ops->bind, try to use ops->bind */
289 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
290 ret
= tz
->ops
->bind(tz
, pos
);
292 print_bind_err_msg(tz
, pos
, ret
);
297 if (!tzp
|| !tzp
->tbp
)
300 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
301 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
302 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
304 if (tzp
->tbp
[i
].match(tz
, pos
))
306 tzp
->tbp
[i
].cdev
= pos
;
307 __bind(tz
, tzp
->tbp
[i
].trip_mask
, pos
,
308 tzp
->tbp
[i
].binding_limits
);
312 mutex_unlock(&thermal_list_lock
);
315 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
319 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
320 round_jiffies(msecs_to_jiffies(delay
)));
322 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
323 msecs_to_jiffies(delay
));
325 cancel_delayed_work(&tz
->poll_queue
);
328 static void monitor_thermal_zone(struct thermal_zone_device
*tz
)
330 mutex_lock(&tz
->lock
);
333 thermal_zone_device_set_polling(tz
, tz
->passive_delay
);
334 else if (tz
->polling_delay
)
335 thermal_zone_device_set_polling(tz
, tz
->polling_delay
);
337 thermal_zone_device_set_polling(tz
, 0);
339 mutex_unlock(&tz
->lock
);
342 static void handle_non_critical_trips(struct thermal_zone_device
*tz
,
343 int trip
, enum thermal_trip_type trip_type
)
346 tz
->governor
->throttle(tz
, trip
);
349 static void handle_critical_trips(struct thermal_zone_device
*tz
,
350 int trip
, enum thermal_trip_type trip_type
)
354 tz
->ops
->get_trip_temp(tz
, trip
, &trip_temp
);
356 /* If we have not crossed the trip_temp, we do not care. */
357 if (tz
->temperature
< trip_temp
)
361 tz
->ops
->notify(tz
, trip
, trip_type
);
363 if (trip_type
== THERMAL_TRIP_CRITICAL
) {
364 dev_emerg(&tz
->device
,
365 "critical temperature reached(%d C),shutting down\n",
366 tz
->temperature
/ 1000);
367 orderly_poweroff(true);
371 static void handle_thermal_trip(struct thermal_zone_device
*tz
, int trip
)
373 enum thermal_trip_type type
;
375 tz
->ops
->get_trip_type(tz
, trip
, &type
);
377 if (type
== THERMAL_TRIP_CRITICAL
|| type
== THERMAL_TRIP_HOT
)
378 handle_critical_trips(tz
, trip
, type
);
380 handle_non_critical_trips(tz
, trip
, type
);
382 * Alright, we handled this trip successfully.
383 * So, start monitoring again.
385 monitor_thermal_zone(tz
);
389 * thermal_zone_get_temp() - returns its the temperature of thermal zone
390 * @tz: a valid pointer to a struct thermal_zone_device
391 * @temp: a valid pointer to where to store the resulting temperature.
393 * When a valid thermal zone reference is passed, it will fetch its
394 * temperature and fill @temp.
396 * Return: On success returns 0, an error code otherwise
398 int thermal_zone_get_temp(struct thermal_zone_device
*tz
, unsigned long *temp
)
401 #ifdef CONFIG_THERMAL_EMULATION
403 unsigned long crit_temp
= -1UL;
404 enum thermal_trip_type type
;
407 if (!tz
|| IS_ERR(tz
) || !tz
->ops
->get_temp
)
410 mutex_lock(&tz
->lock
);
412 ret
= tz
->ops
->get_temp(tz
, temp
);
413 #ifdef CONFIG_THERMAL_EMULATION
414 if (!tz
->emul_temperature
)
417 for (count
= 0; count
< tz
->trips
; count
++) {
418 ret
= tz
->ops
->get_trip_type(tz
, count
, &type
);
419 if (!ret
&& type
== THERMAL_TRIP_CRITICAL
) {
420 ret
= tz
->ops
->get_trip_temp(tz
, count
, &crit_temp
);
428 if (*temp
< crit_temp
)
429 *temp
= tz
->emul_temperature
;
432 mutex_unlock(&tz
->lock
);
436 EXPORT_SYMBOL_GPL(thermal_zone_get_temp
);
438 static void update_temperature(struct thermal_zone_device
*tz
)
443 ret
= thermal_zone_get_temp(tz
, &temp
);
445 dev_warn(&tz
->device
, "failed to read out thermal zone %d\n",
450 mutex_lock(&tz
->lock
);
451 tz
->last_temperature
= tz
->temperature
;
452 tz
->temperature
= temp
;
453 mutex_unlock(&tz
->lock
);
455 dev_dbg(&tz
->device
, "last_temperature=%d, current_temperature=%d\n",
456 tz
->last_temperature
, tz
->temperature
);
459 void thermal_zone_device_update(struct thermal_zone_device
*tz
)
463 if (!tz
->ops
->get_temp
)
466 update_temperature(tz
);
468 for (count
= 0; count
< tz
->trips
; count
++)
469 handle_thermal_trip(tz
, count
);
471 EXPORT_SYMBOL_GPL(thermal_zone_device_update
);
473 static void thermal_zone_device_check(struct work_struct
*work
)
475 struct thermal_zone_device
*tz
= container_of(work
, struct
478 thermal_zone_device_update(tz
);
481 /* sys I/F for thermal zone */
483 #define to_thermal_zone(_dev) \
484 container_of(_dev, struct thermal_zone_device, device)
487 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
489 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
491 return sprintf(buf
, "%s\n", tz
->type
);
495 temp_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
497 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
501 ret
= thermal_zone_get_temp(tz
, &temperature
);
506 return sprintf(buf
, "%ld\n", temperature
);
510 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
512 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
513 enum thermal_device_mode mode
;
516 if (!tz
->ops
->get_mode
)
519 result
= tz
->ops
->get_mode(tz
, &mode
);
523 return sprintf(buf
, "%s\n", mode
== THERMAL_DEVICE_ENABLED
? "enabled"
528 mode_store(struct device
*dev
, struct device_attribute
*attr
,
529 const char *buf
, size_t count
)
531 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
534 if (!tz
->ops
->set_mode
)
537 if (!strncmp(buf
, "enabled", sizeof("enabled") - 1))
538 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_ENABLED
);
539 else if (!strncmp(buf
, "disabled", sizeof("disabled") - 1))
540 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_DISABLED
);
551 trip_point_type_show(struct device
*dev
, struct device_attribute
*attr
,
554 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
555 enum thermal_trip_type type
;
558 if (!tz
->ops
->get_trip_type
)
561 if (!sscanf(attr
->attr
.name
, "trip_point_%d_type", &trip
))
564 result
= tz
->ops
->get_trip_type(tz
, trip
, &type
);
569 case THERMAL_TRIP_CRITICAL
:
570 return sprintf(buf
, "critical\n");
571 case THERMAL_TRIP_HOT
:
572 return sprintf(buf
, "hot\n");
573 case THERMAL_TRIP_PASSIVE
:
574 return sprintf(buf
, "passive\n");
575 case THERMAL_TRIP_ACTIVE
:
576 return sprintf(buf
, "active\n");
578 return sprintf(buf
, "unknown\n");
583 trip_point_temp_store(struct device
*dev
, struct device_attribute
*attr
,
584 const char *buf
, size_t count
)
586 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
588 unsigned long temperature
;
590 if (!tz
->ops
->set_trip_temp
)
593 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
596 if (kstrtoul(buf
, 10, &temperature
))
599 ret
= tz
->ops
->set_trip_temp(tz
, trip
, temperature
);
601 return ret
? ret
: count
;
605 trip_point_temp_show(struct device
*dev
, struct device_attribute
*attr
,
608 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
612 if (!tz
->ops
->get_trip_temp
)
615 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
618 ret
= tz
->ops
->get_trip_temp(tz
, trip
, &temperature
);
623 return sprintf(buf
, "%ld\n", temperature
);
627 trip_point_hyst_store(struct device
*dev
, struct device_attribute
*attr
,
628 const char *buf
, size_t count
)
630 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
632 unsigned long temperature
;
634 if (!tz
->ops
->set_trip_hyst
)
637 if (!sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
))
640 if (kstrtoul(buf
, 10, &temperature
))
644 * We are not doing any check on the 'temperature' value
645 * here. The driver implementing 'set_trip_hyst' has to
648 ret
= tz
->ops
->set_trip_hyst(tz
, trip
, temperature
);
650 return ret
? ret
: count
;
654 trip_point_hyst_show(struct device
*dev
, struct device_attribute
*attr
,
657 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
659 unsigned long temperature
;
661 if (!tz
->ops
->get_trip_hyst
)
664 if (!sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
))
667 ret
= tz
->ops
->get_trip_hyst(tz
, trip
, &temperature
);
669 return ret
? ret
: sprintf(buf
, "%ld\n", temperature
);
673 passive_store(struct device
*dev
, struct device_attribute
*attr
,
674 const char *buf
, size_t count
)
676 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
677 struct thermal_cooling_device
*cdev
= NULL
;
680 if (!sscanf(buf
, "%d\n", &state
))
683 /* sanity check: values below 1000 millicelcius don't make sense
684 * and can cause the system to go into a thermal heart attack
686 if (state
&& state
< 1000)
689 if (state
&& !tz
->forced_passive
) {
690 mutex_lock(&thermal_list_lock
);
691 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
692 if (!strncmp("Processor", cdev
->type
,
693 sizeof("Processor")))
694 thermal_zone_bind_cooling_device(tz
,
695 THERMAL_TRIPS_NONE
, cdev
,
699 mutex_unlock(&thermal_list_lock
);
700 if (!tz
->passive_delay
)
701 tz
->passive_delay
= 1000;
702 } else if (!state
&& tz
->forced_passive
) {
703 mutex_lock(&thermal_list_lock
);
704 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
705 if (!strncmp("Processor", cdev
->type
,
706 sizeof("Processor")))
707 thermal_zone_unbind_cooling_device(tz
,
711 mutex_unlock(&thermal_list_lock
);
712 tz
->passive_delay
= 0;
715 tz
->forced_passive
= state
;
717 thermal_zone_device_update(tz
);
723 passive_show(struct device
*dev
, struct device_attribute
*attr
,
726 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
728 return sprintf(buf
, "%d\n", tz
->forced_passive
);
732 policy_store(struct device
*dev
, struct device_attribute
*attr
,
733 const char *buf
, size_t count
)
736 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
737 struct thermal_governor
*gov
;
738 char name
[THERMAL_NAME_LENGTH
];
740 snprintf(name
, sizeof(name
), "%s", buf
);
742 mutex_lock(&thermal_governor_lock
);
744 gov
= __find_governor(strim(name
));
752 mutex_unlock(&thermal_governor_lock
);
757 policy_show(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
759 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
761 return sprintf(buf
, "%s\n", tz
->governor
->name
);
764 #ifdef CONFIG_THERMAL_EMULATION
766 emul_temp_store(struct device
*dev
, struct device_attribute
*attr
,
767 const char *buf
, size_t count
)
769 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
771 unsigned long temperature
;
773 if (kstrtoul(buf
, 10, &temperature
))
776 if (!tz
->ops
->set_emul_temp
) {
777 mutex_lock(&tz
->lock
);
778 tz
->emul_temperature
= temperature
;
779 mutex_unlock(&tz
->lock
);
781 ret
= tz
->ops
->set_emul_temp(tz
, temperature
);
785 thermal_zone_device_update(tz
);
787 return ret
? ret
: count
;
789 static DEVICE_ATTR(emul_temp
, S_IWUSR
, NULL
, emul_temp_store
);
790 #endif/*CONFIG_THERMAL_EMULATION*/
792 static DEVICE_ATTR(type
, 0444, type_show
, NULL
);
793 static DEVICE_ATTR(temp
, 0444, temp_show
, NULL
);
794 static DEVICE_ATTR(mode
, 0644, mode_show
, mode_store
);
795 static DEVICE_ATTR(passive
, S_IRUGO
| S_IWUSR
, passive_show
, passive_store
);
796 static DEVICE_ATTR(policy
, S_IRUGO
| S_IWUSR
, policy_show
, policy_store
);
798 /* sys I/F for cooling device */
799 #define to_cooling_device(_dev) \
800 container_of(_dev, struct thermal_cooling_device, device)
803 thermal_cooling_device_type_show(struct device
*dev
,
804 struct device_attribute
*attr
, char *buf
)
806 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
808 return sprintf(buf
, "%s\n", cdev
->type
);
812 thermal_cooling_device_max_state_show(struct device
*dev
,
813 struct device_attribute
*attr
, char *buf
)
815 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
819 ret
= cdev
->ops
->get_max_state(cdev
, &state
);
822 return sprintf(buf
, "%ld\n", state
);
826 thermal_cooling_device_cur_state_show(struct device
*dev
,
827 struct device_attribute
*attr
, char *buf
)
829 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
833 ret
= cdev
->ops
->get_cur_state(cdev
, &state
);
836 return sprintf(buf
, "%ld\n", state
);
840 thermal_cooling_device_cur_state_store(struct device
*dev
,
841 struct device_attribute
*attr
,
842 const char *buf
, size_t count
)
844 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
848 if (!sscanf(buf
, "%ld\n", &state
))
854 result
= cdev
->ops
->set_cur_state(cdev
, state
);
860 static struct device_attribute dev_attr_cdev_type
=
861 __ATTR(type
, 0444, thermal_cooling_device_type_show
, NULL
);
862 static DEVICE_ATTR(max_state
, 0444,
863 thermal_cooling_device_max_state_show
, NULL
);
864 static DEVICE_ATTR(cur_state
, 0644,
865 thermal_cooling_device_cur_state_show
,
866 thermal_cooling_device_cur_state_store
);
869 thermal_cooling_device_trip_point_show(struct device
*dev
,
870 struct device_attribute
*attr
, char *buf
)
872 struct thermal_instance
*instance
;
875 container_of(attr
, struct thermal_instance
, attr
);
877 if (instance
->trip
== THERMAL_TRIPS_NONE
)
878 return sprintf(buf
, "-1\n");
880 return sprintf(buf
, "%d\n", instance
->trip
);
883 /* Device management */
886 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
887 * @tz: pointer to struct thermal_zone_device
888 * @trip: indicates which trip point the cooling devices is
889 * associated with in this thermal zone.
890 * @cdev: pointer to struct thermal_cooling_device
891 * @upper: the Maximum cooling state for this trip point.
892 * THERMAL_NO_LIMIT means no upper limit,
893 * and the cooling device can be in max_state.
894 * @lower: the Minimum cooling state can be used for this trip point.
895 * THERMAL_NO_LIMIT means no lower limit,
896 * and the cooling device can be in cooling state 0.
898 * This interface function bind a thermal cooling device to the certain trip
899 * point of a thermal zone device.
900 * This function is usually called in the thermal zone device .bind callback.
902 * Return: 0 on success, the proper error value otherwise.
904 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
906 struct thermal_cooling_device
*cdev
,
907 unsigned long upper
, unsigned long lower
)
909 struct thermal_instance
*dev
;
910 struct thermal_instance
*pos
;
911 struct thermal_zone_device
*pos1
;
912 struct thermal_cooling_device
*pos2
;
913 unsigned long max_state
;
916 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
919 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
923 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
928 if (tz
!= pos1
|| cdev
!= pos2
)
931 cdev
->ops
->get_max_state(cdev
, &max_state
);
933 /* lower default 0, upper default max_state */
934 lower
= lower
== THERMAL_NO_LIMIT
? 0 : lower
;
935 upper
= upper
== THERMAL_NO_LIMIT
? max_state
: upper
;
937 if (lower
> upper
|| upper
> max_state
)
941 kzalloc(sizeof(struct thermal_instance
), GFP_KERNEL
);
949 dev
->target
= THERMAL_NO_TARGET
;
951 result
= get_idr(&tz
->idr
, &tz
->lock
, &dev
->id
);
955 sprintf(dev
->name
, "cdev%d", dev
->id
);
957 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
961 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
962 sysfs_attr_init(&dev
->attr
.attr
);
963 dev
->attr
.attr
.name
= dev
->attr_name
;
964 dev
->attr
.attr
.mode
= 0444;
965 dev
->attr
.show
= thermal_cooling_device_trip_point_show
;
966 result
= device_create_file(&tz
->device
, &dev
->attr
);
968 goto remove_symbol_link
;
970 mutex_lock(&tz
->lock
);
971 mutex_lock(&cdev
->lock
);
972 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
)
973 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
978 list_add_tail(&dev
->tz_node
, &tz
->thermal_instances
);
979 list_add_tail(&dev
->cdev_node
, &cdev
->thermal_instances
);
981 mutex_unlock(&cdev
->lock
);
982 mutex_unlock(&tz
->lock
);
987 device_remove_file(&tz
->device
, &dev
->attr
);
989 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
991 release_idr(&tz
->idr
, &tz
->lock
, dev
->id
);
996 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device
);
999 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1001 * @tz: pointer to a struct thermal_zone_device.
1002 * @trip: indicates which trip point the cooling devices is
1003 * associated with in this thermal zone.
1004 * @cdev: pointer to a struct thermal_cooling_device.
1006 * This interface function unbind a thermal cooling device from the certain
1007 * trip point of a thermal zone device.
1008 * This function is usually called in the thermal zone device .unbind callback.
1010 * Return: 0 on success, the proper error value otherwise.
1012 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
1014 struct thermal_cooling_device
*cdev
)
1016 struct thermal_instance
*pos
, *next
;
1018 mutex_lock(&tz
->lock
);
1019 mutex_lock(&cdev
->lock
);
1020 list_for_each_entry_safe(pos
, next
, &tz
->thermal_instances
, tz_node
) {
1021 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
1022 list_del(&pos
->tz_node
);
1023 list_del(&pos
->cdev_node
);
1024 mutex_unlock(&cdev
->lock
);
1025 mutex_unlock(&tz
->lock
);
1029 mutex_unlock(&cdev
->lock
);
1030 mutex_unlock(&tz
->lock
);
1035 device_remove_file(&tz
->device
, &pos
->attr
);
1036 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
1037 release_idr(&tz
->idr
, &tz
->lock
, pos
->id
);
1041 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device
);
1043 static void thermal_release(struct device
*dev
)
1045 struct thermal_zone_device
*tz
;
1046 struct thermal_cooling_device
*cdev
;
1048 if (!strncmp(dev_name(dev
), "thermal_zone",
1049 sizeof("thermal_zone") - 1)) {
1050 tz
= to_thermal_zone(dev
);
1052 } else if(!strncmp(dev_name(dev
), "cooling_device",
1053 sizeof("cooling_device") - 1)){
1054 cdev
= to_cooling_device(dev
);
1059 static struct class thermal_class
= {
1061 .dev_release
= thermal_release
,
1065 * __thermal_cooling_device_register() - register a new thermal cooling device
1066 * @np: a pointer to a device tree node.
1067 * @type: the thermal cooling device type.
1068 * @devdata: device private data.
1069 * @ops: standard thermal cooling devices callbacks.
1071 * This interface function adds a new thermal cooling device (fan/processor/...)
1072 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1073 * to all the thermal zone devices registered at the same time.
1074 * It also gives the opportunity to link the cooling device to a device tree
1075 * node, so that it can be bound to a thermal zone created out of device tree.
1077 * Return: a pointer to the created struct thermal_cooling_device or an
1078 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1080 static struct thermal_cooling_device
*
1081 __thermal_cooling_device_register(struct device_node
*np
,
1082 char *type
, void *devdata
,
1083 const struct thermal_cooling_device_ops
*ops
)
1085 struct thermal_cooling_device
*cdev
;
1088 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
1089 return ERR_PTR(-EINVAL
);
1091 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
1092 !ops
->set_cur_state
)
1093 return ERR_PTR(-EINVAL
);
1095 cdev
= kzalloc(sizeof(struct thermal_cooling_device
), GFP_KERNEL
);
1097 return ERR_PTR(-ENOMEM
);
1099 result
= get_idr(&thermal_cdev_idr
, &thermal_idr_lock
, &cdev
->id
);
1102 return ERR_PTR(result
);
1105 strlcpy(cdev
->type
, type
? : "", sizeof(cdev
->type
));
1106 mutex_init(&cdev
->lock
);
1107 INIT_LIST_HEAD(&cdev
->thermal_instances
);
1110 cdev
->updated
= true;
1111 cdev
->device
.class = &thermal_class
;
1112 cdev
->devdata
= devdata
;
1113 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
1114 result
= device_register(&cdev
->device
);
1116 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1118 return ERR_PTR(result
);
1123 result
= device_create_file(&cdev
->device
, &dev_attr_cdev_type
);
1128 result
= device_create_file(&cdev
->device
, &dev_attr_max_state
);
1132 result
= device_create_file(&cdev
->device
, &dev_attr_cur_state
);
1136 /* Add 'this' new cdev to the global cdev list */
1137 mutex_lock(&thermal_list_lock
);
1138 list_add(&cdev
->node
, &thermal_cdev_list
);
1139 mutex_unlock(&thermal_list_lock
);
1141 /* Update binding information for 'this' new cdev */
1147 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1148 device_unregister(&cdev
->device
);
1149 return ERR_PTR(result
);
1153 * thermal_cooling_device_register() - register a new thermal cooling device
1154 * @type: the thermal cooling device type.
1155 * @devdata: device private data.
1156 * @ops: standard thermal cooling devices callbacks.
1158 * This interface function adds a new thermal cooling device (fan/processor/...)
1159 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1160 * to all the thermal zone devices registered at the same time.
1162 * Return: a pointer to the created struct thermal_cooling_device or an
1163 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1165 struct thermal_cooling_device
*
1166 thermal_cooling_device_register(char *type
, void *devdata
,
1167 const struct thermal_cooling_device_ops
*ops
)
1169 return __thermal_cooling_device_register(NULL
, type
, devdata
, ops
);
1171 EXPORT_SYMBOL_GPL(thermal_cooling_device_register
);
1174 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1175 * @np: a pointer to a device tree node.
1176 * @type: the thermal cooling device type.
1177 * @devdata: device private data.
1178 * @ops: standard thermal cooling devices callbacks.
1180 * This function will register a cooling device with device tree node reference.
1181 * This interface function adds a new thermal cooling device (fan/processor/...)
1182 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1183 * to all the thermal zone devices registered at the same time.
1185 * Return: a pointer to the created struct thermal_cooling_device or an
1186 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1188 struct thermal_cooling_device
*
1189 thermal_of_cooling_device_register(struct device_node
*np
,
1190 char *type
, void *devdata
,
1191 const struct thermal_cooling_device_ops
*ops
)
1193 return __thermal_cooling_device_register(np
, type
, devdata
, ops
);
1195 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register
);
1198 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1199 * @cdev: the thermal cooling device to remove.
1201 * thermal_cooling_device_unregister() must be called when the device is no
1204 void thermal_cooling_device_unregister(struct thermal_cooling_device
*cdev
)
1207 const struct thermal_zone_params
*tzp
;
1208 struct thermal_zone_device
*tz
;
1209 struct thermal_cooling_device
*pos
= NULL
;
1214 mutex_lock(&thermal_list_lock
);
1215 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
1219 /* thermal cooling device not found */
1220 mutex_unlock(&thermal_list_lock
);
1223 list_del(&cdev
->node
);
1225 /* Unbind all thermal zones associated with 'this' cdev */
1226 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1227 if (tz
->ops
->unbind
) {
1228 tz
->ops
->unbind(tz
, cdev
);
1232 if (!tz
->tzp
|| !tz
->tzp
->tbp
)
1236 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1237 if (tzp
->tbp
[i
].cdev
== cdev
) {
1238 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1239 tzp
->tbp
[i
].cdev
= NULL
;
1244 mutex_unlock(&thermal_list_lock
);
1247 device_remove_file(&cdev
->device
, &dev_attr_cdev_type
);
1248 device_remove_file(&cdev
->device
, &dev_attr_max_state
);
1249 device_remove_file(&cdev
->device
, &dev_attr_cur_state
);
1251 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1252 device_unregister(&cdev
->device
);
1255 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister
);
1257 void thermal_cdev_update(struct thermal_cooling_device
*cdev
)
1259 struct thermal_instance
*instance
;
1260 unsigned long target
= 0;
1262 /* cooling device is updated*/
1266 mutex_lock(&cdev
->lock
);
1267 /* Make sure cdev enters the deepest cooling state */
1268 list_for_each_entry(instance
, &cdev
->thermal_instances
, cdev_node
) {
1269 dev_dbg(&cdev
->device
, "zone%d->target=%lu\n",
1270 instance
->tz
->id
, instance
->target
);
1271 if (instance
->target
== THERMAL_NO_TARGET
)
1273 if (instance
->target
> target
)
1274 target
= instance
->target
;
1276 mutex_unlock(&cdev
->lock
);
1277 cdev
->ops
->set_cur_state(cdev
, target
);
1278 cdev
->updated
= true;
1279 dev_dbg(&cdev
->device
, "set to state %lu\n", target
);
1281 EXPORT_SYMBOL(thermal_cdev_update
);
1284 * thermal_notify_framework - Sensor drivers use this API to notify framework
1285 * @tz: thermal zone device
1286 * @trip: indicates which trip point has been crossed
1288 * This function handles the trip events from sensor drivers. It starts
1289 * throttling the cooling devices according to the policy configured.
1290 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1291 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1292 * The throttling policy is based on the configured platform data; if no
1293 * platform data is provided, this uses the step_wise throttling policy.
1295 void thermal_notify_framework(struct thermal_zone_device
*tz
, int trip
)
1297 handle_thermal_trip(tz
, trip
);
1299 EXPORT_SYMBOL_GPL(thermal_notify_framework
);
1302 * create_trip_attrs() - create attributes for trip points
1303 * @tz: the thermal zone device
1304 * @mask: Writeable trip point bitmap.
1306 * helper function to instantiate sysfs entries for every trip
1307 * point and its properties of a struct thermal_zone_device.
1309 * Return: 0 on success, the proper error value otherwise.
1311 static int create_trip_attrs(struct thermal_zone_device
*tz
, int mask
)
1314 int size
= sizeof(struct thermal_attr
) * tz
->trips
;
1316 tz
->trip_type_attrs
= kzalloc(size
, GFP_KERNEL
);
1317 if (!tz
->trip_type_attrs
)
1320 tz
->trip_temp_attrs
= kzalloc(size
, GFP_KERNEL
);
1321 if (!tz
->trip_temp_attrs
) {
1322 kfree(tz
->trip_type_attrs
);
1326 if (tz
->ops
->get_trip_hyst
) {
1327 tz
->trip_hyst_attrs
= kzalloc(size
, GFP_KERNEL
);
1328 if (!tz
->trip_hyst_attrs
) {
1329 kfree(tz
->trip_type_attrs
);
1330 kfree(tz
->trip_temp_attrs
);
1336 for (indx
= 0; indx
< tz
->trips
; indx
++) {
1337 /* create trip type attribute */
1338 snprintf(tz
->trip_type_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1339 "trip_point_%d_type", indx
);
1341 sysfs_attr_init(&tz
->trip_type_attrs
[indx
].attr
.attr
);
1342 tz
->trip_type_attrs
[indx
].attr
.attr
.name
=
1343 tz
->trip_type_attrs
[indx
].name
;
1344 tz
->trip_type_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1345 tz
->trip_type_attrs
[indx
].attr
.show
= trip_point_type_show
;
1347 device_create_file(&tz
->device
,
1348 &tz
->trip_type_attrs
[indx
].attr
);
1350 /* create trip temp attribute */
1351 snprintf(tz
->trip_temp_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1352 "trip_point_%d_temp", indx
);
1354 sysfs_attr_init(&tz
->trip_temp_attrs
[indx
].attr
.attr
);
1355 tz
->trip_temp_attrs
[indx
].attr
.attr
.name
=
1356 tz
->trip_temp_attrs
[indx
].name
;
1357 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1358 tz
->trip_temp_attrs
[indx
].attr
.show
= trip_point_temp_show
;
1359 if (mask
& (1 << indx
)) {
1360 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
1361 tz
->trip_temp_attrs
[indx
].attr
.store
=
1362 trip_point_temp_store
;
1365 device_create_file(&tz
->device
,
1366 &tz
->trip_temp_attrs
[indx
].attr
);
1368 /* create Optional trip hyst attribute */
1369 if (!tz
->ops
->get_trip_hyst
)
1371 snprintf(tz
->trip_hyst_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1372 "trip_point_%d_hyst", indx
);
1374 sysfs_attr_init(&tz
->trip_hyst_attrs
[indx
].attr
.attr
);
1375 tz
->trip_hyst_attrs
[indx
].attr
.attr
.name
=
1376 tz
->trip_hyst_attrs
[indx
].name
;
1377 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1378 tz
->trip_hyst_attrs
[indx
].attr
.show
= trip_point_hyst_show
;
1379 if (tz
->ops
->set_trip_hyst
) {
1380 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
1381 tz
->trip_hyst_attrs
[indx
].attr
.store
=
1382 trip_point_hyst_store
;
1385 device_create_file(&tz
->device
,
1386 &tz
->trip_hyst_attrs
[indx
].attr
);
1391 static void remove_trip_attrs(struct thermal_zone_device
*tz
)
1395 for (indx
= 0; indx
< tz
->trips
; indx
++) {
1396 device_remove_file(&tz
->device
,
1397 &tz
->trip_type_attrs
[indx
].attr
);
1398 device_remove_file(&tz
->device
,
1399 &tz
->trip_temp_attrs
[indx
].attr
);
1400 if (tz
->ops
->get_trip_hyst
)
1401 device_remove_file(&tz
->device
,
1402 &tz
->trip_hyst_attrs
[indx
].attr
);
1404 kfree(tz
->trip_type_attrs
);
1405 kfree(tz
->trip_temp_attrs
);
1406 kfree(tz
->trip_hyst_attrs
);
1410 * thermal_zone_device_register() - register a new thermal zone device
1411 * @type: the thermal zone device type
1412 * @trips: the number of trip points the thermal zone support
1413 * @mask: a bit string indicating the writeablility of trip points
1414 * @devdata: private device data
1415 * @ops: standard thermal zone device callbacks
1416 * @tzp: thermal zone platform parameters
1417 * @passive_delay: number of milliseconds to wait between polls when
1418 * performing passive cooling
1419 * @polling_delay: number of milliseconds to wait between polls when checking
1420 * whether trip points have been crossed (0 for interrupt
1423 * This interface function adds a new thermal zone device (sensor) to
1424 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1425 * thermal cooling devices registered at the same time.
1426 * thermal_zone_device_unregister() must be called when the device is no
1427 * longer needed. The passive cooling depends on the .get_trend() return value.
1429 * Return: a pointer to the created struct thermal_zone_device or an
1430 * in case of error, an ERR_PTR. Caller must check return value with
1431 * IS_ERR*() helpers.
1433 struct thermal_zone_device
*thermal_zone_device_register(const char *type
,
1434 int trips
, int mask
, void *devdata
,
1435 struct thermal_zone_device_ops
*ops
,
1436 const struct thermal_zone_params
*tzp
,
1437 int passive_delay
, int polling_delay
)
1439 struct thermal_zone_device
*tz
;
1440 enum thermal_trip_type trip_type
;
1445 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
1446 return ERR_PTR(-EINVAL
);
1448 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0 || mask
>> trips
)
1449 return ERR_PTR(-EINVAL
);
1452 return ERR_PTR(-EINVAL
);
1454 if (trips
> 0 && (!ops
->get_trip_type
|| !ops
->get_trip_temp
))
1455 return ERR_PTR(-EINVAL
);
1457 tz
= kzalloc(sizeof(struct thermal_zone_device
), GFP_KERNEL
);
1459 return ERR_PTR(-ENOMEM
);
1461 INIT_LIST_HEAD(&tz
->thermal_instances
);
1463 mutex_init(&tz
->lock
);
1464 result
= get_idr(&thermal_tz_idr
, &thermal_idr_lock
, &tz
->id
);
1467 return ERR_PTR(result
);
1470 strlcpy(tz
->type
, type
? : "", sizeof(tz
->type
));
1473 tz
->device
.class = &thermal_class
;
1474 tz
->devdata
= devdata
;
1476 tz
->passive_delay
= passive_delay
;
1477 tz
->polling_delay
= polling_delay
;
1479 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1480 result
= device_register(&tz
->device
);
1482 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1484 return ERR_PTR(result
);
1489 result
= device_create_file(&tz
->device
, &dev_attr_type
);
1494 result
= device_create_file(&tz
->device
, &dev_attr_temp
);
1498 if (ops
->get_mode
) {
1499 result
= device_create_file(&tz
->device
, &dev_attr_mode
);
1504 result
= create_trip_attrs(tz
, mask
);
1508 for (count
= 0; count
< trips
; count
++) {
1509 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
1510 if (trip_type
== THERMAL_TRIP_PASSIVE
)
1515 result
= device_create_file(&tz
->device
, &dev_attr_passive
);
1520 #ifdef CONFIG_THERMAL_EMULATION
1521 result
= device_create_file(&tz
->device
, &dev_attr_emul_temp
);
1525 /* Create policy attribute */
1526 result
= device_create_file(&tz
->device
, &dev_attr_policy
);
1530 /* Update 'this' zone's governor information */
1531 mutex_lock(&thermal_governor_lock
);
1534 tz
->governor
= __find_governor(tz
->tzp
->governor_name
);
1536 tz
->governor
= __find_governor(DEFAULT_THERMAL_GOVERNOR
);
1538 mutex_unlock(&thermal_governor_lock
);
1540 if (!tz
->tzp
|| !tz
->tzp
->no_hwmon
) {
1541 result
= thermal_add_hwmon_sysfs(tz
);
1546 mutex_lock(&thermal_list_lock
);
1547 list_add_tail(&tz
->node
, &thermal_tz_list
);
1548 mutex_unlock(&thermal_list_lock
);
1550 /* Bind cooling devices for this zone */
1553 INIT_DELAYED_WORK(&(tz
->poll_queue
), thermal_zone_device_check
);
1555 if (!tz
->ops
->get_temp
)
1556 thermal_zone_device_set_polling(tz
, 0);
1558 thermal_zone_device_update(tz
);
1564 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1565 device_unregister(&tz
->device
);
1566 return ERR_PTR(result
);
1568 EXPORT_SYMBOL_GPL(thermal_zone_device_register
);
1571 * thermal_device_unregister - removes the registered thermal zone device
1572 * @tz: the thermal zone device to remove
1574 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1577 const struct thermal_zone_params
*tzp
;
1578 struct thermal_cooling_device
*cdev
;
1579 struct thermal_zone_device
*pos
= NULL
;
1586 mutex_lock(&thermal_list_lock
);
1587 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1591 /* thermal zone device not found */
1592 mutex_unlock(&thermal_list_lock
);
1595 list_del(&tz
->node
);
1597 /* Unbind all cdevs associated with 'this' thermal zone */
1598 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
1599 if (tz
->ops
->unbind
) {
1600 tz
->ops
->unbind(tz
, cdev
);
1604 if (!tzp
|| !tzp
->tbp
)
1607 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1608 if (tzp
->tbp
[i
].cdev
== cdev
) {
1609 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1610 tzp
->tbp
[i
].cdev
= NULL
;
1615 mutex_unlock(&thermal_list_lock
);
1617 thermal_zone_device_set_polling(tz
, 0);
1620 device_remove_file(&tz
->device
, &dev_attr_type
);
1621 device_remove_file(&tz
->device
, &dev_attr_temp
);
1622 if (tz
->ops
->get_mode
)
1623 device_remove_file(&tz
->device
, &dev_attr_mode
);
1624 device_remove_file(&tz
->device
, &dev_attr_policy
);
1625 remove_trip_attrs(tz
);
1626 tz
->governor
= NULL
;
1628 thermal_remove_hwmon_sysfs(tz
);
1629 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1630 idr_destroy(&tz
->idr
);
1631 mutex_destroy(&tz
->lock
);
1632 device_unregister(&tz
->device
);
1635 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister
);
1638 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1639 * @name: thermal zone name to fetch the temperature
1641 * When only one zone is found with the passed name, returns a reference to it.
1643 * Return: On success returns a reference to an unique thermal zone with
1644 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1645 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1647 struct thermal_zone_device
*thermal_zone_get_zone_by_name(const char *name
)
1649 struct thermal_zone_device
*pos
= NULL
, *ref
= ERR_PTR(-EINVAL
);
1650 unsigned int found
= 0;
1655 mutex_lock(&thermal_list_lock
);
1656 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1657 if (!strnicmp(name
, pos
->type
, THERMAL_NAME_LENGTH
)) {
1661 mutex_unlock(&thermal_list_lock
);
1663 /* nothing has been found, thus an error code for it */
1665 ref
= ERR_PTR(-ENODEV
);
1667 /* Success only when an unique zone is found */
1668 ref
= ERR_PTR(-EEXIST
);
1673 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name
);
1676 static const struct genl_multicast_group thermal_event_mcgrps
[] = {
1677 { .name
= THERMAL_GENL_MCAST_GROUP_NAME
, },
1680 static struct genl_family thermal_event_genl_family
= {
1681 .id
= GENL_ID_GENERATE
,
1682 .name
= THERMAL_GENL_FAMILY_NAME
,
1683 .version
= THERMAL_GENL_VERSION
,
1684 .maxattr
= THERMAL_GENL_ATTR_MAX
,
1685 .mcgrps
= thermal_event_mcgrps
,
1686 .n_mcgrps
= ARRAY_SIZE(thermal_event_mcgrps
),
1689 int thermal_generate_netlink_event(struct thermal_zone_device
*tz
,
1692 struct sk_buff
*skb
;
1693 struct nlattr
*attr
;
1694 struct thermal_genl_event
*thermal_event
;
1698 static unsigned int thermal_event_seqnum
;
1703 /* allocate memory */
1704 size
= nla_total_size(sizeof(struct thermal_genl_event
)) +
1707 skb
= genlmsg_new(size
, GFP_ATOMIC
);
1711 /* add the genetlink message header */
1712 msg_header
= genlmsg_put(skb
, 0, thermal_event_seqnum
++,
1713 &thermal_event_genl_family
, 0,
1714 THERMAL_GENL_CMD_EVENT
);
1721 attr
= nla_reserve(skb
, THERMAL_GENL_ATTR_EVENT
,
1722 sizeof(struct thermal_genl_event
));
1729 thermal_event
= nla_data(attr
);
1730 if (!thermal_event
) {
1735 memset(thermal_event
, 0, sizeof(struct thermal_genl_event
));
1737 thermal_event
->orig
= tz
->id
;
1738 thermal_event
->event
= event
;
1740 /* send multicast genetlink message */
1741 result
= genlmsg_end(skb
, msg_header
);
1747 result
= genlmsg_multicast(&thermal_event_genl_family
, skb
, 0,
1750 dev_err(&tz
->device
, "Failed to send netlink event:%d", result
);
1754 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event
);
1756 static int genetlink_init(void)
1758 return genl_register_family(&thermal_event_genl_family
);
1761 static void genetlink_exit(void)
1763 genl_unregister_family(&thermal_event_genl_family
);
1765 #else /* !CONFIG_NET */
1766 static inline int genetlink_init(void) { return 0; }
1767 static inline void genetlink_exit(void) {}
1768 #endif /* !CONFIG_NET */
1770 static int __init
thermal_register_governors(void)
1774 result
= thermal_gov_step_wise_register();
1778 result
= thermal_gov_fair_share_register();
1782 return thermal_gov_user_space_register();
1785 static void thermal_unregister_governors(void)
1787 thermal_gov_step_wise_unregister();
1788 thermal_gov_fair_share_unregister();
1789 thermal_gov_user_space_unregister();
1792 static int __init
thermal_init(void)
1796 result
= thermal_register_governors();
1800 result
= class_register(&thermal_class
);
1802 goto unregister_governors
;
1804 result
= genetlink_init();
1806 goto unregister_class
;
1808 result
= of_parse_thermal_zones();
1816 unregister_governors
:
1817 thermal_unregister_governors();
1819 class_unregister(&thermal_class
);
1821 idr_destroy(&thermal_tz_idr
);
1822 idr_destroy(&thermal_cdev_idr
);
1823 mutex_destroy(&thermal_idr_lock
);
1824 mutex_destroy(&thermal_list_lock
);
1825 mutex_destroy(&thermal_governor_lock
);
1829 static void __exit
thermal_exit(void)
1831 of_thermal_destroy_zones();
1833 class_unregister(&thermal_class
);
1834 thermal_unregister_governors();
1835 idr_destroy(&thermal_tz_idr
);
1836 idr_destroy(&thermal_cdev_idr
);
1837 mutex_destroy(&thermal_idr_lock
);
1838 mutex_destroy(&thermal_list_lock
);
1839 mutex_destroy(&thermal_governor_lock
);
1842 fs_initcall(thermal_init
);
1843 module_exit(thermal_exit
);