treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / cpufreq / qoriq-cpufreq.c
blob8e436dc75c8bc84b4cd8011f6062671f3a8ca3a5
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2013 Freescale Semiconductor, Inc.
5 * CPU Frequency Scaling driver for Freescale QorIQ SoCs.
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/cpufreq.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/of.h>
19 #include <linux/slab.h>
20 #include <linux/smp.h>
22 /**
23 * struct cpu_data
24 * @pclk: the parent clock of cpu
25 * @table: frequency table
27 struct cpu_data {
28 struct clk **pclk;
29 struct cpufreq_frequency_table *table;
33 * Don't use cpufreq on this SoC -- used when the SoC would have otherwise
34 * matched a more generic compatible.
36 #define SOC_BLACKLIST 1
38 /**
39 * struct soc_data - SoC specific data
40 * @flags: SOC_xxx
42 struct soc_data {
43 u32 flags;
46 static u32 get_bus_freq(void)
48 struct device_node *soc;
49 u32 sysfreq;
50 struct clk *pltclk;
51 int ret;
53 /* get platform freq by searching bus-frequency property */
54 soc = of_find_node_by_type(NULL, "soc");
55 if (soc) {
56 ret = of_property_read_u32(soc, "bus-frequency", &sysfreq);
57 of_node_put(soc);
58 if (!ret)
59 return sysfreq;
62 /* get platform freq by its clock name */
63 pltclk = clk_get(NULL, "cg-pll0-div1");
64 if (IS_ERR(pltclk)) {
65 pr_err("%s: can't get bus frequency %ld\n",
66 __func__, PTR_ERR(pltclk));
67 return PTR_ERR(pltclk);
70 return clk_get_rate(pltclk);
73 static struct clk *cpu_to_clk(int cpu)
75 struct device_node *np;
76 struct clk *clk;
78 if (!cpu_present(cpu))
79 return NULL;
81 np = of_get_cpu_node(cpu, NULL);
82 if (!np)
83 return NULL;
85 clk = of_clk_get(np, 0);
86 of_node_put(np);
87 return clk;
90 /* traverse cpu nodes to get cpu mask of sharing clock wire */
91 static void set_affected_cpus(struct cpufreq_policy *policy)
93 struct cpumask *dstp = policy->cpus;
94 struct clk *clk;
95 int i;
97 for_each_present_cpu(i) {
98 clk = cpu_to_clk(i);
99 if (IS_ERR(clk)) {
100 pr_err("%s: no clock for cpu %d\n", __func__, i);
101 continue;
104 if (clk_is_match(policy->clk, clk))
105 cpumask_set_cpu(i, dstp);
109 /* reduce the duplicated frequencies in frequency table */
110 static void freq_table_redup(struct cpufreq_frequency_table *freq_table,
111 int count)
113 int i, j;
115 for (i = 1; i < count; i++) {
116 for (j = 0; j < i; j++) {
117 if (freq_table[j].frequency == CPUFREQ_ENTRY_INVALID ||
118 freq_table[j].frequency !=
119 freq_table[i].frequency)
120 continue;
122 freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
123 break;
128 /* sort the frequencies in frequency table in descenting order */
129 static void freq_table_sort(struct cpufreq_frequency_table *freq_table,
130 int count)
132 int i, j, ind;
133 unsigned int freq, max_freq;
134 struct cpufreq_frequency_table table;
136 for (i = 0; i < count - 1; i++) {
137 max_freq = freq_table[i].frequency;
138 ind = i;
139 for (j = i + 1; j < count; j++) {
140 freq = freq_table[j].frequency;
141 if (freq == CPUFREQ_ENTRY_INVALID ||
142 freq <= max_freq)
143 continue;
144 ind = j;
145 max_freq = freq;
148 if (ind != i) {
149 /* exchange the frequencies */
150 table.driver_data = freq_table[i].driver_data;
151 table.frequency = freq_table[i].frequency;
152 freq_table[i].driver_data = freq_table[ind].driver_data;
153 freq_table[i].frequency = freq_table[ind].frequency;
154 freq_table[ind].driver_data = table.driver_data;
155 freq_table[ind].frequency = table.frequency;
160 static int qoriq_cpufreq_cpu_init(struct cpufreq_policy *policy)
162 struct device_node *np;
163 int i, count;
164 u32 freq;
165 struct clk *clk;
166 const struct clk_hw *hwclk;
167 struct cpufreq_frequency_table *table;
168 struct cpu_data *data;
169 unsigned int cpu = policy->cpu;
170 u64 u64temp;
172 np = of_get_cpu_node(cpu, NULL);
173 if (!np)
174 return -ENODEV;
176 data = kzalloc(sizeof(*data), GFP_KERNEL);
177 if (!data)
178 goto err_np;
180 policy->clk = of_clk_get(np, 0);
181 if (IS_ERR(policy->clk)) {
182 pr_err("%s: no clock information\n", __func__);
183 goto err_nomem2;
186 hwclk = __clk_get_hw(policy->clk);
187 count = clk_hw_get_num_parents(hwclk);
189 data->pclk = kcalloc(count, sizeof(struct clk *), GFP_KERNEL);
190 if (!data->pclk)
191 goto err_nomem2;
193 table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
194 if (!table)
195 goto err_pclk;
197 for (i = 0; i < count; i++) {
198 clk = clk_hw_get_parent_by_index(hwclk, i)->clk;
199 data->pclk[i] = clk;
200 freq = clk_get_rate(clk);
201 table[i].frequency = freq / 1000;
202 table[i].driver_data = i;
204 freq_table_redup(table, count);
205 freq_table_sort(table, count);
206 table[i].frequency = CPUFREQ_TABLE_END;
207 policy->freq_table = table;
208 data->table = table;
210 /* update ->cpus if we have cluster, no harm if not */
211 set_affected_cpus(policy);
212 policy->driver_data = data;
214 /* Minimum transition latency is 12 platform clocks */
215 u64temp = 12ULL * NSEC_PER_SEC;
216 do_div(u64temp, get_bus_freq());
217 policy->cpuinfo.transition_latency = u64temp + 1;
219 of_node_put(np);
221 return 0;
223 err_pclk:
224 kfree(data->pclk);
225 err_nomem2:
226 kfree(data);
227 err_np:
228 of_node_put(np);
230 return -ENODEV;
233 static int qoriq_cpufreq_cpu_exit(struct cpufreq_policy *policy)
235 struct cpu_data *data = policy->driver_data;
237 kfree(data->pclk);
238 kfree(data->table);
239 kfree(data);
240 policy->driver_data = NULL;
242 return 0;
245 static int qoriq_cpufreq_target(struct cpufreq_policy *policy,
246 unsigned int index)
248 struct clk *parent;
249 struct cpu_data *data = policy->driver_data;
251 parent = data->pclk[data->table[index].driver_data];
252 return clk_set_parent(policy->clk, parent);
255 static struct cpufreq_driver qoriq_cpufreq_driver = {
256 .name = "qoriq_cpufreq",
257 .flags = CPUFREQ_CONST_LOOPS |
258 CPUFREQ_IS_COOLING_DEV,
259 .init = qoriq_cpufreq_cpu_init,
260 .exit = qoriq_cpufreq_cpu_exit,
261 .verify = cpufreq_generic_frequency_table_verify,
262 .target_index = qoriq_cpufreq_target,
263 .get = cpufreq_generic_get,
264 .attr = cpufreq_generic_attr,
267 static const struct soc_data blacklist = {
268 .flags = SOC_BLACKLIST,
271 static const struct of_device_id node_matches[] __initconst = {
272 /* e6500 cannot use cpufreq due to erratum A-008083 */
273 { .compatible = "fsl,b4420-clockgen", &blacklist },
274 { .compatible = "fsl,b4860-clockgen", &blacklist },
275 { .compatible = "fsl,t2080-clockgen", &blacklist },
276 { .compatible = "fsl,t4240-clockgen", &blacklist },
278 { .compatible = "fsl,ls1012a-clockgen", },
279 { .compatible = "fsl,ls1021a-clockgen", },
280 { .compatible = "fsl,ls1028a-clockgen", },
281 { .compatible = "fsl,ls1043a-clockgen", },
282 { .compatible = "fsl,ls1046a-clockgen", },
283 { .compatible = "fsl,ls1088a-clockgen", },
284 { .compatible = "fsl,ls2080a-clockgen", },
285 { .compatible = "fsl,lx2160a-clockgen", },
286 { .compatible = "fsl,p4080-clockgen", },
287 { .compatible = "fsl,qoriq-clockgen-1.0", },
288 { .compatible = "fsl,qoriq-clockgen-2.0", },
292 static int __init qoriq_cpufreq_init(void)
294 int ret;
295 struct device_node *np;
296 const struct of_device_id *match;
297 const struct soc_data *data;
299 np = of_find_matching_node(NULL, node_matches);
300 if (!np)
301 return -ENODEV;
303 match = of_match_node(node_matches, np);
304 data = match->data;
306 of_node_put(np);
308 if (data && data->flags & SOC_BLACKLIST)
309 return -ENODEV;
311 ret = cpufreq_register_driver(&qoriq_cpufreq_driver);
312 if (!ret)
313 pr_info("Freescale QorIQ CPU frequency scaling driver\n");
315 return ret;
317 module_init(qoriq_cpufreq_init);
319 static void __exit qoriq_cpufreq_exit(void)
321 cpufreq_unregister_driver(&qoriq_cpufreq_driver);
323 module_exit(qoriq_cpufreq_exit);
325 MODULE_LICENSE("GPL");
326 MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
327 MODULE_DESCRIPTION("cpufreq driver for Freescale QorIQ series SoCs");