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, int trip_temp, enum thermal_trip_type trip_type)
344 {
345 	/* If we have not crossed the trip_temp, we do not care. */
346 	if (trip_temp <= 0 || tz->temperature < trip_temp)
347 		return;
348 
349 	trace_thermal_zone_trip(tz, trip, trip_type);
350 
351 	if (trip_type == THERMAL_TRIP_HOT && tz->ops->hot)
352 		tz->ops->hot(tz);
353 	else if (trip_type == THERMAL_TRIP_CRITICAL)
354 		tz->ops->critical(tz);
355 }
356 
357 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
358 {
359 	enum thermal_trip_type type;
360 	int trip_temp, hyst = 0;
361 
362 	/* Ignore disabled trip points */
363 	if (test_bit(trip, &tz->trips_disabled))
364 		return;
365 
366 	tz->ops->get_trip_temp(tz, trip, &trip_temp);
367 	tz->ops->get_trip_type(tz, trip, &type);
368 	if (tz->ops->get_trip_hyst)
369 		tz->ops->get_trip_hyst(tz, trip, &hyst);
370 
371 	if (tz->last_temperature != THERMAL_TEMP_INVALID) {
372 		if (tz->last_temperature < trip_temp &&
373 		    tz->temperature >= trip_temp)
374 			thermal_notify_tz_trip_up(tz->id, trip,
375 						  tz->temperature);
376 		if (tz->last_temperature >= trip_temp &&
377 		    tz->temperature < (trip_temp - hyst))
378 			thermal_notify_tz_trip_down(tz->id, trip,
379 						    tz->temperature);
380 	}
381 
382 	if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
383 		handle_critical_trips(tz, trip, trip_temp, type);
384 	else
385 		handle_non_critical_trips(tz, trip);
386 	/*
387 	 * Alright, we handled this trip successfully.
388 	 * So, start monitoring again.
389 	 */
390 	monitor_thermal_zone(tz);
391 }
392 
393 static void update_temperature(struct thermal_zone_device *tz)
394 {
395 	int temp, ret;
396 
397 	ret = thermal_zone_get_temp(tz, &temp);
398 	if (ret) {
399 		if (ret != -EAGAIN)
400 			dev_warn(&tz->device,
401 				 "failed to read out thermal zone (%d)\n",
402 				 ret);
403 		return;
404 	}
405 
406 	mutex_lock(&tz->lock);
407 	tz->last_temperature = tz->temperature;
408 	tz->temperature = temp;
409 	mutex_unlock(&tz->lock);
410 
411 	trace_thermal_temperature(tz);
412 
413 	thermal_genl_sampling_temp(tz->id, temp);
414 }
415 
416 static void thermal_zone_device_init(struct thermal_zone_device *tz)
417 {
418 	struct thermal_instance *pos;
419 	tz->temperature = THERMAL_TEMP_INVALID;
420 	tz->prev_low_trip = -INT_MAX;
421 	tz->prev_high_trip = INT_MAX;
422 	list_for_each_entry(pos, &tz->thermal_instances, tz_node)
423 		pos->initialized = false;
424 }
425 
426 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
427 					enum thermal_device_mode mode)
428 {
429 	int ret = 0;
430 
431 	mutex_lock(&tz->lock);
432 
433 	/* do nothing if mode isn't changing */
434 	if (mode == tz->mode) {
435 		mutex_unlock(&tz->lock);
436 
437 		return ret;
438 	}
439 
440 	if (tz->ops->change_mode)
441 		ret = tz->ops->change_mode(tz, mode);
442 
443 	if (!ret)
444 		tz->mode = mode;
445 
446 	mutex_unlock(&tz->lock);
447 
448 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
449 
450 	if (mode == THERMAL_DEVICE_ENABLED)
451 		thermal_notify_tz_enable(tz->id);
452 	else
453 		thermal_notify_tz_disable(tz->id);
454 
455 	return ret;
456 }
457 
458 int thermal_zone_device_enable(struct thermal_zone_device *tz)
459 {
460 	return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
461 }
462 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
463 
464 int thermal_zone_device_disable(struct thermal_zone_device *tz)
465 {
466 	return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
467 }
468 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
469 
470 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
471 {
472 	enum thermal_device_mode mode;
473 
474 	mutex_lock(&tz->lock);
475 
476 	mode = tz->mode;
477 
478 	mutex_unlock(&tz->lock);
479 
480 	return mode == THERMAL_DEVICE_ENABLED;
481 }
482 
483 void thermal_zone_device_update(struct thermal_zone_device *tz,
484 				enum thermal_notify_event event)
485 {
486 	int count;
487 
488 	if (should_stop_polling(tz))
489 		return;
490 
491 	if (atomic_read(&in_suspend))
492 		return;
493 
494 	if (WARN_ONCE(!tz->ops->get_temp, "'%s' must not be called without "
495 		      "'get_temp' ops set\n", __func__))
496 		return;
497 
498 	update_temperature(tz);
499 
500 	thermal_zone_set_trips(tz);
501 
502 	tz->notify_event = event;
503 
504 	for (count = 0; count < tz->num_trips; count++)
505 		handle_thermal_trip(tz, count);
506 }
507 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
508 
509 static void thermal_zone_device_check(struct work_struct *work)
510 {
511 	struct thermal_zone_device *tz = container_of(work, struct
512 						      thermal_zone_device,
513 						      poll_queue.work);
514 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
515 }
516 
517 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
518 			      void *data)
519 {
520 	struct thermal_governor *gov;
521 	int ret = 0;
522 
523 	mutex_lock(&thermal_governor_lock);
524 	list_for_each_entry(gov, &thermal_governor_list, governor_list) {
525 		ret = cb(gov, data);
526 		if (ret)
527 			break;
528 	}
529 	mutex_unlock(&thermal_governor_lock);
530 
531 	return ret;
532 }
533 
534 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
535 					      void *), void *data)
536 {
537 	struct thermal_cooling_device *cdev;
538 	int ret = 0;
539 
540 	mutex_lock(&thermal_list_lock);
541 	list_for_each_entry(cdev, &thermal_cdev_list, node) {
542 		ret = cb(cdev, data);
543 		if (ret)
544 			break;
545 	}
546 	mutex_unlock(&thermal_list_lock);
547 
548 	return ret;
549 }
550 
551 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
552 			  void *data)
553 {
554 	struct thermal_zone_device *tz;
555 	int ret = 0;
556 
557 	mutex_lock(&thermal_list_lock);
558 	list_for_each_entry(tz, &thermal_tz_list, node) {
559 		ret = cb(tz, data);
560 		if (ret)
561 			break;
562 	}
563 	mutex_unlock(&thermal_list_lock);
564 
565 	return ret;
566 }
567 
568 struct thermal_zone_device *thermal_zone_get_by_id(int id)
569 {
570 	struct thermal_zone_device *tz, *match = NULL;
571 
572 	mutex_lock(&thermal_list_lock);
573 	list_for_each_entry(tz, &thermal_tz_list, node) {
574 		if (tz->id == id) {
575 			match = tz;
576 			break;
577 		}
578 	}
579 	mutex_unlock(&thermal_list_lock);
580 
581 	return match;
582 }
583 
584 /*
585  * Device management section: cooling devices, zones devices, and binding
586  *
587  * Set of functions provided by the thermal core for:
588  * - cooling devices lifecycle: registration, unregistration,
589  *				binding, and unbinding.
590  * - thermal zone devices lifecycle: registration, unregistration,
591  *				     binding, and unbinding.
592  */
593 
594 /**
595  * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
596  * @tz:		pointer to struct thermal_zone_device
597  * @trip:	indicates which trip point the cooling devices is
598  *		associated with in this thermal zone.
599  * @cdev:	pointer to struct thermal_cooling_device
600  * @upper:	the Maximum cooling state for this trip point.
601  *		THERMAL_NO_LIMIT means no upper limit,
602  *		and the cooling device can be in max_state.
603  * @lower:	the Minimum cooling state can be used for this trip point.
604  *		THERMAL_NO_LIMIT means no lower limit,
605  *		and the cooling device can be in cooling state 0.
606  * @weight:	The weight of the cooling device to be bound to the
607  *		thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
608  *		default value
609  *
610  * This interface function bind a thermal cooling device to the certain trip
611  * point of a thermal zone device.
612  * This function is usually called in the thermal zone device .bind callback.
613  *
614  * Return: 0 on success, the proper error value otherwise.
615  */
616 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
617 				     int trip,
618 				     struct thermal_cooling_device *cdev,
619 				     unsigned long upper, unsigned long lower,
620 				     unsigned int weight)
621 {
622 	struct thermal_instance *dev;
623 	struct thermal_instance *pos;
624 	struct thermal_zone_device *pos1;
625 	struct thermal_cooling_device *pos2;
626 	unsigned long max_state;
627 	int result, ret;
628 
629 	if (trip >= tz->num_trips || trip < 0)
630 		return -EINVAL;
631 
632 	list_for_each_entry(pos1, &thermal_tz_list, node) {
633 		if (pos1 == tz)
634 			break;
635 	}
636 	list_for_each_entry(pos2, &thermal_cdev_list, node) {
637 		if (pos2 == cdev)
638 			break;
639 	}
640 
641 	if (tz != pos1 || cdev != pos2)
642 		return -EINVAL;
643 
644 	ret = cdev->ops->get_max_state(cdev, &max_state);
645 	if (ret)
646 		return ret;
647 
648 	/* lower default 0, upper default max_state */
649 	lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
650 	upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
651 
652 	if (lower > upper || upper > max_state)
653 		return -EINVAL;
654 
655 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
656 	if (!dev)
657 		return -ENOMEM;
658 	dev->tz = tz;
659 	dev->cdev = cdev;
660 	dev->trip = trip;
661 	dev->upper = upper;
662 	dev->lower = lower;
663 	dev->target = THERMAL_NO_TARGET;
664 	dev->weight = weight;
665 
666 	result = ida_alloc(&tz->ida, GFP_KERNEL);
667 	if (result < 0)
668 		goto free_mem;
669 
670 	dev->id = result;
671 	sprintf(dev->name, "cdev%d", dev->id);
672 	result =
673 	    sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
674 	if (result)
675 		goto release_ida;
676 
677 	sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
678 	sysfs_attr_init(&dev->attr.attr);
679 	dev->attr.attr.name = dev->attr_name;
680 	dev->attr.attr.mode = 0444;
681 	dev->attr.show = trip_point_show;
682 	result = device_create_file(&tz->device, &dev->attr);
683 	if (result)
684 		goto remove_symbol_link;
685 
686 	sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
687 	sysfs_attr_init(&dev->weight_attr.attr);
688 	dev->weight_attr.attr.name = dev->weight_attr_name;
689 	dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
690 	dev->weight_attr.show = weight_show;
691 	dev->weight_attr.store = weight_store;
692 	result = device_create_file(&tz->device, &dev->weight_attr);
693 	if (result)
694 		goto remove_trip_file;
695 
696 	mutex_lock(&tz->lock);
697 	mutex_lock(&cdev->lock);
698 	list_for_each_entry(pos, &tz->thermal_instances, tz_node)
699 		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
700 			result = -EEXIST;
701 			break;
702 		}
703 	if (!result) {
704 		list_add_tail(&dev->tz_node, &tz->thermal_instances);
705 		list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
706 		atomic_set(&tz->need_update, 1);
707 	}
708 	mutex_unlock(&cdev->lock);
709 	mutex_unlock(&tz->lock);
710 
711 	if (!result)
712 		return 0;
713 
714 	device_remove_file(&tz->device, &dev->weight_attr);
715 remove_trip_file:
716 	device_remove_file(&tz->device, &dev->attr);
717 remove_symbol_link:
718 	sysfs_remove_link(&tz->device.kobj, dev->name);
719 release_ida:
720 	ida_free(&tz->ida, dev->id);
721 free_mem:
722 	kfree(dev);
723 	return result;
724 }
725 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
726 
727 /**
728  * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
729  *					  thermal zone.
730  * @tz:		pointer to a struct thermal_zone_device.
731  * @trip:	indicates which trip point the cooling devices is
732  *		associated with in this thermal zone.
733  * @cdev:	pointer to a struct thermal_cooling_device.
734  *
735  * This interface function unbind a thermal cooling device from the certain
736  * trip point of a thermal zone device.
737  * This function is usually called in the thermal zone device .unbind callback.
738  *
739  * Return: 0 on success, the proper error value otherwise.
740  */
741 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
742 				       int trip,
743 				       struct thermal_cooling_device *cdev)
744 {
745 	struct thermal_instance *pos, *next;
746 
747 	mutex_lock(&tz->lock);
748 	mutex_lock(&cdev->lock);
749 	list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
750 		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
751 			list_del(&pos->tz_node);
752 			list_del(&pos->cdev_node);
753 			mutex_unlock(&cdev->lock);
754 			mutex_unlock(&tz->lock);
755 			goto unbind;
756 		}
757 	}
758 	mutex_unlock(&cdev->lock);
759 	mutex_unlock(&tz->lock);
760 
761 	return -ENODEV;
762 
763 unbind:
764 	device_remove_file(&tz->device, &pos->weight_attr);
765 	device_remove_file(&tz->device, &pos->attr);
766 	sysfs_remove_link(&tz->device.kobj, pos->name);
767 	ida_free(&tz->ida, pos->id);
768 	kfree(pos);
769 	return 0;
770 }
771 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
772 
773 static void thermal_release(struct device *dev)
774 {
775 	struct thermal_zone_device *tz;
776 	struct thermal_cooling_device *cdev;
777 
778 	if (!strncmp(dev_name(dev), "thermal_zone",
779 		     sizeof("thermal_zone") - 1)) {
780 		tz = to_thermal_zone(dev);
781 		thermal_zone_destroy_device_groups(tz);
782 		kfree(tz);
783 	} else if (!strncmp(dev_name(dev), "cooling_device",
784 			    sizeof("cooling_device") - 1)) {
785 		cdev = to_cooling_device(dev);
786 		kfree(cdev);
787 	}
788 }
789 
790 static struct class thermal_class = {
791 	.name = "thermal",
792 	.dev_release = thermal_release,
793 };
794 
795 static inline
796 void print_bind_err_msg(struct thermal_zone_device *tz,
797 			struct thermal_cooling_device *cdev, int ret)
798 {
799 	dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
800 		tz->type, cdev->type, ret);
801 }
802 
803 static void __bind(struct thermal_zone_device *tz, int mask,
804 		   struct thermal_cooling_device *cdev,
805 		   unsigned long *limits,
806 		   unsigned int weight)
807 {
808 	int i, ret;
809 
810 	for (i = 0; i < tz->num_trips; i++) {
811 		if (mask & (1 << i)) {
812 			unsigned long upper, lower;
813 
814 			upper = THERMAL_NO_LIMIT;
815 			lower = THERMAL_NO_LIMIT;
816 			if (limits) {
817 				lower = limits[i * 2];
818 				upper = limits[i * 2 + 1];
819 			}
820 			ret = thermal_zone_bind_cooling_device(tz, i, cdev,
821 							       upper, lower,
822 							       weight);
823 			if (ret)
824 				print_bind_err_msg(tz, cdev, ret);
825 		}
826 	}
827 }
828 
829 static void bind_cdev(struct thermal_cooling_device *cdev)
830 {
831 	int i, ret;
832 	const struct thermal_zone_params *tzp;
833 	struct thermal_zone_device *pos = NULL;
834 
835 	mutex_lock(&thermal_list_lock);
836 
837 	list_for_each_entry(pos, &thermal_tz_list, node) {
838 		if (!pos->tzp && !pos->ops->bind)
839 			continue;
840 
841 		if (pos->ops->bind) {
842 			ret = pos->ops->bind(pos, cdev);
843 			if (ret)
844 				print_bind_err_msg(pos, cdev, ret);
845 			continue;
846 		}
847 
848 		tzp = pos->tzp;
849 		if (!tzp || !tzp->tbp)
850 			continue;
851 
852 		for (i = 0; i < tzp->num_tbps; i++) {
853 			if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
854 				continue;
855 			if (tzp->tbp[i].match(pos, cdev))
856 				continue;
857 			tzp->tbp[i].cdev = cdev;
858 			__bind(pos, tzp->tbp[i].trip_mask, cdev,
859 			       tzp->tbp[i].binding_limits,
860 			       tzp->tbp[i].weight);
861 		}
862 	}
863 
864 	mutex_unlock(&thermal_list_lock);
865 }
866 
867 /**
868  * __thermal_cooling_device_register() - register a new thermal cooling device
869  * @np:		a pointer to a device tree node.
870  * @type:	the thermal cooling device type.
871  * @devdata:	device private data.
872  * @ops:		standard thermal cooling devices callbacks.
873  *
874  * This interface function adds a new thermal cooling device (fan/processor/...)
875  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
876  * to all the thermal zone devices registered at the same time.
877  * It also gives the opportunity to link the cooling device to a device tree
878  * node, so that it can be bound to a thermal zone created out of device tree.
879  *
880  * Return: a pointer to the created struct thermal_cooling_device or an
881  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
882  */
883 static struct thermal_cooling_device *
884 __thermal_cooling_device_register(struct device_node *np,
885 				  const char *type, void *devdata,
886 				  const struct thermal_cooling_device_ops *ops)
887 {
888 	struct thermal_cooling_device *cdev;
889 	struct thermal_zone_device *pos = NULL;
890 	int id, ret;
891 
892 	if (!ops || !ops->get_max_state || !ops->get_cur_state ||
893 	    !ops->set_cur_state)
894 		return ERR_PTR(-EINVAL);
895 
896 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
897 	if (!cdev)
898 		return ERR_PTR(-ENOMEM);
899 
900 	ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
901 	if (ret < 0)
902 		goto out_kfree_cdev;
903 	cdev->id = ret;
904 	id = ret;
905 
906 	ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
907 	if (ret)
908 		goto out_ida_remove;
909 
910 	cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
911 	if (!cdev->type) {
912 		ret = -ENOMEM;
913 		goto out_ida_remove;
914 	}
915 
916 	mutex_init(&cdev->lock);
917 	INIT_LIST_HEAD(&cdev->thermal_instances);
918 	cdev->np = np;
919 	cdev->ops = ops;
920 	cdev->updated = false;
921 	cdev->device.class = &thermal_class;
922 	cdev->devdata = devdata;
923 	thermal_cooling_device_setup_sysfs(cdev);
924 	ret = device_register(&cdev->device);
925 	if (ret)
926 		goto out_kfree_type;
927 
928 	/* Add 'this' new cdev to the global cdev list */
929 	mutex_lock(&thermal_list_lock);
930 	list_add(&cdev->node, &thermal_cdev_list);
931 	mutex_unlock(&thermal_list_lock);
932 
933 	/* Update binding information for 'this' new cdev */
934 	bind_cdev(cdev);
935 
936 	mutex_lock(&thermal_list_lock);
937 	list_for_each_entry(pos, &thermal_tz_list, node)
938 		if (atomic_cmpxchg(&pos->need_update, 1, 0))
939 			thermal_zone_device_update(pos,
940 						   THERMAL_EVENT_UNSPECIFIED);
941 	mutex_unlock(&thermal_list_lock);
942 
943 	return cdev;
944 
945 out_kfree_type:
946 	thermal_cooling_device_destroy_sysfs(cdev);
947 	kfree(cdev->type);
948 	put_device(&cdev->device);
949 	cdev = NULL;
950 out_ida_remove:
951 	ida_free(&thermal_cdev_ida, id);
952 out_kfree_cdev:
953 	kfree(cdev);
954 	return ERR_PTR(ret);
955 }
956 
957 /**
958  * thermal_cooling_device_register() - register a new thermal cooling device
959  * @type:	the thermal cooling device type.
960  * @devdata:	device private data.
961  * @ops:		standard thermal cooling devices callbacks.
962  *
963  * This interface function adds a new thermal cooling device (fan/processor/...)
964  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
965  * to all the thermal zone devices registered at the same time.
966  *
967  * Return: a pointer to the created struct thermal_cooling_device or an
968  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
969  */
970 struct thermal_cooling_device *
971 thermal_cooling_device_register(const char *type, void *devdata,
972 				const struct thermal_cooling_device_ops *ops)
973 {
974 	return __thermal_cooling_device_register(NULL, type, devdata, ops);
975 }
976 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
977 
978 /**
979  * thermal_of_cooling_device_register() - register an OF thermal cooling device
980  * @np:		a pointer to a device tree node.
981  * @type:	the thermal cooling device type.
982  * @devdata:	device private data.
983  * @ops:		standard thermal cooling devices callbacks.
984  *
985  * This function will register a cooling device with device tree node reference.
986  * This interface function adds a new thermal cooling device (fan/processor/...)
987  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
988  * to all the thermal zone devices registered at the same time.
989  *
990  * Return: a pointer to the created struct thermal_cooling_device or an
991  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
992  */
993 struct thermal_cooling_device *
994 thermal_of_cooling_device_register(struct device_node *np,
995 				   const char *type, void *devdata,
996 				   const struct thermal_cooling_device_ops *ops)
997 {
998 	return __thermal_cooling_device_register(np, type, devdata, ops);
999 }
1000 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1001 
1002 static void thermal_cooling_device_release(struct device *dev, void *res)
1003 {
1004 	thermal_cooling_device_unregister(
1005 				*(struct thermal_cooling_device **)res);
1006 }
1007 
1008 /**
1009  * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1010  *					       device
1011  * @dev:	a valid struct device pointer of a sensor device.
1012  * @np:		a pointer to a device tree node.
1013  * @type:	the thermal cooling device type.
1014  * @devdata:	device private data.
1015  * @ops:	standard thermal cooling devices callbacks.
1016  *
1017  * This function will register a cooling device with device tree node reference.
1018  * This interface function adds a new thermal cooling device (fan/processor/...)
1019  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1020  * to all the thermal zone devices registered at the same time.
1021  *
1022  * Return: a pointer to the created struct thermal_cooling_device or an
1023  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1024  */
1025 struct thermal_cooling_device *
1026 devm_thermal_of_cooling_device_register(struct device *dev,
1027 				struct device_node *np,
1028 				char *type, void *devdata,
1029 				const struct thermal_cooling_device_ops *ops)
1030 {
1031 	struct thermal_cooling_device **ptr, *tcd;
1032 
1033 	ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1034 			   GFP_KERNEL);
1035 	if (!ptr)
1036 		return ERR_PTR(-ENOMEM);
1037 
1038 	tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1039 	if (IS_ERR(tcd)) {
1040 		devres_free(ptr);
1041 		return tcd;
1042 	}
1043 
1044 	*ptr = tcd;
1045 	devres_add(dev, ptr);
1046 
1047 	return tcd;
1048 }
1049 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1050 
1051 static void __unbind(struct thermal_zone_device *tz, int mask,
1052 		     struct thermal_cooling_device *cdev)
1053 {
1054 	int i;
1055 
1056 	for (i = 0; i < tz->num_trips; i++)
1057 		if (mask & (1 << i))
1058 			thermal_zone_unbind_cooling_device(tz, i, cdev);
1059 }
1060 
1061 /**
1062  * thermal_cooling_device_unregister - removes a thermal cooling device
1063  * @cdev:	the thermal cooling device to remove.
1064  *
1065  * thermal_cooling_device_unregister() must be called when a registered
1066  * thermal cooling device is no longer needed.
1067  */
1068 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1069 {
1070 	int i;
1071 	const struct thermal_zone_params *tzp;
1072 	struct thermal_zone_device *tz;
1073 	struct thermal_cooling_device *pos = NULL;
1074 
1075 	if (!cdev)
1076 		return;
1077 
1078 	mutex_lock(&thermal_list_lock);
1079 	list_for_each_entry(pos, &thermal_cdev_list, node)
1080 		if (pos == cdev)
1081 			break;
1082 	if (pos != cdev) {
1083 		/* thermal cooling device not found */
1084 		mutex_unlock(&thermal_list_lock);
1085 		return;
1086 	}
1087 	list_del(&cdev->node);
1088 
1089 	/* Unbind all thermal zones associated with 'this' cdev */
1090 	list_for_each_entry(tz, &thermal_tz_list, node) {
1091 		if (tz->ops->unbind) {
1092 			tz->ops->unbind(tz, cdev);
1093 			continue;
1094 		}
1095 
1096 		if (!tz->tzp || !tz->tzp->tbp)
1097 			continue;
1098 
1099 		tzp = tz->tzp;
1100 		for (i = 0; i < tzp->num_tbps; i++) {
1101 			if (tzp->tbp[i].cdev == cdev) {
1102 				__unbind(tz, tzp->tbp[i].trip_mask, cdev);
1103 				tzp->tbp[i].cdev = NULL;
1104 			}
1105 		}
1106 	}
1107 
1108 	mutex_unlock(&thermal_list_lock);
1109 
1110 	ida_free(&thermal_cdev_ida, cdev->id);
1111 	device_del(&cdev->device);
1112 	thermal_cooling_device_destroy_sysfs(cdev);
1113 	kfree(cdev->type);
1114 	put_device(&cdev->device);
1115 }
1116 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1117 
1118 static void bind_tz(struct thermal_zone_device *tz)
1119 {
1120 	int i, ret;
1121 	struct thermal_cooling_device *pos = NULL;
1122 	const struct thermal_zone_params *tzp = tz->tzp;
1123 
1124 	if (!tzp && !tz->ops->bind)
1125 		return;
1126 
1127 	mutex_lock(&thermal_list_lock);
1128 
1129 	/* If there is ops->bind, try to use ops->bind */
1130 	if (tz->ops->bind) {
1131 		list_for_each_entry(pos, &thermal_cdev_list, node) {
1132 			ret = tz->ops->bind(tz, pos);
1133 			if (ret)
1134 				print_bind_err_msg(tz, pos, ret);
1135 		}
1136 		goto exit;
1137 	}
1138 
1139 	if (!tzp || !tzp->tbp)
1140 		goto exit;
1141 
1142 	list_for_each_entry(pos, &thermal_cdev_list, node) {
1143 		for (i = 0; i < tzp->num_tbps; i++) {
1144 			if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1145 				continue;
1146 			if (tzp->tbp[i].match(tz, pos))
1147 				continue;
1148 			tzp->tbp[i].cdev = pos;
1149 			__bind(tz, tzp->tbp[i].trip_mask, pos,
1150 			       tzp->tbp[i].binding_limits,
1151 			       tzp->tbp[i].weight);
1152 		}
1153 	}
1154 exit:
1155 	mutex_unlock(&thermal_list_lock);
1156 }
1157 
1158 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1159 {
1160 	*delay_jiffies = msecs_to_jiffies(delay_ms);
1161 	if (delay_ms > 1000)
1162 		*delay_jiffies = round_jiffies(*delay_jiffies);
1163 }
1164 
1165 /**
1166  * thermal_zone_device_register_with_trips() - register a new thermal zone device
1167  * @type:	the thermal zone device type
1168  * @trips:	a pointer to an array of thermal trips
1169  * @num_trips:	the number of trip points the thermal zone support
1170  * @mask:	a bit string indicating the writeablility of trip points
1171  * @devdata:	private device data
1172  * @ops:	standard thermal zone device callbacks
1173  * @tzp:	thermal zone platform parameters
1174  * @passive_delay: number of milliseconds to wait between polls when
1175  *		   performing passive cooling
1176  * @polling_delay: number of milliseconds to wait between polls when checking
1177  *		   whether trip points have been crossed (0 for interrupt
1178  *		   driven systems)
1179  *
1180  * This interface function adds a new thermal zone device (sensor) to
1181  * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1182  * thermal cooling devices registered at the same time.
1183  * thermal_zone_device_unregister() must be called when the device is no
1184  * longer needed. The passive cooling depends on the .get_trend() return value.
1185  *
1186  * Return: a pointer to the created struct thermal_zone_device or an
1187  * in case of error, an ERR_PTR. Caller must check return value with
1188  * IS_ERR*() helpers.
1189  */
1190 struct thermal_zone_device *
1191 thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask,
1192 					void *devdata, struct thermal_zone_device_ops *ops,
1193 					struct thermal_zone_params *tzp, int passive_delay,
1194 					int polling_delay)
1195 {
1196 	struct thermal_zone_device *tz;
1197 	enum thermal_trip_type trip_type;
1198 	int trip_temp;
1199 	int id;
1200 	int result;
1201 	int count;
1202 	struct thermal_governor *governor;
1203 
1204 	if (!type || strlen(type) == 0) {
1205 		pr_err("No thermal zone type defined\n");
1206 		return ERR_PTR(-EINVAL);
1207 	}
1208 
1209 	if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1210 		pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1211 		       type, THERMAL_NAME_LENGTH);
1212 		return ERR_PTR(-EINVAL);
1213 	}
1214 
1215 	if (num_trips > THERMAL_MAX_TRIPS || num_trips < 0 || mask >> num_trips) {
1216 		pr_err("Incorrect number of thermal trips\n");
1217 		return ERR_PTR(-EINVAL);
1218 	}
1219 
1220 	if (!ops) {
1221 		pr_err("Thermal zone device ops not defined\n");
1222 		return ERR_PTR(-EINVAL);
1223 	}
1224 
1225 	if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1226 		return ERR_PTR(-EINVAL);
1227 
1228 	tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1229 	if (!tz)
1230 		return ERR_PTR(-ENOMEM);
1231 
1232 	INIT_LIST_HEAD(&tz->thermal_instances);
1233 	ida_init(&tz->ida);
1234 	mutex_init(&tz->lock);
1235 	id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1236 	if (id < 0) {
1237 		result = id;
1238 		goto free_tz;
1239 	}
1240 
1241 	tz->id = id;
1242 	strlcpy(tz->type, type, sizeof(tz->type));
1243 
1244 	result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1245 	if (result)
1246 		goto remove_id;
1247 
1248 	if (!ops->critical)
1249 		ops->critical = thermal_zone_device_critical;
1250 
1251 	tz->ops = ops;
1252 	tz->tzp = tzp;
1253 	tz->device.class = &thermal_class;
1254 	tz->devdata = devdata;
1255 	tz->trips = trips;
1256 	tz->num_trips = num_trips;
1257 
1258 	thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1259 	thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1260 
1261 	/* sys I/F */
1262 	/* Add nodes that are always present via .groups */
1263 	result = thermal_zone_create_device_groups(tz, mask);
1264 	if (result)
1265 		goto remove_id;
1266 
1267 	/* A new thermal zone needs to be updated anyway. */
1268 	atomic_set(&tz->need_update, 1);
1269 
1270 	result = device_register(&tz->device);
1271 	if (result)
1272 		goto release_device;
1273 
1274 	for (count = 0; count < num_trips; count++) {
1275 		if (tz->ops->get_trip_type(tz, count, &trip_type) ||
1276 		    tz->ops->get_trip_temp(tz, count, &trip_temp) ||
1277 		    !trip_temp)
1278 			set_bit(count, &tz->trips_disabled);
1279 	}
1280 
1281 	/* Update 'this' zone's governor information */
1282 	mutex_lock(&thermal_governor_lock);
1283 
1284 	if (tz->tzp)
1285 		governor = __find_governor(tz->tzp->governor_name);
1286 	else
1287 		governor = def_governor;
1288 
1289 	result = thermal_set_governor(tz, governor);
1290 	if (result) {
1291 		mutex_unlock(&thermal_governor_lock);
1292 		goto unregister;
1293 	}
1294 
1295 	mutex_unlock(&thermal_governor_lock);
1296 
1297 	if (!tz->tzp || !tz->tzp->no_hwmon) {
1298 		result = thermal_add_hwmon_sysfs(tz);
1299 		if (result)
1300 			goto unregister;
1301 	}
1302 
1303 	mutex_lock(&thermal_list_lock);
1304 	list_add_tail(&tz->node, &thermal_tz_list);
1305 	mutex_unlock(&thermal_list_lock);
1306 
1307 	/* Bind cooling devices for this zone */
1308 	bind_tz(tz);
1309 
1310 	INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1311 
1312 	thermal_zone_device_init(tz);
1313 	/* Update the new thermal zone and mark it as already updated. */
1314 	if (atomic_cmpxchg(&tz->need_update, 1, 0))
1315 		thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1316 
1317 	thermal_notify_tz_create(tz->id, tz->type);
1318 
1319 	return tz;
1320 
1321 unregister:
1322 	device_del(&tz->device);
1323 release_device:
1324 	put_device(&tz->device);
1325 	tz = NULL;
1326 remove_id:
1327 	ida_free(&thermal_tz_ida, id);
1328 free_tz:
1329 	kfree(tz);
1330 	return ERR_PTR(result);
1331 }
1332 
1333 struct thermal_zone_device *thermal_zone_device_register(const char *type, int ntrips, int mask,
1334 							 void *devdata, struct thermal_zone_device_ops *ops,
1335 							 struct thermal_zone_params *tzp, int passive_delay,
1336 							 int polling_delay)
1337 {
1338 	return thermal_zone_device_register_with_trips(type, NULL, ntrips, mask,
1339 						       devdata, ops, tzp,
1340 						       passive_delay, polling_delay);
1341 }
1342 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1343 
1344 /**
1345  * thermal_zone_device_unregister - removes the registered thermal zone device
1346  * @tz: the thermal zone device to remove
1347  */
1348 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1349 {
1350 	int i, tz_id;
1351 	const struct thermal_zone_params *tzp;
1352 	struct thermal_cooling_device *cdev;
1353 	struct thermal_zone_device *pos = NULL;
1354 
1355 	if (!tz)
1356 		return;
1357 
1358 	tzp = tz->tzp;
1359 	tz_id = tz->id;
1360 
1361 	mutex_lock(&thermal_list_lock);
1362 	list_for_each_entry(pos, &thermal_tz_list, node)
1363 		if (pos == tz)
1364 			break;
1365 	if (pos != tz) {
1366 		/* thermal zone device not found */
1367 		mutex_unlock(&thermal_list_lock);
1368 		return;
1369 	}
1370 	list_del(&tz->node);
1371 
1372 	/* Unbind all cdevs associated with 'this' thermal zone */
1373 	list_for_each_entry(cdev, &thermal_cdev_list, node) {
1374 		if (tz->ops->unbind) {
1375 			tz->ops->unbind(tz, cdev);
1376 			continue;
1377 		}
1378 
1379 		if (!tzp || !tzp->tbp)
1380 			break;
1381 
1382 		for (i = 0; i < tzp->num_tbps; i++) {
1383 			if (tzp->tbp[i].cdev == cdev) {
1384 				__unbind(tz, tzp->tbp[i].trip_mask, cdev);
1385 				tzp->tbp[i].cdev = NULL;
1386 			}
1387 		}
1388 	}
1389 
1390 	mutex_unlock(&thermal_list_lock);
1391 
1392 	cancel_delayed_work_sync(&tz->poll_queue);
1393 
1394 	thermal_set_governor(tz, NULL);
1395 
1396 	thermal_remove_hwmon_sysfs(tz);
1397 	ida_free(&thermal_tz_ida, tz->id);
1398 	ida_destroy(&tz->ida);
1399 	mutex_destroy(&tz->lock);
1400 	device_unregister(&tz->device);
1401 
1402 	thermal_notify_tz_delete(tz_id);
1403 }
1404 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1405 
1406 /**
1407  * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1408  * @name: thermal zone name to fetch the temperature
1409  *
1410  * When only one zone is found with the passed name, returns a reference to it.
1411  *
1412  * Return: On success returns a reference to an unique thermal zone with
1413  * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1414  * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1415  */
1416 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1417 {
1418 	struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1419 	unsigned int found = 0;
1420 
1421 	if (!name)
1422 		goto exit;
1423 
1424 	mutex_lock(&thermal_list_lock);
1425 	list_for_each_entry(pos, &thermal_tz_list, node)
1426 		if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1427 			found++;
1428 			ref = pos;
1429 		}
1430 	mutex_unlock(&thermal_list_lock);
1431 
1432 	/* nothing has been found, thus an error code for it */
1433 	if (found == 0)
1434 		ref = ERR_PTR(-ENODEV);
1435 	else if (found > 1)
1436 	/* Success only when an unique zone is found */
1437 		ref = ERR_PTR(-EEXIST);
1438 
1439 exit:
1440 	return ref;
1441 }
1442 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1443 
1444 static int thermal_pm_notify(struct notifier_block *nb,
1445 			     unsigned long mode, void *_unused)
1446 {
1447 	struct thermal_zone_device *tz;
1448 
1449 	switch (mode) {
1450 	case PM_HIBERNATION_PREPARE:
1451 	case PM_RESTORE_PREPARE:
1452 	case PM_SUSPEND_PREPARE:
1453 		atomic_set(&in_suspend, 1);
1454 		break;
1455 	case PM_POST_HIBERNATION:
1456 	case PM_POST_RESTORE:
1457 	case PM_POST_SUSPEND:
1458 		atomic_set(&in_suspend, 0);
1459 		list_for_each_entry(tz, &thermal_tz_list, node) {
1460 			if (!thermal_zone_device_is_enabled(tz))
1461 				continue;
1462 
1463 			thermal_zone_device_init(tz);
1464 			thermal_zone_device_update(tz,
1465 						   THERMAL_EVENT_UNSPECIFIED);
1466 		}
1467 		break;
1468 	default:
1469 		break;
1470 	}
1471 	return 0;
1472 }
1473 
1474 static struct notifier_block thermal_pm_nb = {
1475 	.notifier_call = thermal_pm_notify,
1476 };
1477 
1478 static int __init thermal_init(void)
1479 {
1480 	int result;
1481 
1482 	result = thermal_netlink_init();
1483 	if (result)
1484 		goto error;
1485 
1486 	result = thermal_register_governors();
1487 	if (result)
1488 		goto error;
1489 
1490 	result = class_register(&thermal_class);
1491 	if (result)
1492 		goto unregister_governors;
1493 
1494 	result = of_parse_thermal_zones();
1495 	if (result)
1496 		goto unregister_class;
1497 
1498 	result = register_pm_notifier(&thermal_pm_nb);
1499 	if (result)
1500 		pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1501 			result);
1502 
1503 	return 0;
1504 
1505 unregister_class:
1506 	class_unregister(&thermal_class);
1507 unregister_governors:
1508 	thermal_unregister_governors();
1509 error:
1510 	ida_destroy(&thermal_tz_ida);
1511 	ida_destroy(&thermal_cdev_ida);
1512 	mutex_destroy(&thermal_list_lock);
1513 	mutex_destroy(&thermal_governor_lock);
1514 	return result;
1515 }
1516 postcore_initcall(thermal_init);
1517