xref: /linux/sound/soc/fsl/fsl_rpmsg.c (revision 505d66d1abfb90853e24ab6cbdf83b611473d6fc)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2018-2021 NXP
3 
4 #include <linux/clk.h>
5 #include <linux/clk-provider.h>
6 #include <linux/delay.h>
7 #include <linux/dmaengine.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/rpmsg.h>
12 #include <linux/slab.h>
13 #include <sound/core.h>
14 #include <sound/dmaengine_pcm.h>
15 #include <sound/pcm_params.h>
16 
17 #include "fsl_rpmsg.h"
18 #include "imx-pcm.h"
19 
20 #define FSL_RPMSG_RATES        (SNDRV_PCM_RATE_8000 | \
21 				SNDRV_PCM_RATE_16000 | \
22 				SNDRV_PCM_RATE_48000)
23 #define FSL_RPMSG_FORMATS	SNDRV_PCM_FMTBIT_S16_LE
24 
25 /* 192kHz/32bit/2ch/60s size is 0x574e00 */
26 #define LPA_LARGE_BUFFER_SIZE  (0x6000000)
27 
28 static const unsigned int fsl_rpmsg_rates[] = {
29 	8000, 11025, 16000, 22050, 44100,
30 	32000, 48000, 96000, 88200, 176400, 192000,
31 	352800, 384000, 705600, 768000, 1411200, 2822400,
32 };
33 
34 static const struct snd_pcm_hw_constraint_list fsl_rpmsg_rate_constraints = {
35 	.count = ARRAY_SIZE(fsl_rpmsg_rates),
36 	.list = fsl_rpmsg_rates,
37 };
38 
39 static int fsl_rpmsg_hw_params(struct snd_pcm_substream *substream,
40 			       struct snd_pcm_hw_params *params,
41 			       struct snd_soc_dai *dai)
42 {
43 	struct fsl_rpmsg *rpmsg = snd_soc_dai_get_drvdata(dai);
44 	struct clk *p = rpmsg->mclk, *pll = NULL, *npll = NULL;
45 	u64 rate = params_rate(params);
46 	int ret = 0;
47 
48 	/* Get current pll parent */
49 	while (p && rpmsg->pll8k && rpmsg->pll11k) {
50 		struct clk *pp = clk_get_parent(p);
51 
52 		if (clk_is_match(pp, rpmsg->pll8k) ||
53 		    clk_is_match(pp, rpmsg->pll11k)) {
54 			pll = pp;
55 			break;
56 		}
57 		p = pp;
58 	}
59 
60 	/* Switch to another pll parent if needed. */
61 	if (pll) {
62 		npll = (do_div(rate, 8000) ? rpmsg->pll11k : rpmsg->pll8k);
63 		if (!clk_is_match(pll, npll)) {
64 			ret = clk_set_parent(p, npll);
65 			if (ret < 0)
66 				dev_warn(dai->dev, "failed to set parent %s: %d\n",
67 					 __clk_get_name(npll), ret);
68 		}
69 	}
70 
71 	if (!(rpmsg->mclk_streams & BIT(substream->stream))) {
72 		ret = clk_prepare_enable(rpmsg->mclk);
73 		if (ret) {
74 			dev_err(dai->dev, "failed to enable mclk: %d\n", ret);
75 			return ret;
76 		}
77 
78 		rpmsg->mclk_streams |= BIT(substream->stream);
79 	}
80 
81 	return ret;
82 }
83 
84 static int fsl_rpmsg_hw_free(struct snd_pcm_substream *substream,
85 			     struct snd_soc_dai *dai)
86 {
87 	struct fsl_rpmsg *rpmsg = snd_soc_dai_get_drvdata(dai);
88 
89 	if (rpmsg->mclk_streams & BIT(substream->stream)) {
90 		clk_disable_unprepare(rpmsg->mclk);
91 		rpmsg->mclk_streams &= ~BIT(substream->stream);
92 	}
93 
94 	return 0;
95 }
96 
97 static int fsl_rpmsg_startup(struct snd_pcm_substream *substream,
98 			     struct snd_soc_dai *cpu_dai)
99 {
100 	int ret;
101 
102 	ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
103 					 SNDRV_PCM_HW_PARAM_RATE,
104 					 &fsl_rpmsg_rate_constraints);
105 
106 	return ret;
107 }
108 
109 static const struct snd_soc_dai_ops fsl_rpmsg_dai_ops = {
110 	.startup	= fsl_rpmsg_startup,
111 	.hw_params      = fsl_rpmsg_hw_params,
112 	.hw_free        = fsl_rpmsg_hw_free,
113 };
114 
115 static struct snd_soc_dai_driver fsl_rpmsg_dai = {
116 	.playback = {
117 		.stream_name = "CPU-Playback",
118 		.channels_min = 2,
119 		.channels_max = 32,
120 		.rates = SNDRV_PCM_RATE_KNOT,
121 		.formats = FSL_RPMSG_FORMATS,
122 	},
123 	.capture = {
124 		.stream_name = "CPU-Capture",
125 		.channels_min = 2,
126 		.channels_max = 32,
127 		.rates = SNDRV_PCM_RATE_KNOT,
128 		.formats = FSL_RPMSG_FORMATS,
129 	},
130 	.symmetric_rate        = 1,
131 	.symmetric_channels    = 1,
132 	.symmetric_sample_bits = 1,
133 	.ops = &fsl_rpmsg_dai_ops,
134 };
135 
136 static const struct snd_soc_component_driver fsl_component = {
137 	.name			= "fsl-rpmsg",
138 };
139 
140 static const struct fsl_rpmsg_soc_data imx7ulp_data = {
141 	.rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
142 		 SNDRV_PCM_RATE_48000,
143 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
144 };
145 
146 static const struct fsl_rpmsg_soc_data imx8mm_data = {
147 	.rates = SNDRV_PCM_RATE_KNOT,
148 	.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
149 		   SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_DSD_U8 |
150 		   SNDRV_PCM_FMTBIT_DSD_U16_LE | SNDRV_PCM_FMTBIT_DSD_U32_LE,
151 };
152 
153 static const struct fsl_rpmsg_soc_data imx8mn_data = {
154 	.rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
155 		 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
156 		 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
157 		 SNDRV_PCM_RATE_192000,
158 	.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
159 		   SNDRV_PCM_FMTBIT_S32_LE,
160 };
161 
162 static const struct fsl_rpmsg_soc_data imx8mp_data = {
163 	.rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
164 		 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
165 		 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
166 		 SNDRV_PCM_RATE_192000,
167 	.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
168 		   SNDRV_PCM_FMTBIT_S32_LE,
169 };
170 
171 static const struct fsl_rpmsg_soc_data imx93_data = {
172 	.rates = SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_32000 |
173 		 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000,
174 	.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
175 		   SNDRV_PCM_FMTBIT_S32_LE,
176 };
177 
178 static const struct of_device_id fsl_rpmsg_ids[] = {
179 	{ .compatible = "fsl,imx7ulp-rpmsg-audio", .data = &imx7ulp_data},
180 	{ .compatible = "fsl,imx8mm-rpmsg-audio", .data = &imx8mm_data},
181 	{ .compatible = "fsl,imx8mn-rpmsg-audio", .data = &imx8mn_data},
182 	{ .compatible = "fsl,imx8mp-rpmsg-audio", .data = &imx8mp_data},
183 	{ .compatible = "fsl,imx8ulp-rpmsg-audio", .data = &imx7ulp_data},
184 	{ .compatible = "fsl,imx93-rpmsg-audio", .data = &imx93_data},
185 	{ /* sentinel */ }
186 };
187 MODULE_DEVICE_TABLE(of, fsl_rpmsg_ids);
188 
189 static int fsl_rpmsg_probe(struct platform_device *pdev)
190 {
191 	struct device_node *np = pdev->dev.of_node;
192 	struct snd_soc_dai_driver *dai_drv;
193 	const char *dai_name;
194 	struct fsl_rpmsg *rpmsg;
195 	int ret;
196 
197 	dai_drv = devm_kzalloc(&pdev->dev, sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
198 	if (!dai_drv)
199 		return -ENOMEM;
200 	memcpy(dai_drv, &fsl_rpmsg_dai, sizeof(fsl_rpmsg_dai));
201 
202 	rpmsg = devm_kzalloc(&pdev->dev, sizeof(struct fsl_rpmsg), GFP_KERNEL);
203 	if (!rpmsg)
204 		return -ENOMEM;
205 
206 	rpmsg->soc_data = of_device_get_match_data(&pdev->dev);
207 
208 	if (rpmsg->soc_data) {
209 		dai_drv->playback.rates = rpmsg->soc_data->rates;
210 		dai_drv->capture.rates = rpmsg->soc_data->rates;
211 		dai_drv->playback.formats = rpmsg->soc_data->formats;
212 		dai_drv->capture.formats = rpmsg->soc_data->formats;
213 	}
214 
215 	/* Use rpmsg channel name as cpu dai name */
216 	ret = of_property_read_string(np, "fsl,rpmsg-channel-name", &dai_name);
217 	if (ret) {
218 		if (ret == -EINVAL) {
219 			dai_name = "rpmsg-audio-channel";
220 		} else {
221 			dev_err(&pdev->dev, "Failed to get rpmsg channel name: %d!\n", ret);
222 			return ret;
223 		}
224 	}
225 	dai_drv->name = dai_name;
226 
227 	if (of_property_read_bool(np, "fsl,enable-lpa")) {
228 		rpmsg->enable_lpa = 1;
229 		rpmsg->buffer_size = LPA_LARGE_BUFFER_SIZE;
230 	} else {
231 		rpmsg->buffer_size = IMX_DEFAULT_DMABUF_SIZE;
232 	}
233 
234 	/* Get the optional clocks */
235 	rpmsg->ipg = devm_clk_get_optional(&pdev->dev, "ipg");
236 	if (IS_ERR(rpmsg->ipg))
237 		return PTR_ERR(rpmsg->ipg);
238 
239 	rpmsg->mclk = devm_clk_get_optional(&pdev->dev, "mclk");
240 	if (IS_ERR(rpmsg->mclk))
241 		return PTR_ERR(rpmsg->mclk);
242 
243 	rpmsg->dma = devm_clk_get_optional(&pdev->dev, "dma");
244 	if (IS_ERR(rpmsg->dma))
245 		return PTR_ERR(rpmsg->dma);
246 
247 	rpmsg->pll8k = devm_clk_get_optional(&pdev->dev, "pll8k");
248 	if (IS_ERR(rpmsg->pll8k))
249 		return PTR_ERR(rpmsg->pll8k);
250 
251 	rpmsg->pll11k = devm_clk_get_optional(&pdev->dev, "pll11k");
252 	if (IS_ERR(rpmsg->pll11k))
253 		return PTR_ERR(rpmsg->pll11k);
254 
255 	platform_set_drvdata(pdev, rpmsg);
256 	pm_runtime_enable(&pdev->dev);
257 
258 	ret = devm_snd_soc_register_component(&pdev->dev, &fsl_component,
259 					      dai_drv, 1);
260 	if (ret)
261 		goto err_pm_disable;
262 
263 	return 0;
264 
265 err_pm_disable:
266 	pm_runtime_disable(&pdev->dev);
267 	return ret;
268 }
269 
270 static void fsl_rpmsg_remove(struct platform_device *pdev)
271 {
272 	struct fsl_rpmsg *rpmsg = platform_get_drvdata(pdev);
273 
274 	pm_runtime_disable(&pdev->dev);
275 
276 	if (rpmsg->card_pdev)
277 		platform_device_unregister(rpmsg->card_pdev);
278 }
279 
280 #ifdef CONFIG_PM
281 static int fsl_rpmsg_runtime_resume(struct device *dev)
282 {
283 	struct fsl_rpmsg *rpmsg = dev_get_drvdata(dev);
284 	int ret;
285 
286 	ret = clk_prepare_enable(rpmsg->ipg);
287 	if (ret) {
288 		dev_err(dev, "failed to enable ipg clock: %d\n", ret);
289 		goto ipg_err;
290 	}
291 
292 	ret = clk_prepare_enable(rpmsg->dma);
293 	if (ret) {
294 		dev_err(dev, "Failed to enable dma clock %d\n", ret);
295 		goto dma_err;
296 	}
297 
298 	return 0;
299 
300 dma_err:
301 	clk_disable_unprepare(rpmsg->ipg);
302 ipg_err:
303 	return ret;
304 }
305 
306 static int fsl_rpmsg_runtime_suspend(struct device *dev)
307 {
308 	struct fsl_rpmsg *rpmsg = dev_get_drvdata(dev);
309 
310 	clk_disable_unprepare(rpmsg->dma);
311 	clk_disable_unprepare(rpmsg->ipg);
312 
313 	return 0;
314 }
315 #endif
316 
317 static const struct dev_pm_ops fsl_rpmsg_pm_ops = {
318 	SET_RUNTIME_PM_OPS(fsl_rpmsg_runtime_suspend,
319 			   fsl_rpmsg_runtime_resume,
320 			   NULL)
321 };
322 
323 static struct platform_driver fsl_rpmsg_driver = {
324 	.probe  = fsl_rpmsg_probe,
325 	.remove_new = fsl_rpmsg_remove,
326 	.driver = {
327 		.name = "fsl_rpmsg",
328 		.pm = &fsl_rpmsg_pm_ops,
329 		.of_match_table = fsl_rpmsg_ids,
330 	},
331 };
332 module_platform_driver(fsl_rpmsg_driver);
333 
334 MODULE_DESCRIPTION("Freescale SoC Audio PRMSG CPU Interface");
335 MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>");
336 MODULE_ALIAS("platform:fsl_rpmsg");
337 MODULE_LICENSE("GPL");
338