1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21241ef94SPadmavathi Venna /*
31241ef94SPadmavathi Venna * Copyright (c) 2013 Samsung Electronics Co., Ltd.
41241ef94SPadmavathi Venna * Author: Padmavathi Venna <padma.v@samsung.com>
51241ef94SPadmavathi Venna *
61241ef94SPadmavathi Venna * Common Clock Framework support for Audio Subsystem Clock Controller.
71241ef94SPadmavathi Venna */
81241ef94SPadmavathi Venna
96f1ed07aSStephen Boyd #include <linux/slab.h>
101241ef94SPadmavathi Venna #include <linux/io.h>
116f1ed07aSStephen Boyd #include <linux/clk.h>
121241ef94SPadmavathi Venna #include <linux/clk-provider.h>
13*a96cbb14SRob Herring #include <linux/of.h>
14b37a4224SAndrew Bresticker #include <linux/module.h>
15b37a4224SAndrew Bresticker #include <linux/platform_device.h>
16ae432a9bSMarek Szyprowski #include <linux/pm_runtime.h>
171241ef94SPadmavathi Venna
18602408e3STushar Behera #include <dt-bindings/clock/exynos-audss-clk.h>
191241ef94SPadmavathi Venna
201241ef94SPadmavathi Venna static DEFINE_SPINLOCK(lock);
211241ef94SPadmavathi Venna static void __iomem *reg_base;
225b2c3da1SMarek Szyprowski static struct clk_hw_onecell_data *clk_data;
23f1e9203eSKrzysztof Kozlowski /*
24f1e9203eSKrzysztof Kozlowski * On Exynos5420 this will be a clock which has to be enabled before any
25f1e9203eSKrzysztof Kozlowski * access to audss registers. Typically a child of EPLL.
26f1e9203eSKrzysztof Kozlowski *
27f1e9203eSKrzysztof Kozlowski * On other platforms this will be -ENODEV.
28f1e9203eSKrzysztof Kozlowski */
29f1e9203eSKrzysztof Kozlowski static struct clk *epll;
301241ef94SPadmavathi Venna
311241ef94SPadmavathi Venna #define ASS_CLK_SRC 0x0
321241ef94SPadmavathi Venna #define ASS_CLK_DIV 0x4
331241ef94SPadmavathi Venna #define ASS_CLK_GATE 0x8
341241ef94SPadmavathi Venna
351241ef94SPadmavathi Venna static unsigned long reg_save[][2] = {
361241ef94SPadmavathi Venna { ASS_CLK_SRC, 0 },
371241ef94SPadmavathi Venna { ASS_CLK_DIV, 0 },
381241ef94SPadmavathi Venna { ASS_CLK_GATE, 0 },
391241ef94SPadmavathi Venna };
401241ef94SPadmavathi Venna
exynos_audss_clk_suspend(struct device * dev)41ae432a9bSMarek Szyprowski static int __maybe_unused exynos_audss_clk_suspend(struct device *dev)
421241ef94SPadmavathi Venna {
431241ef94SPadmavathi Venna int i;
441241ef94SPadmavathi Venna
451241ef94SPadmavathi Venna for (i = 0; i < ARRAY_SIZE(reg_save); i++)
461241ef94SPadmavathi Venna reg_save[i][1] = readl(reg_base + reg_save[i][0]);
471241ef94SPadmavathi Venna
481241ef94SPadmavathi Venna return 0;
491241ef94SPadmavathi Venna }
501241ef94SPadmavathi Venna
exynos_audss_clk_resume(struct device * dev)51ae432a9bSMarek Szyprowski static int __maybe_unused exynos_audss_clk_resume(struct device *dev)
521241ef94SPadmavathi Venna {
531241ef94SPadmavathi Venna int i;
541241ef94SPadmavathi Venna
551241ef94SPadmavathi Venna for (i = 0; i < ARRAY_SIZE(reg_save); i++)
561241ef94SPadmavathi Venna writel(reg_save[i][1], reg_base + reg_save[i][0]);
571241ef94SPadmavathi Venna
58a5b16dfaSMarek Szyprowski return 0;
59a5b16dfaSMarek Szyprowski }
601241ef94SPadmavathi Venna
617c3ca061SSylwester Nawrocki struct exynos_audss_clk_drvdata {
627c3ca061SSylwester Nawrocki unsigned int has_adma_clk:1;
632ec865b7SSylwester Nawrocki unsigned int has_mst_clk:1;
647c3ca061SSylwester Nawrocki unsigned int enable_epll:1;
657c3ca061SSylwester Nawrocki unsigned int num_clks;
667c3ca061SSylwester Nawrocki };
677c3ca061SSylwester Nawrocki
687c3ca061SSylwester Nawrocki static const struct exynos_audss_clk_drvdata exynos4210_drvdata = {
697c3ca061SSylwester Nawrocki .num_clks = EXYNOS_AUDSS_MAX_CLKS - 1,
705bb4053bSKrzysztof Kozlowski .enable_epll = 1,
717c3ca061SSylwester Nawrocki };
727c3ca061SSylwester Nawrocki
732ec865b7SSylwester Nawrocki static const struct exynos_audss_clk_drvdata exynos5410_drvdata = {
742ec865b7SSylwester Nawrocki .num_clks = EXYNOS_AUDSS_MAX_CLKS - 1,
752ec865b7SSylwester Nawrocki .has_mst_clk = 1,
762ec865b7SSylwester Nawrocki };
772ec865b7SSylwester Nawrocki
787c3ca061SSylwester Nawrocki static const struct exynos_audss_clk_drvdata exynos5420_drvdata = {
797c3ca061SSylwester Nawrocki .num_clks = EXYNOS_AUDSS_MAX_CLKS,
807c3ca061SSylwester Nawrocki .has_adma_clk = 1,
817c3ca061SSylwester Nawrocki .enable_epll = 1,
827c3ca061SSylwester Nawrocki };
837c3ca061SSylwester Nawrocki
843538a2cfSAndrew Bresticker static const struct of_device_id exynos_audss_clk_of_match[] = {
857c3ca061SSylwester Nawrocki {
867c3ca061SSylwester Nawrocki .compatible = "samsung,exynos4210-audss-clock",
877c3ca061SSylwester Nawrocki .data = &exynos4210_drvdata,
887c3ca061SSylwester Nawrocki }, {
897c3ca061SSylwester Nawrocki .compatible = "samsung,exynos5250-audss-clock",
907c3ca061SSylwester Nawrocki .data = &exynos4210_drvdata,
917c3ca061SSylwester Nawrocki }, {
922ec865b7SSylwester Nawrocki .compatible = "samsung,exynos5410-audss-clock",
932ec865b7SSylwester Nawrocki .data = &exynos5410_drvdata,
942ec865b7SSylwester Nawrocki }, {
957c3ca061SSylwester Nawrocki .compatible = "samsung,exynos5420-audss-clock",
967c3ca061SSylwester Nawrocki .data = &exynos5420_drvdata,
977c3ca061SSylwester Nawrocki },
983538a2cfSAndrew Bresticker { },
993538a2cfSAndrew Bresticker };
10034b89b29SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, exynos_audss_clk_of_match);
1013538a2cfSAndrew Bresticker
exynos_audss_clk_teardown(void)10227c76c43SKrzysztof Kozlowski static void exynos_audss_clk_teardown(void)
10327c76c43SKrzysztof Kozlowski {
10427c76c43SKrzysztof Kozlowski int i;
10527c76c43SKrzysztof Kozlowski
10627c76c43SKrzysztof Kozlowski for (i = EXYNOS_MOUT_AUDSS; i < EXYNOS_DOUT_SRP; i++) {
1075b2c3da1SMarek Szyprowski if (!IS_ERR(clk_data->hws[i]))
1085b2c3da1SMarek Szyprowski clk_hw_unregister_mux(clk_data->hws[i]);
10927c76c43SKrzysztof Kozlowski }
11027c76c43SKrzysztof Kozlowski
11127c76c43SKrzysztof Kozlowski for (; i < EXYNOS_SRP_CLK; i++) {
1125b2c3da1SMarek Szyprowski if (!IS_ERR(clk_data->hws[i]))
1135b2c3da1SMarek Szyprowski clk_hw_unregister_divider(clk_data->hws[i]);
11427c76c43SKrzysztof Kozlowski }
11527c76c43SKrzysztof Kozlowski
1165b2c3da1SMarek Szyprowski for (; i < clk_data->num; i++) {
1175b2c3da1SMarek Szyprowski if (!IS_ERR(clk_data->hws[i]))
1185b2c3da1SMarek Szyprowski clk_hw_unregister_gate(clk_data->hws[i]);
11927c76c43SKrzysztof Kozlowski }
12027c76c43SKrzysztof Kozlowski }
12127c76c43SKrzysztof Kozlowski
1221241ef94SPadmavathi Venna /* register exynos_audss clocks */
exynos_audss_clk_probe(struct platform_device * pdev)123b37a4224SAndrew Bresticker static int exynos_audss_clk_probe(struct platform_device *pdev)
1241241ef94SPadmavathi Venna {
125547f3350SAndrew Bresticker const char *mout_audss_p[] = {"fin_pll", "fout_epll"};
126547f3350SAndrew Bresticker const char *mout_i2s_p[] = {"mout_audss", "cdclk0", "sclk_audio0"};
127547f3350SAndrew Bresticker const char *sclk_pcm_p = "sclk_pcm0";
128547f3350SAndrew Bresticker struct clk *pll_ref, *pll_in, *cdclk, *sclk_audio, *sclk_pcm_in;
1297c3ca061SSylwester Nawrocki const struct exynos_audss_clk_drvdata *variant;
1305b2c3da1SMarek Szyprowski struct clk_hw **clk_table;
131232d7e47SMarek Szyprowski struct device *dev = &pdev->dev;
1327c3ca061SSylwester Nawrocki int i, ret = 0;
1333538a2cfSAndrew Bresticker
1347c3ca061SSylwester Nawrocki variant = of_device_get_match_data(&pdev->dev);
1357c3ca061SSylwester Nawrocki if (!variant)
1363538a2cfSAndrew Bresticker return -EINVAL;
137b37a4224SAndrew Bresticker
138c5c1a0acSCai Huoqing reg_base = devm_platform_ioremap_resource(pdev, 0);
139073f698dSWei Yongjun if (IS_ERR(reg_base))
140b37a4224SAndrew Bresticker return PTR_ERR(reg_base);
1417c3ca061SSylwester Nawrocki
142f1e9203eSKrzysztof Kozlowski epll = ERR_PTR(-ENODEV);
1431241ef94SPadmavathi Venna
144232d7e47SMarek Szyprowski clk_data = devm_kzalloc(dev,
1450ed2dd03SKees Cook struct_size(clk_data, hws,
1460ed2dd03SKees Cook EXYNOS_AUDSS_MAX_CLKS),
1471241ef94SPadmavathi Venna GFP_KERNEL);
1485b2c3da1SMarek Szyprowski if (!clk_data)
149b37a4224SAndrew Bresticker return -ENOMEM;
1501241ef94SPadmavathi Venna
1515b2c3da1SMarek Szyprowski clk_data->num = variant->num_clks;
1525b2c3da1SMarek Szyprowski clk_table = clk_data->hws;
1531241ef94SPadmavathi Venna
154232d7e47SMarek Szyprowski pll_ref = devm_clk_get(dev, "pll_ref");
155232d7e47SMarek Szyprowski pll_in = devm_clk_get(dev, "pll_in");
156547f3350SAndrew Bresticker if (!IS_ERR(pll_ref))
157547f3350SAndrew Bresticker mout_audss_p[0] = __clk_get_name(pll_ref);
158f1e9203eSKrzysztof Kozlowski if (!IS_ERR(pll_in)) {
159547f3350SAndrew Bresticker mout_audss_p[1] = __clk_get_name(pll_in);
160f1e9203eSKrzysztof Kozlowski
1617c3ca061SSylwester Nawrocki if (variant->enable_epll) {
162f1e9203eSKrzysztof Kozlowski epll = pll_in;
163f1e9203eSKrzysztof Kozlowski
164f1e9203eSKrzysztof Kozlowski ret = clk_prepare_enable(epll);
165f1e9203eSKrzysztof Kozlowski if (ret) {
166232d7e47SMarek Szyprowski dev_err(dev,
167f1e9203eSKrzysztof Kozlowski "failed to prepare the epll clock\n");
168f1e9203eSKrzysztof Kozlowski return ret;
169f1e9203eSKrzysztof Kozlowski }
170f1e9203eSKrzysztof Kozlowski }
171f1e9203eSKrzysztof Kozlowski }
172ae432a9bSMarek Szyprowski
173ae432a9bSMarek Szyprowski /*
174ae432a9bSMarek Szyprowski * Enable runtime PM here to allow the clock core using runtime PM
175ae432a9bSMarek Szyprowski * for the registered clocks. Additionally, we increase the runtime
176ae432a9bSMarek Szyprowski * PM usage count before registering the clocks, to prevent the
177ae432a9bSMarek Szyprowski * clock core from runtime suspending the device.
178ae432a9bSMarek Szyprowski */
179ae432a9bSMarek Szyprowski pm_runtime_get_noresume(dev);
180ae432a9bSMarek Szyprowski pm_runtime_set_active(dev);
181ae432a9bSMarek Szyprowski pm_runtime_enable(dev);
182ae432a9bSMarek Szyprowski
183ae432a9bSMarek Szyprowski clk_table[EXYNOS_MOUT_AUDSS] = clk_hw_register_mux(dev, "mout_audss",
184819c1de3SJames Hogan mout_audss_p, ARRAY_SIZE(mout_audss_p),
1857df45a53SSylwester Nawrocki CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT,
1861241ef94SPadmavathi Venna reg_base + ASS_CLK_SRC, 0, 1, 0, &lock);
1871241ef94SPadmavathi Venna
188232d7e47SMarek Szyprowski cdclk = devm_clk_get(dev, "cdclk");
189232d7e47SMarek Szyprowski sclk_audio = devm_clk_get(dev, "sclk_audio");
190547f3350SAndrew Bresticker if (!IS_ERR(cdclk))
191547f3350SAndrew Bresticker mout_i2s_p[1] = __clk_get_name(cdclk);
192547f3350SAndrew Bresticker if (!IS_ERR(sclk_audio))
193547f3350SAndrew Bresticker mout_i2s_p[2] = __clk_get_name(sclk_audio);
194ae432a9bSMarek Szyprowski clk_table[EXYNOS_MOUT_I2S] = clk_hw_register_mux(dev, "mout_i2s",
195819c1de3SJames Hogan mout_i2s_p, ARRAY_SIZE(mout_i2s_p),
196819c1de3SJames Hogan CLK_SET_RATE_NO_REPARENT,
1971241ef94SPadmavathi Venna reg_base + ASS_CLK_SRC, 2, 2, 0, &lock);
1981241ef94SPadmavathi Venna
199ae432a9bSMarek Szyprowski clk_table[EXYNOS_DOUT_SRP] = clk_hw_register_divider(dev, "dout_srp",
2007df45a53SSylwester Nawrocki "mout_audss", CLK_SET_RATE_PARENT,
2017df45a53SSylwester Nawrocki reg_base + ASS_CLK_DIV, 0, 4, 0, &lock);
2021241ef94SPadmavathi Venna
203ae432a9bSMarek Szyprowski clk_table[EXYNOS_DOUT_AUD_BUS] = clk_hw_register_divider(dev,
2047df45a53SSylwester Nawrocki "dout_aud_bus", "dout_srp", CLK_SET_RATE_PARENT,
2051241ef94SPadmavathi Venna reg_base + ASS_CLK_DIV, 4, 4, 0, &lock);
2061241ef94SPadmavathi Venna
207ae432a9bSMarek Szyprowski clk_table[EXYNOS_DOUT_I2S] = clk_hw_register_divider(dev, "dout_i2s",
2081241ef94SPadmavathi Venna "mout_i2s", 0, reg_base + ASS_CLK_DIV, 8, 4, 0,
2091241ef94SPadmavathi Venna &lock);
2101241ef94SPadmavathi Venna
211ae432a9bSMarek Szyprowski clk_table[EXYNOS_SRP_CLK] = clk_hw_register_gate(dev, "srp_clk",
2121241ef94SPadmavathi Venna "dout_srp", CLK_SET_RATE_PARENT,
2131241ef94SPadmavathi Venna reg_base + ASS_CLK_GATE, 0, 0, &lock);
2141241ef94SPadmavathi Venna
215ae432a9bSMarek Szyprowski clk_table[EXYNOS_I2S_BUS] = clk_hw_register_gate(dev, "i2s_bus",
2161241ef94SPadmavathi Venna "dout_aud_bus", CLK_SET_RATE_PARENT,
2171241ef94SPadmavathi Venna reg_base + ASS_CLK_GATE, 2, 0, &lock);
2181241ef94SPadmavathi Venna
219ae432a9bSMarek Szyprowski clk_table[EXYNOS_SCLK_I2S] = clk_hw_register_gate(dev, "sclk_i2s",
2201241ef94SPadmavathi Venna "dout_i2s", CLK_SET_RATE_PARENT,
2211241ef94SPadmavathi Venna reg_base + ASS_CLK_GATE, 3, 0, &lock);
2221241ef94SPadmavathi Venna
223ae432a9bSMarek Szyprowski clk_table[EXYNOS_PCM_BUS] = clk_hw_register_gate(dev, "pcm_bus",
2241241ef94SPadmavathi Venna "sclk_pcm", CLK_SET_RATE_PARENT,
2251241ef94SPadmavathi Venna reg_base + ASS_CLK_GATE, 4, 0, &lock);
2261241ef94SPadmavathi Venna
227232d7e47SMarek Szyprowski sclk_pcm_in = devm_clk_get(dev, "sclk_pcm_in");
228547f3350SAndrew Bresticker if (!IS_ERR(sclk_pcm_in))
229547f3350SAndrew Bresticker sclk_pcm_p = __clk_get_name(sclk_pcm_in);
230ae432a9bSMarek Szyprowski clk_table[EXYNOS_SCLK_PCM] = clk_hw_register_gate(dev, "sclk_pcm",
231547f3350SAndrew Bresticker sclk_pcm_p, CLK_SET_RATE_PARENT,
2321241ef94SPadmavathi Venna reg_base + ASS_CLK_GATE, 5, 0, &lock);
2331241ef94SPadmavathi Venna
2347c3ca061SSylwester Nawrocki if (variant->has_adma_clk) {
235ae432a9bSMarek Szyprowski clk_table[EXYNOS_ADMA] = clk_hw_register_gate(dev, "adma",
2363538a2cfSAndrew Bresticker "dout_srp", CLK_SET_RATE_PARENT,
2373538a2cfSAndrew Bresticker reg_base + ASS_CLK_GATE, 9, 0, &lock);
2383538a2cfSAndrew Bresticker }
2393538a2cfSAndrew Bresticker
2405b2c3da1SMarek Szyprowski for (i = 0; i < clk_data->num; i++) {
241b37a4224SAndrew Bresticker if (IS_ERR(clk_table[i])) {
242232d7e47SMarek Szyprowski dev_err(dev, "failed to register clock %d\n", i);
243b37a4224SAndrew Bresticker ret = PTR_ERR(clk_table[i]);
244b37a4224SAndrew Bresticker goto unregister;
245b37a4224SAndrew Bresticker }
246b37a4224SAndrew Bresticker }
247b37a4224SAndrew Bresticker
248232d7e47SMarek Szyprowski ret = of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
2495b2c3da1SMarek Szyprowski clk_data);
250b37a4224SAndrew Bresticker if (ret) {
251232d7e47SMarek Szyprowski dev_err(dev, "failed to add clock provider\n");
252b37a4224SAndrew Bresticker goto unregister;
253b37a4224SAndrew Bresticker }
254b37a4224SAndrew Bresticker
255ae432a9bSMarek Szyprowski pm_runtime_put_sync(dev);
256ae432a9bSMarek Szyprowski
257b37a4224SAndrew Bresticker return 0;
258b37a4224SAndrew Bresticker
259b37a4224SAndrew Bresticker unregister:
26027c76c43SKrzysztof Kozlowski exynos_audss_clk_teardown();
261ae432a9bSMarek Szyprowski pm_runtime_put_sync(dev);
262ae432a9bSMarek Szyprowski pm_runtime_disable(dev);
263b37a4224SAndrew Bresticker
264f1e9203eSKrzysztof Kozlowski if (!IS_ERR(epll))
265f1e9203eSKrzysztof Kozlowski clk_disable_unprepare(epll);
266f1e9203eSKrzysztof Kozlowski
267b37a4224SAndrew Bresticker return ret;
268b37a4224SAndrew Bresticker }
269b37a4224SAndrew Bresticker
exynos_audss_clk_remove(struct platform_device * pdev)270e853fb18SUwe Kleine-König static void exynos_audss_clk_remove(struct platform_device *pdev)
271b37a4224SAndrew Bresticker {
272b37a4224SAndrew Bresticker of_clk_del_provider(pdev->dev.of_node);
273b37a4224SAndrew Bresticker
27427c76c43SKrzysztof Kozlowski exynos_audss_clk_teardown();
275ae432a9bSMarek Szyprowski pm_runtime_disable(&pdev->dev);
276b37a4224SAndrew Bresticker
277f1e9203eSKrzysztof Kozlowski if (!IS_ERR(epll))
278f1e9203eSKrzysztof Kozlowski clk_disable_unprepare(epll);
279b37a4224SAndrew Bresticker }
280b37a4224SAndrew Bresticker
281a5b16dfaSMarek Szyprowski static const struct dev_pm_ops exynos_audss_clk_pm_ops = {
282ae432a9bSMarek Szyprowski SET_RUNTIME_PM_OPS(exynos_audss_clk_suspend, exynos_audss_clk_resume,
283ae432a9bSMarek Szyprowski NULL)
284ae432a9bSMarek Szyprowski SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
285ae432a9bSMarek Szyprowski pm_runtime_force_resume)
286a5b16dfaSMarek Szyprowski };
287a5b16dfaSMarek Szyprowski
288b37a4224SAndrew Bresticker static struct platform_driver exynos_audss_clk_driver = {
289b37a4224SAndrew Bresticker .driver = {
290b37a4224SAndrew Bresticker .name = "exynos-audss-clk",
291b37a4224SAndrew Bresticker .of_match_table = exynos_audss_clk_of_match,
292a5b16dfaSMarek Szyprowski .pm = &exynos_audss_clk_pm_ops,
293b37a4224SAndrew Bresticker },
294b37a4224SAndrew Bresticker .probe = exynos_audss_clk_probe,
295e853fb18SUwe Kleine-König .remove_new = exynos_audss_clk_remove,
296b37a4224SAndrew Bresticker };
297b37a4224SAndrew Bresticker
2984d252fd5SSylwester Nawrocki module_platform_driver(exynos_audss_clk_driver);
299b37a4224SAndrew Bresticker
300b37a4224SAndrew Bresticker MODULE_AUTHOR("Padmavathi Venna <padma.v@samsung.com>");
301b37a4224SAndrew Bresticker MODULE_DESCRIPTION("Exynos Audio Subsystem Clock Controller");
302b37a4224SAndrew Bresticker MODULE_LICENSE("GPL v2");
303b37a4224SAndrew Bresticker MODULE_ALIAS("platform:exynos-audss-clk");
304