xref: /openbmc/linux/sound/soc/sof/pcm.c (revision d9a7246534753efa383ad8d05ab3691df846c4b4)
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"
18868bd00fSLiam Girdwood #include "ops.h"
1970368106SCezary Rojewski #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
207bbdda80SPeter Ujfalusi #include "sof-probes.h"
2170368106SCezary Rojewski #endif
22868bd00fSLiam Girdwood 
23868bd00fSLiam Girdwood /* Create DMA buffer page table for DSP */
241c91d77eSKuninori Morimoto static int create_page_table(struct snd_soc_component *component,
251c91d77eSKuninori Morimoto 			     struct snd_pcm_substream *substream,
26868bd00fSLiam Girdwood 			     unsigned char *dma_area, size_t size)
27868bd00fSLiam Girdwood {
281205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
29868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
30868bd00fSLiam Girdwood 	struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
31868bd00fSLiam Girdwood 	int stream = substream->stream;
32868bd00fSLiam Girdwood 
33ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
34868bd00fSLiam Girdwood 	if (!spcm)
35868bd00fSLiam Girdwood 		return -EINVAL;
36868bd00fSLiam Girdwood 
37ee1e79b7SRanjani Sridharan 	return snd_sof_create_page_table(component->dev, dmab,
38868bd00fSLiam Girdwood 		spcm->stream[stream].page_table.area, size);
39868bd00fSLiam Girdwood }
40868bd00fSLiam Girdwood 
41868bd00fSLiam Girdwood static int sof_pcm_dsp_params(struct snd_sof_pcm *spcm, struct snd_pcm_substream *substream,
42868bd00fSLiam Girdwood 			      const struct sof_ipc_pcm_params_reply *reply)
43868bd00fSLiam Girdwood {
44ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = spcm->scomp;
45ee1e79b7SRanjani Sridharan 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
46ee1e79b7SRanjani Sridharan 
47868bd00fSLiam Girdwood 	/* validate offset */
48868bd00fSLiam Girdwood 	int ret = snd_sof_ipc_pcm_params(sdev, substream, reply);
49868bd00fSLiam Girdwood 
50868bd00fSLiam Girdwood 	if (ret < 0)
51ee1e79b7SRanjani Sridharan 		dev_err(scomp->dev, "error: got wrong reply for PCM %d\n",
52868bd00fSLiam Girdwood 			spcm->pcm.pcm_id);
53868bd00fSLiam Girdwood 
54868bd00fSLiam Girdwood 	return ret;
55868bd00fSLiam Girdwood }
56868bd00fSLiam Girdwood 
57e2803e61SKeyon Jie /*
58e2803e61SKeyon Jie  * sof pcm period elapse work
59e2803e61SKeyon Jie  */
60858f7a5cSDaniel Baluta static void snd_sof_pcm_period_elapsed_work(struct work_struct *work)
61e2803e61SKeyon Jie {
62e2803e61SKeyon Jie 	struct snd_sof_pcm_stream *sps =
63e2803e61SKeyon Jie 		container_of(work, struct snd_sof_pcm_stream,
64e2803e61SKeyon Jie 			     period_elapsed_work);
65e2803e61SKeyon Jie 
66e2803e61SKeyon Jie 	snd_pcm_period_elapsed(sps->substream);
67e2803e61SKeyon Jie }
68e2803e61SKeyon Jie 
69858f7a5cSDaniel Baluta void snd_sof_pcm_init_elapsed_work(struct work_struct *work)
70858f7a5cSDaniel Baluta {
71858f7a5cSDaniel Baluta 	 INIT_WORK(work, snd_sof_pcm_period_elapsed_work);
72858f7a5cSDaniel Baluta }
73858f7a5cSDaniel Baluta 
74e2803e61SKeyon Jie /*
75e2803e61SKeyon Jie  * sof pcm period elapse, this could be called at irq thread context.
76e2803e61SKeyon Jie  */
77e2803e61SKeyon Jie void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
78e2803e61SKeyon Jie {
791205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
80e2803e61SKeyon Jie 	struct snd_soc_component *component =
81ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
82e2803e61SKeyon Jie 	struct snd_sof_pcm *spcm;
83e2803e61SKeyon Jie 
84ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
85e2803e61SKeyon Jie 	if (!spcm) {
86ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
87e2803e61SKeyon Jie 			"error: period elapsed for unknown stream!\n");
88e2803e61SKeyon Jie 		return;
89e2803e61SKeyon Jie 	}
90e2803e61SKeyon Jie 
91e2803e61SKeyon Jie 	/*
92e2803e61SKeyon Jie 	 * snd_pcm_period_elapsed() can be called in interrupt context
93e2803e61SKeyon Jie 	 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(),
94e2803e61SKeyon Jie 	 * when the PCM is done draining or xrun happened, a STOP IPC will
95e2803e61SKeyon Jie 	 * then be sent and this IPC will hit IPC timeout.
96e2803e61SKeyon Jie 	 * To avoid sending IPC before the previous IPC is handled, we
97e2803e61SKeyon Jie 	 * schedule delayed work here to call the snd_pcm_period_elapsed().
98e2803e61SKeyon Jie 	 */
99e2803e61SKeyon Jie 	schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
100e2803e61SKeyon Jie }
101e2803e61SKeyon Jie EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
102e2803e61SKeyon Jie 
10396da1740SRanjani Sridharan int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream, struct snd_sof_dev *sdev,
104cfe8191bSKai Vehmanen 			 struct snd_sof_pcm *spcm)
105cfe8191bSKai Vehmanen {
106cfe8191bSKai Vehmanen 	struct sof_ipc_stream stream;
107cfe8191bSKai Vehmanen 	struct sof_ipc_reply reply;
108cfe8191bSKai Vehmanen 	int ret;
109cfe8191bSKai Vehmanen 
110cfe8191bSKai Vehmanen 	stream.hdr.size = sizeof(stream);
111cfe8191bSKai Vehmanen 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
112cfe8191bSKai Vehmanen 	stream.comp_id = spcm->stream[substream->stream].comp_id;
113cfe8191bSKai Vehmanen 
114cfe8191bSKai Vehmanen 	/* send IPC to the DSP */
115cfe8191bSKai Vehmanen 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
116cfe8191bSKai Vehmanen 				 sizeof(stream), &reply, sizeof(reply));
117cfe8191bSKai Vehmanen 	if (!ret)
118cfe8191bSKai Vehmanen 		spcm->prepared[substream->stream] = false;
119cfe8191bSKai Vehmanen 
120cfe8191bSKai Vehmanen 	return ret;
121cfe8191bSKai Vehmanen }
122cfe8191bSKai Vehmanen 
1235fcdbb2dSRanjani Sridharan static int sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev,
1245fcdbb2dSRanjani Sridharan 					   struct snd_soc_pcm_runtime *rtd,
1255fcdbb2dSRanjani Sridharan 					   struct snd_sof_pcm *spcm, int dir)
1265fcdbb2dSRanjani Sridharan {
1275fcdbb2dSRanjani Sridharan 	struct snd_soc_dai *dai;
1285fcdbb2dSRanjani Sridharan 	int ret, j;
1295fcdbb2dSRanjani Sridharan 
1305fcdbb2dSRanjani Sridharan 	/* query DAPM for list of connected widgets and set them up */
1315fcdbb2dSRanjani Sridharan 	for_each_rtd_cpu_dais(rtd, j, dai) {
1325fcdbb2dSRanjani Sridharan 		struct snd_soc_dapm_widget_list *list;
1335fcdbb2dSRanjani Sridharan 
1345fcdbb2dSRanjani Sridharan 		ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list,
1355fcdbb2dSRanjani Sridharan 							     dpcm_end_walk_at_be);
1365fcdbb2dSRanjani Sridharan 		if (ret < 0) {
1375fcdbb2dSRanjani Sridharan 			dev_err(sdev->dev, "error: dai %s has no valid %s path\n", dai->name,
1385fcdbb2dSRanjani Sridharan 				dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
1395fcdbb2dSRanjani Sridharan 			return ret;
1405fcdbb2dSRanjani Sridharan 		}
1415fcdbb2dSRanjani Sridharan 
1425fcdbb2dSRanjani Sridharan 		spcm->stream[dir].list = list;
1435fcdbb2dSRanjani Sridharan 
1445fcdbb2dSRanjani Sridharan 		ret = sof_widget_list_setup(sdev, spcm, dir);
1455fcdbb2dSRanjani Sridharan 		if (ret < 0) {
1465fcdbb2dSRanjani Sridharan 			dev_err(sdev->dev, "error: failed widget list set up for pcm %d dir %d\n",
1475fcdbb2dSRanjani Sridharan 				spcm->pcm.pcm_id, dir);
1485fcdbb2dSRanjani Sridharan 			spcm->stream[dir].list = NULL;
1495fcdbb2dSRanjani Sridharan 			snd_soc_dapm_dai_free_widgets(&list);
1505fcdbb2dSRanjani Sridharan 			return ret;
1515fcdbb2dSRanjani Sridharan 		}
1525fcdbb2dSRanjani Sridharan 	}
1535fcdbb2dSRanjani Sridharan 
1545fcdbb2dSRanjani Sridharan 	return 0;
1555fcdbb2dSRanjani Sridharan }
1565fcdbb2dSRanjani Sridharan 
1571c91d77eSKuninori Morimoto static int sof_pcm_hw_params(struct snd_soc_component *component,
1581c91d77eSKuninori Morimoto 			     struct snd_pcm_substream *substream,
159868bd00fSLiam Girdwood 			     struct snd_pcm_hw_params *params)
160868bd00fSLiam Girdwood {
1611205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
162868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
163868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
164868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
165868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params pcm;
166868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params_reply ipc_params_reply;
167868bd00fSLiam Girdwood 	int ret;
168868bd00fSLiam Girdwood 
169868bd00fSLiam Girdwood 	/* nothing to do for BE */
170868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
171868bd00fSLiam Girdwood 		return 0;
172868bd00fSLiam Girdwood 
173ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
174868bd00fSLiam Girdwood 	if (!spcm)
175868bd00fSLiam Girdwood 		return -EINVAL;
176868bd00fSLiam Girdwood 
177cfe8191bSKai Vehmanen 	/*
178cfe8191bSKai Vehmanen 	 * Handle repeated calls to hw_params() without free_pcm() in
179cfe8191bSKai Vehmanen 	 * between. At least ALSA OSS emulation depends on this.
180cfe8191bSKai Vehmanen 	 */
181cfe8191bSKai Vehmanen 	if (spcm->prepared[substream->stream]) {
182cfe8191bSKai Vehmanen 		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
183cfe8191bSKai Vehmanen 		if (ret < 0)
184cfe8191bSKai Vehmanen 			return ret;
185cfe8191bSKai Vehmanen 	}
186cfe8191bSKai Vehmanen 
187ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
188868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream);
189868bd00fSLiam Girdwood 
190868bd00fSLiam Girdwood 	memset(&pcm, 0, sizeof(pcm));
191868bd00fSLiam Girdwood 
19257e960f0STakashi Iwai 	/* create compressed page table for audio firmware */
19357e960f0STakashi Iwai 	if (runtime->buffer_changed) {
1941c91d77eSKuninori Morimoto 		ret = create_page_table(component, substream, runtime->dma_area,
195868bd00fSLiam Girdwood 					runtime->dma_bytes);
196868bd00fSLiam Girdwood 		if (ret < 0)
197868bd00fSLiam Girdwood 			return ret;
198868bd00fSLiam Girdwood 	}
199868bd00fSLiam Girdwood 
200868bd00fSLiam Girdwood 	/* number of pages should be rounded up */
201868bd00fSLiam Girdwood 	pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
202868bd00fSLiam Girdwood 
203868bd00fSLiam Girdwood 	/* set IPC PCM parameters */
204868bd00fSLiam Girdwood 	pcm.hdr.size = sizeof(pcm);
205868bd00fSLiam Girdwood 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
206868bd00fSLiam Girdwood 	pcm.comp_id = spcm->stream[substream->stream].comp_id;
207868bd00fSLiam Girdwood 	pcm.params.hdr.size = sizeof(pcm.params);
208868bd00fSLiam Girdwood 	pcm.params.buffer.phy_addr =
209868bd00fSLiam Girdwood 		spcm->stream[substream->stream].page_table.addr;
210868bd00fSLiam Girdwood 	pcm.params.buffer.size = runtime->dma_bytes;
211868bd00fSLiam Girdwood 	pcm.params.direction = substream->stream;
212868bd00fSLiam Girdwood 	pcm.params.sample_valid_bytes = params_width(params) >> 3;
213868bd00fSLiam Girdwood 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
214868bd00fSLiam Girdwood 	pcm.params.rate = params_rate(params);
215868bd00fSLiam Girdwood 	pcm.params.channels = params_channels(params);
216868bd00fSLiam Girdwood 	pcm.params.host_period_bytes = params_period_bytes(params);
217868bd00fSLiam Girdwood 
218868bd00fSLiam Girdwood 	/* container size */
219868bd00fSLiam Girdwood 	ret = snd_pcm_format_physical_width(params_format(params));
220868bd00fSLiam Girdwood 	if (ret < 0)
221868bd00fSLiam Girdwood 		return ret;
222868bd00fSLiam Girdwood 	pcm.params.sample_container_bytes = ret >> 3;
223868bd00fSLiam Girdwood 
224868bd00fSLiam Girdwood 	/* format */
225868bd00fSLiam Girdwood 	switch (params_format(params)) {
226868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S16:
227868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
228868bd00fSLiam Girdwood 		break;
229868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S24:
230868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
231868bd00fSLiam Girdwood 		break;
232868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S32:
233868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
234868bd00fSLiam Girdwood 		break;
235868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_FLOAT:
236868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
237868bd00fSLiam Girdwood 		break;
238868bd00fSLiam Girdwood 	default:
239868bd00fSLiam Girdwood 		return -EINVAL;
240868bd00fSLiam Girdwood 	}
241868bd00fSLiam Girdwood 
242868bd00fSLiam Girdwood 	/* firmware already configured host stream */
243868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_hw_params(sdev,
244868bd00fSLiam Girdwood 					     substream,
245868bd00fSLiam Girdwood 					     params,
246868bd00fSLiam Girdwood 					     &pcm.params);
247868bd00fSLiam Girdwood 	if (ret < 0) {
248ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw params failed\n");
249868bd00fSLiam Girdwood 		return ret;
250868bd00fSLiam Girdwood 	}
251868bd00fSLiam Girdwood 
252ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag);
253868bd00fSLiam Girdwood 
2545fcdbb2dSRanjani Sridharan 	/* if this is a repeated hw_params without hw_free, skip setting up widgets */
2555fcdbb2dSRanjani Sridharan 	if (!spcm->stream[substream->stream].list) {
2565fcdbb2dSRanjani Sridharan 		ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, substream->stream);
2575fcdbb2dSRanjani Sridharan 		if (ret < 0)
2585fcdbb2dSRanjani Sridharan 			return ret;
2595fcdbb2dSRanjani Sridharan 	}
2605fcdbb2dSRanjani Sridharan 
2615fcdbb2dSRanjani Sridharan 	/* send hw_params IPC to the DSP */
262868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
263868bd00fSLiam Girdwood 				 &ipc_params_reply, sizeof(ipc_params_reply));
264868bd00fSLiam Girdwood 	if (ret < 0) {
265ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: hw params ipc failed for stream %d\n",
266868bd00fSLiam Girdwood 			pcm.params.stream_tag);
267868bd00fSLiam Girdwood 		return ret;
268868bd00fSLiam Girdwood 	}
269868bd00fSLiam Girdwood 
270868bd00fSLiam Girdwood 	ret = sof_pcm_dsp_params(spcm, substream, &ipc_params_reply);
271868bd00fSLiam Girdwood 	if (ret < 0)
272868bd00fSLiam Girdwood 		return ret;
273868bd00fSLiam Girdwood 
27404c80277SKai Vehmanen 	spcm->prepared[substream->stream] = true;
27504c80277SKai Vehmanen 
276868bd00fSLiam Girdwood 	/* save pcm hw_params */
277868bd00fSLiam Girdwood 	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
278868bd00fSLiam Girdwood 
279868bd00fSLiam Girdwood 	return ret;
280868bd00fSLiam Girdwood }
281868bd00fSLiam Girdwood 
2821c91d77eSKuninori Morimoto static int sof_pcm_hw_free(struct snd_soc_component *component,
2831c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
284868bd00fSLiam Girdwood {
2851205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
286868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
287868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
288e66e52c5SKai Vehmanen 	int ret, err = 0;
289868bd00fSLiam Girdwood 
290868bd00fSLiam Girdwood 	/* nothing to do for BE */
291868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
292868bd00fSLiam Girdwood 		return 0;
293868bd00fSLiam Girdwood 
294ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
295868bd00fSLiam Girdwood 	if (!spcm)
296868bd00fSLiam Girdwood 		return -EINVAL;
297868bd00fSLiam Girdwood 
298ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
299ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
300868bd00fSLiam Girdwood 
301e66e52c5SKai Vehmanen 	if (spcm->prepared[substream->stream]) {
302a49b6871SKai Vehmanen 		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
303e66e52c5SKai Vehmanen 		if (ret < 0)
304e66e52c5SKai Vehmanen 			err = ret;
305e66e52c5SKai Vehmanen 	}
306868bd00fSLiam Girdwood 
3075fcdbb2dSRanjani Sridharan 	ret = sof_widget_list_free(sdev, spcm, substream->stream);
3085fcdbb2dSRanjani Sridharan 	if (ret < 0)
3095fcdbb2dSRanjani Sridharan 		err = ret;
3105fcdbb2dSRanjani Sridharan 
311e2803e61SKeyon Jie 	cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
312e2803e61SKeyon Jie 
31393146bc2SRanjani Sridharan 	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
314e66e52c5SKai Vehmanen 	if (ret < 0) {
315ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw free failed\n");
316e66e52c5SKai Vehmanen 		err = ret;
317e66e52c5SKai Vehmanen 	}
31893146bc2SRanjani Sridharan 
319e66e52c5SKai Vehmanen 	return err;
320868bd00fSLiam Girdwood }
321868bd00fSLiam Girdwood 
3221c91d77eSKuninori Morimoto static int sof_pcm_prepare(struct snd_soc_component *component,
3231c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
324868bd00fSLiam Girdwood {
3251205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
326868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
327868bd00fSLiam Girdwood 	int ret;
328868bd00fSLiam Girdwood 
329868bd00fSLiam Girdwood 	/* nothing to do for BE */
330868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
331868bd00fSLiam Girdwood 		return 0;
332868bd00fSLiam Girdwood 
333ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
334868bd00fSLiam Girdwood 	if (!spcm)
335868bd00fSLiam Girdwood 		return -EINVAL;
336868bd00fSLiam Girdwood 
33704c80277SKai Vehmanen 	if (spcm->prepared[substream->stream])
338868bd00fSLiam Girdwood 		return 0;
339868bd00fSLiam Girdwood 
340ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
341ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
342868bd00fSLiam Girdwood 
343868bd00fSLiam Girdwood 	/* set hw_params */
3441c91d77eSKuninori Morimoto 	ret = sof_pcm_hw_params(component,
3451c91d77eSKuninori Morimoto 				substream, &spcm->params[substream->stream]);
346868bd00fSLiam Girdwood 	if (ret < 0) {
347ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
348ee1e79b7SRanjani Sridharan 			"error: set pcm hw_params after resume\n");
349868bd00fSLiam Girdwood 		return ret;
350868bd00fSLiam Girdwood 	}
351868bd00fSLiam Girdwood 
352868bd00fSLiam Girdwood 	return 0;
353868bd00fSLiam Girdwood }
354868bd00fSLiam Girdwood 
355868bd00fSLiam Girdwood /*
356868bd00fSLiam Girdwood  * FE dai link trigger actions are always executed in non-atomic context because
357868bd00fSLiam Girdwood  * they involve IPC's.
358868bd00fSLiam Girdwood  */
3591c91d77eSKuninori Morimoto static int sof_pcm_trigger(struct snd_soc_component *component,
3601c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream, int cmd)
361868bd00fSLiam Girdwood {
3621205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
363868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
364868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
365868bd00fSLiam Girdwood 	struct sof_ipc_stream stream;
366868bd00fSLiam Girdwood 	struct sof_ipc_reply reply;
36704c80277SKai Vehmanen 	bool reset_hw_params = false;
3685fcdbb2dSRanjani Sridharan 	bool free_widget_list = false;
3690a1b0834SPan Xiuli 	bool ipc_first = false;
370868bd00fSLiam Girdwood 	int ret;
371868bd00fSLiam Girdwood 
372868bd00fSLiam Girdwood 	/* nothing to do for BE */
373868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
374868bd00fSLiam Girdwood 		return 0;
375868bd00fSLiam Girdwood 
376ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
377868bd00fSLiam Girdwood 	if (!spcm)
378868bd00fSLiam Girdwood 		return -EINVAL;
379868bd00fSLiam Girdwood 
380ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
381868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream, cmd);
382868bd00fSLiam Girdwood 
383868bd00fSLiam Girdwood 	stream.hdr.size = sizeof(stream);
384868bd00fSLiam Girdwood 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
385868bd00fSLiam Girdwood 	stream.comp_id = spcm->stream[substream->stream].comp_id;
386868bd00fSLiam Girdwood 
387868bd00fSLiam Girdwood 	switch (cmd) {
388868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
389868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
3900a1b0834SPan Xiuli 		ipc_first = true;
391868bd00fSLiam Girdwood 		break;
392868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
393868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
394868bd00fSLiam Girdwood 		break;
395868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
396ac8c046fSKeyon Jie 		if (spcm->stream[substream->stream].suspend_ignored) {
397ac8c046fSKeyon Jie 			/*
398ac8c046fSKeyon Jie 			 * this case will be triggered when INFO_RESUME is
399ac8c046fSKeyon Jie 			 * supported, no need to resume streams that remained
400ac8c046fSKeyon Jie 			 * enabled in D0ix.
401ac8c046fSKeyon Jie 			 */
402ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = false;
403ac8c046fSKeyon Jie 			return 0;
404ac8c046fSKeyon Jie 		}
405ac8c046fSKeyon Jie 
406868bd00fSLiam Girdwood 		/* set up hw_params */
4071c91d77eSKuninori Morimoto 		ret = sof_pcm_prepare(component, substream);
408868bd00fSLiam Girdwood 		if (ret < 0) {
409ee1e79b7SRanjani Sridharan 			dev_err(component->dev,
410868bd00fSLiam Girdwood 				"error: failed to set up hw_params upon resume\n");
411868bd00fSLiam Girdwood 			return ret;
412868bd00fSLiam Girdwood 		}
413868bd00fSLiam Girdwood 
414df561f66SGustavo A. R. Silva 		fallthrough;
415868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
416ac8c046fSKeyon Jie 		if (spcm->stream[substream->stream].suspend_ignored) {
417ac8c046fSKeyon Jie 			/*
418ac8c046fSKeyon Jie 			 * This case will be triggered when INFO_RESUME is
419ac8c046fSKeyon Jie 			 * not supported, no need to re-start streams that
420ac8c046fSKeyon Jie 			 * remained enabled in D0ix.
421ac8c046fSKeyon Jie 			 */
422ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = false;
423ac8c046fSKeyon Jie 			return 0;
424ac8c046fSKeyon Jie 		}
425868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
426868bd00fSLiam Girdwood 		break;
427868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
428043ae13bSRanjani Sridharan 		if (sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
429ac8c046fSKeyon Jie 		    spcm->stream[substream->stream].d0i3_compatible) {
430ac8c046fSKeyon Jie 			/*
431ac8c046fSKeyon Jie 			 * trap the event, not sending trigger stop to
432ac8c046fSKeyon Jie 			 * prevent the FW pipelines from being stopped,
433ac8c046fSKeyon Jie 			 * and mark the flag to ignore the upcoming DAPM
434ac8c046fSKeyon Jie 			 * PM events.
435ac8c046fSKeyon Jie 			 */
436ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = true;
437ac8c046fSKeyon Jie 			return 0;
438ac8c046fSKeyon Jie 		}
4395fcdbb2dSRanjani Sridharan 		free_widget_list = true;
440df561f66SGustavo A. R. Silva 		fallthrough;
441868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
442868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
4430a1b0834SPan Xiuli 		ipc_first = true;
44404c80277SKai Vehmanen 		reset_hw_params = true;
445868bd00fSLiam Girdwood 		break;
446868bd00fSLiam Girdwood 	default:
447ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: unhandled trigger cmd %d\n",
448ee1e79b7SRanjani Sridharan 			cmd);
449868bd00fSLiam Girdwood 		return -EINVAL;
450868bd00fSLiam Girdwood 	}
451868bd00fSLiam Girdwood 
4520a1b0834SPan Xiuli 	/*
4530a1b0834SPan Xiuli 	 * DMA and IPC sequence is different for start and stop. Need to send
4540a1b0834SPan Xiuli 	 * STOP IPC before stop DMA
4550a1b0834SPan Xiuli 	 */
4560a1b0834SPan Xiuli 	if (!ipc_first)
457868bd00fSLiam Girdwood 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
458868bd00fSLiam Girdwood 
459868bd00fSLiam Girdwood 	/* send IPC to the DSP */
460868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
461868bd00fSLiam Girdwood 				 sizeof(stream), &reply, sizeof(reply));
462868bd00fSLiam Girdwood 
4630a1b0834SPan Xiuli 	/* need to STOP DMA even if STOP IPC failed */
4640a1b0834SPan Xiuli 	if (ipc_first)
4650a1b0834SPan Xiuli 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
4660a1b0834SPan Xiuli 
4670a1b0834SPan Xiuli 	/* free PCM if reset_hw_params is set and the STOP IPC is successful */
4685fcdbb2dSRanjani Sridharan 	if (!ret && reset_hw_params) {
469*d9a72465SRanjani Sridharan 		ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream,
470*d9a72465SRanjani Sridharan 					  free_widget_list);
4715fcdbb2dSRanjani Sridharan 		if (ret < 0)
4725fcdbb2dSRanjani Sridharan 			return ret;
4735fcdbb2dSRanjani Sridharan 	}
474a49b6871SKai Vehmanen 
475868bd00fSLiam Girdwood 	return ret;
476868bd00fSLiam Girdwood }
477868bd00fSLiam Girdwood 
4781c91d77eSKuninori Morimoto static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
4791c91d77eSKuninori Morimoto 					 struct snd_pcm_substream *substream)
480868bd00fSLiam Girdwood {
4811205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
482868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
483868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
484868bd00fSLiam Girdwood 	snd_pcm_uframes_t host, dai;
485868bd00fSLiam Girdwood 
486868bd00fSLiam Girdwood 	/* nothing to do for BE */
487868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
488868bd00fSLiam Girdwood 		return 0;
489868bd00fSLiam Girdwood 
490868bd00fSLiam Girdwood 	/* use dsp ops pointer callback directly if set */
491868bd00fSLiam Girdwood 	if (sof_ops(sdev)->pcm_pointer)
492868bd00fSLiam Girdwood 		return sof_ops(sdev)->pcm_pointer(sdev, substream);
493868bd00fSLiam Girdwood 
494ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
495868bd00fSLiam Girdwood 	if (!spcm)
496868bd00fSLiam Girdwood 		return -EINVAL;
497868bd00fSLiam Girdwood 
498868bd00fSLiam Girdwood 	/* read position from DSP */
499868bd00fSLiam Girdwood 	host = bytes_to_frames(substream->runtime,
500868bd00fSLiam Girdwood 			       spcm->stream[substream->stream].posn.host_posn);
501868bd00fSLiam Girdwood 	dai = bytes_to_frames(substream->runtime,
502868bd00fSLiam Girdwood 			      spcm->stream[substream->stream].posn.dai_posn);
503868bd00fSLiam Girdwood 
504277ff236SPierre-Louis Bossart 	dev_vdbg(component->dev,
505ee1e79b7SRanjani Sridharan 		 "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
506868bd00fSLiam Girdwood 		 spcm->pcm.pcm_id, substream->stream, host, dai);
507868bd00fSLiam Girdwood 
508868bd00fSLiam Girdwood 	return host;
509868bd00fSLiam Girdwood }
510868bd00fSLiam Girdwood 
5111c91d77eSKuninori Morimoto static int sof_pcm_open(struct snd_soc_component *component,
5121c91d77eSKuninori Morimoto 			struct snd_pcm_substream *substream)
513868bd00fSLiam Girdwood {
5141205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
515868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
516868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
51727e322faSPierre-Louis Bossart 	const struct snd_sof_dsp_ops *ops = sof_ops(sdev);
518868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
519868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
520868bd00fSLiam Girdwood 	int ret;
521868bd00fSLiam Girdwood 
522868bd00fSLiam Girdwood 	/* nothing to do for BE */
523868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
524868bd00fSLiam Girdwood 		return 0;
525868bd00fSLiam Girdwood 
526ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
527868bd00fSLiam Girdwood 	if (!spcm)
528868bd00fSLiam Girdwood 		return -EINVAL;
529868bd00fSLiam Girdwood 
530ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
531ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
532868bd00fSLiam Girdwood 
533868bd00fSLiam Girdwood 
534868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[substream->stream];
535868bd00fSLiam Girdwood 
536868bd00fSLiam Girdwood 	/* set runtime config */
53727e322faSPierre-Louis Bossart 	runtime->hw.info = ops->hw_info; /* platform-specific */
53827e322faSPierre-Louis Bossart 
5399983ac49SKai Vehmanen 	/* set any runtime constraints based on topology */
540868bd00fSLiam Girdwood 	runtime->hw.formats = le64_to_cpu(caps->formats);
541868bd00fSLiam Girdwood 	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
542868bd00fSLiam Girdwood 	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
543868bd00fSLiam Girdwood 	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
544868bd00fSLiam Girdwood 	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
545868bd00fSLiam Girdwood 
546868bd00fSLiam Girdwood 	/*
547868bd00fSLiam Girdwood 	 * caps->buffer_size_min is not used since the
548868bd00fSLiam Girdwood 	 * snd_pcm_hardware structure only defines buffer_bytes_max
549868bd00fSLiam Girdwood 	 */
550868bd00fSLiam Girdwood 	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
551868bd00fSLiam Girdwood 
552ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period min %zd max %zd bytes\n",
553868bd00fSLiam Girdwood 		runtime->hw.period_bytes_min,
554868bd00fSLiam Girdwood 		runtime->hw.period_bytes_max);
555ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period count %d max %d\n",
556868bd00fSLiam Girdwood 		runtime->hw.periods_min,
557868bd00fSLiam Girdwood 		runtime->hw.periods_max);
558ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "buffer max %zd bytes\n",
559868bd00fSLiam Girdwood 		runtime->hw.buffer_bytes_max);
560868bd00fSLiam Girdwood 
561868bd00fSLiam Girdwood 	/* set wait time - TODO: come from topology */
562868bd00fSLiam Girdwood 	substream->wait_time = 500;
563868bd00fSLiam Girdwood 
564868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.host_posn = 0;
565868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.dai_posn = 0;
566868bd00fSLiam Girdwood 	spcm->stream[substream->stream].substream = substream;
56704c80277SKai Vehmanen 	spcm->prepared[substream->stream] = false;
568868bd00fSLiam Girdwood 
569868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_open(sdev, substream);
57014a2212dSRanjani Sridharan 	if (ret < 0)
571ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm open failed %d\n", ret);
572868bd00fSLiam Girdwood 
573868bd00fSLiam Girdwood 	return ret;
574868bd00fSLiam Girdwood }
575868bd00fSLiam Girdwood 
5761c91d77eSKuninori Morimoto static int sof_pcm_close(struct snd_soc_component *component,
5771c91d77eSKuninori Morimoto 			 struct snd_pcm_substream *substream)
578868bd00fSLiam Girdwood {
5791205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
580868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
581868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
582868bd00fSLiam Girdwood 	int err;
583868bd00fSLiam Girdwood 
584868bd00fSLiam Girdwood 	/* nothing to do for BE */
585868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
586868bd00fSLiam Girdwood 		return 0;
587868bd00fSLiam Girdwood 
588ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
589868bd00fSLiam Girdwood 	if (!spcm)
590868bd00fSLiam Girdwood 		return -EINVAL;
591868bd00fSLiam Girdwood 
592ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
593ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
594868bd00fSLiam Girdwood 
595868bd00fSLiam Girdwood 	err = snd_sof_pcm_platform_close(sdev, substream);
596868bd00fSLiam Girdwood 	if (err < 0) {
597ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm close failed %d\n",
598868bd00fSLiam Girdwood 			err);
599868bd00fSLiam Girdwood 		/*
600868bd00fSLiam Girdwood 		 * keep going, no point in preventing the close
601868bd00fSLiam Girdwood 		 * from happening
602868bd00fSLiam Girdwood 		 */
603868bd00fSLiam Girdwood 	}
604868bd00fSLiam Girdwood 
605868bd00fSLiam Girdwood 	return 0;
606868bd00fSLiam Girdwood }
607868bd00fSLiam Girdwood 
608868bd00fSLiam Girdwood /*
609868bd00fSLiam Girdwood  * Pre-allocate playback/capture audio buffer pages.
610868bd00fSLiam Girdwood  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
611868bd00fSLiam Girdwood  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
612868bd00fSLiam Girdwood  */
6131c91d77eSKuninori Morimoto static int sof_pcm_new(struct snd_soc_component *component,
6141c91d77eSKuninori Morimoto 		       struct snd_soc_pcm_runtime *rtd)
615868bd00fSLiam Girdwood {
616868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
617868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
618868bd00fSLiam Girdwood 	struct snd_pcm *pcm = rtd->pcm;
619868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
620868bd00fSLiam Girdwood 	int stream = SNDRV_PCM_STREAM_PLAYBACK;
621868bd00fSLiam Girdwood 
622868bd00fSLiam Girdwood 	/* find SOF PCM for this RTD */
623ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
624868bd00fSLiam Girdwood 	if (!spcm) {
625ee1e79b7SRanjani Sridharan 		dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
626868bd00fSLiam Girdwood 			 rtd->dai_link->id);
627868bd00fSLiam Girdwood 		return 0;
628868bd00fSLiam Girdwood 	}
629868bd00fSLiam Girdwood 
630ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
631868bd00fSLiam Girdwood 
632868bd00fSLiam Girdwood 	/* do we need to pre-allocate playback audio buffer pages */
633868bd00fSLiam Girdwood 	if (!spcm->pcm.playback)
634868bd00fSLiam Girdwood 		goto capture;
635868bd00fSLiam Girdwood 
636868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
637868bd00fSLiam Girdwood 
638868bd00fSLiam Girdwood 	/* pre-allocate playback audio buffer pages */
639ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
640ee1e79b7SRanjani Sridharan 		"spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
641868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
642868bd00fSLiam Girdwood 
6434f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
6444f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL playback substream!\n");
6454f7f9564SGuennadi Liakhovetski 		return -EINVAL;
6464f7f9564SGuennadi Liakhovetski 	}
6474f7f9564SGuennadi Liakhovetski 
64857e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
649868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
650e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
651868bd00fSLiam Girdwood capture:
652868bd00fSLiam Girdwood 	stream = SNDRV_PCM_STREAM_CAPTURE;
653868bd00fSLiam Girdwood 
654868bd00fSLiam Girdwood 	/* do we need to pre-allocate capture audio buffer pages */
655868bd00fSLiam Girdwood 	if (!spcm->pcm.capture)
656868bd00fSLiam Girdwood 		return 0;
657868bd00fSLiam Girdwood 
658868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
659868bd00fSLiam Girdwood 
660868bd00fSLiam Girdwood 	/* pre-allocate capture audio buffer pages */
661ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
662ee1e79b7SRanjani Sridharan 		"spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
663868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
664868bd00fSLiam Girdwood 
6654f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
6664f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL capture substream!\n");
6674f7f9564SGuennadi Liakhovetski 		return -EINVAL;
6684f7f9564SGuennadi Liakhovetski 	}
6694f7f9564SGuennadi Liakhovetski 
67057e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
671868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
672e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
673868bd00fSLiam Girdwood 
674868bd00fSLiam Girdwood 	return 0;
675868bd00fSLiam Girdwood }
676868bd00fSLiam Girdwood 
677c943a586SJaska Uimonen static void ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
678c943a586SJaska Uimonen 					    struct snd_pcm_hw_params *params)
679c943a586SJaska Uimonen {
680c943a586SJaska Uimonen 	struct sof_ipc_dai_config *config;
681c943a586SJaska Uimonen 	struct snd_sof_dai *dai;
682c943a586SJaska Uimonen 	int i;
683c943a586SJaska Uimonen 
684c943a586SJaska Uimonen 	/*
685c943a586SJaska Uimonen 	 * Search for all matching DAIs as we can have both playback and capture DAI
686c943a586SJaska Uimonen 	 * associated with the same link.
687c943a586SJaska Uimonen 	 */
688c943a586SJaska Uimonen 	list_for_each_entry(dai, &sdev->dai_list, list) {
689c943a586SJaska Uimonen 		if (!dai->name || strcmp(link_name, dai->name))
690c943a586SJaska Uimonen 			continue;
691c943a586SJaska Uimonen 		for (i = 0; i < dai->number_configs; i++) {
692c943a586SJaska Uimonen 			config = &dai->dai_config[i];
693c943a586SJaska Uimonen 			if (config->ssp.fsync_rate == params_rate(params)) {
694c943a586SJaska Uimonen 				dev_dbg(sdev->dev, "DAI config %d matches pcm hw params\n", i);
695c943a586SJaska Uimonen 				dai->current_config = i;
696c943a586SJaska Uimonen 				break;
697c943a586SJaska Uimonen 			}
698c943a586SJaska Uimonen 		}
699c943a586SJaska Uimonen 	}
700c943a586SJaska Uimonen }
701c943a586SJaska Uimonen 
702868bd00fSLiam Girdwood /* fixup the BE DAI link to match any values from topology */
703f805e7e0SRanjani Sridharan int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
704868bd00fSLiam Girdwood {
705868bd00fSLiam Girdwood 	struct snd_interval *rate = hw_param_interval(params,
706868bd00fSLiam Girdwood 			SNDRV_PCM_HW_PARAM_RATE);
707868bd00fSLiam Girdwood 	struct snd_interval *channels = hw_param_interval(params,
708868bd00fSLiam Girdwood 						SNDRV_PCM_HW_PARAM_CHANNELS);
709868bd00fSLiam Girdwood 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
710868bd00fSLiam Girdwood 	struct snd_soc_component *component =
711ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
712868bd00fSLiam Girdwood 	struct snd_sof_dai *dai =
713ee1e79b7SRanjani Sridharan 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
714c943a586SJaska Uimonen 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
715fd045558Sranderwang 	struct snd_soc_dpcm *dpcm;
716868bd00fSLiam Girdwood 
717868bd00fSLiam Girdwood 	/* no topology exists for this BE, try a common configuration */
718868bd00fSLiam Girdwood 	if (!dai) {
719ee1e79b7SRanjani Sridharan 		dev_warn(component->dev,
720ee1e79b7SRanjani Sridharan 			 "warning: no topology found for BE DAI %s config\n",
721868bd00fSLiam Girdwood 			 rtd->dai_link->name);
722868bd00fSLiam Girdwood 
723868bd00fSLiam Girdwood 		/*  set 48k, stereo, 16bits by default */
724868bd00fSLiam Girdwood 		rate->min = 48000;
725868bd00fSLiam Girdwood 		rate->max = 48000;
726868bd00fSLiam Girdwood 
727868bd00fSLiam Girdwood 		channels->min = 2;
728868bd00fSLiam Girdwood 		channels->max = 2;
729868bd00fSLiam Girdwood 
730868bd00fSLiam Girdwood 		snd_mask_none(fmt);
731868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
732868bd00fSLiam Girdwood 
733868bd00fSLiam Girdwood 		return 0;
734868bd00fSLiam Girdwood 	}
735868bd00fSLiam Girdwood 
736868bd00fSLiam Girdwood 	/* read format from topology */
737868bd00fSLiam Girdwood 	snd_mask_none(fmt);
738868bd00fSLiam Girdwood 
739868bd00fSLiam Girdwood 	switch (dai->comp_dai.config.frame_fmt) {
740868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S16_LE:
741868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
742868bd00fSLiam Girdwood 		break;
743868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S24_4LE:
744868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
745868bd00fSLiam Girdwood 		break;
746868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S32_LE:
747868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
748868bd00fSLiam Girdwood 		break;
749868bd00fSLiam Girdwood 	default:
750ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: No available DAI format!\n");
751868bd00fSLiam Girdwood 		return -EINVAL;
752868bd00fSLiam Girdwood 	}
753868bd00fSLiam Girdwood 
754868bd00fSLiam Girdwood 	/* read rate and channels from topology */
755868bd00fSLiam Girdwood 	switch (dai->dai_config->type) {
756868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_SSP:
757c943a586SJaska Uimonen 		/* search for config to pcm params match, if not found use default */
758c943a586SJaska Uimonen 		ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);
759c943a586SJaska Uimonen 
760c1c03888SJaska Uimonen 		rate->min = dai->dai_config[dai->current_config].ssp.fsync_rate;
761c1c03888SJaska Uimonen 		rate->max = dai->dai_config[dai->current_config].ssp.fsync_rate;
762c1c03888SJaska Uimonen 		channels->min = dai->dai_config[dai->current_config].ssp.tdm_slots;
763c1c03888SJaska Uimonen 		channels->max = dai->dai_config[dai->current_config].ssp.tdm_slots;
764868bd00fSLiam Girdwood 
765ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
766868bd00fSLiam Girdwood 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
767ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
768868bd00fSLiam Girdwood 			"channels_min: %d channels_max: %d\n",
769868bd00fSLiam Girdwood 			channels->min, channels->max);
770868bd00fSLiam Girdwood 
771868bd00fSLiam Girdwood 		break;
772868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_DMIC:
773868bd00fSLiam Girdwood 		/* DMIC only supports 16 or 32 bit formats */
774868bd00fSLiam Girdwood 		if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
775ee1e79b7SRanjani Sridharan 			dev_err(component->dev,
776868bd00fSLiam Girdwood 				"error: invalid fmt %d for DAI type %d\n",
777868bd00fSLiam Girdwood 				dai->comp_dai.config.frame_fmt,
778868bd00fSLiam Girdwood 				dai->dai_config->type);
779868bd00fSLiam Girdwood 		}
780868bd00fSLiam Girdwood 		break;
781868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_HDA:
782fd045558Sranderwang 		/*
783135ab457SPierre-Louis Bossart 		 * HDAudio does not follow the default trigger
784fd045558Sranderwang 		 * sequence due to firmware implementation
785fd045558Sranderwang 		 */
786fd045558Sranderwang 		for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
787fd045558Sranderwang 			struct snd_soc_pcm_runtime *fe = dpcm->fe;
788fd045558Sranderwang 
789fd045558Sranderwang 			fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
790fd045558Sranderwang 				SND_SOC_DPCM_TRIGGER_POST;
791fd045558Sranderwang 		}
792868bd00fSLiam Girdwood 		break;
7936e3360cdSPierre-Louis Bossart 	case SOF_DAI_INTEL_ALH:
794e1711b1fSRander Wang 		/*
795e1711b1fSRander Wang 		 * Dai could run with different channel count compared with
796e1711b1fSRander Wang 		 * front end, so get dai channel count from topology
797e1711b1fSRander Wang 		 */
798e1711b1fSRander Wang 		channels->min = dai->dai_config->alh.channels;
799e1711b1fSRander Wang 		channels->max = dai->dai_config->alh.channels;
8006e3360cdSPierre-Louis Bossart 		break;
801a4eff5f8SDaniel Baluta 	case SOF_DAI_IMX_ESAI:
80251b0243aSDaniel Baluta 		rate->min = dai->dai_config->esai.fsync_rate;
80351b0243aSDaniel Baluta 		rate->max = dai->dai_config->esai.fsync_rate;
804a4eff5f8SDaniel Baluta 		channels->min = dai->dai_config->esai.tdm_slots;
805a4eff5f8SDaniel Baluta 		channels->max = dai->dai_config->esai.tdm_slots;
806a4eff5f8SDaniel Baluta 
807ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
80851b0243aSDaniel Baluta 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
80951b0243aSDaniel Baluta 		dev_dbg(component->dev,
810a4eff5f8SDaniel Baluta 			"channels_min: %d channels_max: %d\n",
811a4eff5f8SDaniel Baluta 			channels->min, channels->max);
812a4eff5f8SDaniel Baluta 		break;
813b72bfcffSYC Hung 	case SOF_DAI_MEDIATEK_AFE:
814b72bfcffSYC Hung 		rate->min = dai->dai_config->afe.rate;
815b72bfcffSYC Hung 		rate->max = dai->dai_config->afe.rate;
816b72bfcffSYC Hung 		channels->min = dai->dai_config->afe.channels;
817b72bfcffSYC Hung 		channels->max = dai->dai_config->afe.channels;
818b72bfcffSYC Hung 
819b72bfcffSYC Hung 		dev_dbg(component->dev,
820b72bfcffSYC Hung 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
821b72bfcffSYC Hung 		dev_dbg(component->dev,
822b72bfcffSYC Hung 			"channels_min: %d channels_max: %d\n",
823b72bfcffSYC Hung 			channels->min, channels->max);
824b72bfcffSYC Hung 		break;
825d88cbd6fSGuido Roncarolo 	case SOF_DAI_IMX_SAI:
82651b0243aSDaniel Baluta 		rate->min = dai->dai_config->sai.fsync_rate;
82751b0243aSDaniel Baluta 		rate->max = dai->dai_config->sai.fsync_rate;
828d88cbd6fSGuido Roncarolo 		channels->min = dai->dai_config->sai.tdm_slots;
829d88cbd6fSGuido Roncarolo 		channels->max = dai->dai_config->sai.tdm_slots;
830d88cbd6fSGuido Roncarolo 
831d88cbd6fSGuido Roncarolo 		dev_dbg(component->dev,
83251b0243aSDaniel Baluta 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
83351b0243aSDaniel Baluta 		dev_dbg(component->dev,
834d88cbd6fSGuido Roncarolo 			"channels_min: %d channels_max: %d\n",
835d88cbd6fSGuido Roncarolo 			channels->min, channels->max);
836d88cbd6fSGuido Roncarolo 		break;
837efb931cdSAjit Kumar Pandey 	case SOF_DAI_AMD_BT:
838efb931cdSAjit Kumar Pandey 		rate->min = dai->dai_config->acpbt.fsync_rate;
839efb931cdSAjit Kumar Pandey 		rate->max = dai->dai_config->acpbt.fsync_rate;
840efb931cdSAjit Kumar Pandey 		channels->min = dai->dai_config->acpbt.tdm_slots;
841efb931cdSAjit Kumar Pandey 		channels->max = dai->dai_config->acpbt.tdm_slots;
842efb931cdSAjit Kumar Pandey 
843efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
844efb931cdSAjit Kumar Pandey 			"AMD_BT rate_min: %d rate_max: %d\n", rate->min, rate->max);
845efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
846efb931cdSAjit Kumar Pandey 			"AMD_BT channels_min: %d channels_max: %d\n",
847efb931cdSAjit Kumar Pandey 			channels->min, channels->max);
848efb931cdSAjit Kumar Pandey 		break;
849efb931cdSAjit Kumar Pandey 	case SOF_DAI_AMD_SP:
850efb931cdSAjit Kumar Pandey 		rate->min = dai->dai_config->acpsp.fsync_rate;
851efb931cdSAjit Kumar Pandey 		rate->max = dai->dai_config->acpsp.fsync_rate;
852efb931cdSAjit Kumar Pandey 		channels->min = dai->dai_config->acpsp.tdm_slots;
853efb931cdSAjit Kumar Pandey 		channels->max = dai->dai_config->acpsp.tdm_slots;
854efb931cdSAjit Kumar Pandey 
855efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
856efb931cdSAjit Kumar Pandey 			"AMD_SP rate_min: %d rate_max: %d\n", rate->min, rate->max);
857efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
858efb931cdSAjit Kumar Pandey 			"AMD_SP channels_min: %d channels_max: %d\n",
859efb931cdSAjit Kumar Pandey 			channels->min, channels->max);
860efb931cdSAjit Kumar Pandey 		break;
861efb931cdSAjit Kumar Pandey 	case SOF_DAI_AMD_DMIC:
862efb931cdSAjit Kumar Pandey 		rate->min = dai->dai_config->acpdmic.fsync_rate;
863efb931cdSAjit Kumar Pandey 		rate->max = dai->dai_config->acpdmic.fsync_rate;
864efb931cdSAjit Kumar Pandey 		channels->min = dai->dai_config->acpdmic.tdm_slots;
865efb931cdSAjit Kumar Pandey 		channels->max = dai->dai_config->acpdmic.tdm_slots;
866efb931cdSAjit Kumar Pandey 
867efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
868efb931cdSAjit Kumar Pandey 			"AMD_DMIC rate_min: %d rate_max: %d\n", rate->min, rate->max);
869efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
870efb931cdSAjit Kumar Pandey 			"AMD_DMIC channels_min: %d channels_max: %d\n",
871efb931cdSAjit Kumar Pandey 			channels->min, channels->max);
872efb931cdSAjit Kumar Pandey 		break;
873868bd00fSLiam Girdwood 	default:
874ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: invalid DAI type %d\n",
875868bd00fSLiam Girdwood 			dai->dai_config->type);
876868bd00fSLiam Girdwood 		break;
877868bd00fSLiam Girdwood 	}
878868bd00fSLiam Girdwood 
879868bd00fSLiam Girdwood 	return 0;
880868bd00fSLiam Girdwood }
881f3f3af17SPierre-Louis Bossart EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
882868bd00fSLiam Girdwood 
883868bd00fSLiam Girdwood static int sof_pcm_probe(struct snd_soc_component *component)
884868bd00fSLiam Girdwood {
885868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
886868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
887868bd00fSLiam Girdwood 	const char *tplg_filename;
888868bd00fSLiam Girdwood 	int ret;
889868bd00fSLiam Girdwood 
890868bd00fSLiam Girdwood 	/* load the default topology */
891868bd00fSLiam Girdwood 	sdev->component = component;
892868bd00fSLiam Girdwood 
893868bd00fSLiam Girdwood 	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
894868bd00fSLiam Girdwood 				       "%s/%s",
895868bd00fSLiam Girdwood 				       plat_data->tplg_filename_prefix,
896868bd00fSLiam Girdwood 				       plat_data->tplg_filename);
897868bd00fSLiam Girdwood 	if (!tplg_filename)
898868bd00fSLiam Girdwood 		return -ENOMEM;
899868bd00fSLiam Girdwood 
900ee1e79b7SRanjani Sridharan 	ret = snd_sof_load_topology(component, tplg_filename);
901868bd00fSLiam Girdwood 	if (ret < 0) {
902ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: failed to load DSP topology %d\n",
903868bd00fSLiam Girdwood 			ret);
904868bd00fSLiam Girdwood 		return ret;
905868bd00fSLiam Girdwood 	}
906868bd00fSLiam Girdwood 
907868bd00fSLiam Girdwood 	return ret;
908868bd00fSLiam Girdwood }
909868bd00fSLiam Girdwood 
910868bd00fSLiam Girdwood static void sof_pcm_remove(struct snd_soc_component *component)
911868bd00fSLiam Girdwood {
912868bd00fSLiam Girdwood 	/* remove topology */
913a5b8f71cSAmadeusz Sławiński 	snd_soc_tplg_component_remove(component);
914868bd00fSLiam Girdwood }
915868bd00fSLiam Girdwood 
9164a39ea3fSRanjani Sridharan static int sof_pcm_ack(struct snd_soc_component *component,
9174a39ea3fSRanjani Sridharan 		       struct snd_pcm_substream *substream)
9184a39ea3fSRanjani Sridharan {
9194a39ea3fSRanjani Sridharan 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
9204a39ea3fSRanjani Sridharan 
9214a39ea3fSRanjani Sridharan 	return snd_sof_pcm_platform_ack(sdev, substream);
9224a39ea3fSRanjani Sridharan }
9234a39ea3fSRanjani Sridharan 
924868bd00fSLiam Girdwood void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
925868bd00fSLiam Girdwood {
926868bd00fSLiam Girdwood 	struct snd_soc_component_driver *pd = &sdev->plat_drv;
927868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
928868bd00fSLiam Girdwood 	const char *drv_name;
929868bd00fSLiam Girdwood 
930868bd00fSLiam Girdwood 	drv_name = plat_data->machine->drv_name;
931868bd00fSLiam Girdwood 
932868bd00fSLiam Girdwood 	pd->name = "sof-audio-component";
933868bd00fSLiam Girdwood 	pd->probe = sof_pcm_probe;
934868bd00fSLiam Girdwood 	pd->remove = sof_pcm_remove;
9351c91d77eSKuninori Morimoto 	pd->open = sof_pcm_open;
9361c91d77eSKuninori Morimoto 	pd->close = sof_pcm_close;
9371c91d77eSKuninori Morimoto 	pd->hw_params = sof_pcm_hw_params;
9381c91d77eSKuninori Morimoto 	pd->prepare = sof_pcm_prepare;
9391c91d77eSKuninori Morimoto 	pd->hw_free = sof_pcm_hw_free;
9401c91d77eSKuninori Morimoto 	pd->trigger = sof_pcm_trigger;
9411c91d77eSKuninori Morimoto 	pd->pointer = sof_pcm_pointer;
9424a39ea3fSRanjani Sridharan 	pd->ack = sof_pcm_ack;
9431c91d77eSKuninori Morimoto 
94470368106SCezary Rojewski #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
94539118ce5SKuninori Morimoto 	pd->compress_ops = &sof_probe_compressed_ops;
94670368106SCezary Rojewski #endif
9471c91d77eSKuninori Morimoto 	pd->pcm_construct = sof_pcm_new;
948868bd00fSLiam Girdwood 	pd->ignore_machine = drv_name;
949868bd00fSLiam Girdwood 	pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
950868bd00fSLiam Girdwood 	pd->be_pcm_base = SOF_BE_PCM_BASE;
951868bd00fSLiam Girdwood 	pd->use_dai_pcm_id = true;
952868bd00fSLiam Girdwood 	pd->topology_name_prefix = "sof";
953868bd00fSLiam Girdwood 
954868bd00fSLiam Girdwood 	 /* increment module refcount when a pcm is opened */
955868bd00fSLiam Girdwood 	pd->module_get_upon_open = 1;
956868bd00fSLiam Girdwood }
957