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