1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017-2020 NXP
4 #include <linux/module.h>
5 #include <linux/of_platform.h>
6 #include <linux/of_reserved_mem.h>
8 #include <linux/of_gpio.h>
9 #include <linux/slab.h>
10 #include <linux/gpio.h>
11 #include <linux/clk.h>
12 #include <sound/soc.h>
13 #include <sound/jack.h>
14 #include <sound/control.h>
15 #include <sound/pcm_params.h>
16 #include <sound/soc-dapm.h>
17 #include "imx-pcm-rpmsg.h"
20 struct snd_soc_dai_link dai;
21 struct snd_soc_card card;
25 static const struct snd_soc_dapm_widget imx_rpmsg_dapm_widgets[] = {
26 SND_SOC_DAPM_HP("Headphone Jack", NULL),
27 SND_SOC_DAPM_SPK("Ext Spk", NULL),
28 SND_SOC_DAPM_MIC("Mic Jack", NULL),
29 SND_SOC_DAPM_MIC("Main MIC", NULL),
32 static int imx_rpmsg_late_probe(struct snd_soc_card *card)
34 struct imx_rpmsg *data = snd_soc_card_get_drvdata(card);
35 struct snd_soc_pcm_runtime *rtd = list_first_entry(&card->rtd_list,
36 struct snd_soc_pcm_runtime, list);
37 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
38 struct device *dev = card->dev;
44 ret = snd_soc_dai_set_sysclk(codec_dai, 0, data->sysclk, SND_SOC_CLOCK_IN);
45 if (ret && ret != -ENOTSUPP) {
46 dev_err(dev, "failed to set sysclk in %s\n", __func__);
53 static int imx_rpmsg_probe(struct platform_device *pdev)
55 struct snd_soc_dai_link_component *dlc;
56 struct device *dev = pdev->dev.parent;
57 /* rpmsg_pdev is the platform device for the rpmsg node that probed us */
58 struct platform_device *rpmsg_pdev = to_platform_device(dev);
59 struct device_node *np = rpmsg_pdev->dev.of_node;
60 struct of_phandle_args args;
61 const char *platform_name;
62 struct imx_rpmsg *data;
65 dlc = devm_kzalloc(&pdev->dev, 3 * sizeof(*dlc), GFP_KERNEL);
69 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
75 ret = of_reserved_mem_device_init_by_idx(&pdev->dev, np, 0);
77 dev_warn(&pdev->dev, "no reserved DMA memory\n");
79 data->dai.cpus = &dlc[0];
80 data->dai.num_cpus = 1;
81 data->dai.platforms = &dlc[1];
82 data->dai.num_platforms = 1;
83 data->dai.codecs = &dlc[2];
84 data->dai.num_codecs = 1;
86 data->dai.name = "rpmsg hifi";
87 data->dai.stream_name = "rpmsg hifi";
88 data->dai.dai_fmt = SND_SOC_DAIFMT_I2S |
89 SND_SOC_DAIFMT_NB_NF |
90 SND_SOC_DAIFMT_CBC_CFC;
93 * i.MX rpmsg sound cards work on codec slave mode. MCLK will be
94 * disabled by CPU DAI driver in hw_free(). Some codec requires MCLK
95 * present at power up/down sequence. So need to set ignore_pmdown_time
96 * to power down codec immediately before MCLK is turned off.
98 data->dai.ignore_pmdown_time = 1;
100 /* Optional codec node */
101 ret = of_parse_phandle_with_fixed_args(np, "audio-codec", 0, 0, &args);
103 *data->dai.codecs = asoc_dummy_dlc;
107 ret = snd_soc_get_dlc(&args, data->dai.codecs);
109 dev_err(&pdev->dev, "Unable to get codec_dai_name\n");
113 clk = devm_get_clk_from_child(&pdev->dev, args.np, NULL);
115 data->sysclk = clk_get_rate(clk);
118 data->dai.cpus->dai_name = dev_name(&rpmsg_pdev->dev);
119 if (!of_property_read_string(np, "fsl,rpmsg-channel-name", &platform_name))
120 data->dai.platforms->name = platform_name;
122 data->dai.platforms->name = "rpmsg-audio-channel";
123 data->dai.playback_only = true;
124 data->dai.capture_only = true;
125 data->card.num_links = 1;
126 data->card.dai_link = &data->dai;
128 if (of_property_read_bool(np, "fsl,rpmsg-out"))
129 data->dai.capture_only = false;
131 if (of_property_read_bool(np, "fsl,rpmsg-in"))
132 data->dai.playback_only = false;
134 if (data->dai.playback_only && data->dai.capture_only) {
135 dev_err(&pdev->dev, "no enabled rpmsg DAI link\n");
140 data->card.dev = &pdev->dev;
141 data->card.owner = THIS_MODULE;
142 data->card.dapm_widgets = imx_rpmsg_dapm_widgets;
143 data->card.num_dapm_widgets = ARRAY_SIZE(imx_rpmsg_dapm_widgets);
144 data->card.late_probe = imx_rpmsg_late_probe;
146 * Inoder to use common api to get card name and audio routing.
147 * Use parent of_node for this device, revert it after finishing using
149 data->card.dev->of_node = np;
151 ret = snd_soc_of_parse_card_name(&data->card, "model");
155 if (of_property_read_bool(np, "audio-routing")) {
156 ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
158 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
163 platform_set_drvdata(pdev, &data->card);
164 snd_soc_card_set_drvdata(&data->card, data);
165 ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
167 dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
172 pdev->dev.of_node = NULL;
176 static struct platform_driver imx_rpmsg_driver = {
178 .name = "imx-audio-rpmsg",
179 .pm = &snd_soc_pm_ops,
181 .probe = imx_rpmsg_probe,
183 module_platform_driver(imx_rpmsg_driver);
185 MODULE_DESCRIPTION("Freescale SoC Audio RPMSG Machine Driver");
186 MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>");
187 MODULE_ALIAS("platform:imx-audio-rpmsg");
188 MODULE_LICENSE("GPL v2");