Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Anson Huang | 7d6b5e4f | 2020-08-05 07:17:29 +0800 | [diff] [blame] | 2 | #include <linux/bits.h> |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 3 | #include <linux/clk-provider.h> |
| 4 | #include <linux/io.h> |
| 5 | #include <linux/slab.h> |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/err.h> |
Sascha Hauer | 3a84d17 | 2012-09-11 08:50:00 +0200 | [diff] [blame] | 8 | |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 9 | #include "clk.h" |
| 10 | |
| 11 | /** |
| 12 | * pll v1 |
| 13 | * |
| 14 | * @clk_hw clock source |
| 15 | * @parent the parent clock name |
| 16 | * @base base address of pll registers |
| 17 | * |
| 18 | * PLL clock version 1, found on i.MX1/21/25/27/31/35 |
| 19 | */ |
Alexander Shiyan | a594790 | 2013-11-10 13:34:49 +0400 | [diff] [blame] | 20 | |
| 21 | #define MFN_BITS (10) |
| 22 | #define MFN_SIGN (BIT(MFN_BITS - 1)) |
| 23 | #define MFN_MASK (MFN_SIGN - 1) |
| 24 | |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 25 | struct clk_pllv1 { |
| 26 | struct clk_hw hw; |
| 27 | void __iomem *base; |
Shawn Guo | 3bec5f8 | 2015-04-26 13:33:39 +0800 | [diff] [blame] | 28 | enum imx_pllv1_type type; |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | #define to_clk_pllv1(clk) (container_of(clk, struct clk_pllv1, clk)) |
| 32 | |
Shawn Guo | 3bec5f8 | 2015-04-26 13:33:39 +0800 | [diff] [blame] | 33 | static inline bool is_imx1_pllv1(struct clk_pllv1 *pll) |
Alexander Shiyan | a594790 | 2013-11-10 13:34:49 +0400 | [diff] [blame] | 34 | { |
Shawn Guo | 3bec5f8 | 2015-04-26 13:33:39 +0800 | [diff] [blame] | 35 | return pll->type == IMX_PLLV1_IMX1; |
| 36 | } |
| 37 | |
| 38 | static inline bool is_imx21_pllv1(struct clk_pllv1 *pll) |
| 39 | { |
| 40 | return pll->type == IMX_PLLV1_IMX21; |
| 41 | } |
| 42 | |
| 43 | static inline bool is_imx27_pllv1(struct clk_pllv1 *pll) |
| 44 | { |
| 45 | return pll->type == IMX_PLLV1_IMX27; |
| 46 | } |
| 47 | |
| 48 | static inline bool mfn_is_negative(struct clk_pllv1 *pll, unsigned int mfn) |
| 49 | { |
| 50 | return !is_imx1_pllv1(pll) && !is_imx21_pllv1(pll) && (mfn & MFN_SIGN); |
Alexander Shiyan | a594790 | 2013-11-10 13:34:49 +0400 | [diff] [blame] | 51 | } |
| 52 | |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 53 | static unsigned long clk_pllv1_recalc_rate(struct clk_hw *hw, |
| 54 | unsigned long parent_rate) |
| 55 | { |
| 56 | struct clk_pllv1 *pll = to_clk_pllv1(hw); |
Nicolas Pitre | 741e96e | 2015-11-03 19:46:23 -0500 | [diff] [blame] | 57 | unsigned long long ull; |
Sascha Hauer | a6dd3c8 | 2012-09-11 08:40:38 +0200 | [diff] [blame] | 58 | int mfn_abs; |
| 59 | unsigned int mfi, mfn, mfd, pd; |
| 60 | u32 reg; |
| 61 | unsigned long rate; |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 62 | |
Sascha Hauer | a6dd3c8 | 2012-09-11 08:40:38 +0200 | [diff] [blame] | 63 | reg = readl(pll->base); |
| 64 | |
| 65 | /* |
| 66 | * Get the resulting clock rate from a PLL register value and the input |
| 67 | * frequency. PLLs with this register layout can be found on i.MX1, |
| 68 | * i.MX21, i.MX27 and i,MX31 |
| 69 | * |
| 70 | * mfi + mfn / (mfd + 1) |
| 71 | * f = 2 * f_ref * -------------------- |
| 72 | * pd + 1 |
| 73 | */ |
| 74 | |
| 75 | mfi = (reg >> 10) & 0xf; |
| 76 | mfn = reg & 0x3ff; |
| 77 | mfd = (reg >> 16) & 0x3ff; |
| 78 | pd = (reg >> 26) & 0xf; |
| 79 | |
| 80 | mfi = mfi <= 5 ? 5 : mfi; |
| 81 | |
| 82 | mfn_abs = mfn; |
| 83 | |
| 84 | /* |
| 85 | * On all i.MXs except i.MX1 and i.MX21 mfn is a 10bit |
Alexander Shiyan | a594790 | 2013-11-10 13:34:49 +0400 | [diff] [blame] | 86 | * 2's complements number. |
| 87 | * On i.MX27 the bit 9 is the sign bit. |
Sascha Hauer | a6dd3c8 | 2012-09-11 08:40:38 +0200 | [diff] [blame] | 88 | */ |
Shawn Guo | 3bec5f8 | 2015-04-26 13:33:39 +0800 | [diff] [blame] | 89 | if (mfn_is_negative(pll, mfn)) { |
| 90 | if (is_imx27_pllv1(pll)) |
Alexander Shiyan | a594790 | 2013-11-10 13:34:49 +0400 | [diff] [blame] | 91 | mfn_abs = mfn & MFN_MASK; |
| 92 | else |
| 93 | mfn_abs = BIT(MFN_BITS) - mfn; |
| 94 | } |
Sascha Hauer | a6dd3c8 | 2012-09-11 08:40:38 +0200 | [diff] [blame] | 95 | |
| 96 | rate = parent_rate * 2; |
| 97 | rate /= pd + 1; |
| 98 | |
Nicolas Pitre | 741e96e | 2015-11-03 19:46:23 -0500 | [diff] [blame] | 99 | ull = (unsigned long long)rate * mfn_abs; |
Sascha Hauer | a6dd3c8 | 2012-09-11 08:40:38 +0200 | [diff] [blame] | 100 | |
Nicolas Pitre | 741e96e | 2015-11-03 19:46:23 -0500 | [diff] [blame] | 101 | do_div(ull, mfd + 1); |
Sascha Hauer | a6dd3c8 | 2012-09-11 08:40:38 +0200 | [diff] [blame] | 102 | |
Shawn Guo | 3bec5f8 | 2015-04-26 13:33:39 +0800 | [diff] [blame] | 103 | if (mfn_is_negative(pll, mfn)) |
Nicolas Pitre | 741e96e | 2015-11-03 19:46:23 -0500 | [diff] [blame] | 104 | ull = (rate * mfi) - ull; |
| 105 | else |
| 106 | ull = (rate * mfi) + ull; |
Sascha Hauer | a6dd3c8 | 2012-09-11 08:40:38 +0200 | [diff] [blame] | 107 | |
Nicolas Pitre | 741e96e | 2015-11-03 19:46:23 -0500 | [diff] [blame] | 108 | return ull; |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Bhumika Goyal | fa1da98 | 2017-08-22 18:48:29 +0530 | [diff] [blame] | 111 | static const struct clk_ops clk_pllv1_ops = { |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 112 | .recalc_rate = clk_pllv1_recalc_rate, |
| 113 | }; |
| 114 | |
Abel Vesa | 556f788 | 2019-12-11 11:25:43 +0200 | [diff] [blame] | 115 | struct clk_hw *imx_clk_hw_pllv1(enum imx_pllv1_type type, const char *name, |
Shawn Guo | 3bec5f8 | 2015-04-26 13:33:39 +0800 | [diff] [blame] | 116 | const char *parent, void __iomem *base) |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 117 | { |
| 118 | struct clk_pllv1 *pll; |
Abel Vesa | 556f788 | 2019-12-11 11:25:43 +0200 | [diff] [blame] | 119 | struct clk_hw *hw; |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 120 | struct clk_init_data init; |
Abel Vesa | 556f788 | 2019-12-11 11:25:43 +0200 | [diff] [blame] | 121 | int ret; |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 122 | |
| 123 | pll = kmalloc(sizeof(*pll), GFP_KERNEL); |
| 124 | if (!pll) |
| 125 | return ERR_PTR(-ENOMEM); |
| 126 | |
| 127 | pll->base = base; |
Shawn Guo | 3bec5f8 | 2015-04-26 13:33:39 +0800 | [diff] [blame] | 128 | pll->type = type; |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 129 | |
| 130 | init.name = name; |
| 131 | init.ops = &clk_pllv1_ops; |
| 132 | init.flags = 0; |
| 133 | init.parent_names = &parent; |
| 134 | init.num_parents = 1; |
| 135 | |
| 136 | pll->hw.init = &init; |
Abel Vesa | 556f788 | 2019-12-11 11:25:43 +0200 | [diff] [blame] | 137 | hw = &pll->hw; |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 138 | |
Abel Vesa | 556f788 | 2019-12-11 11:25:43 +0200 | [diff] [blame] | 139 | ret = clk_hw_register(NULL, hw); |
| 140 | if (ret) { |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 141 | kfree(pll); |
Abel Vesa | 556f788 | 2019-12-11 11:25:43 +0200 | [diff] [blame] | 142 | return ERR_PTR(ret); |
| 143 | } |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 144 | |
Abel Vesa | 556f788 | 2019-12-11 11:25:43 +0200 | [diff] [blame] | 145 | return hw; |
Sascha Hauer | 2af9e6d | 2012-03-09 09:11:55 +0100 | [diff] [blame] | 146 | } |