1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2009 Wolfson Microelectronics plc
5 * S3C64xx CPUfreq Support
8 #define pr_fmt(fmt) "cpufreq: " fmt
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/init.h>
13 #include <linux/cpufreq.h>
14 #include <linux/clk.h>
15 #include <linux/err.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/module.h>
19 static struct regulator *vddarm;
20 static unsigned long regulator_latency;
22 #ifdef CONFIG_CPU_S3C6410
24 unsigned int vddarm_min;
25 unsigned int vddarm_max;
28 static struct s3c64xx_dvfs s3c64xx_dvfs_table[] = {
29 [0] = { 1000000, 1150000 },
30 [1] = { 1050000, 1150000 },
31 [2] = { 1100000, 1150000 },
32 [3] = { 1200000, 1350000 },
33 [4] = { 1300000, 1350000 },
36 static struct cpufreq_frequency_table s3c64xx_freq_table[] = {
49 { 0, 0, CPUFREQ_TABLE_END },
53 static int s3c64xx_cpufreq_set_target(struct cpufreq_policy *policy,
56 struct s3c64xx_dvfs *dvfs;
57 unsigned int old_freq, new_freq;
60 old_freq = clk_get_rate(policy->clk) / 1000;
61 new_freq = s3c64xx_freq_table[index].frequency;
62 dvfs = &s3c64xx_dvfs_table[s3c64xx_freq_table[index].driver_data];
64 #ifdef CONFIG_REGULATOR
65 if (vddarm && new_freq > old_freq) {
66 ret = regulator_set_voltage(vddarm,
70 pr_err("Failed to set VDDARM for %dkHz: %d\n",
77 ret = clk_set_rate(policy->clk, new_freq * 1000);
79 pr_err("Failed to set rate %dkHz: %d\n",
84 #ifdef CONFIG_REGULATOR
85 if (vddarm && new_freq < old_freq) {
86 ret = regulator_set_voltage(vddarm,
90 pr_err("Failed to set VDDARM for %dkHz: %d\n",
92 if (clk_set_rate(policy->clk, old_freq * 1000) < 0)
93 pr_err("Failed to restore original clock rate\n");
100 pr_debug("Set actual frequency %lukHz\n",
101 clk_get_rate(policy->clk) / 1000);
106 #ifdef CONFIG_REGULATOR
107 static void s3c64xx_cpufreq_config_regulator(void)
109 int count, v, i, found;
110 struct cpufreq_frequency_table *freq;
111 struct s3c64xx_dvfs *dvfs;
113 count = regulator_count_voltages(vddarm);
115 pr_err("Unable to check supported voltages\n");
121 cpufreq_for_each_valid_entry(freq, s3c64xx_freq_table) {
122 dvfs = &s3c64xx_dvfs_table[freq->driver_data];
125 for (i = 0; i < count; i++) {
126 v = regulator_list_voltage(vddarm, i);
127 if (v >= dvfs->vddarm_min && v <= dvfs->vddarm_max)
132 pr_debug("%dkHz unsupported by regulator\n",
134 freq->frequency = CPUFREQ_ENTRY_INVALID;
139 /* Guess based on having to do an I2C/SPI write; in future we
140 * will be able to query the regulator performance here. */
141 regulator_latency = 1 * 1000 * 1000;
145 static int s3c64xx_cpufreq_driver_init(struct cpufreq_policy *policy)
147 struct cpufreq_frequency_table *freq;
149 if (policy->cpu != 0)
152 if (s3c64xx_freq_table == NULL) {
153 pr_err("No frequency information for this CPU\n");
157 policy->clk = clk_get(NULL, "armclk");
158 if (IS_ERR(policy->clk)) {
159 pr_err("Unable to obtain ARMCLK: %ld\n",
160 PTR_ERR(policy->clk));
161 return PTR_ERR(policy->clk);
164 #ifdef CONFIG_REGULATOR
165 vddarm = regulator_get(NULL, "vddarm");
166 if (IS_ERR(vddarm)) {
167 pr_err("Failed to obtain VDDARM: %ld\n", PTR_ERR(vddarm));
168 pr_err("Only frequency scaling available\n");
171 s3c64xx_cpufreq_config_regulator();
175 cpufreq_for_each_entry(freq, s3c64xx_freq_table) {
178 /* Check for frequencies we can generate */
179 r = clk_round_rate(policy->clk, freq->frequency * 1000);
181 if (r != freq->frequency) {
182 pr_debug("%dkHz unsupported by clock\n",
184 freq->frequency = CPUFREQ_ENTRY_INVALID;
187 /* If we have no regulator then assume startup
188 * frequency is the maximum we can support. */
189 if (!vddarm && freq->frequency > clk_get_rate(policy->clk) / 1000)
190 freq->frequency = CPUFREQ_ENTRY_INVALID;
193 /* Datasheet says PLL stabalisation time (if we were to use
194 * the PLLs, which we don't currently) is ~300us worst case,
195 * but add some fudge.
197 cpufreq_generic_init(policy, s3c64xx_freq_table,
198 (500 * 1000) + regulator_latency);
202 static struct cpufreq_driver s3c64xx_cpufreq_driver = {
203 .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
204 .verify = cpufreq_generic_frequency_table_verify,
205 .target_index = s3c64xx_cpufreq_set_target,
206 .get = cpufreq_generic_get,
207 .init = s3c64xx_cpufreq_driver_init,
211 static int __init s3c64xx_cpufreq_init(void)
213 return cpufreq_register_driver(&s3c64xx_cpufreq_driver);
215 module_init(s3c64xx_cpufreq_init);