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