2 * thermal.c - Generic Thermal Management Sysfs support.
4 * Copyright (C) 2008 Intel Corp
5 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
6 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/slab.h>
32 #include <linux/kdev_t.h>
33 #include <linux/idr.h>
34 #include <linux/thermal.h>
35 #include <linux/reboot.h>
36 #include <linux/string.h>
37 #include <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 v2");
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
);
104 void thermal_unregister_governor(struct thermal_governor
*governor
)
106 struct thermal_zone_device
*pos
;
111 mutex_lock(&thermal_governor_lock
);
113 if (__find_governor(governor
->name
) == NULL
)
116 mutex_lock(&thermal_list_lock
);
118 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
119 if (!strnicmp(pos
->governor
->name
, governor
->name
,
120 THERMAL_NAME_LENGTH
))
121 pos
->governor
= NULL
;
124 mutex_unlock(&thermal_list_lock
);
125 list_del(&governor
->governor_list
);
127 mutex_unlock(&thermal_governor_lock
);
131 static int get_idr(struct idr
*idr
, struct mutex
*lock
, int *id
)
137 ret
= idr_alloc(idr
, NULL
, 0, 0, GFP_KERNEL
);
140 if (unlikely(ret
< 0))
146 static void release_idr(struct idr
*idr
, struct mutex
*lock
, int id
)
155 int get_tz_trend(struct thermal_zone_device
*tz
, int trip
)
157 enum thermal_trend trend
;
159 if (tz
->emul_temperature
|| !tz
->ops
->get_trend
||
160 tz
->ops
->get_trend(tz
, trip
, &trend
)) {
161 if (tz
->temperature
> tz
->last_temperature
)
162 trend
= THERMAL_TREND_RAISING
;
163 else if (tz
->temperature
< tz
->last_temperature
)
164 trend
= THERMAL_TREND_DROPPING
;
166 trend
= THERMAL_TREND_STABLE
;
171 EXPORT_SYMBOL(get_tz_trend
);
173 struct thermal_instance
*get_thermal_instance(struct thermal_zone_device
*tz
,
174 struct thermal_cooling_device
*cdev
, int trip
)
176 struct thermal_instance
*pos
= NULL
;
177 struct thermal_instance
*target_instance
= NULL
;
179 mutex_lock(&tz
->lock
);
180 mutex_lock(&cdev
->lock
);
182 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
) {
183 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
184 target_instance
= pos
;
189 mutex_unlock(&cdev
->lock
);
190 mutex_unlock(&tz
->lock
);
192 return target_instance
;
194 EXPORT_SYMBOL(get_thermal_instance
);
196 static void print_bind_err_msg(struct thermal_zone_device
*tz
,
197 struct thermal_cooling_device
*cdev
, int ret
)
199 dev_err(&tz
->device
, "binding zone %s with cdev %s failed:%d\n",
200 tz
->type
, cdev
->type
, ret
);
203 static void __bind(struct thermal_zone_device
*tz
, int mask
,
204 struct thermal_cooling_device
*cdev
)
208 for (i
= 0; i
< tz
->trips
; i
++) {
209 if (mask
& (1 << i
)) {
210 ret
= thermal_zone_bind_cooling_device(tz
, i
, cdev
,
211 THERMAL_NO_LIMIT
, THERMAL_NO_LIMIT
);
213 print_bind_err_msg(tz
, cdev
, ret
);
218 static void __unbind(struct thermal_zone_device
*tz
, int mask
,
219 struct thermal_cooling_device
*cdev
)
223 for (i
= 0; i
< tz
->trips
; i
++)
225 thermal_zone_unbind_cooling_device(tz
, i
, cdev
);
228 static void bind_cdev(struct thermal_cooling_device
*cdev
)
231 const struct thermal_zone_params
*tzp
;
232 struct thermal_zone_device
*pos
= NULL
;
234 mutex_lock(&thermal_list_lock
);
236 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
237 if (!pos
->tzp
&& !pos
->ops
->bind
)
240 if (!pos
->tzp
&& pos
->ops
->bind
) {
241 ret
= pos
->ops
->bind(pos
, cdev
);
243 print_bind_err_msg(pos
, cdev
, ret
);
247 if (!tzp
|| !tzp
->tbp
)
250 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
251 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
253 if (tzp
->tbp
[i
].match(pos
, cdev
))
255 tzp
->tbp
[i
].cdev
= cdev
;
256 __bind(pos
, tzp
->tbp
[i
].trip_mask
, cdev
);
260 mutex_unlock(&thermal_list_lock
);
263 static void bind_tz(struct thermal_zone_device
*tz
)
266 struct thermal_cooling_device
*pos
= NULL
;
267 const struct thermal_zone_params
*tzp
= tz
->tzp
;
269 if (!tzp
&& !tz
->ops
->bind
)
272 mutex_lock(&thermal_list_lock
);
274 /* If there is no platform data, try to use ops->bind */
275 if (!tzp
&& tz
->ops
->bind
) {
276 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
277 ret
= tz
->ops
->bind(tz
, pos
);
279 print_bind_err_msg(tz
, pos
, ret
);
284 if (!tzp
|| !tzp
->tbp
)
287 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
288 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
289 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
291 if (tzp
->tbp
[i
].match(tz
, pos
))
293 tzp
->tbp
[i
].cdev
= pos
;
294 __bind(tz
, tzp
->tbp
[i
].trip_mask
, pos
);
298 mutex_unlock(&thermal_list_lock
);
301 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
305 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
306 round_jiffies(msecs_to_jiffies(delay
)));
308 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
309 msecs_to_jiffies(delay
));
311 cancel_delayed_work(&tz
->poll_queue
);
314 static void monitor_thermal_zone(struct thermal_zone_device
*tz
)
316 mutex_lock(&tz
->lock
);
319 thermal_zone_device_set_polling(tz
, tz
->passive_delay
);
320 else if (tz
->polling_delay
)
321 thermal_zone_device_set_polling(tz
, tz
->polling_delay
);
323 thermal_zone_device_set_polling(tz
, 0);
325 mutex_unlock(&tz
->lock
);
328 static void handle_non_critical_trips(struct thermal_zone_device
*tz
,
329 int trip
, enum thermal_trip_type trip_type
)
332 tz
->governor
->throttle(tz
, trip
);
335 static void handle_critical_trips(struct thermal_zone_device
*tz
,
336 int trip
, enum thermal_trip_type trip_type
)
340 tz
->ops
->get_trip_temp(tz
, trip
, &trip_temp
);
342 /* If we have not crossed the trip_temp, we do not care. */
343 if (tz
->temperature
< trip_temp
)
347 tz
->ops
->notify(tz
, trip
, trip_type
);
349 if (trip_type
== THERMAL_TRIP_CRITICAL
) {
350 dev_emerg(&tz
->device
,
351 "critical temperature reached(%d C),shutting down\n",
352 tz
->temperature
/ 1000);
353 orderly_poweroff(true);
357 static void handle_thermal_trip(struct thermal_zone_device
*tz
, int trip
)
359 enum thermal_trip_type type
;
361 tz
->ops
->get_trip_type(tz
, trip
, &type
);
363 if (type
== THERMAL_TRIP_CRITICAL
|| type
== THERMAL_TRIP_HOT
)
364 handle_critical_trips(tz
, trip
, type
);
366 handle_non_critical_trips(tz
, trip
, type
);
368 * Alright, we handled this trip successfully.
369 * So, start monitoring again.
371 monitor_thermal_zone(tz
);
375 * thermal_zone_get_temp() - returns its the temperature of thermal zone
376 * @tz: a valid pointer to a struct thermal_zone_device
377 * @temp: a valid pointer to where to store the resulting temperature.
379 * When a valid thermal zone reference is passed, it will fetch its
380 * temperature and fill @temp.
382 * Return: On success returns 0, an error code otherwise
384 int thermal_zone_get_temp(struct thermal_zone_device
*tz
, unsigned long *temp
)
387 #ifdef CONFIG_THERMAL_EMULATION
389 unsigned long crit_temp
= -1UL;
390 enum thermal_trip_type type
;
393 if (!tz
|| IS_ERR(tz
))
396 mutex_lock(&tz
->lock
);
398 ret
= tz
->ops
->get_temp(tz
, temp
);
399 #ifdef CONFIG_THERMAL_EMULATION
400 if (!tz
->emul_temperature
)
403 for (count
= 0; count
< tz
->trips
; count
++) {
404 ret
= tz
->ops
->get_trip_type(tz
, count
, &type
);
405 if (!ret
&& type
== THERMAL_TRIP_CRITICAL
) {
406 ret
= tz
->ops
->get_trip_temp(tz
, count
, &crit_temp
);
414 if (*temp
< crit_temp
)
415 *temp
= tz
->emul_temperature
;
418 mutex_unlock(&tz
->lock
);
422 EXPORT_SYMBOL_GPL(thermal_zone_get_temp
);
424 static void update_temperature(struct thermal_zone_device
*tz
)
429 ret
= thermal_zone_get_temp(tz
, &temp
);
431 dev_warn(&tz
->device
, "failed to read out thermal zone %d\n",
436 mutex_lock(&tz
->lock
);
437 tz
->last_temperature
= tz
->temperature
;
438 tz
->temperature
= temp
;
439 mutex_unlock(&tz
->lock
);
442 void thermal_zone_device_update(struct thermal_zone_device
*tz
)
446 update_temperature(tz
);
448 for (count
= 0; count
< tz
->trips
; count
++)
449 handle_thermal_trip(tz
, count
);
451 EXPORT_SYMBOL_GPL(thermal_zone_device_update
);
453 static void thermal_zone_device_check(struct work_struct
*work
)
455 struct thermal_zone_device
*tz
= container_of(work
, struct
458 thermal_zone_device_update(tz
);
461 /* sys I/F for thermal zone */
463 #define to_thermal_zone(_dev) \
464 container_of(_dev, struct thermal_zone_device, device)
467 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
469 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
471 return sprintf(buf
, "%s\n", tz
->type
);
475 temp_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
477 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
481 ret
= thermal_zone_get_temp(tz
, &temperature
);
486 return sprintf(buf
, "%ld\n", temperature
);
490 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
492 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
493 enum thermal_device_mode mode
;
496 if (!tz
->ops
->get_mode
)
499 result
= tz
->ops
->get_mode(tz
, &mode
);
503 return sprintf(buf
, "%s\n", mode
== THERMAL_DEVICE_ENABLED
? "enabled"
508 mode_store(struct device
*dev
, struct device_attribute
*attr
,
509 const char *buf
, size_t count
)
511 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
514 if (!tz
->ops
->set_mode
)
517 if (!strncmp(buf
, "enabled", sizeof("enabled") - 1))
518 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_ENABLED
);
519 else if (!strncmp(buf
, "disabled", sizeof("disabled") - 1))
520 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_DISABLED
);
531 trip_point_type_show(struct device
*dev
, struct device_attribute
*attr
,
534 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
535 enum thermal_trip_type type
;
538 if (!tz
->ops
->get_trip_type
)
541 if (!sscanf(attr
->attr
.name
, "trip_point_%d_type", &trip
))
544 result
= tz
->ops
->get_trip_type(tz
, trip
, &type
);
549 case THERMAL_TRIP_CRITICAL
:
550 return sprintf(buf
, "critical\n");
551 case THERMAL_TRIP_HOT
:
552 return sprintf(buf
, "hot\n");
553 case THERMAL_TRIP_PASSIVE
:
554 return sprintf(buf
, "passive\n");
555 case THERMAL_TRIP_ACTIVE
:
556 return sprintf(buf
, "active\n");
558 return sprintf(buf
, "unknown\n");
563 trip_point_temp_store(struct device
*dev
, struct device_attribute
*attr
,
564 const char *buf
, size_t count
)
566 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
568 unsigned long temperature
;
570 if (!tz
->ops
->set_trip_temp
)
573 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
576 if (kstrtoul(buf
, 10, &temperature
))
579 ret
= tz
->ops
->set_trip_temp(tz
, trip
, temperature
);
581 return ret
? ret
: count
;
585 trip_point_temp_show(struct device
*dev
, struct device_attribute
*attr
,
588 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
592 if (!tz
->ops
->get_trip_temp
)
595 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
598 ret
= tz
->ops
->get_trip_temp(tz
, trip
, &temperature
);
603 return sprintf(buf
, "%ld\n", temperature
);
607 trip_point_hyst_store(struct device
*dev
, struct device_attribute
*attr
,
608 const char *buf
, size_t count
)
610 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
612 unsigned long temperature
;
614 if (!tz
->ops
->set_trip_hyst
)
617 if (!sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
))
620 if (kstrtoul(buf
, 10, &temperature
))
624 * We are not doing any check on the 'temperature' value
625 * here. The driver implementing 'set_trip_hyst' has to
628 ret
= tz
->ops
->set_trip_hyst(tz
, trip
, temperature
);
630 return ret
? ret
: count
;
634 trip_point_hyst_show(struct device
*dev
, struct device_attribute
*attr
,
637 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
639 unsigned long temperature
;
641 if (!tz
->ops
->get_trip_hyst
)
644 if (!sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
))
647 ret
= tz
->ops
->get_trip_hyst(tz
, trip
, &temperature
);
649 return ret
? ret
: sprintf(buf
, "%ld\n", temperature
);
653 passive_store(struct device
*dev
, struct device_attribute
*attr
,
654 const char *buf
, size_t count
)
656 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
657 struct thermal_cooling_device
*cdev
= NULL
;
660 if (!sscanf(buf
, "%d\n", &state
))
663 /* sanity check: values below 1000 millicelcius don't make sense
664 * and can cause the system to go into a thermal heart attack
666 if (state
&& state
< 1000)
669 if (state
&& !tz
->forced_passive
) {
670 mutex_lock(&thermal_list_lock
);
671 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
672 if (!strncmp("Processor", cdev
->type
,
673 sizeof("Processor")))
674 thermal_zone_bind_cooling_device(tz
,
675 THERMAL_TRIPS_NONE
, cdev
,
679 mutex_unlock(&thermal_list_lock
);
680 if (!tz
->passive_delay
)
681 tz
->passive_delay
= 1000;
682 } else if (!state
&& tz
->forced_passive
) {
683 mutex_lock(&thermal_list_lock
);
684 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
685 if (!strncmp("Processor", cdev
->type
,
686 sizeof("Processor")))
687 thermal_zone_unbind_cooling_device(tz
,
691 mutex_unlock(&thermal_list_lock
);
692 tz
->passive_delay
= 0;
695 tz
->forced_passive
= state
;
697 thermal_zone_device_update(tz
);
703 passive_show(struct device
*dev
, struct device_attribute
*attr
,
706 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
708 return sprintf(buf
, "%d\n", tz
->forced_passive
);
712 policy_store(struct device
*dev
, struct device_attribute
*attr
,
713 const char *buf
, size_t count
)
716 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
717 struct thermal_governor
*gov
;
718 char name
[THERMAL_NAME_LENGTH
];
720 snprintf(name
, sizeof(name
), "%s", buf
);
722 mutex_lock(&thermal_governor_lock
);
724 gov
= __find_governor(strim(name
));
732 mutex_unlock(&thermal_governor_lock
);
737 policy_show(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
739 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
741 return sprintf(buf
, "%s\n", tz
->governor
->name
);
744 #ifdef CONFIG_THERMAL_EMULATION
746 emul_temp_store(struct device
*dev
, struct device_attribute
*attr
,
747 const char *buf
, size_t count
)
749 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
751 unsigned long temperature
;
753 if (kstrtoul(buf
, 10, &temperature
))
756 if (!tz
->ops
->set_emul_temp
) {
757 mutex_lock(&tz
->lock
);
758 tz
->emul_temperature
= temperature
;
759 mutex_unlock(&tz
->lock
);
761 ret
= tz
->ops
->set_emul_temp(tz
, temperature
);
764 return ret
? ret
: count
;
766 static DEVICE_ATTR(emul_temp
, S_IWUSR
, NULL
, emul_temp_store
);
767 #endif/*CONFIG_THERMAL_EMULATION*/
769 static DEVICE_ATTR(type
, 0444, type_show
, NULL
);
770 static DEVICE_ATTR(temp
, 0444, temp_show
, NULL
);
771 static DEVICE_ATTR(mode
, 0644, mode_show
, mode_store
);
772 static DEVICE_ATTR(passive
, S_IRUGO
| S_IWUSR
, passive_show
, passive_store
);
773 static DEVICE_ATTR(policy
, S_IRUGO
| S_IWUSR
, policy_show
, policy_store
);
775 /* sys I/F for cooling device */
776 #define to_cooling_device(_dev) \
777 container_of(_dev, struct thermal_cooling_device, device)
780 thermal_cooling_device_type_show(struct device
*dev
,
781 struct device_attribute
*attr
, char *buf
)
783 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
785 return sprintf(buf
, "%s\n", cdev
->type
);
789 thermal_cooling_device_max_state_show(struct device
*dev
,
790 struct device_attribute
*attr
, char *buf
)
792 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
796 ret
= cdev
->ops
->get_max_state(cdev
, &state
);
799 return sprintf(buf
, "%ld\n", state
);
803 thermal_cooling_device_cur_state_show(struct device
*dev
,
804 struct device_attribute
*attr
, char *buf
)
806 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
810 ret
= cdev
->ops
->get_cur_state(cdev
, &state
);
813 return sprintf(buf
, "%ld\n", state
);
817 thermal_cooling_device_cur_state_store(struct device
*dev
,
818 struct device_attribute
*attr
,
819 const char *buf
, size_t count
)
821 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
825 if (!sscanf(buf
, "%ld\n", &state
))
831 result
= cdev
->ops
->set_cur_state(cdev
, state
);
837 static struct device_attribute dev_attr_cdev_type
=
838 __ATTR(type
, 0444, thermal_cooling_device_type_show
, NULL
);
839 static DEVICE_ATTR(max_state
, 0444,
840 thermal_cooling_device_max_state_show
, NULL
);
841 static DEVICE_ATTR(cur_state
, 0644,
842 thermal_cooling_device_cur_state_show
,
843 thermal_cooling_device_cur_state_store
);
846 thermal_cooling_device_trip_point_show(struct device
*dev
,
847 struct device_attribute
*attr
, char *buf
)
849 struct thermal_instance
*instance
;
852 container_of(attr
, struct thermal_instance
, attr
);
854 if (instance
->trip
== THERMAL_TRIPS_NONE
)
855 return sprintf(buf
, "-1\n");
857 return sprintf(buf
, "%d\n", instance
->trip
);
860 /* Device management */
862 #if defined(CONFIG_THERMAL_HWMON)
865 #include <linux/hwmon.h>
867 /* thermal zone devices with the same type share one hwmon device */
868 struct thermal_hwmon_device
{
869 char type
[THERMAL_NAME_LENGTH
];
870 struct device
*device
;
872 struct list_head tz_list
;
873 struct list_head node
;
876 struct thermal_hwmon_attr
{
877 struct device_attribute attr
;
881 /* one temperature input for each thermal zone */
882 struct thermal_hwmon_temp
{
883 struct list_head hwmon_node
;
884 struct thermal_zone_device
*tz
;
885 struct thermal_hwmon_attr temp_input
; /* hwmon sys attr */
886 struct thermal_hwmon_attr temp_crit
; /* hwmon sys attr */
889 static LIST_HEAD(thermal_hwmon_list
);
892 name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
894 struct thermal_hwmon_device
*hwmon
= dev_get_drvdata(dev
);
895 return sprintf(buf
, "%s\n", hwmon
->type
);
897 static DEVICE_ATTR(name
, 0444, name_show
, NULL
);
900 temp_input_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
904 struct thermal_hwmon_attr
*hwmon_attr
905 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
906 struct thermal_hwmon_temp
*temp
907 = container_of(hwmon_attr
, struct thermal_hwmon_temp
,
909 struct thermal_zone_device
*tz
= temp
->tz
;
911 ret
= thermal_zone_get_temp(tz
, &temperature
);
916 return sprintf(buf
, "%ld\n", temperature
);
920 temp_crit_show(struct device
*dev
, struct device_attribute
*attr
,
923 struct thermal_hwmon_attr
*hwmon_attr
924 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
925 struct thermal_hwmon_temp
*temp
926 = container_of(hwmon_attr
, struct thermal_hwmon_temp
,
928 struct thermal_zone_device
*tz
= temp
->tz
;
932 ret
= tz
->ops
->get_trip_temp(tz
, 0, &temperature
);
936 return sprintf(buf
, "%ld\n", temperature
);
940 static struct thermal_hwmon_device
*
941 thermal_hwmon_lookup_by_type(const struct thermal_zone_device
*tz
)
943 struct thermal_hwmon_device
*hwmon
;
945 mutex_lock(&thermal_list_lock
);
946 list_for_each_entry(hwmon
, &thermal_hwmon_list
, node
)
947 if (!strcmp(hwmon
->type
, tz
->type
)) {
948 mutex_unlock(&thermal_list_lock
);
951 mutex_unlock(&thermal_list_lock
);
956 /* Find the temperature input matching a given thermal zone */
957 static struct thermal_hwmon_temp
*
958 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device
*hwmon
,
959 const struct thermal_zone_device
*tz
)
961 struct thermal_hwmon_temp
*temp
;
963 mutex_lock(&thermal_list_lock
);
964 list_for_each_entry(temp
, &hwmon
->tz_list
, hwmon_node
)
965 if (temp
->tz
== tz
) {
966 mutex_unlock(&thermal_list_lock
);
969 mutex_unlock(&thermal_list_lock
);
975 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
977 struct thermal_hwmon_device
*hwmon
;
978 struct thermal_hwmon_temp
*temp
;
979 int new_hwmon_device
= 1;
982 hwmon
= thermal_hwmon_lookup_by_type(tz
);
984 new_hwmon_device
= 0;
985 goto register_sys_interface
;
988 hwmon
= kzalloc(sizeof(struct thermal_hwmon_device
), GFP_KERNEL
);
992 INIT_LIST_HEAD(&hwmon
->tz_list
);
993 strlcpy(hwmon
->type
, tz
->type
, THERMAL_NAME_LENGTH
);
994 hwmon
->device
= hwmon_device_register(NULL
);
995 if (IS_ERR(hwmon
->device
)) {
996 result
= PTR_ERR(hwmon
->device
);
999 dev_set_drvdata(hwmon
->device
, hwmon
);
1000 result
= device_create_file(hwmon
->device
, &dev_attr_name
);
1004 register_sys_interface
:
1005 temp
= kzalloc(sizeof(struct thermal_hwmon_temp
), GFP_KERNEL
);
1008 goto unregister_name
;
1014 snprintf(temp
->temp_input
.name
, sizeof(temp
->temp_input
.name
),
1015 "temp%d_input", hwmon
->count
);
1016 temp
->temp_input
.attr
.attr
.name
= temp
->temp_input
.name
;
1017 temp
->temp_input
.attr
.attr
.mode
= 0444;
1018 temp
->temp_input
.attr
.show
= temp_input_show
;
1019 sysfs_attr_init(&temp
->temp_input
.attr
.attr
);
1020 result
= device_create_file(hwmon
->device
, &temp
->temp_input
.attr
);
1024 if (tz
->ops
->get_crit_temp
) {
1025 unsigned long temperature
;
1026 if (!tz
->ops
->get_crit_temp(tz
, &temperature
)) {
1027 snprintf(temp
->temp_crit
.name
,
1028 sizeof(temp
->temp_crit
.name
),
1029 "temp%d_crit", hwmon
->count
);
1030 temp
->temp_crit
.attr
.attr
.name
= temp
->temp_crit
.name
;
1031 temp
->temp_crit
.attr
.attr
.mode
= 0444;
1032 temp
->temp_crit
.attr
.show
= temp_crit_show
;
1033 sysfs_attr_init(&temp
->temp_crit
.attr
.attr
);
1034 result
= device_create_file(hwmon
->device
,
1035 &temp
->temp_crit
.attr
);
1037 goto unregister_input
;
1041 mutex_lock(&thermal_list_lock
);
1042 if (new_hwmon_device
)
1043 list_add_tail(&hwmon
->node
, &thermal_hwmon_list
);
1044 list_add_tail(&temp
->hwmon_node
, &hwmon
->tz_list
);
1045 mutex_unlock(&thermal_list_lock
);
1050 device_remove_file(hwmon
->device
, &temp
->temp_input
.attr
);
1054 if (new_hwmon_device
) {
1055 device_remove_file(hwmon
->device
, &dev_attr_name
);
1056 hwmon_device_unregister(hwmon
->device
);
1059 if (new_hwmon_device
)
1066 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
1068 struct thermal_hwmon_device
*hwmon
;
1069 struct thermal_hwmon_temp
*temp
;
1071 hwmon
= thermal_hwmon_lookup_by_type(tz
);
1072 if (unlikely(!hwmon
)) {
1073 /* Should never happen... */
1074 dev_dbg(&tz
->device
, "hwmon device lookup failed!\n");
1078 temp
= thermal_hwmon_lookup_temp(hwmon
, tz
);
1079 if (unlikely(!temp
)) {
1080 /* Should never happen... */
1081 dev_dbg(&tz
->device
, "temperature input lookup failed!\n");
1085 device_remove_file(hwmon
->device
, &temp
->temp_input
.attr
);
1086 if (tz
->ops
->get_crit_temp
)
1087 device_remove_file(hwmon
->device
, &temp
->temp_crit
.attr
);
1089 mutex_lock(&thermal_list_lock
);
1090 list_del(&temp
->hwmon_node
);
1092 if (!list_empty(&hwmon
->tz_list
)) {
1093 mutex_unlock(&thermal_list_lock
);
1096 list_del(&hwmon
->node
);
1097 mutex_unlock(&thermal_list_lock
);
1099 device_remove_file(hwmon
->device
, &dev_attr_name
);
1100 hwmon_device_unregister(hwmon
->device
);
1105 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
1111 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
1117 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1118 * @tz: pointer to struct thermal_zone_device
1119 * @trip: indicates which trip point the cooling devices is
1120 * associated with in this thermal zone.
1121 * @cdev: pointer to struct thermal_cooling_device
1122 * @upper: the Maximum cooling state for this trip point.
1123 * THERMAL_NO_LIMIT means no upper limit,
1124 * and the cooling device can be in max_state.
1125 * @lower: the Minimum cooling state can be used for this trip point.
1126 * THERMAL_NO_LIMIT means no lower limit,
1127 * and the cooling device can be in cooling state 0.
1129 * This interface function bind a thermal cooling device to the certain trip
1130 * point of a thermal zone device.
1131 * This function is usually called in the thermal zone device .bind callback.
1133 * Return: 0 on success, the proper error value otherwise.
1135 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
1137 struct thermal_cooling_device
*cdev
,
1138 unsigned long upper
, unsigned long lower
)
1140 struct thermal_instance
*dev
;
1141 struct thermal_instance
*pos
;
1142 struct thermal_zone_device
*pos1
;
1143 struct thermal_cooling_device
*pos2
;
1144 unsigned long max_state
;
1147 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
1150 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
1154 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
1159 if (tz
!= pos1
|| cdev
!= pos2
)
1162 cdev
->ops
->get_max_state(cdev
, &max_state
);
1164 /* lower default 0, upper default max_state */
1165 lower
= lower
== THERMAL_NO_LIMIT
? 0 : lower
;
1166 upper
= upper
== THERMAL_NO_LIMIT
? max_state
: upper
;
1168 if (lower
> upper
|| upper
> max_state
)
1172 kzalloc(sizeof(struct thermal_instance
), GFP_KERNEL
);
1180 dev
->target
= THERMAL_NO_TARGET
;
1182 result
= get_idr(&tz
->idr
, &tz
->lock
, &dev
->id
);
1186 sprintf(dev
->name
, "cdev%d", dev
->id
);
1188 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
1192 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
1193 sysfs_attr_init(&dev
->attr
.attr
);
1194 dev
->attr
.attr
.name
= dev
->attr_name
;
1195 dev
->attr
.attr
.mode
= 0444;
1196 dev
->attr
.show
= thermal_cooling_device_trip_point_show
;
1197 result
= device_create_file(&tz
->device
, &dev
->attr
);
1199 goto remove_symbol_link
;
1201 mutex_lock(&tz
->lock
);
1202 mutex_lock(&cdev
->lock
);
1203 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
)
1204 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
1209 list_add_tail(&dev
->tz_node
, &tz
->thermal_instances
);
1210 list_add_tail(&dev
->cdev_node
, &cdev
->thermal_instances
);
1212 mutex_unlock(&cdev
->lock
);
1213 mutex_unlock(&tz
->lock
);
1218 device_remove_file(&tz
->device
, &dev
->attr
);
1220 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
1222 release_idr(&tz
->idr
, &tz
->lock
, dev
->id
);
1227 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device
);
1230 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1232 * @tz: pointer to a struct thermal_zone_device.
1233 * @trip: indicates which trip point the cooling devices is
1234 * associated with in this thermal zone.
1235 * @cdev: pointer to a struct thermal_cooling_device.
1237 * This interface function unbind a thermal cooling device from the certain
1238 * trip point of a thermal zone device.
1239 * This function is usually called in the thermal zone device .unbind callback.
1241 * Return: 0 on success, the proper error value otherwise.
1243 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
1245 struct thermal_cooling_device
*cdev
)
1247 struct thermal_instance
*pos
, *next
;
1249 mutex_lock(&tz
->lock
);
1250 mutex_lock(&cdev
->lock
);
1251 list_for_each_entry_safe(pos
, next
, &tz
->thermal_instances
, tz_node
) {
1252 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
1253 list_del(&pos
->tz_node
);
1254 list_del(&pos
->cdev_node
);
1255 mutex_unlock(&cdev
->lock
);
1256 mutex_unlock(&tz
->lock
);
1260 mutex_unlock(&cdev
->lock
);
1261 mutex_unlock(&tz
->lock
);
1266 device_remove_file(&tz
->device
, &pos
->attr
);
1267 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
1268 release_idr(&tz
->idr
, &tz
->lock
, pos
->id
);
1272 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device
);
1274 static void thermal_release(struct device
*dev
)
1276 struct thermal_zone_device
*tz
;
1277 struct thermal_cooling_device
*cdev
;
1279 if (!strncmp(dev_name(dev
), "thermal_zone",
1280 sizeof("thermal_zone") - 1)) {
1281 tz
= to_thermal_zone(dev
);
1284 cdev
= to_cooling_device(dev
);
1289 static struct class thermal_class
= {
1291 .dev_release
= thermal_release
,
1295 * thermal_cooling_device_register() - register a new thermal cooling device
1296 * @type: the thermal cooling device type.
1297 * @devdata: device private data.
1298 * @ops: standard thermal cooling devices callbacks.
1300 * This interface function adds a new thermal cooling device (fan/processor/...)
1301 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1302 * to all the thermal zone devices registered at the same time.
1304 * Return: a pointer to the created struct thermal_cooling_device or an
1305 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1307 struct thermal_cooling_device
*
1308 thermal_cooling_device_register(char *type
, void *devdata
,
1309 const struct thermal_cooling_device_ops
*ops
)
1311 struct thermal_cooling_device
*cdev
;
1314 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
1315 return ERR_PTR(-EINVAL
);
1317 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
1318 !ops
->set_cur_state
)
1319 return ERR_PTR(-EINVAL
);
1321 cdev
= kzalloc(sizeof(struct thermal_cooling_device
), GFP_KERNEL
);
1323 return ERR_PTR(-ENOMEM
);
1325 result
= get_idr(&thermal_cdev_idr
, &thermal_idr_lock
, &cdev
->id
);
1328 return ERR_PTR(result
);
1331 strlcpy(cdev
->type
, type
? : "", sizeof(cdev
->type
));
1332 mutex_init(&cdev
->lock
);
1333 INIT_LIST_HEAD(&cdev
->thermal_instances
);
1335 cdev
->updated
= true;
1336 cdev
->device
.class = &thermal_class
;
1337 cdev
->devdata
= devdata
;
1338 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
1339 result
= device_register(&cdev
->device
);
1341 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1343 return ERR_PTR(result
);
1348 result
= device_create_file(&cdev
->device
, &dev_attr_cdev_type
);
1353 result
= device_create_file(&cdev
->device
, &dev_attr_max_state
);
1357 result
= device_create_file(&cdev
->device
, &dev_attr_cur_state
);
1361 /* Add 'this' new cdev to the global cdev list */
1362 mutex_lock(&thermal_list_lock
);
1363 list_add(&cdev
->node
, &thermal_cdev_list
);
1364 mutex_unlock(&thermal_list_lock
);
1366 /* Update binding information for 'this' new cdev */
1372 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1373 device_unregister(&cdev
->device
);
1374 return ERR_PTR(result
);
1376 EXPORT_SYMBOL_GPL(thermal_cooling_device_register
);
1379 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1380 * @cdev: the thermal cooling device to remove.
1382 * thermal_cooling_device_unregister() must be called when the device is no
1385 void thermal_cooling_device_unregister(struct thermal_cooling_device
*cdev
)
1388 const struct thermal_zone_params
*tzp
;
1389 struct thermal_zone_device
*tz
;
1390 struct thermal_cooling_device
*pos
= NULL
;
1395 mutex_lock(&thermal_list_lock
);
1396 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
1400 /* thermal cooling device not found */
1401 mutex_unlock(&thermal_list_lock
);
1404 list_del(&cdev
->node
);
1406 /* Unbind all thermal zones associated with 'this' cdev */
1407 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1408 if (tz
->ops
->unbind
) {
1409 tz
->ops
->unbind(tz
, cdev
);
1413 if (!tz
->tzp
|| !tz
->tzp
->tbp
)
1417 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1418 if (tzp
->tbp
[i
].cdev
== cdev
) {
1419 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1420 tzp
->tbp
[i
].cdev
= NULL
;
1425 mutex_unlock(&thermal_list_lock
);
1428 device_remove_file(&cdev
->device
, &dev_attr_cdev_type
);
1429 device_remove_file(&cdev
->device
, &dev_attr_max_state
);
1430 device_remove_file(&cdev
->device
, &dev_attr_cur_state
);
1432 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1433 device_unregister(&cdev
->device
);
1436 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister
);
1438 void thermal_cdev_update(struct thermal_cooling_device
*cdev
)
1440 struct thermal_instance
*instance
;
1441 unsigned long target
= 0;
1443 /* cooling device is updated*/
1447 mutex_lock(&cdev
->lock
);
1448 /* Make sure cdev enters the deepest cooling state */
1449 list_for_each_entry(instance
, &cdev
->thermal_instances
, cdev_node
) {
1450 if (instance
->target
== THERMAL_NO_TARGET
)
1452 if (instance
->target
> target
)
1453 target
= instance
->target
;
1455 mutex_unlock(&cdev
->lock
);
1456 cdev
->ops
->set_cur_state(cdev
, target
);
1457 cdev
->updated
= true;
1459 EXPORT_SYMBOL(thermal_cdev_update
);
1462 * thermal_notify_framework - Sensor drivers use this API to notify framework
1463 * @tz: thermal zone device
1464 * @trip: indicates which trip point has been crossed
1466 * This function handles the trip events from sensor drivers. It starts
1467 * throttling the cooling devices according to the policy configured.
1468 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1469 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1470 * The throttling policy is based on the configured platform data; if no
1471 * platform data is provided, this uses the step_wise throttling policy.
1473 void thermal_notify_framework(struct thermal_zone_device
*tz
, int trip
)
1475 handle_thermal_trip(tz
, trip
);
1477 EXPORT_SYMBOL_GPL(thermal_notify_framework
);
1480 * create_trip_attrs() - create attributes for trip points
1481 * @tz: the thermal zone device
1482 * @mask: Writeable trip point bitmap.
1484 * helper function to instantiate sysfs entries for every trip
1485 * point and its properties of a struct thermal_zone_device.
1487 * Return: 0 on success, the proper error value otherwise.
1489 static int create_trip_attrs(struct thermal_zone_device
*tz
, int mask
)
1492 int size
= sizeof(struct thermal_attr
) * tz
->trips
;
1494 tz
->trip_type_attrs
= kzalloc(size
, GFP_KERNEL
);
1495 if (!tz
->trip_type_attrs
)
1498 tz
->trip_temp_attrs
= kzalloc(size
, GFP_KERNEL
);
1499 if (!tz
->trip_temp_attrs
) {
1500 kfree(tz
->trip_type_attrs
);
1504 if (tz
->ops
->get_trip_hyst
) {
1505 tz
->trip_hyst_attrs
= kzalloc(size
, GFP_KERNEL
);
1506 if (!tz
->trip_hyst_attrs
) {
1507 kfree(tz
->trip_type_attrs
);
1508 kfree(tz
->trip_temp_attrs
);
1514 for (indx
= 0; indx
< tz
->trips
; indx
++) {
1515 /* create trip type attribute */
1516 snprintf(tz
->trip_type_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1517 "trip_point_%d_type", indx
);
1519 sysfs_attr_init(&tz
->trip_type_attrs
[indx
].attr
.attr
);
1520 tz
->trip_type_attrs
[indx
].attr
.attr
.name
=
1521 tz
->trip_type_attrs
[indx
].name
;
1522 tz
->trip_type_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1523 tz
->trip_type_attrs
[indx
].attr
.show
= trip_point_type_show
;
1525 device_create_file(&tz
->device
,
1526 &tz
->trip_type_attrs
[indx
].attr
);
1528 /* create trip temp attribute */
1529 snprintf(tz
->trip_temp_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1530 "trip_point_%d_temp", indx
);
1532 sysfs_attr_init(&tz
->trip_temp_attrs
[indx
].attr
.attr
);
1533 tz
->trip_temp_attrs
[indx
].attr
.attr
.name
=
1534 tz
->trip_temp_attrs
[indx
].name
;
1535 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1536 tz
->trip_temp_attrs
[indx
].attr
.show
= trip_point_temp_show
;
1537 if (mask
& (1 << indx
)) {
1538 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
1539 tz
->trip_temp_attrs
[indx
].attr
.store
=
1540 trip_point_temp_store
;
1543 device_create_file(&tz
->device
,
1544 &tz
->trip_temp_attrs
[indx
].attr
);
1546 /* create Optional trip hyst attribute */
1547 if (!tz
->ops
->get_trip_hyst
)
1549 snprintf(tz
->trip_hyst_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1550 "trip_point_%d_hyst", indx
);
1552 sysfs_attr_init(&tz
->trip_hyst_attrs
[indx
].attr
.attr
);
1553 tz
->trip_hyst_attrs
[indx
].attr
.attr
.name
=
1554 tz
->trip_hyst_attrs
[indx
].name
;
1555 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1556 tz
->trip_hyst_attrs
[indx
].attr
.show
= trip_point_hyst_show
;
1557 if (tz
->ops
->set_trip_hyst
) {
1558 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
1559 tz
->trip_hyst_attrs
[indx
].attr
.store
=
1560 trip_point_hyst_store
;
1563 device_create_file(&tz
->device
,
1564 &tz
->trip_hyst_attrs
[indx
].attr
);
1569 static void remove_trip_attrs(struct thermal_zone_device
*tz
)
1573 for (indx
= 0; indx
< tz
->trips
; indx
++) {
1574 device_remove_file(&tz
->device
,
1575 &tz
->trip_type_attrs
[indx
].attr
);
1576 device_remove_file(&tz
->device
,
1577 &tz
->trip_temp_attrs
[indx
].attr
);
1578 if (tz
->ops
->get_trip_hyst
)
1579 device_remove_file(&tz
->device
,
1580 &tz
->trip_hyst_attrs
[indx
].attr
);
1582 kfree(tz
->trip_type_attrs
);
1583 kfree(tz
->trip_temp_attrs
);
1584 kfree(tz
->trip_hyst_attrs
);
1588 * thermal_zone_device_register() - register a new thermal zone device
1589 * @type: the thermal zone device type
1590 * @trips: the number of trip points the thermal zone support
1591 * @mask: a bit string indicating the writeablility of trip points
1592 * @devdata: private device data
1593 * @ops: standard thermal zone device callbacks
1594 * @tzp: thermal zone platform parameters
1595 * @passive_delay: number of milliseconds to wait between polls when
1596 * performing passive cooling
1597 * @polling_delay: number of milliseconds to wait between polls when checking
1598 * whether trip points have been crossed (0 for interrupt
1601 * This interface function adds a new thermal zone device (sensor) to
1602 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1603 * thermal cooling devices registered at the same time.
1604 * thermal_zone_device_unregister() must be called when the device is no
1605 * longer needed. The passive cooling depends on the .get_trend() return value.
1607 * Return: a pointer to the created struct thermal_zone_device or an
1608 * in case of error, an ERR_PTR. Caller must check return value with
1609 * IS_ERR*() helpers.
1611 struct thermal_zone_device
*thermal_zone_device_register(const char *type
,
1612 int trips
, int mask
, void *devdata
,
1613 const struct thermal_zone_device_ops
*ops
,
1614 const struct thermal_zone_params
*tzp
,
1615 int passive_delay
, int polling_delay
)
1617 struct thermal_zone_device
*tz
;
1618 enum thermal_trip_type trip_type
;
1623 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
1624 return ERR_PTR(-EINVAL
);
1626 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0 || mask
>> trips
)
1627 return ERR_PTR(-EINVAL
);
1629 if (!ops
|| !ops
->get_temp
)
1630 return ERR_PTR(-EINVAL
);
1632 if (trips
> 0 && (!ops
->get_trip_type
|| !ops
->get_trip_temp
))
1633 return ERR_PTR(-EINVAL
);
1635 tz
= kzalloc(sizeof(struct thermal_zone_device
), GFP_KERNEL
);
1637 return ERR_PTR(-ENOMEM
);
1639 INIT_LIST_HEAD(&tz
->thermal_instances
);
1641 mutex_init(&tz
->lock
);
1642 result
= get_idr(&thermal_tz_idr
, &thermal_idr_lock
, &tz
->id
);
1645 return ERR_PTR(result
);
1648 strlcpy(tz
->type
, type
? : "", sizeof(tz
->type
));
1651 tz
->device
.class = &thermal_class
;
1652 tz
->devdata
= devdata
;
1654 tz
->passive_delay
= passive_delay
;
1655 tz
->polling_delay
= polling_delay
;
1657 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1658 result
= device_register(&tz
->device
);
1660 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1662 return ERR_PTR(result
);
1667 result
= device_create_file(&tz
->device
, &dev_attr_type
);
1672 result
= device_create_file(&tz
->device
, &dev_attr_temp
);
1676 if (ops
->get_mode
) {
1677 result
= device_create_file(&tz
->device
, &dev_attr_mode
);
1682 result
= create_trip_attrs(tz
, mask
);
1686 for (count
= 0; count
< trips
; count
++) {
1687 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
1688 if (trip_type
== THERMAL_TRIP_PASSIVE
)
1693 result
= device_create_file(&tz
->device
, &dev_attr_passive
);
1698 #ifdef CONFIG_THERMAL_EMULATION
1699 result
= device_create_file(&tz
->device
, &dev_attr_emul_temp
);
1703 /* Create policy attribute */
1704 result
= device_create_file(&tz
->device
, &dev_attr_policy
);
1708 /* Update 'this' zone's governor information */
1709 mutex_lock(&thermal_governor_lock
);
1712 tz
->governor
= __find_governor(tz
->tzp
->governor_name
);
1714 tz
->governor
= __find_governor(DEFAULT_THERMAL_GOVERNOR
);
1716 mutex_unlock(&thermal_governor_lock
);
1718 result
= thermal_add_hwmon_sysfs(tz
);
1722 mutex_lock(&thermal_list_lock
);
1723 list_add_tail(&tz
->node
, &thermal_tz_list
);
1724 mutex_unlock(&thermal_list_lock
);
1726 /* Bind cooling devices for this zone */
1729 INIT_DELAYED_WORK(&(tz
->poll_queue
), thermal_zone_device_check
);
1731 thermal_zone_device_update(tz
);
1737 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1738 device_unregister(&tz
->device
);
1739 return ERR_PTR(result
);
1741 EXPORT_SYMBOL_GPL(thermal_zone_device_register
);
1744 * thermal_device_unregister - removes the registered thermal zone device
1745 * @tz: the thermal zone device to remove
1747 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1750 const struct thermal_zone_params
*tzp
;
1751 struct thermal_cooling_device
*cdev
;
1752 struct thermal_zone_device
*pos
= NULL
;
1759 mutex_lock(&thermal_list_lock
);
1760 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1764 /* thermal zone device not found */
1765 mutex_unlock(&thermal_list_lock
);
1768 list_del(&tz
->node
);
1770 /* Unbind all cdevs associated with 'this' thermal zone */
1771 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
1772 if (tz
->ops
->unbind
) {
1773 tz
->ops
->unbind(tz
, cdev
);
1777 if (!tzp
|| !tzp
->tbp
)
1780 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1781 if (tzp
->tbp
[i
].cdev
== cdev
) {
1782 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1783 tzp
->tbp
[i
].cdev
= NULL
;
1788 mutex_unlock(&thermal_list_lock
);
1790 thermal_zone_device_set_polling(tz
, 0);
1793 device_remove_file(&tz
->device
, &dev_attr_type
);
1794 device_remove_file(&tz
->device
, &dev_attr_temp
);
1795 if (tz
->ops
->get_mode
)
1796 device_remove_file(&tz
->device
, &dev_attr_mode
);
1797 device_remove_file(&tz
->device
, &dev_attr_policy
);
1798 remove_trip_attrs(tz
);
1799 tz
->governor
= NULL
;
1801 thermal_remove_hwmon_sysfs(tz
);
1802 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1803 idr_destroy(&tz
->idr
);
1804 mutex_destroy(&tz
->lock
);
1805 device_unregister(&tz
->device
);
1808 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister
);
1811 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1812 * @name: thermal zone name to fetch the temperature
1814 * When only one zone is found with the passed name, returns a reference to it.
1816 * Return: On success returns a reference to an unique thermal zone with
1817 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1818 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1820 struct thermal_zone_device
*thermal_zone_get_zone_by_name(const char *name
)
1822 struct thermal_zone_device
*pos
= NULL
, *ref
= ERR_PTR(-EINVAL
);
1823 unsigned int found
= 0;
1828 mutex_lock(&thermal_list_lock
);
1829 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1830 if (!strnicmp(name
, pos
->type
, THERMAL_NAME_LENGTH
)) {
1834 mutex_unlock(&thermal_list_lock
);
1836 /* nothing has been found, thus an error code for it */
1838 ref
= ERR_PTR(-ENODEV
);
1840 /* Success only when an unique zone is found */
1841 ref
= ERR_PTR(-EEXIST
);
1846 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name
);
1849 static struct genl_family thermal_event_genl_family
= {
1850 .id
= GENL_ID_GENERATE
,
1851 .name
= THERMAL_GENL_FAMILY_NAME
,
1852 .version
= THERMAL_GENL_VERSION
,
1853 .maxattr
= THERMAL_GENL_ATTR_MAX
,
1856 static struct genl_multicast_group thermal_event_mcgrp
= {
1857 .name
= THERMAL_GENL_MCAST_GROUP_NAME
,
1860 int thermal_generate_netlink_event(struct thermal_zone_device
*tz
,
1863 struct sk_buff
*skb
;
1864 struct nlattr
*attr
;
1865 struct thermal_genl_event
*thermal_event
;
1869 static unsigned int thermal_event_seqnum
;
1874 /* allocate memory */
1875 size
= nla_total_size(sizeof(struct thermal_genl_event
)) +
1878 skb
= genlmsg_new(size
, GFP_ATOMIC
);
1882 /* add the genetlink message header */
1883 msg_header
= genlmsg_put(skb
, 0, thermal_event_seqnum
++,
1884 &thermal_event_genl_family
, 0,
1885 THERMAL_GENL_CMD_EVENT
);
1892 attr
= nla_reserve(skb
, THERMAL_GENL_ATTR_EVENT
,
1893 sizeof(struct thermal_genl_event
));
1900 thermal_event
= nla_data(attr
);
1901 if (!thermal_event
) {
1906 memset(thermal_event
, 0, sizeof(struct thermal_genl_event
));
1908 thermal_event
->orig
= tz
->id
;
1909 thermal_event
->event
= event
;
1911 /* send multicast genetlink message */
1912 result
= genlmsg_end(skb
, msg_header
);
1918 result
= genlmsg_multicast(skb
, 0, thermal_event_mcgrp
.id
, GFP_ATOMIC
);
1920 dev_err(&tz
->device
, "Failed to send netlink event:%d", result
);
1924 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event
);
1926 static int genetlink_init(void)
1930 result
= genl_register_family(&thermal_event_genl_family
);
1934 result
= genl_register_mc_group(&thermal_event_genl_family
,
1935 &thermal_event_mcgrp
);
1937 genl_unregister_family(&thermal_event_genl_family
);
1941 static void genetlink_exit(void)
1943 genl_unregister_family(&thermal_event_genl_family
);
1945 #else /* !CONFIG_NET */
1946 static inline int genetlink_init(void) { return 0; }
1947 static inline void genetlink_exit(void) {}
1948 #endif /* !CONFIG_NET */
1950 static int __init
thermal_register_governors(void)
1954 result
= thermal_gov_step_wise_register();
1958 result
= thermal_gov_fair_share_register();
1962 return thermal_gov_user_space_register();
1965 static void thermal_unregister_governors(void)
1967 thermal_gov_step_wise_unregister();
1968 thermal_gov_fair_share_unregister();
1969 thermal_gov_user_space_unregister();
1972 static int __init
thermal_init(void)
1976 result
= thermal_register_governors();
1980 result
= class_register(&thermal_class
);
1982 goto unregister_governors
;
1984 result
= genetlink_init();
1986 goto unregister_class
;
1990 unregister_governors
:
1991 thermal_unregister_governors();
1993 class_unregister(&thermal_class
);
1995 idr_destroy(&thermal_tz_idr
);
1996 idr_destroy(&thermal_cdev_idr
);
1997 mutex_destroy(&thermal_idr_lock
);
1998 mutex_destroy(&thermal_list_lock
);
1999 mutex_destroy(&thermal_governor_lock
);
2003 static void __exit
thermal_exit(void)
2006 class_unregister(&thermal_class
);
2007 thermal_unregister_governors();
2008 idr_destroy(&thermal_tz_idr
);
2009 idr_destroy(&thermal_cdev_idr
);
2010 mutex_destroy(&thermal_idr_lock
);
2011 mutex_destroy(&thermal_list_lock
);
2012 mutex_destroy(&thermal_governor_lock
);
2015 fs_initcall(thermal_init
);
2016 module_exit(thermal_exit
);