]> Git Repo - linux.git/blob - drivers/cpufreq/cpufreq-dt.c
drm/vc4: Run DRM default client setup
[linux.git] / drivers / cpufreq / cpufreq-dt.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Freescale Semiconductor, Inc.
4  *
5  * Copyright (C) 2014 Linaro.
6  * Viresh Kumar <[email protected]>
7  */
8
9 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
10
11 #include <linux/clk.h>
12 #include <linux/cpu.h>
13 #include <linux/cpufreq.h>
14 #include <linux/cpumask.h>
15 #include <linux/err.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/pm_opp.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <linux/thermal.h>
24
25 #include "cpufreq-dt.h"
26
27 struct private_data {
28         struct list_head node;
29
30         cpumask_var_t cpus;
31         struct device *cpu_dev;
32         struct cpufreq_frequency_table *freq_table;
33         bool have_static_opps;
34         int opp_token;
35 };
36
37 static LIST_HEAD(priv_list);
38
39 static 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
45 static struct private_data *cpufreq_dt_find_data(int cpu)
46 {
47         struct private_data *priv;
48
49         list_for_each_entry(priv, &priv_list, node) {
50                 if (cpumask_test_cpu(cpu, priv->cpus))
51                         return priv;
52         }
53
54         return NULL;
55 }
56
57 static int set_target(struct cpufreq_policy *policy, unsigned int index)
58 {
59         struct private_data *priv = policy->driver_data;
60         unsigned long freq = policy->freq_table[index].frequency;
61
62         return dev_pm_opp_set_rate(priv->cpu_dev, freq * 1000);
63 }
64
65 /*
66  * An earlier version of opp-v1 bindings used to name the regulator
67  * "cpu0-supply", we still need to handle that for backwards compatibility.
68  */
69 static const char *find_supply_name(struct device *dev)
70 {
71         struct device_node *np __free(device_node) = of_node_get(dev->of_node);
72         struct property *pp;
73         int cpu = dev->id;
74
75         /* This must be valid for sure */
76         if (WARN_ON(!np))
77                 return NULL;
78
79         /* Try "cpu0" for older DTs */
80         if (!cpu) {
81                 pp = of_find_property(np, "cpu0-supply", NULL);
82                 if (pp)
83                         return "cpu0";
84         }
85
86         pp = of_find_property(np, "cpu-supply", NULL);
87         if (pp)
88                 return "cpu";
89
90         dev_dbg(dev, "no regulator for cpu%d\n", cpu);
91         return NULL;
92 }
93
94 static int cpufreq_init(struct cpufreq_policy *policy)
95 {
96         struct private_data *priv;
97         struct device *cpu_dev;
98         struct clk *cpu_clk;
99         unsigned int transition_latency;
100         int ret;
101
102         priv = cpufreq_dt_find_data(policy->cpu);
103         if (!priv) {
104                 pr_err("failed to find data for cpu%d\n", policy->cpu);
105                 return -ENODEV;
106         }
107         cpu_dev = priv->cpu_dev;
108
109         cpu_clk = clk_get(cpu_dev, NULL);
110         if (IS_ERR(cpu_clk)) {
111                 ret = PTR_ERR(cpu_clk);
112                 dev_err(cpu_dev, "%s: failed to get clk: %d\n", __func__, ret);
113                 return ret;
114         }
115
116         transition_latency = dev_pm_opp_get_max_transition_latency(cpu_dev);
117         if (!transition_latency)
118                 transition_latency = CPUFREQ_ETERNAL;
119
120         cpumask_copy(policy->cpus, priv->cpus);
121         policy->driver_data = priv;
122         policy->clk = cpu_clk;
123         policy->freq_table = priv->freq_table;
124         policy->suspend_freq = dev_pm_opp_get_suspend_opp_freq(cpu_dev) / 1000;
125         policy->cpuinfo.transition_latency = transition_latency;
126         policy->dvfs_possible_from_any_cpu = true;
127
128         /* Support turbo/boost mode */
129         if (policy_has_boost_freq(policy)) {
130                 /* This gets disabled by core on driver unregister */
131                 ret = cpufreq_enable_boost_support();
132                 if (ret)
133                         goto out_clk_put;
134                 cpufreq_dt_attr[1] = &cpufreq_freq_attr_scaling_boost_freqs;
135         }
136
137         return 0;
138
139 out_clk_put:
140         clk_put(cpu_clk);
141
142         return ret;
143 }
144
145 static int cpufreq_online(struct cpufreq_policy *policy)
146 {
147         /* We did light-weight tear down earlier, nothing to do here */
148         return 0;
149 }
150
151 static int cpufreq_offline(struct cpufreq_policy *policy)
152 {
153         /*
154          * Preserve policy->driver_data and don't free resources on light-weight
155          * tear down.
156          */
157         return 0;
158 }
159
160 static void cpufreq_exit(struct cpufreq_policy *policy)
161 {
162         clk_put(policy->clk);
163 }
164
165 static struct cpufreq_driver dt_cpufreq_driver = {
166         .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK |
167                  CPUFREQ_IS_COOLING_DEV,
168         .verify = cpufreq_generic_frequency_table_verify,
169         .target_index = set_target,
170         .get = cpufreq_generic_get,
171         .init = cpufreq_init,
172         .exit = cpufreq_exit,
173         .online = cpufreq_online,
174         .offline = cpufreq_offline,
175         .register_em = cpufreq_register_em_with_opp,
176         .name = "cpufreq-dt",
177         .attr = cpufreq_dt_attr,
178         .suspend = cpufreq_generic_suspend,
179 };
180
181 static int dt_cpufreq_early_init(struct device *dev, int cpu)
182 {
183         struct private_data *priv;
184         struct device *cpu_dev;
185         bool fallback = false;
186         const char *reg_name[] = { NULL, NULL };
187         int ret;
188
189         /* Check if this CPU is already covered by some other policy */
190         if (cpufreq_dt_find_data(cpu))
191                 return 0;
192
193         cpu_dev = get_cpu_device(cpu);
194         if (!cpu_dev)
195                 return -EPROBE_DEFER;
196
197         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
198         if (!priv)
199                 return -ENOMEM;
200
201         if (!zalloc_cpumask_var(&priv->cpus, GFP_KERNEL))
202                 return -ENOMEM;
203
204         cpumask_set_cpu(cpu, priv->cpus);
205         priv->cpu_dev = cpu_dev;
206
207         /*
208          * OPP layer will be taking care of regulators now, but it needs to know
209          * the name of the regulator first.
210          */
211         reg_name[0] = find_supply_name(cpu_dev);
212         if (reg_name[0]) {
213                 priv->opp_token = dev_pm_opp_set_regulators(cpu_dev, reg_name);
214                 if (priv->opp_token < 0) {
215                         ret = dev_err_probe(cpu_dev, priv->opp_token,
216                                             "failed to set regulators\n");
217                         goto free_cpumask;
218                 }
219         }
220
221         /* Get OPP-sharing information from "operating-points-v2" bindings */
222         ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->cpus);
223         if (ret) {
224                 if (ret != -ENOENT)
225                         goto out;
226
227                 /*
228                  * operating-points-v2 not supported, fallback to all CPUs share
229                  * OPP for backward compatibility if the platform hasn't set
230                  * sharing CPUs.
231                  */
232                 if (dev_pm_opp_get_sharing_cpus(cpu_dev, priv->cpus))
233                         fallback = true;
234         }
235
236         /*
237          * Initialize OPP tables for all priv->cpus. They will be shared by
238          * all CPUs which have marked their CPUs shared with OPP bindings.
239          *
240          * For platforms not using operating-points-v2 bindings, we do this
241          * before updating priv->cpus. Otherwise, we will end up creating
242          * duplicate OPPs for the CPUs.
243          *
244          * OPPs might be populated at runtime, don't fail for error here unless
245          * it is -EPROBE_DEFER.
246          */
247         ret = dev_pm_opp_of_cpumask_add_table(priv->cpus);
248         if (!ret) {
249                 priv->have_static_opps = true;
250         } else if (ret == -EPROBE_DEFER) {
251                 goto out;
252         }
253
254         /*
255          * The OPP table must be initialized, statically or dynamically, by this
256          * point.
257          */
258         ret = dev_pm_opp_get_opp_count(cpu_dev);
259         if (ret <= 0) {
260                 dev_err(cpu_dev, "OPP table can't be empty\n");
261                 ret = -ENODEV;
262                 goto out;
263         }
264
265         if (fallback) {
266                 cpumask_setall(priv->cpus);
267                 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, priv->cpus);
268                 if (ret)
269                         dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
270                                 __func__, ret);
271         }
272
273         ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &priv->freq_table);
274         if (ret) {
275                 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
276                 goto out;
277         }
278
279         list_add(&priv->node, &priv_list);
280         return 0;
281
282 out:
283         if (priv->have_static_opps)
284                 dev_pm_opp_of_cpumask_remove_table(priv->cpus);
285         dev_pm_opp_put_regulators(priv->opp_token);
286 free_cpumask:
287         free_cpumask_var(priv->cpus);
288         return ret;
289 }
290
291 static void dt_cpufreq_release(void)
292 {
293         struct private_data *priv, *tmp;
294
295         list_for_each_entry_safe(priv, tmp, &priv_list, node) {
296                 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &priv->freq_table);
297                 if (priv->have_static_opps)
298                         dev_pm_opp_of_cpumask_remove_table(priv->cpus);
299                 dev_pm_opp_put_regulators(priv->opp_token);
300                 free_cpumask_var(priv->cpus);
301                 list_del(&priv->node);
302         }
303 }
304
305 static int dt_cpufreq_probe(struct platform_device *pdev)
306 {
307         struct cpufreq_dt_platform_data *data = dev_get_platdata(&pdev->dev);
308         int ret, cpu;
309
310         /* Request resources early so we can return in case of -EPROBE_DEFER */
311         for_each_possible_cpu(cpu) {
312                 ret = dt_cpufreq_early_init(&pdev->dev, cpu);
313                 if (ret)
314                         goto err;
315         }
316
317         if (data) {
318                 if (data->have_governor_per_policy)
319                         dt_cpufreq_driver.flags |= CPUFREQ_HAVE_GOVERNOR_PER_POLICY;
320
321                 dt_cpufreq_driver.resume = data->resume;
322                 if (data->suspend)
323                         dt_cpufreq_driver.suspend = data->suspend;
324                 if (data->get_intermediate) {
325                         dt_cpufreq_driver.target_intermediate = data->target_intermediate;
326                         dt_cpufreq_driver.get_intermediate = data->get_intermediate;
327                 }
328         }
329
330         ret = cpufreq_register_driver(&dt_cpufreq_driver);
331         if (ret) {
332                 dev_err(&pdev->dev, "failed register driver: %d\n", ret);
333                 goto err;
334         }
335
336         return 0;
337 err:
338         dt_cpufreq_release();
339         return ret;
340 }
341
342 static void dt_cpufreq_remove(struct platform_device *pdev)
343 {
344         cpufreq_unregister_driver(&dt_cpufreq_driver);
345         dt_cpufreq_release();
346 }
347
348 static struct platform_driver dt_cpufreq_platdrv = {
349         .driver = {
350                 .name   = "cpufreq-dt",
351         },
352         .probe          = dt_cpufreq_probe,
353         .remove_new     = dt_cpufreq_remove,
354 };
355 module_platform_driver(dt_cpufreq_platdrv);
356
357 MODULE_ALIAS("platform:cpufreq-dt");
358 MODULE_AUTHOR("Viresh Kumar <[email protected]>");
359 MODULE_AUTHOR("Shawn Guo <[email protected]>");
360 MODULE_DESCRIPTION("Generic cpufreq driver");
361 MODULE_LICENSE("GPL");
This page took 0.0506 seconds and 4 git commands to generate.