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