xref: /linux/sound/soc/fsl/imx-audmix.c (revision de70b2a581125a8a627db6fdc1d7f9c05536fe84)
1b86ef536SViorel Suman // SPDX-License-Identifier: GPL-2.0
2b86ef536SViorel Suman /*
3b86ef536SViorel Suman  * Copyright 2017 NXP
4b86ef536SViorel Suman  *
5b86ef536SViorel Suman  * The code contained herein is licensed under the GNU General Public
6b86ef536SViorel Suman  * License. You may obtain a copy of the GNU General Public License
7b86ef536SViorel Suman  * Version 2 or later at the following locations:
8b86ef536SViorel Suman  *
9b86ef536SViorel Suman  * http://www.opensource.org/licenses/gpl-license.html
10b86ef536SViorel Suman  * http://www.gnu.org/copyleft/gpl.html
11b86ef536SViorel Suman  */
12b86ef536SViorel Suman 
13b86ef536SViorel Suman #include <linux/module.h>
14b86ef536SViorel Suman #include <linux/of_platform.h>
15b86ef536SViorel Suman #include <linux/clk.h>
16b86ef536SViorel Suman #include <sound/soc.h>
17b86ef536SViorel Suman #include <sound/soc-dapm.h>
18b86ef536SViorel Suman #include <linux/pm_runtime.h>
19b86ef536SViorel Suman #include "fsl_sai.h"
20b86ef536SViorel Suman #include "fsl_audmix.h"
21b86ef536SViorel Suman 
22b86ef536SViorel Suman struct imx_audmix {
23b86ef536SViorel Suman 	struct platform_device *pdev;
24b86ef536SViorel Suman 	struct snd_soc_card card;
25b86ef536SViorel Suman 	struct platform_device *audmix_pdev;
26b86ef536SViorel Suman 	struct platform_device *out_pdev;
27b86ef536SViorel Suman 	struct clk *cpu_mclk;
28b86ef536SViorel Suman 	int num_dai;
29b86ef536SViorel Suman 	struct snd_soc_dai_link *dai;
30b86ef536SViorel Suman 	int num_dai_conf;
31b86ef536SViorel Suman 	struct snd_soc_codec_conf *dai_conf;
32b86ef536SViorel Suman 	int num_dapm_routes;
33b86ef536SViorel Suman 	struct snd_soc_dapm_route *dapm_routes;
34b86ef536SViorel Suman };
35b86ef536SViorel Suman 
36b86ef536SViorel Suman static const u32 imx_audmix_rates[] = {
37b86ef536SViorel Suman 	8000, 12000, 16000, 24000, 32000, 48000, 64000, 96000,
38b86ef536SViorel Suman };
39b86ef536SViorel Suman 
40b86ef536SViorel Suman static const struct snd_pcm_hw_constraint_list imx_audmix_rate_constraints = {
41b86ef536SViorel Suman 	.count = ARRAY_SIZE(imx_audmix_rates),
42b86ef536SViorel Suman 	.list = imx_audmix_rates,
43b86ef536SViorel Suman };
44b86ef536SViorel Suman 
45b86ef536SViorel Suman static int imx_audmix_fe_startup(struct snd_pcm_substream *substream)
46b86ef536SViorel Suman {
47b86ef536SViorel Suman 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
48b86ef536SViorel Suman 	struct imx_audmix *priv = snd_soc_card_get_drvdata(rtd->card);
49b86ef536SViorel Suman 	struct snd_pcm_runtime *runtime = substream->runtime;
50b86ef536SViorel Suman 	struct device *dev = rtd->card->dev;
51b86ef536SViorel Suman 	unsigned long clk_rate = clk_get_rate(priv->cpu_mclk);
52b86ef536SViorel Suman 	int ret;
53b86ef536SViorel Suman 
54b86ef536SViorel Suman 	if (clk_rate % 24576000 == 0) {
55b86ef536SViorel Suman 		ret = snd_pcm_hw_constraint_list(runtime, 0,
56b86ef536SViorel Suman 						 SNDRV_PCM_HW_PARAM_RATE,
57b86ef536SViorel Suman 						 &imx_audmix_rate_constraints);
58b86ef536SViorel Suman 		if (ret < 0)
59b86ef536SViorel Suman 			return ret;
60b86ef536SViorel Suman 	} else {
61b86ef536SViorel Suman 		dev_warn(dev, "mclk may be not supported %lu\n", clk_rate);
62b86ef536SViorel Suman 	}
63b86ef536SViorel Suman 
64b86ef536SViorel Suman 	ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
65b86ef536SViorel Suman 					   1, 8);
66b86ef536SViorel Suman 	if (ret < 0)
67b86ef536SViorel Suman 		return ret;
68b86ef536SViorel Suman 
69b86ef536SViorel Suman 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
70b86ef536SViorel Suman 					    FSL_AUDMIX_FORMATS);
71b86ef536SViorel Suman }
72b86ef536SViorel Suman 
73b86ef536SViorel Suman static int imx_audmix_fe_hw_params(struct snd_pcm_substream *substream,
74b86ef536SViorel Suman 				   struct snd_pcm_hw_params *params)
75b86ef536SViorel Suman {
76b86ef536SViorel Suman 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
77b86ef536SViorel Suman 	struct device *dev = rtd->card->dev;
78b86ef536SViorel Suman 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
79b86ef536SViorel Suman 	unsigned int fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF;
80b86ef536SViorel Suman 	u32 channels = params_channels(params);
81b86ef536SViorel Suman 	int ret, dir;
82b86ef536SViorel Suman 
83b86ef536SViorel Suman 	/* For playback the AUDMIX is slave, and for record is master */
84b86ef536SViorel Suman 	fmt |= tx ? SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBM_CFM;
85b86ef536SViorel Suman 	dir  = tx ? SND_SOC_CLOCK_OUT : SND_SOC_CLOCK_IN;
86b86ef536SViorel Suman 
87b86ef536SViorel Suman 	/* set DAI configuration */
88b86ef536SViorel Suman 	ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
89b86ef536SViorel Suman 	if (ret) {
90b86ef536SViorel Suman 		dev_err(dev, "failed to set cpu dai fmt: %d\n", ret);
91b86ef536SViorel Suman 		return ret;
92b86ef536SViorel Suman 	}
93b86ef536SViorel Suman 
94b86ef536SViorel Suman 	ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, FSL_SAI_CLK_MAST1, 0, dir);
95b86ef536SViorel Suman 	if (ret) {
96b86ef536SViorel Suman 		dev_err(dev, "failed to set cpu sysclk: %d\n", ret);
97b86ef536SViorel Suman 		return ret;
98b86ef536SViorel Suman 	}
99b86ef536SViorel Suman 
100b86ef536SViorel Suman 	/*
101b86ef536SViorel Suman 	 * Per datasheet, AUDMIX expects 8 slots and 32 bits
102b86ef536SViorel Suman 	 * for every slot in TDM mode.
103b86ef536SViorel Suman 	 */
104b86ef536SViorel Suman 	ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, BIT(channels) - 1,
105b86ef536SViorel Suman 				       BIT(channels) - 1, 8, 32);
106b86ef536SViorel Suman 	if (ret)
107b86ef536SViorel Suman 		dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret);
108b86ef536SViorel Suman 
109b86ef536SViorel Suman 	return ret;
110b86ef536SViorel Suman }
111b86ef536SViorel Suman 
112b86ef536SViorel Suman static int imx_audmix_be_hw_params(struct snd_pcm_substream *substream,
113b86ef536SViorel Suman 				   struct snd_pcm_hw_params *params)
114b86ef536SViorel Suman {
115b86ef536SViorel Suman 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
116b86ef536SViorel Suman 	struct device *dev = rtd->card->dev;
117b86ef536SViorel Suman 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
118b86ef536SViorel Suman 	unsigned int fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF;
119b86ef536SViorel Suman 	int ret;
120b86ef536SViorel Suman 
121b86ef536SViorel Suman 	if (!tx)
122b86ef536SViorel Suman 		return 0;
123b86ef536SViorel Suman 
124b86ef536SViorel Suman 	/* For playback the AUDMIX is slave */
125b86ef536SViorel Suman 	fmt |= SND_SOC_DAIFMT_CBM_CFM;
126b86ef536SViorel Suman 
127b86ef536SViorel Suman 	/* set AUDMIX DAI configuration */
128b86ef536SViorel Suman 	ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
129b86ef536SViorel Suman 	if (ret)
130b86ef536SViorel Suman 		dev_err(dev, "failed to set AUDMIX DAI fmt: %d\n", ret);
131b86ef536SViorel Suman 
132b86ef536SViorel Suman 	return ret;
133b86ef536SViorel Suman }
134b86ef536SViorel Suman 
135b86ef536SViorel Suman static struct snd_soc_ops imx_audmix_fe_ops = {
136b86ef536SViorel Suman 	.startup = imx_audmix_fe_startup,
137b86ef536SViorel Suman 	.hw_params = imx_audmix_fe_hw_params,
138b86ef536SViorel Suman };
139b86ef536SViorel Suman 
140b86ef536SViorel Suman static struct snd_soc_ops imx_audmix_be_ops = {
141b86ef536SViorel Suman 	.hw_params = imx_audmix_be_hw_params,
142b86ef536SViorel Suman };
143b86ef536SViorel Suman 
144b86ef536SViorel Suman static int imx_audmix_probe(struct platform_device *pdev)
145b86ef536SViorel Suman {
146b86ef536SViorel Suman 	struct device_node *np = pdev->dev.of_node;
147b86ef536SViorel Suman 	struct device_node *audmix_np = NULL, *out_cpu_np = NULL;
148b86ef536SViorel Suman 	struct platform_device *audmix_pdev = NULL;
149b86ef536SViorel Suman 	struct platform_device *cpu_pdev;
150b86ef536SViorel Suman 	struct of_phandle_args args;
151b86ef536SViorel Suman 	struct imx_audmix *priv;
152b86ef536SViorel Suman 	int i, num_dai, ret;
153b86ef536SViorel Suman 	const char *fe_name_pref = "HiFi-AUDMIX-FE-";
154b86ef536SViorel Suman 	char *be_name, *be_pb, *be_cp, *dai_name, *capture_dai_name;
155b86ef536SViorel Suman 
156b86ef536SViorel Suman 	if (pdev->dev.parent) {
157b86ef536SViorel Suman 		audmix_np = pdev->dev.parent->of_node;
158b86ef536SViorel Suman 	} else {
159b86ef536SViorel Suman 		dev_err(&pdev->dev, "Missing parent device.\n");
160b86ef536SViorel Suman 		return -EINVAL;
161b86ef536SViorel Suman 	}
162b86ef536SViorel Suman 
163b86ef536SViorel Suman 	if (!audmix_np) {
164*de70b2a5SColin Ian King 		dev_err(&pdev->dev, "Missing DT node for parent device.\n");
165b86ef536SViorel Suman 		return -EINVAL;
166b86ef536SViorel Suman 	}
167b86ef536SViorel Suman 
168b86ef536SViorel Suman 	audmix_pdev = of_find_device_by_node(audmix_np);
169b86ef536SViorel Suman 	if (!audmix_pdev) {
170b86ef536SViorel Suman 		dev_err(&pdev->dev, "Missing AUDMIX platform device for %s\n",
171b86ef536SViorel Suman 			np->full_name);
172b86ef536SViorel Suman 		return -EINVAL;
173b86ef536SViorel Suman 	}
174b86ef536SViorel Suman 
175b86ef536SViorel Suman 	num_dai = of_count_phandle_with_args(audmix_np, "dais", NULL);
176b86ef536SViorel Suman 	if (num_dai != FSL_AUDMIX_MAX_DAIS) {
177b86ef536SViorel Suman 		dev_err(&pdev->dev, "Need 2 dais to be provided for %s\n",
178b86ef536SViorel Suman 			audmix_np->full_name);
179b86ef536SViorel Suman 		return -EINVAL;
180b86ef536SViorel Suman 	}
181b86ef536SViorel Suman 
182b86ef536SViorel Suman 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
183b86ef536SViorel Suman 	if (!priv)
184b86ef536SViorel Suman 		return -ENOMEM;
185b86ef536SViorel Suman 
186b86ef536SViorel Suman 	priv->num_dai = 2 * num_dai;
187b86ef536SViorel Suman 	priv->dai = devm_kzalloc(&pdev->dev, priv->num_dai *
188b86ef536SViorel Suman 				 sizeof(struct snd_soc_dai_link), GFP_KERNEL);
189b86ef536SViorel Suman 	if (!priv->dai)
190b86ef536SViorel Suman 		return -ENOMEM;
191b86ef536SViorel Suman 
192b86ef536SViorel Suman 	priv->num_dai_conf = num_dai;
193b86ef536SViorel Suman 	priv->dai_conf = devm_kzalloc(&pdev->dev, priv->num_dai_conf *
194b86ef536SViorel Suman 				      sizeof(struct snd_soc_codec_conf),
195b86ef536SViorel Suman 				      GFP_KERNEL);
196b86ef536SViorel Suman 	if (!priv->dai_conf)
197b86ef536SViorel Suman 		return -ENOMEM;
198b86ef536SViorel Suman 
199b86ef536SViorel Suman 	priv->num_dapm_routes = 3 * num_dai;
200b86ef536SViorel Suman 	priv->dapm_routes = devm_kzalloc(&pdev->dev, priv->num_dapm_routes *
201b86ef536SViorel Suman 					 sizeof(struct snd_soc_dapm_route),
202b86ef536SViorel Suman 					 GFP_KERNEL);
203b86ef536SViorel Suman 	if (!priv->dapm_routes)
204b86ef536SViorel Suman 		return -ENOMEM;
205b86ef536SViorel Suman 
206b86ef536SViorel Suman 	for (i = 0; i < num_dai; i++) {
207b86ef536SViorel Suman 		ret = of_parse_phandle_with_args(audmix_np, "dais", NULL, i,
208b86ef536SViorel Suman 						 &args);
209b86ef536SViorel Suman 		if (ret < 0) {
210b86ef536SViorel Suman 			dev_err(&pdev->dev, "of_parse_phandle_with_args failed\n");
211b86ef536SViorel Suman 			return ret;
212b86ef536SViorel Suman 		}
213b86ef536SViorel Suman 
214b86ef536SViorel Suman 		cpu_pdev = of_find_device_by_node(args.np);
215b86ef536SViorel Suman 		if (!cpu_pdev) {
216b86ef536SViorel Suman 			dev_err(&pdev->dev, "failed to find SAI platform device\n");
217b86ef536SViorel Suman 			return -EINVAL;
218b86ef536SViorel Suman 		}
219b86ef536SViorel Suman 
220b86ef536SViorel Suman 		dai_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s%s",
221b86ef536SViorel Suman 					  fe_name_pref, args.np->full_name + 1);
222b86ef536SViorel Suman 
223b86ef536SViorel Suman 		dev_info(pdev->dev.parent, "DAI FE name:%s\n", dai_name);
224b86ef536SViorel Suman 
225b86ef536SViorel Suman 		if (i == 0) {
226b86ef536SViorel Suman 			out_cpu_np = args.np;
227b86ef536SViorel Suman 			capture_dai_name =
228b86ef536SViorel Suman 				devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s %s",
229b86ef536SViorel Suman 					       dai_name, "CPU-Capture");
230b86ef536SViorel Suman 		}
231b86ef536SViorel Suman 
232b86ef536SViorel Suman 		priv->dai[i].name = dai_name;
233b86ef536SViorel Suman 		priv->dai[i].stream_name = "HiFi-AUDMIX-FE";
234b86ef536SViorel Suman 		priv->dai[i].codec_dai_name = "snd-soc-dummy-dai";
235b86ef536SViorel Suman 		priv->dai[i].codec_name = "snd-soc-dummy";
236b86ef536SViorel Suman 		priv->dai[i].cpu_of_node = args.np;
237b86ef536SViorel Suman 		priv->dai[i].cpu_dai_name = dev_name(&cpu_pdev->dev);
238b86ef536SViorel Suman 		priv->dai[i].platform_of_node = args.np;
239b86ef536SViorel Suman 		priv->dai[i].dynamic = 1;
240b86ef536SViorel Suman 		priv->dai[i].dpcm_playback = 1;
241b86ef536SViorel Suman 		priv->dai[i].dpcm_capture = (i == 0 ? 1 : 0);
242b86ef536SViorel Suman 		priv->dai[i].ignore_pmdown_time = 1;
243b86ef536SViorel Suman 		priv->dai[i].ops = &imx_audmix_fe_ops;
244b86ef536SViorel Suman 
245b86ef536SViorel Suman 		/* Add AUDMIX Backend */
246b86ef536SViorel Suman 		be_name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
247b86ef536SViorel Suman 					 "audmix-%d", i);
248b86ef536SViorel Suman 		be_pb = devm_kasprintf(&pdev->dev, GFP_KERNEL,
249b86ef536SViorel Suman 				       "AUDMIX-Playback-%d", i);
250b86ef536SViorel Suman 		be_cp = devm_kasprintf(&pdev->dev, GFP_KERNEL,
251b86ef536SViorel Suman 				       "AUDMIX-Capture-%d", i);
252b86ef536SViorel Suman 
253b86ef536SViorel Suman 		priv->dai[num_dai + i].name = be_name;
254b86ef536SViorel Suman 		priv->dai[num_dai + i].codec_dai_name = "snd-soc-dummy-dai";
255b86ef536SViorel Suman 		priv->dai[num_dai + i].codec_name = "snd-soc-dummy";
256b86ef536SViorel Suman 		priv->dai[num_dai + i].cpu_of_node = audmix_np;
257b86ef536SViorel Suman 		priv->dai[num_dai + i].cpu_dai_name = be_name;
258b86ef536SViorel Suman 		priv->dai[num_dai + i].platform_name = "snd-soc-dummy";
259b86ef536SViorel Suman 		priv->dai[num_dai + i].no_pcm = 1;
260b86ef536SViorel Suman 		priv->dai[num_dai + i].dpcm_playback = 1;
261b86ef536SViorel Suman 		priv->dai[num_dai + i].dpcm_capture  = 1;
262b86ef536SViorel Suman 		priv->dai[num_dai + i].ignore_pmdown_time = 1;
263b86ef536SViorel Suman 		priv->dai[num_dai + i].ops = &imx_audmix_be_ops;
264b86ef536SViorel Suman 
265b86ef536SViorel Suman 		priv->dai_conf[i].of_node = args.np;
266b86ef536SViorel Suman 		priv->dai_conf[i].name_prefix = dai_name;
267b86ef536SViorel Suman 
268b86ef536SViorel Suman 		priv->dapm_routes[i].source =
269b86ef536SViorel Suman 			devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s %s",
270b86ef536SViorel Suman 				       dai_name, "CPU-Playback");
271b86ef536SViorel Suman 		priv->dapm_routes[i].sink = be_pb;
272b86ef536SViorel Suman 		priv->dapm_routes[num_dai + i].source   = be_pb;
273b86ef536SViorel Suman 		priv->dapm_routes[num_dai + i].sink     = be_cp;
274b86ef536SViorel Suman 		priv->dapm_routes[2 * num_dai + i].source = be_cp;
275b86ef536SViorel Suman 		priv->dapm_routes[2 * num_dai + i].sink   = capture_dai_name;
276b86ef536SViorel Suman 	}
277b86ef536SViorel Suman 
278b86ef536SViorel Suman 	cpu_pdev = of_find_device_by_node(out_cpu_np);
279b86ef536SViorel Suman 	if (!cpu_pdev) {
280b86ef536SViorel Suman 		dev_err(&pdev->dev, "failed to find SAI platform device\n");
281b86ef536SViorel Suman 		return -EINVAL;
282b86ef536SViorel Suman 	}
283b86ef536SViorel Suman 	priv->cpu_mclk = devm_clk_get(&cpu_pdev->dev, "mclk1");
284b86ef536SViorel Suman 	if (IS_ERR(priv->cpu_mclk)) {
285b86ef536SViorel Suman 		ret = PTR_ERR(priv->cpu_mclk);
286b86ef536SViorel Suman 		dev_err(&cpu_pdev->dev, "failed to get DAI mclk1: %d\n", ret);
287b86ef536SViorel Suman 		return -EINVAL;
288b86ef536SViorel Suman 	}
289b86ef536SViorel Suman 
290b86ef536SViorel Suman 	priv->audmix_pdev = audmix_pdev;
291b86ef536SViorel Suman 	priv->out_pdev  = cpu_pdev;
292b86ef536SViorel Suman 
293b86ef536SViorel Suman 	priv->card.dai_link = priv->dai;
294b86ef536SViorel Suman 	priv->card.num_links = priv->num_dai;
295b86ef536SViorel Suman 	priv->card.codec_conf = priv->dai_conf;
296b86ef536SViorel Suman 	priv->card.num_configs = priv->num_dai_conf;
297b86ef536SViorel Suman 	priv->card.dapm_routes = priv->dapm_routes;
298b86ef536SViorel Suman 	priv->card.num_dapm_routes = priv->num_dapm_routes;
299b86ef536SViorel Suman 	priv->card.dev = pdev->dev.parent;
300b86ef536SViorel Suman 	priv->card.owner = THIS_MODULE;
301b86ef536SViorel Suman 	priv->card.name = "imx-audmix";
302b86ef536SViorel Suman 
303b86ef536SViorel Suman 	platform_set_drvdata(pdev, &priv->card);
304b86ef536SViorel Suman 	snd_soc_card_set_drvdata(&priv->card, priv);
305b86ef536SViorel Suman 
306b86ef536SViorel Suman 	ret = devm_snd_soc_register_card(pdev->dev.parent, &priv->card);
307b86ef536SViorel Suman 	if (ret) {
308b86ef536SViorel Suman 		dev_err(&pdev->dev, "snd_soc_register_card failed\n");
309b86ef536SViorel Suman 		return ret;
310b86ef536SViorel Suman 	}
311b86ef536SViorel Suman 
312b86ef536SViorel Suman 	return ret;
313b86ef536SViorel Suman }
314b86ef536SViorel Suman 
315b86ef536SViorel Suman static struct platform_driver imx_audmix_driver = {
316b86ef536SViorel Suman 	.probe = imx_audmix_probe,
317b86ef536SViorel Suman 	.driver = {
318b86ef536SViorel Suman 		.name = "imx-audmix",
319b86ef536SViorel Suman 		.pm = &snd_soc_pm_ops,
320b86ef536SViorel Suman 	},
321b86ef536SViorel Suman };
322b86ef536SViorel Suman module_platform_driver(imx_audmix_driver);
323b86ef536SViorel Suman 
324b86ef536SViorel Suman MODULE_DESCRIPTION("NXP AUDMIX ASoC machine driver");
325b86ef536SViorel Suman MODULE_AUTHOR("Viorel Suman <viorel.suman@nxp.com>");
326b86ef536SViorel Suman MODULE_ALIAS("platform:imx-audmix");
327b86ef536SViorel Suman MODULE_LICENSE("GPL v2");
328