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