]>
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> | |
dc28094b | 27 | #include <linux/export.h> |
6601b803 SN |
28 | |
29 | #include <mach/hardware.h> | |
30 | #include <mach/cpufreq.h> | |
31 | #include <mach/common.h> | |
32 | ||
6601b803 SN |
33 | struct davinci_cpufreq { |
34 | struct device *dev; | |
35 | struct clk *armclk; | |
30a2c5d2 SN |
36 | struct clk *asyncclk; |
37 | unsigned long asyncrate; | |
6601b803 SN |
38 | }; |
39 | static struct davinci_cpufreq cpufreq; | |
40 | ||
41 | static 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 | ||
be49e346 | 53 | cpufreq_verify_within_cpu_limits(policy); |
6601b803 SN |
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 | ||
9c0ebcf7 | 61 | static int davinci_target(struct cpufreq_policy *policy, unsigned int idx) |
6601b803 | 62 | { |
6601b803 SN |
63 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
64 | struct clk *armclk = cpufreq.armclk; | |
d4019f0a VK |
65 | unsigned int old_freq, new_freq; |
66 | int ret = 0; | |
6601b803 | 67 | |
652ed95d | 68 | old_freq = policy->cur; |
d4019f0a | 69 | new_freq = pdata->freq_table[idx].frequency; |
6601b803 SN |
70 | |
71 | /* if moving to higher frequency, up the voltage beforehand */ | |
d4019f0a | 72 | if (pdata->set_voltage && new_freq > old_freq) { |
fca97b33 SN |
73 | ret = pdata->set_voltage(idx); |
74 | if (ret) | |
d4019f0a | 75 | return ret; |
fca97b33 | 76 | } |
6601b803 SN |
77 | |
78 | ret = clk_set_rate(armclk, idx); | |
fca97b33 | 79 | if (ret) |
d4019f0a | 80 | return ret; |
6601b803 | 81 | |
30a2c5d2 SN |
82 | if (cpufreq.asyncclk) { |
83 | ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate); | |
84 | if (ret) | |
d4019f0a | 85 | return ret; |
30a2c5d2 SN |
86 | } |
87 | ||
6601b803 | 88 | /* if moving to lower freq, lower the voltage after lowering freq */ |
d4019f0a | 89 | if (pdata->set_voltage && new_freq < old_freq) |
6601b803 SN |
90 | pdata->set_voltage(idx); |
91 | ||
d4019f0a | 92 | return 0; |
6601b803 SN |
93 | } |
94 | ||
079db590 | 95 | static int davinci_cpu_init(struct cpufreq_policy *policy) |
6601b803 SN |
96 | { |
97 | int result = 0; | |
98 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; | |
99 | struct cpufreq_frequency_table *freq_table = pdata->freq_table; | |
100 | ||
101 | if (policy->cpu != 0) | |
102 | return -EINVAL; | |
103 | ||
13d5e27a SN |
104 | /* Finish platform specific initialization */ |
105 | if (pdata->init) { | |
106 | result = pdata->init(); | |
107 | if (result) | |
108 | return result; | |
109 | } | |
110 | ||
652ed95d VK |
111 | policy->clk = cpufreq.armclk; |
112 | ||
6601b803 SN |
113 | /* |
114 | * Time measurement across the target() function yields ~1500-1800us | |
115 | * time taken with no drivers on notification list. | |
25985edc | 116 | * Setting the latency to 2000 us to accommodate addition of drivers |
6601b803 SN |
117 | * to pre/post change notification list. |
118 | */ | |
af8c4cfa | 119 | return cpufreq_generic_init(policy, freq_table, 2000 * 1000); |
6601b803 SN |
120 | } |
121 | ||
6601b803 | 122 | static struct cpufreq_driver davinci_driver = { |
ae6b4271 | 123 | .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
6601b803 | 124 | .verify = davinci_verify_speed, |
9c0ebcf7 | 125 | .target_index = davinci_target, |
652ed95d | 126 | .get = cpufreq_generic_get, |
6601b803 | 127 | .init = davinci_cpu_init, |
39d0c362 | 128 | .exit = cpufreq_generic_exit, |
6601b803 | 129 | .name = "davinci", |
39d0c362 | 130 | .attr = cpufreq_generic_attr, |
6601b803 SN |
131 | }; |
132 | ||
133 | static int __init davinci_cpufreq_probe(struct platform_device *pdev) | |
134 | { | |
135 | struct davinci_cpufreq_config *pdata = pdev->dev.platform_data; | |
30a2c5d2 | 136 | struct clk *asyncclk; |
6601b803 SN |
137 | |
138 | if (!pdata) | |
139 | return -EINVAL; | |
140 | if (!pdata->freq_table) | |
141 | return -EINVAL; | |
142 | ||
143 | cpufreq.dev = &pdev->dev; | |
144 | ||
145 | cpufreq.armclk = clk_get(NULL, "arm"); | |
146 | if (IS_ERR(cpufreq.armclk)) { | |
147 | dev_err(cpufreq.dev, "Unable to get ARM clock\n"); | |
148 | return PTR_ERR(cpufreq.armclk); | |
149 | } | |
150 | ||
30a2c5d2 SN |
151 | asyncclk = clk_get(cpufreq.dev, "async"); |
152 | if (!IS_ERR(asyncclk)) { | |
153 | cpufreq.asyncclk = asyncclk; | |
154 | cpufreq.asyncrate = clk_get_rate(asyncclk); | |
155 | } | |
156 | ||
6601b803 SN |
157 | return cpufreq_register_driver(&davinci_driver); |
158 | } | |
159 | ||
160 | static int __exit davinci_cpufreq_remove(struct platform_device *pdev) | |
161 | { | |
162 | clk_put(cpufreq.armclk); | |
163 | ||
30a2c5d2 SN |
164 | if (cpufreq.asyncclk) |
165 | clk_put(cpufreq.asyncclk); | |
166 | ||
6601b803 SN |
167 | return cpufreq_unregister_driver(&davinci_driver); |
168 | } | |
169 | ||
170 | static struct platform_driver davinci_cpufreq_driver = { | |
171 | .driver = { | |
172 | .name = "cpufreq-davinci", | |
173 | .owner = THIS_MODULE, | |
174 | }, | |
175 | .remove = __exit_p(davinci_cpufreq_remove), | |
176 | }; | |
177 | ||
3aa3e840 | 178 | int __init davinci_cpufreq_init(void) |
6601b803 SN |
179 | { |
180 | return platform_driver_probe(&davinci_cpufreq_driver, | |
181 | davinci_cpufreq_probe); | |
182 | } | |
6601b803 | 183 |