File Index
Files and directories
Sign in
FILE

cpufreq-dt.c

File preview with syntax highlighting.

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