]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/cpufreq/cpufreq-cpu0.c
cpufreq: Notify all policy->cpus in cpufreq_notify_transition()
[mirror_ubuntu-artful-kernel.git] / drivers / cpufreq / cpufreq-cpu0.c
CommitLineData
95ceafd4
SG
1/*
2 * Copyright (C) 2012 Freescale Semiconductor, Inc.
3 *
4 * The OPP code in function cpu0_set_target() is reused from
5 * drivers/cpufreq/omap-cpufreq.c
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/clk.h>
95ceafd4
SG
15#include <linux/cpufreq.h>
16#include <linux/err.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/opp.h>
5553f9e2 20#include <linux/platform_device.h>
95ceafd4
SG
21#include <linux/regulator/consumer.h>
22#include <linux/slab.h>
23
24static unsigned int transition_latency;
25static unsigned int voltage_tolerance; /* in percentage */
26
27static struct device *cpu_dev;
28static struct clk *cpu_clk;
29static struct regulator *cpu_reg;
30static struct cpufreq_frequency_table *freq_table;
31
32static int cpu0_verify_speed(struct cpufreq_policy *policy)
33{
34 return cpufreq_frequency_table_verify(policy, freq_table);
35}
36
37static unsigned int cpu0_get_speed(unsigned int cpu)
38{
39 return clk_get_rate(cpu_clk) / 1000;
40}
41
42static int cpu0_set_target(struct cpufreq_policy *policy,
43 unsigned int target_freq, unsigned int relation)
44{
45 struct cpufreq_freqs freqs;
46 struct opp *opp;
5df60559 47 unsigned long volt = 0, volt_old = 0, tol = 0;
48 long freq_Hz;
b43a7ffb 49 unsigned int index;
95ceafd4
SG
50 int ret;
51
52 ret = cpufreq_frequency_table_target(policy, freq_table, target_freq,
53 relation, &index);
54 if (ret) {
55 pr_err("failed to match target freqency %d: %d\n",
56 target_freq, ret);
57 return ret;
58 }
59
60 freq_Hz = clk_round_rate(cpu_clk, freq_table[index].frequency * 1000);
61 if (freq_Hz < 0)
62 freq_Hz = freq_table[index].frequency * 1000;
63 freqs.new = freq_Hz / 1000;
64 freqs.old = clk_get_rate(cpu_clk) / 1000;
65
66 if (freqs.old == freqs.new)
67 return 0;
68
b43a7ffb 69 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
95ceafd4
SG
70
71 if (cpu_reg) {
78e8eb8f 72 rcu_read_lock();
95ceafd4
SG
73 opp = opp_find_freq_ceil(cpu_dev, &freq_Hz);
74 if (IS_ERR(opp)) {
78e8eb8f 75 rcu_read_unlock();
95ceafd4 76 pr_err("failed to find OPP for %ld\n", freq_Hz);
fd143b4d
VK
77 freqs.new = freqs.old;
78 ret = PTR_ERR(opp);
79 goto post_notify;
95ceafd4
SG
80 }
81 volt = opp_get_voltage(opp);
78e8eb8f 82 rcu_read_unlock();
95ceafd4
SG
83 tol = volt * voltage_tolerance / 100;
84 volt_old = regulator_get_voltage(cpu_reg);
85 }
86
87 pr_debug("%u MHz, %ld mV --> %u MHz, %ld mV\n",
88 freqs.old / 1000, volt_old ? volt_old / 1000 : -1,
89 freqs.new / 1000, volt ? volt / 1000 : -1);
90
91 /* scaling up? scale voltage before frequency */
92 if (cpu_reg && freqs.new > freqs.old) {
93 ret = regulator_set_voltage_tol(cpu_reg, volt, tol);
94 if (ret) {
95 pr_err("failed to scale voltage up: %d\n", ret);
96 freqs.new = freqs.old;
fd143b4d 97 goto post_notify;
95ceafd4
SG
98 }
99 }
100
101 ret = clk_set_rate(cpu_clk, freqs.new * 1000);
102 if (ret) {
103 pr_err("failed to set clock rate: %d\n", ret);
104 if (cpu_reg)
105 regulator_set_voltage_tol(cpu_reg, volt_old, tol);
fd143b4d
VK
106 freqs.new = freqs.old;
107 goto post_notify;
95ceafd4
SG
108 }
109
110 /* scaling down? scale voltage after frequency */
111 if (cpu_reg && freqs.new < freqs.old) {
112 ret = regulator_set_voltage_tol(cpu_reg, volt, tol);
113 if (ret) {
114 pr_err("failed to scale voltage down: %d\n", ret);
115 clk_set_rate(cpu_clk, freqs.old * 1000);
116 freqs.new = freqs.old;
95ceafd4
SG
117 }
118 }
119
fd143b4d 120post_notify:
b43a7ffb 121 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
95ceafd4 122
fd143b4d 123 return ret;
95ceafd4
SG
124}
125
126static int cpu0_cpufreq_init(struct cpufreq_policy *policy)
127{
128 int ret;
129
95ceafd4
SG
130 ret = cpufreq_frequency_table_cpuinfo(policy, freq_table);
131 if (ret) {
132 pr_err("invalid frequency table: %d\n", ret);
133 return ret;
134 }
135
136 policy->cpuinfo.transition_latency = transition_latency;
137 policy->cur = clk_get_rate(cpu_clk) / 1000;
138
139 /*
140 * The driver only supports the SMP configuartion where all processors
141 * share the clock and voltage and clock. Use cpufreq affected_cpus
142 * interface to have all CPUs scaled together.
143 */
95ceafd4
SG
144 cpumask_setall(policy->cpus);
145
146 cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
147
148 return 0;
149}
150
151static int cpu0_cpufreq_exit(struct cpufreq_policy *policy)
152{
153 cpufreq_frequency_table_put_attr(policy->cpu);
154
155 return 0;
156}
157
158static struct freq_attr *cpu0_cpufreq_attr[] = {
159 &cpufreq_freq_attr_scaling_available_freqs,
160 NULL,
161};
162
163static struct cpufreq_driver cpu0_cpufreq_driver = {
164 .flags = CPUFREQ_STICKY,
165 .verify = cpu0_verify_speed,
166 .target = cpu0_set_target,
167 .get = cpu0_get_speed,
168 .init = cpu0_cpufreq_init,
169 .exit = cpu0_cpufreq_exit,
170 .name = "generic_cpu0",
171 .attr = cpu0_cpufreq_attr,
172};
173
5553f9e2 174static int cpu0_cpufreq_probe(struct platform_device *pdev)
95ceafd4
SG
175{
176 struct device_node *np;
177 int ret;
178
6754f556
ML
179 for_each_child_of_node(of_find_node_by_path("/cpus"), np) {
180 if (of_get_property(np, "operating-points", NULL))
181 break;
182 }
183
95ceafd4
SG
184 if (!np) {
185 pr_err("failed to find cpu0 node\n");
186 return -ENOENT;
187 }
188
5553f9e2 189 cpu_dev = &pdev->dev;
95ceafd4
SG
190 cpu_dev->of_node = np;
191
5553f9e2 192 cpu_clk = devm_clk_get(cpu_dev, NULL);
95ceafd4
SG
193 if (IS_ERR(cpu_clk)) {
194 ret = PTR_ERR(cpu_clk);
195 pr_err("failed to get cpu0 clock: %d\n", ret);
196 goto out_put_node;
197 }
198
5553f9e2 199 cpu_reg = devm_regulator_get(cpu_dev, "cpu0");
95ceafd4
SG
200 if (IS_ERR(cpu_reg)) {
201 pr_warn("failed to get cpu0 regulator\n");
202 cpu_reg = NULL;
203 }
204
205 ret = of_init_opp_table(cpu_dev);
206 if (ret) {
207 pr_err("failed to init OPP table: %d\n", ret);
208 goto out_put_node;
209 }
210
211 ret = opp_init_cpufreq_table(cpu_dev, &freq_table);
212 if (ret) {
213 pr_err("failed to init cpufreq table: %d\n", ret);
214 goto out_put_node;
215 }
216
217 of_property_read_u32(np, "voltage-tolerance", &voltage_tolerance);
218
219 if (of_property_read_u32(np, "clock-latency", &transition_latency))
220 transition_latency = CPUFREQ_ETERNAL;
221
222 if (cpu_reg) {
223 struct opp *opp;
224 unsigned long min_uV, max_uV;
225 int i;
226
227 /*
228 * OPP is maintained in order of increasing frequency, and
229 * freq_table initialised from OPP is therefore sorted in the
230 * same order.
231 */
232 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++)
233 ;
78e8eb8f 234 rcu_read_lock();
95ceafd4
SG
235 opp = opp_find_freq_exact(cpu_dev,
236 freq_table[0].frequency * 1000, true);
237 min_uV = opp_get_voltage(opp);
238 opp = opp_find_freq_exact(cpu_dev,
239 freq_table[i-1].frequency * 1000, true);
240 max_uV = opp_get_voltage(opp);
78e8eb8f 241 rcu_read_unlock();
95ceafd4
SG
242 ret = regulator_set_voltage_time(cpu_reg, min_uV, max_uV);
243 if (ret > 0)
244 transition_latency += ret * 1000;
245 }
246
247 ret = cpufreq_register_driver(&cpu0_cpufreq_driver);
248 if (ret) {
249 pr_err("failed register driver: %d\n", ret);
250 goto out_free_table;
251 }
252
253 of_node_put(np);
254 return 0;
255
256out_free_table:
257 opp_free_cpufreq_table(cpu_dev, &freq_table);
258out_put_node:
259 of_node_put(np);
260 return ret;
261}
5553f9e2
SG
262
263static int cpu0_cpufreq_remove(struct platform_device *pdev)
264{
265 cpufreq_unregister_driver(&cpu0_cpufreq_driver);
266 opp_free_cpufreq_table(cpu_dev, &freq_table);
267
268 return 0;
269}
270
271static struct platform_driver cpu0_cpufreq_platdrv = {
272 .driver = {
273 .name = "cpufreq-cpu0",
274 .owner = THIS_MODULE,
275 },
276 .probe = cpu0_cpufreq_probe,
277 .remove = cpu0_cpufreq_remove,
278};
279module_platform_driver(cpu0_cpufreq_platdrv);
95ceafd4
SG
280
281MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
282MODULE_DESCRIPTION("Generic CPU0 cpufreq driver");
283MODULE_LICENSE("GPL");