1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 MediaTek Inc.
4 * Author: James Liao <jamesjj.liao@mediatek.com>
8 #include <linux/of_address.h>
10 #include <linux/slab.h>
11 #include <linux/clkdev.h>
12 #include <linux/delay.h>
19 #define CON0_BASE_EN BIT(0)
20 #define CON0_PWR_ON BIT(0)
21 #define CON0_ISO_EN BIT(1)
22 #define PCW_CHG_MASK BIT(31)
24 #define AUDPLL_TUNER_EN BIT(31)
26 #define POSTDIV_MASK 0x7
28 /* default 7 bits integer, can be overridden with pcwibits. */
29 #define INTEGER_BITS 7
32 * MediaTek PLLs are configured through their pcw value. The pcw value describes
33 * a divider in the PLL feedback loop which consists of 7 bits for the integer
34 * part and the remaining bits (if present) for the fractional part. Also they
35 * have a 3 bit power-of-two post divider.
40 void __iomem
*base_addr
;
41 void __iomem
*pd_addr
;
42 void __iomem
*pwr_addr
;
43 void __iomem
*tuner_addr
;
44 void __iomem
*tuner_en_addr
;
45 void __iomem
*pcw_addr
;
46 void __iomem
*pcw_chg_addr
;
47 const struct mtk_pll_data
*data
;
50 static inline struct mtk_clk_pll
*to_mtk_clk_pll(struct clk_hw
*hw
)
52 return container_of(hw
, struct mtk_clk_pll
, hw
);
55 static int mtk_pll_is_prepared(struct clk_hw
*hw
)
57 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
59 return (readl(pll
->base_addr
+ REG_CON0
) & CON0_BASE_EN
) != 0;
62 static unsigned long __mtk_pll_recalc_rate(struct mtk_clk_pll
*pll
, u32 fin
,
65 int pcwbits
= pll
->data
->pcwbits
;
71 /* The fractional part of the PLL divider. */
72 ibits
= pll
->data
->pcwibits
? pll
->data
->pcwibits
: INTEGER_BITS
;
74 pcwfbits
= pcwbits
- ibits
;
78 if (pcwfbits
&& (vco
& GENMASK(pcwfbits
- 1, 0)))
86 return ((unsigned long)vco
+ postdiv
- 1) / postdiv
;
89 static void __mtk_pll_tuner_enable(struct mtk_clk_pll
*pll
)
93 if (pll
->tuner_en_addr
) {
94 r
= readl(pll
->tuner_en_addr
) | BIT(pll
->data
->tuner_en_bit
);
95 writel(r
, pll
->tuner_en_addr
);
96 } else if (pll
->tuner_addr
) {
97 r
= readl(pll
->tuner_addr
) | AUDPLL_TUNER_EN
;
98 writel(r
, pll
->tuner_addr
);
102 static void __mtk_pll_tuner_disable(struct mtk_clk_pll
*pll
)
106 if (pll
->tuner_en_addr
) {
107 r
= readl(pll
->tuner_en_addr
) & ~BIT(pll
->data
->tuner_en_bit
);
108 writel(r
, pll
->tuner_en_addr
);
109 } else if (pll
->tuner_addr
) {
110 r
= readl(pll
->tuner_addr
) & ~AUDPLL_TUNER_EN
;
111 writel(r
, pll
->tuner_addr
);
115 static void mtk_pll_set_rate_regs(struct mtk_clk_pll
*pll
, u32 pcw
,
121 __mtk_pll_tuner_disable(pll
);
124 val
= readl(pll
->pd_addr
);
125 val
&= ~(POSTDIV_MASK
<< pll
->data
->pd_shift
);
126 val
|= (ffs(postdiv
) - 1) << pll
->data
->pd_shift
;
128 /* postdiv and pcw need to set at the same time if on same register */
129 if (pll
->pd_addr
!= pll
->pcw_addr
) {
130 writel(val
, pll
->pd_addr
);
131 val
= readl(pll
->pcw_addr
);
135 val
&= ~GENMASK(pll
->data
->pcw_shift
+ pll
->data
->pcwbits
- 1,
136 pll
->data
->pcw_shift
);
137 val
|= pcw
<< pll
->data
->pcw_shift
;
138 writel(val
, pll
->pcw_addr
);
139 chg
= readl(pll
->pcw_chg_addr
) | PCW_CHG_MASK
;
140 writel(chg
, pll
->pcw_chg_addr
);
142 writel(val
+ 1, pll
->tuner_addr
);
144 /* restore tuner_en */
145 __mtk_pll_tuner_enable(pll
);
151 * mtk_pll_calc_values - calculate good values for a given input frequency.
153 * @pcw: The pcw value (output)
154 * @postdiv: The post divider (output)
155 * @freq: The desired target frequency
156 * @fin: The input frequency
159 static void mtk_pll_calc_values(struct mtk_clk_pll
*pll
, u32
*pcw
, u32
*postdiv
,
162 unsigned long fmin
= pll
->data
->fmin
? pll
->data
->fmin
: (1000 * MHZ
);
163 const struct mtk_pll_div_table
*div_table
= pll
->data
->div_table
;
168 if (freq
> pll
->data
->fmax
)
169 freq
= pll
->data
->fmax
;
172 if (freq
> div_table
[0].freq
)
173 freq
= div_table
[0].freq
;
175 for (val
= 0; div_table
[val
+ 1].freq
!= 0; val
++) {
176 if (freq
> div_table
[val
+ 1].freq
)
181 for (val
= 0; val
< 5; val
++) {
183 if ((u64
)freq
* *postdiv
>= fmin
)
188 /* _pcw = freq * postdiv / fin * 2^pcwfbits */
189 ibits
= pll
->data
->pcwibits
? pll
->data
->pcwibits
: INTEGER_BITS
;
190 _pcw
= ((u64
)freq
<< val
) << (pll
->data
->pcwbits
- ibits
);
196 static int mtk_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
197 unsigned long parent_rate
)
199 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
203 mtk_pll_calc_values(pll
, &pcw
, &postdiv
, rate
, parent_rate
);
204 mtk_pll_set_rate_regs(pll
, pcw
, postdiv
);
209 static unsigned long mtk_pll_recalc_rate(struct clk_hw
*hw
,
210 unsigned long parent_rate
)
212 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
216 postdiv
= (readl(pll
->pd_addr
) >> pll
->data
->pd_shift
) & POSTDIV_MASK
;
217 postdiv
= 1 << postdiv
;
219 pcw
= readl(pll
->pcw_addr
) >> pll
->data
->pcw_shift
;
220 pcw
&= GENMASK(pll
->data
->pcwbits
- 1, 0);
222 return __mtk_pll_recalc_rate(pll
, parent_rate
, pcw
, postdiv
);
225 static long mtk_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
226 unsigned long *prate
)
228 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
232 mtk_pll_calc_values(pll
, &pcw
, &postdiv
, rate
, *prate
);
234 return __mtk_pll_recalc_rate(pll
, *prate
, pcw
, postdiv
);
237 static int mtk_pll_prepare(struct clk_hw
*hw
)
239 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
242 r
= readl(pll
->pwr_addr
) | CON0_PWR_ON
;
243 writel(r
, pll
->pwr_addr
);
246 r
= readl(pll
->pwr_addr
) & ~CON0_ISO_EN
;
247 writel(r
, pll
->pwr_addr
);
250 r
= readl(pll
->base_addr
+ REG_CON0
);
251 r
|= pll
->data
->en_mask
;
252 writel(r
, pll
->base_addr
+ REG_CON0
);
254 __mtk_pll_tuner_enable(pll
);
258 if (pll
->data
->flags
& HAVE_RST_BAR
) {
259 r
= readl(pll
->base_addr
+ REG_CON0
);
260 r
|= pll
->data
->rst_bar_mask
;
261 writel(r
, pll
->base_addr
+ REG_CON0
);
267 static void mtk_pll_unprepare(struct clk_hw
*hw
)
269 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
272 if (pll
->data
->flags
& HAVE_RST_BAR
) {
273 r
= readl(pll
->base_addr
+ REG_CON0
);
274 r
&= ~pll
->data
->rst_bar_mask
;
275 writel(r
, pll
->base_addr
+ REG_CON0
);
278 __mtk_pll_tuner_disable(pll
);
280 r
= readl(pll
->base_addr
+ REG_CON0
);
282 writel(r
, pll
->base_addr
+ REG_CON0
);
284 r
= readl(pll
->pwr_addr
) | CON0_ISO_EN
;
285 writel(r
, pll
->pwr_addr
);
287 r
= readl(pll
->pwr_addr
) & ~CON0_PWR_ON
;
288 writel(r
, pll
->pwr_addr
);
291 static const struct clk_ops mtk_pll_ops
= {
292 .is_prepared
= mtk_pll_is_prepared
,
293 .prepare
= mtk_pll_prepare
,
294 .unprepare
= mtk_pll_unprepare
,
295 .recalc_rate
= mtk_pll_recalc_rate
,
296 .round_rate
= mtk_pll_round_rate
,
297 .set_rate
= mtk_pll_set_rate
,
300 static struct clk
*mtk_clk_register_pll(const struct mtk_pll_data
*data
,
303 struct mtk_clk_pll
*pll
;
304 struct clk_init_data init
= {};
306 const char *parent_name
= "clk26m";
308 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
310 return ERR_PTR(-ENOMEM
);
312 pll
->base_addr
= base
+ data
->reg
;
313 pll
->pwr_addr
= base
+ data
->pwr_reg
;
314 pll
->pd_addr
= base
+ data
->pd_reg
;
315 pll
->pcw_addr
= base
+ data
->pcw_reg
;
316 if (data
->pcw_chg_reg
)
317 pll
->pcw_chg_addr
= base
+ data
->pcw_chg_reg
;
319 pll
->pcw_chg_addr
= pll
->base_addr
+ REG_CON1
;
321 pll
->tuner_addr
= base
+ data
->tuner_reg
;
322 if (data
->tuner_en_reg
)
323 pll
->tuner_en_addr
= base
+ data
->tuner_en_reg
;
324 pll
->hw
.init
= &init
;
327 init
.name
= data
->name
;
328 init
.flags
= (data
->flags
& PLL_AO
) ? CLK_IS_CRITICAL
: 0;
329 init
.ops
= &mtk_pll_ops
;
330 if (data
->parent_name
)
331 init
.parent_names
= &data
->parent_name
;
333 init
.parent_names
= &parent_name
;
334 init
.num_parents
= 1;
336 clk
= clk_register(NULL
, &pll
->hw
);
344 void mtk_clk_register_plls(struct device_node
*node
,
345 const struct mtk_pll_data
*plls
, int num_plls
, struct clk_onecell_data
*clk_data
)
351 base
= of_iomap(node
, 0);
353 pr_err("%s(): ioremap failed\n", __func__
);
357 for (i
= 0; i
< num_plls
; i
++) {
358 const struct mtk_pll_data
*pll
= &plls
[i
];
360 clk
= mtk_clk_register_pll(pll
, base
);
363 pr_err("Failed to register clk %s: %ld\n",
364 pll
->name
, PTR_ERR(clk
));
368 clk_data
->clks
[pll
->id
] = clk
;