xref: /linux/drivers/thermal/thermal_core.c (revision 5c36cf27846a364f830e5ea86b411e05f7c8bd2b)
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 <trace/events/thermal.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 += scnprintf(buf + count, PAGE_SIZE - count, "%s ",
233 				   pos->name);
234 	}
235 	count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
236 
237 	mutex_unlock(&thermal_governor_lock);
238 
239 	return count;
240 }
241 
242 static void __init thermal_unregister_governors(void)
243 {
244 	struct thermal_governor **governor;
245 
246 	for_each_governor_table(governor)
247 		thermal_unregister_governor(*governor);
248 }
249 
250 static int __init thermal_register_governors(void)
251 {
252 	int ret = 0;
253 	struct thermal_governor **governor;
254 
255 	for_each_governor_table(governor) {
256 		ret = thermal_register_governor(*governor);
257 		if (ret) {
258 			pr_err("Failed to register governor: '%s'",
259 			       (*governor)->name);
260 			break;
261 		}
262 
263 		pr_info("Registered thermal governor '%s'",
264 			(*governor)->name);
265 	}
266 
267 	if (ret) {
268 		struct thermal_governor **gov;
269 
270 		for_each_governor_table(gov) {
271 			if (gov == governor)
272 				break;
273 			thermal_unregister_governor(*gov);
274 		}
275 	}
276 
277 	return ret;
278 }
279 
280 /*
281  * Zone update section: main control loop applied to each zone while monitoring
282  *
283  * in polling mode. The monitoring is done using a workqueue.
284  * Same update may be done on a zone by calling thermal_zone_device_update().
285  *
286  * An update means:
287  * - Non-critical trips will invoke the governor responsible for that zone;
288  * - Hot trips will produce a notification to userspace;
289  * - Critical trip point will cause a system shutdown.
290  */
291 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
292 					    unsigned long delay)
293 {
294 	if (delay)
295 		mod_delayed_work(system_freezable_power_efficient_wq,
296 				 &tz->poll_queue, delay);
297 	else
298 		cancel_delayed_work(&tz->poll_queue);
299 }
300 
301 static void monitor_thermal_zone(struct thermal_zone_device *tz)
302 {
303 	if (tz->mode != THERMAL_DEVICE_ENABLED)
304 		thermal_zone_device_set_polling(tz, 0);
305 	else if (tz->passive)
306 		thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
307 	else if (tz->polling_delay_jiffies)
308 		thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
309 }
310 
311 static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
312 {
313 	tz->governor ? tz->governor->throttle(tz, trip) :
314 		       def_governor->throttle(tz, trip);
315 }
316 
317 void thermal_zone_device_critical(struct thermal_zone_device *tz)
318 {
319 	/*
320 	 * poweroff_delay_ms must be a carefully profiled positive value.
321 	 * Its a must for forced_emergency_poweroff_work to be scheduled.
322 	 */
323 	int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
324 
325 	dev_emerg(&tz->device, "%s: critical temperature reached, "
326 		  "shutting down\n", tz->type);
327 
328 	hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
329 }
330 EXPORT_SYMBOL(thermal_zone_device_critical);
331 
332 static void handle_critical_trips(struct thermal_zone_device *tz,
333 				  int trip, int trip_temp, enum thermal_trip_type trip_type)
334 {
335 	/* If we have not crossed the trip_temp, we do not care. */
336 	if (trip_temp <= 0 || tz->temperature < trip_temp)
337 		return;
338 
339 	trace_thermal_zone_trip(tz, trip, trip_type);
340 
341 	if (trip_type == THERMAL_TRIP_HOT && tz->ops->hot)
342 		tz->ops->hot(tz);
343 	else if (trip_type == THERMAL_TRIP_CRITICAL)
344 		tz->ops->critical(tz);
345 }
346 
347 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip_id)
348 {
349 	struct thermal_trip trip;
350 
351 	/* Ignore disabled trip points */
352 	if (test_bit(trip_id, &tz->trips_disabled))
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 static void thermal_zone_device_check(struct work_struct *work)
501 {
502 	struct thermal_zone_device *tz = container_of(work, struct
503 						      thermal_zone_device,
504 						      poll_queue.work);
505 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
506 }
507 
508 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
509 			      void *data)
510 {
511 	struct thermal_governor *gov;
512 	int ret = 0;
513 
514 	mutex_lock(&thermal_governor_lock);
515 	list_for_each_entry(gov, &thermal_governor_list, governor_list) {
516 		ret = cb(gov, data);
517 		if (ret)
518 			break;
519 	}
520 	mutex_unlock(&thermal_governor_lock);
521 
522 	return ret;
523 }
524 
525 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
526 					      void *), void *data)
527 {
528 	struct thermal_cooling_device *cdev;
529 	int ret = 0;
530 
531 	mutex_lock(&thermal_list_lock);
532 	list_for_each_entry(cdev, &thermal_cdev_list, node) {
533 		ret = cb(cdev, data);
534 		if (ret)
535 			break;
536 	}
537 	mutex_unlock(&thermal_list_lock);
538 
539 	return ret;
540 }
541 
542 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
543 			  void *data)
544 {
545 	struct thermal_zone_device *tz;
546 	int ret = 0;
547 
548 	mutex_lock(&thermal_list_lock);
549 	list_for_each_entry(tz, &thermal_tz_list, node) {
550 		ret = cb(tz, data);
551 		if (ret)
552 			break;
553 	}
554 	mutex_unlock(&thermal_list_lock);
555 
556 	return ret;
557 }
558 
559 struct thermal_zone_device *thermal_zone_get_by_id(int id)
560 {
561 	struct thermal_zone_device *tz, *match = NULL;
562 
563 	mutex_lock(&thermal_list_lock);
564 	list_for_each_entry(tz, &thermal_tz_list, node) {
565 		if (tz->id == id) {
566 			match = tz;
567 			break;
568 		}
569 	}
570 	mutex_unlock(&thermal_list_lock);
571 
572 	return match;
573 }
574 
575 /*
576  * Device management section: cooling devices, zones devices, and binding
577  *
578  * Set of functions provided by the thermal core for:
579  * - cooling devices lifecycle: registration, unregistration,
580  *				binding, and unbinding.
581  * - thermal zone devices lifecycle: registration, unregistration,
582  *				     binding, and unbinding.
583  */
584 
585 /**
586  * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
587  * @tz:		pointer to struct thermal_zone_device
588  * @trip:	indicates which trip point the cooling devices is
589  *		associated with in this thermal zone.
590  * @cdev:	pointer to struct thermal_cooling_device
591  * @upper:	the Maximum cooling state for this trip point.
592  *		THERMAL_NO_LIMIT means no upper limit,
593  *		and the cooling device can be in max_state.
594  * @lower:	the Minimum cooling state can be used for this trip point.
595  *		THERMAL_NO_LIMIT means no lower limit,
596  *		and the cooling device can be in cooling state 0.
597  * @weight:	The weight of the cooling device to be bound to the
598  *		thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
599  *		default value
600  *
601  * This interface function bind a thermal cooling device to the certain trip
602  * point of a thermal zone device.
603  * This function is usually called in the thermal zone device .bind callback.
604  *
605  * Return: 0 on success, the proper error value otherwise.
606  */
607 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
608 				     int trip,
609 				     struct thermal_cooling_device *cdev,
610 				     unsigned long upper, unsigned long lower,
611 				     unsigned int weight)
612 {
613 	struct thermal_instance *dev;
614 	struct thermal_instance *pos;
615 	struct thermal_zone_device *pos1;
616 	struct thermal_cooling_device *pos2;
617 	int result;
618 
619 	if (trip >= tz->num_trips || trip < 0)
620 		return -EINVAL;
621 
622 	list_for_each_entry(pos1, &thermal_tz_list, node) {
623 		if (pos1 == tz)
624 			break;
625 	}
626 	list_for_each_entry(pos2, &thermal_cdev_list, node) {
627 		if (pos2 == cdev)
628 			break;
629 	}
630 
631 	if (tz != pos1 || cdev != pos2)
632 		return -EINVAL;
633 
634 	/* lower default 0, upper default max_state */
635 	lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
636 	upper = upper == THERMAL_NO_LIMIT ? cdev->max_state : upper;
637 
638 	if (lower > upper || upper > cdev->max_state)
639 		return -EINVAL;
640 
641 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
642 	if (!dev)
643 		return -ENOMEM;
644 	dev->tz = tz;
645 	dev->cdev = cdev;
646 	dev->trip = trip;
647 	dev->upper = upper;
648 	dev->lower = lower;
649 	dev->target = THERMAL_NO_TARGET;
650 	dev->weight = weight;
651 
652 	result = ida_alloc(&tz->ida, GFP_KERNEL);
653 	if (result < 0)
654 		goto free_mem;
655 
656 	dev->id = result;
657 	sprintf(dev->name, "cdev%d", dev->id);
658 	result =
659 	    sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
660 	if (result)
661 		goto release_ida;
662 
663 	sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
664 	sysfs_attr_init(&dev->attr.attr);
665 	dev->attr.attr.name = dev->attr_name;
666 	dev->attr.attr.mode = 0444;
667 	dev->attr.show = trip_point_show;
668 	result = device_create_file(&tz->device, &dev->attr);
669 	if (result)
670 		goto remove_symbol_link;
671 
672 	sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
673 	sysfs_attr_init(&dev->weight_attr.attr);
674 	dev->weight_attr.attr.name = dev->weight_attr_name;
675 	dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
676 	dev->weight_attr.show = weight_show;
677 	dev->weight_attr.store = weight_store;
678 	result = device_create_file(&tz->device, &dev->weight_attr);
679 	if (result)
680 		goto remove_trip_file;
681 
682 	mutex_lock(&tz->lock);
683 	mutex_lock(&cdev->lock);
684 	list_for_each_entry(pos, &tz->thermal_instances, tz_node)
685 		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
686 			result = -EEXIST;
687 			break;
688 		}
689 	if (!result) {
690 		list_add_tail(&dev->tz_node, &tz->thermal_instances);
691 		list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
692 		atomic_set(&tz->need_update, 1);
693 	}
694 	mutex_unlock(&cdev->lock);
695 	mutex_unlock(&tz->lock);
696 
697 	if (!result)
698 		return 0;
699 
700 	device_remove_file(&tz->device, &dev->weight_attr);
701 remove_trip_file:
702 	device_remove_file(&tz->device, &dev->attr);
703 remove_symbol_link:
704 	sysfs_remove_link(&tz->device.kobj, dev->name);
705 release_ida:
706 	ida_free(&tz->ida, dev->id);
707 free_mem:
708 	kfree(dev);
709 	return result;
710 }
711 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
712 
713 /**
714  * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
715  *					  thermal zone.
716  * @tz:		pointer to a struct thermal_zone_device.
717  * @trip:	indicates which trip point the cooling devices is
718  *		associated with in this thermal zone.
719  * @cdev:	pointer to a struct thermal_cooling_device.
720  *
721  * This interface function unbind a thermal cooling device from the certain
722  * trip point of a thermal zone device.
723  * This function is usually called in the thermal zone device .unbind callback.
724  *
725  * Return: 0 on success, the proper error value otherwise.
726  */
727 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
728 				       int trip,
729 				       struct thermal_cooling_device *cdev)
730 {
731 	struct thermal_instance *pos, *next;
732 
733 	mutex_lock(&tz->lock);
734 	mutex_lock(&cdev->lock);
735 	list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
736 		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
737 			list_del(&pos->tz_node);
738 			list_del(&pos->cdev_node);
739 			mutex_unlock(&cdev->lock);
740 			mutex_unlock(&tz->lock);
741 			goto unbind;
742 		}
743 	}
744 	mutex_unlock(&cdev->lock);
745 	mutex_unlock(&tz->lock);
746 
747 	return -ENODEV;
748 
749 unbind:
750 	device_remove_file(&tz->device, &pos->weight_attr);
751 	device_remove_file(&tz->device, &pos->attr);
752 	sysfs_remove_link(&tz->device.kobj, pos->name);
753 	ida_free(&tz->ida, pos->id);
754 	kfree(pos);
755 	return 0;
756 }
757 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
758 
759 static void thermal_release(struct device *dev)
760 {
761 	struct thermal_zone_device *tz;
762 	struct thermal_cooling_device *cdev;
763 
764 	if (!strncmp(dev_name(dev), "thermal_zone",
765 		     sizeof("thermal_zone") - 1)) {
766 		tz = to_thermal_zone(dev);
767 		thermal_zone_destroy_device_groups(tz);
768 		mutex_destroy(&tz->lock);
769 		kfree(tz);
770 	} else if (!strncmp(dev_name(dev), "cooling_device",
771 			    sizeof("cooling_device") - 1)) {
772 		cdev = to_cooling_device(dev);
773 		kfree(cdev);
774 	}
775 }
776 
777 static struct class thermal_class = {
778 	.name = "thermal",
779 	.dev_release = thermal_release,
780 };
781 
782 static inline
783 void print_bind_err_msg(struct thermal_zone_device *tz,
784 			struct thermal_cooling_device *cdev, int ret)
785 {
786 	dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
787 		tz->type, cdev->type, ret);
788 }
789 
790 static void __bind(struct thermal_zone_device *tz, int mask,
791 		   struct thermal_cooling_device *cdev,
792 		   unsigned long *limits,
793 		   unsigned int weight)
794 {
795 	int i, ret;
796 
797 	for (i = 0; i < tz->num_trips; i++) {
798 		if (mask & (1 << i)) {
799 			unsigned long upper, lower;
800 
801 			upper = THERMAL_NO_LIMIT;
802 			lower = THERMAL_NO_LIMIT;
803 			if (limits) {
804 				lower = limits[i * 2];
805 				upper = limits[i * 2 + 1];
806 			}
807 			ret = thermal_zone_bind_cooling_device(tz, i, cdev,
808 							       upper, lower,
809 							       weight);
810 			if (ret)
811 				print_bind_err_msg(tz, cdev, ret);
812 		}
813 	}
814 }
815 
816 static void bind_cdev(struct thermal_cooling_device *cdev)
817 {
818 	int i, ret;
819 	const struct thermal_zone_params *tzp;
820 	struct thermal_zone_device *pos = NULL;
821 
822 	mutex_lock(&thermal_list_lock);
823 
824 	list_for_each_entry(pos, &thermal_tz_list, node) {
825 		if (!pos->tzp && !pos->ops->bind)
826 			continue;
827 
828 		if (pos->ops->bind) {
829 			ret = pos->ops->bind(pos, cdev);
830 			if (ret)
831 				print_bind_err_msg(pos, cdev, ret);
832 			continue;
833 		}
834 
835 		tzp = pos->tzp;
836 		if (!tzp || !tzp->tbp)
837 			continue;
838 
839 		for (i = 0; i < tzp->num_tbps; i++) {
840 			if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
841 				continue;
842 			if (tzp->tbp[i].match(pos, cdev))
843 				continue;
844 			tzp->tbp[i].cdev = cdev;
845 			__bind(pos, tzp->tbp[i].trip_mask, cdev,
846 			       tzp->tbp[i].binding_limits,
847 			       tzp->tbp[i].weight);
848 		}
849 	}
850 
851 	mutex_unlock(&thermal_list_lock);
852 }
853 
854 /**
855  * __thermal_cooling_device_register() - register a new thermal cooling device
856  * @np:		a pointer to a device tree node.
857  * @type:	the thermal cooling device type.
858  * @devdata:	device private data.
859  * @ops:		standard thermal cooling devices callbacks.
860  *
861  * This interface function adds a new thermal cooling device (fan/processor/...)
862  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
863  * to all the thermal zone devices registered at the same time.
864  * It also gives the opportunity to link the cooling device to a device tree
865  * node, so that it can be bound to a thermal zone created out of device tree.
866  *
867  * Return: a pointer to the created struct thermal_cooling_device or an
868  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
869  */
870 static struct thermal_cooling_device *
871 __thermal_cooling_device_register(struct device_node *np,
872 				  const char *type, void *devdata,
873 				  const struct thermal_cooling_device_ops *ops)
874 {
875 	struct thermal_cooling_device *cdev;
876 	struct thermal_zone_device *pos = NULL;
877 	int id, ret;
878 
879 	if (!ops || !ops->get_max_state || !ops->get_cur_state ||
880 	    !ops->set_cur_state)
881 		return ERR_PTR(-EINVAL);
882 
883 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
884 	if (!cdev)
885 		return ERR_PTR(-ENOMEM);
886 
887 	ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
888 	if (ret < 0)
889 		goto out_kfree_cdev;
890 	cdev->id = ret;
891 	id = ret;
892 
893 	cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
894 	if (!cdev->type) {
895 		ret = -ENOMEM;
896 		goto out_ida_remove;
897 	}
898 
899 	mutex_init(&cdev->lock);
900 	INIT_LIST_HEAD(&cdev->thermal_instances);
901 	cdev->np = np;
902 	cdev->ops = ops;
903 	cdev->updated = false;
904 	cdev->device.class = &thermal_class;
905 	cdev->devdata = devdata;
906 
907 	ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
908 	if (ret) {
909 		kfree(cdev->type);
910 		goto out_ida_remove;
911 	}
912 
913 	thermal_cooling_device_setup_sysfs(cdev);
914 
915 	ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
916 	if (ret) {
917 		kfree(cdev->type);
918 		thermal_cooling_device_destroy_sysfs(cdev);
919 		goto out_ida_remove;
920 	}
921 
922 	ret = device_register(&cdev->device);
923 	if (ret)
924 		goto out_kfree_type;
925 
926 	/* Add 'this' new cdev to the global cdev list */
927 	mutex_lock(&thermal_list_lock);
928 	list_add(&cdev->node, &thermal_cdev_list);
929 	mutex_unlock(&thermal_list_lock);
930 
931 	/* Update binding information for 'this' new cdev */
932 	bind_cdev(cdev);
933 
934 	mutex_lock(&thermal_list_lock);
935 	list_for_each_entry(pos, &thermal_tz_list, node)
936 		if (atomic_cmpxchg(&pos->need_update, 1, 0))
937 			thermal_zone_device_update(pos,
938 						   THERMAL_EVENT_UNSPECIFIED);
939 	mutex_unlock(&thermal_list_lock);
940 
941 	return cdev;
942 
943 out_kfree_type:
944 	thermal_cooling_device_destroy_sysfs(cdev);
945 	kfree(cdev->type);
946 	put_device(&cdev->device);
947 
948 	/* thermal_release() takes care of the rest */
949 	cdev = NULL;
950 out_ida_remove:
951 	ida_free(&thermal_cdev_ida, id);
952 out_kfree_cdev:
953 	kfree(cdev);
954 	return ERR_PTR(ret);
955 }
956 
957 /**
958  * thermal_cooling_device_register() - register a new thermal cooling device
959  * @type:	the thermal cooling device type.
960  * @devdata:	device private data.
961  * @ops:		standard thermal cooling devices callbacks.
962  *
963  * This interface function adds a new thermal cooling device (fan/processor/...)
964  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
965  * to all the thermal zone devices registered at the same time.
966  *
967  * Return: a pointer to the created struct thermal_cooling_device or an
968  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
969  */
970 struct thermal_cooling_device *
971 thermal_cooling_device_register(const char *type, void *devdata,
972 				const struct thermal_cooling_device_ops *ops)
973 {
974 	return __thermal_cooling_device_register(NULL, type, devdata, ops);
975 }
976 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
977 
978 /**
979  * thermal_of_cooling_device_register() - register an OF thermal cooling device
980  * @np:		a pointer to a device tree node.
981  * @type:	the thermal cooling device type.
982  * @devdata:	device private data.
983  * @ops:		standard thermal cooling devices callbacks.
984  *
985  * This function will register a cooling device with device tree node reference.
986  * This interface function adds a new thermal cooling device (fan/processor/...)
987  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
988  * to all the thermal zone devices registered at the same time.
989  *
990  * Return: a pointer to the created struct thermal_cooling_device or an
991  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
992  */
993 struct thermal_cooling_device *
994 thermal_of_cooling_device_register(struct device_node *np,
995 				   const char *type, void *devdata,
996 				   const struct thermal_cooling_device_ops *ops)
997 {
998 	return __thermal_cooling_device_register(np, type, devdata, ops);
999 }
1000 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1001 
1002 static void thermal_cooling_device_release(struct device *dev, void *res)
1003 {
1004 	thermal_cooling_device_unregister(
1005 				*(struct thermal_cooling_device **)res);
1006 }
1007 
1008 /**
1009  * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1010  *					       device
1011  * @dev:	a valid struct device pointer of a sensor device.
1012  * @np:		a pointer to a device tree node.
1013  * @type:	the thermal cooling device type.
1014  * @devdata:	device private data.
1015  * @ops:	standard thermal cooling devices callbacks.
1016  *
1017  * This function will register a cooling device with device tree node reference.
1018  * This interface function adds a new thermal cooling device (fan/processor/...)
1019  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1020  * to all the thermal zone devices registered at the same time.
1021  *
1022  * Return: a pointer to the created struct thermal_cooling_device or an
1023  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1024  */
1025 struct thermal_cooling_device *
1026 devm_thermal_of_cooling_device_register(struct device *dev,
1027 				struct device_node *np,
1028 				char *type, void *devdata,
1029 				const struct thermal_cooling_device_ops *ops)
1030 {
1031 	struct thermal_cooling_device **ptr, *tcd;
1032 
1033 	ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1034 			   GFP_KERNEL);
1035 	if (!ptr)
1036 		return ERR_PTR(-ENOMEM);
1037 
1038 	tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1039 	if (IS_ERR(tcd)) {
1040 		devres_free(ptr);
1041 		return tcd;
1042 	}
1043 
1044 	*ptr = tcd;
1045 	devres_add(dev, ptr);
1046 
1047 	return tcd;
1048 }
1049 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1050 
1051 static void __unbind(struct thermal_zone_device *tz, int mask,
1052 		     struct thermal_cooling_device *cdev)
1053 {
1054 	int i;
1055 
1056 	for (i = 0; i < tz->num_trips; i++)
1057 		if (mask & (1 << i))
1058 			thermal_zone_unbind_cooling_device(tz, i, cdev);
1059 }
1060 
1061 /**
1062  * thermal_cooling_device_unregister - removes a thermal cooling device
1063  * @cdev:	the thermal cooling device to remove.
1064  *
1065  * thermal_cooling_device_unregister() must be called when a registered
1066  * thermal cooling device is no longer needed.
1067  */
1068 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1069 {
1070 	int i;
1071 	const struct thermal_zone_params *tzp;
1072 	struct thermal_zone_device *tz;
1073 	struct thermal_cooling_device *pos = NULL;
1074 
1075 	if (!cdev)
1076 		return;
1077 
1078 	mutex_lock(&thermal_list_lock);
1079 	list_for_each_entry(pos, &thermal_cdev_list, node)
1080 		if (pos == cdev)
1081 			break;
1082 	if (pos != cdev) {
1083 		/* thermal cooling device not found */
1084 		mutex_unlock(&thermal_list_lock);
1085 		return;
1086 	}
1087 	list_del(&cdev->node);
1088 
1089 	/* Unbind all thermal zones associated with 'this' cdev */
1090 	list_for_each_entry(tz, &thermal_tz_list, node) {
1091 		if (tz->ops->unbind) {
1092 			tz->ops->unbind(tz, cdev);
1093 			continue;
1094 		}
1095 
1096 		if (!tz->tzp || !tz->tzp->tbp)
1097 			continue;
1098 
1099 		tzp = tz->tzp;
1100 		for (i = 0; i < tzp->num_tbps; i++) {
1101 			if (tzp->tbp[i].cdev == cdev) {
1102 				__unbind(tz, tzp->tbp[i].trip_mask, cdev);
1103 				tzp->tbp[i].cdev = NULL;
1104 			}
1105 		}
1106 	}
1107 
1108 	mutex_unlock(&thermal_list_lock);
1109 
1110 	ida_free(&thermal_cdev_ida, cdev->id);
1111 	device_del(&cdev->device);
1112 	thermal_cooling_device_destroy_sysfs(cdev);
1113 	kfree(cdev->type);
1114 	put_device(&cdev->device);
1115 }
1116 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1117 
1118 static void bind_tz(struct thermal_zone_device *tz)
1119 {
1120 	int i, ret;
1121 	struct thermal_cooling_device *pos = NULL;
1122 	const struct thermal_zone_params *tzp = tz->tzp;
1123 
1124 	if (!tzp && !tz->ops->bind)
1125 		return;
1126 
1127 	mutex_lock(&thermal_list_lock);
1128 
1129 	/* If there is ops->bind, try to use ops->bind */
1130 	if (tz->ops->bind) {
1131 		list_for_each_entry(pos, &thermal_cdev_list, node) {
1132 			ret = tz->ops->bind(tz, pos);
1133 			if (ret)
1134 				print_bind_err_msg(tz, pos, ret);
1135 		}
1136 		goto exit;
1137 	}
1138 
1139 	if (!tzp || !tzp->tbp)
1140 		goto exit;
1141 
1142 	list_for_each_entry(pos, &thermal_cdev_list, node) {
1143 		for (i = 0; i < tzp->num_tbps; i++) {
1144 			if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1145 				continue;
1146 			if (tzp->tbp[i].match(tz, pos))
1147 				continue;
1148 			tzp->tbp[i].cdev = pos;
1149 			__bind(tz, tzp->tbp[i].trip_mask, pos,
1150 			       tzp->tbp[i].binding_limits,
1151 			       tzp->tbp[i].weight);
1152 		}
1153 	}
1154 exit:
1155 	mutex_unlock(&thermal_list_lock);
1156 }
1157 
1158 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1159 {
1160 	*delay_jiffies = msecs_to_jiffies(delay_ms);
1161 	if (delay_ms > 1000)
1162 		*delay_jiffies = round_jiffies(*delay_jiffies);
1163 }
1164 
1165 int thermal_zone_get_num_trips(struct thermal_zone_device *tz)
1166 {
1167 	return tz->num_trips;
1168 }
1169 EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips);
1170 
1171 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
1172 {
1173 	int i, ret = -EINVAL;
1174 
1175 	if (tz->ops->get_crit_temp)
1176 		return tz->ops->get_crit_temp(tz, temp);
1177 
1178 	if (!tz->trips)
1179 		return -EINVAL;
1180 
1181 	mutex_lock(&tz->lock);
1182 
1183 	for (i = 0; i < tz->num_trips; i++) {
1184 		if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) {
1185 			*temp = tz->trips[i].temperature;
1186 			ret = 0;
1187 			break;
1188 		}
1189 	}
1190 
1191 	mutex_unlock(&tz->lock);
1192 
1193 	return ret;
1194 }
1195 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
1196 
1197 int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
1198 			    struct thermal_trip *trip)
1199 {
1200 	int ret;
1201 
1202 	if (!tz || trip_id < 0 || trip_id >= tz->num_trips || !trip)
1203 		return -EINVAL;
1204 
1205 	if (tz->trips) {
1206 		*trip = tz->trips[trip_id];
1207 		return 0;
1208 	}
1209 
1210 	if (tz->ops->get_trip_hyst) {
1211 		ret = tz->ops->get_trip_hyst(tz, trip_id, &trip->hysteresis);
1212 		if (ret)
1213 			return ret;
1214 	} else {
1215 		trip->hysteresis = 0;
1216 	}
1217 
1218 	ret = tz->ops->get_trip_temp(tz, trip_id, &trip->temperature);
1219 	if (ret)
1220 		return ret;
1221 
1222 	return tz->ops->get_trip_type(tz, trip_id, &trip->type);
1223 }
1224 EXPORT_SYMBOL_GPL(__thermal_zone_get_trip);
1225 
1226 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
1227 			  struct thermal_trip *trip)
1228 {
1229 	int ret;
1230 
1231 	mutex_lock(&tz->lock);
1232 	ret = __thermal_zone_get_trip(tz, trip_id, trip);
1233 	mutex_unlock(&tz->lock);
1234 
1235 	return ret;
1236 }
1237 EXPORT_SYMBOL_GPL(thermal_zone_get_trip);
1238 
1239 int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id,
1240 			  const struct thermal_trip *trip)
1241 {
1242 	struct thermal_trip t;
1243 	int ret;
1244 
1245 	if (!tz->ops->set_trip_temp && !tz->ops->set_trip_hyst && !tz->trips)
1246 		return -EINVAL;
1247 
1248 	ret = __thermal_zone_get_trip(tz, trip_id, &t);
1249 	if (ret)
1250 		return ret;
1251 
1252 	if (t.type != trip->type)
1253 		return -EINVAL;
1254 
1255 	if (t.temperature != trip->temperature && tz->ops->set_trip_temp) {
1256 		ret = tz->ops->set_trip_temp(tz, trip_id, trip->temperature);
1257 		if (ret)
1258 			return ret;
1259 	}
1260 
1261 	if (t.hysteresis != trip->hysteresis && tz->ops->set_trip_hyst) {
1262 		ret = tz->ops->set_trip_hyst(tz, trip_id, trip->hysteresis);
1263 		if (ret)
1264 			return ret;
1265 	}
1266 
1267 	if (tz->trips && (t.temperature != trip->temperature || t.hysteresis != trip->hysteresis))
1268 		tz->trips[trip_id] = *trip;
1269 
1270 	thermal_notify_tz_trip_change(tz->id, trip_id, trip->type,
1271 				      trip->temperature, trip->hysteresis);
1272 
1273 	__thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
1274 
1275 	return 0;
1276 }
1277 
1278 /**
1279  * thermal_zone_device_register_with_trips() - register a new thermal zone device
1280  * @type:	the thermal zone device type
1281  * @trips:	a pointer to an array of thermal trips
1282  * @num_trips:	the number of trip points the thermal zone support
1283  * @mask:	a bit string indicating the writeablility of trip points
1284  * @devdata:	private device data
1285  * @ops:	standard thermal zone device callbacks
1286  * @tzp:	thermal zone platform parameters
1287  * @passive_delay: number of milliseconds to wait between polls when
1288  *		   performing passive cooling
1289  * @polling_delay: number of milliseconds to wait between polls when checking
1290  *		   whether trip points have been crossed (0 for interrupt
1291  *		   driven systems)
1292  *
1293  * This interface function adds a new thermal zone device (sensor) to
1294  * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1295  * thermal cooling devices registered at the same time.
1296  * thermal_zone_device_unregister() must be called when the device is no
1297  * longer needed. The passive cooling depends on the .get_trend() return value.
1298  *
1299  * Return: a pointer to the created struct thermal_zone_device or an
1300  * in case of error, an ERR_PTR. Caller must check return value with
1301  * IS_ERR*() helpers.
1302  */
1303 struct thermal_zone_device *
1304 thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask,
1305 					void *devdata, struct thermal_zone_device_ops *ops,
1306 					struct thermal_zone_params *tzp, int passive_delay,
1307 					int polling_delay)
1308 {
1309 	struct thermal_zone_device *tz;
1310 	int id;
1311 	int result;
1312 	int count;
1313 	struct thermal_governor *governor;
1314 
1315 	if (!type || strlen(type) == 0) {
1316 		pr_err("No thermal zone type defined\n");
1317 		return ERR_PTR(-EINVAL);
1318 	}
1319 
1320 	if (strlen(type) >= THERMAL_NAME_LENGTH) {
1321 		pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1322 		       type, THERMAL_NAME_LENGTH);
1323 		return ERR_PTR(-EINVAL);
1324 	}
1325 
1326 	/*
1327 	 * Max trip count can't exceed 31 as the "mask >> num_trips" condition.
1328 	 * For example, shifting by 32 will result in compiler warning:
1329 	 * warning: right shift count >= width of type [-Wshift-count- overflow]
1330 	 *
1331 	 * Also "mask >> num_trips" will always be true with 32 bit shift.
1332 	 * E.g. mask = 0x80000000 for trip id 31 to be RW. Then
1333 	 * mask >> 32 = 0x80000000
1334 	 * This will result in failure for the below condition.
1335 	 *
1336 	 * Check will be true when the bit 31 of the mask is set.
1337 	 * 32 bit shift will cause overflow of 4 byte integer.
1338 	 */
1339 	if (num_trips > (BITS_PER_TYPE(int) - 1) || num_trips < 0 || mask >> num_trips) {
1340 		pr_err("Incorrect number of thermal trips\n");
1341 		return ERR_PTR(-EINVAL);
1342 	}
1343 
1344 	if (!ops) {
1345 		pr_err("Thermal zone device ops not defined\n");
1346 		return ERR_PTR(-EINVAL);
1347 	}
1348 
1349 	if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp) && !trips)
1350 		return ERR_PTR(-EINVAL);
1351 
1352 	tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1353 	if (!tz)
1354 		return ERR_PTR(-ENOMEM);
1355 
1356 	INIT_LIST_HEAD(&tz->thermal_instances);
1357 	ida_init(&tz->ida);
1358 	mutex_init(&tz->lock);
1359 	id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1360 	if (id < 0) {
1361 		result = id;
1362 		goto free_tz;
1363 	}
1364 
1365 	tz->id = id;
1366 	strscpy(tz->type, type, sizeof(tz->type));
1367 
1368 	if (!ops->critical)
1369 		ops->critical = thermal_zone_device_critical;
1370 
1371 	tz->ops = ops;
1372 	tz->tzp = tzp;
1373 	tz->device.class = &thermal_class;
1374 	tz->devdata = devdata;
1375 	tz->trips = trips;
1376 	tz->num_trips = num_trips;
1377 
1378 	thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1379 	thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1380 
1381 	/* sys I/F */
1382 	/* Add nodes that are always present via .groups */
1383 	result = thermal_zone_create_device_groups(tz, mask);
1384 	if (result)
1385 		goto remove_id;
1386 
1387 	/* A new thermal zone needs to be updated anyway. */
1388 	atomic_set(&tz->need_update, 1);
1389 
1390 	result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1391 	if (result) {
1392 		thermal_zone_destroy_device_groups(tz);
1393 		goto remove_id;
1394 	}
1395 	result = device_register(&tz->device);
1396 	if (result)
1397 		goto release_device;
1398 
1399 	for (count = 0; count < num_trips; count++) {
1400 		struct thermal_trip trip;
1401 
1402 		result = thermal_zone_get_trip(tz, count, &trip);
1403 		if (result)
1404 			set_bit(count, &tz->trips_disabled);
1405 	}
1406 
1407 	/* Update 'this' zone's governor information */
1408 	mutex_lock(&thermal_governor_lock);
1409 
1410 	if (tz->tzp)
1411 		governor = __find_governor(tz->tzp->governor_name);
1412 	else
1413 		governor = def_governor;
1414 
1415 	result = thermal_set_governor(tz, governor);
1416 	if (result) {
1417 		mutex_unlock(&thermal_governor_lock);
1418 		goto unregister;
1419 	}
1420 
1421 	mutex_unlock(&thermal_governor_lock);
1422 
1423 	if (!tz->tzp || !tz->tzp->no_hwmon) {
1424 		result = thermal_add_hwmon_sysfs(tz);
1425 		if (result)
1426 			goto unregister;
1427 	}
1428 
1429 	mutex_lock(&thermal_list_lock);
1430 	list_add_tail(&tz->node, &thermal_tz_list);
1431 	mutex_unlock(&thermal_list_lock);
1432 
1433 	/* Bind cooling devices for this zone */
1434 	bind_tz(tz);
1435 
1436 	INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1437 
1438 	thermal_zone_device_init(tz);
1439 	/* Update the new thermal zone and mark it as already updated. */
1440 	if (atomic_cmpxchg(&tz->need_update, 1, 0))
1441 		thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1442 
1443 	thermal_notify_tz_create(tz->id, tz->type);
1444 
1445 	return tz;
1446 
1447 unregister:
1448 	device_del(&tz->device);
1449 release_device:
1450 	put_device(&tz->device);
1451 	tz = NULL;
1452 remove_id:
1453 	ida_free(&thermal_tz_ida, id);
1454 free_tz:
1455 	kfree(tz);
1456 	return ERR_PTR(result);
1457 }
1458 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1459 
1460 struct thermal_zone_device *thermal_zone_device_register(const char *type, int ntrips, int mask,
1461 							 void *devdata, struct thermal_zone_device_ops *ops,
1462 							 struct thermal_zone_params *tzp, int passive_delay,
1463 							 int polling_delay)
1464 {
1465 	return thermal_zone_device_register_with_trips(type, NULL, ntrips, mask,
1466 						       devdata, ops, tzp,
1467 						       passive_delay, polling_delay);
1468 }
1469 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1470 
1471 /**
1472  * thermal_zone_device_unregister - removes the registered thermal zone device
1473  * @tz: the thermal zone device to remove
1474  */
1475 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1476 {
1477 	int i, tz_id;
1478 	const struct thermal_zone_params *tzp;
1479 	struct thermal_cooling_device *cdev;
1480 	struct thermal_zone_device *pos = NULL;
1481 
1482 	if (!tz)
1483 		return;
1484 
1485 	tzp = tz->tzp;
1486 	tz_id = tz->id;
1487 
1488 	mutex_lock(&thermal_list_lock);
1489 	list_for_each_entry(pos, &thermal_tz_list, node)
1490 		if (pos == tz)
1491 			break;
1492 	if (pos != tz) {
1493 		/* thermal zone device not found */
1494 		mutex_unlock(&thermal_list_lock);
1495 		return;
1496 	}
1497 	list_del(&tz->node);
1498 
1499 	/* Unbind all cdevs associated with 'this' thermal zone */
1500 	list_for_each_entry(cdev, &thermal_cdev_list, node) {
1501 		if (tz->ops->unbind) {
1502 			tz->ops->unbind(tz, cdev);
1503 			continue;
1504 		}
1505 
1506 		if (!tzp || !tzp->tbp)
1507 			break;
1508 
1509 		for (i = 0; i < tzp->num_tbps; i++) {
1510 			if (tzp->tbp[i].cdev == cdev) {
1511 				__unbind(tz, tzp->tbp[i].trip_mask, cdev);
1512 				tzp->tbp[i].cdev = NULL;
1513 			}
1514 		}
1515 	}
1516 
1517 	mutex_unlock(&thermal_list_lock);
1518 
1519 	cancel_delayed_work_sync(&tz->poll_queue);
1520 
1521 	thermal_set_governor(tz, NULL);
1522 
1523 	thermal_remove_hwmon_sysfs(tz);
1524 	ida_free(&thermal_tz_ida, tz->id);
1525 	ida_destroy(&tz->ida);
1526 
1527 	mutex_lock(&tz->lock);
1528 	device_del(&tz->device);
1529 	mutex_unlock(&tz->lock);
1530 
1531 	put_device(&tz->device);
1532 
1533 	thermal_notify_tz_delete(tz_id);
1534 }
1535 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1536 
1537 /**
1538  * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1539  * @name: thermal zone name to fetch the temperature
1540  *
1541  * When only one zone is found with the passed name, returns a reference to it.
1542  *
1543  * Return: On success returns a reference to an unique thermal zone with
1544  * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1545  * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1546  */
1547 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1548 {
1549 	struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1550 	unsigned int found = 0;
1551 
1552 	if (!name)
1553 		goto exit;
1554 
1555 	mutex_lock(&thermal_list_lock);
1556 	list_for_each_entry(pos, &thermal_tz_list, node)
1557 		if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1558 			found++;
1559 			ref = pos;
1560 		}
1561 	mutex_unlock(&thermal_list_lock);
1562 
1563 	/* nothing has been found, thus an error code for it */
1564 	if (found == 0)
1565 		ref = ERR_PTR(-ENODEV);
1566 	else if (found > 1)
1567 	/* Success only when an unique zone is found */
1568 		ref = ERR_PTR(-EEXIST);
1569 
1570 exit:
1571 	return ref;
1572 }
1573 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1574 
1575 static int thermal_pm_notify(struct notifier_block *nb,
1576 			     unsigned long mode, void *_unused)
1577 {
1578 	struct thermal_zone_device *tz;
1579 
1580 	switch (mode) {
1581 	case PM_HIBERNATION_PREPARE:
1582 	case PM_RESTORE_PREPARE:
1583 	case PM_SUSPEND_PREPARE:
1584 		atomic_set(&in_suspend, 1);
1585 		break;
1586 	case PM_POST_HIBERNATION:
1587 	case PM_POST_RESTORE:
1588 	case PM_POST_SUSPEND:
1589 		atomic_set(&in_suspend, 0);
1590 		list_for_each_entry(tz, &thermal_tz_list, node) {
1591 			thermal_zone_device_init(tz);
1592 			thermal_zone_device_update(tz,
1593 						   THERMAL_EVENT_UNSPECIFIED);
1594 		}
1595 		break;
1596 	default:
1597 		break;
1598 	}
1599 	return 0;
1600 }
1601 
1602 static struct notifier_block thermal_pm_nb = {
1603 	.notifier_call = thermal_pm_notify,
1604 };
1605 
1606 static int __init thermal_init(void)
1607 {
1608 	int result;
1609 
1610 	result = thermal_netlink_init();
1611 	if (result)
1612 		goto error;
1613 
1614 	result = thermal_register_governors();
1615 	if (result)
1616 		goto error;
1617 
1618 	result = class_register(&thermal_class);
1619 	if (result)
1620 		goto unregister_governors;
1621 
1622 	result = register_pm_notifier(&thermal_pm_nb);
1623 	if (result)
1624 		pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1625 			result);
1626 
1627 	return 0;
1628 
1629 unregister_governors:
1630 	thermal_unregister_governors();
1631 error:
1632 	ida_destroy(&thermal_tz_ida);
1633 	ida_destroy(&thermal_cdev_ida);
1634 	mutex_destroy(&thermal_list_lock);
1635 	mutex_destroy(&thermal_governor_lock);
1636 	return result;
1637 }
1638 postcore_initcall(thermal_init);
1639