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