blob: e4d75ca9f3b07de20137a2ba96216d17e5141f0b [file] [log] [blame]
Viresh Kumar8a67f0e2013-04-01 12:57:49 +00001/*
2 * ARM big.LITTLE Platforms CPUFreq support
3 *
4 * Copyright (C) 2013 ARM Ltd.
5 * Sudeep KarkadaNagesha <sudeep.karkadanagesha@arm.com>
6 *
7 * Copyright (C) 2013 Linaro.
8 * Viresh Kumar <viresh.kumar@linaro.org>
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>
26#include <linux/export.h>
Uwe Kleine-König39c8bba2014-08-08 08:56:30 +020027#include <linux/module.h>
Viresh Kumare79a23c2013-10-30 15:44:40 -040028#include <linux/mutex.h>
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000029#include <linux/of_platform.h>
Nishanth Menone4db1c72013-09-19 16:03:52 -050030#include <linux/pm_opp.h>
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000031#include <linux/slab.h>
32#include <linux/topology.h>
33#include <linux/types.h>
Viresh Kumare79a23c2013-10-30 15:44:40 -040034#include <asm/bL_switcher.h>
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000035
36#include "arm_big_little.h"
37
38/* Currently we support only two clusters */
Viresh Kumare79a23c2013-10-30 15:44:40 -040039#define A15_CLUSTER 0
40#define A7_CLUSTER 1
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000041#define MAX_CLUSTERS 2
42
Viresh Kumare79a23c2013-10-30 15:44:40 -040043#ifdef CONFIG_BL_SWITCHER
Nicolas Pitre45cac112013-10-30 15:44:41 -040044static bool bL_switching_enabled;
45#define is_bL_switching_enabled() bL_switching_enabled
46#define set_switching_enabled(x) (bL_switching_enabled = (x))
Viresh Kumare79a23c2013-10-30 15:44:40 -040047#else
48#define is_bL_switching_enabled() false
Nicolas Pitre45cac112013-10-30 15:44:41 -040049#define set_switching_enabled(x) do { } while (0)
Viresh Kumare79a23c2013-10-30 15:44:40 -040050#endif
51
52#define ACTUAL_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq << 1 : freq)
53#define VIRT_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq >> 1 : freq)
54
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000055static struct cpufreq_arm_bL_ops *arm_bL_ops;
56static struct clk *clk[MAX_CLUSTERS];
Viresh Kumare79a23c2013-10-30 15:44:40 -040057static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS + 1];
58static atomic_t cluster_usage[MAX_CLUSTERS + 1];
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000059
Viresh Kumare79a23c2013-10-30 15:44:40 -040060static unsigned int clk_big_min; /* (Big) clock frequencies */
61static unsigned int clk_little_max; /* Maximum clock frequency (Little) */
62
63static DEFINE_PER_CPU(unsigned int, physical_cluster);
64static DEFINE_PER_CPU(unsigned int, cpu_last_req_freq);
65
66static struct mutex cluster_lock[MAX_CLUSTERS];
67
68static inline int raw_cpu_to_cluster(int cpu)
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000069{
Viresh Kumare79a23c2013-10-30 15:44:40 -040070 return topology_physical_package_id(cpu);
71}
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000072
Viresh Kumare79a23c2013-10-30 15:44:40 -040073static inline int cpu_to_cluster(int cpu)
74{
75 return is_bL_switching_enabled() ?
76 MAX_CLUSTERS : raw_cpu_to_cluster(cpu);
77}
78
79static unsigned int find_cluster_maxfreq(int cluster)
80{
81 int j;
82 u32 max_freq = 0, cpu_freq;
83
84 for_each_online_cpu(j) {
85 cpu_freq = per_cpu(cpu_last_req_freq, j);
86
87 if ((cluster == per_cpu(physical_cluster, j)) &&
88 (max_freq < cpu_freq))
89 max_freq = cpu_freq;
90 }
91
92 pr_debug("%s: cluster: %d, max freq: %d\n", __func__, cluster,
93 max_freq);
94
95 return max_freq;
96}
97
98static unsigned int clk_get_cpu_rate(unsigned int cpu)
99{
100 u32 cur_cluster = per_cpu(physical_cluster, cpu);
101 u32 rate = clk_get_rate(clk[cur_cluster]) / 1000;
102
103 /* For switcher we use virtual A7 clock rates */
104 if (is_bL_switching_enabled())
105 rate = VIRT_FREQ(cur_cluster, rate);
106
107 pr_debug("%s: cpu: %d, cluster: %d, freq: %u\n", __func__, cpu,
108 cur_cluster, rate);
109
110 return rate;
111}
112
113static unsigned int bL_cpufreq_get_rate(unsigned int cpu)
114{
115 if (is_bL_switching_enabled()) {
116 pr_debug("%s: freq: %d\n", __func__, per_cpu(cpu_last_req_freq,
117 cpu));
118
119 return per_cpu(cpu_last_req_freq, cpu);
120 } else {
121 return clk_get_cpu_rate(cpu);
122 }
123}
124
125static unsigned int
126bL_cpufreq_set_rate(u32 cpu, u32 old_cluster, u32 new_cluster, u32 rate)
127{
128 u32 new_rate, prev_rate;
129 int ret;
130 bool bLs = is_bL_switching_enabled();
131
132 mutex_lock(&cluster_lock[new_cluster]);
133
134 if (bLs) {
135 prev_rate = per_cpu(cpu_last_req_freq, cpu);
136 per_cpu(cpu_last_req_freq, cpu) = rate;
137 per_cpu(physical_cluster, cpu) = new_cluster;
138
139 new_rate = find_cluster_maxfreq(new_cluster);
140 new_rate = ACTUAL_FREQ(new_cluster, new_rate);
141 } else {
142 new_rate = rate;
143 }
144
145 pr_debug("%s: cpu: %d, old cluster: %d, new cluster: %d, freq: %d\n",
146 __func__, cpu, old_cluster, new_cluster, new_rate);
147
148 ret = clk_set_rate(clk[new_cluster], new_rate * 1000);
149 if (WARN_ON(ret)) {
150 pr_err("clk_set_rate failed: %d, new cluster: %d\n", ret,
151 new_cluster);
152 if (bLs) {
153 per_cpu(cpu_last_req_freq, cpu) = prev_rate;
154 per_cpu(physical_cluster, cpu) = old_cluster;
155 }
156
157 mutex_unlock(&cluster_lock[new_cluster]);
158
159 return ret;
160 }
161
162 mutex_unlock(&cluster_lock[new_cluster]);
163
164 /* Recalc freq for old cluster when switching clusters */
165 if (old_cluster != new_cluster) {
166 pr_debug("%s: cpu: %d, old cluster: %d, new cluster: %d\n",
167 __func__, cpu, old_cluster, new_cluster);
168
169 /* Switch cluster */
170 bL_switch_request(cpu, new_cluster);
171
172 mutex_lock(&cluster_lock[old_cluster]);
173
174 /* Set freq of old cluster if there are cpus left on it */
175 new_rate = find_cluster_maxfreq(old_cluster);
176 new_rate = ACTUAL_FREQ(old_cluster, new_rate);
177
178 if (new_rate) {
179 pr_debug("%s: Updating rate of old cluster: %d, to freq: %d\n",
180 __func__, old_cluster, new_rate);
181
182 if (clk_set_rate(clk[old_cluster], new_rate * 1000))
183 pr_err("%s: clk_set_rate failed: %d, old cluster: %d\n",
184 __func__, ret, old_cluster);
185 }
186 mutex_unlock(&cluster_lock[old_cluster]);
187 }
188
Sudeep Holla0a95e632015-04-27 10:51:05 +0100189 /*
190 * FIXME: clk_set_rate has to handle the case where clk_change_rate
191 * can fail due to hardware or firmware issues. Until the clk core
192 * layer is fixed, we can check here. In most of the cases we will
193 * be reading only the cached value anyway. This needs to be removed
194 * once clk core is fixed.
195 */
196 if (bL_cpufreq_get_rate(cpu) != new_rate)
197 return -EIO;
Viresh Kumare79a23c2013-10-30 15:44:40 -0400198 return 0;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000199}
200
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000201/* Set clock frequency */
202static int bL_cpufreq_set_target(struct cpufreq_policy *policy,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530203 unsigned int index)
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000204{
Viresh Kumare79a23c2013-10-30 15:44:40 -0400205 u32 cpu = policy->cpu, cur_cluster, new_cluster, actual_cluster;
Viresh Kumard4019f02013-08-14 19:38:24 +0530206 unsigned int freqs_new;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000207
Viresh Kumare79a23c2013-10-30 15:44:40 -0400208 cur_cluster = cpu_to_cluster(cpu);
209 new_cluster = actual_cluster = per_cpu(physical_cluster, cpu);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000210
Viresh Kumard4019f02013-08-14 19:38:24 +0530211 freqs_new = freq_table[cur_cluster][index].frequency;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000212
Viresh Kumare79a23c2013-10-30 15:44:40 -0400213 if (is_bL_switching_enabled()) {
214 if ((actual_cluster == A15_CLUSTER) &&
Viresh Kumard4019f02013-08-14 19:38:24 +0530215 (freqs_new < clk_big_min)) {
Viresh Kumare79a23c2013-10-30 15:44:40 -0400216 new_cluster = A7_CLUSTER;
217 } else if ((actual_cluster == A7_CLUSTER) &&
Viresh Kumard4019f02013-08-14 19:38:24 +0530218 (freqs_new > clk_little_max)) {
Viresh Kumare79a23c2013-10-30 15:44:40 -0400219 new_cluster = A15_CLUSTER;
220 }
221 }
222
Viresh Kumard4019f02013-08-14 19:38:24 +0530223 return bL_cpufreq_set_rate(cpu, actual_cluster, new_cluster, freqs_new);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000224}
225
Viresh Kumare79a23c2013-10-30 15:44:40 -0400226static inline u32 get_table_count(struct cpufreq_frequency_table *table)
227{
228 int count;
229
230 for (count = 0; table[count].frequency != CPUFREQ_TABLE_END; count++)
231 ;
232
233 return count;
234}
235
236/* get the minimum frequency in the cpufreq_frequency_table */
237static inline u32 get_table_min(struct cpufreq_frequency_table *table)
238{
Stratos Karafotis041526f2014-04-25 23:15:38 +0300239 struct cpufreq_frequency_table *pos;
Viresh Kumare79a23c2013-10-30 15:44:40 -0400240 uint32_t min_freq = ~0;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300241 cpufreq_for_each_entry(pos, table)
242 if (pos->frequency < min_freq)
243 min_freq = pos->frequency;
Viresh Kumare79a23c2013-10-30 15:44:40 -0400244 return min_freq;
245}
246
247/* get the maximum frequency in the cpufreq_frequency_table */
248static inline u32 get_table_max(struct cpufreq_frequency_table *table)
249{
Stratos Karafotis041526f2014-04-25 23:15:38 +0300250 struct cpufreq_frequency_table *pos;
Viresh Kumare79a23c2013-10-30 15:44:40 -0400251 uint32_t max_freq = 0;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300252 cpufreq_for_each_entry(pos, table)
253 if (pos->frequency > max_freq)
254 max_freq = pos->frequency;
Viresh Kumare79a23c2013-10-30 15:44:40 -0400255 return max_freq;
256}
257
258static int merge_cluster_tables(void)
259{
260 int i, j, k = 0, count = 1;
261 struct cpufreq_frequency_table *table;
262
263 for (i = 0; i < MAX_CLUSTERS; i++)
264 count += get_table_count(freq_table[i]);
265
266 table = kzalloc(sizeof(*table) * count, GFP_KERNEL);
267 if (!table)
268 return -ENOMEM;
269
270 freq_table[MAX_CLUSTERS] = table;
271
272 /* Add in reverse order to get freqs in increasing order */
273 for (i = MAX_CLUSTERS - 1; i >= 0; i--) {
274 for (j = 0; freq_table[i][j].frequency != CPUFREQ_TABLE_END;
275 j++) {
276 table[k].frequency = VIRT_FREQ(i,
277 freq_table[i][j].frequency);
278 pr_debug("%s: index: %d, freq: %d\n", __func__, k,
279 table[k].frequency);
280 k++;
281 }
282 }
283
284 table[k].driver_data = k;
285 table[k].frequency = CPUFREQ_TABLE_END;
286
287 pr_debug("%s: End, table: %p, count: %d\n", __func__, table, k);
288
289 return 0;
290}
291
292static void _put_cluster_clk_and_freq_table(struct device *cpu_dev)
293{
294 u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
295
296 if (!freq_table[cluster])
297 return;
298
299 clk_put(clk[cluster]);
300 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
Viresh Kumar493b4cd2014-11-25 16:04:20 +0530301 if (arm_bL_ops->free_opp_table)
302 arm_bL_ops->free_opp_table(cpu_dev);
Viresh Kumare79a23c2013-10-30 15:44:40 -0400303 dev_dbg(cpu_dev, "%s: cluster: %d\n", __func__, cluster);
304}
305
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000306static void put_cluster_clk_and_freq_table(struct device *cpu_dev)
307{
308 u32 cluster = cpu_to_cluster(cpu_dev->id);
Viresh Kumare79a23c2013-10-30 15:44:40 -0400309 int i;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000310
Viresh Kumare79a23c2013-10-30 15:44:40 -0400311 if (atomic_dec_return(&cluster_usage[cluster]))
312 return;
313
314 if (cluster < MAX_CLUSTERS)
315 return _put_cluster_clk_and_freq_table(cpu_dev);
316
317 for_each_present_cpu(i) {
318 struct device *cdev = get_cpu_device(i);
319 if (!cdev) {
320 pr_err("%s: failed to get cpu%d device\n", __func__, i);
321 return;
322 }
323
324 _put_cluster_clk_and_freq_table(cdev);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000325 }
Viresh Kumare79a23c2013-10-30 15:44:40 -0400326
327 /* free virtual table */
328 kfree(freq_table[cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000329}
330
Viresh Kumare79a23c2013-10-30 15:44:40 -0400331static int _get_cluster_clk_and_freq_table(struct device *cpu_dev)
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000332{
Viresh Kumare79a23c2013-10-30 15:44:40 -0400333 u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000334 int ret;
335
Viresh Kumare79a23c2013-10-30 15:44:40 -0400336 if (freq_table[cluster])
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000337 return 0;
338
339 ret = arm_bL_ops->init_opp_table(cpu_dev);
340 if (ret) {
341 dev_err(cpu_dev, "%s: init_opp_table failed, cpu: %d, err: %d\n",
342 __func__, cpu_dev->id, ret);
Viresh Kumare79a23c2013-10-30 15:44:40 -0400343 goto out;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000344 }
345
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500346 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000347 if (ret) {
348 dev_err(cpu_dev, "%s: failed to init cpufreq table, cpu: %d, err: %d\n",
349 __func__, cpu_dev->id, ret);
Viresh Kumar493b4cd2014-11-25 16:04:20 +0530350 goto free_opp_table;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000351 }
352
Sudeep Hollab904f5c2015-04-27 10:51:06 +0100353 clk[cluster] = clk_get(cpu_dev, NULL);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000354 if (!IS_ERR(clk[cluster])) {
355 dev_dbg(cpu_dev, "%s: clk: %p & freq table: %p, cluster: %d\n",
356 __func__, clk[cluster], freq_table[cluster],
357 cluster);
358 return 0;
359 }
360
361 dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
362 __func__, cpu_dev->id, cluster);
363 ret = PTR_ERR(clk[cluster]);
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500364 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000365
Viresh Kumar493b4cd2014-11-25 16:04:20 +0530366free_opp_table:
367 if (arm_bL_ops->free_opp_table)
368 arm_bL_ops->free_opp_table(cpu_dev);
Viresh Kumare79a23c2013-10-30 15:44:40 -0400369out:
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000370 dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__,
371 cluster);
372 return ret;
373}
374
Viresh Kumare79a23c2013-10-30 15:44:40 -0400375static int get_cluster_clk_and_freq_table(struct device *cpu_dev)
376{
377 u32 cluster = cpu_to_cluster(cpu_dev->id);
378 int i, ret;
379
380 if (atomic_inc_return(&cluster_usage[cluster]) != 1)
381 return 0;
382
383 if (cluster < MAX_CLUSTERS) {
384 ret = _get_cluster_clk_and_freq_table(cpu_dev);
385 if (ret)
386 atomic_dec(&cluster_usage[cluster]);
387 return ret;
388 }
389
390 /*
391 * Get data for all clusters and fill virtual cluster with a merge of
392 * both
393 */
394 for_each_present_cpu(i) {
395 struct device *cdev = get_cpu_device(i);
396 if (!cdev) {
397 pr_err("%s: failed to get cpu%d device\n", __func__, i);
398 return -ENODEV;
399 }
400
401 ret = _get_cluster_clk_and_freq_table(cdev);
402 if (ret)
403 goto put_clusters;
404 }
405
406 ret = merge_cluster_tables();
407 if (ret)
408 goto put_clusters;
409
410 /* Assuming 2 cluster, set clk_big_min and clk_little_max */
411 clk_big_min = get_table_min(freq_table[0]);
412 clk_little_max = VIRT_FREQ(1, get_table_max(freq_table[1]));
413
414 pr_debug("%s: cluster: %d, clk_big_min: %d, clk_little_max: %d\n",
415 __func__, cluster, clk_big_min, clk_little_max);
416
417 return 0;
418
419put_clusters:
420 for_each_present_cpu(i) {
421 struct device *cdev = get_cpu_device(i);
422 if (!cdev) {
423 pr_err("%s: failed to get cpu%d device\n", __func__, i);
424 return -ENODEV;
425 }
426
427 _put_cluster_clk_and_freq_table(cdev);
428 }
429
430 atomic_dec(&cluster_usage[cluster]);
431
432 return ret;
433}
434
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000435/* Per-CPU initialization */
436static int bL_cpufreq_init(struct cpufreq_policy *policy)
437{
438 u32 cur_cluster = cpu_to_cluster(policy->cpu);
439 struct device *cpu_dev;
440 int ret;
441
442 cpu_dev = get_cpu_device(policy->cpu);
443 if (!cpu_dev) {
444 pr_err("%s: failed to get cpu%d device\n", __func__,
445 policy->cpu);
446 return -ENODEV;
447 }
448
449 ret = get_cluster_clk_and_freq_table(cpu_dev);
450 if (ret)
451 return ret;
452
Viresh Kumar39b10eb2013-09-16 18:56:08 +0530453 ret = cpufreq_table_validate_and_show(policy, freq_table[cur_cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000454 if (ret) {
455 dev_err(cpu_dev, "CPU %d, cluster: %d invalid freq table\n",
456 policy->cpu, cur_cluster);
457 put_cluster_clk_and_freq_table(cpu_dev);
458 return ret;
459 }
460
Viresh Kumare79a23c2013-10-30 15:44:40 -0400461 if (cur_cluster < MAX_CLUSTERS) {
viresh kumar8f3ba3d2014-03-14 12:10:55 +0530462 int cpu;
463
Viresh Kumare79a23c2013-10-30 15:44:40 -0400464 cpumask_copy(policy->cpus, topology_core_cpumask(policy->cpu));
465
viresh kumar8f3ba3d2014-03-14 12:10:55 +0530466 for_each_cpu(cpu, policy->cpus)
467 per_cpu(physical_cluster, cpu) = cur_cluster;
Viresh Kumare79a23c2013-10-30 15:44:40 -0400468 } else {
469 /* Assumption: during init, we are always running on A15 */
470 per_cpu(physical_cluster, policy->cpu) = A15_CLUSTER;
471 }
472
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000473 if (arm_bL_ops->get_transition_latency)
474 policy->cpuinfo.transition_latency =
475 arm_bL_ops->get_transition_latency(cpu_dev);
476 else
477 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
478
Viresh Kumare79a23c2013-10-30 15:44:40 -0400479 if (is_bL_switching_enabled())
480 per_cpu(cpu_last_req_freq, policy->cpu) = clk_get_cpu_rate(policy->cpu);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000481
Viresh Kumar2b80f312013-04-29 13:24:48 +0000482 dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000483 return 0;
484}
485
486static int bL_cpufreq_exit(struct cpufreq_policy *policy)
487{
488 struct device *cpu_dev;
489
490 cpu_dev = get_cpu_device(policy->cpu);
491 if (!cpu_dev) {
492 pr_err("%s: failed to get cpu%d device\n", __func__,
493 policy->cpu);
494 return -ENODEV;
495 }
496
497 put_cluster_clk_and_freq_table(cpu_dev);
498 dev_dbg(cpu_dev, "%s: Exited, cpu: %d\n", __func__, policy->cpu);
499
500 return 0;
501}
502
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000503static struct cpufreq_driver bL_cpufreq_driver = {
504 .name = "arm-big-little",
Viresh Kumar0b981e72013-10-02 14:13:18 +0530505 .flags = CPUFREQ_STICKY |
Viresh Kumarae6b4272013-12-03 11:20:45 +0530506 CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
507 CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Viresh Kumar3c75a152013-10-03 20:27:57 +0530508 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530509 .target_index = bL_cpufreq_set_target,
Viresh Kumare79a23c2013-10-30 15:44:40 -0400510 .get = bL_cpufreq_get_rate,
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000511 .init = bL_cpufreq_init,
512 .exit = bL_cpufreq_exit,
Viresh Kumar3c75a152013-10-03 20:27:57 +0530513 .attr = cpufreq_generic_attr,
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000514};
515
Nicolas Pitre45cac112013-10-30 15:44:41 -0400516static int bL_cpufreq_switcher_notifier(struct notifier_block *nfb,
517 unsigned long action, void *_arg)
518{
519 pr_debug("%s: action: %ld\n", __func__, action);
520
521 switch (action) {
522 case BL_NOTIFY_PRE_ENABLE:
523 case BL_NOTIFY_PRE_DISABLE:
524 cpufreq_unregister_driver(&bL_cpufreq_driver);
525 break;
526
527 case BL_NOTIFY_POST_ENABLE:
528 set_switching_enabled(true);
529 cpufreq_register_driver(&bL_cpufreq_driver);
530 break;
531
532 case BL_NOTIFY_POST_DISABLE:
533 set_switching_enabled(false);
534 cpufreq_register_driver(&bL_cpufreq_driver);
535 break;
536
537 default:
538 return NOTIFY_DONE;
539 }
540
541 return NOTIFY_OK;
542}
543
544static struct notifier_block bL_switcher_notifier = {
545 .notifier_call = bL_cpufreq_switcher_notifier,
546};
547
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000548int bL_cpufreq_register(struct cpufreq_arm_bL_ops *ops)
549{
Viresh Kumare79a23c2013-10-30 15:44:40 -0400550 int ret, i;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000551
552 if (arm_bL_ops) {
553 pr_debug("%s: Already registered: %s, exiting\n", __func__,
554 arm_bL_ops->name);
555 return -EBUSY;
556 }
557
558 if (!ops || !strlen(ops->name) || !ops->init_opp_table) {
559 pr_err("%s: Invalid arm_bL_ops, exiting\n", __func__);
560 return -ENODEV;
561 }
562
563 arm_bL_ops = ops;
564
Nicolas Pitre45cac112013-10-30 15:44:41 -0400565 ret = bL_switcher_get_enabled();
566 set_switching_enabled(ret);
567
Viresh Kumare79a23c2013-10-30 15:44:40 -0400568 for (i = 0; i < MAX_CLUSTERS; i++)
569 mutex_init(&cluster_lock[i]);
570
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000571 ret = cpufreq_register_driver(&bL_cpufreq_driver);
572 if (ret) {
573 pr_info("%s: Failed registering platform driver: %s, err: %d\n",
574 __func__, ops->name, ret);
575 arm_bL_ops = NULL;
576 } else {
Nicolas Pitre45cac112013-10-30 15:44:41 -0400577 ret = bL_switcher_register_notifier(&bL_switcher_notifier);
578 if (ret) {
579 cpufreq_unregister_driver(&bL_cpufreq_driver);
580 arm_bL_ops = NULL;
581 } else {
582 pr_info("%s: Registered platform driver: %s\n",
583 __func__, ops->name);
584 }
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000585 }
586
Nicolas Pitre45cac112013-10-30 15:44:41 -0400587 bL_switcher_put_enabled();
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000588 return ret;
589}
590EXPORT_SYMBOL_GPL(bL_cpufreq_register);
591
592void bL_cpufreq_unregister(struct cpufreq_arm_bL_ops *ops)
593{
594 if (arm_bL_ops != ops) {
595 pr_err("%s: Registered with: %s, can't unregister, exiting\n",
596 __func__, arm_bL_ops->name);
597 return;
598 }
599
Nicolas Pitre45cac112013-10-30 15:44:41 -0400600 bL_switcher_get_enabled();
601 bL_switcher_unregister_notifier(&bL_switcher_notifier);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000602 cpufreq_unregister_driver(&bL_cpufreq_driver);
Nicolas Pitre45cac112013-10-30 15:44:41 -0400603 bL_switcher_put_enabled();
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000604 pr_info("%s: Un-registered platform driver: %s\n", __func__,
605 arm_bL_ops->name);
606 arm_bL_ops = NULL;
607}
608EXPORT_SYMBOL_GPL(bL_cpufreq_unregister);
Uwe Kleine-König39c8bba2014-08-08 08:56:30 +0200609
610MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
611MODULE_DESCRIPTION("Generic ARM big LITTLE cpufreq driver");
612MODULE_LICENSE("GPL v2");