]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/cpufreq/cpufreq-dt.c
cpufreq: dt: Use dev_pm_opp_set_rate() to switch frequency
[mirror_ubuntu-artful-kernel.git] / drivers / cpufreq / cpufreq-dt.c
CommitLineData
95ceafd4
SG
1/*
2 * Copyright (C) 2012 Freescale Semiconductor, Inc.
3 *
748c8766
VK
4 * Copyright (C) 2014 Linaro.
5 * Viresh Kumar <viresh.kumar@linaro.org>
6 *
bbcf0719 7 * The OPP code in function set_target() is reused from
95ceafd4
SG
8 * drivers/cpufreq/omap-cpufreq.c
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/clk.h>
e1825b25 18#include <linux/cpu.h>
77cff592 19#include <linux/cpu_cooling.h>
95ceafd4 20#include <linux/cpufreq.h>
34e5a527 21#include <linux/cpufreq-dt.h>
77cff592 22#include <linux/cpumask.h>
95ceafd4
SG
23#include <linux/err.h>
24#include <linux/module.h>
25#include <linux/of.h>
e4db1c74 26#include <linux/pm_opp.h>
5553f9e2 27#include <linux/platform_device.h>
95ceafd4
SG
28#include <linux/regulator/consumer.h>
29#include <linux/slab.h>
77cff592 30#include <linux/thermal.h>
95ceafd4 31
d2f31f1d
VK
32struct private_data {
33 struct device *cpu_dev;
34 struct regulator *cpu_reg;
35 struct thermal_cooling_device *cdev;
36 unsigned int voltage_tolerance; /* in percentage */
050794aa 37 const char *reg_name;
d2f31f1d 38};
95ceafd4 39
21c36d35
BZ
40static struct freq_attr *cpufreq_dt_attr[] = {
41 &cpufreq_freq_attr_scaling_available_freqs,
42 NULL, /* Extra space for boost-attr if required */
43 NULL,
44};
45
bbcf0719 46static int set_target(struct cpufreq_policy *policy, unsigned int index)
95ceafd4 47{
d2f31f1d 48 struct private_data *priv = policy->driver_data;
95ceafd4 49
78c3ba5d
VK
50 return dev_pm_opp_set_rate(priv->cpu_dev,
51 policy->freq_table[index].frequency * 1000);
95ceafd4
SG
52}
53
050794aa
VK
54/*
55 * An earlier version of opp-v1 bindings used to name the regulator
56 * "cpu0-supply", we still need to handle that for backwards compatibility.
57 */
58static const char *find_supply_name(struct device *dev, struct device_node *np)
59{
60 struct property *pp;
61 int cpu = dev->id;
62
63 /* Try "cpu0" for older DTs */
64 if (!cpu) {
65 pp = of_find_property(np, "cpu0-supply", NULL);
66 if (pp)
67 return "cpu0";
68 }
69
70 pp = of_find_property(np, "cpu-supply", NULL);
71 if (pp)
72 return "cpu";
73
74 dev_dbg(dev, "no regulator for cpu%d\n", cpu);
75 return NULL;
76}
77
95b61058 78static int allocate_resources(int cpu, struct device **cdev,
d2f31f1d 79 struct regulator **creg, struct clk **cclk)
95ceafd4 80{
d2f31f1d
VK
81 struct device *cpu_dev;
82 struct regulator *cpu_reg;
83 struct clk *cpu_clk;
84 int ret = 0;
2d2c5e0e 85 char *reg_cpu0 = "cpu0", *reg_cpu = "cpu", *reg;
95ceafd4 86
95b61058 87 cpu_dev = get_cpu_device(cpu);
e1825b25 88 if (!cpu_dev) {
95b61058 89 pr_err("failed to get cpu%d device\n", cpu);
e1825b25
SH
90 return -ENODEV;
91 }
6754f556 92
2d2c5e0e 93 /* Try "cpu0" for older DTs */
95b61058
VK
94 if (!cpu)
95 reg = reg_cpu0;
96 else
97 reg = reg_cpu;
2d2c5e0e
VK
98
99try_again:
100 cpu_reg = regulator_get_optional(cpu_dev, reg);
b331bc20
AB
101 ret = PTR_ERR_OR_ZERO(cpu_reg);
102 if (ret) {
fc31d6f5 103 /*
95b61058 104 * If cpu's regulator supply node is present, but regulator is
fc31d6f5
NM
105 * not yet registered, we should try defering probe.
106 */
b331bc20 107 if (ret == -EPROBE_DEFER) {
95b61058
VK
108 dev_dbg(cpu_dev, "cpu%d regulator not ready, retry\n",
109 cpu);
b331bc20 110 return ret;
fc31d6f5 111 }
2d2c5e0e
VK
112
113 /* Try with "cpu-supply" */
114 if (reg == reg_cpu0) {
115 reg = reg_cpu;
116 goto try_again;
117 }
118
b331bc20 119 dev_dbg(cpu_dev, "no regulator for cpu%d: %d\n", cpu, ret);
fc31d6f5
NM
120 }
121
e3beb0ac 122 cpu_clk = clk_get(cpu_dev, NULL);
b331bc20
AB
123 ret = PTR_ERR_OR_ZERO(cpu_clk);
124 if (ret) {
d2f31f1d
VK
125 /* put regulator */
126 if (!IS_ERR(cpu_reg))
127 regulator_put(cpu_reg);
128
48a8624b
VK
129 /*
130 * If cpu's clk node is present, but clock is not yet
131 * registered, we should try defering probe.
132 */
133 if (ret == -EPROBE_DEFER)
95b61058 134 dev_dbg(cpu_dev, "cpu%d clock not ready, retry\n", cpu);
48a8624b 135 else
71796210
AK
136 dev_err(cpu_dev, "failed to get cpu%d clock: %d\n", cpu,
137 ret);
d2f31f1d
VK
138 } else {
139 *cdev = cpu_dev;
140 *creg = cpu_reg;
141 *cclk = cpu_clk;
142 }
143
144 return ret;
145}
146
bbcf0719 147static int cpufreq_init(struct cpufreq_policy *policy)
d2f31f1d
VK
148{
149 struct cpufreq_frequency_table *freq_table;
d2f31f1d
VK
150 struct device_node *np;
151 struct private_data *priv;
152 struct device *cpu_dev;
153 struct regulator *cpu_reg;
154 struct clk *cpu_clk;
953ba9ff 155 struct dev_pm_opp *suspend_opp;
d2f31f1d 156 unsigned int transition_latency;
457e99e6 157 bool opp_v1 = false;
050794aa 158 const char *name;
d2f31f1d
VK
159 int ret;
160
95b61058 161 ret = allocate_resources(policy->cpu, &cpu_dev, &cpu_reg, &cpu_clk);
d2f31f1d 162 if (ret) {
edd52b1c 163 pr_err("%s: Failed to allocate resources: %d\n", __func__, ret);
d2f31f1d
VK
164 return ret;
165 }
48a8624b 166
d2f31f1d
VK
167 np = of_node_get(cpu_dev->of_node);
168 if (!np) {
169 dev_err(cpu_dev, "failed to find cpu%d node\n", policy->cpu);
170 ret = -ENOENT;
171 goto out_put_reg_clk;
95ceafd4
SG
172 }
173
2e02d872 174 /* Get OPP-sharing information from "operating-points-v2" bindings */
8f8d37b2 175 ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, policy->cpus);
2e02d872
VK
176 if (ret) {
177 /*
178 * operating-points-v2 not supported, fallback to old method of
179 * finding shared-OPPs for backward compatibility.
180 */
181 if (ret == -ENOENT)
457e99e6 182 opp_v1 = true;
2e02d872
VK
183 else
184 goto out_node_put;
185 }
186
050794aa
VK
187 /*
188 * OPP layer will be taking care of regulators now, but it needs to know
189 * the name of the regulator first.
190 */
191 name = find_supply_name(cpu_dev, np);
192 if (name) {
193 ret = dev_pm_opp_set_regulator(cpu_dev, name);
194 if (ret) {
195 dev_err(cpu_dev, "Failed to set regulator for cpu%d: %d\n",
196 policy->cpu, ret);
197 goto out_node_put;
198 }
199 }
200
2e02d872
VK
201 /*
202 * Initialize OPP tables for all policy->cpus. They will be shared by
203 * all CPUs which have marked their CPUs shared with OPP bindings.
204 *
205 * For platforms not using operating-points-v2 bindings, we do this
206 * before updating policy->cpus. Otherwise, we will end up creating
207 * duplicate OPPs for policy->cpus.
208 *
209 * OPPs might be populated at runtime, don't check for error here
210 */
8f8d37b2 211 dev_pm_opp_of_cpumask_add_table(policy->cpus);
2e02d872 212
7d5d0c8b
VK
213 /*
214 * But we need OPP table to function so if it is not there let's
215 * give platform code chance to provide it for us.
216 */
217 ret = dev_pm_opp_get_opp_count(cpu_dev);
218 if (ret <= 0) {
896d6a4c 219 dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
7d5d0c8b
VK
220 ret = -EPROBE_DEFER;
221 goto out_free_opp;
222 }
223
457e99e6 224 if (opp_v1) {
2e02d872
VK
225 struct cpufreq_dt_platform_data *pd = cpufreq_get_driver_data();
226
227 if (!pd || !pd->independent_clocks)
228 cpumask_setall(policy->cpus);
229
230 /*
231 * OPP tables are initialized only for policy->cpu, do it for
232 * others as well.
233 */
8f8d37b2 234 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
8bc86284
VK
235 if (ret)
236 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
237 __func__, ret);
2e02d872 238 }
95ceafd4 239
d2f31f1d
VK
240 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
241 if (!priv) {
242 ret = -ENOMEM;
2f0f609f 243 goto out_free_opp;
95ceafd4
SG
244 }
245
050794aa 246 priv->reg_name = name;
d2f31f1d 247 of_property_read_u32(np, "voltage-tolerance", &priv->voltage_tolerance);
95ceafd4 248
045ee45c
LS
249 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
250 if (ret) {
896d6a4c 251 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
045ee45c
LS
252 goto out_free_priv;
253 }
254
d2f31f1d
VK
255 priv->cpu_dev = cpu_dev;
256 priv->cpu_reg = cpu_reg;
257 policy->driver_data = priv;
258
259 policy->clk = cpu_clk;
953ba9ff
BZ
260
261 rcu_read_lock();
262 suspend_opp = dev_pm_opp_get_suspend_opp(cpu_dev);
263 if (suspend_opp)
264 policy->suspend_freq = dev_pm_opp_get_freq(suspend_opp) / 1000;
265 rcu_read_unlock();
266
34e5a527
TP
267 ret = cpufreq_table_validate_and_show(policy, freq_table);
268 if (ret) {
269 dev_err(cpu_dev, "%s: invalid frequency table: %d\n", __func__,
270 ret);
9a004428 271 goto out_free_cpufreq_table;
d15fa862
VK
272 }
273
274 /* Support turbo/boost mode */
275 if (policy_has_boost_freq(policy)) {
276 /* This gets disabled by core on driver unregister */
277 ret = cpufreq_enable_boost_support();
278 if (ret)
279 goto out_free_cpufreq_table;
21c36d35 280 cpufreq_dt_attr[1] = &cpufreq_freq_attr_scaling_boost_freqs;
34e5a527
TP
281 }
282
755b888f
VK
283 transition_latency = dev_pm_opp_get_max_transition_latency(cpu_dev);
284 if (!transition_latency)
285 transition_latency = CPUFREQ_ETERNAL;
286
34e5a527
TP
287 policy->cpuinfo.transition_latency = transition_latency;
288
f9739d27
LS
289 of_node_put(np);
290
95ceafd4
SG
291 return 0;
292
9a004428 293out_free_cpufreq_table:
5d4879cd 294 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
045ee45c
LS
295out_free_priv:
296 kfree(priv);
2f0f609f 297out_free_opp:
8f8d37b2 298 dev_pm_opp_of_cpumask_remove_table(policy->cpus);
050794aa
VK
299 if (name)
300 dev_pm_opp_put_regulator(cpu_dev);
2e02d872 301out_node_put:
d2f31f1d
VK
302 of_node_put(np);
303out_put_reg_clk:
ed4b053c 304 clk_put(cpu_clk);
e3beb0ac
LS
305 if (!IS_ERR(cpu_reg))
306 regulator_put(cpu_reg);
d2f31f1d
VK
307
308 return ret;
309}
310
bbcf0719 311static int cpufreq_exit(struct cpufreq_policy *policy)
d2f31f1d
VK
312{
313 struct private_data *priv = policy->driver_data;
314
17ad13ba 315 cpufreq_cooling_unregister(priv->cdev);
d2f31f1d 316 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
8f8d37b2 317 dev_pm_opp_of_cpumask_remove_table(policy->related_cpus);
050794aa
VK
318 if (priv->reg_name)
319 dev_pm_opp_put_regulator(priv->cpu_dev);
320
d2f31f1d
VK
321 clk_put(policy->clk);
322 if (!IS_ERR(priv->cpu_reg))
323 regulator_put(priv->cpu_reg);
324 kfree(priv);
325
326 return 0;
327}
328
9a004428
VK
329static void cpufreq_ready(struct cpufreq_policy *policy)
330{
331 struct private_data *priv = policy->driver_data;
332 struct device_node *np = of_node_get(priv->cpu_dev->of_node);
333
334 if (WARN_ON(!np))
335 return;
336
337 /*
338 * For now, just loading the cooling device;
339 * thermal DT code takes care of matching them.
340 */
341 if (of_find_property(np, "#cooling-cells", NULL)) {
f8fa8ae0
PA
342 u32 power_coefficient = 0;
343
344 of_property_read_u32(np, "dynamic-power-coefficient",
345 &power_coefficient);
346
347 priv->cdev = of_cpufreq_power_cooling_register(np,
348 policy->related_cpus, power_coefficient, NULL);
9a004428
VK
349 if (IS_ERR(priv->cdev)) {
350 dev_err(priv->cpu_dev,
351 "running cpufreq without cooling device: %ld\n",
352 PTR_ERR(priv->cdev));
353
354 priv->cdev = NULL;
355 }
356 }
357
358 of_node_put(np);
359}
360
bbcf0719 361static struct cpufreq_driver dt_cpufreq_driver = {
d2f31f1d
VK
362 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
363 .verify = cpufreq_generic_frequency_table_verify,
bbcf0719 364 .target_index = set_target,
d2f31f1d 365 .get = cpufreq_generic_get,
bbcf0719
VK
366 .init = cpufreq_init,
367 .exit = cpufreq_exit,
9a004428 368 .ready = cpufreq_ready,
bbcf0719 369 .name = "cpufreq-dt",
21c36d35 370 .attr = cpufreq_dt_attr,
953ba9ff 371 .suspend = cpufreq_generic_suspend,
d2f31f1d
VK
372};
373
bbcf0719 374static int dt_cpufreq_probe(struct platform_device *pdev)
d2f31f1d
VK
375{
376 struct device *cpu_dev;
377 struct regulator *cpu_reg;
378 struct clk *cpu_clk;
379 int ret;
380
381 /*
382 * All per-cluster (CPUs sharing clock/voltages) initialization is done
383 * from ->init(). In probe(), we just need to make sure that clk and
384 * regulators are available. Else defer probe and retry.
385 *
386 * FIXME: Is checking this only for CPU0 sufficient ?
387 */
95b61058 388 ret = allocate_resources(0, &cpu_dev, &cpu_reg, &cpu_clk);
d2f31f1d
VK
389 if (ret)
390 return ret;
391
392 clk_put(cpu_clk);
393 if (!IS_ERR(cpu_reg))
394 regulator_put(cpu_reg);
395
34e5a527
TP
396 dt_cpufreq_driver.driver_data = dev_get_platdata(&pdev->dev);
397
bbcf0719 398 ret = cpufreq_register_driver(&dt_cpufreq_driver);
d2f31f1d
VK
399 if (ret)
400 dev_err(cpu_dev, "failed register driver: %d\n", ret);
401
95ceafd4
SG
402 return ret;
403}
5553f9e2 404
bbcf0719 405static int dt_cpufreq_remove(struct platform_device *pdev)
5553f9e2 406{
bbcf0719 407 cpufreq_unregister_driver(&dt_cpufreq_driver);
5553f9e2
SG
408 return 0;
409}
410
bbcf0719 411static struct platform_driver dt_cpufreq_platdrv = {
5553f9e2 412 .driver = {
bbcf0719 413 .name = "cpufreq-dt",
5553f9e2 414 },
bbcf0719
VK
415 .probe = dt_cpufreq_probe,
416 .remove = dt_cpufreq_remove,
5553f9e2 417};
bbcf0719 418module_platform_driver(dt_cpufreq_platdrv);
95ceafd4 419
07949bf9 420MODULE_ALIAS("platform:cpufreq-dt");
748c8766 421MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
95ceafd4 422MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
bbcf0719 423MODULE_DESCRIPTION("Generic cpufreq driver");
95ceafd4 424MODULE_LICENSE("GPL");