aboutsummaryrefslogtreecommitdiff
path: root/drivers/cpufreq/clk-reg-cpufreq.c
blob: c30d2c5993ed45ef0c2bc303ffc521a65271a4ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright (C) 2011 Freescale Semiconductor, Inc.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt

#include <linux/module.h>
#include <linux/cpufreq.h>
#include <linux/clk.h>
#include <linux/regulator/consumer.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/of.h>

static u32 *cpu_freqs; /* Hz */
static u32 *cpu_volts; /* uV */
static u32 trans_latency; /* ns */
static int cpu_op_nr;
static unsigned int cur_index;

static struct clk *cpu_clk;
static struct regulator *cpu_reg;
static struct cpufreq_frequency_table *freq_table;

static int set_cpu_freq(unsigned long freq, int index, int higher)
{
	int ret = 0;

	if (higher && cpu_reg) {
		ret = regulator_set_voltage(cpu_reg,
				cpu_volts[index * 2], cpu_volts[index * 2 + 1]);
		if (ret) {
			pr_err("set cpu voltage failed!\n");
			return ret;
		}
	}

	ret = clk_set_rate(cpu_clk, freq);
	if (ret) {
		if (cpu_reg)
			regulator_set_voltage(cpu_reg, cpu_volts[cur_index * 2],
						cpu_volts[cur_index * 2 + 1]);
		pr_err("cannot set CPU clock rate\n");
		return ret;
	}

	if (!higher && cpu_reg) {
		ret = regulator_set_voltage(cpu_reg,
				cpu_volts[index * 2], cpu_volts[index * 2 + 1]);
		if (ret)
			pr_warn("set cpu voltage failed, might run on"
				" higher voltage!\n");
		ret = 0;
	}

	return ret;
}

static int clk_reg_verify_speed(struct cpufreq_policy *policy)
{
	return cpufreq_frequency_table_verify(policy, freq_table);
}

static unsigned int clk_reg_get_speed(unsigned int cpu)
{
	return clk_get_rate(cpu_clk) / 1000;
}

static int clk_reg_set_target(struct cpufreq_policy *policy,
			  unsigned int target_freq, unsigned int relation)
{
	struct cpufreq_freqs freqs;
	unsigned long freq_Hz;
	int cpu;
	int ret = 0;
	unsigned int index;

	cpufreq_frequency_table_target(policy, freq_table,
			target_freq, relation, &index);
	freq_Hz = clk_round_rate(cpu_clk, cpu_freqs[index]);
	freq_Hz = freq_Hz ? freq_Hz : cpu_freqs[index];
	freqs.old = clk_get_rate(cpu_clk) / 1000;
	freqs.new = freq_Hz / 1000;
	freqs.flags = 0;

	if (freqs.old == freqs.new)
		return 0;

	for_each_possible_cpu(cpu) {
		freqs.cpu = cpu;
		cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
	}

	ret = set_cpu_freq(freq_Hz, index, (freqs.new > freqs.old));
	if (ret)
		freqs.new = clk_get_rate(cpu_clk) / 1000;
	else
		cur_index = index;

	for_each_possible_cpu(cpu) {
		freqs.cpu = cpu;
		cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
	}

	return ret;
}

static int clk_reg_cpufreq_init(struct cpufreq_policy *policy)
{
	int ret;

	if (policy->cpu >= num_possible_cpus())
		return -EINVAL;

	policy->cur = clk_get_rate(cpu_clk) / 1000;
	policy->shared_type = CPUFREQ_SHARED_TYPE_ANY;
	cpumask_setall(policy->cpus);
	policy->cpuinfo.transition_latency = trans_latency;

	ret = cpufreq_frequency_table_cpuinfo(policy, freq_table);

	if (ret < 0) {
		pr_err("invalid frequency table for cpu %d\n",
			policy->cpu);
		return ret;
	}

	cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
	cpufreq_frequency_table_target(policy, freq_table, policy->cur,
					CPUFREQ_RELATION_H, &cur_index);
	return 0;
}

static int clk_reg_cpufreq_exit(struct cpufreq_policy *policy)
{
	cpufreq_frequency_table_put_attr(policy->cpu);
	return 0;
}

static struct cpufreq_driver clk_reg_cpufreq_driver = {
	.flags = CPUFREQ_STICKY,
	.verify = clk_reg_verify_speed,
	.target = clk_reg_set_target,
	.get = clk_reg_get_speed,
	.init = clk_reg_cpufreq_init,
	.exit = clk_reg_cpufreq_exit,
	.name = "clk-reg",
};


