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 = snd_soc_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 /* Optional codec node */ 93 ret = of_parse_phandle_with_fixed_args(np, "audio-codec", 0, 0, &args); 94 if (ret) { 95 *data->dai.codecs = snd_soc_dummy_dlc; 96 } else { 97 struct clk *clk; 98 99 ret = snd_soc_get_dlc(&args, data->dai.codecs); 100 if (ret) { 101 dev_err(&pdev->dev, "Unable to get codec_dai_name\n"); 102 goto fail; 103 } 104 105 clk = devm_get_clk_from_child(&pdev->dev, args.np, NULL); 106 if (!IS_ERR(clk)) 107 data->sysclk = clk_get_rate(clk); 108 } 109 110 data->dai.cpus->dai_name = dev_name(&rpmsg_pdev->dev); 111 if (!of_property_read_string(np, "fsl,rpmsg-channel-name", &platform_name)) 112 data->dai.platforms->name = platform_name; 113 else 114 data->dai.platforms->name = "rpmsg-audio-channel"; 115 data->dai.playback_only = true; 116 data->dai.capture_only = true; 117 data->card.num_links = 1; 118 data->card.dai_link = &data->dai; 119 120 if (of_property_read_bool(np, "fsl,rpmsg-out")) 121 data->dai.capture_only = false; 122 123 if (of_property_read_bool(np, "fsl,rpmsg-in")) 124 data->dai.playback_only = false; 125 126 if (data->dai.playback_only && data->dai.capture_only) { 127 dev_err(&pdev->dev, "no enabled rpmsg DAI link\n"); 128 ret = -EINVAL; 129 goto fail; 130 } 131 132 data->card.dev = &pdev->dev; 133 data->card.owner = THIS_MODULE; 134 data->card.dapm_widgets = imx_rpmsg_dapm_widgets; 135 data->card.num_dapm_widgets = ARRAY_SIZE(imx_rpmsg_dapm_widgets); 136 data->card.late_probe = imx_rpmsg_late_probe; 137 /* 138 * Inoder to use common api to get card name and audio routing. 139 * Use parent of_node for this device, revert it after finishing using 140 */ 141 data->card.dev->of_node = np; 142 143 ret = snd_soc_of_parse_card_name(&data->card, "model"); 144 if (ret) 145 goto fail; 146 147 if (of_property_read_bool(np, "audio-routing")) { 148 ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing"); 149 if (ret) { 150 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret); 151 goto fail; 152 } 153 } 154 155 platform_set_drvdata(pdev, &data->card); 156 snd_soc_card_set_drvdata(&data->card, data); 157 ret = devm_snd_soc_register_card(&pdev->dev, &data->card); 158 if (ret) { 159 dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n"); 160 goto fail; 161 } 162 163 fail: 164 pdev->dev.of_node = NULL; 165 return ret; 166 } 167 168 static struct platform_driver imx_rpmsg_driver = { 169 .driver = { 170 .name = "imx-audio-rpmsg", 171 .pm = &snd_soc_pm_ops, 172 }, 173 .probe = imx_rpmsg_probe, 174 }; 175 module_platform_driver(imx_rpmsg_driver); 176 177 MODULE_DESCRIPTION("Freescale SoC Audio RPMSG Machine Driver"); 178 MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>"); 179 MODULE_ALIAS("platform:imx-audio-rpmsg"); 180 MODULE_LICENSE("GPL v2"); 181