cpufreq: remove unused notifier: CPUFREQ_{SUSPENDCHANGE|RESUMECHANGE}
[deliverable/linux.git] / drivers / cpufreq / ppc-corenet-cpufreq.c
CommitLineData
defa4c73
TY
1/*
2 * Copyright 2013 Freescale Semiconductor, Inc.
3 *
4 * CPU Frequency Scaling driver for Freescale PowerPC corenet SoCs.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/clk.h>
14#include <linux/cpufreq.h>
15#include <linux/errno.h>
16#include <sysdev/fsl_soc.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/mutex.h>
21#include <linux/of.h>
22#include <linux/slab.h>
23#include <linux/smp.h>
24
25/**
26 * struct cpu_data - per CPU data struct
defa4c73
TY
27 * @parent: the parent node of cpu clock
28 * @table: frequency table
29 */
30struct cpu_data {
defa4c73
TY
31 struct device_node *parent;
32 struct cpufreq_frequency_table *table;
33};
34
35/**
36 * struct soc_data - SoC specific data
37 * @freq_mask: mask the disallowed frequencies
38 * @flag: unique flags
39 */
40struct soc_data {
41 u32 freq_mask[4];
42 u32 flag;
43};
44
45#define FREQ_MASK 1
46/* see hardware specification for the allowed frqeuencies */
47static const struct soc_data sdata[] = {
48 { /* used by p2041 and p3041 */
49 .freq_mask = {0x8, 0x8, 0x2, 0x2},
50 .flag = FREQ_MASK,
51 },
52 { /* used by p5020 */
53 .freq_mask = {0x8, 0x2},
54 .flag = FREQ_MASK,
55 },
56 { /* used by p4080, p5040 */
57 .freq_mask = {0},
58 .flag = 0,
59 },
60};
61
62/*
63 * the minimum allowed core frequency, in Hz
64 * for chassis v1.0, >= platform frequency
65 * for chassis v2.0, >= platform frequency / 2
66 */
67static u32 min_cpufreq;
68static const u32 *fmask;
69
defa4c73
TY
70static DEFINE_PER_CPU(struct cpu_data *, cpu_data);
71
72/* cpumask in a cluster */
73static DEFINE_PER_CPU(cpumask_var_t, cpu_mask);
74
75#ifndef CONFIG_SMP
76static inline const struct cpumask *cpu_core_mask(int cpu)
77{
78 return cpumask_of(0);
79}
80#endif
81
defa4c73
TY
82/* reduce the duplicated frequencies in frequency table */
83static void freq_table_redup(struct cpufreq_frequency_table *freq_table,
84 int count)
85{
86 int i, j;
87
88 for (i = 1; i < count; i++) {
89 for (j = 0; j < i; j++) {
90 if (freq_table[j].frequency == CPUFREQ_ENTRY_INVALID ||
91 freq_table[j].frequency !=
92 freq_table[i].frequency)
93 continue;
94
95 freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
96 break;
97 }
98 }
99}
100
101/* sort the frequencies in frequency table in descenting order */
102static void freq_table_sort(struct cpufreq_frequency_table *freq_table,
103 int count)
104{
105 int i, j, ind;
106 unsigned int freq, max_freq;
107 struct cpufreq_frequency_table table;
108 for (i = 0; i < count - 1; i++) {
109 max_freq = freq_table[i].frequency;
110 ind = i;
111 for (j = i + 1; j < count; j++) {
112 freq = freq_table[j].frequency;
113 if (freq == CPUFREQ_ENTRY_INVALID ||
114 freq <= max_freq)
115 continue;
116 ind = j;
117 max_freq = freq;
118 }
119
120 if (ind != i) {
121 /* exchange the frequencies */
122 table.driver_data = freq_table[i].driver_data;
123 table.frequency = freq_table[i].frequency;
124 freq_table[i].driver_data = freq_table[ind].driver_data;
125 freq_table[i].frequency = freq_table[ind].frequency;
126 freq_table[ind].driver_data = table.driver_data;
127 freq_table[ind].frequency = table.frequency;
128 }
129 }
130}
131
132static int corenet_cpufreq_cpu_init(struct cpufreq_policy *policy)
133{
134 struct device_node *np;
135 int i, count, ret;
136 u32 freq, mask;
137 struct clk *clk;
138 struct cpufreq_frequency_table *table;
139 struct cpu_data *data;
140 unsigned int cpu = policy->cpu;
141
142 np = of_get_cpu_node(cpu, NULL);
143 if (!np)
144 return -ENODEV;
145
146 data = kzalloc(sizeof(*data), GFP_KERNEL);
147 if (!data) {
148 pr_err("%s: no memory\n", __func__);
149 goto err_np;
150 }
151
652ed95d
VK
152 policy->clk = of_clk_get(np, 0);
153 if (IS_ERR(policy->clk)) {
defa4c73
TY
154 pr_err("%s: no clock information\n", __func__);
155 goto err_nomem2;
156 }
157
158 data->parent = of_parse_phandle(np, "clocks", 0);
159 if (!data->parent) {
160 pr_err("%s: could not get clock information\n", __func__);
161 goto err_nomem2;
162 }
163
164 count = of_property_count_strings(data->parent, "clock-names");
165 table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
166 if (!table) {
167 pr_err("%s: no memory\n", __func__);
168 goto err_node;
169 }
170
171 if (fmask)
172 mask = fmask[get_hard_smp_processor_id(cpu)];
173 else
174 mask = 0x0;
175
176 for (i = 0; i < count; i++) {
177 clk = of_clk_get(data->parent, i);
178 freq = clk_get_rate(clk);
179 /*
180 * the clock is valid if its frequency is not masked
181 * and large than minimum allowed frequency.
182 */
183 if (freq < min_cpufreq || (mask & (1 << i)))
184 table[i].frequency = CPUFREQ_ENTRY_INVALID;
185 else
186 table[i].frequency = freq / 1000;
187 table[i].driver_data = i;
188 }
189 freq_table_redup(table, count);
190 freq_table_sort(table, count);
191 table[i].frequency = CPUFREQ_TABLE_END;
192
193 /* set the min and max frequency properly */
6b4147db 194 ret = cpufreq_table_validate_and_show(policy, table);
defa4c73
TY
195 if (ret) {
196 pr_err("invalid frequency table: %d\n", ret);
197 goto err_nomem1;
198 }
199
200 data->table = table;
201 per_cpu(cpu_data, cpu) = data;
202
203 /* update ->cpus if we have cluster, no harm if not */
204 cpumask_copy(policy->cpus, per_cpu(cpu_mask, cpu));
205 for_each_cpu(i, per_cpu(cpu_mask, cpu))
206 per_cpu(cpu_data, i) = data;
207
208 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
defa4c73
TY
209 of_node_put(np);
210
211 return 0;
212
213err_nomem1:
214 kfree(table);
215err_node:
216 of_node_put(data->parent);
217err_nomem2:
218 per_cpu(cpu_data, cpu) = NULL;
219 kfree(data);
220err_np:
221 of_node_put(np);
222
223 return -ENODEV;
224}
225
226static int __exit corenet_cpufreq_cpu_exit(struct cpufreq_policy *policy)
227{
228 struct cpu_data *data = per_cpu(cpu_data, policy->cpu);
229 unsigned int cpu;
230
defa4c73
TY
231 of_node_put(data->parent);
232 kfree(data->table);
233 kfree(data);
234
235 for_each_cpu(cpu, per_cpu(cpu_mask, policy->cpu))
236 per_cpu(cpu_data, cpu) = NULL;
237
238 return 0;
239}
240
defa4c73 241static int corenet_cpufreq_target(struct cpufreq_policy *policy,
9c0ebcf7 242 unsigned int index)
defa4c73 243{
defa4c73 244 struct clk *parent;
defa4c73
TY
245 struct cpu_data *data = per_cpu(cpu_data, policy->cpu);
246
9c0ebcf7 247 parent = of_clk_get(data->parent, data->table[index].driver_data);
652ed95d 248 return clk_set_parent(policy->clk, parent);
defa4c73
TY
249}
250
defa4c73
TY
251static struct cpufreq_driver ppc_corenet_cpufreq_driver = {
252 .name = "ppc_cpufreq",
defa4c73
TY
253 .flags = CPUFREQ_CONST_LOOPS,
254 .init = corenet_cpufreq_cpu_init,
255 .exit = __exit_p(corenet_cpufreq_cpu_exit),
dc2398d7 256 .verify = cpufreq_generic_frequency_table_verify,
9c0ebcf7 257 .target_index = corenet_cpufreq_target,
652ed95d 258 .get = cpufreq_generic_get,
dc2398d7 259 .attr = cpufreq_generic_attr,
defa4c73
TY
260};
261
262static const struct of_device_id node_matches[] __initdata = {
263 { .compatible = "fsl,p2041-clockgen", .data = &sdata[0], },
264 { .compatible = "fsl,p3041-clockgen", .data = &sdata[0], },
265 { .compatible = "fsl,p5020-clockgen", .data = &sdata[1], },
266 { .compatible = "fsl,p4080-clockgen", .data = &sdata[2], },
267 { .compatible = "fsl,p5040-clockgen", .data = &sdata[2], },
268 { .compatible = "fsl,qoriq-clockgen-2.0", },
269 {}
270};
271
272static int __init ppc_corenet_cpufreq_init(void)
273{
274 int ret;
275 struct device_node *np;
276 const struct of_device_id *match;
277 const struct soc_data *data;
278 unsigned int cpu;
279
280 np = of_find_matching_node(NULL, node_matches);
281 if (!np)
282 return -ENODEV;
283
284 for_each_possible_cpu(cpu) {
285 if (!alloc_cpumask_var(&per_cpu(cpu_mask, cpu), GFP_KERNEL))
286 goto err_mask;
287 cpumask_copy(per_cpu(cpu_mask, cpu), cpu_core_mask(cpu));
288 }
289
290 match = of_match_node(node_matches, np);
291 data = match->data;
292 if (data) {
293 if (data->flag)
294 fmask = data->freq_mask;
295 min_cpufreq = fsl_get_sys_freq();
296 } else {
297 min_cpufreq = fsl_get_sys_freq() / 2;
298 }
299
300 of_node_put(np);
301
302 ret = cpufreq_register_driver(&ppc_corenet_cpufreq_driver);
303 if (!ret)
304 pr_info("Freescale PowerPC corenet CPU frequency scaling driver\n");
305
306 return ret;
307
308err_mask:
309 for_each_possible_cpu(cpu)
310 free_cpumask_var(per_cpu(cpu_mask, cpu));
311
312 return -ENOMEM;
313}
314module_init(ppc_corenet_cpufreq_init);
315
316static void __exit ppc_corenet_cpufreq_exit(void)
317{
318 unsigned int cpu;
319
320 for_each_possible_cpu(cpu)
321 free_cpumask_var(per_cpu(cpu_mask, cpu));
322
323 cpufreq_unregister_driver(&ppc_corenet_cpufreq_driver);
324}
325module_exit(ppc_corenet_cpufreq_exit);
326
327MODULE_LICENSE("GPL");
328MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
329MODULE_DESCRIPTION("cpufreq driver for Freescale e500mc series SoCs");
This page took 0.072774 seconds and 5 git commands to generate.