xref: /openbmc/linux/drivers/clk/samsung/clk-exynos-audss.c (revision 7df45a532c5ee3efe106e8a9042a3524b0b587b1)
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;
265b2c3da1SMarek 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,
765bb4053bSKrzysztof Kozlowski 	.enable_epll	= 1,
777c3ca061SSylwester Nawrocki };
787c3ca061SSylwester Nawrocki 
792ec865b7SSylwester Nawrocki static const struct exynos_audss_clk_drvdata exynos5410_drvdata = {
802ec865b7SSylwester Nawrocki 	.num_clks	= EXYNOS_AUDSS_MAX_CLKS - 1,
812ec865b7SSylwester Nawrocki 	.has_mst_clk	= 1,
822ec865b7SSylwester Nawrocki };
832ec865b7SSylwester Nawrocki 
847c3ca061SSylwester Nawrocki static const struct exynos_audss_clk_drvdata exynos5420_drvdata = {
857c3ca061SSylwester Nawrocki 	.num_clks	= EXYNOS_AUDSS_MAX_CLKS,
867c3ca061SSylwester Nawrocki 	.has_adma_clk	= 1,
877c3ca061SSylwester Nawrocki 	.enable_epll	= 1,
887c3ca061SSylwester Nawrocki };
897c3ca061SSylwester Nawrocki 
903538a2cfSAndrew Bresticker static const struct of_device_id exynos_audss_clk_of_match[] = {
917c3ca061SSylwester Nawrocki 	{
927c3ca061SSylwester Nawrocki 		.compatible	= "samsung,exynos4210-audss-clock",
937c3ca061SSylwester Nawrocki 		.data		= &exynos4210_drvdata,
947c3ca061SSylwester Nawrocki 	}, {
957c3ca061SSylwester Nawrocki 		.compatible	= "samsung,exynos5250-audss-clock",
967c3ca061SSylwester Nawrocki 		.data		= &exynos4210_drvdata,
977c3ca061SSylwester Nawrocki 	}, {
982ec865b7SSylwester Nawrocki 		.compatible	= "samsung,exynos5410-audss-clock",
992ec865b7SSylwester Nawrocki 		.data		= &exynos5410_drvdata,
1002ec865b7SSylwester Nawrocki 	}, {
1017c3ca061SSylwester Nawrocki 		.compatible	= "samsung,exynos5420-audss-clock",
1027c3ca061SSylwester Nawrocki 		.data		= &exynos5420_drvdata,
1037c3ca061SSylwester Nawrocki 	},
1043538a2cfSAndrew Bresticker 	{ },
1053538a2cfSAndrew Bresticker };
10634b89b29SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, exynos_audss_clk_of_match);
1073538a2cfSAndrew Bresticker 
10827c76c43SKrzysztof Kozlowski static void exynos_audss_clk_teardown(void)
10927c76c43SKrzysztof Kozlowski {
11027c76c43SKrzysztof Kozlowski 	int i;
11127c76c43SKrzysztof Kozlowski 
11227c76c43SKrzysztof Kozlowski 	for (i = EXYNOS_MOUT_AUDSS; i < EXYNOS_DOUT_SRP; i++) {
1135b2c3da1SMarek Szyprowski 		if (!IS_ERR(clk_data->hws[i]))
1145b2c3da1SMarek Szyprowski 			clk_hw_unregister_mux(clk_data->hws[i]);
11527c76c43SKrzysztof Kozlowski 	}
11627c76c43SKrzysztof Kozlowski 
11727c76c43SKrzysztof Kozlowski 	for (; i < EXYNOS_SRP_CLK; i++) {
1185b2c3da1SMarek Szyprowski 		if (!IS_ERR(clk_data->hws[i]))
1195b2c3da1SMarek Szyprowski 			clk_hw_unregister_divider(clk_data->hws[i]);
12027c76c43SKrzysztof Kozlowski 	}
12127c76c43SKrzysztof Kozlowski 
1225b2c3da1SMarek Szyprowski 	for (; i < clk_data->num; i++) {
1235b2c3da1SMarek Szyprowski 		if (!IS_ERR(clk_data->hws[i]))
1245b2c3da1SMarek Szyprowski 			clk_hw_unregister_gate(clk_data->hws[i]);
12527c76c43SKrzysztof Kozlowski 	}
12627c76c43SKrzysztof Kozlowski }
12727c76c43SKrzysztof Kozlowski 
1281241ef94SPadmavathi Venna /* register exynos_audss clocks */
129b37a4224SAndrew Bresticker static int exynos_audss_clk_probe(struct platform_device *pdev)
1301241ef94SPadmavathi Venna {
131547f3350SAndrew Bresticker 	const char *mout_audss_p[] = {"fin_pll", "fout_epll"};
132547f3350SAndrew Bresticker 	const char *mout_i2s_p[] = {"mout_audss", "cdclk0", "sclk_audio0"};
133547f3350SAndrew Bresticker 	const char *sclk_pcm_p = "sclk_pcm0";
134547f3350SAndrew Bresticker 	struct clk *pll_ref, *pll_in, *cdclk, *sclk_audio, *sclk_pcm_in;
1357c3ca061SSylwester Nawrocki 	const struct exynos_audss_clk_drvdata *variant;
1365b2c3da1SMarek Szyprowski 	struct clk_hw **clk_table;
1377c3ca061SSylwester Nawrocki 	struct resource *res;
1387c3ca061SSylwester Nawrocki 	int i, ret = 0;
1393538a2cfSAndrew Bresticker 
1407c3ca061SSylwester Nawrocki 	variant = of_device_get_match_data(&pdev->dev);
1417c3ca061SSylwester Nawrocki 	if (!variant)
1423538a2cfSAndrew Bresticker 		return -EINVAL;
143b37a4224SAndrew Bresticker 
144b37a4224SAndrew Bresticker 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
145b37a4224SAndrew Bresticker 	reg_base = devm_ioremap_resource(&pdev->dev, res);
146b37a4224SAndrew Bresticker 	if (IS_ERR(reg_base)) {
147b37a4224SAndrew Bresticker 		dev_err(&pdev->dev, "failed to map audss registers\n");
148b37a4224SAndrew Bresticker 		return PTR_ERR(reg_base);
1491241ef94SPadmavathi Venna 	}
1507c3ca061SSylwester Nawrocki 
151f1e9203eSKrzysztof Kozlowski 	epll = ERR_PTR(-ENODEV);
1521241ef94SPadmavathi Venna 
1535b2c3da1SMarek Szyprowski 	clk_data = devm_kzalloc(&pdev->dev,
1545b2c3da1SMarek Szyprowski 				sizeof(*clk_data) +
1555b2c3da1SMarek Szyprowski 				sizeof(*clk_data->hws) * EXYNOS_AUDSS_MAX_CLKS,
1561241ef94SPadmavathi Venna 				GFP_KERNEL);
1575b2c3da1SMarek Szyprowski 	if (!clk_data)
158b37a4224SAndrew Bresticker 		return -ENOMEM;
1591241ef94SPadmavathi Venna 
1605b2c3da1SMarek Szyprowski 	clk_data->num = variant->num_clks;
1615b2c3da1SMarek Szyprowski 	clk_table = clk_data->hws;
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 	}
1815b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_MOUT_AUDSS] = clk_hw_register_mux(NULL, "mout_audss",
182819c1de3SJames Hogan 				mout_audss_p, ARRAY_SIZE(mout_audss_p),
183*7df45a53SSylwester Nawrocki 				CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT,
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);
1925b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_MOUT_I2S] = clk_hw_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 
1975b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_DOUT_SRP] = clk_hw_register_divider(NULL, "dout_srp",
198*7df45a53SSylwester Nawrocki 				"mout_audss", CLK_SET_RATE_PARENT,
199*7df45a53SSylwester Nawrocki 				reg_base + ASS_CLK_DIV, 0, 4, 0, &lock);
2001241ef94SPadmavathi Venna 
2015b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_DOUT_AUD_BUS] = clk_hw_register_divider(NULL,
202*7df45a53SSylwester Nawrocki 				"dout_aud_bus", "dout_srp", CLK_SET_RATE_PARENT,
2031241ef94SPadmavathi Venna 				reg_base + ASS_CLK_DIV, 4, 4, 0, &lock);
2041241ef94SPadmavathi Venna 
2055b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_DOUT_I2S] = clk_hw_register_divider(NULL, "dout_i2s",
2061241ef94SPadmavathi Venna 				"mout_i2s", 0, reg_base + ASS_CLK_DIV, 8, 4, 0,
2071241ef94SPadmavathi Venna 				&lock);
2081241ef94SPadmavathi Venna 
2095b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_SRP_CLK] = clk_hw_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 
2135b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_I2S_BUS] = clk_hw_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 
2175b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_SCLK_I2S] = clk_hw_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 
2215b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_PCM_BUS] = clk_hw_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);
2285b2c3da1SMarek Szyprowski 	clk_table[EXYNOS_SCLK_PCM] = clk_hw_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) {
2335b2c3da1SMarek Szyprowski 		clk_table[EXYNOS_ADMA] = clk_hw_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 
2385b2c3da1SMarek Szyprowski 	for (i = 0; i < clk_data->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 
2465b2c3da1SMarek Szyprowski 	ret = of_clk_add_hw_provider(pdev->dev.of_node, of_clk_hw_onecell_get,
2475b2c3da1SMarek Szyprowski 				     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 
253b37a4224SAndrew Bresticker 	return 0;
254b37a4224SAndrew Bresticker 
255b37a4224SAndrew Bresticker unregister:
25627c76c43SKrzysztof Kozlowski 	exynos_audss_clk_teardown();
257b37a4224SAndrew Bresticker 
258f1e9203eSKrzysztof Kozlowski 	if (!IS_ERR(epll))
259f1e9203eSKrzysztof Kozlowski 		clk_disable_unprepare(epll);
260f1e9203eSKrzysztof Kozlowski 
261b37a4224SAndrew Bresticker 	return ret;
262b37a4224SAndrew Bresticker }
263b37a4224SAndrew Bresticker 
264b37a4224SAndrew Bresticker static int exynos_audss_clk_remove(struct platform_device *pdev)
265b37a4224SAndrew Bresticker {
266b37a4224SAndrew Bresticker 	of_clk_del_provider(pdev->dev.of_node);
267b37a4224SAndrew Bresticker 
26827c76c43SKrzysztof Kozlowski 	exynos_audss_clk_teardown();
269b37a4224SAndrew Bresticker 
270f1e9203eSKrzysztof Kozlowski 	if (!IS_ERR(epll))
271f1e9203eSKrzysztof Kozlowski 		clk_disable_unprepare(epll);
272f1e9203eSKrzysztof Kozlowski 
273b37a4224SAndrew Bresticker 	return 0;
274b37a4224SAndrew Bresticker }
275b37a4224SAndrew Bresticker 
276a5b16dfaSMarek Szyprowski static const struct dev_pm_ops exynos_audss_clk_pm_ops = {
277a5b16dfaSMarek Szyprowski 	SET_LATE_SYSTEM_SLEEP_PM_OPS(exynos_audss_clk_suspend,
278a5b16dfaSMarek Szyprowski 				     exynos_audss_clk_resume)
279a5b16dfaSMarek Szyprowski };
280a5b16dfaSMarek Szyprowski 
281b37a4224SAndrew Bresticker static struct platform_driver exynos_audss_clk_driver = {
282b37a4224SAndrew Bresticker 	.driver	= {
283b37a4224SAndrew Bresticker 		.name = "exynos-audss-clk",
284b37a4224SAndrew Bresticker 		.of_match_table = exynos_audss_clk_of_match,
285a5b16dfaSMarek Szyprowski 		.pm = &exynos_audss_clk_pm_ops,
286b37a4224SAndrew Bresticker 	},
287b37a4224SAndrew Bresticker 	.probe = exynos_audss_clk_probe,
288b37a4224SAndrew Bresticker 	.remove = exynos_audss_clk_remove,
289b37a4224SAndrew Bresticker };
290b37a4224SAndrew Bresticker 
2914d252fd5SSylwester Nawrocki module_platform_driver(exynos_audss_clk_driver);
292b37a4224SAndrew Bresticker 
293b37a4224SAndrew Bresticker MODULE_AUTHOR("Padmavathi Venna <padma.v@samsung.com>");
294b37a4224SAndrew Bresticker MODULE_DESCRIPTION("Exynos Audio Subsystem Clock Controller");
295b37a4224SAndrew Bresticker MODULE_LICENSE("GPL v2");
296b37a4224SAndrew Bresticker MODULE_ALIAS("platform:exynos-audss-clk");
297