xref: /openbmc/linux/drivers/clk/ti/adpll.c (revision 183a1f1b)
152e6676eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
221330497STony Lindgren 
321330497STony Lindgren #include <linux/clk.h>
421330497STony Lindgren #include <linux/clkdev.h>
521330497STony Lindgren #include <linux/clk-provider.h>
621330497STony Lindgren #include <linux/delay.h>
721330497STony Lindgren #include <linux/err.h>
862e59c4eSStephen Boyd #include <linux/io.h>
921330497STony Lindgren #include <linux/math64.h>
1021330497STony Lindgren #include <linux/module.h>
1121330497STony Lindgren #include <linux/of_device.h>
1221330497STony Lindgren #include <linux/string.h>
1321330497STony Lindgren 
1421330497STony Lindgren #define ADPLL_PLLSS_MMR_LOCK_OFFSET	0x00	/* Managed by MPPULL */
1521330497STony Lindgren #define ADPLL_PLLSS_MMR_LOCK_ENABLED	0x1f125B64
1621330497STony Lindgren #define ADPLL_PLLSS_MMR_UNLOCK_MAGIC	0x1eda4c3d
1721330497STony Lindgren 
1821330497STony Lindgren #define ADPLL_PWRCTRL_OFFSET		0x00
1921330497STony Lindgren #define ADPLL_PWRCTRL_PONIN		5
2021330497STony Lindgren #define ADPLL_PWRCTRL_PGOODIN		4
2121330497STony Lindgren #define ADPLL_PWRCTRL_RET		3
2221330497STony Lindgren #define ADPLL_PWRCTRL_ISORET		2
2321330497STony Lindgren #define ADPLL_PWRCTRL_ISOSCAN		1
2421330497STony Lindgren #define ADPLL_PWRCTRL_OFFMODE		0
2521330497STony Lindgren 
2621330497STony Lindgren #define ADPLL_CLKCTRL_OFFSET		0x04
2721330497STony Lindgren #define ADPLL_CLKCTRL_CLKDCOLDOEN	29
2821330497STony Lindgren #define ADPLL_CLKCTRL_IDLE		23
2921330497STony Lindgren #define ADPLL_CLKCTRL_CLKOUTEN		20
3021330497STony Lindgren #define ADPLL_CLKINPHIFSEL_ADPLL_S	19	/* REVISIT: which bit? */
3121330497STony Lindgren #define ADPLL_CLKCTRL_CLKOUTLDOEN_ADPLL_LJ 19
3221330497STony Lindgren #define ADPLL_CLKCTRL_ULOWCLKEN		18
3321330497STony Lindgren #define ADPLL_CLKCTRL_CLKDCOLDOPWDNZ	17
3421330497STony Lindgren #define ADPLL_CLKCTRL_M2PWDNZ		16
3521330497STony Lindgren #define ADPLL_CLKCTRL_M3PWDNZ_ADPLL_S	15
3621330497STony Lindgren #define ADPLL_CLKCTRL_LOWCURRSTDBY_ADPLL_S 13
3721330497STony Lindgren #define ADPLL_CLKCTRL_LPMODE_ADPLL_S	12
3821330497STony Lindgren #define ADPLL_CLKCTRL_REGM4XEN_ADPLL_S	10
3921330497STony Lindgren #define ADPLL_CLKCTRL_SELFREQDCO_ADPLL_LJ 10
4021330497STony Lindgren #define ADPLL_CLKCTRL_TINITZ		0
4121330497STony Lindgren 
4221330497STony Lindgren #define ADPLL_TENABLE_OFFSET		0x08
4321330497STony Lindgren #define ADPLL_TENABLEDIV_OFFSET		0x8c
4421330497STony Lindgren 
4521330497STony Lindgren #define ADPLL_M2NDIV_OFFSET		0x10
4621330497STony Lindgren #define ADPLL_M2NDIV_M2			16
4721330497STony Lindgren #define ADPLL_M2NDIV_M2_ADPLL_S_WIDTH	5
4821330497STony Lindgren #define ADPLL_M2NDIV_M2_ADPLL_LJ_WIDTH	7
4921330497STony Lindgren 
5021330497STony Lindgren #define ADPLL_MN2DIV_OFFSET		0x14
5121330497STony Lindgren #define ADPLL_MN2DIV_N2			16
5221330497STony Lindgren 
5321330497STony Lindgren #define ADPLL_FRACDIV_OFFSET		0x18
5421330497STony Lindgren #define ADPLL_FRACDIV_REGSD		24
5521330497STony Lindgren #define ADPLL_FRACDIV_FRACTIONALM	0
5621330497STony Lindgren #define ADPLL_FRACDIV_FRACTIONALM_MASK	0x3ffff
5721330497STony Lindgren 
5821330497STony Lindgren #define ADPLL_BWCTRL_OFFSET		0x1c
5921330497STony Lindgren #define ADPLL_BWCTRL_BWCONTROL		1
6021330497STony Lindgren #define ADPLL_BWCTRL_BW_INCR_DECRZ	0
6121330497STony Lindgren 
6221330497STony Lindgren #define ADPLL_RESERVED_OFFSET		0x20
6321330497STony Lindgren 
6421330497STony Lindgren #define ADPLL_STATUS_OFFSET		0x24
6521330497STony Lindgren #define ADPLL_STATUS_PONOUT		31
6621330497STony Lindgren #define ADPLL_STATUS_PGOODOUT		30
6721330497STony Lindgren #define ADPLL_STATUS_LDOPWDN		29
6821330497STony Lindgren #define ADPLL_STATUS_RECAL_BSTATUS3	28
6921330497STony Lindgren #define ADPLL_STATUS_RECAL_OPPIN	27
7021330497STony Lindgren #define ADPLL_STATUS_PHASELOCK		10
7121330497STony Lindgren #define ADPLL_STATUS_FREQLOCK		9
7221330497STony Lindgren #define ADPLL_STATUS_BYPASSACK		8
7321330497STony Lindgren #define ADPLL_STATUS_LOSSREF		6
7421330497STony Lindgren #define ADPLL_STATUS_CLKOUTENACK	5
7521330497STony Lindgren #define ADPLL_STATUS_LOCK2		4
7621330497STony Lindgren #define ADPLL_STATUS_M2CHANGEACK	3
7721330497STony Lindgren #define ADPLL_STATUS_HIGHJITTER		1
7821330497STony Lindgren #define ADPLL_STATUS_BYPASS		0
7921330497STony Lindgren #define ADPLL_STATUS_PREPARED_MASK	(BIT(ADPLL_STATUS_PHASELOCK) | \
8021330497STony Lindgren 					 BIT(ADPLL_STATUS_FREQLOCK))
8121330497STony Lindgren 
8221330497STony Lindgren #define ADPLL_M3DIV_OFFSET		0x28	/* Only on MPUPLL */
8321330497STony Lindgren #define ADPLL_M3DIV_M3			0
8421330497STony Lindgren #define ADPLL_M3DIV_M3_WIDTH		5
8521330497STony Lindgren #define ADPLL_M3DIV_M3_MASK		0x1f
8621330497STony Lindgren 
8721330497STony Lindgren #define ADPLL_RAMPCTRL_OFFSET		0x2c	/* Only on MPUPLL */
8821330497STony Lindgren #define ADPLL_RAMPCTRL_CLKRAMPLEVEL	19
8921330497STony Lindgren #define ADPLL_RAMPCTRL_CLKRAMPRATE	16
9021330497STony Lindgren #define ADPLL_RAMPCTRL_RELOCK_RAMP_EN	0
9121330497STony Lindgren 
9221330497STony Lindgren #define MAX_ADPLL_INPUTS		3
9321330497STony Lindgren #define MAX_ADPLL_OUTPUTS		4
9421330497STony Lindgren #define ADPLL_MAX_RETRIES		5
9521330497STony Lindgren 
9621330497STony Lindgren #define to_dco(_hw)	container_of(_hw, struct ti_adpll_dco_data, hw)
9721330497STony Lindgren #define to_adpll(_hw)	container_of(_hw, struct ti_adpll_data, dco)
9821330497STony Lindgren #define to_clkout(_hw)	container_of(_hw, struct ti_adpll_clkout_data, hw)
9921330497STony Lindgren 
10021330497STony Lindgren enum ti_adpll_clocks {
10121330497STony Lindgren 	TI_ADPLL_DCO,
10221330497STony Lindgren 	TI_ADPLL_DCO_GATE,
10321330497STony Lindgren 	TI_ADPLL_N2,
10421330497STony Lindgren 	TI_ADPLL_M2,
10521330497STony Lindgren 	TI_ADPLL_M2_GATE,
10621330497STony Lindgren 	TI_ADPLL_BYPASS,
10721330497STony Lindgren 	TI_ADPLL_HIF,
10821330497STony Lindgren 	TI_ADPLL_DIV2,
10921330497STony Lindgren 	TI_ADPLL_CLKOUT,
11021330497STony Lindgren 	TI_ADPLL_CLKOUT2,
11121330497STony Lindgren 	TI_ADPLL_M3,
11221330497STony Lindgren };
11321330497STony Lindgren 
11421330497STony Lindgren #define TI_ADPLL_NR_CLOCKS	(TI_ADPLL_M3 + 1)
11521330497STony Lindgren 
11621330497STony Lindgren enum ti_adpll_inputs {
11721330497STony Lindgren 	TI_ADPLL_CLKINP,
11821330497STony Lindgren 	TI_ADPLL_CLKINPULOW,
11921330497STony Lindgren 	TI_ADPLL_CLKINPHIF,
12021330497STony Lindgren };
12121330497STony Lindgren 
12221330497STony Lindgren enum ti_adpll_s_outputs {
12321330497STony Lindgren 	TI_ADPLL_S_DCOCLKLDO,
12421330497STony Lindgren 	TI_ADPLL_S_CLKOUT,
12521330497STony Lindgren 	TI_ADPLL_S_CLKOUTX2,
12621330497STony Lindgren 	TI_ADPLL_S_CLKOUTHIF,
12721330497STony Lindgren };
12821330497STony Lindgren 
12921330497STony Lindgren enum ti_adpll_lj_outputs {
13021330497STony Lindgren 	TI_ADPLL_LJ_CLKDCOLDO,
13121330497STony Lindgren 	TI_ADPLL_LJ_CLKOUT,
13221330497STony Lindgren 	TI_ADPLL_LJ_CLKOUTLDO,
13321330497STony Lindgren };
13421330497STony Lindgren 
13521330497STony Lindgren struct ti_adpll_platform_data {
13621330497STony Lindgren 	const bool is_type_s;
13721330497STony Lindgren 	const int nr_max_inputs;
13821330497STony Lindgren 	const int nr_max_outputs;
13921330497STony Lindgren 	const int output_index;
14021330497STony Lindgren };
14121330497STony Lindgren 
14221330497STony Lindgren struct ti_adpll_clock {
14321330497STony Lindgren 	struct clk *clk;
14421330497STony Lindgren 	struct clk_lookup *cl;
14521330497STony Lindgren 	void (*unregister)(struct clk *clk);
14621330497STony Lindgren };
14721330497STony Lindgren 
14821330497STony Lindgren struct ti_adpll_dco_data {
14921330497STony Lindgren 	struct clk_hw hw;
15021330497STony Lindgren };
15121330497STony Lindgren 
15221330497STony Lindgren struct ti_adpll_clkout_data {
15321330497STony Lindgren 	struct ti_adpll_data *adpll;
15421330497STony Lindgren 	struct clk_gate gate;
15521330497STony Lindgren 	struct clk_hw hw;
15621330497STony Lindgren };
15721330497STony Lindgren 
15821330497STony Lindgren struct ti_adpll_data {
15921330497STony Lindgren 	struct device *dev;
16021330497STony Lindgren 	const struct ti_adpll_platform_data *c;
16121330497STony Lindgren 	struct device_node *np;
16221330497STony Lindgren 	unsigned long pa;
16321330497STony Lindgren 	void __iomem *iobase;
16421330497STony Lindgren 	void __iomem *regs;
16521330497STony Lindgren 	spinlock_t lock;	/* For ADPLL shared register access */
16621330497STony Lindgren 	const char *parent_names[MAX_ADPLL_INPUTS];
16721330497STony Lindgren 	struct clk *parent_clocks[MAX_ADPLL_INPUTS];
16821330497STony Lindgren 	struct ti_adpll_clock *clocks;
16921330497STony Lindgren 	struct clk_onecell_data outputs;
17021330497STony Lindgren 	struct ti_adpll_dco_data dco;
17121330497STony Lindgren };
17221330497STony Lindgren 
ti_adpll_clk_get_name(struct ti_adpll_data * d,int output_index,const char * postfix)17321330497STony Lindgren static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d,
17421330497STony Lindgren 					 int output_index,
17521330497STony Lindgren 					 const char *postfix)
17621330497STony Lindgren {
17721330497STony Lindgren 	const char *name;
17821330497STony Lindgren 	int err;
17921330497STony Lindgren 
18021330497STony Lindgren 	if (output_index >= 0) {
18121330497STony Lindgren 		err = of_property_read_string_index(d->np,
18221330497STony Lindgren 						    "clock-output-names",
18321330497STony Lindgren 						    output_index,
18421330497STony Lindgren 						    &name);
18521330497STony Lindgren 		if (err)
18621330497STony Lindgren 			return NULL;
18721330497STony Lindgren 	} else {
1887f6ac729SStephen Kitt 		name = devm_kasprintf(d->dev, GFP_KERNEL, "%08lx.adpll.%s",
1897f6ac729SStephen Kitt 				      d->pa, postfix);
19021330497STony Lindgren 	}
19121330497STony Lindgren 
19221330497STony Lindgren 	return name;
19321330497STony Lindgren }
19421330497STony Lindgren 
19521330497STony Lindgren #define ADPLL_MAX_CON_ID	16	/* See MAX_CON_ID */
19621330497STony Lindgren 
ti_adpll_setup_clock(struct ti_adpll_data * d,struct clk * clock,int index,int output_index,const char * name,void (* unregister)(struct clk * clk))19721330497STony Lindgren static int ti_adpll_setup_clock(struct ti_adpll_data *d, struct clk *clock,
19821330497STony Lindgren 				int index, int output_index, const char *name,
19921330497STony Lindgren 				void (*unregister)(struct clk *clk))
20021330497STony Lindgren {
20121330497STony Lindgren 	struct clk_lookup *cl;
20221330497STony Lindgren 	const char *postfix = NULL;
20321330497STony Lindgren 	char con_id[ADPLL_MAX_CON_ID];
20421330497STony Lindgren 
20521330497STony Lindgren 	d->clocks[index].clk = clock;
20621330497STony Lindgren 	d->clocks[index].unregister = unregister;
20721330497STony Lindgren 
20821330497STony Lindgren 	/* Separate con_id in format "pll040dcoclkldo" to fit MAX_CON_ID */
20921330497STony Lindgren 	postfix = strrchr(name, '.');
210df2f8451SColin Ian King 	if (postfix && strlen(postfix) > 1) {
21121330497STony Lindgren 		if (strlen(postfix) > ADPLL_MAX_CON_ID)
21221330497STony Lindgren 			dev_warn(d->dev, "clock %s con_id lookup may fail\n",
21321330497STony Lindgren 				 name);
21421330497STony Lindgren 		snprintf(con_id, 16, "pll%03lx%s", d->pa & 0xfff, postfix + 1);
21521330497STony Lindgren 		cl = clkdev_create(clock, con_id, NULL);
21621330497STony Lindgren 		if (!cl)
21721330497STony Lindgren 			return -ENOMEM;
21821330497STony Lindgren 		d->clocks[index].cl = cl;
21921330497STony Lindgren 	} else {
22021330497STony Lindgren 		dev_warn(d->dev, "no con_id for clock %s\n", name);
22121330497STony Lindgren 	}
22221330497STony Lindgren 
22321330497STony Lindgren 	if (output_index < 0)
22421330497STony Lindgren 		return 0;
22521330497STony Lindgren 
22621330497STony Lindgren 	d->outputs.clks[output_index] = clock;
22721330497STony Lindgren 	d->outputs.clk_num++;
22821330497STony Lindgren 
22921330497STony Lindgren 	return 0;
23021330497STony Lindgren }
23121330497STony Lindgren 
ti_adpll_init_divider(struct ti_adpll_data * d,enum ti_adpll_clocks index,int output_index,char * name,struct clk * parent_clock,void __iomem * reg,u8 shift,u8 width,u8 clk_divider_flags)23221330497STony Lindgren static int ti_adpll_init_divider(struct ti_adpll_data *d,
23321330497STony Lindgren 				 enum ti_adpll_clocks index,
23421330497STony Lindgren 				 int output_index, char *name,
23521330497STony Lindgren 				 struct clk *parent_clock,
23621330497STony Lindgren 				 void __iomem *reg,
23721330497STony Lindgren 				 u8 shift, u8 width,
23821330497STony Lindgren 				 u8 clk_divider_flags)
23921330497STony Lindgren {
24021330497STony Lindgren 	const char *child_name;
24121330497STony Lindgren 	const char *parent_name;
24221330497STony Lindgren 	struct clk *clock;
24321330497STony Lindgren 
24421330497STony Lindgren 	child_name = ti_adpll_clk_get_name(d, output_index, name);
24521330497STony Lindgren 	if (!child_name)
24621330497STony Lindgren 		return -EINVAL;
24721330497STony Lindgren 
24821330497STony Lindgren 	parent_name = __clk_get_name(parent_clock);
24921330497STony Lindgren 	clock = clk_register_divider(d->dev, child_name, parent_name, 0,
25021330497STony Lindgren 				     reg, shift, width, clk_divider_flags,
25121330497STony Lindgren 				     &d->lock);
25221330497STony Lindgren 	if (IS_ERR(clock)) {
25321330497STony Lindgren 		dev_err(d->dev, "failed to register divider %s: %li\n",
25421330497STony Lindgren 			name, PTR_ERR(clock));
25521330497STony Lindgren 		return PTR_ERR(clock);
25621330497STony Lindgren 	}
25721330497STony Lindgren 
25821330497STony Lindgren 	return ti_adpll_setup_clock(d, clock, index, output_index, child_name,
25921330497STony Lindgren 				    clk_unregister_divider);
26021330497STony Lindgren }
26121330497STony Lindgren 
ti_adpll_init_mux(struct ti_adpll_data * d,enum ti_adpll_clocks index,char * name,struct clk * clk0,struct clk * clk1,void __iomem * reg,u8 shift)26221330497STony Lindgren static int ti_adpll_init_mux(struct ti_adpll_data *d,
26321330497STony Lindgren 			     enum ti_adpll_clocks index,
26421330497STony Lindgren 			     char *name, struct clk *clk0,
26521330497STony Lindgren 			     struct clk *clk1,
26621330497STony Lindgren 			     void __iomem *reg,
26721330497STony Lindgren 			     u8 shift)
26821330497STony Lindgren {
26921330497STony Lindgren 	const char *child_name;
27021330497STony Lindgren 	const char *parents[2];
27121330497STony Lindgren 	struct clk *clock;
27221330497STony Lindgren 
27321330497STony Lindgren 	child_name = ti_adpll_clk_get_name(d, -ENODEV, name);
27421330497STony Lindgren 	if (!child_name)
27521330497STony Lindgren 		return -ENOMEM;
27621330497STony Lindgren 	parents[0] = __clk_get_name(clk0);
27721330497STony Lindgren 	parents[1] = __clk_get_name(clk1);
27821330497STony Lindgren 	clock = clk_register_mux(d->dev, child_name, parents, 2, 0,
27921330497STony Lindgren 				 reg, shift, 1, 0, &d->lock);
28021330497STony Lindgren 	if (IS_ERR(clock)) {
28121330497STony Lindgren 		dev_err(d->dev, "failed to register mux %s: %li\n",
28221330497STony Lindgren 			name, PTR_ERR(clock));
28321330497STony Lindgren 		return PTR_ERR(clock);
28421330497STony Lindgren 	}
28521330497STony Lindgren 
28621330497STony Lindgren 	return ti_adpll_setup_clock(d, clock, index, -ENODEV, child_name,
28721330497STony Lindgren 				    clk_unregister_mux);
28821330497STony Lindgren }
28921330497STony Lindgren 
ti_adpll_init_gate(struct ti_adpll_data * d,enum ti_adpll_clocks index,int output_index,char * name,struct clk * parent_clock,void __iomem * reg,u8 bit_idx,u8 clk_gate_flags)29021330497STony Lindgren static int ti_adpll_init_gate(struct ti_adpll_data *d,
29121330497STony Lindgren 			      enum ti_adpll_clocks index,
29221330497STony Lindgren 			      int output_index, char *name,
29321330497STony Lindgren 			      struct clk *parent_clock,
29421330497STony Lindgren 			      void __iomem *reg,
29521330497STony Lindgren 			      u8 bit_idx,
29621330497STony Lindgren 			      u8 clk_gate_flags)
29721330497STony Lindgren {
29821330497STony Lindgren 	const char *child_name;
29921330497STony Lindgren 	const char *parent_name;
30021330497STony Lindgren 	struct clk *clock;
30121330497STony Lindgren 
30221330497STony Lindgren 	child_name = ti_adpll_clk_get_name(d, output_index, name);
30321330497STony Lindgren 	if (!child_name)
30421330497STony Lindgren 		return -EINVAL;
30521330497STony Lindgren 
30621330497STony Lindgren 	parent_name = __clk_get_name(parent_clock);
30721330497STony Lindgren 	clock = clk_register_gate(d->dev, child_name, parent_name, 0,
30821330497STony Lindgren 				  reg, bit_idx, clk_gate_flags,
30921330497STony Lindgren 				  &d->lock);
31021330497STony Lindgren 	if (IS_ERR(clock)) {
31121330497STony Lindgren 		dev_err(d->dev, "failed to register gate %s: %li\n",
31221330497STony Lindgren 			name, PTR_ERR(clock));
31321330497STony Lindgren 		return PTR_ERR(clock);
31421330497STony Lindgren 	}
31521330497STony Lindgren 
31621330497STony Lindgren 	return ti_adpll_setup_clock(d, clock, index, output_index, child_name,
31721330497STony Lindgren 				    clk_unregister_gate);
31821330497STony Lindgren }
31921330497STony Lindgren 
ti_adpll_init_fixed_factor(struct ti_adpll_data * d,enum ti_adpll_clocks index,char * name,struct clk * parent_clock,unsigned int mult,unsigned int div)32021330497STony Lindgren static int ti_adpll_init_fixed_factor(struct ti_adpll_data *d,
32121330497STony Lindgren 				      enum ti_adpll_clocks index,
32221330497STony Lindgren 				      char *name,
32321330497STony Lindgren 				      struct clk *parent_clock,
32421330497STony Lindgren 				      unsigned int mult,
32521330497STony Lindgren 				      unsigned int div)
32621330497STony Lindgren {
32721330497STony Lindgren 	const char *child_name;
32821330497STony Lindgren 	const char *parent_name;
32921330497STony Lindgren 	struct clk *clock;
33021330497STony Lindgren 
33121330497STony Lindgren 	child_name = ti_adpll_clk_get_name(d, -ENODEV, name);
33221330497STony Lindgren 	if (!child_name)
33321330497STony Lindgren 		return -ENOMEM;
33421330497STony Lindgren 
33521330497STony Lindgren 	parent_name = __clk_get_name(parent_clock);
33621330497STony Lindgren 	clock = clk_register_fixed_factor(d->dev, child_name, parent_name,
33721330497STony Lindgren 					  0, mult, div);
33821330497STony Lindgren 	if (IS_ERR(clock))
33921330497STony Lindgren 		return PTR_ERR(clock);
34021330497STony Lindgren 
34121330497STony Lindgren 	return ti_adpll_setup_clock(d, clock, index, -ENODEV, child_name,
34221330497STony Lindgren 				    clk_unregister);
34321330497STony Lindgren }
34421330497STony Lindgren 
ti_adpll_set_idle_bypass(struct ti_adpll_data * d)34521330497STony Lindgren static void ti_adpll_set_idle_bypass(struct ti_adpll_data *d)
34621330497STony Lindgren {
34721330497STony Lindgren 	unsigned long flags;
34821330497STony Lindgren 	u32 v;
34921330497STony Lindgren 
35021330497STony Lindgren 	spin_lock_irqsave(&d->lock, flags);
35121330497STony Lindgren 	v = readl_relaxed(d->regs + ADPLL_CLKCTRL_OFFSET);
35221330497STony Lindgren 	v |= BIT(ADPLL_CLKCTRL_IDLE);
35321330497STony Lindgren 	writel_relaxed(v, d->regs + ADPLL_CLKCTRL_OFFSET);
35421330497STony Lindgren 	spin_unlock_irqrestore(&d->lock, flags);
35521330497STony Lindgren }
35621330497STony Lindgren 
ti_adpll_clear_idle_bypass(struct ti_adpll_data * d)35721330497STony Lindgren static void ti_adpll_clear_idle_bypass(struct ti_adpll_data *d)
35821330497STony Lindgren {
35921330497STony Lindgren 	unsigned long flags;
36021330497STony Lindgren 	u32 v;
36121330497STony Lindgren 
36221330497STony Lindgren 	spin_lock_irqsave(&d->lock, flags);
36321330497STony Lindgren 	v = readl_relaxed(d->regs + ADPLL_CLKCTRL_OFFSET);
36421330497STony Lindgren 	v &= ~BIT(ADPLL_CLKCTRL_IDLE);
36521330497STony Lindgren 	writel_relaxed(v, d->regs + ADPLL_CLKCTRL_OFFSET);
36621330497STony Lindgren 	spin_unlock_irqrestore(&d->lock, flags);
36721330497STony Lindgren }
36821330497STony Lindgren 
ti_adpll_clock_is_bypass(struct ti_adpll_data * d)36921330497STony Lindgren static bool ti_adpll_clock_is_bypass(struct ti_adpll_data *d)
37021330497STony Lindgren {
37121330497STony Lindgren 	u32 v;
37221330497STony Lindgren 
37321330497STony Lindgren 	v = readl_relaxed(d->regs + ADPLL_STATUS_OFFSET);
37421330497STony Lindgren 
37521330497STony Lindgren 	return v & BIT(ADPLL_STATUS_BYPASS);
37621330497STony Lindgren }
37721330497STony Lindgren 
37821330497STony Lindgren /*
37921330497STony Lindgren  * Locked and bypass are not actually mutually exclusive:  if you only care
38021330497STony Lindgren  * about the DCO clock and not CLKOUT you can clear M2PWDNZ before enabling
38121330497STony Lindgren  * the PLL, resulting in status (FREQLOCK | PHASELOCK | BYPASS) after lock.
38221330497STony Lindgren  */
ti_adpll_is_locked(struct ti_adpll_data * d)38321330497STony Lindgren static bool ti_adpll_is_locked(struct ti_adpll_data *d)
38421330497STony Lindgren {
38521330497STony Lindgren 	u32 v = readl_relaxed(d->regs + ADPLL_STATUS_OFFSET);
38621330497STony Lindgren 
38721330497STony Lindgren 	return (v & ADPLL_STATUS_PREPARED_MASK) == ADPLL_STATUS_PREPARED_MASK;
38821330497STony Lindgren }
38921330497STony Lindgren 
ti_adpll_wait_lock(struct ti_adpll_data * d)39021330497STony Lindgren static int ti_adpll_wait_lock(struct ti_adpll_data *d)
39121330497STony Lindgren {
39221330497STony Lindgren 	int retries = ADPLL_MAX_RETRIES;
39321330497STony Lindgren 
39421330497STony Lindgren 	do {
39521330497STony Lindgren 		if (ti_adpll_is_locked(d))
39621330497STony Lindgren 			return 0;
39721330497STony Lindgren 		usleep_range(200, 300);
39821330497STony Lindgren 	} while (retries--);
39921330497STony Lindgren 
40021330497STony Lindgren 	dev_err(d->dev, "pll failed to lock\n");
40121330497STony Lindgren 	return -ETIMEDOUT;
40221330497STony Lindgren }
40321330497STony Lindgren 
ti_adpll_prepare(struct clk_hw * hw)40421330497STony Lindgren static int ti_adpll_prepare(struct clk_hw *hw)
40521330497STony Lindgren {
40621330497STony Lindgren 	struct ti_adpll_dco_data *dco = to_dco(hw);
40721330497STony Lindgren 	struct ti_adpll_data *d = to_adpll(dco);
40821330497STony Lindgren 
40921330497STony Lindgren 	ti_adpll_clear_idle_bypass(d);
41021330497STony Lindgren 	ti_adpll_wait_lock(d);
41121330497STony Lindgren 
41221330497STony Lindgren 	return 0;
41321330497STony Lindgren }
41421330497STony Lindgren 
ti_adpll_unprepare(struct clk_hw * hw)41521330497STony Lindgren static void ti_adpll_unprepare(struct clk_hw *hw)
41621330497STony Lindgren {
41721330497STony Lindgren 	struct ti_adpll_dco_data *dco = to_dco(hw);
41821330497STony Lindgren 	struct ti_adpll_data *d = to_adpll(dco);
41921330497STony Lindgren 
42021330497STony Lindgren 	ti_adpll_set_idle_bypass(d);
42121330497STony Lindgren }
42221330497STony Lindgren 
ti_adpll_is_prepared(struct clk_hw * hw)42321330497STony Lindgren static int ti_adpll_is_prepared(struct clk_hw *hw)
42421330497STony Lindgren {
42521330497STony Lindgren 	struct ti_adpll_dco_data *dco = to_dco(hw);
42621330497STony Lindgren 	struct ti_adpll_data *d = to_adpll(dco);
42721330497STony Lindgren 
42821330497STony Lindgren 	return ti_adpll_is_locked(d);
42921330497STony Lindgren }
43021330497STony Lindgren 
43121330497STony Lindgren /*
43221330497STony Lindgren  * Note that the DCO clock is never subject to bypass: if the PLL is off,
43321330497STony Lindgren  * dcoclk is low.
43421330497STony Lindgren  */
ti_adpll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)43521330497STony Lindgren static unsigned long ti_adpll_recalc_rate(struct clk_hw *hw,
43621330497STony Lindgren 					  unsigned long parent_rate)
43721330497STony Lindgren {
43821330497STony Lindgren 	struct ti_adpll_dco_data *dco = to_dco(hw);
43921330497STony Lindgren 	struct ti_adpll_data *d = to_adpll(dco);
44021330497STony Lindgren 	u32 frac_m, divider, v;
44121330497STony Lindgren 	u64 rate;
44221330497STony Lindgren 	unsigned long flags;
44321330497STony Lindgren 
44421330497STony Lindgren 	if (ti_adpll_clock_is_bypass(d))
44521330497STony Lindgren 		return 0;
44621330497STony Lindgren 
44721330497STony Lindgren 	spin_lock_irqsave(&d->lock, flags);
44821330497STony Lindgren 	frac_m = readl_relaxed(d->regs + ADPLL_FRACDIV_OFFSET);
44921330497STony Lindgren 	frac_m &= ADPLL_FRACDIV_FRACTIONALM_MASK;
4508a8b6eb7SStephen Boyd 	rate = (u64)readw_relaxed(d->regs + ADPLL_MN2DIV_OFFSET) << 18;
45121330497STony Lindgren 	rate += frac_m;
45221330497STony Lindgren 	rate *= parent_rate;
45321330497STony Lindgren 	divider = (readw_relaxed(d->regs + ADPLL_M2NDIV_OFFSET) + 1) << 18;
45421330497STony Lindgren 	spin_unlock_irqrestore(&d->lock, flags);
45521330497STony Lindgren 
45621330497STony Lindgren 	do_div(rate, divider);
45721330497STony Lindgren 
45821330497STony Lindgren 	if (d->c->is_type_s) {
45921330497STony Lindgren 		v = readl_relaxed(d->regs + ADPLL_CLKCTRL_OFFSET);
46021330497STony Lindgren 		if (v & BIT(ADPLL_CLKCTRL_REGM4XEN_ADPLL_S))
46121330497STony Lindgren 			rate *= 4;
46221330497STony Lindgren 		rate *= 2;
46321330497STony Lindgren 	}
46421330497STony Lindgren 
46521330497STony Lindgren 	return rate;
46621330497STony Lindgren }
46721330497STony Lindgren 
46821330497STony Lindgren /* PLL parent is always clkinp, bypass only affects the children */
ti_adpll_get_parent(struct clk_hw * hw)46921330497STony Lindgren static u8 ti_adpll_get_parent(struct clk_hw *hw)
47021330497STony Lindgren {
47121330497STony Lindgren 	return 0;
47221330497STony Lindgren }
47321330497STony Lindgren 
4747cc566a8SBhumika Goyal static const struct clk_ops ti_adpll_ops = {
47521330497STony Lindgren 	.prepare = ti_adpll_prepare,
47621330497STony Lindgren 	.unprepare = ti_adpll_unprepare,
47721330497STony Lindgren 	.is_prepared = ti_adpll_is_prepared,
47821330497STony Lindgren 	.recalc_rate = ti_adpll_recalc_rate,
47921330497STony Lindgren 	.get_parent = ti_adpll_get_parent,
48021330497STony Lindgren };
48121330497STony Lindgren 
ti_adpll_init_dco(struct ti_adpll_data * d)48221330497STony Lindgren static int ti_adpll_init_dco(struct ti_adpll_data *d)
48321330497STony Lindgren {
48421330497STony Lindgren 	struct clk_init_data init;
48521330497STony Lindgren 	struct clk *clock;
48621330497STony Lindgren 	const char *postfix;
48721330497STony Lindgren 	int width, err;
48821330497STony Lindgren 
489a86854d0SKees Cook 	d->outputs.clks = devm_kcalloc(d->dev,
49021330497STony Lindgren 				       MAX_ADPLL_OUTPUTS,
491a86854d0SKees Cook 				       sizeof(struct clk *),
49221330497STony Lindgren 				       GFP_KERNEL);
49321330497STony Lindgren 	if (!d->outputs.clks)
49421330497STony Lindgren 		return -ENOMEM;
49521330497STony Lindgren 
49621330497STony Lindgren 	if (d->c->output_index < 0)
49721330497STony Lindgren 		postfix = "dco";
49821330497STony Lindgren 	else
49921330497STony Lindgren 		postfix = NULL;
50021330497STony Lindgren 
50121330497STony Lindgren 	init.name = ti_adpll_clk_get_name(d, d->c->output_index, postfix);
50221330497STony Lindgren 	if (!init.name)
50321330497STony Lindgren 		return -EINVAL;
50421330497STony Lindgren 
50521330497STony Lindgren 	init.parent_names = d->parent_names;
50621330497STony Lindgren 	init.num_parents = d->c->nr_max_inputs;
50721330497STony Lindgren 	init.ops = &ti_adpll_ops;
50821330497STony Lindgren 	init.flags = CLK_GET_RATE_NOCACHE;
50921330497STony Lindgren 	d->dco.hw.init = &init;
51021330497STony Lindgren 
51121330497STony Lindgren 	if (d->c->is_type_s)
51221330497STony Lindgren 		width = 5;
51321330497STony Lindgren 	else
51421330497STony Lindgren 		width = 4;
51521330497STony Lindgren 
51621330497STony Lindgren 	/* Internal input clock divider N2 */
51721330497STony Lindgren 	err = ti_adpll_init_divider(d, TI_ADPLL_N2, -ENODEV, "n2",
51821330497STony Lindgren 				    d->parent_clocks[TI_ADPLL_CLKINP],
51921330497STony Lindgren 				    d->regs + ADPLL_MN2DIV_OFFSET,
52021330497STony Lindgren 				    ADPLL_MN2DIV_N2, width, 0);
52121330497STony Lindgren 	if (err)
52221330497STony Lindgren 		return err;
52321330497STony Lindgren 
52421330497STony Lindgren 	clock = devm_clk_register(d->dev, &d->dco.hw);
52521330497STony Lindgren 	if (IS_ERR(clock))
52621330497STony Lindgren 		return PTR_ERR(clock);
52721330497STony Lindgren 
52821330497STony Lindgren 	return ti_adpll_setup_clock(d, clock, TI_ADPLL_DCO, d->c->output_index,
52921330497STony Lindgren 				    init.name, NULL);
53021330497STony Lindgren }
53121330497STony Lindgren 
ti_adpll_clkout_enable(struct clk_hw * hw)53221330497STony Lindgren static int ti_adpll_clkout_enable(struct clk_hw *hw)
53321330497STony Lindgren {
53421330497STony Lindgren 	struct ti_adpll_clkout_data *co = to_clkout(hw);
53521330497STony Lindgren 	struct clk_hw *gate_hw = &co->gate.hw;
53621330497STony Lindgren 
53721330497STony Lindgren 	__clk_hw_set_clk(gate_hw, hw);
53821330497STony Lindgren 
53921330497STony Lindgren 	return clk_gate_ops.enable(gate_hw);
54021330497STony Lindgren }
54121330497STony Lindgren 
ti_adpll_clkout_disable(struct clk_hw * hw)54221330497STony Lindgren static void ti_adpll_clkout_disable(struct clk_hw *hw)
54321330497STony Lindgren {
54421330497STony Lindgren 	struct ti_adpll_clkout_data *co = to_clkout(hw);
54521330497STony Lindgren 	struct clk_hw *gate_hw = &co->gate.hw;
54621330497STony Lindgren 
54721330497STony Lindgren 	__clk_hw_set_clk(gate_hw, hw);
54821330497STony Lindgren 	clk_gate_ops.disable(gate_hw);
54921330497STony Lindgren }
55021330497STony Lindgren 
ti_adpll_clkout_is_enabled(struct clk_hw * hw)55121330497STony Lindgren static int ti_adpll_clkout_is_enabled(struct clk_hw *hw)
55221330497STony Lindgren {
55321330497STony Lindgren 	struct ti_adpll_clkout_data *co = to_clkout(hw);
55421330497STony Lindgren 	struct clk_hw *gate_hw = &co->gate.hw;
55521330497STony Lindgren 
55621330497STony Lindgren 	__clk_hw_set_clk(gate_hw, hw);
55721330497STony Lindgren 
55821330497STony Lindgren 	return clk_gate_ops.is_enabled(gate_hw);
55921330497STony Lindgren }
56021330497STony Lindgren 
56121330497STony Lindgren /* Setting PLL bypass puts clkout and clkoutx2 into bypass */
ti_adpll_clkout_get_parent(struct clk_hw * hw)56221330497STony Lindgren static u8 ti_adpll_clkout_get_parent(struct clk_hw *hw)
56321330497STony Lindgren {
56421330497STony Lindgren 	struct ti_adpll_clkout_data *co = to_clkout(hw);
56521330497STony Lindgren 	struct ti_adpll_data *d = co->adpll;
56621330497STony Lindgren 
56721330497STony Lindgren 	return ti_adpll_clock_is_bypass(d);
56821330497STony Lindgren }
56921330497STony Lindgren 
ti_adpll_init_clkout(struct ti_adpll_data * d,enum ti_adpll_clocks index,int output_index,int gate_bit,char * name,struct clk * clk0,struct clk * clk1)57021330497STony Lindgren static int ti_adpll_init_clkout(struct ti_adpll_data *d,
57121330497STony Lindgren 				enum ti_adpll_clocks index,
57221330497STony Lindgren 				int output_index, int gate_bit,
57321330497STony Lindgren 				char *name, struct clk *clk0,
57421330497STony Lindgren 				struct clk *clk1)
57521330497STony Lindgren {
57621330497STony Lindgren 	struct ti_adpll_clkout_data *co;
57721330497STony Lindgren 	struct clk_init_data init;
57821330497STony Lindgren 	struct clk_ops *ops;
57921330497STony Lindgren 	const char *parent_names[2];
58021330497STony Lindgren 	const char *child_name;
58121330497STony Lindgren 	struct clk *clock;
58221330497STony Lindgren 	int err;
58321330497STony Lindgren 
58421330497STony Lindgren 	co = devm_kzalloc(d->dev, sizeof(*co), GFP_KERNEL);
58521330497STony Lindgren 	if (!co)
58621330497STony Lindgren 		return -ENOMEM;
58721330497STony Lindgren 	co->adpll = d;
58821330497STony Lindgren 
58921330497STony Lindgren 	err = of_property_read_string_index(d->np,
59021330497STony Lindgren 					    "clock-output-names",
59121330497STony Lindgren 					    output_index,
59221330497STony Lindgren 					    &child_name);
59321330497STony Lindgren 	if (err)
59421330497STony Lindgren 		return err;
59521330497STony Lindgren 
59621330497STony Lindgren 	ops = devm_kzalloc(d->dev, sizeof(*ops), GFP_KERNEL);
59721330497STony Lindgren 	if (!ops)
59821330497STony Lindgren 		return -ENOMEM;
59921330497STony Lindgren 
60021330497STony Lindgren 	init.name = child_name;
60121330497STony Lindgren 	init.ops = ops;
6028aa09cf3STero Kristo 	init.flags = 0;
60321330497STony Lindgren 	co->hw.init = &init;
60421330497STony Lindgren 	parent_names[0] = __clk_get_name(clk0);
60521330497STony Lindgren 	parent_names[1] = __clk_get_name(clk1);
60621330497STony Lindgren 	init.parent_names = parent_names;
60721330497STony Lindgren 	init.num_parents = 2;
60821330497STony Lindgren 
60921330497STony Lindgren 	ops->get_parent = ti_adpll_clkout_get_parent;
61021330497STony Lindgren 	ops->determine_rate = __clk_mux_determine_rate;
61121330497STony Lindgren 	if (gate_bit) {
61221330497STony Lindgren 		co->gate.lock = &d->lock;
61321330497STony Lindgren 		co->gate.reg = d->regs + ADPLL_CLKCTRL_OFFSET;
61421330497STony Lindgren 		co->gate.bit_idx = gate_bit;
61521330497STony Lindgren 		ops->enable = ti_adpll_clkout_enable;
61621330497STony Lindgren 		ops->disable = ti_adpll_clkout_disable;
61721330497STony Lindgren 		ops->is_enabled = ti_adpll_clkout_is_enabled;
61821330497STony Lindgren 	}
61921330497STony Lindgren 
62021330497STony Lindgren 	clock = devm_clk_register(d->dev, &co->hw);
62121330497STony Lindgren 	if (IS_ERR(clock)) {
62221330497STony Lindgren 		dev_err(d->dev, "failed to register output %s: %li\n",
62321330497STony Lindgren 			name, PTR_ERR(clock));
62421330497STony Lindgren 		return PTR_ERR(clock);
62521330497STony Lindgren 	}
62621330497STony Lindgren 
62721330497STony Lindgren 	return ti_adpll_setup_clock(d, clock, index, output_index, child_name,
62821330497STony Lindgren 				    NULL);
62921330497STony Lindgren }
63021330497STony Lindgren 
ti_adpll_init_children_adpll_s(struct ti_adpll_data * d)63121330497STony Lindgren static int ti_adpll_init_children_adpll_s(struct ti_adpll_data *d)
63221330497STony Lindgren {
63321330497STony Lindgren 	int err;
63421330497STony Lindgren 
63521330497STony Lindgren 	if (!d->c->is_type_s)
63621330497STony Lindgren 		return 0;
63721330497STony Lindgren 
63821330497STony Lindgren 	/* Internal mux, sources from divider N2 or clkinpulow */
63921330497STony Lindgren 	err = ti_adpll_init_mux(d, TI_ADPLL_BYPASS, "bypass",
64021330497STony Lindgren 				d->clocks[TI_ADPLL_N2].clk,
64121330497STony Lindgren 				d->parent_clocks[TI_ADPLL_CLKINPULOW],
64221330497STony Lindgren 				d->regs + ADPLL_CLKCTRL_OFFSET,
64321330497STony Lindgren 				ADPLL_CLKCTRL_ULOWCLKEN);
64421330497STony Lindgren 	if (err)
64521330497STony Lindgren 		return err;
64621330497STony Lindgren 
64721330497STony Lindgren 	/* Internal divider M2, sources DCO */
64821330497STony Lindgren 	err = ti_adpll_init_divider(d, TI_ADPLL_M2, -ENODEV, "m2",
64921330497STony Lindgren 				    d->clocks[TI_ADPLL_DCO].clk,
65021330497STony Lindgren 				    d->regs + ADPLL_M2NDIV_OFFSET,
65121330497STony Lindgren 				    ADPLL_M2NDIV_M2,
65221330497STony Lindgren 				    ADPLL_M2NDIV_M2_ADPLL_S_WIDTH,
65321330497STony Lindgren 				    CLK_DIVIDER_ONE_BASED);
65421330497STony Lindgren 	if (err)
65521330497STony Lindgren 		return err;
65621330497STony Lindgren 
65721330497STony Lindgren 	/* Internal fixed divider, after M2 before clkout */
65821330497STony Lindgren 	err = ti_adpll_init_fixed_factor(d, TI_ADPLL_DIV2, "div2",
65921330497STony Lindgren 					 d->clocks[TI_ADPLL_M2].clk,
66021330497STony Lindgren 					 1, 2);
66121330497STony Lindgren 	if (err)
66221330497STony Lindgren 		return err;
66321330497STony Lindgren 
66421330497STony Lindgren 	/* Output clkout with a mux and gate, sources from div2 or bypass */
66521330497STony Lindgren 	err = ti_adpll_init_clkout(d, TI_ADPLL_CLKOUT, TI_ADPLL_S_CLKOUT,
66621330497STony Lindgren 				   ADPLL_CLKCTRL_CLKOUTEN, "clkout",
66721330497STony Lindgren 				   d->clocks[TI_ADPLL_DIV2].clk,
66821330497STony Lindgren 				   d->clocks[TI_ADPLL_BYPASS].clk);
66921330497STony Lindgren 	if (err)
67021330497STony Lindgren 		return err;
67121330497STony Lindgren 
67221330497STony Lindgren 	/* Output clkoutx2 with a mux and gate, sources from M2 or bypass */
67321330497STony Lindgren 	err = ti_adpll_init_clkout(d, TI_ADPLL_CLKOUT2, TI_ADPLL_S_CLKOUTX2, 0,
67421330497STony Lindgren 				   "clkout2", d->clocks[TI_ADPLL_M2].clk,
67521330497STony Lindgren 				   d->clocks[TI_ADPLL_BYPASS].clk);
67621330497STony Lindgren 	if (err)
67721330497STony Lindgren 		return err;
67821330497STony Lindgren 
67921330497STony Lindgren 	/* Internal mux, sources from DCO and clkinphif */
68021330497STony Lindgren 	if (d->parent_clocks[TI_ADPLL_CLKINPHIF]) {
68121330497STony Lindgren 		err = ti_adpll_init_mux(d, TI_ADPLL_HIF, "hif",
68221330497STony Lindgren 					d->clocks[TI_ADPLL_DCO].clk,
68321330497STony Lindgren 					d->parent_clocks[TI_ADPLL_CLKINPHIF],
68421330497STony Lindgren 					d->regs + ADPLL_CLKCTRL_OFFSET,
68521330497STony Lindgren 					ADPLL_CLKINPHIFSEL_ADPLL_S);
68621330497STony Lindgren 		if (err)
68721330497STony Lindgren 			return err;
68821330497STony Lindgren 	}
68921330497STony Lindgren 
69021330497STony Lindgren 	/* Output clkouthif with a divider M3, sources from hif */
69121330497STony Lindgren 	err = ti_adpll_init_divider(d, TI_ADPLL_M3, TI_ADPLL_S_CLKOUTHIF, "m3",
69221330497STony Lindgren 				    d->clocks[TI_ADPLL_HIF].clk,
69321330497STony Lindgren 				    d->regs + ADPLL_M3DIV_OFFSET,
69421330497STony Lindgren 				    ADPLL_M3DIV_M3,
69521330497STony Lindgren 				    ADPLL_M3DIV_M3_WIDTH,
69621330497STony Lindgren 				    CLK_DIVIDER_ONE_BASED);
69721330497STony Lindgren 	if (err)
69821330497STony Lindgren 		return err;
69921330497STony Lindgren 
70021330497STony Lindgren 	/* Output clock dcoclkldo is the DCO */
70121330497STony Lindgren 
70221330497STony Lindgren 	return 0;
70321330497STony Lindgren }
70421330497STony Lindgren 
ti_adpll_init_children_adpll_lj(struct ti_adpll_data * d)70521330497STony Lindgren static int ti_adpll_init_children_adpll_lj(struct ti_adpll_data *d)
70621330497STony Lindgren {
70721330497STony Lindgren 	int err;
70821330497STony Lindgren 
70921330497STony Lindgren 	if (d->c->is_type_s)
71021330497STony Lindgren 		return 0;
71121330497STony Lindgren 
71221330497STony Lindgren 	/* Output clkdcoldo, gated output of DCO */
71321330497STony Lindgren 	err = ti_adpll_init_gate(d, TI_ADPLL_DCO_GATE, TI_ADPLL_LJ_CLKDCOLDO,
71421330497STony Lindgren 				 "clkdcoldo", d->clocks[TI_ADPLL_DCO].clk,
71521330497STony Lindgren 				 d->regs + ADPLL_CLKCTRL_OFFSET,
71621330497STony Lindgren 				 ADPLL_CLKCTRL_CLKDCOLDOEN, 0);
71721330497STony Lindgren 	if (err)
71821330497STony Lindgren 		return err;
71921330497STony Lindgren 
72021330497STony Lindgren 	/* Internal divider M2, sources from DCO */
72121330497STony Lindgren 	err = ti_adpll_init_divider(d, TI_ADPLL_M2, -ENODEV,
72221330497STony Lindgren 				    "m2", d->clocks[TI_ADPLL_DCO].clk,
72321330497STony Lindgren 				    d->regs + ADPLL_M2NDIV_OFFSET,
72421330497STony Lindgren 				    ADPLL_M2NDIV_M2,
72521330497STony Lindgren 				    ADPLL_M2NDIV_M2_ADPLL_LJ_WIDTH,
72621330497STony Lindgren 				    CLK_DIVIDER_ONE_BASED);
72721330497STony Lindgren 	if (err)
72821330497STony Lindgren 		return err;
72921330497STony Lindgren 
73021330497STony Lindgren 	/* Output clkoutldo, gated output of M2 */
73121330497STony Lindgren 	err = ti_adpll_init_gate(d, TI_ADPLL_M2_GATE, TI_ADPLL_LJ_CLKOUTLDO,
73221330497STony Lindgren 				 "clkoutldo", d->clocks[TI_ADPLL_M2].clk,
73321330497STony Lindgren 				 d->regs + ADPLL_CLKCTRL_OFFSET,
73421330497STony Lindgren 				 ADPLL_CLKCTRL_CLKOUTLDOEN_ADPLL_LJ,
73521330497STony Lindgren 				 0);
73621330497STony Lindgren 	if (err)
73721330497STony Lindgren 		return err;
73821330497STony Lindgren 
73921330497STony Lindgren 	/* Internal mux, sources from divider N2 or clkinpulow */
74021330497STony Lindgren 	err = ti_adpll_init_mux(d, TI_ADPLL_BYPASS, "bypass",
74121330497STony Lindgren 				d->clocks[TI_ADPLL_N2].clk,
74221330497STony Lindgren 				d->parent_clocks[TI_ADPLL_CLKINPULOW],
74321330497STony Lindgren 				d->regs + ADPLL_CLKCTRL_OFFSET,
74421330497STony Lindgren 				ADPLL_CLKCTRL_ULOWCLKEN);
74521330497STony Lindgren 	if (err)
74621330497STony Lindgren 		return err;
74721330497STony Lindgren 
74821330497STony Lindgren 	/* Output clkout, sources M2 or bypass */
74921330497STony Lindgren 	err = ti_adpll_init_clkout(d, TI_ADPLL_CLKOUT, TI_ADPLL_S_CLKOUT,
75021330497STony Lindgren 				   ADPLL_CLKCTRL_CLKOUTEN, "clkout",
75121330497STony Lindgren 				   d->clocks[TI_ADPLL_M2].clk,
75221330497STony Lindgren 				   d->clocks[TI_ADPLL_BYPASS].clk);
75321330497STony Lindgren 	if (err)
75421330497STony Lindgren 		return err;
75521330497STony Lindgren 
75621330497STony Lindgren 	return 0;
75721330497STony Lindgren }
75821330497STony Lindgren 
ti_adpll_free_resources(struct ti_adpll_data * d)75921330497STony Lindgren static void ti_adpll_free_resources(struct ti_adpll_data *d)
76021330497STony Lindgren {
76121330497STony Lindgren 	int i;
76221330497STony Lindgren 
76321330497STony Lindgren 	for (i = TI_ADPLL_M3; i >= 0; i--) {
76421330497STony Lindgren 		struct ti_adpll_clock *ac = &d->clocks[i];
76521330497STony Lindgren 
76621330497STony Lindgren 		if (!ac || IS_ERR_OR_NULL(ac->clk))
76721330497STony Lindgren 			continue;
76821330497STony Lindgren 		if (ac->cl)
76921330497STony Lindgren 			clkdev_drop(ac->cl);
77021330497STony Lindgren 		if (ac->unregister)
77121330497STony Lindgren 			ac->unregister(ac->clk);
77221330497STony Lindgren 	}
77321330497STony Lindgren }
77421330497STony Lindgren 
77521330497STony Lindgren /* MPU PLL manages the lock register for all PLLs */
ti_adpll_unlock_all(void __iomem * reg)77621330497STony Lindgren static void ti_adpll_unlock_all(void __iomem *reg)
77721330497STony Lindgren {
77821330497STony Lindgren 	u32 v;
77921330497STony Lindgren 
78021330497STony Lindgren 	v = readl_relaxed(reg);
78121330497STony Lindgren 	if (v == ADPLL_PLLSS_MMR_LOCK_ENABLED)
78221330497STony Lindgren 		writel_relaxed(ADPLL_PLLSS_MMR_UNLOCK_MAGIC, reg);
78321330497STony Lindgren }
78421330497STony Lindgren 
ti_adpll_init_registers(struct ti_adpll_data * d)78521330497STony Lindgren static int ti_adpll_init_registers(struct ti_adpll_data *d)
78621330497STony Lindgren {
78721330497STony Lindgren 	int register_offset = 0;
78821330497STony Lindgren 
78921330497STony Lindgren 	if (d->c->is_type_s) {
79021330497STony Lindgren 		register_offset = 8;
79121330497STony Lindgren 		ti_adpll_unlock_all(d->iobase + ADPLL_PLLSS_MMR_LOCK_OFFSET);
79221330497STony Lindgren 	}
79321330497STony Lindgren 
79421330497STony Lindgren 	d->regs = d->iobase + register_offset + ADPLL_PWRCTRL_OFFSET;
79521330497STony Lindgren 
79621330497STony Lindgren 	return 0;
79721330497STony Lindgren }
79821330497STony Lindgren 
ti_adpll_init_inputs(struct ti_adpll_data * d)79921330497STony Lindgren static int ti_adpll_init_inputs(struct ti_adpll_data *d)
80021330497STony Lindgren {
80192592280SColin Ian King 	static const char error[] = "need at least %i inputs";
80221330497STony Lindgren 	struct clk *clock;
80321330497STony Lindgren 	int nr_inputs;
80421330497STony Lindgren 
80521330497STony Lindgren 	nr_inputs = of_clk_get_parent_count(d->np);
80621330497STony Lindgren 	if (nr_inputs < d->c->nr_max_inputs) {
80721330497STony Lindgren 		dev_err(d->dev, error, nr_inputs);
80821330497STony Lindgren 		return -EINVAL;
80921330497STony Lindgren 	}
81021330497STony Lindgren 	of_clk_parent_fill(d->np, d->parent_names, nr_inputs);
81121330497STony Lindgren 
81221330497STony Lindgren 	clock = devm_clk_get(d->dev, d->parent_names[0]);
81321330497STony Lindgren 	if (IS_ERR(clock)) {
81421330497STony Lindgren 		dev_err(d->dev, "could not get clkinp\n");
81521330497STony Lindgren 		return PTR_ERR(clock);
81621330497STony Lindgren 	}
81721330497STony Lindgren 	d->parent_clocks[TI_ADPLL_CLKINP] = clock;
81821330497STony Lindgren 
81921330497STony Lindgren 	clock = devm_clk_get(d->dev, d->parent_names[1]);
82021330497STony Lindgren 	if (IS_ERR(clock)) {
82121330497STony Lindgren 		dev_err(d->dev, "could not get clkinpulow clock\n");
82221330497STony Lindgren 		return PTR_ERR(clock);
82321330497STony Lindgren 	}
82421330497STony Lindgren 	d->parent_clocks[TI_ADPLL_CLKINPULOW] = clock;
82521330497STony Lindgren 
82621330497STony Lindgren 	if (d->c->is_type_s) {
82721330497STony Lindgren 		clock =  devm_clk_get(d->dev, d->parent_names[2]);
82821330497STony Lindgren 		if (IS_ERR(clock)) {
82921330497STony Lindgren 			dev_err(d->dev, "could not get clkinphif clock\n");
83021330497STony Lindgren 			return PTR_ERR(clock);
83121330497STony Lindgren 		}
83221330497STony Lindgren 		d->parent_clocks[TI_ADPLL_CLKINPHIF] = clock;
83321330497STony Lindgren 	}
83421330497STony Lindgren 
83521330497STony Lindgren 	return 0;
83621330497STony Lindgren }
83721330497STony Lindgren 
83821330497STony Lindgren static const struct ti_adpll_platform_data ti_adpll_type_s = {
83921330497STony Lindgren 	.is_type_s = true,
84021330497STony Lindgren 	.nr_max_inputs = MAX_ADPLL_INPUTS,
84121330497STony Lindgren 	.nr_max_outputs = MAX_ADPLL_OUTPUTS,
84221330497STony Lindgren 	.output_index = TI_ADPLL_S_DCOCLKLDO,
84321330497STony Lindgren };
84421330497STony Lindgren 
84521330497STony Lindgren static const struct ti_adpll_platform_data ti_adpll_type_lj = {
84621330497STony Lindgren 	.is_type_s = false,
84721330497STony Lindgren 	.nr_max_inputs = MAX_ADPLL_INPUTS - 1,
84821330497STony Lindgren 	.nr_max_outputs = MAX_ADPLL_OUTPUTS - 1,
84921330497STony Lindgren 	.output_index = -EINVAL,
85021330497STony Lindgren };
85121330497STony Lindgren 
85221330497STony Lindgren static const struct of_device_id ti_adpll_match[] = {
85321330497STony Lindgren 	{ .compatible = "ti,dm814-adpll-s-clock", &ti_adpll_type_s },
85421330497STony Lindgren 	{ .compatible = "ti,dm814-adpll-lj-clock", &ti_adpll_type_lj },
85521330497STony Lindgren 	{},
85621330497STony Lindgren };
85721330497STony Lindgren MODULE_DEVICE_TABLE(of, ti_adpll_match);
85821330497STony Lindgren 
ti_adpll_probe(struct platform_device * pdev)85921330497STony Lindgren static int ti_adpll_probe(struct platform_device *pdev)
86021330497STony Lindgren {
86121330497STony Lindgren 	struct device_node *node = pdev->dev.of_node;
86221330497STony Lindgren 	struct device *dev = &pdev->dev;
86321330497STony Lindgren 	const struct of_device_id *match;
86421330497STony Lindgren 	const struct ti_adpll_platform_data *pdata;
86521330497STony Lindgren 	struct ti_adpll_data *d;
86621330497STony Lindgren 	struct resource *res;
86721330497STony Lindgren 	int err;
86821330497STony Lindgren 
86921330497STony Lindgren 	match = of_match_device(ti_adpll_match, dev);
87021330497STony Lindgren 	if (match)
87121330497STony Lindgren 		pdata = match->data;
87221330497STony Lindgren 	else
87321330497STony Lindgren 		return -ENODEV;
87421330497STony Lindgren 
87521330497STony Lindgren 	d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
87621330497STony Lindgren 	if (!d)
87721330497STony Lindgren 		return -ENOMEM;
87821330497STony Lindgren 	d->dev = dev;
87921330497STony Lindgren 	d->np = node;
88021330497STony Lindgren 	d->c = pdata;
88121330497STony Lindgren 	dev_set_drvdata(d->dev, d);
88221330497STony Lindgren 	spin_lock_init(&d->lock);
88321330497STony Lindgren 
884*183a1f1bSYangtao Li 	d->iobase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
8859ba98c08SYu Jiahua 	if (IS_ERR(d->iobase))
88621330497STony Lindgren 		return PTR_ERR(d->iobase);
887*183a1f1bSYangtao Li 	d->pa = res->start;
88821330497STony Lindgren 
88921330497STony Lindgren 	err = ti_adpll_init_registers(d);
89021330497STony Lindgren 	if (err)
89121330497STony Lindgren 		return err;
89221330497STony Lindgren 
89321330497STony Lindgren 	err = ti_adpll_init_inputs(d);
89421330497STony Lindgren 	if (err)
89521330497STony Lindgren 		return err;
89621330497STony Lindgren 
897a86854d0SKees Cook 	d->clocks = devm_kcalloc(d->dev,
89821330497STony Lindgren 				 TI_ADPLL_NR_CLOCKS,
899a86854d0SKees Cook 				 sizeof(struct ti_adpll_clock),
90021330497STony Lindgren 				 GFP_KERNEL);
90121330497STony Lindgren 	if (!d->clocks)
9028a8b6eb7SStephen Boyd 		return -ENOMEM;
90321330497STony Lindgren 
90421330497STony Lindgren 	err = ti_adpll_init_dco(d);
90521330497STony Lindgren 	if (err) {
90621330497STony Lindgren 		dev_err(dev, "could not register dco: %i\n", err);
90721330497STony Lindgren 		goto free;
90821330497STony Lindgren 	}
90921330497STony Lindgren 
91021330497STony Lindgren 	err = ti_adpll_init_children_adpll_s(d);
91121330497STony Lindgren 	if (err)
91221330497STony Lindgren 		goto free;
91321330497STony Lindgren 	err = ti_adpll_init_children_adpll_lj(d);
91421330497STony Lindgren 	if (err)
91521330497STony Lindgren 		goto free;
91621330497STony Lindgren 
91721330497STony Lindgren 	err = of_clk_add_provider(d->np, of_clk_src_onecell_get, &d->outputs);
91821330497STony Lindgren 	if (err)
91921330497STony Lindgren 		goto free;
92021330497STony Lindgren 
92121330497STony Lindgren 	return 0;
92221330497STony Lindgren 
92321330497STony Lindgren free:
92421330497STony Lindgren 	WARN_ON(1);
92521330497STony Lindgren 	ti_adpll_free_resources(d);
92621330497STony Lindgren 
92721330497STony Lindgren 	return err;
92821330497STony Lindgren }
92921330497STony Lindgren 
ti_adpll_remove(struct platform_device * pdev)930c8bb21beSUwe Kleine-König static void ti_adpll_remove(struct platform_device *pdev)
93121330497STony Lindgren {
93221330497STony Lindgren 	struct ti_adpll_data *d = dev_get_drvdata(&pdev->dev);
93321330497STony Lindgren 
93421330497STony Lindgren 	ti_adpll_free_resources(d);
93521330497STony Lindgren }
93621330497STony Lindgren 
93721330497STony Lindgren static struct platform_driver ti_adpll_driver = {
93821330497STony Lindgren 	.driver = {
93921330497STony Lindgren 		.name = "ti-adpll",
94021330497STony Lindgren 		.of_match_table = ti_adpll_match,
94121330497STony Lindgren 	},
94221330497STony Lindgren 	.probe = ti_adpll_probe,
943c8bb21beSUwe Kleine-König 	.remove_new = ti_adpll_remove,
94421330497STony Lindgren };
94521330497STony Lindgren 
ti_adpll_init(void)94621330497STony Lindgren static int __init ti_adpll_init(void)
94721330497STony Lindgren {
94821330497STony Lindgren 	return platform_driver_register(&ti_adpll_driver);
94921330497STony Lindgren }
95021330497STony Lindgren core_initcall(ti_adpll_init);
95121330497STony Lindgren 
ti_adpll_exit(void)95221330497STony Lindgren static void __exit ti_adpll_exit(void)
95321330497STony Lindgren {
95421330497STony Lindgren 	platform_driver_unregister(&ti_adpll_driver);
95521330497STony Lindgren }
95621330497STony Lindgren module_exit(ti_adpll_exit);
95721330497STony Lindgren 
95821330497STony Lindgren MODULE_DESCRIPTION("Clock driver for dm814x ADPLL");
95921330497STony Lindgren MODULE_ALIAS("platform:dm814-adpll-clock");
96021330497STony Lindgren MODULE_AUTHOR("Tony LIndgren <tony@atomide.com>");
96121330497STony Lindgren MODULE_LICENSE("GPL v2");
962