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