• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #undef CREATE_TRACE_POINTS
27 #include <trace/hooks/thermal.h>
28 
29 #include "thermal_core.h"
30 #include "thermal_hwmon.h"
31 
32 static DEFINE_IDA(thermal_tz_ida);
33 static DEFINE_IDA(thermal_cdev_ida);
34 
35 static LIST_HEAD(thermal_tz_list);
36 static LIST_HEAD(thermal_cdev_list);
37 static LIST_HEAD(thermal_governor_list);
38 
39 static DEFINE_MUTEX(thermal_list_lock);
40 static DEFINE_MUTEX(thermal_governor_lock);
41 
42 static atomic_t in_suspend;
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 
__find_governor(const char * name)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  */
bind_previous_governor(struct thermal_zone_device * tz,const char * failed_gov_name)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  */
thermal_set_governor(struct thermal_zone_device * tz,struct thermal_governor * new_gov)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 
thermal_register_governor(struct thermal_governor * governor)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 
thermal_unregister_governor(struct thermal_governor * governor)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 
thermal_zone_device_set_policy(struct thermal_zone_device * tz,char * policy)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 
thermal_build_list_of_policies(char * buf)223 int thermal_build_list_of_policies(char *buf)
224 {
225 	struct thermal_governor *pos;
226 	ssize_t count = 0;
227 
228 	mutex_lock(&thermal_governor_lock);
229 
230 	list_for_each_entry(pos, &thermal_governor_list, governor_list) {
231 		count += scnprintf(buf + count, PAGE_SIZE - count, "%s ",
232 				   pos->name);
233 	}
234 	count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
235 
236 	mutex_unlock(&thermal_governor_lock);
237 
238 	return count;
239 }
240 
thermal_unregister_governors(void)241 static void __init thermal_unregister_governors(void)
242 {
243 	struct thermal_governor **governor;
244 
245 	for_each_governor_table(governor)
246 		thermal_unregister_governor(*governor);
247 }
248 
thermal_register_governors(void)249 static int __init thermal_register_governors(void)
250 {
251 	int ret = 0;
252 	struct thermal_governor **governor;
253 
254 	for_each_governor_table(governor) {
255 		ret = thermal_register_governor(*governor);
256 		if (ret) {
257 			pr_err("Failed to register governor: '%s'",
258 			       (*governor)->name);
259 			break;
260 		}
261 
262 		pr_info("Registered thermal governor '%s'",
263 			(*governor)->name);
264 	}
265 
266 	if (ret) {
267 		struct thermal_governor **gov;
268 
269 		for_each_governor_table(gov) {
270 			if (gov == governor)
271 				break;
272 			thermal_unregister_governor(*gov);
273 		}
274 	}
275 
276 	return ret;
277 }
278 
279 /*
280  * Zone update section: main control loop applied to each zone while monitoring
281  *
282  * in polling mode. The monitoring is done using a workqueue.
283  * Same update may be done on a zone by calling thermal_zone_device_update().
284  *
285  * An update means:
286  * - Non-critical trips will invoke the governor responsible for that zone;
287  * - Hot trips will produce a notification to userspace;
288  * - Critical trip point will cause a system shutdown.
289  */
thermal_zone_device_set_polling(struct thermal_zone_device * tz,unsigned long delay)290 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
291 					    unsigned long delay)
292 {
293 	if (delay)
294 		mod_delayed_work(system_freezable_power_efficient_wq,
295 				 &tz->poll_queue, delay);
296 	else
297 		cancel_delayed_work(&tz->poll_queue);
298 }
299 
should_stop_polling(struct thermal_zone_device * tz)300 static inline bool should_stop_polling(struct thermal_zone_device *tz)
301 {
302 	return !thermal_zone_device_is_enabled(tz);
303 }
304 
monitor_thermal_zone(struct thermal_zone_device * tz)305 static void monitor_thermal_zone(struct thermal_zone_device *tz)
306 {
307 	bool stop;
308 
309 	stop = should_stop_polling(tz);
310 
311 	mutex_lock(&tz->lock);
312 
313 	if (!stop && tz->passive)
314 		thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
315 	else if (!stop && tz->polling_delay_jiffies)
316 		thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
317 	else
318 		thermal_zone_device_set_polling(tz, 0);
319 
320 	mutex_unlock(&tz->lock);
321 }
322 
handle_non_critical_trips(struct thermal_zone_device * tz,int trip)323 static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
324 {
325 	tz->governor ? tz->governor->throttle(tz, trip) :
326 		       def_governor->throttle(tz, trip);
327 }
328 
thermal_zone_device_critical(struct thermal_zone_device * tz)329 void thermal_zone_device_critical(struct thermal_zone_device *tz)
330 {
331 	/*
332 	 * poweroff_delay_ms must be a carefully profiled positive value.
333 	 * Its a must for forced_emergency_poweroff_work to be scheduled.
334 	 */
335 	int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
336 
337 	dev_emerg(&tz->device, "%s: critical temperature reached, "
338 		  "shutting down\n", tz->type);
339 
340 	hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
341 }
342 EXPORT_SYMBOL(thermal_zone_device_critical);
343 
handle_critical_trips(struct thermal_zone_device * tz,int trip,enum thermal_trip_type trip_type)344 static void handle_critical_trips(struct thermal_zone_device *tz,
345 				  int trip, enum thermal_trip_type trip_type)
346 {
347 	int trip_temp;
348 
349 	tz->ops->get_trip_temp(tz, trip, &trip_temp);
350 
351 	/* If we have not crossed the trip_temp, we do not care. */
352 	if (trip_temp <= 0 || tz->temperature < trip_temp)
353 		return;
354 
355 	trace_thermal_zone_trip(tz, trip, trip_type);
356 
357 	if (trip_type == THERMAL_TRIP_HOT && tz->ops->hot)
358 		tz->ops->hot(tz);
359 	else if (trip_type == THERMAL_TRIP_CRITICAL)
360 		tz->ops->critical(tz);
361 }
362 
handle_thermal_trip(struct thermal_zone_device * tz,int trip)363 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
364 {
365 	enum thermal_trip_type type;
366 	int trip_temp, hyst = 0;
367 
368 	/* Ignore disabled trip points */
369 	if (test_bit(trip, &tz->trips_disabled))
370 		return;
371 
372 	tz->ops->get_trip_temp(tz, trip, &trip_temp);
373 	tz->ops->get_trip_type(tz, trip, &type);
374 	if (tz->ops->get_trip_hyst)
375 		tz->ops->get_trip_hyst(tz, trip, &hyst);
376 
377 	if (tz->last_temperature != THERMAL_TEMP_INVALID) {
378 		if (tz->last_temperature < trip_temp &&
379 		    tz->temperature >= trip_temp)
380 			thermal_notify_tz_trip_up(tz->id, trip);
381 		if (tz->last_temperature >= trip_temp &&
382 		    tz->temperature < (trip_temp - hyst))
383 			thermal_notify_tz_trip_down(tz->id, trip);
384 	}
385 
386 	if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
387 		handle_critical_trips(tz, trip, type);
388 	else
389 		handle_non_critical_trips(tz, trip);
390 	/*
391 	 * Alright, we handled this trip successfully.
392 	 * So, start monitoring again.
393 	 */
394 	monitor_thermal_zone(tz);
395 }
396 
update_temperature(struct thermal_zone_device * tz)397 static void update_temperature(struct thermal_zone_device *tz)
398 {
399 	int temp, ret;
400 
401 	ret = thermal_zone_get_temp(tz, &temp);
402 	if (ret) {
403 		if (ret != -EAGAIN)
404 			dev_warn(&tz->device,
405 				 "failed to read out thermal zone (%d)\n",
406 				 ret);
407 		return;
408 	}
409 
410 	mutex_lock(&tz->lock);
411 	tz->last_temperature = tz->temperature;
412 	tz->temperature = temp;
413 	mutex_unlock(&tz->lock);
414 
415 	trace_thermal_temperature(tz);
416 
417 	thermal_genl_sampling_temp(tz->id, temp);
418 }
419 
thermal_zone_device_init(struct thermal_zone_device * tz)420 static void thermal_zone_device_init(struct thermal_zone_device *tz)
421 {
422 	struct thermal_instance *pos;
423 	tz->temperature = THERMAL_TEMP_INVALID;
424 	tz->prev_low_trip = -INT_MAX;
425 	tz->prev_high_trip = INT_MAX;
426 	list_for_each_entry(pos, &tz->thermal_instances, tz_node)
427 		pos->initialized = false;
428 }
429 
thermal_zone_device_set_mode(struct thermal_zone_device * tz,enum thermal_device_mode mode)430 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
431 					enum thermal_device_mode mode)
432 {
433 	int ret = 0;
434 
435 	mutex_lock(&tz->lock);
436 
437 	/* do nothing if mode isn't changing */
438 	if (mode == tz->mode) {
439 		mutex_unlock(&tz->lock);
440 
441 		return ret;
442 	}
443 
444 	if (tz->ops->change_mode)
445 		ret = tz->ops->change_mode(tz, mode);
446 
447 	if (!ret)
448 		tz->mode = mode;
449 
450 	mutex_unlock(&tz->lock);
451 
452 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
453 
454 	if (mode == THERMAL_DEVICE_ENABLED)
455 		thermal_notify_tz_enable(tz->id);
456 	else
457 		thermal_notify_tz_disable(tz->id);
458 
459 	return ret;
460 }
461 
thermal_zone_device_enable(struct thermal_zone_device * tz)462 int thermal_zone_device_enable(struct thermal_zone_device *tz)
463 {
464 	return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
465 }
466 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
467 
thermal_zone_device_disable(struct thermal_zone_device * tz)468 int thermal_zone_device_disable(struct thermal_zone_device *tz)
469 {
470 	return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
471 }
472 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
473 
thermal_zone_device_is_enabled(struct thermal_zone_device * tz)474 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
475 {
476 	enum thermal_device_mode mode;
477 
478 	mutex_lock(&tz->lock);
479 
480 	mode = tz->mode;
481 
482 	mutex_unlock(&tz->lock);
483 
484 	return mode == THERMAL_DEVICE_ENABLED;
485 }
486 
thermal_zone_device_update(struct thermal_zone_device * tz,enum thermal_notify_event event)487 void thermal_zone_device_update(struct thermal_zone_device *tz,
488 				enum thermal_notify_event event)
489 {
490 	int count;
491 
492 	if (should_stop_polling(tz))
493 		return;
494 
495 	if (atomic_read(&in_suspend))
496 		return;
497 
498 	if (WARN_ONCE(!tz->ops->get_temp, "'%s' must not be called without "
499 		      "'get_temp' ops set\n", __func__))
500 		return;
501 
502 	update_temperature(tz);
503 
504 	thermal_zone_set_trips(tz);
505 
506 	tz->notify_event = event;
507 
508 	for (count = 0; count < tz->trips; count++)
509 		handle_thermal_trip(tz, count);
510 
511 	trace_android_vh_get_thermal_zone_device(tz);
512 }
513 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
514 
thermal_zone_device_check(struct work_struct * work)515 static void thermal_zone_device_check(struct work_struct *work)
516 {
517 	struct thermal_zone_device *tz = container_of(work, struct
518 						      thermal_zone_device,
519 						      poll_queue.work);
520 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
521 }
522 
for_each_thermal_governor(int (* cb)(struct thermal_governor *,void *),void * data)523 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
524 			      void *data)
525 {
526 	struct thermal_governor *gov;
527 	int ret = 0;
528 
529 	mutex_lock(&thermal_governor_lock);
530 	list_for_each_entry(gov, &thermal_governor_list, governor_list) {
531 		ret = cb(gov, data);
532 		if (ret)
533 			break;
534 	}
535 	mutex_unlock(&thermal_governor_lock);
536 
537 	return ret;
538 }
539 
for_each_thermal_cooling_device(int (* cb)(struct thermal_cooling_device *,void *),void * data)540 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
541 					      void *), void *data)
542 {
543 	struct thermal_cooling_device *cdev;
544 	int ret = 0;
545 
546 	mutex_lock(&thermal_list_lock);
547 	list_for_each_entry(cdev, &thermal_cdev_list, node) {
548 		ret = cb(cdev, data);
549 		if (ret)
550 			break;
551 	}
552 	mutex_unlock(&thermal_list_lock);
553 
554 	return ret;
555 }
556 
for_each_thermal_zone(int (* cb)(struct thermal_zone_device *,void *),void * data)557 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
558 			  void *data)
559 {
560 	struct thermal_zone_device *tz;
561 	int ret = 0;
562 
563 	mutex_lock(&thermal_list_lock);
564 	list_for_each_entry(tz, &thermal_tz_list, node) {
565 		ret = cb(tz, data);
566 		if (ret)
567 			break;
568 	}
569 	mutex_unlock(&thermal_list_lock);
570 
571 	return ret;
572 }
573 
thermal_zone_get_by_id(int id)574 struct thermal_zone_device *thermal_zone_get_by_id(int id)
575 {
576 	struct thermal_zone_device *tz, *match = NULL;
577 
578 	mutex_lock(&thermal_list_lock);
579 	list_for_each_entry(tz, &thermal_tz_list, node) {
580 		if (tz->id == id) {
581 			match = tz;
582 			break;
583 		}
584 	}
585 	mutex_unlock(&thermal_list_lock);
586 
587 	return match;
588 }
589 
590 /*
591  * Device management section: cooling devices, zones devices, and binding
592  *
593  * Set of functions provided by the thermal core for:
594  * - cooling devices lifecycle: registration, unregistration,
595  *				binding, and unbinding.
596  * - thermal zone devices lifecycle: registration, unregistration,
597  *				     binding, and unbinding.
598  */
599 
600 /**
601  * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
602  * @tz:		pointer to struct thermal_zone_device
603  * @trip:	indicates which trip point the cooling devices is
604  *		associated with in this thermal zone.
605  * @cdev:	pointer to struct thermal_cooling_device
606  * @upper:	the Maximum cooling state for this trip point.
607  *		THERMAL_NO_LIMIT means no upper limit,
608  *		and the cooling device can be in max_state.
609  * @lower:	the Minimum cooling state can be used for this trip point.
610  *		THERMAL_NO_LIMIT means no lower limit,
611  *		and the cooling device can be in cooling state 0.
612  * @weight:	The weight of the cooling device to be bound to the
613  *		thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
614  *		default value
615  *
616  * This interface function bind a thermal cooling device to the certain trip
617  * point of a thermal zone device.
618  * This function is usually called in the thermal zone device .bind callback.
619  *
620  * Return: 0 on success, the proper error value otherwise.
621  */
thermal_zone_bind_cooling_device(struct thermal_zone_device * tz,int trip,struct thermal_cooling_device * cdev,unsigned long upper,unsigned long lower,unsigned int weight)622 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
623 				     int trip,
624 				     struct thermal_cooling_device *cdev,
625 				     unsigned long upper, unsigned long lower,
626 				     unsigned int weight)
627 {
628 	struct thermal_instance *dev;
629 	struct thermal_instance *pos;
630 	struct thermal_zone_device *pos1;
631 	struct thermal_cooling_device *pos2;
632 	unsigned long max_state;
633 	int result, ret;
634 
635 	if (trip >= tz->trips || trip < 0)
636 		return -EINVAL;
637 
638 	list_for_each_entry(pos1, &thermal_tz_list, node) {
639 		if (pos1 == tz)
640 			break;
641 	}
642 	list_for_each_entry(pos2, &thermal_cdev_list, node) {
643 		if (pos2 == cdev)
644 			break;
645 	}
646 
647 	if (tz != pos1 || cdev != pos2)
648 		return -EINVAL;
649 
650 	ret = cdev->ops->get_max_state(cdev, &max_state);
651 	if (ret)
652 		return ret;
653 
654 	/* lower default 0, upper default max_state */
655 	lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
656 	upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
657 
658 	if (lower > upper || upper > max_state)
659 		return -EINVAL;
660 
661 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
662 	if (!dev)
663 		return -ENOMEM;
664 	dev->tz = tz;
665 	dev->cdev = cdev;
666 	dev->trip = trip;
667 	dev->upper = upper;
668 	dev->lower = lower;
669 	dev->target = THERMAL_NO_TARGET;
670 	dev->weight = weight;
671 
672 	result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
673 	if (result < 0)
674 		goto free_mem;
675 
676 	dev->id = result;
677 	sprintf(dev->name, "cdev%d", dev->id);
678 	result =
679 	    sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
680 	if (result)
681 		goto release_ida;
682 
683 	snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point",
684 		 dev->id);
685 	sysfs_attr_init(&dev->attr.attr);
686 	dev->attr.attr.name = dev->attr_name;
687 	dev->attr.attr.mode = 0444;
688 	dev->attr.show = trip_point_show;
689 	result = device_create_file(&tz->device, &dev->attr);
690 	if (result)
691 		goto remove_symbol_link;
692 
693 	snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name),
694 		 "cdev%d_weight", dev->id);
695 	sysfs_attr_init(&dev->weight_attr.attr);
696 	dev->weight_attr.attr.name = dev->weight_attr_name;
697 	dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
698 	dev->weight_attr.show = weight_show;
699 	dev->weight_attr.store = weight_store;
700 	result = device_create_file(&tz->device, &dev->weight_attr);
701 	if (result)
702 		goto remove_trip_file;
703 
704 	mutex_lock(&tz->lock);
705 	mutex_lock(&cdev->lock);
706 	list_for_each_entry(pos, &tz->thermal_instances, tz_node)
707 		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
708 			result = -EEXIST;
709 			break;
710 		}
711 	if (!result) {
712 		list_add_tail(&dev->tz_node, &tz->thermal_instances);
713 		list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
714 		atomic_set(&tz->need_update, 1);
715 	}
716 	mutex_unlock(&cdev->lock);
717 	mutex_unlock(&tz->lock);
718 
719 	if (!result)
720 		return 0;
721 
722 	device_remove_file(&tz->device, &dev->weight_attr);
723 remove_trip_file:
724 	device_remove_file(&tz->device, &dev->attr);
725 remove_symbol_link:
726 	sysfs_remove_link(&tz->device.kobj, dev->name);
727 release_ida:
728 	ida_simple_remove(&tz->ida, dev->id);
729 free_mem:
730 	kfree(dev);
731 	return result;
732 }
733 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
734 
735 /**
736  * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
737  *					  thermal zone.
738  * @tz:		pointer to a struct thermal_zone_device.
739  * @trip:	indicates which trip point the cooling devices is
740  *		associated with in this thermal zone.
741  * @cdev:	pointer to a struct thermal_cooling_device.
742  *
743  * This interface function unbind a thermal cooling device from the certain
744  * trip point of a thermal zone device.
745  * This function is usually called in the thermal zone device .unbind callback.
746  *
747  * Return: 0 on success, the proper error value otherwise.
748  */
thermal_zone_unbind_cooling_device(struct thermal_zone_device * tz,int trip,struct thermal_cooling_device * cdev)749 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
750 				       int trip,
751 				       struct thermal_cooling_device *cdev)
752 {
753 	struct thermal_instance *pos, *next;
754 
755 	mutex_lock(&tz->lock);
756 	mutex_lock(&cdev->lock);
757 	list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
758 		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
759 			list_del(&pos->tz_node);
760 			list_del(&pos->cdev_node);
761 			mutex_unlock(&cdev->lock);
762 			mutex_unlock(&tz->lock);
763 			goto unbind;
764 		}
765 	}
766 	mutex_unlock(&cdev->lock);
767 	mutex_unlock(&tz->lock);
768 
769 	return -ENODEV;
770 
771 unbind:
772 	device_remove_file(&tz->device, &pos->weight_attr);
773 	device_remove_file(&tz->device, &pos->attr);
774 	sysfs_remove_link(&tz->device.kobj, pos->name);
775 	ida_simple_remove(&tz->ida, pos->id);
776 	kfree(pos);
777 	return 0;
778 }
779 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
780 
thermal_release(struct device * dev)781 static void thermal_release(struct device *dev)
782 {
783 	struct thermal_zone_device *tz;
784 	struct thermal_cooling_device *cdev;
785 
786 	if (!strncmp(dev_name(dev), "thermal_zone",
787 		     sizeof("thermal_zone") - 1)) {
788 		tz = to_thermal_zone(dev);
789 		thermal_zone_destroy_device_groups(tz);
790 		kfree(tz);
791 	} else if (!strncmp(dev_name(dev), "cooling_device",
792 			    sizeof("cooling_device") - 1)) {
793 		cdev = to_cooling_device(dev);
794 		kfree(cdev);
795 	}
796 }
797 
798 static struct class thermal_class = {
799 	.name = "thermal",
800 	.dev_release = thermal_release,
801 };
802 
803 static inline
print_bind_err_msg(struct thermal_zone_device * tz,struct thermal_cooling_device * cdev,int ret)804 void print_bind_err_msg(struct thermal_zone_device *tz,
805 			struct thermal_cooling_device *cdev, int ret)
806 {
807 	dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
808 		tz->type, cdev->type, ret);
809 }
810 
__bind(struct thermal_zone_device * tz,int mask,struct thermal_cooling_device * cdev,unsigned long * limits,unsigned int weight)811 static void __bind(struct thermal_zone_device *tz, int mask,
812 		   struct thermal_cooling_device *cdev,
813 		   unsigned long *limits,
814 		   unsigned int weight)
815 {
816 	int i, ret;
817 
818 	for (i = 0; i < tz->trips; i++) {
819 		if (mask & (1 << i)) {
820 			unsigned long upper, lower;
821 
822 			upper = THERMAL_NO_LIMIT;
823 			lower = THERMAL_NO_LIMIT;
824 			if (limits) {
825 				lower = limits[i * 2];
826 				upper = limits[i * 2 + 1];
827 			}
828 			ret = thermal_zone_bind_cooling_device(tz, i, cdev,
829 							       upper, lower,
830 							       weight);
831 			if (ret)
832 				print_bind_err_msg(tz, cdev, ret);
833 		}
834 	}
835 }
836 
bind_cdev(struct thermal_cooling_device * cdev)837 static void bind_cdev(struct thermal_cooling_device *cdev)
838 {
839 	int i, ret;
840 	const struct thermal_zone_params *tzp;
841 	struct thermal_zone_device *pos = NULL;
842 
843 	mutex_lock(&thermal_list_lock);
844 
845 	list_for_each_entry(pos, &thermal_tz_list, node) {
846 		if (!pos->tzp && !pos->ops->bind)
847 			continue;
848 
849 		if (pos->ops->bind) {
850 			ret = pos->ops->bind(pos, cdev);
851 			if (ret)
852 				print_bind_err_msg(pos, cdev, ret);
853 			continue;
854 		}
855 
856 		tzp = pos->tzp;
857 		if (!tzp || !tzp->tbp)
858 			continue;
859 
860 		for (i = 0; i < tzp->num_tbps; i++) {
861 			if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
862 				continue;
863 			if (tzp->tbp[i].match(pos, cdev))
864 				continue;
865 			tzp->tbp[i].cdev = cdev;
866 			__bind(pos, tzp->tbp[i].trip_mask, cdev,
867 			       tzp->tbp[i].binding_limits,
868 			       tzp->tbp[i].weight);
869 		}
870 	}
871 
872 	mutex_unlock(&thermal_list_lock);
873 }
874 
875 /**
876  * __thermal_cooling_device_register() - register a new thermal cooling device
877  * @np:		a pointer to a device tree node.
878  * @type:	the thermal cooling device type.
879  * @devdata:	device private data.
880  * @ops:		standard thermal cooling devices callbacks.
881  *
882  * This interface function adds a new thermal cooling device (fan/processor/...)
883  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
884  * to all the thermal zone devices registered at the same time.
885  * It also gives the opportunity to link the cooling device to a device tree
886  * node, so that it can be bound to a thermal zone created out of device tree.
887  *
888  * Return: a pointer to the created struct thermal_cooling_device or an
889  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
890  */
891 static struct thermal_cooling_device *
__thermal_cooling_device_register(struct device_node * np,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)892 __thermal_cooling_device_register(struct device_node *np,
893 				  const char *type, void *devdata,
894 				  const struct thermal_cooling_device_ops *ops)
895 {
896 	struct thermal_cooling_device *cdev;
897 	struct thermal_zone_device *pos = NULL;
898 	int id, ret;
899 
900 	if (!ops || !ops->get_max_state || !ops->get_cur_state ||
901 	    !ops->set_cur_state)
902 		return ERR_PTR(-EINVAL);
903 
904 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
905 	if (!cdev)
906 		return ERR_PTR(-ENOMEM);
907 
908 	ret = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
909 	if (ret < 0)
910 		goto out_kfree_cdev;
911 	cdev->id = ret;
912 	id = ret;
913 
914 	cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
915 	if (!cdev->type) {
916 		ret = -ENOMEM;
917 		goto out_ida_remove;
918 	}
919 
920 	mutex_init(&cdev->lock);
921 	INIT_LIST_HEAD(&cdev->thermal_instances);
922 	cdev->np = np;
923 	cdev->ops = ops;
924 	cdev->updated = false;
925 	cdev->device.class = &thermal_class;
926 	cdev->devdata = devdata;
927 	thermal_cooling_device_setup_sysfs(cdev);
928 	ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
929 	if (ret) {
930 		thermal_cooling_device_destroy_sysfs(cdev);
931 		goto out_kfree_type;
932 	}
933 	ret = device_register(&cdev->device);
934 	if (ret)
935 		goto out_kfree_type;
936 
937 	/* Add 'this' new cdev to the global cdev list */
938 	mutex_lock(&thermal_list_lock);
939 	list_add(&cdev->node, &thermal_cdev_list);
940 	mutex_unlock(&thermal_list_lock);
941 
942 	/* Update binding information for 'this' new cdev */
943 	bind_cdev(cdev);
944 
945 	mutex_lock(&thermal_list_lock);
946 	list_for_each_entry(pos, &thermal_tz_list, node)
947 		if (atomic_cmpxchg(&pos->need_update, 1, 0))
948 			thermal_zone_device_update(pos,
949 						   THERMAL_EVENT_UNSPECIFIED);
950 	mutex_unlock(&thermal_list_lock);
951 
952 	return cdev;
953 
954 out_kfree_type:
955 	thermal_cooling_device_destroy_sysfs(cdev);
956 	kfree(cdev->type);
957 	put_device(&cdev->device);
958 	cdev = NULL;
959 out_ida_remove:
960 	ida_simple_remove(&thermal_cdev_ida, id);
961 out_kfree_cdev:
962 	kfree(cdev);
963 	return ERR_PTR(ret);
964 }
965 
966 /**
967  * thermal_cooling_device_register() - register a new thermal cooling device
968  * @type:	the thermal cooling device type.
969  * @devdata:	device private data.
970  * @ops:		standard thermal cooling devices callbacks.
971  *
972  * This interface function adds a new thermal cooling device (fan/processor/...)
973  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
974  * to all the thermal zone devices registered at the same time.
975  *
976  * Return: a pointer to the created struct thermal_cooling_device or an
977  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
978  */
979 struct thermal_cooling_device *
thermal_cooling_device_register(const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)980 thermal_cooling_device_register(const char *type, void *devdata,
981 				const struct thermal_cooling_device_ops *ops)
982 {
983 	return __thermal_cooling_device_register(NULL, type, devdata, ops);
984 }
985 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
986 
987 /**
988  * thermal_of_cooling_device_register() - register an OF thermal cooling device
989  * @np:		a pointer to a device tree node.
990  * @type:	the thermal cooling device type.
991  * @devdata:	device private data.
992  * @ops:		standard thermal cooling devices callbacks.
993  *
994  * This function will register a cooling device with device tree node reference.
995  * This interface function adds a new thermal cooling device (fan/processor/...)
996  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
997  * to all the thermal zone devices registered at the same time.
998  *
999  * Return: a pointer to the created struct thermal_cooling_device or an
1000  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1001  */
1002 struct thermal_cooling_device *
thermal_of_cooling_device_register(struct device_node * np,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)1003 thermal_of_cooling_device_register(struct device_node *np,
1004 				   const char *type, void *devdata,
1005 				   const struct thermal_cooling_device_ops *ops)
1006 {
1007 	return __thermal_cooling_device_register(np, type, devdata, ops);
1008 }
1009 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1010 
thermal_cooling_device_release(struct device * dev,void * res)1011 static void thermal_cooling_device_release(struct device *dev, void *res)
1012 {
1013 	thermal_cooling_device_unregister(
1014 				*(struct thermal_cooling_device **)res);
1015 }
1016 
1017 /**
1018  * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1019  *					       device
1020  * @dev:	a valid struct device pointer of a sensor device.
1021  * @np:		a pointer to a device tree node.
1022  * @type:	the thermal cooling device type.
1023  * @devdata:	device private data.
1024  * @ops:	standard thermal cooling devices callbacks.
1025  *
1026  * This function will register a cooling device with device tree node reference.
1027  * This interface function adds a new thermal cooling device (fan/processor/...)
1028  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1029  * to all the thermal zone devices registered at the same time.
1030  *
1031  * Return: a pointer to the created struct thermal_cooling_device or an
1032  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1033  */
1034 struct thermal_cooling_device *
devm_thermal_of_cooling_device_register(struct device * dev,struct device_node * np,char * type,void * devdata,const struct thermal_cooling_device_ops * ops)1035 devm_thermal_of_cooling_device_register(struct device *dev,
1036 				struct device_node *np,
1037 				char *type, void *devdata,
1038 				const struct thermal_cooling_device_ops *ops)
1039 {
1040 	struct thermal_cooling_device **ptr, *tcd;
1041 
1042 	ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1043 			   GFP_KERNEL);
1044 	if (!ptr)
1045 		return ERR_PTR(-ENOMEM);
1046 
1047 	tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1048 	if (IS_ERR(tcd)) {
1049 		devres_free(ptr);
1050 		return tcd;
1051 	}
1052 
1053 	*ptr = tcd;
1054 	devres_add(dev, ptr);
1055 
1056 	return tcd;
1057 }
1058 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1059 
__unbind(struct thermal_zone_device * tz,int mask,struct thermal_cooling_device * cdev)1060 static void __unbind(struct thermal_zone_device *tz, int mask,
1061 		     struct thermal_cooling_device *cdev)
1062 {
1063 	int i;
1064 
1065 	for (i = 0; i < tz->trips; i++)
1066 		if (mask & (1 << i))
1067 			thermal_zone_unbind_cooling_device(tz, i, cdev);
1068 }
1069 
1070 /**
1071  * thermal_cooling_device_unregister - removes a thermal cooling device
1072  * @cdev:	the thermal cooling device to remove.
1073  *
1074  * thermal_cooling_device_unregister() must be called when a registered
1075  * thermal cooling device is no longer needed.
1076  */
thermal_cooling_device_unregister(struct thermal_cooling_device * cdev)1077 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1078 {
1079 	int i;
1080 	const struct thermal_zone_params *tzp;
1081 	struct thermal_zone_device *tz;
1082 	struct thermal_cooling_device *pos = NULL;
1083 
1084 	if (!cdev)
1085 		return;
1086 
1087 	mutex_lock(&thermal_list_lock);
1088 	list_for_each_entry(pos, &thermal_cdev_list, node)
1089 		if (pos == cdev)
1090 			break;
1091 	if (pos != cdev) {
1092 		/* thermal cooling device not found */
1093 		mutex_unlock(&thermal_list_lock);
1094 		return;
1095 	}
1096 	list_del(&cdev->node);
1097 
1098 	/* Unbind all thermal zones associated with 'this' cdev */
1099 	list_for_each_entry(tz, &thermal_tz_list, node) {
1100 		if (tz->ops->unbind) {
1101 			tz->ops->unbind(tz, cdev);
1102 			continue;
1103 		}
1104 
1105 		if (!tz->tzp || !tz->tzp->tbp)
1106 			continue;
1107 
1108 		tzp = tz->tzp;
1109 		for (i = 0; i < tzp->num_tbps; i++) {
1110 			if (tzp->tbp[i].cdev == cdev) {
1111 				__unbind(tz, tzp->tbp[i].trip_mask, cdev);
1112 				tzp->tbp[i].cdev = NULL;
1113 			}
1114 		}
1115 	}
1116 
1117 	mutex_unlock(&thermal_list_lock);
1118 
1119 	ida_simple_remove(&thermal_cdev_ida, cdev->id);
1120 	device_del(&cdev->device);
1121 	thermal_cooling_device_destroy_sysfs(cdev);
1122 	kfree(cdev->type);
1123 	put_device(&cdev->device);
1124 }
1125 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1126 
bind_tz(struct thermal_zone_device * tz)1127 static void bind_tz(struct thermal_zone_device *tz)
1128 {
1129 	int i, ret;
1130 	struct thermal_cooling_device *pos = NULL;
1131 	const struct thermal_zone_params *tzp = tz->tzp;
1132 
1133 	if (!tzp && !tz->ops->bind)
1134 		return;
1135 
1136 	mutex_lock(&thermal_list_lock);
1137 
1138 	/* If there is ops->bind, try to use ops->bind */
1139 	if (tz->ops->bind) {
1140 		list_for_each_entry(pos, &thermal_cdev_list, node) {
1141 			ret = tz->ops->bind(tz, pos);
1142 			if (ret)
1143 				print_bind_err_msg(tz, pos, ret);
1144 		}
1145 		goto exit;
1146 	}
1147 
1148 	if (!tzp || !tzp->tbp)
1149 		goto exit;
1150 
1151 	list_for_each_entry(pos, &thermal_cdev_list, node) {
1152 		for (i = 0; i < tzp->num_tbps; i++) {
1153 			if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1154 				continue;
1155 			if (tzp->tbp[i].match(tz, pos))
1156 				continue;
1157 			tzp->tbp[i].cdev = pos;
1158 			__bind(tz, tzp->tbp[i].trip_mask, pos,
1159 			       tzp->tbp[i].binding_limits,
1160 			       tzp->tbp[i].weight);
1161 		}
1162 	}
1163 exit:
1164 	mutex_unlock(&thermal_list_lock);
1165 }
1166 
1167 /**
1168  * thermal_zone_device_register() - register a new thermal zone device
1169  * @type:	the thermal zone device type
1170  * @trips:	the number of trip points the thermal zone support
1171  * @mask:	a bit string indicating the writeablility of trip points
1172  * @devdata:	private device data
1173  * @ops:	standard thermal zone device callbacks
1174  * @tzp:	thermal zone platform parameters
1175  * @passive_delay: number of milliseconds to wait between polls when
1176  *		   performing passive cooling
1177  * @polling_delay: number of milliseconds to wait between polls when checking
1178  *		   whether trip points have been crossed (0 for interrupt
1179  *		   driven systems)
1180  *
1181  * This interface function adds a new thermal zone device (sensor) to
1182  * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1183  * thermal cooling devices registered at the same time.
1184  * thermal_zone_device_unregister() must be called when the device is no
1185  * longer needed. The passive cooling depends on the .get_trend() return value.
1186  *
1187  * Return: a pointer to the created struct thermal_zone_device or an
1188  * in case of error, an ERR_PTR. Caller must check return value with
1189  * IS_ERR*() helpers.
1190  */
1191 struct thermal_zone_device *
thermal_zone_device_register(const char * type,int trips,int mask,void * devdata,struct thermal_zone_device_ops * ops,struct thermal_zone_params * tzp,int passive_delay,int polling_delay)1192 thermal_zone_device_register(const char *type, int trips, int mask,
1193 			     void *devdata, struct thermal_zone_device_ops *ops,
1194 			     struct thermal_zone_params *tzp, int passive_delay,
1195 			     int polling_delay)
1196 {
1197 	struct thermal_zone_device *tz;
1198 	enum thermal_trip_type trip_type;
1199 	int trip_temp;
1200 	int id;
1201 	int result;
1202 	int count;
1203 	struct thermal_governor *governor;
1204 
1205 	if (!type || strlen(type) == 0) {
1206 		pr_err("No thermal zone type defined\n");
1207 		return ERR_PTR(-EINVAL);
1208 	}
1209 
1210 	if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1211 		pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1212 		       type, THERMAL_NAME_LENGTH);
1213 		return ERR_PTR(-EINVAL);
1214 	}
1215 
1216 	if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) {
1217 		pr_err("Incorrect number of thermal trips\n");
1218 		return ERR_PTR(-EINVAL);
1219 	}
1220 
1221 	if (!ops) {
1222 		pr_err("Thermal zone device ops not defined\n");
1223 		return ERR_PTR(-EINVAL);
1224 	}
1225 
1226 	if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1227 		return ERR_PTR(-EINVAL);
1228 
1229 	tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1230 	if (!tz)
1231 		return ERR_PTR(-ENOMEM);
1232 
1233 	INIT_LIST_HEAD(&tz->thermal_instances);
1234 	ida_init(&tz->ida);
1235 	mutex_init(&tz->lock);
1236 	id = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1237 	if (id < 0) {
1238 		result = id;
1239 		goto free_tz;
1240 	}
1241 
1242 	tz->id = id;
1243 	strlcpy(tz->type, type, sizeof(tz->type));
1244 
1245 	if (!ops->critical)
1246 		ops->critical = thermal_zone_device_critical;
1247 
1248 	tz->ops = ops;
1249 	tz->tzp = tzp;
1250 	tz->device.class = &thermal_class;
1251 	tz->devdata = devdata;
1252 	tz->trips = trips;
1253 
1254 	thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1255 	thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1256 
1257 	/* sys I/F */
1258 	/* Add nodes that are always present via .groups */
1259 	result = thermal_zone_create_device_groups(tz, mask);
1260 	if (result)
1261 		goto remove_id;
1262 
1263 	/* A new thermal zone needs to be updated anyway. */
1264 	atomic_set(&tz->need_update, 1);
1265 
1266 	result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1267 	if (result) {
1268 		thermal_zone_destroy_device_groups(tz);
1269 		goto remove_id;
1270 	}
1271 	result = device_register(&tz->device);
1272 	if (result)
1273 		goto release_device;
1274 
1275 	for (count = 0; count < trips; count++) {
1276 		if (tz->ops->get_trip_type(tz, count, &trip_type) ||
1277 		    tz->ops->get_trip_temp(tz, count, &trip_temp) ||
1278 		    !trip_temp)
1279 			set_bit(count, &tz->trips_disabled);
1280 	}
1281 
1282 	/* Update 'this' zone's governor information */
1283 	mutex_lock(&thermal_governor_lock);
1284 
1285 	if (tz->tzp)
1286 		governor = __find_governor(tz->tzp->governor_name);
1287 	else
1288 		governor = def_governor;
1289 
1290 	result = thermal_set_governor(tz, governor);
1291 	if (result) {
1292 		mutex_unlock(&thermal_governor_lock);
1293 		goto unregister;
1294 	}
1295 
1296 	mutex_unlock(&thermal_governor_lock);
1297 
1298 	if (!tz->tzp || !tz->tzp->no_hwmon) {
1299 		result = thermal_add_hwmon_sysfs(tz);
1300 		if (result)
1301 			goto unregister;
1302 	}
1303 
1304 	mutex_lock(&thermal_list_lock);
1305 	list_add_tail(&tz->node, &thermal_tz_list);
1306 	mutex_unlock(&thermal_list_lock);
1307 
1308 	/* Bind cooling devices for this zone */
1309 	bind_tz(tz);
1310 
1311 	INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1312 
1313 	thermal_zone_device_init(tz);
1314 	/* Update the new thermal zone and mark it as already updated. */
1315 	if (atomic_cmpxchg(&tz->need_update, 1, 0))
1316 		thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1317 
1318 	thermal_notify_tz_create(tz->id, tz->type);
1319 
1320 	return tz;
1321 
1322 unregister:
1323 	device_del(&tz->device);
1324 release_device:
1325 	put_device(&tz->device);
1326 	tz = NULL;
1327 remove_id:
1328 	ida_simple_remove(&thermal_tz_ida, id);
1329 free_tz:
1330 	kfree(tz);
1331 	return ERR_PTR(result);
1332 }
1333 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1334 
1335 /**
1336  * thermal_zone_device_unregister - removes the registered thermal zone device
1337  * @tz: the thermal zone device to remove
1338  */
thermal_zone_device_unregister(struct thermal_zone_device * tz)1339 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1340 {
1341 	int i, tz_id;
1342 	const struct thermal_zone_params *tzp;
1343 	struct thermal_cooling_device *cdev;
1344 	struct thermal_zone_device *pos = NULL;
1345 
1346 	if (!tz)
1347 		return;
1348 
1349 	tzp = tz->tzp;
1350 	tz_id = tz->id;
1351 
1352 	mutex_lock(&thermal_list_lock);
1353 	list_for_each_entry(pos, &thermal_tz_list, node)
1354 		if (pos == tz)
1355 			break;
1356 	if (pos != tz) {
1357 		/* thermal zone device not found */
1358 		mutex_unlock(&thermal_list_lock);
1359 		return;
1360 	}
1361 	list_del(&tz->node);
1362 
1363 	/* Unbind all cdevs associated with 'this' thermal zone */
1364 	list_for_each_entry(cdev, &thermal_cdev_list, node) {
1365 		if (tz->ops->unbind) {
1366 			tz->ops->unbind(tz, cdev);
1367 			continue;
1368 		}
1369 
1370 		if (!tzp || !tzp->tbp)
1371 			break;
1372 
1373 		for (i = 0; i < tzp->num_tbps; i++) {
1374 			if (tzp->tbp[i].cdev == cdev) {
1375 				__unbind(tz, tzp->tbp[i].trip_mask, cdev);
1376 				tzp->tbp[i].cdev = NULL;
1377 			}
1378 		}
1379 	}
1380 
1381 	mutex_unlock(&thermal_list_lock);
1382 
1383 	cancel_delayed_work_sync(&tz->poll_queue);
1384 
1385 	thermal_set_governor(tz, NULL);
1386 
1387 	thermal_remove_hwmon_sysfs(tz);
1388 	ida_simple_remove(&thermal_tz_ida, tz->id);
1389 	ida_destroy(&tz->ida);
1390 	mutex_destroy(&tz->lock);
1391 	device_unregister(&tz->device);
1392 
1393 	thermal_notify_tz_delete(tz_id);
1394 }
1395 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1396 
1397 /**
1398  * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1399  * @name: thermal zone name to fetch the temperature
1400  *
1401  * When only one zone is found with the passed name, returns a reference to it.
1402  *
1403  * Return: On success returns a reference to an unique thermal zone with
1404  * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1405  * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1406  */
thermal_zone_get_zone_by_name(const char * name)1407 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1408 {
1409 	struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1410 	unsigned int found = 0;
1411 
1412 	if (!name)
1413 		goto exit;
1414 
1415 	mutex_lock(&thermal_list_lock);
1416 	list_for_each_entry(pos, &thermal_tz_list, node)
1417 		if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1418 			found++;
1419 			ref = pos;
1420 		}
1421 	mutex_unlock(&thermal_list_lock);
1422 
1423 	/* nothing has been found, thus an error code for it */
1424 	if (found == 0)
1425 		ref = ERR_PTR(-ENODEV);
1426 	else if (found > 1)
1427 	/* Success only when an unique zone is found */
1428 		ref = ERR_PTR(-EEXIST);
1429 
1430 exit:
1431 	return ref;
1432 }
1433 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1434 
thermal_pm_notify(struct notifier_block * nb,unsigned long mode,void * _unused)1435 static int thermal_pm_notify(struct notifier_block *nb,
1436 			     unsigned long mode, void *_unused)
1437 {
1438 	struct thermal_zone_device *tz;
1439 	int irq_wakeable = 0;
1440 
1441 	switch (mode) {
1442 	case PM_HIBERNATION_PREPARE:
1443 	case PM_RESTORE_PREPARE:
1444 	case PM_SUSPEND_PREPARE:
1445 		atomic_set(&in_suspend, 1);
1446 		break;
1447 	case PM_POST_HIBERNATION:
1448 	case PM_POST_RESTORE:
1449 	case PM_POST_SUSPEND:
1450 		atomic_set(&in_suspend, 0);
1451 		list_for_each_entry(tz, &thermal_tz_list, node) {
1452 			if (!thermal_zone_device_is_enabled(tz))
1453 				continue;
1454 
1455 			trace_android_vh_thermal_pm_notify_suspend(tz, &irq_wakeable);
1456 			if (irq_wakeable)
1457 				continue;
1458 
1459 			thermal_zone_device_init(tz);
1460 			thermal_zone_device_update(tz,
1461 						   THERMAL_EVENT_UNSPECIFIED);
1462 		}
1463 		break;
1464 	default:
1465 		break;
1466 	}
1467 	return 0;
1468 }
1469 
1470 static struct notifier_block thermal_pm_nb = {
1471 	.notifier_call = thermal_pm_notify,
1472 };
1473 
thermal_init(void)1474 static int __init thermal_init(void)
1475 {
1476 	int result;
1477 
1478 	result = thermal_netlink_init();
1479 	if (result)
1480 		goto error;
1481 
1482 	result = thermal_register_governors();
1483 	if (result)
1484 		goto error;
1485 
1486 	result = class_register(&thermal_class);
1487 	if (result)
1488 		goto unregister_governors;
1489 
1490 	result = of_parse_thermal_zones();
1491 	if (result)
1492 		goto unregister_class;
1493 
1494 	result = register_pm_notifier(&thermal_pm_nb);
1495 	if (result)
1496 		pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1497 			result);
1498 
1499 	return 0;
1500 
1501 unregister_class:
1502 	class_unregister(&thermal_class);
1503 unregister_governors:
1504 	thermal_unregister_governors();
1505 error:
1506 	ida_destroy(&thermal_tz_ida);
1507 	ida_destroy(&thermal_cdev_ida);
1508 	mutex_destroy(&thermal_list_lock);
1509 	mutex_destroy(&thermal_governor_lock);
1510 	return result;
1511 }
1512 postcore_initcall(thermal_init);
1513