1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/types.h>
22#include <linux/cpufreq.h>
23#include <linux/init.h>
24#include <linux/err.h>
25#include <linux/clk.h>
26#include <linux/platform_device.h>
27
28#include <mach/hardware.h>
29#include <mach/cpufreq.h>
30#include <mach/common.h>
31
32#include "clock.h"
33
34struct davinci_cpufreq {
35 struct device *dev;
36 struct clk *armclk;
37 struct clk *asyncclk;
38 unsigned long asyncrate;
39};
40static struct davinci_cpufreq cpufreq;
41
42static int davinci_verify_speed(struct cpufreq_policy *policy)
43{
44 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
45 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
46 struct clk *armclk = cpufreq.armclk;
47
48 if (freq_table)
49 return cpufreq_frequency_table_verify(policy, freq_table);
50
51 if (policy->cpu)
52 return -EINVAL;
53
54 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
55 policy->cpuinfo.max_freq);
56
57 policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000;
58 policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000;
59 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
60 policy->cpuinfo.max_freq);
61 return 0;
62}
63
64static unsigned int davinci_getspeed(unsigned int cpu)
65{
66 if (cpu)
67 return 0;
68
69 return clk_get_rate(cpufreq.armclk) / 1000;
70}
71
72static int davinci_target(struct cpufreq_policy *policy,
73 unsigned int target_freq, unsigned int relation)
74{
75 int ret = 0;
76 unsigned int idx;
77 struct cpufreq_freqs freqs;
78 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
79 struct clk *armclk = cpufreq.armclk;
80
81
82
83
84
85 if (target_freq < policy->cpuinfo.min_freq)
86 target_freq = policy->cpuinfo.min_freq;
87 if (target_freq > policy->cpuinfo.max_freq)
88 target_freq = policy->cpuinfo.max_freq;
89
90 freqs.old = davinci_getspeed(0);
91 freqs.new = clk_round_rate(armclk, target_freq * 1000) / 1000;
92 freqs.cpu = 0;
93
94 if (freqs.old == freqs.new)
95 return ret;
96
97 dev_dbg(&cpufreq.dev, "transition: %u --> %u\n", freqs.old, freqs.new);
98
99 ret = cpufreq_frequency_table_target(policy, pdata->freq_table,
100 freqs.new, relation, &idx);
101 if (ret)
102 return -EINVAL;
103
104 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
105
106
107 if (pdata->set_voltage && freqs.new > freqs.old) {
108 ret = pdata->set_voltage(idx);
109 if (ret)
110 goto out;
111 }
112
113 ret = clk_set_rate(armclk, idx);
114 if (ret)
115 goto out;
116
117 if (cpufreq.asyncclk) {
118 ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
119 if (ret)
120 goto out;
121 }
122
123
124 if (pdata->set_voltage && freqs.new < freqs.old)
125 pdata->set_voltage(idx);
126
127out:
128 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
129
130 return ret;
131}
132
133static int davinci_cpu_init(struct cpufreq_policy *policy)
134{
135 int result = 0;
136 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
137 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
138
139 if (policy->cpu != 0)
140 return -EINVAL;
141
142
143 if (pdata->init) {
144 result = pdata->init();
145 if (result)
146 return result;
147 }
148
149 policy->cur = policy->min = policy->max = davinci_getspeed(0);
150
151 if (freq_table) {
152 result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
153 if (!result)
154 cpufreq_frequency_table_get_attr(freq_table,
155 policy->cpu);
156 } else {
157 policy->cpuinfo.min_freq = policy->min;
158 policy->cpuinfo.max_freq = policy->max;
159 }
160
161 policy->min = policy->cpuinfo.min_freq;
162 policy->max = policy->cpuinfo.max_freq;
163 policy->cur = davinci_getspeed(0);
164
165
166
167
168
169
170
171 policy->cpuinfo.transition_latency = 2000 * 1000;
172 return 0;
173}
174
175static int davinci_cpu_exit(struct cpufreq_policy *policy)
176{
177 cpufreq_frequency_table_put_attr(policy->cpu);
178 return 0;
179}
180
181static struct freq_attr *davinci_cpufreq_attr[] = {
182 &cpufreq_freq_attr_scaling_available_freqs,
183 NULL,
184};
185
186static struct cpufreq_driver davinci_driver = {
187 .flags = CPUFREQ_STICKY,
188 .verify = davinci_verify_speed,
189 .target = davinci_target,
190 .get = davinci_getspeed,
191 .init = davinci_cpu_init,
192 .exit = davinci_cpu_exit,
193 .name = "davinci",
194 .attr = davinci_cpufreq_attr,
195};
196
197static int __init davinci_cpufreq_probe(struct platform_device *pdev)
198{
199 struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
200 struct clk *asyncclk;
201
202 if (!pdata)
203 return -EINVAL;
204 if (!pdata->freq_table)
205 return -EINVAL;
206
207 cpufreq.dev = &pdev->dev;
208
209 cpufreq.armclk = clk_get(NULL, "arm");
210 if (IS_ERR(cpufreq.armclk)) {
211 dev_err(cpufreq.dev, "Unable to get ARM clock\n");
212 return PTR_ERR(cpufreq.armclk);
213 }
214
215 asyncclk = clk_get(cpufreq.dev, "async");
216 if (!IS_ERR(asyncclk)) {
217 cpufreq.asyncclk = asyncclk;
218 cpufreq.asyncrate = clk_get_rate(asyncclk);
219 }
220
221 return cpufreq_register_driver(&davinci_driver);
222}
223
224static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
225{
226 clk_put(cpufreq.armclk);
227
228 if (cpufreq.asyncclk)
229 clk_put(cpufreq.asyncclk);
230
231 return cpufreq_unregister_driver(&davinci_driver);
232}
233
234static struct platform_driver davinci_cpufreq_driver = {
235 .driver = {
236 .name = "cpufreq-davinci",
237 .owner = THIS_MODULE,
238 },
239 .remove = __exit_p(davinci_cpufreq_remove),
240};
241
242static int __init davinci_cpufreq_init(void)
243{
244 return platform_driver_probe(&davinci_cpufreq_driver,
245 davinci_cpufreq_probe);
246}
247late_initcall(davinci_cpufreq_init);
248
249