1*d2912cb1SThomas 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 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 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 */ 639978f28fSTomasz Figa static int s5pv210_audss_clk_probe(struct platform_device *pdev) 649978f28fSTomasz Figa { 659978f28fSTomasz Figa int i, ret = 0; 669978f28fSTomasz Figa struct resource *res; 679978f28fSTomasz Figa const char *mout_audss_p[2]; 689978f28fSTomasz Figa const char *mout_i2s_p[3]; 699978f28fSTomasz Figa const char *hclk_p; 70ce3bb8f5SMarek Szyprowski struct clk_hw **clk_table; 719978f28fSTomasz Figa struct clk *hclk, *pll_ref, *pll_in, *cdclk, *sclk_audio; 729978f28fSTomasz Figa 739978f28fSTomasz Figa res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 749978f28fSTomasz Figa reg_base = devm_ioremap_resource(&pdev->dev, res); 759978f28fSTomasz Figa if (IS_ERR(reg_base)) { 769978f28fSTomasz Figa dev_err(&pdev->dev, "failed to map audss registers\n"); 779978f28fSTomasz Figa return PTR_ERR(reg_base); 789978f28fSTomasz Figa } 799978f28fSTomasz Figa 80ce3bb8f5SMarek Szyprowski clk_data = devm_kzalloc(&pdev->dev, 810ed2dd03SKees Cook struct_size(clk_data, hws, AUDSS_MAX_CLKS), 829978f28fSTomasz Figa GFP_KERNEL); 83ce3bb8f5SMarek Szyprowski 84ce3bb8f5SMarek Szyprowski if (!clk_data) 859978f28fSTomasz Figa return -ENOMEM; 869978f28fSTomasz Figa 87ce3bb8f5SMarek Szyprowski clk_data->num = AUDSS_MAX_CLKS; 88ce3bb8f5SMarek Szyprowski clk_table = clk_data->hws; 899978f28fSTomasz Figa 909978f28fSTomasz Figa hclk = devm_clk_get(&pdev->dev, "hclk"); 919978f28fSTomasz Figa if (IS_ERR(hclk)) { 929978f28fSTomasz Figa dev_err(&pdev->dev, "failed to get hclk clock\n"); 939978f28fSTomasz Figa return PTR_ERR(hclk); 949978f28fSTomasz Figa } 959978f28fSTomasz Figa 969978f28fSTomasz Figa pll_in = devm_clk_get(&pdev->dev, "fout_epll"); 979978f28fSTomasz Figa if (IS_ERR(pll_in)) { 989978f28fSTomasz Figa dev_err(&pdev->dev, "failed to get fout_epll clock\n"); 999978f28fSTomasz Figa return PTR_ERR(pll_in); 1009978f28fSTomasz Figa } 1019978f28fSTomasz Figa 1029978f28fSTomasz Figa sclk_audio = devm_clk_get(&pdev->dev, "sclk_audio0"); 1039978f28fSTomasz Figa if (IS_ERR(sclk_audio)) { 1049978f28fSTomasz Figa dev_err(&pdev->dev, "failed to get sclk_audio0 clock\n"); 1059978f28fSTomasz Figa return PTR_ERR(sclk_audio); 1069978f28fSTomasz Figa } 1079978f28fSTomasz Figa 1089978f28fSTomasz Figa /* iiscdclk0 is an optional external I2S codec clock */ 1099978f28fSTomasz Figa cdclk = devm_clk_get(&pdev->dev, "iiscdclk0"); 1109978f28fSTomasz Figa pll_ref = devm_clk_get(&pdev->dev, "xxti"); 1119978f28fSTomasz Figa 1129978f28fSTomasz Figa if (!IS_ERR(pll_ref)) 1139978f28fSTomasz Figa mout_audss_p[0] = __clk_get_name(pll_ref); 1149978f28fSTomasz Figa else 1159978f28fSTomasz Figa mout_audss_p[0] = "xxti"; 1169978f28fSTomasz Figa mout_audss_p[1] = __clk_get_name(pll_in); 117ce3bb8f5SMarek Szyprowski clk_table[CLK_MOUT_AUDSS] = clk_hw_register_mux(NULL, "mout_audss", 1189978f28fSTomasz Figa mout_audss_p, ARRAY_SIZE(mout_audss_p), 1199978f28fSTomasz Figa CLK_SET_RATE_NO_REPARENT, 1209978f28fSTomasz Figa reg_base + ASS_CLK_SRC, 0, 1, 0, &lock); 1219978f28fSTomasz Figa 1229978f28fSTomasz Figa mout_i2s_p[0] = "mout_audss"; 1239978f28fSTomasz Figa if (!IS_ERR(cdclk)) 1249978f28fSTomasz Figa mout_i2s_p[1] = __clk_get_name(cdclk); 1259978f28fSTomasz Figa else 1269978f28fSTomasz Figa mout_i2s_p[1] = "iiscdclk0"; 1279978f28fSTomasz Figa mout_i2s_p[2] = __clk_get_name(sclk_audio); 128ce3bb8f5SMarek Szyprowski clk_table[CLK_MOUT_I2S_A] = clk_hw_register_mux(NULL, "mout_i2s_audss", 1299978f28fSTomasz Figa mout_i2s_p, ARRAY_SIZE(mout_i2s_p), 1309978f28fSTomasz Figa CLK_SET_RATE_NO_REPARENT, 1319978f28fSTomasz Figa reg_base + ASS_CLK_SRC, 2, 2, 0, &lock); 1329978f28fSTomasz Figa 133ce3bb8f5SMarek Szyprowski clk_table[CLK_DOUT_AUD_BUS] = clk_hw_register_divider(NULL, 1349978f28fSTomasz Figa "dout_aud_bus", "mout_audss", 0, 1359978f28fSTomasz Figa reg_base + ASS_CLK_DIV, 0, 4, 0, &lock); 136ce3bb8f5SMarek Szyprowski clk_table[CLK_DOUT_I2S_A] = clk_hw_register_divider(NULL, 137ce3bb8f5SMarek Szyprowski "dout_i2s_audss", "mout_i2s_audss", 0, 138ce3bb8f5SMarek Szyprowski reg_base + ASS_CLK_DIV, 4, 4, 0, &lock); 1399978f28fSTomasz Figa 140ce3bb8f5SMarek Szyprowski clk_table[CLK_I2S] = clk_hw_register_gate(NULL, "i2s_audss", 1419978f28fSTomasz Figa "dout_i2s_audss", CLK_SET_RATE_PARENT, 1429978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 6, 0, &lock); 1439978f28fSTomasz Figa 1449978f28fSTomasz Figa hclk_p = __clk_get_name(hclk); 1459978f28fSTomasz Figa 146ce3bb8f5SMarek Szyprowski clk_table[CLK_HCLK_I2S] = clk_hw_register_gate(NULL, "hclk_i2s_audss", 1479978f28fSTomasz Figa hclk_p, CLK_IGNORE_UNUSED, 1489978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 5, 0, &lock); 149ce3bb8f5SMarek Szyprowski clk_table[CLK_HCLK_UART] = clk_hw_register_gate(NULL, "hclk_uart_audss", 1509978f28fSTomasz Figa hclk_p, CLK_IGNORE_UNUSED, 1519978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 4, 0, &lock); 152ce3bb8f5SMarek Szyprowski clk_table[CLK_HCLK_HWA] = clk_hw_register_gate(NULL, "hclk_hwa_audss", 1539978f28fSTomasz Figa hclk_p, CLK_IGNORE_UNUSED, 1549978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 3, 0, &lock); 155ce3bb8f5SMarek Szyprowski clk_table[CLK_HCLK_DMA] = clk_hw_register_gate(NULL, "hclk_dma_audss", 1569978f28fSTomasz Figa hclk_p, CLK_IGNORE_UNUSED, 1579978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 2, 0, &lock); 158ce3bb8f5SMarek Szyprowski clk_table[CLK_HCLK_BUF] = clk_hw_register_gate(NULL, "hclk_buf_audss", 1599978f28fSTomasz Figa hclk_p, CLK_IGNORE_UNUSED, 1609978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 1, 0, &lock); 161ce3bb8f5SMarek Szyprowski clk_table[CLK_HCLK_RP] = clk_hw_register_gate(NULL, "hclk_rp_audss", 1629978f28fSTomasz Figa hclk_p, CLK_IGNORE_UNUSED, 1639978f28fSTomasz Figa reg_base + ASS_CLK_GATE, 0, 0, &lock); 1649978f28fSTomasz Figa 165ce3bb8f5SMarek Szyprowski for (i = 0; i < clk_data->num; i++) { 1669978f28fSTomasz Figa if (IS_ERR(clk_table[i])) { 1679978f28fSTomasz Figa dev_err(&pdev->dev, "failed to register clock %d\n", i); 1689978f28fSTomasz Figa ret = PTR_ERR(clk_table[i]); 1699978f28fSTomasz Figa goto unregister; 1709978f28fSTomasz Figa } 1719978f28fSTomasz Figa } 1729978f28fSTomasz Figa 173ce3bb8f5SMarek Szyprowski ret = of_clk_add_hw_provider(pdev->dev.of_node, of_clk_hw_onecell_get, 174ce3bb8f5SMarek Szyprowski clk_data); 1759978f28fSTomasz Figa if (ret) { 1769978f28fSTomasz Figa dev_err(&pdev->dev, "failed to add clock provider\n"); 1779978f28fSTomasz Figa goto unregister; 1789978f28fSTomasz Figa } 1799978f28fSTomasz Figa 1809978f28fSTomasz Figa #ifdef CONFIG_PM_SLEEP 1819978f28fSTomasz Figa register_syscore_ops(&s5pv210_audss_clk_syscore_ops); 1829978f28fSTomasz Figa #endif 1839978f28fSTomasz Figa 1849978f28fSTomasz Figa return 0; 1859978f28fSTomasz Figa 1869978f28fSTomasz Figa unregister: 187ce3bb8f5SMarek Szyprowski for (i = 0; i < clk_data->num; i++) { 1889978f28fSTomasz Figa if (!IS_ERR(clk_table[i])) 189ce3bb8f5SMarek Szyprowski clk_hw_unregister(clk_table[i]); 1909978f28fSTomasz Figa } 1919978f28fSTomasz Figa 1929978f28fSTomasz Figa return ret; 1939978f28fSTomasz Figa } 1949978f28fSTomasz Figa 1959978f28fSTomasz Figa static const struct of_device_id s5pv210_audss_clk_of_match[] = { 1969978f28fSTomasz Figa { .compatible = "samsung,s5pv210-audss-clock", }, 1979978f28fSTomasz Figa {}, 1989978f28fSTomasz Figa }; 1999978f28fSTomasz Figa 2009978f28fSTomasz Figa static struct platform_driver s5pv210_audss_clk_driver = { 2019978f28fSTomasz Figa .driver = { 2029978f28fSTomasz Figa .name = "s5pv210-audss-clk", 2034c7c28ffSPaul Gortmaker .suppress_bind_attrs = true, 2049978f28fSTomasz Figa .of_match_table = s5pv210_audss_clk_of_match, 2059978f28fSTomasz Figa }, 2069978f28fSTomasz Figa .probe = s5pv210_audss_clk_probe, 2079978f28fSTomasz Figa }; 2089978f28fSTomasz Figa 2099978f28fSTomasz Figa static int __init s5pv210_audss_clk_init(void) 2109978f28fSTomasz Figa { 2119978f28fSTomasz Figa return platform_driver_register(&s5pv210_audss_clk_driver); 2129978f28fSTomasz Figa } 2139978f28fSTomasz Figa core_initcall(s5pv210_audss_clk_init); 214