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/spinlock.h>
36 #include <linux/reboot.h>
37 #include <net/netlink.h>
38 #include <net/genetlink.h>
40 #include "thermal_core.h"
42 MODULE_AUTHOR("Zhang Rui");
43 MODULE_DESCRIPTION("Generic thermal management sysfs support");
44 MODULE_LICENSE("GPL");
46 static DEFINE_IDR(thermal_tz_idr
);
47 static DEFINE_IDR(thermal_cdev_idr
);
48 static DEFINE_MUTEX(thermal_idr_lock
);
50 static LIST_HEAD(thermal_tz_list
);
51 static LIST_HEAD(thermal_cdev_list
);
52 static LIST_HEAD(thermal_governor_list
);
54 static DEFINE_MUTEX(thermal_list_lock
);
55 static DEFINE_MUTEX(thermal_governor_lock
);
57 static struct thermal_governor
*__find_governor(const char *name
)
59 struct thermal_governor
*pos
;
61 list_for_each_entry(pos
, &thermal_governor_list
, governor_list
)
62 if (!strnicmp(name
, pos
->name
, THERMAL_NAME_LENGTH
))
68 int thermal_register_governor(struct thermal_governor
*governor
)
72 struct thermal_zone_device
*pos
;
77 mutex_lock(&thermal_governor_lock
);
80 if (__find_governor(governor
->name
) == NULL
) {
82 list_add(&governor
->governor_list
, &thermal_governor_list
);
85 mutex_lock(&thermal_list_lock
);
87 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
91 name
= pos
->tzp
->governor_name
;
93 name
= DEFAULT_THERMAL_GOVERNOR
;
94 if (!strnicmp(name
, governor
->name
, THERMAL_NAME_LENGTH
))
95 pos
->governor
= governor
;
98 mutex_unlock(&thermal_list_lock
);
99 mutex_unlock(&thermal_governor_lock
);
103 EXPORT_SYMBOL_GPL(thermal_register_governor
);
105 void thermal_unregister_governor(struct thermal_governor
*governor
)
107 struct thermal_zone_device
*pos
;
112 mutex_lock(&thermal_governor_lock
);
114 if (__find_governor(governor
->name
) == NULL
)
117 mutex_lock(&thermal_list_lock
);
119 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
120 if (!strnicmp(pos
->governor
->name
, governor
->name
,
121 THERMAL_NAME_LENGTH
))
122 pos
->governor
= NULL
;
125 mutex_unlock(&thermal_list_lock
);
126 list_del(&governor
->governor_list
);
128 mutex_unlock(&thermal_governor_lock
);
131 EXPORT_SYMBOL_GPL(thermal_unregister_governor
);
133 static int get_idr(struct idr
*idr
, struct mutex
*lock
, int *id
)
138 if (unlikely(idr_pre_get(idr
, GFP_KERNEL
) == 0))
143 err
= idr_get_new(idr
, NULL
, id
);
146 if (unlikely(err
== -EAGAIN
))
148 else if (unlikely(err
))
151 *id
= *id
& MAX_IDR_MASK
;
155 static void release_idr(struct idr
*idr
, struct mutex
*lock
, int id
)
164 int get_tz_trend(struct thermal_zone_device
*tz
, int trip
)
166 enum thermal_trend trend
;
168 if (!tz
->ops
->get_trend
|| tz
->ops
->get_trend(tz
, trip
, &trend
)) {
169 if (tz
->temperature
> tz
->last_temperature
)
170 trend
= THERMAL_TREND_RAISING
;
171 else if (tz
->temperature
< tz
->last_temperature
)
172 trend
= THERMAL_TREND_DROPPING
;
174 trend
= THERMAL_TREND_STABLE
;
179 EXPORT_SYMBOL(get_tz_trend
);
181 struct thermal_instance
*get_thermal_instance(struct thermal_zone_device
*tz
,
182 struct thermal_cooling_device
*cdev
, int trip
)
184 struct thermal_instance
*pos
= NULL
;
185 struct thermal_instance
*target_instance
= NULL
;
187 mutex_lock(&tz
->lock
);
188 mutex_lock(&cdev
->lock
);
190 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
) {
191 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
192 target_instance
= pos
;
197 mutex_unlock(&cdev
->lock
);
198 mutex_unlock(&tz
->lock
);
200 return target_instance
;
202 EXPORT_SYMBOL(get_thermal_instance
);
204 static void print_bind_err_msg(struct thermal_zone_device
*tz
,
205 struct thermal_cooling_device
*cdev
, int ret
)
207 dev_err(&tz
->device
, "binding zone %s with cdev %s failed:%d\n",
208 tz
->type
, cdev
->type
, ret
);
211 static void __bind(struct thermal_zone_device
*tz
, int mask
,
212 struct thermal_cooling_device
*cdev
)
216 for (i
= 0; i
< tz
->trips
; i
++) {
217 if (mask
& (1 << i
)) {
218 ret
= thermal_zone_bind_cooling_device(tz
, i
, cdev
,
219 THERMAL_NO_LIMIT
, THERMAL_NO_LIMIT
);
221 print_bind_err_msg(tz
, cdev
, ret
);
226 static void __unbind(struct thermal_zone_device
*tz
, int mask
,
227 struct thermal_cooling_device
*cdev
)
231 for (i
= 0; i
< tz
->trips
; i
++)
233 thermal_zone_unbind_cooling_device(tz
, i
, cdev
);
236 static void bind_cdev(struct thermal_cooling_device
*cdev
)
239 const struct thermal_zone_params
*tzp
;
240 struct thermal_zone_device
*pos
= NULL
;
242 mutex_lock(&thermal_list_lock
);
244 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
245 if (!pos
->tzp
&& !pos
->ops
->bind
)
248 if (!pos
->tzp
&& pos
->ops
->bind
) {
249 ret
= pos
->ops
->bind(pos
, cdev
);
251 print_bind_err_msg(pos
, cdev
, ret
);
255 if (!tzp
|| !tzp
->tbp
)
258 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
259 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
261 if (tzp
->tbp
[i
].match(pos
, cdev
))
263 tzp
->tbp
[i
].cdev
= cdev
;
264 __bind(pos
, tzp
->tbp
[i
].trip_mask
, cdev
);
268 mutex_unlock(&thermal_list_lock
);
271 static void bind_tz(struct thermal_zone_device
*tz
)
274 struct thermal_cooling_device
*pos
= NULL
;
275 const struct thermal_zone_params
*tzp
= tz
->tzp
;
277 if (!tzp
&& !tz
->ops
->bind
)
280 mutex_lock(&thermal_list_lock
);
282 /* If there is no platform data, try to use ops->bind */
283 if (!tzp
&& tz
->ops
->bind
) {
284 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
285 ret
= tz
->ops
->bind(tz
, pos
);
287 print_bind_err_msg(tz
, pos
, ret
);
292 if (!tzp
|| !tzp
->tbp
)
295 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
296 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
297 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
299 if (tzp
->tbp
[i
].match(tz
, pos
))
301 tzp
->tbp
[i
].cdev
= pos
;
302 __bind(tz
, tzp
->tbp
[i
].trip_mask
, pos
);
306 mutex_unlock(&thermal_list_lock
);
309 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
313 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
314 round_jiffies(msecs_to_jiffies(delay
)));
316 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
317 msecs_to_jiffies(delay
));
319 cancel_delayed_work(&tz
->poll_queue
);
322 static void monitor_thermal_zone(struct thermal_zone_device
*tz
)
324 mutex_lock(&tz
->lock
);
327 thermal_zone_device_set_polling(tz
, tz
->passive_delay
);
328 else if (tz
->polling_delay
)
329 thermal_zone_device_set_polling(tz
, tz
->polling_delay
);
331 thermal_zone_device_set_polling(tz
, 0);
333 mutex_unlock(&tz
->lock
);
336 static void handle_non_critical_trips(struct thermal_zone_device
*tz
,
337 int trip
, enum thermal_trip_type trip_type
)
340 tz
->governor
->throttle(tz
, trip
);
343 static void handle_critical_trips(struct thermal_zone_device
*tz
,
344 int trip
, enum thermal_trip_type trip_type
)
348 tz
->ops
->get_trip_temp(tz
, trip
, &trip_temp
);
350 /* If we have not crossed the trip_temp, we do not care. */
351 if (tz
->temperature
< trip_temp
)
355 tz
->ops
->notify(tz
, trip
, trip_type
);
357 if (trip_type
== THERMAL_TRIP_CRITICAL
) {
358 pr_emerg("Critical temperature reached(%d C),shutting down\n",
359 tz
->temperature
/ 1000);
360 orderly_poweroff(true);
364 static void handle_thermal_trip(struct thermal_zone_device
*tz
, int trip
)
366 enum thermal_trip_type type
;
368 tz
->ops
->get_trip_type(tz
, trip
, &type
);
370 if (type
== THERMAL_TRIP_CRITICAL
|| type
== THERMAL_TRIP_HOT
)
371 handle_critical_trips(tz
, trip
, type
);
373 handle_non_critical_trips(tz
, trip
, type
);
375 * Alright, we handled this trip successfully.
376 * So, start monitoring again.
378 monitor_thermal_zone(tz
);
381 static void update_temperature(struct thermal_zone_device
*tz
)
386 mutex_lock(&tz
->lock
);
388 ret
= tz
->ops
->get_temp(tz
, &temp
);
390 pr_warn("failed to read out thermal zone %d\n", tz
->id
);
394 tz
->last_temperature
= tz
->temperature
;
395 tz
->temperature
= temp
;
398 mutex_unlock(&tz
->lock
);
401 void thermal_zone_device_update(struct thermal_zone_device
*tz
)
405 update_temperature(tz
);
407 for (count
= 0; count
< tz
->trips
; count
++)
408 handle_thermal_trip(tz
, count
);
410 EXPORT_SYMBOL(thermal_zone_device_update
);
412 static void thermal_zone_device_check(struct work_struct
*work
)
414 struct thermal_zone_device
*tz
= container_of(work
, struct
417 thermal_zone_device_update(tz
);
420 /* sys I/F for thermal zone */
422 #define to_thermal_zone(_dev) \
423 container_of(_dev, struct thermal_zone_device, device)
426 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
428 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
430 return sprintf(buf
, "%s\n", tz
->type
);
434 temp_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
436 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
440 if (!tz
->ops
->get_temp
)
443 ret
= tz
->ops
->get_temp(tz
, &temperature
);
448 return sprintf(buf
, "%ld\n", temperature
);
452 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
454 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
455 enum thermal_device_mode mode
;
458 if (!tz
->ops
->get_mode
)
461 result
= tz
->ops
->get_mode(tz
, &mode
);
465 return sprintf(buf
, "%s\n", mode
== THERMAL_DEVICE_ENABLED
? "enabled"
470 mode_store(struct device
*dev
, struct device_attribute
*attr
,
471 const char *buf
, size_t count
)
473 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
476 if (!tz
->ops
->set_mode
)
479 if (!strncmp(buf
, "enabled", sizeof("enabled") - 1))
480 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_ENABLED
);
481 else if (!strncmp(buf
, "disabled", sizeof("disabled") - 1))
482 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_DISABLED
);
493 trip_point_type_show(struct device
*dev
, struct device_attribute
*attr
,
496 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
497 enum thermal_trip_type type
;
500 if (!tz
->ops
->get_trip_type
)
503 if (!sscanf(attr
->attr
.name
, "trip_point_%d_type", &trip
))
506 result
= tz
->ops
->get_trip_type(tz
, trip
, &type
);
511 case THERMAL_TRIP_CRITICAL
:
512 return sprintf(buf
, "critical\n");
513 case THERMAL_TRIP_HOT
:
514 return sprintf(buf
, "hot\n");
515 case THERMAL_TRIP_PASSIVE
:
516 return sprintf(buf
, "passive\n");
517 case THERMAL_TRIP_ACTIVE
:
518 return sprintf(buf
, "active\n");
520 return sprintf(buf
, "unknown\n");
525 trip_point_temp_store(struct device
*dev
, struct device_attribute
*attr
,
526 const char *buf
, size_t count
)
528 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
530 unsigned long temperature
;
532 if (!tz
->ops
->set_trip_temp
)
535 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
538 if (kstrtoul(buf
, 10, &temperature
))
541 ret
= tz
->ops
->set_trip_temp(tz
, trip
, temperature
);
543 return ret
? ret
: count
;
547 trip_point_temp_show(struct device
*dev
, struct device_attribute
*attr
,
550 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
554 if (!tz
->ops
->get_trip_temp
)
557 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
560 ret
= tz
->ops
->get_trip_temp(tz
, trip
, &temperature
);
565 return sprintf(buf
, "%ld\n", temperature
);
569 trip_point_hyst_store(struct device
*dev
, struct device_attribute
*attr
,
570 const char *buf
, size_t count
)
572 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
574 unsigned long temperature
;
576 if (!tz
->ops
->set_trip_hyst
)
579 if (!sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
))
582 if (kstrtoul(buf
, 10, &temperature
))
586 * We are not doing any check on the 'temperature' value
587 * here. The driver implementing 'set_trip_hyst' has to
590 ret
= tz
->ops
->set_trip_hyst(tz
, trip
, temperature
);
592 return ret
? ret
: count
;
596 trip_point_hyst_show(struct device
*dev
, struct device_attribute
*attr
,
599 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
601 unsigned long temperature
;
603 if (!tz
->ops
->get_trip_hyst
)
606 if (!sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
))
609 ret
= tz
->ops
->get_trip_hyst(tz
, trip
, &temperature
);
611 return ret
? ret
: sprintf(buf
, "%ld\n", temperature
);
615 passive_store(struct device
*dev
, struct device_attribute
*attr
,
616 const char *buf
, size_t count
)
618 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
619 struct thermal_cooling_device
*cdev
= NULL
;
622 if (!sscanf(buf
, "%d\n", &state
))
625 /* sanity check: values below 1000 millicelcius don't make sense
626 * and can cause the system to go into a thermal heart attack
628 if (state
&& state
< 1000)
631 if (state
&& !tz
->forced_passive
) {
632 mutex_lock(&thermal_list_lock
);
633 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
634 if (!strncmp("Processor", cdev
->type
,
635 sizeof("Processor")))
636 thermal_zone_bind_cooling_device(tz
,
637 THERMAL_TRIPS_NONE
, cdev
,
641 mutex_unlock(&thermal_list_lock
);
642 if (!tz
->passive_delay
)
643 tz
->passive_delay
= 1000;
644 } else if (!state
&& tz
->forced_passive
) {
645 mutex_lock(&thermal_list_lock
);
646 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
647 if (!strncmp("Processor", cdev
->type
,
648 sizeof("Processor")))
649 thermal_zone_unbind_cooling_device(tz
,
653 mutex_unlock(&thermal_list_lock
);
654 tz
->passive_delay
= 0;
657 tz
->forced_passive
= state
;
659 thermal_zone_device_update(tz
);
665 passive_show(struct device
*dev
, struct device_attribute
*attr
,
668 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
670 return sprintf(buf
, "%d\n", tz
->forced_passive
);
674 policy_store(struct device
*dev
, struct device_attribute
*attr
,
675 const char *buf
, size_t count
)
678 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
679 struct thermal_governor
*gov
;
681 mutex_lock(&thermal_governor_lock
);
683 gov
= __find_governor(buf
);
691 mutex_unlock(&thermal_governor_lock
);
696 policy_show(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
698 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
700 return sprintf(buf
, "%s\n", tz
->governor
->name
);
703 static DEVICE_ATTR(type
, 0444, type_show
, NULL
);
704 static DEVICE_ATTR(temp
, 0444, temp_show
, NULL
);
705 static DEVICE_ATTR(mode
, 0644, mode_show
, mode_store
);
706 static DEVICE_ATTR(passive
, S_IRUGO
| S_IWUSR
, passive_show
, passive_store
);
707 static DEVICE_ATTR(policy
, S_IRUGO
| S_IWUSR
, policy_show
, policy_store
);
709 /* sys I/F for cooling device */
710 #define to_cooling_device(_dev) \
711 container_of(_dev, struct thermal_cooling_device, device)
714 thermal_cooling_device_type_show(struct device
*dev
,
715 struct device_attribute
*attr
, char *buf
)
717 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
719 return sprintf(buf
, "%s\n", cdev
->type
);
723 thermal_cooling_device_max_state_show(struct device
*dev
,
724 struct device_attribute
*attr
, char *buf
)
726 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
730 ret
= cdev
->ops
->get_max_state(cdev
, &state
);
733 return sprintf(buf
, "%ld\n", state
);
737 thermal_cooling_device_cur_state_show(struct device
*dev
,
738 struct device_attribute
*attr
, char *buf
)
740 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
744 ret
= cdev
->ops
->get_cur_state(cdev
, &state
);
747 return sprintf(buf
, "%ld\n", state
);
751 thermal_cooling_device_cur_state_store(struct device
*dev
,
752 struct device_attribute
*attr
,
753 const char *buf
, size_t count
)
755 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
759 if (!sscanf(buf
, "%ld\n", &state
))
765 result
= cdev
->ops
->set_cur_state(cdev
, state
);
771 static struct device_attribute dev_attr_cdev_type
=
772 __ATTR(type
, 0444, thermal_cooling_device_type_show
, NULL
);
773 static DEVICE_ATTR(max_state
, 0444,
774 thermal_cooling_device_max_state_show
, NULL
);
775 static DEVICE_ATTR(cur_state
, 0644,
776 thermal_cooling_device_cur_state_show
,
777 thermal_cooling_device_cur_state_store
);
780 thermal_cooling_device_trip_point_show(struct device
*dev
,
781 struct device_attribute
*attr
, char *buf
)
783 struct thermal_instance
*instance
;
786 container_of(attr
, struct thermal_instance
, attr
);
788 if (instance
->trip
== THERMAL_TRIPS_NONE
)
789 return sprintf(buf
, "-1\n");
791 return sprintf(buf
, "%d\n", instance
->trip
);
794 /* Device management */
796 #if defined(CONFIG_THERMAL_HWMON)
799 #include <linux/hwmon.h>
801 /* thermal zone devices with the same type share one hwmon device */
802 struct thermal_hwmon_device
{
803 char type
[THERMAL_NAME_LENGTH
];
804 struct device
*device
;
806 struct list_head tz_list
;
807 struct list_head node
;
810 struct thermal_hwmon_attr
{
811 struct device_attribute attr
;
815 /* one temperature input for each thermal zone */
816 struct thermal_hwmon_temp
{
817 struct list_head hwmon_node
;
818 struct thermal_zone_device
*tz
;
819 struct thermal_hwmon_attr temp_input
; /* hwmon sys attr */
820 struct thermal_hwmon_attr temp_crit
; /* hwmon sys attr */
823 static LIST_HEAD(thermal_hwmon_list
);
826 name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
828 struct thermal_hwmon_device
*hwmon
= dev_get_drvdata(dev
);
829 return sprintf(buf
, "%s\n", hwmon
->type
);
831 static DEVICE_ATTR(name
, 0444, name_show
, NULL
);
834 temp_input_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
838 struct thermal_hwmon_attr
*hwmon_attr
839 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
840 struct thermal_hwmon_temp
*temp
841 = container_of(hwmon_attr
, struct thermal_hwmon_temp
,
843 struct thermal_zone_device
*tz
= temp
->tz
;
845 ret
= tz
->ops
->get_temp(tz
, &temperature
);
850 return sprintf(buf
, "%ld\n", temperature
);
854 temp_crit_show(struct device
*dev
, struct device_attribute
*attr
,
857 struct thermal_hwmon_attr
*hwmon_attr
858 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
859 struct thermal_hwmon_temp
*temp
860 = container_of(hwmon_attr
, struct thermal_hwmon_temp
,
862 struct thermal_zone_device
*tz
= temp
->tz
;
866 ret
= tz
->ops
->get_trip_temp(tz
, 0, &temperature
);
870 return sprintf(buf
, "%ld\n", temperature
);
874 static struct thermal_hwmon_device
*
875 thermal_hwmon_lookup_by_type(const struct thermal_zone_device
*tz
)
877 struct thermal_hwmon_device
*hwmon
;
879 mutex_lock(&thermal_list_lock
);
880 list_for_each_entry(hwmon
, &thermal_hwmon_list
, node
)
881 if (!strcmp(hwmon
->type
, tz
->type
)) {
882 mutex_unlock(&thermal_list_lock
);
885 mutex_unlock(&thermal_list_lock
);
890 /* Find the temperature input matching a given thermal zone */
891 static struct thermal_hwmon_temp
*
892 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device
*hwmon
,
893 const struct thermal_zone_device
*tz
)
895 struct thermal_hwmon_temp
*temp
;
897 mutex_lock(&thermal_list_lock
);
898 list_for_each_entry(temp
, &hwmon
->tz_list
, hwmon_node
)
899 if (temp
->tz
== tz
) {
900 mutex_unlock(&thermal_list_lock
);
903 mutex_unlock(&thermal_list_lock
);
909 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
911 struct thermal_hwmon_device
*hwmon
;
912 struct thermal_hwmon_temp
*temp
;
913 int new_hwmon_device
= 1;
916 hwmon
= thermal_hwmon_lookup_by_type(tz
);
918 new_hwmon_device
= 0;
919 goto register_sys_interface
;
922 hwmon
= kzalloc(sizeof(struct thermal_hwmon_device
), GFP_KERNEL
);
926 INIT_LIST_HEAD(&hwmon
->tz_list
);
927 strlcpy(hwmon
->type
, tz
->type
, THERMAL_NAME_LENGTH
);
928 hwmon
->device
= hwmon_device_register(NULL
);
929 if (IS_ERR(hwmon
->device
)) {
930 result
= PTR_ERR(hwmon
->device
);
933 dev_set_drvdata(hwmon
->device
, hwmon
);
934 result
= device_create_file(hwmon
->device
, &dev_attr_name
);
938 register_sys_interface
:
939 temp
= kzalloc(sizeof(struct thermal_hwmon_temp
), GFP_KERNEL
);
942 goto unregister_name
;
948 snprintf(temp
->temp_input
.name
, sizeof(temp
->temp_input
.name
),
949 "temp%d_input", hwmon
->count
);
950 temp
->temp_input
.attr
.attr
.name
= temp
->temp_input
.name
;
951 temp
->temp_input
.attr
.attr
.mode
= 0444;
952 temp
->temp_input
.attr
.show
= temp_input_show
;
953 sysfs_attr_init(&temp
->temp_input
.attr
.attr
);
954 result
= device_create_file(hwmon
->device
, &temp
->temp_input
.attr
);
958 if (tz
->ops
->get_crit_temp
) {
959 unsigned long temperature
;
960 if (!tz
->ops
->get_crit_temp(tz
, &temperature
)) {
961 snprintf(temp
->temp_crit
.name
,
962 sizeof(temp
->temp_crit
.name
),
963 "temp%d_crit", hwmon
->count
);
964 temp
->temp_crit
.attr
.attr
.name
= temp
->temp_crit
.name
;
965 temp
->temp_crit
.attr
.attr
.mode
= 0444;
966 temp
->temp_crit
.attr
.show
= temp_crit_show
;
967 sysfs_attr_init(&temp
->temp_crit
.attr
.attr
);
968 result
= device_create_file(hwmon
->device
,
969 &temp
->temp_crit
.attr
);
971 goto unregister_input
;
975 mutex_lock(&thermal_list_lock
);
976 if (new_hwmon_device
)
977 list_add_tail(&hwmon
->node
, &thermal_hwmon_list
);
978 list_add_tail(&temp
->hwmon_node
, &hwmon
->tz_list
);
979 mutex_unlock(&thermal_list_lock
);
984 device_remove_file(hwmon
->device
, &temp
->temp_input
.attr
);
988 if (new_hwmon_device
) {
989 device_remove_file(hwmon
->device
, &dev_attr_name
);
990 hwmon_device_unregister(hwmon
->device
);
993 if (new_hwmon_device
)
1000 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
1002 struct thermal_hwmon_device
*hwmon
;
1003 struct thermal_hwmon_temp
*temp
;
1005 hwmon
= thermal_hwmon_lookup_by_type(tz
);
1006 if (unlikely(!hwmon
)) {
1007 /* Should never happen... */
1008 dev_dbg(&tz
->device
, "hwmon device lookup failed!\n");
1012 temp
= thermal_hwmon_lookup_temp(hwmon
, tz
);
1013 if (unlikely(!temp
)) {
1014 /* Should never happen... */
1015 dev_dbg(&tz
->device
, "temperature input lookup failed!\n");
1019 device_remove_file(hwmon
->device
, &temp
->temp_input
.attr
);
1020 if (tz
->ops
->get_crit_temp
)
1021 device_remove_file(hwmon
->device
, &temp
->temp_crit
.attr
);
1023 mutex_lock(&thermal_list_lock
);
1024 list_del(&temp
->hwmon_node
);
1026 if (!list_empty(&hwmon
->tz_list
)) {
1027 mutex_unlock(&thermal_list_lock
);
1030 list_del(&hwmon
->node
);
1031 mutex_unlock(&thermal_list_lock
);
1033 device_remove_file(hwmon
->device
, &dev_attr_name
);
1034 hwmon_device_unregister(hwmon
->device
);
1039 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
1045 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
1051 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
1052 * @tz: thermal zone device
1053 * @trip: indicates which trip point the cooling devices is
1054 * associated with in this thermal zone.
1055 * @cdev: thermal cooling device
1057 * This function is usually called in the thermal zone device .bind callback.
1059 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
1061 struct thermal_cooling_device
*cdev
,
1062 unsigned long upper
, unsigned long lower
)
1064 struct thermal_instance
*dev
;
1065 struct thermal_instance
*pos
;
1066 struct thermal_zone_device
*pos1
;
1067 struct thermal_cooling_device
*pos2
;
1068 unsigned long max_state
;
1071 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
1074 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
1078 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
1083 if (tz
!= pos1
|| cdev
!= pos2
)
1086 cdev
->ops
->get_max_state(cdev
, &max_state
);
1088 /* lower default 0, upper default max_state */
1089 lower
= lower
== THERMAL_NO_LIMIT
? 0 : lower
;
1090 upper
= upper
== THERMAL_NO_LIMIT
? max_state
: upper
;
1092 if (lower
> upper
|| upper
> max_state
)
1096 kzalloc(sizeof(struct thermal_instance
), GFP_KERNEL
);
1104 dev
->target
= THERMAL_NO_TARGET
;
1106 result
= get_idr(&tz
->idr
, &tz
->lock
, &dev
->id
);
1110 sprintf(dev
->name
, "cdev%d", dev
->id
);
1112 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
1116 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
1117 sysfs_attr_init(&dev
->attr
.attr
);
1118 dev
->attr
.attr
.name
= dev
->attr_name
;
1119 dev
->attr
.attr
.mode
= 0444;
1120 dev
->attr
.show
= thermal_cooling_device_trip_point_show
;
1121 result
= device_create_file(&tz
->device
, &dev
->attr
);
1123 goto remove_symbol_link
;
1125 mutex_lock(&tz
->lock
);
1126 mutex_lock(&cdev
->lock
);
1127 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
)
1128 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
1133 list_add_tail(&dev
->tz_node
, &tz
->thermal_instances
);
1134 list_add_tail(&dev
->cdev_node
, &cdev
->thermal_instances
);
1136 mutex_unlock(&cdev
->lock
);
1137 mutex_unlock(&tz
->lock
);
1142 device_remove_file(&tz
->device
, &dev
->attr
);
1144 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
1146 release_idr(&tz
->idr
, &tz
->lock
, dev
->id
);
1151 EXPORT_SYMBOL(thermal_zone_bind_cooling_device
);
1154 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
1155 * @tz: thermal zone device
1156 * @trip: indicates which trip point the cooling devices is
1157 * associated with in this thermal zone.
1158 * @cdev: thermal cooling device
1160 * This function is usually called in the thermal zone device .unbind callback.
1162 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
1164 struct thermal_cooling_device
*cdev
)
1166 struct thermal_instance
*pos
, *next
;
1168 mutex_lock(&tz
->lock
);
1169 mutex_lock(&cdev
->lock
);
1170 list_for_each_entry_safe(pos
, next
, &tz
->thermal_instances
, tz_node
) {
1171 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
1172 list_del(&pos
->tz_node
);
1173 list_del(&pos
->cdev_node
);
1174 mutex_unlock(&cdev
->lock
);
1175 mutex_unlock(&tz
->lock
);
1179 mutex_unlock(&cdev
->lock
);
1180 mutex_unlock(&tz
->lock
);
1185 device_remove_file(&tz
->device
, &pos
->attr
);
1186 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
1187 release_idr(&tz
->idr
, &tz
->lock
, pos
->id
);
1191 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device
);
1193 static void thermal_release(struct device
*dev
)
1195 struct thermal_zone_device
*tz
;
1196 struct thermal_cooling_device
*cdev
;
1198 if (!strncmp(dev_name(dev
), "thermal_zone",
1199 sizeof("thermal_zone") - 1)) {
1200 tz
= to_thermal_zone(dev
);
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 * @type: the thermal cooling device type.
1216 * @devdata: device private data.
1217 * @ops: standard thermal cooling devices callbacks.
1219 struct thermal_cooling_device
*
1220 thermal_cooling_device_register(char *type
, void *devdata
,
1221 const struct thermal_cooling_device_ops
*ops
)
1223 struct thermal_cooling_device
*cdev
;
1226 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
1227 return ERR_PTR(-EINVAL
);
1229 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
1230 !ops
->set_cur_state
)
1231 return ERR_PTR(-EINVAL
);
1233 cdev
= kzalloc(sizeof(struct thermal_cooling_device
), GFP_KERNEL
);
1235 return ERR_PTR(-ENOMEM
);
1237 result
= get_idr(&thermal_cdev_idr
, &thermal_idr_lock
, &cdev
->id
);
1240 return ERR_PTR(result
);
1243 strcpy(cdev
->type
, type
? : "");
1244 mutex_init(&cdev
->lock
);
1245 INIT_LIST_HEAD(&cdev
->thermal_instances
);
1247 cdev
->updated
= true;
1248 cdev
->device
.class = &thermal_class
;
1249 cdev
->devdata
= devdata
;
1250 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
1251 result
= device_register(&cdev
->device
);
1253 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1255 return ERR_PTR(result
);
1260 result
= device_create_file(&cdev
->device
, &dev_attr_cdev_type
);
1265 result
= device_create_file(&cdev
->device
, &dev_attr_max_state
);
1269 result
= device_create_file(&cdev
->device
, &dev_attr_cur_state
);
1273 /* Add 'this' new cdev to the global cdev list */
1274 mutex_lock(&thermal_list_lock
);
1275 list_add(&cdev
->node
, &thermal_cdev_list
);
1276 mutex_unlock(&thermal_list_lock
);
1278 /* Update binding information for 'this' new cdev */
1284 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1285 device_unregister(&cdev
->device
);
1286 return ERR_PTR(result
);
1288 EXPORT_SYMBOL(thermal_cooling_device_register
);
1291 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1292 * @cdev: the thermal cooling device to remove.
1294 * thermal_cooling_device_unregister() must be called when the device is no
1297 void thermal_cooling_device_unregister(struct thermal_cooling_device
*cdev
)
1300 const struct thermal_zone_params
*tzp
;
1301 struct thermal_zone_device
*tz
;
1302 struct thermal_cooling_device
*pos
= NULL
;
1307 mutex_lock(&thermal_list_lock
);
1308 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
1312 /* thermal cooling device not found */
1313 mutex_unlock(&thermal_list_lock
);
1316 list_del(&cdev
->node
);
1318 /* Unbind all thermal zones associated with 'this' cdev */
1319 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1320 if (tz
->ops
->unbind
) {
1321 tz
->ops
->unbind(tz
, cdev
);
1325 if (!tz
->tzp
|| !tz
->tzp
->tbp
)
1329 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1330 if (tzp
->tbp
[i
].cdev
== cdev
) {
1331 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1332 tzp
->tbp
[i
].cdev
= NULL
;
1337 mutex_unlock(&thermal_list_lock
);
1340 device_remove_file(&cdev
->device
, &dev_attr_cdev_type
);
1341 device_remove_file(&cdev
->device
, &dev_attr_max_state
);
1342 device_remove_file(&cdev
->device
, &dev_attr_cur_state
);
1344 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1345 device_unregister(&cdev
->device
);
1348 EXPORT_SYMBOL(thermal_cooling_device_unregister
);
1350 void thermal_cdev_update(struct thermal_cooling_device
*cdev
)
1352 struct thermal_instance
*instance
;
1353 unsigned long target
= 0;
1355 /* cooling device is updated*/
1359 mutex_lock(&cdev
->lock
);
1360 /* Make sure cdev enters the deepest cooling state */
1361 list_for_each_entry(instance
, &cdev
->thermal_instances
, cdev_node
) {
1362 if (instance
->target
== THERMAL_NO_TARGET
)
1364 if (instance
->target
> target
)
1365 target
= instance
->target
;
1367 mutex_unlock(&cdev
->lock
);
1368 cdev
->ops
->set_cur_state(cdev
, target
);
1369 cdev
->updated
= true;
1371 EXPORT_SYMBOL(thermal_cdev_update
);
1374 * notify_thermal_framework - Sensor drivers use this API to notify framework
1375 * @tz: thermal zone device
1376 * @trip: indicates which trip point has been crossed
1378 * This function handles the trip events from sensor drivers. It starts
1379 * throttling the cooling devices according to the policy configured.
1380 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1381 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1382 * The throttling policy is based on the configured platform data; if no
1383 * platform data is provided, this uses the step_wise throttling policy.
1385 void notify_thermal_framework(struct thermal_zone_device
*tz
, int trip
)
1387 handle_thermal_trip(tz
, trip
);
1389 EXPORT_SYMBOL(notify_thermal_framework
);
1392 * create_trip_attrs - create attributes for trip points
1393 * @tz: the thermal zone device
1394 * @mask: Writeable trip point bitmap.
1396 static int create_trip_attrs(struct thermal_zone_device
*tz
, int mask
)
1399 int size
= sizeof(struct thermal_attr
) * tz
->trips
;
1401 tz
->trip_type_attrs
= kzalloc(size
, GFP_KERNEL
);
1402 if (!tz
->trip_type_attrs
)
1405 tz
->trip_temp_attrs
= kzalloc(size
, GFP_KERNEL
);
1406 if (!tz
->trip_temp_attrs
) {
1407 kfree(tz
->trip_type_attrs
);
1411 if (tz
->ops
->get_trip_hyst
) {
1412 tz
->trip_hyst_attrs
= kzalloc(size
, GFP_KERNEL
);
1413 if (!tz
->trip_hyst_attrs
) {
1414 kfree(tz
->trip_type_attrs
);
1415 kfree(tz
->trip_temp_attrs
);
1421 for (indx
= 0; indx
< tz
->trips
; indx
++) {
1422 /* create trip type attribute */
1423 snprintf(tz
->trip_type_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1424 "trip_point_%d_type", indx
);
1426 sysfs_attr_init(&tz
->trip_type_attrs
[indx
].attr
.attr
);
1427 tz
->trip_type_attrs
[indx
].attr
.attr
.name
=
1428 tz
->trip_type_attrs
[indx
].name
;
1429 tz
->trip_type_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1430 tz
->trip_type_attrs
[indx
].attr
.show
= trip_point_type_show
;
1432 device_create_file(&tz
->device
,
1433 &tz
->trip_type_attrs
[indx
].attr
);
1435 /* create trip temp attribute */
1436 snprintf(tz
->trip_temp_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1437 "trip_point_%d_temp", indx
);
1439 sysfs_attr_init(&tz
->trip_temp_attrs
[indx
].attr
.attr
);
1440 tz
->trip_temp_attrs
[indx
].attr
.attr
.name
=
1441 tz
->trip_temp_attrs
[indx
].name
;
1442 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1443 tz
->trip_temp_attrs
[indx
].attr
.show
= trip_point_temp_show
;
1444 if (mask
& (1 << indx
)) {
1445 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
1446 tz
->trip_temp_attrs
[indx
].attr
.store
=
1447 trip_point_temp_store
;
1450 device_create_file(&tz
->device
,
1451 &tz
->trip_temp_attrs
[indx
].attr
);
1453 /* create Optional trip hyst attribute */
1454 if (!tz
->ops
->get_trip_hyst
)
1456 snprintf(tz
->trip_hyst_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1457 "trip_point_%d_hyst", indx
);
1459 sysfs_attr_init(&tz
->trip_hyst_attrs
[indx
].attr
.attr
);
1460 tz
->trip_hyst_attrs
[indx
].attr
.attr
.name
=
1461 tz
->trip_hyst_attrs
[indx
].name
;
1462 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1463 tz
->trip_hyst_attrs
[indx
].attr
.show
= trip_point_hyst_show
;
1464 if (tz
->ops
->set_trip_hyst
) {
1465 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
1466 tz
->trip_hyst_attrs
[indx
].attr
.store
=
1467 trip_point_hyst_store
;
1470 device_create_file(&tz
->device
,
1471 &tz
->trip_hyst_attrs
[indx
].attr
);
1476 static void remove_trip_attrs(struct thermal_zone_device
*tz
)
1480 for (indx
= 0; indx
< tz
->trips
; indx
++) {
1481 device_remove_file(&tz
->device
,
1482 &tz
->trip_type_attrs
[indx
].attr
);
1483 device_remove_file(&tz
->device
,
1484 &tz
->trip_temp_attrs
[indx
].attr
);
1485 if (tz
->ops
->get_trip_hyst
)
1486 device_remove_file(&tz
->device
,
1487 &tz
->trip_hyst_attrs
[indx
].attr
);
1489 kfree(tz
->trip_type_attrs
);
1490 kfree(tz
->trip_temp_attrs
);
1491 kfree(tz
->trip_hyst_attrs
);
1495 * thermal_zone_device_register - register a new thermal zone device
1496 * @type: the thermal zone device type
1497 * @trips: the number of trip points the thermal zone support
1498 * @mask: a bit string indicating the writeablility of trip points
1499 * @devdata: private device data
1500 * @ops: standard thermal zone device callbacks
1501 * @tzp: thermal zone platform parameters
1502 * @passive_delay: number of milliseconds to wait between polls when
1503 * performing passive cooling
1504 * @polling_delay: number of milliseconds to wait between polls when checking
1505 * whether trip points have been crossed (0 for interrupt
1508 * thermal_zone_device_unregister() must be called when the device is no
1509 * longer needed. The passive cooling depends on the .get_trend() return value.
1511 struct thermal_zone_device
*thermal_zone_device_register(const char *type
,
1512 int trips
, int mask
, void *devdata
,
1513 const struct thermal_zone_device_ops
*ops
,
1514 const struct thermal_zone_params
*tzp
,
1515 int passive_delay
, int polling_delay
)
1517 struct thermal_zone_device
*tz
;
1518 enum thermal_trip_type trip_type
;
1523 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
1524 return ERR_PTR(-EINVAL
);
1526 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0 || mask
>> trips
)
1527 return ERR_PTR(-EINVAL
);
1529 if (!ops
|| !ops
->get_temp
)
1530 return ERR_PTR(-EINVAL
);
1532 tz
= kzalloc(sizeof(struct thermal_zone_device
), GFP_KERNEL
);
1534 return ERR_PTR(-ENOMEM
);
1536 INIT_LIST_HEAD(&tz
->thermal_instances
);
1538 mutex_init(&tz
->lock
);
1539 result
= get_idr(&thermal_tz_idr
, &thermal_idr_lock
, &tz
->id
);
1542 return ERR_PTR(result
);
1545 strcpy(tz
->type
, type
? : "");
1548 tz
->device
.class = &thermal_class
;
1549 tz
->devdata
= devdata
;
1551 tz
->passive_delay
= passive_delay
;
1552 tz
->polling_delay
= polling_delay
;
1554 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1555 result
= device_register(&tz
->device
);
1557 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1559 return ERR_PTR(result
);
1564 result
= device_create_file(&tz
->device
, &dev_attr_type
);
1569 result
= device_create_file(&tz
->device
, &dev_attr_temp
);
1573 if (ops
->get_mode
) {
1574 result
= device_create_file(&tz
->device
, &dev_attr_mode
);
1579 result
= create_trip_attrs(tz
, mask
);
1583 for (count
= 0; count
< trips
; count
++) {
1584 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
1585 if (trip_type
== THERMAL_TRIP_PASSIVE
)
1590 result
= device_create_file(&tz
->device
, &dev_attr_passive
);
1595 /* Create policy attribute */
1596 result
= device_create_file(&tz
->device
, &dev_attr_policy
);
1600 /* Update 'this' zone's governor information */
1601 mutex_lock(&thermal_governor_lock
);
1604 tz
->governor
= __find_governor(tz
->tzp
->governor_name
);
1606 tz
->governor
= __find_governor(DEFAULT_THERMAL_GOVERNOR
);
1608 mutex_unlock(&thermal_governor_lock
);
1610 result
= thermal_add_hwmon_sysfs(tz
);
1614 mutex_lock(&thermal_list_lock
);
1615 list_add_tail(&tz
->node
, &thermal_tz_list
);
1616 mutex_unlock(&thermal_list_lock
);
1618 /* Bind cooling devices for this zone */
1621 INIT_DELAYED_WORK(&(tz
->poll_queue
), thermal_zone_device_check
);
1623 thermal_zone_device_update(tz
);
1629 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1630 device_unregister(&tz
->device
);
1631 return ERR_PTR(result
);
1633 EXPORT_SYMBOL(thermal_zone_device_register
);
1636 * thermal_device_unregister - removes the registered thermal zone device
1637 * @tz: the thermal zone device to remove
1639 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1642 const struct thermal_zone_params
*tzp
;
1643 struct thermal_cooling_device
*cdev
;
1644 struct thermal_zone_device
*pos
= NULL
;
1651 mutex_lock(&thermal_list_lock
);
1652 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1656 /* thermal zone device not found */
1657 mutex_unlock(&thermal_list_lock
);
1660 list_del(&tz
->node
);
1662 /* Unbind all cdevs associated with 'this' thermal zone */
1663 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
1664 if (tz
->ops
->unbind
) {
1665 tz
->ops
->unbind(tz
, cdev
);
1669 if (!tzp
|| !tzp
->tbp
)
1672 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1673 if (tzp
->tbp
[i
].cdev
== cdev
) {
1674 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1675 tzp
->tbp
[i
].cdev
= NULL
;
1680 mutex_unlock(&thermal_list_lock
);
1682 thermal_zone_device_set_polling(tz
, 0);
1685 device_remove_file(&tz
->device
, &dev_attr_type
);
1686 device_remove_file(&tz
->device
, &dev_attr_temp
);
1687 if (tz
->ops
->get_mode
)
1688 device_remove_file(&tz
->device
, &dev_attr_mode
);
1689 device_remove_file(&tz
->device
, &dev_attr_policy
);
1690 remove_trip_attrs(tz
);
1691 tz
->governor
= NULL
;
1693 thermal_remove_hwmon_sysfs(tz
);
1694 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1695 idr_destroy(&tz
->idr
);
1696 mutex_destroy(&tz
->lock
);
1697 device_unregister(&tz
->device
);
1700 EXPORT_SYMBOL(thermal_zone_device_unregister
);
1703 static struct genl_family thermal_event_genl_family
= {
1704 .id
= GENL_ID_GENERATE
,
1705 .name
= THERMAL_GENL_FAMILY_NAME
,
1706 .version
= THERMAL_GENL_VERSION
,
1707 .maxattr
= THERMAL_GENL_ATTR_MAX
,
1710 static struct genl_multicast_group thermal_event_mcgrp
= {
1711 .name
= THERMAL_GENL_MCAST_GROUP_NAME
,
1714 int thermal_generate_netlink_event(struct thermal_zone_device
*tz
,
1717 struct sk_buff
*skb
;
1718 struct nlattr
*attr
;
1719 struct thermal_genl_event
*thermal_event
;
1723 static unsigned int thermal_event_seqnum
;
1728 /* allocate memory */
1729 size
= nla_total_size(sizeof(struct thermal_genl_event
)) +
1732 skb
= genlmsg_new(size
, GFP_ATOMIC
);
1736 /* add the genetlink message header */
1737 msg_header
= genlmsg_put(skb
, 0, thermal_event_seqnum
++,
1738 &thermal_event_genl_family
, 0,
1739 THERMAL_GENL_CMD_EVENT
);
1746 attr
= nla_reserve(skb
, THERMAL_GENL_ATTR_EVENT
,
1747 sizeof(struct thermal_genl_event
));
1754 thermal_event
= nla_data(attr
);
1755 if (!thermal_event
) {
1760 memset(thermal_event
, 0, sizeof(struct thermal_genl_event
));
1762 thermal_event
->orig
= tz
->id
;
1763 thermal_event
->event
= event
;
1765 /* send multicast genetlink message */
1766 result
= genlmsg_end(skb
, msg_header
);
1772 result
= genlmsg_multicast(skb
, 0, thermal_event_mcgrp
.id
, GFP_ATOMIC
);
1774 pr_info("failed to send netlink event:%d\n", result
);
1778 EXPORT_SYMBOL(thermal_generate_netlink_event
);
1780 static int genetlink_init(void)
1784 result
= genl_register_family(&thermal_event_genl_family
);
1788 result
= genl_register_mc_group(&thermal_event_genl_family
,
1789 &thermal_event_mcgrp
);
1791 genl_unregister_family(&thermal_event_genl_family
);
1795 static void genetlink_exit(void)
1797 genl_unregister_family(&thermal_event_genl_family
);
1799 #else /* !CONFIG_NET */
1800 static inline int genetlink_init(void) { return 0; }
1801 static inline void genetlink_exit(void) {}
1802 #endif /* !CONFIG_NET */
1804 static int __init
thermal_init(void)
1808 result
= class_register(&thermal_class
);
1810 idr_destroy(&thermal_tz_idr
);
1811 idr_destroy(&thermal_cdev_idr
);
1812 mutex_destroy(&thermal_idr_lock
);
1813 mutex_destroy(&thermal_list_lock
);
1815 result
= genetlink_init();
1819 static void __exit
thermal_exit(void)
1821 class_unregister(&thermal_class
);
1822 idr_destroy(&thermal_tz_idr
);
1823 idr_destroy(&thermal_cdev_idr
);
1824 mutex_destroy(&thermal_idr_lock
);
1825 mutex_destroy(&thermal_list_lock
);
1829 fs_initcall(thermal_init
);
1830 module_exit(thermal_exit
);