Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / thermal / thermal.c
blobe529dabb74920c31be53d4f02aa46088c5ab2b78
1 /*
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 {
41 int id;
42 char name[THERMAL_NAME_LENGTH];
43 struct thermal_zone_device *tz;
44 struct thermal_cooling_device *cdev;
45 int trip;
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)
61 int err;
63 again:
64 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
65 return -ENOMEM;
67 if (lock)
68 mutex_lock(lock);
69 err = idr_get_new(idr, NULL, id);
70 if (lock)
71 mutex_unlock(lock);
72 if (unlikely(err == -EAGAIN))
73 goto again;
74 else if (unlikely(err))
75 return err;
77 *id = *id & MAX_ID_MASK;
78 return 0;
81 static void release_idr(struct idr *idr, struct mutex *lock, int id)
83 if (lock)
84 mutex_lock(lock);
85 idr_remove(idr, id);
86 if (lock)
87 mutex_unlock(lock);
90 /* sys I/F for thermal zone */
92 #define to_thermal_zone(_dev) \
93 container_of(_dev, struct thermal_zone_device, device)
95 static ssize_t
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);
103 static ssize_t
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)
109 return -EPERM;
111 return tz->ops->get_temp(tz, buf);
114 static ssize_t
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)
120 return -EPERM;
122 return tz->ops->get_mode(tz, buf);
125 static ssize_t
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);
130 int result;
132 if (!tz->ops->set_mode)
133 return -EPERM;
135 result = tz->ops->set_mode(tz, buf);
136 if (result)
137 return result;
139 return count;
142 static ssize_t
143 trip_point_type_show(struct device *dev, struct device_attribute *attr,
144 char *buf)
146 struct thermal_zone_device *tz = to_thermal_zone(dev);
147 int trip;
149 if (!tz->ops->get_trip_type)
150 return -EPERM;
152 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
153 return -EINVAL;
155 return tz->ops->get_trip_type(tz, trip, buf);
158 static ssize_t
159 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
160 char *buf)
162 struct thermal_zone_device *tz = to_thermal_zone(dev);
163 int trip;
165 if (!tz->ops->get_trip_temp)
166 return -EPERM;
168 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
169 return -EINVAL;
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) \
202 do { \
203 result = device_create_file(_dev, \
204 &trip_point_attrs[_index * 2]); \
205 if (result) \
206 break; \
207 result = device_create_file(_dev, \
208 &trip_point_attrs[_index * 2 + 1]); \
209 } while (0)
211 #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
212 do { \
213 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
214 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
215 } while (0)
217 /* sys I/F for cooling device */
218 #define to_cooling_device(_dev) \
219 container_of(_dev, struct thermal_cooling_device, device)
221 static ssize_t
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);
230 static ssize_t
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);
239 static ssize_t
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);
248 static ssize_t
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);
254 int state;
255 int result;
257 if (!sscanf(buf, "%d\n", &state))
258 return -EINVAL;
260 if (state < 0)
261 return -EINVAL;
263 result = cdev->ops->set_cur_state(cdev, state);
264 if (result)
265 return result;
266 return count;
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);
277 static ssize_t
278 thermal_cooling_device_trip_point_show(struct device *dev,
279 struct device_attribute *attr, char *buf)
281 struct thermal_cooling_device_instance *instance;
283 instance =
284 container_of(attr, struct thermal_cooling_device_instance, attr);
286 if (instance->trip == THERMAL_TRIPS_NONE)
287 return sprintf(buf, "-1\n");
288 else
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,
304 int trip,
305 struct thermal_cooling_device *cdev)
307 struct thermal_cooling_device_instance *dev;
308 struct thermal_cooling_device_instance *pos;
309 <<<<<<< HEAD:drivers/thermal/thermal.c
310 =======
311 struct thermal_zone_device *pos1;
312 struct thermal_cooling_device *pos2;
313 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
314 int result;
316 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
317 return -EINVAL;
319 <<<<<<< HEAD:drivers/thermal/thermal.c
320 if (!tz || !cdev)
321 =======
322 list_for_each_entry(pos1, &thermal_tz_list, node) {
323 if (pos1 == tz)
324 break;
326 list_for_each_entry(pos2, &thermal_cdev_list, node) {
327 if (pos2 == cdev)
328 break;
331 if (tz != pos1 || cdev != pos2)
332 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
333 return -EINVAL;
335 dev =
336 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
337 if (!dev)
338 return -ENOMEM;
339 dev->tz = tz;
340 dev->cdev = cdev;
341 dev->trip = trip;
342 result = get_idr(&tz->idr, &tz->lock, &dev->id);
343 if (result)
344 goto free_mem;
346 sprintf(dev->name, "cdev%d", dev->id);
347 result =
348 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
349 if (result)
350 goto release_idr;
352 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
353 dev->attr.attr.name = dev->attr_name;
354 dev->attr.attr.mode = 0444;
355 dev->attr.show = thermal_cooling_device_trip_point_show;
356 result = device_create_file(&tz->device, &dev->attr);
357 if (result)
358 goto remove_symbol_link;
360 mutex_lock(&tz->lock);
361 list_for_each_entry(pos, &tz->cooling_devices, node)
362 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
363 result = -EEXIST;
364 break;
366 if (!result)
367 list_add_tail(&dev->node, &tz->cooling_devices);
368 mutex_unlock(&tz->lock);
370 if (!result)
371 return 0;
373 device_remove_file(&tz->device, &dev->attr);
374 remove_symbol_link:
375 sysfs_remove_link(&tz->device.kobj, dev->name);
376 release_idr:
377 release_idr(&tz->idr, &tz->lock, dev->id);
378 free_mem:
379 kfree(dev);
380 return result;
383 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
386 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
387 * @tz: thermal zone device
388 * @trip: indicates which trip point the cooling devices is
389 * associated with in this thermal zone.
390 * @cdev: thermal cooling device
392 * This function is usually called in the thermal zone device .unbind callback.
394 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
395 int trip,
396 struct thermal_cooling_device *cdev)
398 struct thermal_cooling_device_instance *pos, *next;
400 mutex_lock(&tz->lock);
401 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
402 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
403 list_del(&pos->node);
404 mutex_unlock(&tz->lock);
405 goto unbind;
408 mutex_unlock(&tz->lock);
410 return -ENODEV;
412 unbind:
413 device_remove_file(&tz->device, &pos->attr);
414 sysfs_remove_link(&tz->device.kobj, pos->name);
415 release_idr(&tz->idr, &tz->lock, pos->id);
416 kfree(pos);
417 return 0;
420 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
422 static void thermal_release(struct device *dev)
424 struct thermal_zone_device *tz;
425 struct thermal_cooling_device *cdev;
427 if (!strncmp(dev->bus_id, "thermal_zone", sizeof "thermal_zone" - 1)) {
428 tz = to_thermal_zone(dev);
429 kfree(tz);
430 } else {
431 cdev = to_cooling_device(dev);
432 kfree(cdev);
436 static struct class thermal_class = {
437 .name = "thermal",
438 .dev_release = thermal_release,
442 * thermal_cooling_device_register - register a new thermal cooling device
443 * @type: the thermal cooling device type.
444 * @devdata: device private data.
445 * @ops: standard thermal cooling devices callbacks.
447 struct thermal_cooling_device *thermal_cooling_device_register(char *type,
448 void *devdata,
449 struct
450 thermal_cooling_device_ops
451 *ops)
453 struct thermal_cooling_device *cdev;
454 struct thermal_zone_device *pos;
455 int result;
457 if (strlen(type) >= THERMAL_NAME_LENGTH)
458 <<<<<<< HEAD:drivers/thermal/thermal.c
459 return NULL;
460 =======
461 return ERR_PTR(-EINVAL);
462 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
464 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
465 !ops->set_cur_state)
466 <<<<<<< HEAD:drivers/thermal/thermal.c
467 return NULL;
468 =======
469 return ERR_PTR(-EINVAL);
470 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
472 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
473 if (!cdev)
474 <<<<<<< HEAD:drivers/thermal/thermal.c
475 return NULL;
476 =======
477 return ERR_PTR(-ENOMEM);
478 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
480 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
481 if (result) {
482 kfree(cdev);
483 <<<<<<< HEAD:drivers/thermal/thermal.c
484 return NULL;
485 =======
486 return ERR_PTR(result);
487 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
490 strcpy(cdev->type, type);
491 cdev->ops = ops;
492 cdev->device.class = &thermal_class;
493 cdev->devdata = devdata;
494 sprintf(cdev->device.bus_id, "cooling_device%d", cdev->id);
495 result = device_register(&cdev->device);
496 if (result) {
497 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
498 kfree(cdev);
499 <<<<<<< HEAD:drivers/thermal/thermal.c
500 return NULL;
501 =======
502 return ERR_PTR(result);
503 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
506 /* sys I/F */
507 if (type) {
508 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
509 if (result)
510 goto unregister;
513 result = device_create_file(&cdev->device, &dev_attr_max_state);
514 if (result)
515 goto unregister;
517 result = device_create_file(&cdev->device, &dev_attr_cur_state);
518 if (result)
519 goto unregister;
521 mutex_lock(&thermal_list_lock);
522 list_add(&cdev->node, &thermal_cdev_list);
523 list_for_each_entry(pos, &thermal_tz_list, node) {
524 if (!pos->ops->bind)
525 continue;
526 result = pos->ops->bind(pos, cdev);
527 if (result)
528 break;
531 mutex_unlock(&thermal_list_lock);
533 if (!result)
534 return cdev;
536 unregister:
537 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
538 device_unregister(&cdev->device);
539 <<<<<<< HEAD:drivers/thermal/thermal.c
540 return NULL;
541 =======
542 return ERR_PTR(result);
543 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
546 EXPORT_SYMBOL(thermal_cooling_device_register);
549 * thermal_cooling_device_unregister - removes the registered thermal cooling device
550 * @cdev: the thermal cooling device to remove.
552 * thermal_cooling_device_unregister() must be called when the device is no
553 * longer needed.
555 void thermal_cooling_device_unregister(struct
556 thermal_cooling_device
557 *cdev)
559 struct thermal_zone_device *tz;
560 struct thermal_cooling_device *pos = NULL;
562 if (!cdev)
563 return;
565 mutex_lock(&thermal_list_lock);
566 list_for_each_entry(pos, &thermal_cdev_list, node)
567 if (pos == cdev)
568 break;
569 if (pos != cdev) {
570 /* thermal cooling device not found */
571 mutex_unlock(&thermal_list_lock);
572 return;
574 list_del(&cdev->node);
575 list_for_each_entry(tz, &thermal_tz_list, node) {
576 if (!tz->ops->unbind)
577 continue;
578 tz->ops->unbind(tz, cdev);
580 mutex_unlock(&thermal_list_lock);
581 if (cdev->type[0])
582 device_remove_file(&cdev->device, &dev_attr_cdev_type);
583 device_remove_file(&cdev->device, &dev_attr_max_state);
584 device_remove_file(&cdev->device, &dev_attr_cur_state);
586 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
587 device_unregister(&cdev->device);
588 return;
591 EXPORT_SYMBOL(thermal_cooling_device_unregister);
594 * thermal_zone_device_register - register a new thermal zone device
595 * @type: the thermal zone device type
596 * @trips: the number of trip points the thermal zone support
597 * @devdata: private device data
598 * @ops: standard thermal zone device callbacks
600 * thermal_zone_device_unregister() must be called when the device is no
601 * longer needed.
603 struct thermal_zone_device *thermal_zone_device_register(char *type,
604 int trips,
605 void *devdata, struct
606 thermal_zone_device_ops
607 *ops)
609 struct thermal_zone_device *tz;
610 struct thermal_cooling_device *pos;
611 int result;
612 int count;
614 if (strlen(type) >= THERMAL_NAME_LENGTH)
615 <<<<<<< HEAD:drivers/thermal/thermal.c
616 return NULL;
617 =======
618 return ERR_PTR(-EINVAL);
619 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
621 if (trips > THERMAL_MAX_TRIPS || trips < 0)
622 <<<<<<< HEAD:drivers/thermal/thermal.c
623 return NULL;
624 =======
625 return ERR_PTR(-EINVAL);
626 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
628 if (!ops || !ops->get_temp)
629 <<<<<<< HEAD:drivers/thermal/thermal.c
630 return NULL;
631 =======
632 return ERR_PTR(-EINVAL);
633 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
635 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
636 if (!tz)
637 <<<<<<< HEAD:drivers/thermal/thermal.c
638 return NULL;
639 =======
640 return ERR_PTR(-ENOMEM);
641 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
643 INIT_LIST_HEAD(&tz->cooling_devices);
644 idr_init(&tz->idr);
645 mutex_init(&tz->lock);
646 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
647 if (result) {
648 kfree(tz);
649 <<<<<<< HEAD:drivers/thermal/thermal.c
650 return NULL;
651 =======
652 return ERR_PTR(result);
653 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
656 strcpy(tz->type, type);
657 tz->ops = ops;
658 tz->device.class = &thermal_class;
659 tz->devdata = devdata;
660 tz->trips = trips;
661 sprintf(tz->device.bus_id, "thermal_zone%d", tz->id);
662 result = device_register(&tz->device);
663 if (result) {
664 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
665 kfree(tz);
666 <<<<<<< HEAD:drivers/thermal/thermal.c
667 return NULL;
668 =======
669 return ERR_PTR(result);
670 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
673 /* sys I/F */
674 if (type) {
675 result = device_create_file(&tz->device, &dev_attr_type);
676 if (result)
677 goto unregister;
680 result = device_create_file(&tz->device, &dev_attr_temp);
681 if (result)
682 goto unregister;
684 if (ops->get_mode) {
685 result = device_create_file(&tz->device, &dev_attr_mode);
686 if (result)
687 goto unregister;
690 for (count = 0; count < trips; count++) {
691 TRIP_POINT_ATTR_ADD(&tz->device, count, result);
692 if (result)
693 goto unregister;
696 mutex_lock(&thermal_list_lock);
697 list_add_tail(&tz->node, &thermal_tz_list);
698 if (ops->bind)
699 list_for_each_entry(pos, &thermal_cdev_list, node) {
700 result = ops->bind(tz, pos);
701 if (result)
702 break;
704 mutex_unlock(&thermal_list_lock);
706 if (!result)
707 return tz;
709 unregister:
710 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
711 device_unregister(&tz->device);
712 <<<<<<< HEAD:drivers/thermal/thermal.c
713 return NULL;
714 =======
715 return ERR_PTR(result);
716 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/thermal/thermal.c
719 EXPORT_SYMBOL(thermal_zone_device_register);
722 * thermal_device_unregister - removes the registered thermal zone device
723 * @tz: the thermal zone device to remove
725 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
727 struct thermal_cooling_device *cdev;
728 struct thermal_zone_device *pos = NULL;
729 int count;
731 if (!tz)
732 return;
734 mutex_lock(&thermal_list_lock);
735 list_for_each_entry(pos, &thermal_tz_list, node)
736 if (pos == tz)
737 break;
738 if (pos != tz) {
739 /* thermal zone device not found */
740 mutex_unlock(&thermal_list_lock);
741 return;
743 list_del(&tz->node);
744 if (tz->ops->unbind)
745 list_for_each_entry(cdev, &thermal_cdev_list, node)
746 tz->ops->unbind(tz, cdev);
747 mutex_unlock(&thermal_list_lock);
749 if (tz->type[0])
750 device_remove_file(&tz->device, &dev_attr_type);
751 device_remove_file(&tz->device, &dev_attr_temp);
752 if (tz->ops->get_mode)
753 device_remove_file(&tz->device, &dev_attr_mode);
755 for (count = 0; count < tz->trips; count++)
756 TRIP_POINT_ATTR_REMOVE(&tz->device, count);
758 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
759 idr_destroy(&tz->idr);
760 mutex_destroy(&tz->lock);
761 device_unregister(&tz->device);
762 return;
765 EXPORT_SYMBOL(thermal_zone_device_unregister);
767 static int __init thermal_init(void)
769 int result = 0;
771 result = class_register(&thermal_class);
772 if (result) {
773 idr_destroy(&thermal_tz_idr);
774 idr_destroy(&thermal_cdev_idr);
775 mutex_destroy(&thermal_idr_lock);
776 mutex_destroy(&thermal_list_lock);
778 return result;
781 static void __exit thermal_exit(void)
783 class_unregister(&thermal_class);
784 idr_destroy(&thermal_tz_idr);
785 idr_destroy(&thermal_cdev_idr);
786 mutex_destroy(&thermal_idr_lock);
787 mutex_destroy(&thermal_list_lock);
790 subsys_initcall(thermal_init);
791 module_exit(thermal_exit);