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;
308 mutex_lock(&cpufreq_lock);
310 /* disable further changes */
311 s3c_freq->disable_dvs = 1;
313 mutex_unlock(&cpufreq_lock);
315 /* some boards don't reconfigure the regulator on reboot, which
316 * could lead to undervolting the cpu when the clock is reset.
317 * Therefore we always leave the DVS mode on reboot.
319 if (s3c_freq->is_dvs) {
320 pr_debug("cpufreq: leave dvs on reboot\n");
321 ret = cpufreq_driver_target(cpufreq_cpu_get(0), FREQ_SLEEP, 0);
329 static struct notifier_block s3c2416_cpufreq_reboot_notifier = {
330 .notifier_call = s3c2416_cpufreq_reboot_notifier_evt,
333 static int s3c2416_cpufreq_driver_init(struct cpufreq_policy *policy)
335 struct s3c2416_data *s3c_freq = &s3c2416_cpufreq;
336 struct cpufreq_frequency_table *pos;
341 if (policy->cpu != 0)
344 msysclk = clk_get(NULL, "msysclk");
345 if (IS_ERR(msysclk)) {
346 ret = PTR_ERR(msysclk);
347 pr_err("cpufreq: Unable to obtain msysclk: %d\n", ret);
352 * S3C2416 and S3C2450 share the same processor-ID and also provide no
353 * other means to distinguish them other than through the rate of
354 * msysclk. On S3C2416 msysclk runs at 800MHz and on S3C2450 at 533MHz.
356 rate = clk_get_rate(msysclk);
357 if (rate == 800 * 1000 * 1000) {
358 pr_info("cpufreq: msysclk running at %lukHz, using S3C2416 frequency table\n",
360 s3c_freq->freq_table = s3c2416_freq_table;
361 policy->cpuinfo.max_freq = 400000;
362 } else if (rate / 1000 == 534000) {
363 pr_info("cpufreq: msysclk running at %lukHz, using S3C2450 frequency table\n",
365 s3c_freq->freq_table = s3c2450_freq_table;
366 policy->cpuinfo.max_freq = 534000;
369 /* not needed anymore */
372 if (s3c_freq->freq_table == NULL) {
373 pr_err("cpufreq: No frequency information for this CPU, msysclk at %lukHz\n",
378 s3c_freq->is_dvs = 0;
380 s3c_freq->armdiv = clk_get(NULL, "armdiv");
381 if (IS_ERR(s3c_freq->armdiv)) {
382 ret = PTR_ERR(s3c_freq->armdiv);
383 pr_err("cpufreq: Unable to obtain ARMDIV: %d\n", ret);
387 s3c_freq->hclk = clk_get(NULL, "hclk");
388 if (IS_ERR(s3c_freq->hclk)) {
389 ret = PTR_ERR(s3c_freq->hclk);
390 pr_err("cpufreq: Unable to obtain HCLK: %d\n", ret);
394 /* chech hclk rate, we only support the common 133MHz for now
395 * hclk could also run at 66MHz, but this not often used
397 rate = clk_get_rate(s3c_freq->hclk);
398 if (rate < 133 * 1000 * 1000) {
399 pr_err("cpufreq: HCLK not at 133MHz\n");
404 s3c_freq->armclk = clk_get(NULL, "armclk");
405 if (IS_ERR(s3c_freq->armclk)) {
406 ret = PTR_ERR(s3c_freq->armclk);
407 pr_err("cpufreq: Unable to obtain ARMCLK: %d\n", ret);
411 #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
412 s3c_freq->vddarm = regulator_get(NULL, "vddarm");
413 if (IS_ERR(s3c_freq->vddarm)) {
414 ret = PTR_ERR(s3c_freq->vddarm);
415 pr_err("cpufreq: Failed to obtain VDDARM: %d\n", ret);
419 s3c2416_cpufreq_cfg_regulator(s3c_freq);
421 s3c_freq->regulator_latency = 0;
424 cpufreq_for_each_entry(pos, s3c_freq->freq_table) {
425 /* special handling for dvs mode */
426 if (pos->driver_data == 0) {
427 if (!s3c_freq->hclk) {
428 pr_debug("cpufreq: %dkHz unsupported as it would need unavailable dvs mode\n",
430 pos->frequency = CPUFREQ_ENTRY_INVALID;
436 /* Check for frequencies we can generate */
437 rate = clk_round_rate(s3c_freq->armdiv,
438 pos->frequency * 1000);
440 if (rate != pos->frequency) {
441 pr_debug("cpufreq: %dkHz unsupported by clock (clk_round_rate return %lu)\n",
442 pos->frequency, rate);
443 pos->frequency = CPUFREQ_ENTRY_INVALID;
447 /* Datasheet says PLL stabalisation time must be at least 300us,
448 * so but add some fudge. (reference in LOCKCON0 register description)
450 cpufreq_generic_init(policy, s3c_freq->freq_table,
451 (500 * 1000) + s3c_freq->regulator_latency);
452 register_reboot_notifier(&s3c2416_cpufreq_reboot_notifier);
456 #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
458 clk_put(s3c_freq->armclk);
461 clk_put(s3c_freq->hclk);
463 clk_put(s3c_freq->armdiv);
468 static struct cpufreq_driver s3c2416_cpufreq_driver = {
469 .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
470 .verify = cpufreq_generic_frequency_table_verify,
471 .target_index = s3c2416_cpufreq_set_target,
472 .get = s3c2416_cpufreq_get_speed,
473 .init = s3c2416_cpufreq_driver_init,
475 .attr = cpufreq_generic_attr,
478 static int __init s3c2416_cpufreq_init(void)
480 return cpufreq_register_driver(&s3c2416_cpufreq_driver);
482 module_init(s3c2416_cpufreq_init);