]>
Commit | Line | Data |
---|---|---|
8a67f0ef VK |
1 | /* |
2 | * ARM big.LITTLE Platforms CPUFreq support | |
3 | * | |
4 | * Copyright (C) 2013 ARM Ltd. | |
5 | * Sudeep KarkadaNagesha <[email protected]> | |
6 | * | |
7 | * Copyright (C) 2013 Linaro. | |
8 | * Viresh Kumar <[email protected]> | |
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 version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | * | |
14 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any | |
15 | * kind, whether express or implied; without even the implied warranty | |
16 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | */ | |
19 | ||
20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
21 | ||
22 | #include <linux/clk.h> | |
23 | #include <linux/cpu.h> | |
24 | #include <linux/cpufreq.h> | |
25 | #include <linux/cpumask.h> | |
2f7e8a17 | 26 | #include <linux/cpu_cooling.h> |
8a67f0ef | 27 | #include <linux/export.h> |
39c8bbaf | 28 | #include <linux/module.h> |
e79a23c5 | 29 | #include <linux/mutex.h> |
8a67f0ef | 30 | #include <linux/of_platform.h> |
e4db1c74 | 31 | #include <linux/pm_opp.h> |
8a67f0ef VK |
32 | #include <linux/slab.h> |
33 | #include <linux/topology.h> | |
34 | #include <linux/types.h> | |
35 | ||
36 | #include "arm_big_little.h" | |
37 | ||
38 | /* Currently we support only two clusters */ | |
e79a23c5 VK |
39 | #define A15_CLUSTER 0 |
40 | #define A7_CLUSTER 1 | |
8a67f0ef VK |
41 | #define MAX_CLUSTERS 2 |
42 | ||
e79a23c5 | 43 | #ifdef CONFIG_BL_SWITCHER |
14730145 | 44 | #include <asm/bL_switcher.h> |
45cac118 NP |
45 | static bool bL_switching_enabled; |
46 | #define is_bL_switching_enabled() bL_switching_enabled | |
47 | #define set_switching_enabled(x) (bL_switching_enabled = (x)) | |
e79a23c5 VK |
48 | #else |
49 | #define is_bL_switching_enabled() false | |
45cac118 | 50 | #define set_switching_enabled(x) do { } while (0) |
14730145 SH |
51 | #define bL_switch_request(...) do { } while (0) |
52 | #define bL_switcher_put_enabled() do { } while (0) | |
53 | #define bL_switcher_get_enabled() do { } while (0) | |
e79a23c5 VK |
54 | #endif |
55 | ||
56 | #define ACTUAL_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq << 1 : freq) | |
57 | #define VIRT_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq >> 1 : freq) | |
58 | ||
2f7e8a17 | 59 | static struct thermal_cooling_device *cdev[MAX_CLUSTERS]; |
cd6ce860 | 60 | static const struct cpufreq_arm_bL_ops *arm_bL_ops; |
8a67f0ef | 61 | static struct clk *clk[MAX_CLUSTERS]; |
e79a23c5 VK |
62 | static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS + 1]; |
63 | static atomic_t cluster_usage[MAX_CLUSTERS + 1]; | |
64 | ||
65 | static unsigned int clk_big_min; /* (Big) clock frequencies */ | |
66 | static unsigned int clk_little_max; /* Maximum clock frequency (Little) */ | |
67 | ||
68 | static DEFINE_PER_CPU(unsigned int, physical_cluster); | |
69 | static DEFINE_PER_CPU(unsigned int, cpu_last_req_freq); | |
70 | ||
71 | static struct mutex cluster_lock[MAX_CLUSTERS]; | |
72 | ||
73 | static inline int raw_cpu_to_cluster(int cpu) | |
74 | { | |
75 | return topology_physical_package_id(cpu); | |
76 | } | |
77 | ||
78 | static inline int cpu_to_cluster(int cpu) | |
79 | { | |
80 | return is_bL_switching_enabled() ? | |
81 | MAX_CLUSTERS : raw_cpu_to_cluster(cpu); | |
82 | } | |
83 | ||
84 | static unsigned int find_cluster_maxfreq(int cluster) | |
85 | { | |
86 | int j; | |
87 | u32 max_freq = 0, cpu_freq; | |
88 | ||
89 | for_each_online_cpu(j) { | |
90 | cpu_freq = per_cpu(cpu_last_req_freq, j); | |
91 | ||
92 | if ((cluster == per_cpu(physical_cluster, j)) && | |
93 | (max_freq < cpu_freq)) | |
94 | max_freq = cpu_freq; | |
95 | } | |
96 | ||
97 | pr_debug("%s: cluster: %d, max freq: %d\n", __func__, cluster, | |
98 | max_freq); | |
99 | ||
100 | return max_freq; | |
101 | } | |
102 | ||
103 | static unsigned int clk_get_cpu_rate(unsigned int cpu) | |
104 | { | |
105 | u32 cur_cluster = per_cpu(physical_cluster, cpu); | |
106 | u32 rate = clk_get_rate(clk[cur_cluster]) / 1000; | |
107 | ||
108 | /* For switcher we use virtual A7 clock rates */ | |
109 | if (is_bL_switching_enabled()) | |
110 | rate = VIRT_FREQ(cur_cluster, rate); | |
111 | ||
112 | pr_debug("%s: cpu: %d, cluster: %d, freq: %u\n", __func__, cpu, | |
113 | cur_cluster, rate); | |
114 | ||
115 | return rate; | |
116 | } | |
117 | ||
118 | static unsigned int bL_cpufreq_get_rate(unsigned int cpu) | |
119 | { | |
120 | if (is_bL_switching_enabled()) { | |
121 | pr_debug("%s: freq: %d\n", __func__, per_cpu(cpu_last_req_freq, | |
122 | cpu)); | |
123 | ||
124 | return per_cpu(cpu_last_req_freq, cpu); | |
125 | } else { | |
126 | return clk_get_cpu_rate(cpu); | |
127 | } | |
128 | } | |
8a67f0ef | 129 | |
e79a23c5 VK |
130 | static unsigned int |
131 | bL_cpufreq_set_rate(u32 cpu, u32 old_cluster, u32 new_cluster, u32 rate) | |
8a67f0ef | 132 | { |
e79a23c5 VK |
133 | u32 new_rate, prev_rate; |
134 | int ret; | |
135 | bool bLs = is_bL_switching_enabled(); | |
136 | ||
137 | mutex_lock(&cluster_lock[new_cluster]); | |
138 | ||
139 | if (bLs) { | |
140 | prev_rate = per_cpu(cpu_last_req_freq, cpu); | |
141 | per_cpu(cpu_last_req_freq, cpu) = rate; | |
142 | per_cpu(physical_cluster, cpu) = new_cluster; | |
143 | ||
144 | new_rate = find_cluster_maxfreq(new_cluster); | |
145 | new_rate = ACTUAL_FREQ(new_cluster, new_rate); | |
146 | } else { | |
147 | new_rate = rate; | |
148 | } | |
149 | ||
150 | pr_debug("%s: cpu: %d, old cluster: %d, new cluster: %d, freq: %d\n", | |
151 | __func__, cpu, old_cluster, new_cluster, new_rate); | |
152 | ||
153 | ret = clk_set_rate(clk[new_cluster], new_rate * 1000); | |
14f1ba3a JMT |
154 | if (!ret) { |
155 | /* | |
156 | * FIXME: clk_set_rate hasn't returned an error here however it | |
157 | * may be that clk_change_rate failed due to hardware or | |
158 | * firmware issues and wasn't able to report that due to the | |
159 | * current design of the clk core layer. To work around this | |
160 | * problem we will read back the clock rate and check it is | |
161 | * correct. This needs to be removed once clk core is fixed. | |
162 | */ | |
163 | if (clk_get_rate(clk[new_cluster]) != new_rate * 1000) | |
164 | ret = -EIO; | |
165 | } | |
166 | ||
e79a23c5 VK |
167 | if (WARN_ON(ret)) { |
168 | pr_err("clk_set_rate failed: %d, new cluster: %d\n", ret, | |
169 | new_cluster); | |
170 | if (bLs) { | |
171 | per_cpu(cpu_last_req_freq, cpu) = prev_rate; | |
172 | per_cpu(physical_cluster, cpu) = old_cluster; | |
173 | } | |
174 | ||
175 | mutex_unlock(&cluster_lock[new_cluster]); | |
176 | ||
177 | return ret; | |
178 | } | |
179 | ||
180 | mutex_unlock(&cluster_lock[new_cluster]); | |
181 | ||
182 | /* Recalc freq for old cluster when switching clusters */ | |
183 | if (old_cluster != new_cluster) { | |
184 | pr_debug("%s: cpu: %d, old cluster: %d, new cluster: %d\n", | |
185 | __func__, cpu, old_cluster, new_cluster); | |
186 | ||
187 | /* Switch cluster */ | |
188 | bL_switch_request(cpu, new_cluster); | |
189 | ||
190 | mutex_lock(&cluster_lock[old_cluster]); | |
8a67f0ef | 191 | |
e79a23c5 VK |
192 | /* Set freq of old cluster if there are cpus left on it */ |
193 | new_rate = find_cluster_maxfreq(old_cluster); | |
194 | new_rate = ACTUAL_FREQ(old_cluster, new_rate); | |
195 | ||
196 | if (new_rate) { | |
197 | pr_debug("%s: Updating rate of old cluster: %d, to freq: %d\n", | |
198 | __func__, old_cluster, new_rate); | |
199 | ||
200 | if (clk_set_rate(clk[old_cluster], new_rate * 1000)) | |
201 | pr_err("%s: clk_set_rate failed: %d, old cluster: %d\n", | |
202 | __func__, ret, old_cluster); | |
203 | } | |
204 | mutex_unlock(&cluster_lock[old_cluster]); | |
205 | } | |
206 | ||
207 | return 0; | |
8a67f0ef VK |
208 | } |
209 | ||
8a67f0ef VK |
210 | /* Set clock frequency */ |
211 | static int bL_cpufreq_set_target(struct cpufreq_policy *policy, | |
9c0ebcf7 | 212 | unsigned int index) |
8a67f0ef | 213 | { |
e79a23c5 | 214 | u32 cpu = policy->cpu, cur_cluster, new_cluster, actual_cluster; |
d4019f0a | 215 | unsigned int freqs_new; |
518accf2 | 216 | int ret; |
8a67f0ef | 217 | |
e79a23c5 VK |
218 | cur_cluster = cpu_to_cluster(cpu); |
219 | new_cluster = actual_cluster = per_cpu(physical_cluster, cpu); | |
8a67f0ef | 220 | |
d4019f0a | 221 | freqs_new = freq_table[cur_cluster][index].frequency; |
8a67f0ef | 222 | |
e79a23c5 VK |
223 | if (is_bL_switching_enabled()) { |
224 | if ((actual_cluster == A15_CLUSTER) && | |
d4019f0a | 225 | (freqs_new < clk_big_min)) { |
e79a23c5 VK |
226 | new_cluster = A7_CLUSTER; |
227 | } else if ((actual_cluster == A7_CLUSTER) && | |
d4019f0a | 228 | (freqs_new > clk_little_max)) { |
e79a23c5 VK |
229 | new_cluster = A15_CLUSTER; |
230 | } | |
231 | } | |
232 | ||
518accf2 DE |
233 | ret = bL_cpufreq_set_rate(cpu, actual_cluster, new_cluster, freqs_new); |
234 | ||
235 | if (!ret) { | |
236 | arch_set_freq_scale(policy->related_cpus, freqs_new, | |
237 | policy->cpuinfo.max_freq); | |
238 | } | |
239 | ||
240 | return ret; | |
8a67f0ef VK |
241 | } |
242 | ||
e79a23c5 VK |
243 | static inline u32 get_table_count(struct cpufreq_frequency_table *table) |
244 | { | |
245 | int count; | |
246 | ||
247 | for (count = 0; table[count].frequency != CPUFREQ_TABLE_END; count++) | |
248 | ; | |
249 | ||
250 | return count; | |
251 | } | |
252 | ||
253 | /* get the minimum frequency in the cpufreq_frequency_table */ | |
254 | static inline u32 get_table_min(struct cpufreq_frequency_table *table) | |
255 | { | |
041526f9 | 256 | struct cpufreq_frequency_table *pos; |
e79a23c5 | 257 | uint32_t min_freq = ~0; |
041526f9 SK |
258 | cpufreq_for_each_entry(pos, table) |
259 | if (pos->frequency < min_freq) | |
260 | min_freq = pos->frequency; | |
e79a23c5 VK |
261 | return min_freq; |
262 | } | |
263 | ||
264 | /* get the maximum frequency in the cpufreq_frequency_table */ | |
265 | static inline u32 get_table_max(struct cpufreq_frequency_table *table) | |
266 | { | |
041526f9 | 267 | struct cpufreq_frequency_table *pos; |
e79a23c5 | 268 | uint32_t max_freq = 0; |
041526f9 SK |
269 | cpufreq_for_each_entry(pos, table) |
270 | if (pos->frequency > max_freq) | |
271 | max_freq = pos->frequency; | |
e79a23c5 VK |
272 | return max_freq; |
273 | } | |
274 | ||
275 | static int merge_cluster_tables(void) | |
276 | { | |
277 | int i, j, k = 0, count = 1; | |
278 | struct cpufreq_frequency_table *table; | |
279 | ||
280 | for (i = 0; i < MAX_CLUSTERS; i++) | |
281 | count += get_table_count(freq_table[i]); | |
282 | ||
6396bb22 | 283 | table = kcalloc(count, sizeof(*table), GFP_KERNEL); |
e79a23c5 VK |
284 | if (!table) |
285 | return -ENOMEM; | |
286 | ||
287 | freq_table[MAX_CLUSTERS] = table; | |
288 | ||
289 | /* Add in reverse order to get freqs in increasing order */ | |
290 | for (i = MAX_CLUSTERS - 1; i >= 0; i--) { | |
291 | for (j = 0; freq_table[i][j].frequency != CPUFREQ_TABLE_END; | |
292 | j++) { | |
293 | table[k].frequency = VIRT_FREQ(i, | |
294 | freq_table[i][j].frequency); | |
295 | pr_debug("%s: index: %d, freq: %d\n", __func__, k, | |
296 | table[k].frequency); | |
297 | k++; | |
298 | } | |
299 | } | |
300 | ||
301 | table[k].driver_data = k; | |
302 | table[k].frequency = CPUFREQ_TABLE_END; | |
303 | ||
304 | pr_debug("%s: End, table: %p, count: %d\n", __func__, table, k); | |
305 | ||
306 | return 0; | |
307 | } | |
308 | ||
d9975b0b SH |
309 | static void _put_cluster_clk_and_freq_table(struct device *cpu_dev, |
310 | const struct cpumask *cpumask) | |
e79a23c5 VK |
311 | { |
312 | u32 cluster = raw_cpu_to_cluster(cpu_dev->id); | |
313 | ||
314 | if (!freq_table[cluster]) | |
315 | return; | |
316 | ||
317 | clk_put(clk[cluster]); | |
318 | dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]); | |
493b4cd2 | 319 | if (arm_bL_ops->free_opp_table) |
d9975b0b | 320 | arm_bL_ops->free_opp_table(cpumask); |
e79a23c5 VK |
321 | dev_dbg(cpu_dev, "%s: cluster: %d\n", __func__, cluster); |
322 | } | |
323 | ||
d9975b0b SH |
324 | static void put_cluster_clk_and_freq_table(struct device *cpu_dev, |
325 | const struct cpumask *cpumask) | |
8a67f0ef VK |
326 | { |
327 | u32 cluster = cpu_to_cluster(cpu_dev->id); | |
e79a23c5 VK |
328 | int i; |
329 | ||
330 | if (atomic_dec_return(&cluster_usage[cluster])) | |
331 | return; | |
332 | ||
333 | if (cluster < MAX_CLUSTERS) | |
d9975b0b | 334 | return _put_cluster_clk_and_freq_table(cpu_dev, cpumask); |
8a67f0ef | 335 | |
e79a23c5 VK |
336 | for_each_present_cpu(i) { |
337 | struct device *cdev = get_cpu_device(i); | |
338 | if (!cdev) { | |
339 | pr_err("%s: failed to get cpu%d device\n", __func__, i); | |
340 | return; | |
341 | } | |
342 | ||
d9975b0b | 343 | _put_cluster_clk_and_freq_table(cdev, cpumask); |
8a67f0ef | 344 | } |
e79a23c5 VK |
345 | |
346 | /* free virtual table */ | |
347 | kfree(freq_table[cluster]); | |
8a67f0ef VK |
348 | } |
349 | ||
d9975b0b SH |
350 | static int _get_cluster_clk_and_freq_table(struct device *cpu_dev, |
351 | const struct cpumask *cpumask) | |
8a67f0ef | 352 | { |
e79a23c5 | 353 | u32 cluster = raw_cpu_to_cluster(cpu_dev->id); |
8a67f0ef VK |
354 | int ret; |
355 | ||
e79a23c5 | 356 | if (freq_table[cluster]) |
8a67f0ef VK |
357 | return 0; |
358 | ||
d9975b0b | 359 | ret = arm_bL_ops->init_opp_table(cpumask); |
8a67f0ef VK |
360 | if (ret) { |
361 | dev_err(cpu_dev, "%s: init_opp_table failed, cpu: %d, err: %d\n", | |
362 | __func__, cpu_dev->id, ret); | |
e79a23c5 | 363 | goto out; |
8a67f0ef VK |
364 | } |
365 | ||
5d4879cd | 366 | ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]); |
8a67f0ef VK |
367 | if (ret) { |
368 | dev_err(cpu_dev, "%s: failed to init cpufreq table, cpu: %d, err: %d\n", | |
369 | __func__, cpu_dev->id, ret); | |
493b4cd2 | 370 | goto free_opp_table; |
8a67f0ef VK |
371 | } |
372 | ||
b904f5cc | 373 | clk[cluster] = clk_get(cpu_dev, NULL); |
8a67f0ef VK |
374 | if (!IS_ERR(clk[cluster])) { |
375 | dev_dbg(cpu_dev, "%s: clk: %p & freq table: %p, cluster: %d\n", | |
376 | __func__, clk[cluster], freq_table[cluster], | |
377 | cluster); | |
378 | return 0; | |
379 | } | |
380 | ||
381 | dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n", | |
382 | __func__, cpu_dev->id, cluster); | |
383 | ret = PTR_ERR(clk[cluster]); | |
5d4879cd | 384 | dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]); |
8a67f0ef | 385 | |
493b4cd2 VK |
386 | free_opp_table: |
387 | if (arm_bL_ops->free_opp_table) | |
d9975b0b | 388 | arm_bL_ops->free_opp_table(cpumask); |
e79a23c5 | 389 | out: |
8a67f0ef VK |
390 | dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__, |
391 | cluster); | |
392 | return ret; | |
393 | } | |
394 | ||
d9975b0b SH |
395 | static int get_cluster_clk_and_freq_table(struct device *cpu_dev, |
396 | const struct cpumask *cpumask) | |
e79a23c5 VK |
397 | { |
398 | u32 cluster = cpu_to_cluster(cpu_dev->id); | |
399 | int i, ret; | |
400 | ||
401 | if (atomic_inc_return(&cluster_usage[cluster]) != 1) | |
402 | return 0; | |
403 | ||
404 | if (cluster < MAX_CLUSTERS) { | |
d9975b0b | 405 | ret = _get_cluster_clk_and_freq_table(cpu_dev, cpumask); |
e79a23c5 VK |
406 | if (ret) |
407 | atomic_dec(&cluster_usage[cluster]); | |
408 | return ret; | |
409 | } | |
410 | ||
411 | /* | |
412 | * Get data for all clusters and fill virtual cluster with a merge of | |
413 | * both | |
414 | */ | |
415 | for_each_present_cpu(i) { | |
416 | struct device *cdev = get_cpu_device(i); | |
417 | if (!cdev) { | |
418 | pr_err("%s: failed to get cpu%d device\n", __func__, i); | |
419 | return -ENODEV; | |
420 | } | |
421 | ||
d9975b0b | 422 | ret = _get_cluster_clk_and_freq_table(cdev, cpumask); |
e79a23c5 VK |
423 | if (ret) |
424 | goto put_clusters; | |
425 | } | |
426 | ||
427 | ret = merge_cluster_tables(); | |
428 | if (ret) | |
429 | goto put_clusters; | |
430 | ||
431 | /* Assuming 2 cluster, set clk_big_min and clk_little_max */ | |
432 | clk_big_min = get_table_min(freq_table[0]); | |
433 | clk_little_max = VIRT_FREQ(1, get_table_max(freq_table[1])); | |
434 | ||
435 | pr_debug("%s: cluster: %d, clk_big_min: %d, clk_little_max: %d\n", | |
436 | __func__, cluster, clk_big_min, clk_little_max); | |
437 | ||
438 | return 0; | |
439 | ||
440 | put_clusters: | |
441 | for_each_present_cpu(i) { | |
442 | struct device *cdev = get_cpu_device(i); | |
443 | if (!cdev) { | |
444 | pr_err("%s: failed to get cpu%d device\n", __func__, i); | |
445 | return -ENODEV; | |
446 | } | |
447 | ||
d9975b0b | 448 | _put_cluster_clk_and_freq_table(cdev, cpumask); |
e79a23c5 VK |
449 | } |
450 | ||
451 | atomic_dec(&cluster_usage[cluster]); | |
452 | ||
453 | return ret; | |
454 | } | |
455 | ||
8a67f0ef VK |
456 | /* Per-CPU initialization */ |
457 | static int bL_cpufreq_init(struct cpufreq_policy *policy) | |
458 | { | |
459 | u32 cur_cluster = cpu_to_cluster(policy->cpu); | |
460 | struct device *cpu_dev; | |
461 | int ret; | |
462 | ||
463 | cpu_dev = get_cpu_device(policy->cpu); | |
464 | if (!cpu_dev) { | |
465 | pr_err("%s: failed to get cpu%d device\n", __func__, | |
466 | policy->cpu); | |
467 | return -ENODEV; | |
468 | } | |
469 | ||
e79a23c5 | 470 | if (cur_cluster < MAX_CLUSTERS) { |
8f3ba3d3 VK |
471 | int cpu; |
472 | ||
e79a23c5 VK |
473 | cpumask_copy(policy->cpus, topology_core_cpumask(policy->cpu)); |
474 | ||
8f3ba3d3 VK |
475 | for_each_cpu(cpu, policy->cpus) |
476 | per_cpu(physical_cluster, cpu) = cur_cluster; | |
e79a23c5 VK |
477 | } else { |
478 | /* Assumption: during init, we are always running on A15 */ | |
479 | per_cpu(physical_cluster, policy->cpu) = A15_CLUSTER; | |
480 | } | |
481 | ||
d9975b0b SH |
482 | ret = get_cluster_clk_and_freq_table(cpu_dev, policy->cpus); |
483 | if (ret) | |
484 | return ret; | |
485 | ||
3c1f5a46 | 486 | policy->freq_table = freq_table[cur_cluster]; |
768608a5 VK |
487 | policy->cpuinfo.transition_latency = |
488 | arm_bL_ops->get_transition_latency(cpu_dev); | |
8a67f0ef | 489 | |
e79a23c5 VK |
490 | if (is_bL_switching_enabled()) |
491 | per_cpu(cpu_last_req_freq, policy->cpu) = clk_get_cpu_rate(policy->cpu); | |
8a67f0ef | 492 | |
2b80f313 | 493 | dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu); |
8a67f0ef VK |
494 | return 0; |
495 | } | |
496 | ||
497 | static int bL_cpufreq_exit(struct cpufreq_policy *policy) | |
498 | { | |
499 | struct device *cpu_dev; | |
2f7e8a17 PA |
500 | int cur_cluster = cpu_to_cluster(policy->cpu); |
501 | ||
502 | if (cur_cluster < MAX_CLUSTERS) { | |
503 | cpufreq_cooling_unregister(cdev[cur_cluster]); | |
504 | cdev[cur_cluster] = NULL; | |
505 | } | |
8a67f0ef VK |
506 | |
507 | cpu_dev = get_cpu_device(policy->cpu); | |
508 | if (!cpu_dev) { | |
509 | pr_err("%s: failed to get cpu%d device\n", __func__, | |
510 | policy->cpu); | |
511 | return -ENODEV; | |
512 | } | |
513 | ||
d9975b0b | 514 | put_cluster_clk_and_freq_table(cpu_dev, policy->related_cpus); |
8a67f0ef VK |
515 | dev_dbg(cpu_dev, "%s: Exited, cpu: %d\n", __func__, policy->cpu); |
516 | ||
517 | return 0; | |
518 | } | |
519 | ||
2f7e8a17 PA |
520 | static void bL_cpufreq_ready(struct cpufreq_policy *policy) |
521 | { | |
2f7e8a17 | 522 | int cur_cluster = cpu_to_cluster(policy->cpu); |
2f7e8a17 PA |
523 | |
524 | /* Do not register a cpu_cooling device if we are in IKS mode */ | |
525 | if (cur_cluster >= MAX_CLUSTERS) | |
526 | return; | |
527 | ||
3ebb62ff | 528 | cdev[cur_cluster] = of_cpufreq_cooling_register(policy); |
2f7e8a17 PA |
529 | } |
530 | ||
8a67f0ef VK |
531 | static struct cpufreq_driver bL_cpufreq_driver = { |
532 | .name = "arm-big-little", | |
0b981e70 | 533 | .flags = CPUFREQ_STICKY | |
ae6b4271 VK |
534 | CPUFREQ_HAVE_GOVERNOR_PER_POLICY | |
535 | CPUFREQ_NEED_INITIAL_FREQ_CHECK, | |
3c75a150 | 536 | .verify = cpufreq_generic_frequency_table_verify, |
9c0ebcf7 | 537 | .target_index = bL_cpufreq_set_target, |
e79a23c5 | 538 | .get = bL_cpufreq_get_rate, |
8a67f0ef VK |
539 | .init = bL_cpufreq_init, |
540 | .exit = bL_cpufreq_exit, | |
2f7e8a17 | 541 | .ready = bL_cpufreq_ready, |
3c75a150 | 542 | .attr = cpufreq_generic_attr, |
8a67f0ef VK |
543 | }; |
544 | ||
14730145 | 545 | #ifdef CONFIG_BL_SWITCHER |
45cac118 NP |
546 | static int bL_cpufreq_switcher_notifier(struct notifier_block *nfb, |
547 | unsigned long action, void *_arg) | |
548 | { | |
549 | pr_debug("%s: action: %ld\n", __func__, action); | |
550 | ||
551 | switch (action) { | |
552 | case BL_NOTIFY_PRE_ENABLE: | |
553 | case BL_NOTIFY_PRE_DISABLE: | |
554 | cpufreq_unregister_driver(&bL_cpufreq_driver); | |
555 | break; | |
556 | ||
557 | case BL_NOTIFY_POST_ENABLE: | |
558 | set_switching_enabled(true); | |
559 | cpufreq_register_driver(&bL_cpufreq_driver); | |
560 | break; | |
561 | ||
562 | case BL_NOTIFY_POST_DISABLE: | |
563 | set_switching_enabled(false); | |
564 | cpufreq_register_driver(&bL_cpufreq_driver); | |
565 | break; | |
566 | ||
567 | default: | |
568 | return NOTIFY_DONE; | |
569 | } | |
570 | ||
571 | return NOTIFY_OK; | |
572 | } | |
573 | ||
574 | static struct notifier_block bL_switcher_notifier = { | |
575 | .notifier_call = bL_cpufreq_switcher_notifier, | |
576 | }; | |
577 | ||
14730145 SH |
578 | static int __bLs_register_notifier(void) |
579 | { | |
580 | return bL_switcher_register_notifier(&bL_switcher_notifier); | |
581 | } | |
582 | ||
583 | static int __bLs_unregister_notifier(void) | |
584 | { | |
585 | return bL_switcher_unregister_notifier(&bL_switcher_notifier); | |
586 | } | |
587 | #else | |
588 | static int __bLs_register_notifier(void) { return 0; } | |
589 | static int __bLs_unregister_notifier(void) { return 0; } | |
590 | #endif | |
591 | ||
cd6ce860 | 592 | int bL_cpufreq_register(const struct cpufreq_arm_bL_ops *ops) |
8a67f0ef | 593 | { |
e79a23c5 | 594 | int ret, i; |
8a67f0ef VK |
595 | |
596 | if (arm_bL_ops) { | |
597 | pr_debug("%s: Already registered: %s, exiting\n", __func__, | |
598 | arm_bL_ops->name); | |
599 | return -EBUSY; | |
600 | } | |
601 | ||
768608a5 VK |
602 | if (!ops || !strlen(ops->name) || !ops->init_opp_table || |
603 | !ops->get_transition_latency) { | |
8a67f0ef VK |
604 | pr_err("%s: Invalid arm_bL_ops, exiting\n", __func__); |
605 | return -ENODEV; | |
606 | } | |
607 | ||
608 | arm_bL_ops = ops; | |
609 | ||
14730145 | 610 | set_switching_enabled(bL_switcher_get_enabled()); |
45cac118 | 611 | |
e79a23c5 VK |
612 | for (i = 0; i < MAX_CLUSTERS; i++) |
613 | mutex_init(&cluster_lock[i]); | |
614 | ||
8a67f0ef VK |
615 | ret = cpufreq_register_driver(&bL_cpufreq_driver); |
616 | if (ret) { | |
617 | pr_info("%s: Failed registering platform driver: %s, err: %d\n", | |
618 | __func__, ops->name, ret); | |
619 | arm_bL_ops = NULL; | |
620 | } else { | |
14730145 | 621 | ret = __bLs_register_notifier(); |
45cac118 NP |
622 | if (ret) { |
623 | cpufreq_unregister_driver(&bL_cpufreq_driver); | |
624 | arm_bL_ops = NULL; | |
625 | } else { | |
626 | pr_info("%s: Registered platform driver: %s\n", | |
627 | __func__, ops->name); | |
628 | } | |
8a67f0ef VK |
629 | } |
630 | ||
45cac118 | 631 | bL_switcher_put_enabled(); |
8a67f0ef VK |
632 | return ret; |
633 | } | |
634 | EXPORT_SYMBOL_GPL(bL_cpufreq_register); | |
635 | ||
cd6ce860 | 636 | void bL_cpufreq_unregister(const struct cpufreq_arm_bL_ops *ops) |
8a67f0ef VK |
637 | { |
638 | if (arm_bL_ops != ops) { | |
639 | pr_err("%s: Registered with: %s, can't unregister, exiting\n", | |
640 | __func__, arm_bL_ops->name); | |
641 | return; | |
642 | } | |
643 | ||
45cac118 | 644 | bL_switcher_get_enabled(); |
14730145 | 645 | __bLs_unregister_notifier(); |
8a67f0ef | 646 | cpufreq_unregister_driver(&bL_cpufreq_driver); |
45cac118 | 647 | bL_switcher_put_enabled(); |
8a67f0ef VK |
648 | pr_info("%s: Un-registered platform driver: %s\n", __func__, |
649 | arm_bL_ops->name); | |
650 | arm_bL_ops = NULL; | |
651 | } | |
652 | EXPORT_SYMBOL_GPL(bL_cpufreq_unregister); | |
39c8bbaf UKK |
653 | |
654 | MODULE_AUTHOR("Viresh Kumar <[email protected]>"); | |
655 | MODULE_DESCRIPTION("Generic ARM big LITTLE cpufreq driver"); | |
656 | MODULE_LICENSE("GPL v2"); |