cpufreq: pmac64: use cpufreq_generic_init()
[deliverable/linux.git] / drivers / cpufreq / s3c64xx-cpufreq.c
CommitLineData
be2de99b 1/*
b3748ddd
MB
2 * Copyright 2009 Wolfson Microelectronics plc
3 *
4 * S3C64xx CPUfreq Support
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
a6a43412
MB
11#define pr_fmt(fmt) "cpufreq: " fmt
12
b3748ddd
MB
13#include <linux/kernel.h>
14#include <linux/types.h>
15#include <linux/init.h>
16#include <linux/cpufreq.h>
17#include <linux/clk.h>
18#include <linux/err.h>
19#include <linux/regulator/consumer.h>
a6ee8779 20#include <linux/module.h>
b3748ddd
MB
21
22static struct clk *armclk;
23static struct regulator *vddarm;
43f1069e 24static unsigned long regulator_latency;
b3748ddd
MB
25
26#ifdef CONFIG_CPU_S3C6410
27struct s3c64xx_dvfs {
28 unsigned int vddarm_min;
29 unsigned int vddarm_max;
30};
31
32static struct s3c64xx_dvfs s3c64xx_dvfs_table[] = {
e9c08f0d
MB
33 [0] = { 1000000, 1150000 },
34 [1] = { 1050000, 1150000 },
35 [2] = { 1100000, 1150000 },
36 [3] = { 1200000, 1350000 },
c6e2d685 37 [4] = { 1300000, 1350000 },
b3748ddd
MB
38};
39
40static struct cpufreq_frequency_table s3c64xx_freq_table[] = {
41 { 0, 66000 },
ef993ef8 42 { 0, 100000 },
b3748ddd 43 { 0, 133000 },
ef993ef8 44 { 1, 200000 },
b3748ddd
MB
45 { 1, 222000 },
46 { 1, 266000 },
47 { 2, 333000 },
48 { 2, 400000 },
e9c08f0d
MB
49 { 2, 532000 },
50 { 2, 533000 },
51 { 3, 667000 },
c6e2d685 52 { 4, 800000 },
b3748ddd
MB
53 { 0, CPUFREQ_TABLE_END },
54};
55#endif
56
b3748ddd
MB
57static unsigned int s3c64xx_cpufreq_get_speed(unsigned int cpu)
58{
59 if (cpu != 0)
60 return 0;
61
62 return clk_get_rate(armclk) / 1000;
63}
64
65static int s3c64xx_cpufreq_set_target(struct cpufreq_policy *policy,
66 unsigned int target_freq,
67 unsigned int relation)
68{
69 int ret;
70 unsigned int i;
71 struct cpufreq_freqs freqs;
72 struct s3c64xx_dvfs *dvfs;
73
74 ret = cpufreq_frequency_table_target(policy, s3c64xx_freq_table,
75 target_freq, relation, &i);
76 if (ret != 0)
77 return ret;
78
b3748ddd
MB
79 freqs.old = clk_get_rate(armclk) / 1000;
80 freqs.new = s3c64xx_freq_table[i].frequency;
81 freqs.flags = 0;
50701588 82 dvfs = &s3c64xx_dvfs_table[s3c64xx_freq_table[i].driver_data];
b3748ddd
MB
83
84 if (freqs.old == freqs.new)
85 return 0;
86
a6a43412 87 pr_debug("Transition %d-%dkHz\n", freqs.old, freqs.new);
b3748ddd 88
b43a7ffb 89 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
b3748ddd
MB
90
91#ifdef CONFIG_REGULATOR
92 if (vddarm && freqs.new > freqs.old) {
93 ret = regulator_set_voltage(vddarm,
94 dvfs->vddarm_min,
95 dvfs->vddarm_max);
96 if (ret != 0) {
a6a43412 97 pr_err("Failed to set VDDARM for %dkHz: %d\n",
b3748ddd 98 freqs.new, ret);
6cdc9ef3
VK
99 freqs.new = freqs.old;
100 goto post_notify;
b3748ddd
MB
101 }
102 }
103#endif
104
105 ret = clk_set_rate(armclk, freqs.new * 1000);
106 if (ret < 0) {
a6a43412 107 pr_err("Failed to set rate %dkHz: %d\n",
b3748ddd 108 freqs.new, ret);
6cdc9ef3 109 freqs.new = freqs.old;
b3748ddd
MB
110 }
111
6cdc9ef3 112post_notify:
b43a7ffb 113 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
6cdc9ef3
VK
114 if (ret)
115 goto err;
fb3b1fef 116
b3748ddd
MB
117#ifdef CONFIG_REGULATOR
118 if (vddarm && freqs.new < freqs.old) {
119 ret = regulator_set_voltage(vddarm,
120 dvfs->vddarm_min,
121 dvfs->vddarm_max);
122 if (ret != 0) {
a6a43412 123 pr_err("Failed to set VDDARM for %dkHz: %d\n",
b3748ddd
MB
124 freqs.new, ret);
125 goto err_clk;
126 }
127 }
128#endif
129
a6a43412 130 pr_debug("Set actual frequency %lukHz\n",
b3748ddd
MB
131 clk_get_rate(armclk) / 1000);
132
133 return 0;
134
135err_clk:
136 if (clk_set_rate(armclk, freqs.old * 1000) < 0)
137 pr_err("Failed to restore original clock rate\n");
138err:
b43a7ffb 139 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
b3748ddd
MB
140
141 return ret;
142}
143
144#ifdef CONFIG_REGULATOR
43f1069e 145static void __init s3c64xx_cpufreq_config_regulator(void)
b3748ddd
MB
146{
147 int count, v, i, found;
148 struct cpufreq_frequency_table *freq;
149 struct s3c64xx_dvfs *dvfs;
150
151 count = regulator_count_voltages(vddarm);
152 if (count < 0) {
a6a43412 153 pr_err("Unable to check supported voltages\n");
b3748ddd
MB
154 }
155
156 freq = s3c64xx_freq_table;
43f1069e 157 while (count > 0 && freq->frequency != CPUFREQ_TABLE_END) {
b3748ddd
MB
158 if (freq->frequency == CPUFREQ_ENTRY_INVALID)
159 continue;
160
161 dvfs = &s3c64xx_dvfs_table[freq->index];
162 found = 0;
163
164 for (i = 0; i < count; i++) {
165 v = regulator_list_voltage(vddarm, i);
166 if (v >= dvfs->vddarm_min && v <= dvfs->vddarm_max)
167 found = 1;
168 }
169
170 if (!found) {
a6a43412 171 pr_debug("%dkHz unsupported by regulator\n",
b3748ddd
MB
172 freq->frequency);
173 freq->frequency = CPUFREQ_ENTRY_INVALID;
174 }
175
176 freq++;
177 }
43f1069e
MB
178
179 /* Guess based on having to do an I2C/SPI write; in future we
180 * will be able to query the regulator performance here. */
181 regulator_latency = 1 * 1000 * 1000;
b3748ddd
MB
182}
183#endif
184
6d0de157 185static int s3c64xx_cpufreq_driver_init(struct cpufreq_policy *policy)
b3748ddd
MB
186{
187 int ret;
188 struct cpufreq_frequency_table *freq;
189
190 if (policy->cpu != 0)
191 return -EINVAL;
192
193 if (s3c64xx_freq_table == NULL) {
a6a43412 194 pr_err("No frequency information for this CPU\n");
b3748ddd
MB
195 return -ENODEV;
196 }
197
198 armclk = clk_get(NULL, "armclk");
199 if (IS_ERR(armclk)) {
a6a43412 200 pr_err("Unable to obtain ARMCLK: %ld\n",
b3748ddd
MB
201 PTR_ERR(armclk));
202 return PTR_ERR(armclk);
203 }
204
205#ifdef CONFIG_REGULATOR
206 vddarm = regulator_get(NULL, "vddarm");
207 if (IS_ERR(vddarm)) {
208 ret = PTR_ERR(vddarm);
a6a43412
MB
209 pr_err("Failed to obtain VDDARM: %d\n", ret);
210 pr_err("Only frequency scaling available\n");
b3748ddd
MB
211 vddarm = NULL;
212 } else {
43f1069e 213 s3c64xx_cpufreq_config_regulator();
b3748ddd
MB
214 }
215#endif
216
217 freq = s3c64xx_freq_table;
218 while (freq->frequency != CPUFREQ_TABLE_END) {
219 unsigned long r;
220
221 /* Check for frequencies we can generate */
222 r = clk_round_rate(armclk, freq->frequency * 1000);
223 r /= 1000;
383af9c2 224 if (r != freq->frequency) {
a6a43412 225 pr_debug("%dkHz unsupported by clock\n",
383af9c2 226 freq->frequency);
b3748ddd 227 freq->frequency = CPUFREQ_ENTRY_INVALID;
383af9c2 228 }
b3748ddd
MB
229
230 /* If we have no regulator then assume startup
231 * frequency is the maximum we can support. */
232 if (!vddarm && freq->frequency > s3c64xx_cpufreq_get_speed(0))
233 freq->frequency = CPUFREQ_ENTRY_INVALID;
234
235 freq++;
236 }
237
43f1069e
MB
238 /* Datasheet says PLL stabalisation time (if we were to use
239 * the PLLs, which we don't currently) is ~300us worst case,
240 * but add some fudge.
241 */
242 policy->cpuinfo.transition_latency = (500 * 1000) + regulator_latency;
b3748ddd 243
4974b8ea 244 ret = cpufreq_table_validate_and_show(policy, s3c64xx_freq_table);
b3748ddd 245 if (ret != 0) {
a6a43412 246 pr_err("Failed to configure frequency table: %d\n",
b3748ddd
MB
247 ret);
248 regulator_put(vddarm);
249 clk_put(armclk);
250 }
251
252 return ret;
253}
254
255static struct cpufreq_driver s3c64xx_cpufreq_driver = {
b3748ddd 256 .flags = 0,
e96a4105 257 .verify = cpufreq_generic_frequency_table_verify,
b3748ddd
MB
258 .target = s3c64xx_cpufreq_set_target,
259 .get = s3c64xx_cpufreq_get_speed,
260 .init = s3c64xx_cpufreq_driver_init,
261 .name = "s3c",
262};
263
264static int __init s3c64xx_cpufreq_init(void)
265{
266 return cpufreq_register_driver(&s3c64xx_cpufreq_driver);
267}
268module_init(s3c64xx_cpufreq_init);
This page took 0.263258 seconds and 5 git commands to generate.