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