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 #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 #ifdef CONFIG_ARCH_ROCKCHIP
27 #include "rockchip-cpufreq.h"
28 #endif
29
30 struct private_data {
31 struct list_head node;
32
33 cpumask_var_t cpus;
34 struct device *cpu_dev;
35 struct opp_table *opp_table;
36 struct cpufreq_frequency_table *freq_table;
37 bool have_static_opps;
38 };
39
40 static LIST_HEAD(priv_list);
41
42 static struct freq_attr *cpufreq_dt_attr[] = {
43 &cpufreq_freq_attr_scaling_available_freqs,
44 NULL, /* Extra space for boost-attr if required */
45 NULL,
46 };
47
cpufreq_dt_find_data(int cpu)48 static struct private_data *cpufreq_dt_find_data(int cpu)
49 {
50 struct private_data *priv;
51
52 list_for_each_entry(priv, &priv_list, node) {
53 if (cpumask_test_cpu(cpu, priv->cpus))
54 return priv;
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 * 1000);
67 #else
68 return dev_pm_opp_set_rate(priv->cpu_dev, freq * 1000);
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
85 /* This must be valid for sure */
86 if (WARN_ON(!np))
87 return NULL;
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 cpumask_copy(policy->cpus, priv->cpus);
137 policy->driver_data = priv;
138 policy->clk = cpu_clk;
139 policy->freq_table = priv->freq_table;
140 policy->suspend_freq = dev_pm_opp_get_suspend_opp_freq(cpu_dev) / 1000;
141 policy->cpuinfo.transition_latency = transition_latency;
142 policy->dvfs_possible_from_any_cpu = true;
143
144 /* Support turbo/boost mode */
145 if (policy_has_boost_freq(policy)) {
146 /* This gets disabled by core on driver unregister */
147 ret = cpufreq_enable_boost_support();
148 if (ret)
149 goto out_clk_put;
150 cpufreq_dt_attr[1] = &cpufreq_freq_attr_scaling_boost_freqs;
151 }
152
153 dev_pm_opp_of_register_em(cpu_dev, policy->cpus);
154
155 return 0;
156
157 out_clk_put:
158 clk_put(cpu_clk);
159
160 return ret;
161 }
162
cpufreq_online(struct cpufreq_policy * policy)163 static int cpufreq_online(struct cpufreq_policy *policy)
164 {
165 /* We did light-weight tear down earlier, nothing to do here */
166 return 0;
167 }
168
cpufreq_offline(struct cpufreq_policy * policy)169 static int cpufreq_offline(struct cpufreq_policy *policy)
170 {
171 /*
172 * Preserve policy->driver_data and don't free resources on light-weight
173 * tear down.
174 */
175 return 0;
176 }
177
cpufreq_exit(struct cpufreq_policy * policy)178 static int cpufreq_exit(struct cpufreq_policy *policy)
179 {
180 clk_put(policy->clk);
181 return 0;
182 }
183
184 static struct cpufreq_driver dt_cpufreq_driver = {
185 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK |
186 CPUFREQ_IS_COOLING_DEV,
187 .verify = cpufreq_generic_frequency_table_verify,
188 .target_index = set_target,
189 .get = cpufreq_generic_get,
190 .init = cpufreq_init,
191 .exit = cpufreq_exit,
192 .online = cpufreq_online,
193 .offline = cpufreq_offline,
194 .name = "cpufreq-dt",
195 .attr = cpufreq_dt_attr,
196 .suspend = cpufreq_generic_suspend,
197 };
198
dt_cpufreq_early_init(struct device * dev,int cpu)199 static int dt_cpufreq_early_init(struct device *dev, int cpu)
200 {
201 struct private_data *priv;
202 struct device *cpu_dev;
203 bool fallback = false;
204 const char *reg_name;
205 int ret;
206
207 /* Check if this CPU is already covered by some other policy */
208 if (cpufreq_dt_find_data(cpu))
209 return 0;
210
211 cpu_dev = get_cpu_device(cpu);
212 if (!cpu_dev)
213 return -EPROBE_DEFER;
214
215 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
216 if (!priv)
217 return -ENOMEM;
218
219 if (!alloc_cpumask_var(&priv->cpus, GFP_KERNEL))
220 return -ENOMEM;
221
222 cpumask_set_cpu(cpu, priv->cpus);
223 priv->cpu_dev = cpu_dev;
224
225 /*
226 * OPP layer will be taking care of regulators now, but it needs to know
227 * the name of the regulator first.
228 */
229 reg_name = find_supply_name(cpu_dev);
230 if (reg_name) {
231 priv->opp_table = dev_pm_opp_set_regulators(cpu_dev, ®_name,
232 1);
233 if (IS_ERR(priv->opp_table)) {
234 ret = PTR_ERR(priv->opp_table);
235 if (ret != -EPROBE_DEFER)
236 dev_err(cpu_dev, "failed to set regulators: %d\n",
237 ret);
238 goto free_cpumask;
239 }
240 }
241
242 /* Get OPP-sharing information from "operating-points-v2" bindings */
243 ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->cpus);
244 if (ret) {
245 if (ret != -ENOENT)
246 goto out;
247
248 /*
249 * operating-points-v2 not supported, fallback to all CPUs share
250 * OPP for backward compatibility if the platform hasn't set
251 * sharing CPUs.
252 */
253 if (dev_pm_opp_get_sharing_cpus(cpu_dev, priv->cpus))
254 fallback = true;
255 }
256
257 /*
258 * Initialize OPP tables for all priv->cpus. They will be shared by
259 * all CPUs which have marked their CPUs shared with OPP bindings.
260 *
261 * For platforms not using operating-points-v2 bindings, we do this
262 * before updating priv->cpus. Otherwise, we will end up creating
263 * duplicate OPPs for the CPUs.
264 *
265 * OPPs might be populated at runtime, don't check for error here.
266 */
267 if (!dev_pm_opp_of_cpumask_add_table(priv->cpus))
268 priv->have_static_opps = true;
269
270 /*
271 * The OPP table must be initialized, statically or dynamically, by this
272 * point.
273 */
274 ret = dev_pm_opp_get_opp_count(cpu_dev);
275 if (ret <= 0) {
276 dev_err(cpu_dev, "OPP table can't be empty\n");
277 ret = -ENODEV;
278 goto out;
279 }
280
281 if (fallback) {
282 cpumask_setall(priv->cpus);
283 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, priv->cpus);
284 if (ret)
285 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
286 __func__, ret);
287 }
288
289 #ifdef CONFIG_ARCH_ROCKCHIP
290 rockchip_cpufreq_adjust_power_scale(cpu_dev);
291 #endif
292
293 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &priv->freq_table);
294 if (ret) {
295 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
296 goto out;
297 }
298
299 list_add(&priv->node, &priv_list);
300 return 0;
301
302 out:
303 if (priv->have_static_opps)
304 dev_pm_opp_of_cpumask_remove_table(priv->cpus);
305 if (priv->opp_table)
306 dev_pm_opp_put_regulators(priv->opp_table);
307 free_cpumask:
308 free_cpumask_var(priv->cpus);
309 return ret;
310 }
311
dt_cpufreq_release(void)312 static void dt_cpufreq_release(void)
313 {
314 struct private_data *priv, *tmp;
315
316 list_for_each_entry_safe(priv, tmp, &priv_list, node) {
317 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &priv->freq_table);
318 if (priv->have_static_opps)
319 dev_pm_opp_of_cpumask_remove_table(priv->cpus);
320 if (priv->opp_table)
321 dev_pm_opp_put_regulators(priv->opp_table);
322 free_cpumask_var(priv->cpus);
323 list_del(&priv->node);
324 }
325 }
326
dt_cpufreq_probe(struct platform_device * pdev)327 static int dt_cpufreq_probe(struct platform_device *pdev)
328 {
329 struct cpufreq_dt_platform_data *data = dev_get_platdata(&pdev->dev);
330 int ret, cpu;
331
332 /* Request resources early so we can return in case of -EPROBE_DEFER */
333 for_each_possible_cpu(cpu) {
334 ret = dt_cpufreq_early_init(&pdev->dev, cpu);
335 if (ret)
336 goto err;
337 }
338
339 if (data) {
340 if (data->have_governor_per_policy)
341 dt_cpufreq_driver.flags |= CPUFREQ_HAVE_GOVERNOR_PER_POLICY;
342
343 dt_cpufreq_driver.resume = data->resume;
344 if (data->suspend)
345 dt_cpufreq_driver.suspend = data->suspend;
346 if (data->get_intermediate) {
347 dt_cpufreq_driver.target_intermediate = data->target_intermediate;
348 dt_cpufreq_driver.get_intermediate = data->get_intermediate;
349 }
350 }
351
352 ret = cpufreq_register_driver(&dt_cpufreq_driver);
353 if (ret) {
354 dev_err(&pdev->dev, "failed register driver: %d\n", ret);
355 goto err;
356 }
357
358 return 0;
359 err:
360 dt_cpufreq_release();
361 return ret;
362 }
363
dt_cpufreq_remove(struct platform_device * pdev)364 static int dt_cpufreq_remove(struct platform_device *pdev)
365 {
366 cpufreq_unregister_driver(&dt_cpufreq_driver);
367 dt_cpufreq_release();
368 return 0;
369 }
370
371 static struct platform_driver dt_cpufreq_platdrv = {
372 .driver = {
373 .name = "cpufreq-dt",
374 },
375 .probe = dt_cpufreq_probe,
376 .remove = dt_cpufreq_remove,
377 };
378 module_platform_driver(dt_cpufreq_platdrv);
379
380 MODULE_ALIAS("platform:cpufreq-dt");
381 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
382 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
383 MODULE_DESCRIPTION("Generic cpufreq driver");
384 MODULE_LICENSE("GPL");
385