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