xref: /openbmc/linux/drivers/cpufreq/armada-8k-cpufreq.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1f525a670SGregory CLEMENT // SPDX-License-Identifier: GPL-2.0+
2f525a670SGregory CLEMENT /*
3f525a670SGregory CLEMENT  * CPUFreq support for Armada 8K
4f525a670SGregory CLEMENT  *
5f525a670SGregory CLEMENT  * Copyright (C) 2018 Marvell
6f525a670SGregory CLEMENT  *
7f525a670SGregory CLEMENT  * Omri Itach <omrii@marvell.com>
8f525a670SGregory CLEMENT  * Gregory Clement <gregory.clement@bootlin.com>
9f525a670SGregory CLEMENT  */
10f525a670SGregory CLEMENT 
11f525a670SGregory CLEMENT #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12f525a670SGregory CLEMENT 
13f525a670SGregory CLEMENT #include <linux/clk.h>
14f525a670SGregory CLEMENT #include <linux/cpu.h>
15f525a670SGregory CLEMENT #include <linux/err.h>
16f525a670SGregory CLEMENT #include <linux/init.h>
17f525a670SGregory CLEMENT #include <linux/kernel.h>
18f525a670SGregory CLEMENT #include <linux/module.h>
19f525a670SGregory CLEMENT #include <linux/of.h>
20f525a670SGregory CLEMENT #include <linux/platform_device.h>
21f525a670SGregory CLEMENT #include <linux/pm_opp.h>
22f525a670SGregory CLEMENT #include <linux/slab.h>
23f525a670SGregory CLEMENT 
24*8eec6e74SRussell King (Oracle) static const struct of_device_id __maybe_unused armada_8k_cpufreq_of_match[] = {
25*8eec6e74SRussell King (Oracle) 	{ .compatible = "marvell,ap806-cpu-clock" },
26*8eec6e74SRussell King (Oracle) 	{ .compatible = "marvell,ap807-cpu-clock" },
27*8eec6e74SRussell King (Oracle) 	{ },
28*8eec6e74SRussell King (Oracle) };
29*8eec6e74SRussell King (Oracle) MODULE_DEVICE_TABLE(of, armada_8k_cpufreq_of_match);
30*8eec6e74SRussell King (Oracle) 
31f525a670SGregory CLEMENT /*
32f525a670SGregory CLEMENT  * Setup the opps list with the divider for the max frequency, that
33f525a670SGregory CLEMENT  * will be filled at runtime.
34f525a670SGregory CLEMENT  */
35f525a670SGregory CLEMENT static const int opps_div[] __initconst = {1, 2, 3, 4};
36f525a670SGregory CLEMENT 
37f525a670SGregory CLEMENT static struct platform_device *armada_8k_pdev;
38f525a670SGregory CLEMENT 
39f525a670SGregory CLEMENT struct freq_table {
40f525a670SGregory CLEMENT 	struct device *cpu_dev;
41f525a670SGregory CLEMENT 	unsigned int freq[ARRAY_SIZE(opps_div)];
42f525a670SGregory CLEMENT };
43f525a670SGregory CLEMENT 
44f525a670SGregory CLEMENT /* If the CPUs share the same clock, then they are in the same cluster. */
armada_8k_get_sharing_cpus(struct clk * cur_clk,struct cpumask * cpumask)45f525a670SGregory CLEMENT static void __init armada_8k_get_sharing_cpus(struct clk *cur_clk,
46f525a670SGregory CLEMENT 					      struct cpumask *cpumask)
47f525a670SGregory CLEMENT {
48f525a670SGregory CLEMENT 	int cpu;
49f525a670SGregory CLEMENT 
50f525a670SGregory CLEMENT 	for_each_possible_cpu(cpu) {
51f525a670SGregory CLEMENT 		struct device *cpu_dev;
52f525a670SGregory CLEMENT 		struct clk *clk;
53f525a670SGregory CLEMENT 
54f525a670SGregory CLEMENT 		cpu_dev = get_cpu_device(cpu);
55f525a670SGregory CLEMENT 		if (!cpu_dev) {
56f525a670SGregory CLEMENT 			pr_warn("Failed to get cpu%d device\n", cpu);
57f525a670SGregory CLEMENT 			continue;
58f525a670SGregory CLEMENT 		}
59f525a670SGregory CLEMENT 
60f525a670SGregory CLEMENT 		clk = clk_get(cpu_dev, 0);
61f525a670SGregory CLEMENT 		if (IS_ERR(clk)) {
62f525a670SGregory CLEMENT 			pr_warn("Cannot get clock for CPU %d\n", cpu);
63f525a670SGregory CLEMENT 		} else {
64f525a670SGregory CLEMENT 			if (clk_is_match(clk, cur_clk))
65f525a670SGregory CLEMENT 				cpumask_set_cpu(cpu, cpumask);
66f525a670SGregory CLEMENT 
67f525a670SGregory CLEMENT 			clk_put(clk);
68f525a670SGregory CLEMENT 		}
69f525a670SGregory CLEMENT 	}
70f525a670SGregory CLEMENT }
71f525a670SGregory CLEMENT 
armada_8k_add_opp(struct clk * clk,struct device * cpu_dev,struct freq_table * freq_tables,int opps_index)72f525a670SGregory CLEMENT static int __init armada_8k_add_opp(struct clk *clk, struct device *cpu_dev,
73f525a670SGregory CLEMENT 				    struct freq_table *freq_tables,
74f525a670SGregory CLEMENT 				    int opps_index)
75f525a670SGregory CLEMENT {
76f525a670SGregory CLEMENT 	unsigned int cur_frequency;
77f525a670SGregory CLEMENT 	unsigned int freq;
78f525a670SGregory CLEMENT 	int i, ret;
79f525a670SGregory CLEMENT 
80f525a670SGregory CLEMENT 	/* Get nominal (current) CPU frequency. */
81f525a670SGregory CLEMENT 	cur_frequency = clk_get_rate(clk);
82f525a670SGregory CLEMENT 	if (!cur_frequency) {
83f525a670SGregory CLEMENT 		dev_err(cpu_dev, "Failed to get clock rate for this CPU\n");
84f525a670SGregory CLEMENT 		return -EINVAL;
85f525a670SGregory CLEMENT 	}
86f525a670SGregory CLEMENT 
87f525a670SGregory CLEMENT 	freq_tables[opps_index].cpu_dev = cpu_dev;
88f525a670SGregory CLEMENT 
89f525a670SGregory CLEMENT 	for (i = 0; i < ARRAY_SIZE(opps_div); i++) {
90f525a670SGregory CLEMENT 		freq = cur_frequency / opps_div[i];
91f525a670SGregory CLEMENT 
92f525a670SGregory CLEMENT 		ret = dev_pm_opp_add(cpu_dev, freq, 0);
93f525a670SGregory CLEMENT 		if (ret)
94f525a670SGregory CLEMENT 			return ret;
95f525a670SGregory CLEMENT 
96f525a670SGregory CLEMENT 		freq_tables[opps_index].freq[i] = freq;
97f525a670SGregory CLEMENT 	}
98f525a670SGregory CLEMENT 
99f525a670SGregory CLEMENT 	return 0;
100f525a670SGregory CLEMENT }
101f525a670SGregory CLEMENT 
armada_8k_cpufreq_free_table(struct freq_table * freq_tables)102f525a670SGregory CLEMENT static void armada_8k_cpufreq_free_table(struct freq_table *freq_tables)
103f525a670SGregory CLEMENT {
104f525a670SGregory CLEMENT 	int opps_index, nb_cpus = num_possible_cpus();
105f525a670SGregory CLEMENT 
106f525a670SGregory CLEMENT 	for (opps_index = 0 ; opps_index <= nb_cpus; opps_index++) {
107f525a670SGregory CLEMENT 		int i;
108f525a670SGregory CLEMENT 
109f525a670SGregory CLEMENT 		/* If cpu_dev is NULL then we reached the end of the array */
110f525a670SGregory CLEMENT 		if (!freq_tables[opps_index].cpu_dev)
111f525a670SGregory CLEMENT 			break;
112f525a670SGregory CLEMENT 
113f525a670SGregory CLEMENT 		for (i = 0; i < ARRAY_SIZE(opps_div); i++) {
114f525a670SGregory CLEMENT 			/*
115f525a670SGregory CLEMENT 			 * A 0Hz frequency is not valid, this meant
116f525a670SGregory CLEMENT 			 * that it was not yet initialized so there is
117f525a670SGregory CLEMENT 			 * no more opp to free
118f525a670SGregory CLEMENT 			 */
119f525a670SGregory CLEMENT 			if (freq_tables[opps_index].freq[i] == 0)
120f525a670SGregory CLEMENT 				break;
121f525a670SGregory CLEMENT 
122f525a670SGregory CLEMENT 			dev_pm_opp_remove(freq_tables[opps_index].cpu_dev,
123f525a670SGregory CLEMENT 					  freq_tables[opps_index].freq[i]);
124f525a670SGregory CLEMENT 		}
125f525a670SGregory CLEMENT 	}
126f525a670SGregory CLEMENT 
127f525a670SGregory CLEMENT 	kfree(freq_tables);
128f525a670SGregory CLEMENT }
129f525a670SGregory CLEMENT 
armada_8k_cpufreq_init(void)130f525a670SGregory CLEMENT static int __init armada_8k_cpufreq_init(void)
131f525a670SGregory CLEMENT {
132f525a670SGregory CLEMENT 	int ret = 0, opps_index = 0, cpu, nb_cpus;
133f525a670SGregory CLEMENT 	struct freq_table *freq_tables;
134f525a670SGregory CLEMENT 	struct device_node *node;
135f525a670SGregory CLEMENT 	struct cpumask cpus;
136f525a670SGregory CLEMENT 
137*8eec6e74SRussell King (Oracle) 	node = of_find_matching_node_and_match(NULL, armada_8k_cpufreq_of_match,
138*8eec6e74SRussell King (Oracle) 					       NULL);
139d3c1e33fSJulia Lawall 	if (!node || !of_device_is_available(node)) {
140d3c1e33fSJulia Lawall 		of_node_put(node);
141f525a670SGregory CLEMENT 		return -ENODEV;
142d3c1e33fSJulia Lawall 	}
143b623fa32SWen Yang 	of_node_put(node);
144f525a670SGregory CLEMENT 
145f525a670SGregory CLEMENT 	nb_cpus = num_possible_cpus();
146f525a670SGregory CLEMENT 	freq_tables = kcalloc(nb_cpus, sizeof(*freq_tables), GFP_KERNEL);
1473355c91bSHariprasad Kelam 	if (!freq_tables)
1483355c91bSHariprasad Kelam 		return -ENOMEM;
149f525a670SGregory CLEMENT 	cpumask_copy(&cpus, cpu_possible_mask);
150f525a670SGregory CLEMENT 
151f525a670SGregory CLEMENT 	/*
152f525a670SGregory CLEMENT 	 * For each CPU, this loop registers the operating points
153f525a670SGregory CLEMENT 	 * supported (which are the nominal CPU frequency and full integer
154f525a670SGregory CLEMENT 	 * divisions of it).
155f525a670SGregory CLEMENT 	 */
156f525a670SGregory CLEMENT 	for_each_cpu(cpu, &cpus) {
157f525a670SGregory CLEMENT 		struct cpumask shared_cpus;
158f525a670SGregory CLEMENT 		struct device *cpu_dev;
159f525a670SGregory CLEMENT 		struct clk *clk;
160f525a670SGregory CLEMENT 
161f525a670SGregory CLEMENT 		cpu_dev = get_cpu_device(cpu);
162f525a670SGregory CLEMENT 
163f525a670SGregory CLEMENT 		if (!cpu_dev) {
164f525a670SGregory CLEMENT 			pr_err("Cannot get CPU %d\n", cpu);
165f525a670SGregory CLEMENT 			continue;
166f525a670SGregory CLEMENT 		}
167f525a670SGregory CLEMENT 
168f525a670SGregory CLEMENT 		clk = clk_get(cpu_dev, 0);
169f525a670SGregory CLEMENT 
170f525a670SGregory CLEMENT 		if (IS_ERR(clk)) {
171f525a670SGregory CLEMENT 			pr_err("Cannot get clock for CPU %d\n", cpu);
172f525a670SGregory CLEMENT 			ret = PTR_ERR(clk);
173f525a670SGregory CLEMENT 			goto remove_opp;
174f525a670SGregory CLEMENT 		}
175f525a670SGregory CLEMENT 
176f525a670SGregory CLEMENT 		ret = armada_8k_add_opp(clk, cpu_dev, freq_tables, opps_index);
177f525a670SGregory CLEMENT 		if (ret) {
178f525a670SGregory CLEMENT 			clk_put(clk);
179f525a670SGregory CLEMENT 			goto remove_opp;
180f525a670SGregory CLEMENT 		}
181f525a670SGregory CLEMENT 
182f525a670SGregory CLEMENT 		opps_index++;
183f525a670SGregory CLEMENT 		cpumask_clear(&shared_cpus);
184f525a670SGregory CLEMENT 		armada_8k_get_sharing_cpus(clk, &shared_cpus);
185f525a670SGregory CLEMENT 		dev_pm_opp_set_sharing_cpus(cpu_dev, &shared_cpus);
186f525a670SGregory CLEMENT 		cpumask_andnot(&cpus, &cpus, &shared_cpus);
187f525a670SGregory CLEMENT 		clk_put(clk);
188f525a670SGregory CLEMENT 	}
189f525a670SGregory CLEMENT 
190f525a670SGregory CLEMENT 	armada_8k_pdev = platform_device_register_simple("cpufreq-dt", -1,
191f525a670SGregory CLEMENT 							 NULL, 0);
192f525a670SGregory CLEMENT 	ret = PTR_ERR_OR_ZERO(armada_8k_pdev);
193f525a670SGregory CLEMENT 	if (ret)
194f525a670SGregory CLEMENT 		goto remove_opp;
195f525a670SGregory CLEMENT 
196f525a670SGregory CLEMENT 	platform_set_drvdata(armada_8k_pdev, freq_tables);
197f525a670SGregory CLEMENT 
198f525a670SGregory CLEMENT 	return 0;
199f525a670SGregory CLEMENT 
200f525a670SGregory CLEMENT remove_opp:
201f525a670SGregory CLEMENT 	armada_8k_cpufreq_free_table(freq_tables);
202f525a670SGregory CLEMENT 	return ret;
203f525a670SGregory CLEMENT }
204f525a670SGregory CLEMENT module_init(armada_8k_cpufreq_init);
205f525a670SGregory CLEMENT 
armada_8k_cpufreq_exit(void)206f525a670SGregory CLEMENT static void __exit armada_8k_cpufreq_exit(void)
207f525a670SGregory CLEMENT {
208f525a670SGregory CLEMENT 	struct freq_table *freq_tables = platform_get_drvdata(armada_8k_pdev);
209f525a670SGregory CLEMENT 
210f525a670SGregory CLEMENT 	platform_device_unregister(armada_8k_pdev);
211f525a670SGregory CLEMENT 	armada_8k_cpufreq_free_table(freq_tables);
212f525a670SGregory CLEMENT }
213f525a670SGregory CLEMENT module_exit(armada_8k_cpufreq_exit);
214f525a670SGregory CLEMENT 
215f525a670SGregory CLEMENT MODULE_AUTHOR("Gregory Clement <gregory.clement@bootlin.com>");
216f525a670SGregory CLEMENT MODULE_DESCRIPTION("Armada 8K cpufreq driver");
217f525a670SGregory CLEMENT MODULE_LICENSE("GPL");
218