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