Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 // Copyright 2017-2020 NXP
0003 
0004 #include <linux/module.h>
0005 #include <linux/of_platform.h>
0006 #include <linux/of_reserved_mem.h>
0007 #include <linux/i2c.h>
0008 #include <linux/of_gpio.h>
0009 #include <linux/slab.h>
0010 #include <linux/gpio.h>
0011 #include <linux/clk.h>
0012 #include <sound/soc.h>
0013 #include <sound/jack.h>
0014 #include <sound/control.h>
0015 #include <sound/pcm_params.h>
0016 #include <sound/soc-dapm.h>
0017 #include "imx-pcm-rpmsg.h"
0018 
0019 struct imx_rpmsg {
0020     struct snd_soc_dai_link dai;
0021     struct snd_soc_card card;
0022 };
0023 
0024 static const struct snd_soc_dapm_widget imx_rpmsg_dapm_widgets[] = {
0025     SND_SOC_DAPM_HP("Headphone Jack", NULL),
0026     SND_SOC_DAPM_SPK("Ext Spk", NULL),
0027     SND_SOC_DAPM_MIC("Mic Jack", NULL),
0028     SND_SOC_DAPM_MIC("Main MIC", NULL),
0029 };
0030 
0031 static int imx_rpmsg_probe(struct platform_device *pdev)
0032 {
0033     struct snd_soc_dai_link_component *dlc;
0034     struct device *dev = pdev->dev.parent;
0035     /* rpmsg_pdev is the platform device for the rpmsg node that probed us */
0036     struct platform_device *rpmsg_pdev = to_platform_device(dev);
0037     struct device_node *np = rpmsg_pdev->dev.of_node;
0038     struct of_phandle_args args;
0039     struct imx_rpmsg *data;
0040     int ret = 0;
0041 
0042     dlc = devm_kzalloc(&pdev->dev, 3 * sizeof(*dlc), GFP_KERNEL);
0043     if (!dlc)
0044         return -ENOMEM;
0045 
0046     data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
0047     if (!data) {
0048         ret = -ENOMEM;
0049         goto fail;
0050     }
0051 
0052     ret = of_reserved_mem_device_init_by_idx(&pdev->dev, np, 0);
0053     if (ret)
0054         dev_warn(&pdev->dev, "no reserved DMA memory\n");
0055 
0056     data->dai.cpus = &dlc[0];
0057     data->dai.num_cpus = 1;
0058     data->dai.platforms = &dlc[1];
0059     data->dai.num_platforms = 1;
0060     data->dai.codecs = &dlc[2];
0061     data->dai.num_codecs = 1;
0062 
0063     data->dai.name = "rpmsg hifi";
0064     data->dai.stream_name = "rpmsg hifi";
0065     data->dai.dai_fmt = SND_SOC_DAIFMT_I2S |
0066                 SND_SOC_DAIFMT_NB_NF |
0067                 SND_SOC_DAIFMT_CBC_CFC;
0068 
0069     /* Optional codec node */
0070     ret = of_parse_phandle_with_fixed_args(np, "audio-codec", 0, 0, &args);
0071     if (ret) {
0072         data->dai.codecs->dai_name = "snd-soc-dummy-dai";
0073         data->dai.codecs->name = "snd-soc-dummy";
0074     } else {
0075         data->dai.codecs->of_node = args.np;
0076         ret = snd_soc_get_dai_name(&args, &data->dai.codecs->dai_name);
0077         if (ret) {
0078             dev_err(&pdev->dev, "Unable to get codec_dai_name\n");
0079             goto fail;
0080         }
0081     }
0082 
0083     data->dai.cpus->dai_name = dev_name(&rpmsg_pdev->dev);
0084     data->dai.platforms->name = IMX_PCM_DRV_NAME;
0085     data->dai.playback_only = true;
0086     data->dai.capture_only = true;
0087     data->card.num_links = 1;
0088     data->card.dai_link = &data->dai;
0089 
0090     if (of_property_read_bool(np, "fsl,rpmsg-out"))
0091         data->dai.capture_only = false;
0092 
0093     if (of_property_read_bool(np, "fsl,rpmsg-in"))
0094         data->dai.playback_only = false;
0095 
0096     if (data->dai.playback_only && data->dai.capture_only) {
0097         dev_err(&pdev->dev, "no enabled rpmsg DAI link\n");
0098         ret = -EINVAL;
0099         goto fail;
0100     }
0101 
0102     data->card.dev = &pdev->dev;
0103     data->card.owner = THIS_MODULE;
0104     data->card.dapm_widgets = imx_rpmsg_dapm_widgets;
0105     data->card.num_dapm_widgets = ARRAY_SIZE(imx_rpmsg_dapm_widgets);
0106     /*
0107      * Inoder to use common api to get card name and audio routing.
0108      * Use parent of_node for this device, revert it after finishing using
0109      */
0110     data->card.dev->of_node = np;
0111 
0112     ret = snd_soc_of_parse_card_name(&data->card, "model");
0113     if (ret)
0114         goto fail;
0115 
0116     if (of_property_read_bool(np, "audio-routing")) {
0117         ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
0118         if (ret) {
0119             dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
0120             goto fail;
0121         }
0122     }
0123 
0124     platform_set_drvdata(pdev, &data->card);
0125     snd_soc_card_set_drvdata(&data->card, data);
0126     ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
0127     if (ret) {
0128         dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
0129         goto fail;
0130     }
0131 
0132 fail:
0133     pdev->dev.of_node = NULL;
0134     return ret;
0135 }
0136 
0137 static struct platform_driver imx_rpmsg_driver = {
0138     .driver = {
0139         .name = "imx-audio-rpmsg",
0140         .pm = &snd_soc_pm_ops,
0141     },
0142     .probe = imx_rpmsg_probe,
0143 };
0144 module_platform_driver(imx_rpmsg_driver);
0145 
0146 MODULE_DESCRIPTION("Freescale SoC Audio RPMSG Machine Driver");
0147 MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>");
0148 MODULE_ALIAS("platform:imx-audio-rpmsg");
0149 MODULE_LICENSE("GPL v2");