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