2 * drivers/cpufreq/spear-cpufreq.c
4 * CPU Frequency Scaling for SPEAr platform
6 * Copyright (C) 2012 ST Microelectronics
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/clk.h>
17 #include <linux/cpufreq.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
25 /* SPEAr CPUFreq driver data structure */
28 unsigned int transition_latency;
29 struct cpufreq_frequency_table *freq_tbl;
33 static int spear_cpufreq_verify(struct cpufreq_policy *policy)
35 return cpufreq_frequency_table_verify(policy, spear_cpufreq.freq_tbl);
38 static unsigned int spear_cpufreq_get(unsigned int cpu)
40 return clk_get_rate(spear_cpufreq.clk) / 1000;
43 static struct clk *spear1340_cpu_get_possible_parent(unsigned long newfreq)
48 * In SPEAr1340, cpu clk's parent sys clk can take input from
51 const char *sys_clk_src[] = {
59 * As sys clk can have multiple source with their own range
60 * limitation so we choose possible sources accordingly
62 if (newfreq <= 300000000)
63 pclk = 0; /* src is sys_syn_clk */
64 else if (newfreq > 300000000 && newfreq <= 500000000)
65 pclk = 3; /* src is pll3_clk */
66 else if (newfreq == 600000000)
67 pclk = 1; /* src is pll1_clk */
69 return ERR_PTR(-EINVAL);
71 /* Get parent to sys clock */
72 sys_pclk = clk_get(NULL, sys_clk_src[pclk]);
74 pr_err("Failed to get %s clock\n", sys_clk_src[pclk]);
80 * In SPEAr1340, we cannot use newfreq directly because we need to actually
81 * access a source clock (clk) which might not be ancestor of cpu at present.
82 * Hence in SPEAr1340 we would operate on source clock directly before switching
85 static int spear1340_set_cpu_rate(struct clk *sys_pclk, unsigned long newfreq)
90 sys_clk = clk_get_parent(spear_cpufreq.clk);
91 if (IS_ERR(sys_clk)) {
92 pr_err("failed to get cpu's parent (sys) clock\n");
93 return PTR_ERR(sys_clk);
96 /* Set the rate of the source clock before changing the parent */
97 ret = clk_set_rate(sys_pclk, newfreq);
99 pr_err("Failed to set sys clk rate to %lu\n", newfreq);
103 ret = clk_set_parent(sys_clk, sys_pclk);
105 pr_err("Failed to set sys clk parent\n");
112 static int spear_cpufreq_target(struct cpufreq_policy *policy,
113 unsigned int target_freq, unsigned int relation)
115 struct cpufreq_freqs freqs;
116 unsigned long newfreq;
118 int index, ret, mult = 1;
120 if (cpufreq_frequency_table_target(policy, spear_cpufreq.freq_tbl,
121 target_freq, relation, &index))
124 freqs.cpu = policy->cpu;
125 freqs.old = spear_cpufreq_get(0);
127 newfreq = spear_cpufreq.freq_tbl[index].frequency * 1000;
128 if (of_machine_is_compatible("st,spear1340")) {
130 * SPEAr1340 is special in the sense that due to the possibility
131 * of multiple clock sources for cpu clk's parent we can have
132 * different clock source for different frequency of cpu clk.
133 * Hence we need to choose one from amongst these possible clock
136 srcclk = spear1340_cpu_get_possible_parent(newfreq);
137 if (IS_ERR(srcclk)) {
138 pr_err("Failed to get src clk\n");
139 return PTR_ERR(srcclk);
142 /* SPEAr1340: src clk is always 2 * intended cpu clk */
146 * src clock to be altered is ancestor of cpu clock. Hence we
147 * can directly work on cpu clk
149 srcclk = spear_cpufreq.clk;
152 newfreq = clk_round_rate(srcclk, newfreq * mult);
154 pr_err("clk_round_rate failed for cpu src clock\n");
158 freqs.new = newfreq / 1000;
161 for_each_cpu(freqs.cpu, policy->cpus)
162 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
165 ret = spear1340_set_cpu_rate(srcclk, newfreq);
167 ret = clk_set_rate(spear_cpufreq.clk, newfreq);
169 /* Get current rate after clk_set_rate, in case of failure */
171 pr_err("CPU Freq: cpu clk_set_rate failed: %d\n", ret);
172 freqs.new = clk_get_rate(spear_cpufreq.clk) / 1000;
175 for_each_cpu(freqs.cpu, policy->cpus)
176 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
180 static int spear_cpufreq_init(struct cpufreq_policy *policy)
184 ret = cpufreq_frequency_table_cpuinfo(policy, spear_cpufreq.freq_tbl);
186 pr_err("cpufreq_frequency_table_cpuinfo() failed");
190 cpufreq_frequency_table_get_attr(spear_cpufreq.freq_tbl, policy->cpu);
191 policy->cpuinfo.transition_latency = spear_cpufreq.transition_latency;
192 policy->cur = spear_cpufreq_get(0);
194 cpumask_setall(policy->cpus);
199 static int spear_cpufreq_exit(struct cpufreq_policy *policy)
201 cpufreq_frequency_table_put_attr(policy->cpu);
205 static struct freq_attr *spear_cpufreq_attr[] = {
206 &cpufreq_freq_attr_scaling_available_freqs,
210 static struct cpufreq_driver spear_cpufreq_driver = {
211 .name = "cpufreq-spear",
212 .flags = CPUFREQ_STICKY,
213 .verify = spear_cpufreq_verify,
214 .target = spear_cpufreq_target,
215 .get = spear_cpufreq_get,
216 .init = spear_cpufreq_init,
217 .exit = spear_cpufreq_exit,
218 .attr = spear_cpufreq_attr,
221 static int spear_cpufreq_driver_init(void)
223 struct device_node *np;
224 const struct property *prop;
225 struct cpufreq_frequency_table *freq_tbl;
229 np = of_find_node_by_path("/cpus/cpu@0");
231 pr_err("No cpu node found");
235 if (of_property_read_u32(np, "clock-latency",
236 &spear_cpufreq.transition_latency))
237 spear_cpufreq.transition_latency = CPUFREQ_ETERNAL;
239 prop = of_find_property(np, "cpufreq_tbl", NULL);
240 if (!prop || !prop->value) {
241 pr_err("Invalid cpufreq_tbl");
246 cnt = prop->length / sizeof(u32);
249 freq_tbl = kmalloc(sizeof(*freq_tbl) * (cnt + 1), GFP_KERNEL);
255 for (i = 0; i < cnt; i++) {
256 freq_tbl[i].index = i;
257 freq_tbl[i].frequency = be32_to_cpup(val++);
260 freq_tbl[i].index = i;
261 freq_tbl[i].frequency = CPUFREQ_TABLE_END;
263 spear_cpufreq.freq_tbl = freq_tbl;
267 spear_cpufreq.clk = clk_get(NULL, "cpu_clk");
268 if (IS_ERR(spear_cpufreq.clk)) {
269 pr_err("Unable to get CPU clock\n");
270 ret = PTR_ERR(spear_cpufreq.clk);
274 ret = cpufreq_register_driver(&spear_cpufreq_driver);
278 pr_err("failed register driver: %d\n", ret);
279 clk_put(spear_cpufreq.clk);
289 late_initcall(spear_cpufreq_driver_init);
292 MODULE_DESCRIPTION("SPEAr CPUFreq driver");
293 MODULE_LICENSE("GPL");