xref: /linux/drivers/thermal/thermal_core.c (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  thermal.c - Generic Thermal Management Sysfs support.
4  *
5  *  Copyright (C) 2008 Intel Corp
6  *  Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
7  *  Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/kdev_t.h>
17 #include <linux/idr.h>
18 #include <linux/thermal.h>
19 #include <linux/reboot.h>
20 #include <linux/string.h>
21 #include <linux/of.h>
22 #include <linux/suspend.h>
23 
24 #define CREATE_TRACE_POINTS
25 #include <trace/events/thermal.h>
26 
27 #include "thermal_core.h"
28 #include "thermal_hwmon.h"
29 
30 static DEFINE_IDA(thermal_tz_ida);
31 static DEFINE_IDA(thermal_cdev_ida);
32 
33 static LIST_HEAD(thermal_tz_list);
34 static LIST_HEAD(thermal_cdev_list);
35 static LIST_HEAD(thermal_governor_list);
36 
37 static DEFINE_MUTEX(thermal_list_lock);
38 static DEFINE_MUTEX(thermal_governor_lock);
39 
40 static atomic_t in_suspend;
41 
42 static struct thermal_governor *def_governor;
43 
44 /*
45  * Governor section: set of functions to handle thermal governors
46  *
47  * Functions to help in the life cycle of thermal governors within
48  * the thermal core and by the thermal governor code.
49  */
50 
51 static struct thermal_governor *__find_governor(const char *name)
52 {
53 	struct thermal_governor *pos;
54 
55 	if (!name || !name[0])
56 		return def_governor;
57 
58 	list_for_each_entry(pos, &thermal_governor_list, governor_list)
59 		if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
60 			return pos;
61 
62 	return NULL;
63 }
64 
65 /**
66  * bind_previous_governor() - bind the previous governor of the thermal zone
67  * @tz:		a valid pointer to a struct thermal_zone_device
68  * @failed_gov_name:	the name of the governor that failed to register
69  *
70  * Register the previous governor of the thermal zone after a new
71  * governor has failed to be bound.
72  */
73 static void bind_previous_governor(struct thermal_zone_device *tz,
74 				   const char *failed_gov_name)
75 {
76 	if (tz->governor && tz->governor->bind_to_tz) {
77 		if (tz->governor->bind_to_tz(tz)) {
78 			dev_err(&tz->device,
79 				"governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
80 				failed_gov_name, tz->governor->name, tz->type);
81 			tz->governor = NULL;
82 		}
83 	}
84 }
85 
86 /**
87  * thermal_set_governor() - Switch to another governor
88  * @tz:		a valid pointer to a struct thermal_zone_device
89  * @new_gov:	pointer to the new governor
90  *
91  * Change the governor of thermal zone @tz.
92  *
93  * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
94  */
95 static int thermal_set_governor(struct thermal_zone_device *tz,
96 				struct thermal_governor *new_gov)
97 {
98 	int ret = 0;
99 
100 	if (tz->governor && tz->governor->unbind_from_tz)
101 		tz->governor->unbind_from_tz(tz);
102 
103 	if (new_gov && new_gov->bind_to_tz) {
104 		ret = new_gov->bind_to_tz(tz);
105 		if (ret) {
106 			bind_previous_governor(tz, new_gov->name);
107 
108 			return ret;
109 		}
110 	}
111 
112 	tz->governor = new_gov;
113 
114 	return ret;
115 }
116 
117 int thermal_register_governor(struct thermal_governor *governor)
118 {
119 	int err;
120 	const char *name;
121 	struct thermal_zone_device *pos;
122 
123 	if (!governor)
124 		return -EINVAL;
125 
126 	mutex_lock(&thermal_governor_lock);
127 
128 	err = -EBUSY;
129 	if (!__find_governor(governor->name)) {
130 		bool match_default;
131 
132 		err = 0;
133 		list_add(&governor->governor_list, &thermal_governor_list);
134 		match_default = !strncmp(governor->name,
135 					 DEFAULT_THERMAL_GOVERNOR,
136 					 THERMAL_NAME_LENGTH);
137 
138 		if (!def_governor && match_default)
139 			def_governor = governor;
140 	}
141 
142 	mutex_lock(&thermal_list_lock);
143 
144 	list_for_each_entry(pos, &thermal_tz_list, node) {
145 		/*
146 		 * only thermal zones with specified tz->tzp->governor_name
147 		 * may run with tz->govenor unset
148 		 */
149 		if (pos->governor)
150 			continue;
151 
152 		name = pos->tzp->governor_name;
153 
154 		if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
155 			int ret;
156 
157 			ret = thermal_set_governor(pos, governor);
158 			if (ret)
159 				dev_err(&pos->device,
160 					"Failed to set governor %s for thermal zone %s: %d\n",
161 					governor->name, pos->type, ret);
162 		}
163 	}
164 
165 	mutex_unlock(&thermal_list_lock);
166 	mutex_unlock(&thermal_governor_lock);
167 
168 	return err;
169 }
170 
171 void thermal_unregister_governor(struct thermal_governor *governor)
172 {
173 	struct thermal_zone_device *pos;
174 
175 	if (!governor)
176 		return;
177 
178 	mutex_lock(&thermal_governor_lock);
179 
180 	if (!__find_governor(governor->name))
181 		goto exit;
182 
183 	mutex_lock(&thermal_list_lock);
184 
185 	list_for_each_entry(pos, &thermal_tz_list, node) {
186 		if (!strncasecmp(pos->governor->name, governor->name,
187 				 THERMAL_NAME_LENGTH))
188 			thermal_set_governor(pos, NULL);
189 	}
190 
191 	mutex_unlock(&thermal_list_lock);
192 	list_del(&governor->governor_list);
193 exit:
194 	mutex_unlock(&thermal_governor_lock);
195 }
196 
197 int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
198 				   char *policy)
199 {
200 	struct thermal_governor *gov;
201 	int ret = -EINVAL;
202 
203 	mutex_lock(&thermal_governor_lock);
204 	mutex_lock(&tz->lock);
205 
206 	gov = __find_governor(strim(policy));
207 	if (!gov)
208 		goto exit;
209 
210 	ret = thermal_set_governor(tz, gov);
211 
212 exit:
213 	mutex_unlock(&tz->lock);
214 	mutex_unlock(&thermal_governor_lock);
215 
216 	thermal_notify_tz_gov_change(tz->id, policy);
217 
218 	return ret;
219 }
220 
221 int thermal_build_list_of_policies(char *buf)
222 {
223 	struct thermal_governor *pos;
224 	ssize_t count = 0;
225 
226 	mutex_lock(&thermal_governor_lock);
227 
228 	list_for_each_entry(pos, &thermal_governor_list, governor_list) {
229 		count += scnprintf(buf + count, PAGE_SIZE - count, "%s ",
230 				   pos->name);
231 	}
232 	count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
233 
234 	mutex_unlock(&thermal_governor_lock);
235 
236 	return count;
237 }
238 
239 static void __init thermal_unregister_governors(void)
240 {
241 	struct thermal_governor **governor;
242 
243 	for_each_governor_table(governor)
244 		thermal_unregister_governor(*governor);
245 }
246 
247 static int __init thermal_register_governors(void)
248 {
249 	int ret = 0;
250 	struct thermal_governor **governor;
251 
252 	for_each_governor_table(governor) {
253 		ret = thermal_register_governor(*governor);
254 		if (ret) {
255 			pr_err("Failed to register governor: '%s'",
256 			       (*governor)->name);
257 			break;
258 		}
259 
260 		pr_info("Registered thermal governor '%s'",
261 			(*governor)->name);
262 	}
263 
264 	if (ret) {
265 		struct thermal_governor **gov;
266 
267 		for_each_governor_table(gov) {
268 			if (gov == governor)
269 				break;
270 			thermal_unregister_governor(*gov);
271 		}
272 	}
273 
274 	return ret;
275 }
276 
277 /*
278  * Zone update section: main control loop applied to each zone while monitoring
279  *
280  * in polling mode. The monitoring is done using a workqueue.
281  * Same update may be done on a zone by calling thermal_zone_device_update().
282  *
283  * An update means:
284  * - Non-critical trips will invoke the governor responsible for that zone;
285  * - Hot trips will produce a notification to userspace;
286  * - Critical trip point will cause a system shutdown.
287  */
288 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
289 					    unsigned long delay)
290 {
291 	if (delay)
292 		mod_delayed_work(system_freezable_power_efficient_wq,
293 				 &tz->poll_queue, delay);
294 	else
295 		cancel_delayed_work(&tz->poll_queue);
296 }
297 
298 static inline bool should_stop_polling(struct thermal_zone_device *tz)
299 {
300 	return !thermal_zone_device_is_enabled(tz);
301 }
302 
303 static void monitor_thermal_zone(struct thermal_zone_device *tz)
304 {
305 	bool stop;
306 
307 	stop = should_stop_polling(tz);
308 
309 	mutex_lock(&tz->lock);
310 
311 	if (!stop && tz->passive)
312 		thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
313 	else if (!stop && tz->polling_delay_jiffies)
314 		thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
315 	else
316 		thermal_zone_device_set_polling(tz, 0);
317 
318 	mutex_unlock(&tz->lock);
319 }
320 
321 static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
322 {
323 	tz->governor ? tz->governor->throttle(tz, trip) :
324 		       def_governor->throttle(tz, trip);
325 }
326 
327 void thermal_zone_device_critical(struct thermal_zone_device *tz)
328 {
329 	/*
330 	 * poweroff_delay_ms must be a carefully profiled positive value.
331 	 * Its a must for forced_emergency_poweroff_work to be scheduled.
332 	 */
333 	int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
334 
335 	dev_emerg(&tz->device, "%s: critical temperature reached, "
336 		  "shutting down\n", tz->type);
337 
338 	hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
339 }
340 EXPORT_SYMBOL(thermal_zone_device_critical);
341 
342 static void handle_critical_trips(struct thermal_zone_device *tz,
343 				  int trip, enum thermal_trip_type trip_type)
344 {
345 	int trip_temp;
346 
347 	tz->ops->get_trip_temp(tz, trip, &trip_temp);
348 
349 	/* If we have not crossed the trip_temp, we do not care. */
350 	if (trip_temp <= 0 || tz->temperature < trip_temp)
351 		return;
352 
353 	trace_thermal_zone_trip(tz, trip, trip_type);
354 
355 	if (trip_type == THERMAL_TRIP_HOT && tz->ops->hot)
356 		tz->ops->hot(tz);
357 	else if (trip_type == THERMAL_TRIP_CRITICAL)
358 		tz->ops->critical(tz);
359 }
360 
361 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
362 {
363 	enum thermal_trip_type type;
364 	int trip_temp, hyst = 0;
365 
366 	/* Ignore disabled trip points */
367 	if (test_bit(trip, &tz->trips_disabled))
368 		return;
369 
370 	tz->ops->get_trip_temp(tz, trip, &trip_temp);
371 	tz->ops->get_trip_type(tz, trip, &type);
372 	if (tz->ops->get_trip_hyst)
373 		tz->ops->get_trip_hyst(tz, trip, &hyst);
374 
375 	if (tz->last_temperature != THERMAL_TEMP_INVALID) {
376 		if (tz->last_temperature < trip_temp &&
377 		    tz->temperature >= trip_temp)
378 			thermal_notify_tz_trip_up(tz->id, trip,
379 						  tz->temperature);
380 		if (tz->last_temperature >= trip_temp &&
381 		    tz->temperature < (trip_temp - hyst))
382 			thermal_notify_tz_trip_down(tz->id, trip,
383 						    tz->temperature);
384 	}
385 
386 	if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
387 		handle_critical_trips(tz, trip, type);
388 	else
389 		handle_non_critical_trips(tz, trip);
390 	/*
391 	 * Alright, we handled this trip successfully.
392 	 * So, start monitoring again.
393 	 */
394 	monitor_thermal_zone(tz);
395 }
396 
397 static void update_temperature(struct thermal_zone_device *tz)
398 {
399 	int temp, ret;
400 
401 	ret = thermal_zone_get_temp(tz, &temp);
402 	if (ret) {
403 		if (ret != -EAGAIN)
404 			dev_warn(&tz->device,
405 				 "failed to read out thermal zone (%d)\n",
406 				 ret);
407 		return;
408 	}
409 
410 	mutex_lock(&tz->lock);
411 	tz->last_temperature = tz->temperature;
412 	tz->temperature = temp;
413 	mutex_unlock(&tz->lock);
414 
415 	trace_thermal_temperature(tz);
416 
417 	thermal_genl_sampling_temp(tz->id, temp);
418 }
419 
420 static void thermal_zone_device_init(struct thermal_zone_device *tz)
421 {
422 	struct thermal_instance *pos;
423 	tz->temperature = THERMAL_TEMP_INVALID;
424 	tz->prev_low_trip = -INT_MAX;
425 	tz->prev_high_trip = INT_MAX;
426 	list_for_each_entry(pos, &tz->thermal_instances, tz_node)
427 		pos->initialized = false;
428 }
429 
430 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
431 					enum thermal_device_mode mode)
432 {
433 	int ret = 0;
434 
435 	mutex_lock(&tz->lock);
436 
437 	/* do nothing if mode isn't changing */
438 	if (mode == tz->mode) {
439 		mutex_unlock(&tz->lock);
440 
441 		return ret;
442 	}
443 
444 	if (tz->ops->change_mode)
445 		ret = tz->ops->change_mode(tz, mode);
446 
447 	if (!ret)
448 		tz->mode = mode;
449 
450 	mutex_unlock(&tz->lock);
451 
452 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
453 
454 	if (mode == THERMAL_DEVICE_ENABLED)
455 		thermal_notify_tz_enable(tz->id);
456 	else
457 		thermal_notify_tz_disable(tz->id);
458 
459 	return ret;
460 }
461 
462 int thermal_zone_device_enable(struct thermal_zone_device *tz)
463 {
464 	return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
465 }
466 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
467 
468 int thermal_zone_device_disable(struct thermal_zone_device *tz)
469 {
470 	return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
471 }
472 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
473 
474 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
475 {
476 	enum thermal_device_mode mode;
477 
478 	mutex_lock(&tz->lock);
479 
480 	mode = tz->mode;
481 
482 	mutex_unlock(&tz->lock);
483 
484 	return mode == THERMAL_DEVICE_ENABLED;
485 }
486 
487 void thermal_zone_device_update(struct thermal_zone_device *tz,
488 				enum thermal_notify_event event)
489 {
490 	int count;
491 
492 	if (should_stop_polling(tz))
493 		return;
494 
495 	if (atomic_read(&in_suspend))
496 		return;
497 
498 	if (WARN_ONCE(!tz->ops->get_temp, "'%s' must not be called without "
499 		      "'get_temp' ops set\n", __func__))
500 		return;
501 
502 	update_temperature(tz);
503 
504 	thermal_zone_set_trips(tz);
505 
506 	tz->notify_event = event;
507 
508 	for (count = 0; count < tz->trips; count++)
509 		handle_thermal_trip(tz, count);
510 }
511 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
512 
513 static void thermal_zone_device_check(struct work_struct *work)
514 {
515 	struct thermal_zone_device *tz = container_of(work, struct
516 						      thermal_zone_device,
517 						      poll_queue.work);
518 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
519 }
520 
521 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
522 			      void *data)
523 {
524 	struct thermal_governor *gov;
525 	int ret = 0;
526 
527 	mutex_lock(&thermal_governor_lock);
528 	list_for_each_entry(gov, &thermal_governor_list, governor_list) {
529 		ret = cb(gov, data);
530 		if (ret)
531 			break;
532 	}
533 	mutex_unlock(&thermal_governor_lock);
534 
535 	return ret;
536 }
537 
538 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
539 					      void *), void *data)
540 {
541 	struct thermal_cooling_device *cdev;
542 	int ret = 0;
543 
544 	mutex_lock(&thermal_list_lock);
545 	list_for_each_entry(cdev, &thermal_cdev_list, node) {
546 		ret = cb(cdev, data);
547 		if (ret)
548 			break;
549 	}
550 	mutex_unlock(&thermal_list_lock);
551 
552 	return ret;
553 }
554 
555 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
556 			  void *data)
557 {
558 	struct thermal_zone_device *tz;
559 	int ret = 0;
560 
561 	mutex_lock(&thermal_list_lock);
562 	list_for_each_entry(tz, &thermal_tz_list, node) {
563 		ret = cb(tz, data);
564 		if (ret)
565 			break;
566 	}
567 	mutex_unlock(&thermal_list_lock);
568 
569 	return ret;
570 }
571 
572 struct thermal_zone_device *thermal_zone_get_by_id(int id)
573 {
574 	struct thermal_zone_device *tz, *match = NULL;
575 
576 	mutex_lock(&thermal_list_lock);
577 	list_for_each_entry(tz, &thermal_tz_list, node) {
578 		if (tz->id == id) {
579 			match = tz;
580 			break;
581 		}
582 	}
583 	mutex_unlock(&thermal_list_lock);
584 
585 	return match;
586 }
587 
588 /*
589  * Device management section: cooling devices, zones devices, and binding
590  *
591  * Set of functions provided by the thermal core for:
592  * - cooling devices lifecycle: registration, unregistration,
593  *				binding, and unbinding.
594  * - thermal zone devices lifecycle: registration, unregistration,
595  *				     binding, and unbinding.
596  */
597 
598 /**
599  * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
600  * @tz:		pointer to struct thermal_zone_device
601  * @trip:	indicates which trip point the cooling devices is
602  *		associated with in this thermal zone.
603  * @cdev:	pointer to struct thermal_cooling_device
604  * @upper:	the Maximum cooling state for this trip point.
605  *		THERMAL_NO_LIMIT means no upper limit,
606  *		and the cooling device can be in max_state.
607  * @lower:	the Minimum cooling state can be used for this trip point.
608  *		THERMAL_NO_LIMIT means no lower limit,
609  *		and the cooling device can be in cooling state 0.
610  * @weight:	The weight of the cooling device to be bound to the
611  *		thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
612  *		default value
613  *
614  * This interface function bind a thermal cooling device to the certain trip
615  * point of a thermal zone device.
616  * This function is usually called in the thermal zone device .bind callback.
617  *
618  * Return: 0 on success, the proper error value otherwise.
619  */
620 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
621 				     int trip,
622 				     struct thermal_cooling_device *cdev,
623 				     unsigned long upper, unsigned long lower,
624 				     unsigned int weight)
625 {
626 	struct thermal_instance *dev;
627 	struct thermal_instance *pos;
628 	struct thermal_zone_device *pos1;
629 	struct thermal_cooling_device *pos2;
630 	unsigned long max_state;
631 	int result, ret;
632 
633 	if (trip >= tz->trips || trip < 0)
634 		return -EINVAL;
635 
636 	list_for_each_entry(pos1, &thermal_tz_list, node) {
637 		if (pos1 == tz)
638 			break;
639 	}
640 	list_for_each_entry(pos2, &thermal_cdev_list, node) {
641 		if (pos2 == cdev)
642 			break;
643 	}
644 
645 	if (tz != pos1 || cdev != pos2)
646 		return -EINVAL;
647 
648 	ret = cdev->ops->get_max_state(cdev, &max_state);
649 	if (ret)
650 		return ret;
651 
652 	/* lower default 0, upper default max_state */
653 	lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
654 	upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
655 
656 	if (lower > upper || upper > max_state)
657 		return -EINVAL;
658 
659 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
660 	if (!dev)
661 		return -ENOMEM;
662 	dev->tz = tz;
663 	dev->cdev = cdev;
664 	dev->trip = trip;
665 	dev->upper = upper;
666 	dev->lower = lower;
667 	dev->target = THERMAL_NO_TARGET;
668 	dev->weight = weight;
669 
670 	result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
671 	if (result < 0)
672 		goto free_mem;
673 
674 	dev->id = result;
675 	sprintf(dev->name, "cdev%d", dev->id);
676 	result =
677 	    sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
678 	if (result)
679 		goto release_ida;
680 
681 	sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
682 	sysfs_attr_init(&dev->attr.attr);
683 	dev->attr.attr.name = dev->attr_name;
684 	dev->attr.attr.mode = 0444;
685 	dev->attr.show = trip_point_show;
686 	result = device_create_file(&tz->device, &dev->attr);
687 	if (result)
688 		goto remove_symbol_link;
689 
690 	sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
691 	sysfs_attr_init(&dev->weight_attr.attr);
692 	dev->weight_attr.attr.name = dev->weight_attr_name;
693 	dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
694 	dev->weight_attr.show = weight_show;
695 	dev->weight_attr.store = weight_store;
696 	result = device_create_file(&tz->device, &dev->weight_attr);
697 	if (result)
698 		goto remove_trip_file;
699 
700 	mutex_lock(&tz->lock);
701 	mutex_lock(&cdev->lock);
702 	list_for_each_entry(pos, &tz->thermal_instances, tz_node)
703 		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
704 			result = -EEXIST;
705 			break;
706 		}
707 	if (!result) {
708 		list_add_tail(&dev->tz_node, &tz->thermal_instances);
709 		list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
710 		atomic_set(&tz->need_update, 1);
711 	}
712 	mutex_unlock(&cdev->lock);
713 	mutex_unlock(&tz->lock);
714 
715 	if (!result)
716 		return 0;
717 
718 	device_remove_file(&tz->device, &dev->weight_attr);
719 remove_trip_file:
720 	device_remove_file(&tz->device, &dev->attr);
721 remove_symbol_link:
722 	sysfs_remove_link(&tz->device.kobj, dev->name);
723 release_ida:
724 	ida_simple_remove(&tz->ida, dev->id);
725 free_mem:
726 	kfree(dev);
727 	return result;
728 }
729 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
730 
731 /**
732  * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
733  *					  thermal zone.
734  * @tz:		pointer to a struct thermal_zone_device.
735  * @trip:	indicates which trip point the cooling devices is
736  *		associated with in this thermal zone.
737  * @cdev:	pointer to a struct thermal_cooling_device.
738  *
739  * This interface function unbind a thermal cooling device from the certain
740  * trip point of a thermal zone device.
741  * This function is usually called in the thermal zone device .unbind callback.
742  *
743  * Return: 0 on success, the proper error value otherwise.
744  */
745 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
746 				       int trip,
747 				       struct thermal_cooling_device *cdev)
748 {
749 	struct thermal_instance *pos, *next;
750 
751 	mutex_lock(&tz->lock);
752 	mutex_lock(&cdev->lock);
753 	list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
754 		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
755 			list_del(&pos->tz_node);
756 			list_del(&pos->cdev_node);
757 			mutex_unlock(&cdev->lock);
758 			mutex_unlock(&tz->lock);
759 			goto unbind;
760 		}
761 	}
762 	mutex_unlock(&cdev->lock);
763 	mutex_unlock(&tz->lock);
764 
765 	return -ENODEV;
766 
767 unbind:
768 	device_remove_file(&tz->device, &pos->weight_attr);
769 	device_remove_file(&tz->device, &pos->attr);
770 	sysfs_remove_link(&tz->device.kobj, pos->name);
771 	ida_simple_remove(&tz->ida, pos->id);
772 	kfree(pos);
773 	return 0;
774 }
775 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
776 
777 static void thermal_release(struct device *dev)
778 {
779 	struct thermal_zone_device *tz;
780 	struct thermal_cooling_device *cdev;
781 
782 	if (!strncmp(dev_name(dev), "thermal_zone",
783 		     sizeof("thermal_zone") - 1)) {
784 		tz = to_thermal_zone(dev);
785 		thermal_zone_destroy_device_groups(tz);
786 		kfree(tz);
787 	} else if (!strncmp(dev_name(dev), "cooling_device",
788 			    sizeof("cooling_device") - 1)) {
789 		cdev = to_cooling_device(dev);
790 		kfree(cdev);
791 	}
792 }
793 
794 static struct class thermal_class = {
795 	.name = "thermal",
796 	.dev_release = thermal_release,
797 };
798 
799 static inline
800 void print_bind_err_msg(struct thermal_zone_device *tz,
801 			struct thermal_cooling_device *cdev, int ret)
802 {
803 	dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
804 		tz->type, cdev->type, ret);
805 }
806 
807 static void __bind(struct thermal_zone_device *tz, int mask,
808 		   struct thermal_cooling_device *cdev,
809 		   unsigned long *limits,
810 		   unsigned int weight)
811 {
812 	int i, ret;
813 
814 	for (i = 0; i < tz->trips; i++) {
815 		if (mask & (1 << i)) {
816 			unsigned long upper, lower;
817 
818 			upper = THERMAL_NO_LIMIT;
819 			lower = THERMAL_NO_LIMIT;
820 			if (limits) {
821 				lower = limits[i * 2];
822 				upper = limits[i * 2 + 1];
823 			}
824 			ret = thermal_zone_bind_cooling_device(tz, i, cdev,
825 							       upper, lower,
826 							       weight);
827 			if (ret)
828 				print_bind_err_msg(tz, cdev, ret);
829 		}
830 	}
831 }
832 
833 static void bind_cdev(struct thermal_cooling_device *cdev)
834 {
835 	int i, ret;
836 	const struct thermal_zone_params *tzp;
837 	struct thermal_zone_device *pos = NULL;
838 
839 	mutex_lock(&thermal_list_lock);
840 
841 	list_for_each_entry(pos, &thermal_tz_list, node) {
842 		if (!pos->tzp && !pos->ops->bind)
843 			continue;
844 
845 		if (pos->ops->bind) {
846 			ret = pos->ops->bind(pos, cdev);
847 			if (ret)
848 				print_bind_err_msg(pos, cdev, ret);
849 			continue;
850 		}
851 
852 		tzp = pos->tzp;
853 		if (!tzp || !tzp->tbp)
854 			continue;
855 
856 		for (i = 0; i < tzp->num_tbps; i++) {
857 			if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
858 				continue;
859 			if (tzp->tbp[i].match(pos, cdev))
860 				continue;
861 			tzp->tbp[i].cdev = cdev;
862 			__bind(pos, tzp->tbp[i].trip_mask, cdev,
863 			       tzp->tbp[i].binding_limits,
864 			       tzp->tbp[i].weight);
865 		}
866 	}
867 
868 	mutex_unlock(&thermal_list_lock);
869 }
870 
871 /**
872  * __thermal_cooling_device_register() - register a new thermal cooling device
873  * @np:		a pointer to a device tree node.
874  * @type:	the thermal cooling device type.
875  * @devdata:	device private data.
876  * @ops:		standard thermal cooling devices callbacks.
877  *
878  * This interface function adds a new thermal cooling device (fan/processor/...)
879  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
880  * to all the thermal zone devices registered at the same time.
881  * It also gives the opportunity to link the cooling device to a device tree
882  * node, so that it can be bound to a thermal zone created out of device tree.
883  *
884  * Return: a pointer to the created struct thermal_cooling_device or an
885  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
886  */
887 static struct thermal_cooling_device *
888 __thermal_cooling_device_register(struct device_node *np,
889 				  const char *type, void *devdata,
890 				  const struct thermal_cooling_device_ops *ops)
891 {
892 	struct thermal_cooling_device *cdev;
893 	struct thermal_zone_device *pos = NULL;
894 	int id, ret;
895 
896 	if (!ops || !ops->get_max_state || !ops->get_cur_state ||
897 	    !ops->set_cur_state)
898 		return ERR_PTR(-EINVAL);
899 
900 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
901 	if (!cdev)
902 		return ERR_PTR(-ENOMEM);
903 
904 	ret = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
905 	if (ret < 0)
906 		goto out_kfree_cdev;
907 	cdev->id = ret;
908 	id = ret;
909 
910 	ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
911 	if (ret)
912 		goto out_ida_remove;
913 
914 	cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
915 	if (!cdev->type) {
916 		ret = -ENOMEM;
917 		goto out_ida_remove;
918 	}
919 
920 	mutex_init(&cdev->lock);
921 	INIT_LIST_HEAD(&cdev->thermal_instances);
922 	cdev->np = np;
923 	cdev->ops = ops;
924 	cdev->updated = false;
925 	cdev->device.class = &thermal_class;
926 	cdev->devdata = devdata;
927 	thermal_cooling_device_setup_sysfs(cdev);
928 	ret = device_register(&cdev->device);
929 	if (ret)
930 		goto out_kfree_type;
931 
932 	/* Add 'this' new cdev to the global cdev list */
933 	mutex_lock(&thermal_list_lock);
934 	list_add(&cdev->node, &thermal_cdev_list);
935 	mutex_unlock(&thermal_list_lock);
936 
937 	/* Update binding information for 'this' new cdev */
938 	bind_cdev(cdev);
939 
940 	mutex_lock(&thermal_list_lock);
941 	list_for_each_entry(pos, &thermal_tz_list, node)
942 		if (atomic_cmpxchg(&pos->need_update, 1, 0))
943 			thermal_zone_device_update(pos,
944 						   THERMAL_EVENT_UNSPECIFIED);
945 	mutex_unlock(&thermal_list_lock);
946 
947 	return cdev;
948 
949 out_kfree_type:
950 	thermal_cooling_device_destroy_sysfs(cdev);
951 	kfree(cdev->type);
952 	put_device(&cdev->device);
953 	cdev = NULL;
954 out_ida_remove:
955 	ida_simple_remove(&thermal_cdev_ida, id);
956 out_kfree_cdev:
957 	kfree(cdev);
958 	return ERR_PTR(ret);
959 }
960 
961 /**
962  * thermal_cooling_device_register() - register a new thermal cooling device
963  * @type:	the thermal cooling device type.
964  * @devdata:	device private data.
965  * @ops:		standard thermal cooling devices callbacks.
966  *
967  * This interface function adds a new thermal cooling device (fan/processor/...)
968  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
969  * to all the thermal zone devices registered at the same time.
970  *
971  * Return: a pointer to the created struct thermal_cooling_device or an
972  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
973  */
974 struct thermal_cooling_device *
975 thermal_cooling_device_register(const char *type, void *devdata,
976 				const struct thermal_cooling_device_ops *ops)
977 {
978 	return __thermal_cooling_device_register(NULL, type, devdata, ops);
979 }
980 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
981 
982 /**
983  * thermal_of_cooling_device_register() - register an OF thermal cooling device
984  * @np:		a pointer to a device tree node.
985  * @type:	the thermal cooling device type.
986  * @devdata:	device private data.
987  * @ops:		standard thermal cooling devices callbacks.
988  *
989  * This function will register a cooling device with device tree node reference.
990  * This interface function adds a new thermal cooling device (fan/processor/...)
991  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
992  * to all the thermal zone devices registered at the same time.
993  *
994  * Return: a pointer to the created struct thermal_cooling_device or an
995  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
996  */
997 struct thermal_cooling_device *
998 thermal_of_cooling_device_register(struct device_node *np,
999 				   const char *type, void *devdata,
1000 				   const struct thermal_cooling_device_ops *ops)
1001 {
1002 	return __thermal_cooling_device_register(np, type, devdata, ops);
1003 }
1004 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1005 
1006 static void thermal_cooling_device_release(struct device *dev, void *res)
1007 {
1008 	thermal_cooling_device_unregister(
1009 				*(struct thermal_cooling_device **)res);
1010 }
1011 
1012 /**
1013  * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1014  *					       device
1015  * @dev:	a valid struct device pointer of a sensor device.
1016  * @np:		a pointer to a device tree node.
1017  * @type:	the thermal cooling device type.
1018  * @devdata:	device private data.
1019  * @ops:	standard thermal cooling devices callbacks.
1020  *
1021  * This function will register a cooling device with device tree node reference.
1022  * This interface function adds a new thermal cooling device (fan/processor/...)
1023  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1024  * to all the thermal zone devices registered at the same time.
1025  *
1026  * Return: a pointer to the created struct thermal_cooling_device or an
1027  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1028  */
1029 struct thermal_cooling_device *
1030 devm_thermal_of_cooling_device_register(struct device *dev,
1031 				struct device_node *np,
1032 				char *type, void *devdata,
1033 				const struct thermal_cooling_device_ops *ops)
1034 {
1035 	struct thermal_cooling_device **ptr, *tcd;
1036 
1037 	ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1038 			   GFP_KERNEL);
1039 	if (!ptr)
1040 		return ERR_PTR(-ENOMEM);
1041 
1042 	tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1043 	if (IS_ERR(tcd)) {
1044 		devres_free(ptr);
1045 		return tcd;
1046 	}
1047 
1048 	*ptr = tcd;
1049 	devres_add(dev, ptr);
1050 
1051 	return tcd;
1052 }
1053 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1054 
1055 static void __unbind(struct thermal_zone_device *tz, int mask,
1056 		     struct thermal_cooling_device *cdev)
1057 {
1058 	int i;
1059 
1060 	for (i = 0; i < tz->trips; i++)
1061 		if (mask & (1 << i))
1062 			thermal_zone_unbind_cooling_device(tz, i, cdev);
1063 }
1064 
1065 /**
1066  * thermal_cooling_device_unregister - removes a thermal cooling device
1067  * @cdev:	the thermal cooling device to remove.
1068  *
1069  * thermal_cooling_device_unregister() must be called when a registered
1070  * thermal cooling device is no longer needed.
1071  */
1072 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1073 {
1074 	int i;
1075 	const struct thermal_zone_params *tzp;
1076 	struct thermal_zone_device *tz;
1077 	struct thermal_cooling_device *pos = NULL;
1078 
1079 	if (!cdev)
1080 		return;
1081 
1082 	mutex_lock(&thermal_list_lock);
1083 	list_for_each_entry(pos, &thermal_cdev_list, node)
1084 		if (pos == cdev)
1085 			break;
1086 	if (pos != cdev) {
1087 		/* thermal cooling device not found */
1088 		mutex_unlock(&thermal_list_lock);
1089 		return;
1090 	}
1091 	list_del(&cdev->node);
1092 
1093 	/* Unbind all thermal zones associated with 'this' cdev */
1094 	list_for_each_entry(tz, &thermal_tz_list, node) {
1095 		if (tz->ops->unbind) {
1096 			tz->ops->unbind(tz, cdev);
1097 			continue;
1098 		}
1099 
1100 		if (!tz->tzp || !tz->tzp->tbp)
1101 			continue;
1102 
1103 		tzp = tz->tzp;
1104 		for (i = 0; i < tzp->num_tbps; i++) {
1105 			if (tzp->tbp[i].cdev == cdev) {
1106 				__unbind(tz, tzp->tbp[i].trip_mask, cdev);
1107 				tzp->tbp[i].cdev = NULL;
1108 			}
1109 		}
1110 	}
1111 
1112 	mutex_unlock(&thermal_list_lock);
1113 
1114 	ida_simple_remove(&thermal_cdev_ida, cdev->id);
1115 	device_del(&cdev->device);
1116 	thermal_cooling_device_destroy_sysfs(cdev);
1117 	kfree(cdev->type);
1118 	put_device(&cdev->device);
1119 }
1120 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1121 
1122 static void bind_tz(struct thermal_zone_device *tz)
1123 {
1124 	int i, ret;
1125 	struct thermal_cooling_device *pos = NULL;
1126 	const struct thermal_zone_params *tzp = tz->tzp;
1127 
1128 	if (!tzp && !tz->ops->bind)
1129 		return;
1130 
1131 	mutex_lock(&thermal_list_lock);
1132 
1133 	/* If there is ops->bind, try to use ops->bind */
1134 	if (tz->ops->bind) {
1135 		list_for_each_entry(pos, &thermal_cdev_list, node) {
1136 			ret = tz->ops->bind(tz, pos);
1137 			if (ret)
1138 				print_bind_err_msg(tz, pos, ret);
1139 		}
1140 		goto exit;
1141 	}
1142 
1143 	if (!tzp || !tzp->tbp)
1144 		goto exit;
1145 
1146 	list_for_each_entry(pos, &thermal_cdev_list, node) {
1147 		for (i = 0; i < tzp->num_tbps; i++) {
1148 			if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1149 				continue;
1150 			if (tzp->tbp[i].match(tz, pos))
1151 				continue;
1152 			tzp->tbp[i].cdev = pos;
1153 			__bind(tz, tzp->tbp[i].trip_mask, pos,
1154 			       tzp->tbp[i].binding_limits,
1155 			       tzp->tbp[i].weight);
1156 		}
1157 	}
1158 exit:
1159 	mutex_unlock(&thermal_list_lock);
1160 }
1161 
1162 /**
1163  * thermal_zone_device_register() - register a new thermal zone device
1164  * @type:	the thermal zone device type
1165  * @trips:	the number of trip points the thermal zone support
1166  * @mask:	a bit string indicating the writeablility of trip points
1167  * @devdata:	private device data
1168  * @ops:	standard thermal zone device callbacks
1169  * @tzp:	thermal zone platform parameters
1170  * @passive_delay: number of milliseconds to wait between polls when
1171  *		   performing passive cooling
1172  * @polling_delay: number of milliseconds to wait between polls when checking
1173  *		   whether trip points have been crossed (0 for interrupt
1174  *		   driven systems)
1175  *
1176  * This interface function adds a new thermal zone device (sensor) to
1177  * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1178  * thermal cooling devices registered at the same time.
1179  * thermal_zone_device_unregister() must be called when the device is no
1180  * longer needed. The passive cooling depends on the .get_trend() return value.
1181  *
1182  * Return: a pointer to the created struct thermal_zone_device or an
1183  * in case of error, an ERR_PTR. Caller must check return value with
1184  * IS_ERR*() helpers.
1185  */
1186 struct thermal_zone_device *
1187 thermal_zone_device_register(const char *type, int trips, int mask,
1188 			     void *devdata, struct thermal_zone_device_ops *ops,
1189 			     struct thermal_zone_params *tzp, int passive_delay,
1190 			     int polling_delay)
1191 {
1192 	struct thermal_zone_device *tz;
1193 	enum thermal_trip_type trip_type;
1194 	int trip_temp;
1195 	int id;
1196 	int result;
1197 	int count;
1198 	struct thermal_governor *governor;
1199 
1200 	if (!type || strlen(type) == 0) {
1201 		pr_err("Error: No thermal zone type defined\n");
1202 		return ERR_PTR(-EINVAL);
1203 	}
1204 
1205 	if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1206 		pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
1207 		       type, THERMAL_NAME_LENGTH);
1208 		return ERR_PTR(-EINVAL);
1209 	}
1210 
1211 	if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) {
1212 		pr_err("Error: Incorrect number of thermal trips\n");
1213 		return ERR_PTR(-EINVAL);
1214 	}
1215 
1216 	if (!ops) {
1217 		pr_err("Error: Thermal zone device ops not defined\n");
1218 		return ERR_PTR(-EINVAL);
1219 	}
1220 
1221 	if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1222 		return ERR_PTR(-EINVAL);
1223 
1224 	tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1225 	if (!tz)
1226 		return ERR_PTR(-ENOMEM);
1227 
1228 	INIT_LIST_HEAD(&tz->thermal_instances);
1229 	ida_init(&tz->ida);
1230 	mutex_init(&tz->lock);
1231 	id = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1232 	if (id < 0) {
1233 		result = id;
1234 		goto free_tz;
1235 	}
1236 
1237 	tz->id = id;
1238 	strlcpy(tz->type, type, sizeof(tz->type));
1239 
1240 	result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1241 	if (result)
1242 		goto remove_id;
1243 
1244 	if (!ops->critical)
1245 		ops->critical = thermal_zone_device_critical;
1246 
1247 	tz->ops = ops;
1248 	tz->tzp = tzp;
1249 	tz->device.class = &thermal_class;
1250 	tz->devdata = devdata;
1251 	tz->trips = trips;
1252 
1253 	thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1254 	thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1255 
1256 	/* sys I/F */
1257 	/* Add nodes that are always present via .groups */
1258 	result = thermal_zone_create_device_groups(tz, mask);
1259 	if (result)
1260 		goto remove_id;
1261 
1262 	/* A new thermal zone needs to be updated anyway. */
1263 	atomic_set(&tz->need_update, 1);
1264 
1265 	result = device_register(&tz->device);
1266 	if (result)
1267 		goto release_device;
1268 
1269 	for (count = 0; count < trips; count++) {
1270 		if (tz->ops->get_trip_type(tz, count, &trip_type) ||
1271 		    tz->ops->get_trip_temp(tz, count, &trip_temp) ||
1272 		    !trip_temp)
1273 			set_bit(count, &tz->trips_disabled);
1274 	}
1275 
1276 	/* Update 'this' zone's governor information */
1277 	mutex_lock(&thermal_governor_lock);
1278 
1279 	if (tz->tzp)
1280 		governor = __find_governor(tz->tzp->governor_name);
1281 	else
1282 		governor = def_governor;
1283 
1284 	result = thermal_set_governor(tz, governor);
1285 	if (result) {
1286 		mutex_unlock(&thermal_governor_lock);
1287 		goto unregister;
1288 	}
1289 
1290 	mutex_unlock(&thermal_governor_lock);
1291 
1292 	if (!tz->tzp || !tz->tzp->no_hwmon) {
1293 		result = thermal_add_hwmon_sysfs(tz);
1294 		if (result)
1295 			goto unregister;
1296 	}
1297 
1298 	mutex_lock(&thermal_list_lock);
1299 	list_add_tail(&tz->node, &thermal_tz_list);
1300 	mutex_unlock(&thermal_list_lock);
1301 
1302 	/* Bind cooling devices for this zone */
1303 	bind_tz(tz);
1304 
1305 	INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1306 
1307 	thermal_zone_device_init(tz);
1308 	/* Update the new thermal zone and mark it as already updated. */
1309 	if (atomic_cmpxchg(&tz->need_update, 1, 0))
1310 		thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1311 
1312 	thermal_notify_tz_create(tz->id, tz->type);
1313 
1314 	return tz;
1315 
1316 unregister:
1317 	device_del(&tz->device);
1318 release_device:
1319 	put_device(&tz->device);
1320 	tz = NULL;
1321 remove_id:
1322 	ida_simple_remove(&thermal_tz_ida, id);
1323 free_tz:
1324 	kfree(tz);
1325 	return ERR_PTR(result);
1326 }
1327 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1328 
1329 /**
1330  * thermal_zone_device_unregister - removes the registered thermal zone device
1331  * @tz: the thermal zone device to remove
1332  */
1333 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1334 {
1335 	int i, tz_id;
1336 	const struct thermal_zone_params *tzp;
1337 	struct thermal_cooling_device *cdev;
1338 	struct thermal_zone_device *pos = NULL;
1339 
1340 	if (!tz)
1341 		return;
1342 
1343 	tzp = tz->tzp;
1344 	tz_id = tz->id;
1345 
1346 	mutex_lock(&thermal_list_lock);
1347 	list_for_each_entry(pos, &thermal_tz_list, node)
1348 		if (pos == tz)
1349 			break;
1350 	if (pos != tz) {
1351 		/* thermal zone device not found */
1352 		mutex_unlock(&thermal_list_lock);
1353 		return;
1354 	}
1355 	list_del(&tz->node);
1356 
1357 	/* Unbind all cdevs associated with 'this' thermal zone */
1358 	list_for_each_entry(cdev, &thermal_cdev_list, node) {
1359 		if (tz->ops->unbind) {
1360 			tz->ops->unbind(tz, cdev);
1361 			continue;
1362 		}
1363 
1364 		if (!tzp || !tzp->tbp)
1365 			break;
1366 
1367 		for (i = 0; i < tzp->num_tbps; i++) {
1368 			if (tzp->tbp[i].cdev == cdev) {
1369 				__unbind(tz, tzp->tbp[i].trip_mask, cdev);
1370 				tzp->tbp[i].cdev = NULL;
1371 			}
1372 		}
1373 	}
1374 
1375 	mutex_unlock(&thermal_list_lock);
1376 
1377 	cancel_delayed_work_sync(&tz->poll_queue);
1378 
1379 	thermal_set_governor(tz, NULL);
1380 
1381 	thermal_remove_hwmon_sysfs(tz);
1382 	ida_simple_remove(&thermal_tz_ida, tz->id);
1383 	ida_destroy(&tz->ida);
1384 	mutex_destroy(&tz->lock);
1385 	device_unregister(&tz->device);
1386 
1387 	thermal_notify_tz_delete(tz_id);
1388 }
1389 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1390 
1391 /**
1392  * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1393  * @name: thermal zone name to fetch the temperature
1394  *
1395  * When only one zone is found with the passed name, returns a reference to it.
1396  *
1397  * Return: On success returns a reference to an unique thermal zone with
1398  * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1399  * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1400  */
1401 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1402 {
1403 	struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1404 	unsigned int found = 0;
1405 
1406 	if (!name)
1407 		goto exit;
1408 
1409 	mutex_lock(&thermal_list_lock);
1410 	list_for_each_entry(pos, &thermal_tz_list, node)
1411 		if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1412 			found++;
1413 			ref = pos;
1414 		}
1415 	mutex_unlock(&thermal_list_lock);
1416 
1417 	/* nothing has been found, thus an error code for it */
1418 	if (found == 0)
1419 		ref = ERR_PTR(-ENODEV);
1420 	else if (found > 1)
1421 	/* Success only when an unique zone is found */
1422 		ref = ERR_PTR(-EEXIST);
1423 
1424 exit:
1425 	return ref;
1426 }
1427 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1428 
1429 static int thermal_pm_notify(struct notifier_block *nb,
1430 			     unsigned long mode, void *_unused)
1431 {
1432 	struct thermal_zone_device *tz;
1433 
1434 	switch (mode) {
1435 	case PM_HIBERNATION_PREPARE:
1436 	case PM_RESTORE_PREPARE:
1437 	case PM_SUSPEND_PREPARE:
1438 		atomic_set(&in_suspend, 1);
1439 		break;
1440 	case PM_POST_HIBERNATION:
1441 	case PM_POST_RESTORE:
1442 	case PM_POST_SUSPEND:
1443 		atomic_set(&in_suspend, 0);
1444 		list_for_each_entry(tz, &thermal_tz_list, node) {
1445 			if (!thermal_zone_device_is_enabled(tz))
1446 				continue;
1447 
1448 			thermal_zone_device_init(tz);
1449 			thermal_zone_device_update(tz,
1450 						   THERMAL_EVENT_UNSPECIFIED);
1451 		}
1452 		break;
1453 	default:
1454 		break;
1455 	}
1456 	return 0;
1457 }
1458 
1459 static struct notifier_block thermal_pm_nb = {
1460 	.notifier_call = thermal_pm_notify,
1461 };
1462 
1463 static int __init thermal_init(void)
1464 {
1465 	int result;
1466 
1467 	result = thermal_netlink_init();
1468 	if (result)
1469 		goto error;
1470 
1471 	result = thermal_register_governors();
1472 	if (result)
1473 		goto error;
1474 
1475 	result = class_register(&thermal_class);
1476 	if (result)
1477 		goto unregister_governors;
1478 
1479 	result = of_parse_thermal_zones();
1480 	if (result)
1481 		goto unregister_class;
1482 
1483 	result = register_pm_notifier(&thermal_pm_nb);
1484 	if (result)
1485 		pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1486 			result);
1487 
1488 	return 0;
1489 
1490 unregister_class:
1491 	class_unregister(&thermal_class);
1492 unregister_governors:
1493 	thermal_unregister_governors();
1494 error:
1495 	ida_destroy(&thermal_tz_ida);
1496 	ida_destroy(&thermal_cdev_ida);
1497 	mutex_destroy(&thermal_list_lock);
1498 	mutex_destroy(&thermal_governor_lock);
1499 	return result;
1500 }
1501 postcore_initcall(thermal_init);
1502