blob: c251247ae6613e860164627d8c8161f2b0d0cfe5 [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>
Punit Agrawal2f7e8a12015-11-17 12:06:23 +000026#include <linux/cpu_cooling.h>
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000027#include <linux/export.h>
Uwe Kleine-König39c8bba2014-08-08 08:56:30 +020028#include <linux/module.h>
Viresh Kumare79a23c2013-10-30 15:44:40 -040029#include <linux/mutex.h>
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000030#include <linux/of_platform.h>
Nishanth Menone4db1c72013-09-19 16:03:52 -050031#include <linux/pm_opp.h>
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000032#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 */
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
Sudeep Holla14730142015-05-13 13:35:52 +010044#include <asm/bL_switcher.h>
Nicolas Pitre45cac112013-10-30 15:44:41 -040045static bool bL_switching_enabled;
46#define is_bL_switching_enabled() bL_switching_enabled
47#define set_switching_enabled(x) (bL_switching_enabled = (x))
Viresh Kumare79a23c2013-10-30 15:44:40 -040048#else
49#define is_bL_switching_enabled() false
Nicolas Pitre45cac112013-10-30 15:44:41 -040050#define set_switching_enabled(x) do { } while (0)
Sudeep Holla14730142015-05-13 13:35:52 +010051#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)
Viresh Kumare79a23c2013-10-30 15:44:40 -040054#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
Punit Agrawal2f7e8a12015-11-17 12:06:23 +000059static struct thermal_cooling_device *cdev[MAX_CLUSTERS];
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000060static struct cpufreq_arm_bL_ops *arm_bL_ops;
61static struct clk *clk[MAX_CLUSTERS];
Viresh Kumare79a23c2013-10-30 15:44:40 -040062static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS + 1];
63static atomic_t cluster_usage[MAX_CLUSTERS + 1];
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000064
Viresh Kumare79a23c2013-10-30 15:44:40 -040065static unsigned int clk_big_min; /* (Big) clock frequencies */
66static unsigned int clk_little_max; /* Maximum clock frequency (Little) */
67
68static DEFINE_PER_CPU(unsigned int, physical_cluster);
69static DEFINE_PER_CPU(unsigned int, cpu_last_req_freq);
70
71static struct mutex cluster_lock[MAX_CLUSTERS];
72
73static inline int raw_cpu_to_cluster(int cpu)
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000074{
Viresh Kumare79a23c2013-10-30 15:44:40 -040075 return topology_physical_package_id(cpu);
76}
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000077
Viresh Kumare79a23c2013-10-30 15:44:40 -040078static inline int cpu_to_cluster(int cpu)
79{
80 return is_bL_switching_enabled() ?
81 MAX_CLUSTERS : raw_cpu_to_cluster(cpu);
82}
83
84static 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
103static 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
118static 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}
129
130static unsigned int
131bL_cpufreq_set_rate(u32 cpu, u32 old_cluster, u32 new_cluster, u32 rate)
132{
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);
Jon Medhurst \(Tixy\)14f1ba32015-10-21 10:55:33 +0100154 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
Viresh Kumare79a23c2013-10-30 15:44:40 -0400167 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]);
191
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;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000208}
209
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000210/* Set clock frequency */
211static int bL_cpufreq_set_target(struct cpufreq_policy *policy,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530212 unsigned int index)
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000213{
Viresh Kumare79a23c2013-10-30 15:44:40 -0400214 u32 cpu = policy->cpu, cur_cluster, new_cluster, actual_cluster;
Viresh Kumard4019f02013-08-14 19:38:24 +0530215 unsigned int freqs_new;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000216
Viresh Kumare79a23c2013-10-30 15:44:40 -0400217 cur_cluster = cpu_to_cluster(cpu);
218 new_cluster = actual_cluster = per_cpu(physical_cluster, cpu);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000219
Viresh Kumard4019f02013-08-14 19:38:24 +0530220 freqs_new = freq_table[cur_cluster][index].frequency;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000221
Viresh Kumare79a23c2013-10-30 15:44:40 -0400222 if (is_bL_switching_enabled()) {
223 if ((actual_cluster == A15_CLUSTER) &&
Viresh Kumard4019f02013-08-14 19:38:24 +0530224 (freqs_new < clk_big_min)) {
Viresh Kumare79a23c2013-10-30 15:44:40 -0400225 new_cluster = A7_CLUSTER;
226 } else if ((actual_cluster == A7_CLUSTER) &&
Viresh Kumard4019f02013-08-14 19:38:24 +0530227 (freqs_new > clk_little_max)) {
Viresh Kumare79a23c2013-10-30 15:44:40 -0400228 new_cluster = A15_CLUSTER;
229 }
230 }
231
Viresh Kumard4019f02013-08-14 19:38:24 +0530232 return bL_cpufreq_set_rate(cpu, actual_cluster, new_cluster, freqs_new);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000233}
234
Viresh Kumare79a23c2013-10-30 15:44:40 -0400235static inline u32 get_table_count(struct cpufreq_frequency_table *table)
236{
237 int count;
238
239 for (count = 0; table[count].frequency != CPUFREQ_TABLE_END; count++)
240 ;
241
242 return count;
243}
244
245/* get the minimum frequency in the cpufreq_frequency_table */
246static inline u32 get_table_min(struct cpufreq_frequency_table *table)
247{
Stratos Karafotis041526f2014-04-25 23:15:38 +0300248 struct cpufreq_frequency_table *pos;
Viresh Kumare79a23c2013-10-30 15:44:40 -0400249 uint32_t min_freq = ~0;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300250 cpufreq_for_each_entry(pos, table)
251 if (pos->frequency < min_freq)
252 min_freq = pos->frequency;
Viresh Kumare79a23c2013-10-30 15:44:40 -0400253 return min_freq;
254}
255
256/* get the maximum frequency in the cpufreq_frequency_table */
257static inline u32 get_table_max(struct cpufreq_frequency_table *table)
258{
Stratos Karafotis041526f2014-04-25 23:15:38 +0300259 struct cpufreq_frequency_table *pos;
Viresh Kumare79a23c2013-10-30 15:44:40 -0400260 uint32_t max_freq = 0;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300261 cpufreq_for_each_entry(pos, table)
262 if (pos->frequency > max_freq)
263 max_freq = pos->frequency;
Viresh Kumare79a23c2013-10-30 15:44:40 -0400264 return max_freq;
265}
266
267static int merge_cluster_tables(void)
268{
269 int i, j, k = 0, count = 1;
270 struct cpufreq_frequency_table *table;
271
272 for (i = 0; i < MAX_CLUSTERS; i++)
273 count += get_table_count(freq_table[i]);
274
275 table = kzalloc(sizeof(*table) * count, GFP_KERNEL);
276 if (!table)
277 return -ENOMEM;
278
279 freq_table[MAX_CLUSTERS] = table;
280
281 /* Add in reverse order to get freqs in increasing order */
282 for (i = MAX_CLUSTERS - 1; i >= 0; i--) {
283 for (j = 0; freq_table[i][j].frequency != CPUFREQ_TABLE_END;
284 j++) {
285 table[k].frequency = VIRT_FREQ(i,
286 freq_table[i][j].frequency);
287 pr_debug("%s: index: %d, freq: %d\n", __func__, k,
288 table[k].frequency);
289 k++;
290 }
291 }
292
293 table[k].driver_data = k;
294 table[k].frequency = CPUFREQ_TABLE_END;
295
296 pr_debug("%s: End, table: %p, count: %d\n", __func__, table, k);
297
298 return 0;
299}
300
301static void _put_cluster_clk_and_freq_table(struct device *cpu_dev)
302{
303 u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
304
305 if (!freq_table[cluster])
306 return;
307
308 clk_put(clk[cluster]);
309 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
Viresh Kumar493b4cd2014-11-25 16:04:20 +0530310 if (arm_bL_ops->free_opp_table)
311 arm_bL_ops->free_opp_table(cpu_dev);
Viresh Kumare79a23c2013-10-30 15:44:40 -0400312 dev_dbg(cpu_dev, "%s: cluster: %d\n", __func__, cluster);
313}
314
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000315static void put_cluster_clk_and_freq_table(struct device *cpu_dev)
316{
317 u32 cluster = cpu_to_cluster(cpu_dev->id);
Viresh Kumare79a23c2013-10-30 15:44:40 -0400318 int i;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000319
Viresh Kumare79a23c2013-10-30 15:44:40 -0400320 if (atomic_dec_return(&cluster_usage[cluster]))
321 return;
322
323 if (cluster < MAX_CLUSTERS)
324 return _put_cluster_clk_and_freq_table(cpu_dev);
325
326 for_each_present_cpu(i) {
327 struct device *cdev = get_cpu_device(i);
328 if (!cdev) {
329 pr_err("%s: failed to get cpu%d device\n", __func__, i);
330 return;
331 }
332
333 _put_cluster_clk_and_freq_table(cdev);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000334 }
Viresh Kumare79a23c2013-10-30 15:44:40 -0400335
336 /* free virtual table */
337 kfree(freq_table[cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000338}
339
Viresh Kumare79a23c2013-10-30 15:44:40 -0400340static int _get_cluster_clk_and_freq_table(struct device *cpu_dev)
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000341{
Viresh Kumare79a23c2013-10-30 15:44:40 -0400342 u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000343 int ret;
344
Viresh Kumare79a23c2013-10-30 15:44:40 -0400345 if (freq_table[cluster])
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000346 return 0;
347
348 ret = arm_bL_ops->init_opp_table(cpu_dev);
349 if (ret) {
350 dev_err(cpu_dev, "%s: init_opp_table failed, cpu: %d, err: %d\n",
351 __func__, cpu_dev->id, ret);
Viresh Kumare79a23c2013-10-30 15:44:40 -0400352 goto out;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000353 }
354
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500355 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000356 if (ret) {
357 dev_err(cpu_dev, "%s: failed to init cpufreq table, cpu: %d, err: %d\n",
358 __func__, cpu_dev->id, ret);
Viresh Kumar493b4cd2014-11-25 16:04:20 +0530359 goto free_opp_table;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000360 }
361
Sudeep Hollab904f5c2015-04-27 10:51:06 +0100362 clk[cluster] = clk_get(cpu_dev, NULL);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000363 if (!IS_ERR(clk[cluster])) {
364 dev_dbg(cpu_dev, "%s: clk: %p & freq table: %p, cluster: %d\n",
365 __func__, clk[cluster], freq_table[cluster],
366 cluster);
367 return 0;
368 }
369
370 dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
371 __func__, cpu_dev->id, cluster);
372 ret = PTR_ERR(clk[cluster]);
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500373 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000374
Viresh Kumar493b4cd2014-11-25 16:04:20 +0530375free_opp_table:
376 if (arm_bL_ops->free_opp_table)
377 arm_bL_ops->free_opp_table(cpu_dev);
Viresh Kumare79a23c2013-10-30 15:44:40 -0400378out:
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000379 dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__,
380 cluster);
381 return ret;
382}
383
Viresh Kumare79a23c2013-10-30 15:44:40 -0400384static int get_cluster_clk_and_freq_table(struct device *cpu_dev)
385{
386 u32 cluster = cpu_to_cluster(cpu_dev->id);
387 int i, ret;
388
389 if (atomic_inc_return(&cluster_usage[cluster]) != 1)
390 return 0;
391
392 if (cluster < MAX_CLUSTERS) {
393 ret = _get_cluster_clk_and_freq_table(cpu_dev);
394 if (ret)
395 atomic_dec(&cluster_usage[cluster]);
396 return ret;
397 }
398
399 /*
400 * Get data for all clusters and fill virtual cluster with a merge of
401 * both
402 */
403 for_each_present_cpu(i) {
404 struct device *cdev = get_cpu_device(i);
405 if (!cdev) {
406 pr_err("%s: failed to get cpu%d device\n", __func__, i);
407 return -ENODEV;
408 }
409
410 ret = _get_cluster_clk_and_freq_table(cdev);
411 if (ret)
412 goto put_clusters;
413 }
414
415 ret = merge_cluster_tables();
416 if (ret)
417 goto put_clusters;
418
419 /* Assuming 2 cluster, set clk_big_min and clk_little_max */
420 clk_big_min = get_table_min(freq_table[0]);
421 clk_little_max = VIRT_FREQ(1, get_table_max(freq_table[1]));
422
423 pr_debug("%s: cluster: %d, clk_big_min: %d, clk_little_max: %d\n",
424 __func__, cluster, clk_big_min, clk_little_max);
425
426 return 0;
427
428put_clusters:
429 for_each_present_cpu(i) {
430 struct device *cdev = get_cpu_device(i);
431 if (!cdev) {
432 pr_err("%s: failed to get cpu%d device\n", __func__, i);
433 return -ENODEV;
434 }
435
436 _put_cluster_clk_and_freq_table(cdev);
437 }
438
439 atomic_dec(&cluster_usage[cluster]);
440
441 return ret;
442}
443
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000444/* Per-CPU initialization */
445static int bL_cpufreq_init(struct cpufreq_policy *policy)
446{
447 u32 cur_cluster = cpu_to_cluster(policy->cpu);
448 struct device *cpu_dev;
449 int ret;
450
451 cpu_dev = get_cpu_device(policy->cpu);
452 if (!cpu_dev) {
453 pr_err("%s: failed to get cpu%d device\n", __func__,
454 policy->cpu);
455 return -ENODEV;
456 }
457
458 ret = get_cluster_clk_and_freq_table(cpu_dev);
459 if (ret)
460 return ret;
461
Viresh Kumar39b10eb2013-09-16 18:56:08 +0530462 ret = cpufreq_table_validate_and_show(policy, freq_table[cur_cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000463 if (ret) {
464 dev_err(cpu_dev, "CPU %d, cluster: %d invalid freq table\n",
465 policy->cpu, cur_cluster);
466 put_cluster_clk_and_freq_table(cpu_dev);
467 return ret;
468 }
469
Viresh Kumare79a23c2013-10-30 15:44:40 -0400470 if (cur_cluster < MAX_CLUSTERS) {
viresh kumar8f3ba3d2014-03-14 12:10:55 +0530471 int cpu;
472
Viresh Kumare79a23c2013-10-30 15:44:40 -0400473 cpumask_copy(policy->cpus, topology_core_cpumask(policy->cpu));
474
viresh kumar8f3ba3d2014-03-14 12:10:55 +0530475 for_each_cpu(cpu, policy->cpus)
476 per_cpu(physical_cluster, cpu) = cur_cluster;
Viresh Kumare79a23c2013-10-30 15:44:40 -0400477 } else {
478 /* Assumption: during init, we are always running on A15 */
479 per_cpu(physical_cluster, policy->cpu) = A15_CLUSTER;
480 }
481
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000482 if (arm_bL_ops->get_transition_latency)
483 policy->cpuinfo.transition_latency =
484 arm_bL_ops->get_transition_latency(cpu_dev);
485 else
486 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
487
Viresh Kumare79a23c2013-10-30 15:44:40 -0400488 if (is_bL_switching_enabled())
489 per_cpu(cpu_last_req_freq, policy->cpu) = clk_get_cpu_rate(policy->cpu);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000490
Viresh Kumar2b80f312013-04-29 13:24:48 +0000491 dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000492 return 0;
493}
494
495static int bL_cpufreq_exit(struct cpufreq_policy *policy)
496{
497 struct device *cpu_dev;
Punit Agrawal2f7e8a12015-11-17 12:06:23 +0000498 int cur_cluster = cpu_to_cluster(policy->cpu);
499
500 if (cur_cluster < MAX_CLUSTERS) {
501 cpufreq_cooling_unregister(cdev[cur_cluster]);
502 cdev[cur_cluster] = NULL;
503 }
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000504
505 cpu_dev = get_cpu_device(policy->cpu);
506 if (!cpu_dev) {
507 pr_err("%s: failed to get cpu%d device\n", __func__,
508 policy->cpu);
509 return -ENODEV;
510 }
511
512 put_cluster_clk_and_freq_table(cpu_dev);
513 dev_dbg(cpu_dev, "%s: Exited, cpu: %d\n", __func__, policy->cpu);
514
515 return 0;
516}
517
Punit Agrawal2f7e8a12015-11-17 12:06:23 +0000518static void bL_cpufreq_ready(struct cpufreq_policy *policy)
519{
520 struct device *cpu_dev = get_cpu_device(policy->cpu);
521 int cur_cluster = cpu_to_cluster(policy->cpu);
522 struct device_node *np;
523
524 /* Do not register a cpu_cooling device if we are in IKS mode */
525 if (cur_cluster >= MAX_CLUSTERS)
526 return;
527
528 np = of_node_get(cpu_dev->of_node);
529 if (WARN_ON(!np))
530 return;
531
532 if (of_find_property(np, "#cooling-cells", NULL)) {
533 u32 power_coefficient = 0;
534
535 of_property_read_u32(np, "dynamic-power-coefficient",
536 &power_coefficient);
537
538 cdev[cur_cluster] = of_cpufreq_power_cooling_register(np,
539 policy->related_cpus, power_coefficient, NULL);
540 if (IS_ERR(cdev[cur_cluster])) {
541 dev_err(cpu_dev,
542 "running cpufreq without cooling device: %ld\n",
543 PTR_ERR(cdev[cur_cluster]));
544 cdev[cur_cluster] = NULL;
545 }
546 }
547 of_node_put(np);
548}
549
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000550static struct cpufreq_driver bL_cpufreq_driver = {
551 .name = "arm-big-little",
Viresh Kumar0b981e72013-10-02 14:13:18 +0530552 .flags = CPUFREQ_STICKY |
Viresh Kumarae6b4272013-12-03 11:20:45 +0530553 CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
554 CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Viresh Kumar3c75a152013-10-03 20:27:57 +0530555 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530556 .target_index = bL_cpufreq_set_target,
Viresh Kumare79a23c2013-10-30 15:44:40 -0400557 .get = bL_cpufreq_get_rate,
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000558 .init = bL_cpufreq_init,
559 .exit = bL_cpufreq_exit,
Punit Agrawal2f7e8a12015-11-17 12:06:23 +0000560 .ready = bL_cpufreq_ready,
Viresh Kumar3c75a152013-10-03 20:27:57 +0530561 .attr = cpufreq_generic_attr,
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000562};
563
Sudeep Holla14730142015-05-13 13:35:52 +0100564#ifdef CONFIG_BL_SWITCHER
Nicolas Pitre45cac112013-10-30 15:44:41 -0400565static int bL_cpufreq_switcher_notifier(struct notifier_block *nfb,
566 unsigned long action, void *_arg)
567{
568 pr_debug("%s: action: %ld\n", __func__, action);
569
570 switch (action) {
571 case BL_NOTIFY_PRE_ENABLE:
572 case BL_NOTIFY_PRE_DISABLE:
573 cpufreq_unregister_driver(&bL_cpufreq_driver);
574 break;
575
576 case BL_NOTIFY_POST_ENABLE:
577 set_switching_enabled(true);
578 cpufreq_register_driver(&bL_cpufreq_driver);
579 break;
580
581 case BL_NOTIFY_POST_DISABLE:
582 set_switching_enabled(false);
583 cpufreq_register_driver(&bL_cpufreq_driver);
584 break;
585
586 default:
587 return NOTIFY_DONE;
588 }
589
590 return NOTIFY_OK;
591}
592
593static struct notifier_block bL_switcher_notifier = {
594 .notifier_call = bL_cpufreq_switcher_notifier,
595};
596
Sudeep Holla14730142015-05-13 13:35:52 +0100597static int __bLs_register_notifier(void)
598{
599 return bL_switcher_register_notifier(&bL_switcher_notifier);
600}
601
602static int __bLs_unregister_notifier(void)
603{
604 return bL_switcher_unregister_notifier(&bL_switcher_notifier);
605}
606#else
607static int __bLs_register_notifier(void) { return 0; }
608static int __bLs_unregister_notifier(void) { return 0; }
609#endif
610
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000611int bL_cpufreq_register(struct cpufreq_arm_bL_ops *ops)
612{
Viresh Kumare79a23c2013-10-30 15:44:40 -0400613 int ret, i;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000614
615 if (arm_bL_ops) {
616 pr_debug("%s: Already registered: %s, exiting\n", __func__,
617 arm_bL_ops->name);
618 return -EBUSY;
619 }
620
621 if (!ops || !strlen(ops->name) || !ops->init_opp_table) {
622 pr_err("%s: Invalid arm_bL_ops, exiting\n", __func__);
623 return -ENODEV;
624 }
625
626 arm_bL_ops = ops;
627
Sudeep Holla14730142015-05-13 13:35:52 +0100628 set_switching_enabled(bL_switcher_get_enabled());
Nicolas Pitre45cac112013-10-30 15:44:41 -0400629
Viresh Kumare79a23c2013-10-30 15:44:40 -0400630 for (i = 0; i < MAX_CLUSTERS; i++)
631 mutex_init(&cluster_lock[i]);
632
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000633 ret = cpufreq_register_driver(&bL_cpufreq_driver);
634 if (ret) {
635 pr_info("%s: Failed registering platform driver: %s, err: %d\n",
636 __func__, ops->name, ret);
637 arm_bL_ops = NULL;
638 } else {
Sudeep Holla14730142015-05-13 13:35:52 +0100639 ret = __bLs_register_notifier();
Nicolas Pitre45cac112013-10-30 15:44:41 -0400640 if (ret) {
641 cpufreq_unregister_driver(&bL_cpufreq_driver);
642 arm_bL_ops = NULL;
643 } else {
644 pr_info("%s: Registered platform driver: %s\n",
645 __func__, ops->name);
646 }
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000647 }
648
Nicolas Pitre45cac112013-10-30 15:44:41 -0400649 bL_switcher_put_enabled();
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000650 return ret;
651}
652EXPORT_SYMBOL_GPL(bL_cpufreq_register);
653
654void bL_cpufreq_unregister(struct cpufreq_arm_bL_ops *ops)
655{
656 if (arm_bL_ops != ops) {
657 pr_err("%s: Registered with: %s, can't unregister, exiting\n",
658 __func__, arm_bL_ops->name);
659 return;
660 }
661
Nicolas Pitre45cac112013-10-30 15:44:41 -0400662 bL_switcher_get_enabled();
Sudeep Holla14730142015-05-13 13:35:52 +0100663 __bLs_unregister_notifier();
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000664 cpufreq_unregister_driver(&bL_cpufreq_driver);
Nicolas Pitre45cac112013-10-30 15:44:41 -0400665 bL_switcher_put_enabled();
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000666 pr_info("%s: Un-registered platform driver: %s\n", __func__,
667 arm_bL_ops->name);
668 arm_bL_ops = NULL;
669}
670EXPORT_SYMBOL_GPL(bL_cpufreq_unregister);
Uwe Kleine-König39c8bba2014-08-08 08:56:30 +0200671
672MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
673MODULE_DESCRIPTION("Generic ARM big LITTLE cpufreq driver");
674MODULE_LICENSE("GPL v2");