xref: /linux/drivers/clk/imx/clk-pllv1.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
27d6b5e4fSAnson Huang #include <linux/bits.h>
311f68120SShawn Guo #include <linux/clk-provider.h>
411f68120SShawn Guo #include <linux/io.h>
511f68120SShawn Guo #include <linux/slab.h>
611f68120SShawn Guo #include <linux/kernel.h>
711f68120SShawn Guo #include <linux/err.h>
811f68120SShawn Guo 
911f68120SShawn Guo #include "clk.h"
1011f68120SShawn Guo 
1111f68120SShawn Guo #define MFN_BITS	(10)
1211f68120SShawn Guo #define MFN_SIGN	(BIT(MFN_BITS - 1))
1311f68120SShawn Guo #define MFN_MASK	(MFN_SIGN - 1)
1411f68120SShawn Guo 
15*71e76231SRandy Dunlap /**
16*71e76231SRandy Dunlap  * struct clk_pllv1 - IMX PLLv1 clock descriptor
17*71e76231SRandy Dunlap  *
18*71e76231SRandy Dunlap  * @hw:		clock source
19*71e76231SRandy Dunlap  * @base:	base address of pll registers
20*71e76231SRandy Dunlap  * @type:	type of IMX_PLLV1
21*71e76231SRandy Dunlap  *
22*71e76231SRandy Dunlap  * PLL clock version 1, found on i.MX1/21/25/27/31/35
23*71e76231SRandy Dunlap  */
2411f68120SShawn Guo struct clk_pllv1 {
2511f68120SShawn Guo 	struct clk_hw	hw;
2611f68120SShawn Guo 	void __iomem	*base;
2711f68120SShawn Guo 	enum imx_pllv1_type type;
2811f68120SShawn Guo };
2911f68120SShawn Guo 
3011f68120SShawn Guo #define to_clk_pllv1(clk) (container_of(clk, struct clk_pllv1, clk))
3111f68120SShawn Guo 
is_imx1_pllv1(struct clk_pllv1 * pll)3211f68120SShawn Guo static inline bool is_imx1_pllv1(struct clk_pllv1 *pll)
3311f68120SShawn Guo {
3411f68120SShawn Guo 	return pll->type == IMX_PLLV1_IMX1;
3511f68120SShawn Guo }
3611f68120SShawn Guo 
is_imx21_pllv1(struct clk_pllv1 * pll)3711f68120SShawn Guo static inline bool is_imx21_pllv1(struct clk_pllv1 *pll)
3811f68120SShawn Guo {
3911f68120SShawn Guo 	return pll->type == IMX_PLLV1_IMX21;
4011f68120SShawn Guo }
4111f68120SShawn Guo 
is_imx27_pllv1(struct clk_pllv1 * pll)4211f68120SShawn Guo static inline bool is_imx27_pllv1(struct clk_pllv1 *pll)
4311f68120SShawn Guo {
4411f68120SShawn Guo 	return pll->type == IMX_PLLV1_IMX27;
4511f68120SShawn Guo }
4611f68120SShawn Guo 
mfn_is_negative(struct clk_pllv1 * pll,unsigned int mfn)4711f68120SShawn Guo static inline bool mfn_is_negative(struct clk_pllv1 *pll, unsigned int mfn)
4811f68120SShawn Guo {
4911f68120SShawn Guo 	return !is_imx1_pllv1(pll) && !is_imx21_pllv1(pll) && (mfn & MFN_SIGN);
5011f68120SShawn Guo }
5111f68120SShawn Guo 
clk_pllv1_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)5211f68120SShawn Guo static unsigned long clk_pllv1_recalc_rate(struct clk_hw *hw,
5311f68120SShawn Guo 		unsigned long parent_rate)
5411f68120SShawn Guo {
5511f68120SShawn Guo 	struct clk_pllv1 *pll = to_clk_pllv1(hw);
56741e96e8SNicolas Pitre 	unsigned long long ull;
5711f68120SShawn Guo 	int mfn_abs;
5811f68120SShawn Guo 	unsigned int mfi, mfn, mfd, pd;
5911f68120SShawn Guo 	u32 reg;
6011f68120SShawn Guo 	unsigned long rate;
6111f68120SShawn Guo 
6211f68120SShawn Guo 	reg = readl(pll->base);
6311f68120SShawn Guo 
6411f68120SShawn Guo 	/*
6511f68120SShawn Guo 	 * Get the resulting clock rate from a PLL register value and the input
6611f68120SShawn Guo 	 * frequency. PLLs with this register layout can be found on i.MX1,
6711f68120SShawn Guo 	 * i.MX21, i.MX27 and i,MX31
6811f68120SShawn Guo 	 *
6911f68120SShawn Guo 	 *                  mfi + mfn / (mfd + 1)
7011f68120SShawn Guo 	 *  f = 2 * f_ref * --------------------
7111f68120SShawn Guo 	 *                        pd + 1
7211f68120SShawn Guo 	 */
7311f68120SShawn Guo 
7411f68120SShawn Guo 	mfi = (reg >> 10) & 0xf;
7511f68120SShawn Guo 	mfn = reg & 0x3ff;
7611f68120SShawn Guo 	mfd = (reg >> 16) & 0x3ff;
7711f68120SShawn Guo 	pd =  (reg >> 26) & 0xf;
7811f68120SShawn Guo 
7911f68120SShawn Guo 	mfi = mfi <= 5 ? 5 : mfi;
8011f68120SShawn Guo 
8111f68120SShawn Guo 	mfn_abs = mfn;
8211f68120SShawn Guo 
8311f68120SShawn Guo 	/*
8411f68120SShawn Guo 	 * On all i.MXs except i.MX1 and i.MX21 mfn is a 10bit
8511f68120SShawn Guo 	 * 2's complements number.
8611f68120SShawn Guo 	 * On i.MX27 the bit 9 is the sign bit.
8711f68120SShawn Guo 	 */
8811f68120SShawn Guo 	if (mfn_is_negative(pll, mfn)) {
8911f68120SShawn Guo 		if (is_imx27_pllv1(pll))
9011f68120SShawn Guo 			mfn_abs = mfn & MFN_MASK;
9111f68120SShawn Guo 		else
9211f68120SShawn Guo 			mfn_abs = BIT(MFN_BITS) - mfn;
9311f68120SShawn Guo 	}
9411f68120SShawn Guo 
9511f68120SShawn Guo 	rate = parent_rate * 2;
9611f68120SShawn Guo 	rate /= pd + 1;
9711f68120SShawn Guo 
98741e96e8SNicolas Pitre 	ull = (unsigned long long)rate * mfn_abs;
9911f68120SShawn Guo 
100741e96e8SNicolas Pitre 	do_div(ull, mfd + 1);
10111f68120SShawn Guo 
10211f68120SShawn Guo 	if (mfn_is_negative(pll, mfn))
103741e96e8SNicolas Pitre 		ull = (rate * mfi) - ull;
104741e96e8SNicolas Pitre 	else
105741e96e8SNicolas Pitre 		ull = (rate * mfi) + ull;
10611f68120SShawn Guo 
107741e96e8SNicolas Pitre 	return ull;
10811f68120SShawn Guo }
10911f68120SShawn Guo 
110fa1da981SBhumika Goyal static const struct clk_ops clk_pllv1_ops = {
11111f68120SShawn Guo 	.recalc_rate = clk_pllv1_recalc_rate,
11211f68120SShawn Guo };
11311f68120SShawn Guo 
imx_clk_hw_pllv1(enum imx_pllv1_type type,const char * name,const char * parent,void __iomem * base)114556f7880SAbel Vesa struct clk_hw *imx_clk_hw_pllv1(enum imx_pllv1_type type, const char *name,
11511f68120SShawn Guo 		const char *parent, void __iomem *base)
11611f68120SShawn Guo {
11711f68120SShawn Guo 	struct clk_pllv1 *pll;
118556f7880SAbel Vesa 	struct clk_hw *hw;
11911f68120SShawn Guo 	struct clk_init_data init;
120556f7880SAbel Vesa 	int ret;
12111f68120SShawn Guo 
12211f68120SShawn Guo 	pll = kmalloc(sizeof(*pll), GFP_KERNEL);
12311f68120SShawn Guo 	if (!pll)
12411f68120SShawn Guo 		return ERR_PTR(-ENOMEM);
12511f68120SShawn Guo 
12611f68120SShawn Guo 	pll->base = base;
12711f68120SShawn Guo 	pll->type = type;
12811f68120SShawn Guo 
12911f68120SShawn Guo 	init.name = name;
13011f68120SShawn Guo 	init.ops = &clk_pllv1_ops;
13111f68120SShawn Guo 	init.flags = 0;
13211f68120SShawn Guo 	init.parent_names = &parent;
13311f68120SShawn Guo 	init.num_parents = 1;
13411f68120SShawn Guo 
13511f68120SShawn Guo 	pll->hw.init = &init;
136556f7880SAbel Vesa 	hw = &pll->hw;
13711f68120SShawn Guo 
138556f7880SAbel Vesa 	ret = clk_hw_register(NULL, hw);
139556f7880SAbel Vesa 	if (ret) {
14011f68120SShawn Guo 		kfree(pll);
141556f7880SAbel Vesa 		return ERR_PTR(ret);
142556f7880SAbel Vesa 	}
14311f68120SShawn Guo 
144556f7880SAbel Vesa 	return hw;
14511f68120SShawn Guo }
146