xref: /OK3568_Linux_fs/kernel/drivers/clk/at91/clk-pll.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/clk-provider.h>
7*4882a593Smuzhiyun #include <linux/clkdev.h>
8*4882a593Smuzhiyun #include <linux/clk/at91_pmc.h>
9*4882a593Smuzhiyun #include <linux/of.h>
10*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
11*4882a593Smuzhiyun #include <linux/regmap.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include "pmc.h"
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define PLL_STATUS_MASK(id)	(1 << (1 + (id)))
16*4882a593Smuzhiyun #define PLL_REG(id)		(AT91_CKGR_PLLAR + ((id) * 4))
17*4882a593Smuzhiyun #define PLL_DIV_MASK		0xff
18*4882a593Smuzhiyun #define PLL_DIV_MAX		PLL_DIV_MASK
19*4882a593Smuzhiyun #define PLL_DIV(reg)		((reg) & PLL_DIV_MASK)
20*4882a593Smuzhiyun #define PLL_MUL(reg, layout)	(((reg) >> (layout)->mul_shift) & \
21*4882a593Smuzhiyun 				 (layout)->mul_mask)
22*4882a593Smuzhiyun #define PLL_MUL_MIN		2
23*4882a593Smuzhiyun #define PLL_MUL_MASK(layout)	((layout)->mul_mask)
24*4882a593Smuzhiyun #define PLL_MUL_MAX(layout)	(PLL_MUL_MASK(layout) + 1)
25*4882a593Smuzhiyun #define PLL_ICPR_SHIFT(id)	((id) * 16)
26*4882a593Smuzhiyun #define PLL_ICPR_MASK(id)	(0xffff << PLL_ICPR_SHIFT(id))
27*4882a593Smuzhiyun #define PLL_MAX_COUNT		0x3f
28*4882a593Smuzhiyun #define PLL_COUNT_SHIFT		8
29*4882a593Smuzhiyun #define PLL_OUT_SHIFT		14
30*4882a593Smuzhiyun #define PLL_MAX_ID		1
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define to_clk_pll(hw) container_of(hw, struct clk_pll, hw)
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun struct clk_pll {
35*4882a593Smuzhiyun 	struct clk_hw hw;
36*4882a593Smuzhiyun 	struct regmap *regmap;
37*4882a593Smuzhiyun 	u8 id;
38*4882a593Smuzhiyun 	u8 div;
39*4882a593Smuzhiyun 	u8 range;
40*4882a593Smuzhiyun 	u16 mul;
41*4882a593Smuzhiyun 	const struct clk_pll_layout *layout;
42*4882a593Smuzhiyun 	const struct clk_pll_characteristics *characteristics;
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun 
clk_pll_ready(struct regmap * regmap,int id)45*4882a593Smuzhiyun static inline bool clk_pll_ready(struct regmap *regmap, int id)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	unsigned int status;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	regmap_read(regmap, AT91_PMC_SR, &status);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	return status & PLL_STATUS_MASK(id) ? 1 : 0;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
clk_pll_prepare(struct clk_hw * hw)54*4882a593Smuzhiyun static int clk_pll_prepare(struct clk_hw *hw)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	struct clk_pll *pll = to_clk_pll(hw);
57*4882a593Smuzhiyun 	struct regmap *regmap = pll->regmap;
58*4882a593Smuzhiyun 	const struct clk_pll_layout *layout = pll->layout;
59*4882a593Smuzhiyun 	const struct clk_pll_characteristics *characteristics =
60*4882a593Smuzhiyun 							pll->characteristics;
61*4882a593Smuzhiyun 	u8 id = pll->id;
62*4882a593Smuzhiyun 	u32 mask = PLL_STATUS_MASK(id);
63*4882a593Smuzhiyun 	int offset = PLL_REG(id);
64*4882a593Smuzhiyun 	u8 out = 0;
65*4882a593Smuzhiyun 	unsigned int pllr;
66*4882a593Smuzhiyun 	unsigned int status;
67*4882a593Smuzhiyun 	u8 div;
68*4882a593Smuzhiyun 	u16 mul;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	regmap_read(regmap, offset, &pllr);
71*4882a593Smuzhiyun 	div = PLL_DIV(pllr);
72*4882a593Smuzhiyun 	mul = PLL_MUL(pllr, layout);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	regmap_read(regmap, AT91_PMC_SR, &status);
75*4882a593Smuzhiyun 	if ((status & mask) &&
76*4882a593Smuzhiyun 	    (div == pll->div && mul == pll->mul))
77*4882a593Smuzhiyun 		return 0;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	if (characteristics->out)
80*4882a593Smuzhiyun 		out = characteristics->out[pll->range];
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	if (characteristics->icpll)
83*4882a593Smuzhiyun 		regmap_update_bits(regmap, AT91_PMC_PLLICPR, PLL_ICPR_MASK(id),
84*4882a593Smuzhiyun 			characteristics->icpll[pll->range] << PLL_ICPR_SHIFT(id));
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	regmap_update_bits(regmap, offset, layout->pllr_mask,
87*4882a593Smuzhiyun 			pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
88*4882a593Smuzhiyun 			(out << PLL_OUT_SHIFT) |
89*4882a593Smuzhiyun 			((pll->mul & layout->mul_mask) << layout->mul_shift));
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	while (!clk_pll_ready(regmap, pll->id))
92*4882a593Smuzhiyun 		cpu_relax();
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	return 0;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
clk_pll_is_prepared(struct clk_hw * hw)97*4882a593Smuzhiyun static int clk_pll_is_prepared(struct clk_hw *hw)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	struct clk_pll *pll = to_clk_pll(hw);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	return clk_pll_ready(pll->regmap, pll->id);
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
clk_pll_unprepare(struct clk_hw * hw)104*4882a593Smuzhiyun static void clk_pll_unprepare(struct clk_hw *hw)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	struct clk_pll *pll = to_clk_pll(hw);
107*4882a593Smuzhiyun 	unsigned int mask = pll->layout->pllr_mask;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	regmap_update_bits(pll->regmap, PLL_REG(pll->id), mask, ~mask);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
clk_pll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)112*4882a593Smuzhiyun static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
113*4882a593Smuzhiyun 					 unsigned long parent_rate)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	struct clk_pll *pll = to_clk_pll(hw);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	if (!pll->div || !pll->mul)
118*4882a593Smuzhiyun 		return 0;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	return (parent_rate / pll->div) * (pll->mul + 1);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
clk_pll_get_best_div_mul(struct clk_pll * pll,unsigned long rate,unsigned long parent_rate,u32 * div,u32 * mul,u32 * index)123*4882a593Smuzhiyun static long clk_pll_get_best_div_mul(struct clk_pll *pll, unsigned long rate,
124*4882a593Smuzhiyun 				     unsigned long parent_rate,
125*4882a593Smuzhiyun 				     u32 *div, u32 *mul,
126*4882a593Smuzhiyun 				     u32 *index) {
127*4882a593Smuzhiyun 	const struct clk_pll_layout *layout = pll->layout;
128*4882a593Smuzhiyun 	const struct clk_pll_characteristics *characteristics =
129*4882a593Smuzhiyun 							pll->characteristics;
130*4882a593Smuzhiyun 	unsigned long bestremainder = ULONG_MAX;
131*4882a593Smuzhiyun 	unsigned long maxdiv, mindiv, tmpdiv;
132*4882a593Smuzhiyun 	long bestrate = -ERANGE;
133*4882a593Smuzhiyun 	unsigned long bestdiv;
134*4882a593Smuzhiyun 	unsigned long bestmul;
135*4882a593Smuzhiyun 	int i = 0;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	/* Check if parent_rate is a valid input rate */
138*4882a593Smuzhiyun 	if (parent_rate < characteristics->input.min)
139*4882a593Smuzhiyun 		return -ERANGE;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	/*
142*4882a593Smuzhiyun 	 * Calculate minimum divider based on the minimum multiplier, the
143*4882a593Smuzhiyun 	 * parent_rate and the requested rate.
144*4882a593Smuzhiyun 	 * Should always be 2 according to the input and output characteristics
145*4882a593Smuzhiyun 	 * of the PLL blocks.
146*4882a593Smuzhiyun 	 */
147*4882a593Smuzhiyun 	mindiv = (parent_rate * PLL_MUL_MIN) / rate;
148*4882a593Smuzhiyun 	if (!mindiv)
149*4882a593Smuzhiyun 		mindiv = 1;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	if (parent_rate > characteristics->input.max) {
152*4882a593Smuzhiyun 		tmpdiv = DIV_ROUND_UP(parent_rate, characteristics->input.max);
153*4882a593Smuzhiyun 		if (tmpdiv > PLL_DIV_MAX)
154*4882a593Smuzhiyun 			return -ERANGE;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 		if (tmpdiv > mindiv)
157*4882a593Smuzhiyun 			mindiv = tmpdiv;
158*4882a593Smuzhiyun 	}
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/*
161*4882a593Smuzhiyun 	 * Calculate the maximum divider which is limited by PLL register
162*4882a593Smuzhiyun 	 * layout (limited by the MUL or DIV field size).
163*4882a593Smuzhiyun 	 */
164*4882a593Smuzhiyun 	maxdiv = DIV_ROUND_UP(parent_rate * PLL_MUL_MAX(layout), rate);
165*4882a593Smuzhiyun 	if (maxdiv > PLL_DIV_MAX)
166*4882a593Smuzhiyun 		maxdiv = PLL_DIV_MAX;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	/*
169*4882a593Smuzhiyun 	 * Iterate over the acceptable divider values to find the best
170*4882a593Smuzhiyun 	 * divider/multiplier pair (the one that generates the closest
171*4882a593Smuzhiyun 	 * rate to the requested one).
172*4882a593Smuzhiyun 	 */
173*4882a593Smuzhiyun 	for (tmpdiv = mindiv; tmpdiv <= maxdiv; tmpdiv++) {
174*4882a593Smuzhiyun 		unsigned long remainder;
175*4882a593Smuzhiyun 		unsigned long tmprate;
176*4882a593Smuzhiyun 		unsigned long tmpmul;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 		/*
179*4882a593Smuzhiyun 		 * Calculate the multiplier associated with the current
180*4882a593Smuzhiyun 		 * divider that provide the closest rate to the requested one.
181*4882a593Smuzhiyun 		 */
182*4882a593Smuzhiyun 		tmpmul = DIV_ROUND_CLOSEST(rate, parent_rate / tmpdiv);
183*4882a593Smuzhiyun 		tmprate = (parent_rate / tmpdiv) * tmpmul;
184*4882a593Smuzhiyun 		if (tmprate > rate)
185*4882a593Smuzhiyun 			remainder = tmprate - rate;
186*4882a593Smuzhiyun 		else
187*4882a593Smuzhiyun 			remainder = rate - tmprate;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 		/*
190*4882a593Smuzhiyun 		 * Compare the remainder with the best remainder found until
191*4882a593Smuzhiyun 		 * now and elect a new best multiplier/divider pair if the
192*4882a593Smuzhiyun 		 * current remainder is smaller than the best one.
193*4882a593Smuzhiyun 		 */
194*4882a593Smuzhiyun 		if (remainder < bestremainder) {
195*4882a593Smuzhiyun 			bestremainder = remainder;
196*4882a593Smuzhiyun 			bestdiv = tmpdiv;
197*4882a593Smuzhiyun 			bestmul = tmpmul;
198*4882a593Smuzhiyun 			bestrate = tmprate;
199*4882a593Smuzhiyun 		}
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 		/*
202*4882a593Smuzhiyun 		 * We've found a perfect match!
203*4882a593Smuzhiyun 		 * Stop searching now and use this multiplier/divider pair.
204*4882a593Smuzhiyun 		 */
205*4882a593Smuzhiyun 		if (!remainder)
206*4882a593Smuzhiyun 			break;
207*4882a593Smuzhiyun 	}
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	/* We haven't found any multiplier/divider pair => return -ERANGE */
210*4882a593Smuzhiyun 	if (bestrate < 0)
211*4882a593Smuzhiyun 		return bestrate;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	/* Check if bestrate is a valid output rate  */
214*4882a593Smuzhiyun 	for (i = 0; i < characteristics->num_output; i++) {
215*4882a593Smuzhiyun 		if (bestrate >= characteristics->output[i].min &&
216*4882a593Smuzhiyun 		    bestrate <= characteristics->output[i].max)
217*4882a593Smuzhiyun 			break;
218*4882a593Smuzhiyun 	}
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	if (i >= characteristics->num_output)
221*4882a593Smuzhiyun 		return -ERANGE;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	if (div)
224*4882a593Smuzhiyun 		*div = bestdiv;
225*4882a593Smuzhiyun 	if (mul)
226*4882a593Smuzhiyun 		*mul = bestmul - 1;
227*4882a593Smuzhiyun 	if (index)
228*4882a593Smuzhiyun 		*index = i;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	return bestrate;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun 
clk_pll_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)233*4882a593Smuzhiyun static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
234*4882a593Smuzhiyun 					unsigned long *parent_rate)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun 	struct clk_pll *pll = to_clk_pll(hw);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	return clk_pll_get_best_div_mul(pll, rate, *parent_rate,
239*4882a593Smuzhiyun 					NULL, NULL, NULL);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
clk_pll_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)242*4882a593Smuzhiyun static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
243*4882a593Smuzhiyun 			    unsigned long parent_rate)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	struct clk_pll *pll = to_clk_pll(hw);
246*4882a593Smuzhiyun 	long ret;
247*4882a593Smuzhiyun 	u32 div;
248*4882a593Smuzhiyun 	u32 mul;
249*4882a593Smuzhiyun 	u32 index;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	ret = clk_pll_get_best_div_mul(pll, rate, parent_rate,
252*4882a593Smuzhiyun 				       &div, &mul, &index);
253*4882a593Smuzhiyun 	if (ret < 0)
254*4882a593Smuzhiyun 		return ret;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	pll->range = index;
257*4882a593Smuzhiyun 	pll->div = div;
258*4882a593Smuzhiyun 	pll->mul = mul;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	return 0;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun static const struct clk_ops pll_ops = {
264*4882a593Smuzhiyun 	.prepare = clk_pll_prepare,
265*4882a593Smuzhiyun 	.unprepare = clk_pll_unprepare,
266*4882a593Smuzhiyun 	.is_prepared = clk_pll_is_prepared,
267*4882a593Smuzhiyun 	.recalc_rate = clk_pll_recalc_rate,
268*4882a593Smuzhiyun 	.round_rate = clk_pll_round_rate,
269*4882a593Smuzhiyun 	.set_rate = clk_pll_set_rate,
270*4882a593Smuzhiyun };
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun struct clk_hw * __init
at91_clk_register_pll(struct regmap * regmap,const char * name,const char * parent_name,u8 id,const struct clk_pll_layout * layout,const struct clk_pll_characteristics * characteristics)273*4882a593Smuzhiyun at91_clk_register_pll(struct regmap *regmap, const char *name,
274*4882a593Smuzhiyun 		      const char *parent_name, u8 id,
275*4882a593Smuzhiyun 		      const struct clk_pll_layout *layout,
276*4882a593Smuzhiyun 		      const struct clk_pll_characteristics *characteristics)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	struct clk_pll *pll;
279*4882a593Smuzhiyun 	struct clk_hw *hw;
280*4882a593Smuzhiyun 	struct clk_init_data init;
281*4882a593Smuzhiyun 	int offset = PLL_REG(id);
282*4882a593Smuzhiyun 	unsigned int pllr;
283*4882a593Smuzhiyun 	int ret;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	if (id > PLL_MAX_ID)
286*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	pll = kzalloc(sizeof(*pll), GFP_KERNEL);
289*4882a593Smuzhiyun 	if (!pll)
290*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	init.name = name;
293*4882a593Smuzhiyun 	init.ops = &pll_ops;
294*4882a593Smuzhiyun 	init.parent_names = &parent_name;
295*4882a593Smuzhiyun 	init.num_parents = 1;
296*4882a593Smuzhiyun 	init.flags = CLK_SET_RATE_GATE;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	pll->id = id;
299*4882a593Smuzhiyun 	pll->hw.init = &init;
300*4882a593Smuzhiyun 	pll->layout = layout;
301*4882a593Smuzhiyun 	pll->characteristics = characteristics;
302*4882a593Smuzhiyun 	pll->regmap = regmap;
303*4882a593Smuzhiyun 	regmap_read(regmap, offset, &pllr);
304*4882a593Smuzhiyun 	pll->div = PLL_DIV(pllr);
305*4882a593Smuzhiyun 	pll->mul = PLL_MUL(pllr, layout);
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	hw = &pll->hw;
308*4882a593Smuzhiyun 	ret = clk_hw_register(NULL, &pll->hw);
309*4882a593Smuzhiyun 	if (ret) {
310*4882a593Smuzhiyun 		kfree(pll);
311*4882a593Smuzhiyun 		hw = ERR_PTR(ret);
312*4882a593Smuzhiyun 	}
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	return hw;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun const struct clk_pll_layout at91rm9200_pll_layout = {
319*4882a593Smuzhiyun 	.pllr_mask = 0x7FFFFFF,
320*4882a593Smuzhiyun 	.mul_shift = 16,
321*4882a593Smuzhiyun 	.mul_mask = 0x7FF,
322*4882a593Smuzhiyun };
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun const struct clk_pll_layout at91sam9g45_pll_layout = {
325*4882a593Smuzhiyun 	.pllr_mask = 0xFFFFFF,
326*4882a593Smuzhiyun 	.mul_shift = 16,
327*4882a593Smuzhiyun 	.mul_mask = 0xFF,
328*4882a593Smuzhiyun };
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun const struct clk_pll_layout at91sam9g20_pllb_layout = {
331*4882a593Smuzhiyun 	.pllr_mask = 0x3FFFFF,
332*4882a593Smuzhiyun 	.mul_shift = 16,
333*4882a593Smuzhiyun 	.mul_mask = 0x3F,
334*4882a593Smuzhiyun };
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun const struct clk_pll_layout sama5d3_pll_layout = {
337*4882a593Smuzhiyun 	.pllr_mask = 0x1FFFFFF,
338*4882a593Smuzhiyun 	.mul_shift = 18,
339*4882a593Smuzhiyun 	.mul_mask = 0x7F,
340*4882a593Smuzhiyun };
341