xref: /OK3568_Linux_fs/kernel/sound/soc/codecs/dummy-codec.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // dummy_codec.c  --  dummy audio codec for rockchip
4 //
5 // Copyright (C) 2018 Fuzhou Rockchip Electronics Co., Ltd
6 
7 #include <linux/clk.h>
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/slab.h>
11 #include <linux/of.h>
12 #include <linux/of_gpio.h>
13 #include <sound/soc.h>
14 #include <sound/pcm.h>
15 #include <sound/initval.h>
16 
17 struct dummy_codec_priv {
18 	struct snd_soc_component *component;
19 	struct clk *mclk;
20 };
21 
dummy_codec_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)22 static int dummy_codec_startup(struct snd_pcm_substream *substream,
23 			       struct snd_soc_dai *dai)
24 {
25 	struct dummy_codec_priv *dcp = snd_soc_component_get_drvdata(dai->component);
26 
27 	if (!IS_ERR(dcp->mclk))
28 		clk_prepare_enable(dcp->mclk);
29 
30 	return 0;
31 }
32 
dummy_codec_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)33 static void dummy_codec_shutdown(struct snd_pcm_substream *substream,
34 				 struct snd_soc_dai *dai)
35 {
36 	struct dummy_codec_priv *dcp = snd_soc_component_get_drvdata(dai->component);
37 
38 	if (!IS_ERR(dcp->mclk))
39 		clk_disable_unprepare(dcp->mclk);
40 }
41 
42 static struct snd_soc_dai_ops dummy_codec_dai_ops = {
43 	.startup	= dummy_codec_startup,
44 	.shutdown	= dummy_codec_shutdown,
45 };
46 
47 struct snd_soc_dai_driver dummy_dai = {
48 	.name = "dummy_codec",
49 	.playback = {
50 		.stream_name = "Dummy Playback",
51 		.channels_min = 1,
52 		.channels_max = 384,
53 		.rates = SNDRV_PCM_RATE_8000_384000,
54 		.formats = (SNDRV_PCM_FMTBIT_S8 |
55 			    SNDRV_PCM_FMTBIT_S16_LE |
56 			    SNDRV_PCM_FMTBIT_S20_3LE |
57 			    SNDRV_PCM_FMTBIT_S24_LE |
58 			    SNDRV_PCM_FMTBIT_S32_LE),
59 	},
60 	.capture = {
61 		.stream_name = "Dummy Capture",
62 		.channels_min = 1,
63 		.channels_max = 384,
64 		.rates = SNDRV_PCM_RATE_8000_384000,
65 		.formats = (SNDRV_PCM_FMTBIT_S8 |
66 			    SNDRV_PCM_FMTBIT_S16_LE |
67 			    SNDRV_PCM_FMTBIT_S20_3LE |
68 			    SNDRV_PCM_FMTBIT_S24_LE |
69 			    SNDRV_PCM_FMTBIT_S32_LE),
70 	},
71 	.ops = &dummy_codec_dai_ops,
72 };
73 
74 static const struct snd_soc_component_driver soc_dummy_codec;
75 
rockchip_dummy_codec_probe(struct platform_device * pdev)76 static int rockchip_dummy_codec_probe(struct platform_device *pdev)
77 {
78 	struct dummy_codec_priv *dcp;
79 
80 	dcp = devm_kzalloc(&pdev->dev, sizeof(*dcp), GFP_KERNEL);
81 	if (!dcp)
82 		return -ENOMEM;
83 
84 	platform_set_drvdata(pdev, dcp);
85 
86 	/* optional mclk, if needs, assign mclk in dts node */
87 	dcp->mclk = devm_clk_get(&pdev->dev, "mclk");
88 	if (IS_ERR(dcp->mclk)) {
89 		if (PTR_ERR(dcp->mclk) == -EPROBE_DEFER)
90 			return -EPROBE_DEFER;
91 		else if (PTR_ERR(dcp->mclk) != -ENOENT)
92 			return -EINVAL;
93 	}
94 
95 	return devm_snd_soc_register_component(&pdev->dev, &soc_dummy_codec,
96 					       &dummy_dai, 1);
97 }
98 
99 static const struct of_device_id rockchip_dummy_codec_of_match[] = {
100 	{ .compatible = "rockchip,dummy-codec", },
101 	{},
102 };
103 MODULE_DEVICE_TABLE(of, rockchip_dummy_codec_of_match);
104 
105 static struct platform_driver rockchip_dummy_codec_driver = {
106 	.driver = {
107 		.name = "dummy_codec",
108 		.of_match_table = of_match_ptr(rockchip_dummy_codec_of_match),
109 	},
110 	.probe = rockchip_dummy_codec_probe,
111 };
112 
113 module_platform_driver(rockchip_dummy_codec_driver);
114 
115 MODULE_AUTHOR("Sugar <sugar.zhang@rock-chips.com>");
116 MODULE_DESCRIPTION("Rockchip Dummy Codec Driver");
117 MODULE_LICENSE("GPL v2");
118