1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
215964d38SKukjin Kim /*
315964d38SKukjin Kim * Copyright 2009 Wolfson Microelectronics plc
415964d38SKukjin Kim *
515964d38SKukjin Kim * S3C64xx CPUfreq Support
615964d38SKukjin Kim */
715964d38SKukjin Kim
8a6a43412SMark Brown #define pr_fmt(fmt) "cpufreq: " fmt
9a6a43412SMark Brown
1015964d38SKukjin Kim #include <linux/kernel.h>
1115964d38SKukjin Kim #include <linux/types.h>
1215964d38SKukjin Kim #include <linux/init.h>
1315964d38SKukjin Kim #include <linux/cpufreq.h>
1415964d38SKukjin Kim #include <linux/clk.h>
1515964d38SKukjin Kim #include <linux/err.h>
1615964d38SKukjin Kim #include <linux/regulator/consumer.h>
17a6ee8779SMark Brown #include <linux/module.h>
1815964d38SKukjin Kim
1915964d38SKukjin Kim static struct regulator *vddarm;
2015964d38SKukjin Kim static unsigned long regulator_latency;
2115964d38SKukjin Kim
2215964d38SKukjin Kim struct s3c64xx_dvfs {
2315964d38SKukjin Kim unsigned int vddarm_min;
2415964d38SKukjin Kim unsigned int vddarm_max;
2515964d38SKukjin Kim };
2615964d38SKukjin Kim
27*96616a37SViresh Kumar #ifdef CONFIG_REGULATOR
2815964d38SKukjin Kim static struct s3c64xx_dvfs s3c64xx_dvfs_table[] = {
2915964d38SKukjin Kim [0] = { 1000000, 1150000 },
3015964d38SKukjin Kim [1] = { 1050000, 1150000 },
3115964d38SKukjin Kim [2] = { 1100000, 1150000 },
3215964d38SKukjin Kim [3] = { 1200000, 1350000 },
33c6e2d685SMark Brown [4] = { 1300000, 1350000 },
3415964d38SKukjin Kim };
35*96616a37SViresh Kumar #endif
3615964d38SKukjin Kim
3715964d38SKukjin Kim static struct cpufreq_frequency_table s3c64xx_freq_table[] = {
387f4b0461SViresh Kumar { 0, 0, 66000 },
397f4b0461SViresh Kumar { 0, 0, 100000 },
407f4b0461SViresh Kumar { 0, 0, 133000 },
417f4b0461SViresh Kumar { 0, 1, 200000 },
427f4b0461SViresh Kumar { 0, 1, 222000 },
437f4b0461SViresh Kumar { 0, 1, 266000 },
447f4b0461SViresh Kumar { 0, 2, 333000 },
457f4b0461SViresh Kumar { 0, 2, 400000 },
467f4b0461SViresh Kumar { 0, 2, 532000 },
477f4b0461SViresh Kumar { 0, 2, 533000 },
487f4b0461SViresh Kumar { 0, 3, 667000 },
497f4b0461SViresh Kumar { 0, 4, 800000 },
507f4b0461SViresh Kumar { 0, 0, CPUFREQ_TABLE_END },
5115964d38SKukjin Kim };
5215964d38SKukjin Kim
s3c64xx_cpufreq_set_target(struct cpufreq_policy * policy,unsigned int index)5315964d38SKukjin Kim static int s3c64xx_cpufreq_set_target(struct cpufreq_policy *policy,
549c0ebcf7SViresh Kumar unsigned int index)
5515964d38SKukjin Kim {
56*96616a37SViresh Kumar unsigned int new_freq = s3c64xx_freq_table[index].frequency;
57d4019f0aSViresh Kumar int ret;
5815964d38SKukjin Kim
59*96616a37SViresh Kumar #ifdef CONFIG_REGULATOR
60*96616a37SViresh Kumar struct s3c64xx_dvfs *dvfs;
61*96616a37SViresh Kumar unsigned int old_freq;
62*96616a37SViresh Kumar
63652ed95dSViresh Kumar old_freq = clk_get_rate(policy->clk) / 1000;
649c0ebcf7SViresh Kumar dvfs = &s3c64xx_dvfs_table[s3c64xx_freq_table[index].driver_data];
6515964d38SKukjin Kim
66d4019f0aSViresh Kumar if (vddarm && new_freq > old_freq) {
6715964d38SKukjin Kim ret = regulator_set_voltage(vddarm,
6815964d38SKukjin Kim dvfs->vddarm_min,
6915964d38SKukjin Kim dvfs->vddarm_max);
7015964d38SKukjin Kim if (ret != 0) {
71a6a43412SMark Brown pr_err("Failed to set VDDARM for %dkHz: %d\n",
72d4019f0aSViresh Kumar new_freq, ret);
73d4019f0aSViresh Kumar return ret;
7415964d38SKukjin Kim }
7515964d38SKukjin Kim }
7615964d38SKukjin Kim #endif
7715964d38SKukjin Kim
78652ed95dSViresh Kumar ret = clk_set_rate(policy->clk, new_freq * 1000);
7915964d38SKukjin Kim if (ret < 0) {
80a6a43412SMark Brown pr_err("Failed to set rate %dkHz: %d\n",
81d4019f0aSViresh Kumar new_freq, ret);
82d4019f0aSViresh Kumar return ret;
8315964d38SKukjin Kim }
8415964d38SKukjin Kim
8515964d38SKukjin Kim #ifdef CONFIG_REGULATOR
86d4019f0aSViresh Kumar if (vddarm && new_freq < old_freq) {
8715964d38SKukjin Kim ret = regulator_set_voltage(vddarm,
8815964d38SKukjin Kim dvfs->vddarm_min,
8915964d38SKukjin Kim dvfs->vddarm_max);
9015964d38SKukjin Kim if (ret != 0) {
91a6a43412SMark Brown pr_err("Failed to set VDDARM for %dkHz: %d\n",
92d4019f0aSViresh Kumar new_freq, ret);
93652ed95dSViresh Kumar if (clk_set_rate(policy->clk, old_freq * 1000) < 0)
94d4019f0aSViresh Kumar pr_err("Failed to restore original clock rate\n");
95d4019f0aSViresh Kumar
96d4019f0aSViresh Kumar return ret;
9715964d38SKukjin Kim }
9815964d38SKukjin Kim }
9915964d38SKukjin Kim #endif
10015964d38SKukjin Kim
101a6a43412SMark Brown pr_debug("Set actual frequency %lukHz\n",
102652ed95dSViresh Kumar clk_get_rate(policy->clk) / 1000);
10315964d38SKukjin Kim
10415964d38SKukjin Kim return 0;
10515964d38SKukjin Kim }
10615964d38SKukjin Kim
10715964d38SKukjin Kim #ifdef CONFIG_REGULATOR
s3c64xx_cpufreq_config_regulator(void)108adec57c6SArnd Bergmann static void s3c64xx_cpufreq_config_regulator(void)
10915964d38SKukjin Kim {
11015964d38SKukjin Kim int count, v, i, found;
11115964d38SKukjin Kim struct cpufreq_frequency_table *freq;
11215964d38SKukjin Kim struct s3c64xx_dvfs *dvfs;
11315964d38SKukjin Kim
11415964d38SKukjin Kim count = regulator_count_voltages(vddarm);
11515964d38SKukjin Kim if (count < 0) {
116a6a43412SMark Brown pr_err("Unable to check supported voltages\n");
11715964d38SKukjin Kim }
11815964d38SKukjin Kim
119041526f9SStratos Karafotis if (!count)
120041526f9SStratos Karafotis goto out;
12115964d38SKukjin Kim
122041526f9SStratos Karafotis cpufreq_for_each_valid_entry(freq, s3c64xx_freq_table) {
1230e824432SCharles Keepax dvfs = &s3c64xx_dvfs_table[freq->driver_data];
12415964d38SKukjin Kim found = 0;
12515964d38SKukjin Kim
12615964d38SKukjin Kim for (i = 0; i < count; i++) {
12715964d38SKukjin Kim v = regulator_list_voltage(vddarm, i);
12815964d38SKukjin Kim if (v >= dvfs->vddarm_min && v <= dvfs->vddarm_max)
12915964d38SKukjin Kim found = 1;
13015964d38SKukjin Kim }
13115964d38SKukjin Kim
13215964d38SKukjin Kim if (!found) {
133a6a43412SMark Brown pr_debug("%dkHz unsupported by regulator\n",
13415964d38SKukjin Kim freq->frequency);
13515964d38SKukjin Kim freq->frequency = CPUFREQ_ENTRY_INVALID;
13615964d38SKukjin Kim }
13715964d38SKukjin Kim }
13815964d38SKukjin Kim
139041526f9SStratos Karafotis out:
14015964d38SKukjin Kim /* Guess based on having to do an I2C/SPI write; in future we
14115964d38SKukjin Kim * will be able to query the regulator performance here. */
14215964d38SKukjin Kim regulator_latency = 1 * 1000 * 1000;
14315964d38SKukjin Kim }
14415964d38SKukjin Kim #endif
14515964d38SKukjin Kim
s3c64xx_cpufreq_driver_init(struct cpufreq_policy * policy)14615964d38SKukjin Kim static int s3c64xx_cpufreq_driver_init(struct cpufreq_policy *policy)
14715964d38SKukjin Kim {
14815964d38SKukjin Kim struct cpufreq_frequency_table *freq;
14915964d38SKukjin Kim
15015964d38SKukjin Kim if (policy->cpu != 0)
15115964d38SKukjin Kim return -EINVAL;
15215964d38SKukjin Kim
153652ed95dSViresh Kumar policy->clk = clk_get(NULL, "armclk");
154652ed95dSViresh Kumar if (IS_ERR(policy->clk)) {
155a6a43412SMark Brown pr_err("Unable to obtain ARMCLK: %ld\n",
156652ed95dSViresh Kumar PTR_ERR(policy->clk));
157652ed95dSViresh Kumar return PTR_ERR(policy->clk);
15815964d38SKukjin Kim }
15915964d38SKukjin Kim
16015964d38SKukjin Kim #ifdef CONFIG_REGULATOR
16115964d38SKukjin Kim vddarm = regulator_get(NULL, "vddarm");
16215964d38SKukjin Kim if (IS_ERR(vddarm)) {
163c4dcc8a1SViresh Kumar pr_err("Failed to obtain VDDARM: %ld\n", PTR_ERR(vddarm));
164a6a43412SMark Brown pr_err("Only frequency scaling available\n");
16515964d38SKukjin Kim vddarm = NULL;
16615964d38SKukjin Kim } else {
16715964d38SKukjin Kim s3c64xx_cpufreq_config_regulator();
16815964d38SKukjin Kim }
16915964d38SKukjin Kim #endif
17015964d38SKukjin Kim
171041526f9SStratos Karafotis cpufreq_for_each_entry(freq, s3c64xx_freq_table) {
17215964d38SKukjin Kim unsigned long r;
17315964d38SKukjin Kim
17415964d38SKukjin Kim /* Check for frequencies we can generate */
175652ed95dSViresh Kumar r = clk_round_rate(policy->clk, freq->frequency * 1000);
17615964d38SKukjin Kim r /= 1000;
17715964d38SKukjin Kim if (r != freq->frequency) {
178a6a43412SMark Brown pr_debug("%dkHz unsupported by clock\n",
17915964d38SKukjin Kim freq->frequency);
18015964d38SKukjin Kim freq->frequency = CPUFREQ_ENTRY_INVALID;
18115964d38SKukjin Kim }
18215964d38SKukjin Kim
18315964d38SKukjin Kim /* If we have no regulator then assume startup
18415964d38SKukjin Kim * frequency is the maximum we can support. */
185652ed95dSViresh Kumar if (!vddarm && freq->frequency > clk_get_rate(policy->clk) / 1000)
18615964d38SKukjin Kim freq->frequency = CPUFREQ_ENTRY_INVALID;
18715964d38SKukjin Kim }
18815964d38SKukjin Kim
18915964d38SKukjin Kim /* Datasheet says PLL stabalisation time (if we were to use
19015964d38SKukjin Kim * the PLLs, which we don't currently) is ~300us worst case,
19115964d38SKukjin Kim * but add some fudge.
19215964d38SKukjin Kim */
193c4dcc8a1SViresh Kumar cpufreq_generic_init(policy, s3c64xx_freq_table,
194a307a1e6SViresh Kumar (500 * 1000) + regulator_latency);
195c4dcc8a1SViresh Kumar return 0;
19615964d38SKukjin Kim }
19715964d38SKukjin Kim
19815964d38SKukjin Kim static struct cpufreq_driver s3c64xx_cpufreq_driver = {
199ae6b4271SViresh Kumar .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
200e96a4105SViresh Kumar .verify = cpufreq_generic_frequency_table_verify,
2019c0ebcf7SViresh Kumar .target_index = s3c64xx_cpufreq_set_target,
202652ed95dSViresh Kumar .get = cpufreq_generic_get,
20315964d38SKukjin Kim .init = s3c64xx_cpufreq_driver_init,
20415964d38SKukjin Kim .name = "s3c",
20515964d38SKukjin Kim };
20615964d38SKukjin Kim
s3c64xx_cpufreq_init(void)20715964d38SKukjin Kim static int __init s3c64xx_cpufreq_init(void)
20815964d38SKukjin Kim {
20915964d38SKukjin Kim return cpufreq_register_driver(&s3c64xx_cpufreq_driver);
21015964d38SKukjin Kim }
21115964d38SKukjin Kim module_init(s3c64xx_cpufreq_init);
212