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 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/err.h>
29 #include <linux/kdev_t.h>
30 #include <linux/idr.h>
31 #include <linux/thermal.h>
32 #include <linux/spinlock.h>
33 #include <linux/reboot.h>
35 MODULE_AUTHOR("Zhang Rui");
36 MODULE_DESCRIPTION("Generic thermal management sysfs support");
37 MODULE_LICENSE("GPL");
39 #define PREFIX "Thermal: "
41 struct thermal_cooling_device_instance
{
43 char name
[THERMAL_NAME_LENGTH
];
44 struct thermal_zone_device
*tz
;
45 struct thermal_cooling_device
*cdev
;
47 char attr_name
[THERMAL_NAME_LENGTH
];
48 struct device_attribute attr
;
49 struct list_head node
;
52 static DEFINE_IDR(thermal_tz_idr
);
53 static DEFINE_IDR(thermal_cdev_idr
);
54 static DEFINE_MUTEX(thermal_idr_lock
);
56 static LIST_HEAD(thermal_tz_list
);
57 static LIST_HEAD(thermal_cdev_list
);
58 static DEFINE_MUTEX(thermal_list_lock
);
60 static int get_idr(struct idr
*idr
, struct mutex
*lock
, int *id
)
65 if (unlikely(idr_pre_get(idr
, GFP_KERNEL
) == 0))
70 err
= idr_get_new(idr
, NULL
, id
);
73 if (unlikely(err
== -EAGAIN
))
75 else if (unlikely(err
))
78 *id
= *id
& MAX_ID_MASK
;
82 static void release_idr(struct idr
*idr
, struct mutex
*lock
, int id
)
91 /* sys I/F for thermal zone */
93 #define to_thermal_zone(_dev) \
94 container_of(_dev, struct thermal_zone_device, device)
97 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
99 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
101 return sprintf(buf
, "%s\n", tz
->type
);
105 temp_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
107 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
111 if (!tz
->ops
->get_temp
)
114 ret
= tz
->ops
->get_temp(tz
, &temperature
);
119 return sprintf(buf
, "%ld\n", temperature
);
123 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
125 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
126 enum thermal_device_mode mode
;
129 if (!tz
->ops
->get_mode
)
132 result
= tz
->ops
->get_mode(tz
, &mode
);
136 return sprintf(buf
, "%s\n", mode
== THERMAL_DEVICE_ENABLED
? "enabled"
141 mode_store(struct device
*dev
, struct device_attribute
*attr
,
142 const char *buf
, size_t count
)
144 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
147 if (!tz
->ops
->set_mode
)
150 if (!strncmp(buf
, "enabled", sizeof("enabled")))
151 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_ENABLED
);
152 else if (!strncmp(buf
, "disabled", sizeof("disabled")))
153 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_DISABLED
);
164 trip_point_type_show(struct device
*dev
, struct device_attribute
*attr
,
167 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
168 enum thermal_trip_type type
;
171 if (!tz
->ops
->get_trip_type
)
174 if (!sscanf(attr
->attr
.name
, "trip_point_%d_type", &trip
))
177 result
= tz
->ops
->get_trip_type(tz
, trip
, &type
);
182 case THERMAL_TRIP_CRITICAL
:
183 return sprintf(buf
, "critical");
184 case THERMAL_TRIP_HOT
:
185 return sprintf(buf
, "hot");
186 case THERMAL_TRIP_PASSIVE
:
187 return sprintf(buf
, "passive");
188 case THERMAL_TRIP_ACTIVE
:
189 return sprintf(buf
, "active");
191 return sprintf(buf
, "unknown");
196 trip_point_temp_show(struct device
*dev
, struct device_attribute
*attr
,
199 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
203 if (!tz
->ops
->get_trip_temp
)
206 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
209 ret
= tz
->ops
->get_trip_temp(tz
, trip
, &temperature
);
214 return sprintf(buf
, "%ld\n", temperature
);
218 passive_store(struct device
*dev
, struct device_attribute
*attr
,
219 const char *buf
, size_t count
)
221 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
222 struct thermal_cooling_device
*cdev
= NULL
;
225 if (!sscanf(buf
, "%d\n", &state
))
228 if (state
&& !tz
->forced_passive
) {
229 mutex_lock(&thermal_list_lock
);
230 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
231 if (!strncmp("Processor", cdev
->type
,
232 sizeof("Processor")))
233 thermal_zone_bind_cooling_device(tz
,
237 mutex_unlock(&thermal_list_lock
);
238 } else if (!state
&& tz
->forced_passive
) {
239 mutex_lock(&thermal_list_lock
);
240 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
241 if (!strncmp("Processor", cdev
->type
,
242 sizeof("Processor")))
243 thermal_zone_unbind_cooling_device(tz
,
247 mutex_unlock(&thermal_list_lock
);
253 if (!tz
->passive_delay
)
254 tz
->passive_delay
= 1000;
256 if (!tz
->polling_delay
)
257 tz
->polling_delay
= 10000;
259 tz
->forced_passive
= state
;
261 thermal_zone_device_update(tz
);
267 passive_show(struct device
*dev
, struct device_attribute
*attr
,
270 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
272 return sprintf(buf
, "%d\n", tz
->forced_passive
);
275 static DEVICE_ATTR(type
, 0444, type_show
, NULL
);
276 static DEVICE_ATTR(temp
, 0444, temp_show
, NULL
);
277 static DEVICE_ATTR(mode
, 0644, mode_show
, mode_store
);
278 static DEVICE_ATTR(passive
, S_IRUGO
| S_IWUSR
, passive_show
, \
281 static struct device_attribute trip_point_attrs
[] = {
282 __ATTR(trip_point_0_type
, 0444, trip_point_type_show
, NULL
),
283 __ATTR(trip_point_0_temp
, 0444, trip_point_temp_show
, NULL
),
284 __ATTR(trip_point_1_type
, 0444, trip_point_type_show
, NULL
),
285 __ATTR(trip_point_1_temp
, 0444, trip_point_temp_show
, NULL
),
286 __ATTR(trip_point_2_type
, 0444, trip_point_type_show
, NULL
),
287 __ATTR(trip_point_2_temp
, 0444, trip_point_temp_show
, NULL
),
288 __ATTR(trip_point_3_type
, 0444, trip_point_type_show
, NULL
),
289 __ATTR(trip_point_3_temp
, 0444, trip_point_temp_show
, NULL
),
290 __ATTR(trip_point_4_type
, 0444, trip_point_type_show
, NULL
),
291 __ATTR(trip_point_4_temp
, 0444, trip_point_temp_show
, NULL
),
292 __ATTR(trip_point_5_type
, 0444, trip_point_type_show
, NULL
),
293 __ATTR(trip_point_5_temp
, 0444, trip_point_temp_show
, NULL
),
294 __ATTR(trip_point_6_type
, 0444, trip_point_type_show
, NULL
),
295 __ATTR(trip_point_6_temp
, 0444, trip_point_temp_show
, NULL
),
296 __ATTR(trip_point_7_type
, 0444, trip_point_type_show
, NULL
),
297 __ATTR(trip_point_7_temp
, 0444, trip_point_temp_show
, NULL
),
298 __ATTR(trip_point_8_type
, 0444, trip_point_type_show
, NULL
),
299 __ATTR(trip_point_8_temp
, 0444, trip_point_temp_show
, NULL
),
300 __ATTR(trip_point_9_type
, 0444, trip_point_type_show
, NULL
),
301 __ATTR(trip_point_9_temp
, 0444, trip_point_temp_show
, NULL
),
302 __ATTR(trip_point_10_type
, 0444, trip_point_type_show
, NULL
),
303 __ATTR(trip_point_10_temp
, 0444, trip_point_temp_show
, NULL
),
304 __ATTR(trip_point_11_type
, 0444, trip_point_type_show
, NULL
),
305 __ATTR(trip_point_11_temp
, 0444, trip_point_temp_show
, NULL
),
308 #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
310 result = device_create_file(_dev, \
311 &trip_point_attrs[_index * 2]); \
314 result = device_create_file(_dev, \
315 &trip_point_attrs[_index * 2 + 1]); \
318 #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
320 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
321 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
324 /* sys I/F for cooling device */
325 #define to_cooling_device(_dev) \
326 container_of(_dev, struct thermal_cooling_device, device)
329 thermal_cooling_device_type_show(struct device
*dev
,
330 struct device_attribute
*attr
, char *buf
)
332 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
334 return sprintf(buf
, "%s\n", cdev
->type
);
338 thermal_cooling_device_max_state_show(struct device
*dev
,
339 struct device_attribute
*attr
, char *buf
)
341 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
345 ret
= cdev
->ops
->get_max_state(cdev
, &state
);
348 return sprintf(buf
, "%ld\n", state
);
352 thermal_cooling_device_cur_state_show(struct device
*dev
,
353 struct device_attribute
*attr
, char *buf
)
355 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
359 ret
= cdev
->ops
->get_cur_state(cdev
, &state
);
362 return sprintf(buf
, "%ld\n", state
);
366 thermal_cooling_device_cur_state_store(struct device
*dev
,
367 struct device_attribute
*attr
,
368 const char *buf
, size_t count
)
370 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
374 if (!sscanf(buf
, "%ld\n", &state
))
380 result
= cdev
->ops
->set_cur_state(cdev
, state
);
386 static struct device_attribute dev_attr_cdev_type
=
387 __ATTR(type
, 0444, thermal_cooling_device_type_show
, NULL
);
388 static DEVICE_ATTR(max_state
, 0444,
389 thermal_cooling_device_max_state_show
, NULL
);
390 static DEVICE_ATTR(cur_state
, 0644,
391 thermal_cooling_device_cur_state_show
,
392 thermal_cooling_device_cur_state_store
);
395 thermal_cooling_device_trip_point_show(struct device
*dev
,
396 struct device_attribute
*attr
, char *buf
)
398 struct thermal_cooling_device_instance
*instance
;
401 container_of(attr
, struct thermal_cooling_device_instance
, attr
);
403 if (instance
->trip
== THERMAL_TRIPS_NONE
)
404 return sprintf(buf
, "-1\n");
406 return sprintf(buf
, "%d\n", instance
->trip
);
409 /* Device management */
411 #if defined(CONFIG_THERMAL_HWMON)
414 #include <linux/hwmon.h>
415 static LIST_HEAD(thermal_hwmon_list
);
418 name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
420 struct thermal_hwmon_device
*hwmon
= dev
->driver_data
;
421 return sprintf(buf
, "%s\n", hwmon
->type
);
423 static DEVICE_ATTR(name
, 0444, name_show
, NULL
);
426 temp_input_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
430 struct thermal_hwmon_attr
*hwmon_attr
431 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
432 struct thermal_zone_device
*tz
433 = container_of(hwmon_attr
, struct thermal_zone_device
,
436 ret
= tz
->ops
->get_temp(tz
, &temperature
);
441 return sprintf(buf
, "%ld\n", temperature
);
445 temp_crit_show(struct device
*dev
, struct device_attribute
*attr
,
448 struct thermal_hwmon_attr
*hwmon_attr
449 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
450 struct thermal_zone_device
*tz
451 = container_of(hwmon_attr
, struct thermal_zone_device
,
456 ret
= tz
->ops
->get_trip_temp(tz
, 0, &temperature
);
460 return sprintf(buf
, "%ld\n", temperature
);
465 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
467 struct thermal_hwmon_device
*hwmon
;
468 int new_hwmon_device
= 1;
471 mutex_lock(&thermal_list_lock
);
472 list_for_each_entry(hwmon
, &thermal_hwmon_list
, node
)
473 if (!strcmp(hwmon
->type
, tz
->type
)) {
474 new_hwmon_device
= 0;
475 mutex_unlock(&thermal_list_lock
);
476 goto register_sys_interface
;
478 mutex_unlock(&thermal_list_lock
);
480 hwmon
= kzalloc(sizeof(struct thermal_hwmon_device
), GFP_KERNEL
);
484 INIT_LIST_HEAD(&hwmon
->tz_list
);
485 strlcpy(hwmon
->type
, tz
->type
, THERMAL_NAME_LENGTH
);
486 hwmon
->device
= hwmon_device_register(NULL
);
487 if (IS_ERR(hwmon
->device
)) {
488 result
= PTR_ERR(hwmon
->device
);
491 hwmon
->device
->driver_data
= hwmon
;
492 result
= device_create_file(hwmon
->device
, &dev_attr_name
);
494 goto unregister_hwmon_device
;
496 register_sys_interface
:
500 snprintf(tz
->temp_input
.name
, THERMAL_NAME_LENGTH
,
501 "temp%d_input", hwmon
->count
);
502 tz
->temp_input
.attr
.attr
.name
= tz
->temp_input
.name
;
503 tz
->temp_input
.attr
.attr
.mode
= 0444;
504 tz
->temp_input
.attr
.show
= temp_input_show
;
505 result
= device_create_file(hwmon
->device
, &tz
->temp_input
.attr
);
507 goto unregister_hwmon_device
;
509 if (tz
->ops
->get_crit_temp
) {
510 unsigned long temperature
;
511 if (!tz
->ops
->get_crit_temp(tz
, &temperature
)) {
512 snprintf(tz
->temp_crit
.name
, THERMAL_NAME_LENGTH
,
513 "temp%d_crit", hwmon
->count
);
514 tz
->temp_crit
.attr
.attr
.name
= tz
->temp_crit
.name
;
515 tz
->temp_crit
.attr
.attr
.mode
= 0444;
516 tz
->temp_crit
.attr
.show
= temp_crit_show
;
517 result
= device_create_file(hwmon
->device
,
518 &tz
->temp_crit
.attr
);
520 goto unregister_hwmon_device
;
524 mutex_lock(&thermal_list_lock
);
525 if (new_hwmon_device
)
526 list_add_tail(&hwmon
->node
, &thermal_hwmon_list
);
527 list_add_tail(&tz
->hwmon_node
, &hwmon
->tz_list
);
528 mutex_unlock(&thermal_list_lock
);
532 unregister_hwmon_device
:
533 device_remove_file(hwmon
->device
, &tz
->temp_crit
.attr
);
534 device_remove_file(hwmon
->device
, &tz
->temp_input
.attr
);
535 if (new_hwmon_device
) {
536 device_remove_file(hwmon
->device
, &dev_attr_name
);
537 hwmon_device_unregister(hwmon
->device
);
540 if (new_hwmon_device
)
547 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
549 struct thermal_hwmon_device
*hwmon
= tz
->hwmon
;
552 device_remove_file(hwmon
->device
, &tz
->temp_input
.attr
);
553 device_remove_file(hwmon
->device
, &tz
->temp_crit
.attr
);
555 mutex_lock(&thermal_list_lock
);
556 list_del(&tz
->hwmon_node
);
557 if (!list_empty(&hwmon
->tz_list
)) {
558 mutex_unlock(&thermal_list_lock
);
561 list_del(&hwmon
->node
);
562 mutex_unlock(&thermal_list_lock
);
564 device_remove_file(hwmon
->device
, &dev_attr_name
);
565 hwmon_device_unregister(hwmon
->device
);
570 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
576 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
581 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
584 cancel_delayed_work(&(tz
->poll_queue
));
590 schedule_delayed_work(&(tz
->poll_queue
),
591 round_jiffies(msecs_to_jiffies(delay
)));
593 schedule_delayed_work(&(tz
->poll_queue
),
594 msecs_to_jiffies(delay
));
597 static void thermal_zone_device_passive(struct thermal_zone_device
*tz
,
598 int temp
, int trip_temp
, int trip
)
601 struct thermal_cooling_device_instance
*instance
;
602 struct thermal_cooling_device
*cdev
;
603 long state
, max_state
;
608 * Calculate the thermal trend (using the passive cooling equation)
609 * and modify the performance limit for all passive cooling devices
610 * accordingly. Note that we assume symmetry.
612 if (temp
>= trip_temp
) {
615 trend
= (tz
->tc1
* (temp
- tz
->last_temperature
)) +
616 (tz
->tc2
* (temp
- trip_temp
));
620 list_for_each_entry(instance
, &tz
->cooling_devices
,
622 if (instance
->trip
!= trip
)
624 cdev
= instance
->cdev
;
625 cdev
->ops
->get_cur_state(cdev
, &state
);
626 cdev
->ops
->get_max_state(cdev
, &max_state
);
627 if (state
++ < max_state
)
628 cdev
->ops
->set_cur_state(cdev
, state
);
630 } else if (trend
< 0) { /* Cooling off? */
631 list_for_each_entry(instance
, &tz
->cooling_devices
,
633 if (instance
->trip
!= trip
)
635 cdev
= instance
->cdev
;
636 cdev
->ops
->get_cur_state(cdev
, &state
);
637 cdev
->ops
->get_max_state(cdev
, &max_state
);
639 cdev
->ops
->set_cur_state(cdev
, --state
);
648 * Implement passive cooling hysteresis to slowly increase performance
649 * and avoid thrashing around the passive trip point. Note that we
652 list_for_each_entry(instance
, &tz
->cooling_devices
, node
) {
653 if (instance
->trip
!= trip
)
655 cdev
= instance
->cdev
;
656 cdev
->ops
->get_cur_state(cdev
, &state
);
657 cdev
->ops
->get_max_state(cdev
, &max_state
);
659 cdev
->ops
->set_cur_state(cdev
, --state
);
665 static void thermal_zone_device_check(struct work_struct
*work
)
667 struct thermal_zone_device
*tz
= container_of(work
, struct
670 thermal_zone_device_update(tz
);
674 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
675 * @tz: thermal zone device
676 * @trip: indicates which trip point the cooling devices is
677 * associated with in this thermal zone.
678 * @cdev: thermal cooling device
680 * This function is usually called in the thermal zone device .bind callback.
682 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
684 struct thermal_cooling_device
*cdev
)
686 struct thermal_cooling_device_instance
*dev
;
687 struct thermal_cooling_device_instance
*pos
;
688 struct thermal_zone_device
*pos1
;
689 struct thermal_cooling_device
*pos2
;
692 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
695 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
699 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
704 if (tz
!= pos1
|| cdev
!= pos2
)
708 kzalloc(sizeof(struct thermal_cooling_device_instance
), GFP_KERNEL
);
714 result
= get_idr(&tz
->idr
, &tz
->lock
, &dev
->id
);
718 sprintf(dev
->name
, "cdev%d", dev
->id
);
720 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
724 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
725 dev
->attr
.attr
.name
= dev
->attr_name
;
726 dev
->attr
.attr
.mode
= 0444;
727 dev
->attr
.show
= thermal_cooling_device_trip_point_show
;
728 result
= device_create_file(&tz
->device
, &dev
->attr
);
730 goto remove_symbol_link
;
732 mutex_lock(&tz
->lock
);
733 list_for_each_entry(pos
, &tz
->cooling_devices
, node
)
734 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
739 list_add_tail(&dev
->node
, &tz
->cooling_devices
);
740 mutex_unlock(&tz
->lock
);
745 device_remove_file(&tz
->device
, &dev
->attr
);
747 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
749 release_idr(&tz
->idr
, &tz
->lock
, dev
->id
);
755 EXPORT_SYMBOL(thermal_zone_bind_cooling_device
);
758 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
759 * @tz: thermal zone device
760 * @trip: indicates which trip point the cooling devices is
761 * associated with in this thermal zone.
762 * @cdev: thermal cooling device
764 * This function is usually called in the thermal zone device .unbind callback.
766 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
768 struct thermal_cooling_device
*cdev
)
770 struct thermal_cooling_device_instance
*pos
, *next
;
772 mutex_lock(&tz
->lock
);
773 list_for_each_entry_safe(pos
, next
, &tz
->cooling_devices
, node
) {
774 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
775 list_del(&pos
->node
);
776 mutex_unlock(&tz
->lock
);
780 mutex_unlock(&tz
->lock
);
785 device_remove_file(&tz
->device
, &pos
->attr
);
786 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
787 release_idr(&tz
->idr
, &tz
->lock
, pos
->id
);
792 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device
);
794 static void thermal_release(struct device
*dev
)
796 struct thermal_zone_device
*tz
;
797 struct thermal_cooling_device
*cdev
;
799 if (!strncmp(dev_name(dev
), "thermal_zone", sizeof "thermal_zone" - 1)) {
800 tz
= to_thermal_zone(dev
);
803 cdev
= to_cooling_device(dev
);
808 static struct class thermal_class
= {
810 .dev_release
= thermal_release
,
814 * thermal_cooling_device_register - register a new thermal cooling device
815 * @type: the thermal cooling device type.
816 * @devdata: device private data.
817 * @ops: standard thermal cooling devices callbacks.
819 struct thermal_cooling_device
*thermal_cooling_device_register(char *type
,
822 thermal_cooling_device_ops
825 struct thermal_cooling_device
*cdev
;
826 struct thermal_zone_device
*pos
;
829 if (strlen(type
) >= THERMAL_NAME_LENGTH
)
830 return ERR_PTR(-EINVAL
);
832 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
834 return ERR_PTR(-EINVAL
);
836 cdev
= kzalloc(sizeof(struct thermal_cooling_device
), GFP_KERNEL
);
838 return ERR_PTR(-ENOMEM
);
840 result
= get_idr(&thermal_cdev_idr
, &thermal_idr_lock
, &cdev
->id
);
843 return ERR_PTR(result
);
846 strcpy(cdev
->type
, type
);
848 cdev
->device
.class = &thermal_class
;
849 cdev
->devdata
= devdata
;
850 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
851 result
= device_register(&cdev
->device
);
853 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
855 return ERR_PTR(result
);
860 result
= device_create_file(&cdev
->device
, &dev_attr_cdev_type
);
865 result
= device_create_file(&cdev
->device
, &dev_attr_max_state
);
869 result
= device_create_file(&cdev
->device
, &dev_attr_cur_state
);
873 mutex_lock(&thermal_list_lock
);
874 list_add(&cdev
->node
, &thermal_cdev_list
);
875 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
878 result
= pos
->ops
->bind(pos
, cdev
);
883 mutex_unlock(&thermal_list_lock
);
889 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
890 device_unregister(&cdev
->device
);
891 return ERR_PTR(result
);
894 EXPORT_SYMBOL(thermal_cooling_device_register
);
897 * thermal_cooling_device_unregister - removes the registered thermal cooling device
898 * @cdev: the thermal cooling device to remove.
900 * thermal_cooling_device_unregister() must be called when the device is no
903 void thermal_cooling_device_unregister(struct
904 thermal_cooling_device
907 struct thermal_zone_device
*tz
;
908 struct thermal_cooling_device
*pos
= NULL
;
913 mutex_lock(&thermal_list_lock
);
914 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
918 /* thermal cooling device not found */
919 mutex_unlock(&thermal_list_lock
);
922 list_del(&cdev
->node
);
923 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
924 if (!tz
->ops
->unbind
)
926 tz
->ops
->unbind(tz
, cdev
);
928 mutex_unlock(&thermal_list_lock
);
930 device_remove_file(&cdev
->device
, &dev_attr_cdev_type
);
931 device_remove_file(&cdev
->device
, &dev_attr_max_state
);
932 device_remove_file(&cdev
->device
, &dev_attr_cur_state
);
934 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
935 device_unregister(&cdev
->device
);
939 EXPORT_SYMBOL(thermal_cooling_device_unregister
);
942 * thermal_zone_device_update - force an update of a thermal zone's state
943 * @ttz: the thermal zone to update
946 void thermal_zone_device_update(struct thermal_zone_device
*tz
)
949 long temp
, trip_temp
;
950 enum thermal_trip_type trip_type
;
951 struct thermal_cooling_device_instance
*instance
;
952 struct thermal_cooling_device
*cdev
;
954 mutex_lock(&tz
->lock
);
956 tz
->ops
->get_temp(tz
, &temp
);
958 for (count
= 0; count
< tz
->trips
; count
++) {
959 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
960 tz
->ops
->get_trip_temp(tz
, count
, &trip_temp
);
963 case THERMAL_TRIP_CRITICAL
:
964 if (temp
>= trip_temp
) {
966 ret
= tz
->ops
->notify(tz
, count
,
970 "Critical temperature reached (%ld C), shutting down.\n",
972 orderly_poweroff(true);
976 case THERMAL_TRIP_HOT
:
977 if (temp
>= trip_temp
)
979 tz
->ops
->notify(tz
, count
, trip_type
);
981 case THERMAL_TRIP_ACTIVE
:
982 list_for_each_entry(instance
, &tz
->cooling_devices
,
984 if (instance
->trip
!= count
)
987 cdev
= instance
->cdev
;
989 if (temp
>= trip_temp
)
990 cdev
->ops
->set_cur_state(cdev
, 1);
992 cdev
->ops
->set_cur_state(cdev
, 0);
995 case THERMAL_TRIP_PASSIVE
:
996 if (temp
>= trip_temp
|| tz
->passive
)
997 thermal_zone_device_passive(tz
, temp
,
1003 if (tz
->forced_passive
)
1004 thermal_zone_device_passive(tz
, temp
, tz
->forced_passive
,
1005 THERMAL_TRIPS_NONE
);
1007 tz
->last_temperature
= temp
;
1009 thermal_zone_device_set_polling(tz
, tz
->passive_delay
);
1010 else if (tz
->polling_delay
)
1011 thermal_zone_device_set_polling(tz
, tz
->polling_delay
);
1012 mutex_unlock(&tz
->lock
);
1014 EXPORT_SYMBOL(thermal_zone_device_update
);
1017 * thermal_zone_device_register - register a new thermal zone device
1018 * @type: the thermal zone device type
1019 * @trips: the number of trip points the thermal zone support
1020 * @devdata: private device data
1021 * @ops: standard thermal zone device callbacks
1022 * @tc1: thermal coefficient 1 for passive calculations
1023 * @tc2: thermal coefficient 2 for passive calculations
1024 * @passive_delay: number of milliseconds to wait between polls when
1025 * performing passive cooling
1026 * @polling_delay: number of milliseconds to wait between polls when checking
1027 * whether trip points have been crossed (0 for interrupt
1030 * thermal_zone_device_unregister() must be called when the device is no
1031 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1032 * section 11.1.5.1 of the ACPI specification 3.0.
1034 struct thermal_zone_device
*thermal_zone_device_register(char *type
,
1036 void *devdata
, struct
1037 thermal_zone_device_ops
1043 struct thermal_zone_device
*tz
;
1044 struct thermal_cooling_device
*pos
;
1045 enum thermal_trip_type trip_type
;
1050 if (strlen(type
) >= THERMAL_NAME_LENGTH
)
1051 return ERR_PTR(-EINVAL
);
1053 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0)
1054 return ERR_PTR(-EINVAL
);
1056 if (!ops
|| !ops
->get_temp
)
1057 return ERR_PTR(-EINVAL
);
1059 tz
= kzalloc(sizeof(struct thermal_zone_device
), GFP_KERNEL
);
1061 return ERR_PTR(-ENOMEM
);
1063 INIT_LIST_HEAD(&tz
->cooling_devices
);
1065 mutex_init(&tz
->lock
);
1066 result
= get_idr(&thermal_tz_idr
, &thermal_idr_lock
, &tz
->id
);
1069 return ERR_PTR(result
);
1072 strcpy(tz
->type
, type
);
1074 tz
->device
.class = &thermal_class
;
1075 tz
->devdata
= devdata
;
1079 tz
->passive_delay
= passive_delay
;
1080 tz
->polling_delay
= polling_delay
;
1082 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1083 result
= device_register(&tz
->device
);
1085 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1087 return ERR_PTR(result
);
1092 result
= device_create_file(&tz
->device
, &dev_attr_type
);
1097 result
= device_create_file(&tz
->device
, &dev_attr_temp
);
1101 if (ops
->get_mode
) {
1102 result
= device_create_file(&tz
->device
, &dev_attr_mode
);
1107 for (count
= 0; count
< trips
; count
++) {
1108 TRIP_POINT_ATTR_ADD(&tz
->device
, count
, result
);
1111 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
1112 if (trip_type
== THERMAL_TRIP_PASSIVE
)
1117 result
= device_create_file(&tz
->device
,
1123 result
= thermal_add_hwmon_sysfs(tz
);
1127 mutex_lock(&thermal_list_lock
);
1128 list_add_tail(&tz
->node
, &thermal_tz_list
);
1130 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1131 result
= ops
->bind(tz
, pos
);
1135 mutex_unlock(&thermal_list_lock
);
1137 INIT_DELAYED_WORK(&(tz
->poll_queue
), thermal_zone_device_check
);
1139 thermal_zone_device_update(tz
);
1145 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1146 device_unregister(&tz
->device
);
1147 return ERR_PTR(result
);
1150 EXPORT_SYMBOL(thermal_zone_device_register
);
1153 * thermal_device_unregister - removes the registered thermal zone device
1154 * @tz: the thermal zone device to remove
1156 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1158 struct thermal_cooling_device
*cdev
;
1159 struct thermal_zone_device
*pos
= NULL
;
1165 mutex_lock(&thermal_list_lock
);
1166 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1170 /* thermal zone device not found */
1171 mutex_unlock(&thermal_list_lock
);
1174 list_del(&tz
->node
);
1175 if (tz
->ops
->unbind
)
1176 list_for_each_entry(cdev
, &thermal_cdev_list
, node
)
1177 tz
->ops
->unbind(tz
, cdev
);
1178 mutex_unlock(&thermal_list_lock
);
1180 thermal_zone_device_set_polling(tz
, 0);
1183 device_remove_file(&tz
->device
, &dev_attr_type
);
1184 device_remove_file(&tz
->device
, &dev_attr_temp
);
1185 if (tz
->ops
->get_mode
)
1186 device_remove_file(&tz
->device
, &dev_attr_mode
);
1188 for (count
= 0; count
< tz
->trips
; count
++)
1189 TRIP_POINT_ATTR_REMOVE(&tz
->device
, count
);
1191 thermal_remove_hwmon_sysfs(tz
);
1192 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1193 idr_destroy(&tz
->idr
);
1194 mutex_destroy(&tz
->lock
);
1195 device_unregister(&tz
->device
);
1199 EXPORT_SYMBOL(thermal_zone_device_unregister
);
1201 static int __init
thermal_init(void)
1205 result
= class_register(&thermal_class
);
1207 idr_destroy(&thermal_tz_idr
);
1208 idr_destroy(&thermal_cdev_idr
);
1209 mutex_destroy(&thermal_idr_lock
);
1210 mutex_destroy(&thermal_list_lock
);
1215 static void __exit
thermal_exit(void)
1217 class_unregister(&thermal_class
);
1218 idr_destroy(&thermal_tz_idr
);
1219 idr_destroy(&thermal_cdev_idr
);
1220 mutex_destroy(&thermal_idr_lock
);
1221 mutex_destroy(&thermal_list_lock
);
1224 subsys_initcall(thermal_init
);
1225 module_exit(thermal_exit
);