1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/drivers/thermal/cpufreq_cooling.c
4  *
5  *  Copyright (C) 2012	Samsung Electronics Co., Ltd(http://www.samsung.com)
6  *
7  *  Copyright (C) 2012-2018 Linaro Limited.
8  *
9  *  Authors:	Amit Daniel <amit.kachhap@linaro.org>
10  *		Viresh Kumar <viresh.kumar@linaro.org>
11  *
12  */
13 #include <linux/cpu.h>
14 #include <linux/cpufreq.h>
15 #include <linux/cpu_cooling.h>
16 #include <linux/device.h>
17 #include <linux/energy_model.h>
18 #include <linux/err.h>
19 #include <linux/export.h>
20 #include <linux/pm_opp.h>
21 #include <linux/pm_qos.h>
22 #include <linux/slab.h>
23 #include <linux/thermal.h>
24 
25 #include <trace/events/thermal.h>
26 
27 /*
28  * Cooling state <-> CPUFreq frequency
29  *
30  * Cooling states are translated to frequencies throughout this driver and this
31  * is the relation between them.
32  *
33  * Highest cooling state corresponds to lowest possible frequency.
34  *
35  * i.e.
36  *	level 0 --> 1st Max Freq
37  *	level 1 --> 2nd Max Freq
38  *	...
39  */
40 
41 /**
42  * struct time_in_idle - Idle time stats
43  * @time: previous reading of the absolute time that this cpu was idle
44  * @timestamp: wall time of the last invocation of get_cpu_idle_time_us()
45  */
46 struct time_in_idle {
47 	u64 time;
48 	u64 timestamp;
49 };
50 
51 /**
52  * struct cpufreq_cooling_device - data for cooling device with cpufreq
53  * @last_load: load measured by the latest call to cpufreq_get_requested_power()
54  * @cpufreq_state: integer value representing the current state of cpufreq
55  *	cooling	devices.
56  * @max_level: maximum cooling level. One less than total number of valid
57  *	cpufreq frequencies.
58  * @em: Reference on the Energy Model of the device
59  * @cdev: thermal_cooling_device pointer to keep track of the
60  *	registered cooling device.
61  * @policy: cpufreq policy.
62  * @cooling_ops: cpufreq callbacks to thermal cooling device ops
63  * @idle_time: idle time stats
64  * @qos_req: PM QoS contraint to apply
65  *
66  * This structure is required for keeping information of each registered
67  * cpufreq_cooling_device.
68  */
69 struct cpufreq_cooling_device {
70 	u32 last_load;
71 	unsigned int cpufreq_state;
72 	unsigned int max_level;
73 	struct em_perf_domain *em;
74 	struct cpufreq_policy *policy;
75 	struct thermal_cooling_device_ops cooling_ops;
76 #ifndef CONFIG_SMP
77 	struct time_in_idle *idle_time;
78 #endif
79 	struct freq_qos_request qos_req;
80 };
81 
82 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
83 /**
84  * get_level: Find the level for a particular frequency
85  * @cpufreq_cdev: cpufreq_cdev for which the property is required
86  * @freq: Frequency
87  *
88  * Return: level corresponding to the frequency.
89  */
90 static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
91 			       unsigned int freq)
92 {
93 	int i;
94 
95 	for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
96 		if (freq > cpufreq_cdev->em->table[i].frequency)
97 			break;
98 	}
99 
100 	return cpufreq_cdev->max_level - i - 1;
101 }
102 
103 static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
104 			     u32 freq)
105 {
106 	int i;
107 
108 	for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
109 		if (freq > cpufreq_cdev->em->table[i].frequency)
110 			break;
111 	}
112 
113 	return cpufreq_cdev->em->table[i + 1].power;
114 }
115 
116 static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
117 			     u32 power)
118 {
119 	int i;
120 
121 	for (i = cpufreq_cdev->max_level; i > 0; i--) {
122 		if (power >= cpufreq_cdev->em->table[i].power)
123 			break;
124 	}
125 
126 	return cpufreq_cdev->em->table[i].frequency;
127 }
128 
129 /**
130  * get_load() - get load for a cpu
131  * @cpufreq_cdev: struct cpufreq_cooling_device for the cpu
132  * @cpu: cpu number
133  * @cpu_idx: index of the cpu in time_in_idle array
134  *
135  * Return: The average load of cpu @cpu in percentage since this
136  * function was last called.
137  */
138 #ifdef CONFIG_SMP
139 static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
140 		    int cpu_idx)
141 {
142 	unsigned long max = arch_scale_cpu_capacity(cpu);
143 	unsigned long util;
144 
145 	util = sched_cpu_util(cpu, max);
146 	return (util * 100) / max;
147 }
148 #else /* !CONFIG_SMP */
149 static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
150 		    int cpu_idx)
151 {
152 	u32 load;
153 	u64 now, now_idle, delta_time, delta_idle;
154 	struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
155 
156 	now_idle = get_cpu_idle_time(cpu, &now, 0);
157 	delta_idle = now_idle - idle_time->time;
158 	delta_time = now - idle_time->timestamp;
159 
160 	if (delta_time <= delta_idle)
161 		load = 0;
162 	else
163 		load = div64_u64(100 * (delta_time - delta_idle), delta_time);
164 
165 	idle_time->time = now_idle;
166 	idle_time->timestamp = now;
167 
168 	return load;
169 }
170 #endif /* CONFIG_SMP */
171 
172 /**
173  * get_dynamic_power() - calculate the dynamic power
174  * @cpufreq_cdev:	&cpufreq_cooling_device for this cdev
175  * @freq:	current frequency
176  *
177  * Return: the dynamic power consumed by the cpus described by
178  * @cpufreq_cdev.
179  */
180 static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
181 			     unsigned long freq)
182 {
183 	u32 raw_cpu_power;
184 
185 	raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
186 	return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
187 }
188 
189 /**
190  * cpufreq_get_requested_power() - get the current power
191  * @cdev:	&thermal_cooling_device pointer
192  * @power:	pointer in which to store the resulting power
193  *
194  * Calculate the current power consumption of the cpus in milliwatts
195  * and store it in @power.  This function should actually calculate
196  * the requested power, but it's hard to get the frequency that
197  * cpufreq would have assigned if there were no thermal limits.
198  * Instead, we calculate the current power on the assumption that the
199  * immediate future will look like the immediate past.
200  *
201  * We use the current frequency and the average load since this
202  * function was last called.  In reality, there could have been
203  * multiple opps since this function was last called and that affects
204  * the load calculation.  While it's not perfectly accurate, this
205  * simplification is good enough and works.  REVISIT this, as more
206  * complex code may be needed if experiments show that it's not
207  * accurate enough.
208  *
209  * Return: 0 on success, -E* if getting the static power failed.
210  */
211 static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
212 				       u32 *power)
213 {
214 	unsigned long freq;
215 	int i = 0, cpu;
216 	u32 total_load = 0;
217 	struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
218 	struct cpufreq_policy *policy = cpufreq_cdev->policy;
219 	u32 *load_cpu = NULL;
220 
221 	freq = cpufreq_quick_get(policy->cpu);
222 
223 	if (trace_thermal_power_cpu_get_power_enabled()) {
224 		u32 ncpus = cpumask_weight(policy->related_cpus);
225 
226 		load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
227 	}
228 
229 	for_each_cpu(cpu, policy->related_cpus) {
230 		u32 load;
231 
232 		if (cpu_online(cpu))
233 			load = get_load(cpufreq_cdev, cpu, i);
234 		else
235 			load = 0;
236 
237 		total_load += load;
238 		if (load_cpu)
239 			load_cpu[i] = load;
240 
241 		i++;
242 	}
243 
244 	cpufreq_cdev->last_load = total_load;
245 
246 	*power = get_dynamic_power(cpufreq_cdev, freq);
247 
248 	if (load_cpu) {
249 		trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
250 						  load_cpu, i, *power);
251 
252 		kfree(load_cpu);
253 	}
254 
255 	return 0;
256 }
257 
258 /**
259  * cpufreq_state2power() - convert a cpu cdev state to power consumed
260  * @cdev:	&thermal_cooling_device pointer
261  * @state:	cooling device state to be converted
262  * @power:	pointer in which to store the resulting power
263  *
264  * Convert cooling device state @state into power consumption in
265  * milliwatts assuming 100% load.  Store the calculated power in
266  * @power.
267  *
268  * Return: 0 on success, -EINVAL if the cooling device state could not
269  * be converted into a frequency or other -E* if there was an error
270  * when calculating the static power.
271  */
272 static int cpufreq_state2power(struct thermal_cooling_device *cdev,
273 			       unsigned long state, u32 *power)
274 {
275 	unsigned int freq, num_cpus, idx;
276 	struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
277 
278 	/* Request state should be less than max_level */
279 	if (state > cpufreq_cdev->max_level)
280 		return -EINVAL;
281 
282 	num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
283 
284 	idx = cpufreq_cdev->max_level - state;
285 	freq = cpufreq_cdev->em->table[idx].frequency;
286 	*power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
287 
288 	return 0;
289 }
290 
291 /**
292  * cpufreq_power2state() - convert power to a cooling device state
293  * @cdev:	&thermal_cooling_device pointer
294  * @power:	power in milliwatts to be converted
295  * @state:	pointer in which to store the resulting state
296  *
297  * Calculate a cooling device state for the cpus described by @cdev
298  * that would allow them to consume at most @power mW and store it in
299  * @state.  Note that this calculation depends on external factors
300  * such as the cpu load or the current static power.  Calling this
301  * function with the same power as input can yield different cooling
302  * device states depending on those external factors.
303  *
304  * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
305  * the calculated frequency could not be converted to a valid state.
306  * The latter should not happen unless the frequencies available to
307  * cpufreq have changed since the initialization of the cpu cooling
308  * device.
309  */
310 static int cpufreq_power2state(struct thermal_cooling_device *cdev,
311 			       u32 power, unsigned long *state)
312 {
313 	unsigned int target_freq;
314 	u32 last_load, normalised_power;
315 	struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
316 	struct cpufreq_policy *policy = cpufreq_cdev->policy;
317 
318 	last_load = cpufreq_cdev->last_load ?: 1;
319 	normalised_power = (power * 100) / last_load;
320 	target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
321 
322 	*state = get_level(cpufreq_cdev, target_freq);
323 	trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
324 				      power);
325 	return 0;
326 }
327 
328 static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev,
329 			      struct em_perf_domain *em) {
330 	struct cpufreq_policy *policy;
331 	unsigned int nr_levels;
332 
333 	if (!em || em_is_artificial(em))
334 		return false;
335 
336 	policy = cpufreq_cdev->policy;
337 	if (!cpumask_equal(policy->related_cpus, em_span_cpus(em))) {
338 		pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n",
339 			cpumask_pr_args(em_span_cpus(em)),
340 			cpumask_pr_args(policy->related_cpus));
341 		return false;
342 	}
343 
344 	nr_levels = cpufreq_cdev->max_level + 1;
345 	if (em_pd_nr_perf_states(em) != nr_levels) {
346 		pr_err("The number of performance states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n",
347 			cpumask_pr_args(em_span_cpus(em)),
348 			em_pd_nr_perf_states(em), nr_levels);
349 		return false;
350 	}
351 
352 	return true;
353 }
354 #endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */
355 
356 #ifdef CONFIG_SMP
357 static inline int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
358 {
359 	return 0;
360 }
361 
362 static inline void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
363 {
364 }
365 #else
366 static int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
367 {
368 	unsigned int num_cpus = cpumask_weight(cpufreq_cdev->policy->related_cpus);
369 
370 	cpufreq_cdev->idle_time = kcalloc(num_cpus,
371 					  sizeof(*cpufreq_cdev->idle_time),
372 					  GFP_KERNEL);
373 	if (!cpufreq_cdev->idle_time)
374 		return -ENOMEM;
375 
376 	return 0;
377 }
378 
379 static void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
380 {
381 	kfree(cpufreq_cdev->idle_time);
382 	cpufreq_cdev->idle_time = NULL;
383 }
384 #endif /* CONFIG_SMP */
385 
386 static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
387 				   unsigned long state)
388 {
389 	struct cpufreq_policy *policy;
390 	unsigned long idx;
391 
392 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
393 	/* Use the Energy Model table if available */
394 	if (cpufreq_cdev->em) {
395 		idx = cpufreq_cdev->max_level - state;
396 		return cpufreq_cdev->em->table[idx].frequency;
397 	}
398 #endif
399 
400 	/* Otherwise, fallback on the CPUFreq table */
401 	policy = cpufreq_cdev->policy;
402 	if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
403 		idx = cpufreq_cdev->max_level - state;
404 	else
405 		idx = state;
406 
407 	return policy->freq_table[idx].frequency;
408 }
409 
410 /* cpufreq cooling device callback functions are defined below */
411 
412 /**
413  * cpufreq_get_max_state - callback function to get the max cooling state.
414  * @cdev: thermal cooling device pointer.
415  * @state: fill this variable with the max cooling state.
416  *
417  * Callback for the thermal cooling device to return the cpufreq
418  * max cooling state.
419  *
420  * Return: 0 on success, an error code otherwise.
421  */
422 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
423 				 unsigned long *state)
424 {
425 	struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
426 
427 	*state = cpufreq_cdev->max_level;
428 	return 0;
429 }
430 
431 /**
432  * cpufreq_get_cur_state - callback function to get the current cooling state.
433  * @cdev: thermal cooling device pointer.
434  * @state: fill this variable with the current cooling state.
435  *
436  * Callback for the thermal cooling device to return the cpufreq
437  * current cooling state.
438  *
439  * Return: 0 on success, an error code otherwise.
440  */
441 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
442 				 unsigned long *state)
443 {
444 	struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
445 
446 	*state = cpufreq_cdev->cpufreq_state;
447 
448 	return 0;
449 }
450 
451 /**
452  * cpufreq_set_cur_state - callback function to set the current cooling state.
453  * @cdev: thermal cooling device pointer.
454  * @state: set this variable to the current cooling state.
455  *
456  * Callback for the thermal cooling device to change the cpufreq
457  * current cooling state.
458  *
459  * Return: 0 on success, an error code otherwise.
460  */
461 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
462 				 unsigned long state)
463 {
464 	struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
465 	struct cpumask *cpus;
466 	unsigned int frequency;
467 	int ret;
468 
469 	/* Request state should be less than max_level */
470 	if (state > cpufreq_cdev->max_level)
471 		return -EINVAL;
472 
473 	/* Check if the old cooling action is same as new cooling action */
474 	if (cpufreq_cdev->cpufreq_state == state)
475 		return 0;
476 
477 	frequency = get_state_freq(cpufreq_cdev, state);
478 
479 	ret = freq_qos_update_request(&cpufreq_cdev->qos_req, frequency);
480 	if (ret >= 0) {
481 		cpufreq_cdev->cpufreq_state = state;
482 		cpus = cpufreq_cdev->policy->related_cpus;
483 		arch_update_thermal_pressure(cpus, frequency);
484 		ret = 0;
485 	}
486 
487 	return ret;
488 }
489 
490 /**
491  * __cpufreq_cooling_register - helper function to create cpufreq cooling device
492  * @np: a valid struct device_node to the cooling device device tree node
493  * @policy: cpufreq policy
494  * Normally this should be same as cpufreq policy->related_cpus.
495  * @em: Energy Model of the cpufreq policy
496  *
497  * This interface function registers the cpufreq cooling device with the name
498  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
499  * cooling devices. It also gives the opportunity to link the cooling device
500  * with a device tree node, in order to bind it via the thermal DT code.
501  *
502  * Return: a valid struct thermal_cooling_device pointer on success,
503  * on failure, it returns a corresponding ERR_PTR().
504  */
505 static struct thermal_cooling_device *
506 __cpufreq_cooling_register(struct device_node *np,
507 			struct cpufreq_policy *policy,
508 			struct em_perf_domain *em)
509 {
510 	struct thermal_cooling_device *cdev;
511 	struct cpufreq_cooling_device *cpufreq_cdev;
512 	unsigned int i;
513 	struct device *dev;
514 	int ret;
515 	struct thermal_cooling_device_ops *cooling_ops;
516 	char *name;
517 
518 	dev = get_cpu_device(policy->cpu);
519 	if (unlikely(!dev)) {
520 		pr_warn("No cpu device for cpu %d\n", policy->cpu);
521 		return ERR_PTR(-ENODEV);
522 	}
523 
524 	if (IS_ERR_OR_NULL(policy)) {
525 		pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
526 		return ERR_PTR(-EINVAL);
527 	}
528 
529 	i = cpufreq_table_count_valid_entries(policy);
530 	if (!i) {
531 		pr_debug("%s: CPUFreq table not found or has no valid entries\n",
532 			 __func__);
533 		return ERR_PTR(-ENODEV);
534 	}
535 
536 	cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
537 	if (!cpufreq_cdev)
538 		return ERR_PTR(-ENOMEM);
539 
540 	cpufreq_cdev->policy = policy;
541 
542 	ret = allocate_idle_time(cpufreq_cdev);
543 	if (ret) {
544 		cdev = ERR_PTR(ret);
545 		goto free_cdev;
546 	}
547 
548 	/* max_level is an index, not a counter */
549 	cpufreq_cdev->max_level = i - 1;
550 
551 	cooling_ops = &cpufreq_cdev->cooling_ops;
552 	cooling_ops->get_max_state = cpufreq_get_max_state;
553 	cooling_ops->get_cur_state = cpufreq_get_cur_state;
554 	cooling_ops->set_cur_state = cpufreq_set_cur_state;
555 
556 #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
557 	if (em_is_sane(cpufreq_cdev, em)) {
558 		cpufreq_cdev->em = em;
559 		cooling_ops->get_requested_power = cpufreq_get_requested_power;
560 		cooling_ops->state2power = cpufreq_state2power;
561 		cooling_ops->power2state = cpufreq_power2state;
562 	} else
563 #endif
564 	if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) {
565 		pr_err("%s: unsorted frequency tables are not supported\n",
566 		       __func__);
567 		cdev = ERR_PTR(-EINVAL);
568 		goto free_idle_time;
569 	}
570 
571 	ret = freq_qos_add_request(&policy->constraints,
572 				   &cpufreq_cdev->qos_req, FREQ_QOS_MAX,
573 				   get_state_freq(cpufreq_cdev, 0));
574 	if (ret < 0) {
575 		pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
576 		       ret);
577 		cdev = ERR_PTR(ret);
578 		goto free_idle_time;
579 	}
580 
581 	cdev = ERR_PTR(-ENOMEM);
582 	name = kasprintf(GFP_KERNEL, "cpufreq-%s", dev_name(dev));
583 	if (!name)
584 		goto remove_qos_req;
585 
586 	cdev = thermal_of_cooling_device_register(np, name, cpufreq_cdev,
587 						  cooling_ops);
588 	kfree(name);
589 
590 	if (IS_ERR(cdev))
591 		goto remove_qos_req;
592 
593 	return cdev;
594 
595 remove_qos_req:
596 	freq_qos_remove_request(&cpufreq_cdev->qos_req);
597 free_idle_time:
598 	free_idle_time(cpufreq_cdev);
599 free_cdev:
600 	kfree(cpufreq_cdev);
601 	return cdev;
602 }
603 
604 /**
605  * cpufreq_cooling_register - function to create cpufreq cooling device.
606  * @policy: cpufreq policy
607  *
608  * This interface function registers the cpufreq cooling device with the name
609  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
610  * cooling devices.
611  *
612  * Return: a valid struct thermal_cooling_device pointer on success,
613  * on failure, it returns a corresponding ERR_PTR().
614  */
615 struct thermal_cooling_device *
616 cpufreq_cooling_register(struct cpufreq_policy *policy)
617 {
618 	return __cpufreq_cooling_register(NULL, policy, NULL);
619 }
620 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
621 
622 /**
623  * of_cpufreq_cooling_register - function to create cpufreq cooling device.
624  * @policy: cpufreq policy
625  *
626  * This interface function registers the cpufreq cooling device with the name
627  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
628  * cooling devices. Using this API, the cpufreq cooling device will be
629  * linked to the device tree node provided.
630  *
631  * Using this function, the cooling device will implement the power
632  * extensions by using a simple cpu power model.  The cpus must have
633  * registered their OPPs using the OPP library.
634  *
635  * It also takes into account, if property present in policy CPU node, the
636  * static power consumed by the cpu.
637  *
638  * Return: a valid struct thermal_cooling_device pointer on success,
639  * and NULL on failure.
640  */
641 struct thermal_cooling_device *
642 of_cpufreq_cooling_register(struct cpufreq_policy *policy)
643 {
644 	struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
645 	struct thermal_cooling_device *cdev = NULL;
646 
647 	if (!np) {
648 		pr_err("cpufreq_cooling: OF node not available for cpu%d\n",
649 		       policy->cpu);
650 		return NULL;
651 	}
652 
653 	if (of_find_property(np, "#cooling-cells", NULL)) {
654 		struct em_perf_domain *em = em_cpu_get(policy->cpu);
655 
656 		cdev = __cpufreq_cooling_register(np, policy, em);
657 		if (IS_ERR(cdev)) {
658 			pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n",
659 			       policy->cpu, PTR_ERR(cdev));
660 			cdev = NULL;
661 		}
662 	}
663 
664 	of_node_put(np);
665 	return cdev;
666 }
667 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
668 
669 /**
670  * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
671  * @cdev: thermal cooling device pointer.
672  *
673  * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
674  */
675 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
676 {
677 	struct cpufreq_cooling_device *cpufreq_cdev;
678 
679 	if (!cdev)
680 		return;
681 
682 	cpufreq_cdev = cdev->devdata;
683 
684 	thermal_cooling_device_unregister(cdev);
685 	freq_qos_remove_request(&cpufreq_cdev->qos_req);
686 	free_idle_time(cpufreq_cdev);
687 	kfree(cpufreq_cdev);
688 }
689 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);
690