Commit | Line | Data |
---|---|---|
02361418 ADK |
1 | /* |
2 | * linux/drivers/thermal/cpu_cooling.c | |
3 | * | |
4 | * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com) | |
5 | * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org> | |
6 | * | |
73904cbc VK |
7 | * Copyright (C) 2014 Viresh Kumar <viresh.kumar@linaro.org> |
8 | * | |
02361418 ADK |
9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; version 2 of the License. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, but | |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License along | |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
22 | * | |
23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
24 | */ | |
02361418 ADK |
25 | #include <linux/module.h> |
26 | #include <linux/thermal.h> | |
02361418 ADK |
27 | #include <linux/cpufreq.h> |
28 | #include <linux/err.h> | |
c36cf071 | 29 | #include <linux/pm_opp.h> |
02361418 ADK |
30 | #include <linux/slab.h> |
31 | #include <linux/cpu.h> | |
32 | #include <linux/cpu_cooling.h> | |
33 | ||
6828a471 JM |
34 | #include <trace/events/thermal.h> |
35 | ||
07d888d8 VK |
36 | /* |
37 | * Cooling state <-> CPUFreq frequency | |
38 | * | |
39 | * Cooling states are translated to frequencies throughout this driver and this | |
40 | * is the relation between them. | |
41 | * | |
42 | * Highest cooling state corresponds to lowest possible frequency. | |
43 | * | |
44 | * i.e. | |
45 | * level 0 --> 1st Max Freq | |
46 | * level 1 --> 2nd Max Freq | |
47 | * ... | |
48 | */ | |
49 | ||
c36cf071 JM |
50 | /** |
51 | * struct power_table - frequency to power conversion | |
52 | * @frequency: frequency in KHz | |
53 | * @power: power in mW | |
54 | * | |
55 | * This structure is built when the cooling device registers and helps | |
56 | * in translating frequency to power and viceversa. | |
57 | */ | |
58 | struct power_table { | |
59 | u32 frequency; | |
60 | u32 power; | |
61 | }; | |
62 | ||
02361418 | 63 | /** |
3b3c0748 | 64 | * struct cpufreq_cooling_device - data for cooling device with cpufreq |
02361418 ADK |
65 | * @id: unique integer value corresponding to each cpufreq_cooling_device |
66 | * registered. | |
3b3c0748 EV |
67 | * @cool_dev: thermal_cooling_device pointer to keep track of the |
68 | * registered cooling device. | |
02361418 ADK |
69 | * @cpufreq_state: integer value representing the current state of cpufreq |
70 | * cooling devices. | |
71 | * @cpufreq_val: integer value representing the absolute value of the clipped | |
72 | * frequency. | |
dcc6c7fd VK |
73 | * @max_level: maximum cooling level. One less than total number of valid |
74 | * cpufreq frequencies. | |
02361418 | 75 | * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device. |
fc4de356 | 76 | * @node: list_head to link all cpufreq_cooling_device together. |
c36cf071 JM |
77 | * @last_load: load measured by the latest call to cpufreq_get_actual_power() |
78 | * @time_in_idle: previous reading of the absolute time that this cpu was idle | |
79 | * @time_in_idle_timestamp: wall time of the last invocation of | |
80 | * get_cpu_idle_time_us() | |
81 | * @dyn_power_table: array of struct power_table for frequency to power | |
82 | * conversion, sorted in ascending order. | |
83 | * @dyn_power_table_entries: number of entries in the @dyn_power_table array | |
84 | * @cpu_dev: the first cpu_device from @allowed_cpus that has OPPs registered | |
85 | * @plat_get_static_power: callback to calculate the static power | |
02361418 | 86 | * |
beca6053 VK |
87 | * This structure is required for keeping information of each registered |
88 | * cpufreq_cooling_device. | |
02361418 ADK |
89 | */ |
90 | struct cpufreq_cooling_device { | |
91 | int id; | |
92 | struct thermal_cooling_device *cool_dev; | |
93 | unsigned int cpufreq_state; | |
94 | unsigned int cpufreq_val; | |
dcc6c7fd | 95 | unsigned int max_level; |
f6859014 | 96 | unsigned int *freq_table; /* In descending order */ |
02361418 | 97 | struct cpumask allowed_cpus; |
2dcd851f | 98 | struct list_head node; |
c36cf071 JM |
99 | u32 last_load; |
100 | u64 *time_in_idle; | |
101 | u64 *time_in_idle_timestamp; | |
102 | struct power_table *dyn_power_table; | |
103 | int dyn_power_table_entries; | |
104 | struct device *cpu_dev; | |
105 | get_static_t plat_get_static_power; | |
02361418 | 106 | }; |
02361418 | 107 | static DEFINE_IDR(cpufreq_idr); |
160b7d80 | 108 | static DEFINE_MUTEX(cooling_cpufreq_lock); |
02361418 | 109 | |
2dcd851f | 110 | static LIST_HEAD(cpufreq_dev_list); |
02361418 ADK |
111 | |
112 | /** | |
113 | * get_idr - function to get a unique id. | |
114 | * @idr: struct idr * handle used to create a id. | |
115 | * @id: int * value generated by this function. | |
79491e53 EV |
116 | * |
117 | * This function will populate @id with an unique | |
118 | * id, using the idr API. | |
119 | * | |
120 | * Return: 0 on success, an error code on failure. | |
02361418 ADK |
121 | */ |
122 | static int get_idr(struct idr *idr, int *id) | |
123 | { | |
6deb69fa | 124 | int ret; |
02361418 ADK |
125 | |
126 | mutex_lock(&cooling_cpufreq_lock); | |
6deb69fa | 127 | ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL); |
02361418 | 128 | mutex_unlock(&cooling_cpufreq_lock); |
6deb69fa TH |
129 | if (unlikely(ret < 0)) |
130 | return ret; | |
131 | *id = ret; | |
79491e53 | 132 | |
02361418 ADK |
133 | return 0; |
134 | } | |
135 | ||
136 | /** | |
137 | * release_idr - function to free the unique id. | |
138 | * @idr: struct idr * handle used for creating the id. | |
139 | * @id: int value representing the unique id. | |
140 | */ | |
141 | static void release_idr(struct idr *idr, int id) | |
142 | { | |
143 | mutex_lock(&cooling_cpufreq_lock); | |
144 | idr_remove(idr, id); | |
145 | mutex_unlock(&cooling_cpufreq_lock); | |
146 | } | |
147 | ||
148 | /* Below code defines functions to be used for cpufreq as cooling device */ | |
149 | ||
150 | /** | |
4843c4a1 | 151 | * get_level: Find the level for a particular frequency |
b9f8b416 | 152 | * @cpufreq_dev: cpufreq_dev for which the property is required |
4843c4a1 | 153 | * @freq: Frequency |
82b9ee40 | 154 | * |
4843c4a1 | 155 | * Return: level on success, THERMAL_CSTATE_INVALID on error. |
02361418 | 156 | */ |
4843c4a1 VK |
157 | static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev, |
158 | unsigned int freq) | |
02361418 | 159 | { |
4843c4a1 | 160 | unsigned long level; |
a116776f | 161 | |
4843c4a1 VK |
162 | for (level = 0; level <= cpufreq_dev->max_level; level++) { |
163 | if (freq == cpufreq_dev->freq_table[level]) | |
164 | return level; | |
02361418 | 165 | |
4843c4a1 VK |
166 | if (freq > cpufreq_dev->freq_table[level]) |
167 | break; | |
fc35b35c | 168 | } |
02361418 | 169 | |
4843c4a1 | 170 | return THERMAL_CSTATE_INVALID; |
fc35b35c ZR |
171 | } |
172 | ||
44952d33 | 173 | /** |
728c03c9 | 174 | * cpufreq_cooling_get_level - for a given cpu, return the cooling level. |
44952d33 EV |
175 | * @cpu: cpu for which the level is required |
176 | * @freq: the frequency of interest | |
177 | * | |
178 | * This function will match the cooling level corresponding to the | |
179 | * requested @freq and return it. | |
180 | * | |
181 | * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID | |
182 | * otherwise. | |
183 | */ | |
57df8106 ZR |
184 | unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq) |
185 | { | |
b9f8b416 | 186 | struct cpufreq_cooling_device *cpufreq_dev; |
02361418 | 187 | |
b9f8b416 VK |
188 | mutex_lock(&cooling_cpufreq_lock); |
189 | list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) { | |
190 | if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) { | |
b9f8b416 | 191 | mutex_unlock(&cooling_cpufreq_lock); |
4843c4a1 | 192 | return get_level(cpufreq_dev, freq); |
b9f8b416 | 193 | } |
02361418 | 194 | } |
b9f8b416 | 195 | mutex_unlock(&cooling_cpufreq_lock); |
02361418 | 196 | |
b9f8b416 VK |
197 | pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu); |
198 | return THERMAL_CSTATE_INVALID; | |
02361418 | 199 | } |
243dbd9c | 200 | EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level); |
02361418 | 201 | |
c36cf071 JM |
202 | static void update_cpu_device(int cpu) |
203 | { | |
204 | struct cpufreq_cooling_device *cpufreq_dev; | |
205 | ||
206 | mutex_lock(&cooling_cpufreq_lock); | |
207 | list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) { | |
208 | if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) { | |
209 | cpufreq_dev->cpu_dev = get_cpu_device(cpu); | |
210 | if (!cpufreq_dev->cpu_dev) { | |
211 | dev_warn(&cpufreq_dev->cool_dev->device, | |
212 | "No cpu device for new policy cpu %d\n", | |
213 | cpu); | |
214 | } | |
215 | break; | |
216 | } | |
217 | } | |
218 | mutex_unlock(&cooling_cpufreq_lock); | |
219 | } | |
220 | ||
221 | static void remove_cpu_device(int cpu) | |
222 | { | |
223 | struct cpufreq_cooling_device *cpufreq_dev; | |
224 | ||
225 | mutex_lock(&cooling_cpufreq_lock); | |
226 | list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) { | |
227 | if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) { | |
228 | cpufreq_dev->cpu_dev = NULL; | |
229 | break; | |
230 | } | |
231 | } | |
232 | mutex_unlock(&cooling_cpufreq_lock); | |
233 | } | |
234 | ||
02361418 ADK |
235 | /** |
236 | * cpufreq_thermal_notifier - notifier callback for cpufreq policy change. | |
237 | * @nb: struct notifier_block * with callback info. | |
238 | * @event: value showing cpufreq event for which this function invoked. | |
239 | * @data: callback-specific data | |
bab30554 | 240 | * |
9746b6e7 | 241 | * Callback to hijack the notification on cpufreq policy transition. |
bab30554 EV |
242 | * Every time there is a change in policy, we will intercept and |
243 | * update the cpufreq policy with thermal constraints. | |
244 | * | |
245 | * Return: 0 (success) | |
02361418 ADK |
246 | */ |
247 | static int cpufreq_thermal_notifier(struct notifier_block *nb, | |
5fda7f68 | 248 | unsigned long event, void *data) |
02361418 ADK |
249 | { |
250 | struct cpufreq_policy *policy = data; | |
251 | unsigned long max_freq = 0; | |
2dcd851f | 252 | struct cpufreq_cooling_device *cpufreq_dev; |
02361418 | 253 | |
c36cf071 | 254 | switch (event) { |
02361418 | 255 | |
c36cf071 JM |
256 | case CPUFREQ_ADJUST: |
257 | mutex_lock(&cooling_cpufreq_lock); | |
258 | list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) { | |
259 | if (!cpumask_test_cpu(policy->cpu, | |
260 | &cpufreq_dev->allowed_cpus)) | |
261 | continue; | |
262 | ||
263 | max_freq = cpufreq_dev->cpufreq_val; | |
264 | ||
265 | if (policy->max != max_freq) | |
266 | cpufreq_verify_within_limits(policy, 0, | |
267 | max_freq); | |
268 | } | |
269 | mutex_unlock(&cooling_cpufreq_lock); | |
270 | break; | |
271 | ||
272 | case CPUFREQ_CREATE_POLICY: | |
273 | update_cpu_device(policy->cpu); | |
274 | break; | |
275 | case CPUFREQ_REMOVE_POLICY: | |
276 | remove_cpu_device(policy->cpu); | |
277 | break; | |
278 | default: | |
279 | return NOTIFY_DONE; | |
280 | } | |
281 | ||
282 | return NOTIFY_OK; | |
283 | } | |
284 | ||
285 | /** | |
286 | * build_dyn_power_table() - create a dynamic power to frequency table | |
287 | * @cpufreq_device: the cpufreq cooling device in which to store the table | |
288 | * @capacitance: dynamic power coefficient for these cpus | |
289 | * | |
290 | * Build a dynamic power to frequency table for this cpu and store it | |
291 | * in @cpufreq_device. This table will be used in cpu_power_to_freq() and | |
292 | * cpu_freq_to_power() to convert between power and frequency | |
293 | * efficiently. Power is stored in mW, frequency in KHz. The | |
294 | * resulting table is in ascending order. | |
295 | * | |
296 | * Return: 0 on success, -E* on error. | |
297 | */ | |
298 | static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_device, | |
299 | u32 capacitance) | |
300 | { | |
301 | struct power_table *power_table; | |
302 | struct dev_pm_opp *opp; | |
303 | struct device *dev = NULL; | |
304 | int num_opps = 0, cpu, i, ret = 0; | |
305 | unsigned long freq; | |
306 | ||
307 | rcu_read_lock(); | |
308 | ||
309 | for_each_cpu(cpu, &cpufreq_device->allowed_cpus) { | |
310 | dev = get_cpu_device(cpu); | |
311 | if (!dev) { | |
312 | dev_warn(&cpufreq_device->cool_dev->device, | |
313 | "No cpu device for cpu %d\n", cpu); | |
2dcd851f | 314 | continue; |
c36cf071 | 315 | } |
2dcd851f | 316 | |
c36cf071 JM |
317 | num_opps = dev_pm_opp_get_opp_count(dev); |
318 | if (num_opps > 0) { | |
319 | break; | |
320 | } else if (num_opps < 0) { | |
321 | ret = num_opps; | |
322 | goto unlock; | |
323 | } | |
324 | } | |
02361418 | 325 | |
c36cf071 JM |
326 | if (num_opps == 0) { |
327 | ret = -EINVAL; | |
328 | goto unlock; | |
2dcd851f | 329 | } |
02361418 | 330 | |
c36cf071 JM |
331 | power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL); |
332 | ||
333 | for (freq = 0, i = 0; | |
334 | opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp); | |
335 | freq++, i++) { | |
336 | u32 freq_mhz, voltage_mv; | |
337 | u64 power; | |
338 | ||
339 | freq_mhz = freq / 1000000; | |
340 | voltage_mv = dev_pm_opp_get_voltage(opp) / 1000; | |
341 | ||
342 | /* | |
343 | * Do the multiplication with MHz and millivolt so as | |
344 | * to not overflow. | |
345 | */ | |
346 | power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv; | |
347 | do_div(power, 1000000000); | |
348 | ||
349 | /* frequency is stored in power_table in KHz */ | |
350 | power_table[i].frequency = freq / 1000; | |
351 | ||
352 | /* power is stored in mW */ | |
353 | power_table[i].power = power; | |
354 | } | |
355 | ||
356 | if (i == 0) { | |
357 | ret = PTR_ERR(opp); | |
358 | goto unlock; | |
359 | } | |
360 | ||
361 | cpufreq_device->cpu_dev = dev; | |
362 | cpufreq_device->dyn_power_table = power_table; | |
363 | cpufreq_device->dyn_power_table_entries = i; | |
364 | ||
365 | unlock: | |
366 | rcu_read_unlock(); | |
367 | return ret; | |
368 | } | |
369 | ||
370 | static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_device, | |
371 | u32 freq) | |
372 | { | |
373 | int i; | |
374 | struct power_table *pt = cpufreq_device->dyn_power_table; | |
375 | ||
376 | for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++) | |
377 | if (freq < pt[i].frequency) | |
378 | break; | |
379 | ||
380 | return pt[i - 1].power; | |
381 | } | |
382 | ||
383 | static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_device, | |
384 | u32 power) | |
385 | { | |
386 | int i; | |
387 | struct power_table *pt = cpufreq_device->dyn_power_table; | |
388 | ||
389 | for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++) | |
390 | if (power < pt[i].power) | |
391 | break; | |
392 | ||
393 | return pt[i - 1].frequency; | |
394 | } | |
395 | ||
396 | /** | |
397 | * get_load() - get load for a cpu since last updated | |
398 | * @cpufreq_device: &struct cpufreq_cooling_device for this cpu | |
399 | * @cpu: cpu number | |
400 | * | |
401 | * Return: The average load of cpu @cpu in percentage since this | |
402 | * function was last called. | |
403 | */ | |
404 | static u32 get_load(struct cpufreq_cooling_device *cpufreq_device, int cpu) | |
405 | { | |
406 | u32 load; | |
407 | u64 now, now_idle, delta_time, delta_idle; | |
408 | ||
409 | now_idle = get_cpu_idle_time(cpu, &now, 0); | |
410 | delta_idle = now_idle - cpufreq_device->time_in_idle[cpu]; | |
411 | delta_time = now - cpufreq_device->time_in_idle_timestamp[cpu]; | |
412 | ||
413 | if (delta_time <= delta_idle) | |
414 | load = 0; | |
415 | else | |
416 | load = div64_u64(100 * (delta_time - delta_idle), delta_time); | |
417 | ||
418 | cpufreq_device->time_in_idle[cpu] = now_idle; | |
419 | cpufreq_device->time_in_idle_timestamp[cpu] = now; | |
420 | ||
421 | return load; | |
422 | } | |
423 | ||
424 | /** | |
425 | * get_static_power() - calculate the static power consumed by the cpus | |
426 | * @cpufreq_device: struct &cpufreq_cooling_device for this cpu cdev | |
427 | * @tz: thermal zone device in which we're operating | |
428 | * @freq: frequency in KHz | |
429 | * @power: pointer in which to store the calculated static power | |
430 | * | |
431 | * Calculate the static power consumed by the cpus described by | |
432 | * @cpu_actor running at frequency @freq. This function relies on a | |
433 | * platform specific function that should have been provided when the | |
434 | * actor was registered. If it wasn't, the static power is assumed to | |
435 | * be negligible. The calculated static power is stored in @power. | |
436 | * | |
437 | * Return: 0 on success, -E* on failure. | |
438 | */ | |
439 | static int get_static_power(struct cpufreq_cooling_device *cpufreq_device, | |
440 | struct thermal_zone_device *tz, unsigned long freq, | |
441 | u32 *power) | |
442 | { | |
443 | struct dev_pm_opp *opp; | |
444 | unsigned long voltage; | |
445 | struct cpumask *cpumask = &cpufreq_device->allowed_cpus; | |
446 | unsigned long freq_hz = freq * 1000; | |
447 | ||
448 | if (!cpufreq_device->plat_get_static_power || | |
449 | !cpufreq_device->cpu_dev) { | |
450 | *power = 0; | |
451 | return 0; | |
452 | } | |
453 | ||
454 | rcu_read_lock(); | |
455 | ||
456 | opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz, | |
457 | true); | |
458 | voltage = dev_pm_opp_get_voltage(opp); | |
459 | ||
460 | rcu_read_unlock(); | |
461 | ||
462 | if (voltage == 0) { | |
463 | dev_warn_ratelimited(cpufreq_device->cpu_dev, | |
464 | "Failed to get voltage for frequency %lu: %ld\n", | |
465 | freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0); | |
466 | return -EINVAL; | |
467 | } | |
468 | ||
469 | return cpufreq_device->plat_get_static_power(cpumask, tz->passive_delay, | |
470 | voltage, power); | |
471 | } | |
472 | ||
473 | /** | |
474 | * get_dynamic_power() - calculate the dynamic power | |
475 | * @cpufreq_device: &cpufreq_cooling_device for this cdev | |
476 | * @freq: current frequency | |
477 | * | |
478 | * Return: the dynamic power consumed by the cpus described by | |
479 | * @cpufreq_device. | |
480 | */ | |
481 | static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_device, | |
482 | unsigned long freq) | |
483 | { | |
484 | u32 raw_cpu_power; | |
485 | ||
486 | raw_cpu_power = cpu_freq_to_power(cpufreq_device, freq); | |
487 | return (raw_cpu_power * cpufreq_device->last_load) / 100; | |
02361418 ADK |
488 | } |
489 | ||
1b9e3526 | 490 | /* cpufreq cooling device callback functions are defined below */ |
02361418 ADK |
491 | |
492 | /** | |
493 | * cpufreq_get_max_state - callback function to get the max cooling state. | |
494 | * @cdev: thermal cooling device pointer. | |
495 | * @state: fill this variable with the max cooling state. | |
62c00421 EV |
496 | * |
497 | * Callback for the thermal cooling device to return the cpufreq | |
498 | * max cooling state. | |
499 | * | |
500 | * Return: 0 on success, an error code otherwise. | |
02361418 ADK |
501 | */ |
502 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, | |
503 | unsigned long *state) | |
504 | { | |
160b7d80 | 505 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
9c51b05a | 506 | |
dcc6c7fd VK |
507 | *state = cpufreq_device->max_level; |
508 | return 0; | |
02361418 ADK |
509 | } |
510 | ||
511 | /** | |
512 | * cpufreq_get_cur_state - callback function to get the current cooling state. | |
513 | * @cdev: thermal cooling device pointer. | |
514 | * @state: fill this variable with the current cooling state. | |
3672552d EV |
515 | * |
516 | * Callback for the thermal cooling device to return the cpufreq | |
517 | * current cooling state. | |
518 | * | |
519 | * Return: 0 on success, an error code otherwise. | |
02361418 ADK |
520 | */ |
521 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, | |
522 | unsigned long *state) | |
523 | { | |
160b7d80 | 524 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
02361418 | 525 | |
160b7d80 | 526 | *state = cpufreq_device->cpufreq_state; |
79491e53 | 527 | |
160b7d80 | 528 | return 0; |
02361418 ADK |
529 | } |
530 | ||
531 | /** | |
532 | * cpufreq_set_cur_state - callback function to set the current cooling state. | |
533 | * @cdev: thermal cooling device pointer. | |
534 | * @state: set this variable to the current cooling state. | |
56e05fdb EV |
535 | * |
536 | * Callback for the thermal cooling device to change the cpufreq | |
537 | * current cooling state. | |
538 | * | |
539 | * Return: 0 on success, an error code otherwise. | |
02361418 ADK |
540 | */ |
541 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, | |
542 | unsigned long state) | |
543 | { | |
160b7d80 | 544 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
5194fe46 VK |
545 | unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus); |
546 | unsigned int clip_freq; | |
4843c4a1 VK |
547 | |
548 | /* Request state should be less than max_level */ | |
549 | if (WARN_ON(state > cpufreq_device->max_level)) | |
550 | return -EINVAL; | |
5194fe46 VK |
551 | |
552 | /* Check if the old cooling action is same as new cooling action */ | |
553 | if (cpufreq_device->cpufreq_state == state) | |
554 | return 0; | |
02361418 | 555 | |
4843c4a1 | 556 | clip_freq = cpufreq_device->freq_table[state]; |
5194fe46 VK |
557 | cpufreq_device->cpufreq_state = state; |
558 | cpufreq_device->cpufreq_val = clip_freq; | |
559 | ||
560 | cpufreq_update_policy(cpu); | |
561 | ||
562 | return 0; | |
02361418 ADK |
563 | } |
564 | ||
c36cf071 JM |
565 | /** |
566 | * cpufreq_get_requested_power() - get the current power | |
567 | * @cdev: &thermal_cooling_device pointer | |
568 | * @tz: a valid thermal zone device pointer | |
569 | * @power: pointer in which to store the resulting power | |
570 | * | |
571 | * Calculate the current power consumption of the cpus in milliwatts | |
572 | * and store it in @power. This function should actually calculate | |
573 | * the requested power, but it's hard to get the frequency that | |
574 | * cpufreq would have assigned if there were no thermal limits. | |
575 | * Instead, we calculate the current power on the assumption that the | |
576 | * immediate future will look like the immediate past. | |
577 | * | |
578 | * We use the current frequency and the average load since this | |
579 | * function was last called. In reality, there could have been | |
580 | * multiple opps since this function was last called and that affects | |
581 | * the load calculation. While it's not perfectly accurate, this | |
582 | * simplification is good enough and works. REVISIT this, as more | |
583 | * complex code may be needed if experiments show that it's not | |
584 | * accurate enough. | |
585 | * | |
586 | * Return: 0 on success, -E* if getting the static power failed. | |
587 | */ | |
588 | static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev, | |
589 | struct thermal_zone_device *tz, | |
590 | u32 *power) | |
591 | { | |
592 | unsigned long freq; | |
6828a471 | 593 | int i = 0, cpu, ret; |
c36cf071 JM |
594 | u32 static_power, dynamic_power, total_load = 0; |
595 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; | |
6828a471 | 596 | u32 *load_cpu = NULL; |
c36cf071 JM |
597 | |
598 | freq = cpufreq_quick_get(cpumask_any(&cpufreq_device->allowed_cpus)); | |
599 | ||
6828a471 JM |
600 | if (trace_thermal_power_cpu_get_power_enabled()) { |
601 | u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus); | |
602 | ||
603 | load_cpu = devm_kcalloc(&cdev->device, ncpus, sizeof(*load_cpu), | |
604 | GFP_KERNEL); | |
605 | } | |
606 | ||
c36cf071 JM |
607 | for_each_cpu(cpu, &cpufreq_device->allowed_cpus) { |
608 | u32 load; | |
609 | ||
610 | if (cpu_online(cpu)) | |
611 | load = get_load(cpufreq_device, cpu); | |
612 | else | |
613 | load = 0; | |
614 | ||
615 | total_load += load; | |
6828a471 JM |
616 | if (trace_thermal_power_cpu_limit_enabled() && load_cpu) |
617 | load_cpu[i] = load; | |
618 | ||
619 | i++; | |
c36cf071 JM |
620 | } |
621 | ||
622 | cpufreq_device->last_load = total_load; | |
623 | ||
624 | dynamic_power = get_dynamic_power(cpufreq_device, freq); | |
625 | ret = get_static_power(cpufreq_device, tz, freq, &static_power); | |
6828a471 JM |
626 | if (ret) { |
627 | if (load_cpu) | |
628 | devm_kfree(&cdev->device, load_cpu); | |
c36cf071 | 629 | return ret; |
6828a471 JM |
630 | } |
631 | ||
632 | if (load_cpu) { | |
633 | trace_thermal_power_cpu_get_power( | |
634 | &cpufreq_device->allowed_cpus, | |
635 | freq, load_cpu, i, dynamic_power, static_power); | |
636 | ||
637 | devm_kfree(&cdev->device, load_cpu); | |
638 | } | |
c36cf071 JM |
639 | |
640 | *power = static_power + dynamic_power; | |
641 | return 0; | |
642 | } | |
643 | ||
644 | /** | |
645 | * cpufreq_state2power() - convert a cpu cdev state to power consumed | |
646 | * @cdev: &thermal_cooling_device pointer | |
647 | * @tz: a valid thermal zone device pointer | |
648 | * @state: cooling device state to be converted | |
649 | * @power: pointer in which to store the resulting power | |
650 | * | |
651 | * Convert cooling device state @state into power consumption in | |
652 | * milliwatts assuming 100% load. Store the calculated power in | |
653 | * @power. | |
654 | * | |
655 | * Return: 0 on success, -EINVAL if the cooling device state could not | |
656 | * be converted into a frequency or other -E* if there was an error | |
657 | * when calculating the static power. | |
658 | */ | |
659 | static int cpufreq_state2power(struct thermal_cooling_device *cdev, | |
660 | struct thermal_zone_device *tz, | |
661 | unsigned long state, u32 *power) | |
662 | { | |
663 | unsigned int freq, num_cpus; | |
664 | cpumask_t cpumask; | |
665 | u32 static_power, dynamic_power; | |
666 | int ret; | |
667 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; | |
668 | ||
669 | cpumask_and(&cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask); | |
670 | num_cpus = cpumask_weight(&cpumask); | |
671 | ||
672 | /* None of our cpus are online, so no power */ | |
673 | if (num_cpus == 0) { | |
674 | *power = 0; | |
675 | return 0; | |
676 | } | |
677 | ||
678 | freq = cpufreq_device->freq_table[state]; | |
679 | if (!freq) | |
680 | return -EINVAL; | |
681 | ||
682 | dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus; | |
683 | ret = get_static_power(cpufreq_device, tz, freq, &static_power); | |
684 | if (ret) | |
685 | return ret; | |
686 | ||
687 | *power = static_power + dynamic_power; | |
688 | return 0; | |
689 | } | |
690 | ||
691 | /** | |
692 | * cpufreq_power2state() - convert power to a cooling device state | |
693 | * @cdev: &thermal_cooling_device pointer | |
694 | * @tz: a valid thermal zone device pointer | |
695 | * @power: power in milliwatts to be converted | |
696 | * @state: pointer in which to store the resulting state | |
697 | * | |
698 | * Calculate a cooling device state for the cpus described by @cdev | |
699 | * that would allow them to consume at most @power mW and store it in | |
700 | * @state. Note that this calculation depends on external factors | |
701 | * such as the cpu load or the current static power. Calling this | |
702 | * function with the same power as input can yield different cooling | |
703 | * device states depending on those external factors. | |
704 | * | |
705 | * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if | |
706 | * the calculated frequency could not be converted to a valid state. | |
707 | * The latter should not happen unless the frequencies available to | |
708 | * cpufreq have changed since the initialization of the cpu cooling | |
709 | * device. | |
710 | */ | |
711 | static int cpufreq_power2state(struct thermal_cooling_device *cdev, | |
712 | struct thermal_zone_device *tz, u32 power, | |
713 | unsigned long *state) | |
714 | { | |
715 | unsigned int cpu, cur_freq, target_freq; | |
716 | int ret; | |
717 | s32 dyn_power; | |
718 | u32 last_load, normalised_power, static_power; | |
719 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; | |
720 | ||
721 | cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask); | |
722 | ||
723 | /* None of our cpus are online */ | |
724 | if (cpu >= nr_cpu_ids) | |
725 | return -ENODEV; | |
726 | ||
727 | cur_freq = cpufreq_quick_get(cpu); | |
728 | ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power); | |
729 | if (ret) | |
730 | return ret; | |
731 | ||
732 | dyn_power = power - static_power; | |
733 | dyn_power = dyn_power > 0 ? dyn_power : 0; | |
734 | last_load = cpufreq_device->last_load ?: 1; | |
735 | normalised_power = (dyn_power * 100) / last_load; | |
736 | target_freq = cpu_power_to_freq(cpufreq_device, normalised_power); | |
737 | ||
738 | *state = cpufreq_cooling_get_level(cpu, target_freq); | |
739 | if (*state == THERMAL_CSTATE_INVALID) { | |
740 | dev_warn_ratelimited(&cdev->device, | |
741 | "Failed to convert %dKHz for cpu %d into a cdev state\n", | |
742 | target_freq, cpu); | |
743 | return -EINVAL; | |
744 | } | |
745 | ||
6828a471 JM |
746 | trace_thermal_power_cpu_limit(&cpufreq_device->allowed_cpus, |
747 | target_freq, *state, power); | |
c36cf071 JM |
748 | return 0; |
749 | } | |
750 | ||
02361418 | 751 | /* Bind cpufreq callbacks to thermal cooling device ops */ |
c36cf071 | 752 | static struct thermal_cooling_device_ops cpufreq_cooling_ops = { |
02361418 ADK |
753 | .get_max_state = cpufreq_get_max_state, |
754 | .get_cur_state = cpufreq_get_cur_state, | |
755 | .set_cur_state = cpufreq_set_cur_state, | |
756 | }; | |
757 | ||
758 | /* Notifier for cpufreq policy change */ | |
759 | static struct notifier_block thermal_cpufreq_notifier_block = { | |
760 | .notifier_call = cpufreq_thermal_notifier, | |
761 | }; | |
762 | ||
f6859014 VK |
763 | static unsigned int find_next_max(struct cpufreq_frequency_table *table, |
764 | unsigned int prev_max) | |
765 | { | |
766 | struct cpufreq_frequency_table *pos; | |
767 | unsigned int max = 0; | |
768 | ||
769 | cpufreq_for_each_valid_entry(pos, table) { | |
770 | if (pos->frequency > max && pos->frequency < prev_max) | |
771 | max = pos->frequency; | |
772 | } | |
773 | ||
774 | return max; | |
775 | } | |
776 | ||
02361418 | 777 | /** |
39d99cff EV |
778 | * __cpufreq_cooling_register - helper function to create cpufreq cooling device |
779 | * @np: a valid struct device_node to the cooling device device tree node | |
02361418 | 780 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. |
405fb825 | 781 | * Normally this should be same as cpufreq policy->related_cpus. |
c36cf071 JM |
782 | * @capacitance: dynamic power coefficient for these cpus |
783 | * @plat_static_func: function to calculate the static power consumed by these | |
784 | * cpus (optional) | |
12cb08ba EV |
785 | * |
786 | * This interface function registers the cpufreq cooling device with the name | |
787 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
39d99cff EV |
788 | * cooling devices. It also gives the opportunity to link the cooling device |
789 | * with a device tree node, in order to bind it via the thermal DT code. | |
12cb08ba EV |
790 | * |
791 | * Return: a valid struct thermal_cooling_device pointer on success, | |
792 | * on failure, it returns a corresponding ERR_PTR(). | |
02361418 | 793 | */ |
39d99cff EV |
794 | static struct thermal_cooling_device * |
795 | __cpufreq_cooling_register(struct device_node *np, | |
c36cf071 JM |
796 | const struct cpumask *clip_cpus, u32 capacitance, |
797 | get_static_t plat_static_func) | |
02361418 ADK |
798 | { |
799 | struct thermal_cooling_device *cool_dev; | |
5d3bdb89 | 800 | struct cpufreq_cooling_device *cpufreq_dev; |
02361418 | 801 | char dev_name[THERMAL_NAME_LENGTH]; |
dcc6c7fd | 802 | struct cpufreq_frequency_table *pos, *table; |
c36cf071 | 803 | unsigned int freq, i, num_cpus; |
405fb825 | 804 | int ret; |
02361418 | 805 | |
dcc6c7fd VK |
806 | table = cpufreq_frequency_get_table(cpumask_first(clip_cpus)); |
807 | if (!table) { | |
0f1be51c EV |
808 | pr_debug("%s: CPUFreq table not found\n", __func__); |
809 | return ERR_PTR(-EPROBE_DEFER); | |
02361418 | 810 | } |
0f1be51c | 811 | |
98d522f0 | 812 | cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL); |
02361418 ADK |
813 | if (!cpufreq_dev) |
814 | return ERR_PTR(-ENOMEM); | |
815 | ||
c36cf071 JM |
816 | num_cpus = cpumask_weight(clip_cpus); |
817 | cpufreq_dev->time_in_idle = kcalloc(num_cpus, | |
818 | sizeof(*cpufreq_dev->time_in_idle), | |
819 | GFP_KERNEL); | |
820 | if (!cpufreq_dev->time_in_idle) { | |
821 | cool_dev = ERR_PTR(-ENOMEM); | |
822 | goto free_cdev; | |
823 | } | |
824 | ||
825 | cpufreq_dev->time_in_idle_timestamp = | |
826 | kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp), | |
827 | GFP_KERNEL); | |
828 | if (!cpufreq_dev->time_in_idle_timestamp) { | |
829 | cool_dev = ERR_PTR(-ENOMEM); | |
830 | goto free_time_in_idle; | |
831 | } | |
832 | ||
dcc6c7fd VK |
833 | /* Find max levels */ |
834 | cpufreq_for_each_valid_entry(pos, table) | |
835 | cpufreq_dev->max_level++; | |
836 | ||
f6859014 VK |
837 | cpufreq_dev->freq_table = kmalloc(sizeof(*cpufreq_dev->freq_table) * |
838 | cpufreq_dev->max_level, GFP_KERNEL); | |
839 | if (!cpufreq_dev->freq_table) { | |
f6859014 | 840 | cool_dev = ERR_PTR(-ENOMEM); |
c36cf071 | 841 | goto free_time_in_idle_timestamp; |
f6859014 VK |
842 | } |
843 | ||
dcc6c7fd VK |
844 | /* max_level is an index, not a counter */ |
845 | cpufreq_dev->max_level--; | |
846 | ||
02361418 ADK |
847 | cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus); |
848 | ||
c36cf071 JM |
849 | if (capacitance) { |
850 | cpufreq_cooling_ops.get_requested_power = | |
851 | cpufreq_get_requested_power; | |
852 | cpufreq_cooling_ops.state2power = cpufreq_state2power; | |
853 | cpufreq_cooling_ops.power2state = cpufreq_power2state; | |
854 | cpufreq_dev->plat_get_static_power = plat_static_func; | |
855 | ||
856 | ret = build_dyn_power_table(cpufreq_dev, capacitance); | |
857 | if (ret) { | |
858 | cool_dev = ERR_PTR(ret); | |
859 | goto free_table; | |
860 | } | |
861 | } | |
862 | ||
02361418 ADK |
863 | ret = get_idr(&cpufreq_idr, &cpufreq_dev->id); |
864 | if (ret) { | |
730abe06 | 865 | cool_dev = ERR_PTR(ret); |
f6859014 | 866 | goto free_table; |
02361418 ADK |
867 | } |
868 | ||
99871a71 EV |
869 | snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", |
870 | cpufreq_dev->id); | |
02361418 | 871 | |
39d99cff EV |
872 | cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev, |
873 | &cpufreq_cooling_ops); | |
730abe06 VK |
874 | if (IS_ERR(cool_dev)) |
875 | goto remove_idr; | |
876 | ||
f6859014 VK |
877 | /* Fill freq-table in descending order of frequencies */ |
878 | for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) { | |
879 | freq = find_next_max(table, freq); | |
880 | cpufreq_dev->freq_table[i] = freq; | |
881 | ||
882 | /* Warn for duplicate entries */ | |
883 | if (!freq) | |
884 | pr_warn("%s: table has duplicate entries\n", __func__); | |
885 | else | |
886 | pr_debug("%s: freq:%u KHz\n", __func__, freq); | |
02361418 | 887 | } |
f6859014 | 888 | |
4843c4a1 | 889 | cpufreq_dev->cpufreq_val = cpufreq_dev->freq_table[0]; |
02361418 | 890 | cpufreq_dev->cool_dev = cool_dev; |
92e615ec | 891 | |
02361418 | 892 | mutex_lock(&cooling_cpufreq_lock); |
02361418 ADK |
893 | |
894 | /* Register the notifier for first cpufreq cooling device */ | |
2479bb64 | 895 | if (list_empty(&cpufreq_dev_list)) |
02361418 | 896 | cpufreq_register_notifier(&thermal_cpufreq_notifier_block, |
5fda7f68 | 897 | CPUFREQ_POLICY_NOTIFIER); |
2dcd851f | 898 | list_add(&cpufreq_dev->node, &cpufreq_dev_list); |
02361418 ADK |
899 | |
900 | mutex_unlock(&cooling_cpufreq_lock); | |
79491e53 | 901 | |
730abe06 VK |
902 | return cool_dev; |
903 | ||
904 | remove_idr: | |
905 | release_idr(&cpufreq_idr, cpufreq_dev->id); | |
f6859014 VK |
906 | free_table: |
907 | kfree(cpufreq_dev->freq_table); | |
c36cf071 JM |
908 | free_time_in_idle_timestamp: |
909 | kfree(cpufreq_dev->time_in_idle_timestamp); | |
910 | free_time_in_idle: | |
911 | kfree(cpufreq_dev->time_in_idle); | |
730abe06 VK |
912 | free_cdev: |
913 | kfree(cpufreq_dev); | |
914 | ||
02361418 ADK |
915 | return cool_dev; |
916 | } | |
39d99cff EV |
917 | |
918 | /** | |
919 | * cpufreq_cooling_register - function to create cpufreq cooling device. | |
920 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. | |
921 | * | |
922 | * This interface function registers the cpufreq cooling device with the name | |
923 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
924 | * cooling devices. | |
925 | * | |
926 | * Return: a valid struct thermal_cooling_device pointer on success, | |
927 | * on failure, it returns a corresponding ERR_PTR(). | |
928 | */ | |
929 | struct thermal_cooling_device * | |
930 | cpufreq_cooling_register(const struct cpumask *clip_cpus) | |
931 | { | |
c36cf071 | 932 | return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL); |
39d99cff | 933 | } |
243dbd9c | 934 | EXPORT_SYMBOL_GPL(cpufreq_cooling_register); |
02361418 | 935 | |
39d99cff EV |
936 | /** |
937 | * of_cpufreq_cooling_register - function to create cpufreq cooling device. | |
938 | * @np: a valid struct device_node to the cooling device device tree node | |
939 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. | |
940 | * | |
941 | * This interface function registers the cpufreq cooling device with the name | |
942 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
943 | * cooling devices. Using this API, the cpufreq cooling device will be | |
944 | * linked to the device tree node provided. | |
945 | * | |
946 | * Return: a valid struct thermal_cooling_device pointer on success, | |
947 | * on failure, it returns a corresponding ERR_PTR(). | |
948 | */ | |
949 | struct thermal_cooling_device * | |
950 | of_cpufreq_cooling_register(struct device_node *np, | |
951 | const struct cpumask *clip_cpus) | |
952 | { | |
953 | if (!np) | |
954 | return ERR_PTR(-EINVAL); | |
955 | ||
c36cf071 | 956 | return __cpufreq_cooling_register(np, clip_cpus, 0, NULL); |
39d99cff EV |
957 | } |
958 | EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); | |
959 | ||
c36cf071 JM |
960 | /** |
961 | * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions | |
962 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen | |
963 | * @capacitance: dynamic power coefficient for these cpus | |
964 | * @plat_static_func: function to calculate the static power consumed by these | |
965 | * cpus (optional) | |
966 | * | |
967 | * This interface function registers the cpufreq cooling device with | |
968 | * the name "thermal-cpufreq-%x". This api can support multiple | |
969 | * instances of cpufreq cooling devices. Using this function, the | |
970 | * cooling device will implement the power extensions by using a | |
971 | * simple cpu power model. The cpus must have registered their OPPs | |
972 | * using the OPP library. | |
973 | * | |
974 | * An optional @plat_static_func may be provided to calculate the | |
975 | * static power consumed by these cpus. If the platform's static | |
976 | * power consumption is unknown or negligible, make it NULL. | |
977 | * | |
978 | * Return: a valid struct thermal_cooling_device pointer on success, | |
979 | * on failure, it returns a corresponding ERR_PTR(). | |
980 | */ | |
981 | struct thermal_cooling_device * | |
982 | cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance, | |
983 | get_static_t plat_static_func) | |
984 | { | |
985 | return __cpufreq_cooling_register(NULL, clip_cpus, capacitance, | |
986 | plat_static_func); | |
987 | } | |
988 | EXPORT_SYMBOL(cpufreq_power_cooling_register); | |
989 | ||
990 | /** | |
991 | * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions | |
992 | * @np: a valid struct device_node to the cooling device device tree node | |
993 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen | |
994 | * @capacitance: dynamic power coefficient for these cpus | |
995 | * @plat_static_func: function to calculate the static power consumed by these | |
996 | * cpus (optional) | |
997 | * | |
998 | * This interface function registers the cpufreq cooling device with | |
999 | * the name "thermal-cpufreq-%x". This api can support multiple | |
1000 | * instances of cpufreq cooling devices. Using this API, the cpufreq | |
1001 | * cooling device will be linked to the device tree node provided. | |
1002 | * Using this function, the cooling device will implement the power | |
1003 | * extensions by using a simple cpu power model. The cpus must have | |
1004 | * registered their OPPs using the OPP library. | |
1005 | * | |
1006 | * An optional @plat_static_func may be provided to calculate the | |
1007 | * static power consumed by these cpus. If the platform's static | |
1008 | * power consumption is unknown or negligible, make it NULL. | |
1009 | * | |
1010 | * Return: a valid struct thermal_cooling_device pointer on success, | |
1011 | * on failure, it returns a corresponding ERR_PTR(). | |
1012 | */ | |
1013 | struct thermal_cooling_device * | |
1014 | of_cpufreq_power_cooling_register(struct device_node *np, | |
1015 | const struct cpumask *clip_cpus, | |
1016 | u32 capacitance, | |
1017 | get_static_t plat_static_func) | |
1018 | { | |
1019 | if (!np) | |
1020 | return ERR_PTR(-EINVAL); | |
1021 | ||
1022 | return __cpufreq_cooling_register(np, clip_cpus, capacitance, | |
1023 | plat_static_func); | |
1024 | } | |
1025 | EXPORT_SYMBOL(of_cpufreq_power_cooling_register); | |
1026 | ||
02361418 ADK |
1027 | /** |
1028 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. | |
1029 | * @cdev: thermal cooling device pointer. | |
135266b4 EV |
1030 | * |
1031 | * This interface function unregisters the "thermal-cpufreq-%x" cooling device. | |
02361418 ADK |
1032 | */ |
1033 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) | |
1034 | { | |
50e66c7e | 1035 | struct cpufreq_cooling_device *cpufreq_dev; |
02361418 | 1036 | |
50e66c7e EV |
1037 | if (!cdev) |
1038 | return; | |
1039 | ||
1040 | cpufreq_dev = cdev->devdata; | |
02361418 | 1041 | mutex_lock(&cooling_cpufreq_lock); |
2dcd851f | 1042 | list_del(&cpufreq_dev->node); |
02361418 ADK |
1043 | |
1044 | /* Unregister the notifier for the last cpufreq cooling device */ | |
2479bb64 | 1045 | if (list_empty(&cpufreq_dev_list)) |
02361418 | 1046 | cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block, |
5fda7f68 | 1047 | CPUFREQ_POLICY_NOTIFIER); |
02361418 | 1048 | mutex_unlock(&cooling_cpufreq_lock); |
160b7d80 | 1049 | |
02361418 ADK |
1050 | thermal_cooling_device_unregister(cpufreq_dev->cool_dev); |
1051 | release_idr(&cpufreq_idr, cpufreq_dev->id); | |
c36cf071 JM |
1052 | kfree(cpufreq_dev->time_in_idle_timestamp); |
1053 | kfree(cpufreq_dev->time_in_idle); | |
f6859014 | 1054 | kfree(cpufreq_dev->freq_table); |
02361418 ADK |
1055 | kfree(cpufreq_dev); |
1056 | } | |
243dbd9c | 1057 | EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister); |