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