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