Commit | Line | Data |
---|---|---|
6601b803 SN |
1 | /* |
2 | * CPU frequency scaling for DaVinci | |
3 | * | |
4 | * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/ | |
5 | * | |
6 | * Based on linux/arch/arm/plat-omap/cpu-omap.c. Original Copyright follows: | |
7 | * | |
8 | * Copyright (C) 2005 Nokia Corporation | |
9 | * Written by Tony Lindgren <tony@atomide.com> | |
10 | * | |
11 | * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King | |
12 | * | |
13 | * Copyright (C) 2007-2008 Texas Instruments, Inc. | |
14 | * Updated to support OMAP3 | |
15 | * Rajendra Nayak <rnayak@ti.com> | |
16 | * | |
17 | * This program is free software; you can redistribute it and/or modify | |
18 | * it under the terms of the GNU General Public License version 2 as | |
19 | * published by the Free Software Foundation. | |
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 | ||
34 | struct davinci_cpufreq { | |
35 | struct device *dev; | |
36 | struct clk *armclk; | |
37 | }; | |
38 | static struct davinci_cpufreq cpufreq; | |
39 | ||
40 | static int davinci_verify_speed(struct cpufreq_policy *policy) | |
41 | { | |
42 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; | |
43 | struct cpufreq_frequency_table *freq_table = pdata->freq_table; | |
44 | struct clk *armclk = cpufreq.armclk; | |
45 | ||
46 | if (freq_table) | |
47 | return cpufreq_frequency_table_verify(policy, freq_table); | |
48 | ||
49 | if (policy->cpu) | |
50 | return -EINVAL; | |
51 | ||
52 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, | |
53 | policy->cpuinfo.max_freq); | |
54 | ||
55 | policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000; | |
56 | policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000; | |
57 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, | |
58 | policy->cpuinfo.max_freq); | |
59 | return 0; | |
60 | } | |
61 | ||
62 | static unsigned int davinci_getspeed(unsigned int cpu) | |
63 | { | |
64 | if (cpu) | |
65 | return 0; | |
66 | ||
67 | return clk_get_rate(cpufreq.armclk) / 1000; | |
68 | } | |
69 | ||
70 | static int davinci_target(struct cpufreq_policy *policy, | |
71 | unsigned int target_freq, unsigned int relation) | |
72 | { | |
73 | int ret = 0; | |
74 | unsigned int idx; | |
75 | struct cpufreq_freqs freqs; | |
76 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; | |
77 | struct clk *armclk = cpufreq.armclk; | |
78 | ||
79 | /* | |
80 | * Ensure desired rate is within allowed range. Some govenors | |
81 | * (ondemand) will just pass target_freq=0 to get the minimum. | |
82 | */ | |
83 | if (target_freq < policy->cpuinfo.min_freq) | |
84 | target_freq = policy->cpuinfo.min_freq; | |
85 | if (target_freq > policy->cpuinfo.max_freq) | |
86 | target_freq = policy->cpuinfo.max_freq; | |
87 | ||
88 | freqs.old = davinci_getspeed(0); | |
89 | freqs.new = clk_round_rate(armclk, target_freq * 1000) / 1000; | |
90 | freqs.cpu = 0; | |
91 | ||
92 | if (freqs.old == freqs.new) | |
93 | return ret; | |
94 | ||
95 | cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, | |
96 | dev_driver_string(cpufreq.dev), | |
97 | "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 | /* if moving to higher frequency, up the voltage beforehand */ | |
fca97b33 SN |
107 | if (pdata->set_voltage && freqs.new > freqs.old) { |
108 | ret = pdata->set_voltage(idx); | |
109 | if (ret) | |
110 | goto out; | |
111 | } | |
6601b803 SN |
112 | |
113 | ret = clk_set_rate(armclk, idx); | |
fca97b33 SN |
114 | if (ret) |
115 | goto out; | |
6601b803 SN |
116 | |
117 | /* if moving to lower freq, lower the voltage after lowering freq */ | |
118 | if (pdata->set_voltage && freqs.new < freqs.old) | |
119 | pdata->set_voltage(idx); | |
120 | ||
fca97b33 | 121 | out: |
6601b803 SN |
122 | cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); |
123 | ||
124 | return ret; | |
125 | } | |
126 | ||
127 | static int __init davinci_cpu_init(struct cpufreq_policy *policy) | |
128 | { | |
129 | int result = 0; | |
130 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; | |
131 | struct cpufreq_frequency_table *freq_table = pdata->freq_table; | |
132 | ||
133 | if (policy->cpu != 0) | |
134 | return -EINVAL; | |
135 | ||
13d5e27a SN |
136 | /* Finish platform specific initialization */ |
137 | if (pdata->init) { | |
138 | result = pdata->init(); | |
139 | if (result) | |
140 | return result; | |
141 | } | |
142 | ||
6601b803 SN |
143 | policy->cur = policy->min = policy->max = davinci_getspeed(0); |
144 | ||
145 | if (freq_table) { | |
146 | result = cpufreq_frequency_table_cpuinfo(policy, freq_table); | |
147 | if (!result) | |
148 | cpufreq_frequency_table_get_attr(freq_table, | |
149 | policy->cpu); | |
150 | } else { | |
151 | policy->cpuinfo.min_freq = policy->min; | |
152 | policy->cpuinfo.max_freq = policy->max; | |
153 | } | |
154 | ||
155 | policy->min = policy->cpuinfo.min_freq; | |
156 | policy->max = policy->cpuinfo.max_freq; | |
157 | policy->cur = davinci_getspeed(0); | |
158 | ||
159 | /* | |
160 | * Time measurement across the target() function yields ~1500-1800us | |
161 | * time taken with no drivers on notification list. | |
162 | * Setting the latency to 2000 us to accomodate addition of drivers | |
163 | * to pre/post change notification list. | |
164 | */ | |
165 | policy->cpuinfo.transition_latency = 2000 * 1000; | |
166 | return 0; | |
167 | } | |
168 | ||
169 | static int davinci_cpu_exit(struct cpufreq_policy *policy) | |
170 | { | |
171 | cpufreq_frequency_table_put_attr(policy->cpu); | |
172 | return 0; | |
173 | } | |
174 | ||
175 | static struct freq_attr *davinci_cpufreq_attr[] = { | |
176 | &cpufreq_freq_attr_scaling_available_freqs, | |
177 | NULL, | |
178 | }; | |
179 | ||
180 | static struct cpufreq_driver davinci_driver = { | |
181 | .flags = CPUFREQ_STICKY, | |
182 | .verify = davinci_verify_speed, | |
183 | .target = davinci_target, | |
184 | .get = davinci_getspeed, | |
185 | .init = davinci_cpu_init, | |
186 | .exit = davinci_cpu_exit, | |
187 | .name = "davinci", | |
188 | .attr = davinci_cpufreq_attr, | |
189 | }; | |
190 | ||
191 | static int __init davinci_cpufreq_probe(struct platform_device *pdev) | |
192 | { | |
193 | struct davinci_cpufreq_config *pdata = pdev->dev.platform_data; | |
194 | ||
195 | if (!pdata) | |
196 | return -EINVAL; | |
197 | if (!pdata->freq_table) | |
198 | return -EINVAL; | |
199 | ||
200 | cpufreq.dev = &pdev->dev; | |
201 | ||
202 | cpufreq.armclk = clk_get(NULL, "arm"); | |
203 | if (IS_ERR(cpufreq.armclk)) { | |
204 | dev_err(cpufreq.dev, "Unable to get ARM clock\n"); | |
205 | return PTR_ERR(cpufreq.armclk); | |
206 | } | |
207 | ||
208 | return cpufreq_register_driver(&davinci_driver); | |
209 | } | |
210 | ||
211 | static int __exit davinci_cpufreq_remove(struct platform_device *pdev) | |
212 | { | |
213 | clk_put(cpufreq.armclk); | |
214 | ||
215 | return cpufreq_unregister_driver(&davinci_driver); | |
216 | } | |
217 | ||
218 | static struct platform_driver davinci_cpufreq_driver = { | |
219 | .driver = { | |
220 | .name = "cpufreq-davinci", | |
221 | .owner = THIS_MODULE, | |
222 | }, | |
223 | .remove = __exit_p(davinci_cpufreq_remove), | |
224 | }; | |
225 | ||
226 | static int __init davinci_cpufreq_init(void) | |
227 | { | |
228 | return platform_driver_probe(&davinci_cpufreq_driver, | |
229 | davinci_cpufreq_probe); | |
230 | } | |
231 | late_initcall(davinci_cpufreq_init); | |
232 |