Merge tag 'wq-for-6.6-rc5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git...
[platform/kernel/linux-starfive.git] / sound / soc / fsl / imx-rpmsg.c
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017-2020 NXP
3
4 #include <linux/module.h>
5 #include <linux/of_platform.h>
6 #include <linux/of_reserved_mem.h>
7 #include <linux/i2c.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"
18
19 struct imx_rpmsg {
20         struct snd_soc_dai_link dai;
21         struct snd_soc_card card;
22         unsigned long sysclk;
23 };
24
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),
30 };
31
32 static int imx_rpmsg_late_probe(struct snd_soc_card *card)
33 {
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;
39         int ret;
40
41         if (!data->sysclk)
42                 return 0;
43
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__);
47                 return ret;
48         }
49
50         return 0;
51 }
52
53 static int imx_rpmsg_probe(struct platform_device *pdev)
54 {
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;
63         int ret = 0;
64
65         dlc = devm_kzalloc(&pdev->dev, 3 * sizeof(*dlc), GFP_KERNEL);
66         if (!dlc)
67                 return -ENOMEM;
68
69         data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
70         if (!data) {
71                 ret = -ENOMEM;
72                 goto fail;
73         }
74
75         ret = of_reserved_mem_device_init_by_idx(&pdev->dev, np, 0);
76         if (ret)
77                 dev_warn(&pdev->dev, "no reserved DMA memory\n");
78
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;
85
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;
91
92         /*
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.
97          */
98         data->dai.ignore_pmdown_time = 1;
99
100         /* Optional codec node */
101         ret = of_parse_phandle_with_fixed_args(np, "audio-codec", 0, 0, &args);
102         if (ret) {
103                 *data->dai.codecs = asoc_dummy_dlc;
104         } else {
105                 struct clk *clk;
106
107                 ret = snd_soc_get_dlc(&args, data->dai.codecs);
108                 if (ret) {
109                         dev_err(&pdev->dev, "Unable to get codec_dai_name\n");
110                         goto fail;
111                 }
112
113                 clk = devm_get_clk_from_child(&pdev->dev, args.np, NULL);
114                 if (!IS_ERR(clk))
115                         data->sysclk = clk_get_rate(clk);
116         }
117
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;
121         else
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;
127
128         if (of_property_read_bool(np, "fsl,rpmsg-out"))
129                 data->dai.capture_only = false;
130
131         if (of_property_read_bool(np, "fsl,rpmsg-in"))
132                 data->dai.playback_only = false;
133
134         if (data->dai.playback_only && data->dai.capture_only) {
135                 dev_err(&pdev->dev, "no enabled rpmsg DAI link\n");
136                 ret = -EINVAL;
137                 goto fail;
138         }
139
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;
145         /*
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
148          */
149         data->card.dev->of_node = np;
150
151         ret = snd_soc_of_parse_card_name(&data->card, "model");
152         if (ret)
153                 goto fail;
154
155         if (of_property_read_bool(np, "audio-routing")) {
156                 ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
157                 if (ret) {
158                         dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
159                         goto fail;
160                 }
161         }
162
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);
166         if (ret) {
167                 dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
168                 goto fail;
169         }
170
171 fail:
172         pdev->dev.of_node = NULL;
173         return ret;
174 }
175
176 static struct platform_driver imx_rpmsg_driver = {
177         .driver = {
178                 .name = "imx-audio-rpmsg",
179                 .pm = &snd_soc_pm_ops,
180         },
181         .probe = imx_rpmsg_probe,
182 };
183 module_platform_driver(imx_rpmsg_driver);
184
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");