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>
34 MODULE_AUTHOR("Zhang Rui")
35 MODULE_DESCRIPTION("Generic thermal management sysfs support");
36 MODULE_LICENSE("GPL");
38 #define PREFIX "Thermal: "
40 struct thermal_cooling_device_instance
{
42 char name
[THERMAL_NAME_LENGTH
];
43 struct thermal_zone_device
*tz
;
44 struct thermal_cooling_device
*cdev
;
46 char attr_name
[THERMAL_NAME_LENGTH
];
47 struct device_attribute attr
;
48 struct list_head node
;
51 static DEFINE_IDR(thermal_tz_idr
);
52 static DEFINE_IDR(thermal_cdev_idr
);
53 static DEFINE_MUTEX(thermal_idr_lock
);
55 static LIST_HEAD(thermal_tz_list
);
56 static LIST_HEAD(thermal_cdev_list
);
57 static DEFINE_MUTEX(thermal_list_lock
);
59 static int get_idr(struct idr
*idr
, struct mutex
*lock
, int *id
)
64 if (unlikely(idr_pre_get(idr
, GFP_KERNEL
) == 0))
69 err
= idr_get_new(idr
, NULL
, id
);
72 if (unlikely(err
== -EAGAIN
))
74 else if (unlikely(err
))
77 *id
= *id
& MAX_ID_MASK
;
81 static void release_idr(struct idr
*idr
, struct mutex
*lock
, int id
)
90 /* sys I/F for thermal zone */
92 #define to_thermal_zone(_dev) \
93 container_of(_dev, struct thermal_zone_device, device)
96 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
98 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
100 return sprintf(buf
, "%s\n", tz
->type
);
104 temp_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
106 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
108 if (!tz
->ops
->get_temp
)
111 return tz
->ops
->get_temp(tz
, buf
);
115 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
117 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
119 if (!tz
->ops
->get_mode
)
122 return tz
->ops
->get_mode(tz
, buf
);
126 mode_store(struct device
*dev
, struct device_attribute
*attr
,
127 const char *buf
, size_t count
)
129 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
132 if (!tz
->ops
->set_mode
)
135 result
= tz
->ops
->set_mode(tz
, buf
);
143 trip_point_type_show(struct device
*dev
, struct device_attribute
*attr
,
146 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
149 if (!tz
->ops
->get_trip_type
)
152 if (!sscanf(attr
->attr
.name
, "trip_point_%d_type", &trip
))
155 return tz
->ops
->get_trip_type(tz
, trip
, buf
);
159 trip_point_temp_show(struct device
*dev
, struct device_attribute
*attr
,
162 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
165 if (!tz
->ops
->get_trip_temp
)
168 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
171 return tz
->ops
->get_trip_temp(tz
, trip
, buf
);
174 static DEVICE_ATTR(type
, 0444, type_show
, NULL
);
175 static DEVICE_ATTR(temp
, 0444, temp_show
, NULL
);
176 static DEVICE_ATTR(mode
, 0644, mode_show
, mode_store
);
178 static struct device_attribute trip_point_attrs
[] = {
179 __ATTR(trip_point_0_type
, 0444, trip_point_type_show
, NULL
),
180 __ATTR(trip_point_0_temp
, 0444, trip_point_temp_show
, NULL
),
181 __ATTR(trip_point_1_type
, 0444, trip_point_type_show
, NULL
),
182 __ATTR(trip_point_1_temp
, 0444, trip_point_temp_show
, NULL
),
183 __ATTR(trip_point_2_type
, 0444, trip_point_type_show
, NULL
),
184 __ATTR(trip_point_2_temp
, 0444, trip_point_temp_show
, NULL
),
185 __ATTR(trip_point_3_type
, 0444, trip_point_type_show
, NULL
),
186 __ATTR(trip_point_3_temp
, 0444, trip_point_temp_show
, NULL
),
187 __ATTR(trip_point_4_type
, 0444, trip_point_type_show
, NULL
),
188 __ATTR(trip_point_4_temp
, 0444, trip_point_temp_show
, NULL
),
189 __ATTR(trip_point_5_type
, 0444, trip_point_type_show
, NULL
),
190 __ATTR(trip_point_5_temp
, 0444, trip_point_temp_show
, NULL
),
191 __ATTR(trip_point_6_type
, 0444, trip_point_type_show
, NULL
),
192 __ATTR(trip_point_6_temp
, 0444, trip_point_temp_show
, NULL
),
193 __ATTR(trip_point_7_type
, 0444, trip_point_type_show
, NULL
),
194 __ATTR(trip_point_7_temp
, 0444, trip_point_temp_show
, NULL
),
195 __ATTR(trip_point_8_type
, 0444, trip_point_type_show
, NULL
),
196 __ATTR(trip_point_8_temp
, 0444, trip_point_temp_show
, NULL
),
197 __ATTR(trip_point_9_type
, 0444, trip_point_type_show
, NULL
),
198 __ATTR(trip_point_9_temp
, 0444, trip_point_temp_show
, NULL
),
201 #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
203 result = device_create_file(_dev, \
204 &trip_point_attrs[_index * 2]); \
207 result = device_create_file(_dev, \
208 &trip_point_attrs[_index * 2 + 1]); \
211 #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
213 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
214 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
217 /* sys I/F for cooling device */
218 #define to_cooling_device(_dev) \
219 container_of(_dev, struct thermal_cooling_device, device)
222 thermal_cooling_device_type_show(struct device
*dev
,
223 struct device_attribute
*attr
, char *buf
)
225 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
227 return sprintf(buf
, "%s\n", cdev
->type
);
231 thermal_cooling_device_max_state_show(struct device
*dev
,
232 struct device_attribute
*attr
, char *buf
)
234 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
236 return cdev
->ops
->get_max_state(cdev
, buf
);
240 thermal_cooling_device_cur_state_show(struct device
*dev
,
241 struct device_attribute
*attr
, char *buf
)
243 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
245 return cdev
->ops
->get_cur_state(cdev
, buf
);
249 thermal_cooling_device_cur_state_store(struct device
*dev
,
250 struct device_attribute
*attr
,
251 const char *buf
, size_t count
)
253 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
257 if (!sscanf(buf
, "%d\n", &state
))
263 result
= cdev
->ops
->set_cur_state(cdev
, state
);
269 static struct device_attribute dev_attr_cdev_type
=
270 __ATTR(type
, 0444, thermal_cooling_device_type_show
, NULL
);
271 static DEVICE_ATTR(max_state
, 0444,
272 thermal_cooling_device_max_state_show
, NULL
);
273 static DEVICE_ATTR(cur_state
, 0644,
274 thermal_cooling_device_cur_state_show
,
275 thermal_cooling_device_cur_state_store
);
278 thermal_cooling_device_trip_point_show(struct device
*dev
,
279 struct device_attribute
*attr
, char *buf
)
281 struct thermal_cooling_device_instance
*instance
;
284 container_of(attr
, struct thermal_cooling_device_instance
, attr
);
286 if (instance
->trip
== THERMAL_TRIPS_NONE
)
287 return sprintf(buf
, "-1\n");
289 return sprintf(buf
, "%d\n", instance
->trip
);
292 /* Device management */
295 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
296 * @tz: thermal zone device
297 * @trip: indicates which trip point the cooling devices is
298 * associated with in this thermal zone.
299 * @cdev: thermal cooling device
301 * This function is usually called in the thermal zone device .bind callback.
303 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
305 struct thermal_cooling_device
*cdev
)
307 struct thermal_cooling_device_instance
*dev
;
308 struct thermal_cooling_device_instance
*pos
;
311 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
318 kzalloc(sizeof(struct thermal_cooling_device_instance
), GFP_KERNEL
);
324 result
= get_idr(&tz
->idr
, &tz
->lock
, &dev
->id
);
328 sprintf(dev
->name
, "cdev%d", dev
->id
);
330 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
334 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
335 dev
->attr
.attr
.name
= dev
->attr_name
;
336 dev
->attr
.attr
.mode
= 0444;
337 dev
->attr
.show
= thermal_cooling_device_trip_point_show
;
338 result
= device_create_file(&tz
->device
, &dev
->attr
);
340 goto remove_symbol_link
;
342 mutex_lock(&tz
->lock
);
343 list_for_each_entry(pos
, &tz
->cooling_devices
, node
)
344 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
349 list_add_tail(&dev
->node
, &tz
->cooling_devices
);
350 mutex_unlock(&tz
->lock
);
355 device_remove_file(&tz
->device
, &dev
->attr
);
357 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
359 release_idr(&tz
->idr
, &tz
->lock
, dev
->id
);
365 EXPORT_SYMBOL(thermal_zone_bind_cooling_device
);
368 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
369 * @tz: thermal zone device
370 * @trip: indicates which trip point the cooling devices is
371 * associated with in this thermal zone.
372 * @cdev: thermal cooling device
374 * This function is usually called in the thermal zone device .unbind callback.
376 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
378 struct thermal_cooling_device
*cdev
)
380 struct thermal_cooling_device_instance
*pos
, *next
;
382 mutex_lock(&tz
->lock
);
383 list_for_each_entry_safe(pos
, next
, &tz
->cooling_devices
, node
) {
384 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
385 list_del(&pos
->node
);
386 mutex_unlock(&tz
->lock
);
390 mutex_unlock(&tz
->lock
);
395 device_remove_file(&tz
->device
, &pos
->attr
);
396 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
397 release_idr(&tz
->idr
, &tz
->lock
, pos
->id
);
402 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device
);
404 static void thermal_release(struct device
*dev
)
406 struct thermal_zone_device
*tz
;
407 struct thermal_cooling_device
*cdev
;
409 if (!strncmp(dev
->bus_id
, "thermal_zone", sizeof "thermal_zone" - 1)) {
410 tz
= to_thermal_zone(dev
);
413 cdev
= to_cooling_device(dev
);
418 static struct class thermal_class
= {
420 .dev_release
= thermal_release
,
424 * thermal_cooling_device_register - register a new thermal cooling device
425 * @type: the thermal cooling device type.
426 * @devdata: device private data.
427 * @ops: standard thermal cooling devices callbacks.
429 struct thermal_cooling_device
*thermal_cooling_device_register(char *type
,
432 thermal_cooling_device_ops
435 struct thermal_cooling_device
*cdev
;
436 struct thermal_zone_device
*pos
;
439 if (strlen(type
) >= THERMAL_NAME_LENGTH
)
442 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
446 cdev
= kzalloc(sizeof(struct thermal_cooling_device
), GFP_KERNEL
);
450 result
= get_idr(&thermal_cdev_idr
, &thermal_idr_lock
, &cdev
->id
);
456 strcpy(cdev
->type
, type
);
458 cdev
->device
.class = &thermal_class
;
459 cdev
->devdata
= devdata
;
460 sprintf(cdev
->device
.bus_id
, "cooling_device%d", cdev
->id
);
461 result
= device_register(&cdev
->device
);
463 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
470 result
= device_create_file(&cdev
->device
, &dev_attr_cdev_type
);
475 result
= device_create_file(&cdev
->device
, &dev_attr_max_state
);
479 result
= device_create_file(&cdev
->device
, &dev_attr_cur_state
);
483 mutex_lock(&thermal_list_lock
);
484 list_add(&cdev
->node
, &thermal_cdev_list
);
485 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
488 result
= pos
->ops
->bind(pos
, cdev
);
493 mutex_unlock(&thermal_list_lock
);
499 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
500 device_unregister(&cdev
->device
);
504 EXPORT_SYMBOL(thermal_cooling_device_register
);
507 * thermal_cooling_device_unregister - removes the registered thermal cooling device
508 * @cdev: the thermal cooling device to remove.
510 * thermal_cooling_device_unregister() must be called when the device is no
513 void thermal_cooling_device_unregister(struct
514 thermal_cooling_device
517 struct thermal_zone_device
*tz
;
518 struct thermal_cooling_device
*pos
= NULL
;
523 mutex_lock(&thermal_list_lock
);
524 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
528 /* thermal cooling device not found */
529 mutex_unlock(&thermal_list_lock
);
532 list_del(&cdev
->node
);
533 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
534 if (!tz
->ops
->unbind
)
536 tz
->ops
->unbind(tz
, cdev
);
538 mutex_unlock(&thermal_list_lock
);
540 device_remove_file(&cdev
->device
, &dev_attr_cdev_type
);
541 device_remove_file(&cdev
->device
, &dev_attr_max_state
);
542 device_remove_file(&cdev
->device
, &dev_attr_cur_state
);
544 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
545 device_unregister(&cdev
->device
);
549 EXPORT_SYMBOL(thermal_cooling_device_unregister
);
552 * thermal_zone_device_register - register a new thermal zone device
553 * @type: the thermal zone device type
554 * @trips: the number of trip points the thermal zone support
555 * @devdata: private device data
556 * @ops: standard thermal zone device callbacks
558 * thermal_zone_device_unregister() must be called when the device is no
561 struct thermal_zone_device
*thermal_zone_device_register(char *type
,
563 void *devdata
, struct
564 thermal_zone_device_ops
567 struct thermal_zone_device
*tz
;
568 struct thermal_cooling_device
*pos
;
572 if (strlen(type
) >= THERMAL_NAME_LENGTH
)
575 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0)
578 if (!ops
|| !ops
->get_temp
)
581 tz
= kzalloc(sizeof(struct thermal_zone_device
), GFP_KERNEL
);
585 INIT_LIST_HEAD(&tz
->cooling_devices
);
587 mutex_init(&tz
->lock
);
588 result
= get_idr(&thermal_tz_idr
, &thermal_idr_lock
, &tz
->id
);
594 strcpy(tz
->type
, type
);
596 tz
->device
.class = &thermal_class
;
597 tz
->devdata
= devdata
;
599 sprintf(tz
->device
.bus_id
, "thermal_zone%d", tz
->id
);
600 result
= device_register(&tz
->device
);
602 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
609 result
= device_create_file(&tz
->device
, &dev_attr_type
);
614 result
= device_create_file(&tz
->device
, &dev_attr_temp
);
619 result
= device_create_file(&tz
->device
, &dev_attr_mode
);
624 for (count
= 0; count
< trips
; count
++) {
625 TRIP_POINT_ATTR_ADD(&tz
->device
, count
, result
);
630 mutex_lock(&thermal_list_lock
);
631 list_add_tail(&tz
->node
, &thermal_tz_list
);
633 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
634 result
= ops
->bind(tz
, pos
);
638 mutex_unlock(&thermal_list_lock
);
644 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
645 device_unregister(&tz
->device
);
649 EXPORT_SYMBOL(thermal_zone_device_register
);
652 * thermal_device_unregister - removes the registered thermal zone device
653 * @tz: the thermal zone device to remove
655 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
657 struct thermal_cooling_device
*cdev
;
658 struct thermal_zone_device
*pos
= NULL
;
664 mutex_lock(&thermal_list_lock
);
665 list_for_each_entry(pos
, &thermal_tz_list
, node
)
669 /* thermal zone device not found */
670 mutex_unlock(&thermal_list_lock
);
675 list_for_each_entry(cdev
, &thermal_cdev_list
, node
)
676 tz
->ops
->unbind(tz
, cdev
);
677 mutex_unlock(&thermal_list_lock
);
680 device_remove_file(&tz
->device
, &dev_attr_type
);
681 device_remove_file(&tz
->device
, &dev_attr_temp
);
682 if (tz
->ops
->get_mode
)
683 device_remove_file(&tz
->device
, &dev_attr_mode
);
685 for (count
= 0; count
< tz
->trips
; count
++)
686 TRIP_POINT_ATTR_REMOVE(&tz
->device
, count
);
688 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
689 idr_destroy(&tz
->idr
);
690 mutex_destroy(&tz
->lock
);
691 device_unregister(&tz
->device
);
695 EXPORT_SYMBOL(thermal_zone_device_unregister
);
697 static int __init
thermal_init(void)
701 result
= class_register(&thermal_class
);
703 idr_destroy(&thermal_tz_idr
);
704 idr_destroy(&thermal_cdev_idr
);
705 mutex_destroy(&thermal_idr_lock
);
706 mutex_destroy(&thermal_list_lock
);
711 static void __exit
thermal_exit(void)
713 class_unregister(&thermal_class
);
714 idr_destroy(&thermal_tz_idr
);
715 idr_destroy(&thermal_cdev_idr
);
716 mutex_destroy(&thermal_idr_lock
);
717 mutex_destroy(&thermal_list_lock
);
720 subsys_initcall(thermal_init
);
721 module_exit(thermal_exit
);