]>
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 | * | |
7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; version 2 of the License. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along | |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
20 | * | |
21 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
22 | */ | |
02361418 ADK |
23 | #include <linux/module.h> |
24 | #include <linux/thermal.h> | |
02361418 ADK |
25 | #include <linux/cpufreq.h> |
26 | #include <linux/err.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/cpu.h> | |
29 | #include <linux/cpu_cooling.h> | |
30 | ||
07d888d8 VK |
31 | /* |
32 | * Cooling state <-> CPUFreq frequency | |
33 | * | |
34 | * Cooling states are translated to frequencies throughout this driver and this | |
35 | * is the relation between them. | |
36 | * | |
37 | * Highest cooling state corresponds to lowest possible frequency. | |
38 | * | |
39 | * i.e. | |
40 | * level 0 --> 1st Max Freq | |
41 | * level 1 --> 2nd Max Freq | |
42 | * ... | |
43 | */ | |
44 | ||
02361418 | 45 | /** |
3b3c0748 | 46 | * struct cpufreq_cooling_device - data for cooling device with cpufreq |
02361418 ADK |
47 | * @id: unique integer value corresponding to each cpufreq_cooling_device |
48 | * registered. | |
3b3c0748 EV |
49 | * @cool_dev: thermal_cooling_device pointer to keep track of the |
50 | * registered cooling device. | |
02361418 ADK |
51 | * @cpufreq_state: integer value representing the current state of cpufreq |
52 | * cooling devices. | |
53 | * @cpufreq_val: integer value representing the absolute value of the clipped | |
54 | * frequency. | |
55 | * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device. | |
02361418 | 56 | * |
beca6053 VK |
57 | * This structure is required for keeping information of each registered |
58 | * cpufreq_cooling_device. | |
02361418 ADK |
59 | */ |
60 | struct cpufreq_cooling_device { | |
61 | int id; | |
62 | struct thermal_cooling_device *cool_dev; | |
63 | unsigned int cpufreq_state; | |
64 | unsigned int cpufreq_val; | |
65 | struct cpumask allowed_cpus; | |
2dcd851f | 66 | struct list_head node; |
02361418 | 67 | }; |
02361418 | 68 | static DEFINE_IDR(cpufreq_idr); |
160b7d80 | 69 | static DEFINE_MUTEX(cooling_cpufreq_lock); |
02361418 | 70 | |
160b7d80 | 71 | static unsigned int cpufreq_dev_count; |
02361418 | 72 | |
2dcd851f | 73 | static LIST_HEAD(cpufreq_dev_list); |
02361418 ADK |
74 | |
75 | /** | |
76 | * get_idr - function to get a unique id. | |
77 | * @idr: struct idr * handle used to create a id. | |
78 | * @id: int * value generated by this function. | |
79491e53 EV |
79 | * |
80 | * This function will populate @id with an unique | |
81 | * id, using the idr API. | |
82 | * | |
83 | * Return: 0 on success, an error code on failure. | |
02361418 ADK |
84 | */ |
85 | static int get_idr(struct idr *idr, int *id) | |
86 | { | |
6deb69fa | 87 | int ret; |
02361418 ADK |
88 | |
89 | mutex_lock(&cooling_cpufreq_lock); | |
6deb69fa | 90 | ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL); |
02361418 | 91 | mutex_unlock(&cooling_cpufreq_lock); |
6deb69fa TH |
92 | if (unlikely(ret < 0)) |
93 | return ret; | |
94 | *id = ret; | |
79491e53 | 95 | |
02361418 ADK |
96 | return 0; |
97 | } | |
98 | ||
99 | /** | |
100 | * release_idr - function to free the unique id. | |
101 | * @idr: struct idr * handle used for creating the id. | |
102 | * @id: int value representing the unique id. | |
103 | */ | |
104 | static void release_idr(struct idr *idr, int id) | |
105 | { | |
106 | mutex_lock(&cooling_cpufreq_lock); | |
107 | idr_remove(idr, id); | |
108 | mutex_unlock(&cooling_cpufreq_lock); | |
109 | } | |
110 | ||
111 | /* Below code defines functions to be used for cpufreq as cooling device */ | |
112 | ||
113 | /** | |
82b9ee40 | 114 | * is_cpufreq_valid - function to check frequency transitioning capability. |
02361418 | 115 | * @cpu: cpu for which check is needed. |
82b9ee40 EV |
116 | * |
117 | * This function will check the current state of the system if | |
118 | * it is capable of changing the frequency for a given @cpu. | |
119 | * | |
120 | * Return: 0 if the system is not currently capable of changing | |
121 | * the frequency of given cpu. !0 in case the frequency is changeable. | |
02361418 ADK |
122 | */ |
123 | static int is_cpufreq_valid(int cpu) | |
124 | { | |
125 | struct cpufreq_policy policy; | |
79491e53 | 126 | |
02361418 ADK |
127 | return !cpufreq_get_policy(&policy, cpu); |
128 | } | |
129 | ||
fc35b35c ZR |
130 | enum cpufreq_cooling_property { |
131 | GET_LEVEL, | |
132 | GET_FREQ, | |
133 | GET_MAXL, | |
134 | }; | |
135 | ||
2d6f28fe | 136 | /** |
728c03c9 | 137 | * get_property - fetch a property of interest for a given cpu. |
2d6f28fe EV |
138 | * @cpu: cpu for which the property is required |
139 | * @input: query parameter | |
140 | * @output: query return | |
141 | * @property: type of query (frequency, level, max level) | |
142 | * | |
143 | * This is the common function to | |
fc35b35c ZR |
144 | * 1. get maximum cpu cooling states |
145 | * 2. translate frequency to cooling state | |
146 | * 3. translate cooling state to frequency | |
728c03c9 | 147 | * |
fc35b35c ZR |
148 | * Note that the code may be not in good shape |
149 | * but it is written in this way in order to: | |
150 | * a) reduce duplicate code as most of the code can be shared. | |
151 | * b) make sure the logic is consistent when translating between | |
152 | * cooling states and frequencies. | |
2d6f28fe EV |
153 | * |
154 | * Return: 0 on success, -EINVAL when invalid parameters are passed. | |
155 | */ | |
fc35b35c | 156 | static int get_property(unsigned int cpu, unsigned long input, |
d8892a01 EV |
157 | unsigned int *output, |
158 | enum cpufreq_cooling_property property) | |
02361418 | 159 | { |
3c84ef3a | 160 | int i; |
4469b997 | 161 | unsigned long max_level = 0, level = 0; |
fc35b35c ZR |
162 | unsigned int freq = CPUFREQ_ENTRY_INVALID; |
163 | int descend = -1; | |
3c84ef3a | 164 | struct cpufreq_frequency_table *pos, *table = |
02361418 | 165 | cpufreq_frequency_get_table(cpu); |
79d26401 | 166 | |
fc35b35c ZR |
167 | if (!output) |
168 | return -EINVAL; | |
169 | ||
02361418 | 170 | if (!table) |
fc35b35c | 171 | return -EINVAL; |
02361418 | 172 | |
3c84ef3a | 173 | cpufreq_for_each_valid_entry(pos, table) { |
fc35b35c | 174 | /* ignore duplicate entry */ |
3c84ef3a | 175 | if (freq == pos->frequency) |
fc35b35c ZR |
176 | continue; |
177 | ||
178 | /* get the frequency order */ | |
24c7a381 | 179 | if (freq != CPUFREQ_ENTRY_INVALID && descend == -1) |
3c84ef3a | 180 | descend = freq > pos->frequency; |
02361418 | 181 | |
3c84ef3a | 182 | freq = pos->frequency; |
fc35b35c | 183 | max_level++; |
02361418 | 184 | } |
a116776f ZR |
185 | |
186 | /* No valid cpu frequency entry */ | |
187 | if (max_level == 0) | |
188 | return -EINVAL; | |
189 | ||
1c9573a4 EV |
190 | /* max_level is an index, not a counter */ |
191 | max_level--; | |
02361418 | 192 | |
fc35b35c ZR |
193 | /* get max level */ |
194 | if (property == GET_MAXL) { | |
195 | *output = (unsigned int)max_level; | |
196 | return 0; | |
197 | } | |
02361418 | 198 | |
fc35b35c | 199 | if (property == GET_FREQ) |
1c9573a4 | 200 | level = descend ? input : (max_level - input); |
fc35b35c | 201 | |
3c84ef3a SK |
202 | i = 0; |
203 | cpufreq_for_each_valid_entry(pos, table) { | |
fc35b35c | 204 | /* ignore duplicate entry */ |
3c84ef3a | 205 | if (freq == pos->frequency) |
fc35b35c ZR |
206 | continue; |
207 | ||
208 | /* now we have a valid frequency entry */ | |
3c84ef3a | 209 | freq = pos->frequency; |
fc35b35c ZR |
210 | |
211 | if (property == GET_LEVEL && (unsigned int)input == freq) { | |
212 | /* get level by frequency */ | |
3c84ef3a | 213 | *output = descend ? i : (max_level - i); |
fc35b35c ZR |
214 | return 0; |
215 | } | |
3c84ef3a | 216 | if (property == GET_FREQ && level == i) { |
fc35b35c ZR |
217 | /* get frequency by level */ |
218 | *output = freq; | |
219 | return 0; | |
220 | } | |
3c84ef3a | 221 | i++; |
02361418 | 222 | } |
79491e53 | 223 | |
fc35b35c ZR |
224 | return -EINVAL; |
225 | } | |
226 | ||
44952d33 | 227 | /** |
728c03c9 | 228 | * cpufreq_cooling_get_level - for a given cpu, return the cooling level. |
44952d33 EV |
229 | * @cpu: cpu for which the level is required |
230 | * @freq: the frequency of interest | |
231 | * | |
232 | * This function will match the cooling level corresponding to the | |
233 | * requested @freq and return it. | |
234 | * | |
235 | * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID | |
236 | * otherwise. | |
237 | */ | |
57df8106 ZR |
238 | unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq) |
239 | { | |
240 | unsigned int val; | |
241 | ||
242 | if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL)) | |
243 | return THERMAL_CSTATE_INVALID; | |
79491e53 | 244 | |
57df8106 ZR |
245 | return (unsigned long)val; |
246 | } | |
243dbd9c | 247 | EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level); |
57df8106 | 248 | |
fc35b35c ZR |
249 | /** |
250 | * get_cpu_frequency - get the absolute value of frequency from level. | |
251 | * @cpu: cpu for which frequency is fetched. | |
41518c41 EV |
252 | * @level: cooling level |
253 | * | |
254 | * This function matches cooling level with frequency. Based on a cooling level | |
255 | * of frequency, equals cooling state of cpu cooling device, it will return | |
256 | * the corresponding frequency. | |
fc35b35c | 257 | * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc |
41518c41 EV |
258 | * |
259 | * Return: 0 on error, the corresponding frequency otherwise. | |
fc35b35c ZR |
260 | */ |
261 | static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level) | |
262 | { | |
263 | int ret = 0; | |
264 | unsigned int freq; | |
265 | ||
266 | ret = get_property(cpu, level, &freq, GET_FREQ); | |
267 | if (ret) | |
268 | return 0; | |
79491e53 | 269 | |
fc35b35c | 270 | return freq; |
02361418 ADK |
271 | } |
272 | ||
273 | /** | |
274 | * cpufreq_apply_cooling - function to apply frequency clipping. | |
275 | * @cpufreq_device: cpufreq_cooling_device pointer containing frequency | |
276 | * clipping data. | |
277 | * @cooling_state: value of the cooling state. | |
4b33deb5 EV |
278 | * |
279 | * Function used to make sure the cpufreq layer is aware of current thermal | |
280 | * limits. The limits are applied by updating the cpufreq policy. | |
281 | * | |
282 | * Return: 0 on success, an error code otherwise (-EINVAL in case wrong | |
283 | * cooling state). | |
02361418 ADK |
284 | */ |
285 | static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device, | |
5fda7f68 | 286 | unsigned long cooling_state) |
02361418 ADK |
287 | { |
288 | unsigned int cpuid, clip_freq; | |
bde00663 LNM |
289 | struct cpumask *mask = &cpufreq_device->allowed_cpus; |
290 | unsigned int cpu = cpumask_any(mask); | |
02361418 ADK |
291 | |
292 | ||
293 | /* Check if the old cooling action is same as new cooling action */ | |
294 | if (cpufreq_device->cpufreq_state == cooling_state) | |
295 | return 0; | |
296 | ||
297 | clip_freq = get_cpu_frequency(cpu, cooling_state); | |
298 | if (!clip_freq) | |
299 | return -EINVAL; | |
300 | ||
301 | cpufreq_device->cpufreq_state = cooling_state; | |
302 | cpufreq_device->cpufreq_val = clip_freq; | |
02361418 | 303 | |
bde00663 | 304 | for_each_cpu(cpuid, mask) { |
02361418 ADK |
305 | if (is_cpufreq_valid(cpuid)) |
306 | cpufreq_update_policy(cpuid); | |
307 | } | |
308 | ||
02361418 ADK |
309 | return 0; |
310 | } | |
311 | ||
312 | /** | |
313 | * cpufreq_thermal_notifier - notifier callback for cpufreq policy change. | |
314 | * @nb: struct notifier_block * with callback info. | |
315 | * @event: value showing cpufreq event for which this function invoked. | |
316 | * @data: callback-specific data | |
bab30554 | 317 | * |
9746b6e7 | 318 | * Callback to hijack the notification on cpufreq policy transition. |
bab30554 EV |
319 | * Every time there is a change in policy, we will intercept and |
320 | * update the cpufreq policy with thermal constraints. | |
321 | * | |
322 | * Return: 0 (success) | |
02361418 ADK |
323 | */ |
324 | static int cpufreq_thermal_notifier(struct notifier_block *nb, | |
5fda7f68 | 325 | unsigned long event, void *data) |
02361418 ADK |
326 | { |
327 | struct cpufreq_policy *policy = data; | |
328 | unsigned long max_freq = 0; | |
2dcd851f | 329 | struct cpufreq_cooling_device *cpufreq_dev; |
02361418 | 330 | |
2dcd851f | 331 | if (event != CPUFREQ_ADJUST) |
02361418 ADK |
332 | return 0; |
333 | ||
2dcd851f YSB |
334 | mutex_lock(&cooling_cpufreq_lock); |
335 | list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) { | |
336 | if (!cpumask_test_cpu(policy->cpu, | |
337 | &cpufreq_dev->allowed_cpus)) | |
338 | continue; | |
339 | ||
340 | if (!cpufreq_dev->cpufreq_val) | |
341 | cpufreq_dev->cpufreq_val = get_cpu_frequency( | |
342 | cpumask_any(&cpufreq_dev->allowed_cpus), | |
343 | cpufreq_dev->cpufreq_state); | |
02361418 | 344 | |
2dcd851f | 345 | max_freq = cpufreq_dev->cpufreq_val; |
02361418 | 346 | |
2dcd851f YSB |
347 | if (policy->max != max_freq) |
348 | cpufreq_verify_within_limits(policy, 0, max_freq); | |
349 | } | |
350 | mutex_unlock(&cooling_cpufreq_lock); | |
02361418 ADK |
351 | |
352 | return 0; | |
353 | } | |
354 | ||
1b9e3526 | 355 | /* cpufreq cooling device callback functions are defined below */ |
02361418 ADK |
356 | |
357 | /** | |
358 | * cpufreq_get_max_state - callback function to get the max cooling state. | |
359 | * @cdev: thermal cooling device pointer. | |
360 | * @state: fill this variable with the max cooling state. | |
62c00421 EV |
361 | * |
362 | * Callback for the thermal cooling device to return the cpufreq | |
363 | * max cooling state. | |
364 | * | |
365 | * Return: 0 on success, an error code otherwise. | |
02361418 ADK |
366 | */ |
367 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, | |
368 | unsigned long *state) | |
369 | { | |
160b7d80 | 370 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
bde00663 | 371 | struct cpumask *mask = &cpufreq_device->allowed_cpus; |
02361418 | 372 | unsigned int cpu; |
4f89038f | 373 | unsigned int count = 0; |
fc35b35c | 374 | int ret; |
02361418 | 375 | |
bde00663 | 376 | cpu = cpumask_any(mask); |
02361418 | 377 | |
4f89038f | 378 | ret = get_property(cpu, 0, &count, GET_MAXL); |
9c51b05a | 379 | |
fc35b35c ZR |
380 | if (count > 0) |
381 | *state = count; | |
02361418 | 382 | |
fc35b35c | 383 | return ret; |
02361418 ADK |
384 | } |
385 | ||
386 | /** | |
387 | * cpufreq_get_cur_state - callback function to get the current cooling state. | |
388 | * @cdev: thermal cooling device pointer. | |
389 | * @state: fill this variable with the current cooling state. | |
3672552d EV |
390 | * |
391 | * Callback for the thermal cooling device to return the cpufreq | |
392 | * current cooling state. | |
393 | * | |
394 | * Return: 0 on success, an error code otherwise. | |
02361418 ADK |
395 | */ |
396 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, | |
397 | unsigned long *state) | |
398 | { | |
160b7d80 | 399 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
02361418 | 400 | |
160b7d80 | 401 | *state = cpufreq_device->cpufreq_state; |
79491e53 | 402 | |
160b7d80 | 403 | return 0; |
02361418 ADK |
404 | } |
405 | ||
406 | /** | |
407 | * cpufreq_set_cur_state - callback function to set the current cooling state. | |
408 | * @cdev: thermal cooling device pointer. | |
409 | * @state: set this variable to the current cooling state. | |
56e05fdb EV |
410 | * |
411 | * Callback for the thermal cooling device to change the cpufreq | |
412 | * current cooling state. | |
413 | * | |
414 | * Return: 0 on success, an error code otherwise. | |
02361418 ADK |
415 | */ |
416 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, | |
417 | unsigned long state) | |
418 | { | |
160b7d80 | 419 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
02361418 | 420 | |
160b7d80 | 421 | return cpufreq_apply_cooling(cpufreq_device, state); |
02361418 ADK |
422 | } |
423 | ||
424 | /* Bind cpufreq callbacks to thermal cooling device ops */ | |
425 | static struct thermal_cooling_device_ops const cpufreq_cooling_ops = { | |
426 | .get_max_state = cpufreq_get_max_state, | |
427 | .get_cur_state = cpufreq_get_cur_state, | |
428 | .set_cur_state = cpufreq_set_cur_state, | |
429 | }; | |
430 | ||
431 | /* Notifier for cpufreq policy change */ | |
432 | static struct notifier_block thermal_cpufreq_notifier_block = { | |
433 | .notifier_call = cpufreq_thermal_notifier, | |
434 | }; | |
435 | ||
436 | /** | |
39d99cff EV |
437 | * __cpufreq_cooling_register - helper function to create cpufreq cooling device |
438 | * @np: a valid struct device_node to the cooling device device tree node | |
02361418 | 439 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. |
12cb08ba EV |
440 | * |
441 | * This interface function registers the cpufreq cooling device with the name | |
442 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
39d99cff EV |
443 | * cooling devices. It also gives the opportunity to link the cooling device |
444 | * with a device tree node, in order to bind it via the thermal DT code. | |
12cb08ba EV |
445 | * |
446 | * Return: a valid struct thermal_cooling_device pointer on success, | |
447 | * on failure, it returns a corresponding ERR_PTR(). | |
02361418 | 448 | */ |
39d99cff EV |
449 | static struct thermal_cooling_device * |
450 | __cpufreq_cooling_register(struct device_node *np, | |
451 | const struct cpumask *clip_cpus) | |
02361418 ADK |
452 | { |
453 | struct thermal_cooling_device *cool_dev; | |
454 | struct cpufreq_cooling_device *cpufreq_dev = NULL; | |
160b7d80 | 455 | unsigned int min = 0, max = 0; |
02361418 | 456 | char dev_name[THERMAL_NAME_LENGTH]; |
a4b6fec9 | 457 | int ret = 0, i; |
02361418 ADK |
458 | struct cpufreq_policy policy; |
459 | ||
0f1be51c EV |
460 | if (!cpufreq_frequency_get_table(cpumask_first(clip_cpus))) { |
461 | pr_debug("%s: CPUFreq table not found\n", __func__); | |
462 | return ERR_PTR(-EPROBE_DEFER); | |
463 | } | |
464 | ||
1b9e3526 | 465 | /* Verify that all the clip cpus have same freq_min, freq_max limit */ |
02361418 | 466 | for_each_cpu(i, clip_cpus) { |
1b9e3526 | 467 | /* continue if cpufreq policy not found and not return error */ |
02361418 ADK |
468 | if (!cpufreq_get_policy(&policy, i)) |
469 | continue; | |
470 | if (min == 0 && max == 0) { | |
471 | min = policy.cpuinfo.min_freq; | |
472 | max = policy.cpuinfo.max_freq; | |
473 | } else { | |
474 | if (min != policy.cpuinfo.min_freq || | |
5fda7f68 | 475 | max != policy.cpuinfo.max_freq) |
02361418 | 476 | return ERR_PTR(-EINVAL); |
6b6519df | 477 | } |
02361418 ADK |
478 | } |
479 | cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device), | |
5fda7f68 | 480 | GFP_KERNEL); |
02361418 ADK |
481 | if (!cpufreq_dev) |
482 | return ERR_PTR(-ENOMEM); | |
483 | ||
484 | cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus); | |
485 | ||
02361418 ADK |
486 | ret = get_idr(&cpufreq_idr, &cpufreq_dev->id); |
487 | if (ret) { | |
488 | kfree(cpufreq_dev); | |
489 | return ERR_PTR(-EINVAL); | |
490 | } | |
491 | ||
99871a71 EV |
492 | snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", |
493 | cpufreq_dev->id); | |
02361418 | 494 | |
39d99cff EV |
495 | cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev, |
496 | &cpufreq_cooling_ops); | |
73b9bcd7 | 497 | if (IS_ERR(cool_dev)) { |
02361418 ADK |
498 | release_idr(&cpufreq_idr, cpufreq_dev->id); |
499 | kfree(cpufreq_dev); | |
73b9bcd7 | 500 | return cool_dev; |
02361418 | 501 | } |
02361418 ADK |
502 | cpufreq_dev->cool_dev = cool_dev; |
503 | cpufreq_dev->cpufreq_state = 0; | |
504 | mutex_lock(&cooling_cpufreq_lock); | |
02361418 ADK |
505 | |
506 | /* Register the notifier for first cpufreq cooling device */ | |
507 | if (cpufreq_dev_count == 0) | |
508 | cpufreq_register_notifier(&thermal_cpufreq_notifier_block, | |
5fda7f68 | 509 | CPUFREQ_POLICY_NOTIFIER); |
160b7d80 | 510 | cpufreq_dev_count++; |
2dcd851f | 511 | list_add(&cpufreq_dev->node, &cpufreq_dev_list); |
02361418 ADK |
512 | |
513 | mutex_unlock(&cooling_cpufreq_lock); | |
79491e53 | 514 | |
02361418 ADK |
515 | return cool_dev; |
516 | } | |
39d99cff EV |
517 | |
518 | /** | |
519 | * cpufreq_cooling_register - function to create cpufreq cooling device. | |
520 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. | |
521 | * | |
522 | * This interface function registers the cpufreq cooling device with the name | |
523 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
524 | * cooling devices. | |
525 | * | |
526 | * Return: a valid struct thermal_cooling_device pointer on success, | |
527 | * on failure, it returns a corresponding ERR_PTR(). | |
528 | */ | |
529 | struct thermal_cooling_device * | |
530 | cpufreq_cooling_register(const struct cpumask *clip_cpus) | |
531 | { | |
532 | return __cpufreq_cooling_register(NULL, clip_cpus); | |
533 | } | |
243dbd9c | 534 | EXPORT_SYMBOL_GPL(cpufreq_cooling_register); |
02361418 | 535 | |
39d99cff EV |
536 | /** |
537 | * of_cpufreq_cooling_register - function to create cpufreq cooling device. | |
538 | * @np: a valid struct device_node to the cooling device device tree node | |
539 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. | |
540 | * | |
541 | * This interface function registers the cpufreq cooling device with the name | |
542 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq | |
543 | * cooling devices. Using this API, the cpufreq cooling device will be | |
544 | * linked to the device tree node provided. | |
545 | * | |
546 | * Return: a valid struct thermal_cooling_device pointer on success, | |
547 | * on failure, it returns a corresponding ERR_PTR(). | |
548 | */ | |
549 | struct thermal_cooling_device * | |
550 | of_cpufreq_cooling_register(struct device_node *np, | |
551 | const struct cpumask *clip_cpus) | |
552 | { | |
553 | if (!np) | |
554 | return ERR_PTR(-EINVAL); | |
555 | ||
556 | return __cpufreq_cooling_register(np, clip_cpus); | |
557 | } | |
558 | EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); | |
559 | ||
02361418 ADK |
560 | /** |
561 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. | |
562 | * @cdev: thermal cooling device pointer. | |
135266b4 EV |
563 | * |
564 | * This interface function unregisters the "thermal-cpufreq-%x" cooling device. | |
02361418 ADK |
565 | */ |
566 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) | |
567 | { | |
50e66c7e | 568 | struct cpufreq_cooling_device *cpufreq_dev; |
02361418 | 569 | |
50e66c7e EV |
570 | if (!cdev) |
571 | return; | |
572 | ||
573 | cpufreq_dev = cdev->devdata; | |
02361418 | 574 | mutex_lock(&cooling_cpufreq_lock); |
2dcd851f | 575 | list_del(&cpufreq_dev->node); |
160b7d80 | 576 | cpufreq_dev_count--; |
02361418 ADK |
577 | |
578 | /* Unregister the notifier for the last cpufreq cooling device */ | |
2d9bf71c | 579 | if (cpufreq_dev_count == 0) |
02361418 | 580 | cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block, |
5fda7f68 | 581 | CPUFREQ_POLICY_NOTIFIER); |
02361418 | 582 | mutex_unlock(&cooling_cpufreq_lock); |
160b7d80 | 583 | |
02361418 ADK |
584 | thermal_cooling_device_unregister(cpufreq_dev->cool_dev); |
585 | release_idr(&cpufreq_idr, cpufreq_dev->id); | |
02361418 ADK |
586 | kfree(cpufreq_dev); |
587 | } | |
243dbd9c | 588 | EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister); |