1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MMP Audio Clock Controller driver
5 * Copyright (C) 2020 Lubomir Rintel <lkundrak@v3.sk>
8 #include <linux/clk-provider.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_clock.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/slab.h>
15 #include <dt-bindings/clock/marvell,mmp2-audio.h>
17 /* Audio Controller Registers */
18 #define SSPA_AUD_CTRL 0x04
19 #define SSPA_AUD_PLL_CTRL0 0x08
20 #define SSPA_AUD_PLL_CTRL1 0x0c
22 /* SSPA Audio Control Register */
23 #define SSPA_AUD_CTRL_SYSCLK_SHIFT 0
24 #define SSPA_AUD_CTRL_SYSCLK_DIV_SHIFT 1
25 #define SSPA_AUD_CTRL_SSPA0_MUX_SHIFT 7
26 #define SSPA_AUD_CTRL_SSPA0_SHIFT 8
27 #define SSPA_AUD_CTRL_SSPA0_DIV_SHIFT 9
28 #define SSPA_AUD_CTRL_SSPA1_SHIFT 16
29 #define SSPA_AUD_CTRL_SSPA1_DIV_SHIFT 17
30 #define SSPA_AUD_CTRL_SSPA1_MUX_SHIFT 23
31 #define SSPA_AUD_CTRL_DIV_MASK 0x7e
33 /* SSPA Audio PLL Control 0 Register */
34 #define SSPA_AUD_PLL_CTRL0_DIV_OCLK_MODULO_MASK (0x7 << 28)
35 #define SSPA_AUD_PLL_CTRL0_DIV_OCLK_MODULO(x) ((x) << 28)
36 #define SSPA_AUD_PLL_CTRL0_FRACT_MASK (0xfffff << 8)
37 #define SSPA_AUD_PLL_CTRL0_FRACT(x) ((x) << 8)
38 #define SSPA_AUD_PLL_CTRL0_ENA_DITHER (1 << 7)
39 #define SSPA_AUD_PLL_CTRL0_ICP_2UA (0 << 5)
40 #define SSPA_AUD_PLL_CTRL0_ICP_5UA (1 << 5)
41 #define SSPA_AUD_PLL_CTRL0_ICP_7UA (2 << 5)
42 #define SSPA_AUD_PLL_CTRL0_ICP_10UA (3 << 5)
43 #define SSPA_AUD_PLL_CTRL0_DIV_FBCCLK_MASK (0x3 << 3)
44 #define SSPA_AUD_PLL_CTRL0_DIV_FBCCLK(x) ((x) << 3)
45 #define SSPA_AUD_PLL_CTRL0_DIV_MCLK_MASK (0x1 << 2)
46 #define SSPA_AUD_PLL_CTRL0_DIV_MCLK(x) ((x) << 2)
47 #define SSPA_AUD_PLL_CTRL0_PD_OVPROT_DIS (1 << 1)
48 #define SSPA_AUD_PLL_CTRL0_PU (1 << 0)
50 /* SSPA Audio PLL Control 1 Register */
51 #define SSPA_AUD_PLL_CTRL1_SEL_FAST_CLK (1 << 24)
52 #define SSPA_AUD_PLL_CTRL1_CLK_SEL_MASK (1 << 11)
53 #define SSPA_AUD_PLL_CTRL1_CLK_SEL_AUDIO_PLL (1 << 11)
54 #define SSPA_AUD_PLL_CTRL1_CLK_SEL_VCXO (0 << 11)
55 #define SSPA_AUD_PLL_CTRL1_DIV_OCLK_PATTERN_MASK (0x7ff << 0)
56 #define SSPA_AUD_PLL_CTRL1_DIV_OCLK_PATTERN(x) ((x) << 0)
58 #define CLK_AUDIO_NR_CLKS 3
60 struct mmp2_audio_clk
{
61 void __iomem
*mmio_base
;
63 struct clk_hw audio_pll_hw
;
64 struct clk_mux sspa_mux
;
65 struct clk_mux sspa1_mux
;
66 struct clk_divider sysclk_div
;
67 struct clk_divider sspa0_div
;
68 struct clk_divider sspa1_div
;
69 struct clk_gate sysclk_gate
;
70 struct clk_gate sspa0_gate
;
71 struct clk_gate sspa1_gate
;
80 struct clk_hw_onecell_data clk_data
;
84 unsigned long parent_rate
;
85 unsigned long freq_vco
;
90 { 26000000, 135475200, 0, 0, 0x8a18 },
91 { 26000000, 147456000, 0, 1, 0x0da1 },
92 { 38400000, 135475200, 1, 2, 0x8208 },
93 { 38400000, 147456000, 1, 3, 0xaaaa },
97 unsigned char divisor
;
99 unsigned char pattern
;
116 static unsigned long audio_pll_recalc_rate(struct clk_hw
*hw
,
117 unsigned long parent_rate
)
119 struct mmp2_audio_clk
*priv
= container_of(hw
, struct mmp2_audio_clk
, audio_pll_hw
);
121 unsigned int postdiv
;
125 aud_pll_ctrl0
= readl(priv
->mmio_base
+ SSPA_AUD_PLL_CTRL0
);
126 aud_pll_ctrl0
&= SSPA_AUD_PLL_CTRL0_DIV_OCLK_MODULO_MASK
|
127 SSPA_AUD_PLL_CTRL0_FRACT_MASK
|
128 SSPA_AUD_PLL_CTRL0_ENA_DITHER
|
129 SSPA_AUD_PLL_CTRL0_DIV_FBCCLK_MASK
|
130 SSPA_AUD_PLL_CTRL0_DIV_MCLK_MASK
|
131 SSPA_AUD_PLL_CTRL0_PU
;
133 aud_pll_ctrl1
= readl(priv
->mmio_base
+ SSPA_AUD_PLL_CTRL1
);
134 aud_pll_ctrl1
&= SSPA_AUD_PLL_CTRL1_CLK_SEL_MASK
|
135 SSPA_AUD_PLL_CTRL1_DIV_OCLK_PATTERN_MASK
;
137 for (prediv
= 0; prediv
< ARRAY_SIZE(predivs
); prediv
++) {
138 if (predivs
[prediv
].parent_rate
!= parent_rate
)
140 for (postdiv
= 0; postdiv
< ARRAY_SIZE(postdivs
); postdiv
++) {
144 val
= SSPA_AUD_PLL_CTRL0_ENA_DITHER
;
145 val
|= SSPA_AUD_PLL_CTRL0_PU
;
146 val
|= SSPA_AUD_PLL_CTRL0_DIV_OCLK_MODULO(postdivs
[postdiv
].modulo
);
147 val
|= SSPA_AUD_PLL_CTRL0_FRACT(predivs
[prediv
].fract
);
148 val
|= SSPA_AUD_PLL_CTRL0_DIV_FBCCLK(predivs
[prediv
].fbcclk
);
149 val
|= SSPA_AUD_PLL_CTRL0_DIV_MCLK(predivs
[prediv
].mclk
);
150 if (val
!= aud_pll_ctrl0
)
153 val
= SSPA_AUD_PLL_CTRL1_CLK_SEL_AUDIO_PLL
;
154 val
|= SSPA_AUD_PLL_CTRL1_DIV_OCLK_PATTERN(postdivs
[postdiv
].pattern
);
155 if (val
!= aud_pll_ctrl1
)
158 freq
= predivs
[prediv
].freq_vco
;
159 freq
/= postdivs
[postdiv
].divisor
;
167 static long audio_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
168 unsigned long *parent_rate
)
171 unsigned int postdiv
;
174 for (prediv
= 0; prediv
< ARRAY_SIZE(predivs
); prediv
++) {
175 if (predivs
[prediv
].parent_rate
!= *parent_rate
)
177 for (postdiv
= 0; postdiv
< ARRAY_SIZE(postdivs
); postdiv
++) {
178 long freq
= predivs
[prediv
].freq_vco
;
180 freq
/= postdivs
[postdiv
].divisor
;
185 if (rounded
&& freq
> rounded
)
194 static int audio_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
195 unsigned long parent_rate
)
197 struct mmp2_audio_clk
*priv
= container_of(hw
, struct mmp2_audio_clk
, audio_pll_hw
);
199 unsigned int postdiv
;
202 for (prediv
= 0; prediv
< ARRAY_SIZE(predivs
); prediv
++) {
203 if (predivs
[prediv
].parent_rate
!= parent_rate
)
206 for (postdiv
= 0; postdiv
< ARRAY_SIZE(postdivs
); postdiv
++) {
207 if (rate
* postdivs
[postdiv
].divisor
!= predivs
[prediv
].freq_vco
)
210 val
= SSPA_AUD_PLL_CTRL0_ENA_DITHER
;
211 val
|= SSPA_AUD_PLL_CTRL0_PU
;
212 val
|= SSPA_AUD_PLL_CTRL0_DIV_OCLK_MODULO(postdivs
[postdiv
].modulo
);
213 val
|= SSPA_AUD_PLL_CTRL0_FRACT(predivs
[prediv
].fract
);
214 val
|= SSPA_AUD_PLL_CTRL0_DIV_FBCCLK(predivs
[prediv
].fbcclk
);
215 val
|= SSPA_AUD_PLL_CTRL0_DIV_MCLK(predivs
[prediv
].mclk
);
216 writel(val
, priv
->mmio_base
+ SSPA_AUD_PLL_CTRL0
);
218 val
= SSPA_AUD_PLL_CTRL1_CLK_SEL_AUDIO_PLL
;
219 val
|= SSPA_AUD_PLL_CTRL1_DIV_OCLK_PATTERN(postdivs
[postdiv
].pattern
);
220 writel(val
, priv
->mmio_base
+ SSPA_AUD_PLL_CTRL1
);
229 static const struct clk_ops audio_pll_ops
= {
230 .recalc_rate
= audio_pll_recalc_rate
,
231 .round_rate
= audio_pll_round_rate
,
232 .set_rate
= audio_pll_set_rate
,
235 static int register_clocks(struct mmp2_audio_clk
*priv
, struct device
*dev
)
237 const struct clk_parent_data sspa_mux_parents
[] = {
238 { .hw
= &priv
->audio_pll_hw
},
239 { .fw_name
= "i2s0" },
241 const struct clk_parent_data sspa1_mux_parents
[] = {
242 { .hw
= &priv
->audio_pll_hw
},
243 { .fw_name
= "i2s1" },
247 priv
->audio_pll_hw
.init
= CLK_HW_INIT_FW_NAME("audio_pll",
248 "vctcxo", &audio_pll_ops
,
249 CLK_SET_RATE_PARENT
);
250 ret
= devm_clk_hw_register(dev
, &priv
->audio_pll_hw
);
254 priv
->sspa_mux
.hw
.init
= CLK_HW_INIT_PARENTS_DATA("sspa_mux",
255 sspa_mux_parents
, &clk_mux_ops
,
256 CLK_SET_RATE_PARENT
);
257 priv
->sspa_mux
.reg
= priv
->mmio_base
+ SSPA_AUD_CTRL
;
258 priv
->sspa_mux
.mask
= 1;
259 priv
->sspa_mux
.shift
= SSPA_AUD_CTRL_SSPA0_MUX_SHIFT
;
260 ret
= devm_clk_hw_register(dev
, &priv
->sspa_mux
.hw
);
264 priv
->sysclk_div
.hw
.init
= CLK_HW_INIT_HW("sys_div",
265 &priv
->sspa_mux
.hw
, &clk_divider_ops
,
266 CLK_SET_RATE_PARENT
);
267 priv
->sysclk_div
.reg
= priv
->mmio_base
+ SSPA_AUD_CTRL
;
268 priv
->sysclk_div
.shift
= SSPA_AUD_CTRL_SYSCLK_DIV_SHIFT
;
269 priv
->sysclk_div
.width
= 6;
270 priv
->sysclk_div
.flags
= CLK_DIVIDER_ONE_BASED
;
271 priv
->sysclk_div
.flags
|= CLK_DIVIDER_ROUND_CLOSEST
;
272 priv
->sysclk_div
.flags
|= CLK_DIVIDER_ALLOW_ZERO
;
273 ret
= devm_clk_hw_register(dev
, &priv
->sysclk_div
.hw
);
277 priv
->sysclk_gate
.hw
.init
= CLK_HW_INIT_HW("sys_clk",
278 &priv
->sysclk_div
.hw
, &clk_gate_ops
,
279 CLK_SET_RATE_PARENT
);
280 priv
->sysclk_gate
.reg
= priv
->mmio_base
+ SSPA_AUD_CTRL
;
281 priv
->sysclk_gate
.bit_idx
= SSPA_AUD_CTRL_SYSCLK_SHIFT
;
282 ret
= devm_clk_hw_register(dev
, &priv
->sysclk_gate
.hw
);
286 priv
->sspa0_div
.hw
.init
= CLK_HW_INIT_HW("sspa0_div",
287 &priv
->sspa_mux
.hw
, &clk_divider_ops
, 0);
288 priv
->sspa0_div
.reg
= priv
->mmio_base
+ SSPA_AUD_CTRL
;
289 priv
->sspa0_div
.shift
= SSPA_AUD_CTRL_SSPA0_DIV_SHIFT
;
290 priv
->sspa0_div
.width
= 6;
291 priv
->sspa0_div
.flags
= CLK_DIVIDER_ONE_BASED
;
292 priv
->sspa0_div
.flags
|= CLK_DIVIDER_ROUND_CLOSEST
;
293 priv
->sspa0_div
.flags
|= CLK_DIVIDER_ALLOW_ZERO
;
294 ret
= devm_clk_hw_register(dev
, &priv
->sspa0_div
.hw
);
298 priv
->sspa0_gate
.hw
.init
= CLK_HW_INIT_HW("sspa0_clk",
299 &priv
->sspa0_div
.hw
, &clk_gate_ops
,
300 CLK_SET_RATE_PARENT
);
301 priv
->sspa0_gate
.reg
= priv
->mmio_base
+ SSPA_AUD_CTRL
;
302 priv
->sspa0_gate
.bit_idx
= SSPA_AUD_CTRL_SSPA0_SHIFT
;
303 ret
= devm_clk_hw_register(dev
, &priv
->sspa0_gate
.hw
);
307 priv
->sspa1_mux
.hw
.init
= CLK_HW_INIT_PARENTS_DATA("sspa1_mux",
308 sspa1_mux_parents
, &clk_mux_ops
,
309 CLK_SET_RATE_PARENT
);
310 priv
->sspa1_mux
.reg
= priv
->mmio_base
+ SSPA_AUD_CTRL
;
311 priv
->sspa1_mux
.mask
= 1;
312 priv
->sspa1_mux
.shift
= SSPA_AUD_CTRL_SSPA1_MUX_SHIFT
;
313 ret
= devm_clk_hw_register(dev
, &priv
->sspa1_mux
.hw
);
317 priv
->sspa1_div
.hw
.init
= CLK_HW_INIT_HW("sspa1_div",
318 &priv
->sspa1_mux
.hw
, &clk_divider_ops
, 0);
319 priv
->sspa1_div
.reg
= priv
->mmio_base
+ SSPA_AUD_CTRL
;
320 priv
->sspa1_div
.shift
= SSPA_AUD_CTRL_SSPA1_DIV_SHIFT
;
321 priv
->sspa1_div
.width
= 6;
322 priv
->sspa1_div
.flags
= CLK_DIVIDER_ONE_BASED
;
323 priv
->sspa1_div
.flags
|= CLK_DIVIDER_ROUND_CLOSEST
;
324 priv
->sspa1_div
.flags
|= CLK_DIVIDER_ALLOW_ZERO
;
325 ret
= devm_clk_hw_register(dev
, &priv
->sspa1_div
.hw
);
329 priv
->sspa1_gate
.hw
.init
= CLK_HW_INIT_HW("sspa1_clk",
330 &priv
->sspa1_div
.hw
, &clk_gate_ops
,
331 CLK_SET_RATE_PARENT
);
332 priv
->sspa1_gate
.reg
= priv
->mmio_base
+ SSPA_AUD_CTRL
;
333 priv
->sspa1_gate
.bit_idx
= SSPA_AUD_CTRL_SSPA1_SHIFT
;
334 ret
= devm_clk_hw_register(dev
, &priv
->sspa1_gate
.hw
);
338 priv
->clk_data
.hws
[MMP2_CLK_AUDIO_SYSCLK
] = &priv
->sysclk_gate
.hw
;
339 priv
->clk_data
.hws
[MMP2_CLK_AUDIO_SSPA0
] = &priv
->sspa0_gate
.hw
;
340 priv
->clk_data
.hws
[MMP2_CLK_AUDIO_SSPA1
] = &priv
->sspa1_gate
.hw
;
341 priv
->clk_data
.num
= CLK_AUDIO_NR_CLKS
;
343 return of_clk_add_hw_provider(dev
->of_node
, of_clk_hw_onecell_get
,
347 static int mmp2_audio_clk_probe(struct platform_device
*pdev
)
349 struct mmp2_audio_clk
*priv
;
352 priv
= devm_kzalloc(&pdev
->dev
,
353 struct_size(priv
, clk_data
.hws
,
359 spin_lock_init(&priv
->lock
);
360 platform_set_drvdata(pdev
, priv
);
362 priv
->mmio_base
= devm_platform_ioremap_resource(pdev
, 0);
363 if (IS_ERR(priv
->mmio_base
))
364 return PTR_ERR(priv
->mmio_base
);
366 pm_runtime_enable(&pdev
->dev
);
367 ret
= pm_clk_create(&pdev
->dev
);
369 goto disable_pm_runtime
;
371 ret
= pm_clk_add(&pdev
->dev
, "audio");
375 ret
= register_clocks(priv
, &pdev
->dev
);
382 pm_clk_destroy(&pdev
->dev
);
384 pm_runtime_disable(&pdev
->dev
);
389 static void mmp2_audio_clk_remove(struct platform_device
*pdev
)
391 pm_clk_destroy(&pdev
->dev
);
392 pm_runtime_disable(&pdev
->dev
);
396 static int mmp2_audio_clk_suspend(struct device
*dev
)
398 struct mmp2_audio_clk
*priv
= dev_get_drvdata(dev
);
400 priv
->aud_ctrl
= readl(priv
->mmio_base
+ SSPA_AUD_CTRL
);
401 priv
->aud_pll_ctrl0
= readl(priv
->mmio_base
+ SSPA_AUD_PLL_CTRL0
);
402 priv
->aud_pll_ctrl1
= readl(priv
->mmio_base
+ SSPA_AUD_PLL_CTRL1
);
408 static int mmp2_audio_clk_resume(struct device
*dev
)
410 struct mmp2_audio_clk
*priv
= dev_get_drvdata(dev
);
413 writel(priv
->aud_ctrl
, priv
->mmio_base
+ SSPA_AUD_CTRL
);
414 writel(priv
->aud_pll_ctrl0
, priv
->mmio_base
+ SSPA_AUD_PLL_CTRL0
);
415 writel(priv
->aud_pll_ctrl1
, priv
->mmio_base
+ SSPA_AUD_PLL_CTRL1
);
421 static const struct dev_pm_ops mmp2_audio_clk_pm_ops
= {
422 SET_RUNTIME_PM_OPS(mmp2_audio_clk_suspend
, mmp2_audio_clk_resume
, NULL
)
425 static const struct of_device_id mmp2_audio_clk_of_match
[] = {
426 { .compatible
= "marvell,mmp2-audio-clock" },
430 MODULE_DEVICE_TABLE(of
, mmp2_audio_clk_of_match
);
432 static struct platform_driver mmp2_audio_clk_driver
= {
434 .name
= "mmp2-audio-clock",
435 .of_match_table
= of_match_ptr(mmp2_audio_clk_of_match
),
436 .pm
= &mmp2_audio_clk_pm_ops
,
438 .probe
= mmp2_audio_clk_probe
,
439 .remove
= mmp2_audio_clk_remove
,
441 module_platform_driver(mmp2_audio_clk_driver
);
443 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
444 MODULE_DESCRIPTION("Clock driver for MMP2 Audio subsystem");
445 MODULE_LICENSE("GPL");