xref: /linux/drivers/thermal/thermal_core.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
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 static int __thermal_zone_device_set_mode(struct thermal_zone_device *tz,
276 					  enum thermal_device_mode mode)
277 {
278 	if (tz->ops.change_mode) {
279 		int ret;
280 
281 		ret = tz->ops.change_mode(tz, mode);
282 		if (ret)
283 			return ret;
284 	}
285 
286 	tz->mode = mode;
287 
288 	return 0;
289 }
290 
291 static void thermal_zone_broken_disable(struct thermal_zone_device *tz)
292 {
293 	struct thermal_trip_desc *td;
294 
295 	dev_err(&tz->device, "Unable to get temperature, disabling!\n");
296 	/*
297 	 * This function only runs for enabled thermal zones, so no need to
298 	 * check for the current mode.
299 	 */
300 	__thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
301 	thermal_notify_tz_disable(tz);
302 
303 	for_each_trip_desc(tz, td) {
304 		if (td->trip.type == THERMAL_TRIP_CRITICAL &&
305 		    td->trip.temperature > THERMAL_TEMP_INVALID) {
306 			dev_crit(&tz->device,
307 				 "Disabled thermal zone with critical trip point\n");
308 			return;
309 		}
310 	}
311 }
312 
313 /*
314  * Zone update section: main control loop applied to each zone while monitoring
315  * in polling mode. The monitoring is done using a workqueue.
316  * Same update may be done on a zone by calling thermal_zone_device_update().
317  *
318  * An update means:
319  * - Non-critical trips will invoke the governor responsible for that zone;
320  * - Hot trips will produce a notification to userspace;
321  * - Critical trip point will cause a system shutdown.
322  */
323 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
324 					    unsigned long delay)
325 {
326 	if (delay)
327 		mod_delayed_work(system_freezable_power_efficient_wq,
328 				 &tz->poll_queue, delay);
329 	else
330 		cancel_delayed_work(&tz->poll_queue);
331 }
332 
333 static void thermal_zone_recheck(struct thermal_zone_device *tz, int error)
334 {
335 	if (error == -EAGAIN) {
336 		thermal_zone_device_set_polling(tz, THERMAL_RECHECK_DELAY);
337 		return;
338 	}
339 
340 	/*
341 	 * Print the message once to reduce log noise.  It will be followed by
342 	 * another one if the temperature cannot be determined after multiple
343 	 * attempts.
344 	 */
345 	if (tz->recheck_delay_jiffies == THERMAL_RECHECK_DELAY)
346 		dev_info(&tz->device, "Temperature check failed (%d)\n", error);
347 
348 	thermal_zone_device_set_polling(tz, tz->recheck_delay_jiffies);
349 
350 	tz->recheck_delay_jiffies += max(tz->recheck_delay_jiffies >> 1, 1ULL);
351 	if (tz->recheck_delay_jiffies > THERMAL_MAX_RECHECK_DELAY) {
352 		thermal_zone_broken_disable(tz);
353 		/*
354 		 * Restore the original recheck delay value to allow the thermal
355 		 * zone to try to recover when it is reenabled by user space.
356 		 */
357 		tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
358 	}
359 }
360 
361 static void monitor_thermal_zone(struct thermal_zone_device *tz)
362 {
363 	if (tz->mode != THERMAL_DEVICE_ENABLED)
364 		thermal_zone_device_set_polling(tz, 0);
365 	else if (tz->passive > 0)
366 		thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
367 	else if (tz->polling_delay_jiffies)
368 		thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
369 }
370 
371 static struct thermal_governor *thermal_get_tz_governor(struct thermal_zone_device *tz)
372 {
373 	if (tz->governor)
374 		return tz->governor;
375 
376 	return def_governor;
377 }
378 
379 void thermal_governor_update_tz(struct thermal_zone_device *tz,
380 				enum thermal_notify_event reason)
381 {
382 	if (!tz->governor || !tz->governor->update_tz)
383 		return;
384 
385 	tz->governor->update_tz(tz, reason);
386 }
387 
388 static void thermal_zone_device_halt(struct thermal_zone_device *tz, bool shutdown)
389 {
390 	/*
391 	 * poweroff_delay_ms must be a carefully profiled positive value.
392 	 * Its a must for forced_emergency_poweroff_work to be scheduled.
393 	 */
394 	int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
395 	const char *msg = "Temperature too high";
396 
397 	dev_emerg(&tz->device, "%s: critical temperature reached\n", tz->type);
398 
399 	if (shutdown)
400 		hw_protection_shutdown(msg, poweroff_delay_ms);
401 	else
402 		hw_protection_reboot(msg, poweroff_delay_ms);
403 }
404 
405 void thermal_zone_device_critical(struct thermal_zone_device *tz)
406 {
407 	thermal_zone_device_halt(tz, true);
408 }
409 EXPORT_SYMBOL(thermal_zone_device_critical);
410 
411 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz)
412 {
413 	thermal_zone_device_halt(tz, false);
414 }
415 
416 static void handle_critical_trips(struct thermal_zone_device *tz,
417 				  const struct thermal_trip *trip)
418 {
419 	trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type);
420 
421 	if (trip->type == THERMAL_TRIP_CRITICAL)
422 		tz->ops.critical(tz);
423 	else if (tz->ops.hot)
424 		tz->ops.hot(tz);
425 }
426 
427 static void handle_thermal_trip(struct thermal_zone_device *tz,
428 				struct thermal_trip_desc *td,
429 				struct list_head *way_up_list,
430 				struct list_head *way_down_list)
431 {
432 	const struct thermal_trip *trip = &td->trip;
433 	int old_threshold;
434 
435 	if (trip->temperature == THERMAL_TEMP_INVALID)
436 		return;
437 
438 	/*
439 	 * If the trip temperature or hysteresis has been updated recently,
440 	 * the threshold needs to be computed again using the new values.
441 	 * However, its initial value still reflects the old ones and that
442 	 * is what needs to be compared with the previous zone temperature
443 	 * to decide which action to take.
444 	 */
445 	old_threshold = td->threshold;
446 	td->threshold = trip->temperature;
447 
448 	if (tz->last_temperature >= old_threshold &&
449 	    tz->last_temperature != THERMAL_TEMP_INIT) {
450 		/*
451 		 * Mitigation is under way, so it needs to stop if the zone
452 		 * temperature falls below the low temperature of the trip.
453 		 * In that case, the trip temperature becomes the new threshold.
454 		 */
455 		if (tz->temperature < trip->temperature - trip->hysteresis) {
456 			list_add(&td->notify_list_node, way_down_list);
457 			td->notify_temp = trip->temperature - trip->hysteresis;
458 
459 			if (trip->type == THERMAL_TRIP_PASSIVE) {
460 				tz->passive--;
461 				WARN_ON(tz->passive < 0);
462 			}
463 		} else {
464 			td->threshold -= trip->hysteresis;
465 		}
466 	} else if (tz->temperature >= trip->temperature) {
467 		/*
468 		 * There is no mitigation under way, so it needs to be started
469 		 * if the zone temperature exceeds the trip one.  The new
470 		 * threshold is then set to the low temperature of the trip.
471 		 */
472 		list_add_tail(&td->notify_list_node, way_up_list);
473 		td->notify_temp = trip->temperature;
474 		td->threshold -= trip->hysteresis;
475 
476 		if (trip->type == THERMAL_TRIP_PASSIVE)
477 			tz->passive++;
478 		else if (trip->type == THERMAL_TRIP_CRITICAL ||
479 			 trip->type == THERMAL_TRIP_HOT)
480 			handle_critical_trips(tz, trip);
481 	}
482 }
483 
484 static void thermal_zone_device_check(struct work_struct *work)
485 {
486 	struct thermal_zone_device *tz = container_of(work, struct
487 						      thermal_zone_device,
488 						      poll_queue.work);
489 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
490 }
491 
492 static void thermal_zone_device_init(struct thermal_zone_device *tz)
493 {
494 	struct thermal_instance *pos;
495 
496 	INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
497 
498 	tz->temperature = THERMAL_TEMP_INIT;
499 	tz->passive = 0;
500 	tz->prev_low_trip = -INT_MAX;
501 	tz->prev_high_trip = INT_MAX;
502 	list_for_each_entry(pos, &tz->thermal_instances, tz_node)
503 		pos->initialized = false;
504 }
505 
506 static void thermal_governor_trip_crossed(struct thermal_governor *governor,
507 					  struct thermal_zone_device *tz,
508 					  const struct thermal_trip *trip,
509 					  bool crossed_up)
510 {
511 	if (trip->type == THERMAL_TRIP_HOT || trip->type == THERMAL_TRIP_CRITICAL)
512 		return;
513 
514 	if (governor->trip_crossed)
515 		governor->trip_crossed(tz, trip, crossed_up);
516 }
517 
518 static void thermal_trip_crossed(struct thermal_zone_device *tz,
519 				 const struct thermal_trip *trip,
520 				 struct thermal_governor *governor,
521 				 bool crossed_up)
522 {
523 	if (crossed_up) {
524 		thermal_notify_tz_trip_up(tz, trip);
525 		thermal_debug_tz_trip_up(tz, trip);
526 	} else {
527 		thermal_notify_tz_trip_down(tz, trip);
528 		thermal_debug_tz_trip_down(tz, trip);
529 	}
530 	thermal_governor_trip_crossed(governor, tz, trip, crossed_up);
531 }
532 
533 static int thermal_trip_notify_cmp(void *not_used, const struct list_head *a,
534 				   const struct list_head *b)
535 {
536 	struct thermal_trip_desc *tda = container_of(a, struct thermal_trip_desc,
537 						     notify_list_node);
538 	struct thermal_trip_desc *tdb = container_of(b, struct thermal_trip_desc,
539 						     notify_list_node);
540 	return tda->notify_temp - tdb->notify_temp;
541 }
542 
543 void __thermal_zone_device_update(struct thermal_zone_device *tz,
544 				  enum thermal_notify_event event)
545 {
546 	struct thermal_governor *governor = thermal_get_tz_governor(tz);
547 	struct thermal_trip_desc *td;
548 	LIST_HEAD(way_down_list);
549 	LIST_HEAD(way_up_list);
550 	int temp, ret;
551 
552 	if (tz->suspended)
553 		return;
554 
555 	if (!thermal_zone_device_is_enabled(tz))
556 		return;
557 
558 	ret = __thermal_zone_get_temp(tz, &temp);
559 	if (ret) {
560 		thermal_zone_recheck(tz, ret);
561 		return;
562 	} else if (temp <= THERMAL_TEMP_INVALID) {
563 		/*
564 		 * Special case: No valid temperature value is available, but
565 		 * the zone owner does not want the core to do anything about
566 		 * it.  Continue regular zone polling if needed, so that this
567 		 * function can be called again, but skip everything else.
568 		 */
569 		goto monitor;
570 	}
571 
572 	tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
573 
574 	tz->last_temperature = tz->temperature;
575 	tz->temperature = temp;
576 
577 	trace_thermal_temperature(tz);
578 
579 	thermal_genl_sampling_temp(tz->id, temp);
580 
581 	tz->notify_event = event;
582 
583 	for_each_trip_desc(tz, td)
584 		handle_thermal_trip(tz, td, &way_up_list, &way_down_list);
585 
586 	thermal_zone_set_trips(tz);
587 
588 	list_sort(NULL, &way_up_list, thermal_trip_notify_cmp);
589 	list_for_each_entry(td, &way_up_list, notify_list_node)
590 		thermal_trip_crossed(tz, &td->trip, governor, true);
591 
592 	list_sort(NULL, &way_down_list, thermal_trip_notify_cmp);
593 	list_for_each_entry_reverse(td, &way_down_list, notify_list_node)
594 		thermal_trip_crossed(tz, &td->trip, governor, false);
595 
596 	if (governor->manage)
597 		governor->manage(tz);
598 
599 	thermal_debug_update_trip_stats(tz);
600 
601 monitor:
602 	monitor_thermal_zone(tz);
603 }
604 
605 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
606 					enum thermal_device_mode mode)
607 {
608 	int ret;
609 
610 	mutex_lock(&tz->lock);
611 
612 	/* do nothing if mode isn't changing */
613 	if (mode == tz->mode) {
614 		mutex_unlock(&tz->lock);
615 
616 		return 0;
617 	}
618 
619 	ret = __thermal_zone_device_set_mode(tz, mode);
620 	if (ret) {
621 		mutex_unlock(&tz->lock);
622 
623 		return ret;
624 	}
625 
626 	__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
627 
628 	mutex_unlock(&tz->lock);
629 
630 	if (mode == THERMAL_DEVICE_ENABLED)
631 		thermal_notify_tz_enable(tz);
632 	else
633 		thermal_notify_tz_disable(tz);
634 
635 	return 0;
636 }
637 
638 int thermal_zone_device_enable(struct thermal_zone_device *tz)
639 {
640 	return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
641 }
642 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
643 
644 int thermal_zone_device_disable(struct thermal_zone_device *tz)
645 {
646 	return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
647 }
648 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
649 
650 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
651 {
652 	lockdep_assert_held(&tz->lock);
653 
654 	return tz->mode == THERMAL_DEVICE_ENABLED;
655 }
656 
657 static bool thermal_zone_is_present(struct thermal_zone_device *tz)
658 {
659 	return !list_empty(&tz->node);
660 }
661 
662 void thermal_zone_device_update(struct thermal_zone_device *tz,
663 				enum thermal_notify_event event)
664 {
665 	mutex_lock(&tz->lock);
666 	if (thermal_zone_is_present(tz))
667 		__thermal_zone_device_update(tz, event);
668 	mutex_unlock(&tz->lock);
669 }
670 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
671 
672 void thermal_zone_trip_down(struct thermal_zone_device *tz,
673 			    const struct thermal_trip *trip)
674 {
675 	thermal_trip_crossed(tz, trip, thermal_get_tz_governor(tz), false);
676 }
677 
678 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
679 			      void *data)
680 {
681 	struct thermal_governor *gov;
682 	int ret = 0;
683 
684 	mutex_lock(&thermal_governor_lock);
685 	list_for_each_entry(gov, &thermal_governor_list, governor_list) {
686 		ret = cb(gov, data);
687 		if (ret)
688 			break;
689 	}
690 	mutex_unlock(&thermal_governor_lock);
691 
692 	return ret;
693 }
694 
695 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
696 					      void *), void *data)
697 {
698 	struct thermal_cooling_device *cdev;
699 	int ret = 0;
700 
701 	mutex_lock(&thermal_list_lock);
702 	list_for_each_entry(cdev, &thermal_cdev_list, node) {
703 		ret = cb(cdev, data);
704 		if (ret)
705 			break;
706 	}
707 	mutex_unlock(&thermal_list_lock);
708 
709 	return ret;
710 }
711 
712 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
713 			  void *data)
714 {
715 	struct thermal_zone_device *tz;
716 	int ret = 0;
717 
718 	mutex_lock(&thermal_list_lock);
719 	list_for_each_entry(tz, &thermal_tz_list, node) {
720 		ret = cb(tz, data);
721 		if (ret)
722 			break;
723 	}
724 	mutex_unlock(&thermal_list_lock);
725 
726 	return ret;
727 }
728 
729 struct thermal_zone_device *thermal_zone_get_by_id(int id)
730 {
731 	struct thermal_zone_device *tz, *match = NULL;
732 
733 	mutex_lock(&thermal_list_lock);
734 	list_for_each_entry(tz, &thermal_tz_list, node) {
735 		if (tz->id == id) {
736 			match = tz;
737 			break;
738 		}
739 	}
740 	mutex_unlock(&thermal_list_lock);
741 
742 	return match;
743 }
744 
745 /*
746  * Device management section: cooling devices, zones devices, and binding
747  *
748  * Set of functions provided by the thermal core for:
749  * - cooling devices lifecycle: registration, unregistration,
750  *				binding, and unbinding.
751  * - thermal zone devices lifecycle: registration, unregistration,
752  *				     binding, and unbinding.
753  */
754 
755 /**
756  * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone
757  * @tz:		pointer to struct thermal_zone_device
758  * @trip:	trip point the cooling devices is associated with in this zone.
759  * @cdev:	pointer to struct thermal_cooling_device
760  * @upper:	the Maximum cooling state for this trip point.
761  *		THERMAL_NO_LIMIT means no upper limit,
762  *		and the cooling device can be in max_state.
763  * @lower:	the Minimum cooling state can be used for this trip point.
764  *		THERMAL_NO_LIMIT means no lower limit,
765  *		and the cooling device can be in cooling state 0.
766  * @weight:	The weight of the cooling device to be bound to the
767  *		thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
768  *		default value
769  *
770  * This interface function bind a thermal cooling device to the certain trip
771  * point of a thermal zone device.
772  * This function is usually called in the thermal zone device .bind callback.
773  *
774  * Return: 0 on success, the proper error value otherwise.
775  */
776 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
777 				     const struct thermal_trip *trip,
778 				     struct thermal_cooling_device *cdev,
779 				     unsigned long upper, unsigned long lower,
780 				     unsigned int weight)
781 {
782 	struct thermal_instance *dev;
783 	struct thermal_instance *pos;
784 	struct thermal_zone_device *pos1;
785 	struct thermal_cooling_device *pos2;
786 	bool upper_no_limit;
787 	int result;
788 
789 	list_for_each_entry(pos1, &thermal_tz_list, node) {
790 		if (pos1 == tz)
791 			break;
792 	}
793 	list_for_each_entry(pos2, &thermal_cdev_list, node) {
794 		if (pos2 == cdev)
795 			break;
796 	}
797 
798 	if (tz != pos1 || cdev != pos2)
799 		return -EINVAL;
800 
801 	/* lower default 0, upper default max_state */
802 	lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
803 
804 	if (upper == THERMAL_NO_LIMIT) {
805 		upper = cdev->max_state;
806 		upper_no_limit = true;
807 	} else {
808 		upper_no_limit = false;
809 	}
810 
811 	if (lower > upper || upper > cdev->max_state)
812 		return -EINVAL;
813 
814 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
815 	if (!dev)
816 		return -ENOMEM;
817 	dev->tz = tz;
818 	dev->cdev = cdev;
819 	dev->trip = trip;
820 	dev->upper = upper;
821 	dev->upper_no_limit = upper_no_limit;
822 	dev->lower = lower;
823 	dev->target = THERMAL_NO_TARGET;
824 	dev->weight = weight;
825 
826 	result = ida_alloc(&tz->ida, GFP_KERNEL);
827 	if (result < 0)
828 		goto free_mem;
829 
830 	dev->id = result;
831 	sprintf(dev->name, "cdev%d", dev->id);
832 	result =
833 	    sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
834 	if (result)
835 		goto release_ida;
836 
837 	snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point",
838 		 dev->id);
839 	sysfs_attr_init(&dev->attr.attr);
840 	dev->attr.attr.name = dev->attr_name;
841 	dev->attr.attr.mode = 0444;
842 	dev->attr.show = trip_point_show;
843 	result = device_create_file(&tz->device, &dev->attr);
844 	if (result)
845 		goto remove_symbol_link;
846 
847 	snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name),
848 		 "cdev%d_weight", dev->id);
849 	sysfs_attr_init(&dev->weight_attr.attr);
850 	dev->weight_attr.attr.name = dev->weight_attr_name;
851 	dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
852 	dev->weight_attr.show = weight_show;
853 	dev->weight_attr.store = weight_store;
854 	result = device_create_file(&tz->device, &dev->weight_attr);
855 	if (result)
856 		goto remove_trip_file;
857 
858 	mutex_lock(&tz->lock);
859 	mutex_lock(&cdev->lock);
860 	list_for_each_entry(pos, &tz->thermal_instances, tz_node)
861 		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
862 			result = -EEXIST;
863 			break;
864 		}
865 	if (!result) {
866 		list_add_tail(&dev->tz_node, &tz->thermal_instances);
867 		list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
868 		atomic_set(&tz->need_update, 1);
869 
870 		thermal_governor_update_tz(tz, THERMAL_TZ_BIND_CDEV);
871 	}
872 	mutex_unlock(&cdev->lock);
873 	mutex_unlock(&tz->lock);
874 
875 	if (!result)
876 		return 0;
877 
878 	device_remove_file(&tz->device, &dev->weight_attr);
879 remove_trip_file:
880 	device_remove_file(&tz->device, &dev->attr);
881 remove_symbol_link:
882 	sysfs_remove_link(&tz->device.kobj, dev->name);
883 release_ida:
884 	ida_free(&tz->ida, dev->id);
885 free_mem:
886 	kfree(dev);
887 	return result;
888 }
889 EXPORT_SYMBOL_GPL(thermal_bind_cdev_to_trip);
890 
891 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
892 				     int trip_index,
893 				     struct thermal_cooling_device *cdev,
894 				     unsigned long upper, unsigned long lower,
895 				     unsigned int weight)
896 {
897 	if (trip_index < 0 || trip_index >= tz->num_trips)
898 		return -EINVAL;
899 
900 	return thermal_bind_cdev_to_trip(tz, &tz->trips[trip_index].trip, cdev,
901 					 upper, lower, weight);
902 }
903 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
904 
905 /**
906  * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone.
907  * @tz:		pointer to a struct thermal_zone_device.
908  * @trip:	trip point the cooling devices is associated with in this zone.
909  * @cdev:	pointer to a struct thermal_cooling_device.
910  *
911  * This interface function unbind a thermal cooling device from the certain
912  * trip point of a thermal zone device.
913  * This function is usually called in the thermal zone device .unbind callback.
914  *
915  * Return: 0 on success, the proper error value otherwise.
916  */
917 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
918 				  const struct thermal_trip *trip,
919 				  struct thermal_cooling_device *cdev)
920 {
921 	struct thermal_instance *pos, *next;
922 
923 	mutex_lock(&tz->lock);
924 	mutex_lock(&cdev->lock);
925 	list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
926 		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
927 			list_del(&pos->tz_node);
928 			list_del(&pos->cdev_node);
929 
930 			thermal_governor_update_tz(tz, THERMAL_TZ_UNBIND_CDEV);
931 
932 			mutex_unlock(&cdev->lock);
933 			mutex_unlock(&tz->lock);
934 			goto unbind;
935 		}
936 	}
937 	mutex_unlock(&cdev->lock);
938 	mutex_unlock(&tz->lock);
939 
940 	return -ENODEV;
941 
942 unbind:
943 	device_remove_file(&tz->device, &pos->weight_attr);
944 	device_remove_file(&tz->device, &pos->attr);
945 	sysfs_remove_link(&tz->device.kobj, pos->name);
946 	ida_free(&tz->ida, pos->id);
947 	kfree(pos);
948 	return 0;
949 }
950 EXPORT_SYMBOL_GPL(thermal_unbind_cdev_from_trip);
951 
952 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
953 				       int trip_index,
954 				       struct thermal_cooling_device *cdev)
955 {
956 	if (trip_index < 0 || trip_index >= tz->num_trips)
957 		return -EINVAL;
958 
959 	return thermal_unbind_cdev_from_trip(tz, &tz->trips[trip_index].trip, cdev);
960 }
961 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
962 
963 static void thermal_release(struct device *dev)
964 {
965 	struct thermal_zone_device *tz;
966 	struct thermal_cooling_device *cdev;
967 
968 	if (!strncmp(dev_name(dev), "thermal_zone",
969 		     sizeof("thermal_zone") - 1)) {
970 		tz = to_thermal_zone(dev);
971 		thermal_zone_destroy_device_groups(tz);
972 		mutex_destroy(&tz->lock);
973 		complete(&tz->removal);
974 	} else if (!strncmp(dev_name(dev), "cooling_device",
975 			    sizeof("cooling_device") - 1)) {
976 		cdev = to_cooling_device(dev);
977 		thermal_cooling_device_destroy_sysfs(cdev);
978 		kfree_const(cdev->type);
979 		ida_free(&thermal_cdev_ida, cdev->id);
980 		kfree(cdev);
981 	}
982 }
983 
984 static struct class *thermal_class;
985 
986 static inline
987 void print_bind_err_msg(struct thermal_zone_device *tz,
988 			struct thermal_cooling_device *cdev, int ret)
989 {
990 	dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
991 		tz->type, cdev->type, ret);
992 }
993 
994 static void bind_cdev(struct thermal_cooling_device *cdev)
995 {
996 	int ret;
997 	struct thermal_zone_device *pos = NULL;
998 
999 	list_for_each_entry(pos, &thermal_tz_list, node) {
1000 		if (pos->ops.bind) {
1001 			ret = pos->ops.bind(pos, cdev);
1002 			if (ret)
1003 				print_bind_err_msg(pos, cdev, ret);
1004 		}
1005 	}
1006 }
1007 
1008 /**
1009  * __thermal_cooling_device_register() - register a new thermal cooling device
1010  * @np:		a pointer to a device tree node.
1011  * @type:	the thermal cooling device type.
1012  * @devdata:	device private data.
1013  * @ops:		standard thermal cooling devices callbacks.
1014  *
1015  * This interface function adds a new thermal cooling device (fan/processor/...)
1016  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1017  * to all the thermal zone devices registered at the same time.
1018  * It also gives the opportunity to link the cooling device to a device tree
1019  * node, so that it can be bound to a thermal zone created out of device tree.
1020  *
1021  * Return: a pointer to the created struct thermal_cooling_device or an
1022  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1023  */
1024 static struct thermal_cooling_device *
1025 __thermal_cooling_device_register(struct device_node *np,
1026 				  const char *type, void *devdata,
1027 				  const struct thermal_cooling_device_ops *ops)
1028 {
1029 	struct thermal_cooling_device *cdev;
1030 	struct thermal_zone_device *pos = NULL;
1031 	unsigned long current_state;
1032 	int id, ret;
1033 
1034 	if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1035 	    !ops->set_cur_state)
1036 		return ERR_PTR(-EINVAL);
1037 
1038 	if (!thermal_class)
1039 		return ERR_PTR(-ENODEV);
1040 
1041 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1042 	if (!cdev)
1043 		return ERR_PTR(-ENOMEM);
1044 
1045 	ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
1046 	if (ret < 0)
1047 		goto out_kfree_cdev;
1048 	cdev->id = ret;
1049 	id = ret;
1050 
1051 	cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL);
1052 	if (!cdev->type) {
1053 		ret = -ENOMEM;
1054 		goto out_ida_remove;
1055 	}
1056 
1057 	mutex_init(&cdev->lock);
1058 	INIT_LIST_HEAD(&cdev->thermal_instances);
1059 	cdev->np = np;
1060 	cdev->ops = ops;
1061 	cdev->updated = false;
1062 	cdev->device.class = thermal_class;
1063 	cdev->devdata = devdata;
1064 
1065 	ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
1066 	if (ret)
1067 		goto out_cdev_type;
1068 
1069 	/*
1070 	 * The cooling device's current state is only needed for debug
1071 	 * initialization below, so a failure to get it does not cause
1072 	 * the entire cooling device initialization to fail.  However,
1073 	 * the debug will not work for the device if its initial state
1074 	 * cannot be determined and drivers are responsible for ensuring
1075 	 * that this will not happen.
1076 	 */
1077 	ret = cdev->ops->get_cur_state(cdev, &current_state);
1078 	if (ret)
1079 		current_state = ULONG_MAX;
1080 
1081 	thermal_cooling_device_setup_sysfs(cdev);
1082 
1083 	ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1084 	if (ret)
1085 		goto out_cooling_dev;
1086 
1087 	ret = device_register(&cdev->device);
1088 	if (ret) {
1089 		/* thermal_release() handles rest of the cleanup */
1090 		put_device(&cdev->device);
1091 		return ERR_PTR(ret);
1092 	}
1093 
1094 	if (current_state <= cdev->max_state)
1095 		thermal_debug_cdev_add(cdev, current_state);
1096 
1097 	/* Add 'this' new cdev to the global cdev list */
1098 	mutex_lock(&thermal_list_lock);
1099 
1100 	list_add(&cdev->node, &thermal_cdev_list);
1101 
1102 	/* Update binding information for 'this' new cdev */
1103 	bind_cdev(cdev);
1104 
1105 	list_for_each_entry(pos, &thermal_tz_list, node)
1106 		if (atomic_cmpxchg(&pos->need_update, 1, 0))
1107 			thermal_zone_device_update(pos,
1108 						   THERMAL_EVENT_UNSPECIFIED);
1109 
1110 	mutex_unlock(&thermal_list_lock);
1111 
1112 	return cdev;
1113 
1114 out_cooling_dev:
1115 	thermal_cooling_device_destroy_sysfs(cdev);
1116 out_cdev_type:
1117 	kfree_const(cdev->type);
1118 out_ida_remove:
1119 	ida_free(&thermal_cdev_ida, id);
1120 out_kfree_cdev:
1121 	kfree(cdev);
1122 	return ERR_PTR(ret);
1123 }
1124 
1125 /**
1126  * thermal_cooling_device_register() - register a new thermal cooling device
1127  * @type:	the thermal cooling device type.
1128  * @devdata:	device private data.
1129  * @ops:		standard thermal cooling devices callbacks.
1130  *
1131  * This interface function adds a new thermal cooling device (fan/processor/...)
1132  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1133  * to all the thermal zone devices registered at the same time.
1134  *
1135  * Return: a pointer to the created struct thermal_cooling_device or an
1136  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1137  */
1138 struct thermal_cooling_device *
1139 thermal_cooling_device_register(const char *type, void *devdata,
1140 				const struct thermal_cooling_device_ops *ops)
1141 {
1142 	return __thermal_cooling_device_register(NULL, type, devdata, ops);
1143 }
1144 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1145 
1146 /**
1147  * thermal_of_cooling_device_register() - register an OF thermal cooling device
1148  * @np:		a pointer to a device tree node.
1149  * @type:	the thermal cooling device type.
1150  * @devdata:	device private data.
1151  * @ops:		standard thermal cooling devices callbacks.
1152  *
1153  * This function will register a cooling device with device tree node reference.
1154  * This interface function adds a new thermal cooling device (fan/processor/...)
1155  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1156  * to all the thermal zone devices registered at the same time.
1157  *
1158  * Return: a pointer to the created struct thermal_cooling_device or an
1159  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1160  */
1161 struct thermal_cooling_device *
1162 thermal_of_cooling_device_register(struct device_node *np,
1163 				   const char *type, void *devdata,
1164 				   const struct thermal_cooling_device_ops *ops)
1165 {
1166 	return __thermal_cooling_device_register(np, type, devdata, ops);
1167 }
1168 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1169 
1170 static void thermal_cooling_device_release(struct device *dev, void *res)
1171 {
1172 	thermal_cooling_device_unregister(
1173 				*(struct thermal_cooling_device **)res);
1174 }
1175 
1176 /**
1177  * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1178  *					       device
1179  * @dev:	a valid struct device pointer of a sensor device.
1180  * @np:		a pointer to a device tree node.
1181  * @type:	the thermal cooling device type.
1182  * @devdata:	device private data.
1183  * @ops:	standard thermal cooling devices callbacks.
1184  *
1185  * This function will register a cooling device with device tree node reference.
1186  * This interface function adds a new thermal cooling device (fan/processor/...)
1187  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1188  * to all the thermal zone devices registered at the same time.
1189  *
1190  * Return: a pointer to the created struct thermal_cooling_device or an
1191  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1192  */
1193 struct thermal_cooling_device *
1194 devm_thermal_of_cooling_device_register(struct device *dev,
1195 				struct device_node *np,
1196 				const char *type, void *devdata,
1197 				const struct thermal_cooling_device_ops *ops)
1198 {
1199 	struct thermal_cooling_device **ptr, *tcd;
1200 
1201 	ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1202 			   GFP_KERNEL);
1203 	if (!ptr)
1204 		return ERR_PTR(-ENOMEM);
1205 
1206 	tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1207 	if (IS_ERR(tcd)) {
1208 		devres_free(ptr);
1209 		return tcd;
1210 	}
1211 
1212 	*ptr = tcd;
1213 	devres_add(dev, ptr);
1214 
1215 	return tcd;
1216 }
1217 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1218 
1219 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev)
1220 {
1221 	struct thermal_cooling_device *pos = NULL;
1222 
1223 	list_for_each_entry(pos, &thermal_cdev_list, node) {
1224 		if (pos == cdev)
1225 			return true;
1226 	}
1227 
1228 	return false;
1229 }
1230 
1231 /**
1232  * thermal_cooling_device_update - Update a cooling device object
1233  * @cdev: Target cooling device.
1234  *
1235  * Update @cdev to reflect a change of the underlying hardware or platform.
1236  *
1237  * Must be called when the maximum cooling state of @cdev becomes invalid and so
1238  * its .get_max_state() callback needs to be run to produce the new maximum
1239  * cooling state value.
1240  */
1241 void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
1242 {
1243 	struct thermal_instance *ti;
1244 	unsigned long state;
1245 
1246 	if (IS_ERR_OR_NULL(cdev))
1247 		return;
1248 
1249 	/*
1250 	 * Hold thermal_list_lock throughout the update to prevent the device
1251 	 * from going away while being updated.
1252 	 */
1253 	mutex_lock(&thermal_list_lock);
1254 
1255 	if (!thermal_cooling_device_present(cdev))
1256 		goto unlock_list;
1257 
1258 	/*
1259 	 * Update under the cdev lock to prevent the state from being set beyond
1260 	 * the new limit concurrently.
1261 	 */
1262 	mutex_lock(&cdev->lock);
1263 
1264 	if (cdev->ops->get_max_state(cdev, &cdev->max_state))
1265 		goto unlock;
1266 
1267 	thermal_cooling_device_stats_reinit(cdev);
1268 
1269 	list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) {
1270 		if (ti->upper == cdev->max_state)
1271 			continue;
1272 
1273 		if (ti->upper < cdev->max_state) {
1274 			if (ti->upper_no_limit)
1275 				ti->upper = cdev->max_state;
1276 
1277 			continue;
1278 		}
1279 
1280 		ti->upper = cdev->max_state;
1281 		if (ti->lower > ti->upper)
1282 			ti->lower = ti->upper;
1283 
1284 		if (ti->target == THERMAL_NO_TARGET)
1285 			continue;
1286 
1287 		if (ti->target > ti->upper)
1288 			ti->target = ti->upper;
1289 	}
1290 
1291 	if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state)
1292 		goto unlock;
1293 
1294 	thermal_cooling_device_stats_update(cdev, state);
1295 
1296 unlock:
1297 	mutex_unlock(&cdev->lock);
1298 
1299 unlock_list:
1300 	mutex_unlock(&thermal_list_lock);
1301 }
1302 EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
1303 
1304 /**
1305  * thermal_cooling_device_unregister - removes a thermal cooling device
1306  * @cdev:	the thermal cooling device to remove.
1307  *
1308  * thermal_cooling_device_unregister() must be called when a registered
1309  * thermal cooling device is no longer needed.
1310  */
1311 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1312 {
1313 	struct thermal_zone_device *tz;
1314 
1315 	if (!cdev)
1316 		return;
1317 
1318 	thermal_debug_cdev_remove(cdev);
1319 
1320 	mutex_lock(&thermal_list_lock);
1321 
1322 	if (!thermal_cooling_device_present(cdev)) {
1323 		mutex_unlock(&thermal_list_lock);
1324 		return;
1325 	}
1326 
1327 	list_del(&cdev->node);
1328 
1329 	/* Unbind all thermal zones associated with 'this' cdev */
1330 	list_for_each_entry(tz, &thermal_tz_list, node) {
1331 		if (tz->ops.unbind)
1332 			tz->ops.unbind(tz, cdev);
1333 	}
1334 
1335 	mutex_unlock(&thermal_list_lock);
1336 
1337 	device_unregister(&cdev->device);
1338 }
1339 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1340 
1341 static void bind_tz(struct thermal_zone_device *tz)
1342 {
1343 	int ret;
1344 	struct thermal_cooling_device *pos = NULL;
1345 
1346 	if (!tz->ops.bind)
1347 		return;
1348 
1349 	mutex_lock(&thermal_list_lock);
1350 
1351 	list_for_each_entry(pos, &thermal_cdev_list, node) {
1352 		ret = tz->ops.bind(tz, pos);
1353 		if (ret)
1354 			print_bind_err_msg(tz, pos, ret);
1355 	}
1356 
1357 	mutex_unlock(&thermal_list_lock);
1358 }
1359 
1360 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1361 {
1362 	*delay_jiffies = msecs_to_jiffies(delay_ms);
1363 	if (delay_ms > 1000)
1364 		*delay_jiffies = round_jiffies(*delay_jiffies);
1365 }
1366 
1367 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
1368 {
1369 	const struct thermal_trip_desc *td;
1370 	int ret = -EINVAL;
1371 
1372 	if (tz->ops.get_crit_temp)
1373 		return tz->ops.get_crit_temp(tz, temp);
1374 
1375 	mutex_lock(&tz->lock);
1376 
1377 	for_each_trip_desc(tz, td) {
1378 		const struct thermal_trip *trip = &td->trip;
1379 
1380 		if (trip->type == THERMAL_TRIP_CRITICAL) {
1381 			*temp = trip->temperature;
1382 			ret = 0;
1383 			break;
1384 		}
1385 	}
1386 
1387 	mutex_unlock(&tz->lock);
1388 
1389 	return ret;
1390 }
1391 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
1392 
1393 /**
1394  * thermal_zone_device_register_with_trips() - register a new thermal zone device
1395  * @type:	the thermal zone device type
1396  * @trips:	a pointer to an array of thermal trips
1397  * @num_trips:	the number of trip points the thermal zone support
1398  * @devdata:	private device data
1399  * @ops:	standard thermal zone device callbacks
1400  * @tzp:	thermal zone platform parameters
1401  * @passive_delay: number of milliseconds to wait between polls when
1402  *		   performing passive cooling
1403  * @polling_delay: number of milliseconds to wait between polls when checking
1404  *		   whether trip points have been crossed (0 for interrupt
1405  *		   driven systems)
1406  *
1407  * This interface function adds a new thermal zone device (sensor) to
1408  * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1409  * thermal cooling devices registered at the same time.
1410  * thermal_zone_device_unregister() must be called when the device is no
1411  * longer needed. The passive cooling depends on the .get_trend() return value.
1412  *
1413  * Return: a pointer to the created struct thermal_zone_device or an
1414  * in case of error, an ERR_PTR. Caller must check return value with
1415  * IS_ERR*() helpers.
1416  */
1417 struct thermal_zone_device *
1418 thermal_zone_device_register_with_trips(const char *type,
1419 					const struct thermal_trip *trips,
1420 					int num_trips, void *devdata,
1421 					const struct thermal_zone_device_ops *ops,
1422 					const struct thermal_zone_params *tzp,
1423 					unsigned int passive_delay,
1424 					unsigned int polling_delay)
1425 {
1426 	const struct thermal_trip *trip = trips;
1427 	struct thermal_zone_device *tz;
1428 	struct thermal_trip_desc *td;
1429 	int id;
1430 	int result;
1431 	struct thermal_governor *governor;
1432 
1433 	if (!type || strlen(type) == 0) {
1434 		pr_err("No thermal zone type defined\n");
1435 		return ERR_PTR(-EINVAL);
1436 	}
1437 
1438 	if (strlen(type) >= THERMAL_NAME_LENGTH) {
1439 		pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1440 		       type, THERMAL_NAME_LENGTH);
1441 		return ERR_PTR(-EINVAL);
1442 	}
1443 
1444 	if (num_trips < 0) {
1445 		pr_err("Incorrect number of thermal trips\n");
1446 		return ERR_PTR(-EINVAL);
1447 	}
1448 
1449 	if (!ops || !ops->get_temp) {
1450 		pr_err("Thermal zone device ops not defined\n");
1451 		return ERR_PTR(-EINVAL);
1452 	}
1453 
1454 	if (num_trips > 0 && !trips)
1455 		return ERR_PTR(-EINVAL);
1456 
1457 	if (polling_delay) {
1458 		if (passive_delay > polling_delay)
1459 			return ERR_PTR(-EINVAL);
1460 
1461 		if (!passive_delay)
1462 			passive_delay = polling_delay;
1463 	}
1464 
1465 	if (!thermal_class)
1466 		return ERR_PTR(-ENODEV);
1467 
1468 	tz = kzalloc(struct_size(tz, trips, num_trips), GFP_KERNEL);
1469 	if (!tz)
1470 		return ERR_PTR(-ENOMEM);
1471 
1472 	if (tzp) {
1473 		tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL);
1474 		if (!tz->tzp) {
1475 			result = -ENOMEM;
1476 			goto free_tz;
1477 		}
1478 	}
1479 
1480 	INIT_LIST_HEAD(&tz->thermal_instances);
1481 	INIT_LIST_HEAD(&tz->node);
1482 	ida_init(&tz->ida);
1483 	mutex_init(&tz->lock);
1484 	init_completion(&tz->removal);
1485 	init_completion(&tz->resume);
1486 	id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1487 	if (id < 0) {
1488 		result = id;
1489 		goto free_tzp;
1490 	}
1491 
1492 	tz->id = id;
1493 	strscpy(tz->type, type, sizeof(tz->type));
1494 
1495 	tz->ops = *ops;
1496 	if (!tz->ops.critical)
1497 		tz->ops.critical = thermal_zone_device_critical;
1498 
1499 	tz->device.class = thermal_class;
1500 	tz->devdata = devdata;
1501 	tz->num_trips = num_trips;
1502 	for_each_trip_desc(tz, td) {
1503 		td->trip = *trip++;
1504 		/*
1505 		 * Mark all thresholds as invalid to start with even though
1506 		 * this only matters for the trips that start as invalid and
1507 		 * become valid later.
1508 		 */
1509 		td->threshold = INT_MAX;
1510 	}
1511 
1512 	thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1513 	thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1514 	tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
1515 
1516 	/* sys I/F */
1517 	/* Add nodes that are always present via .groups */
1518 	result = thermal_zone_create_device_groups(tz);
1519 	if (result)
1520 		goto remove_id;
1521 
1522 	/* A new thermal zone needs to be updated anyway. */
1523 	atomic_set(&tz->need_update, 1);
1524 
1525 	result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1526 	if (result) {
1527 		thermal_zone_destroy_device_groups(tz);
1528 		goto remove_id;
1529 	}
1530 	result = device_register(&tz->device);
1531 	if (result)
1532 		goto release_device;
1533 
1534 	/* Update 'this' zone's governor information */
1535 	mutex_lock(&thermal_governor_lock);
1536 
1537 	if (tz->tzp)
1538 		governor = __find_governor(tz->tzp->governor_name);
1539 	else
1540 		governor = def_governor;
1541 
1542 	result = thermal_set_governor(tz, governor);
1543 	if (result) {
1544 		mutex_unlock(&thermal_governor_lock);
1545 		goto unregister;
1546 	}
1547 
1548 	mutex_unlock(&thermal_governor_lock);
1549 
1550 	if (!tz->tzp || !tz->tzp->no_hwmon) {
1551 		result = thermal_add_hwmon_sysfs(tz);
1552 		if (result)
1553 			goto unregister;
1554 	}
1555 
1556 	mutex_lock(&thermal_list_lock);
1557 	mutex_lock(&tz->lock);
1558 	list_add_tail(&tz->node, &thermal_tz_list);
1559 	mutex_unlock(&tz->lock);
1560 	mutex_unlock(&thermal_list_lock);
1561 
1562 	/* Bind cooling devices for this zone */
1563 	bind_tz(tz);
1564 
1565 	thermal_zone_device_init(tz);
1566 	/* Update the new thermal zone and mark it as already updated. */
1567 	if (atomic_cmpxchg(&tz->need_update, 1, 0))
1568 		thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1569 
1570 	thermal_notify_tz_create(tz);
1571 
1572 	thermal_debug_tz_add(tz);
1573 
1574 	return tz;
1575 
1576 unregister:
1577 	device_del(&tz->device);
1578 release_device:
1579 	put_device(&tz->device);
1580 remove_id:
1581 	ida_free(&thermal_tz_ida, id);
1582 free_tzp:
1583 	kfree(tz->tzp);
1584 free_tz:
1585 	kfree(tz);
1586 	return ERR_PTR(result);
1587 }
1588 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1589 
1590 struct thermal_zone_device *thermal_tripless_zone_device_register(
1591 					const char *type,
1592 					void *devdata,
1593 					const struct thermal_zone_device_ops *ops,
1594 					const struct thermal_zone_params *tzp)
1595 {
1596 	return thermal_zone_device_register_with_trips(type, NULL, 0, devdata,
1597 						       ops, tzp, 0, 0);
1598 }
1599 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register);
1600 
1601 void *thermal_zone_device_priv(struct thermal_zone_device *tzd)
1602 {
1603 	return tzd->devdata;
1604 }
1605 EXPORT_SYMBOL_GPL(thermal_zone_device_priv);
1606 
1607 const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
1608 {
1609 	return tzd->type;
1610 }
1611 EXPORT_SYMBOL_GPL(thermal_zone_device_type);
1612 
1613 int thermal_zone_device_id(struct thermal_zone_device *tzd)
1614 {
1615 	return tzd->id;
1616 }
1617 EXPORT_SYMBOL_GPL(thermal_zone_device_id);
1618 
1619 struct device *thermal_zone_device(struct thermal_zone_device *tzd)
1620 {
1621 	return &tzd->device;
1622 }
1623 EXPORT_SYMBOL_GPL(thermal_zone_device);
1624 
1625 /**
1626  * thermal_zone_device_unregister - removes the registered thermal zone device
1627  * @tz: the thermal zone device to remove
1628  */
1629 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1630 {
1631 	struct thermal_cooling_device *cdev;
1632 	struct thermal_zone_device *pos = NULL;
1633 
1634 	if (!tz)
1635 		return;
1636 
1637 	thermal_debug_tz_remove(tz);
1638 
1639 	mutex_lock(&thermal_list_lock);
1640 	list_for_each_entry(pos, &thermal_tz_list, node)
1641 		if (pos == tz)
1642 			break;
1643 	if (pos != tz) {
1644 		/* thermal zone device not found */
1645 		mutex_unlock(&thermal_list_lock);
1646 		return;
1647 	}
1648 
1649 	mutex_lock(&tz->lock);
1650 	list_del(&tz->node);
1651 	mutex_unlock(&tz->lock);
1652 
1653 	/* Unbind all cdevs associated with 'this' thermal zone */
1654 	list_for_each_entry(cdev, &thermal_cdev_list, node)
1655 		if (tz->ops.unbind)
1656 			tz->ops.unbind(tz, cdev);
1657 
1658 	mutex_unlock(&thermal_list_lock);
1659 
1660 	cancel_delayed_work_sync(&tz->poll_queue);
1661 
1662 	thermal_set_governor(tz, NULL);
1663 
1664 	thermal_remove_hwmon_sysfs(tz);
1665 	ida_free(&thermal_tz_ida, tz->id);
1666 	ida_destroy(&tz->ida);
1667 
1668 	device_del(&tz->device);
1669 
1670 	kfree(tz->tzp);
1671 
1672 	put_device(&tz->device);
1673 
1674 	thermal_notify_tz_delete(tz);
1675 
1676 	wait_for_completion(&tz->removal);
1677 	kfree(tz);
1678 }
1679 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1680 
1681 /**
1682  * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1683  * @name: thermal zone name to fetch the temperature
1684  *
1685  * When only one zone is found with the passed name, returns a reference to it.
1686  *
1687  * Return: On success returns a reference to an unique thermal zone with
1688  * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1689  * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1690  */
1691 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1692 {
1693 	struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1694 	unsigned int found = 0;
1695 
1696 	if (!name)
1697 		goto exit;
1698 
1699 	mutex_lock(&thermal_list_lock);
1700 	list_for_each_entry(pos, &thermal_tz_list, node)
1701 		if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1702 			found++;
1703 			ref = pos;
1704 		}
1705 	mutex_unlock(&thermal_list_lock);
1706 
1707 	/* nothing has been found, thus an error code for it */
1708 	if (found == 0)
1709 		ref = ERR_PTR(-ENODEV);
1710 	else if (found > 1)
1711 	/* Success only when an unique zone is found */
1712 		ref = ERR_PTR(-EEXIST);
1713 
1714 exit:
1715 	return ref;
1716 }
1717 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1718 
1719 static void thermal_zone_device_resume(struct work_struct *work)
1720 {
1721 	struct thermal_zone_device *tz;
1722 
1723 	tz = container_of(work, struct thermal_zone_device, poll_queue.work);
1724 
1725 	mutex_lock(&tz->lock);
1726 
1727 	tz->suspended = false;
1728 
1729 	thermal_debug_tz_resume(tz);
1730 	thermal_zone_device_init(tz);
1731 	thermal_governor_update_tz(tz, THERMAL_TZ_RESUME);
1732 	__thermal_zone_device_update(tz, THERMAL_TZ_RESUME);
1733 
1734 	complete(&tz->resume);
1735 	tz->resuming = false;
1736 
1737 	mutex_unlock(&tz->lock);
1738 }
1739 
1740 static int thermal_pm_notify(struct notifier_block *nb,
1741 			     unsigned long mode, void *_unused)
1742 {
1743 	struct thermal_zone_device *tz;
1744 
1745 	switch (mode) {
1746 	case PM_HIBERNATION_PREPARE:
1747 	case PM_RESTORE_PREPARE:
1748 	case PM_SUSPEND_PREPARE:
1749 		mutex_lock(&thermal_list_lock);
1750 
1751 		list_for_each_entry(tz, &thermal_tz_list, node) {
1752 			mutex_lock(&tz->lock);
1753 
1754 			if (tz->resuming) {
1755 				/*
1756 				 * thermal_zone_device_resume() queued up for
1757 				 * this zone has not acquired the lock yet, so
1758 				 * release it to let the function run and wait
1759 				 * util it has done the work.
1760 				 */
1761 				mutex_unlock(&tz->lock);
1762 
1763 				wait_for_completion(&tz->resume);
1764 
1765 				mutex_lock(&tz->lock);
1766 			}
1767 
1768 			tz->suspended = true;
1769 
1770 			mutex_unlock(&tz->lock);
1771 		}
1772 
1773 		mutex_unlock(&thermal_list_lock);
1774 		break;
1775 	case PM_POST_HIBERNATION:
1776 	case PM_POST_RESTORE:
1777 	case PM_POST_SUSPEND:
1778 		mutex_lock(&thermal_list_lock);
1779 
1780 		list_for_each_entry(tz, &thermal_tz_list, node) {
1781 			mutex_lock(&tz->lock);
1782 
1783 			cancel_delayed_work(&tz->poll_queue);
1784 
1785 			reinit_completion(&tz->resume);
1786 			tz->resuming = true;
1787 
1788 			/*
1789 			 * Replace the work function with the resume one, which
1790 			 * will restore the original work function and schedule
1791 			 * the polling work if needed.
1792 			 */
1793 			INIT_DELAYED_WORK(&tz->poll_queue,
1794 					  thermal_zone_device_resume);
1795 			/* Queue up the work without a delay. */
1796 			mod_delayed_work(system_freezable_power_efficient_wq,
1797 					 &tz->poll_queue, 0);
1798 
1799 			mutex_unlock(&tz->lock);
1800 		}
1801 
1802 		mutex_unlock(&thermal_list_lock);
1803 		break;
1804 	default:
1805 		break;
1806 	}
1807 	return 0;
1808 }
1809 
1810 static struct notifier_block thermal_pm_nb = {
1811 	.notifier_call = thermal_pm_notify,
1812 	/*
1813 	 * Run at the lowest priority to avoid interference between the thermal
1814 	 * zone resume work items spawned by thermal_pm_notify() and the other
1815 	 * PM notifiers.
1816 	 */
1817 	.priority = INT_MIN,
1818 };
1819 
1820 static int __init thermal_init(void)
1821 {
1822 	int result;
1823 
1824 	thermal_debug_init();
1825 
1826 	result = thermal_netlink_init();
1827 	if (result)
1828 		goto error;
1829 
1830 	result = thermal_register_governors();
1831 	if (result)
1832 		goto unregister_netlink;
1833 
1834 	thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);
1835 	if (!thermal_class) {
1836 		result = -ENOMEM;
1837 		goto unregister_governors;
1838 	}
1839 
1840 	thermal_class->name = "thermal";
1841 	thermal_class->dev_release = thermal_release;
1842 
1843 	result = class_register(thermal_class);
1844 	if (result) {
1845 		kfree(thermal_class);
1846 		thermal_class = NULL;
1847 		goto unregister_governors;
1848 	}
1849 
1850 	result = register_pm_notifier(&thermal_pm_nb);
1851 	if (result)
1852 		pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1853 			result);
1854 
1855 	return 0;
1856 
1857 unregister_governors:
1858 	thermal_unregister_governors();
1859 unregister_netlink:
1860 	thermal_netlink_exit();
1861 error:
1862 	mutex_destroy(&thermal_list_lock);
1863 	mutex_destroy(&thermal_governor_lock);
1864 	return result;
1865 }
1866 postcore_initcall(thermal_init);
1867