xref: /openbmc/linux/drivers/clk/ti/fapll.c (revision 52e6676e)
1*52e6676eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2163152cbSTony Lindgren 
31b29e601SStephen Boyd #include <linux/clk.h>
4163152cbSTony Lindgren #include <linux/clk-provider.h>
5163152cbSTony Lindgren #include <linux/delay.h>
6163152cbSTony Lindgren #include <linux/err.h>
762e59c4eSStephen Boyd #include <linux/io.h>
8cafeb002STony Lindgren #include <linux/math64.h>
9163152cbSTony Lindgren #include <linux/of.h>
10163152cbSTony Lindgren #include <linux/of_address.h>
11163152cbSTony Lindgren #include <linux/clk/ti.h>
12163152cbSTony Lindgren 
139e56a7d4STony Lindgren #include "clock.h"
149e56a7d4STony Lindgren 
15163152cbSTony Lindgren /* FAPLL Control Register PLL_CTRL */
169089848dSTony Lindgren #define FAPLL_MAIN_MULT_N_SHIFT	16
179089848dSTony Lindgren #define FAPLL_MAIN_DIV_P_SHIFT	8
18163152cbSTony Lindgren #define FAPLL_MAIN_LOCK		BIT(7)
19163152cbSTony Lindgren #define FAPLL_MAIN_PLLEN	BIT(3)
20163152cbSTony Lindgren #define FAPLL_MAIN_BP		BIT(2)
21163152cbSTony Lindgren #define FAPLL_MAIN_LOC_CTL	BIT(0)
22163152cbSTony Lindgren 
239089848dSTony Lindgren #define FAPLL_MAIN_MAX_MULT_N	0xffff
249089848dSTony Lindgren #define FAPLL_MAIN_MAX_DIV_P	0xff
259089848dSTony Lindgren #define FAPLL_MAIN_CLEAR_MASK	\
269089848dSTony Lindgren 	((FAPLL_MAIN_MAX_MULT_N << FAPLL_MAIN_MULT_N_SHIFT) | \
279089848dSTony Lindgren 	 (FAPLL_MAIN_DIV_P_SHIFT << FAPLL_MAIN_DIV_P_SHIFT) | \
289089848dSTony Lindgren 	 FAPLL_MAIN_LOC_CTL)
299089848dSTony Lindgren 
30163152cbSTony Lindgren /* FAPLL powerdown register PWD */
31163152cbSTony Lindgren #define FAPLL_PWD_OFFSET	4
32163152cbSTony Lindgren 
33163152cbSTony Lindgren #define MAX_FAPLL_OUTPUTS	7
34163152cbSTony Lindgren #define FAPLL_MAX_RETRIES	1000
35163152cbSTony Lindgren 
36163152cbSTony Lindgren #define to_fapll(_hw)		container_of(_hw, struct fapll_data, hw)
37163152cbSTony Lindgren #define to_synth(_hw)		container_of(_hw, struct fapll_synth, hw)
38163152cbSTony Lindgren 
39163152cbSTony Lindgren /* The bypass bit is inverted on the ddr_pll.. */
40163152cbSTony Lindgren #define fapll_is_ddr_pll(va)	(((u32)(va) & 0xffff) == 0x0440)
41163152cbSTony Lindgren 
42163152cbSTony Lindgren /*
43163152cbSTony Lindgren  * The audio_pll_clk1 input is hard wired to the 27MHz bypass clock,
44163152cbSTony Lindgren  * and the audio_pll_clk1 synthesizer is hardwared to 32KiHz output.
45163152cbSTony Lindgren  */
46163152cbSTony Lindgren #define is_ddr_pll_clk1(va)	(((u32)(va) & 0xffff) == 0x044c)
47163152cbSTony Lindgren #define is_audio_pll_clk1(va)	(((u32)(va) & 0xffff) == 0x04a8)
48163152cbSTony Lindgren 
49163152cbSTony Lindgren /* Synthesizer divider register */
50163152cbSTony Lindgren #define SYNTH_LDMDIV1		BIT(8)
51163152cbSTony Lindgren 
52163152cbSTony Lindgren /* Synthesizer frequency register */
53163152cbSTony Lindgren #define SYNTH_LDFREQ		BIT(31)
54163152cbSTony Lindgren 
55cafeb002STony Lindgren #define SYNTH_PHASE_K		8
56cafeb002STony Lindgren #define SYNTH_MAX_INT_DIV	0xf
5733ca29c9STony Lindgren #define SYNTH_MAX_DIV_M		0xff
5833ca29c9STony Lindgren 
59163152cbSTony Lindgren struct fapll_data {
60163152cbSTony Lindgren 	struct clk_hw hw;
61163152cbSTony Lindgren 	void __iomem *base;
62163152cbSTony Lindgren 	const char *name;
63163152cbSTony Lindgren 	struct clk *clk_ref;
64163152cbSTony Lindgren 	struct clk *clk_bypass;
65163152cbSTony Lindgren 	struct clk_onecell_data outputs;
66163152cbSTony Lindgren 	bool bypass_bit_inverted;
67163152cbSTony Lindgren };
68163152cbSTony Lindgren 
69163152cbSTony Lindgren struct fapll_synth {
70163152cbSTony Lindgren 	struct clk_hw hw;
71163152cbSTony Lindgren 	struct fapll_data *fd;
72163152cbSTony Lindgren 	int index;
73163152cbSTony Lindgren 	void __iomem *freq;
74163152cbSTony Lindgren 	void __iomem *div;
75163152cbSTony Lindgren 	const char *name;
76163152cbSTony Lindgren 	struct clk *clk_pll;
77163152cbSTony Lindgren };
78163152cbSTony Lindgren 
ti_fapll_clock_is_bypass(struct fapll_data * fd)79163152cbSTony Lindgren static bool ti_fapll_clock_is_bypass(struct fapll_data *fd)
80163152cbSTony Lindgren {
81163152cbSTony Lindgren 	u32 v = readl_relaxed(fd->base);
82163152cbSTony Lindgren 
83163152cbSTony Lindgren 	if (fd->bypass_bit_inverted)
84163152cbSTony Lindgren 		return !(v & FAPLL_MAIN_BP);
85163152cbSTony Lindgren 	else
86163152cbSTony Lindgren 		return !!(v & FAPLL_MAIN_BP);
87163152cbSTony Lindgren }
88163152cbSTony Lindgren 
ti_fapll_set_bypass(struct fapll_data * fd)899089848dSTony Lindgren static void ti_fapll_set_bypass(struct fapll_data *fd)
909089848dSTony Lindgren {
919089848dSTony Lindgren 	u32 v = readl_relaxed(fd->base);
929089848dSTony Lindgren 
939089848dSTony Lindgren 	if (fd->bypass_bit_inverted)
949089848dSTony Lindgren 		v &= ~FAPLL_MAIN_BP;
959089848dSTony Lindgren 	else
969089848dSTony Lindgren 		v |= FAPLL_MAIN_BP;
979089848dSTony Lindgren 	writel_relaxed(v, fd->base);
989089848dSTony Lindgren }
999089848dSTony Lindgren 
ti_fapll_clear_bypass(struct fapll_data * fd)1009089848dSTony Lindgren static void ti_fapll_clear_bypass(struct fapll_data *fd)
1019089848dSTony Lindgren {
1029089848dSTony Lindgren 	u32 v = readl_relaxed(fd->base);
1039089848dSTony Lindgren 
1049089848dSTony Lindgren 	if (fd->bypass_bit_inverted)
1059089848dSTony Lindgren 		v |= FAPLL_MAIN_BP;
1069089848dSTony Lindgren 	else
1079089848dSTony Lindgren 		v &= ~FAPLL_MAIN_BP;
1089089848dSTony Lindgren 	writel_relaxed(v, fd->base);
1099089848dSTony Lindgren }
1109089848dSTony Lindgren 
ti_fapll_wait_lock(struct fapll_data * fd)1119089848dSTony Lindgren static int ti_fapll_wait_lock(struct fapll_data *fd)
1129089848dSTony Lindgren {
1139089848dSTony Lindgren 	int retries = FAPLL_MAX_RETRIES;
1149089848dSTony Lindgren 	u32 v;
1159089848dSTony Lindgren 
1169089848dSTony Lindgren 	while ((v = readl_relaxed(fd->base))) {
1179089848dSTony Lindgren 		if (v & FAPLL_MAIN_LOCK)
1189089848dSTony Lindgren 			return 0;
1199089848dSTony Lindgren 
1209089848dSTony Lindgren 		if (retries-- <= 0)
1219089848dSTony Lindgren 			break;
1229089848dSTony Lindgren 
1239089848dSTony Lindgren 		udelay(1);
1249089848dSTony Lindgren 	}
1259089848dSTony Lindgren 
1269089848dSTony Lindgren 	pr_err("%s failed to lock\n", fd->name);
1279089848dSTony Lindgren 
1289089848dSTony Lindgren 	return -ETIMEDOUT;
1299089848dSTony Lindgren }
1309089848dSTony Lindgren 
ti_fapll_enable(struct clk_hw * hw)131163152cbSTony Lindgren static int ti_fapll_enable(struct clk_hw *hw)
132163152cbSTony Lindgren {
133163152cbSTony Lindgren 	struct fapll_data *fd = to_fapll(hw);
134163152cbSTony Lindgren 	u32 v = readl_relaxed(fd->base);
135163152cbSTony Lindgren 
13603208cc6STony Lindgren 	v |= FAPLL_MAIN_PLLEN;
137163152cbSTony Lindgren 	writel_relaxed(v, fd->base);
1389089848dSTony Lindgren 	ti_fapll_wait_lock(fd);
139163152cbSTony Lindgren 
140163152cbSTony Lindgren 	return 0;
141163152cbSTony Lindgren }
142163152cbSTony Lindgren 
ti_fapll_disable(struct clk_hw * hw)143163152cbSTony Lindgren static void ti_fapll_disable(struct clk_hw *hw)
144163152cbSTony Lindgren {
145163152cbSTony Lindgren 	struct fapll_data *fd = to_fapll(hw);
146163152cbSTony Lindgren 	u32 v = readl_relaxed(fd->base);
147163152cbSTony Lindgren 
14803208cc6STony Lindgren 	v &= ~FAPLL_MAIN_PLLEN;
149163152cbSTony Lindgren 	writel_relaxed(v, fd->base);
150163152cbSTony Lindgren }
151163152cbSTony Lindgren 
ti_fapll_is_enabled(struct clk_hw * hw)152163152cbSTony Lindgren static int ti_fapll_is_enabled(struct clk_hw *hw)
153163152cbSTony Lindgren {
154163152cbSTony Lindgren 	struct fapll_data *fd = to_fapll(hw);
155163152cbSTony Lindgren 	u32 v = readl_relaxed(fd->base);
156163152cbSTony Lindgren 
15703208cc6STony Lindgren 	return v & FAPLL_MAIN_PLLEN;
158163152cbSTony Lindgren }
159163152cbSTony Lindgren 
ti_fapll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)160163152cbSTony Lindgren static unsigned long ti_fapll_recalc_rate(struct clk_hw *hw,
161163152cbSTony Lindgren 					  unsigned long parent_rate)
162163152cbSTony Lindgren {
163163152cbSTony Lindgren 	struct fapll_data *fd = to_fapll(hw);
164163152cbSTony Lindgren 	u32 fapll_n, fapll_p, v;
165c51185b4SNicolas Pitre 	u64 rate;
166163152cbSTony Lindgren 
167163152cbSTony Lindgren 	if (ti_fapll_clock_is_bypass(fd))
168163152cbSTony Lindgren 		return parent_rate;
169163152cbSTony Lindgren 
170163152cbSTony Lindgren 	rate = parent_rate;
171163152cbSTony Lindgren 
172163152cbSTony Lindgren 	/* PLL pre-divider is P and multiplier is N */
173163152cbSTony Lindgren 	v = readl_relaxed(fd->base);
174163152cbSTony Lindgren 	fapll_p = (v >> 8) & 0xff;
175163152cbSTony Lindgren 	if (fapll_p)
176163152cbSTony Lindgren 		do_div(rate, fapll_p);
177163152cbSTony Lindgren 	fapll_n = v >> 16;
178163152cbSTony Lindgren 	if (fapll_n)
179163152cbSTony Lindgren 		rate *= fapll_n;
180163152cbSTony Lindgren 
181163152cbSTony Lindgren 	return rate;
182163152cbSTony Lindgren }
183163152cbSTony Lindgren 
ti_fapll_get_parent(struct clk_hw * hw)184163152cbSTony Lindgren static u8 ti_fapll_get_parent(struct clk_hw *hw)
185163152cbSTony Lindgren {
186163152cbSTony Lindgren 	struct fapll_data *fd = to_fapll(hw);
187163152cbSTony Lindgren 
188163152cbSTony Lindgren 	if (ti_fapll_clock_is_bypass(fd))
189163152cbSTony Lindgren 		return 1;
190163152cbSTony Lindgren 
191163152cbSTony Lindgren 	return 0;
192163152cbSTony Lindgren }
193163152cbSTony Lindgren 
ti_fapll_set_div_mult(unsigned long rate,unsigned long parent_rate,u32 * pre_div_p,u32 * mult_n)1949089848dSTony Lindgren static int ti_fapll_set_div_mult(unsigned long rate,
1959089848dSTony Lindgren 				 unsigned long parent_rate,
1969089848dSTony Lindgren 				 u32 *pre_div_p, u32 *mult_n)
1979089848dSTony Lindgren {
1989089848dSTony Lindgren 	/*
1999089848dSTony Lindgren 	 * So far no luck getting decent clock with PLL divider,
2009089848dSTony Lindgren 	 * PLL does not seem to lock and the signal does not look
2019089848dSTony Lindgren 	 * right. It seems the divider can only be used together
2029089848dSTony Lindgren 	 * with the multiplier?
2039089848dSTony Lindgren 	 */
2049089848dSTony Lindgren 	if (rate < parent_rate) {
2059089848dSTony Lindgren 		pr_warn("FAPLL main divider rates unsupported\n");
2069089848dSTony Lindgren 		return -EINVAL;
2079089848dSTony Lindgren 	}
2089089848dSTony Lindgren 
2099089848dSTony Lindgren 	*mult_n = rate / parent_rate;
2109089848dSTony Lindgren 	if (*mult_n > FAPLL_MAIN_MAX_MULT_N)
2119089848dSTony Lindgren 		return -EINVAL;
2129089848dSTony Lindgren 	*pre_div_p = 1;
2139089848dSTony Lindgren 
2149089848dSTony Lindgren 	return 0;
2159089848dSTony Lindgren }
2169089848dSTony Lindgren 
ti_fapll_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)2179089848dSTony Lindgren static long ti_fapll_round_rate(struct clk_hw *hw, unsigned long rate,
2189089848dSTony Lindgren 				unsigned long *parent_rate)
2199089848dSTony Lindgren {
2209089848dSTony Lindgren 	u32 pre_div_p, mult_n;
2219089848dSTony Lindgren 	int error;
2229089848dSTony Lindgren 
2239089848dSTony Lindgren 	if (!rate)
2249089848dSTony Lindgren 		return -EINVAL;
2259089848dSTony Lindgren 
2269089848dSTony Lindgren 	error = ti_fapll_set_div_mult(rate, *parent_rate,
2279089848dSTony Lindgren 				      &pre_div_p, &mult_n);
2289089848dSTony Lindgren 	if (error)
2299089848dSTony Lindgren 		return error;
2309089848dSTony Lindgren 
2319089848dSTony Lindgren 	rate = *parent_rate / pre_div_p;
2329089848dSTony Lindgren 	rate *= mult_n;
2339089848dSTony Lindgren 
2349089848dSTony Lindgren 	return rate;
2359089848dSTony Lindgren }
2369089848dSTony Lindgren 
ti_fapll_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)2379089848dSTony Lindgren static int ti_fapll_set_rate(struct clk_hw *hw, unsigned long rate,
2389089848dSTony Lindgren 			     unsigned long parent_rate)
2399089848dSTony Lindgren {
2409089848dSTony Lindgren 	struct fapll_data *fd = to_fapll(hw);
2419089848dSTony Lindgren 	u32 pre_div_p, mult_n, v;
2429089848dSTony Lindgren 	int error;
2439089848dSTony Lindgren 
2449089848dSTony Lindgren 	if (!rate)
2459089848dSTony Lindgren 		return -EINVAL;
2469089848dSTony Lindgren 
2479089848dSTony Lindgren 	error = ti_fapll_set_div_mult(rate, parent_rate,
2489089848dSTony Lindgren 				      &pre_div_p, &mult_n);
2499089848dSTony Lindgren 	if (error)
2509089848dSTony Lindgren 		return error;
2519089848dSTony Lindgren 
2529089848dSTony Lindgren 	ti_fapll_set_bypass(fd);
2539089848dSTony Lindgren 	v = readl_relaxed(fd->base);
2549089848dSTony Lindgren 	v &= ~FAPLL_MAIN_CLEAR_MASK;
2559089848dSTony Lindgren 	v |= pre_div_p << FAPLL_MAIN_DIV_P_SHIFT;
2569089848dSTony Lindgren 	v |= mult_n << FAPLL_MAIN_MULT_N_SHIFT;
2579089848dSTony Lindgren 	writel_relaxed(v, fd->base);
2589089848dSTony Lindgren 	if (ti_fapll_is_enabled(hw))
2599089848dSTony Lindgren 		ti_fapll_wait_lock(fd);
2609089848dSTony Lindgren 	ti_fapll_clear_bypass(fd);
2619089848dSTony Lindgren 
2629089848dSTony Lindgren 	return 0;
2639089848dSTony Lindgren }
2649089848dSTony Lindgren 
2657cc566a8SBhumika Goyal static const struct clk_ops ti_fapll_ops = {
266163152cbSTony Lindgren 	.enable = ti_fapll_enable,
267163152cbSTony Lindgren 	.disable = ti_fapll_disable,
268163152cbSTony Lindgren 	.is_enabled = ti_fapll_is_enabled,
269163152cbSTony Lindgren 	.recalc_rate = ti_fapll_recalc_rate,
270163152cbSTony Lindgren 	.get_parent = ti_fapll_get_parent,
2719089848dSTony Lindgren 	.round_rate = ti_fapll_round_rate,
2729089848dSTony Lindgren 	.set_rate = ti_fapll_set_rate,
273163152cbSTony Lindgren };
274163152cbSTony Lindgren 
ti_fapll_synth_enable(struct clk_hw * hw)275163152cbSTony Lindgren static int ti_fapll_synth_enable(struct clk_hw *hw)
276163152cbSTony Lindgren {
277163152cbSTony Lindgren 	struct fapll_synth *synth = to_synth(hw);
278163152cbSTony Lindgren 	u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
279163152cbSTony Lindgren 
280163152cbSTony Lindgren 	v &= ~(1 << synth->index);
281163152cbSTony Lindgren 	writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET);
282163152cbSTony Lindgren 
283163152cbSTony Lindgren 	return 0;
284163152cbSTony Lindgren }
285163152cbSTony Lindgren 
ti_fapll_synth_disable(struct clk_hw * hw)286163152cbSTony Lindgren static void ti_fapll_synth_disable(struct clk_hw *hw)
287163152cbSTony Lindgren {
288163152cbSTony Lindgren 	struct fapll_synth *synth = to_synth(hw);
289163152cbSTony Lindgren 	u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
290163152cbSTony Lindgren 
291163152cbSTony Lindgren 	v |= 1 << synth->index;
292163152cbSTony Lindgren 	writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET);
293163152cbSTony Lindgren }
294163152cbSTony Lindgren 
ti_fapll_synth_is_enabled(struct clk_hw * hw)295163152cbSTony Lindgren static int ti_fapll_synth_is_enabled(struct clk_hw *hw)
296163152cbSTony Lindgren {
297163152cbSTony Lindgren 	struct fapll_synth *synth = to_synth(hw);
298163152cbSTony Lindgren 	u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
299163152cbSTony Lindgren 
300163152cbSTony Lindgren 	return !(v & (1 << synth->index));
301163152cbSTony Lindgren }
302163152cbSTony Lindgren 
303163152cbSTony Lindgren /*
304163152cbSTony Lindgren  * See dm816x TRM chapter 1.10.3 Flying Adder PLL fore more info
305163152cbSTony Lindgren  */
ti_fapll_synth_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)306163152cbSTony Lindgren static unsigned long ti_fapll_synth_recalc_rate(struct clk_hw *hw,
307163152cbSTony Lindgren 						unsigned long parent_rate)
308163152cbSTony Lindgren {
309163152cbSTony Lindgren 	struct fapll_synth *synth = to_synth(hw);
310163152cbSTony Lindgren 	u32 synth_div_m;
311c51185b4SNicolas Pitre 	u64 rate;
312163152cbSTony Lindgren 
313163152cbSTony Lindgren 	/* The audio_pll_clk1 is hardwired to produce 32.768KiHz clock */
314163152cbSTony Lindgren 	if (!synth->div)
315163152cbSTony Lindgren 		return 32768;
316163152cbSTony Lindgren 
317163152cbSTony Lindgren 	/*
318163152cbSTony Lindgren 	 * PLL in bypass sets the synths in bypass mode too. The PLL rate
319163152cbSTony Lindgren 	 * can be also be set to 27MHz, so we can't use parent_rate to
320163152cbSTony Lindgren 	 * check for bypass mode.
321163152cbSTony Lindgren 	 */
322163152cbSTony Lindgren 	if (ti_fapll_clock_is_bypass(synth->fd))
323163152cbSTony Lindgren 		return parent_rate;
324163152cbSTony Lindgren 
325163152cbSTony Lindgren 	rate = parent_rate;
326163152cbSTony Lindgren 
327163152cbSTony Lindgren 	/*
328163152cbSTony Lindgren 	 * Synth frequency integer and fractional divider.
329163152cbSTony Lindgren 	 * Note that the phase output K is 8, so the result needs
330cafeb002STony Lindgren 	 * to be multiplied by SYNTH_PHASE_K.
331163152cbSTony Lindgren 	 */
332163152cbSTony Lindgren 	if (synth->freq) {
333163152cbSTony Lindgren 		u32 v, synth_int_div, synth_frac_div, synth_div_freq;
334163152cbSTony Lindgren 
335163152cbSTony Lindgren 		v = readl_relaxed(synth->freq);
336163152cbSTony Lindgren 		synth_int_div = (v >> 24) & 0xf;
337163152cbSTony Lindgren 		synth_frac_div = v & 0xffffff;
338163152cbSTony Lindgren 		synth_div_freq = (synth_int_div * 10000000) + synth_frac_div;
339163152cbSTony Lindgren 		rate *= 10000000;
340163152cbSTony Lindgren 		do_div(rate, synth_div_freq);
341cafeb002STony Lindgren 		rate *= SYNTH_PHASE_K;
342163152cbSTony Lindgren 	}
343163152cbSTony Lindgren 
34433ca29c9STony Lindgren 	/* Synth post-divider M */
34533ca29c9STony Lindgren 	synth_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M;
346163152cbSTony Lindgren 
34733ca29c9STony Lindgren 	return DIV_ROUND_UP_ULL(rate, synth_div_m);
348163152cbSTony Lindgren }
349163152cbSTony Lindgren 
ti_fapll_synth_get_frac_rate(struct clk_hw * hw,unsigned long parent_rate)350cafeb002STony Lindgren static unsigned long ti_fapll_synth_get_frac_rate(struct clk_hw *hw,
351cafeb002STony Lindgren 						  unsigned long parent_rate)
352cafeb002STony Lindgren {
353cafeb002STony Lindgren 	struct fapll_synth *synth = to_synth(hw);
354cafeb002STony Lindgren 	unsigned long current_rate, frac_rate;
355cafeb002STony Lindgren 	u32 post_div_m;
356cafeb002STony Lindgren 
357cafeb002STony Lindgren 	current_rate = ti_fapll_synth_recalc_rate(hw, parent_rate);
358cafeb002STony Lindgren 	post_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M;
359cafeb002STony Lindgren 	frac_rate = current_rate * post_div_m;
360cafeb002STony Lindgren 
361cafeb002STony Lindgren 	return frac_rate;
362cafeb002STony Lindgren }
363cafeb002STony Lindgren 
ti_fapll_synth_set_frac_rate(struct fapll_synth * synth,unsigned long rate,unsigned long parent_rate)364cafeb002STony Lindgren static u32 ti_fapll_synth_set_frac_rate(struct fapll_synth *synth,
365cafeb002STony Lindgren 					unsigned long rate,
366cafeb002STony Lindgren 					unsigned long parent_rate)
367cafeb002STony Lindgren {
368cafeb002STony Lindgren 	u32 post_div_m, synth_int_div = 0, synth_frac_div = 0, v;
369cafeb002STony Lindgren 
370cafeb002STony Lindgren 	post_div_m = DIV_ROUND_UP_ULL((u64)parent_rate * SYNTH_PHASE_K, rate);
371cafeb002STony Lindgren 	post_div_m = post_div_m / SYNTH_MAX_INT_DIV;
372cafeb002STony Lindgren 	if (post_div_m > SYNTH_MAX_DIV_M)
373cafeb002STony Lindgren 		return -EINVAL;
374cafeb002STony Lindgren 	if (!post_div_m)
375cafeb002STony Lindgren 		post_div_m = 1;
376cafeb002STony Lindgren 
377cafeb002STony Lindgren 	for (; post_div_m < SYNTH_MAX_DIV_M; post_div_m++) {
378cafeb002STony Lindgren 		synth_int_div = DIV_ROUND_UP_ULL((u64)parent_rate *
379cafeb002STony Lindgren 						 SYNTH_PHASE_K *
380cafeb002STony Lindgren 						 10000000,
381cafeb002STony Lindgren 						 rate * post_div_m);
382cafeb002STony Lindgren 		synth_frac_div = synth_int_div % 10000000;
383cafeb002STony Lindgren 		synth_int_div /= 10000000;
384cafeb002STony Lindgren 
385cafeb002STony Lindgren 		if (synth_int_div <= SYNTH_MAX_INT_DIV)
386cafeb002STony Lindgren 			break;
387cafeb002STony Lindgren 	}
388cafeb002STony Lindgren 
389cafeb002STony Lindgren 	if (synth_int_div > SYNTH_MAX_INT_DIV)
390cafeb002STony Lindgren 		return -EINVAL;
391cafeb002STony Lindgren 
392cafeb002STony Lindgren 	v = readl_relaxed(synth->freq);
393cafeb002STony Lindgren 	v &= ~0x1fffffff;
394cafeb002STony Lindgren 	v |= (synth_int_div & SYNTH_MAX_INT_DIV) << 24;
395cafeb002STony Lindgren 	v |= (synth_frac_div & 0xffffff);
396cafeb002STony Lindgren 	v |= SYNTH_LDFREQ;
397cafeb002STony Lindgren 	writel_relaxed(v, synth->freq);
398cafeb002STony Lindgren 
399cafeb002STony Lindgren 	return post_div_m;
400cafeb002STony Lindgren }
401cafeb002STony Lindgren 
ti_fapll_synth_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)402cafeb002STony Lindgren static long ti_fapll_synth_round_rate(struct clk_hw *hw, unsigned long rate,
403cafeb002STony Lindgren 				      unsigned long *parent_rate)
404cafeb002STony Lindgren {
405cafeb002STony Lindgren 	struct fapll_synth *synth = to_synth(hw);
406cafeb002STony Lindgren 	struct fapll_data *fd = synth->fd;
407cafeb002STony Lindgren 	unsigned long r;
408cafeb002STony Lindgren 
409cafeb002STony Lindgren 	if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate)
410cafeb002STony Lindgren 		return -EINVAL;
411cafeb002STony Lindgren 
412cafeb002STony Lindgren 	/* Only post divider m available with no fractional divider? */
413cafeb002STony Lindgren 	if (!synth->freq) {
414cafeb002STony Lindgren 		unsigned long frac_rate;
415cafeb002STony Lindgren 		u32 synth_post_div_m;
416cafeb002STony Lindgren 
417cafeb002STony Lindgren 		frac_rate = ti_fapll_synth_get_frac_rate(hw, *parent_rate);
418cafeb002STony Lindgren 		synth_post_div_m = DIV_ROUND_UP(frac_rate, rate);
419cafeb002STony Lindgren 		r = DIV_ROUND_UP(frac_rate, synth_post_div_m);
420cafeb002STony Lindgren 		goto out;
421cafeb002STony Lindgren 	}
422cafeb002STony Lindgren 
423cafeb002STony Lindgren 	r = *parent_rate * SYNTH_PHASE_K;
424cafeb002STony Lindgren 	if (rate > r)
425cafeb002STony Lindgren 		goto out;
426cafeb002STony Lindgren 
427cafeb002STony Lindgren 	r = DIV_ROUND_UP_ULL(r, SYNTH_MAX_INT_DIV * SYNTH_MAX_DIV_M);
428cafeb002STony Lindgren 	if (rate < r)
429cafeb002STony Lindgren 		goto out;
430cafeb002STony Lindgren 
431cafeb002STony Lindgren 	r = rate;
432cafeb002STony Lindgren out:
433cafeb002STony Lindgren 	return r;
434cafeb002STony Lindgren }
435cafeb002STony Lindgren 
ti_fapll_synth_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)436cafeb002STony Lindgren static int ti_fapll_synth_set_rate(struct clk_hw *hw, unsigned long rate,
437cafeb002STony Lindgren 				   unsigned long parent_rate)
438cafeb002STony Lindgren {
439cafeb002STony Lindgren 	struct fapll_synth *synth = to_synth(hw);
440cafeb002STony Lindgren 	struct fapll_data *fd = synth->fd;
441cafeb002STony Lindgren 	unsigned long frac_rate, post_rate = 0;
442cafeb002STony Lindgren 	u32 post_div_m = 0, v;
443cafeb002STony Lindgren 
444cafeb002STony Lindgren 	if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate)
445cafeb002STony Lindgren 		return -EINVAL;
446cafeb002STony Lindgren 
447cafeb002STony Lindgren 	/* Produce the rate with just post divider M? */
448cafeb002STony Lindgren 	frac_rate = ti_fapll_synth_get_frac_rate(hw, parent_rate);
449cafeb002STony Lindgren 	if (frac_rate < rate) {
450cafeb002STony Lindgren 		if (!synth->freq)
451cafeb002STony Lindgren 			return -EINVAL;
452cafeb002STony Lindgren 	} else {
453cafeb002STony Lindgren 		post_div_m = DIV_ROUND_UP(frac_rate, rate);
454cafeb002STony Lindgren 		if (post_div_m && (post_div_m <= SYNTH_MAX_DIV_M))
455cafeb002STony Lindgren 			post_rate = DIV_ROUND_UP(frac_rate, post_div_m);
456cafeb002STony Lindgren 		if (!synth->freq && !post_rate)
457cafeb002STony Lindgren 			return -EINVAL;
458cafeb002STony Lindgren 	}
459cafeb002STony Lindgren 
460cafeb002STony Lindgren 	/* Need to recalculate the fractional divider? */
461cafeb002STony Lindgren 	if ((post_rate != rate) && synth->freq)
462cafeb002STony Lindgren 		post_div_m = ti_fapll_synth_set_frac_rate(synth,
463cafeb002STony Lindgren 							  rate,
464cafeb002STony Lindgren 							  parent_rate);
465cafeb002STony Lindgren 
466cafeb002STony Lindgren 	v = readl_relaxed(synth->div);
467cafeb002STony Lindgren 	v &= ~SYNTH_MAX_DIV_M;
468cafeb002STony Lindgren 	v |= post_div_m;
469cafeb002STony Lindgren 	v |= SYNTH_LDMDIV1;
470cafeb002STony Lindgren 	writel_relaxed(v, synth->div);
471cafeb002STony Lindgren 
472cafeb002STony Lindgren 	return 0;
473163152cbSTony Lindgren }
474163152cbSTony Lindgren 
4757cc566a8SBhumika Goyal static const struct clk_ops ti_fapll_synt_ops = {
476163152cbSTony Lindgren 	.enable = ti_fapll_synth_enable,
477163152cbSTony Lindgren 	.disable = ti_fapll_synth_disable,
478163152cbSTony Lindgren 	.is_enabled = ti_fapll_synth_is_enabled,
479163152cbSTony Lindgren 	.recalc_rate = ti_fapll_synth_recalc_rate,
480cafeb002STony Lindgren 	.round_rate = ti_fapll_synth_round_rate,
481cafeb002STony Lindgren 	.set_rate = ti_fapll_synth_set_rate,
482163152cbSTony Lindgren };
483163152cbSTony Lindgren 
ti_fapll_synth_setup(struct fapll_data * fd,void __iomem * freq,void __iomem * div,int index,const char * name,const char * parent,struct clk * pll_clk)484163152cbSTony Lindgren static struct clk * __init ti_fapll_synth_setup(struct fapll_data *fd,
485163152cbSTony Lindgren 						void __iomem *freq,
486163152cbSTony Lindgren 						void __iomem *div,
487163152cbSTony Lindgren 						int index,
488163152cbSTony Lindgren 						const char *name,
489163152cbSTony Lindgren 						const char *parent,
490163152cbSTony Lindgren 						struct clk *pll_clk)
491163152cbSTony Lindgren {
492163152cbSTony Lindgren 	struct clk_init_data *init;
493163152cbSTony Lindgren 	struct fapll_synth *synth;
4948c6239f6SZhang Qilong 	struct clk *clk = ERR_PTR(-ENOMEM);
495163152cbSTony Lindgren 
496163152cbSTony Lindgren 	init = kzalloc(sizeof(*init), GFP_KERNEL);
497163152cbSTony Lindgren 	if (!init)
498163152cbSTony Lindgren 		return ERR_PTR(-ENOMEM);
499163152cbSTony Lindgren 
500163152cbSTony Lindgren 	init->ops = &ti_fapll_synt_ops;
501163152cbSTony Lindgren 	init->name = name;
502163152cbSTony Lindgren 	init->parent_names = &parent;
503163152cbSTony Lindgren 	init->num_parents = 1;
504163152cbSTony Lindgren 
505163152cbSTony Lindgren 	synth = kzalloc(sizeof(*synth), GFP_KERNEL);
506163152cbSTony Lindgren 	if (!synth)
507163152cbSTony Lindgren 		goto free;
508163152cbSTony Lindgren 
509163152cbSTony Lindgren 	synth->fd = fd;
510163152cbSTony Lindgren 	synth->index = index;
511163152cbSTony Lindgren 	synth->freq = freq;
512163152cbSTony Lindgren 	synth->div = div;
513163152cbSTony Lindgren 	synth->name = name;
514163152cbSTony Lindgren 	synth->hw.init = init;
515163152cbSTony Lindgren 	synth->clk_pll = pll_clk;
516163152cbSTony Lindgren 
5178c6239f6SZhang Qilong 	clk = clk_register(NULL, &synth->hw);
5188c6239f6SZhang Qilong 	if (IS_ERR(clk)) {
5198c6239f6SZhang Qilong 		pr_err("failed to register clock\n");
5208c6239f6SZhang Qilong 		goto free;
5218c6239f6SZhang Qilong 	}
5228c6239f6SZhang Qilong 
5238c6239f6SZhang Qilong 	return clk;
524163152cbSTony Lindgren 
525163152cbSTony Lindgren free:
526163152cbSTony Lindgren 	kfree(synth);
527163152cbSTony Lindgren 	kfree(init);
528163152cbSTony Lindgren 
5298c6239f6SZhang Qilong 	return clk;
530163152cbSTony Lindgren }
531163152cbSTony Lindgren 
ti_fapll_setup(struct device_node * node)532163152cbSTony Lindgren static void __init ti_fapll_setup(struct device_node *node)
533163152cbSTony Lindgren {
534163152cbSTony Lindgren 	struct fapll_data *fd;
535163152cbSTony Lindgren 	struct clk_init_data *init = NULL;
536163152cbSTony Lindgren 	const char *parent_name[2];
537163152cbSTony Lindgren 	struct clk *pll_clk;
5389e56a7d4STony Lindgren 	const char *name;
539163152cbSTony Lindgren 	int i;
540163152cbSTony Lindgren 
541163152cbSTony Lindgren 	fd = kzalloc(sizeof(*fd), GFP_KERNEL);
542163152cbSTony Lindgren 	if (!fd)
543163152cbSTony Lindgren 		return;
544163152cbSTony Lindgren 
545163152cbSTony Lindgren 	fd->outputs.clks = kzalloc(sizeof(struct clk *) *
546163152cbSTony Lindgren 				   MAX_FAPLL_OUTPUTS + 1,
547163152cbSTony Lindgren 				   GFP_KERNEL);
548163152cbSTony Lindgren 	if (!fd->outputs.clks)
549163152cbSTony Lindgren 		goto free;
550163152cbSTony Lindgren 
551163152cbSTony Lindgren 	init = kzalloc(sizeof(*init), GFP_KERNEL);
552163152cbSTony Lindgren 	if (!init)
553163152cbSTony Lindgren 		goto free;
554163152cbSTony Lindgren 
555163152cbSTony Lindgren 	init->ops = &ti_fapll_ops;
5569e56a7d4STony Lindgren 	name = ti_dt_clk_name(node);
5579e56a7d4STony Lindgren 	init->name = name;
558163152cbSTony Lindgren 
559163152cbSTony Lindgren 	init->num_parents = of_clk_get_parent_count(node);
560163152cbSTony Lindgren 	if (init->num_parents != 2) {
561e665f029SRob Herring 		pr_err("%pOFn must have two parents\n", node);
562163152cbSTony Lindgren 		goto free;
563163152cbSTony Lindgren 	}
564163152cbSTony Lindgren 
5659da9e761SDinh Nguyen 	of_clk_parent_fill(node, parent_name, 2);
566163152cbSTony Lindgren 	init->parent_names = parent_name;
567163152cbSTony Lindgren 
568163152cbSTony Lindgren 	fd->clk_ref = of_clk_get(node, 0);
569163152cbSTony Lindgren 	if (IS_ERR(fd->clk_ref)) {
570e665f029SRob Herring 		pr_err("%pOFn could not get clk_ref\n", node);
571163152cbSTony Lindgren 		goto free;
572163152cbSTony Lindgren 	}
573163152cbSTony Lindgren 
574163152cbSTony Lindgren 	fd->clk_bypass = of_clk_get(node, 1);
575163152cbSTony Lindgren 	if (IS_ERR(fd->clk_bypass)) {
576e665f029SRob Herring 		pr_err("%pOFn could not get clk_bypass\n", node);
577163152cbSTony Lindgren 		goto free;
578163152cbSTony Lindgren 	}
579163152cbSTony Lindgren 
580163152cbSTony Lindgren 	fd->base = of_iomap(node, 0);
581163152cbSTony Lindgren 	if (!fd->base) {
582e665f029SRob Herring 		pr_err("%pOFn could not get IO base\n", node);
583163152cbSTony Lindgren 		goto free;
584163152cbSTony Lindgren 	}
585163152cbSTony Lindgren 
586163152cbSTony Lindgren 	if (fapll_is_ddr_pll(fd->base))
587163152cbSTony Lindgren 		fd->bypass_bit_inverted = true;
588163152cbSTony Lindgren 
5899e56a7d4STony Lindgren 	fd->name = name;
590163152cbSTony Lindgren 	fd->hw.init = init;
591163152cbSTony Lindgren 
592163152cbSTony Lindgren 	/* Register the parent PLL */
593163152cbSTony Lindgren 	pll_clk = clk_register(NULL, &fd->hw);
594163152cbSTony Lindgren 	if (IS_ERR(pll_clk))
595163152cbSTony Lindgren 		goto unmap;
596163152cbSTony Lindgren 
597163152cbSTony Lindgren 	fd->outputs.clks[0] = pll_clk;
598163152cbSTony Lindgren 	fd->outputs.clk_num++;
599163152cbSTony Lindgren 
600163152cbSTony Lindgren 	/*
601163152cbSTony Lindgren 	 * Set up the child synthesizers starting at index 1 as the
602163152cbSTony Lindgren 	 * PLL output is at index 0. We need to check the clock-indices
603163152cbSTony Lindgren 	 * for numbering in case there are holes in the synth mapping,
604163152cbSTony Lindgren 	 * and then probe the synth register to see if it has a FREQ
605163152cbSTony Lindgren 	 * register available.
606163152cbSTony Lindgren 	 */
607163152cbSTony Lindgren 	for (i = 0; i < MAX_FAPLL_OUTPUTS; i++) {
608163152cbSTony Lindgren 		const char *output_name;
609163152cbSTony Lindgren 		void __iomem *freq, *div;
610163152cbSTony Lindgren 		struct clk *synth_clk;
611163152cbSTony Lindgren 		int output_instance;
612163152cbSTony Lindgren 		u32 v;
613163152cbSTony Lindgren 
614163152cbSTony Lindgren 		if (of_property_read_string_index(node, "clock-output-names",
615163152cbSTony Lindgren 						  i, &output_name))
616163152cbSTony Lindgren 			continue;
617163152cbSTony Lindgren 
618163152cbSTony Lindgren 		if (of_property_read_u32_index(node, "clock-indices", i,
619163152cbSTony Lindgren 					       &output_instance))
620163152cbSTony Lindgren 			output_instance = i;
621163152cbSTony Lindgren 
622163152cbSTony Lindgren 		freq = fd->base + (output_instance * 8);
623163152cbSTony Lindgren 		div = freq + 4;
624163152cbSTony Lindgren 
625163152cbSTony Lindgren 		/* Check for hardwired audio_pll_clk1 */
626163152cbSTony Lindgren 		if (is_audio_pll_clk1(freq)) {
627412d6b47SStephen Boyd 			freq = NULL;
628412d6b47SStephen Boyd 			div = NULL;
629163152cbSTony Lindgren 		} else {
630163152cbSTony Lindgren 			/* Does the synthesizer have a FREQ register? */
631163152cbSTony Lindgren 			v = readl_relaxed(freq);
632163152cbSTony Lindgren 			if (!v)
633412d6b47SStephen Boyd 				freq = NULL;
634163152cbSTony Lindgren 		}
635163152cbSTony Lindgren 		synth_clk = ti_fapll_synth_setup(fd, freq, div, output_instance,
6369e56a7d4STony Lindgren 						 output_name, name, pll_clk);
637163152cbSTony Lindgren 		if (IS_ERR(synth_clk))
638163152cbSTony Lindgren 			continue;
639163152cbSTony Lindgren 
640163152cbSTony Lindgren 		fd->outputs.clks[output_instance] = synth_clk;
641163152cbSTony Lindgren 		fd->outputs.clk_num++;
642163152cbSTony Lindgren 
643163152cbSTony Lindgren 		clk_register_clkdev(synth_clk, output_name, NULL);
644163152cbSTony Lindgren 	}
645163152cbSTony Lindgren 
646163152cbSTony Lindgren 	/* Register the child synthesizers as the FAPLL outputs */
647163152cbSTony Lindgren 	of_clk_add_provider(node, of_clk_src_onecell_get, &fd->outputs);
648163152cbSTony Lindgren 	/* Add clock alias for the outputs */
649163152cbSTony Lindgren 
650163152cbSTony Lindgren 	kfree(init);
651163152cbSTony Lindgren 
652163152cbSTony Lindgren 	return;
653163152cbSTony Lindgren 
654163152cbSTony Lindgren unmap:
655163152cbSTony Lindgren 	iounmap(fd->base);
656163152cbSTony Lindgren free:
657163152cbSTony Lindgren 	if (fd->clk_bypass)
658163152cbSTony Lindgren 		clk_put(fd->clk_bypass);
659163152cbSTony Lindgren 	if (fd->clk_ref)
660163152cbSTony Lindgren 		clk_put(fd->clk_ref);
661163152cbSTony Lindgren 	kfree(fd->outputs.clks);
662163152cbSTony Lindgren 	kfree(fd);
663163152cbSTony Lindgren 	kfree(init);
664163152cbSTony Lindgren }
665163152cbSTony Lindgren 
666163152cbSTony Lindgren CLK_OF_DECLARE(ti_fapll_clock, "ti,dm816-fapll-clock", ti_fapll_setup);
667