xref: /openbmc/linux/sound/soc/sof/pcm.c (revision 3dc0d709177828a22dfc9d0072e3ac937ef90d06)
1e149ca29SPierre-Louis Bossart // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2868bd00fSLiam Girdwood //
3868bd00fSLiam Girdwood // This file is provided under a dual BSD/GPLv2 license.  When using or
4868bd00fSLiam Girdwood // redistributing this file, you may do so under either license.
5868bd00fSLiam Girdwood //
6868bd00fSLiam Girdwood // Copyright(c) 2018 Intel Corporation. All rights reserved.
7868bd00fSLiam Girdwood //
8868bd00fSLiam Girdwood // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9868bd00fSLiam Girdwood //
10868bd00fSLiam Girdwood // PCM Layer, interface between ALSA and IPC.
11868bd00fSLiam Girdwood //
12868bd00fSLiam Girdwood 
13868bd00fSLiam Girdwood #include <linux/pm_runtime.h>
14868bd00fSLiam Girdwood #include <sound/pcm_params.h>
15868bd00fSLiam Girdwood #include <sound/sof.h>
16868bd00fSLiam Girdwood #include "sof-priv.h"
17ee1e79b7SRanjani Sridharan #include "sof-audio.h"
18ee844305SPeter Ujfalusi #include "sof-utils.h"
19*3dc0d709SPeter Ujfalusi #include "ops.h"
20868bd00fSLiam Girdwood 
21868bd00fSLiam Girdwood /* Create DMA buffer page table for DSP */
221c91d77eSKuninori Morimoto static int create_page_table(struct snd_soc_component *component,
231c91d77eSKuninori Morimoto 			     struct snd_pcm_substream *substream,
24868bd00fSLiam Girdwood 			     unsigned char *dma_area, size_t size)
25868bd00fSLiam Girdwood {
261205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
27868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
28868bd00fSLiam Girdwood 	struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
29868bd00fSLiam Girdwood 	int stream = substream->stream;
30868bd00fSLiam Girdwood 
31ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
32868bd00fSLiam Girdwood 	if (!spcm)
33868bd00fSLiam Girdwood 		return -EINVAL;
34868bd00fSLiam Girdwood 
35ee1e79b7SRanjani Sridharan 	return snd_sof_create_page_table(component->dev, dmab,
36868bd00fSLiam Girdwood 		spcm->stream[stream].page_table.area, size);
37868bd00fSLiam Girdwood }
38868bd00fSLiam Girdwood 
39868bd00fSLiam Girdwood static int sof_pcm_dsp_params(struct snd_sof_pcm *spcm, struct snd_pcm_substream *substream,
40868bd00fSLiam Girdwood 			      const struct sof_ipc_pcm_params_reply *reply)
41868bd00fSLiam Girdwood {
42ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = spcm->scomp;
43ee1e79b7SRanjani Sridharan 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
44ee1e79b7SRanjani Sridharan 
45868bd00fSLiam Girdwood 	/* validate offset */
46868bd00fSLiam Girdwood 	int ret = snd_sof_ipc_pcm_params(sdev, substream, reply);
47868bd00fSLiam Girdwood 
48868bd00fSLiam Girdwood 	if (ret < 0)
49ee1e79b7SRanjani Sridharan 		dev_err(scomp->dev, "error: got wrong reply for PCM %d\n",
50868bd00fSLiam Girdwood 			spcm->pcm.pcm_id);
51868bd00fSLiam Girdwood 
52868bd00fSLiam Girdwood 	return ret;
53868bd00fSLiam Girdwood }
54868bd00fSLiam Girdwood 
55e2803e61SKeyon Jie /*
56e2803e61SKeyon Jie  * sof pcm period elapse work
57e2803e61SKeyon Jie  */
58858f7a5cSDaniel Baluta static void snd_sof_pcm_period_elapsed_work(struct work_struct *work)
59e2803e61SKeyon Jie {
60e2803e61SKeyon Jie 	struct snd_sof_pcm_stream *sps =
61e2803e61SKeyon Jie 		container_of(work, struct snd_sof_pcm_stream,
62e2803e61SKeyon Jie 			     period_elapsed_work);
63e2803e61SKeyon Jie 
64e2803e61SKeyon Jie 	snd_pcm_period_elapsed(sps->substream);
65e2803e61SKeyon Jie }
66e2803e61SKeyon Jie 
67858f7a5cSDaniel Baluta void snd_sof_pcm_init_elapsed_work(struct work_struct *work)
68858f7a5cSDaniel Baluta {
69858f7a5cSDaniel Baluta 	 INIT_WORK(work, snd_sof_pcm_period_elapsed_work);
70858f7a5cSDaniel Baluta }
71858f7a5cSDaniel Baluta 
72e2803e61SKeyon Jie /*
73e2803e61SKeyon Jie  * sof pcm period elapse, this could be called at irq thread context.
74e2803e61SKeyon Jie  */
75e2803e61SKeyon Jie void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
76e2803e61SKeyon Jie {
771205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
78e2803e61SKeyon Jie 	struct snd_soc_component *component =
79ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
80e2803e61SKeyon Jie 	struct snd_sof_pcm *spcm;
81e2803e61SKeyon Jie 
82ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
83e2803e61SKeyon Jie 	if (!spcm) {
84ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
85e2803e61SKeyon Jie 			"error: period elapsed for unknown stream!\n");
86e2803e61SKeyon Jie 		return;
87e2803e61SKeyon Jie 	}
88e2803e61SKeyon Jie 
89e2803e61SKeyon Jie 	/*
90e2803e61SKeyon Jie 	 * snd_pcm_period_elapsed() can be called in interrupt context
91e2803e61SKeyon Jie 	 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(),
92e2803e61SKeyon Jie 	 * when the PCM is done draining or xrun happened, a STOP IPC will
93e2803e61SKeyon Jie 	 * then be sent and this IPC will hit IPC timeout.
94e2803e61SKeyon Jie 	 * To avoid sending IPC before the previous IPC is handled, we
95e2803e61SKeyon Jie 	 * schedule delayed work here to call the snd_pcm_period_elapsed().
96e2803e61SKeyon Jie 	 */
97e2803e61SKeyon Jie 	schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
98e2803e61SKeyon Jie }
99e2803e61SKeyon Jie EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
100e2803e61SKeyon Jie 
10196da1740SRanjani Sridharan int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream, struct snd_sof_dev *sdev,
102cfe8191bSKai Vehmanen 			 struct snd_sof_pcm *spcm)
103cfe8191bSKai Vehmanen {
104cfe8191bSKai Vehmanen 	struct sof_ipc_stream stream;
105cfe8191bSKai Vehmanen 	struct sof_ipc_reply reply;
106cfe8191bSKai Vehmanen 	int ret;
107cfe8191bSKai Vehmanen 
10885d7acd0SRanjani Sridharan 	if (!spcm->prepared[substream->stream])
10985d7acd0SRanjani Sridharan 		return 0;
11085d7acd0SRanjani Sridharan 
111cfe8191bSKai Vehmanen 	stream.hdr.size = sizeof(stream);
112cfe8191bSKai Vehmanen 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
113cfe8191bSKai Vehmanen 	stream.comp_id = spcm->stream[substream->stream].comp_id;
114cfe8191bSKai Vehmanen 
115cfe8191bSKai Vehmanen 	/* send IPC to the DSP */
116cfe8191bSKai Vehmanen 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
117cfe8191bSKai Vehmanen 				 sizeof(stream), &reply, sizeof(reply));
118cfe8191bSKai Vehmanen 	if (!ret)
119cfe8191bSKai Vehmanen 		spcm->prepared[substream->stream] = false;
120cfe8191bSKai Vehmanen 
121cfe8191bSKai Vehmanen 	return ret;
122cfe8191bSKai Vehmanen }
123cfe8191bSKai Vehmanen 
1245fcdbb2dSRanjani Sridharan static int sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev,
1255fcdbb2dSRanjani Sridharan 					   struct snd_soc_pcm_runtime *rtd,
1265fcdbb2dSRanjani Sridharan 					   struct snd_sof_pcm *spcm, int dir)
1275fcdbb2dSRanjani Sridharan {
1285fcdbb2dSRanjani Sridharan 	struct snd_soc_dai *dai;
1295fcdbb2dSRanjani Sridharan 	int ret, j;
1305fcdbb2dSRanjani Sridharan 
1315fcdbb2dSRanjani Sridharan 	/* query DAPM for list of connected widgets and set them up */
1325fcdbb2dSRanjani Sridharan 	for_each_rtd_cpu_dais(rtd, j, dai) {
1335fcdbb2dSRanjani Sridharan 		struct snd_soc_dapm_widget_list *list;
1345fcdbb2dSRanjani Sridharan 
1355fcdbb2dSRanjani Sridharan 		ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list,
1365fcdbb2dSRanjani Sridharan 							     dpcm_end_walk_at_be);
1375fcdbb2dSRanjani Sridharan 		if (ret < 0) {
1385fcdbb2dSRanjani Sridharan 			dev_err(sdev->dev, "error: dai %s has no valid %s path\n", dai->name,
1395fcdbb2dSRanjani Sridharan 				dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
1405fcdbb2dSRanjani Sridharan 			return ret;
1415fcdbb2dSRanjani Sridharan 		}
1425fcdbb2dSRanjani Sridharan 
1435fcdbb2dSRanjani Sridharan 		spcm->stream[dir].list = list;
1445fcdbb2dSRanjani Sridharan 
1455fcdbb2dSRanjani Sridharan 		ret = sof_widget_list_setup(sdev, spcm, dir);
1465fcdbb2dSRanjani Sridharan 		if (ret < 0) {
1475fcdbb2dSRanjani Sridharan 			dev_err(sdev->dev, "error: failed widget list set up for pcm %d dir %d\n",
1485fcdbb2dSRanjani Sridharan 				spcm->pcm.pcm_id, dir);
1495fcdbb2dSRanjani Sridharan 			spcm->stream[dir].list = NULL;
1505fcdbb2dSRanjani Sridharan 			snd_soc_dapm_dai_free_widgets(&list);
1515fcdbb2dSRanjani Sridharan 			return ret;
1525fcdbb2dSRanjani Sridharan 		}
1535fcdbb2dSRanjani Sridharan 	}
1545fcdbb2dSRanjani Sridharan 
1555fcdbb2dSRanjani Sridharan 	return 0;
1565fcdbb2dSRanjani Sridharan }
1575fcdbb2dSRanjani Sridharan 
1581c91d77eSKuninori Morimoto static int sof_pcm_hw_params(struct snd_soc_component *component,
1591c91d77eSKuninori Morimoto 			     struct snd_pcm_substream *substream,
160868bd00fSLiam Girdwood 			     struct snd_pcm_hw_params *params)
161868bd00fSLiam Girdwood {
1621205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
163868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
164868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
165868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
166868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params pcm;
167868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params_reply ipc_params_reply;
168868bd00fSLiam Girdwood 	int ret;
169868bd00fSLiam Girdwood 
170868bd00fSLiam Girdwood 	/* nothing to do for BE */
171868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
172868bd00fSLiam Girdwood 		return 0;
173868bd00fSLiam Girdwood 
174ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
175868bd00fSLiam Girdwood 	if (!spcm)
176868bd00fSLiam Girdwood 		return -EINVAL;
177868bd00fSLiam Girdwood 
178cfe8191bSKai Vehmanen 	/*
179cfe8191bSKai Vehmanen 	 * Handle repeated calls to hw_params() without free_pcm() in
180cfe8191bSKai Vehmanen 	 * between. At least ALSA OSS emulation depends on this.
181cfe8191bSKai Vehmanen 	 */
182cfe8191bSKai Vehmanen 	ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
183cfe8191bSKai Vehmanen 	if (ret < 0)
184cfe8191bSKai Vehmanen 		return ret;
185cfe8191bSKai Vehmanen 
186ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
187868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream);
188868bd00fSLiam Girdwood 
189868bd00fSLiam Girdwood 	memset(&pcm, 0, sizeof(pcm));
190868bd00fSLiam Girdwood 
19157e960f0STakashi Iwai 	/* create compressed page table for audio firmware */
19257e960f0STakashi Iwai 	if (runtime->buffer_changed) {
1931c91d77eSKuninori Morimoto 		ret = create_page_table(component, substream, runtime->dma_area,
194868bd00fSLiam Girdwood 					runtime->dma_bytes);
195868bd00fSLiam Girdwood 		if (ret < 0)
196868bd00fSLiam Girdwood 			return ret;
197868bd00fSLiam Girdwood 	}
198868bd00fSLiam Girdwood 
199868bd00fSLiam Girdwood 	/* number of pages should be rounded up */
200868bd00fSLiam Girdwood 	pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
201868bd00fSLiam Girdwood 
202868bd00fSLiam Girdwood 	/* set IPC PCM parameters */
203868bd00fSLiam Girdwood 	pcm.hdr.size = sizeof(pcm);
204868bd00fSLiam Girdwood 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
205868bd00fSLiam Girdwood 	pcm.comp_id = spcm->stream[substream->stream].comp_id;
206868bd00fSLiam Girdwood 	pcm.params.hdr.size = sizeof(pcm.params);
207868bd00fSLiam Girdwood 	pcm.params.buffer.phy_addr =
208868bd00fSLiam Girdwood 		spcm->stream[substream->stream].page_table.addr;
209868bd00fSLiam Girdwood 	pcm.params.buffer.size = runtime->dma_bytes;
210868bd00fSLiam Girdwood 	pcm.params.direction = substream->stream;
211868bd00fSLiam Girdwood 	pcm.params.sample_valid_bytes = params_width(params) >> 3;
212868bd00fSLiam Girdwood 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
213868bd00fSLiam Girdwood 	pcm.params.rate = params_rate(params);
214868bd00fSLiam Girdwood 	pcm.params.channels = params_channels(params);
215868bd00fSLiam Girdwood 	pcm.params.host_period_bytes = params_period_bytes(params);
216868bd00fSLiam Girdwood 
217868bd00fSLiam Girdwood 	/* container size */
218868bd00fSLiam Girdwood 	ret = snd_pcm_format_physical_width(params_format(params));
219868bd00fSLiam Girdwood 	if (ret < 0)
220868bd00fSLiam Girdwood 		return ret;
221868bd00fSLiam Girdwood 	pcm.params.sample_container_bytes = ret >> 3;
222868bd00fSLiam Girdwood 
223868bd00fSLiam Girdwood 	/* format */
224868bd00fSLiam Girdwood 	switch (params_format(params)) {
225868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S16:
226868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
227868bd00fSLiam Girdwood 		break;
228868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S24:
229868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
230868bd00fSLiam Girdwood 		break;
231868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S32:
232868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
233868bd00fSLiam Girdwood 		break;
234868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_FLOAT:
235868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
236868bd00fSLiam Girdwood 		break;
237868bd00fSLiam Girdwood 	default:
238868bd00fSLiam Girdwood 		return -EINVAL;
239868bd00fSLiam Girdwood 	}
240868bd00fSLiam Girdwood 
241868bd00fSLiam Girdwood 	/* firmware already configured host stream */
242868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_hw_params(sdev,
243868bd00fSLiam Girdwood 					     substream,
244868bd00fSLiam Girdwood 					     params,
245868bd00fSLiam Girdwood 					     &pcm.params);
246868bd00fSLiam Girdwood 	if (ret < 0) {
247ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw params failed\n");
248868bd00fSLiam Girdwood 		return ret;
249868bd00fSLiam Girdwood 	}
250868bd00fSLiam Girdwood 
251ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag);
252868bd00fSLiam Girdwood 
2535fcdbb2dSRanjani Sridharan 	/* if this is a repeated hw_params without hw_free, skip setting up widgets */
2545fcdbb2dSRanjani Sridharan 	if (!spcm->stream[substream->stream].list) {
2555fcdbb2dSRanjani Sridharan 		ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, substream->stream);
2565fcdbb2dSRanjani Sridharan 		if (ret < 0)
2575fcdbb2dSRanjani Sridharan 			return ret;
2585fcdbb2dSRanjani Sridharan 	}
2595fcdbb2dSRanjani Sridharan 
2605fcdbb2dSRanjani Sridharan 	/* send hw_params IPC to the DSP */
261868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
262868bd00fSLiam Girdwood 				 &ipc_params_reply, sizeof(ipc_params_reply));
263868bd00fSLiam Girdwood 	if (ret < 0) {
264ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: hw params ipc failed for stream %d\n",
265868bd00fSLiam Girdwood 			pcm.params.stream_tag);
266868bd00fSLiam Girdwood 		return ret;
267868bd00fSLiam Girdwood 	}
268868bd00fSLiam Girdwood 
269868bd00fSLiam Girdwood 	ret = sof_pcm_dsp_params(spcm, substream, &ipc_params_reply);
270868bd00fSLiam Girdwood 	if (ret < 0)
271868bd00fSLiam Girdwood 		return ret;
272868bd00fSLiam Girdwood 
27304c80277SKai Vehmanen 	spcm->prepared[substream->stream] = true;
27404c80277SKai Vehmanen 
275868bd00fSLiam Girdwood 	/* save pcm hw_params */
276868bd00fSLiam Girdwood 	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
277868bd00fSLiam Girdwood 
278868bd00fSLiam Girdwood 	return ret;
279868bd00fSLiam Girdwood }
280868bd00fSLiam Girdwood 
2811c91d77eSKuninori Morimoto static int sof_pcm_hw_free(struct snd_soc_component *component,
2821c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
283868bd00fSLiam Girdwood {
2841205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
285868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
286868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
287e66e52c5SKai Vehmanen 	int ret, err = 0;
288868bd00fSLiam Girdwood 
289868bd00fSLiam Girdwood 	/* nothing to do for BE */
290868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
291868bd00fSLiam Girdwood 		return 0;
292868bd00fSLiam Girdwood 
293ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
294868bd00fSLiam Girdwood 	if (!spcm)
295868bd00fSLiam Girdwood 		return -EINVAL;
296868bd00fSLiam Girdwood 
297ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
298ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
299868bd00fSLiam Girdwood 
3000b639dcdSRanjani Sridharan 	/* free PCM in the DSP */
301a49b6871SKai Vehmanen 	ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
302e66e52c5SKai Vehmanen 	if (ret < 0)
303e66e52c5SKai Vehmanen 		err = ret;
304868bd00fSLiam Girdwood 
3055fcdbb2dSRanjani Sridharan 
3060b639dcdSRanjani Sridharan 	/* stop DMA */
30793146bc2SRanjani Sridharan 	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
308e66e52c5SKai Vehmanen 	if (ret < 0) {
309ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw free failed\n");
310e66e52c5SKai Vehmanen 		err = ret;
311e66e52c5SKai Vehmanen 	}
31293146bc2SRanjani Sridharan 
3130b639dcdSRanjani Sridharan 	/* free the DAPM widget list */
3140b639dcdSRanjani Sridharan 	ret = sof_widget_list_free(sdev, spcm, substream->stream);
3150b639dcdSRanjani Sridharan 	if (ret < 0)
3160b639dcdSRanjani Sridharan 		err = ret;
3170b639dcdSRanjani Sridharan 
3180b639dcdSRanjani Sridharan 	cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
3190b639dcdSRanjani Sridharan 
320e66e52c5SKai Vehmanen 	return err;
321868bd00fSLiam Girdwood }
322868bd00fSLiam Girdwood 
3231c91d77eSKuninori Morimoto static int sof_pcm_prepare(struct snd_soc_component *component,
3241c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
325868bd00fSLiam Girdwood {
3261205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
327868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
328868bd00fSLiam Girdwood 	int ret;
329868bd00fSLiam Girdwood 
330868bd00fSLiam Girdwood 	/* nothing to do for BE */
331868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
332868bd00fSLiam Girdwood 		return 0;
333868bd00fSLiam Girdwood 
334ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
335868bd00fSLiam Girdwood 	if (!spcm)
336868bd00fSLiam Girdwood 		return -EINVAL;
337868bd00fSLiam Girdwood 
33804c80277SKai Vehmanen 	if (spcm->prepared[substream->stream])
339868bd00fSLiam Girdwood 		return 0;
340868bd00fSLiam Girdwood 
341ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
342ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
343868bd00fSLiam Girdwood 
344868bd00fSLiam Girdwood 	/* set hw_params */
3451c91d77eSKuninori Morimoto 	ret = sof_pcm_hw_params(component,
3461c91d77eSKuninori Morimoto 				substream, &spcm->params[substream->stream]);
347868bd00fSLiam Girdwood 	if (ret < 0) {
348ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
349ee1e79b7SRanjani Sridharan 			"error: set pcm hw_params after resume\n");
350868bd00fSLiam Girdwood 		return ret;
351868bd00fSLiam Girdwood 	}
352868bd00fSLiam Girdwood 
353868bd00fSLiam Girdwood 	return 0;
354868bd00fSLiam Girdwood }
355868bd00fSLiam Girdwood 
356868bd00fSLiam Girdwood /*
357868bd00fSLiam Girdwood  * FE dai link trigger actions are always executed in non-atomic context because
358868bd00fSLiam Girdwood  * they involve IPC's.
359868bd00fSLiam Girdwood  */
3601c91d77eSKuninori Morimoto static int sof_pcm_trigger(struct snd_soc_component *component,
3611c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream, int cmd)
362868bd00fSLiam Girdwood {
3631205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
364868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
365868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
366868bd00fSLiam Girdwood 	struct sof_ipc_stream stream;
367868bd00fSLiam Girdwood 	struct sof_ipc_reply reply;
36804c80277SKai Vehmanen 	bool reset_hw_params = false;
3695fcdbb2dSRanjani Sridharan 	bool free_widget_list = false;
3700a1b0834SPan Xiuli 	bool ipc_first = false;
371868bd00fSLiam Girdwood 	int ret;
372868bd00fSLiam Girdwood 
373868bd00fSLiam Girdwood 	/* nothing to do for BE */
374868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
375868bd00fSLiam Girdwood 		return 0;
376868bd00fSLiam Girdwood 
377ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
378868bd00fSLiam Girdwood 	if (!spcm)
379868bd00fSLiam Girdwood 		return -EINVAL;
380868bd00fSLiam Girdwood 
381ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
382868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream, cmd);
383868bd00fSLiam Girdwood 
384868bd00fSLiam Girdwood 	stream.hdr.size = sizeof(stream);
385868bd00fSLiam Girdwood 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
386868bd00fSLiam Girdwood 	stream.comp_id = spcm->stream[substream->stream].comp_id;
387868bd00fSLiam Girdwood 
388868bd00fSLiam Girdwood 	switch (cmd) {
389868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
390868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
3910a1b0834SPan Xiuli 		ipc_first = true;
392868bd00fSLiam Girdwood 		break;
393868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
394868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
395868bd00fSLiam Girdwood 		break;
396868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
397ac8c046fSKeyon Jie 		if (spcm->stream[substream->stream].suspend_ignored) {
398ac8c046fSKeyon Jie 			/*
399ac8c046fSKeyon Jie 			 * This case will be triggered when INFO_RESUME is
400ac8c046fSKeyon Jie 			 * not supported, no need to re-start streams that
401ac8c046fSKeyon Jie 			 * remained enabled in D0ix.
402ac8c046fSKeyon Jie 			 */
403ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = false;
404ac8c046fSKeyon Jie 			return 0;
405ac8c046fSKeyon Jie 		}
406868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
407868bd00fSLiam Girdwood 		break;
408868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
409043ae13bSRanjani Sridharan 		if (sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
410ac8c046fSKeyon Jie 		    spcm->stream[substream->stream].d0i3_compatible) {
411ac8c046fSKeyon Jie 			/*
412ac8c046fSKeyon Jie 			 * trap the event, not sending trigger stop to
413ac8c046fSKeyon Jie 			 * prevent the FW pipelines from being stopped,
414ac8c046fSKeyon Jie 			 * and mark the flag to ignore the upcoming DAPM
415ac8c046fSKeyon Jie 			 * PM events.
416ac8c046fSKeyon Jie 			 */
417ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = true;
418ac8c046fSKeyon Jie 			return 0;
419ac8c046fSKeyon Jie 		}
4205fcdbb2dSRanjani Sridharan 		free_widget_list = true;
421df561f66SGustavo A. R. Silva 		fallthrough;
422868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
423868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
4240a1b0834SPan Xiuli 		ipc_first = true;
42504c80277SKai Vehmanen 		reset_hw_params = true;
426868bd00fSLiam Girdwood 		break;
427868bd00fSLiam Girdwood 	default:
428ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: unhandled trigger cmd %d\n",
429ee1e79b7SRanjani Sridharan 			cmd);
430868bd00fSLiam Girdwood 		return -EINVAL;
431868bd00fSLiam Girdwood 	}
432868bd00fSLiam Girdwood 
4330a1b0834SPan Xiuli 	/*
4340a1b0834SPan Xiuli 	 * DMA and IPC sequence is different for start and stop. Need to send
4350a1b0834SPan Xiuli 	 * STOP IPC before stop DMA
4360a1b0834SPan Xiuli 	 */
4370a1b0834SPan Xiuli 	if (!ipc_first)
438868bd00fSLiam Girdwood 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
439868bd00fSLiam Girdwood 
440868bd00fSLiam Girdwood 	/* send IPC to the DSP */
441868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
442868bd00fSLiam Girdwood 				 sizeof(stream), &reply, sizeof(reply));
443868bd00fSLiam Girdwood 
4440a1b0834SPan Xiuli 	/* need to STOP DMA even if STOP IPC failed */
4450a1b0834SPan Xiuli 	if (ipc_first)
4460a1b0834SPan Xiuli 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
4470a1b0834SPan Xiuli 
4480a1b0834SPan Xiuli 	/* free PCM if reset_hw_params is set and the STOP IPC is successful */
4495fcdbb2dSRanjani Sridharan 	if (!ret && reset_hw_params) {
450d9a72465SRanjani Sridharan 		ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream,
451d9a72465SRanjani Sridharan 					  free_widget_list);
4525fcdbb2dSRanjani Sridharan 		if (ret < 0)
4535fcdbb2dSRanjani Sridharan 			return ret;
4545fcdbb2dSRanjani Sridharan 	}
455a49b6871SKai Vehmanen 
456868bd00fSLiam Girdwood 	return ret;
457868bd00fSLiam Girdwood }
458868bd00fSLiam Girdwood 
4591c91d77eSKuninori Morimoto static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
4601c91d77eSKuninori Morimoto 					 struct snd_pcm_substream *substream)
461868bd00fSLiam Girdwood {
4621205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
463868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
464868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
465868bd00fSLiam Girdwood 	snd_pcm_uframes_t host, dai;
466868bd00fSLiam Girdwood 
467868bd00fSLiam Girdwood 	/* nothing to do for BE */
468868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
469868bd00fSLiam Girdwood 		return 0;
470868bd00fSLiam Girdwood 
471868bd00fSLiam Girdwood 	/* use dsp ops pointer callback directly if set */
472868bd00fSLiam Girdwood 	if (sof_ops(sdev)->pcm_pointer)
473868bd00fSLiam Girdwood 		return sof_ops(sdev)->pcm_pointer(sdev, substream);
474868bd00fSLiam Girdwood 
475ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
476868bd00fSLiam Girdwood 	if (!spcm)
477868bd00fSLiam Girdwood 		return -EINVAL;
478868bd00fSLiam Girdwood 
479868bd00fSLiam Girdwood 	/* read position from DSP */
480868bd00fSLiam Girdwood 	host = bytes_to_frames(substream->runtime,
481868bd00fSLiam Girdwood 			       spcm->stream[substream->stream].posn.host_posn);
482868bd00fSLiam Girdwood 	dai = bytes_to_frames(substream->runtime,
483868bd00fSLiam Girdwood 			      spcm->stream[substream->stream].posn.dai_posn);
484868bd00fSLiam Girdwood 
485277ff236SPierre-Louis Bossart 	dev_vdbg(component->dev,
486ee1e79b7SRanjani Sridharan 		 "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
487868bd00fSLiam Girdwood 		 spcm->pcm.pcm_id, substream->stream, host, dai);
488868bd00fSLiam Girdwood 
489868bd00fSLiam Girdwood 	return host;
490868bd00fSLiam Girdwood }
491868bd00fSLiam Girdwood 
4921c91d77eSKuninori Morimoto static int sof_pcm_open(struct snd_soc_component *component,
4931c91d77eSKuninori Morimoto 			struct snd_pcm_substream *substream)
494868bd00fSLiam Girdwood {
4951205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
496868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
497868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
49827e322faSPierre-Louis Bossart 	const struct snd_sof_dsp_ops *ops = sof_ops(sdev);
499868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
500868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
501868bd00fSLiam Girdwood 	int ret;
502868bd00fSLiam Girdwood 
503868bd00fSLiam Girdwood 	/* nothing to do for BE */
504868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
505868bd00fSLiam Girdwood 		return 0;
506868bd00fSLiam Girdwood 
507ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
508868bd00fSLiam Girdwood 	if (!spcm)
509868bd00fSLiam Girdwood 		return -EINVAL;
510868bd00fSLiam Girdwood 
511ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
512ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
513868bd00fSLiam Girdwood 
514868bd00fSLiam Girdwood 
515868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[substream->stream];
516868bd00fSLiam Girdwood 
517868bd00fSLiam Girdwood 	/* set runtime config */
51827e322faSPierre-Louis Bossart 	runtime->hw.info = ops->hw_info; /* platform-specific */
51927e322faSPierre-Louis Bossart 
5209983ac49SKai Vehmanen 	/* set any runtime constraints based on topology */
521868bd00fSLiam Girdwood 	runtime->hw.formats = le64_to_cpu(caps->formats);
522868bd00fSLiam Girdwood 	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
523868bd00fSLiam Girdwood 	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
524868bd00fSLiam Girdwood 	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
525868bd00fSLiam Girdwood 	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
526868bd00fSLiam Girdwood 
527868bd00fSLiam Girdwood 	/*
528868bd00fSLiam Girdwood 	 * caps->buffer_size_min is not used since the
529868bd00fSLiam Girdwood 	 * snd_pcm_hardware structure only defines buffer_bytes_max
530868bd00fSLiam Girdwood 	 */
531868bd00fSLiam Girdwood 	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
532868bd00fSLiam Girdwood 
533ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period min %zd max %zd bytes\n",
534868bd00fSLiam Girdwood 		runtime->hw.period_bytes_min,
535868bd00fSLiam Girdwood 		runtime->hw.period_bytes_max);
536ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period count %d max %d\n",
537868bd00fSLiam Girdwood 		runtime->hw.periods_min,
538868bd00fSLiam Girdwood 		runtime->hw.periods_max);
539ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "buffer max %zd bytes\n",
540868bd00fSLiam Girdwood 		runtime->hw.buffer_bytes_max);
541868bd00fSLiam Girdwood 
542868bd00fSLiam Girdwood 	/* set wait time - TODO: come from topology */
543868bd00fSLiam Girdwood 	substream->wait_time = 500;
544868bd00fSLiam Girdwood 
545868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.host_posn = 0;
546868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.dai_posn = 0;
547868bd00fSLiam Girdwood 	spcm->stream[substream->stream].substream = substream;
54804c80277SKai Vehmanen 	spcm->prepared[substream->stream] = false;
549868bd00fSLiam Girdwood 
550868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_open(sdev, substream);
55114a2212dSRanjani Sridharan 	if (ret < 0)
552ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm open failed %d\n", ret);
553868bd00fSLiam Girdwood 
554868bd00fSLiam Girdwood 	return ret;
555868bd00fSLiam Girdwood }
556868bd00fSLiam Girdwood 
5571c91d77eSKuninori Morimoto static int sof_pcm_close(struct snd_soc_component *component,
5581c91d77eSKuninori Morimoto 			 struct snd_pcm_substream *substream)
559868bd00fSLiam Girdwood {
5601205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
561868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
562868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
563868bd00fSLiam Girdwood 	int err;
564868bd00fSLiam Girdwood 
565868bd00fSLiam Girdwood 	/* nothing to do for BE */
566868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
567868bd00fSLiam Girdwood 		return 0;
568868bd00fSLiam Girdwood 
569ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
570868bd00fSLiam Girdwood 	if (!spcm)
571868bd00fSLiam Girdwood 		return -EINVAL;
572868bd00fSLiam Girdwood 
573ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
574ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
575868bd00fSLiam Girdwood 
576868bd00fSLiam Girdwood 	err = snd_sof_pcm_platform_close(sdev, substream);
577868bd00fSLiam Girdwood 	if (err < 0) {
578ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm close failed %d\n",
579868bd00fSLiam Girdwood 			err);
580868bd00fSLiam Girdwood 		/*
581868bd00fSLiam Girdwood 		 * keep going, no point in preventing the close
582868bd00fSLiam Girdwood 		 * from happening
583868bd00fSLiam Girdwood 		 */
584868bd00fSLiam Girdwood 	}
585868bd00fSLiam Girdwood 
586868bd00fSLiam Girdwood 	return 0;
587868bd00fSLiam Girdwood }
588868bd00fSLiam Girdwood 
589868bd00fSLiam Girdwood /*
590868bd00fSLiam Girdwood  * Pre-allocate playback/capture audio buffer pages.
591868bd00fSLiam Girdwood  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
592868bd00fSLiam Girdwood  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
593868bd00fSLiam Girdwood  */
5941c91d77eSKuninori Morimoto static int sof_pcm_new(struct snd_soc_component *component,
5951c91d77eSKuninori Morimoto 		       struct snd_soc_pcm_runtime *rtd)
596868bd00fSLiam Girdwood {
597868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
598868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
599868bd00fSLiam Girdwood 	struct snd_pcm *pcm = rtd->pcm;
600868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
601868bd00fSLiam Girdwood 	int stream = SNDRV_PCM_STREAM_PLAYBACK;
602868bd00fSLiam Girdwood 
603868bd00fSLiam Girdwood 	/* find SOF PCM for this RTD */
604ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
605868bd00fSLiam Girdwood 	if (!spcm) {
606ee1e79b7SRanjani Sridharan 		dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
607868bd00fSLiam Girdwood 			 rtd->dai_link->id);
608868bd00fSLiam Girdwood 		return 0;
609868bd00fSLiam Girdwood 	}
610868bd00fSLiam Girdwood 
611ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
612868bd00fSLiam Girdwood 
613868bd00fSLiam Girdwood 	/* do we need to pre-allocate playback audio buffer pages */
614868bd00fSLiam Girdwood 	if (!spcm->pcm.playback)
615868bd00fSLiam Girdwood 		goto capture;
616868bd00fSLiam Girdwood 
617868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
618868bd00fSLiam Girdwood 
619868bd00fSLiam Girdwood 	/* pre-allocate playback audio buffer pages */
620ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
621ee1e79b7SRanjani Sridharan 		"spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
622868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
623868bd00fSLiam Girdwood 
6244f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
6254f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL playback substream!\n");
6264f7f9564SGuennadi Liakhovetski 		return -EINVAL;
6274f7f9564SGuennadi Liakhovetski 	}
6284f7f9564SGuennadi Liakhovetski 
62957e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
630868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
631e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
632868bd00fSLiam Girdwood capture:
633868bd00fSLiam Girdwood 	stream = SNDRV_PCM_STREAM_CAPTURE;
634868bd00fSLiam Girdwood 
635868bd00fSLiam Girdwood 	/* do we need to pre-allocate capture audio buffer pages */
636868bd00fSLiam Girdwood 	if (!spcm->pcm.capture)
637868bd00fSLiam Girdwood 		return 0;
638868bd00fSLiam Girdwood 
639868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
640868bd00fSLiam Girdwood 
641868bd00fSLiam Girdwood 	/* pre-allocate capture audio buffer pages */
642ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
643ee1e79b7SRanjani Sridharan 		"spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
644868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
645868bd00fSLiam Girdwood 
6464f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
6474f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL capture substream!\n");
6484f7f9564SGuennadi Liakhovetski 		return -EINVAL;
6494f7f9564SGuennadi Liakhovetski 	}
6504f7f9564SGuennadi Liakhovetski 
65157e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
652868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
653e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
654868bd00fSLiam Girdwood 
655868bd00fSLiam Girdwood 	return 0;
656868bd00fSLiam Girdwood }
657868bd00fSLiam Girdwood 
658c943a586SJaska Uimonen static void ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
659c943a586SJaska Uimonen 					    struct snd_pcm_hw_params *params)
660c943a586SJaska Uimonen {
661c943a586SJaska Uimonen 	struct sof_ipc_dai_config *config;
662c943a586SJaska Uimonen 	struct snd_sof_dai *dai;
663c943a586SJaska Uimonen 	int i;
664c943a586SJaska Uimonen 
665c943a586SJaska Uimonen 	/*
666c943a586SJaska Uimonen 	 * Search for all matching DAIs as we can have both playback and capture DAI
667c943a586SJaska Uimonen 	 * associated with the same link.
668c943a586SJaska Uimonen 	 */
669c943a586SJaska Uimonen 	list_for_each_entry(dai, &sdev->dai_list, list) {
670c943a586SJaska Uimonen 		if (!dai->name || strcmp(link_name, dai->name))
671c943a586SJaska Uimonen 			continue;
672c943a586SJaska Uimonen 		for (i = 0; i < dai->number_configs; i++) {
673c943a586SJaska Uimonen 			config = &dai->dai_config[i];
674c943a586SJaska Uimonen 			if (config->ssp.fsync_rate == params_rate(params)) {
675c943a586SJaska Uimonen 				dev_dbg(sdev->dev, "DAI config %d matches pcm hw params\n", i);
676c943a586SJaska Uimonen 				dai->current_config = i;
677c943a586SJaska Uimonen 				break;
678c943a586SJaska Uimonen 			}
679c943a586SJaska Uimonen 		}
680c943a586SJaska Uimonen 	}
681c943a586SJaska Uimonen }
682c943a586SJaska Uimonen 
683868bd00fSLiam Girdwood /* fixup the BE DAI link to match any values from topology */
684f805e7e0SRanjani Sridharan int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
685868bd00fSLiam Girdwood {
686868bd00fSLiam Girdwood 	struct snd_interval *rate = hw_param_interval(params,
687868bd00fSLiam Girdwood 			SNDRV_PCM_HW_PARAM_RATE);
688868bd00fSLiam Girdwood 	struct snd_interval *channels = hw_param_interval(params,
689868bd00fSLiam Girdwood 						SNDRV_PCM_HW_PARAM_CHANNELS);
690868bd00fSLiam Girdwood 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
691868bd00fSLiam Girdwood 	struct snd_soc_component *component =
692ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
693868bd00fSLiam Girdwood 	struct snd_sof_dai *dai =
694ee1e79b7SRanjani Sridharan 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
695c943a586SJaska Uimonen 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
696fd045558Sranderwang 	struct snd_soc_dpcm *dpcm;
697868bd00fSLiam Girdwood 
698868bd00fSLiam Girdwood 	/* no topology exists for this BE, try a common configuration */
699868bd00fSLiam Girdwood 	if (!dai) {
700ee1e79b7SRanjani Sridharan 		dev_warn(component->dev,
701ee1e79b7SRanjani Sridharan 			 "warning: no topology found for BE DAI %s config\n",
702868bd00fSLiam Girdwood 			 rtd->dai_link->name);
703868bd00fSLiam Girdwood 
704868bd00fSLiam Girdwood 		/*  set 48k, stereo, 16bits by default */
705868bd00fSLiam Girdwood 		rate->min = 48000;
706868bd00fSLiam Girdwood 		rate->max = 48000;
707868bd00fSLiam Girdwood 
708868bd00fSLiam Girdwood 		channels->min = 2;
709868bd00fSLiam Girdwood 		channels->max = 2;
710868bd00fSLiam Girdwood 
711868bd00fSLiam Girdwood 		snd_mask_none(fmt);
712868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
713868bd00fSLiam Girdwood 
714868bd00fSLiam Girdwood 		return 0;
715868bd00fSLiam Girdwood 	}
716868bd00fSLiam Girdwood 
717868bd00fSLiam Girdwood 	/* read format from topology */
718868bd00fSLiam Girdwood 	snd_mask_none(fmt);
719868bd00fSLiam Girdwood 
720868bd00fSLiam Girdwood 	switch (dai->comp_dai.config.frame_fmt) {
721868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S16_LE:
722868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
723868bd00fSLiam Girdwood 		break;
724868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S24_4LE:
725868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
726868bd00fSLiam Girdwood 		break;
727868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S32_LE:
728868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
729868bd00fSLiam Girdwood 		break;
730868bd00fSLiam Girdwood 	default:
731ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: No available DAI format!\n");
732868bd00fSLiam Girdwood 		return -EINVAL;
733868bd00fSLiam Girdwood 	}
734868bd00fSLiam Girdwood 
735868bd00fSLiam Girdwood 	/* read rate and channels from topology */
736868bd00fSLiam Girdwood 	switch (dai->dai_config->type) {
737868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_SSP:
738c943a586SJaska Uimonen 		/* search for config to pcm params match, if not found use default */
739c943a586SJaska Uimonen 		ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);
740c943a586SJaska Uimonen 
741c1c03888SJaska Uimonen 		rate->min = dai->dai_config[dai->current_config].ssp.fsync_rate;
742c1c03888SJaska Uimonen 		rate->max = dai->dai_config[dai->current_config].ssp.fsync_rate;
743c1c03888SJaska Uimonen 		channels->min = dai->dai_config[dai->current_config].ssp.tdm_slots;
744c1c03888SJaska Uimonen 		channels->max = dai->dai_config[dai->current_config].ssp.tdm_slots;
745868bd00fSLiam Girdwood 
746ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
747868bd00fSLiam Girdwood 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
748ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
749868bd00fSLiam Girdwood 			"channels_min: %d channels_max: %d\n",
750868bd00fSLiam Girdwood 			channels->min, channels->max);
751868bd00fSLiam Girdwood 
752868bd00fSLiam Girdwood 		break;
753868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_DMIC:
754868bd00fSLiam Girdwood 		/* DMIC only supports 16 or 32 bit formats */
755868bd00fSLiam Girdwood 		if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
756ee1e79b7SRanjani Sridharan 			dev_err(component->dev,
757868bd00fSLiam Girdwood 				"error: invalid fmt %d for DAI type %d\n",
758868bd00fSLiam Girdwood 				dai->comp_dai.config.frame_fmt,
759868bd00fSLiam Girdwood 				dai->dai_config->type);
760868bd00fSLiam Girdwood 		}
761868bd00fSLiam Girdwood 		break;
762868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_HDA:
763fd045558Sranderwang 		/*
764135ab457SPierre-Louis Bossart 		 * HDAudio does not follow the default trigger
765fd045558Sranderwang 		 * sequence due to firmware implementation
766fd045558Sranderwang 		 */
767fd045558Sranderwang 		for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
768fd045558Sranderwang 			struct snd_soc_pcm_runtime *fe = dpcm->fe;
769fd045558Sranderwang 
770fd045558Sranderwang 			fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
771fd045558Sranderwang 				SND_SOC_DPCM_TRIGGER_POST;
772fd045558Sranderwang 		}
773868bd00fSLiam Girdwood 		break;
7746e3360cdSPierre-Louis Bossart 	case SOF_DAI_INTEL_ALH:
775e1711b1fSRander Wang 		/*
776e1711b1fSRander Wang 		 * Dai could run with different channel count compared with
777e1711b1fSRander Wang 		 * front end, so get dai channel count from topology
778e1711b1fSRander Wang 		 */
779e1711b1fSRander Wang 		channels->min = dai->dai_config->alh.channels;
780e1711b1fSRander Wang 		channels->max = dai->dai_config->alh.channels;
7816e3360cdSPierre-Louis Bossart 		break;
782a4eff5f8SDaniel Baluta 	case SOF_DAI_IMX_ESAI:
78351b0243aSDaniel Baluta 		rate->min = dai->dai_config->esai.fsync_rate;
78451b0243aSDaniel Baluta 		rate->max = dai->dai_config->esai.fsync_rate;
785a4eff5f8SDaniel Baluta 		channels->min = dai->dai_config->esai.tdm_slots;
786a4eff5f8SDaniel Baluta 		channels->max = dai->dai_config->esai.tdm_slots;
787a4eff5f8SDaniel Baluta 
788ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
78951b0243aSDaniel Baluta 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
79051b0243aSDaniel Baluta 		dev_dbg(component->dev,
791a4eff5f8SDaniel Baluta 			"channels_min: %d channels_max: %d\n",
792a4eff5f8SDaniel Baluta 			channels->min, channels->max);
793a4eff5f8SDaniel Baluta 		break;
794b72bfcffSYC Hung 	case SOF_DAI_MEDIATEK_AFE:
795b72bfcffSYC Hung 		rate->min = dai->dai_config->afe.rate;
796b72bfcffSYC Hung 		rate->max = dai->dai_config->afe.rate;
797b72bfcffSYC Hung 		channels->min = dai->dai_config->afe.channels;
798b72bfcffSYC Hung 		channels->max = dai->dai_config->afe.channels;
799b72bfcffSYC Hung 
800b72bfcffSYC Hung 		dev_dbg(component->dev,
801b72bfcffSYC Hung 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
802b72bfcffSYC Hung 		dev_dbg(component->dev,
803b72bfcffSYC Hung 			"channels_min: %d channels_max: %d\n",
804b72bfcffSYC Hung 			channels->min, channels->max);
805b72bfcffSYC Hung 		break;
806d88cbd6fSGuido Roncarolo 	case SOF_DAI_IMX_SAI:
80751b0243aSDaniel Baluta 		rate->min = dai->dai_config->sai.fsync_rate;
80851b0243aSDaniel Baluta 		rate->max = dai->dai_config->sai.fsync_rate;
809d88cbd6fSGuido Roncarolo 		channels->min = dai->dai_config->sai.tdm_slots;
810d88cbd6fSGuido Roncarolo 		channels->max = dai->dai_config->sai.tdm_slots;
811d88cbd6fSGuido Roncarolo 
812d88cbd6fSGuido Roncarolo 		dev_dbg(component->dev,
81351b0243aSDaniel Baluta 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
81451b0243aSDaniel Baluta 		dev_dbg(component->dev,
815d88cbd6fSGuido Roncarolo 			"channels_min: %d channels_max: %d\n",
816d88cbd6fSGuido Roncarolo 			channels->min, channels->max);
817d88cbd6fSGuido Roncarolo 		break;
818efb931cdSAjit Kumar Pandey 	case SOF_DAI_AMD_BT:
819efb931cdSAjit Kumar Pandey 		rate->min = dai->dai_config->acpbt.fsync_rate;
820efb931cdSAjit Kumar Pandey 		rate->max = dai->dai_config->acpbt.fsync_rate;
821efb931cdSAjit Kumar Pandey 		channels->min = dai->dai_config->acpbt.tdm_slots;
822efb931cdSAjit Kumar Pandey 		channels->max = dai->dai_config->acpbt.tdm_slots;
823efb931cdSAjit Kumar Pandey 
824efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
825efb931cdSAjit Kumar Pandey 			"AMD_BT rate_min: %d rate_max: %d\n", rate->min, rate->max);
826efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
827efb931cdSAjit Kumar Pandey 			"AMD_BT channels_min: %d channels_max: %d\n",
828efb931cdSAjit Kumar Pandey 			channels->min, channels->max);
829efb931cdSAjit Kumar Pandey 		break;
830efb931cdSAjit Kumar Pandey 	case SOF_DAI_AMD_SP:
831efb931cdSAjit Kumar Pandey 		rate->min = dai->dai_config->acpsp.fsync_rate;
832efb931cdSAjit Kumar Pandey 		rate->max = dai->dai_config->acpsp.fsync_rate;
833efb931cdSAjit Kumar Pandey 		channels->min = dai->dai_config->acpsp.tdm_slots;
834efb931cdSAjit Kumar Pandey 		channels->max = dai->dai_config->acpsp.tdm_slots;
835efb931cdSAjit Kumar Pandey 
836efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
837efb931cdSAjit Kumar Pandey 			"AMD_SP rate_min: %d rate_max: %d\n", rate->min, rate->max);
838efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
839efb931cdSAjit Kumar Pandey 			"AMD_SP channels_min: %d channels_max: %d\n",
840efb931cdSAjit Kumar Pandey 			channels->min, channels->max);
841efb931cdSAjit Kumar Pandey 		break;
842efb931cdSAjit Kumar Pandey 	case SOF_DAI_AMD_DMIC:
843efb931cdSAjit Kumar Pandey 		rate->min = dai->dai_config->acpdmic.fsync_rate;
844efb931cdSAjit Kumar Pandey 		rate->max = dai->dai_config->acpdmic.fsync_rate;
845efb931cdSAjit Kumar Pandey 		channels->min = dai->dai_config->acpdmic.tdm_slots;
846efb931cdSAjit Kumar Pandey 		channels->max = dai->dai_config->acpdmic.tdm_slots;
847efb931cdSAjit Kumar Pandey 
848efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
849efb931cdSAjit Kumar Pandey 			"AMD_DMIC rate_min: %d rate_max: %d\n", rate->min, rate->max);
850efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
851efb931cdSAjit Kumar Pandey 			"AMD_DMIC channels_min: %d channels_max: %d\n",
852efb931cdSAjit Kumar Pandey 			channels->min, channels->max);
853efb931cdSAjit Kumar Pandey 		break;
854868bd00fSLiam Girdwood 	default:
855ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: invalid DAI type %d\n",
856868bd00fSLiam Girdwood 			dai->dai_config->type);
857868bd00fSLiam Girdwood 		break;
858868bd00fSLiam Girdwood 	}
859868bd00fSLiam Girdwood 
860868bd00fSLiam Girdwood 	return 0;
861868bd00fSLiam Girdwood }
862f3f3af17SPierre-Louis Bossart EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
863868bd00fSLiam Girdwood 
864868bd00fSLiam Girdwood static int sof_pcm_probe(struct snd_soc_component *component)
865868bd00fSLiam Girdwood {
866868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
867868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
868868bd00fSLiam Girdwood 	const char *tplg_filename;
869868bd00fSLiam Girdwood 	int ret;
870868bd00fSLiam Girdwood 
871868bd00fSLiam Girdwood 	/* load the default topology */
872868bd00fSLiam Girdwood 	sdev->component = component;
873868bd00fSLiam Girdwood 
874868bd00fSLiam Girdwood 	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
875868bd00fSLiam Girdwood 				       "%s/%s",
876868bd00fSLiam Girdwood 				       plat_data->tplg_filename_prefix,
877868bd00fSLiam Girdwood 				       plat_data->tplg_filename);
878868bd00fSLiam Girdwood 	if (!tplg_filename)
879868bd00fSLiam Girdwood 		return -ENOMEM;
880868bd00fSLiam Girdwood 
881ee1e79b7SRanjani Sridharan 	ret = snd_sof_load_topology(component, tplg_filename);
882868bd00fSLiam Girdwood 	if (ret < 0) {
883ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: failed to load DSP topology %d\n",
884868bd00fSLiam Girdwood 			ret);
885868bd00fSLiam Girdwood 		return ret;
886868bd00fSLiam Girdwood 	}
887868bd00fSLiam Girdwood 
888868bd00fSLiam Girdwood 	return ret;
889868bd00fSLiam Girdwood }
890868bd00fSLiam Girdwood 
891868bd00fSLiam Girdwood static void sof_pcm_remove(struct snd_soc_component *component)
892868bd00fSLiam Girdwood {
893868bd00fSLiam Girdwood 	/* remove topology */
894a5b8f71cSAmadeusz Sławiński 	snd_soc_tplg_component_remove(component);
895868bd00fSLiam Girdwood }
896868bd00fSLiam Girdwood 
8974a39ea3fSRanjani Sridharan static int sof_pcm_ack(struct snd_soc_component *component,
8984a39ea3fSRanjani Sridharan 		       struct snd_pcm_substream *substream)
8994a39ea3fSRanjani Sridharan {
9004a39ea3fSRanjani Sridharan 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
9014a39ea3fSRanjani Sridharan 
9024a39ea3fSRanjani Sridharan 	return snd_sof_pcm_platform_ack(sdev, substream);
9034a39ea3fSRanjani Sridharan }
9044a39ea3fSRanjani Sridharan 
905868bd00fSLiam Girdwood void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
906868bd00fSLiam Girdwood {
907868bd00fSLiam Girdwood 	struct snd_soc_component_driver *pd = &sdev->plat_drv;
908868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
909868bd00fSLiam Girdwood 	const char *drv_name;
910868bd00fSLiam Girdwood 
911868bd00fSLiam Girdwood 	drv_name = plat_data->machine->drv_name;
912868bd00fSLiam Girdwood 
913868bd00fSLiam Girdwood 	pd->name = "sof-audio-component";
914868bd00fSLiam Girdwood 	pd->probe = sof_pcm_probe;
915868bd00fSLiam Girdwood 	pd->remove = sof_pcm_remove;
9161c91d77eSKuninori Morimoto 	pd->open = sof_pcm_open;
9171c91d77eSKuninori Morimoto 	pd->close = sof_pcm_close;
9181c91d77eSKuninori Morimoto 	pd->hw_params = sof_pcm_hw_params;
9191c91d77eSKuninori Morimoto 	pd->prepare = sof_pcm_prepare;
9201c91d77eSKuninori Morimoto 	pd->hw_free = sof_pcm_hw_free;
9211c91d77eSKuninori Morimoto 	pd->trigger = sof_pcm_trigger;
9221c91d77eSKuninori Morimoto 	pd->pointer = sof_pcm_pointer;
9234a39ea3fSRanjani Sridharan 	pd->ack = sof_pcm_ack;
9241c91d77eSKuninori Morimoto 
9251c91d77eSKuninori Morimoto 	pd->pcm_construct = sof_pcm_new;
926868bd00fSLiam Girdwood 	pd->ignore_machine = drv_name;
927868bd00fSLiam Girdwood 	pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
928868bd00fSLiam Girdwood 	pd->be_pcm_base = SOF_BE_PCM_BASE;
929868bd00fSLiam Girdwood 	pd->use_dai_pcm_id = true;
930868bd00fSLiam Girdwood 	pd->topology_name_prefix = "sof";
931868bd00fSLiam Girdwood 
932868bd00fSLiam Girdwood 	 /* increment module refcount when a pcm is opened */
933868bd00fSLiam Girdwood 	pd->module_get_upon_open = 1;
934868bd00fSLiam Girdwood }
935