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