xref: /openbmc/linux/drivers/clk/samsung/clk-exynos-audss.c (revision 5b2c3da14ce1e64f66070e426df697652364e16e)
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 void __iomem *reg_base;
26*5b2c3da1SMarek Szyprowski static struct clk_hw_onecell_data *clk_data;
27f1e9203eSKrzysztof Kozlowski /*
28f1e9203eSKrzysztof Kozlowski  * On Exynos5420 this will be a clock which has to be enabled before any
29f1e9203eSKrzysztof Kozlowski  * access to audss registers. Typically a child of EPLL.
30f1e9203eSKrzysztof Kozlowski  *
31f1e9203eSKrzysztof Kozlowski  * On other platforms this will be -ENODEV.
32f1e9203eSKrzysztof Kozlowski  */
33f1e9203eSKrzysztof Kozlowski static struct clk *epll;
341241ef94SPadmavathi Venna 
351241ef94SPadmavathi Venna #define ASS_CLK_SRC 0x0
361241ef94SPadmavathi Venna #define ASS_CLK_DIV 0x4
371241ef94SPadmavathi Venna #define ASS_CLK_GATE 0x8
381241ef94SPadmavathi Venna 
393fd68c99SKrzysztof Kozlowski #ifdef CONFIG_PM_SLEEP
401241ef94SPadmavathi Venna static unsigned long reg_save[][2] = {
411241ef94SPadmavathi Venna 	{ ASS_CLK_SRC,  0 },
421241ef94SPadmavathi Venna 	{ ASS_CLK_DIV,  0 },
431241ef94SPadmavathi Venna 	{ ASS_CLK_GATE, 0 },
441241ef94SPadmavathi Venna };
451241ef94SPadmavathi Venna 
46a5b16dfaSMarek Szyprowski static int exynos_audss_clk_suspend(struct device *dev)
471241ef94SPadmavathi Venna {
481241ef94SPadmavathi Venna 	int i;
491241ef94SPadmavathi Venna 
501241ef94SPadmavathi Venna 	for (i = 0; i < ARRAY_SIZE(reg_save); i++)
511241ef94SPadmavathi Venna 		reg_save[i][1] = readl(reg_base + reg_save[i][0]);
521241ef94SPadmavathi Venna 
531241ef94SPadmavathi Venna 	return 0;
541241ef94SPadmavathi Venna }
551241ef94SPadmavathi Venna 
56a5b16dfaSMarek Szyprowski static int exynos_audss_clk_resume(struct device *dev)
571241ef94SPadmavathi Venna {
581241ef94SPadmavathi Venna 	int i;
591241ef94SPadmavathi Venna 
601241ef94SPadmavathi Venna 	for (i = 0; i < ARRAY_SIZE(reg_save); i++)
611241ef94SPadmavathi Venna 		writel(reg_save[i][1], reg_base + reg_save[i][0]);
621241ef94SPadmavathi Venna 
63a5b16dfaSMarek Szyprowski 	return 0;
64a5b16dfaSMarek Szyprowski }
651241ef94SPadmavathi Venna #endif /* CONFIG_PM_SLEEP */
661241ef94SPadmavathi Venna 
677c3ca061SSylwester Nawrocki struct exynos_audss_clk_drvdata {
687c3ca061SSylwester Nawrocki 	unsigned int has_adma_clk:1;
692ec865b7SSylwester Nawrocki 	unsigned int has_mst_clk:1;
707c3ca061SSylwester Nawrocki 	unsigned int enable_epll:1;
717c3ca061SSylwester Nawrocki 	unsigned int num_clks;
727c3ca061SSylwester Nawrocki };
737c3ca061SSylwester Nawrocki 
747c3ca061SSylwester Nawrocki static const struct exynos_audss_clk_drvdata exynos4210_drvdata = {
757c3ca061SSylwester Nawrocki 	.num_clks	= EXYNOS_AUDSS_MAX_CLKS - 1,
767c3ca061SSylwester Nawrocki };
777c3ca061SSylwester Nawrocki 
782ec865b7SSylwester Nawrocki static const struct exynos_audss_clk_drvdata exynos5410_drvdata = {
792ec865b7SSylwester Nawrocki 	.num_clks	= EXYNOS_AUDSS_MAX_CLKS - 1,
802ec865b7SSylwester Nawrocki 	.has_mst_clk	= 1,
812ec865b7SSylwester Nawrocki };
822ec865b7SSylwester Nawrocki 
837c3ca061SSylwester Nawrocki static const struct exynos_audss_clk_drvdata exynos5420_drvdata = {
847c3ca061SSylwester Nawrocki 	.num_clks	= EXYNOS_AUDSS_MAX_CLKS,
857c3ca061SSylwester Nawrocki 	.has_adma_clk	= 1,
867c3ca061SSylwester Nawrocki 	.enable_epll	= 1,
877c3ca061SSylwester Nawrocki };
887c3ca061SSylwester Nawrocki 
893538a2cfSAndrew Bresticker static const struct of_device_id exynos_audss_clk_of_match[] = {
907c3ca061SSylwester Nawrocki 	{
917c3ca061SSylwester Nawrocki 		.compatible	= "samsung,exynos4210-audss-clock",
927c3ca061SSylwester Nawrocki 		.data		= &exynos4210_drvdata,
937c3ca061SSylwester Nawrocki 	}, {
947c3ca061SSylwester Nawrocki 		.compatible	= "samsung,exynos5250-audss-clock",
957c3ca061SSylwester Nawrocki 		.data		= &exynos4210_drvdata,
967c3ca061SSylwester Nawrocki 	}, {
972ec865b7SSylwester Nawrocki 		.compatible	= "samsung,exynos5410-audss-clock",
982ec865b7SSylwester Nawrocki 		.data		= &exynos5410_drvdata,
992ec865b7SSylwester Nawrocki 	}, {
1007c3ca061SSylwester Nawrocki 		.compatible	= "samsung,exynos5420-audss-clock",
1017c3ca061SSylwester Nawrocki 		.data		= &exynos5420_drvdata,
1027c3ca061SSylwester Nawrocki 	},
1033538a2cfSAndrew Bresticker 	{ },
1043538a2cfSAndrew Bresticker };
10534b89b29SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, exynos_audss_clk_of_match);
1063538a2cfSAndrew Bresticker 
10727c76c43SKrzysztof Kozlowski static void exynos_audss_clk_teardown(void)
10827c76c43SKrzysztof Kozlowski {
10927c76c43SKrzysztof Kozlowski 	int i;
11027c76c43SKrzysztof Kozlowski 
11127c76c43SKrzysztof Kozlowski 	for (i = EXYNOS_MOUT_AUDSS; i < EXYNOS_DOUT_SRP; i++) {
112*5b2c3da1SMarek Szyprowski 		if (!IS_ERR(clk_data->hws[i]))
113*5b2c3da1SMarek Szyprowski 			clk_hw_unregister_mux(clk_data->hws[i]);
11427c76c43SKrzysztof Kozlowski 	}
11527c76c43SKrzysztof Kozlowski 
11627c76c43SKrzysztof Kozlowski 	for (; i < EXYNOS_SRP_CLK; i++) {
117*5b2c3da1SMarek Szyprowski 		if (!IS_ERR(clk_data->hws[i]))
118*5b2c3da1SMarek Szyprowski 			clk_hw_unregister_divider(clk_data->hws[i]);
11927c76c43SKrzysztof Kozlowski 	}
12027c76c43SKrzysztof Kozlowski 
121*5b2c3da1SMarek Szyprowski 	for (; i < clk_data->num; i++) {
122*5b2c3da1SMarek Szyprowski 		if (!IS_ERR(clk_data->hws[i]))
123*5b2c3da1SMarek Szyprowski 			clk_hw_unregister_gate(clk_data->hws[i]);
12427c76c43SKrzysztof Kozlowski 	}
12527c76c43SKrzysztof Kozlowski }
12627c76c43SKrzysztof Kozlowski 
1271241ef94SPadmavathi Venna /* register exynos_audss clocks */
128b37a4224SAndrew Bresticker static int exynos_audss_clk_probe(struct platform_device *pdev)
1291241ef94SPadmavathi Venna {
130547f3350SAndrew Bresticker 	const char *mout_audss_p[] = {"fin_pll", "fout_epll"};
131547f3350SAndrew Bresticker 	const char *mout_i2s_p[] = {"mout_audss", "cdclk0", "sclk_audio0"};
132547f3350SAndrew Bresticker 	const char *sclk_pcm_p = "sclk_pcm0";
133547f3350SAndrew Bresticker 	struct clk *pll_ref, *pll_in, *cdclk, *sclk_audio, *sclk_pcm_in;
1347c3ca061SSylwester Nawrocki 	const struct exynos_audss_clk_drvdata *variant;
135*5b2c3da1SMarek Szyprowski 	struct clk_hw **clk_table;
1367c3ca061SSylwester Nawrocki 	struct resource *res;
1377c3ca061SSylwester Nawrocki 	int i, ret = 0;
1383538a2cfSAndrew Bresticker 
1397c3ca061SSylwester Nawrocki 	variant = of_device_get_match_data(&pdev->dev);
1407c3ca061SSylwester Nawrocki 	if (!variant)
1413538a2cfSAndrew Bresticker 		return -EINVAL;
142b37a4224SAndrew Bresticker 
143b37a4224SAndrew Bresticker 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
144b37a4224SAndrew Bresticker 	reg_base = devm_ioremap_resource(&pdev->dev, res);
145b37a4224SAndrew Bresticker 	if (IS_ERR(reg_base)) {
146b37a4224SAndrew Bresticker 		dev_err(&pdev->dev, "failed to map audss registers\n");
147b37a4224SAndrew Bresticker 		return PTR_ERR(reg_base);
1481241ef94SPadmavathi Venna 	}
1497c3ca061SSylwester Nawrocki 
150f1e9203eSKrzysztof Kozlowski 	epll = ERR_PTR(-ENODEV);
1511241ef94SPadmavathi Venna 
152*5b2c3da1SMarek Szyprowski 	clk_data = devm_kzalloc(&pdev->dev,
153*5b2c3da1SMarek Szyprowski 				sizeof(*clk_data) +
154*5b2c3da1SMarek Szyprowski 				sizeof(*clk_data->hws) * EXYNOS_AUDSS_MAX_CLKS,
1551241ef94SPadmavathi Venna 				GFP_KERNEL);
156*5b2c3da1SMarek Szyprowski 	if (!clk_data)
157b37a4224SAndrew Bresticker 		return -ENOMEM;
1581241ef94SPadmavathi Venna 
159*5b2c3da1SMarek Szyprowski 	clk_data->num = variant->num_clks;
160*5b2c3da1SMarek Szyprowski 	clk_table = clk_data->hws;
1611241ef94SPadmavathi Venna 
162547f3350SAndrew Bresticker 	pll_ref = devm_clk_get(&pdev->dev, "pll_ref");
163547f3350SAndrew Bresticker 	pll_in = devm_clk_get(&pdev->dev, "pll_in");
164547f3350SAndrew Bresticker 	if (!IS_ERR(pll_ref))
165547f3350SAndrew Bresticker 		mout_audss_p[0] = __clk_get_name(pll_ref);
166f1e9203eSKrzysztof Kozlowski 	if (!IS_ERR(pll_in)) {
167547f3350SAndrew Bresticker 		mout_audss_p[1] = __clk_get_name(pll_in);
168f1e9203eSKrzysztof Kozlowski 
1697c3ca061SSylwester Nawrocki 		if (variant->enable_epll) {
170f1e9203eSKrzysztof Kozlowski 			epll = pll_in;
171f1e9203eSKrzysztof Kozlowski 
172f1e9203eSKrzysztof Kozlowski 			ret = clk_prepare_enable(epll);
173f1e9203eSKrzysztof Kozlowski 			if (ret) {
174f1e9203eSKrzysztof Kozlowski 				dev_err(&pdev->dev,
175f1e9203eSKrzysztof Kozlowski 					"failed to prepare the epll clock\n");
176f1e9203eSKrzysztof Kozlowski 				return ret;
177f1e9203eSKrzysztof Kozlowski 			}
178f1e9203eSKrzysztof Kozlowski 		}
179f1e9203eSKrzysztof Kozlowski 	}
180*5b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_MOUT_AUDSS] = clk_hw_register_mux(NULL, "mout_audss",
181819c1de3SJames Hogan 				mout_audss_p, ARRAY_SIZE(mout_audss_p),
182819c1de3SJames Hogan 				CLK_SET_RATE_NO_REPARENT,
1831241ef94SPadmavathi Venna 				reg_base + ASS_CLK_SRC, 0, 1, 0, &lock);
1841241ef94SPadmavathi Venna 
185547f3350SAndrew Bresticker 	cdclk = devm_clk_get(&pdev->dev, "cdclk");
186547f3350SAndrew Bresticker 	sclk_audio = devm_clk_get(&pdev->dev, "sclk_audio");
187547f3350SAndrew Bresticker 	if (!IS_ERR(cdclk))
188547f3350SAndrew Bresticker 		mout_i2s_p[1] = __clk_get_name(cdclk);
189547f3350SAndrew Bresticker 	if (!IS_ERR(sclk_audio))
190547f3350SAndrew Bresticker 		mout_i2s_p[2] = __clk_get_name(sclk_audio);
191*5b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_MOUT_I2S] = clk_hw_register_mux(NULL, "mout_i2s",
192819c1de3SJames Hogan 				mout_i2s_p, ARRAY_SIZE(mout_i2s_p),
193819c1de3SJames Hogan 				CLK_SET_RATE_NO_REPARENT,
1941241ef94SPadmavathi Venna 				reg_base + ASS_CLK_SRC, 2, 2, 0, &lock);
1951241ef94SPadmavathi Venna 
196*5b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_DOUT_SRP] = clk_hw_register_divider(NULL, "dout_srp",
1971241ef94SPadmavathi Venna 				"mout_audss", 0, reg_base + ASS_CLK_DIV, 0, 4,
1981241ef94SPadmavathi Venna 				0, &lock);
1991241ef94SPadmavathi Venna 
200*5b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_DOUT_AUD_BUS] = clk_hw_register_divider(NULL,
2011241ef94SPadmavathi Venna 				"dout_aud_bus", "dout_srp", 0,
2021241ef94SPadmavathi Venna 				reg_base + ASS_CLK_DIV, 4, 4, 0, &lock);
2031241ef94SPadmavathi Venna 
204*5b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_DOUT_I2S] = clk_hw_register_divider(NULL, "dout_i2s",
2051241ef94SPadmavathi Venna 				"mout_i2s", 0, reg_base + ASS_CLK_DIV, 8, 4, 0,
2061241ef94SPadmavathi Venna 				&lock);
2071241ef94SPadmavathi Venna 
208*5b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_SRP_CLK] = clk_hw_register_gate(NULL, "srp_clk",
2091241ef94SPadmavathi Venna 				"dout_srp", CLK_SET_RATE_PARENT,
2101241ef94SPadmavathi Venna 				reg_base + ASS_CLK_GATE, 0, 0, &lock);
2111241ef94SPadmavathi Venna 
212*5b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_I2S_BUS] = clk_hw_register_gate(NULL, "i2s_bus",
2131241ef94SPadmavathi Venna 				"dout_aud_bus", CLK_SET_RATE_PARENT,
2141241ef94SPadmavathi Venna 				reg_base + ASS_CLK_GATE, 2, 0, &lock);
2151241ef94SPadmavathi Venna 
216*5b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_SCLK_I2S] = clk_hw_register_gate(NULL, "sclk_i2s",
2171241ef94SPadmavathi Venna 				"dout_i2s", CLK_SET_RATE_PARENT,
2181241ef94SPadmavathi Venna 				reg_base + ASS_CLK_GATE, 3, 0, &lock);
2191241ef94SPadmavathi Venna 
220*5b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_PCM_BUS] = clk_hw_register_gate(NULL, "pcm_bus",
2211241ef94SPadmavathi Venna 				 "sclk_pcm", CLK_SET_RATE_PARENT,
2221241ef94SPadmavathi Venna 				reg_base + ASS_CLK_GATE, 4, 0, &lock);
2231241ef94SPadmavathi Venna 
224547f3350SAndrew Bresticker 	sclk_pcm_in = devm_clk_get(&pdev->dev, "sclk_pcm_in");
225547f3350SAndrew Bresticker 	if (!IS_ERR(sclk_pcm_in))
226547f3350SAndrew Bresticker 		sclk_pcm_p = __clk_get_name(sclk_pcm_in);
227*5b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_SCLK_PCM] = clk_hw_register_gate(NULL, "sclk_pcm",
228547f3350SAndrew Bresticker 				sclk_pcm_p, CLK_SET_RATE_PARENT,
2291241ef94SPadmavathi Venna 				reg_base + ASS_CLK_GATE, 5, 0, &lock);
2301241ef94SPadmavathi Venna 
2317c3ca061SSylwester Nawrocki 	if (variant->has_adma_clk) {
232*5b2c3da1SMarek Szyprowski 		clk_table[EXYNOS_ADMA] = clk_hw_register_gate(NULL, "adma",
2333538a2cfSAndrew Bresticker 				"dout_srp", CLK_SET_RATE_PARENT,
2343538a2cfSAndrew Bresticker 				reg_base + ASS_CLK_GATE, 9, 0, &lock);
2353538a2cfSAndrew Bresticker 	}
2363538a2cfSAndrew Bresticker 
237*5b2c3da1SMarek Szyprowski 	for (i = 0; i < clk_data->num; i++) {
238b37a4224SAndrew Bresticker 		if (IS_ERR(clk_table[i])) {
239b37a4224SAndrew Bresticker 			dev_err(&pdev->dev, "failed to register clock %d\n", i);
240b37a4224SAndrew Bresticker 			ret = PTR_ERR(clk_table[i]);
241b37a4224SAndrew Bresticker 			goto unregister;
242b37a4224SAndrew Bresticker 		}
243b37a4224SAndrew Bresticker 	}
244b37a4224SAndrew Bresticker 
245*5b2c3da1SMarek Szyprowski 	ret = of_clk_add_hw_provider(pdev->dev.of_node, of_clk_hw_onecell_get,
246*5b2c3da1SMarek Szyprowski 				     clk_data);
247b37a4224SAndrew Bresticker 	if (ret) {
248b37a4224SAndrew Bresticker 		dev_err(&pdev->dev, "failed to add clock provider\n");
249b37a4224SAndrew Bresticker 		goto unregister;
250b37a4224SAndrew Bresticker 	}
251b37a4224SAndrew Bresticker 
252b37a4224SAndrew Bresticker 	return 0;
253b37a4224SAndrew Bresticker 
254b37a4224SAndrew Bresticker unregister:
25527c76c43SKrzysztof Kozlowski 	exynos_audss_clk_teardown();
256b37a4224SAndrew Bresticker 
257f1e9203eSKrzysztof Kozlowski 	if (!IS_ERR(epll))
258f1e9203eSKrzysztof Kozlowski 		clk_disable_unprepare(epll);
259f1e9203eSKrzysztof Kozlowski 
260b37a4224SAndrew Bresticker 	return ret;
261b37a4224SAndrew Bresticker }
262b37a4224SAndrew Bresticker 
263b37a4224SAndrew Bresticker static int exynos_audss_clk_remove(struct platform_device *pdev)
264b37a4224SAndrew Bresticker {
265b37a4224SAndrew Bresticker 	of_clk_del_provider(pdev->dev.of_node);
266b37a4224SAndrew Bresticker 
26727c76c43SKrzysztof Kozlowski 	exynos_audss_clk_teardown();
268b37a4224SAndrew Bresticker 
269f1e9203eSKrzysztof Kozlowski 	if (!IS_ERR(epll))
270f1e9203eSKrzysztof Kozlowski 		clk_disable_unprepare(epll);
271f1e9203eSKrzysztof Kozlowski 
272b37a4224SAndrew Bresticker 	return 0;
273b37a4224SAndrew Bresticker }
274b37a4224SAndrew Bresticker 
275a5b16dfaSMarek Szyprowski static const struct dev_pm_ops exynos_audss_clk_pm_ops = {
276a5b16dfaSMarek Szyprowski 	SET_LATE_SYSTEM_SLEEP_PM_OPS(exynos_audss_clk_suspend,
277a5b16dfaSMarek Szyprowski 				     exynos_audss_clk_resume)
278a5b16dfaSMarek Szyprowski };
279a5b16dfaSMarek Szyprowski 
280b37a4224SAndrew Bresticker static struct platform_driver exynos_audss_clk_driver = {
281b37a4224SAndrew Bresticker 	.driver	= {
282b37a4224SAndrew Bresticker 		.name = "exynos-audss-clk",
283b37a4224SAndrew Bresticker 		.of_match_table = exynos_audss_clk_of_match,
284a5b16dfaSMarek Szyprowski 		.pm = &exynos_audss_clk_pm_ops,
285b37a4224SAndrew Bresticker 	},
286b37a4224SAndrew Bresticker 	.probe = exynos_audss_clk_probe,
287b37a4224SAndrew Bresticker 	.remove = exynos_audss_clk_remove,
288b37a4224SAndrew Bresticker };
289b37a4224SAndrew Bresticker 
2904d252fd5SSylwester Nawrocki module_platform_driver(exynos_audss_clk_driver);
291b37a4224SAndrew Bresticker 
292b37a4224SAndrew Bresticker MODULE_AUTHOR("Padmavathi Venna <padma.v@samsung.com>");
293b37a4224SAndrew Bresticker MODULE_DESCRIPTION("Exynos Audio Subsystem Clock Controller");
294b37a4224SAndrew Bresticker MODULE_LICENSE("GPL v2");
295b37a4224SAndrew Bresticker MODULE_ALIAS("platform:exynos-audss-clk");
296