1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 MediaTek Inc.
4 * Author: James Liao <jamesjj.liao@mediatek.com>
7 #include <linux/clk-provider.h>
8 #include <linux/container_of.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
12 #include <linux/module.h>
13 #include <linux/of_address.h>
14 #include <linux/slab.h>
18 #define MHZ (1000 * 1000)
23 #define CON0_BASE_EN BIT(0)
24 #define CON0_PWR_ON BIT(0)
25 #define CON0_ISO_EN BIT(1)
26 #define PCW_CHG_BIT 31
28 #define AUDPLL_TUNER_EN BIT(31)
30 /* default 7 bits integer, can be overridden with pcwibits. */
31 #define INTEGER_BITS 7
33 int mtk_pll_is_prepared(struct clk_hw
*hw
)
35 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
37 return (readl(pll
->en_addr
) & BIT(pll
->data
->pll_en_bit
)) != 0;
40 static unsigned long __mtk_pll_recalc_rate(struct mtk_clk_pll
*pll
, u32 fin
,
43 int pcwbits
= pll
->data
->pcwbits
;
49 /* The fractional part of the PLL divider. */
50 ibits
= pll
->data
->pcwibits
? pll
->data
->pcwibits
: INTEGER_BITS
;
52 pcwfbits
= pcwbits
- ibits
;
56 if (pcwfbits
&& (vco
& GENMASK(pcwfbits
- 1, 0)))
64 return ((unsigned long)vco
+ postdiv
- 1) / postdiv
;
67 static void __mtk_pll_tuner_enable(struct mtk_clk_pll
*pll
)
71 if (pll
->tuner_en_addr
) {
72 r
= readl(pll
->tuner_en_addr
) | BIT(pll
->data
->tuner_en_bit
);
73 writel(r
, pll
->tuner_en_addr
);
74 } else if (pll
->tuner_addr
) {
75 r
= readl(pll
->tuner_addr
) | AUDPLL_TUNER_EN
;
76 writel(r
, pll
->tuner_addr
);
80 static void __mtk_pll_tuner_disable(struct mtk_clk_pll
*pll
)
84 if (pll
->tuner_en_addr
) {
85 r
= readl(pll
->tuner_en_addr
) & ~BIT(pll
->data
->tuner_en_bit
);
86 writel(r
, pll
->tuner_en_addr
);
87 } else if (pll
->tuner_addr
) {
88 r
= readl(pll
->tuner_addr
) & ~AUDPLL_TUNER_EN
;
89 writel(r
, pll
->tuner_addr
);
93 static void mtk_pll_set_rate_regs(struct mtk_clk_pll
*pll
, u32 pcw
,
99 __mtk_pll_tuner_disable(pll
);
102 val
= readl(pll
->pd_addr
);
103 val
&= ~(POSTDIV_MASK
<< pll
->data
->pd_shift
);
104 val
|= (ffs(postdiv
) - 1) << pll
->data
->pd_shift
;
106 /* postdiv and pcw need to set at the same time if on same register */
107 if (pll
->pd_addr
!= pll
->pcw_addr
) {
108 writel(val
, pll
->pd_addr
);
109 val
= readl(pll
->pcw_addr
);
113 val
&= ~GENMASK(pll
->data
->pcw_shift
+ pll
->data
->pcwbits
- 1,
114 pll
->data
->pcw_shift
);
115 val
|= pcw
<< pll
->data
->pcw_shift
;
116 writel(val
, pll
->pcw_addr
);
117 chg
= readl(pll
->pcw_chg_addr
) |
118 BIT(pll
->data
->pcw_chg_bit
? : PCW_CHG_BIT
);
119 writel(chg
, pll
->pcw_chg_addr
);
121 writel(val
+ 1, pll
->tuner_addr
);
123 /* restore tuner_en */
124 __mtk_pll_tuner_enable(pll
);
130 * mtk_pll_calc_values - calculate good values for a given input frequency.
132 * @pcw: The pcw value (output)
133 * @postdiv: The post divider (output)
134 * @freq: The desired target frequency
135 * @fin: The input frequency
138 void mtk_pll_calc_values(struct mtk_clk_pll
*pll
, u32
*pcw
, u32
*postdiv
,
141 unsigned long fmin
= pll
->data
->fmin
? pll
->data
->fmin
: (1000 * MHZ
);
142 const struct mtk_pll_div_table
*div_table
= pll
->data
->div_table
;
147 if (freq
> pll
->data
->fmax
)
148 freq
= pll
->data
->fmax
;
151 if (freq
> div_table
[0].freq
)
152 freq
= div_table
[0].freq
;
154 for (val
= 0; div_table
[val
+ 1].freq
!= 0; val
++) {
155 if (freq
> div_table
[val
+ 1].freq
)
160 for (val
= 0; val
< 5; val
++) {
162 if ((u64
)freq
* *postdiv
>= fmin
)
167 /* _pcw = freq * postdiv / fin * 2^pcwfbits */
168 ibits
= pll
->data
->pcwibits
? pll
->data
->pcwibits
: INTEGER_BITS
;
169 _pcw
= ((u64
)freq
<< val
) << (pll
->data
->pcwbits
- ibits
);
175 int mtk_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
176 unsigned long parent_rate
)
178 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
182 mtk_pll_calc_values(pll
, &pcw
, &postdiv
, rate
, parent_rate
);
183 mtk_pll_set_rate_regs(pll
, pcw
, postdiv
);
188 unsigned long mtk_pll_recalc_rate(struct clk_hw
*hw
, unsigned long parent_rate
)
190 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
194 postdiv
= (readl(pll
->pd_addr
) >> pll
->data
->pd_shift
) & POSTDIV_MASK
;
195 postdiv
= 1 << postdiv
;
197 pcw
= readl(pll
->pcw_addr
) >> pll
->data
->pcw_shift
;
198 pcw
&= GENMASK(pll
->data
->pcwbits
- 1, 0);
200 return __mtk_pll_recalc_rate(pll
, parent_rate
, pcw
, postdiv
);
203 long mtk_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
204 unsigned long *prate
)
206 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
210 mtk_pll_calc_values(pll
, &pcw
, &postdiv
, rate
, *prate
);
212 return __mtk_pll_recalc_rate(pll
, *prate
, pcw
, postdiv
);
215 int mtk_pll_prepare(struct clk_hw
*hw
)
217 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
220 r
= readl(pll
->pwr_addr
) | CON0_PWR_ON
;
221 writel(r
, pll
->pwr_addr
);
224 r
= readl(pll
->pwr_addr
) & ~CON0_ISO_EN
;
225 writel(r
, pll
->pwr_addr
);
228 r
= readl(pll
->en_addr
) | BIT(pll
->data
->pll_en_bit
);
229 writel(r
, pll
->en_addr
);
231 if (pll
->data
->en_mask
) {
232 r
= readl(pll
->base_addr
+ REG_CON0
) | pll
->data
->en_mask
;
233 writel(r
, pll
->base_addr
+ REG_CON0
);
236 __mtk_pll_tuner_enable(pll
);
240 if (pll
->data
->flags
& HAVE_RST_BAR
) {
241 r
= readl(pll
->base_addr
+ REG_CON0
);
242 r
|= pll
->data
->rst_bar_mask
;
243 writel(r
, pll
->base_addr
+ REG_CON0
);
249 void mtk_pll_unprepare(struct clk_hw
*hw
)
251 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
254 if (pll
->data
->flags
& HAVE_RST_BAR
) {
255 r
= readl(pll
->base_addr
+ REG_CON0
);
256 r
&= ~pll
->data
->rst_bar_mask
;
257 writel(r
, pll
->base_addr
+ REG_CON0
);
260 __mtk_pll_tuner_disable(pll
);
262 if (pll
->data
->en_mask
) {
263 r
= readl(pll
->base_addr
+ REG_CON0
) & ~pll
->data
->en_mask
;
264 writel(r
, pll
->base_addr
+ REG_CON0
);
267 r
= readl(pll
->en_addr
) & ~BIT(pll
->data
->pll_en_bit
);
268 writel(r
, pll
->en_addr
);
270 r
= readl(pll
->pwr_addr
) | CON0_ISO_EN
;
271 writel(r
, pll
->pwr_addr
);
273 r
= readl(pll
->pwr_addr
) & ~CON0_PWR_ON
;
274 writel(r
, pll
->pwr_addr
);
277 const struct clk_ops mtk_pll_ops
= {
278 .is_prepared
= mtk_pll_is_prepared
,
279 .prepare
= mtk_pll_prepare
,
280 .unprepare
= mtk_pll_unprepare
,
281 .recalc_rate
= mtk_pll_recalc_rate
,
282 .round_rate
= mtk_pll_round_rate
,
283 .set_rate
= mtk_pll_set_rate
,
286 struct clk_hw
*mtk_clk_register_pll_ops(struct mtk_clk_pll
*pll
,
287 const struct mtk_pll_data
*data
,
289 const struct clk_ops
*pll_ops
)
291 struct clk_init_data init
= {};
293 const char *parent_name
= "clk26m";
295 pll
->base_addr
= base
+ data
->reg
;
296 pll
->pwr_addr
= base
+ data
->pwr_reg
;
297 pll
->pd_addr
= base
+ data
->pd_reg
;
298 pll
->pcw_addr
= base
+ data
->pcw_reg
;
299 if (data
->pcw_chg_reg
)
300 pll
->pcw_chg_addr
= base
+ data
->pcw_chg_reg
;
302 pll
->pcw_chg_addr
= pll
->base_addr
+ REG_CON1
;
304 pll
->tuner_addr
= base
+ data
->tuner_reg
;
305 if (data
->tuner_en_reg
|| data
->tuner_en_bit
)
306 pll
->tuner_en_addr
= base
+ data
->tuner_en_reg
;
308 pll
->en_addr
= base
+ data
->en_reg
;
310 pll
->en_addr
= pll
->base_addr
+ REG_CON0
;
311 pll
->hw
.init
= &init
;
314 init
.name
= data
->name
;
315 init
.flags
= (data
->flags
& PLL_AO
) ? CLK_IS_CRITICAL
: 0;
317 if (data
->parent_name
)
318 init
.parent_names
= &data
->parent_name
;
320 init
.parent_names
= &parent_name
;
321 init
.num_parents
= 1;
323 ret
= clk_hw_register(NULL
, &pll
->hw
);
331 struct clk_hw
*mtk_clk_register_pll(const struct mtk_pll_data
*data
,
334 struct mtk_clk_pll
*pll
;
337 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
339 return ERR_PTR(-ENOMEM
);
341 hw
= mtk_clk_register_pll_ops(pll
, data
, base
, &mtk_pll_ops
);
348 void mtk_clk_unregister_pll(struct clk_hw
*hw
)
350 struct mtk_clk_pll
*pll
;
355 pll
= to_mtk_clk_pll(hw
);
357 clk_hw_unregister(hw
);
361 int mtk_clk_register_plls(struct device_node
*node
,
362 const struct mtk_pll_data
*plls
, int num_plls
,
363 struct clk_hw_onecell_data
*clk_data
)
369 base
= of_iomap(node
, 0);
371 pr_err("%s(): ioremap failed\n", __func__
);
375 for (i
= 0; i
< num_plls
; i
++) {
376 const struct mtk_pll_data
*pll
= &plls
[i
];
378 if (!IS_ERR_OR_NULL(clk_data
->hws
[pll
->id
])) {
379 pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
384 hw
= mtk_clk_register_pll(pll
, base
);
387 pr_err("Failed to register clk %s: %pe\n", pll
->name
,
392 clk_data
->hws
[pll
->id
] = hw
;
399 const struct mtk_pll_data
*pll
= &plls
[i
];
401 mtk_clk_unregister_pll(clk_data
->hws
[pll
->id
]);
402 clk_data
->hws
[pll
->id
] = ERR_PTR(-ENOENT
);
409 EXPORT_SYMBOL_GPL(mtk_clk_register_plls
);
411 __iomem
void *mtk_clk_pll_get_base(struct clk_hw
*hw
,
412 const struct mtk_pll_data
*data
)
414 struct mtk_clk_pll
*pll
= to_mtk_clk_pll(hw
);
416 return pll
->base_addr
- data
->reg
;
419 void mtk_clk_unregister_plls(const struct mtk_pll_data
*plls
, int num_plls
,
420 struct clk_hw_onecell_data
*clk_data
)
422 __iomem
void *base
= NULL
;
428 for (i
= num_plls
; i
> 0; i
--) {
429 const struct mtk_pll_data
*pll
= &plls
[i
- 1];
431 if (IS_ERR_OR_NULL(clk_data
->hws
[pll
->id
]))
435 * This is quite ugly but unfortunately the clks don't have
436 * any device tied to them, so there's no place to store the
437 * pointer to the I/O region base address. We have to fetch
438 * it from one of the registered clks.
440 base
= mtk_clk_pll_get_base(clk_data
->hws
[pll
->id
], pll
);
442 mtk_clk_unregister_pll(clk_data
->hws
[pll
->id
]);
443 clk_data
->hws
[pll
->id
] = ERR_PTR(-ENOENT
);
448 EXPORT_SYMBOL_GPL(mtk_clk_unregister_plls
);
450 MODULE_LICENSE("GPL");