1 // SPDX-License-Identifier: GPL-2.0-only
3 * S3C2416/2450 CPUfreq Support
5 * Copyright 2011 Heiko Stuebner <heiko@sntech.de>
7 * based on s3c64xx_cpufreq.c
9 * Copyright 2009 Wolfson Microelectronics plc
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/init.h>
15 #include <linux/cpufreq.h>
16 #include <linux/clk.h>
17 #include <linux/err.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/reboot.h>
20 #include <linux/module.h>
22 static DEFINE_MUTEX(cpufreq_lock
);
29 unsigned long regulator_latency
;
30 #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
31 struct regulator
*vddarm
;
34 struct cpufreq_frequency_table
*freq_table
;
40 static struct s3c2416_data s3c2416_cpufreq
;
43 unsigned int vddarm_min
;
44 unsigned int vddarm_max
;
47 /* pseudo-frequency for dvs mode */
48 #define FREQ_DVS 132333
50 /* frequency to sleep and reboot in
51 * it's essential to leave dvs, as some boards do not reconfigure the
54 #define FREQ_SLEEP 133333
56 /* Sources for the ARMCLK */
58 #define SOURCE_ARMDIV 1
60 #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
61 /* S3C2416 only supports changing the voltage in the dvs-mode.
62 * Voltages down to 1.0V seem to work, so we take what the regulator
65 static struct s3c2416_dvfs s3c2416_dvfs_table
[] = {
66 [SOURCE_HCLK
] = { 950000, 1250000 },
67 [SOURCE_ARMDIV
] = { 1250000, 1350000 },
71 static struct cpufreq_frequency_table s3c2416_freq_table
[] = {
72 { 0, SOURCE_HCLK
, FREQ_DVS
},
73 { 0, SOURCE_ARMDIV
, 133333 },
74 { 0, SOURCE_ARMDIV
, 266666 },
75 { 0, SOURCE_ARMDIV
, 400000 },
76 { 0, 0, CPUFREQ_TABLE_END
},
79 static struct cpufreq_frequency_table s3c2450_freq_table
[] = {
80 { 0, SOURCE_HCLK
, FREQ_DVS
},
81 { 0, SOURCE_ARMDIV
, 133500 },
82 { 0, SOURCE_ARMDIV
, 267000 },
83 { 0, SOURCE_ARMDIV
, 534000 },
84 { 0, 0, CPUFREQ_TABLE_END
},
87 static unsigned int s3c2416_cpufreq_get_speed(unsigned int cpu
)
89 struct s3c2416_data
*s3c_freq
= &s3c2416_cpufreq
;
94 /* return our pseudo-frequency when in dvs mode */
98 return clk_get_rate(s3c_freq
->armclk
) / 1000;
101 static int s3c2416_cpufreq_set_armdiv(struct s3c2416_data
*s3c_freq
,
106 if (clk_get_rate(s3c_freq
->armdiv
) / 1000 != freq
) {
107 ret
= clk_set_rate(s3c_freq
->armdiv
, freq
* 1000);
109 pr_err("cpufreq: Failed to set armdiv rate %dkHz: %d\n",
118 static int s3c2416_cpufreq_enter_dvs(struct s3c2416_data
*s3c_freq
, int idx
)
120 #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
121 struct s3c2416_dvfs
*dvfs
;
125 if (s3c_freq
->is_dvs
) {
126 pr_debug("cpufreq: already in dvs mode, nothing to do\n");
130 pr_debug("cpufreq: switching armclk to hclk (%lukHz)\n",
131 clk_get_rate(s3c_freq
->hclk
) / 1000);
132 ret
= clk_set_parent(s3c_freq
->armclk
, s3c_freq
->hclk
);
134 pr_err("cpufreq: Failed to switch armclk to hclk: %d\n", ret
);
138 #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
139 /* changing the core voltage is only allowed when in dvs mode */
140 if (s3c_freq
->vddarm
) {
141 dvfs
= &s3c2416_dvfs_table
[idx
];
143 pr_debug("cpufreq: setting regulator to %d-%d\n",
144 dvfs
->vddarm_min
, dvfs
->vddarm_max
);
145 ret
= regulator_set_voltage(s3c_freq
->vddarm
,
149 /* when lowering the voltage failed, there is nothing to do */
151 pr_err("cpufreq: Failed to set VDDARM: %d\n", ret
);
155 s3c_freq
->is_dvs
= 1;
160 static int s3c2416_cpufreq_leave_dvs(struct s3c2416_data
*s3c_freq
, int idx
)
162 #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
163 struct s3c2416_dvfs
*dvfs
;
167 if (!s3c_freq
->is_dvs
) {
168 pr_debug("cpufreq: not in dvs mode, so can't leave\n");
172 #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
173 if (s3c_freq
->vddarm
) {
174 dvfs
= &s3c2416_dvfs_table
[idx
];
176 pr_debug("cpufreq: setting regulator to %d-%d\n",
177 dvfs
->vddarm_min
, dvfs
->vddarm_max
);
178 ret
= regulator_set_voltage(s3c_freq
->vddarm
,
182 pr_err("cpufreq: Failed to set VDDARM: %d\n", ret
);
188 /* force armdiv to hclk frequency for transition from dvs*/
189 if (clk_get_rate(s3c_freq
->armdiv
) > clk_get_rate(s3c_freq
->hclk
)) {
190 pr_debug("cpufreq: force armdiv to hclk frequency (%lukHz)\n",
191 clk_get_rate(s3c_freq
->hclk
) / 1000);
192 ret
= s3c2416_cpufreq_set_armdiv(s3c_freq
,
193 clk_get_rate(s3c_freq
->hclk
) / 1000);
195 pr_err("cpufreq: Failed to set the armdiv to %lukHz: %d\n",
196 clk_get_rate(s3c_freq
->hclk
) / 1000, ret
);
201 pr_debug("cpufreq: switching armclk parent to armdiv (%lukHz)\n",
202 clk_get_rate(s3c_freq
->armdiv
) / 1000);
204 ret
= clk_set_parent(s3c_freq
->armclk
, s3c_freq
->armdiv
);
206 pr_err("cpufreq: Failed to switch armclk clock parent to armdiv: %d\n",
211 s3c_freq
->is_dvs
= 0;
216 static int s3c2416_cpufreq_set_target(struct cpufreq_policy
*policy
,
219 struct s3c2416_data
*s3c_freq
= &s3c2416_cpufreq
;
220 unsigned int new_freq
;
221 int idx
, ret
, to_dvs
= 0;
223 mutex_lock(&cpufreq_lock
);
225 idx
= s3c_freq
->freq_table
[index
].driver_data
;
227 if (idx
== SOURCE_HCLK
)
230 /* switching to dvs when it's not allowed */
231 if (to_dvs
&& s3c_freq
->disable_dvs
) {
232 pr_debug("cpufreq: entering dvs mode not allowed\n");
237 /* When leavin dvs mode, always switch the armdiv to the hclk rate
238 * The S3C2416 has stability issues when switching directly to
239 * higher frequencies.
241 new_freq
= (s3c_freq
->is_dvs
&& !to_dvs
)
242 ? clk_get_rate(s3c_freq
->hclk
) / 1000
243 : s3c_freq
->freq_table
[index
].frequency
;
246 pr_debug("cpufreq: enter dvs\n");
247 ret
= s3c2416_cpufreq_enter_dvs(s3c_freq
, idx
);
248 } else if (s3c_freq
->is_dvs
) {
249 pr_debug("cpufreq: leave dvs\n");
250 ret
= s3c2416_cpufreq_leave_dvs(s3c_freq
, idx
);
252 pr_debug("cpufreq: change armdiv to %dkHz\n", new_freq
);
253 ret
= s3c2416_cpufreq_set_armdiv(s3c_freq
, new_freq
);
257 mutex_unlock(&cpufreq_lock
);
262 #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
263 static void s3c2416_cpufreq_cfg_regulator(struct s3c2416_data
*s3c_freq
)
265 int count
, v
, i
, found
;
266 struct cpufreq_frequency_table
*pos
;
267 struct s3c2416_dvfs
*dvfs
;
269 count
= regulator_count_voltages(s3c_freq
->vddarm
);
271 pr_err("cpufreq: Unable to check supported voltages\n");
278 cpufreq_for_each_valid_entry(pos
, s3c_freq
->freq_table
) {
279 dvfs
= &s3c2416_dvfs_table
[pos
->driver_data
];
282 /* Check only the min-voltage, more is always ok on S3C2416 */
283 for (i
= 0; i
< count
; i
++) {
284 v
= regulator_list_voltage(s3c_freq
->vddarm
, i
);
285 if (v
>= dvfs
->vddarm_min
)
290 pr_debug("cpufreq: %dkHz unsupported by regulator\n",
292 pos
->frequency
= CPUFREQ_ENTRY_INVALID
;
298 s3c_freq
->regulator_latency
= 1 * 1000 * 1000;
302 static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block
*this,
303 unsigned long event
, void *ptr
)
305 struct s3c2416_data
*s3c_freq
= &s3c2416_cpufreq
;
307 struct cpufreq_policy
*policy
;
309 mutex_lock(&cpufreq_lock
);
311 /* disable further changes */
312 s3c_freq
->disable_dvs
= 1;
314 mutex_unlock(&cpufreq_lock
);
316 /* some boards don't reconfigure the regulator on reboot, which
317 * could lead to undervolting the cpu when the clock is reset.
318 * Therefore we always leave the DVS mode on reboot.
320 if (s3c_freq
->is_dvs
) {
321 pr_debug("cpufreq: leave dvs on reboot\n");
323 policy
= cpufreq_cpu_get(0);
325 pr_debug("cpufreq: get no policy for cpu0\n");
329 ret
= cpufreq_driver_target(policy
, FREQ_SLEEP
, 0);
330 cpufreq_cpu_put(policy
);
339 static struct notifier_block s3c2416_cpufreq_reboot_notifier
= {
340 .notifier_call
= s3c2416_cpufreq_reboot_notifier_evt
,
343 static int s3c2416_cpufreq_driver_init(struct cpufreq_policy
*policy
)
345 struct s3c2416_data
*s3c_freq
= &s3c2416_cpufreq
;
346 struct cpufreq_frequency_table
*pos
;
351 if (policy
->cpu
!= 0)
354 msysclk
= clk_get(NULL
, "msysclk");
355 if (IS_ERR(msysclk
)) {
356 ret
= PTR_ERR(msysclk
);
357 pr_err("cpufreq: Unable to obtain msysclk: %d\n", ret
);
362 * S3C2416 and S3C2450 share the same processor-ID and also provide no
363 * other means to distinguish them other than through the rate of
364 * msysclk. On S3C2416 msysclk runs at 800MHz and on S3C2450 at 533MHz.
366 rate
= clk_get_rate(msysclk
);
367 if (rate
== 800 * 1000 * 1000) {
368 pr_info("cpufreq: msysclk running at %lukHz, using S3C2416 frequency table\n",
370 s3c_freq
->freq_table
= s3c2416_freq_table
;
371 policy
->cpuinfo
.max_freq
= 400000;
372 } else if (rate
/ 1000 == 534000) {
373 pr_info("cpufreq: msysclk running at %lukHz, using S3C2450 frequency table\n",
375 s3c_freq
->freq_table
= s3c2450_freq_table
;
376 policy
->cpuinfo
.max_freq
= 534000;
379 /* not needed anymore */
382 if (s3c_freq
->freq_table
== NULL
) {
383 pr_err("cpufreq: No frequency information for this CPU, msysclk at %lukHz\n",
388 s3c_freq
->is_dvs
= 0;
390 s3c_freq
->armdiv
= clk_get(NULL
, "armdiv");
391 if (IS_ERR(s3c_freq
->armdiv
)) {
392 ret
= PTR_ERR(s3c_freq
->armdiv
);
393 pr_err("cpufreq: Unable to obtain ARMDIV: %d\n", ret
);
397 s3c_freq
->hclk
= clk_get(NULL
, "hclk");
398 if (IS_ERR(s3c_freq
->hclk
)) {
399 ret
= PTR_ERR(s3c_freq
->hclk
);
400 pr_err("cpufreq: Unable to obtain HCLK: %d\n", ret
);
404 /* chech hclk rate, we only support the common 133MHz for now
405 * hclk could also run at 66MHz, but this not often used
407 rate
= clk_get_rate(s3c_freq
->hclk
);
408 if (rate
< 133 * 1000 * 1000) {
409 pr_err("cpufreq: HCLK not at 133MHz\n");
414 s3c_freq
->armclk
= clk_get(NULL
, "armclk");
415 if (IS_ERR(s3c_freq
->armclk
)) {
416 ret
= PTR_ERR(s3c_freq
->armclk
);
417 pr_err("cpufreq: Unable to obtain ARMCLK: %d\n", ret
);
421 #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
422 s3c_freq
->vddarm
= regulator_get(NULL
, "vddarm");
423 if (IS_ERR(s3c_freq
->vddarm
)) {
424 ret
= PTR_ERR(s3c_freq
->vddarm
);
425 pr_err("cpufreq: Failed to obtain VDDARM: %d\n", ret
);
429 s3c2416_cpufreq_cfg_regulator(s3c_freq
);
431 s3c_freq
->regulator_latency
= 0;
434 cpufreq_for_each_entry(pos
, s3c_freq
->freq_table
) {
435 /* special handling for dvs mode */
436 if (pos
->driver_data
== 0) {
437 if (!s3c_freq
->hclk
) {
438 pr_debug("cpufreq: %dkHz unsupported as it would need unavailable dvs mode\n",
440 pos
->frequency
= CPUFREQ_ENTRY_INVALID
;
446 /* Check for frequencies we can generate */
447 rate
= clk_round_rate(s3c_freq
->armdiv
,
448 pos
->frequency
* 1000);
450 if (rate
!= pos
->frequency
) {
451 pr_debug("cpufreq: %dkHz unsupported by clock (clk_round_rate return %lu)\n",
452 pos
->frequency
, rate
);
453 pos
->frequency
= CPUFREQ_ENTRY_INVALID
;
457 /* Datasheet says PLL stabalisation time must be at least 300us,
458 * so but add some fudge. (reference in LOCKCON0 register description)
460 cpufreq_generic_init(policy
, s3c_freq
->freq_table
,
461 (500 * 1000) + s3c_freq
->regulator_latency
);
462 register_reboot_notifier(&s3c2416_cpufreq_reboot_notifier
);
466 #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
468 clk_put(s3c_freq
->armclk
);
471 clk_put(s3c_freq
->hclk
);
473 clk_put(s3c_freq
->armdiv
);
478 static struct cpufreq_driver s3c2416_cpufreq_driver
= {
479 .flags
= CPUFREQ_NEED_INITIAL_FREQ_CHECK
,
480 .verify
= cpufreq_generic_frequency_table_verify
,
481 .target_index
= s3c2416_cpufreq_set_target
,
482 .get
= s3c2416_cpufreq_get_speed
,
483 .init
= s3c2416_cpufreq_driver_init
,
485 .attr
= cpufreq_generic_attr
,
488 static int __init
s3c2416_cpufreq_init(void)
490 return cpufreq_register_driver(&s3c2416_cpufreq_driver
);
492 module_init(s3c2416_cpufreq_init
);