1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29978f28fSTomasz Figa /*
39978f28fSTomasz Figa * Copyright (c) 2014 Tomasz Figa <t.figa@samsung.com>
49978f28fSTomasz Figa *
59978f28fSTomasz Figa * Based on Exynos Audio Subsystem Clock Controller driver:
69978f28fSTomasz Figa *
79978f28fSTomasz Figa * Copyright (c) 2013 Samsung Electronics Co., Ltd.
89978f28fSTomasz Figa * Author: Padmavathi Venna <padma.v@samsung.com>
99978f28fSTomasz Figa *
109978f28fSTomasz Figa * Driver for Audio Subsystem Clock Controller of S5PV210-compatible SoCs.
119978f28fSTomasz Figa */
129978f28fSTomasz Figa
139978f28fSTomasz Figa #include <linux/io.h>
146f1ed07aSStephen Boyd #include <linux/clk.h>
159978f28fSTomasz Figa #include <linux/clk-provider.h>
169978f28fSTomasz Figa #include <linux/of_address.h>
179978f28fSTomasz Figa #include <linux/syscore_ops.h>
184c7c28ffSPaul Gortmaker #include <linux/init.h>
199978f28fSTomasz Figa #include <linux/platform_device.h>
209978f28fSTomasz Figa
219978f28fSTomasz Figa #include <dt-bindings/clock/s5pv210-audss.h>
229978f28fSTomasz Figa
239978f28fSTomasz Figa static DEFINE_SPINLOCK(lock);
249978f28fSTomasz Figa static void __iomem *reg_base;
25ce3bb8f5SMarek Szyprowski static struct clk_hw_onecell_data *clk_data;
269978f28fSTomasz Figa
279978f28fSTomasz Figa #define ASS_CLK_SRC 0x0
289978f28fSTomasz Figa #define ASS_CLK_DIV 0x4
299978f28fSTomasz Figa #define ASS_CLK_GATE 0x8
309978f28fSTomasz Figa
319978f28fSTomasz Figa #ifdef CONFIG_PM_SLEEP
329978f28fSTomasz Figa static unsigned long reg_save[][2] = {
339978f28fSTomasz Figa {ASS_CLK_SRC, 0},
349978f28fSTomasz Figa {ASS_CLK_DIV, 0},
359978f28fSTomasz Figa {ASS_CLK_GATE, 0},
369978f28fSTomasz Figa };
379978f28fSTomasz Figa
s5pv210_audss_clk_suspend(void)389978f28fSTomasz Figa static int s5pv210_audss_clk_suspend(void)
399978f28fSTomasz Figa {
409978f28fSTomasz Figa int i;
419978f28fSTomasz Figa
429978f28fSTomasz Figa for (i = 0; i < ARRAY_SIZE(reg_save); i++)
439978f28fSTomasz Figa reg_save[i][1] = readl(reg_base + reg_save[i][0]);
449978f28fSTomasz Figa
459978f28fSTomasz Figa return 0;
469978f28fSTomasz Figa }
479978f28fSTomasz Figa
s5pv210_audss_clk_resume(void)489978f28fSTomasz Figa static void s5pv210_audss_clk_resume(void)
499978f28fSTomasz Figa {
509978f28fSTomasz Figa int i;
519978f28fSTomasz Figa
529978f28fSTomasz Figa for (i = 0; i < ARRAY_SIZE(reg_save); i++)
539978f28fSTomasz Figa writel(reg_save[i][1], reg_base + reg_save[i][0]);
549978f28fSTomasz Figa }
559978f28fSTomasz Figa
569978f28fSTomasz Figa static struct syscore_ops s5pv210_audss_clk_syscore_ops = {
579978f28fSTomasz Figa .suspend = s5pv210_audss_clk_suspend,
589978f28fSTomasz Figa .resume = s5pv210_audss_clk_resume,
599978f28fSTomasz Figa };
609978f28fSTomasz Figa #endif /* CONFIG_PM_SLEEP */
619978f28fSTomasz Figa
629978f28fSTomasz Figa /* register s5pv210_audss clocks */
s5pv210_audss_clk_probe(struct platform_device * pdev)639978f28fSTomasz Figa static int s5pv210_audss_clk_probe(struct platform_device *pdev)
649978f28fSTomasz Figa {
659978f28fSTomasz Figa int i, ret = 0;
669978f28fSTomasz Figa const char *mout_audss_p[2];
679978f28fSTomasz Figa const char *mout_i2s_p[3];
689978f28fSTomasz Figa const char *hclk_p;
69ce3bb8f5SMarek Szyprowski struct clk_hw **clk_table;
709978f28fSTomasz Figa struct clk *hclk, *pll_ref, *pll_in, *cdclk, *sclk_audio;
719978f28fSTomasz Figa
72*1d26eaeeSCai Huoqing reg_base = devm_platform_ioremap_resource(pdev, 0);
737f329176SChen Hui if (IS_ERR(reg_base))
749978f28fSTomasz Figa return PTR_ERR(reg_base);
759978f28fSTomasz Figa
76ce3bb8f5SMarek Szyprowski clk_data = devm_kzalloc(&pdev->dev,
770ed2dd03SKees Cook struct_size(clk_data, hws, AUDSS_MAX_CLKS),
789978f28fSTomasz Figa GFP_KERNEL);
79ce3bb8f5SMarek Szyprowski
80ce3bb8f5SMarek Szyprowski if (!clk_data)
819978f28fSTomasz Figa return -ENOMEM;
829978f28fSTomasz Figa
83ce3bb8f5SMarek Szyprowski clk_data->num = AUDSS_MAX_CLKS;
84ce3bb8f5SMarek Szyprowski clk_table = clk_data->hws;
859978f28fSTomasz Figa
869978f28fSTomasz Figa hclk = devm_clk_get(&pdev->dev, "hclk");
879978f28fSTomasz Figa if (IS_ERR(hclk)) {
889978f28fSTomasz Figa dev_err(&pdev->dev, "failed to get hclk clock\n");
899978f28fSTomasz Figa return PTR_ERR(hclk);
909978f28fSTomasz Figa }
919978f28fSTomasz Figa
929978f28fSTomasz Figa pll_in = devm_clk_get(&pdev->dev, "fout_epll");
939978f28fSTomasz Figa if (IS_ERR(pll_in)) {
949978f28fSTomasz Figa dev_err(&pdev->dev, "failed to get fout_epll clock\n");
959978f28fSTomasz Figa return PTR_ERR(pll_in);
969978f28fSTomasz Figa }
979978f28fSTomasz Figa
989978f28fSTomasz Figa sclk_audio = devm_clk_get(&pdev->dev, "sclk_audio0");
999978f28fSTomasz Figa if (IS_ERR(sclk_audio)) {
1009978f28fSTomasz Figa dev_err(&pdev->dev, "failed to get sclk_audio0 clock\n");
1019978f28fSTomasz Figa return PTR_ERR(sclk_audio);
1029978f28fSTomasz Figa }
1039978f28fSTomasz Figa
1049978f28fSTomasz Figa /* iiscdclk0 is an optional external I2S codec clock */
1059978f28fSTomasz Figa cdclk = devm_clk_get(&pdev->dev, "iiscdclk0");
1069978f28fSTomasz Figa pll_ref = devm_clk_get(&pdev->dev, "xxti");
1079978f28fSTomasz Figa
1089978f28fSTomasz Figa if (!IS_ERR(pll_ref))
1099978f28fSTomasz Figa mout_audss_p[0] = __clk_get_name(pll_ref);
1109978f28fSTomasz Figa else
1119978f28fSTomasz Figa mout_audss_p[0] = "xxti";
1129978f28fSTomasz Figa mout_audss_p[1] = __clk_get_name(pll_in);
113ce3bb8f5SMarek Szyprowski clk_table[CLK_MOUT_AUDSS] = clk_hw_register_mux(NULL, "mout_audss",
1149978f28fSTomasz Figa mout_audss_p, ARRAY_SIZE(mout_audss_p),
1159978f28fSTomasz Figa CLK_SET_RATE_NO_REPARENT,
1169978f28fSTomasz Figa reg_base + ASS_CLK_SRC, 0, 1, 0, &lock);
1179978f28fSTomasz Figa
1189978f28fSTomasz Figa mout_i2s_p[0] = "mout_audss";
1199978f28fSTomasz Figa if (!IS_ERR(cdclk))
1209978f28fSTomasz Figa mout_i2s_p[1] = __clk_get_name(cdclk);
1219978f28fSTomasz Figa else
1229978f28fSTomasz Figa mout_i2s_p[1] = "iiscdclk0";
1239978f28fSTomasz Figa mout_i2s_p[2] = __clk_get_name(sclk_audio);
124ce3bb8f5SMarek Szyprowski clk_table[CLK_MOUT_I2S_A] = clk_hw_register_mux(NULL, "mout_i2s_audss",
1259978f28fSTomasz Figa mout_i2s_p, ARRAY_SIZE(mout_i2s_p),
1269978f28fSTomasz Figa CLK_SET_RATE_NO_REPARENT,
1279978f28fSTomasz Figa reg_base + ASS_CLK_SRC, 2, 2, 0, &lock);
1289978f28fSTomasz Figa
129ce3bb8f5SMarek Szyprowski clk_table[CLK_DOUT_AUD_BUS] = clk_hw_register_divider(NULL,
1309978f28fSTomasz Figa "dout_aud_bus", "mout_audss", 0,
1319978f28fSTomasz Figa reg_base + ASS_CLK_DIV, 0, 4, 0, &lock);
132ce3bb8f5SMarek Szyprowski clk_table[CLK_DOUT_I2S_A] = clk_hw_register_divider(NULL,
133ce3bb8f5SMarek Szyprowski "dout_i2s_audss", "mout_i2s_audss", 0,
134ce3bb8f5SMarek Szyprowski reg_base + ASS_CLK_DIV, 4, 4, 0, &lock);
1359978f28fSTomasz Figa
136ce3bb8f5SMarek Szyprowski clk_table[CLK_I2S] = clk_hw_register_gate(NULL, "i2s_audss",
1379978f28fSTomasz Figa "dout_i2s_audss", CLK_SET_RATE_PARENT,
1389978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 6, 0, &lock);
1399978f28fSTomasz Figa
1409978f28fSTomasz Figa hclk_p = __clk_get_name(hclk);
1419978f28fSTomasz Figa
142ce3bb8f5SMarek Szyprowski clk_table[CLK_HCLK_I2S] = clk_hw_register_gate(NULL, "hclk_i2s_audss",
1439978f28fSTomasz Figa hclk_p, CLK_IGNORE_UNUSED,
1449978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 5, 0, &lock);
145ce3bb8f5SMarek Szyprowski clk_table[CLK_HCLK_UART] = clk_hw_register_gate(NULL, "hclk_uart_audss",
1469978f28fSTomasz Figa hclk_p, CLK_IGNORE_UNUSED,
1479978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 4, 0, &lock);
148ce3bb8f5SMarek Szyprowski clk_table[CLK_HCLK_HWA] = clk_hw_register_gate(NULL, "hclk_hwa_audss",
1499978f28fSTomasz Figa hclk_p, CLK_IGNORE_UNUSED,
1509978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 3, 0, &lock);
151ce3bb8f5SMarek Szyprowski clk_table[CLK_HCLK_DMA] = clk_hw_register_gate(NULL, "hclk_dma_audss",
1529978f28fSTomasz Figa hclk_p, CLK_IGNORE_UNUSED,
1539978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 2, 0, &lock);
154ce3bb8f5SMarek Szyprowski clk_table[CLK_HCLK_BUF] = clk_hw_register_gate(NULL, "hclk_buf_audss",
1559978f28fSTomasz Figa hclk_p, CLK_IGNORE_UNUSED,
1569978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 1, 0, &lock);
157ce3bb8f5SMarek Szyprowski clk_table[CLK_HCLK_RP] = clk_hw_register_gate(NULL, "hclk_rp_audss",
1589978f28fSTomasz Figa hclk_p, CLK_IGNORE_UNUSED,
1599978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 0, 0, &lock);
1609978f28fSTomasz Figa
161ce3bb8f5SMarek Szyprowski for (i = 0; i < clk_data->num; i++) {
1629978f28fSTomasz Figa if (IS_ERR(clk_table[i])) {
1639978f28fSTomasz Figa dev_err(&pdev->dev, "failed to register clock %d\n", i);
1649978f28fSTomasz Figa ret = PTR_ERR(clk_table[i]);
1659978f28fSTomasz Figa goto unregister;
1669978f28fSTomasz Figa }
1679978f28fSTomasz Figa }
1689978f28fSTomasz Figa
169ce3bb8f5SMarek Szyprowski ret = of_clk_add_hw_provider(pdev->dev.of_node, of_clk_hw_onecell_get,
170ce3bb8f5SMarek Szyprowski clk_data);
1719978f28fSTomasz Figa if (ret) {
1729978f28fSTomasz Figa dev_err(&pdev->dev, "failed to add clock provider\n");
1739978f28fSTomasz Figa goto unregister;
1749978f28fSTomasz Figa }
1759978f28fSTomasz Figa
1769978f28fSTomasz Figa #ifdef CONFIG_PM_SLEEP
1779978f28fSTomasz Figa register_syscore_ops(&s5pv210_audss_clk_syscore_ops);
1789978f28fSTomasz Figa #endif
1799978f28fSTomasz Figa
1809978f28fSTomasz Figa return 0;
1819978f28fSTomasz Figa
1829978f28fSTomasz Figa unregister:
183ce3bb8f5SMarek Szyprowski for (i = 0; i < clk_data->num; i++) {
1849978f28fSTomasz Figa if (!IS_ERR(clk_table[i]))
185ce3bb8f5SMarek Szyprowski clk_hw_unregister(clk_table[i]);
1869978f28fSTomasz Figa }
1879978f28fSTomasz Figa
1889978f28fSTomasz Figa return ret;
1899978f28fSTomasz Figa }
1909978f28fSTomasz Figa
1919978f28fSTomasz Figa static const struct of_device_id s5pv210_audss_clk_of_match[] = {
1929978f28fSTomasz Figa { .compatible = "samsung,s5pv210-audss-clock", },
1939978f28fSTomasz Figa {},
1949978f28fSTomasz Figa };
1959978f28fSTomasz Figa
1969978f28fSTomasz Figa static struct platform_driver s5pv210_audss_clk_driver = {
1979978f28fSTomasz Figa .driver = {
1989978f28fSTomasz Figa .name = "s5pv210-audss-clk",
1994c7c28ffSPaul Gortmaker .suppress_bind_attrs = true,
2009978f28fSTomasz Figa .of_match_table = s5pv210_audss_clk_of_match,
2019978f28fSTomasz Figa },
2029978f28fSTomasz Figa .probe = s5pv210_audss_clk_probe,
2039978f28fSTomasz Figa };
2049978f28fSTomasz Figa
s5pv210_audss_clk_init(void)2059978f28fSTomasz Figa static int __init s5pv210_audss_clk_init(void)
2069978f28fSTomasz Figa {
2079978f28fSTomasz Figa return platform_driver_register(&s5pv210_audss_clk_driver);
2089978f28fSTomasz Figa }
2099978f28fSTomasz Figa core_initcall(s5pv210_audss_clk_init);
210