xref: /OK3568_Linux_fs/kernel/drivers/clk/mvebu/ap-cpu-clk.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Marvell Armada AP CPU Clock Controller
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2018 Marvell
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Omri Itach <omrii@marvell.com>
8*4882a593Smuzhiyun  * Gregory Clement <gregory.clement@bootlin.com>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #define pr_fmt(fmt) "ap-cpu-clk: " fmt
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/clk-provider.h>
14*4882a593Smuzhiyun #include <linux/clk.h>
15*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
16*4882a593Smuzhiyun #include <linux/of.h>
17*4882a593Smuzhiyun #include <linux/of_address.h>
18*4882a593Smuzhiyun #include <linux/of_platform.h>
19*4882a593Smuzhiyun #include <linux/platform_device.h>
20*4882a593Smuzhiyun #include <linux/regmap.h>
21*4882a593Smuzhiyun #include "armada_ap_cp_helper.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #define AP806_CPU_CLUSTER0		0
24*4882a593Smuzhiyun #define AP806_CPU_CLUSTER1		1
25*4882a593Smuzhiyun #define AP806_CPUS_PER_CLUSTER		2
26*4882a593Smuzhiyun #define APN806_CPU1_MASK		0x1
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define APN806_CLUSTER_NUM_OFFSET	8
29*4882a593Smuzhiyun #define APN806_CLUSTER_NUM_MASK		BIT(APN806_CLUSTER_NUM_OFFSET)
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define APN806_MAX_DIVIDER		32
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /**
34*4882a593Smuzhiyun  * struct cpu_dfs_regs: CPU DFS register mapping
35*4882a593Smuzhiyun  * @divider_reg: full integer ratio from PLL frequency to CPU clock frequency
36*4882a593Smuzhiyun  * @force_reg: request to force new ratio regardless of relation to other clocks
37*4882a593Smuzhiyun  * @ratio_reg: central request to switch ratios
38*4882a593Smuzhiyun  */
39*4882a593Smuzhiyun struct cpu_dfs_regs {
40*4882a593Smuzhiyun 	unsigned int divider_reg;
41*4882a593Smuzhiyun 	unsigned int force_reg;
42*4882a593Smuzhiyun 	unsigned int ratio_reg;
43*4882a593Smuzhiyun 	unsigned int ratio_state_reg;
44*4882a593Smuzhiyun 	unsigned int divider_mask;
45*4882a593Smuzhiyun 	unsigned int cluster_offset;
46*4882a593Smuzhiyun 	unsigned int force_mask;
47*4882a593Smuzhiyun 	int divider_offset;
48*4882a593Smuzhiyun 	int divider_ratio;
49*4882a593Smuzhiyun 	int ratio_offset;
50*4882a593Smuzhiyun 	int ratio_state_offset;
51*4882a593Smuzhiyun 	int ratio_state_cluster_offset;
52*4882a593Smuzhiyun };
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /* AP806 CPU DFS register mapping*/
55*4882a593Smuzhiyun #define AP806_CA72MP2_0_PLL_CR_0_REG_OFFSET		0x278
56*4882a593Smuzhiyun #define AP806_CA72MP2_0_PLL_CR_1_REG_OFFSET		0x280
57*4882a593Smuzhiyun #define AP806_CA72MP2_0_PLL_CR_2_REG_OFFSET		0x284
58*4882a593Smuzhiyun #define AP806_CA72MP2_0_PLL_SR_REG_OFFSET		0xC94
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun #define AP806_CA72MP2_0_PLL_CR_CLUSTER_OFFSET		0x14
61*4882a593Smuzhiyun #define AP806_PLL_CR_0_CPU_CLK_DIV_RATIO_OFFSET		0
62*4882a593Smuzhiyun #define AP806_PLL_CR_CPU_CLK_DIV_RATIO			0
63*4882a593Smuzhiyun #define AP806_PLL_CR_0_CPU_CLK_DIV_RATIO_MASK \
64*4882a593Smuzhiyun 			(0x3f << AP806_PLL_CR_0_CPU_CLK_DIV_RATIO_OFFSET)
65*4882a593Smuzhiyun #define AP806_PLL_CR_0_CPU_CLK_RELOAD_FORCE_OFFSET	24
66*4882a593Smuzhiyun #define AP806_PLL_CR_0_CPU_CLK_RELOAD_FORCE_MASK \
67*4882a593Smuzhiyun 			(0x1 << AP806_PLL_CR_0_CPU_CLK_RELOAD_FORCE_OFFSET)
68*4882a593Smuzhiyun #define AP806_PLL_CR_0_CPU_CLK_RELOAD_RATIO_OFFSET	16
69*4882a593Smuzhiyun #define AP806_CA72MP2_0_PLL_RATIO_STABLE_OFFSET	0
70*4882a593Smuzhiyun #define AP806_CA72MP2_0_PLL_RATIO_STATE			11
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun #define STATUS_POLL_PERIOD_US		1
73*4882a593Smuzhiyun #define STATUS_POLL_TIMEOUT_US		1000000
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #define to_ap_cpu_clk(_hw) container_of(_hw, struct ap_cpu_clk, hw)
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun static const struct cpu_dfs_regs ap806_dfs_regs = {
78*4882a593Smuzhiyun 	.divider_reg = AP806_CA72MP2_0_PLL_CR_0_REG_OFFSET,
79*4882a593Smuzhiyun 	.force_reg = AP806_CA72MP2_0_PLL_CR_1_REG_OFFSET,
80*4882a593Smuzhiyun 	.ratio_reg = AP806_CA72MP2_0_PLL_CR_2_REG_OFFSET,
81*4882a593Smuzhiyun 	.ratio_state_reg = AP806_CA72MP2_0_PLL_SR_REG_OFFSET,
82*4882a593Smuzhiyun 	.divider_mask = AP806_PLL_CR_0_CPU_CLK_DIV_RATIO_MASK,
83*4882a593Smuzhiyun 	.cluster_offset = AP806_CA72MP2_0_PLL_CR_CLUSTER_OFFSET,
84*4882a593Smuzhiyun 	.force_mask = AP806_PLL_CR_0_CPU_CLK_RELOAD_FORCE_MASK,
85*4882a593Smuzhiyun 	.divider_offset = AP806_PLL_CR_0_CPU_CLK_DIV_RATIO_OFFSET,
86*4882a593Smuzhiyun 	.divider_ratio = AP806_PLL_CR_CPU_CLK_DIV_RATIO,
87*4882a593Smuzhiyun 	.ratio_offset = AP806_PLL_CR_0_CPU_CLK_RELOAD_RATIO_OFFSET,
88*4882a593Smuzhiyun 	.ratio_state_offset = AP806_CA72MP2_0_PLL_RATIO_STABLE_OFFSET,
89*4882a593Smuzhiyun 	.ratio_state_cluster_offset = AP806_CA72MP2_0_PLL_RATIO_STABLE_OFFSET,
90*4882a593Smuzhiyun };
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /* AP807 CPU DFS register mapping */
93*4882a593Smuzhiyun #define AP807_DEVICE_GENERAL_CONTROL_10_REG_OFFSET		0x278
94*4882a593Smuzhiyun #define AP807_DEVICE_GENERAL_CONTROL_11_REG_OFFSET		0x27c
95*4882a593Smuzhiyun #define AP807_DEVICE_GENERAL_STATUS_6_REG_OFFSET		0xc98
96*4882a593Smuzhiyun #define AP807_CA72MP2_0_PLL_CR_CLUSTER_OFFSET			0x8
97*4882a593Smuzhiyun #define AP807_PLL_CR_0_CPU_CLK_DIV_RATIO_OFFSET			18
98*4882a593Smuzhiyun #define AP807_PLL_CR_0_CPU_CLK_DIV_RATIO_MASK \
99*4882a593Smuzhiyun 		(0x3f << AP807_PLL_CR_0_CPU_CLK_DIV_RATIO_OFFSET)
100*4882a593Smuzhiyun #define AP807_PLL_CR_1_CPU_CLK_DIV_RATIO_OFFSET			12
101*4882a593Smuzhiyun #define AP807_PLL_CR_1_CPU_CLK_DIV_RATIO_MASK \
102*4882a593Smuzhiyun 		(0x3f << AP807_PLL_CR_1_CPU_CLK_DIV_RATIO_OFFSET)
103*4882a593Smuzhiyun #define AP807_PLL_CR_CPU_CLK_DIV_RATIO				3
104*4882a593Smuzhiyun #define AP807_PLL_CR_0_CPU_CLK_RELOAD_FORCE_OFFSET		0
105*4882a593Smuzhiyun #define AP807_PLL_CR_0_CPU_CLK_RELOAD_FORCE_MASK \
106*4882a593Smuzhiyun 		(0x3 << AP807_PLL_CR_0_CPU_CLK_RELOAD_FORCE_OFFSET)
107*4882a593Smuzhiyun #define AP807_PLL_CR_0_CPU_CLK_RELOAD_RATIO_OFFSET		6
108*4882a593Smuzhiyun #define	AP807_CA72MP2_0_PLL_CLKDIV_RATIO_STABLE_OFFSET		20
109*4882a593Smuzhiyun #define AP807_CA72MP2_0_PLL_CLKDIV_RATIO_STABLE_CLUSTER_OFFSET	3
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun static const struct cpu_dfs_regs ap807_dfs_regs = {
112*4882a593Smuzhiyun 	.divider_reg = AP807_DEVICE_GENERAL_CONTROL_10_REG_OFFSET,
113*4882a593Smuzhiyun 	.force_reg = AP807_DEVICE_GENERAL_CONTROL_11_REG_OFFSET,
114*4882a593Smuzhiyun 	.ratio_reg = AP807_DEVICE_GENERAL_CONTROL_11_REG_OFFSET,
115*4882a593Smuzhiyun 	.ratio_state_reg = AP807_DEVICE_GENERAL_STATUS_6_REG_OFFSET,
116*4882a593Smuzhiyun 	.divider_mask = AP807_PLL_CR_0_CPU_CLK_DIV_RATIO_MASK,
117*4882a593Smuzhiyun 	.cluster_offset = AP807_CA72MP2_0_PLL_CR_CLUSTER_OFFSET,
118*4882a593Smuzhiyun 	.force_mask = AP807_PLL_CR_0_CPU_CLK_RELOAD_FORCE_MASK,
119*4882a593Smuzhiyun 	.divider_offset = AP807_PLL_CR_0_CPU_CLK_DIV_RATIO_OFFSET,
120*4882a593Smuzhiyun 	.divider_ratio = AP807_PLL_CR_CPU_CLK_DIV_RATIO,
121*4882a593Smuzhiyun 	.ratio_offset = AP807_PLL_CR_0_CPU_CLK_RELOAD_RATIO_OFFSET,
122*4882a593Smuzhiyun 	.ratio_state_offset = AP807_CA72MP2_0_PLL_CLKDIV_RATIO_STABLE_OFFSET,
123*4882a593Smuzhiyun 	.ratio_state_cluster_offset =
124*4882a593Smuzhiyun 		AP807_CA72MP2_0_PLL_CLKDIV_RATIO_STABLE_CLUSTER_OFFSET
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun  * struct ap806_clk: CPU cluster clock controller instance
129*4882a593Smuzhiyun  * @cluster: Cluster clock controller index
130*4882a593Smuzhiyun  * @clk_name: Cluster clock controller name
131*4882a593Smuzhiyun  * @dev : Cluster clock device
132*4882a593Smuzhiyun  * @hw: HW specific structure of Cluster clock controller
133*4882a593Smuzhiyun  * @pll_cr_base: CA72MP2 Register base (Device Sample at Reset register)
134*4882a593Smuzhiyun  */
135*4882a593Smuzhiyun struct ap_cpu_clk {
136*4882a593Smuzhiyun 	unsigned int cluster;
137*4882a593Smuzhiyun 	const char *clk_name;
138*4882a593Smuzhiyun 	struct device *dev;
139*4882a593Smuzhiyun 	struct clk_hw hw;
140*4882a593Smuzhiyun 	struct regmap *pll_cr_base;
141*4882a593Smuzhiyun 	const struct cpu_dfs_regs *pll_regs;
142*4882a593Smuzhiyun };
143*4882a593Smuzhiyun 
ap_cpu_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)144*4882a593Smuzhiyun static unsigned long ap_cpu_clk_recalc_rate(struct clk_hw *hw,
145*4882a593Smuzhiyun 					    unsigned long parent_rate)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	struct ap_cpu_clk *clk = to_ap_cpu_clk(hw);
148*4882a593Smuzhiyun 	unsigned int cpu_clkdiv_reg;
149*4882a593Smuzhiyun 	int cpu_clkdiv_ratio;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	cpu_clkdiv_reg = clk->pll_regs->divider_reg +
152*4882a593Smuzhiyun 		(clk->cluster * clk->pll_regs->cluster_offset);
153*4882a593Smuzhiyun 	regmap_read(clk->pll_cr_base, cpu_clkdiv_reg, &cpu_clkdiv_ratio);
154*4882a593Smuzhiyun 	cpu_clkdiv_ratio &= clk->pll_regs->divider_mask;
155*4882a593Smuzhiyun 	cpu_clkdiv_ratio >>= clk->pll_regs->divider_offset;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	return parent_rate / cpu_clkdiv_ratio;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
ap_cpu_clk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)160*4882a593Smuzhiyun static int ap_cpu_clk_set_rate(struct clk_hw *hw, unsigned long rate,
161*4882a593Smuzhiyun 			       unsigned long parent_rate)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	struct ap_cpu_clk *clk = to_ap_cpu_clk(hw);
164*4882a593Smuzhiyun 	int ret, reg, divider = parent_rate / rate;
165*4882a593Smuzhiyun 	unsigned int cpu_clkdiv_reg, cpu_force_reg, cpu_ratio_reg, stable_bit;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	cpu_clkdiv_reg = clk->pll_regs->divider_reg +
168*4882a593Smuzhiyun 		(clk->cluster * clk->pll_regs->cluster_offset);
169*4882a593Smuzhiyun 	cpu_force_reg = clk->pll_regs->force_reg +
170*4882a593Smuzhiyun 		(clk->cluster * clk->pll_regs->cluster_offset);
171*4882a593Smuzhiyun 	cpu_ratio_reg = clk->pll_regs->ratio_reg +
172*4882a593Smuzhiyun 		(clk->cluster * clk->pll_regs->cluster_offset);
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	regmap_read(clk->pll_cr_base, cpu_clkdiv_reg, &reg);
175*4882a593Smuzhiyun 	reg &= ~(clk->pll_regs->divider_mask);
176*4882a593Smuzhiyun 	reg |= (divider << clk->pll_regs->divider_offset);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	/*
179*4882a593Smuzhiyun 	 * AP807 CPU divider has two channels with ratio 1:3 and divider_ratio
180*4882a593Smuzhiyun 	 * is 1. Otherwise, in the case of the AP806, divider_ratio is 0.
181*4882a593Smuzhiyun 	 */
182*4882a593Smuzhiyun 	if (clk->pll_regs->divider_ratio) {
183*4882a593Smuzhiyun 		reg &= ~(AP807_PLL_CR_1_CPU_CLK_DIV_RATIO_MASK);
184*4882a593Smuzhiyun 		reg |= ((divider * clk->pll_regs->divider_ratio) <<
185*4882a593Smuzhiyun 				AP807_PLL_CR_1_CPU_CLK_DIV_RATIO_OFFSET);
186*4882a593Smuzhiyun 	}
187*4882a593Smuzhiyun 	regmap_write(clk->pll_cr_base, cpu_clkdiv_reg, reg);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	regmap_update_bits(clk->pll_cr_base, cpu_force_reg,
191*4882a593Smuzhiyun 			   clk->pll_regs->force_mask,
192*4882a593Smuzhiyun 			   clk->pll_regs->force_mask);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	regmap_update_bits(clk->pll_cr_base, cpu_ratio_reg,
195*4882a593Smuzhiyun 			   BIT(clk->pll_regs->ratio_offset),
196*4882a593Smuzhiyun 			   BIT(clk->pll_regs->ratio_offset));
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	stable_bit = BIT(clk->pll_regs->ratio_state_offset +
199*4882a593Smuzhiyun 			 clk->cluster *
200*4882a593Smuzhiyun 			 clk->pll_regs->ratio_state_cluster_offset);
201*4882a593Smuzhiyun 	ret = regmap_read_poll_timeout(clk->pll_cr_base,
202*4882a593Smuzhiyun 				       clk->pll_regs->ratio_state_reg, reg,
203*4882a593Smuzhiyun 				       reg & stable_bit, STATUS_POLL_PERIOD_US,
204*4882a593Smuzhiyun 				       STATUS_POLL_TIMEOUT_US);
205*4882a593Smuzhiyun 	if (ret)
206*4882a593Smuzhiyun 		return ret;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	regmap_update_bits(clk->pll_cr_base, cpu_ratio_reg,
209*4882a593Smuzhiyun 			   BIT(clk->pll_regs->ratio_offset), 0);
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	return 0;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun 
ap_cpu_clk_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)214*4882a593Smuzhiyun static long ap_cpu_clk_round_rate(struct clk_hw *hw, unsigned long rate,
215*4882a593Smuzhiyun 				  unsigned long *parent_rate)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun 	int divider = *parent_rate / rate;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	divider = min(divider, APN806_MAX_DIVIDER);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	return *parent_rate / divider;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun static const struct clk_ops ap_cpu_clk_ops = {
225*4882a593Smuzhiyun 	.recalc_rate	= ap_cpu_clk_recalc_rate,
226*4882a593Smuzhiyun 	.round_rate	= ap_cpu_clk_round_rate,
227*4882a593Smuzhiyun 	.set_rate	= ap_cpu_clk_set_rate,
228*4882a593Smuzhiyun };
229*4882a593Smuzhiyun 
ap_cpu_clock_probe(struct platform_device * pdev)230*4882a593Smuzhiyun static int ap_cpu_clock_probe(struct platform_device *pdev)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	int ret, nclusters = 0, cluster_index = 0;
233*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
234*4882a593Smuzhiyun 	struct device_node *dn, *np = dev->of_node;
235*4882a593Smuzhiyun 	struct clk_hw_onecell_data *ap_cpu_data;
236*4882a593Smuzhiyun 	struct ap_cpu_clk *ap_cpu_clk;
237*4882a593Smuzhiyun 	struct regmap *regmap;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	regmap = syscon_node_to_regmap(np->parent);
240*4882a593Smuzhiyun 	if (IS_ERR(regmap)) {
241*4882a593Smuzhiyun 		pr_err("cannot get pll_cr_base regmap\n");
242*4882a593Smuzhiyun 		return PTR_ERR(regmap);
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	/*
246*4882a593Smuzhiyun 	 * AP806 has 4 cpus and DFS for AP806 is controlled per
247*4882a593Smuzhiyun 	 * cluster (2 CPUs per cluster), cpu0 and cpu1 are fixed to
248*4882a593Smuzhiyun 	 * cluster0 while cpu2 and cpu3 are fixed to cluster1 whether
249*4882a593Smuzhiyun 	 * they are enabled or not.  Since cpu0 is the boot cpu, then
250*4882a593Smuzhiyun 	 * cluster0 must exist.  If cpu2 or cpu3 is enabled, cluster1
251*4882a593Smuzhiyun 	 * will exist and the cluster number is 2; otherwise the
252*4882a593Smuzhiyun 	 * cluster number is 1.
253*4882a593Smuzhiyun 	 */
254*4882a593Smuzhiyun 	nclusters = 1;
255*4882a593Smuzhiyun 	for_each_of_cpu_node(dn) {
256*4882a593Smuzhiyun 		int cpu, err;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 		err = of_property_read_u32(dn, "reg", &cpu);
259*4882a593Smuzhiyun 		if (WARN_ON(err)) {
260*4882a593Smuzhiyun 			of_node_put(dn);
261*4882a593Smuzhiyun 			return err;
262*4882a593Smuzhiyun 		}
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 		/* If cpu2 or cpu3 is enabled */
265*4882a593Smuzhiyun 		if (cpu & APN806_CLUSTER_NUM_MASK) {
266*4882a593Smuzhiyun 			nclusters = 2;
267*4882a593Smuzhiyun 			of_node_put(dn);
268*4882a593Smuzhiyun 			break;
269*4882a593Smuzhiyun 		}
270*4882a593Smuzhiyun 	}
271*4882a593Smuzhiyun 	/*
272*4882a593Smuzhiyun 	 * DFS for AP806 is controlled per cluster (2 CPUs per cluster),
273*4882a593Smuzhiyun 	 * so allocate structs per cluster
274*4882a593Smuzhiyun 	 */
275*4882a593Smuzhiyun 	ap_cpu_clk = devm_kcalloc(dev, nclusters, sizeof(*ap_cpu_clk),
276*4882a593Smuzhiyun 				  GFP_KERNEL);
277*4882a593Smuzhiyun 	if (!ap_cpu_clk)
278*4882a593Smuzhiyun 		return -ENOMEM;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	ap_cpu_data = devm_kzalloc(dev, struct_size(ap_cpu_data, hws,
281*4882a593Smuzhiyun 						    nclusters),
282*4882a593Smuzhiyun 				GFP_KERNEL);
283*4882a593Smuzhiyun 	if (!ap_cpu_data)
284*4882a593Smuzhiyun 		return -ENOMEM;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	for_each_of_cpu_node(dn) {
287*4882a593Smuzhiyun 		char *clk_name = "cpu-cluster-0";
288*4882a593Smuzhiyun 		struct clk_init_data init;
289*4882a593Smuzhiyun 		const char *parent_name;
290*4882a593Smuzhiyun 		struct clk *parent;
291*4882a593Smuzhiyun 		int cpu, err;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 		err = of_property_read_u32(dn, "reg", &cpu);
294*4882a593Smuzhiyun 		if (WARN_ON(err)) {
295*4882a593Smuzhiyun 			of_node_put(dn);
296*4882a593Smuzhiyun 			return err;
297*4882a593Smuzhiyun 		}
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 		cluster_index = cpu & APN806_CLUSTER_NUM_MASK;
300*4882a593Smuzhiyun 		cluster_index >>= APN806_CLUSTER_NUM_OFFSET;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 		/* Initialize once for one cluster */
303*4882a593Smuzhiyun 		if (ap_cpu_data->hws[cluster_index])
304*4882a593Smuzhiyun 			continue;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 		parent = of_clk_get(np, cluster_index);
307*4882a593Smuzhiyun 		if (IS_ERR(parent)) {
308*4882a593Smuzhiyun 			dev_err(dev, "Could not get the clock parent\n");
309*4882a593Smuzhiyun 			of_node_put(dn);
310*4882a593Smuzhiyun 			return -EINVAL;
311*4882a593Smuzhiyun 		}
312*4882a593Smuzhiyun 		parent_name =  __clk_get_name(parent);
313*4882a593Smuzhiyun 		clk_name[12] += cluster_index;
314*4882a593Smuzhiyun 		ap_cpu_clk[cluster_index].clk_name =
315*4882a593Smuzhiyun 			ap_cp_unique_name(dev, np->parent, clk_name);
316*4882a593Smuzhiyun 		ap_cpu_clk[cluster_index].cluster = cluster_index;
317*4882a593Smuzhiyun 		ap_cpu_clk[cluster_index].pll_cr_base = regmap;
318*4882a593Smuzhiyun 		ap_cpu_clk[cluster_index].hw.init = &init;
319*4882a593Smuzhiyun 		ap_cpu_clk[cluster_index].dev = dev;
320*4882a593Smuzhiyun 		ap_cpu_clk[cluster_index].pll_regs = of_device_get_match_data(&pdev->dev);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 		init.name = ap_cpu_clk[cluster_index].clk_name;
323*4882a593Smuzhiyun 		init.ops = &ap_cpu_clk_ops;
324*4882a593Smuzhiyun 		init.num_parents = 1;
325*4882a593Smuzhiyun 		init.parent_names = &parent_name;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 		ret = devm_clk_hw_register(dev, &ap_cpu_clk[cluster_index].hw);
328*4882a593Smuzhiyun 		if (ret) {
329*4882a593Smuzhiyun 			of_node_put(dn);
330*4882a593Smuzhiyun 			return ret;
331*4882a593Smuzhiyun 		}
332*4882a593Smuzhiyun 		ap_cpu_data->hws[cluster_index] = &ap_cpu_clk[cluster_index].hw;
333*4882a593Smuzhiyun 	}
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	ap_cpu_data->num = cluster_index + 1;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, ap_cpu_data);
338*4882a593Smuzhiyun 	if (ret)
339*4882a593Smuzhiyun 		dev_err(dev, "failed to register OF clock provider\n");
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	return ret;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun static const struct of_device_id ap_cpu_clock_of_match[] = {
345*4882a593Smuzhiyun 	{
346*4882a593Smuzhiyun 		.compatible = "marvell,ap806-cpu-clock",
347*4882a593Smuzhiyun 		.data = &ap806_dfs_regs,
348*4882a593Smuzhiyun 	},
349*4882a593Smuzhiyun 	{
350*4882a593Smuzhiyun 		.compatible = "marvell,ap807-cpu-clock",
351*4882a593Smuzhiyun 		.data = &ap807_dfs_regs,
352*4882a593Smuzhiyun 	},
353*4882a593Smuzhiyun 	{ }
354*4882a593Smuzhiyun };
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun static struct platform_driver ap_cpu_clock_driver = {
357*4882a593Smuzhiyun 	.probe = ap_cpu_clock_probe,
358*4882a593Smuzhiyun 	.driver		= {
359*4882a593Smuzhiyun 		.name	= "marvell-ap-cpu-clock",
360*4882a593Smuzhiyun 		.of_match_table = ap_cpu_clock_of_match,
361*4882a593Smuzhiyun 		.suppress_bind_attrs = true,
362*4882a593Smuzhiyun 	},
363*4882a593Smuzhiyun };
364*4882a593Smuzhiyun builtin_platform_driver(ap_cpu_clock_driver);
365