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