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