11241ef94SPadmavathi Venna /* 21241ef94SPadmavathi Venna * Copyright (c) 2013 Samsung Electronics Co., Ltd. 31241ef94SPadmavathi Venna * Author: Padmavathi Venna <padma.v@samsung.com> 41241ef94SPadmavathi Venna * 51241ef94SPadmavathi Venna * This program is free software; you can redistribute it and/or modify 61241ef94SPadmavathi Venna * it under the terms of the GNU General Public License version 2 as 71241ef94SPadmavathi Venna * published by the Free Software Foundation. 81241ef94SPadmavathi Venna * 91241ef94SPadmavathi Venna * Common Clock Framework support for Audio Subsystem Clock Controller. 101241ef94SPadmavathi Venna */ 111241ef94SPadmavathi Venna 126f1ed07aSStephen Boyd #include <linux/slab.h> 131241ef94SPadmavathi Venna #include <linux/io.h> 146f1ed07aSStephen Boyd #include <linux/clk.h> 151241ef94SPadmavathi Venna #include <linux/clk-provider.h> 161241ef94SPadmavathi Venna #include <linux/of_address.h> 177c3ca061SSylwester Nawrocki #include <linux/of_device.h> 181241ef94SPadmavathi Venna #include <linux/syscore_ops.h> 19b37a4224SAndrew Bresticker #include <linux/module.h> 20b37a4224SAndrew Bresticker #include <linux/platform_device.h> 211241ef94SPadmavathi Venna 22602408e3STushar Behera #include <dt-bindings/clock/exynos-audss-clk.h> 231241ef94SPadmavathi Venna 241241ef94SPadmavathi Venna static DEFINE_SPINLOCK(lock); 251241ef94SPadmavathi Venna static struct clk **clk_table; 261241ef94SPadmavathi Venna static void __iomem *reg_base; 271241ef94SPadmavathi Venna static struct clk_onecell_data clk_data; 28f1e9203eSKrzysztof Kozlowski /* 29f1e9203eSKrzysztof Kozlowski * On Exynos5420 this will be a clock which has to be enabled before any 30f1e9203eSKrzysztof Kozlowski * access to audss registers. Typically a child of EPLL. 31f1e9203eSKrzysztof Kozlowski * 32f1e9203eSKrzysztof Kozlowski * On other platforms this will be -ENODEV. 33f1e9203eSKrzysztof Kozlowski */ 34f1e9203eSKrzysztof Kozlowski static struct clk *epll; 351241ef94SPadmavathi Venna 361241ef94SPadmavathi Venna #define ASS_CLK_SRC 0x0 371241ef94SPadmavathi Venna #define ASS_CLK_DIV 0x4 381241ef94SPadmavathi Venna #define ASS_CLK_GATE 0x8 391241ef94SPadmavathi Venna 403fd68c99SKrzysztof Kozlowski #ifdef CONFIG_PM_SLEEP 411241ef94SPadmavathi Venna static unsigned long reg_save[][2] = { 421241ef94SPadmavathi Venna {ASS_CLK_SRC, 0}, 431241ef94SPadmavathi Venna {ASS_CLK_DIV, 0}, 441241ef94SPadmavathi Venna {ASS_CLK_GATE, 0}, 451241ef94SPadmavathi Venna }; 461241ef94SPadmavathi Venna 471241ef94SPadmavathi Venna static int exynos_audss_clk_suspend(void) 481241ef94SPadmavathi Venna { 491241ef94SPadmavathi Venna int i; 501241ef94SPadmavathi Venna 511241ef94SPadmavathi Venna for (i = 0; i < ARRAY_SIZE(reg_save); i++) 521241ef94SPadmavathi Venna reg_save[i][1] = readl(reg_base + reg_save[i][0]); 531241ef94SPadmavathi Venna 541241ef94SPadmavathi Venna return 0; 551241ef94SPadmavathi Venna } 561241ef94SPadmavathi Venna 571241ef94SPadmavathi Venna static void exynos_audss_clk_resume(void) 581241ef94SPadmavathi Venna { 591241ef94SPadmavathi Venna int i; 601241ef94SPadmavathi Venna 611241ef94SPadmavathi Venna for (i = 0; i < ARRAY_SIZE(reg_save); i++) 621241ef94SPadmavathi Venna writel(reg_save[i][1], reg_base + reg_save[i][0]); 631241ef94SPadmavathi Venna } 641241ef94SPadmavathi Venna 651241ef94SPadmavathi Venna static struct syscore_ops exynos_audss_clk_syscore_ops = { 661241ef94SPadmavathi Venna .suspend = exynos_audss_clk_suspend, 671241ef94SPadmavathi Venna .resume = exynos_audss_clk_resume, 681241ef94SPadmavathi Venna }; 691241ef94SPadmavathi Venna #endif /* CONFIG_PM_SLEEP */ 701241ef94SPadmavathi Venna 717c3ca061SSylwester Nawrocki struct exynos_audss_clk_drvdata { 727c3ca061SSylwester Nawrocki unsigned int has_adma_clk:1; 73*2ec865b7SSylwester Nawrocki unsigned int has_mst_clk:1; 747c3ca061SSylwester Nawrocki unsigned int enable_epll:1; 757c3ca061SSylwester Nawrocki unsigned int num_clks; 767c3ca061SSylwester Nawrocki }; 777c3ca061SSylwester Nawrocki 787c3ca061SSylwester Nawrocki static const struct exynos_audss_clk_drvdata exynos4210_drvdata = { 797c3ca061SSylwester Nawrocki .num_clks = EXYNOS_AUDSS_MAX_CLKS - 1, 807c3ca061SSylwester Nawrocki }; 817c3ca061SSylwester Nawrocki 82*2ec865b7SSylwester Nawrocki static const struct exynos_audss_clk_drvdata exynos5410_drvdata = { 83*2ec865b7SSylwester Nawrocki .num_clks = EXYNOS_AUDSS_MAX_CLKS - 1, 84*2ec865b7SSylwester Nawrocki .has_mst_clk = 1, 85*2ec865b7SSylwester Nawrocki }; 86*2ec865b7SSylwester Nawrocki 877c3ca061SSylwester Nawrocki static const struct exynos_audss_clk_drvdata exynos5420_drvdata = { 887c3ca061SSylwester Nawrocki .num_clks = EXYNOS_AUDSS_MAX_CLKS, 897c3ca061SSylwester Nawrocki .has_adma_clk = 1, 907c3ca061SSylwester Nawrocki .enable_epll = 1, 917c3ca061SSylwester Nawrocki }; 927c3ca061SSylwester Nawrocki 933538a2cfSAndrew Bresticker static const struct of_device_id exynos_audss_clk_of_match[] = { 947c3ca061SSylwester Nawrocki { 957c3ca061SSylwester Nawrocki .compatible = "samsung,exynos4210-audss-clock", 967c3ca061SSylwester Nawrocki .data = &exynos4210_drvdata, 977c3ca061SSylwester Nawrocki }, { 987c3ca061SSylwester Nawrocki .compatible = "samsung,exynos5250-audss-clock", 997c3ca061SSylwester Nawrocki .data = &exynos4210_drvdata, 1007c3ca061SSylwester Nawrocki }, { 101*2ec865b7SSylwester Nawrocki .compatible = "samsung,exynos5410-audss-clock", 102*2ec865b7SSylwester Nawrocki .data = &exynos5410_drvdata, 103*2ec865b7SSylwester Nawrocki }, { 1047c3ca061SSylwester Nawrocki .compatible = "samsung,exynos5420-audss-clock", 1057c3ca061SSylwester Nawrocki .data = &exynos5420_drvdata, 1067c3ca061SSylwester Nawrocki }, 1073538a2cfSAndrew Bresticker { }, 1083538a2cfSAndrew Bresticker }; 1093538a2cfSAndrew Bresticker 11027c76c43SKrzysztof Kozlowski static void exynos_audss_clk_teardown(void) 11127c76c43SKrzysztof Kozlowski { 11227c76c43SKrzysztof Kozlowski int i; 11327c76c43SKrzysztof Kozlowski 11427c76c43SKrzysztof Kozlowski for (i = EXYNOS_MOUT_AUDSS; i < EXYNOS_DOUT_SRP; i++) { 11527c76c43SKrzysztof Kozlowski if (!IS_ERR(clk_table[i])) 11627c76c43SKrzysztof Kozlowski clk_unregister_mux(clk_table[i]); 11727c76c43SKrzysztof Kozlowski } 11827c76c43SKrzysztof Kozlowski 11927c76c43SKrzysztof Kozlowski for (; i < EXYNOS_SRP_CLK; i++) { 12027c76c43SKrzysztof Kozlowski if (!IS_ERR(clk_table[i])) 12127c76c43SKrzysztof Kozlowski clk_unregister_divider(clk_table[i]); 12227c76c43SKrzysztof Kozlowski } 12327c76c43SKrzysztof Kozlowski 12427c76c43SKrzysztof Kozlowski for (; i < clk_data.clk_num; i++) { 12527c76c43SKrzysztof Kozlowski if (!IS_ERR(clk_table[i])) 12627c76c43SKrzysztof Kozlowski clk_unregister_gate(clk_table[i]); 12727c76c43SKrzysztof Kozlowski } 12827c76c43SKrzysztof Kozlowski } 12927c76c43SKrzysztof Kozlowski 1301241ef94SPadmavathi Venna /* register exynos_audss clocks */ 131b37a4224SAndrew Bresticker static int exynos_audss_clk_probe(struct platform_device *pdev) 1321241ef94SPadmavathi Venna { 133547f3350SAndrew Bresticker const char *mout_audss_p[] = {"fin_pll", "fout_epll"}; 134547f3350SAndrew Bresticker const char *mout_i2s_p[] = {"mout_audss", "cdclk0", "sclk_audio0"}; 135547f3350SAndrew Bresticker const char *sclk_pcm_p = "sclk_pcm0"; 136547f3350SAndrew Bresticker struct clk *pll_ref, *pll_in, *cdclk, *sclk_audio, *sclk_pcm_in; 1377c3ca061SSylwester Nawrocki const struct exynos_audss_clk_drvdata *variant; 1387c3ca061SSylwester Nawrocki struct resource *res; 1397c3ca061SSylwester Nawrocki int i, ret = 0; 1403538a2cfSAndrew Bresticker 1417c3ca061SSylwester Nawrocki variant = of_device_get_match_data(&pdev->dev); 1427c3ca061SSylwester Nawrocki if (!variant) 1433538a2cfSAndrew Bresticker return -EINVAL; 144b37a4224SAndrew Bresticker 145b37a4224SAndrew Bresticker res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 146b37a4224SAndrew Bresticker reg_base = devm_ioremap_resource(&pdev->dev, res); 147b37a4224SAndrew Bresticker if (IS_ERR(reg_base)) { 148b37a4224SAndrew Bresticker dev_err(&pdev->dev, "failed to map audss registers\n"); 149b37a4224SAndrew Bresticker return PTR_ERR(reg_base); 1501241ef94SPadmavathi Venna } 1517c3ca061SSylwester Nawrocki 152f1e9203eSKrzysztof Kozlowski epll = ERR_PTR(-ENODEV); 1531241ef94SPadmavathi Venna 154b37a4224SAndrew Bresticker clk_table = devm_kzalloc(&pdev->dev, 155b37a4224SAndrew Bresticker sizeof(struct clk *) * EXYNOS_AUDSS_MAX_CLKS, 1561241ef94SPadmavathi Venna GFP_KERNEL); 157b37a4224SAndrew Bresticker if (!clk_table) 158b37a4224SAndrew Bresticker return -ENOMEM; 1591241ef94SPadmavathi Venna 1601241ef94SPadmavathi Venna clk_data.clks = clk_table; 1617c3ca061SSylwester Nawrocki clk_data.clk_num = variant->num_clks; 1621241ef94SPadmavathi Venna 163547f3350SAndrew Bresticker pll_ref = devm_clk_get(&pdev->dev, "pll_ref"); 164547f3350SAndrew Bresticker pll_in = devm_clk_get(&pdev->dev, "pll_in"); 165547f3350SAndrew Bresticker if (!IS_ERR(pll_ref)) 166547f3350SAndrew Bresticker mout_audss_p[0] = __clk_get_name(pll_ref); 167f1e9203eSKrzysztof Kozlowski if (!IS_ERR(pll_in)) { 168547f3350SAndrew Bresticker mout_audss_p[1] = __clk_get_name(pll_in); 169f1e9203eSKrzysztof Kozlowski 1707c3ca061SSylwester Nawrocki if (variant->enable_epll) { 171f1e9203eSKrzysztof Kozlowski epll = pll_in; 172f1e9203eSKrzysztof Kozlowski 173f1e9203eSKrzysztof Kozlowski ret = clk_prepare_enable(epll); 174f1e9203eSKrzysztof Kozlowski if (ret) { 175f1e9203eSKrzysztof Kozlowski dev_err(&pdev->dev, 176f1e9203eSKrzysztof Kozlowski "failed to prepare the epll clock\n"); 177f1e9203eSKrzysztof Kozlowski return ret; 178f1e9203eSKrzysztof Kozlowski } 179f1e9203eSKrzysztof Kozlowski } 180f1e9203eSKrzysztof Kozlowski } 1811241ef94SPadmavathi Venna clk_table[EXYNOS_MOUT_AUDSS] = clk_register_mux(NULL, "mout_audss", 182819c1de3SJames Hogan mout_audss_p, ARRAY_SIZE(mout_audss_p), 183819c1de3SJames Hogan CLK_SET_RATE_NO_REPARENT, 1841241ef94SPadmavathi Venna reg_base + ASS_CLK_SRC, 0, 1, 0, &lock); 1851241ef94SPadmavathi Venna 186547f3350SAndrew Bresticker cdclk = devm_clk_get(&pdev->dev, "cdclk"); 187547f3350SAndrew Bresticker sclk_audio = devm_clk_get(&pdev->dev, "sclk_audio"); 188547f3350SAndrew Bresticker if (!IS_ERR(cdclk)) 189547f3350SAndrew Bresticker mout_i2s_p[1] = __clk_get_name(cdclk); 190547f3350SAndrew Bresticker if (!IS_ERR(sclk_audio)) 191547f3350SAndrew Bresticker mout_i2s_p[2] = __clk_get_name(sclk_audio); 1921241ef94SPadmavathi Venna clk_table[EXYNOS_MOUT_I2S] = clk_register_mux(NULL, "mout_i2s", 193819c1de3SJames Hogan mout_i2s_p, ARRAY_SIZE(mout_i2s_p), 194819c1de3SJames Hogan CLK_SET_RATE_NO_REPARENT, 1951241ef94SPadmavathi Venna reg_base + ASS_CLK_SRC, 2, 2, 0, &lock); 1961241ef94SPadmavathi Venna 1971241ef94SPadmavathi Venna clk_table[EXYNOS_DOUT_SRP] = clk_register_divider(NULL, "dout_srp", 1981241ef94SPadmavathi Venna "mout_audss", 0, reg_base + ASS_CLK_DIV, 0, 4, 1991241ef94SPadmavathi Venna 0, &lock); 2001241ef94SPadmavathi Venna 2011241ef94SPadmavathi Venna clk_table[EXYNOS_DOUT_AUD_BUS] = clk_register_divider(NULL, 2021241ef94SPadmavathi Venna "dout_aud_bus", "dout_srp", 0, 2031241ef94SPadmavathi Venna reg_base + ASS_CLK_DIV, 4, 4, 0, &lock); 2041241ef94SPadmavathi Venna 2051241ef94SPadmavathi Venna clk_table[EXYNOS_DOUT_I2S] = clk_register_divider(NULL, "dout_i2s", 2061241ef94SPadmavathi Venna "mout_i2s", 0, reg_base + ASS_CLK_DIV, 8, 4, 0, 2071241ef94SPadmavathi Venna &lock); 2081241ef94SPadmavathi Venna 2091241ef94SPadmavathi Venna clk_table[EXYNOS_SRP_CLK] = clk_register_gate(NULL, "srp_clk", 2101241ef94SPadmavathi Venna "dout_srp", CLK_SET_RATE_PARENT, 2111241ef94SPadmavathi Venna reg_base + ASS_CLK_GATE, 0, 0, &lock); 2121241ef94SPadmavathi Venna 2131241ef94SPadmavathi Venna clk_table[EXYNOS_I2S_BUS] = clk_register_gate(NULL, "i2s_bus", 2141241ef94SPadmavathi Venna "dout_aud_bus", CLK_SET_RATE_PARENT, 2151241ef94SPadmavathi Venna reg_base + ASS_CLK_GATE, 2, 0, &lock); 2161241ef94SPadmavathi Venna 2171241ef94SPadmavathi Venna clk_table[EXYNOS_SCLK_I2S] = clk_register_gate(NULL, "sclk_i2s", 2181241ef94SPadmavathi Venna "dout_i2s", CLK_SET_RATE_PARENT, 2191241ef94SPadmavathi Venna reg_base + ASS_CLK_GATE, 3, 0, &lock); 2201241ef94SPadmavathi Venna 2211241ef94SPadmavathi Venna clk_table[EXYNOS_PCM_BUS] = clk_register_gate(NULL, "pcm_bus", 2221241ef94SPadmavathi Venna "sclk_pcm", CLK_SET_RATE_PARENT, 2231241ef94SPadmavathi Venna reg_base + ASS_CLK_GATE, 4, 0, &lock); 2241241ef94SPadmavathi Venna 225547f3350SAndrew Bresticker sclk_pcm_in = devm_clk_get(&pdev->dev, "sclk_pcm_in"); 226547f3350SAndrew Bresticker if (!IS_ERR(sclk_pcm_in)) 227547f3350SAndrew Bresticker sclk_pcm_p = __clk_get_name(sclk_pcm_in); 2281241ef94SPadmavathi Venna clk_table[EXYNOS_SCLK_PCM] = clk_register_gate(NULL, "sclk_pcm", 229547f3350SAndrew Bresticker sclk_pcm_p, CLK_SET_RATE_PARENT, 2301241ef94SPadmavathi Venna reg_base + ASS_CLK_GATE, 5, 0, &lock); 2311241ef94SPadmavathi Venna 2327c3ca061SSylwester Nawrocki if (variant->has_adma_clk) { 2333538a2cfSAndrew Bresticker clk_table[EXYNOS_ADMA] = clk_register_gate(NULL, "adma", 2343538a2cfSAndrew Bresticker "dout_srp", CLK_SET_RATE_PARENT, 2353538a2cfSAndrew Bresticker reg_base + ASS_CLK_GATE, 9, 0, &lock); 2363538a2cfSAndrew Bresticker } 2373538a2cfSAndrew Bresticker 238b37a4224SAndrew Bresticker for (i = 0; i < clk_data.clk_num; i++) { 239b37a4224SAndrew Bresticker if (IS_ERR(clk_table[i])) { 240b37a4224SAndrew Bresticker dev_err(&pdev->dev, "failed to register clock %d\n", i); 241b37a4224SAndrew Bresticker ret = PTR_ERR(clk_table[i]); 242b37a4224SAndrew Bresticker goto unregister; 243b37a4224SAndrew Bresticker } 244b37a4224SAndrew Bresticker } 245b37a4224SAndrew Bresticker 246b37a4224SAndrew Bresticker ret = of_clk_add_provider(pdev->dev.of_node, of_clk_src_onecell_get, 247b37a4224SAndrew Bresticker &clk_data); 248b37a4224SAndrew Bresticker if (ret) { 249b37a4224SAndrew Bresticker dev_err(&pdev->dev, "failed to add clock provider\n"); 250b37a4224SAndrew Bresticker goto unregister; 251b37a4224SAndrew Bresticker } 252b37a4224SAndrew Bresticker 2531241ef94SPadmavathi Venna #ifdef CONFIG_PM_SLEEP 2541241ef94SPadmavathi Venna register_syscore_ops(&exynos_audss_clk_syscore_ops); 2551241ef94SPadmavathi Venna #endif 2561241ef94SPadmavathi Venna 257b37a4224SAndrew Bresticker dev_info(&pdev->dev, "setup completed\n"); 258b37a4224SAndrew Bresticker 259b37a4224SAndrew Bresticker return 0; 260b37a4224SAndrew Bresticker 261b37a4224SAndrew Bresticker unregister: 26227c76c43SKrzysztof Kozlowski exynos_audss_clk_teardown(); 263b37a4224SAndrew Bresticker 264f1e9203eSKrzysztof Kozlowski if (!IS_ERR(epll)) 265f1e9203eSKrzysztof Kozlowski clk_disable_unprepare(epll); 266f1e9203eSKrzysztof Kozlowski 267b37a4224SAndrew Bresticker return ret; 268b37a4224SAndrew Bresticker } 269b37a4224SAndrew Bresticker 270b37a4224SAndrew Bresticker static int exynos_audss_clk_remove(struct platform_device *pdev) 271b37a4224SAndrew Bresticker { 272c31844ffSKrzysztof Kozlowski #ifdef CONFIG_PM_SLEEP 273c31844ffSKrzysztof Kozlowski unregister_syscore_ops(&exynos_audss_clk_syscore_ops); 274c31844ffSKrzysztof Kozlowski #endif 275c31844ffSKrzysztof Kozlowski 276b37a4224SAndrew Bresticker of_clk_del_provider(pdev->dev.of_node); 277b37a4224SAndrew Bresticker 27827c76c43SKrzysztof Kozlowski exynos_audss_clk_teardown(); 279b37a4224SAndrew Bresticker 280f1e9203eSKrzysztof Kozlowski if (!IS_ERR(epll)) 281f1e9203eSKrzysztof Kozlowski clk_disable_unprepare(epll); 282f1e9203eSKrzysztof Kozlowski 283b37a4224SAndrew Bresticker return 0; 284b37a4224SAndrew Bresticker } 285b37a4224SAndrew Bresticker 286b37a4224SAndrew Bresticker static struct platform_driver exynos_audss_clk_driver = { 287b37a4224SAndrew Bresticker .driver = { 288b37a4224SAndrew Bresticker .name = "exynos-audss-clk", 289b37a4224SAndrew Bresticker .of_match_table = exynos_audss_clk_of_match, 290b37a4224SAndrew Bresticker }, 291b37a4224SAndrew Bresticker .probe = exynos_audss_clk_probe, 292b37a4224SAndrew Bresticker .remove = exynos_audss_clk_remove, 293b37a4224SAndrew Bresticker }; 294b37a4224SAndrew Bresticker 2954d252fd5SSylwester Nawrocki module_platform_driver(exynos_audss_clk_driver); 296b37a4224SAndrew Bresticker 297b37a4224SAndrew Bresticker MODULE_AUTHOR("Padmavathi Venna <padma.v@samsung.com>"); 298b37a4224SAndrew Bresticker MODULE_DESCRIPTION("Exynos Audio Subsystem Clock Controller"); 299b37a4224SAndrew Bresticker MODULE_LICENSE("GPL v2"); 300b37a4224SAndrew Bresticker MODULE_ALIAS("platform:exynos-audss-clk"); 301