blob: 36ffb05257350860815b334274783fa72e27ebb0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Anson Huang7d6b5e4f2020-08-05 07:17:29 +08002#include <linux/bits.h>
Sascha Hauer2af9e6d2012-03-09 09:11:55 +01003#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 Hauer3a84d172012-09-11 08:50:00 +02008
Sascha Hauer2af9e6d2012-03-09 09:11:55 +01009#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 Shiyana5947902013-11-10 13:34:49 +040020
21#define MFN_BITS (10)
22#define MFN_SIGN (BIT(MFN_BITS - 1))
23#define MFN_MASK (MFN_SIGN - 1)
24
Sascha Hauer2af9e6d2012-03-09 09:11:55 +010025struct clk_pllv1 {
26 struct clk_hw hw;
27 void __iomem *base;
Shawn Guo3bec5f82015-04-26 13:33:39 +080028 enum imx_pllv1_type type;
Sascha Hauer2af9e6d2012-03-09 09:11:55 +010029};
30
31#define to_clk_pllv1(clk) (container_of(clk, struct clk_pllv1, clk))
32
Shawn Guo3bec5f82015-04-26 13:33:39 +080033static inline bool is_imx1_pllv1(struct clk_pllv1 *pll)
Alexander Shiyana5947902013-11-10 13:34:49 +040034{
Shawn Guo3bec5f82015-04-26 13:33:39 +080035 return pll->type == IMX_PLLV1_IMX1;
36}
37
38static inline bool is_imx21_pllv1(struct clk_pllv1 *pll)
39{
40 return pll->type == IMX_PLLV1_IMX21;
41}
42
43static inline bool is_imx27_pllv1(struct clk_pllv1 *pll)
44{
45 return pll->type == IMX_PLLV1_IMX27;
46}
47
48static 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 Shiyana5947902013-11-10 13:34:49 +040051}
52
Sascha Hauer2af9e6d2012-03-09 09:11:55 +010053static 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 Pitre741e96e2015-11-03 19:46:23 -050057 unsigned long long ull;
Sascha Hauera6dd3c82012-09-11 08:40:38 +020058 int mfn_abs;
59 unsigned int mfi, mfn, mfd, pd;
60 u32 reg;
61 unsigned long rate;
Sascha Hauer2af9e6d2012-03-09 09:11:55 +010062
Sascha Hauera6dd3c82012-09-11 08:40:38 +020063 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 Shiyana5947902013-11-10 13:34:49 +040086 * 2's complements number.
87 * On i.MX27 the bit 9 is the sign bit.
Sascha Hauera6dd3c82012-09-11 08:40:38 +020088 */
Shawn Guo3bec5f82015-04-26 13:33:39 +080089 if (mfn_is_negative(pll, mfn)) {
90 if (is_imx27_pllv1(pll))
Alexander Shiyana5947902013-11-10 13:34:49 +040091 mfn_abs = mfn & MFN_MASK;
92 else
93 mfn_abs = BIT(MFN_BITS) - mfn;
94 }
Sascha Hauera6dd3c82012-09-11 08:40:38 +020095
96 rate = parent_rate * 2;
97 rate /= pd + 1;
98
Nicolas Pitre741e96e2015-11-03 19:46:23 -050099 ull = (unsigned long long)rate * mfn_abs;
Sascha Hauera6dd3c82012-09-11 08:40:38 +0200100
Nicolas Pitre741e96e2015-11-03 19:46:23 -0500101 do_div(ull, mfd + 1);
Sascha Hauera6dd3c82012-09-11 08:40:38 +0200102
Shawn Guo3bec5f82015-04-26 13:33:39 +0800103 if (mfn_is_negative(pll, mfn))
Nicolas Pitre741e96e2015-11-03 19:46:23 -0500104 ull = (rate * mfi) - ull;
105 else
106 ull = (rate * mfi) + ull;
Sascha Hauera6dd3c82012-09-11 08:40:38 +0200107
Nicolas Pitre741e96e2015-11-03 19:46:23 -0500108 return ull;
Sascha Hauer2af9e6d2012-03-09 09:11:55 +0100109}
110
Bhumika Goyalfa1da982017-08-22 18:48:29 +0530111static const struct clk_ops clk_pllv1_ops = {
Sascha Hauer2af9e6d2012-03-09 09:11:55 +0100112 .recalc_rate = clk_pllv1_recalc_rate,
113};
114
Abel Vesa556f7882019-12-11 11:25:43 +0200115struct clk_hw *imx_clk_hw_pllv1(enum imx_pllv1_type type, const char *name,
Shawn Guo3bec5f82015-04-26 13:33:39 +0800116 const char *parent, void __iomem *base)
Sascha Hauer2af9e6d2012-03-09 09:11:55 +0100117{
118 struct clk_pllv1 *pll;
Abel Vesa556f7882019-12-11 11:25:43 +0200119 struct clk_hw *hw;
Sascha Hauer2af9e6d2012-03-09 09:11:55 +0100120 struct clk_init_data init;
Abel Vesa556f7882019-12-11 11:25:43 +0200121 int ret;
Sascha Hauer2af9e6d2012-03-09 09:11:55 +0100122
123 pll = kmalloc(sizeof(*pll), GFP_KERNEL);
124 if (!pll)
125 return ERR_PTR(-ENOMEM);
126
127 pll->base = base;
Shawn Guo3bec5f82015-04-26 13:33:39 +0800128 pll->type = type;
Sascha Hauer2af9e6d2012-03-09 09:11:55 +0100129
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 Vesa556f7882019-12-11 11:25:43 +0200137 hw = &pll->hw;
Sascha Hauer2af9e6d2012-03-09 09:11:55 +0100138
Abel Vesa556f7882019-12-11 11:25:43 +0200139 ret = clk_hw_register(NULL, hw);
140 if (ret) {
Sascha Hauer2af9e6d2012-03-09 09:11:55 +0100141 kfree(pll);
Abel Vesa556f7882019-12-11 11:25:43 +0200142 return ERR_PTR(ret);
143 }
Sascha Hauer2af9e6d2012-03-09 09:11:55 +0100144
Abel Vesa556f7882019-12-11 11:25:43 +0200145 return hw;
Sascha Hauer2af9e6d2012-03-09 09:11:55 +0100146}