xref: /linux/drivers/clk/xilinx/clk-xlnx-clock-wizard.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1c822490fSShubhrajyoti Datta // SPDX-License-Identifier: GPL-2.0
2c822490fSShubhrajyoti Datta /*
3c822490fSShubhrajyoti Datta  * Xilinx 'Clocking Wizard' driver
4c822490fSShubhrajyoti Datta  *
5c822490fSShubhrajyoti Datta  *  Copyright (C) 2013 - 2021 Xilinx
6c822490fSShubhrajyoti Datta  *
7c822490fSShubhrajyoti Datta  *  Sören Brinkmann <soren.brinkmann@xilinx.com>
8c822490fSShubhrajyoti Datta  *
9c822490fSShubhrajyoti Datta  */
10c822490fSShubhrajyoti Datta 
11595c88cdSShubhrajyoti Datta #include <linux/bitfield.h>
12c822490fSShubhrajyoti Datta #include <linux/platform_device.h>
13c822490fSShubhrajyoti Datta #include <linux/clk.h>
14c822490fSShubhrajyoti Datta #include <linux/clk-provider.h>
15c822490fSShubhrajyoti Datta #include <linux/slab.h>
16c822490fSShubhrajyoti Datta #include <linux/io.h>
17c822490fSShubhrajyoti Datta #include <linux/of.h>
18595c88cdSShubhrajyoti Datta #include <linux/math64.h>
19c822490fSShubhrajyoti Datta #include <linux/module.h>
20c822490fSShubhrajyoti Datta #include <linux/err.h>
21c822490fSShubhrajyoti Datta #include <linux/iopoll.h>
22c822490fSShubhrajyoti Datta 
23c822490fSShubhrajyoti Datta #define WZRD_NUM_OUTPUTS	7
24c822490fSShubhrajyoti Datta #define WZRD_ACLK_MAX_FREQ	250000000UL
25c822490fSShubhrajyoti Datta 
263a96393aSShubhrajyoti Datta #define WZRD_CLK_CFG_REG(v, n)	(0x200 + 0x130 * (v) + 4 * (n))
27c822490fSShubhrajyoti Datta 
28c822490fSShubhrajyoti Datta #define WZRD_CLKOUT0_FRAC_EN	BIT(18)
293a96393aSShubhrajyoti Datta #define WZRD_CLKFBOUT_1		0
303a96393aSShubhrajyoti Datta #define WZRD_CLKFBOUT_2		1
313a96393aSShubhrajyoti Datta #define WZRD_CLKOUT0_1		2
323a96393aSShubhrajyoti Datta #define WZRD_CLKOUT0_2		3
333a96393aSShubhrajyoti Datta #define WZRD_DESKEW_2		20
343a96393aSShubhrajyoti Datta #define WZRD_DIVCLK		21
353a96393aSShubhrajyoti Datta #define WZRD_CLKFBOUT_4		51
363a96393aSShubhrajyoti Datta #define WZRD_CLKFBOUT_3		48
373a96393aSShubhrajyoti Datta #define WZRD_DUTY_CYCLE		2
383a96393aSShubhrajyoti Datta #define WZRD_O_DIV		4
393a96393aSShubhrajyoti Datta 
403a96393aSShubhrajyoti Datta #define WZRD_CLKFBOUT_FRAC_EN	BIT(1)
413a96393aSShubhrajyoti Datta #define WZRD_CLKFBOUT_PREDIV2	(BIT(11) | BIT(12) | BIT(9))
423a96393aSShubhrajyoti Datta #define WZRD_MULT_PREDIV2	(BIT(10) | BIT(9) | BIT(12))
433a96393aSShubhrajyoti Datta #define WZRD_CLKFBOUT_EDGE	BIT(8)
443a96393aSShubhrajyoti Datta #define WZRD_P5EN		BIT(13)
453a96393aSShubhrajyoti Datta #define WZRD_P5EN_SHIFT		13
463a96393aSShubhrajyoti Datta #define WZRD_P5FEDGE		BIT(15)
473a96393aSShubhrajyoti Datta #define WZRD_DIVCLK_EDGE	BIT(10)
483a96393aSShubhrajyoti Datta #define WZRD_P5FEDGE_SHIFT	15
493a96393aSShubhrajyoti Datta #define WZRD_CLKOUT0_PREDIV2	BIT(11)
503a96393aSShubhrajyoti Datta #define WZRD_EDGE_SHIFT		8
51c822490fSShubhrajyoti Datta 
52c822490fSShubhrajyoti Datta #define WZRD_CLKFBOUT_MULT_SHIFT	8
53c822490fSShubhrajyoti Datta #define WZRD_CLKFBOUT_MULT_MASK		(0xff << WZRD_CLKFBOUT_MULT_SHIFT)
543a96393aSShubhrajyoti Datta #define WZRD_CLKFBOUT_L_SHIFT	0
553a96393aSShubhrajyoti Datta #define WZRD_CLKFBOUT_H_SHIFT	8
563a96393aSShubhrajyoti Datta #define WZRD_CLKFBOUT_L_MASK	GENMASK(7, 0)
573a96393aSShubhrajyoti Datta #define WZRD_CLKFBOUT_H_MASK	GENMASK(15, 8)
58c822490fSShubhrajyoti Datta #define WZRD_CLKFBOUT_FRAC_SHIFT	16
59c822490fSShubhrajyoti Datta #define WZRD_CLKFBOUT_FRAC_MASK		(0x3ff << WZRD_CLKFBOUT_FRAC_SHIFT)
603a96393aSShubhrajyoti Datta #define WZRD_VERSAL_FRAC_MASK		GENMASK(5, 0)
61c822490fSShubhrajyoti Datta #define WZRD_DIVCLK_DIVIDE_SHIFT	0
62c822490fSShubhrajyoti Datta #define WZRD_DIVCLK_DIVIDE_MASK		(0xff << WZRD_DIVCLK_DIVIDE_SHIFT)
63c822490fSShubhrajyoti Datta #define WZRD_CLKOUT_DIVIDE_SHIFT	0
64c822490fSShubhrajyoti Datta #define WZRD_CLKOUT_DIVIDE_WIDTH	8
65c822490fSShubhrajyoti Datta #define WZRD_CLKOUT_DIVIDE_MASK		(0xff << WZRD_DIVCLK_DIVIDE_SHIFT)
66c822490fSShubhrajyoti Datta #define WZRD_CLKOUT_FRAC_SHIFT		8
67c822490fSShubhrajyoti Datta #define WZRD_CLKOUT_FRAC_MASK		0x3ff
68595c88cdSShubhrajyoti Datta #define WZRD_CLKOUT0_FRAC_MASK		GENMASK(17, 8)
69c822490fSShubhrajyoti Datta 
70c822490fSShubhrajyoti Datta #define WZRD_DR_MAX_INT_DIV_VALUE	255
71c822490fSShubhrajyoti Datta #define WZRD_DR_STATUS_REG_OFFSET	0x04
72c822490fSShubhrajyoti Datta #define WZRD_DR_LOCK_BIT_MASK		0x00000001
73c822490fSShubhrajyoti Datta #define WZRD_DR_INIT_REG_OFFSET		0x25C
743a96393aSShubhrajyoti Datta #define WZRD_DR_INIT_VERSAL_OFFSET	0x14
75c822490fSShubhrajyoti Datta #define WZRD_DR_DIV_TO_PHASE_OFFSET	4
76c822490fSShubhrajyoti Datta #define WZRD_DR_BEGIN_DYNA_RECONF	0x03
77dd5e7431SShubhrajyoti Datta #define WZRD_DR_BEGIN_DYNA_RECONF_5_2	0x07
78dd5e7431SShubhrajyoti Datta #define WZRD_DR_BEGIN_DYNA_RECONF1_5_2	0x02
79c822490fSShubhrajyoti Datta 
80c822490fSShubhrajyoti Datta #define WZRD_USEC_POLL		10
81c822490fSShubhrajyoti Datta #define WZRD_TIMEOUT_POLL		1000
823a96393aSShubhrajyoti Datta #define WZRD_FRAC_GRADIENT		64
833a96393aSShubhrajyoti Datta #define PREDIV2_MULT			2
84595c88cdSShubhrajyoti Datta 
85595c88cdSShubhrajyoti Datta /* Divider limits, from UG572 Table 3-4 for Ultrascale+ */
86595c88cdSShubhrajyoti Datta #define DIV_O				0x01
87595c88cdSShubhrajyoti Datta #define DIV_ALL				0x03
88595c88cdSShubhrajyoti Datta 
89595c88cdSShubhrajyoti Datta #define WZRD_M_MIN			2
90595c88cdSShubhrajyoti Datta #define WZRD_M_MAX			128
91595c88cdSShubhrajyoti Datta #define WZRD_D_MIN			1
92595c88cdSShubhrajyoti Datta #define WZRD_D_MAX			106
93595c88cdSShubhrajyoti Datta #define WZRD_VCO_MIN			800000000
94595c88cdSShubhrajyoti Datta #define WZRD_VCO_MAX			1600000000
95595c88cdSShubhrajyoti Datta #define WZRD_O_MIN			1
96595c88cdSShubhrajyoti Datta #define WZRD_O_MAX			128
973a96393aSShubhrajyoti Datta #define VER_WZRD_M_MIN			4
983a96393aSShubhrajyoti Datta #define VER_WZRD_M_MAX			432
993a96393aSShubhrajyoti Datta #define VER_WZRD_D_MIN			1
1003a96393aSShubhrajyoti Datta #define VER_WZRD_D_MAX			123
1013a96393aSShubhrajyoti Datta #define VER_WZRD_VCO_MIN		2160000000ULL
1023a96393aSShubhrajyoti Datta #define VER_WZRD_VCO_MAX		4320000000ULL
1033a96393aSShubhrajyoti Datta #define VER_WZRD_O_MIN			2
1043a96393aSShubhrajyoti Datta #define VER_WZRD_O_MAX			511
105595c88cdSShubhrajyoti Datta #define WZRD_MIN_ERR			20000
106595c88cdSShubhrajyoti Datta #define WZRD_FRAC_POINTS		1000
107595c88cdSShubhrajyoti Datta 
108c822490fSShubhrajyoti Datta /* Get the mask from width */
109c822490fSShubhrajyoti Datta #define div_mask(width)			((1 << (width)) - 1)
110c822490fSShubhrajyoti Datta 
111c822490fSShubhrajyoti Datta /* Extract divider instance from clock hardware instance */
112c822490fSShubhrajyoti Datta #define to_clk_wzrd_divider(_hw) container_of(_hw, struct clk_wzrd_divider, hw)
113c822490fSShubhrajyoti Datta 
114c822490fSShubhrajyoti Datta enum clk_wzrd_int_clks {
115c822490fSShubhrajyoti Datta 	wzrd_clk_mul,
116c822490fSShubhrajyoti Datta 	wzrd_clk_mul_div,
117c822490fSShubhrajyoti Datta 	wzrd_clk_mul_frac,
118c822490fSShubhrajyoti Datta 	wzrd_clk_int_max
119c822490fSShubhrajyoti Datta };
120c822490fSShubhrajyoti Datta 
121c822490fSShubhrajyoti Datta /**
122c822490fSShubhrajyoti Datta  * struct clk_wzrd - Clock wizard private data structure
123c822490fSShubhrajyoti Datta  *
124c822490fSShubhrajyoti Datta  * @clk_data:		Clock data
125c822490fSShubhrajyoti Datta  * @nb:			Notifier block
126c822490fSShubhrajyoti Datta  * @base:		Memory base
127c822490fSShubhrajyoti Datta  * @clk_in1:		Handle to input clock 'clk_in1'
128c822490fSShubhrajyoti Datta  * @axi_clk:		Handle to input clock 's_axi_aclk'
129c822490fSShubhrajyoti Datta  * @clks_internal:	Internal clocks
130c822490fSShubhrajyoti Datta  * @clkout:		Output clocks
131c822490fSShubhrajyoti Datta  * @speed_grade:	Speed grade of the device
132c822490fSShubhrajyoti Datta  * @suspended:		Flag indicating power state of the device
133c822490fSShubhrajyoti Datta  */
134c822490fSShubhrajyoti Datta struct clk_wzrd {
135c822490fSShubhrajyoti Datta 	struct clk_onecell_data clk_data;
136c822490fSShubhrajyoti Datta 	struct notifier_block nb;
137c822490fSShubhrajyoti Datta 	void __iomem *base;
138c822490fSShubhrajyoti Datta 	struct clk *clk_in1;
139c822490fSShubhrajyoti Datta 	struct clk *axi_clk;
140c822490fSShubhrajyoti Datta 	struct clk *clks_internal[wzrd_clk_int_max];
141c822490fSShubhrajyoti Datta 	struct clk *clkout[WZRD_NUM_OUTPUTS];
142c822490fSShubhrajyoti Datta 	unsigned int speed_grade;
143c822490fSShubhrajyoti Datta 	bool suspended;
144c822490fSShubhrajyoti Datta };
145c822490fSShubhrajyoti Datta 
146c822490fSShubhrajyoti Datta /**
147c822490fSShubhrajyoti Datta  * struct clk_wzrd_divider - clock divider specific to clk_wzrd
148c822490fSShubhrajyoti Datta  *
149c822490fSShubhrajyoti Datta  * @hw:		handle between common and hardware-specific interfaces
150c822490fSShubhrajyoti Datta  * @base:	base address of register containing the divider
151c822490fSShubhrajyoti Datta  * @offset:	offset address of register containing the divider
152c822490fSShubhrajyoti Datta  * @shift:	shift to the divider bit field
153c822490fSShubhrajyoti Datta  * @width:	width of the divider bit field
154c822490fSShubhrajyoti Datta  * @flags:	clk_wzrd divider flags
155c822490fSShubhrajyoti Datta  * @table:	array of value/divider pairs, last entry should have div = 0
156595c88cdSShubhrajyoti Datta  * @m:	value of the multiplier
157595c88cdSShubhrajyoti Datta  * @d:	value of the common divider
158595c88cdSShubhrajyoti Datta  * @o:	value of the leaf divider
159c822490fSShubhrajyoti Datta  * @lock:	register lock
160c822490fSShubhrajyoti Datta  */
161c822490fSShubhrajyoti Datta struct clk_wzrd_divider {
162c822490fSShubhrajyoti Datta 	struct clk_hw hw;
163c822490fSShubhrajyoti Datta 	void __iomem *base;
164c822490fSShubhrajyoti Datta 	u16 offset;
165c822490fSShubhrajyoti Datta 	u8 shift;
166c822490fSShubhrajyoti Datta 	u8 width;
167c822490fSShubhrajyoti Datta 	u8 flags;
168c822490fSShubhrajyoti Datta 	const struct clk_div_table *table;
169595c88cdSShubhrajyoti Datta 	u32 m;
170595c88cdSShubhrajyoti Datta 	u32 d;
171595c88cdSShubhrajyoti Datta 	u32 o;
172c822490fSShubhrajyoti Datta 	spinlock_t *lock;  /* divider lock */
173c822490fSShubhrajyoti Datta };
174c822490fSShubhrajyoti Datta 
1753a96393aSShubhrajyoti Datta struct versal_clk_data {
1763a96393aSShubhrajyoti Datta 	bool is_versal;
1773a96393aSShubhrajyoti Datta };
1783a96393aSShubhrajyoti Datta 
179c822490fSShubhrajyoti Datta #define to_clk_wzrd(_nb) container_of(_nb, struct clk_wzrd, nb)
180c822490fSShubhrajyoti Datta 
181c822490fSShubhrajyoti Datta /* maximum frequencies for input/output clocks per speed grade */
182c822490fSShubhrajyoti Datta static const unsigned long clk_wzrd_max_freq[] = {
183c822490fSShubhrajyoti Datta 	800000000UL,
184c822490fSShubhrajyoti Datta 	933000000UL,
185c822490fSShubhrajyoti Datta 	1066000000UL
186c822490fSShubhrajyoti Datta };
187c822490fSShubhrajyoti Datta 
188c822490fSShubhrajyoti Datta /* spin lock variable for clk_wzrd */
189c822490fSShubhrajyoti Datta static DEFINE_SPINLOCK(clkwzrd_lock);
190c822490fSShubhrajyoti Datta 
clk_wzrd_recalc_rate_ver(struct clk_hw * hw,unsigned long parent_rate)1913a96393aSShubhrajyoti Datta static unsigned long clk_wzrd_recalc_rate_ver(struct clk_hw *hw,
1923a96393aSShubhrajyoti Datta 					      unsigned long parent_rate)
1933a96393aSShubhrajyoti Datta {
1943a96393aSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
1953a96393aSShubhrajyoti Datta 	void __iomem *div_addr = divider->base + divider->offset;
1963a96393aSShubhrajyoti Datta 	u32 div, p5en, edge, prediv2, all;
1973a96393aSShubhrajyoti Datta 	unsigned int vall, valh;
1983a96393aSShubhrajyoti Datta 
1993a96393aSShubhrajyoti Datta 	edge = !!(readl(div_addr) & WZRD_CLKFBOUT_EDGE);
2003a96393aSShubhrajyoti Datta 	p5en = !!(readl(div_addr) & WZRD_P5EN);
2013a96393aSShubhrajyoti Datta 	prediv2 = !!(readl(div_addr) & WZRD_CLKOUT0_PREDIV2);
2023a96393aSShubhrajyoti Datta 	vall = readl(div_addr + 4) & WZRD_CLKFBOUT_L_MASK;
2033a96393aSShubhrajyoti Datta 	valh = readl(div_addr + 4) >> WZRD_CLKFBOUT_H_SHIFT;
2043a96393aSShubhrajyoti Datta 	all = valh + vall + edge;
2053a96393aSShubhrajyoti Datta 	if (!all)
2063a96393aSShubhrajyoti Datta 		all = 1;
2073a96393aSShubhrajyoti Datta 
2083a96393aSShubhrajyoti Datta 	if (prediv2)
2093a96393aSShubhrajyoti Datta 		div = 2 * all + prediv2 * p5en;
2103a96393aSShubhrajyoti Datta 	else
2113a96393aSShubhrajyoti Datta 		div = all;
2123a96393aSShubhrajyoti Datta 
2133a96393aSShubhrajyoti Datta 	return DIV_ROUND_UP_ULL((u64)parent_rate, div);
2143a96393aSShubhrajyoti Datta }
2153a96393aSShubhrajyoti Datta 
clk_wzrd_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)216c822490fSShubhrajyoti Datta static unsigned long clk_wzrd_recalc_rate(struct clk_hw *hw,
217c822490fSShubhrajyoti Datta 					  unsigned long parent_rate)
218c822490fSShubhrajyoti Datta {
219c822490fSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
220c822490fSShubhrajyoti Datta 	void __iomem *div_addr = divider->base + divider->offset;
221c822490fSShubhrajyoti Datta 	unsigned int val;
222c822490fSShubhrajyoti Datta 
223c822490fSShubhrajyoti Datta 	val = readl(div_addr) >> divider->shift;
224c822490fSShubhrajyoti Datta 	val &= div_mask(divider->width);
225c822490fSShubhrajyoti Datta 
226c822490fSShubhrajyoti Datta 	return divider_recalc_rate(hw, parent_rate, val, divider->table,
227c822490fSShubhrajyoti Datta 			divider->flags, divider->width);
228c822490fSShubhrajyoti Datta }
229c822490fSShubhrajyoti Datta 
clk_wzrd_ver_dynamic_reconfig(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)2303a96393aSShubhrajyoti Datta static int clk_wzrd_ver_dynamic_reconfig(struct clk_hw *hw, unsigned long rate,
2313a96393aSShubhrajyoti Datta 					 unsigned long parent_rate)
2323a96393aSShubhrajyoti Datta {
2333a96393aSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
2343a96393aSShubhrajyoti Datta 	void __iomem *div_addr = divider->base + divider->offset;
2353a96393aSShubhrajyoti Datta 	u32 value, regh, edged, p5en, p5fedge, regval, regval1;
2363a96393aSShubhrajyoti Datta 	unsigned long flags;
2373a96393aSShubhrajyoti Datta 	int err;
2383a96393aSShubhrajyoti Datta 
2393a96393aSShubhrajyoti Datta 	spin_lock_irqsave(divider->lock, flags);
2403a96393aSShubhrajyoti Datta 
2413a96393aSShubhrajyoti Datta 	value = DIV_ROUND_CLOSEST(parent_rate, rate);
2423a96393aSShubhrajyoti Datta 
2433a96393aSShubhrajyoti Datta 	regh = (value / 4);
2443a96393aSShubhrajyoti Datta 	regval1 = readl(div_addr);
2453a96393aSShubhrajyoti Datta 	regval1 |= WZRD_CLKFBOUT_PREDIV2;
2463a96393aSShubhrajyoti Datta 	regval1 = regval1 & ~(WZRD_CLKFBOUT_EDGE | WZRD_P5EN | WZRD_P5FEDGE);
2473a96393aSShubhrajyoti Datta 	if (value % 4 > 1) {
2483a96393aSShubhrajyoti Datta 		edged = 1;
2493a96393aSShubhrajyoti Datta 		regval1 |= (edged << WZRD_EDGE_SHIFT);
2503a96393aSShubhrajyoti Datta 	}
2513a96393aSShubhrajyoti Datta 	p5fedge = value % 2;
2523a96393aSShubhrajyoti Datta 	p5en = value % 2;
2533a96393aSShubhrajyoti Datta 	regval1 = regval1 | p5en << WZRD_P5EN_SHIFT | p5fedge << WZRD_P5FEDGE_SHIFT;
2543a96393aSShubhrajyoti Datta 	writel(regval1, div_addr);
2553a96393aSShubhrajyoti Datta 
2563a96393aSShubhrajyoti Datta 	regval = regh | regh << WZRD_CLKFBOUT_H_SHIFT;
2573a96393aSShubhrajyoti Datta 	writel(regval, div_addr + 4);
2583a96393aSShubhrajyoti Datta 	/* Check status register */
2593a96393aSShubhrajyoti Datta 	err = readl_poll_timeout_atomic(divider->base + WZRD_DR_STATUS_REG_OFFSET,
2603a96393aSShubhrajyoti Datta 					value, value & WZRD_DR_LOCK_BIT_MASK,
2613a96393aSShubhrajyoti Datta 					WZRD_USEC_POLL, WZRD_TIMEOUT_POLL);
2623a96393aSShubhrajyoti Datta 	if (err)
2633a96393aSShubhrajyoti Datta 		goto err_reconfig;
2643a96393aSShubhrajyoti Datta 
2653a96393aSShubhrajyoti Datta 	/* Initiate reconfiguration */
2663a96393aSShubhrajyoti Datta 	writel(WZRD_DR_BEGIN_DYNA_RECONF,
2673a96393aSShubhrajyoti Datta 	       divider->base + WZRD_DR_INIT_VERSAL_OFFSET);
2683a96393aSShubhrajyoti Datta 
2693a96393aSShubhrajyoti Datta 	/* Check status register */
2703a96393aSShubhrajyoti Datta 	err = readl_poll_timeout_atomic(divider->base + WZRD_DR_STATUS_REG_OFFSET,
2713a96393aSShubhrajyoti Datta 					value, value & WZRD_DR_LOCK_BIT_MASK,
2723a96393aSShubhrajyoti Datta 					WZRD_USEC_POLL, WZRD_TIMEOUT_POLL);
2733a96393aSShubhrajyoti Datta err_reconfig:
2743a96393aSShubhrajyoti Datta 	spin_unlock_irqrestore(divider->lock, flags);
2753a96393aSShubhrajyoti Datta 	return err;
2763a96393aSShubhrajyoti Datta }
2773a96393aSShubhrajyoti Datta 
clk_wzrd_dynamic_reconfig(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)278c822490fSShubhrajyoti Datta static int clk_wzrd_dynamic_reconfig(struct clk_hw *hw, unsigned long rate,
279c822490fSShubhrajyoti Datta 				     unsigned long parent_rate)
280c822490fSShubhrajyoti Datta {
281c822490fSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
282c822490fSShubhrajyoti Datta 	void __iomem *div_addr = divider->base + divider->offset;
2833a96393aSShubhrajyoti Datta 	unsigned long flags;
2843a96393aSShubhrajyoti Datta 	u32 value;
2853a96393aSShubhrajyoti Datta 	int err;
286c822490fSShubhrajyoti Datta 
287c822490fSShubhrajyoti Datta 	spin_lock_irqsave(divider->lock, flags);
288c822490fSShubhrajyoti Datta 
289c822490fSShubhrajyoti Datta 	value = DIV_ROUND_CLOSEST(parent_rate, rate);
290c822490fSShubhrajyoti Datta 
291c822490fSShubhrajyoti Datta 	/* Cap the value to max */
292c822490fSShubhrajyoti Datta 	min_t(u32, value, WZRD_DR_MAX_INT_DIV_VALUE);
293c822490fSShubhrajyoti Datta 
294c822490fSShubhrajyoti Datta 	/* Set divisor and clear phase offset */
295c822490fSShubhrajyoti Datta 	writel(value, div_addr);
296c822490fSShubhrajyoti Datta 	writel(0x00, div_addr + WZRD_DR_DIV_TO_PHASE_OFFSET);
297c822490fSShubhrajyoti Datta 
298c822490fSShubhrajyoti Datta 	/* Check status register */
2993a96393aSShubhrajyoti Datta 	err = readl_poll_timeout_atomic(divider->base + WZRD_DR_STATUS_REG_OFFSET,
300c822490fSShubhrajyoti Datta 					value, value & WZRD_DR_LOCK_BIT_MASK,
301c822490fSShubhrajyoti Datta 					WZRD_USEC_POLL, WZRD_TIMEOUT_POLL);
302c822490fSShubhrajyoti Datta 	if (err)
303c822490fSShubhrajyoti Datta 		goto err_reconfig;
304c822490fSShubhrajyoti Datta 
305c822490fSShubhrajyoti Datta 	/* Initiate reconfiguration */
306dd5e7431SShubhrajyoti Datta 	writel(WZRD_DR_BEGIN_DYNA_RECONF_5_2,
307dd5e7431SShubhrajyoti Datta 	       divider->base + WZRD_DR_INIT_REG_OFFSET);
308dd5e7431SShubhrajyoti Datta 	writel(WZRD_DR_BEGIN_DYNA_RECONF1_5_2,
309c822490fSShubhrajyoti Datta 	       divider->base + WZRD_DR_INIT_REG_OFFSET);
310c822490fSShubhrajyoti Datta 
311c822490fSShubhrajyoti Datta 	/* Check status register */
3123a96393aSShubhrajyoti Datta 	err = readl_poll_timeout_atomic(divider->base + WZRD_DR_STATUS_REG_OFFSET,
313c822490fSShubhrajyoti Datta 					value, value & WZRD_DR_LOCK_BIT_MASK,
314c822490fSShubhrajyoti Datta 					WZRD_USEC_POLL, WZRD_TIMEOUT_POLL);
315c822490fSShubhrajyoti Datta err_reconfig:
316c822490fSShubhrajyoti Datta 	spin_unlock_irqrestore(divider->lock, flags);
317c822490fSShubhrajyoti Datta 	return err;
318c822490fSShubhrajyoti Datta }
319c822490fSShubhrajyoti Datta 
clk_wzrd_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)320c822490fSShubhrajyoti Datta static long clk_wzrd_round_rate(struct clk_hw *hw, unsigned long rate,
321c822490fSShubhrajyoti Datta 				unsigned long *prate)
322c822490fSShubhrajyoti Datta {
323c822490fSShubhrajyoti Datta 	u8 div;
324c822490fSShubhrajyoti Datta 
325c822490fSShubhrajyoti Datta 	/*
326c822490fSShubhrajyoti Datta 	 * since we don't change parent rate we just round rate to closest
327c822490fSShubhrajyoti Datta 	 * achievable
328c822490fSShubhrajyoti Datta 	 */
329c822490fSShubhrajyoti Datta 	div = DIV_ROUND_CLOSEST(*prate, rate);
330c822490fSShubhrajyoti Datta 
331c822490fSShubhrajyoti Datta 	return *prate / div;
332c822490fSShubhrajyoti Datta }
333c822490fSShubhrajyoti Datta 
clk_wzrd_get_divisors_ver(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)3343a96393aSShubhrajyoti Datta static int clk_wzrd_get_divisors_ver(struct clk_hw *hw, unsigned long rate,
335595c88cdSShubhrajyoti Datta 				     unsigned long parent_rate)
336595c88cdSShubhrajyoti Datta {
337595c88cdSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
3383a96393aSShubhrajyoti Datta 	u64 vco_freq, freq, diff, vcomin, vcomax;
339595c88cdSShubhrajyoti Datta 	u32 m, d, o;
3403a96393aSShubhrajyoti Datta 	u32 mmin, mmax, dmin, dmax, omin, omax;
341595c88cdSShubhrajyoti Datta 
3423a96393aSShubhrajyoti Datta 	mmin = VER_WZRD_M_MIN;
3433a96393aSShubhrajyoti Datta 	mmax = VER_WZRD_M_MAX;
3443a96393aSShubhrajyoti Datta 	dmin = VER_WZRD_D_MIN;
3453a96393aSShubhrajyoti Datta 	dmax = VER_WZRD_D_MAX;
3463a96393aSShubhrajyoti Datta 	omin = VER_WZRD_O_MIN;
3473a96393aSShubhrajyoti Datta 	omax = VER_WZRD_O_MAX;
3483a96393aSShubhrajyoti Datta 	vcomin = VER_WZRD_VCO_MIN;
3493a96393aSShubhrajyoti Datta 	vcomax = VER_WZRD_VCO_MAX;
3503a96393aSShubhrajyoti Datta 
3513a96393aSShubhrajyoti Datta 	for (m = mmin; m <= mmax; m++) {
3523a96393aSShubhrajyoti Datta 		for (d = dmin; d <= dmax; d++) {
353595c88cdSShubhrajyoti Datta 			vco_freq = DIV_ROUND_CLOSEST((parent_rate * m), d);
3543a96393aSShubhrajyoti Datta 			if (vco_freq >= vcomin && vco_freq <= vcomax) {
3553a96393aSShubhrajyoti Datta 				for (o = omin; o <= omax; o++) {
356595c88cdSShubhrajyoti Datta 					freq = DIV_ROUND_CLOSEST_ULL(vco_freq, o);
357595c88cdSShubhrajyoti Datta 					diff = abs(freq - rate);
358595c88cdSShubhrajyoti Datta 
359595c88cdSShubhrajyoti Datta 					if (diff < WZRD_MIN_ERR) {
360595c88cdSShubhrajyoti Datta 						divider->m = m;
361595c88cdSShubhrajyoti Datta 						divider->d = d;
362595c88cdSShubhrajyoti Datta 						divider->o = o;
363595c88cdSShubhrajyoti Datta 						return 0;
364595c88cdSShubhrajyoti Datta 					}
365595c88cdSShubhrajyoti Datta 				}
366595c88cdSShubhrajyoti Datta 			}
367595c88cdSShubhrajyoti Datta 		}
368595c88cdSShubhrajyoti Datta 	}
369595c88cdSShubhrajyoti Datta 	return -EBUSY;
370595c88cdSShubhrajyoti Datta }
371595c88cdSShubhrajyoti Datta 
clk_wzrd_get_divisors(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)3723a96393aSShubhrajyoti Datta static int clk_wzrd_get_divisors(struct clk_hw *hw, unsigned long rate,
3733a96393aSShubhrajyoti Datta 				 unsigned long parent_rate)
3743a96393aSShubhrajyoti Datta {
3753a96393aSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
3763a96393aSShubhrajyoti Datta 	u64 vco_freq, freq, diff, vcomin, vcomax;
3773a96393aSShubhrajyoti Datta 	u32 m, d, o;
3783a96393aSShubhrajyoti Datta 	u32 mmin, mmax, dmin, dmax, omin, omax;
3793a96393aSShubhrajyoti Datta 
3803a96393aSShubhrajyoti Datta 	mmin = WZRD_M_MIN;
3813a96393aSShubhrajyoti Datta 	mmax = WZRD_M_MAX;
3823a96393aSShubhrajyoti Datta 	dmin = WZRD_D_MIN;
3833a96393aSShubhrajyoti Datta 	dmax = WZRD_D_MAX;
3843a96393aSShubhrajyoti Datta 	omin = WZRD_O_MIN;
3853a96393aSShubhrajyoti Datta 	omax = WZRD_O_MAX;
3863a96393aSShubhrajyoti Datta 	vcomin = WZRD_VCO_MIN;
3873a96393aSShubhrajyoti Datta 	vcomax = WZRD_VCO_MAX;
3883a96393aSShubhrajyoti Datta 
3893a96393aSShubhrajyoti Datta 	for (m = mmin; m <= mmax; m++) {
3903a96393aSShubhrajyoti Datta 		for (d = dmin; d <= dmax; d++) {
3913a96393aSShubhrajyoti Datta 			vco_freq = DIV_ROUND_CLOSEST((parent_rate * m), d);
3923a96393aSShubhrajyoti Datta 			if (vco_freq >= vcomin && vco_freq <= vcomax) {
3933a96393aSShubhrajyoti Datta 				for (o = omin; o <= omax; o++) {
3943a96393aSShubhrajyoti Datta 					freq = DIV_ROUND_CLOSEST_ULL(vco_freq, o);
3953a96393aSShubhrajyoti Datta 					diff = abs(freq - rate);
3963a96393aSShubhrajyoti Datta 
3973a96393aSShubhrajyoti Datta 					if (diff < WZRD_MIN_ERR) {
3983a96393aSShubhrajyoti Datta 						divider->m = m;
3993a96393aSShubhrajyoti Datta 						divider->d = d;
4003a96393aSShubhrajyoti Datta 						divider->o = o;
4013a96393aSShubhrajyoti Datta 						return 0;
4023a96393aSShubhrajyoti Datta 					}
4033a96393aSShubhrajyoti Datta 				}
4043a96393aSShubhrajyoti Datta 			}
4053a96393aSShubhrajyoti Datta 		}
4063a96393aSShubhrajyoti Datta 	}
4073a96393aSShubhrajyoti Datta 	return -EBUSY;
4083a96393aSShubhrajyoti Datta }
4093a96393aSShubhrajyoti Datta 
clk_wzrd_reconfig(struct clk_wzrd_divider * divider,void __iomem * div_addr)4103a96393aSShubhrajyoti Datta static int clk_wzrd_reconfig(struct clk_wzrd_divider *divider, void __iomem *div_addr)
4113a96393aSShubhrajyoti Datta {
4123a96393aSShubhrajyoti Datta 	u32 value;
4133a96393aSShubhrajyoti Datta 	int err;
4143a96393aSShubhrajyoti Datta 
4153a96393aSShubhrajyoti Datta 	/* Check status register */
4163a96393aSShubhrajyoti Datta 	err = readl_poll_timeout_atomic(divider->base + WZRD_DR_STATUS_REG_OFFSET, value,
4173a96393aSShubhrajyoti Datta 					value & WZRD_DR_LOCK_BIT_MASK,
4183a96393aSShubhrajyoti Datta 					WZRD_USEC_POLL, WZRD_TIMEOUT_POLL);
4193a96393aSShubhrajyoti Datta 	if (err)
4203a96393aSShubhrajyoti Datta 		return -ETIMEDOUT;
4213a96393aSShubhrajyoti Datta 
4223a96393aSShubhrajyoti Datta 	/* Initiate reconfiguration */
4233a96393aSShubhrajyoti Datta 	writel(WZRD_DR_BEGIN_DYNA_RECONF, div_addr);
4243a96393aSShubhrajyoti Datta 	/* Check status register */
4253a96393aSShubhrajyoti Datta 	return readl_poll_timeout_atomic(divider->base + WZRD_DR_STATUS_REG_OFFSET, value,
4263a96393aSShubhrajyoti Datta 				 value & WZRD_DR_LOCK_BIT_MASK,
4273a96393aSShubhrajyoti Datta 				 WZRD_USEC_POLL, WZRD_TIMEOUT_POLL);
4283a96393aSShubhrajyoti Datta }
4293a96393aSShubhrajyoti Datta 
clk_wzrd_dynamic_ver_all_nolock(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)4303a96393aSShubhrajyoti Datta static int clk_wzrd_dynamic_ver_all_nolock(struct clk_hw *hw, unsigned long rate,
4313a96393aSShubhrajyoti Datta 					   unsigned long parent_rate)
4323a96393aSShubhrajyoti Datta {
4333a96393aSShubhrajyoti Datta 	u32 regh, edged, p5en, p5fedge, value2, m, regval, regval1, value;
4343a96393aSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
4353a96393aSShubhrajyoti Datta 	void __iomem *div_addr;
4363a96393aSShubhrajyoti Datta 	int err;
4373a96393aSShubhrajyoti Datta 
4383a96393aSShubhrajyoti Datta 	err = clk_wzrd_get_divisors_ver(hw, rate, parent_rate);
4393a96393aSShubhrajyoti Datta 	if (err)
4403a96393aSShubhrajyoti Datta 		return err;
4413a96393aSShubhrajyoti Datta 
4423a96393aSShubhrajyoti Datta 	writel(0, divider->base + WZRD_CLK_CFG_REG(1, WZRD_CLKFBOUT_4));
4433a96393aSShubhrajyoti Datta 
4443a96393aSShubhrajyoti Datta 	m = divider->m;
4453a96393aSShubhrajyoti Datta 	edged = m % WZRD_DUTY_CYCLE;
4463a96393aSShubhrajyoti Datta 	regh = m / WZRD_DUTY_CYCLE;
4473a96393aSShubhrajyoti Datta 	regval1 = readl(divider->base + WZRD_CLK_CFG_REG(1,
4483a96393aSShubhrajyoti Datta 							 WZRD_CLKFBOUT_1));
4493a96393aSShubhrajyoti Datta 	regval1 |= WZRD_MULT_PREDIV2;
4503a96393aSShubhrajyoti Datta 	if (edged)
4513a96393aSShubhrajyoti Datta 		regval1 = regval1 | WZRD_CLKFBOUT_EDGE;
4523a96393aSShubhrajyoti Datta 	else
4533a96393aSShubhrajyoti Datta 		regval1 = regval1 & ~WZRD_CLKFBOUT_EDGE;
4543a96393aSShubhrajyoti Datta 
4553a96393aSShubhrajyoti Datta 	writel(regval1, divider->base + WZRD_CLK_CFG_REG(1,
4563a96393aSShubhrajyoti Datta 							 WZRD_CLKFBOUT_1));
4573a96393aSShubhrajyoti Datta 	regval1 = regh | regh << WZRD_CLKFBOUT_H_SHIFT;
4583a96393aSShubhrajyoti Datta 	writel(regval1, divider->base + WZRD_CLK_CFG_REG(1,
4593a96393aSShubhrajyoti Datta 							 WZRD_CLKFBOUT_2));
4603a96393aSShubhrajyoti Datta 
4613a96393aSShubhrajyoti Datta 	value2 = divider->d;
4623a96393aSShubhrajyoti Datta 	edged = value2 % WZRD_DUTY_CYCLE;
4633a96393aSShubhrajyoti Datta 	regh = (value2 / WZRD_DUTY_CYCLE);
4643a96393aSShubhrajyoti Datta 	regval1 = FIELD_PREP(WZRD_DIVCLK_EDGE, edged);
4653a96393aSShubhrajyoti Datta 	writel(regval1, divider->base + WZRD_CLK_CFG_REG(1,
4663a96393aSShubhrajyoti Datta 							 WZRD_DESKEW_2));
4673a96393aSShubhrajyoti Datta 	regval1 = regh | regh << WZRD_CLKFBOUT_H_SHIFT;
4683a96393aSShubhrajyoti Datta 	writel(regval1, divider->base + WZRD_CLK_CFG_REG(1, WZRD_DIVCLK));
4693a96393aSShubhrajyoti Datta 
4703a96393aSShubhrajyoti Datta 	value = divider->o;
4713a96393aSShubhrajyoti Datta 	regh = value / WZRD_O_DIV;
4723a96393aSShubhrajyoti Datta 	regval1 = readl(divider->base + WZRD_CLK_CFG_REG(1,
4733a96393aSShubhrajyoti Datta 							 WZRD_CLKOUT0_1));
4743a96393aSShubhrajyoti Datta 	regval1 |= WZRD_CLKFBOUT_PREDIV2;
4753a96393aSShubhrajyoti Datta 	regval1 = regval1 & ~(WZRD_CLKFBOUT_EDGE | WZRD_P5EN | WZRD_P5FEDGE);
4763a96393aSShubhrajyoti Datta 
4773a96393aSShubhrajyoti Datta 	if (value % WZRD_O_DIV > 1) {
4783a96393aSShubhrajyoti Datta 		edged = 1;
4793a96393aSShubhrajyoti Datta 		regval1 |= edged << WZRD_CLKFBOUT_H_SHIFT;
4803a96393aSShubhrajyoti Datta 	}
4813a96393aSShubhrajyoti Datta 
4823a96393aSShubhrajyoti Datta 	p5fedge = value % WZRD_DUTY_CYCLE;
4833a96393aSShubhrajyoti Datta 	p5en = value % WZRD_DUTY_CYCLE;
4843a96393aSShubhrajyoti Datta 
4853a96393aSShubhrajyoti Datta 	regval1 = regval1 | FIELD_PREP(WZRD_P5EN, p5en) | FIELD_PREP(WZRD_P5FEDGE, p5fedge);
4863a96393aSShubhrajyoti Datta 	writel(regval1, divider->base + WZRD_CLK_CFG_REG(1,
4873a96393aSShubhrajyoti Datta 							 WZRD_CLKOUT0_1));
4883a96393aSShubhrajyoti Datta 	regval = regh | regh << WZRD_CLKFBOUT_H_SHIFT;
4893a96393aSShubhrajyoti Datta 	writel(regval, divider->base + WZRD_CLK_CFG_REG(1,
4903a96393aSShubhrajyoti Datta 							WZRD_CLKOUT0_2));
4913a96393aSShubhrajyoti Datta 	div_addr = divider->base + WZRD_DR_INIT_VERSAL_OFFSET;
4923a96393aSShubhrajyoti Datta 
4933a96393aSShubhrajyoti Datta 	return clk_wzrd_reconfig(divider, div_addr);
4943a96393aSShubhrajyoti Datta }
4953a96393aSShubhrajyoti Datta 
clk_wzrd_dynamic_all_nolock(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)496595c88cdSShubhrajyoti Datta static int clk_wzrd_dynamic_all_nolock(struct clk_hw *hw, unsigned long rate,
497595c88cdSShubhrajyoti Datta 				       unsigned long parent_rate)
498595c88cdSShubhrajyoti Datta {
499595c88cdSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
500595c88cdSShubhrajyoti Datta 	unsigned long vco_freq, rate_div, clockout0_div;
501732b1c2cSColin Ian King 	void __iomem *div_addr;
5023a96393aSShubhrajyoti Datta 	u32 reg, pre, f;
503595c88cdSShubhrajyoti Datta 	int err;
504595c88cdSShubhrajyoti Datta 
505595c88cdSShubhrajyoti Datta 	err = clk_wzrd_get_divisors(hw, rate, parent_rate);
506595c88cdSShubhrajyoti Datta 	if (err)
507595c88cdSShubhrajyoti Datta 		return err;
508595c88cdSShubhrajyoti Datta 
509595c88cdSShubhrajyoti Datta 	vco_freq = DIV_ROUND_CLOSEST(parent_rate * divider->m, divider->d);
510595c88cdSShubhrajyoti Datta 	rate_div = DIV_ROUND_CLOSEST_ULL((vco_freq * WZRD_FRAC_POINTS), rate);
511595c88cdSShubhrajyoti Datta 
512595c88cdSShubhrajyoti Datta 	clockout0_div = div_u64(rate_div,  WZRD_FRAC_POINTS);
513595c88cdSShubhrajyoti Datta 
514595c88cdSShubhrajyoti Datta 	pre = DIV_ROUND_CLOSEST_ULL(vco_freq * WZRD_FRAC_POINTS, rate);
515595c88cdSShubhrajyoti Datta 	f = (pre - (clockout0_div * WZRD_FRAC_POINTS));
516595c88cdSShubhrajyoti Datta 	f &= WZRD_CLKOUT_FRAC_MASK;
517595c88cdSShubhrajyoti Datta 
518595c88cdSShubhrajyoti Datta 	reg = FIELD_PREP(WZRD_CLKOUT_DIVIDE_MASK, clockout0_div) |
519595c88cdSShubhrajyoti Datta 	      FIELD_PREP(WZRD_CLKOUT0_FRAC_MASK, f);
520595c88cdSShubhrajyoti Datta 
5213a96393aSShubhrajyoti Datta 	writel(reg, divider->base + WZRD_CLK_CFG_REG(0, 2));
522595c88cdSShubhrajyoti Datta 	/* Set divisor and clear phase offset */
523595c88cdSShubhrajyoti Datta 	reg = FIELD_PREP(WZRD_CLKFBOUT_MULT_MASK, divider->m) |
524595c88cdSShubhrajyoti Datta 	      FIELD_PREP(WZRD_DIVCLK_DIVIDE_MASK, divider->d);
5253a96393aSShubhrajyoti Datta 	writel(reg, divider->base + WZRD_CLK_CFG_REG(0, 0));
5263a96393aSShubhrajyoti Datta 	writel(divider->o, divider->base + WZRD_CLK_CFG_REG(0, 2));
5273a96393aSShubhrajyoti Datta 	writel(0, divider->base + WZRD_CLK_CFG_REG(0, 3));
5283a96393aSShubhrajyoti Datta 	div_addr = divider->base + WZRD_DR_INIT_REG_OFFSET;
5293a96393aSShubhrajyoti Datta 	return clk_wzrd_reconfig(divider, div_addr);
530595c88cdSShubhrajyoti Datta }
531595c88cdSShubhrajyoti Datta 
clk_wzrd_dynamic_all(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)532595c88cdSShubhrajyoti Datta static int clk_wzrd_dynamic_all(struct clk_hw *hw, unsigned long rate,
533595c88cdSShubhrajyoti Datta 				unsigned long parent_rate)
534595c88cdSShubhrajyoti Datta {
535595c88cdSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
5363a96393aSShubhrajyoti Datta 	unsigned long flags;
537595c88cdSShubhrajyoti Datta 	int ret;
538595c88cdSShubhrajyoti Datta 
539595c88cdSShubhrajyoti Datta 	spin_lock_irqsave(divider->lock, flags);
540595c88cdSShubhrajyoti Datta 
541595c88cdSShubhrajyoti Datta 	ret = clk_wzrd_dynamic_all_nolock(hw, rate, parent_rate);
542595c88cdSShubhrajyoti Datta 
543595c88cdSShubhrajyoti Datta 	spin_unlock_irqrestore(divider->lock, flags);
544595c88cdSShubhrajyoti Datta 
545595c88cdSShubhrajyoti Datta 	return ret;
546595c88cdSShubhrajyoti Datta }
547595c88cdSShubhrajyoti Datta 
clk_wzrd_dynamic_all_ver(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)5483a96393aSShubhrajyoti Datta static int clk_wzrd_dynamic_all_ver(struct clk_hw *hw, unsigned long rate,
5493a96393aSShubhrajyoti Datta 				    unsigned long parent_rate)
5503a96393aSShubhrajyoti Datta {
5513a96393aSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
5523a96393aSShubhrajyoti Datta 	unsigned long flags;
5533a96393aSShubhrajyoti Datta 	int ret;
5543a96393aSShubhrajyoti Datta 
5553a96393aSShubhrajyoti Datta 	spin_lock_irqsave(divider->lock, flags);
5563a96393aSShubhrajyoti Datta 
5573a96393aSShubhrajyoti Datta 	ret = clk_wzrd_dynamic_ver_all_nolock(hw, rate, parent_rate);
5583a96393aSShubhrajyoti Datta 
5593a96393aSShubhrajyoti Datta 	spin_unlock_irqrestore(divider->lock, flags);
5603a96393aSShubhrajyoti Datta 
5613a96393aSShubhrajyoti Datta 	return ret;
5623a96393aSShubhrajyoti Datta }
5633a96393aSShubhrajyoti Datta 
clk_wzrd_recalc_rate_all(struct clk_hw * hw,unsigned long parent_rate)564595c88cdSShubhrajyoti Datta static unsigned long clk_wzrd_recalc_rate_all(struct clk_hw *hw,
565595c88cdSShubhrajyoti Datta 					      unsigned long parent_rate)
566595c88cdSShubhrajyoti Datta {
567595c88cdSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
568595c88cdSShubhrajyoti Datta 	u32 m, d, o, div, reg, f;
569595c88cdSShubhrajyoti Datta 
5703a96393aSShubhrajyoti Datta 	reg = readl(divider->base + WZRD_CLK_CFG_REG(0, 0));
571595c88cdSShubhrajyoti Datta 	d = FIELD_GET(WZRD_DIVCLK_DIVIDE_MASK, reg);
572595c88cdSShubhrajyoti Datta 	m = FIELD_GET(WZRD_CLKFBOUT_MULT_MASK, reg);
5733a96393aSShubhrajyoti Datta 	reg = readl(divider->base + WZRD_CLK_CFG_REG(0, 2));
574595c88cdSShubhrajyoti Datta 	o = FIELD_GET(WZRD_DIVCLK_DIVIDE_MASK, reg);
575595c88cdSShubhrajyoti Datta 	f = FIELD_GET(WZRD_CLKOUT0_FRAC_MASK, reg);
576595c88cdSShubhrajyoti Datta 
577595c88cdSShubhrajyoti Datta 	div = DIV_ROUND_CLOSEST(d * (WZRD_FRAC_POINTS * o + f), WZRD_FRAC_POINTS);
578595c88cdSShubhrajyoti Datta 	return divider_recalc_rate(hw, parent_rate * m, div, divider->table,
579595c88cdSShubhrajyoti Datta 		divider->flags, divider->width);
580595c88cdSShubhrajyoti Datta }
581595c88cdSShubhrajyoti Datta 
clk_wzrd_recalc_rate_all_ver(struct clk_hw * hw,unsigned long parent_rate)5823a96393aSShubhrajyoti Datta static unsigned long clk_wzrd_recalc_rate_all_ver(struct clk_hw *hw,
5833a96393aSShubhrajyoti Datta 						  unsigned long parent_rate)
5843a96393aSShubhrajyoti Datta {
5853a96393aSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
5863a96393aSShubhrajyoti Datta 	u32 edged, div2, p5en, edge, prediv2, all, regl, regh, mult;
5873a96393aSShubhrajyoti Datta 	u32 div, reg;
5883a96393aSShubhrajyoti Datta 
5893a96393aSShubhrajyoti Datta 	edge = !!(readl(divider->base + WZRD_CLK_CFG_REG(1, WZRD_CLKFBOUT_1)) &
5903a96393aSShubhrajyoti Datta 			WZRD_CLKFBOUT_EDGE);
5913a96393aSShubhrajyoti Datta 
5923a96393aSShubhrajyoti Datta 	reg = readl(divider->base + WZRD_CLK_CFG_REG(1, WZRD_CLKFBOUT_2));
5933a96393aSShubhrajyoti Datta 	regl = FIELD_GET(WZRD_CLKFBOUT_L_MASK, reg);
5943a96393aSShubhrajyoti Datta 	regh = FIELD_GET(WZRD_CLKFBOUT_H_MASK, reg);
5953a96393aSShubhrajyoti Datta 
5963a96393aSShubhrajyoti Datta 	mult = regl + regh + edge;
5973a96393aSShubhrajyoti Datta 	if (!mult)
5983a96393aSShubhrajyoti Datta 		mult = 1;
5993a96393aSShubhrajyoti Datta 
6003a96393aSShubhrajyoti Datta 	regl = readl(divider->base + WZRD_CLK_CFG_REG(1, WZRD_CLKFBOUT_4)) &
6013a96393aSShubhrajyoti Datta 		     WZRD_CLKFBOUT_FRAC_EN;
6023a96393aSShubhrajyoti Datta 	if (regl) {
6033a96393aSShubhrajyoti Datta 		regl = readl(divider->base + WZRD_CLK_CFG_REG(1, WZRD_CLKFBOUT_3))
6043a96393aSShubhrajyoti Datta 				& WZRD_VERSAL_FRAC_MASK;
6053a96393aSShubhrajyoti Datta 		mult = mult * WZRD_FRAC_GRADIENT + regl;
6063a96393aSShubhrajyoti Datta 		parent_rate = DIV_ROUND_CLOSEST((parent_rate * mult), WZRD_FRAC_GRADIENT);
6073a96393aSShubhrajyoti Datta 	} else {
6083a96393aSShubhrajyoti Datta 		parent_rate = parent_rate * mult;
6093a96393aSShubhrajyoti Datta 	}
6103a96393aSShubhrajyoti Datta 
6113a96393aSShubhrajyoti Datta 	/* O Calculation */
6123a96393aSShubhrajyoti Datta 	reg = readl(divider->base + WZRD_CLK_CFG_REG(1, WZRD_CLKOUT0_1));
6133a96393aSShubhrajyoti Datta 	edged = FIELD_GET(WZRD_CLKFBOUT_EDGE, reg);
6143a96393aSShubhrajyoti Datta 	p5en = FIELD_GET(WZRD_P5EN, reg);
6153a96393aSShubhrajyoti Datta 	prediv2 = FIELD_GET(WZRD_CLKOUT0_PREDIV2, reg);
6163a96393aSShubhrajyoti Datta 
6173a96393aSShubhrajyoti Datta 	reg = readl(divider->base + WZRD_CLK_CFG_REG(1, WZRD_CLKOUT0_2));
6183a96393aSShubhrajyoti Datta 	/* Low time */
6193a96393aSShubhrajyoti Datta 	regl = FIELD_GET(WZRD_CLKFBOUT_L_MASK, reg);
6203a96393aSShubhrajyoti Datta 	/* High time */
6213a96393aSShubhrajyoti Datta 	regh = FIELD_GET(WZRD_CLKFBOUT_H_MASK, reg);
6223a96393aSShubhrajyoti Datta 	all = regh + regl + edged;
6233a96393aSShubhrajyoti Datta 	if (!all)
6243a96393aSShubhrajyoti Datta 		all = 1;
6253a96393aSShubhrajyoti Datta 
6263a96393aSShubhrajyoti Datta 	if (prediv2)
6273a96393aSShubhrajyoti Datta 		div2 = PREDIV2_MULT * all + p5en;
6283a96393aSShubhrajyoti Datta 	else
6293a96393aSShubhrajyoti Datta 		div2 = all;
6303a96393aSShubhrajyoti Datta 
6313a96393aSShubhrajyoti Datta 	/* D calculation */
6323a96393aSShubhrajyoti Datta 	edged = !!(readl(divider->base + WZRD_CLK_CFG_REG(1, WZRD_DESKEW_2)) &
6333a96393aSShubhrajyoti Datta 		     WZRD_DIVCLK_EDGE);
6343a96393aSShubhrajyoti Datta 	reg = readl(divider->base + WZRD_CLK_CFG_REG(1, WZRD_DIVCLK));
6353a96393aSShubhrajyoti Datta 	/* Low time */
6363a96393aSShubhrajyoti Datta 	regl = FIELD_GET(WZRD_CLKFBOUT_L_MASK, reg);
6373a96393aSShubhrajyoti Datta 	/* High time */
6383a96393aSShubhrajyoti Datta 	regh = FIELD_GET(WZRD_CLKFBOUT_H_MASK, reg);
6393a96393aSShubhrajyoti Datta 	div = regl + regh + edged;
6403a96393aSShubhrajyoti Datta 	if (!div)
6413a96393aSShubhrajyoti Datta 		div = 1;
6423a96393aSShubhrajyoti Datta 
6433a96393aSShubhrajyoti Datta 	div = div * div2;
6443a96393aSShubhrajyoti Datta 	return divider_recalc_rate(hw, parent_rate, div, divider->table,
6453a96393aSShubhrajyoti Datta 			divider->flags, divider->width);
6463a96393aSShubhrajyoti Datta }
6473a96393aSShubhrajyoti Datta 
clk_wzrd_round_rate_all(struct clk_hw * hw,unsigned long rate,unsigned long * prate)648595c88cdSShubhrajyoti Datta static long clk_wzrd_round_rate_all(struct clk_hw *hw, unsigned long rate,
649595c88cdSShubhrajyoti Datta 				    unsigned long *prate)
650595c88cdSShubhrajyoti Datta {
651595c88cdSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
652595c88cdSShubhrajyoti Datta 	unsigned long int_freq;
653595c88cdSShubhrajyoti Datta 	u32 m, d, o, div, f;
654595c88cdSShubhrajyoti Datta 	int err;
655595c88cdSShubhrajyoti Datta 
656595c88cdSShubhrajyoti Datta 	err = clk_wzrd_get_divisors(hw, rate, *prate);
657595c88cdSShubhrajyoti Datta 	if (err)
658595c88cdSShubhrajyoti Datta 		return err;
659595c88cdSShubhrajyoti Datta 
660595c88cdSShubhrajyoti Datta 	m = divider->m;
661595c88cdSShubhrajyoti Datta 	d = divider->d;
662595c88cdSShubhrajyoti Datta 	o = divider->o;
663595c88cdSShubhrajyoti Datta 
664595c88cdSShubhrajyoti Datta 	div = d * o;
665595c88cdSShubhrajyoti Datta 	int_freq =  divider_recalc_rate(hw, *prate * m, div, divider->table,
666595c88cdSShubhrajyoti Datta 					divider->flags, divider->width);
667595c88cdSShubhrajyoti Datta 
668595c88cdSShubhrajyoti Datta 	if (rate > int_freq) {
669595c88cdSShubhrajyoti Datta 		f = DIV_ROUND_CLOSEST_ULL(rate * WZRD_FRAC_POINTS, int_freq);
670595c88cdSShubhrajyoti Datta 		rate = DIV_ROUND_CLOSEST(int_freq * f, WZRD_FRAC_POINTS);
671595c88cdSShubhrajyoti Datta 	}
672595c88cdSShubhrajyoti Datta 	return rate;
673595c88cdSShubhrajyoti Datta }
674595c88cdSShubhrajyoti Datta 
6753a96393aSShubhrajyoti Datta static const struct clk_ops clk_wzrd_ver_divider_ops = {
6763a96393aSShubhrajyoti Datta 	.round_rate = clk_wzrd_round_rate,
6773a96393aSShubhrajyoti Datta 	.set_rate = clk_wzrd_ver_dynamic_reconfig,
6783a96393aSShubhrajyoti Datta 	.recalc_rate = clk_wzrd_recalc_rate_ver,
6793a96393aSShubhrajyoti Datta };
6803a96393aSShubhrajyoti Datta 
6813a96393aSShubhrajyoti Datta static const struct clk_ops clk_wzrd_ver_div_all_ops = {
6823a96393aSShubhrajyoti Datta 	.round_rate = clk_wzrd_round_rate_all,
6833a96393aSShubhrajyoti Datta 	.set_rate = clk_wzrd_dynamic_all_ver,
6843a96393aSShubhrajyoti Datta 	.recalc_rate = clk_wzrd_recalc_rate_all_ver,
6853a96393aSShubhrajyoti Datta };
6863a96393aSShubhrajyoti Datta 
687c822490fSShubhrajyoti Datta static const struct clk_ops clk_wzrd_clk_divider_ops = {
688c822490fSShubhrajyoti Datta 	.round_rate = clk_wzrd_round_rate,
689c822490fSShubhrajyoti Datta 	.set_rate = clk_wzrd_dynamic_reconfig,
690c822490fSShubhrajyoti Datta 	.recalc_rate = clk_wzrd_recalc_rate,
691c822490fSShubhrajyoti Datta };
692c822490fSShubhrajyoti Datta 
693595c88cdSShubhrajyoti Datta static const struct clk_ops clk_wzrd_clk_div_all_ops = {
694595c88cdSShubhrajyoti Datta 	.round_rate = clk_wzrd_round_rate_all,
695595c88cdSShubhrajyoti Datta 	.set_rate = clk_wzrd_dynamic_all,
696595c88cdSShubhrajyoti Datta 	.recalc_rate = clk_wzrd_recalc_rate_all,
697595c88cdSShubhrajyoti Datta };
698595c88cdSShubhrajyoti Datta 
clk_wzrd_recalc_ratef(struct clk_hw * hw,unsigned long parent_rate)699c822490fSShubhrajyoti Datta static unsigned long clk_wzrd_recalc_ratef(struct clk_hw *hw,
700c822490fSShubhrajyoti Datta 					   unsigned long parent_rate)
701c822490fSShubhrajyoti Datta {
702c822490fSShubhrajyoti Datta 	unsigned int val;
703c822490fSShubhrajyoti Datta 	u32 div, frac;
704c822490fSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
705c822490fSShubhrajyoti Datta 	void __iomem *div_addr = divider->base + divider->offset;
706c822490fSShubhrajyoti Datta 
707c822490fSShubhrajyoti Datta 	val = readl(div_addr);
708c822490fSShubhrajyoti Datta 	div = val & div_mask(divider->width);
709c822490fSShubhrajyoti Datta 	frac = (val >> WZRD_CLKOUT_FRAC_SHIFT) & WZRD_CLKOUT_FRAC_MASK;
710c822490fSShubhrajyoti Datta 
711c822490fSShubhrajyoti Datta 	return mult_frac(parent_rate, 1000, (div * 1000) + frac);
712c822490fSShubhrajyoti Datta }
713c822490fSShubhrajyoti Datta 
clk_wzrd_dynamic_reconfig_f(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)714c822490fSShubhrajyoti Datta static int clk_wzrd_dynamic_reconfig_f(struct clk_hw *hw, unsigned long rate,
715c822490fSShubhrajyoti Datta 				       unsigned long parent_rate)
716c822490fSShubhrajyoti Datta {
717c822490fSShubhrajyoti Datta 	int err;
718c822490fSShubhrajyoti Datta 	u32 value, pre;
719c822490fSShubhrajyoti Datta 	unsigned long rate_div, f, clockout0_div;
720c822490fSShubhrajyoti Datta 	struct clk_wzrd_divider *divider = to_clk_wzrd_divider(hw);
721c822490fSShubhrajyoti Datta 	void __iomem *div_addr = divider->base + divider->offset;
722c822490fSShubhrajyoti Datta 
723dd5e7431SShubhrajyoti Datta 	rate_div = DIV_ROUND_DOWN_ULL(parent_rate * 1000, rate);
724c822490fSShubhrajyoti Datta 	clockout0_div = rate_div / 1000;
725c822490fSShubhrajyoti Datta 
726c822490fSShubhrajyoti Datta 	pre = DIV_ROUND_CLOSEST((parent_rate * 1000), rate);
727c822490fSShubhrajyoti Datta 	f = (u32)(pre - (clockout0_div * 1000));
728c822490fSShubhrajyoti Datta 	f = f & WZRD_CLKOUT_FRAC_MASK;
729c822490fSShubhrajyoti Datta 	f = f << WZRD_CLKOUT_DIVIDE_WIDTH;
730c822490fSShubhrajyoti Datta 
731c822490fSShubhrajyoti Datta 	value = (f  | (clockout0_div & WZRD_CLKOUT_DIVIDE_MASK));
732c822490fSShubhrajyoti Datta 
733c822490fSShubhrajyoti Datta 	/* Set divisor and clear phase offset */
734c822490fSShubhrajyoti Datta 	writel(value, div_addr);
735c822490fSShubhrajyoti Datta 	writel(0x0, div_addr + WZRD_DR_DIV_TO_PHASE_OFFSET);
736c822490fSShubhrajyoti Datta 
737c822490fSShubhrajyoti Datta 	/* Check status register */
738c822490fSShubhrajyoti Datta 	err = readl_poll_timeout(divider->base + WZRD_DR_STATUS_REG_OFFSET, value,
739c822490fSShubhrajyoti Datta 				 value & WZRD_DR_LOCK_BIT_MASK,
740c822490fSShubhrajyoti Datta 				 WZRD_USEC_POLL, WZRD_TIMEOUT_POLL);
741c822490fSShubhrajyoti Datta 	if (err)
742c822490fSShubhrajyoti Datta 		return err;
743c822490fSShubhrajyoti Datta 
744c822490fSShubhrajyoti Datta 	/* Initiate reconfiguration */
745dd5e7431SShubhrajyoti Datta 	writel(WZRD_DR_BEGIN_DYNA_RECONF_5_2,
746dd5e7431SShubhrajyoti Datta 	       divider->base + WZRD_DR_INIT_REG_OFFSET);
747dd5e7431SShubhrajyoti Datta 	writel(WZRD_DR_BEGIN_DYNA_RECONF1_5_2,
748c822490fSShubhrajyoti Datta 	       divider->base + WZRD_DR_INIT_REG_OFFSET);
749c822490fSShubhrajyoti Datta 
750c822490fSShubhrajyoti Datta 	/* Check status register */
751c822490fSShubhrajyoti Datta 	return readl_poll_timeout(divider->base + WZRD_DR_STATUS_REG_OFFSET, value,
752c822490fSShubhrajyoti Datta 				value & WZRD_DR_LOCK_BIT_MASK,
753c822490fSShubhrajyoti Datta 				WZRD_USEC_POLL, WZRD_TIMEOUT_POLL);
754c822490fSShubhrajyoti Datta }
755c822490fSShubhrajyoti Datta 
clk_wzrd_round_rate_f(struct clk_hw * hw,unsigned long rate,unsigned long * prate)756c822490fSShubhrajyoti Datta static long clk_wzrd_round_rate_f(struct clk_hw *hw, unsigned long rate,
757c822490fSShubhrajyoti Datta 				  unsigned long *prate)
758c822490fSShubhrajyoti Datta {
759c822490fSShubhrajyoti Datta 	return rate;
760c822490fSShubhrajyoti Datta }
761c822490fSShubhrajyoti Datta 
762c822490fSShubhrajyoti Datta static const struct clk_ops clk_wzrd_clk_divider_ops_f = {
763c822490fSShubhrajyoti Datta 	.round_rate = clk_wzrd_round_rate_f,
764c822490fSShubhrajyoti Datta 	.set_rate = clk_wzrd_dynamic_reconfig_f,
765c822490fSShubhrajyoti Datta 	.recalc_rate = clk_wzrd_recalc_ratef,
766c822490fSShubhrajyoti Datta };
767c822490fSShubhrajyoti Datta 
clk_wzrd_register_divf(struct device * dev,const char * name,const char * parent_name,unsigned long flags,void __iomem * base,u16 offset,u8 shift,u8 width,u8 clk_divider_flags,u32 div_type,spinlock_t * lock)768c822490fSShubhrajyoti Datta static struct clk *clk_wzrd_register_divf(struct device *dev,
769c822490fSShubhrajyoti Datta 					  const char *name,
770c822490fSShubhrajyoti Datta 					  const char *parent_name,
771c822490fSShubhrajyoti Datta 					  unsigned long flags,
772c822490fSShubhrajyoti Datta 					  void __iomem *base, u16 offset,
773c822490fSShubhrajyoti Datta 					  u8 shift, u8 width,
774c822490fSShubhrajyoti Datta 					  u8 clk_divider_flags,
775595c88cdSShubhrajyoti Datta 					  u32 div_type,
776c822490fSShubhrajyoti Datta 					  spinlock_t *lock)
777c822490fSShubhrajyoti Datta {
778c822490fSShubhrajyoti Datta 	struct clk_wzrd_divider *div;
779c822490fSShubhrajyoti Datta 	struct clk_hw *hw;
780c822490fSShubhrajyoti Datta 	struct clk_init_data init;
781c822490fSShubhrajyoti Datta 	int ret;
782c822490fSShubhrajyoti Datta 
783c822490fSShubhrajyoti Datta 	div = devm_kzalloc(dev, sizeof(*div), GFP_KERNEL);
784c822490fSShubhrajyoti Datta 	if (!div)
785c822490fSShubhrajyoti Datta 		return ERR_PTR(-ENOMEM);
786c822490fSShubhrajyoti Datta 
787c822490fSShubhrajyoti Datta 	init.name = name;
788c822490fSShubhrajyoti Datta 
789c822490fSShubhrajyoti Datta 	init.ops = &clk_wzrd_clk_divider_ops_f;
790c822490fSShubhrajyoti Datta 
791c822490fSShubhrajyoti Datta 	init.flags = flags;
792c822490fSShubhrajyoti Datta 	init.parent_names = &parent_name;
793c822490fSShubhrajyoti Datta 	init.num_parents = 1;
794c822490fSShubhrajyoti Datta 
795c822490fSShubhrajyoti Datta 	div->base = base;
796c822490fSShubhrajyoti Datta 	div->offset = offset;
797c822490fSShubhrajyoti Datta 	div->shift = shift;
798c822490fSShubhrajyoti Datta 	div->width = width;
799c822490fSShubhrajyoti Datta 	div->flags = clk_divider_flags;
800c822490fSShubhrajyoti Datta 	div->lock = lock;
801c822490fSShubhrajyoti Datta 	div->hw.init = &init;
802c822490fSShubhrajyoti Datta 
803c822490fSShubhrajyoti Datta 	hw = &div->hw;
804c822490fSShubhrajyoti Datta 	ret =  devm_clk_hw_register(dev, hw);
805c822490fSShubhrajyoti Datta 	if (ret)
806c822490fSShubhrajyoti Datta 		return ERR_PTR(ret);
807c822490fSShubhrajyoti Datta 
808c822490fSShubhrajyoti Datta 	return hw->clk;
809c822490fSShubhrajyoti Datta }
810c822490fSShubhrajyoti Datta 
clk_wzrd_ver_register_divider(struct device * dev,const char * name,const char * parent_name,unsigned long flags,void __iomem * base,u16 offset,u8 shift,u8 width,u8 clk_divider_flags,u32 div_type,spinlock_t * lock)8113a96393aSShubhrajyoti Datta static struct clk *clk_wzrd_ver_register_divider(struct device *dev,
8123a96393aSShubhrajyoti Datta 						 const char *name,
8133a96393aSShubhrajyoti Datta 						 const char *parent_name,
8143a96393aSShubhrajyoti Datta 						 unsigned long flags,
8153a96393aSShubhrajyoti Datta 						 void __iomem *base,
8163a96393aSShubhrajyoti Datta 						 u16 offset,
8173a96393aSShubhrajyoti Datta 						 u8 shift, u8 width,
8183a96393aSShubhrajyoti Datta 						 u8 clk_divider_flags,
8193a96393aSShubhrajyoti Datta 						 u32 div_type,
8203a96393aSShubhrajyoti Datta 						 spinlock_t *lock)
8213a96393aSShubhrajyoti Datta {
8223a96393aSShubhrajyoti Datta 	struct clk_wzrd_divider *div;
8233a96393aSShubhrajyoti Datta 	struct clk_hw *hw;
8243a96393aSShubhrajyoti Datta 	struct clk_init_data init;
8253a96393aSShubhrajyoti Datta 	int ret;
8263a96393aSShubhrajyoti Datta 
8273a96393aSShubhrajyoti Datta 	div = devm_kzalloc(dev, sizeof(*div), GFP_KERNEL);
8283a96393aSShubhrajyoti Datta 	if (!div)
8293a96393aSShubhrajyoti Datta 		return ERR_PTR(-ENOMEM);
8303a96393aSShubhrajyoti Datta 
8313a96393aSShubhrajyoti Datta 	init.name = name;
8323a96393aSShubhrajyoti Datta 	if (clk_divider_flags & CLK_DIVIDER_READ_ONLY)
8333a96393aSShubhrajyoti Datta 		init.ops = &clk_divider_ro_ops;
8343a96393aSShubhrajyoti Datta 	else if (div_type == DIV_O)
8353a96393aSShubhrajyoti Datta 		init.ops = &clk_wzrd_ver_divider_ops;
8363a96393aSShubhrajyoti Datta 	else
8373a96393aSShubhrajyoti Datta 		init.ops = &clk_wzrd_ver_div_all_ops;
8383a96393aSShubhrajyoti Datta 	init.flags = flags;
8393a96393aSShubhrajyoti Datta 	init.parent_names =  &parent_name;
8403a96393aSShubhrajyoti Datta 	init.num_parents =  1;
8413a96393aSShubhrajyoti Datta 
8423a96393aSShubhrajyoti Datta 	div->base = base;
8433a96393aSShubhrajyoti Datta 	div->offset = offset;
8443a96393aSShubhrajyoti Datta 	div->shift = shift;
8453a96393aSShubhrajyoti Datta 	div->width = width;
8463a96393aSShubhrajyoti Datta 	div->flags = clk_divider_flags;
8473a96393aSShubhrajyoti Datta 	div->lock = lock;
8483a96393aSShubhrajyoti Datta 	div->hw.init = &init;
8493a96393aSShubhrajyoti Datta 
8503a96393aSShubhrajyoti Datta 	hw = &div->hw;
8513a96393aSShubhrajyoti Datta 	ret = devm_clk_hw_register(dev, hw);
8523a96393aSShubhrajyoti Datta 	if (ret)
8533a96393aSShubhrajyoti Datta 		return ERR_PTR(ret);
8543a96393aSShubhrajyoti Datta 
8553a96393aSShubhrajyoti Datta 	return hw->clk;
8563a96393aSShubhrajyoti Datta }
8573a96393aSShubhrajyoti Datta 
clk_wzrd_register_divider(struct device * dev,const char * name,const char * parent_name,unsigned long flags,void __iomem * base,u16 offset,u8 shift,u8 width,u8 clk_divider_flags,u32 div_type,spinlock_t * lock)858c822490fSShubhrajyoti Datta static struct clk *clk_wzrd_register_divider(struct device *dev,
859c822490fSShubhrajyoti Datta 					     const char *name,
860c822490fSShubhrajyoti Datta 					     const char *parent_name,
861c822490fSShubhrajyoti Datta 					     unsigned long flags,
862c822490fSShubhrajyoti Datta 					     void __iomem *base, u16 offset,
863c822490fSShubhrajyoti Datta 					     u8 shift, u8 width,
864c822490fSShubhrajyoti Datta 					     u8 clk_divider_flags,
865595c88cdSShubhrajyoti Datta 					     u32 div_type,
866c822490fSShubhrajyoti Datta 					     spinlock_t *lock)
867c822490fSShubhrajyoti Datta {
868c822490fSShubhrajyoti Datta 	struct clk_wzrd_divider *div;
869c822490fSShubhrajyoti Datta 	struct clk_hw *hw;
870c822490fSShubhrajyoti Datta 	struct clk_init_data init;
871c822490fSShubhrajyoti Datta 	int ret;
872c822490fSShubhrajyoti Datta 
873c822490fSShubhrajyoti Datta 	div = devm_kzalloc(dev, sizeof(*div), GFP_KERNEL);
874c822490fSShubhrajyoti Datta 	if (!div)
875c822490fSShubhrajyoti Datta 		return ERR_PTR(-ENOMEM);
876c822490fSShubhrajyoti Datta 
877c822490fSShubhrajyoti Datta 	init.name = name;
878595c88cdSShubhrajyoti Datta 	if (clk_divider_flags & CLK_DIVIDER_READ_ONLY)
879595c88cdSShubhrajyoti Datta 		init.ops = &clk_divider_ro_ops;
880595c88cdSShubhrajyoti Datta 	else if (div_type == DIV_O)
881c822490fSShubhrajyoti Datta 		init.ops = &clk_wzrd_clk_divider_ops;
882595c88cdSShubhrajyoti Datta 	else
883595c88cdSShubhrajyoti Datta 		init.ops = &clk_wzrd_clk_div_all_ops;
884c822490fSShubhrajyoti Datta 	init.flags = flags;
885c822490fSShubhrajyoti Datta 	init.parent_names =  &parent_name;
886c822490fSShubhrajyoti Datta 	init.num_parents =  1;
887c822490fSShubhrajyoti Datta 
888c822490fSShubhrajyoti Datta 	div->base = base;
889c822490fSShubhrajyoti Datta 	div->offset = offset;
890c822490fSShubhrajyoti Datta 	div->shift = shift;
891c822490fSShubhrajyoti Datta 	div->width = width;
892c822490fSShubhrajyoti Datta 	div->flags = clk_divider_flags;
893c822490fSShubhrajyoti Datta 	div->lock = lock;
894c822490fSShubhrajyoti Datta 	div->hw.init = &init;
895c822490fSShubhrajyoti Datta 
896c822490fSShubhrajyoti Datta 	hw = &div->hw;
897c822490fSShubhrajyoti Datta 	ret = devm_clk_hw_register(dev, hw);
898c822490fSShubhrajyoti Datta 	if (ret)
8999c632a63SDan Carpenter 		return ERR_PTR(ret);
900c822490fSShubhrajyoti Datta 
901c822490fSShubhrajyoti Datta 	return hw->clk;
902c822490fSShubhrajyoti Datta }
903c822490fSShubhrajyoti Datta 
clk_wzrd_clk_notifier(struct notifier_block * nb,unsigned long event,void * data)904c822490fSShubhrajyoti Datta static int clk_wzrd_clk_notifier(struct notifier_block *nb, unsigned long event,
905c822490fSShubhrajyoti Datta 				 void *data)
906c822490fSShubhrajyoti Datta {
907c822490fSShubhrajyoti Datta 	unsigned long max;
908c822490fSShubhrajyoti Datta 	struct clk_notifier_data *ndata = data;
909c822490fSShubhrajyoti Datta 	struct clk_wzrd *clk_wzrd = to_clk_wzrd(nb);
910c822490fSShubhrajyoti Datta 
911c822490fSShubhrajyoti Datta 	if (clk_wzrd->suspended)
912c822490fSShubhrajyoti Datta 		return NOTIFY_OK;
913c822490fSShubhrajyoti Datta 
914c822490fSShubhrajyoti Datta 	if (ndata->clk == clk_wzrd->clk_in1)
915c822490fSShubhrajyoti Datta 		max = clk_wzrd_max_freq[clk_wzrd->speed_grade - 1];
916c822490fSShubhrajyoti Datta 	else if (ndata->clk == clk_wzrd->axi_clk)
917c822490fSShubhrajyoti Datta 		max = WZRD_ACLK_MAX_FREQ;
918c822490fSShubhrajyoti Datta 	else
919c822490fSShubhrajyoti Datta 		return NOTIFY_DONE;	/* should never happen */
920c822490fSShubhrajyoti Datta 
921c822490fSShubhrajyoti Datta 	switch (event) {
922c822490fSShubhrajyoti Datta 	case PRE_RATE_CHANGE:
923c822490fSShubhrajyoti Datta 		if (ndata->new_rate > max)
924c822490fSShubhrajyoti Datta 			return NOTIFY_BAD;
925c822490fSShubhrajyoti Datta 		return NOTIFY_OK;
926c822490fSShubhrajyoti Datta 	case POST_RATE_CHANGE:
927c822490fSShubhrajyoti Datta 	case ABORT_RATE_CHANGE:
928c822490fSShubhrajyoti Datta 	default:
929c822490fSShubhrajyoti Datta 		return NOTIFY_DONE;
930c822490fSShubhrajyoti Datta 	}
931c822490fSShubhrajyoti Datta }
932c822490fSShubhrajyoti Datta 
clk_wzrd_suspend(struct device * dev)933c822490fSShubhrajyoti Datta static int __maybe_unused clk_wzrd_suspend(struct device *dev)
934c822490fSShubhrajyoti Datta {
935c822490fSShubhrajyoti Datta 	struct clk_wzrd *clk_wzrd = dev_get_drvdata(dev);
936c822490fSShubhrajyoti Datta 
937c822490fSShubhrajyoti Datta 	clk_disable_unprepare(clk_wzrd->axi_clk);
938c822490fSShubhrajyoti Datta 	clk_wzrd->suspended = true;
939c822490fSShubhrajyoti Datta 
940c822490fSShubhrajyoti Datta 	return 0;
941c822490fSShubhrajyoti Datta }
942c822490fSShubhrajyoti Datta 
clk_wzrd_resume(struct device * dev)943c822490fSShubhrajyoti Datta static int __maybe_unused clk_wzrd_resume(struct device *dev)
944c822490fSShubhrajyoti Datta {
945c822490fSShubhrajyoti Datta 	int ret;
946c822490fSShubhrajyoti Datta 	struct clk_wzrd *clk_wzrd = dev_get_drvdata(dev);
947c822490fSShubhrajyoti Datta 
948c822490fSShubhrajyoti Datta 	ret = clk_prepare_enable(clk_wzrd->axi_clk);
949c822490fSShubhrajyoti Datta 	if (ret) {
950c822490fSShubhrajyoti Datta 		dev_err(dev, "unable to enable s_axi_aclk\n");
951c822490fSShubhrajyoti Datta 		return ret;
952c822490fSShubhrajyoti Datta 	}
953c822490fSShubhrajyoti Datta 
954c822490fSShubhrajyoti Datta 	clk_wzrd->suspended = false;
955c822490fSShubhrajyoti Datta 
956c822490fSShubhrajyoti Datta 	return 0;
957c822490fSShubhrajyoti Datta }
958c822490fSShubhrajyoti Datta 
959c822490fSShubhrajyoti Datta static SIMPLE_DEV_PM_OPS(clk_wzrd_dev_pm_ops, clk_wzrd_suspend,
960c822490fSShubhrajyoti Datta 			 clk_wzrd_resume);
961c822490fSShubhrajyoti Datta 
9623a96393aSShubhrajyoti Datta static const struct versal_clk_data versal_data = {
9633a96393aSShubhrajyoti Datta 	.is_versal	= true,
9643a96393aSShubhrajyoti Datta };
9653a96393aSShubhrajyoti Datta 
clk_wzrd_probe(struct platform_device * pdev)966c822490fSShubhrajyoti Datta static int clk_wzrd_probe(struct platform_device *pdev)
967c822490fSShubhrajyoti Datta {
9683a96393aSShubhrajyoti Datta 	const char *clkout_name, *clk_name, *clk_mul_name;
9693a96393aSShubhrajyoti Datta 	u32 regl, regh, edge, regld, reghd, edged, div;
970c822490fSShubhrajyoti Datta 	struct device_node *np = pdev->dev.of_node;
9713a96393aSShubhrajyoti Datta 	const struct versal_clk_data *data;
9723a96393aSShubhrajyoti Datta 	struct clk_wzrd *clk_wzrd;
973c822490fSShubhrajyoti Datta 	unsigned long flags = 0;
9743a96393aSShubhrajyoti Datta 	void __iomem *ctrl_reg;
9753a96393aSShubhrajyoti Datta 	u32 reg, reg_f, mult;
9763a96393aSShubhrajyoti Datta 	bool is_versal = false;
9773a96393aSShubhrajyoti Datta 	unsigned long rate;
9783a96393aSShubhrajyoti Datta 	int nr_outputs;
9793a96393aSShubhrajyoti Datta 	int i, ret;
980c822490fSShubhrajyoti Datta 
981c822490fSShubhrajyoti Datta 	clk_wzrd = devm_kzalloc(&pdev->dev, sizeof(*clk_wzrd), GFP_KERNEL);
982c822490fSShubhrajyoti Datta 	if (!clk_wzrd)
983c822490fSShubhrajyoti Datta 		return -ENOMEM;
984c822490fSShubhrajyoti Datta 	platform_set_drvdata(pdev, clk_wzrd);
985c822490fSShubhrajyoti Datta 
986c822490fSShubhrajyoti Datta 	clk_wzrd->base = devm_platform_ioremap_resource(pdev, 0);
987c822490fSShubhrajyoti Datta 	if (IS_ERR(clk_wzrd->base))
988c822490fSShubhrajyoti Datta 		return PTR_ERR(clk_wzrd->base);
989c822490fSShubhrajyoti Datta 
990c822490fSShubhrajyoti Datta 	ret = of_property_read_u32(np, "xlnx,speed-grade", &clk_wzrd->speed_grade);
991c822490fSShubhrajyoti Datta 	if (!ret) {
992c822490fSShubhrajyoti Datta 		if (clk_wzrd->speed_grade < 1 || clk_wzrd->speed_grade > 3) {
993c822490fSShubhrajyoti Datta 			dev_warn(&pdev->dev, "invalid speed grade '%d'\n",
994c822490fSShubhrajyoti Datta 				 clk_wzrd->speed_grade);
995c822490fSShubhrajyoti Datta 			clk_wzrd->speed_grade = 0;
996c822490fSShubhrajyoti Datta 		}
997c822490fSShubhrajyoti Datta 	}
998c822490fSShubhrajyoti Datta 
999c822490fSShubhrajyoti Datta 	clk_wzrd->clk_in1 = devm_clk_get(&pdev->dev, "clk_in1");
1000fd30ac84SYang Yingliang 	if (IS_ERR(clk_wzrd->clk_in1))
1001fd30ac84SYang Yingliang 		return dev_err_probe(&pdev->dev, PTR_ERR(clk_wzrd->clk_in1),
1002fd30ac84SYang Yingliang 				     "clk_in1 not found\n");
1003c822490fSShubhrajyoti Datta 
1004c822490fSShubhrajyoti Datta 	clk_wzrd->axi_clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
1005fd30ac84SYang Yingliang 	if (IS_ERR(clk_wzrd->axi_clk))
1006fd30ac84SYang Yingliang 		return dev_err_probe(&pdev->dev, PTR_ERR(clk_wzrd->axi_clk),
1007fd30ac84SYang Yingliang 				     "s_axi_aclk not found\n");
1008c822490fSShubhrajyoti Datta 	ret = clk_prepare_enable(clk_wzrd->axi_clk);
1009c822490fSShubhrajyoti Datta 	if (ret) {
1010c822490fSShubhrajyoti Datta 		dev_err(&pdev->dev, "enabling s_axi_aclk failed\n");
1011c822490fSShubhrajyoti Datta 		return ret;
1012c822490fSShubhrajyoti Datta 	}
1013c822490fSShubhrajyoti Datta 	rate = clk_get_rate(clk_wzrd->axi_clk);
1014c822490fSShubhrajyoti Datta 	if (rate > WZRD_ACLK_MAX_FREQ) {
1015c822490fSShubhrajyoti Datta 		dev_err(&pdev->dev, "s_axi_aclk frequency (%lu) too high\n",
1016c822490fSShubhrajyoti Datta 			rate);
1017c822490fSShubhrajyoti Datta 		ret = -EINVAL;
1018c822490fSShubhrajyoti Datta 		goto err_disable_clk;
1019c822490fSShubhrajyoti Datta 	}
1020c822490fSShubhrajyoti Datta 
10213a96393aSShubhrajyoti Datta 	data = device_get_match_data(&pdev->dev);
10223a96393aSShubhrajyoti Datta 	if (data)
10233a96393aSShubhrajyoti Datta 		is_versal = data->is_versal;
10243a96393aSShubhrajyoti Datta 
1025595c88cdSShubhrajyoti Datta 	ret = of_property_read_u32(np, "xlnx,nr-outputs", &nr_outputs);
1026595c88cdSShubhrajyoti Datta 	if (ret || nr_outputs > WZRD_NUM_OUTPUTS) {
1027595c88cdSShubhrajyoti Datta 		ret = -EINVAL;
1028595c88cdSShubhrajyoti Datta 		goto err_disable_clk;
1029595c88cdSShubhrajyoti Datta 	}
1030595c88cdSShubhrajyoti Datta 
1031595c88cdSShubhrajyoti Datta 	clkout_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_out0", dev_name(&pdev->dev));
1032b1356ed1SClaudiu Beznea 	if (!clkout_name) {
1033b1356ed1SClaudiu Beznea 		ret = -ENOMEM;
1034b1356ed1SClaudiu Beznea 		goto err_disable_clk;
1035b1356ed1SClaudiu Beznea 	}
1036b1356ed1SClaudiu Beznea 
10373a96393aSShubhrajyoti Datta 	if (is_versal) {
1038595c88cdSShubhrajyoti Datta 		if (nr_outputs == 1) {
10393a96393aSShubhrajyoti Datta 			clk_wzrd->clkout[0] = clk_wzrd_ver_register_divider
1040595c88cdSShubhrajyoti Datta 				(&pdev->dev, clkout_name,
1041595c88cdSShubhrajyoti Datta 				__clk_get_name(clk_wzrd->clk_in1), 0,
10423a96393aSShubhrajyoti Datta 				clk_wzrd->base, WZRD_CLK_CFG_REG(is_versal, 3),
1043595c88cdSShubhrajyoti Datta 				WZRD_CLKOUT_DIVIDE_SHIFT,
1044595c88cdSShubhrajyoti Datta 				WZRD_CLKOUT_DIVIDE_WIDTH,
1045595c88cdSShubhrajyoti Datta 				CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
1046595c88cdSShubhrajyoti Datta 				DIV_ALL, &clkwzrd_lock);
1047595c88cdSShubhrajyoti Datta 
1048595c88cdSShubhrajyoti Datta 			goto out;
1049595c88cdSShubhrajyoti Datta 		}
10503a96393aSShubhrajyoti Datta 		/* register multiplier */
10513a96393aSShubhrajyoti Datta 		edge = !!(readl(clk_wzrd->base + WZRD_CLK_CFG_REG(is_versal, 0)) &
10523a96393aSShubhrajyoti Datta 				BIT(8));
10533a96393aSShubhrajyoti Datta 		regl = (readl(clk_wzrd->base + WZRD_CLK_CFG_REG(is_versal, 1)) &
10543a96393aSShubhrajyoti Datta 			     WZRD_CLKFBOUT_L_MASK) >> WZRD_CLKFBOUT_L_SHIFT;
10553a96393aSShubhrajyoti Datta 		regh = (readl(clk_wzrd->base + WZRD_CLK_CFG_REG(is_versal, 1)) &
10563a96393aSShubhrajyoti Datta 			     WZRD_CLKFBOUT_H_MASK) >> WZRD_CLKFBOUT_H_SHIFT;
10573a96393aSShubhrajyoti Datta 		mult = regl + regh + edge;
10583a96393aSShubhrajyoti Datta 		if (!mult)
10593a96393aSShubhrajyoti Datta 			mult = 1;
10603a96393aSShubhrajyoti Datta 		mult = mult * WZRD_FRAC_GRADIENT;
1061595c88cdSShubhrajyoti Datta 
10623a96393aSShubhrajyoti Datta 		regl = readl(clk_wzrd->base + WZRD_CLK_CFG_REG(is_versal, 51)) &
10633a96393aSShubhrajyoti Datta 			     WZRD_CLKFBOUT_FRAC_EN;
10643a96393aSShubhrajyoti Datta 		if (regl) {
10653a96393aSShubhrajyoti Datta 			regl = readl(clk_wzrd->base + WZRD_CLK_CFG_REG(is_versal, 48)) &
10663a96393aSShubhrajyoti Datta 				WZRD_VERSAL_FRAC_MASK;
10673a96393aSShubhrajyoti Datta 			mult = mult + regl;
10683a96393aSShubhrajyoti Datta 		}
10693a96393aSShubhrajyoti Datta 		div = 64;
10703a96393aSShubhrajyoti Datta 	} else {
10713a96393aSShubhrajyoti Datta 		if (nr_outputs == 1) {
10723a96393aSShubhrajyoti Datta 			clk_wzrd->clkout[0] = clk_wzrd_register_divider
10733a96393aSShubhrajyoti Datta 				(&pdev->dev, clkout_name,
10743a96393aSShubhrajyoti Datta 				__clk_get_name(clk_wzrd->clk_in1), 0,
10753a96393aSShubhrajyoti Datta 				clk_wzrd->base, WZRD_CLK_CFG_REG(is_versal, 3),
10763a96393aSShubhrajyoti Datta 				WZRD_CLKOUT_DIVIDE_SHIFT,
10773a96393aSShubhrajyoti Datta 				WZRD_CLKOUT_DIVIDE_WIDTH,
10783a96393aSShubhrajyoti Datta 				CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
10793a96393aSShubhrajyoti Datta 				DIV_ALL, &clkwzrd_lock);
10803a96393aSShubhrajyoti Datta 
10813a96393aSShubhrajyoti Datta 			goto out;
10823a96393aSShubhrajyoti Datta 		}
10833a96393aSShubhrajyoti Datta 		reg = readl(clk_wzrd->base + WZRD_CLK_CFG_REG(is_versal, 0));
1084c822490fSShubhrajyoti Datta 		reg_f = reg & WZRD_CLKFBOUT_FRAC_MASK;
1085c822490fSShubhrajyoti Datta 		reg_f =  reg_f >> WZRD_CLKFBOUT_FRAC_SHIFT;
1086c822490fSShubhrajyoti Datta 
1087c822490fSShubhrajyoti Datta 		reg = reg & WZRD_CLKFBOUT_MULT_MASK;
1088c822490fSShubhrajyoti Datta 		reg =  reg >> WZRD_CLKFBOUT_MULT_SHIFT;
1089c822490fSShubhrajyoti Datta 		mult = (reg * 1000) + reg_f;
10903a96393aSShubhrajyoti Datta 		div = 1000;
10913a96393aSShubhrajyoti Datta 	}
1092595c88cdSShubhrajyoti Datta 	clk_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_mul", dev_name(&pdev->dev));
1093c822490fSShubhrajyoti Datta 	if (!clk_name) {
1094c822490fSShubhrajyoti Datta 		ret = -ENOMEM;
1095c822490fSShubhrajyoti Datta 		goto err_disable_clk;
1096c822490fSShubhrajyoti Datta 	}
1097c822490fSShubhrajyoti Datta 	clk_wzrd->clks_internal[wzrd_clk_mul] = clk_register_fixed_factor
1098c822490fSShubhrajyoti Datta 			(&pdev->dev, clk_name,
1099c822490fSShubhrajyoti Datta 			 __clk_get_name(clk_wzrd->clk_in1),
11003a96393aSShubhrajyoti Datta 			0, mult, div);
1101c822490fSShubhrajyoti Datta 	if (IS_ERR(clk_wzrd->clks_internal[wzrd_clk_mul])) {
1102c822490fSShubhrajyoti Datta 		dev_err(&pdev->dev, "unable to register fixed-factor clock\n");
1103c822490fSShubhrajyoti Datta 		ret = PTR_ERR(clk_wzrd->clks_internal[wzrd_clk_mul]);
1104c822490fSShubhrajyoti Datta 		goto err_disable_clk;
1105c822490fSShubhrajyoti Datta 	}
1106c822490fSShubhrajyoti Datta 
1107595c88cdSShubhrajyoti Datta 	clk_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_mul_div", dev_name(&pdev->dev));
1108c822490fSShubhrajyoti Datta 	if (!clk_name) {
1109c822490fSShubhrajyoti Datta 		ret = -ENOMEM;
1110c822490fSShubhrajyoti Datta 		goto err_rm_int_clk;
1111c822490fSShubhrajyoti Datta 	}
1112c822490fSShubhrajyoti Datta 
11133a96393aSShubhrajyoti Datta 	if (is_versal) {
11143a96393aSShubhrajyoti Datta 		edged = !!(readl(clk_wzrd->base + WZRD_CLK_CFG_REG(is_versal, 20)) &
11153a96393aSShubhrajyoti Datta 			     BIT(10));
11163a96393aSShubhrajyoti Datta 		regld = (readl(clk_wzrd->base + WZRD_CLK_CFG_REG(is_versal, 21)) &
11173a96393aSShubhrajyoti Datta 			     WZRD_CLKFBOUT_L_MASK) >> WZRD_CLKFBOUT_L_SHIFT;
11183a96393aSShubhrajyoti Datta 		reghd = (readl(clk_wzrd->base + WZRD_CLK_CFG_REG(is_versal, 21)) &
11193a96393aSShubhrajyoti Datta 		     WZRD_CLKFBOUT_H_MASK) >> WZRD_CLKFBOUT_H_SHIFT;
11203a96393aSShubhrajyoti Datta 		div = (regld  + reghd + edged);
11213a96393aSShubhrajyoti Datta 		if (!div)
11223a96393aSShubhrajyoti Datta 			div = 1;
11233a96393aSShubhrajyoti Datta 
11243a96393aSShubhrajyoti Datta 		clk_mul_name = __clk_get_name(clk_wzrd->clks_internal[wzrd_clk_mul]);
11253a96393aSShubhrajyoti Datta 		clk_wzrd->clks_internal[wzrd_clk_mul_div] =
11263a96393aSShubhrajyoti Datta 			clk_register_fixed_factor(&pdev->dev, clk_name,
11273a96393aSShubhrajyoti Datta 						  clk_mul_name, 0, 1, div);
11283a96393aSShubhrajyoti Datta 	} else {
11293a96393aSShubhrajyoti Datta 		ctrl_reg = clk_wzrd->base + WZRD_CLK_CFG_REG(is_versal, 0);
1130c822490fSShubhrajyoti Datta 		clk_wzrd->clks_internal[wzrd_clk_mul_div] = clk_register_divider
1131c822490fSShubhrajyoti Datta 			(&pdev->dev, clk_name,
1132c822490fSShubhrajyoti Datta 			 __clk_get_name(clk_wzrd->clks_internal[wzrd_clk_mul]),
1133c822490fSShubhrajyoti Datta 			flags, ctrl_reg, 0, 8, CLK_DIVIDER_ONE_BASED |
1134c822490fSShubhrajyoti Datta 			CLK_DIVIDER_ALLOW_ZERO, &clkwzrd_lock);
11353a96393aSShubhrajyoti Datta 	}
1136c822490fSShubhrajyoti Datta 	if (IS_ERR(clk_wzrd->clks_internal[wzrd_clk_mul_div])) {
1137c822490fSShubhrajyoti Datta 		dev_err(&pdev->dev, "unable to register divider clock\n");
1138c822490fSShubhrajyoti Datta 		ret = PTR_ERR(clk_wzrd->clks_internal[wzrd_clk_mul_div]);
1139c822490fSShubhrajyoti Datta 		goto err_rm_int_clk;
1140c822490fSShubhrajyoti Datta 	}
1141c822490fSShubhrajyoti Datta 
1142c822490fSShubhrajyoti Datta 	/* register div per output */
1143c822490fSShubhrajyoti Datta 	for (i = nr_outputs - 1; i >= 0 ; i--) {
1144595c88cdSShubhrajyoti Datta 		clkout_name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
1145595c88cdSShubhrajyoti Datta 					     "%s_out%d", dev_name(&pdev->dev), i);
1146c822490fSShubhrajyoti Datta 		if (!clkout_name) {
1147c822490fSShubhrajyoti Datta 			ret = -ENOMEM;
1148c822490fSShubhrajyoti Datta 			goto err_rm_int_clk;
1149c822490fSShubhrajyoti Datta 		}
1150c822490fSShubhrajyoti Datta 
11513a96393aSShubhrajyoti Datta 		if (is_versal) {
11523a96393aSShubhrajyoti Datta 			clk_wzrd->clkout[i] = clk_wzrd_ver_register_divider
11533a96393aSShubhrajyoti Datta 						(&pdev->dev,
11543a96393aSShubhrajyoti Datta 						 clkout_name, clk_name, 0,
11553a96393aSShubhrajyoti Datta 						 clk_wzrd->base,
11563a96393aSShubhrajyoti Datta 						 (WZRD_CLK_CFG_REG(is_versal, 3) + i * 8),
11573a96393aSShubhrajyoti Datta 						 WZRD_CLKOUT_DIVIDE_SHIFT,
11583a96393aSShubhrajyoti Datta 						 WZRD_CLKOUT_DIVIDE_WIDTH,
11593a96393aSShubhrajyoti Datta 						 CLK_DIVIDER_ONE_BASED |
11603a96393aSShubhrajyoti Datta 						 CLK_DIVIDER_ALLOW_ZERO,
11613a96393aSShubhrajyoti Datta 						 DIV_O, &clkwzrd_lock);
11623a96393aSShubhrajyoti Datta 		} else {
1163c822490fSShubhrajyoti Datta 			if (!i)
1164c822490fSShubhrajyoti Datta 				clk_wzrd->clkout[i] = clk_wzrd_register_divf
11653a96393aSShubhrajyoti Datta 					(&pdev->dev, clkout_name, clk_name, flags, clk_wzrd->base,
11663a96393aSShubhrajyoti Datta 					(WZRD_CLK_CFG_REG(is_versal, 2) + i * 12),
1167c822490fSShubhrajyoti Datta 					WZRD_CLKOUT_DIVIDE_SHIFT,
1168c822490fSShubhrajyoti Datta 					WZRD_CLKOUT_DIVIDE_WIDTH,
1169c822490fSShubhrajyoti Datta 					CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
1170595c88cdSShubhrajyoti Datta 					DIV_O, &clkwzrd_lock);
1171c822490fSShubhrajyoti Datta 			else
1172c822490fSShubhrajyoti Datta 				clk_wzrd->clkout[i] = clk_wzrd_register_divider
11733a96393aSShubhrajyoti Datta 					(&pdev->dev, clkout_name, clk_name, 0, clk_wzrd->base,
11743a96393aSShubhrajyoti Datta 					(WZRD_CLK_CFG_REG(is_versal, 2) + i * 12),
1175c822490fSShubhrajyoti Datta 					WZRD_CLKOUT_DIVIDE_SHIFT,
1176c822490fSShubhrajyoti Datta 					WZRD_CLKOUT_DIVIDE_WIDTH,
1177c822490fSShubhrajyoti Datta 					CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
1178595c88cdSShubhrajyoti Datta 					DIV_O, &clkwzrd_lock);
11793a96393aSShubhrajyoti Datta 		}
1180c822490fSShubhrajyoti Datta 		if (IS_ERR(clk_wzrd->clkout[i])) {
1181c822490fSShubhrajyoti Datta 			int j;
1182c822490fSShubhrajyoti Datta 
1183c822490fSShubhrajyoti Datta 			for (j = i + 1; j < nr_outputs; j++)
1184c822490fSShubhrajyoti Datta 				clk_unregister(clk_wzrd->clkout[j]);
1185c822490fSShubhrajyoti Datta 			dev_err(&pdev->dev,
1186c822490fSShubhrajyoti Datta 				"unable to register divider clock\n");
1187c822490fSShubhrajyoti Datta 			ret = PTR_ERR(clk_wzrd->clkout[i]);
1188c822490fSShubhrajyoti Datta 			goto err_rm_int_clks;
1189c822490fSShubhrajyoti Datta 		}
1190c822490fSShubhrajyoti Datta 	}
1191c822490fSShubhrajyoti Datta 
1192595c88cdSShubhrajyoti Datta out:
1193c822490fSShubhrajyoti Datta 	clk_wzrd->clk_data.clks = clk_wzrd->clkout;
1194c822490fSShubhrajyoti Datta 	clk_wzrd->clk_data.clk_num = ARRAY_SIZE(clk_wzrd->clkout);
1195c822490fSShubhrajyoti Datta 	of_clk_add_provider(np, of_clk_src_onecell_get, &clk_wzrd->clk_data);
1196c822490fSShubhrajyoti Datta 
1197c822490fSShubhrajyoti Datta 	if (clk_wzrd->speed_grade) {
1198c822490fSShubhrajyoti Datta 		clk_wzrd->nb.notifier_call = clk_wzrd_clk_notifier;
1199c822490fSShubhrajyoti Datta 
1200c822490fSShubhrajyoti Datta 		ret = clk_notifier_register(clk_wzrd->clk_in1,
1201c822490fSShubhrajyoti Datta 					    &clk_wzrd->nb);
1202c822490fSShubhrajyoti Datta 		if (ret)
1203c822490fSShubhrajyoti Datta 			dev_warn(&pdev->dev,
1204c822490fSShubhrajyoti Datta 				 "unable to register clock notifier\n");
1205c822490fSShubhrajyoti Datta 
1206c822490fSShubhrajyoti Datta 		ret = clk_notifier_register(clk_wzrd->axi_clk, &clk_wzrd->nb);
1207c822490fSShubhrajyoti Datta 		if (ret)
1208c822490fSShubhrajyoti Datta 			dev_warn(&pdev->dev,
1209c822490fSShubhrajyoti Datta 				 "unable to register clock notifier\n");
1210c822490fSShubhrajyoti Datta 	}
1211c822490fSShubhrajyoti Datta 
1212c822490fSShubhrajyoti Datta 	return 0;
1213c822490fSShubhrajyoti Datta 
1214c822490fSShubhrajyoti Datta err_rm_int_clks:
1215c822490fSShubhrajyoti Datta 	clk_unregister(clk_wzrd->clks_internal[1]);
1216c822490fSShubhrajyoti Datta err_rm_int_clk:
1217c822490fSShubhrajyoti Datta 	clk_unregister(clk_wzrd->clks_internal[0]);
1218c822490fSShubhrajyoti Datta err_disable_clk:
1219c822490fSShubhrajyoti Datta 	clk_disable_unprepare(clk_wzrd->axi_clk);
1220c822490fSShubhrajyoti Datta 
1221c822490fSShubhrajyoti Datta 	return ret;
1222c822490fSShubhrajyoti Datta }
1223c822490fSShubhrajyoti Datta 
clk_wzrd_remove(struct platform_device * pdev)1224ce1c5f84SUwe Kleine-König static void clk_wzrd_remove(struct platform_device *pdev)
1225c822490fSShubhrajyoti Datta {
1226c822490fSShubhrajyoti Datta 	int i;
1227c822490fSShubhrajyoti Datta 	struct clk_wzrd *clk_wzrd = platform_get_drvdata(pdev);
1228c822490fSShubhrajyoti Datta 
1229c822490fSShubhrajyoti Datta 	of_clk_del_provider(pdev->dev.of_node);
1230c822490fSShubhrajyoti Datta 
1231c822490fSShubhrajyoti Datta 	for (i = 0; i < WZRD_NUM_OUTPUTS; i++)
1232c822490fSShubhrajyoti Datta 		clk_unregister(clk_wzrd->clkout[i]);
1233c822490fSShubhrajyoti Datta 	for (i = 0; i < wzrd_clk_int_max; i++)
1234c822490fSShubhrajyoti Datta 		clk_unregister(clk_wzrd->clks_internal[i]);
1235c822490fSShubhrajyoti Datta 
1236c822490fSShubhrajyoti Datta 	if (clk_wzrd->speed_grade) {
1237c822490fSShubhrajyoti Datta 		clk_notifier_unregister(clk_wzrd->axi_clk, &clk_wzrd->nb);
1238c822490fSShubhrajyoti Datta 		clk_notifier_unregister(clk_wzrd->clk_in1, &clk_wzrd->nb);
1239c822490fSShubhrajyoti Datta 	}
1240c822490fSShubhrajyoti Datta 
1241c822490fSShubhrajyoti Datta 	clk_disable_unprepare(clk_wzrd->axi_clk);
1242c822490fSShubhrajyoti Datta }
1243c822490fSShubhrajyoti Datta 
1244c822490fSShubhrajyoti Datta static const struct of_device_id clk_wzrd_ids[] = {
12453a96393aSShubhrajyoti Datta 	{ .compatible = "xlnx,versal-clk-wizard", .data = &versal_data },
1246c822490fSShubhrajyoti Datta 	{ .compatible = "xlnx,clocking-wizard"   },
1247e8db788dSShubhrajyoti Datta 	{ .compatible = "xlnx,clocking-wizard-v5.2"   },
1248e8db788dSShubhrajyoti Datta 	{ .compatible = "xlnx,clocking-wizard-v6.0"  },
1249c822490fSShubhrajyoti Datta 	{ },
1250c822490fSShubhrajyoti Datta };
1251c822490fSShubhrajyoti Datta MODULE_DEVICE_TABLE(of, clk_wzrd_ids);
1252c822490fSShubhrajyoti Datta 
1253c822490fSShubhrajyoti Datta static struct platform_driver clk_wzrd_driver = {
1254c822490fSShubhrajyoti Datta 	.driver = {
1255c822490fSShubhrajyoti Datta 		.name = "clk-wizard",
1256c822490fSShubhrajyoti Datta 		.of_match_table = clk_wzrd_ids,
1257c822490fSShubhrajyoti Datta 		.pm = &clk_wzrd_dev_pm_ops,
1258c822490fSShubhrajyoti Datta 	},
1259c822490fSShubhrajyoti Datta 	.probe = clk_wzrd_probe,
1260*f00b45dbSUwe Kleine-König 	.remove = clk_wzrd_remove,
1261c822490fSShubhrajyoti Datta };
1262c822490fSShubhrajyoti Datta module_platform_driver(clk_wzrd_driver);
1263c822490fSShubhrajyoti Datta 
1264c822490fSShubhrajyoti Datta MODULE_LICENSE("GPL");
1265c822490fSShubhrajyoti Datta MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com");
1266c822490fSShubhrajyoti Datta MODULE_DESCRIPTION("Driver for the Xilinx Clocking Wizard IP core");
1267