static u32 max_freq = UINT_MAX / 1000; /* kHz */
module_param(max_freq, uint, 0);
MODULE_PARM_DESC(max_freq, "max cpu frequency in unit of kHz");

static int __devinit clk_reg_cpufreq_driver_init(void)
{
	struct device_node *cpu0;
	const struct property *pp;
	int i, ret;

	cpu0 = of_find_node_by_path("/cpus/cpu@0");
	if (!cpu0)
		return -ENODEV;

	pp = of_find_property(cpu0, "cpu-freqs", NULL);
	if (!pp) {
		ret = -ENODEV;
		goto put_node;
	}
	cpu_op_nr = pp->length / sizeof(u32);
	if (!cpu_op_nr) {
		ret = -ENODEV;
		goto put_node;
	}
	ret = -ENOMEM;
	cpu_freqs = kzalloc(sizeof(*cpu_freqs) * cpu_op_nr, GFP_KERNEL);
	if (!cpu_freqs)
		goto put_node;
	of_property_read_u32_array(cpu0, "cpu-freqs", cpu_freqs, cpu_op_nr);

	pp = of_find_property(cpu0, "cpu-volts", NULL);
	if (pp) {
		if (cpu_op_nr * 2 == pp->length / sizeof(u32)) {
			cpu_volts = kzalloc(sizeof(*cpu_volts) * cpu_op_nr * 2,
						GFP_KERNEL);
			if (!cpu_volts)
				goto free_cpu_freqs;
			of_property_read_u32_array(cpu0, "cpu-volts",
						cpu_volts, cpu_op_nr * 2);
		} else
			pr_warn("invalid cpu_volts!\n");
	}

	if (of_property_read_u32(cpu0, "trans-latency", &trans_latency))
		trans_latency = CPUFREQ_ETERNAL;

	cpu_clk = clk_get(NULL, "cpu");
	if (IS_ERR(cpu_clk)) {
		pr_err("failed to get cpu clock\n");
		ret = PTR_ERR(cpu_clk);
		goto free_cpu_volts;
	}

	if (cpu_volts) {
		cpu_reg = regulator_get(NULL, "cpu");
		if (IS_ERR(cpu_reg)) {
			pr_warn("regulator cpu get failed.\n");
			cpu_reg = NULL;
		}
	}

	freq_table = kmalloc(sizeof(struct cpufreq_frequency_table)
				* (cpu_op_nr + 1), GFP_KERNEL);
	if (!freq_table) {
		ret = -ENOMEM;
		goto reg_put;
	}

	for (i = 0; i < cpu_op_nr; i++) {
		freq_table[i].index = i;
		if (cpu_freqs[i] > max_freq * 1000) {
			freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
			continue;
		}

		if (cpu_reg) {
			ret = regulator_is_supported_voltage(cpu_reg,
					cpu_volts[i * 2], cpu_volts[i * 2 + 1]);
			if (ret <= 0) {
				freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
				continue;
			}
		}
		freq_table[i].frequency = cpu_freqs[i] / 1000;
	}

	freq_table[i].index = i;
	freq_table[i].frequency = CPUFREQ_TABLE_END;

	ret = cpufreq_register_driver(&clk_reg_cpufreq_driver);
	if (ret)
		goto free_freq_table;

	of_node_put(cpu0);

	return 0;

free_freq_table:
	kfree(freq_table);
reg_put:
	if (cpu_reg)
		regulator_put(cpu_reg);
	clk_put(cpu_clk);
free_cpu_volts:
	kfree(cpu_volts);
free_cpu_freqs:
	kfree(cpu_freqs);
put_node:
	of_node_put(cpu0);

	return ret;
}

static void clk_reg_cpufreq_driver_exit(void)
{
	cpufreq_unregister_driver(&clk_reg_cpufreq_driver);
	kfree(cpu_freqs);
	kfree(cpu_volts);
	clk_put(cpu_clk);
	if (cpu_reg)
		regulator_put(cpu_reg);
	kfree(freq_table);
}

module_init(clk_reg_cpufreq_driver_init);
module_exit(clk_reg_cpufreq_driver_exit);

MODULE_AUTHOR("Freescale Semiconductor Inc. Richard Zhao <richard.zhao@freescale.com>");
MODULE_DESCRIPTION("Generic CPUFreq driver based on clk and regulator APIs");
MODULE_LICENSE("GPL");