xref: /linux/drivers/thermal/thermal_core.c (revision 3aed61d1eb06b8b19b7bb09d49b222ebc3f83347)
1 /*
2  *  thermal.c - Generic Thermal Management Sysfs support.
3  *
4  *  Copyright (C) 2008 Intel Corp
5  *  Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
6  *  Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
7  *
8  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; version 2 of the License.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25 
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/slab.h>
32 #include <linux/kdev_t.h>
33 #include <linux/idr.h>
34 #include <linux/thermal.h>
35 #include <linux/reboot.h>
36 #include <linux/string.h>
37 #include <linux/of.h>
38 #include <net/netlink.h>
39 #include <net/genetlink.h>
40 
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/thermal.h>
43 
44 #include "thermal_core.h"
45 #include "thermal_hwmon.h"
46 
47 MODULE_AUTHOR("Zhang Rui");
48 MODULE_DESCRIPTION("Generic thermal management sysfs support");
49 MODULE_LICENSE("GPL v2");
50 
51 static DEFINE_IDR(thermal_tz_idr);
52 static DEFINE_IDR(thermal_cdev_idr);
53 static DEFINE_MUTEX(thermal_idr_lock);
54 
55 static LIST_HEAD(thermal_tz_list);
56 static LIST_HEAD(thermal_cdev_list);
57 static LIST_HEAD(thermal_governor_list);
58 
59 static DEFINE_MUTEX(thermal_list_lock);
60 static DEFINE_MUTEX(thermal_governor_lock);
61 
62 static struct thermal_governor *def_governor;
63 
64 static struct thermal_governor *__find_governor(const char *name)
65 {
66 	struct thermal_governor *pos;
67 
68 	if (!name || !name[0])
69 		return def_governor;
70 
71 	list_for_each_entry(pos, &thermal_governor_list, governor_list)
72 		if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
73 			return pos;
74 
75 	return NULL;
76 }
77 
78 /**
79  * bind_previous_governor() - bind the previous governor of the thermal zone
80  * @tz:		a valid pointer to a struct thermal_zone_device
81  * @failed_gov_name:	the name of the governor that failed to register
82  *
83  * Register the previous governor of the thermal zone after a new
84  * governor has failed to be bound.
85  */
86 static void bind_previous_governor(struct thermal_zone_device *tz,
87 				   const char *failed_gov_name)
88 {
89 	if (tz->governor && tz->governor->bind_to_tz) {
90 		if (tz->governor->bind_to_tz(tz)) {
91 			dev_err(&tz->device,
92 				"governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
93 				failed_gov_name, tz->governor->name, tz->type);
94 			tz->governor = NULL;
95 		}
96 	}
97 }
98 
99 /**
100  * thermal_set_governor() - Switch to another governor
101  * @tz:		a valid pointer to a struct thermal_zone_device
102  * @new_gov:	pointer to the new governor
103  *
104  * Change the governor of thermal zone @tz.
105  *
106  * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
107  */
108 static int thermal_set_governor(struct thermal_zone_device *tz,
109 				struct thermal_governor *new_gov)
110 {
111 	int ret = 0;
112 
113 	if (tz->governor && tz->governor->unbind_from_tz)
114 		tz->governor->unbind_from_tz(tz);
115 
116 	if (new_gov && new_gov->bind_to_tz) {
117 		ret = new_gov->bind_to_tz(tz);
118 		if (ret) {
119 			bind_previous_governor(tz, new_gov->name);
120 
121 			return ret;
122 		}
123 	}
124 
125 	tz->governor = new_gov;
126 
127 	return ret;
128 }
129 
130 int thermal_register_governor(struct thermal_governor *governor)
131 {
132 	int err;
133 	const char *name;
134 	struct thermal_zone_device *pos;
135 
136 	if (!governor)
137 		return -EINVAL;
138 
139 	mutex_lock(&thermal_governor_lock);
140 
141 	err = -EBUSY;
142 	if (__find_governor(governor->name) == NULL) {
143 		err = 0;
144 		list_add(&governor->governor_list, &thermal_governor_list);
145 		if (!def_governor && !strncmp(governor->name,
146 			DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
147 			def_governor = governor;
148 	}
149 
150 	mutex_lock(&thermal_list_lock);
151 
152 	list_for_each_entry(pos, &thermal_tz_list, node) {
153 		/*
154 		 * only thermal zones with specified tz->tzp->governor_name
155 		 * may run with tz->govenor unset
156 		 */
157 		if (pos->governor)
158 			continue;
159 
160 		name = pos->tzp->governor_name;
161 
162 		if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
163 			int ret;
164 
165 			ret = thermal_set_governor(pos, governor);
166 			if (ret)
167 				dev_err(&pos->device,
168 					"Failed to set governor %s for thermal zone %s: %d\n",
169 					governor->name, pos->type, ret);
170 		}
171 	}
172 
173 	mutex_unlock(&thermal_list_lock);
174 	mutex_unlock(&thermal_governor_lock);
175 
176 	return err;
177 }
178 
179 void thermal_unregister_governor(struct thermal_governor *governor)
180 {
181 	struct thermal_zone_device *pos;
182 
183 	if (!governor)
184 		return;
185 
186 	mutex_lock(&thermal_governor_lock);
187 
188 	if (__find_governor(governor->name) == NULL)
189 		goto exit;
190 
191 	mutex_lock(&thermal_list_lock);
192 
193 	list_for_each_entry(pos, &thermal_tz_list, node) {
194 		if (!strncasecmp(pos->governor->name, governor->name,
195 						THERMAL_NAME_LENGTH))
196 			thermal_set_governor(pos, NULL);
197 	}
198 
199 	mutex_unlock(&thermal_list_lock);
200 	list_del(&governor->governor_list);
201 exit:
202 	mutex_unlock(&thermal_governor_lock);
203 	return;
204 }
205 
206 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
207 {
208 	int ret;
209 
210 	if (lock)
211 		mutex_lock(lock);
212 	ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
213 	if (lock)
214 		mutex_unlock(lock);
215 	if (unlikely(ret < 0))
216 		return ret;
217 	*id = ret;
218 	return 0;
219 }
220 
221 static void release_idr(struct idr *idr, struct mutex *lock, int id)
222 {
223 	if (lock)
224 		mutex_lock(lock);
225 	idr_remove(idr, id);
226 	if (lock)
227 		mutex_unlock(lock);
228 }
229 
230 int get_tz_trend(struct thermal_zone_device *tz, int trip)
231 {
232 	enum thermal_trend trend;
233 
234 	if (tz->emul_temperature || !tz->ops->get_trend ||
235 	    tz->ops->get_trend(tz, trip, &trend)) {
236 		if (tz->temperature > tz->last_temperature)
237 			trend = THERMAL_TREND_RAISING;
238 		else if (tz->temperature < tz->last_temperature)
239 			trend = THERMAL_TREND_DROPPING;
240 		else
241 			trend = THERMAL_TREND_STABLE;
242 	}
243 
244 	return trend;
245 }
246 EXPORT_SYMBOL(get_tz_trend);
247 
248 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
249 			struct thermal_cooling_device *cdev, int trip)
250 {
251 	struct thermal_instance *pos = NULL;
252 	struct thermal_instance *target_instance = NULL;
253 
254 	mutex_lock(&tz->lock);
255 	mutex_lock(&cdev->lock);
256 
257 	list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
258 		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
259 			target_instance = pos;
260 			break;
261 		}
262 	}
263 
264 	mutex_unlock(&cdev->lock);
265 	mutex_unlock(&tz->lock);
266 
267 	return target_instance;
268 }
269 EXPORT_SYMBOL(get_thermal_instance);
270 
271 static void print_bind_err_msg(struct thermal_zone_device *tz,
272 			struct thermal_cooling_device *cdev, int ret)
273 {
274 	dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
275 				tz->type, cdev->type, ret);
276 }
277 
278 static void __bind(struct thermal_zone_device *tz, int mask,
279 			struct thermal_cooling_device *cdev,
280 			unsigned long *limits,
281 			unsigned int weight)
282 {
283 	int i, ret;
284 
285 	for (i = 0; i < tz->trips; i++) {
286 		if (mask & (1 << i)) {
287 			unsigned long upper, lower;
288 
289 			upper = THERMAL_NO_LIMIT;
290 			lower = THERMAL_NO_LIMIT;
291 			if (limits) {
292 				lower = limits[i * 2];
293 				upper = limits[i * 2 + 1];
294 			}
295 			ret = thermal_zone_bind_cooling_device(tz, i, cdev,
296 							       upper, lower,
297 							       weight);
298 			if (ret)
299 				print_bind_err_msg(tz, cdev, ret);
300 		}
301 	}
302 }
303 
304 static void __unbind(struct thermal_zone_device *tz, int mask,
305 			struct thermal_cooling_device *cdev)
306 {
307 	int i;
308 
309 	for (i = 0; i < tz->trips; i++)
310 		if (mask & (1 << i))
311 			thermal_zone_unbind_cooling_device(tz, i, cdev);
312 }
313 
314 static void bind_cdev(struct thermal_cooling_device *cdev)
315 {
316 	int i, ret;
317 	const struct thermal_zone_params *tzp;
318 	struct thermal_zone_device *pos = NULL;
319 
320 	mutex_lock(&thermal_list_lock);
321 
322 	list_for_each_entry(pos, &thermal_tz_list, node) {
323 		if (!pos->tzp && !pos->ops->bind)
324 			continue;
325 
326 		if (pos->ops->bind) {
327 			ret = pos->ops->bind(pos, cdev);
328 			if (ret)
329 				print_bind_err_msg(pos, cdev, ret);
330 			continue;
331 		}
332 
333 		tzp = pos->tzp;
334 		if (!tzp || !tzp->tbp)
335 			continue;
336 
337 		for (i = 0; i < tzp->num_tbps; i++) {
338 			if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
339 				continue;
340 			if (tzp->tbp[i].match(pos, cdev))
341 				continue;
342 			tzp->tbp[i].cdev = cdev;
343 			__bind(pos, tzp->tbp[i].trip_mask, cdev,
344 			       tzp->tbp[i].binding_limits,
345 			       tzp->tbp[i].weight);
346 		}
347 	}
348 
349 	mutex_unlock(&thermal_list_lock);
350 }
351 
352 static void bind_tz(struct thermal_zone_device *tz)
353 {
354 	int i, ret;
355 	struct thermal_cooling_device *pos = NULL;
356 	const struct thermal_zone_params *tzp = tz->tzp;
357 
358 	if (!tzp && !tz->ops->bind)
359 		return;
360 
361 	mutex_lock(&thermal_list_lock);
362 
363 	/* If there is ops->bind, try to use ops->bind */
364 	if (tz->ops->bind) {
365 		list_for_each_entry(pos, &thermal_cdev_list, node) {
366 			ret = tz->ops->bind(tz, pos);
367 			if (ret)
368 				print_bind_err_msg(tz, pos, ret);
369 		}
370 		goto exit;
371 	}
372 
373 	if (!tzp || !tzp->tbp)
374 		goto exit;
375 
376 	list_for_each_entry(pos, &thermal_cdev_list, node) {
377 		for (i = 0; i < tzp->num_tbps; i++) {
378 			if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
379 				continue;
380 			if (tzp->tbp[i].match(tz, pos))
381 				continue;
382 			tzp->tbp[i].cdev = pos;
383 			__bind(tz, tzp->tbp[i].trip_mask, pos,
384 			       tzp->tbp[i].binding_limits,
385 			       tzp->tbp[i].weight);
386 		}
387 	}
388 exit:
389 	mutex_unlock(&thermal_list_lock);
390 }
391 
392 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
393 					    int delay)
394 {
395 	if (delay > 1000)
396 		mod_delayed_work(system_freezable_wq, &tz->poll_queue,
397 				 round_jiffies(msecs_to_jiffies(delay)));
398 	else if (delay)
399 		mod_delayed_work(system_freezable_wq, &tz->poll_queue,
400 				 msecs_to_jiffies(delay));
401 	else
402 		cancel_delayed_work(&tz->poll_queue);
403 }
404 
405 static void monitor_thermal_zone(struct thermal_zone_device *tz)
406 {
407 	mutex_lock(&tz->lock);
408 
409 	if (tz->passive)
410 		thermal_zone_device_set_polling(tz, tz->passive_delay);
411 	else if (tz->polling_delay)
412 		thermal_zone_device_set_polling(tz, tz->polling_delay);
413 	else
414 		thermal_zone_device_set_polling(tz, 0);
415 
416 	mutex_unlock(&tz->lock);
417 }
418 
419 static void handle_non_critical_trips(struct thermal_zone_device *tz,
420 			int trip, enum thermal_trip_type trip_type)
421 {
422 	tz->governor ? tz->governor->throttle(tz, trip) :
423 		       def_governor->throttle(tz, trip);
424 }
425 
426 static void handle_critical_trips(struct thermal_zone_device *tz,
427 				int trip, enum thermal_trip_type trip_type)
428 {
429 	int trip_temp;
430 
431 	tz->ops->get_trip_temp(tz, trip, &trip_temp);
432 
433 	/* If we have not crossed the trip_temp, we do not care. */
434 	if (trip_temp <= 0 || tz->temperature < trip_temp)
435 		return;
436 
437 	trace_thermal_zone_trip(tz, trip, trip_type);
438 
439 	if (tz->ops->notify)
440 		tz->ops->notify(tz, trip, trip_type);
441 
442 	if (trip_type == THERMAL_TRIP_CRITICAL) {
443 		dev_emerg(&tz->device,
444 			  "critical temperature reached(%d C),shutting down\n",
445 			  tz->temperature / 1000);
446 		orderly_poweroff(true);
447 	}
448 }
449 
450 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
451 {
452 	enum thermal_trip_type type;
453 
454 	tz->ops->get_trip_type(tz, trip, &type);
455 
456 	if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
457 		handle_critical_trips(tz, trip, type);
458 	else
459 		handle_non_critical_trips(tz, trip, type);
460 	/*
461 	 * Alright, we handled this trip successfully.
462 	 * So, start monitoring again.
463 	 */
464 	monitor_thermal_zone(tz);
465 }
466 
467 /**
468  * thermal_zone_get_temp() - returns the temperature of a thermal zone
469  * @tz: a valid pointer to a struct thermal_zone_device
470  * @temp: a valid pointer to where to store the resulting temperature.
471  *
472  * When a valid thermal zone reference is passed, it will fetch its
473  * temperature and fill @temp.
474  *
475  * Return: On success returns 0, an error code otherwise
476  */
477 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
478 {
479 	int ret = -EINVAL;
480 	int count;
481 	int crit_temp = INT_MAX;
482 	enum thermal_trip_type type;
483 
484 	if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
485 		goto exit;
486 
487 	mutex_lock(&tz->lock);
488 
489 	ret = tz->ops->get_temp(tz, temp);
490 
491 	if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
492 		for (count = 0; count < tz->trips; count++) {
493 			ret = tz->ops->get_trip_type(tz, count, &type);
494 			if (!ret && type == THERMAL_TRIP_CRITICAL) {
495 				ret = tz->ops->get_trip_temp(tz, count,
496 						&crit_temp);
497 				break;
498 			}
499 		}
500 
501 		/*
502 		 * Only allow emulating a temperature when the real temperature
503 		 * is below the critical temperature so that the emulation code
504 		 * cannot hide critical conditions.
505 		 */
506 		if (!ret && *temp < crit_temp)
507 			*temp = tz->emul_temperature;
508 	}
509 
510 	mutex_unlock(&tz->lock);
511 exit:
512 	return ret;
513 }
514 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
515 
516 static void update_temperature(struct thermal_zone_device *tz)
517 {
518 	int temp, ret;
519 
520 	ret = thermal_zone_get_temp(tz, &temp);
521 	if (ret) {
522 		if (ret != -EAGAIN)
523 			dev_warn(&tz->device,
524 				 "failed to read out thermal zone (%d)\n",
525 				 ret);
526 		return;
527 	}
528 
529 	mutex_lock(&tz->lock);
530 	tz->last_temperature = tz->temperature;
531 	tz->temperature = temp;
532 	mutex_unlock(&tz->lock);
533 
534 	trace_thermal_temperature(tz);
535 	dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
536 				tz->last_temperature, tz->temperature);
537 }
538 
539 void thermal_zone_device_update(struct thermal_zone_device *tz)
540 {
541 	int count;
542 
543 	if (!tz->ops->get_temp)
544 		return;
545 
546 	update_temperature(tz);
547 
548 	for (count = 0; count < tz->trips; count++)
549 		handle_thermal_trip(tz, count);
550 }
551 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
552 
553 static void thermal_zone_device_check(struct work_struct *work)
554 {
555 	struct thermal_zone_device *tz = container_of(work, struct
556 						      thermal_zone_device,
557 						      poll_queue.work);
558 	thermal_zone_device_update(tz);
559 }
560 
561 /* sys I/F for thermal zone */
562 
563 #define to_thermal_zone(_dev) \
564 	container_of(_dev, struct thermal_zone_device, device)
565 
566 static ssize_t
567 type_show(struct device *dev, struct device_attribute *attr, char *buf)
568 {
569 	struct thermal_zone_device *tz = to_thermal_zone(dev);
570 
571 	return sprintf(buf, "%s\n", tz->type);
572 }
573 
574 static ssize_t
575 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
576 {
577 	struct thermal_zone_device *tz = to_thermal_zone(dev);
578 	int temperature, ret;
579 
580 	ret = thermal_zone_get_temp(tz, &temperature);
581 
582 	if (ret)
583 		return ret;
584 
585 	return sprintf(buf, "%d\n", temperature);
586 }
587 
588 static ssize_t
589 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
590 {
591 	struct thermal_zone_device *tz = to_thermal_zone(dev);
592 	enum thermal_device_mode mode;
593 	int result;
594 
595 	if (!tz->ops->get_mode)
596 		return -EPERM;
597 
598 	result = tz->ops->get_mode(tz, &mode);
599 	if (result)
600 		return result;
601 
602 	return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
603 		       : "disabled");
604 }
605 
606 static ssize_t
607 mode_store(struct device *dev, struct device_attribute *attr,
608 	   const char *buf, size_t count)
609 {
610 	struct thermal_zone_device *tz = to_thermal_zone(dev);
611 	int result;
612 
613 	if (!tz->ops->set_mode)
614 		return -EPERM;
615 
616 	if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
617 		result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
618 	else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
619 		result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
620 	else
621 		result = -EINVAL;
622 
623 	if (result)
624 		return result;
625 
626 	return count;
627 }
628 
629 static ssize_t
630 trip_point_type_show(struct device *dev, struct device_attribute *attr,
631 		     char *buf)
632 {
633 	struct thermal_zone_device *tz = to_thermal_zone(dev);
634 	enum thermal_trip_type type;
635 	int trip, result;
636 
637 	if (!tz->ops->get_trip_type)
638 		return -EPERM;
639 
640 	if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
641 		return -EINVAL;
642 
643 	result = tz->ops->get_trip_type(tz, trip, &type);
644 	if (result)
645 		return result;
646 
647 	switch (type) {
648 	case THERMAL_TRIP_CRITICAL:
649 		return sprintf(buf, "critical\n");
650 	case THERMAL_TRIP_HOT:
651 		return sprintf(buf, "hot\n");
652 	case THERMAL_TRIP_PASSIVE:
653 		return sprintf(buf, "passive\n");
654 	case THERMAL_TRIP_ACTIVE:
655 		return sprintf(buf, "active\n");
656 	default:
657 		return sprintf(buf, "unknown\n");
658 	}
659 }
660 
661 static ssize_t
662 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
663 		     const char *buf, size_t count)
664 {
665 	struct thermal_zone_device *tz = to_thermal_zone(dev);
666 	int trip, ret;
667 	unsigned long temperature;
668 
669 	if (!tz->ops->set_trip_temp)
670 		return -EPERM;
671 
672 	if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
673 		return -EINVAL;
674 
675 	if (kstrtoul(buf, 10, &temperature))
676 		return -EINVAL;
677 
678 	ret = tz->ops->set_trip_temp(tz, trip, temperature);
679 
680 	return ret ? ret : count;
681 }
682 
683 static ssize_t
684 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
685 		     char *buf)
686 {
687 	struct thermal_zone_device *tz = to_thermal_zone(dev);
688 	int trip, ret;
689 	int temperature;
690 
691 	if (!tz->ops->get_trip_temp)
692 		return -EPERM;
693 
694 	if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
695 		return -EINVAL;
696 
697 	ret = tz->ops->get_trip_temp(tz, trip, &temperature);
698 
699 	if (ret)
700 		return ret;
701 
702 	return sprintf(buf, "%d\n", temperature);
703 }
704 
705 static ssize_t
706 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
707 			const char *buf, size_t count)
708 {
709 	struct thermal_zone_device *tz = to_thermal_zone(dev);
710 	int trip, ret;
711 	int temperature;
712 
713 	if (!tz->ops->set_trip_hyst)
714 		return -EPERM;
715 
716 	if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
717 		return -EINVAL;
718 
719 	if (kstrtoint(buf, 10, &temperature))
720 		return -EINVAL;
721 
722 	/*
723 	 * We are not doing any check on the 'temperature' value
724 	 * here. The driver implementing 'set_trip_hyst' has to
725 	 * take care of this.
726 	 */
727 	ret = tz->ops->set_trip_hyst(tz, trip, temperature);
728 
729 	return ret ? ret : count;
730 }
731 
732 static ssize_t
733 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
734 			char *buf)
735 {
736 	struct thermal_zone_device *tz = to_thermal_zone(dev);
737 	int trip, ret;
738 	int temperature;
739 
740 	if (!tz->ops->get_trip_hyst)
741 		return -EPERM;
742 
743 	if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
744 		return -EINVAL;
745 
746 	ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
747 
748 	return ret ? ret : sprintf(buf, "%d\n", temperature);
749 }
750 
751 static ssize_t
752 passive_store(struct device *dev, struct device_attribute *attr,
753 		    const char *buf, size_t count)
754 {
755 	struct thermal_zone_device *tz = to_thermal_zone(dev);
756 	struct thermal_cooling_device *cdev = NULL;
757 	int state;
758 
759 	if (!sscanf(buf, "%d\n", &state))
760 		return -EINVAL;
761 
762 	/* sanity check: values below 1000 millicelcius don't make sense
763 	 * and can cause the system to go into a thermal heart attack
764 	 */
765 	if (state && state < 1000)
766 		return -EINVAL;
767 
768 	if (state && !tz->forced_passive) {
769 		mutex_lock(&thermal_list_lock);
770 		list_for_each_entry(cdev, &thermal_cdev_list, node) {
771 			if (!strncmp("Processor", cdev->type,
772 				     sizeof("Processor")))
773 				thermal_zone_bind_cooling_device(tz,
774 						THERMAL_TRIPS_NONE, cdev,
775 						THERMAL_NO_LIMIT,
776 						THERMAL_NO_LIMIT,
777 						THERMAL_WEIGHT_DEFAULT);
778 		}
779 		mutex_unlock(&thermal_list_lock);
780 		if (!tz->passive_delay)
781 			tz->passive_delay = 1000;
782 	} else if (!state && tz->forced_passive) {
783 		mutex_lock(&thermal_list_lock);
784 		list_for_each_entry(cdev, &thermal_cdev_list, node) {
785 			if (!strncmp("Processor", cdev->type,
786 				     sizeof("Processor")))
787 				thermal_zone_unbind_cooling_device(tz,
788 								   THERMAL_TRIPS_NONE,
789 								   cdev);
790 		}
791 		mutex_unlock(&thermal_list_lock);
792 		tz->passive_delay = 0;
793 	}
794 
795 	tz->forced_passive = state;
796 
797 	thermal_zone_device_update(tz);
798 
799 	return count;
800 }
801 
802 static ssize_t
803 passive_show(struct device *dev, struct device_attribute *attr,
804 		   char *buf)
805 {
806 	struct thermal_zone_device *tz = to_thermal_zone(dev);
807 
808 	return sprintf(buf, "%d\n", tz->forced_passive);
809 }
810 
811 static ssize_t
812 policy_store(struct device *dev, struct device_attribute *attr,
813 		    const char *buf, size_t count)
814 {
815 	int ret = -EINVAL;
816 	struct thermal_zone_device *tz = to_thermal_zone(dev);
817 	struct thermal_governor *gov;
818 	char name[THERMAL_NAME_LENGTH];
819 
820 	snprintf(name, sizeof(name), "%s", buf);
821 
822 	mutex_lock(&thermal_governor_lock);
823 	mutex_lock(&tz->lock);
824 
825 	gov = __find_governor(strim(name));
826 	if (!gov)
827 		goto exit;
828 
829 	ret = thermal_set_governor(tz, gov);
830 	if (!ret)
831 		ret = count;
832 
833 exit:
834 	mutex_unlock(&tz->lock);
835 	mutex_unlock(&thermal_governor_lock);
836 	return ret;
837 }
838 
839 static ssize_t
840 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
841 {
842 	struct thermal_zone_device *tz = to_thermal_zone(dev);
843 
844 	return sprintf(buf, "%s\n", tz->governor->name);
845 }
846 
847 static ssize_t
848 available_policies_show(struct device *dev, struct device_attribute *devattr,
849 			char *buf)
850 {
851 	struct thermal_governor *pos;
852 	ssize_t count = 0;
853 	ssize_t size = PAGE_SIZE;
854 
855 	mutex_lock(&thermal_governor_lock);
856 
857 	list_for_each_entry(pos, &thermal_governor_list, governor_list) {
858 		size = PAGE_SIZE - count;
859 		count += scnprintf(buf + count, size, "%s ", pos->name);
860 	}
861 	count += scnprintf(buf + count, size, "\n");
862 
863 	mutex_unlock(&thermal_governor_lock);
864 
865 	return count;
866 }
867 
868 static ssize_t
869 emul_temp_store(struct device *dev, struct device_attribute *attr,
870 		     const char *buf, size_t count)
871 {
872 	struct thermal_zone_device *tz = to_thermal_zone(dev);
873 	int ret = 0;
874 	unsigned long temperature;
875 
876 	if (kstrtoul(buf, 10, &temperature))
877 		return -EINVAL;
878 
879 	if (!tz->ops->set_emul_temp) {
880 		mutex_lock(&tz->lock);
881 		tz->emul_temperature = temperature;
882 		mutex_unlock(&tz->lock);
883 	} else {
884 		ret = tz->ops->set_emul_temp(tz, temperature);
885 	}
886 
887 	if (!ret)
888 		thermal_zone_device_update(tz);
889 
890 	return ret ? ret : count;
891 }
892 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
893 
894 static ssize_t
895 sustainable_power_show(struct device *dev, struct device_attribute *devattr,
896 		       char *buf)
897 {
898 	struct thermal_zone_device *tz = to_thermal_zone(dev);
899 
900 	if (tz->tzp)
901 		return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
902 	else
903 		return -EIO;
904 }
905 
906 static ssize_t
907 sustainable_power_store(struct device *dev, struct device_attribute *devattr,
908 			const char *buf, size_t count)
909 {
910 	struct thermal_zone_device *tz = to_thermal_zone(dev);
911 	u32 sustainable_power;
912 
913 	if (!tz->tzp)
914 		return -EIO;
915 
916 	if (kstrtou32(buf, 10, &sustainable_power))
917 		return -EINVAL;
918 
919 	tz->tzp->sustainable_power = sustainable_power;
920 
921 	return count;
922 }
923 static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
924 		sustainable_power_store);
925 
926 #define create_s32_tzp_attr(name)					\
927 	static ssize_t							\
928 	name##_show(struct device *dev, struct device_attribute *devattr, \
929 		char *buf)						\
930 	{								\
931 	struct thermal_zone_device *tz = to_thermal_zone(dev);		\
932 									\
933 	if (tz->tzp)							\
934 		return sprintf(buf, "%u\n", tz->tzp->name);		\
935 	else								\
936 		return -EIO;						\
937 	}								\
938 									\
939 	static ssize_t							\
940 	name##_store(struct device *dev, struct device_attribute *devattr, \
941 		const char *buf, size_t count)				\
942 	{								\
943 		struct thermal_zone_device *tz = to_thermal_zone(dev);	\
944 		s32 value;						\
945 									\
946 		if (!tz->tzp)						\
947 			return -EIO;					\
948 									\
949 		if (kstrtos32(buf, 10, &value))				\
950 			return -EINVAL;					\
951 									\
952 		tz->tzp->name = value;					\
953 									\
954 		return count;						\
955 	}								\
956 	static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
957 
958 create_s32_tzp_attr(k_po);
959 create_s32_tzp_attr(k_pu);
960 create_s32_tzp_attr(k_i);
961 create_s32_tzp_attr(k_d);
962 create_s32_tzp_attr(integral_cutoff);
963 create_s32_tzp_attr(slope);
964 create_s32_tzp_attr(offset);
965 #undef create_s32_tzp_attr
966 
967 static struct device_attribute *dev_tzp_attrs[] = {
968 	&dev_attr_sustainable_power,
969 	&dev_attr_k_po,
970 	&dev_attr_k_pu,
971 	&dev_attr_k_i,
972 	&dev_attr_k_d,
973 	&dev_attr_integral_cutoff,
974 	&dev_attr_slope,
975 	&dev_attr_offset,
976 };
977 
978 static int create_tzp_attrs(struct device *dev)
979 {
980 	int i;
981 
982 	for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
983 		int ret;
984 		struct device_attribute *dev_attr = dev_tzp_attrs[i];
985 
986 		ret = device_create_file(dev, dev_attr);
987 		if (ret)
988 			return ret;
989 	}
990 
991 	return 0;
992 }
993 
994 /**
995  * power_actor_get_max_power() - get the maximum power that a cdev can consume
996  * @cdev:	pointer to &thermal_cooling_device
997  * @tz:		a valid thermal zone device pointer
998  * @max_power:	pointer in which to store the maximum power
999  *
1000  * Calculate the maximum power consumption in milliwats that the
1001  * cooling device can currently consume and store it in @max_power.
1002  *
1003  * Return: 0 on success, -EINVAL if @cdev doesn't support the
1004  * power_actor API or -E* on other error.
1005  */
1006 int power_actor_get_max_power(struct thermal_cooling_device *cdev,
1007 			      struct thermal_zone_device *tz, u32 *max_power)
1008 {
1009 	if (!cdev_is_power_actor(cdev))
1010 		return -EINVAL;
1011 
1012 	return cdev->ops->state2power(cdev, tz, 0, max_power);
1013 }
1014 
1015 /**
1016  * power_actor_set_power() - limit the maximum power that a cooling device can consume
1017  * @cdev:	pointer to &thermal_cooling_device
1018  * @instance:	thermal instance to update
1019  * @power:	the power in milliwatts
1020  *
1021  * Set the cooling device to consume at most @power milliwatts.
1022  *
1023  * Return: 0 on success, -EINVAL if the cooling device does not
1024  * implement the power actor API or -E* for other failures.
1025  */
1026 int power_actor_set_power(struct thermal_cooling_device *cdev,
1027 			  struct thermal_instance *instance, u32 power)
1028 {
1029 	unsigned long state;
1030 	int ret;
1031 
1032 	if (!cdev_is_power_actor(cdev))
1033 		return -EINVAL;
1034 
1035 	ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
1036 	if (ret)
1037 		return ret;
1038 
1039 	instance->target = state;
1040 	cdev->updated = false;
1041 	thermal_cdev_update(cdev);
1042 
1043 	return 0;
1044 }
1045 
1046 static DEVICE_ATTR(type, 0444, type_show, NULL);
1047 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
1048 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
1049 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
1050 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
1051 static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
1052 
1053 /* sys I/F for cooling device */
1054 #define to_cooling_device(_dev)	\
1055 	container_of(_dev, struct thermal_cooling_device, device)
1056 
1057 static ssize_t
1058 thermal_cooling_device_type_show(struct device *dev,
1059 				 struct device_attribute *attr, char *buf)
1060 {
1061 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
1062 
1063 	return sprintf(buf, "%s\n", cdev->type);
1064 }
1065 
1066 static ssize_t
1067 thermal_cooling_device_max_state_show(struct device *dev,
1068 				      struct device_attribute *attr, char *buf)
1069 {
1070 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
1071 	unsigned long state;
1072 	int ret;
1073 
1074 	ret = cdev->ops->get_max_state(cdev, &state);
1075 	if (ret)
1076 		return ret;
1077 	return sprintf(buf, "%ld\n", state);
1078 }
1079 
1080 static ssize_t
1081 thermal_cooling_device_cur_state_show(struct device *dev,
1082 				      struct device_attribute *attr, char *buf)
1083 {
1084 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
1085 	unsigned long state;
1086 	int ret;
1087 
1088 	ret = cdev->ops->get_cur_state(cdev, &state);
1089 	if (ret)
1090 		return ret;
1091 	return sprintf(buf, "%ld\n", state);
1092 }
1093 
1094 static ssize_t
1095 thermal_cooling_device_cur_state_store(struct device *dev,
1096 				       struct device_attribute *attr,
1097 				       const char *buf, size_t count)
1098 {
1099 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
1100 	unsigned long state;
1101 	int result;
1102 
1103 	if (!sscanf(buf, "%ld\n", &state))
1104 		return -EINVAL;
1105 
1106 	if ((long)state < 0)
1107 		return -EINVAL;
1108 
1109 	result = cdev->ops->set_cur_state(cdev, state);
1110 	if (result)
1111 		return result;
1112 	return count;
1113 }
1114 
1115 static struct device_attribute dev_attr_cdev_type =
1116 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
1117 static DEVICE_ATTR(max_state, 0444,
1118 		   thermal_cooling_device_max_state_show, NULL);
1119 static DEVICE_ATTR(cur_state, 0644,
1120 		   thermal_cooling_device_cur_state_show,
1121 		   thermal_cooling_device_cur_state_store);
1122 
1123 static ssize_t
1124 thermal_cooling_device_trip_point_show(struct device *dev,
1125 				       struct device_attribute *attr, char *buf)
1126 {
1127 	struct thermal_instance *instance;
1128 
1129 	instance =
1130 	    container_of(attr, struct thermal_instance, attr);
1131 
1132 	if (instance->trip == THERMAL_TRIPS_NONE)
1133 		return sprintf(buf, "-1\n");
1134 	else
1135 		return sprintf(buf, "%d\n", instance->trip);
1136 }
1137 
1138 static struct attribute *cooling_device_attrs[] = {
1139 	&dev_attr_cdev_type.attr,
1140 	&dev_attr_max_state.attr,
1141 	&dev_attr_cur_state.attr,
1142 	NULL,
1143 };
1144 
1145 static const struct attribute_group cooling_device_attr_group = {
1146 	.attrs = cooling_device_attrs,
1147 };
1148 
1149 static const struct attribute_group *cooling_device_attr_groups[] = {
1150 	&cooling_device_attr_group,
1151 	NULL,
1152 };
1153 
1154 static ssize_t
1155 thermal_cooling_device_weight_show(struct device *dev,
1156 				   struct device_attribute *attr, char *buf)
1157 {
1158 	struct thermal_instance *instance;
1159 
1160 	instance = container_of(attr, struct thermal_instance, weight_attr);
1161 
1162 	return sprintf(buf, "%d\n", instance->weight);
1163 }
1164 
1165 static ssize_t
1166 thermal_cooling_device_weight_store(struct device *dev,
1167 				    struct device_attribute *attr,
1168 				    const char *buf, size_t count)
1169 {
1170 	struct thermal_instance *instance;
1171 	int ret, weight;
1172 
1173 	ret = kstrtoint(buf, 0, &weight);
1174 	if (ret)
1175 		return ret;
1176 
1177 	instance = container_of(attr, struct thermal_instance, weight_attr);
1178 	instance->weight = weight;
1179 
1180 	return count;
1181 }
1182 /* Device management */
1183 
1184 /**
1185  * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1186  * @tz:		pointer to struct thermal_zone_device
1187  * @trip:	indicates which trip point the cooling devices is
1188  *		associated with in this thermal zone.
1189  * @cdev:	pointer to struct thermal_cooling_device
1190  * @upper:	the Maximum cooling state for this trip point.
1191  *		THERMAL_NO_LIMIT means no upper limit,
1192  *		and the cooling device can be in max_state.
1193  * @lower:	the Minimum cooling state can be used for this trip point.
1194  *		THERMAL_NO_LIMIT means no lower limit,
1195  *		and the cooling device can be in cooling state 0.
1196  * @weight:	The weight of the cooling device to be bound to the
1197  *		thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1198  *		default value
1199  *
1200  * This interface function bind a thermal cooling device to the certain trip
1201  * point of a thermal zone device.
1202  * This function is usually called in the thermal zone device .bind callback.
1203  *
1204  * Return: 0 on success, the proper error value otherwise.
1205  */
1206 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1207 				     int trip,
1208 				     struct thermal_cooling_device *cdev,
1209 				     unsigned long upper, unsigned long lower,
1210 				     unsigned int weight)
1211 {
1212 	struct thermal_instance *dev;
1213 	struct thermal_instance *pos;
1214 	struct thermal_zone_device *pos1;
1215 	struct thermal_cooling_device *pos2;
1216 	unsigned long max_state;
1217 	int result, ret;
1218 
1219 	if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1220 		return -EINVAL;
1221 
1222 	list_for_each_entry(pos1, &thermal_tz_list, node) {
1223 		if (pos1 == tz)
1224 			break;
1225 	}
1226 	list_for_each_entry(pos2, &thermal_cdev_list, node) {
1227 		if (pos2 == cdev)
1228 			break;
1229 	}
1230 
1231 	if (tz != pos1 || cdev != pos2)
1232 		return -EINVAL;
1233 
1234 	ret = cdev->ops->get_max_state(cdev, &max_state);
1235 	if (ret)
1236 		return ret;
1237 
1238 	/* lower default 0, upper default max_state */
1239 	lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1240 	upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1241 
1242 	if (lower > upper || upper > max_state)
1243 		return -EINVAL;
1244 
1245 	dev =
1246 	    kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1247 	if (!dev)
1248 		return -ENOMEM;
1249 	dev->tz = tz;
1250 	dev->cdev = cdev;
1251 	dev->trip = trip;
1252 	dev->upper = upper;
1253 	dev->lower = lower;
1254 	dev->target = THERMAL_NO_TARGET;
1255 	dev->weight = weight;
1256 
1257 	result = get_idr(&tz->idr, &tz->lock, &dev->id);
1258 	if (result)
1259 		goto free_mem;
1260 
1261 	sprintf(dev->name, "cdev%d", dev->id);
1262 	result =
1263 	    sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1264 	if (result)
1265 		goto release_idr;
1266 
1267 	sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1268 	sysfs_attr_init(&dev->attr.attr);
1269 	dev->attr.attr.name = dev->attr_name;
1270 	dev->attr.attr.mode = 0444;
1271 	dev->attr.show = thermal_cooling_device_trip_point_show;
1272 	result = device_create_file(&tz->device, &dev->attr);
1273 	if (result)
1274 		goto remove_symbol_link;
1275 
1276 	sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1277 	sysfs_attr_init(&dev->weight_attr.attr);
1278 	dev->weight_attr.attr.name = dev->weight_attr_name;
1279 	dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1280 	dev->weight_attr.show = thermal_cooling_device_weight_show;
1281 	dev->weight_attr.store = thermal_cooling_device_weight_store;
1282 	result = device_create_file(&tz->device, &dev->weight_attr);
1283 	if (result)
1284 		goto remove_trip_file;
1285 
1286 	mutex_lock(&tz->lock);
1287 	mutex_lock(&cdev->lock);
1288 	list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1289 	    if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1290 		result = -EEXIST;
1291 		break;
1292 	}
1293 	if (!result) {
1294 		list_add_tail(&dev->tz_node, &tz->thermal_instances);
1295 		list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1296 	}
1297 	mutex_unlock(&cdev->lock);
1298 	mutex_unlock(&tz->lock);
1299 
1300 	if (!result)
1301 		return 0;
1302 
1303 	device_remove_file(&tz->device, &dev->weight_attr);
1304 remove_trip_file:
1305 	device_remove_file(&tz->device, &dev->attr);
1306 remove_symbol_link:
1307 	sysfs_remove_link(&tz->device.kobj, dev->name);
1308 release_idr:
1309 	release_idr(&tz->idr, &tz->lock, dev->id);
1310 free_mem:
1311 	kfree(dev);
1312 	return result;
1313 }
1314 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
1315 
1316 /**
1317  * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1318  *					  thermal zone.
1319  * @tz:		pointer to a struct thermal_zone_device.
1320  * @trip:	indicates which trip point the cooling devices is
1321  *		associated with in this thermal zone.
1322  * @cdev:	pointer to a struct thermal_cooling_device.
1323  *
1324  * This interface function unbind a thermal cooling device from the certain
1325  * trip point of a thermal zone device.
1326  * This function is usually called in the thermal zone device .unbind callback.
1327  *
1328  * Return: 0 on success, the proper error value otherwise.
1329  */
1330 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1331 				       int trip,
1332 				       struct thermal_cooling_device *cdev)
1333 {
1334 	struct thermal_instance *pos, *next;
1335 
1336 	mutex_lock(&tz->lock);
1337 	mutex_lock(&cdev->lock);
1338 	list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1339 		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1340 			list_del(&pos->tz_node);
1341 			list_del(&pos->cdev_node);
1342 			mutex_unlock(&cdev->lock);
1343 			mutex_unlock(&tz->lock);
1344 			goto unbind;
1345 		}
1346 	}
1347 	mutex_unlock(&cdev->lock);
1348 	mutex_unlock(&tz->lock);
1349 
1350 	return -ENODEV;
1351 
1352 unbind:
1353 	device_remove_file(&tz->device, &pos->weight_attr);
1354 	device_remove_file(&tz->device, &pos->attr);
1355 	sysfs_remove_link(&tz->device.kobj, pos->name);
1356 	release_idr(&tz->idr, &tz->lock, pos->id);
1357 	kfree(pos);
1358 	return 0;
1359 }
1360 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1361 
1362 static void thermal_release(struct device *dev)
1363 {
1364 	struct thermal_zone_device *tz;
1365 	struct thermal_cooling_device *cdev;
1366 
1367 	if (!strncmp(dev_name(dev), "thermal_zone",
1368 		     sizeof("thermal_zone") - 1)) {
1369 		tz = to_thermal_zone(dev);
1370 		kfree(tz);
1371 	} else if(!strncmp(dev_name(dev), "cooling_device",
1372 			sizeof("cooling_device") - 1)){
1373 		cdev = to_cooling_device(dev);
1374 		kfree(cdev);
1375 	}
1376 }
1377 
1378 static struct class thermal_class = {
1379 	.name = "thermal",
1380 	.dev_release = thermal_release,
1381 };
1382 
1383 /**
1384  * __thermal_cooling_device_register() - register a new thermal cooling device
1385  * @np:		a pointer to a device tree node.
1386  * @type:	the thermal cooling device type.
1387  * @devdata:	device private data.
1388  * @ops:		standard thermal cooling devices callbacks.
1389  *
1390  * This interface function adds a new thermal cooling device (fan/processor/...)
1391  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1392  * to all the thermal zone devices registered at the same time.
1393  * It also gives the opportunity to link the cooling device to a device tree
1394  * node, so that it can be bound to a thermal zone created out of device tree.
1395  *
1396  * Return: a pointer to the created struct thermal_cooling_device or an
1397  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1398  */
1399 static struct thermal_cooling_device *
1400 __thermal_cooling_device_register(struct device_node *np,
1401 				  char *type, void *devdata,
1402 				  const struct thermal_cooling_device_ops *ops)
1403 {
1404 	struct thermal_cooling_device *cdev;
1405 	int result;
1406 
1407 	if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1408 		return ERR_PTR(-EINVAL);
1409 
1410 	if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1411 	    !ops->set_cur_state)
1412 		return ERR_PTR(-EINVAL);
1413 
1414 	cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1415 	if (!cdev)
1416 		return ERR_PTR(-ENOMEM);
1417 
1418 	result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1419 	if (result) {
1420 		kfree(cdev);
1421 		return ERR_PTR(result);
1422 	}
1423 
1424 	strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1425 	mutex_init(&cdev->lock);
1426 	INIT_LIST_HEAD(&cdev->thermal_instances);
1427 	cdev->np = np;
1428 	cdev->ops = ops;
1429 	cdev->updated = false;
1430 	cdev->device.class = &thermal_class;
1431 	cdev->device.groups = cooling_device_attr_groups;
1432 	cdev->devdata = devdata;
1433 	dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1434 	result = device_register(&cdev->device);
1435 	if (result) {
1436 		release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1437 		kfree(cdev);
1438 		return ERR_PTR(result);
1439 	}
1440 
1441 	/* Add 'this' new cdev to the global cdev list */
1442 	mutex_lock(&thermal_list_lock);
1443 	list_add(&cdev->node, &thermal_cdev_list);
1444 	mutex_unlock(&thermal_list_lock);
1445 
1446 	/* Update binding information for 'this' new cdev */
1447 	bind_cdev(cdev);
1448 
1449 	return cdev;
1450 }
1451 
1452 /**
1453  * thermal_cooling_device_register() - register a new thermal cooling device
1454  * @type:	the thermal cooling device type.
1455  * @devdata:	device private data.
1456  * @ops:		standard thermal cooling devices callbacks.
1457  *
1458  * This interface function adds a new thermal cooling device (fan/processor/...)
1459  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1460  * to all the thermal zone devices registered at the same time.
1461  *
1462  * Return: a pointer to the created struct thermal_cooling_device or an
1463  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1464  */
1465 struct thermal_cooling_device *
1466 thermal_cooling_device_register(char *type, void *devdata,
1467 				const struct thermal_cooling_device_ops *ops)
1468 {
1469 	return __thermal_cooling_device_register(NULL, type, devdata, ops);
1470 }
1471 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1472 
1473 /**
1474  * thermal_of_cooling_device_register() - register an OF thermal cooling device
1475  * @np:		a pointer to a device tree node.
1476  * @type:	the thermal cooling device type.
1477  * @devdata:	device private data.
1478  * @ops:		standard thermal cooling devices callbacks.
1479  *
1480  * This function will register a cooling device with device tree node reference.
1481  * This interface function adds a new thermal cooling device (fan/processor/...)
1482  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1483  * to all the thermal zone devices registered at the same time.
1484  *
1485  * Return: a pointer to the created struct thermal_cooling_device or an
1486  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1487  */
1488 struct thermal_cooling_device *
1489 thermal_of_cooling_device_register(struct device_node *np,
1490 				   char *type, void *devdata,
1491 				   const struct thermal_cooling_device_ops *ops)
1492 {
1493 	return __thermal_cooling_device_register(np, type, devdata, ops);
1494 }
1495 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1496 
1497 /**
1498  * thermal_cooling_device_unregister - removes the registered thermal cooling device
1499  * @cdev:	the thermal cooling device to remove.
1500  *
1501  * thermal_cooling_device_unregister() must be called when the device is no
1502  * longer needed.
1503  */
1504 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1505 {
1506 	int i;
1507 	const struct thermal_zone_params *tzp;
1508 	struct thermal_zone_device *tz;
1509 	struct thermal_cooling_device *pos = NULL;
1510 
1511 	if (!cdev)
1512 		return;
1513 
1514 	mutex_lock(&thermal_list_lock);
1515 	list_for_each_entry(pos, &thermal_cdev_list, node)
1516 	    if (pos == cdev)
1517 		break;
1518 	if (pos != cdev) {
1519 		/* thermal cooling device not found */
1520 		mutex_unlock(&thermal_list_lock);
1521 		return;
1522 	}
1523 	list_del(&cdev->node);
1524 
1525 	/* Unbind all thermal zones associated with 'this' cdev */
1526 	list_for_each_entry(tz, &thermal_tz_list, node) {
1527 		if (tz->ops->unbind) {
1528 			tz->ops->unbind(tz, cdev);
1529 			continue;
1530 		}
1531 
1532 		if (!tz->tzp || !tz->tzp->tbp)
1533 			continue;
1534 
1535 		tzp = tz->tzp;
1536 		for (i = 0; i < tzp->num_tbps; i++) {
1537 			if (tzp->tbp[i].cdev == cdev) {
1538 				__unbind(tz, tzp->tbp[i].trip_mask, cdev);
1539 				tzp->tbp[i].cdev = NULL;
1540 			}
1541 		}
1542 	}
1543 
1544 	mutex_unlock(&thermal_list_lock);
1545 
1546 	if (cdev->type[0])
1547 		device_remove_file(&cdev->device, &dev_attr_cdev_type);
1548 	device_remove_file(&cdev->device, &dev_attr_max_state);
1549 	device_remove_file(&cdev->device, &dev_attr_cur_state);
1550 
1551 	release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1552 	device_unregister(&cdev->device);
1553 	return;
1554 }
1555 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1556 
1557 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1558 {
1559 	struct thermal_instance *instance;
1560 	unsigned long target = 0;
1561 
1562 	/* cooling device is updated*/
1563 	if (cdev->updated)
1564 		return;
1565 
1566 	mutex_lock(&cdev->lock);
1567 	/* Make sure cdev enters the deepest cooling state */
1568 	list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1569 		dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1570 				instance->tz->id, instance->target);
1571 		if (instance->target == THERMAL_NO_TARGET)
1572 			continue;
1573 		if (instance->target > target)
1574 			target = instance->target;
1575 	}
1576 	mutex_unlock(&cdev->lock);
1577 	cdev->ops->set_cur_state(cdev, target);
1578 	cdev->updated = true;
1579 	trace_cdev_update(cdev, target);
1580 	dev_dbg(&cdev->device, "set to state %lu\n", target);
1581 }
1582 EXPORT_SYMBOL(thermal_cdev_update);
1583 
1584 /**
1585  * thermal_notify_framework - Sensor drivers use this API to notify framework
1586  * @tz:		thermal zone device
1587  * @trip:	indicates which trip point has been crossed
1588  *
1589  * This function handles the trip events from sensor drivers. It starts
1590  * throttling the cooling devices according to the policy configured.
1591  * For CRITICAL and HOT trip points, this notifies the respective drivers,
1592  * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1593  * The throttling policy is based on the configured platform data; if no
1594  * platform data is provided, this uses the step_wise throttling policy.
1595  */
1596 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1597 {
1598 	handle_thermal_trip(tz, trip);
1599 }
1600 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1601 
1602 /**
1603  * create_trip_attrs() - create attributes for trip points
1604  * @tz:		the thermal zone device
1605  * @mask:	Writeable trip point bitmap.
1606  *
1607  * helper function to instantiate sysfs entries for every trip
1608  * point and its properties of a struct thermal_zone_device.
1609  *
1610  * Return: 0 on success, the proper error value otherwise.
1611  */
1612 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1613 {
1614 	int indx;
1615 	int size = sizeof(struct thermal_attr) * tz->trips;
1616 
1617 	tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1618 	if (!tz->trip_type_attrs)
1619 		return -ENOMEM;
1620 
1621 	tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1622 	if (!tz->trip_temp_attrs) {
1623 		kfree(tz->trip_type_attrs);
1624 		return -ENOMEM;
1625 	}
1626 
1627 	if (tz->ops->get_trip_hyst) {
1628 		tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1629 		if (!tz->trip_hyst_attrs) {
1630 			kfree(tz->trip_type_attrs);
1631 			kfree(tz->trip_temp_attrs);
1632 			return -ENOMEM;
1633 		}
1634 	}
1635 
1636 
1637 	for (indx = 0; indx < tz->trips; indx++) {
1638 		/* create trip type attribute */
1639 		snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1640 			 "trip_point_%d_type", indx);
1641 
1642 		sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1643 		tz->trip_type_attrs[indx].attr.attr.name =
1644 						tz->trip_type_attrs[indx].name;
1645 		tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1646 		tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1647 
1648 		device_create_file(&tz->device,
1649 				   &tz->trip_type_attrs[indx].attr);
1650 
1651 		/* create trip temp attribute */
1652 		snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1653 			 "trip_point_%d_temp", indx);
1654 
1655 		sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1656 		tz->trip_temp_attrs[indx].attr.attr.name =
1657 						tz->trip_temp_attrs[indx].name;
1658 		tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1659 		tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1660 		if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
1661 		    mask & (1 << indx)) {
1662 			tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1663 			tz->trip_temp_attrs[indx].attr.store =
1664 							trip_point_temp_store;
1665 		}
1666 
1667 		device_create_file(&tz->device,
1668 				   &tz->trip_temp_attrs[indx].attr);
1669 
1670 		/* create Optional trip hyst attribute */
1671 		if (!tz->ops->get_trip_hyst)
1672 			continue;
1673 		snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1674 			 "trip_point_%d_hyst", indx);
1675 
1676 		sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1677 		tz->trip_hyst_attrs[indx].attr.attr.name =
1678 					tz->trip_hyst_attrs[indx].name;
1679 		tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1680 		tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1681 		if (tz->ops->set_trip_hyst) {
1682 			tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1683 			tz->trip_hyst_attrs[indx].attr.store =
1684 					trip_point_hyst_store;
1685 		}
1686 
1687 		device_create_file(&tz->device,
1688 				   &tz->trip_hyst_attrs[indx].attr);
1689 	}
1690 	return 0;
1691 }
1692 
1693 static void remove_trip_attrs(struct thermal_zone_device *tz)
1694 {
1695 	int indx;
1696 
1697 	for (indx = 0; indx < tz->trips; indx++) {
1698 		device_remove_file(&tz->device,
1699 				   &tz->trip_type_attrs[indx].attr);
1700 		device_remove_file(&tz->device,
1701 				   &tz->trip_temp_attrs[indx].attr);
1702 		if (tz->ops->get_trip_hyst)
1703 			device_remove_file(&tz->device,
1704 				  &tz->trip_hyst_attrs[indx].attr);
1705 	}
1706 	kfree(tz->trip_type_attrs);
1707 	kfree(tz->trip_temp_attrs);
1708 	kfree(tz->trip_hyst_attrs);
1709 }
1710 
1711 /**
1712  * thermal_zone_device_register() - register a new thermal zone device
1713  * @type:	the thermal zone device type
1714  * @trips:	the number of trip points the thermal zone support
1715  * @mask:	a bit string indicating the writeablility of trip points
1716  * @devdata:	private device data
1717  * @ops:	standard thermal zone device callbacks
1718  * @tzp:	thermal zone platform parameters
1719  * @passive_delay: number of milliseconds to wait between polls when
1720  *		   performing passive cooling
1721  * @polling_delay: number of milliseconds to wait between polls when checking
1722  *		   whether trip points have been crossed (0 for interrupt
1723  *		   driven systems)
1724  *
1725  * This interface function adds a new thermal zone device (sensor) to
1726  * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1727  * thermal cooling devices registered at the same time.
1728  * thermal_zone_device_unregister() must be called when the device is no
1729  * longer needed. The passive cooling depends on the .get_trend() return value.
1730  *
1731  * Return: a pointer to the created struct thermal_zone_device or an
1732  * in case of error, an ERR_PTR. Caller must check return value with
1733  * IS_ERR*() helpers.
1734  */
1735 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1736 	int trips, int mask, void *devdata,
1737 	struct thermal_zone_device_ops *ops,
1738 	struct thermal_zone_params *tzp,
1739 	int passive_delay, int polling_delay)
1740 {
1741 	struct thermal_zone_device *tz;
1742 	enum thermal_trip_type trip_type;
1743 	int result;
1744 	int count;
1745 	int passive = 0;
1746 	struct thermal_governor *governor;
1747 
1748 	if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1749 		return ERR_PTR(-EINVAL);
1750 
1751 	if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1752 		return ERR_PTR(-EINVAL);
1753 
1754 	if (!ops)
1755 		return ERR_PTR(-EINVAL);
1756 
1757 	if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1758 		return ERR_PTR(-EINVAL);
1759 
1760 	tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1761 	if (!tz)
1762 		return ERR_PTR(-ENOMEM);
1763 
1764 	INIT_LIST_HEAD(&tz->thermal_instances);
1765 	idr_init(&tz->idr);
1766 	mutex_init(&tz->lock);
1767 	result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1768 	if (result) {
1769 		kfree(tz);
1770 		return ERR_PTR(result);
1771 	}
1772 
1773 	strlcpy(tz->type, type ? : "", sizeof(tz->type));
1774 	tz->ops = ops;
1775 	tz->tzp = tzp;
1776 	tz->device.class = &thermal_class;
1777 	tz->devdata = devdata;
1778 	tz->trips = trips;
1779 	tz->passive_delay = passive_delay;
1780 	tz->polling_delay = polling_delay;
1781 
1782 	dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1783 	result = device_register(&tz->device);
1784 	if (result) {
1785 		release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1786 		kfree(tz);
1787 		return ERR_PTR(result);
1788 	}
1789 
1790 	/* sys I/F */
1791 	if (type) {
1792 		result = device_create_file(&tz->device, &dev_attr_type);
1793 		if (result)
1794 			goto unregister;
1795 	}
1796 
1797 	result = device_create_file(&tz->device, &dev_attr_temp);
1798 	if (result)
1799 		goto unregister;
1800 
1801 	if (ops->get_mode) {
1802 		result = device_create_file(&tz->device, &dev_attr_mode);
1803 		if (result)
1804 			goto unregister;
1805 	}
1806 
1807 	result = create_trip_attrs(tz, mask);
1808 	if (result)
1809 		goto unregister;
1810 
1811 	for (count = 0; count < trips; count++) {
1812 		tz->ops->get_trip_type(tz, count, &trip_type);
1813 		if (trip_type == THERMAL_TRIP_PASSIVE)
1814 			passive = 1;
1815 	}
1816 
1817 	if (!passive) {
1818 		result = device_create_file(&tz->device, &dev_attr_passive);
1819 		if (result)
1820 			goto unregister;
1821 	}
1822 
1823 	if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
1824 		result = device_create_file(&tz->device, &dev_attr_emul_temp);
1825 		if (result)
1826 			goto unregister;
1827 	}
1828 
1829 	/* Create policy attribute */
1830 	result = device_create_file(&tz->device, &dev_attr_policy);
1831 	if (result)
1832 		goto unregister;
1833 
1834 	/* Add thermal zone params */
1835 	result = create_tzp_attrs(&tz->device);
1836 	if (result)
1837 		goto unregister;
1838 
1839 	/* Create available_policies attribute */
1840 	result = device_create_file(&tz->device, &dev_attr_available_policies);
1841 	if (result)
1842 		goto unregister;
1843 
1844 	/* Update 'this' zone's governor information */
1845 	mutex_lock(&thermal_governor_lock);
1846 
1847 	if (tz->tzp)
1848 		governor = __find_governor(tz->tzp->governor_name);
1849 	else
1850 		governor = def_governor;
1851 
1852 	result = thermal_set_governor(tz, governor);
1853 	if (result) {
1854 		mutex_unlock(&thermal_governor_lock);
1855 		goto unregister;
1856 	}
1857 
1858 	mutex_unlock(&thermal_governor_lock);
1859 
1860 	if (!tz->tzp || !tz->tzp->no_hwmon) {
1861 		result = thermal_add_hwmon_sysfs(tz);
1862 		if (result)
1863 			goto unregister;
1864 	}
1865 
1866 	mutex_lock(&thermal_list_lock);
1867 	list_add_tail(&tz->node, &thermal_tz_list);
1868 	mutex_unlock(&thermal_list_lock);
1869 
1870 	/* Bind cooling devices for this zone */
1871 	bind_tz(tz);
1872 
1873 	INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1874 
1875 	thermal_zone_device_update(tz);
1876 
1877 	return tz;
1878 
1879 unregister:
1880 	release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1881 	device_unregister(&tz->device);
1882 	return ERR_PTR(result);
1883 }
1884 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1885 
1886 /**
1887  * thermal_device_unregister - removes the registered thermal zone device
1888  * @tz: the thermal zone device to remove
1889  */
1890 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1891 {
1892 	int i;
1893 	const struct thermal_zone_params *tzp;
1894 	struct thermal_cooling_device *cdev;
1895 	struct thermal_zone_device *pos = NULL;
1896 
1897 	if (!tz)
1898 		return;
1899 
1900 	tzp = tz->tzp;
1901 
1902 	mutex_lock(&thermal_list_lock);
1903 	list_for_each_entry(pos, &thermal_tz_list, node)
1904 	    if (pos == tz)
1905 		break;
1906 	if (pos != tz) {
1907 		/* thermal zone device not found */
1908 		mutex_unlock(&thermal_list_lock);
1909 		return;
1910 	}
1911 	list_del(&tz->node);
1912 
1913 	/* Unbind all cdevs associated with 'this' thermal zone */
1914 	list_for_each_entry(cdev, &thermal_cdev_list, node) {
1915 		if (tz->ops->unbind) {
1916 			tz->ops->unbind(tz, cdev);
1917 			continue;
1918 		}
1919 
1920 		if (!tzp || !tzp->tbp)
1921 			break;
1922 
1923 		for (i = 0; i < tzp->num_tbps; i++) {
1924 			if (tzp->tbp[i].cdev == cdev) {
1925 				__unbind(tz, tzp->tbp[i].trip_mask, cdev);
1926 				tzp->tbp[i].cdev = NULL;
1927 			}
1928 		}
1929 	}
1930 
1931 	mutex_unlock(&thermal_list_lock);
1932 
1933 	thermal_zone_device_set_polling(tz, 0);
1934 
1935 	if (tz->type[0])
1936 		device_remove_file(&tz->device, &dev_attr_type);
1937 	device_remove_file(&tz->device, &dev_attr_temp);
1938 	if (tz->ops->get_mode)
1939 		device_remove_file(&tz->device, &dev_attr_mode);
1940 	device_remove_file(&tz->device, &dev_attr_policy);
1941 	device_remove_file(&tz->device, &dev_attr_available_policies);
1942 	remove_trip_attrs(tz);
1943 	thermal_set_governor(tz, NULL);
1944 
1945 	thermal_remove_hwmon_sysfs(tz);
1946 	release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1947 	idr_destroy(&tz->idr);
1948 	mutex_destroy(&tz->lock);
1949 	device_unregister(&tz->device);
1950 	return;
1951 }
1952 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1953 
1954 /**
1955  * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1956  * @name: thermal zone name to fetch the temperature
1957  *
1958  * When only one zone is found with the passed name, returns a reference to it.
1959  *
1960  * Return: On success returns a reference to an unique thermal zone with
1961  * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1962  * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1963  */
1964 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1965 {
1966 	struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1967 	unsigned int found = 0;
1968 
1969 	if (!name)
1970 		goto exit;
1971 
1972 	mutex_lock(&thermal_list_lock);
1973 	list_for_each_entry(pos, &thermal_tz_list, node)
1974 		if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1975 			found++;
1976 			ref = pos;
1977 		}
1978 	mutex_unlock(&thermal_list_lock);
1979 
1980 	/* nothing has been found, thus an error code for it */
1981 	if (found == 0)
1982 		ref = ERR_PTR(-ENODEV);
1983 	else if (found > 1)
1984 	/* Success only when an unique zone is found */
1985 		ref = ERR_PTR(-EEXIST);
1986 
1987 exit:
1988 	return ref;
1989 }
1990 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1991 
1992 #ifdef CONFIG_NET
1993 static const struct genl_multicast_group thermal_event_mcgrps[] = {
1994 	{ .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1995 };
1996 
1997 static struct genl_family thermal_event_genl_family = {
1998 	.id = GENL_ID_GENERATE,
1999 	.name = THERMAL_GENL_FAMILY_NAME,
2000 	.version = THERMAL_GENL_VERSION,
2001 	.maxattr = THERMAL_GENL_ATTR_MAX,
2002 	.mcgrps = thermal_event_mcgrps,
2003 	.n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
2004 };
2005 
2006 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2007 					enum events event)
2008 {
2009 	struct sk_buff *skb;
2010 	struct nlattr *attr;
2011 	struct thermal_genl_event *thermal_event;
2012 	void *msg_header;
2013 	int size;
2014 	int result;
2015 	static unsigned int thermal_event_seqnum;
2016 
2017 	if (!tz)
2018 		return -EINVAL;
2019 
2020 	/* allocate memory */
2021 	size = nla_total_size(sizeof(struct thermal_genl_event)) +
2022 	       nla_total_size(0);
2023 
2024 	skb = genlmsg_new(size, GFP_ATOMIC);
2025 	if (!skb)
2026 		return -ENOMEM;
2027 
2028 	/* add the genetlink message header */
2029 	msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2030 				 &thermal_event_genl_family, 0,
2031 				 THERMAL_GENL_CMD_EVENT);
2032 	if (!msg_header) {
2033 		nlmsg_free(skb);
2034 		return -ENOMEM;
2035 	}
2036 
2037 	/* fill the data */
2038 	attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2039 			   sizeof(struct thermal_genl_event));
2040 
2041 	if (!attr) {
2042 		nlmsg_free(skb);
2043 		return -EINVAL;
2044 	}
2045 
2046 	thermal_event = nla_data(attr);
2047 	if (!thermal_event) {
2048 		nlmsg_free(skb);
2049 		return -EINVAL;
2050 	}
2051 
2052 	memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2053 
2054 	thermal_event->orig = tz->id;
2055 	thermal_event->event = event;
2056 
2057 	/* send multicast genetlink message */
2058 	genlmsg_end(skb, msg_header);
2059 
2060 	result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
2061 				   0, GFP_ATOMIC);
2062 	if (result)
2063 		dev_err(&tz->device, "Failed to send netlink event:%d", result);
2064 
2065 	return result;
2066 }
2067 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
2068 
2069 static int genetlink_init(void)
2070 {
2071 	return genl_register_family(&thermal_event_genl_family);
2072 }
2073 
2074 static void genetlink_exit(void)
2075 {
2076 	genl_unregister_family(&thermal_event_genl_family);
2077 }
2078 #else /* !CONFIG_NET */
2079 static inline int genetlink_init(void) { return 0; }
2080 static inline void genetlink_exit(void) {}
2081 #endif /* !CONFIG_NET */
2082 
2083 static int __init thermal_register_governors(void)
2084 {
2085 	int result;
2086 
2087 	result = thermal_gov_step_wise_register();
2088 	if (result)
2089 		return result;
2090 
2091 	result = thermal_gov_fair_share_register();
2092 	if (result)
2093 		return result;
2094 
2095 	result = thermal_gov_bang_bang_register();
2096 	if (result)
2097 		return result;
2098 
2099 	result = thermal_gov_user_space_register();
2100 	if (result)
2101 		return result;
2102 
2103 	return thermal_gov_power_allocator_register();
2104 }
2105 
2106 static void thermal_unregister_governors(void)
2107 {
2108 	thermal_gov_step_wise_unregister();
2109 	thermal_gov_fair_share_unregister();
2110 	thermal_gov_bang_bang_unregister();
2111 	thermal_gov_user_space_unregister();
2112 	thermal_gov_power_allocator_unregister();
2113 }
2114 
2115 static int __init thermal_init(void)
2116 {
2117 	int result;
2118 
2119 	result = thermal_register_governors();
2120 	if (result)
2121 		goto error;
2122 
2123 	result = class_register(&thermal_class);
2124 	if (result)
2125 		goto unregister_governors;
2126 
2127 	result = genetlink_init();
2128 	if (result)
2129 		goto unregister_class;
2130 
2131 	result = of_parse_thermal_zones();
2132 	if (result)
2133 		goto exit_netlink;
2134 
2135 	return 0;
2136 
2137 exit_netlink:
2138 	genetlink_exit();
2139 unregister_class:
2140 	class_unregister(&thermal_class);
2141 unregister_governors:
2142 	thermal_unregister_governors();
2143 error:
2144 	idr_destroy(&thermal_tz_idr);
2145 	idr_destroy(&thermal_cdev_idr);
2146 	mutex_destroy(&thermal_idr_lock);
2147 	mutex_destroy(&thermal_list_lock);
2148 	mutex_destroy(&thermal_governor_lock);
2149 	return result;
2150 }
2151 
2152 static void __exit thermal_exit(void)
2153 {
2154 	of_thermal_destroy_zones();
2155 	genetlink_exit();
2156 	class_unregister(&thermal_class);
2157 	thermal_unregister_governors();
2158 	idr_destroy(&thermal_tz_idr);
2159 	idr_destroy(&thermal_cdev_idr);
2160 	mutex_destroy(&thermal_idr_lock);
2161 	mutex_destroy(&thermal_list_lock);
2162 	mutex_destroy(&thermal_governor_lock);
2163 }
2164 
2165 fs_initcall(thermal_init);
2166 module_exit(thermal_exit);
2167