blob: 2a031587f52ce0cd87e4161c08069b446a1b95a3 [file] [log] [blame]
Deepak Sikri42099322012-11-27 14:05:26 +01001/*
2 * drivers/cpufreq/spear-cpufreq.c
3 *
4 * CPU Frequency Scaling for SPEAr platform
5 *
6 * Copyright (C) 2012 ST Microelectronics
7 * Deepak Sikri <deepak.sikri@st.com>
8 *
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.
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
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>
Sudeep KarkadaNageshac0e46942013-06-17 15:09:15 +010021#include <linux/of_device.h>
Deepak Sikri42099322012-11-27 14:05:26 +010022#include <linux/slab.h>
23#include <linux/types.h>
24
25/* SPEAr CPUFreq driver data structure */
26static struct {
27 struct clk *clk;
28 unsigned int transition_latency;
29 struct cpufreq_frequency_table *freq_tbl;
30 u32 cnt;
31} spear_cpufreq;
32
Deepak Sikri42099322012-11-27 14:05:26 +010033static unsigned int spear_cpufreq_get(unsigned int cpu)
34{
35 return clk_get_rate(spear_cpufreq.clk) / 1000;
36}
37
38static struct clk *spear1340_cpu_get_possible_parent(unsigned long newfreq)
39{
40 struct clk *sys_pclk;
41 int pclk;
42 /*
43 * In SPEAr1340, cpu clk's parent sys clk can take input from
44 * following sources
45 */
46 const char *sys_clk_src[] = {
47 "sys_syn_clk",
48 "pll1_clk",
49 "pll2_clk",
50 "pll3_clk",
51 };
52
53 /*
54 * As sys clk can have multiple source with their own range
55 * limitation so we choose possible sources accordingly
56 */
57 if (newfreq <= 300000000)
58 pclk = 0; /* src is sys_syn_clk */
59 else if (newfreq > 300000000 && newfreq <= 500000000)
60 pclk = 3; /* src is pll3_clk */
61 else if (newfreq == 600000000)
62 pclk = 1; /* src is pll1_clk */
63 else
64 return ERR_PTR(-EINVAL);
65
66 /* Get parent to sys clock */
67 sys_pclk = clk_get(NULL, sys_clk_src[pclk]);
68 if (IS_ERR(sys_pclk))
69 pr_err("Failed to get %s clock\n", sys_clk_src[pclk]);
70
71 return sys_pclk;
72}
73
74/*
75 * In SPEAr1340, we cannot use newfreq directly because we need to actually
76 * access a source clock (clk) which might not be ancestor of cpu at present.
77 * Hence in SPEAr1340 we would operate on source clock directly before switching
78 * cpu clock to it.
79 */
80static int spear1340_set_cpu_rate(struct clk *sys_pclk, unsigned long newfreq)
81{
82 struct clk *sys_clk;
83 int ret = 0;
84
85 sys_clk = clk_get_parent(spear_cpufreq.clk);
86 if (IS_ERR(sys_clk)) {
87 pr_err("failed to get cpu's parent (sys) clock\n");
88 return PTR_ERR(sys_clk);
89 }
90
91 /* Set the rate of the source clock before changing the parent */
92 ret = clk_set_rate(sys_pclk, newfreq);
93 if (ret) {
94 pr_err("Failed to set sys clk rate to %lu\n", newfreq);
95 return ret;
96 }
97
98 ret = clk_set_parent(sys_clk, sys_pclk);
99 if (ret) {
100 pr_err("Failed to set sys clk parent\n");
101 return ret;
102 }
103
104 return 0;
105}
106
107static int spear_cpufreq_target(struct cpufreq_policy *policy,
108 unsigned int target_freq, unsigned int relation)
109{
110 struct cpufreq_freqs freqs;
111 unsigned long newfreq;
112 struct clk *srcclk;
113 int index, ret, mult = 1;
114
115 if (cpufreq_frequency_table_target(policy, spear_cpufreq.freq_tbl,
116 target_freq, relation, &index))
117 return -EINVAL;
118
Deepak Sikri42099322012-11-27 14:05:26 +0100119 freqs.old = spear_cpufreq_get(0);
120
121 newfreq = spear_cpufreq.freq_tbl[index].frequency * 1000;
122 if (of_machine_is_compatible("st,spear1340")) {
123 /*
124 * SPEAr1340 is special in the sense that due to the possibility
125 * of multiple clock sources for cpu clk's parent we can have
126 * different clock source for different frequency of cpu clk.
127 * Hence we need to choose one from amongst these possible clock
128 * sources.
129 */
130 srcclk = spear1340_cpu_get_possible_parent(newfreq);
131 if (IS_ERR(srcclk)) {
132 pr_err("Failed to get src clk\n");
133 return PTR_ERR(srcclk);
134 }
135
136 /* SPEAr1340: src clk is always 2 * intended cpu clk */
137 mult = 2;
138 } else {
139 /*
140 * src clock to be altered is ancestor of cpu clock. Hence we
141 * can directly work on cpu clk
142 */
143 srcclk = spear_cpufreq.clk;
144 }
145
146 newfreq = clk_round_rate(srcclk, newfreq * mult);
147 if (newfreq < 0) {
148 pr_err("clk_round_rate failed for cpu src clock\n");
149 return newfreq;
150 }
151
152 freqs.new = newfreq / 1000;
153 freqs.new /= mult;
Viresh Kumar6f35a652013-01-31 04:53:56 +0000154
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530155 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Deepak Sikri42099322012-11-27 14:05:26 +0100156
157 if (mult == 2)
158 ret = spear1340_set_cpu_rate(srcclk, newfreq);
159 else
160 ret = clk_set_rate(spear_cpufreq.clk, newfreq);
161
162 /* Get current rate after clk_set_rate, in case of failure */
163 if (ret) {
164 pr_err("CPU Freq: cpu clk_set_rate failed: %d\n", ret);
165 freqs.new = clk_get_rate(spear_cpufreq.clk) / 1000;
166 }
167
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530168 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Deepak Sikri42099322012-11-27 14:05:26 +0100169 return ret;
170}
171
172static int spear_cpufreq_init(struct cpufreq_policy *policy)
173{
Viresh Kumar7a936bd2013-10-03 20:42:10 +0530174 return cpufreq_generic_init(policy, spear_cpufreq.freq_tbl,
175 spear_cpufreq.transition_latency);
Deepak Sikri42099322012-11-27 14:05:26 +0100176}
177
Deepak Sikri42099322012-11-27 14:05:26 +0100178static struct cpufreq_driver spear_cpufreq_driver = {
179 .name = "cpufreq-spear",
180 .flags = CPUFREQ_STICKY,
Viresh Kumare2132fa2013-10-03 20:28:27 +0530181 .verify = cpufreq_generic_frequency_table_verify,
Deepak Sikri42099322012-11-27 14:05:26 +0100182 .target = spear_cpufreq_target,
183 .get = spear_cpufreq_get,
184 .init = spear_cpufreq_init,
Viresh Kumare2132fa2013-10-03 20:28:27 +0530185 .exit = cpufreq_generic_exit,
186 .attr = cpufreq_generic_attr,
Deepak Sikri42099322012-11-27 14:05:26 +0100187};
188
189static int spear_cpufreq_driver_init(void)
190{
191 struct device_node *np;
192 const struct property *prop;
193 struct cpufreq_frequency_table *freq_tbl;
194 const __be32 *val;
195 int cnt, i, ret;
196
Sudeep KarkadaNageshac0e46942013-06-17 15:09:15 +0100197 np = of_cpu_device_node_get(0);
Deepak Sikri42099322012-11-27 14:05:26 +0100198 if (!np) {
199 pr_err("No cpu node found");
200 return -ENODEV;
201 }
202
203 if (of_property_read_u32(np, "clock-latency",
204 &spear_cpufreq.transition_latency))
205 spear_cpufreq.transition_latency = CPUFREQ_ETERNAL;
206
207 prop = of_find_property(np, "cpufreq_tbl", NULL);
208 if (!prop || !prop->value) {
209 pr_err("Invalid cpufreq_tbl");
210 ret = -ENODEV;
211 goto out_put_node;
212 }
213
214 cnt = prop->length / sizeof(u32);
215 val = prop->value;
216
217 freq_tbl = kmalloc(sizeof(*freq_tbl) * (cnt + 1), GFP_KERNEL);
218 if (!freq_tbl) {
219 ret = -ENOMEM;
220 goto out_put_node;
221 }
222
223 for (i = 0; i < cnt; i++) {
Viresh Kumar50701582013-03-30 16:25:15 +0530224 freq_tbl[i].driver_data = i;
Deepak Sikri42099322012-11-27 14:05:26 +0100225 freq_tbl[i].frequency = be32_to_cpup(val++);
226 }
227
Viresh Kumar50701582013-03-30 16:25:15 +0530228 freq_tbl[i].driver_data = i;
Deepak Sikri42099322012-11-27 14:05:26 +0100229 freq_tbl[i].frequency = CPUFREQ_TABLE_END;
230
231 spear_cpufreq.freq_tbl = freq_tbl;
232
233 of_node_put(np);
234
235 spear_cpufreq.clk = clk_get(NULL, "cpu_clk");
236 if (IS_ERR(spear_cpufreq.clk)) {
237 pr_err("Unable to get CPU clock\n");
238 ret = PTR_ERR(spear_cpufreq.clk);
239 goto out_put_mem;
240 }
241
242 ret = cpufreq_register_driver(&spear_cpufreq_driver);
243 if (!ret)
244 return 0;
245
246 pr_err("failed register driver: %d\n", ret);
247 clk_put(spear_cpufreq.clk);
248
249out_put_mem:
250 kfree(freq_tbl);
251 return ret;
252
253out_put_node:
254 of_node_put(np);
255 return ret;
256}
257late_initcall(spear_cpufreq_driver_init);
258
259MODULE_AUTHOR("Deepak Sikri <deepak.sikri@st.com>");
260MODULE_DESCRIPTION("SPEAr CPUFreq driver");
261MODULE_LICENSE("GPL");