blob: 71f1af93b5854e1a67117669dd505370885794c9 [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>
Viresh Kumare79a23c2013-10-30 15:44:40 -040027#include <linux/mutex.h>
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000028#include <linux/of_platform.h>
Nishanth Menone4db1c72013-09-19 16:03:52 -050029#include <linux/pm_opp.h>
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000030#include <linux/slab.h>
31#include <linux/topology.h>
32#include <linux/types.h>
Viresh Kumare79a23c2013-10-30 15:44:40 -040033#include <asm/bL_switcher.h>
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000034
35#include "arm_big_little.h"
36
37/* Currently we support only two clusters */
Viresh Kumare79a23c2013-10-30 15:44:40 -040038#define A15_CLUSTER 0
39#define A7_CLUSTER 1
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000040#define MAX_CLUSTERS 2
41
Viresh Kumare79a23c2013-10-30 15:44:40 -040042#ifdef CONFIG_BL_SWITCHER
Nicolas Pitre45cac112013-10-30 15:44:41 -040043static bool bL_switching_enabled;
44#define is_bL_switching_enabled() bL_switching_enabled
45#define set_switching_enabled(x) (bL_switching_enabled = (x))
Viresh Kumare79a23c2013-10-30 15:44:40 -040046#else
47#define is_bL_switching_enabled() false
Nicolas Pitre45cac112013-10-30 15:44:41 -040048#define set_switching_enabled(x) do { } while (0)
Viresh Kumare79a23c2013-10-30 15:44:40 -040049#endif
50
51#define ACTUAL_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq << 1 : freq)
52#define VIRT_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq >> 1 : freq)
53
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000054static struct cpufreq_arm_bL_ops *arm_bL_ops;
55static struct clk *clk[MAX_CLUSTERS];
Viresh Kumare79a23c2013-10-30 15:44:40 -040056static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS + 1];
57static atomic_t cluster_usage[MAX_CLUSTERS + 1];
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000058
Viresh Kumare79a23c2013-10-30 15:44:40 -040059static unsigned int clk_big_min; /* (Big) clock frequencies */
60static unsigned int clk_little_max; /* Maximum clock frequency (Little) */
61
62static DEFINE_PER_CPU(unsigned int, physical_cluster);
63static DEFINE_PER_CPU(unsigned int, cpu_last_req_freq);
64
65static struct mutex cluster_lock[MAX_CLUSTERS];
66
67static inline int raw_cpu_to_cluster(int cpu)
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000068{
Viresh Kumare79a23c2013-10-30 15:44:40 -040069 return topology_physical_package_id(cpu);
70}
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000071
Viresh Kumare79a23c2013-10-30 15:44:40 -040072static inline int cpu_to_cluster(int cpu)
73{
74 return is_bL_switching_enabled() ?
75 MAX_CLUSTERS : raw_cpu_to_cluster(cpu);
76}
77
78static unsigned int find_cluster_maxfreq(int cluster)
79{
80 int j;
81 u32 max_freq = 0, cpu_freq;
82
83 for_each_online_cpu(j) {
84 cpu_freq = per_cpu(cpu_last_req_freq, j);
85
86 if ((cluster == per_cpu(physical_cluster, j)) &&
87 (max_freq < cpu_freq))
88 max_freq = cpu_freq;
89 }
90
91 pr_debug("%s: cluster: %d, max freq: %d\n", __func__, cluster,
92 max_freq);
93
94 return max_freq;
95}
96
97static unsigned int clk_get_cpu_rate(unsigned int cpu)
98{
99 u32 cur_cluster = per_cpu(physical_cluster, cpu);
100 u32 rate = clk_get_rate(clk[cur_cluster]) / 1000;
101
102 /* For switcher we use virtual A7 clock rates */
103 if (is_bL_switching_enabled())
104 rate = VIRT_FREQ(cur_cluster, rate);
105
106 pr_debug("%s: cpu: %d, cluster: %d, freq: %u\n", __func__, cpu,
107 cur_cluster, rate);
108
109 return rate;
110}
111
112static unsigned int bL_cpufreq_get_rate(unsigned int cpu)
113{
114 if (is_bL_switching_enabled()) {
115 pr_debug("%s: freq: %d\n", __func__, per_cpu(cpu_last_req_freq,
116 cpu));
117
118 return per_cpu(cpu_last_req_freq, cpu);
119 } else {
120 return clk_get_cpu_rate(cpu);
121 }
122}
123
124static unsigned int
125bL_cpufreq_set_rate(u32 cpu, u32 old_cluster, u32 new_cluster, u32 rate)
126{
127 u32 new_rate, prev_rate;
128 int ret;
129 bool bLs = is_bL_switching_enabled();
130
131 mutex_lock(&cluster_lock[new_cluster]);
132
133 if (bLs) {
134 prev_rate = per_cpu(cpu_last_req_freq, cpu);
135 per_cpu(cpu_last_req_freq, cpu) = rate;
136 per_cpu(physical_cluster, cpu) = new_cluster;
137
138 new_rate = find_cluster_maxfreq(new_cluster);
139 new_rate = ACTUAL_FREQ(new_cluster, new_rate);
140 } else {
141 new_rate = rate;
142 }
143
144 pr_debug("%s: cpu: %d, old cluster: %d, new cluster: %d, freq: %d\n",
145 __func__, cpu, old_cluster, new_cluster, new_rate);
146
147 ret = clk_set_rate(clk[new_cluster], new_rate * 1000);
148 if (WARN_ON(ret)) {
149 pr_err("clk_set_rate failed: %d, new cluster: %d\n", ret,
150 new_cluster);
151 if (bLs) {
152 per_cpu(cpu_last_req_freq, cpu) = prev_rate;
153 per_cpu(physical_cluster, cpu) = old_cluster;
154 }
155
156 mutex_unlock(&cluster_lock[new_cluster]);
157
158 return ret;
159 }
160
161 mutex_unlock(&cluster_lock[new_cluster]);
162
163 /* Recalc freq for old cluster when switching clusters */
164 if (old_cluster != new_cluster) {
165 pr_debug("%s: cpu: %d, old cluster: %d, new cluster: %d\n",
166 __func__, cpu, old_cluster, new_cluster);
167
168 /* Switch cluster */
169 bL_switch_request(cpu, new_cluster);
170
171 mutex_lock(&cluster_lock[old_cluster]);
172
173 /* Set freq of old cluster if there are cpus left on it */
174 new_rate = find_cluster_maxfreq(old_cluster);
175 new_rate = ACTUAL_FREQ(old_cluster, new_rate);
176
177 if (new_rate) {
178 pr_debug("%s: Updating rate of old cluster: %d, to freq: %d\n",
179 __func__, old_cluster, new_rate);
180
181 if (clk_set_rate(clk[old_cluster], new_rate * 1000))
182 pr_err("%s: clk_set_rate failed: %d, old cluster: %d\n",
183 __func__, ret, old_cluster);
184 }
185 mutex_unlock(&cluster_lock[old_cluster]);
186 }
187
188 return 0;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000189}
190
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000191/* Set clock frequency */
192static int bL_cpufreq_set_target(struct cpufreq_policy *policy,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530193 unsigned int index)
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000194{
195 struct cpufreq_freqs freqs;
Viresh Kumare79a23c2013-10-30 15:44:40 -0400196 u32 cpu = policy->cpu, cur_cluster, new_cluster, actual_cluster;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000197 int ret = 0;
198
Viresh Kumare79a23c2013-10-30 15:44:40 -0400199 cur_cluster = cpu_to_cluster(cpu);
200 new_cluster = actual_cluster = per_cpu(physical_cluster, cpu);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000201
Viresh Kumare79a23c2013-10-30 15:44:40 -0400202 freqs.old = bL_cpufreq_get_rate(cpu);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530203 freqs.new = freq_table[cur_cluster][index].frequency;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000204
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000205 pr_debug("%s: cpu: %d, cluster: %d, oldfreq: %d, target freq: %d, new freq: %d\n",
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530206 __func__, cpu, cur_cluster, freqs.old, freqs.new,
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000207 freqs.new);
208
Viresh Kumare79a23c2013-10-30 15:44:40 -0400209 if (is_bL_switching_enabled()) {
210 if ((actual_cluster == A15_CLUSTER) &&
211 (freqs.new < clk_big_min)) {
212 new_cluster = A7_CLUSTER;
213 } else if ((actual_cluster == A7_CLUSTER) &&
214 (freqs.new > clk_little_max)) {
215 new_cluster = A15_CLUSTER;
216 }
217 }
218
Viresh Kumarad61f442013-04-15 07:05:25 +0000219 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000220
Viresh Kumare79a23c2013-10-30 15:44:40 -0400221 ret = bL_cpufreq_set_rate(cpu, actual_cluster, new_cluster, freqs.new);
222 if (ret)
Viresh Kumar3d69dd52013-06-19 11:18:20 +0530223 freqs.new = freqs.old;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000224
Viresh Kumarad61f442013-04-15 07:05:25 +0000225 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000226
227 return ret;
228}
229
Viresh Kumare79a23c2013-10-30 15:44:40 -0400230static inline u32 get_table_count(struct cpufreq_frequency_table *table)
231{
232 int count;
233
234 for (count = 0; table[count].frequency != CPUFREQ_TABLE_END; count++)
235 ;
236
237 return count;
238}
239
240/* get the minimum frequency in the cpufreq_frequency_table */
241static inline u32 get_table_min(struct cpufreq_frequency_table *table)
242{
243 int i;
244 uint32_t min_freq = ~0;
245 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++)
246 if (table[i].frequency < min_freq)
247 min_freq = table[i].frequency;
248 return min_freq;
249}
250
251/* get the maximum frequency in the cpufreq_frequency_table */
252static inline u32 get_table_max(struct cpufreq_frequency_table *table)
253{
254 int i;
255 uint32_t max_freq = 0;
256 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++)
257 if (table[i].frequency > max_freq)
258 max_freq = table[i].frequency;
259 return max_freq;
260}
261
262static int merge_cluster_tables(void)
263{
264 int i, j, k = 0, count = 1;
265 struct cpufreq_frequency_table *table;
266
267 for (i = 0; i < MAX_CLUSTERS; i++)
268 count += get_table_count(freq_table[i]);
269
270 table = kzalloc(sizeof(*table) * count, GFP_KERNEL);
271 if (!table)
272 return -ENOMEM;
273
274 freq_table[MAX_CLUSTERS] = table;
275
276 /* Add in reverse order to get freqs in increasing order */
277 for (i = MAX_CLUSTERS - 1; i >= 0; i--) {
278 for (j = 0; freq_table[i][j].frequency != CPUFREQ_TABLE_END;
279 j++) {
280 table[k].frequency = VIRT_FREQ(i,
281 freq_table[i][j].frequency);
282 pr_debug("%s: index: %d, freq: %d\n", __func__, k,
283 table[k].frequency);
284 k++;
285 }
286 }
287
288 table[k].driver_data = k;
289 table[k].frequency = CPUFREQ_TABLE_END;
290
291 pr_debug("%s: End, table: %p, count: %d\n", __func__, table, k);
292
293 return 0;
294}
295
296static void _put_cluster_clk_and_freq_table(struct device *cpu_dev)
297{
298 u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
299
300 if (!freq_table[cluster])
301 return;
302
303 clk_put(clk[cluster]);
304 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
305 dev_dbg(cpu_dev, "%s: cluster: %d\n", __func__, cluster);
306}
307
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000308static void put_cluster_clk_and_freq_table(struct device *cpu_dev)
309{
310 u32 cluster = cpu_to_cluster(cpu_dev->id);
Viresh Kumare79a23c2013-10-30 15:44:40 -0400311 int i;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000312
Viresh Kumare79a23c2013-10-30 15:44:40 -0400313 if (atomic_dec_return(&cluster_usage[cluster]))
314 return;
315
316 if (cluster < MAX_CLUSTERS)
317 return _put_cluster_clk_and_freq_table(cpu_dev);
318
319 for_each_present_cpu(i) {
320 struct device *cdev = get_cpu_device(i);
321 if (!cdev) {
322 pr_err("%s: failed to get cpu%d device\n", __func__, i);
323 return;
324 }
325
326 _put_cluster_clk_and_freq_table(cdev);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000327 }
Viresh Kumare79a23c2013-10-30 15:44:40 -0400328
329 /* free virtual table */
330 kfree(freq_table[cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000331}
332
Viresh Kumare79a23c2013-10-30 15:44:40 -0400333static int _get_cluster_clk_and_freq_table(struct device *cpu_dev)
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000334{
Viresh Kumare79a23c2013-10-30 15:44:40 -0400335 u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000336 char name[14] = "cpu-cluster.";
337 int ret;
338
Viresh Kumare79a23c2013-10-30 15:44:40 -0400339 if (freq_table[cluster])
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000340 return 0;
341
342 ret = arm_bL_ops->init_opp_table(cpu_dev);
343 if (ret) {
344 dev_err(cpu_dev, "%s: init_opp_table failed, cpu: %d, err: %d\n",
345 __func__, cpu_dev->id, ret);
Viresh Kumare79a23c2013-10-30 15:44:40 -0400346 goto out;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000347 }
348
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500349 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000350 if (ret) {
351 dev_err(cpu_dev, "%s: failed to init cpufreq table, cpu: %d, err: %d\n",
352 __func__, cpu_dev->id, ret);
Viresh Kumare79a23c2013-10-30 15:44:40 -0400353 goto out;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000354 }
355
356 name[12] = cluster + '0';
Sudeep KarkadaNagesha076dec92013-10-16 14:52:39 +0100357 clk[cluster] = clk_get(cpu_dev, name);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000358 if (!IS_ERR(clk[cluster])) {
359 dev_dbg(cpu_dev, "%s: clk: %p & freq table: %p, cluster: %d\n",
360 __func__, clk[cluster], freq_table[cluster],
361 cluster);
362 return 0;
363 }
364
365 dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
366 __func__, cpu_dev->id, cluster);
367 ret = PTR_ERR(clk[cluster]);
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500368 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000369
Viresh Kumare79a23c2013-10-30 15:44:40 -0400370out:
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000371 dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__,
372 cluster);
373 return ret;
374}
375
Viresh Kumare79a23c2013-10-30 15:44:40 -0400376static int get_cluster_clk_and_freq_table(struct device *cpu_dev)
377{
378 u32 cluster = cpu_to_cluster(cpu_dev->id);
379 int i, ret;
380
381 if (atomic_inc_return(&cluster_usage[cluster]) != 1)
382 return 0;
383
384 if (cluster < MAX_CLUSTERS) {
385 ret = _get_cluster_clk_and_freq_table(cpu_dev);
386 if (ret)
387 atomic_dec(&cluster_usage[cluster]);
388 return ret;
389 }
390
391 /*
392 * Get data for all clusters and fill virtual cluster with a merge of
393 * both
394 */
395 for_each_present_cpu(i) {
396 struct device *cdev = get_cpu_device(i);
397 if (!cdev) {
398 pr_err("%s: failed to get cpu%d device\n", __func__, i);
399 return -ENODEV;
400 }
401
402 ret = _get_cluster_clk_and_freq_table(cdev);
403 if (ret)
404 goto put_clusters;
405 }
406
407 ret = merge_cluster_tables();
408 if (ret)
409 goto put_clusters;
410
411 /* Assuming 2 cluster, set clk_big_min and clk_little_max */
412 clk_big_min = get_table_min(freq_table[0]);
413 clk_little_max = VIRT_FREQ(1, get_table_max(freq_table[1]));
414
415 pr_debug("%s: cluster: %d, clk_big_min: %d, clk_little_max: %d\n",
416 __func__, cluster, clk_big_min, clk_little_max);
417
418 return 0;
419
420put_clusters:
421 for_each_present_cpu(i) {
422 struct device *cdev = get_cpu_device(i);
423 if (!cdev) {
424 pr_err("%s: failed to get cpu%d device\n", __func__, i);
425 return -ENODEV;
426 }
427
428 _put_cluster_clk_and_freq_table(cdev);
429 }
430
431 atomic_dec(&cluster_usage[cluster]);
432
433 return ret;
434}
435
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000436/* Per-CPU initialization */
437static int bL_cpufreq_init(struct cpufreq_policy *policy)
438{
439 u32 cur_cluster = cpu_to_cluster(policy->cpu);
440 struct device *cpu_dev;
441 int ret;
442
443 cpu_dev = get_cpu_device(policy->cpu);
444 if (!cpu_dev) {
445 pr_err("%s: failed to get cpu%d device\n", __func__,
446 policy->cpu);
447 return -ENODEV;
448 }
449
450 ret = get_cluster_clk_and_freq_table(cpu_dev);
451 if (ret)
452 return ret;
453
Viresh Kumar39b10eb2013-09-16 18:56:08 +0530454 ret = cpufreq_table_validate_and_show(policy, freq_table[cur_cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000455 if (ret) {
456 dev_err(cpu_dev, "CPU %d, cluster: %d invalid freq table\n",
457 policy->cpu, cur_cluster);
458 put_cluster_clk_and_freq_table(cpu_dev);
459 return ret;
460 }
461
Viresh Kumare79a23c2013-10-30 15:44:40 -0400462 if (cur_cluster < MAX_CLUSTERS) {
463 cpumask_copy(policy->cpus, topology_core_cpumask(policy->cpu));
464
465 per_cpu(physical_cluster, policy->cpu) = cur_cluster;
466 } else {
467 /* Assumption: during init, we are always running on A15 */
468 per_cpu(physical_cluster, policy->cpu) = A15_CLUSTER;
469 }
470
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000471 if (arm_bL_ops->get_transition_latency)
472 policy->cpuinfo.transition_latency =
473 arm_bL_ops->get_transition_latency(cpu_dev);
474 else
475 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
476
Viresh Kumare79a23c2013-10-30 15:44:40 -0400477 if (is_bL_switching_enabled())
478 per_cpu(cpu_last_req_freq, policy->cpu) = clk_get_cpu_rate(policy->cpu);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000479
Viresh Kumar2b80f312013-04-29 13:24:48 +0000480 dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000481 return 0;
482}
483
484static int bL_cpufreq_exit(struct cpufreq_policy *policy)
485{
486 struct device *cpu_dev;
487
488 cpu_dev = get_cpu_device(policy->cpu);
489 if (!cpu_dev) {
490 pr_err("%s: failed to get cpu%d device\n", __func__,
491 policy->cpu);
492 return -ENODEV;
493 }
494
Viresh Kumar2457dac2013-09-16 18:56:42 +0530495 cpufreq_frequency_table_put_attr(policy->cpu);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000496 put_cluster_clk_and_freq_table(cpu_dev);
497 dev_dbg(cpu_dev, "%s: Exited, cpu: %d\n", __func__, policy->cpu);
498
499 return 0;
500}
501
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000502static struct cpufreq_driver bL_cpufreq_driver = {
503 .name = "arm-big-little",
Viresh Kumar0b981e72013-10-02 14:13:18 +0530504 .flags = CPUFREQ_STICKY |
505 CPUFREQ_HAVE_GOVERNOR_PER_POLICY,
Viresh Kumar3c75a152013-10-03 20:27:57 +0530506 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530507 .target_index = bL_cpufreq_set_target,
Viresh Kumare79a23c2013-10-30 15:44:40 -0400508 .get = bL_cpufreq_get_rate,
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000509 .init = bL_cpufreq_init,
510 .exit = bL_cpufreq_exit,
Viresh Kumar3c75a152013-10-03 20:27:57 +0530511 .attr = cpufreq_generic_attr,
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000512};
513
Nicolas Pitre45cac112013-10-30 15:44:41 -0400514static int bL_cpufreq_switcher_notifier(struct notifier_block *nfb,
515 unsigned long action, void *_arg)
516{
517 pr_debug("%s: action: %ld\n", __func__, action);
518
519 switch (action) {
520 case BL_NOTIFY_PRE_ENABLE:
521 case BL_NOTIFY_PRE_DISABLE:
522 cpufreq_unregister_driver(&bL_cpufreq_driver);
523 break;
524
525 case BL_NOTIFY_POST_ENABLE:
526 set_switching_enabled(true);
527 cpufreq_register_driver(&bL_cpufreq_driver);
528 break;
529
530 case BL_NOTIFY_POST_DISABLE:
531 set_switching_enabled(false);
532 cpufreq_register_driver(&bL_cpufreq_driver);
533 break;
534
535 default:
536 return NOTIFY_DONE;
537 }
538
539 return NOTIFY_OK;
540}
541
542static struct notifier_block bL_switcher_notifier = {
543 .notifier_call = bL_cpufreq_switcher_notifier,
544};
545
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000546int bL_cpufreq_register(struct cpufreq_arm_bL_ops *ops)
547{
Viresh Kumare79a23c2013-10-30 15:44:40 -0400548 int ret, i;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000549
550 if (arm_bL_ops) {
551 pr_debug("%s: Already registered: %s, exiting\n", __func__,
552 arm_bL_ops->name);
553 return -EBUSY;
554 }
555
556 if (!ops || !strlen(ops->name) || !ops->init_opp_table) {
557 pr_err("%s: Invalid arm_bL_ops, exiting\n", __func__);
558 return -ENODEV;
559 }
560
561 arm_bL_ops = ops;
562
Nicolas Pitre45cac112013-10-30 15:44:41 -0400563 ret = bL_switcher_get_enabled();
564 set_switching_enabled(ret);
565
Viresh Kumare79a23c2013-10-30 15:44:40 -0400566 for (i = 0; i < MAX_CLUSTERS; i++)
567 mutex_init(&cluster_lock[i]);
568
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000569 ret = cpufreq_register_driver(&bL_cpufreq_driver);
570 if (ret) {
571 pr_info("%s: Failed registering platform driver: %s, err: %d\n",
572 __func__, ops->name, ret);
573 arm_bL_ops = NULL;
574 } else {
Nicolas Pitre45cac112013-10-30 15:44:41 -0400575 ret = bL_switcher_register_notifier(&bL_switcher_notifier);
576 if (ret) {
577 cpufreq_unregister_driver(&bL_cpufreq_driver);
578 arm_bL_ops = NULL;
579 } else {
580 pr_info("%s: Registered platform driver: %s\n",
581 __func__, ops->name);
582 }
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000583 }
584
Nicolas Pitre45cac112013-10-30 15:44:41 -0400585 bL_switcher_put_enabled();
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000586 return ret;
587}
588EXPORT_SYMBOL_GPL(bL_cpufreq_register);
589
590void bL_cpufreq_unregister(struct cpufreq_arm_bL_ops *ops)
591{
592 if (arm_bL_ops != ops) {
593 pr_err("%s: Registered with: %s, can't unregister, exiting\n",
594 __func__, arm_bL_ops->name);
595 return;
596 }
597
Nicolas Pitre45cac112013-10-30 15:44:41 -0400598 bL_switcher_get_enabled();
599 bL_switcher_unregister_notifier(&bL_switcher_notifier);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000600 cpufreq_unregister_driver(&bL_cpufreq_driver);
Nicolas Pitre45cac112013-10-30 15:44:41 -0400601 bL_switcher_put_enabled();
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000602 pr_info("%s: Un-registered platform driver: %s\n", __func__,
603 arm_bL_ops->name);
604 arm_bL_ops = NULL;
605}
606EXPORT_SYMBOL_GPL(bL_cpufreq_unregister);