blob: 7556e07e7a9f12e40666f290866f976e7bd42f4f [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * Copyright (C) 2012 Freescale Semiconductor, Inc.
3 *
4 * Copyright (C) 2014 Linaro.
5 * Viresh Kumar <viresh.kumar@linaro.org>
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>
15#include <linux/cpu.h>
16#include <linux/cpu_cooling.h>
17#include <linux/cpufreq.h>
18#include <linux/cpumask.h>
19#include <linux/energy_model.h>
20#include <linux/err.h>
21#include <linux/module.h>
22#include <linux/of.h>
23#include <linux/pm_opp.h>
24#include <linux/platform_device.h>
25#include <linux/regulator/consumer.h>
26#include <linux/slab.h>
27#include <linux/thermal.h>
28
29#include "cpufreq-dt.h"
30
31struct private_data {
32 struct opp_table *opp_table;
33 struct device *cpu_dev;
34 struct thermal_cooling_device *cdev;
35 const char *reg_name;
36 bool have_static_opps;
37};
38
39static struct freq_attr *cpufreq_dt_attr[] = {
40 &cpufreq_freq_attr_scaling_available_freqs,
41 NULL, /* Extra space for boost-attr if required */
42 NULL,
43};
44
45static int set_target(struct cpufreq_policy *policy, unsigned int index)
46{
47 struct private_data *priv = policy->driver_data;
48 unsigned long freq = policy->freq_table[index].frequency;
49 int ret;
50
51 ret = dev_pm_opp_set_rate(priv->cpu_dev, freq * 1000);
52
53 if (!ret) {
54 arch_set_freq_scale(policy->related_cpus, freq,
55 policy->cpuinfo.max_freq);
56 }
57
58 return ret;
59}
60
61/*
62 * An earlier version of opp-v1 bindings used to name the regulator
63 * "cpu0-supply", we still need to handle that for backwards compatibility.
64 */
65static const char *find_supply_name(struct device *dev)
66{
67 struct device_node *np;
68 struct property *pp;
69 int cpu = dev->id;
70 const char *name = NULL;
71
72 np = of_node_get(dev->of_node);
73
74 /* This must be valid for sure */
75 if (WARN_ON(!np))
76 return NULL;
77
78 /* Try "cpu0" for older DTs */
79 if (!cpu) {
80 pp = of_find_property(np, "cpu0-supply", NULL);
81 if (pp) {
82 name = "cpu0";
83 goto node_put;
84 }
85 }
86
87 pp = of_find_property(np, "cpu-supply", NULL);
88 if (pp) {
89 name = "cpu";
90 goto node_put;
91 }
92
93 dev_dbg(dev, "no regulator for cpu%d\n", cpu);
94node_put:
95 of_node_put(np);
96 return name;
97}
98
99static int resources_available(void)
100{
101 struct device *cpu_dev;
102 struct regulator *cpu_reg;
103 struct clk *cpu_clk;
104 int ret = 0;
105 const char *name;
106
107 cpu_dev = get_cpu_device(0);
108 if (!cpu_dev) {
109 pr_err("failed to get cpu0 device\n");
110 return -ENODEV;
111 }
112
113 cpu_clk = clk_get(cpu_dev, NULL);
114 ret = PTR_ERR_OR_ZERO(cpu_clk);
115 if (ret) {
116 /*
117 * If cpu's clk node is present, but clock is not yet
118 * registered, we should try defering probe.
119 */
120 if (ret == -EPROBE_DEFER)
121 dev_dbg(cpu_dev, "clock not ready, retry\n");
122 else
123 dev_err(cpu_dev, "failed to get clock: %d\n", ret);
124
125 return ret;
126 }
127
128 clk_put(cpu_clk);
129
130 name = find_supply_name(cpu_dev);
131 /* Platform doesn't require regulator */
132 if (!name)
133 return 0;
134
135 cpu_reg = regulator_get_optional(cpu_dev, name);
136 ret = PTR_ERR_OR_ZERO(cpu_reg);
137 if (ret) {
138 /*
139 * If cpu's regulator supply node is present, but regulator is
140 * not yet registered, we should try defering probe.
141 */
142 if (ret == -EPROBE_DEFER)
143 dev_dbg(cpu_dev, "cpu0 regulator not ready, retry\n");
144 else
145 dev_dbg(cpu_dev, "no regulator for cpu0: %d\n", ret);
146
147 return ret;
148 }
149
150 regulator_put(cpu_reg);
151 return 0;
152}
153
154static int cpufreq_init(struct cpufreq_policy *policy)
155{
156 struct em_data_callback em_cb = EM_DATA_CB(of_dev_pm_opp_get_cpu_power);
157 struct cpufreq_frequency_table *freq_table;
158 struct opp_table *opp_table = NULL;
159 struct private_data *priv;
160 struct device *cpu_dev;
161 struct clk *cpu_clk;
162 unsigned int transition_latency;
163 bool fallback = false;
164 const char *name;
165 int ret, nr_opp;
166
167 cpu_dev = get_cpu_device(policy->cpu);
168 if (!cpu_dev) {
169 pr_err("failed to get cpu%d device\n", policy->cpu);
170 return -ENODEV;
171 }
172
173 cpu_clk = clk_get(cpu_dev, NULL);
174 if (IS_ERR(cpu_clk)) {
175 ret = PTR_ERR(cpu_clk);
176 dev_err(cpu_dev, "%s: failed to get clk: %d\n", __func__, ret);
177 return ret;
178 }
179
180 /* Get OPP-sharing information from "operating-points-v2" bindings */
181 ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, policy->cpus);
182 if (ret) {
183 if (ret != -ENOENT)
184 goto out_put_clk;
185
186 /*
187 * operating-points-v2 not supported, fallback to old method of
188 * finding shared-OPPs for backward compatibility if the
189 * platform hasn't set sharing CPUs.
190 */
191 if (dev_pm_opp_get_sharing_cpus(cpu_dev, policy->cpus))
192 fallback = true;
193 }
194
195 /*
196 * OPP layer will be taking care of regulators now, but it needs to know
197 * the name of the regulator first.
198 */
199 name = find_supply_name(cpu_dev);
200 if (name) {
201 opp_table = dev_pm_opp_set_regulators(cpu_dev, &name, 1);
202 if (IS_ERR(opp_table)) {
203 ret = PTR_ERR(opp_table);
204 dev_err(cpu_dev, "Failed to set regulator for cpu%d: %d\n",
205 policy->cpu, ret);
206 goto out_put_clk;
207 }
208 }
209
210 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
211 if (!priv) {
212 ret = -ENOMEM;
213 goto out_put_regulator;
214 }
215
216 priv->reg_name = name;
217 priv->opp_table = opp_table;
218
219 /*
220 * Initialize OPP tables for all policy->cpus. They will be shared by
221 * all CPUs which have marked their CPUs shared with OPP bindings.
222 *
223 * For platforms not using operating-points-v2 bindings, we do this
224 * before updating policy->cpus. Otherwise, we will end up creating
225 * duplicate OPPs for policy->cpus.
226 *
227 * OPPs might be populated at runtime, don't check for error here
228 */
229 if (!dev_pm_opp_of_cpumask_add_table(policy->cpus))
230 priv->have_static_opps = true;
231
232 /*
233 * But we need OPP table to function so if it is not there let's
234 * give platform code chance to provide it for us.
235 */
236 ret = dev_pm_opp_get_opp_count(cpu_dev);
237 if (ret <= 0) {
238 dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
239 ret = -EPROBE_DEFER;
240 goto out_free_opp;
241 }
242 nr_opp = ret;
243
244 if (fallback) {
245 cpumask_setall(policy->cpus);
246
247 /*
248 * OPP tables are initialized only for policy->cpu, do it for
249 * others as well.
250 */
251 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
252 if (ret)
253 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
254 __func__, ret);
255 }
256
257 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
258 if (ret) {
259 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
260 goto out_free_opp;
261 }
262
263 priv->cpu_dev = cpu_dev;
264 policy->driver_data = priv;
265 policy->clk = cpu_clk;
266 policy->freq_table = freq_table;
267
268 policy->suspend_freq = dev_pm_opp_get_suspend_opp_freq(cpu_dev) / 1000;
269
270 /* Support turbo/boost mode */
271 if (policy_has_boost_freq(policy)) {
272 /* This gets disabled by core on driver unregister */
273 ret = cpufreq_enable_boost_support();
274 if (ret)
275 goto out_free_cpufreq_table;
276 cpufreq_dt_attr[1] = &cpufreq_freq_attr_scaling_boost_freqs;
277 }
278
279 transition_latency = dev_pm_opp_get_max_transition_latency(cpu_dev);
280 if (!transition_latency)
281 transition_latency = CPUFREQ_ETERNAL;
282
283 policy->cpuinfo.transition_latency = transition_latency;
284 policy->dvfs_possible_from_any_cpu = true;
285
286 em_register_perf_domain(policy->cpus, nr_opp, &em_cb);
287
288 return 0;
289
290out_free_cpufreq_table:
291 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
292out_free_opp:
293 if (priv->have_static_opps)
294 dev_pm_opp_of_cpumask_remove_table(policy->cpus);
295 kfree(priv);
296out_put_regulator:
297 if (name)
298 dev_pm_opp_put_regulators(opp_table);
299out_put_clk:
300 clk_put(cpu_clk);
301
302 return ret;
303}
304
305static int cpufreq_exit(struct cpufreq_policy *policy)
306{
307 struct private_data *priv = policy->driver_data;
308
309 cpufreq_cooling_unregister(priv->cdev);
310 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
311 if (priv->have_static_opps)
312 dev_pm_opp_of_cpumask_remove_table(policy->related_cpus);
313 if (priv->reg_name)
314 dev_pm_opp_put_regulators(priv->opp_table);
315
316 clk_put(policy->clk);
317 kfree(priv);
318
319 return 0;
320}
321
322static void cpufreq_ready(struct cpufreq_policy *policy)
323{
324 struct private_data *priv = policy->driver_data;
325
326 priv->cdev = of_cpufreq_cooling_register(policy);
327}
328
329static struct cpufreq_driver dt_cpufreq_driver = {
330 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
331 .verify = cpufreq_generic_frequency_table_verify,
332 .target_index = set_target,
333 .get = cpufreq_generic_get,
334 .init = cpufreq_init,
335 .exit = cpufreq_exit,
336 .ready = cpufreq_ready,
337 .name = "cpufreq-dt",
338 .attr = cpufreq_dt_attr,
339 .suspend = cpufreq_generic_suspend,
340};
341
342static int dt_cpufreq_probe(struct platform_device *pdev)
343{
344 struct cpufreq_dt_platform_data *data = dev_get_platdata(&pdev->dev);
345 int ret;
346
347 /*
348 * All per-cluster (CPUs sharing clock/voltages) initialization is done
349 * from ->init(). In probe(), we just need to make sure that clk and
350 * regulators are available. Else defer probe and retry.
351 *
352 * FIXME: Is checking this only for CPU0 sufficient ?
353 */
354 ret = resources_available();
355 if (ret)
356 return ret;
357
358 if (data) {
359 if (data->have_governor_per_policy)
360 dt_cpufreq_driver.flags |= CPUFREQ_HAVE_GOVERNOR_PER_POLICY;
361
362 dt_cpufreq_driver.resume = data->resume;
363 if (data->suspend)
364 dt_cpufreq_driver.suspend = data->suspend;
365 }
366
367 ret = cpufreq_register_driver(&dt_cpufreq_driver);
368 if (ret)
369 dev_err(&pdev->dev, "failed register driver: %d\n", ret);
370
371 return ret;
372}
373
374static int dt_cpufreq_remove(struct platform_device *pdev)
375{
376 cpufreq_unregister_driver(&dt_cpufreq_driver);
377 return 0;
378}
379
380static struct platform_driver dt_cpufreq_platdrv = {
381 .driver = {
382 .name = "cpufreq-dt",
383 },
384 .probe = dt_cpufreq_probe,
385 .remove = dt_cpufreq_remove,
386};
387module_platform_driver(dt_cpufreq_platdrv);
388
389MODULE_ALIAS("platform:cpufreq-dt");
390MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
391MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
392MODULE_DESCRIPTION("Generic cpufreq driver");
393MODULE_LICENSE("GPL");