xref: /openbmc/linux/sound/soc/fsl/imx-rpmsg.c (revision 240e6d25)
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 };
23 
24 static const struct snd_soc_dapm_widget imx_rpmsg_dapm_widgets[] = {
25 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
26 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
27 	SND_SOC_DAPM_MIC("Mic Jack", NULL),
28 	SND_SOC_DAPM_MIC("Main MIC", NULL),
29 };
30 
31 static int imx_rpmsg_probe(struct platform_device *pdev)
32 {
33 	struct snd_soc_dai_link_component *dlc;
34 	struct device *dev = pdev->dev.parent;
35 	/* rpmsg_pdev is the platform device for the rpmsg node that probed us */
36 	struct platform_device *rpmsg_pdev = to_platform_device(dev);
37 	struct device_node *np = rpmsg_pdev->dev.of_node;
38 	struct of_phandle_args args;
39 	struct imx_rpmsg *data;
40 	int ret = 0;
41 
42 	dlc = devm_kzalloc(&pdev->dev, 3 * sizeof(*dlc), GFP_KERNEL);
43 	if (!dlc)
44 		return -ENOMEM;
45 
46 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
47 	if (!data) {
48 		ret = -ENOMEM;
49 		goto fail;
50 	}
51 
52 	ret = of_reserved_mem_device_init_by_idx(&pdev->dev, np, 0);
53 	if (ret)
54 		dev_warn(&pdev->dev, "no reserved DMA memory\n");
55 
56 	data->dai.cpus = &dlc[0];
57 	data->dai.num_cpus = 1;
58 	data->dai.platforms = &dlc[1];
59 	data->dai.num_platforms = 1;
60 	data->dai.codecs = &dlc[2];
61 	data->dai.num_codecs = 1;
62 
63 	data->dai.name = "rpmsg hifi";
64 	data->dai.stream_name = "rpmsg hifi";
65 	data->dai.dai_fmt = SND_SOC_DAIFMT_I2S |
66 			    SND_SOC_DAIFMT_NB_NF |
67 			    SND_SOC_DAIFMT_CBC_CFC;
68 
69 	/* Optional codec node */
70 	ret = of_parse_phandle_with_fixed_args(np, "audio-codec", 0, 0, &args);
71 	if (ret) {
72 		data->dai.codecs->dai_name = "snd-soc-dummy-dai";
73 		data->dai.codecs->name = "snd-soc-dummy";
74 	} else {
75 		data->dai.codecs->of_node = args.np;
76 		ret = snd_soc_get_dai_name(&args, &data->dai.codecs->dai_name);
77 		if (ret) {
78 			dev_err(&pdev->dev, "Unable to get codec_dai_name\n");
79 			goto fail;
80 		}
81 	}
82 
83 	data->dai.cpus->dai_name = dev_name(&rpmsg_pdev->dev);
84 	data->dai.platforms->name = IMX_PCM_DRV_NAME;
85 	data->dai.playback_only = true;
86 	data->dai.capture_only = true;
87 	data->card.num_links = 1;
88 	data->card.dai_link = &data->dai;
89 
90 	if (of_property_read_bool(np, "fsl,rpmsg-out"))
91 		data->dai.capture_only = false;
92 
93 	if (of_property_read_bool(np, "fsl,rpmsg-in"))
94 		data->dai.playback_only = false;
95 
96 	if (data->dai.playback_only && data->dai.capture_only) {
97 		dev_err(&pdev->dev, "no enabled rpmsg DAI link\n");
98 		ret = -EINVAL;
99 		goto fail;
100 	}
101 
102 	data->card.dev = &pdev->dev;
103 	data->card.owner = THIS_MODULE;
104 	data->card.dapm_widgets = imx_rpmsg_dapm_widgets;
105 	data->card.num_dapm_widgets = ARRAY_SIZE(imx_rpmsg_dapm_widgets);
106 	/*
107 	 * Inoder to use common api to get card name and audio routing.
108 	 * Use parent of_node for this device, revert it after finishing using
109 	 */
110 	data->card.dev->of_node = np;
111 
112 	ret = snd_soc_of_parse_card_name(&data->card, "model");
113 	if (ret)
114 		goto fail;
115 
116 	if (of_property_read_bool(np, "audio-routing")) {
117 		ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
118 		if (ret) {
119 			dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
120 			goto fail;
121 		}
122 	}
123 
124 	platform_set_drvdata(pdev, &data->card);
125 	snd_soc_card_set_drvdata(&data->card, data);
126 	ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
127 	if (ret) {
128 		dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
129 		goto fail;
130 	}
131 
132 fail:
133 	pdev->dev.of_node = NULL;
134 	return ret;
135 }
136 
137 static struct platform_driver imx_rpmsg_driver = {
138 	.driver = {
139 		.name = "imx-audio-rpmsg",
140 		.pm = &snd_soc_pm_ops,
141 	},
142 	.probe = imx_rpmsg_probe,
143 };
144 module_platform_driver(imx_rpmsg_driver);
145 
146 MODULE_DESCRIPTION("Freescale SoC Audio RPMSG Machine Driver");
147 MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>");
148 MODULE_ALIAS("platform:imx-audio-rpmsg");
149 MODULE_LICENSE("GPL v2");
150