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 bool lpa; 24 }; 25 26 static struct dev_pm_ops lpa_pm; 27 28 static const struct snd_soc_dapm_widget imx_rpmsg_dapm_widgets[] = { 29 SND_SOC_DAPM_HP("Headphone Jack", NULL), 30 SND_SOC_DAPM_SPK("Ext Spk", NULL), 31 SND_SOC_DAPM_MIC("Mic Jack", NULL), 32 SND_SOC_DAPM_MIC("Main MIC", NULL), 33 }; 34 35 static int imx_rpmsg_late_probe(struct snd_soc_card *card) 36 { 37 struct imx_rpmsg *data = snd_soc_card_get_drvdata(card); 38 struct snd_soc_pcm_runtime *rtd = list_first_entry(&card->rtd_list, 39 struct snd_soc_pcm_runtime, list); 40 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); 41 struct device *dev = card->dev; 42 int ret; 43 44 if (data->lpa) { 45 struct snd_soc_component *codec_comp; 46 struct device_node *codec_np; 47 struct device_driver *codec_drv; 48 struct device *codec_dev = NULL; 49 50 codec_np = data->dai.codecs->of_node; 51 if (codec_np) { 52 struct platform_device *codec_pdev; 53 struct i2c_client *codec_i2c; 54 55 codec_i2c = of_find_i2c_device_by_node(codec_np); 56 if (codec_i2c) 57 codec_dev = &codec_i2c->dev; 58 if (!codec_dev) { 59 codec_pdev = of_find_device_by_node(codec_np); 60 if (codec_pdev) 61 codec_dev = &codec_pdev->dev; 62 } 63 } 64 if (codec_dev) { 65 codec_comp = snd_soc_lookup_component_nolocked(codec_dev, NULL); 66 if (codec_comp) { 67 int i, num_widgets; 68 const char *widgets; 69 struct snd_soc_dapm_context *dapm; 70 71 num_widgets = of_property_count_strings(data->card.dev->of_node, 72 "ignore-suspend-widgets"); 73 for (i = 0; i < num_widgets; i++) { 74 of_property_read_string_index(data->card.dev->of_node, 75 "ignore-suspend-widgets", 76 i, &widgets); 77 dapm = snd_soc_component_get_dapm(codec_comp); 78 snd_soc_dapm_ignore_suspend(dapm, widgets); 79 } 80 } 81 codec_drv = codec_dev->driver; 82 if (codec_drv->pm) { 83 memcpy(&lpa_pm, codec_drv->pm, sizeof(lpa_pm)); 84 lpa_pm.suspend = NULL; 85 lpa_pm.resume = NULL; 86 lpa_pm.freeze = NULL; 87 lpa_pm.thaw = NULL; 88 lpa_pm.poweroff = NULL; 89 lpa_pm.restore = NULL; 90 codec_drv->pm = &lpa_pm; 91 } 92 put_device(codec_dev); 93 } 94 } 95 96 if (!data->sysclk) 97 return 0; 98 99 ret = snd_soc_dai_set_sysclk(codec_dai, 0, data->sysclk, SND_SOC_CLOCK_IN); 100 if (ret && ret != -ENOTSUPP) { 101 dev_err(dev, "failed to set sysclk in %s\n", __func__); 102 return ret; 103 } 104 105 return 0; 106 } 107 108 static int imx_rpmsg_probe(struct platform_device *pdev) 109 { 110 struct snd_soc_dai_link_component *dlc; 111 struct snd_soc_dai *cpu_dai; 112 struct device_node *np = NULL; 113 struct of_phandle_args args; 114 const char *platform_name; 115 struct imx_rpmsg *data; 116 int ret = 0; 117 118 dlc = devm_kzalloc(&pdev->dev, 3 * sizeof(*dlc), GFP_KERNEL); 119 if (!dlc) 120 return -ENOMEM; 121 122 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 123 if (!data) { 124 ret = -ENOMEM; 125 goto fail; 126 } 127 128 data->dai.cpus = &dlc[0]; 129 data->dai.num_cpus = 1; 130 data->dai.platforms = &dlc[1]; 131 data->dai.num_platforms = 1; 132 data->dai.codecs = &dlc[2]; 133 data->dai.num_codecs = 1; 134 135 data->dai.name = "rpmsg hifi"; 136 data->dai.stream_name = "rpmsg hifi"; 137 data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | 138 SND_SOC_DAIFMT_NB_NF | 139 SND_SOC_DAIFMT_CBC_CFC; 140 141 /* 142 * i.MX rpmsg sound cards work on codec slave mode. MCLK will be 143 * disabled by CPU DAI driver in hw_free(). Some codec requires MCLK 144 * present at power up/down sequence. So need to set ignore_pmdown_time 145 * to power down codec immediately before MCLK is turned off. 146 */ 147 data->dai.ignore_pmdown_time = 1; 148 149 data->dai.cpus->dai_name = pdev->dev.platform_data; 150 cpu_dai = snd_soc_find_dai(data->dai.cpus); 151 if (!cpu_dai) { 152 ret = -EPROBE_DEFER; 153 goto fail; 154 } 155 np = cpu_dai->dev->of_node; 156 if (!np) { 157 dev_err(&pdev->dev, "failed to parse CPU DAI device node\n"); 158 ret = -ENODEV; 159 goto fail; 160 } 161 162 ret = of_reserved_mem_device_init_by_idx(&pdev->dev, np, 0); 163 if (ret) 164 dev_warn(&pdev->dev, "no reserved DMA memory\n"); 165 166 /* Optional codec node */ 167 ret = of_parse_phandle_with_fixed_args(np, "audio-codec", 0, 0, &args); 168 if (ret) { 169 *data->dai.codecs = snd_soc_dummy_dlc; 170 } else { 171 struct clk *clk; 172 173 ret = snd_soc_get_dlc(&args, data->dai.codecs); 174 if (ret) { 175 dev_err(&pdev->dev, "Unable to get codec_dai_name\n"); 176 goto fail; 177 } 178 179 clk = devm_get_clk_from_child(&pdev->dev, args.np, NULL); 180 if (!IS_ERR(clk)) 181 data->sysclk = clk_get_rate(clk); 182 } 183 184 if (!of_property_read_string(np, "fsl,rpmsg-channel-name", &platform_name)) 185 data->dai.platforms->name = platform_name; 186 else 187 data->dai.platforms->name = "rpmsg-audio-channel"; 188 data->dai.playback_only = true; 189 data->dai.capture_only = true; 190 data->card.num_links = 1; 191 data->card.dai_link = &data->dai; 192 193 if (of_property_read_bool(np, "fsl,rpmsg-out")) 194 data->dai.capture_only = false; 195 196 if (of_property_read_bool(np, "fsl,rpmsg-in")) 197 data->dai.playback_only = false; 198 199 if (data->dai.playback_only && data->dai.capture_only) { 200 dev_err(&pdev->dev, "no enabled rpmsg DAI link\n"); 201 ret = -EINVAL; 202 goto fail; 203 } 204 205 if (of_property_read_bool(np, "fsl,enable-lpa")) 206 data->lpa = true; 207 208 data->card.dev = &pdev->dev; 209 data->card.owner = THIS_MODULE; 210 data->card.dapm_widgets = imx_rpmsg_dapm_widgets; 211 data->card.num_dapm_widgets = ARRAY_SIZE(imx_rpmsg_dapm_widgets); 212 data->card.late_probe = imx_rpmsg_late_probe; 213 /* 214 * Inoder to use common api to get card name and audio routing. 215 * Use parent of_node for this device, revert it after finishing using 216 */ 217 data->card.dev->of_node = np; 218 219 ret = snd_soc_of_parse_card_name(&data->card, "model"); 220 if (ret) 221 goto fail; 222 223 if (of_property_read_bool(np, "audio-routing")) { 224 ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing"); 225 if (ret) { 226 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret); 227 goto fail; 228 } 229 } 230 231 platform_set_drvdata(pdev, &data->card); 232 snd_soc_card_set_drvdata(&data->card, data); 233 ret = devm_snd_soc_register_card(&pdev->dev, &data->card); 234 if (ret) { 235 dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n"); 236 goto fail; 237 } 238 239 fail: 240 pdev->dev.of_node = NULL; 241 return ret; 242 } 243 244 static struct platform_driver imx_rpmsg_driver = { 245 .driver = { 246 .name = "imx-audio-rpmsg", 247 .pm = &snd_soc_pm_ops, 248 }, 249 .probe = imx_rpmsg_probe, 250 }; 251 module_platform_driver(imx_rpmsg_driver); 252 253 MODULE_DESCRIPTION("Freescale SoC Audio RPMSG Machine Driver"); 254 MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>"); 255 MODULE_ALIAS("platform:imx-audio-rpmsg"); 256 MODULE_LICENSE("GPL v2"); 257