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\n");
184 case THERMAL_TRIP_HOT
:
185 return sprintf(buf
, "hot\n");
186 case THERMAL_TRIP_PASSIVE
:
187 return sprintf(buf
, "passive\n");
188 case THERMAL_TRIP_ACTIVE
:
189 return sprintf(buf
, "active\n");
191 return sprintf(buf
, "unknown\n");
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 /* sanity check: values below 1000 millicelcius don't make sense
229 * and can cause the system to go into a thermal heart attack
231 if (state
&& state
< 1000)
234 if (state
&& !tz
->forced_passive
) {
235 mutex_lock(&thermal_list_lock
);
236 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
237 if (!strncmp("Processor", cdev
->type
,
238 sizeof("Processor")))
239 thermal_zone_bind_cooling_device(tz
,
243 mutex_unlock(&thermal_list_lock
);
244 if (!tz
->passive_delay
)
245 tz
->passive_delay
= 1000;
246 } else if (!state
&& tz
->forced_passive
) {
247 mutex_lock(&thermal_list_lock
);
248 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
249 if (!strncmp("Processor", cdev
->type
,
250 sizeof("Processor")))
251 thermal_zone_unbind_cooling_device(tz
,
255 mutex_unlock(&thermal_list_lock
);
256 tz
->passive_delay
= 0;
262 tz
->forced_passive
= state
;
264 thermal_zone_device_update(tz
);
270 passive_show(struct device
*dev
, struct device_attribute
*attr
,
273 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
275 return sprintf(buf
, "%d\n", tz
->forced_passive
);
278 static DEVICE_ATTR(type
, 0444, type_show
, NULL
);
279 static DEVICE_ATTR(temp
, 0444, temp_show
, NULL
);
280 static DEVICE_ATTR(mode
, 0644, mode_show
, mode_store
);
281 static DEVICE_ATTR(passive
, S_IRUGO
| S_IWUSR
, passive_show
, \
284 static struct device_attribute trip_point_attrs
[] = {
285 __ATTR(trip_point_0_type
, 0444, trip_point_type_show
, NULL
),
286 __ATTR(trip_point_0_temp
, 0444, trip_point_temp_show
, NULL
),
287 __ATTR(trip_point_1_type
, 0444, trip_point_type_show
, NULL
),
288 __ATTR(trip_point_1_temp
, 0444, trip_point_temp_show
, NULL
),
289 __ATTR(trip_point_2_type
, 0444, trip_point_type_show
, NULL
),
290 __ATTR(trip_point_2_temp
, 0444, trip_point_temp_show
, NULL
),
291 __ATTR(trip_point_3_type
, 0444, trip_point_type_show
, NULL
),
292 __ATTR(trip_point_3_temp
, 0444, trip_point_temp_show
, NULL
),
293 __ATTR(trip_point_4_type
, 0444, trip_point_type_show
, NULL
),
294 __ATTR(trip_point_4_temp
, 0444, trip_point_temp_show
, NULL
),
295 __ATTR(trip_point_5_type
, 0444, trip_point_type_show
, NULL
),
296 __ATTR(trip_point_5_temp
, 0444, trip_point_temp_show
, NULL
),
297 __ATTR(trip_point_6_type
, 0444, trip_point_type_show
, NULL
),
298 __ATTR(trip_point_6_temp
, 0444, trip_point_temp_show
, NULL
),
299 __ATTR(trip_point_7_type
, 0444, trip_point_type_show
, NULL
),
300 __ATTR(trip_point_7_temp
, 0444, trip_point_temp_show
, NULL
),
301 __ATTR(trip_point_8_type
, 0444, trip_point_type_show
, NULL
),
302 __ATTR(trip_point_8_temp
, 0444, trip_point_temp_show
, NULL
),
303 __ATTR(trip_point_9_type
, 0444, trip_point_type_show
, NULL
),
304 __ATTR(trip_point_9_temp
, 0444, trip_point_temp_show
, NULL
),
305 __ATTR(trip_point_10_type
, 0444, trip_point_type_show
, NULL
),
306 __ATTR(trip_point_10_temp
, 0444, trip_point_temp_show
, NULL
),
307 __ATTR(trip_point_11_type
, 0444, trip_point_type_show
, NULL
),
308 __ATTR(trip_point_11_temp
, 0444, trip_point_temp_show
, NULL
),
311 #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
313 result = device_create_file(_dev, \
314 &trip_point_attrs[_index * 2]); \
317 result = device_create_file(_dev, \
318 &trip_point_attrs[_index * 2 + 1]); \
321 #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
323 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
324 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
327 /* sys I/F for cooling device */
328 #define to_cooling_device(_dev) \
329 container_of(_dev, struct thermal_cooling_device, device)
332 thermal_cooling_device_type_show(struct device
*dev
,
333 struct device_attribute
*attr
, char *buf
)
335 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
337 return sprintf(buf
, "%s\n", cdev
->type
);
341 thermal_cooling_device_max_state_show(struct device
*dev
,
342 struct device_attribute
*attr
, char *buf
)
344 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
348 ret
= cdev
->ops
->get_max_state(cdev
, &state
);
351 return sprintf(buf
, "%ld\n", state
);
355 thermal_cooling_device_cur_state_show(struct device
*dev
,
356 struct device_attribute
*attr
, char *buf
)
358 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
362 ret
= cdev
->ops
->get_cur_state(cdev
, &state
);
365 return sprintf(buf
, "%ld\n", state
);
369 thermal_cooling_device_cur_state_store(struct device
*dev
,
370 struct device_attribute
*attr
,
371 const char *buf
, size_t count
)
373 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
377 if (!sscanf(buf
, "%ld\n", &state
))
383 result
= cdev
->ops
->set_cur_state(cdev
, state
);
389 static struct device_attribute dev_attr_cdev_type
=
390 __ATTR(type
, 0444, thermal_cooling_device_type_show
, NULL
);
391 static DEVICE_ATTR(max_state
, 0444,
392 thermal_cooling_device_max_state_show
, NULL
);
393 static DEVICE_ATTR(cur_state
, 0644,
394 thermal_cooling_device_cur_state_show
,
395 thermal_cooling_device_cur_state_store
);
398 thermal_cooling_device_trip_point_show(struct device
*dev
,
399 struct device_attribute
*attr
, char *buf
)
401 struct thermal_cooling_device_instance
*instance
;
404 container_of(attr
, struct thermal_cooling_device_instance
, attr
);
406 if (instance
->trip
== THERMAL_TRIPS_NONE
)
407 return sprintf(buf
, "-1\n");
409 return sprintf(buf
, "%d\n", instance
->trip
);
412 /* Device management */
414 #if defined(CONFIG_THERMAL_HWMON)
417 #include <linux/hwmon.h>
418 static LIST_HEAD(thermal_hwmon_list
);
421 name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
423 struct thermal_hwmon_device
*hwmon
= dev_get_drvdata(dev
);
424 return sprintf(buf
, "%s\n", hwmon
->type
);
426 static DEVICE_ATTR(name
, 0444, name_show
, NULL
);
429 temp_input_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
433 struct thermal_hwmon_attr
*hwmon_attr
434 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
435 struct thermal_zone_device
*tz
436 = container_of(hwmon_attr
, struct thermal_zone_device
,
439 ret
= tz
->ops
->get_temp(tz
, &temperature
);
444 return sprintf(buf
, "%ld\n", temperature
);
448 temp_crit_show(struct device
*dev
, struct device_attribute
*attr
,
451 struct thermal_hwmon_attr
*hwmon_attr
452 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
453 struct thermal_zone_device
*tz
454 = container_of(hwmon_attr
, struct thermal_zone_device
,
459 ret
= tz
->ops
->get_trip_temp(tz
, 0, &temperature
);
463 return sprintf(buf
, "%ld\n", temperature
);
468 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
470 struct thermal_hwmon_device
*hwmon
;
471 int new_hwmon_device
= 1;
474 mutex_lock(&thermal_list_lock
);
475 list_for_each_entry(hwmon
, &thermal_hwmon_list
, node
)
476 if (!strcmp(hwmon
->type
, tz
->type
)) {
477 new_hwmon_device
= 0;
478 mutex_unlock(&thermal_list_lock
);
479 goto register_sys_interface
;
481 mutex_unlock(&thermal_list_lock
);
483 hwmon
= kzalloc(sizeof(struct thermal_hwmon_device
), GFP_KERNEL
);
487 INIT_LIST_HEAD(&hwmon
->tz_list
);
488 strlcpy(hwmon
->type
, tz
->type
, THERMAL_NAME_LENGTH
);
489 hwmon
->device
= hwmon_device_register(NULL
);
490 if (IS_ERR(hwmon
->device
)) {
491 result
= PTR_ERR(hwmon
->device
);
494 dev_set_drvdata(hwmon
->device
, hwmon
);
495 result
= device_create_file(hwmon
->device
, &dev_attr_name
);
497 goto unregister_hwmon_device
;
499 register_sys_interface
:
503 snprintf(tz
->temp_input
.name
, THERMAL_NAME_LENGTH
,
504 "temp%d_input", hwmon
->count
);
505 tz
->temp_input
.attr
.attr
.name
= tz
->temp_input
.name
;
506 tz
->temp_input
.attr
.attr
.mode
= 0444;
507 tz
->temp_input
.attr
.show
= temp_input_show
;
508 result
= device_create_file(hwmon
->device
, &tz
->temp_input
.attr
);
510 goto unregister_hwmon_device
;
512 if (tz
->ops
->get_crit_temp
) {
513 unsigned long temperature
;
514 if (!tz
->ops
->get_crit_temp(tz
, &temperature
)) {
515 snprintf(tz
->temp_crit
.name
, THERMAL_NAME_LENGTH
,
516 "temp%d_crit", hwmon
->count
);
517 tz
->temp_crit
.attr
.attr
.name
= tz
->temp_crit
.name
;
518 tz
->temp_crit
.attr
.attr
.mode
= 0444;
519 tz
->temp_crit
.attr
.show
= temp_crit_show
;
520 result
= device_create_file(hwmon
->device
,
521 &tz
->temp_crit
.attr
);
523 goto unregister_hwmon_device
;
527 mutex_lock(&thermal_list_lock
);
528 if (new_hwmon_device
)
529 list_add_tail(&hwmon
->node
, &thermal_hwmon_list
);
530 list_add_tail(&tz
->hwmon_node
, &hwmon
->tz_list
);
531 mutex_unlock(&thermal_list_lock
);
535 unregister_hwmon_device
:
536 device_remove_file(hwmon
->device
, &tz
->temp_crit
.attr
);
537 device_remove_file(hwmon
->device
, &tz
->temp_input
.attr
);
538 if (new_hwmon_device
) {
539 device_remove_file(hwmon
->device
, &dev_attr_name
);
540 hwmon_device_unregister(hwmon
->device
);
543 if (new_hwmon_device
)
550 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
552 struct thermal_hwmon_device
*hwmon
= tz
->hwmon
;
555 device_remove_file(hwmon
->device
, &tz
->temp_input
.attr
);
556 device_remove_file(hwmon
->device
, &tz
->temp_crit
.attr
);
558 mutex_lock(&thermal_list_lock
);
559 list_del(&tz
->hwmon_node
);
560 if (!list_empty(&hwmon
->tz_list
)) {
561 mutex_unlock(&thermal_list_lock
);
564 list_del(&hwmon
->node
);
565 mutex_unlock(&thermal_list_lock
);
567 device_remove_file(hwmon
->device
, &dev_attr_name
);
568 hwmon_device_unregister(hwmon
->device
);
573 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
579 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
584 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
587 cancel_delayed_work(&(tz
->poll_queue
));
593 schedule_delayed_work(&(tz
->poll_queue
),
594 round_jiffies(msecs_to_jiffies(delay
)));
596 schedule_delayed_work(&(tz
->poll_queue
),
597 msecs_to_jiffies(delay
));
600 static void thermal_zone_device_passive(struct thermal_zone_device
*tz
,
601 int temp
, int trip_temp
, int trip
)
604 struct thermal_cooling_device_instance
*instance
;
605 struct thermal_cooling_device
*cdev
;
606 long state
, max_state
;
611 * Calculate the thermal trend (using the passive cooling equation)
612 * and modify the performance limit for all passive cooling devices
613 * accordingly. Note that we assume symmetry.
615 if (temp
>= trip_temp
) {
618 trend
= (tz
->tc1
* (temp
- tz
->last_temperature
)) +
619 (tz
->tc2
* (temp
- trip_temp
));
623 list_for_each_entry(instance
, &tz
->cooling_devices
,
625 if (instance
->trip
!= trip
)
627 cdev
= instance
->cdev
;
628 cdev
->ops
->get_cur_state(cdev
, &state
);
629 cdev
->ops
->get_max_state(cdev
, &max_state
);
630 if (state
++ < max_state
)
631 cdev
->ops
->set_cur_state(cdev
, state
);
633 } else if (trend
< 0) { /* Cooling off? */
634 list_for_each_entry(instance
, &tz
->cooling_devices
,
636 if (instance
->trip
!= trip
)
638 cdev
= instance
->cdev
;
639 cdev
->ops
->get_cur_state(cdev
, &state
);
640 cdev
->ops
->get_max_state(cdev
, &max_state
);
642 cdev
->ops
->set_cur_state(cdev
, --state
);
651 * Implement passive cooling hysteresis to slowly increase performance
652 * and avoid thrashing around the passive trip point. Note that we
655 list_for_each_entry(instance
, &tz
->cooling_devices
, node
) {
656 if (instance
->trip
!= trip
)
658 cdev
= instance
->cdev
;
659 cdev
->ops
->get_cur_state(cdev
, &state
);
660 cdev
->ops
->get_max_state(cdev
, &max_state
);
662 cdev
->ops
->set_cur_state(cdev
, --state
);
668 static void thermal_zone_device_check(struct work_struct
*work
)
670 struct thermal_zone_device
*tz
= container_of(work
, struct
673 thermal_zone_device_update(tz
);
677 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
678 * @tz: thermal zone device
679 * @trip: indicates which trip point the cooling devices is
680 * associated with in this thermal zone.
681 * @cdev: thermal cooling device
683 * This function is usually called in the thermal zone device .bind callback.
685 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
687 struct thermal_cooling_device
*cdev
)
689 struct thermal_cooling_device_instance
*dev
;
690 struct thermal_cooling_device_instance
*pos
;
691 struct thermal_zone_device
*pos1
;
692 struct thermal_cooling_device
*pos2
;
695 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
698 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
702 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
707 if (tz
!= pos1
|| cdev
!= pos2
)
711 kzalloc(sizeof(struct thermal_cooling_device_instance
), GFP_KERNEL
);
717 result
= get_idr(&tz
->idr
, &tz
->lock
, &dev
->id
);
721 sprintf(dev
->name
, "cdev%d", dev
->id
);
723 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
727 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
728 dev
->attr
.attr
.name
= dev
->attr_name
;
729 dev
->attr
.attr
.mode
= 0444;
730 dev
->attr
.show
= thermal_cooling_device_trip_point_show
;
731 result
= device_create_file(&tz
->device
, &dev
->attr
);
733 goto remove_symbol_link
;
735 mutex_lock(&tz
->lock
);
736 list_for_each_entry(pos
, &tz
->cooling_devices
, node
)
737 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
742 list_add_tail(&dev
->node
, &tz
->cooling_devices
);
743 mutex_unlock(&tz
->lock
);
748 device_remove_file(&tz
->device
, &dev
->attr
);
750 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
752 release_idr(&tz
->idr
, &tz
->lock
, dev
->id
);
758 EXPORT_SYMBOL(thermal_zone_bind_cooling_device
);
761 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
762 * @tz: thermal zone device
763 * @trip: indicates which trip point the cooling devices is
764 * associated with in this thermal zone.
765 * @cdev: thermal cooling device
767 * This function is usually called in the thermal zone device .unbind callback.
769 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
771 struct thermal_cooling_device
*cdev
)
773 struct thermal_cooling_device_instance
*pos
, *next
;
775 mutex_lock(&tz
->lock
);
776 list_for_each_entry_safe(pos
, next
, &tz
->cooling_devices
, node
) {
777 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
778 list_del(&pos
->node
);
779 mutex_unlock(&tz
->lock
);
783 mutex_unlock(&tz
->lock
);
788 device_remove_file(&tz
->device
, &pos
->attr
);
789 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
790 release_idr(&tz
->idr
, &tz
->lock
, pos
->id
);
795 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device
);
797 static void thermal_release(struct device
*dev
)
799 struct thermal_zone_device
*tz
;
800 struct thermal_cooling_device
*cdev
;
802 if (!strncmp(dev_name(dev
), "thermal_zone", sizeof "thermal_zone" - 1)) {
803 tz
= to_thermal_zone(dev
);
806 cdev
= to_cooling_device(dev
);
811 static struct class thermal_class
= {
813 .dev_release
= thermal_release
,
817 * thermal_cooling_device_register - register a new thermal cooling device
818 * @type: the thermal cooling device type.
819 * @devdata: device private data.
820 * @ops: standard thermal cooling devices callbacks.
822 struct thermal_cooling_device
*thermal_cooling_device_register(char *type
,
825 thermal_cooling_device_ops
828 struct thermal_cooling_device
*cdev
;
829 struct thermal_zone_device
*pos
;
832 if (strlen(type
) >= THERMAL_NAME_LENGTH
)
833 return ERR_PTR(-EINVAL
);
835 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
837 return ERR_PTR(-EINVAL
);
839 cdev
= kzalloc(sizeof(struct thermal_cooling_device
), GFP_KERNEL
);
841 return ERR_PTR(-ENOMEM
);
843 result
= get_idr(&thermal_cdev_idr
, &thermal_idr_lock
, &cdev
->id
);
846 return ERR_PTR(result
);
849 strcpy(cdev
->type
, type
);
851 cdev
->device
.class = &thermal_class
;
852 cdev
->devdata
= devdata
;
853 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
854 result
= device_register(&cdev
->device
);
856 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
858 return ERR_PTR(result
);
863 result
= device_create_file(&cdev
->device
, &dev_attr_cdev_type
);
868 result
= device_create_file(&cdev
->device
, &dev_attr_max_state
);
872 result
= device_create_file(&cdev
->device
, &dev_attr_cur_state
);
876 mutex_lock(&thermal_list_lock
);
877 list_add(&cdev
->node
, &thermal_cdev_list
);
878 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
881 result
= pos
->ops
->bind(pos
, cdev
);
886 mutex_unlock(&thermal_list_lock
);
892 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
893 device_unregister(&cdev
->device
);
894 return ERR_PTR(result
);
897 EXPORT_SYMBOL(thermal_cooling_device_register
);
900 * thermal_cooling_device_unregister - removes the registered thermal cooling device
901 * @cdev: the thermal cooling device to remove.
903 * thermal_cooling_device_unregister() must be called when the device is no
906 void thermal_cooling_device_unregister(struct
907 thermal_cooling_device
910 struct thermal_zone_device
*tz
;
911 struct thermal_cooling_device
*pos
= NULL
;
916 mutex_lock(&thermal_list_lock
);
917 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
921 /* thermal cooling device not found */
922 mutex_unlock(&thermal_list_lock
);
925 list_del(&cdev
->node
);
926 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
927 if (!tz
->ops
->unbind
)
929 tz
->ops
->unbind(tz
, cdev
);
931 mutex_unlock(&thermal_list_lock
);
933 device_remove_file(&cdev
->device
, &dev_attr_cdev_type
);
934 device_remove_file(&cdev
->device
, &dev_attr_max_state
);
935 device_remove_file(&cdev
->device
, &dev_attr_cur_state
);
937 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
938 device_unregister(&cdev
->device
);
942 EXPORT_SYMBOL(thermal_cooling_device_unregister
);
945 * thermal_zone_device_update - force an update of a thermal zone's state
946 * @ttz: the thermal zone to update
949 void thermal_zone_device_update(struct thermal_zone_device
*tz
)
952 long temp
, trip_temp
;
953 enum thermal_trip_type trip_type
;
954 struct thermal_cooling_device_instance
*instance
;
955 struct thermal_cooling_device
*cdev
;
957 mutex_lock(&tz
->lock
);
959 if (tz
->ops
->get_temp(tz
, &temp
)) {
960 /* get_temp failed - retry it later */
961 printk(KERN_WARNING PREFIX
"failed to read out thermal zone "
966 for (count
= 0; count
< tz
->trips
; count
++) {
967 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
968 tz
->ops
->get_trip_temp(tz
, count
, &trip_temp
);
971 case THERMAL_TRIP_CRITICAL
:
972 if (temp
>= trip_temp
) {
974 ret
= tz
->ops
->notify(tz
, count
,
978 "Critical temperature reached (%ld C), shutting down.\n",
980 orderly_poweroff(true);
984 case THERMAL_TRIP_HOT
:
985 if (temp
>= trip_temp
)
987 tz
->ops
->notify(tz
, count
, trip_type
);
989 case THERMAL_TRIP_ACTIVE
:
990 list_for_each_entry(instance
, &tz
->cooling_devices
,
992 if (instance
->trip
!= count
)
995 cdev
= instance
->cdev
;
997 if (temp
>= trip_temp
)
998 cdev
->ops
->set_cur_state(cdev
, 1);
1000 cdev
->ops
->set_cur_state(cdev
, 0);
1003 case THERMAL_TRIP_PASSIVE
:
1004 if (temp
>= trip_temp
|| tz
->passive
)
1005 thermal_zone_device_passive(tz
, temp
,
1011 if (tz
->forced_passive
)
1012 thermal_zone_device_passive(tz
, temp
, tz
->forced_passive
,
1013 THERMAL_TRIPS_NONE
);
1015 tz
->last_temperature
= temp
;
1019 thermal_zone_device_set_polling(tz
, tz
->passive_delay
);
1020 else if (tz
->polling_delay
)
1021 thermal_zone_device_set_polling(tz
, tz
->polling_delay
);
1023 thermal_zone_device_set_polling(tz
, 0);
1024 mutex_unlock(&tz
->lock
);
1026 EXPORT_SYMBOL(thermal_zone_device_update
);
1029 * thermal_zone_device_register - register a new thermal zone device
1030 * @type: the thermal zone device type
1031 * @trips: the number of trip points the thermal zone support
1032 * @devdata: private device data
1033 * @ops: standard thermal zone device callbacks
1034 * @tc1: thermal coefficient 1 for passive calculations
1035 * @tc2: thermal coefficient 2 for passive calculations
1036 * @passive_delay: number of milliseconds to wait between polls when
1037 * performing passive cooling
1038 * @polling_delay: number of milliseconds to wait between polls when checking
1039 * whether trip points have been crossed (0 for interrupt
1042 * thermal_zone_device_unregister() must be called when the device is no
1043 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1044 * section 11.1.5.1 of the ACPI specification 3.0.
1046 struct thermal_zone_device
*thermal_zone_device_register(char *type
,
1048 void *devdata
, struct
1049 thermal_zone_device_ops
1055 struct thermal_zone_device
*tz
;
1056 struct thermal_cooling_device
*pos
;
1057 enum thermal_trip_type trip_type
;
1062 if (strlen(type
) >= THERMAL_NAME_LENGTH
)
1063 return ERR_PTR(-EINVAL
);
1065 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0)
1066 return ERR_PTR(-EINVAL
);
1068 if (!ops
|| !ops
->get_temp
)
1069 return ERR_PTR(-EINVAL
);
1071 tz
= kzalloc(sizeof(struct thermal_zone_device
), GFP_KERNEL
);
1073 return ERR_PTR(-ENOMEM
);
1075 INIT_LIST_HEAD(&tz
->cooling_devices
);
1077 mutex_init(&tz
->lock
);
1078 result
= get_idr(&thermal_tz_idr
, &thermal_idr_lock
, &tz
->id
);
1081 return ERR_PTR(result
);
1084 strcpy(tz
->type
, type
);
1086 tz
->device
.class = &thermal_class
;
1087 tz
->devdata
= devdata
;
1091 tz
->passive_delay
= passive_delay
;
1092 tz
->polling_delay
= polling_delay
;
1094 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1095 result
= device_register(&tz
->device
);
1097 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1099 return ERR_PTR(result
);
1104 result
= device_create_file(&tz
->device
, &dev_attr_type
);
1109 result
= device_create_file(&tz
->device
, &dev_attr_temp
);
1113 if (ops
->get_mode
) {
1114 result
= device_create_file(&tz
->device
, &dev_attr_mode
);
1119 for (count
= 0; count
< trips
; count
++) {
1120 TRIP_POINT_ATTR_ADD(&tz
->device
, count
, result
);
1123 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
1124 if (trip_type
== THERMAL_TRIP_PASSIVE
)
1129 result
= device_create_file(&tz
->device
,
1135 result
= thermal_add_hwmon_sysfs(tz
);
1139 mutex_lock(&thermal_list_lock
);
1140 list_add_tail(&tz
->node
, &thermal_tz_list
);
1142 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1143 result
= ops
->bind(tz
, pos
);
1147 mutex_unlock(&thermal_list_lock
);
1149 INIT_DELAYED_WORK(&(tz
->poll_queue
), thermal_zone_device_check
);
1151 thermal_zone_device_update(tz
);
1157 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1158 device_unregister(&tz
->device
);
1159 return ERR_PTR(result
);
1162 EXPORT_SYMBOL(thermal_zone_device_register
);
1165 * thermal_device_unregister - removes the registered thermal zone device
1166 * @tz: the thermal zone device to remove
1168 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1170 struct thermal_cooling_device
*cdev
;
1171 struct thermal_zone_device
*pos
= NULL
;
1177 mutex_lock(&thermal_list_lock
);
1178 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1182 /* thermal zone device not found */
1183 mutex_unlock(&thermal_list_lock
);
1186 list_del(&tz
->node
);
1187 if (tz
->ops
->unbind
)
1188 list_for_each_entry(cdev
, &thermal_cdev_list
, node
)
1189 tz
->ops
->unbind(tz
, cdev
);
1190 mutex_unlock(&thermal_list_lock
);
1192 thermal_zone_device_set_polling(tz
, 0);
1195 device_remove_file(&tz
->device
, &dev_attr_type
);
1196 device_remove_file(&tz
->device
, &dev_attr_temp
);
1197 if (tz
->ops
->get_mode
)
1198 device_remove_file(&tz
->device
, &dev_attr_mode
);
1200 for (count
= 0; count
< tz
->trips
; count
++)
1201 TRIP_POINT_ATTR_REMOVE(&tz
->device
, count
);
1203 thermal_remove_hwmon_sysfs(tz
);
1204 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1205 idr_destroy(&tz
->idr
);
1206 mutex_destroy(&tz
->lock
);
1207 device_unregister(&tz
->device
);
1211 EXPORT_SYMBOL(thermal_zone_device_unregister
);
1213 static int __init
thermal_init(void)
1217 result
= class_register(&thermal_class
);
1219 idr_destroy(&thermal_tz_idr
);
1220 idr_destroy(&thermal_cdev_idr
);
1221 mutex_destroy(&thermal_idr_lock
);
1222 mutex_destroy(&thermal_list_lock
);
1227 static void __exit
thermal_exit(void)
1229 class_unregister(&thermal_class
);
1230 idr_destroy(&thermal_tz_idr
);
1231 idr_destroy(&thermal_cdev_idr
);
1232 mutex_destroy(&thermal_idr_lock
);
1233 mutex_destroy(&thermal_list_lock
);
1236 subsys_initcall(thermal_init
);
1237 module_exit(thermal_exit
);