blob: 507027388ae32de9bb06a9d6f78074ec93e1ff8e [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>
27#include <linux/of_platform.h>
28#include <linux/opp.h>
29#include <linux/slab.h>
30#include <linux/topology.h>
31#include <linux/types.h>
32
33#include "arm_big_little.h"
34
35/* Currently we support only two clusters */
36#define MAX_CLUSTERS 2
37
38static struct cpufreq_arm_bL_ops *arm_bL_ops;
39static struct clk *clk[MAX_CLUSTERS];
40static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS];
41static atomic_t cluster_usage[MAX_CLUSTERS] = {ATOMIC_INIT(0), ATOMIC_INIT(0)};
42
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000043static unsigned int bL_cpufreq_get(unsigned int cpu)
44{
45 u32 cur_cluster = cpu_to_cluster(cpu);
46
47 return clk_get_rate(clk[cur_cluster]) / 1000;
48}
49
50/* Validate policy frequency range */
51static int bL_cpufreq_verify_policy(struct cpufreq_policy *policy)
52{
53 u32 cur_cluster = cpu_to_cluster(policy->cpu);
54
55 return cpufreq_frequency_table_verify(policy, freq_table[cur_cluster]);
56}
57
58/* Set clock frequency */
59static int bL_cpufreq_set_target(struct cpufreq_policy *policy,
60 unsigned int target_freq, unsigned int relation)
61{
62 struct cpufreq_freqs freqs;
63 u32 cpu = policy->cpu, freq_tab_idx, cur_cluster;
64 int ret = 0;
65
66 cur_cluster = cpu_to_cluster(policy->cpu);
67
68 freqs.old = bL_cpufreq_get(policy->cpu);
69
70 /* Determine valid target frequency using freq_table */
71 cpufreq_frequency_table_target(policy, freq_table[cur_cluster],
72 target_freq, relation, &freq_tab_idx);
73 freqs.new = freq_table[cur_cluster][freq_tab_idx].frequency;
74
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000075 pr_debug("%s: cpu: %d, cluster: %d, oldfreq: %d, target freq: %d, new freq: %d\n",
76 __func__, cpu, cur_cluster, freqs.old, target_freq,
77 freqs.new);
78
79 if (freqs.old == freqs.new)
80 return 0;
81
Viresh Kumarad61f442013-04-15 07:05:25 +000082 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000083
84 ret = clk_set_rate(clk[cur_cluster], freqs.new * 1000);
85 if (ret) {
86 pr_err("clk_set_rate failed: %d\n", ret);
Viresh Kumar3d69dd52013-06-19 11:18:20 +053087 freqs.new = freqs.old;
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000088 }
89
Viresh Kumarad61f442013-04-15 07:05:25 +000090 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +000091
92 return ret;
93}
94
95static void put_cluster_clk_and_freq_table(struct device *cpu_dev)
96{
97 u32 cluster = cpu_to_cluster(cpu_dev->id);
98
99 if (!atomic_dec_return(&cluster_usage[cluster])) {
100 clk_put(clk[cluster]);
101 opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
102 dev_dbg(cpu_dev, "%s: cluster: %d\n", __func__, cluster);
103 }
104}
105
106static int get_cluster_clk_and_freq_table(struct device *cpu_dev)
107{
108 u32 cluster = cpu_to_cluster(cpu_dev->id);
109 char name[14] = "cpu-cluster.";
110 int ret;
111
112 if (atomic_inc_return(&cluster_usage[cluster]) != 1)
113 return 0;
114
115 ret = arm_bL_ops->init_opp_table(cpu_dev);
116 if (ret) {
117 dev_err(cpu_dev, "%s: init_opp_table failed, cpu: %d, err: %d\n",
118 __func__, cpu_dev->id, ret);
119 goto atomic_dec;
120 }
121
122 ret = opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]);
123 if (ret) {
124 dev_err(cpu_dev, "%s: failed to init cpufreq table, cpu: %d, err: %d\n",
125 __func__, cpu_dev->id, ret);
126 goto atomic_dec;
127 }
128
129 name[12] = cluster + '0';
130 clk[cluster] = clk_get_sys(name, NULL);
131 if (!IS_ERR(clk[cluster])) {
132 dev_dbg(cpu_dev, "%s: clk: %p & freq table: %p, cluster: %d\n",
133 __func__, clk[cluster], freq_table[cluster],
134 cluster);
135 return 0;
136 }
137
138 dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
139 __func__, cpu_dev->id, cluster);
140 ret = PTR_ERR(clk[cluster]);
141 opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
142
143atomic_dec:
144 atomic_dec(&cluster_usage[cluster]);
145 dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__,
146 cluster);
147 return ret;
148}
149
150/* Per-CPU initialization */
151static int bL_cpufreq_init(struct cpufreq_policy *policy)
152{
153 u32 cur_cluster = cpu_to_cluster(policy->cpu);
154 struct device *cpu_dev;
155 int ret;
156
157 cpu_dev = get_cpu_device(policy->cpu);
158 if (!cpu_dev) {
159 pr_err("%s: failed to get cpu%d device\n", __func__,
160 policy->cpu);
161 return -ENODEV;
162 }
163
164 ret = get_cluster_clk_and_freq_table(cpu_dev);
165 if (ret)
166 return ret;
167
Viresh Kumar39b10eb2013-09-16 18:56:08 +0530168 ret = cpufreq_table_validate_and_show(policy, freq_table[cur_cluster]);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000169 if (ret) {
170 dev_err(cpu_dev, "CPU %d, cluster: %d invalid freq table\n",
171 policy->cpu, cur_cluster);
172 put_cluster_clk_and_freq_table(cpu_dev);
173 return ret;
174 }
175
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000176 if (arm_bL_ops->get_transition_latency)
177 policy->cpuinfo.transition_latency =
178 arm_bL_ops->get_transition_latency(cpu_dev);
179 else
180 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
181
182 policy->cur = bL_cpufreq_get(policy->cpu);
183
184 cpumask_copy(policy->cpus, topology_core_cpumask(policy->cpu));
185
Viresh Kumar2b80f312013-04-29 13:24:48 +0000186 dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu);
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000187 return 0;
188}
189
190static int bL_cpufreq_exit(struct cpufreq_policy *policy)
191{
192 struct device *cpu_dev;
193
194 cpu_dev = get_cpu_device(policy->cpu);
195 if (!cpu_dev) {
196 pr_err("%s: failed to get cpu%d device\n", __func__,
197 policy->cpu);
198 return -ENODEV;
199 }
200
201 put_cluster_clk_and_freq_table(cpu_dev);
202 dev_dbg(cpu_dev, "%s: Exited, cpu: %d\n", __func__, policy->cpu);
203
204 return 0;
205}
206
207/* Export freq_table to sysfs */
208static struct freq_attr *bL_cpufreq_attr[] = {
209 &cpufreq_freq_attr_scaling_available_freqs,
210 NULL,
211};
212
213static struct cpufreq_driver bL_cpufreq_driver = {
214 .name = "arm-big-little",
215 .flags = CPUFREQ_STICKY,
216 .verify = bL_cpufreq_verify_policy,
217 .target = bL_cpufreq_set_target,
218 .get = bL_cpufreq_get,
219 .init = bL_cpufreq_init,
220 .exit = bL_cpufreq_exit,
Viresh Kumarad61f442013-04-15 07:05:25 +0000221 .have_governor_per_policy = true,
Viresh Kumar8a67f0e2013-04-01 12:57:49 +0000222 .attr = bL_cpufreq_attr,
223};
224
225int bL_cpufreq_register(struct cpufreq_arm_bL_ops *ops)
226{
227 int ret;
228
229 if (arm_bL_ops) {
230 pr_debug("%s: Already registered: %s, exiting\n", __func__,
231 arm_bL_ops->name);
232 return -EBUSY;
233 }
234
235 if (!ops || !strlen(ops->name) || !ops->init_opp_table) {
236 pr_err("%s: Invalid arm_bL_ops, exiting\n", __func__);
237 return -ENODEV;
238 }
239
240 arm_bL_ops = ops;
241
242 ret = cpufreq_register_driver(&bL_cpufreq_driver);
243 if (ret) {
244 pr_info("%s: Failed registering platform driver: %s, err: %d\n",
245 __func__, ops->name, ret);
246 arm_bL_ops = NULL;
247 } else {
248 pr_info("%s: Registered platform driver: %s\n", __func__,
249 ops->name);
250 }
251
252 return ret;
253}
254EXPORT_SYMBOL_GPL(bL_cpufreq_register);
255
256void bL_cpufreq_unregister(struct cpufreq_arm_bL_ops *ops)
257{
258 if (arm_bL_ops != ops) {
259 pr_err("%s: Registered with: %s, can't unregister, exiting\n",
260 __func__, arm_bL_ops->name);
261 return;
262 }
263
264 cpufreq_unregister_driver(&bL_cpufreq_driver);
265 pr_info("%s: Un-registered platform driver: %s\n", __func__,
266 arm_bL_ops->name);
267 arm_bL_ops = NULL;
268}
269EXPORT_SYMBOL_GPL(bL_cpufreq_unregister);