spi-topcliff-pch: Fix issue for transmitting over 4KByte
[zen-stable.git] / arch / arm / mach-davinci / cpufreq.c
blob5bba7070f2714a87ab4f5c4f5cb902c7b7642622
1 /*
2 * CPU frequency scaling for DaVinci
4 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
6 * Based on linux/arch/arm/plat-omap/cpu-omap.c. Original Copyright follows:
8 * Copyright (C) 2005 Nokia Corporation
9 * Written by Tony Lindgren <tony@atomide.com>
11 * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
13 * Copyright (C) 2007-2008 Texas Instruments, Inc.
14 * Updated to support OMAP3
15 * Rajendra Nayak <rnayak@ti.com>
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.
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>
29 #include <mach/hardware.h>
30 #include <mach/cpufreq.h>
31 #include <mach/common.h>
33 #include "clock.h"
35 struct davinci_cpufreq {
36 struct device *dev;
37 struct clk *armclk;
38 struct clk *asyncclk;
39 unsigned long asyncrate;
41 static struct davinci_cpufreq cpufreq;
43 static int davinci_verify_speed(struct cpufreq_policy *policy)
45 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
46 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
47 struct clk *armclk = cpufreq.armclk;
49 if (freq_table)
50 return cpufreq_frequency_table_verify(policy, freq_table);
52 if (policy->cpu)
53 return -EINVAL;
55 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
56 policy->cpuinfo.max_freq);
58 policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000;
59 policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000;
60 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
61 policy->cpuinfo.max_freq);
62 return 0;
65 static unsigned int davinci_getspeed(unsigned int cpu)
67 if (cpu)
68 return 0;
70 return clk_get_rate(cpufreq.armclk) / 1000;
73 static int davinci_target(struct cpufreq_policy *policy,
74 unsigned int target_freq, unsigned int relation)
76 int ret = 0;
77 unsigned int idx;
78 struct cpufreq_freqs freqs;
79 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
80 struct clk *armclk = cpufreq.armclk;
83 * Ensure desired rate is within allowed range. Some govenors
84 * (ondemand) will just pass target_freq=0 to get the minimum.
86 if (target_freq < policy->cpuinfo.min_freq)
87 target_freq = policy->cpuinfo.min_freq;
88 if (target_freq > policy->cpuinfo.max_freq)
89 target_freq = policy->cpuinfo.max_freq;
91 freqs.old = davinci_getspeed(0);
92 freqs.new = clk_round_rate(armclk, target_freq * 1000) / 1000;
93 freqs.cpu = 0;
95 if (freqs.old == freqs.new)
96 return ret;
98 dev_dbg(&cpufreq.dev, "transition: %u --> %u\n", freqs.old, freqs.new);
100 ret = cpufreq_frequency_table_target(policy, pdata->freq_table,
101 freqs.new, relation, &idx);
102 if (ret)
103 return -EINVAL;
105 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
107 /* if moving to higher frequency, up the voltage beforehand */
108 if (pdata->set_voltage && freqs.new > freqs.old) {
109 ret = pdata->set_voltage(idx);
110 if (ret)
111 goto out;
114 ret = clk_set_rate(armclk, idx);
115 if (ret)
116 goto out;
118 if (cpufreq.asyncclk) {
119 ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
120 if (ret)
121 goto out;
124 /* if moving to lower freq, lower the voltage after lowering freq */
125 if (pdata->set_voltage && freqs.new < freqs.old)
126 pdata->set_voltage(idx);
128 out:
129 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
131 return ret;
134 static int davinci_cpu_init(struct cpufreq_policy *policy)
136 int result = 0;
137 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
138 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
140 if (policy->cpu != 0)
141 return -EINVAL;
143 /* Finish platform specific initialization */
144 if (pdata->init) {
145 result = pdata->init();
146 if (result)
147 return result;
150 policy->cur = policy->min = policy->max = davinci_getspeed(0);
152 if (freq_table) {
153 result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
154 if (!result)
155 cpufreq_frequency_table_get_attr(freq_table,
156 policy->cpu);
157 } else {
158 policy->cpuinfo.min_freq = policy->min;
159 policy->cpuinfo.max_freq = policy->max;
162 policy->min = policy->cpuinfo.min_freq;
163 policy->max = policy->cpuinfo.max_freq;
164 policy->cur = davinci_getspeed(0);
167 * Time measurement across the target() function yields ~1500-1800us
168 * time taken with no drivers on notification list.
169 * Setting the latency to 2000 us to accommodate addition of drivers
170 * to pre/post change notification list.
172 policy->cpuinfo.transition_latency = 2000 * 1000;
173 return 0;
176 static int davinci_cpu_exit(struct cpufreq_policy *policy)
178 cpufreq_frequency_table_put_attr(policy->cpu);
179 return 0;
182 static struct freq_attr *davinci_cpufreq_attr[] = {
183 &cpufreq_freq_attr_scaling_available_freqs,
184 NULL,
187 static struct cpufreq_driver davinci_driver = {
188 .flags = CPUFREQ_STICKY,
189 .verify = davinci_verify_speed,
190 .target = davinci_target,
191 .get = davinci_getspeed,
192 .init = davinci_cpu_init,
193 .exit = davinci_cpu_exit,
194 .name = "davinci",
195 .attr = davinci_cpufreq_attr,
198 static int __init davinci_cpufreq_probe(struct platform_device *pdev)
200 struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
201 struct clk *asyncclk;
203 if (!pdata)
204 return -EINVAL;
205 if (!pdata->freq_table)
206 return -EINVAL;
208 cpufreq.dev = &pdev->dev;
210 cpufreq.armclk = clk_get(NULL, "arm");
211 if (IS_ERR(cpufreq.armclk)) {
212 dev_err(cpufreq.dev, "Unable to get ARM clock\n");
213 return PTR_ERR(cpufreq.armclk);
216 asyncclk = clk_get(cpufreq.dev, "async");
217 if (!IS_ERR(asyncclk)) {
218 cpufreq.asyncclk = asyncclk;
219 cpufreq.asyncrate = clk_get_rate(asyncclk);
222 return cpufreq_register_driver(&davinci_driver);
225 static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
227 clk_put(cpufreq.armclk);
229 if (cpufreq.asyncclk)
230 clk_put(cpufreq.asyncclk);
232 return cpufreq_unregister_driver(&davinci_driver);
235 static struct platform_driver davinci_cpufreq_driver = {
236 .driver = {
237 .name = "cpufreq-davinci",
238 .owner = THIS_MODULE,
240 .remove = __exit_p(davinci_cpufreq_remove),
243 static int __init davinci_cpufreq_init(void)
245 return platform_driver_probe(&davinci_cpufreq_driver,
246 davinci_cpufreq_probe);
248 late_initcall(davinci_cpufreq_init);