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