s3c64xx-cpufreq.c 5.07 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3 4 5 6 7
 * Copyright 2009 Wolfson Microelectronics plc
 *
 * S3C64xx CPUfreq Support
 */

8 9
#define pr_fmt(fmt) "cpufreq: " fmt

10 11 12 13 14 15 16
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/init.h>
#include <linux/cpufreq.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/regulator/consumer.h>
17
#include <linux/module.h>
18 19

static struct regulator *vddarm;
20
static unsigned long regulator_latency;
21 22 23 24 25 26 27 28

#ifdef CONFIG_CPU_S3C6410
struct s3c64xx_dvfs {
	unsigned int vddarm_min;
	unsigned int vddarm_max;
};

static struct s3c64xx_dvfs s3c64xx_dvfs_table[] = {
29 30 31 32
	[0] = { 1000000, 1150000 },
	[1] = { 1050000, 1150000 },
	[2] = { 1100000, 1150000 },
	[3] = { 1200000, 1350000 },
33
	[4] = { 1300000, 1350000 },
34 35 36
};

static struct cpufreq_frequency_table s3c64xx_freq_table[] = {
37 38 39 40 41 42 43 44 45 46 47 48 49
	{ 0, 0,  66000 },
	{ 0, 0, 100000 },
	{ 0, 0, 133000 },
	{ 0, 1, 200000 },
	{ 0, 1, 222000 },
	{ 0, 1, 266000 },
	{ 0, 2, 333000 },
	{ 0, 2, 400000 },
	{ 0, 2, 532000 },
	{ 0, 2, 533000 },
	{ 0, 3, 667000 },
	{ 0, 4, 800000 },
	{ 0, 0, CPUFREQ_TABLE_END },
50 51 52 53
};
#endif

static int s3c64xx_cpufreq_set_target(struct cpufreq_policy *policy,
54
				      unsigned int index)
55 56
{
	struct s3c64xx_dvfs *dvfs;
57 58
	unsigned int old_freq, new_freq;
	int ret;
59

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];
63 64

#ifdef CONFIG_REGULATOR
65
	if (vddarm && new_freq > old_freq) {
66 67 68 69
		ret = regulator_set_voltage(vddarm,
					    dvfs->vddarm_min,
					    dvfs->vddarm_max);
		if (ret != 0) {
70
			pr_err("Failed to set VDDARM for %dkHz: %d\n",
71 72
			       new_freq, ret);
			return ret;
73 74 75 76
		}
	}
#endif

77
	ret = clk_set_rate(policy->clk, new_freq * 1000);
78
	if (ret < 0) {
79
		pr_err("Failed to set rate %dkHz: %d\n",
80 81
		       new_freq, ret);
		return ret;
82 83 84
	}

#ifdef CONFIG_REGULATOR
85
	if (vddarm && new_freq < old_freq) {
86 87 88 89
		ret = regulator_set_voltage(vddarm,
					    dvfs->vddarm_min,
					    dvfs->vddarm_max);
		if (ret != 0) {
90
			pr_err("Failed to set VDDARM for %dkHz: %d\n",
91
			       new_freq, ret);
92
			if (clk_set_rate(policy->clk, old_freq * 1000) < 0)
93 94 95
				pr_err("Failed to restore original clock rate\n");

			return ret;
96 97 98 99
		}
	}
#endif

100
	pr_debug("Set actual frequency %lukHz\n",
101
		 clk_get_rate(policy->clk) / 1000);
102 103 104 105 106

	return 0;
}

#ifdef CONFIG_REGULATOR
107
static void s3c64xx_cpufreq_config_regulator(void)
108 109 110 111 112 113 114
{
	int count, v, i, found;
	struct cpufreq_frequency_table *freq;
	struct s3c64xx_dvfs *dvfs;

	count = regulator_count_voltages(vddarm);
	if (count < 0) {
115
		pr_err("Unable to check supported voltages\n");
116 117
	}

118 119
	if (!count)
		goto out;
120

121
	cpufreq_for_each_valid_entry(freq, s3c64xx_freq_table) {
122
		dvfs = &s3c64xx_dvfs_table[freq->driver_data];
123 124 125 126 127 128 129 130 131
		found = 0;

		for (i = 0; i < count; i++) {
			v = regulator_list_voltage(vddarm, i);
			if (v >= dvfs->vddarm_min && v <= dvfs->vddarm_max)
				found = 1;
		}

		if (!found) {
132
			pr_debug("%dkHz unsupported by regulator\n",
133 134 135 136
				 freq->frequency);
			freq->frequency = CPUFREQ_ENTRY_INVALID;
		}
	}
137

138
out:
139 140 141
	/* Guess based on having to do an I2C/SPI write; in future we
	 * will be able to query the regulator performance here. */
	regulator_latency = 1 * 1000 * 1000;
142 143 144
}
#endif

145
static int s3c64xx_cpufreq_driver_init(struct cpufreq_policy *policy)
146 147 148 149 150 151 152
{
	struct cpufreq_frequency_table *freq;

	if (policy->cpu != 0)
		return -EINVAL;

	if (s3c64xx_freq_table == NULL) {
153
		pr_err("No frequency information for this CPU\n");
154 155 156
		return -ENODEV;
	}

157 158
	policy->clk = clk_get(NULL, "armclk");
	if (IS_ERR(policy->clk)) {
159
		pr_err("Unable to obtain ARMCLK: %ld\n",
160 161
		       PTR_ERR(policy->clk));
		return PTR_ERR(policy->clk);
162 163 164 165 166
	}

#ifdef CONFIG_REGULATOR
	vddarm = regulator_get(NULL, "vddarm");
	if (IS_ERR(vddarm)) {
167
		pr_err("Failed to obtain VDDARM: %ld\n", PTR_ERR(vddarm));
168
		pr_err("Only frequency scaling available\n");
169 170
		vddarm = NULL;
	} else {
171
		s3c64xx_cpufreq_config_regulator();
172 173 174
	}
#endif

175
	cpufreq_for_each_entry(freq, s3c64xx_freq_table) {
176 177 178
		unsigned long r;

		/* Check for frequencies we can generate */
179
		r = clk_round_rate(policy->clk, freq->frequency * 1000);
180
		r /= 1000;
181
		if (r != freq->frequency) {
182
			pr_debug("%dkHz unsupported by clock\n",
183
				 freq->frequency);
184
			freq->frequency = CPUFREQ_ENTRY_INVALID;
185
		}
186 187 188

		/* If we have no regulator then assume startup
		 * frequency is the maximum we can support. */
189
		if (!vddarm && freq->frequency > clk_get_rate(policy->clk) / 1000)
190 191 192
			freq->frequency = CPUFREQ_ENTRY_INVALID;
	}

193 194 195 196
	/* Datasheet says PLL stabalisation time (if we were to use
	 * the PLLs, which we don't currently) is ~300us worst case,
	 * but add some fudge.
	 */
197
	cpufreq_generic_init(policy, s3c64xx_freq_table,
198
			(500 * 1000) + regulator_latency);
199
	return 0;
200 201 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 208 209 210 211 212 213 214 215
	.init		= s3c64xx_cpufreq_driver_init,
	.name		= "s3c",
};

static int __init s3c64xx_cpufreq_init(void)
{
	return cpufreq_register_driver(&s3c64xx_cpufreq_driver);
}
module_init(s3c64xx_cpufreq_init);