xref: /openbmc/linux/sound/soc/sof/pcm.c (revision 5fcdbb2d45df6afb654674379546996b0027aa3e)
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  */
609ef91cadSGuennadi Liakhovetski 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 
69e2803e61SKeyon Jie /*
70e2803e61SKeyon Jie  * sof pcm period elapse, this could be called at irq thread context.
71e2803e61SKeyon Jie  */
72e2803e61SKeyon Jie void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
73e2803e61SKeyon Jie {
741205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
75e2803e61SKeyon Jie 	struct snd_soc_component *component =
76ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
77e2803e61SKeyon Jie 	struct snd_sof_pcm *spcm;
78e2803e61SKeyon Jie 
79ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
80e2803e61SKeyon Jie 	if (!spcm) {
81ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
82e2803e61SKeyon Jie 			"error: period elapsed for unknown stream!\n");
83e2803e61SKeyon Jie 		return;
84e2803e61SKeyon Jie 	}
85e2803e61SKeyon Jie 
86e2803e61SKeyon Jie 	/*
87e2803e61SKeyon Jie 	 * snd_pcm_period_elapsed() can be called in interrupt context
88e2803e61SKeyon Jie 	 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(),
89e2803e61SKeyon Jie 	 * when the PCM is done draining or xrun happened, a STOP IPC will
90e2803e61SKeyon Jie 	 * then be sent and this IPC will hit IPC timeout.
91e2803e61SKeyon Jie 	 * To avoid sending IPC before the previous IPC is handled, we
92e2803e61SKeyon Jie 	 * schedule delayed work here to call the snd_pcm_period_elapsed().
93e2803e61SKeyon Jie 	 */
94e2803e61SKeyon Jie 	schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
95e2803e61SKeyon Jie }
96e2803e61SKeyon Jie EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
97e2803e61SKeyon Jie 
98cfe8191bSKai Vehmanen static int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream,
99cfe8191bSKai Vehmanen 				struct snd_sof_dev *sdev,
100cfe8191bSKai Vehmanen 				struct snd_sof_pcm *spcm)
101cfe8191bSKai Vehmanen {
102cfe8191bSKai Vehmanen 	struct sof_ipc_stream stream;
103cfe8191bSKai Vehmanen 	struct sof_ipc_reply reply;
104cfe8191bSKai Vehmanen 	int ret;
105cfe8191bSKai Vehmanen 
106cfe8191bSKai Vehmanen 	stream.hdr.size = sizeof(stream);
107cfe8191bSKai Vehmanen 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
108cfe8191bSKai Vehmanen 	stream.comp_id = spcm->stream[substream->stream].comp_id;
109cfe8191bSKai Vehmanen 
110cfe8191bSKai Vehmanen 	/* send IPC to the DSP */
111cfe8191bSKai Vehmanen 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
112cfe8191bSKai Vehmanen 				 sizeof(stream), &reply, sizeof(reply));
113cfe8191bSKai Vehmanen 	if (!ret)
114cfe8191bSKai Vehmanen 		spcm->prepared[substream->stream] = false;
115cfe8191bSKai Vehmanen 
116cfe8191bSKai Vehmanen 	return ret;
117cfe8191bSKai Vehmanen }
118cfe8191bSKai Vehmanen 
119*5fcdbb2dSRanjani Sridharan static int sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev,
120*5fcdbb2dSRanjani Sridharan 					   struct snd_soc_pcm_runtime *rtd,
121*5fcdbb2dSRanjani Sridharan 					   struct snd_sof_pcm *spcm, int dir)
122*5fcdbb2dSRanjani Sridharan {
123*5fcdbb2dSRanjani Sridharan 	struct snd_soc_dai *dai;
124*5fcdbb2dSRanjani Sridharan 	int ret, j;
125*5fcdbb2dSRanjani Sridharan 
126*5fcdbb2dSRanjani Sridharan 	/* query DAPM for list of connected widgets and set them up */
127*5fcdbb2dSRanjani Sridharan 	for_each_rtd_cpu_dais(rtd, j, dai) {
128*5fcdbb2dSRanjani Sridharan 		struct snd_soc_dapm_widget_list *list;
129*5fcdbb2dSRanjani Sridharan 
130*5fcdbb2dSRanjani Sridharan 		ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list,
131*5fcdbb2dSRanjani Sridharan 							     dpcm_end_walk_at_be);
132*5fcdbb2dSRanjani Sridharan 		if (ret < 0) {
133*5fcdbb2dSRanjani Sridharan 			dev_err(sdev->dev, "error: dai %s has no valid %s path\n", dai->name,
134*5fcdbb2dSRanjani Sridharan 				dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
135*5fcdbb2dSRanjani Sridharan 			return ret;
136*5fcdbb2dSRanjani Sridharan 		}
137*5fcdbb2dSRanjani Sridharan 
138*5fcdbb2dSRanjani Sridharan 		spcm->stream[dir].list = list;
139*5fcdbb2dSRanjani Sridharan 
140*5fcdbb2dSRanjani Sridharan 		ret = sof_widget_list_setup(sdev, spcm, dir);
141*5fcdbb2dSRanjani Sridharan 		if (ret < 0) {
142*5fcdbb2dSRanjani Sridharan 			dev_err(sdev->dev, "error: failed widget list set up for pcm %d dir %d\n",
143*5fcdbb2dSRanjani Sridharan 				spcm->pcm.pcm_id, dir);
144*5fcdbb2dSRanjani Sridharan 			spcm->stream[dir].list = NULL;
145*5fcdbb2dSRanjani Sridharan 			snd_soc_dapm_dai_free_widgets(&list);
146*5fcdbb2dSRanjani Sridharan 			return ret;
147*5fcdbb2dSRanjani Sridharan 		}
148*5fcdbb2dSRanjani Sridharan 	}
149*5fcdbb2dSRanjani Sridharan 
150*5fcdbb2dSRanjani Sridharan 	return 0;
151*5fcdbb2dSRanjani Sridharan }
152*5fcdbb2dSRanjani Sridharan 
1531c91d77eSKuninori Morimoto static int sof_pcm_hw_params(struct snd_soc_component *component,
1541c91d77eSKuninori Morimoto 			     struct snd_pcm_substream *substream,
155868bd00fSLiam Girdwood 			     struct snd_pcm_hw_params *params)
156868bd00fSLiam Girdwood {
1571205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
158868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
159868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
160868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
161868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params pcm;
162868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params_reply ipc_params_reply;
163868bd00fSLiam Girdwood 	int ret;
164868bd00fSLiam Girdwood 
165868bd00fSLiam Girdwood 	/* nothing to do for BE */
166868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
167868bd00fSLiam Girdwood 		return 0;
168868bd00fSLiam Girdwood 
169ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
170868bd00fSLiam Girdwood 	if (!spcm)
171868bd00fSLiam Girdwood 		return -EINVAL;
172868bd00fSLiam Girdwood 
173cfe8191bSKai Vehmanen 	/*
174cfe8191bSKai Vehmanen 	 * Handle repeated calls to hw_params() without free_pcm() in
175cfe8191bSKai Vehmanen 	 * between. At least ALSA OSS emulation depends on this.
176cfe8191bSKai Vehmanen 	 */
177cfe8191bSKai Vehmanen 	if (spcm->prepared[substream->stream]) {
178cfe8191bSKai Vehmanen 		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
179cfe8191bSKai Vehmanen 		if (ret < 0)
180cfe8191bSKai Vehmanen 			return ret;
181cfe8191bSKai Vehmanen 	}
182cfe8191bSKai Vehmanen 
183ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
184868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream);
185868bd00fSLiam Girdwood 
186868bd00fSLiam Girdwood 	memset(&pcm, 0, sizeof(pcm));
187868bd00fSLiam Girdwood 
18857e960f0STakashi Iwai 	/* create compressed page table for audio firmware */
18957e960f0STakashi Iwai 	if (runtime->buffer_changed) {
1901c91d77eSKuninori Morimoto 		ret = create_page_table(component, substream, runtime->dma_area,
191868bd00fSLiam Girdwood 					runtime->dma_bytes);
192868bd00fSLiam Girdwood 		if (ret < 0)
193868bd00fSLiam Girdwood 			return ret;
194868bd00fSLiam Girdwood 	}
195868bd00fSLiam Girdwood 
196868bd00fSLiam Girdwood 	/* number of pages should be rounded up */
197868bd00fSLiam Girdwood 	pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
198868bd00fSLiam Girdwood 
199868bd00fSLiam Girdwood 	/* set IPC PCM parameters */
200868bd00fSLiam Girdwood 	pcm.hdr.size = sizeof(pcm);
201868bd00fSLiam Girdwood 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
202868bd00fSLiam Girdwood 	pcm.comp_id = spcm->stream[substream->stream].comp_id;
203868bd00fSLiam Girdwood 	pcm.params.hdr.size = sizeof(pcm.params);
204868bd00fSLiam Girdwood 	pcm.params.buffer.phy_addr =
205868bd00fSLiam Girdwood 		spcm->stream[substream->stream].page_table.addr;
206868bd00fSLiam Girdwood 	pcm.params.buffer.size = runtime->dma_bytes;
207868bd00fSLiam Girdwood 	pcm.params.direction = substream->stream;
208868bd00fSLiam Girdwood 	pcm.params.sample_valid_bytes = params_width(params) >> 3;
209868bd00fSLiam Girdwood 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
210868bd00fSLiam Girdwood 	pcm.params.rate = params_rate(params);
211868bd00fSLiam Girdwood 	pcm.params.channels = params_channels(params);
212868bd00fSLiam Girdwood 	pcm.params.host_period_bytes = params_period_bytes(params);
213868bd00fSLiam Girdwood 
214868bd00fSLiam Girdwood 	/* container size */
215868bd00fSLiam Girdwood 	ret = snd_pcm_format_physical_width(params_format(params));
216868bd00fSLiam Girdwood 	if (ret < 0)
217868bd00fSLiam Girdwood 		return ret;
218868bd00fSLiam Girdwood 	pcm.params.sample_container_bytes = ret >> 3;
219868bd00fSLiam Girdwood 
220868bd00fSLiam Girdwood 	/* format */
221868bd00fSLiam Girdwood 	switch (params_format(params)) {
222868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S16:
223868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
224868bd00fSLiam Girdwood 		break;
225868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S24:
226868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
227868bd00fSLiam Girdwood 		break;
228868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S32:
229868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
230868bd00fSLiam Girdwood 		break;
231868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_FLOAT:
232868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
233868bd00fSLiam Girdwood 		break;
234868bd00fSLiam Girdwood 	default:
235868bd00fSLiam Girdwood 		return -EINVAL;
236868bd00fSLiam Girdwood 	}
237868bd00fSLiam Girdwood 
238868bd00fSLiam Girdwood 	/* firmware already configured host stream */
239868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_hw_params(sdev,
240868bd00fSLiam Girdwood 					     substream,
241868bd00fSLiam Girdwood 					     params,
242868bd00fSLiam Girdwood 					     &pcm.params);
243868bd00fSLiam Girdwood 	if (ret < 0) {
244ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw params failed\n");
245868bd00fSLiam Girdwood 		return ret;
246868bd00fSLiam Girdwood 	}
247868bd00fSLiam Girdwood 
248ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag);
249868bd00fSLiam Girdwood 
250*5fcdbb2dSRanjani Sridharan 	/* if this is a repeated hw_params without hw_free, skip setting up widgets */
251*5fcdbb2dSRanjani Sridharan 	if (!spcm->stream[substream->stream].list) {
252*5fcdbb2dSRanjani Sridharan 		ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, substream->stream);
253*5fcdbb2dSRanjani Sridharan 		if (ret < 0)
254*5fcdbb2dSRanjani Sridharan 			return ret;
255*5fcdbb2dSRanjani Sridharan 	}
256*5fcdbb2dSRanjani Sridharan 
257*5fcdbb2dSRanjani Sridharan 	/* send hw_params IPC to the DSP */
258868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
259868bd00fSLiam Girdwood 				 &ipc_params_reply, sizeof(ipc_params_reply));
260868bd00fSLiam Girdwood 	if (ret < 0) {
261ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: hw params ipc failed for stream %d\n",
262868bd00fSLiam Girdwood 			pcm.params.stream_tag);
263868bd00fSLiam Girdwood 		return ret;
264868bd00fSLiam Girdwood 	}
265868bd00fSLiam Girdwood 
266868bd00fSLiam Girdwood 	ret = sof_pcm_dsp_params(spcm, substream, &ipc_params_reply);
267868bd00fSLiam Girdwood 	if (ret < 0)
268868bd00fSLiam Girdwood 		return ret;
269868bd00fSLiam Girdwood 
27004c80277SKai Vehmanen 	spcm->prepared[substream->stream] = true;
27104c80277SKai Vehmanen 
272868bd00fSLiam Girdwood 	/* save pcm hw_params */
273868bd00fSLiam Girdwood 	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
274868bd00fSLiam Girdwood 
275868bd00fSLiam Girdwood 	return ret;
276868bd00fSLiam Girdwood }
277868bd00fSLiam Girdwood 
2781c91d77eSKuninori Morimoto static int sof_pcm_hw_free(struct snd_soc_component *component,
2791c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
280868bd00fSLiam Girdwood {
2811205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
282868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
283868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
284e66e52c5SKai Vehmanen 	int ret, err = 0;
285868bd00fSLiam Girdwood 
286868bd00fSLiam Girdwood 	/* nothing to do for BE */
287868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
288868bd00fSLiam Girdwood 		return 0;
289868bd00fSLiam Girdwood 
290ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
291868bd00fSLiam Girdwood 	if (!spcm)
292868bd00fSLiam Girdwood 		return -EINVAL;
293868bd00fSLiam Girdwood 
294ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
295ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
296868bd00fSLiam Girdwood 
297e66e52c5SKai Vehmanen 	if (spcm->prepared[substream->stream]) {
298a49b6871SKai Vehmanen 		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
299e66e52c5SKai Vehmanen 		if (ret < 0)
300e66e52c5SKai Vehmanen 			err = ret;
301e66e52c5SKai Vehmanen 	}
302868bd00fSLiam Girdwood 
303*5fcdbb2dSRanjani Sridharan 	ret = sof_widget_list_free(sdev, spcm, substream->stream);
304*5fcdbb2dSRanjani Sridharan 	if (ret < 0)
305*5fcdbb2dSRanjani Sridharan 		err = ret;
306*5fcdbb2dSRanjani Sridharan 
307e2803e61SKeyon Jie 	cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
308e2803e61SKeyon Jie 
30993146bc2SRanjani Sridharan 	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
310e66e52c5SKai Vehmanen 	if (ret < 0) {
311ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw free failed\n");
312e66e52c5SKai Vehmanen 		err = ret;
313e66e52c5SKai Vehmanen 	}
31493146bc2SRanjani Sridharan 
315e66e52c5SKai Vehmanen 	return err;
316868bd00fSLiam Girdwood }
317868bd00fSLiam Girdwood 
3181c91d77eSKuninori Morimoto static int sof_pcm_prepare(struct snd_soc_component *component,
3191c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
320868bd00fSLiam Girdwood {
3211205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
322868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
323868bd00fSLiam Girdwood 	int ret;
324868bd00fSLiam Girdwood 
325868bd00fSLiam Girdwood 	/* nothing to do for BE */
326868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
327868bd00fSLiam Girdwood 		return 0;
328868bd00fSLiam Girdwood 
329ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
330868bd00fSLiam Girdwood 	if (!spcm)
331868bd00fSLiam Girdwood 		return -EINVAL;
332868bd00fSLiam Girdwood 
33304c80277SKai Vehmanen 	if (spcm->prepared[substream->stream])
334868bd00fSLiam Girdwood 		return 0;
335868bd00fSLiam Girdwood 
336ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
337ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
338868bd00fSLiam Girdwood 
339868bd00fSLiam Girdwood 	/* set hw_params */
3401c91d77eSKuninori Morimoto 	ret = sof_pcm_hw_params(component,
3411c91d77eSKuninori Morimoto 				substream, &spcm->params[substream->stream]);
342868bd00fSLiam Girdwood 	if (ret < 0) {
343ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
344ee1e79b7SRanjani Sridharan 			"error: set pcm hw_params after resume\n");
345868bd00fSLiam Girdwood 		return ret;
346868bd00fSLiam Girdwood 	}
347868bd00fSLiam Girdwood 
348868bd00fSLiam Girdwood 	return 0;
349868bd00fSLiam Girdwood }
350868bd00fSLiam Girdwood 
351868bd00fSLiam Girdwood /*
352868bd00fSLiam Girdwood  * FE dai link trigger actions are always executed in non-atomic context because
353868bd00fSLiam Girdwood  * they involve IPC's.
354868bd00fSLiam Girdwood  */
3551c91d77eSKuninori Morimoto static int sof_pcm_trigger(struct snd_soc_component *component,
3561c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream, int cmd)
357868bd00fSLiam Girdwood {
3581205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
359868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
360868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
361868bd00fSLiam Girdwood 	struct sof_ipc_stream stream;
362868bd00fSLiam Girdwood 	struct sof_ipc_reply reply;
36304c80277SKai Vehmanen 	bool reset_hw_params = false;
364*5fcdbb2dSRanjani Sridharan 	bool free_widget_list = false;
3650a1b0834SPan Xiuli 	bool ipc_first = false;
366868bd00fSLiam Girdwood 	int ret;
367868bd00fSLiam Girdwood 
368868bd00fSLiam Girdwood 	/* nothing to do for BE */
369868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
370868bd00fSLiam Girdwood 		return 0;
371868bd00fSLiam Girdwood 
372ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
373868bd00fSLiam Girdwood 	if (!spcm)
374868bd00fSLiam Girdwood 		return -EINVAL;
375868bd00fSLiam Girdwood 
376ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
377868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream, cmd);
378868bd00fSLiam Girdwood 
379868bd00fSLiam Girdwood 	stream.hdr.size = sizeof(stream);
380868bd00fSLiam Girdwood 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
381868bd00fSLiam Girdwood 	stream.comp_id = spcm->stream[substream->stream].comp_id;
382868bd00fSLiam Girdwood 
383868bd00fSLiam Girdwood 	switch (cmd) {
384868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
385868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
3860a1b0834SPan Xiuli 		ipc_first = true;
387868bd00fSLiam Girdwood 		break;
388868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
389868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
390868bd00fSLiam Girdwood 		break;
391868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
392ac8c046fSKeyon Jie 		if (spcm->stream[substream->stream].suspend_ignored) {
393ac8c046fSKeyon Jie 			/*
394ac8c046fSKeyon Jie 			 * this case will be triggered when INFO_RESUME is
395ac8c046fSKeyon Jie 			 * supported, no need to resume streams that remained
396ac8c046fSKeyon Jie 			 * enabled in D0ix.
397ac8c046fSKeyon Jie 			 */
398ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = false;
399ac8c046fSKeyon Jie 			return 0;
400ac8c046fSKeyon Jie 		}
401ac8c046fSKeyon Jie 
402868bd00fSLiam Girdwood 		/* set up hw_params */
4031c91d77eSKuninori Morimoto 		ret = sof_pcm_prepare(component, substream);
404868bd00fSLiam Girdwood 		if (ret < 0) {
405ee1e79b7SRanjani Sridharan 			dev_err(component->dev,
406868bd00fSLiam Girdwood 				"error: failed to set up hw_params upon resume\n");
407868bd00fSLiam Girdwood 			return ret;
408868bd00fSLiam Girdwood 		}
409868bd00fSLiam Girdwood 
410df561f66SGustavo A. R. Silva 		fallthrough;
411868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
412ac8c046fSKeyon Jie 		if (spcm->stream[substream->stream].suspend_ignored) {
413ac8c046fSKeyon Jie 			/*
414ac8c046fSKeyon Jie 			 * This case will be triggered when INFO_RESUME is
415ac8c046fSKeyon Jie 			 * not supported, no need to re-start streams that
416ac8c046fSKeyon Jie 			 * remained enabled in D0ix.
417ac8c046fSKeyon Jie 			 */
418ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = false;
419ac8c046fSKeyon Jie 			return 0;
420ac8c046fSKeyon Jie 		}
421868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
422868bd00fSLiam Girdwood 		break;
423868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
424043ae13bSRanjani Sridharan 		if (sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
425ac8c046fSKeyon Jie 		    spcm->stream[substream->stream].d0i3_compatible) {
426ac8c046fSKeyon Jie 			/*
427ac8c046fSKeyon Jie 			 * trap the event, not sending trigger stop to
428ac8c046fSKeyon Jie 			 * prevent the FW pipelines from being stopped,
429ac8c046fSKeyon Jie 			 * and mark the flag to ignore the upcoming DAPM
430ac8c046fSKeyon Jie 			 * PM events.
431ac8c046fSKeyon Jie 			 */
432ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = true;
433ac8c046fSKeyon Jie 			return 0;
434ac8c046fSKeyon Jie 		}
435*5fcdbb2dSRanjani Sridharan 		free_widget_list = true;
436df561f66SGustavo A. R. Silva 		fallthrough;
437868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
438868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
4390a1b0834SPan Xiuli 		ipc_first = true;
44004c80277SKai Vehmanen 		reset_hw_params = true;
441868bd00fSLiam Girdwood 		break;
442868bd00fSLiam Girdwood 	default:
443ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: unhandled trigger cmd %d\n",
444ee1e79b7SRanjani Sridharan 			cmd);
445868bd00fSLiam Girdwood 		return -EINVAL;
446868bd00fSLiam Girdwood 	}
447868bd00fSLiam Girdwood 
4480a1b0834SPan Xiuli 	/*
4490a1b0834SPan Xiuli 	 * DMA and IPC sequence is different for start and stop. Need to send
4500a1b0834SPan Xiuli 	 * STOP IPC before stop DMA
4510a1b0834SPan Xiuli 	 */
4520a1b0834SPan Xiuli 	if (!ipc_first)
453868bd00fSLiam Girdwood 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
454868bd00fSLiam Girdwood 
455868bd00fSLiam Girdwood 	/* send IPC to the DSP */
456868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
457868bd00fSLiam Girdwood 				 sizeof(stream), &reply, sizeof(reply));
458868bd00fSLiam Girdwood 
4590a1b0834SPan Xiuli 	/* need to STOP DMA even if STOP IPC failed */
4600a1b0834SPan Xiuli 	if (ipc_first)
4610a1b0834SPan Xiuli 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
4620a1b0834SPan Xiuli 
4630a1b0834SPan Xiuli 	/* free PCM if reset_hw_params is set and the STOP IPC is successful */
464*5fcdbb2dSRanjani Sridharan 	if (!ret && reset_hw_params) {
465a49b6871SKai Vehmanen 		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
466*5fcdbb2dSRanjani Sridharan 		if (ret < 0)
467*5fcdbb2dSRanjani Sridharan 			return ret;
468*5fcdbb2dSRanjani Sridharan 
469*5fcdbb2dSRanjani Sridharan 		/* free widget list only for SUSPEND trigger */
470*5fcdbb2dSRanjani Sridharan 		if (free_widget_list)
471*5fcdbb2dSRanjani Sridharan 			ret = sof_widget_list_free(sdev, spcm, substream->stream);
472*5fcdbb2dSRanjani Sridharan 	}
473a49b6871SKai Vehmanen 
474868bd00fSLiam Girdwood 	return ret;
475868bd00fSLiam Girdwood }
476868bd00fSLiam Girdwood 
4771c91d77eSKuninori Morimoto static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
4781c91d77eSKuninori Morimoto 					 struct snd_pcm_substream *substream)
479868bd00fSLiam Girdwood {
4801205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
481868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
482868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
483868bd00fSLiam Girdwood 	snd_pcm_uframes_t host, dai;
484868bd00fSLiam Girdwood 
485868bd00fSLiam Girdwood 	/* nothing to do for BE */
486868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
487868bd00fSLiam Girdwood 		return 0;
488868bd00fSLiam Girdwood 
489868bd00fSLiam Girdwood 	/* use dsp ops pointer callback directly if set */
490868bd00fSLiam Girdwood 	if (sof_ops(sdev)->pcm_pointer)
491868bd00fSLiam Girdwood 		return sof_ops(sdev)->pcm_pointer(sdev, substream);
492868bd00fSLiam Girdwood 
493ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
494868bd00fSLiam Girdwood 	if (!spcm)
495868bd00fSLiam Girdwood 		return -EINVAL;
496868bd00fSLiam Girdwood 
497868bd00fSLiam Girdwood 	/* read position from DSP */
498868bd00fSLiam Girdwood 	host = bytes_to_frames(substream->runtime,
499868bd00fSLiam Girdwood 			       spcm->stream[substream->stream].posn.host_posn);
500868bd00fSLiam Girdwood 	dai = bytes_to_frames(substream->runtime,
501868bd00fSLiam Girdwood 			      spcm->stream[substream->stream].posn.dai_posn);
502868bd00fSLiam Girdwood 
503277ff236SPierre-Louis Bossart 	dev_vdbg(component->dev,
504ee1e79b7SRanjani Sridharan 		 "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
505868bd00fSLiam Girdwood 		 spcm->pcm.pcm_id, substream->stream, host, dai);
506868bd00fSLiam Girdwood 
507868bd00fSLiam Girdwood 	return host;
508868bd00fSLiam Girdwood }
509868bd00fSLiam Girdwood 
5101c91d77eSKuninori Morimoto static int sof_pcm_open(struct snd_soc_component *component,
5111c91d77eSKuninori Morimoto 			struct snd_pcm_substream *substream)
512868bd00fSLiam Girdwood {
5131205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
514868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
515868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
51627e322faSPierre-Louis Bossart 	const struct snd_sof_dsp_ops *ops = sof_ops(sdev);
517868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
518868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
519868bd00fSLiam Girdwood 	int ret;
520868bd00fSLiam Girdwood 
521868bd00fSLiam Girdwood 	/* nothing to do for BE */
522868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
523868bd00fSLiam Girdwood 		return 0;
524868bd00fSLiam Girdwood 
525ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
526868bd00fSLiam Girdwood 	if (!spcm)
527868bd00fSLiam Girdwood 		return -EINVAL;
528868bd00fSLiam Girdwood 
529ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
530ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
531868bd00fSLiam Girdwood 
532868bd00fSLiam Girdwood 
533868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[substream->stream];
534868bd00fSLiam Girdwood 
535868bd00fSLiam Girdwood 	/* set runtime config */
53627e322faSPierre-Louis Bossart 	runtime->hw.info = ops->hw_info; /* platform-specific */
53727e322faSPierre-Louis Bossart 
5389983ac49SKai Vehmanen 	/* set any runtime constraints based on topology */
539868bd00fSLiam Girdwood 	runtime->hw.formats = le64_to_cpu(caps->formats);
540868bd00fSLiam Girdwood 	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
541868bd00fSLiam Girdwood 	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
542868bd00fSLiam Girdwood 	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
543868bd00fSLiam Girdwood 	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
544868bd00fSLiam Girdwood 
545868bd00fSLiam Girdwood 	/*
546868bd00fSLiam Girdwood 	 * caps->buffer_size_min is not used since the
547868bd00fSLiam Girdwood 	 * snd_pcm_hardware structure only defines buffer_bytes_max
548868bd00fSLiam Girdwood 	 */
549868bd00fSLiam Girdwood 	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
550868bd00fSLiam Girdwood 
551ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period min %zd max %zd bytes\n",
552868bd00fSLiam Girdwood 		runtime->hw.period_bytes_min,
553868bd00fSLiam Girdwood 		runtime->hw.period_bytes_max);
554ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period count %d max %d\n",
555868bd00fSLiam Girdwood 		runtime->hw.periods_min,
556868bd00fSLiam Girdwood 		runtime->hw.periods_max);
557ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "buffer max %zd bytes\n",
558868bd00fSLiam Girdwood 		runtime->hw.buffer_bytes_max);
559868bd00fSLiam Girdwood 
560868bd00fSLiam Girdwood 	/* set wait time - TODO: come from topology */
561868bd00fSLiam Girdwood 	substream->wait_time = 500;
562868bd00fSLiam Girdwood 
563868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.host_posn = 0;
564868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.dai_posn = 0;
565868bd00fSLiam Girdwood 	spcm->stream[substream->stream].substream = substream;
56604c80277SKai Vehmanen 	spcm->prepared[substream->stream] = false;
567868bd00fSLiam Girdwood 
568868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_open(sdev, substream);
56914a2212dSRanjani Sridharan 	if (ret < 0)
570ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm open failed %d\n", ret);
571868bd00fSLiam Girdwood 
572868bd00fSLiam Girdwood 	return ret;
573868bd00fSLiam Girdwood }
574868bd00fSLiam Girdwood 
5751c91d77eSKuninori Morimoto static int sof_pcm_close(struct snd_soc_component *component,
5761c91d77eSKuninori Morimoto 			 struct snd_pcm_substream *substream)
577868bd00fSLiam Girdwood {
5781205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
579868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
580868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
581868bd00fSLiam Girdwood 	int err;
582868bd00fSLiam Girdwood 
583868bd00fSLiam Girdwood 	/* nothing to do for BE */
584868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
585868bd00fSLiam Girdwood 		return 0;
586868bd00fSLiam Girdwood 
587ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
588868bd00fSLiam Girdwood 	if (!spcm)
589868bd00fSLiam Girdwood 		return -EINVAL;
590868bd00fSLiam Girdwood 
591ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
592ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
593868bd00fSLiam Girdwood 
594868bd00fSLiam Girdwood 	err = snd_sof_pcm_platform_close(sdev, substream);
595868bd00fSLiam Girdwood 	if (err < 0) {
596ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm close failed %d\n",
597868bd00fSLiam Girdwood 			err);
598868bd00fSLiam Girdwood 		/*
599868bd00fSLiam Girdwood 		 * keep going, no point in preventing the close
600868bd00fSLiam Girdwood 		 * from happening
601868bd00fSLiam Girdwood 		 */
602868bd00fSLiam Girdwood 	}
603868bd00fSLiam Girdwood 
604868bd00fSLiam Girdwood 	return 0;
605868bd00fSLiam Girdwood }
606868bd00fSLiam Girdwood 
607868bd00fSLiam Girdwood /*
608868bd00fSLiam Girdwood  * Pre-allocate playback/capture audio buffer pages.
609868bd00fSLiam Girdwood  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
610868bd00fSLiam Girdwood  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
611868bd00fSLiam Girdwood  */
6121c91d77eSKuninori Morimoto static int sof_pcm_new(struct snd_soc_component *component,
6131c91d77eSKuninori Morimoto 		       struct snd_soc_pcm_runtime *rtd)
614868bd00fSLiam Girdwood {
615868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
616868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
617868bd00fSLiam Girdwood 	struct snd_pcm *pcm = rtd->pcm;
618868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
619868bd00fSLiam Girdwood 	int stream = SNDRV_PCM_STREAM_PLAYBACK;
620868bd00fSLiam Girdwood 
621868bd00fSLiam Girdwood 	/* find SOF PCM for this RTD */
622ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
623868bd00fSLiam Girdwood 	if (!spcm) {
624ee1e79b7SRanjani Sridharan 		dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
625868bd00fSLiam Girdwood 			 rtd->dai_link->id);
626868bd00fSLiam Girdwood 		return 0;
627868bd00fSLiam Girdwood 	}
628868bd00fSLiam Girdwood 
629ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
630868bd00fSLiam Girdwood 
631868bd00fSLiam Girdwood 	/* do we need to pre-allocate playback audio buffer pages */
632868bd00fSLiam Girdwood 	if (!spcm->pcm.playback)
633868bd00fSLiam Girdwood 		goto capture;
634868bd00fSLiam Girdwood 
635868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
636868bd00fSLiam Girdwood 
637868bd00fSLiam Girdwood 	/* pre-allocate playback audio buffer pages */
638ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
639ee1e79b7SRanjani Sridharan 		"spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
640868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
641868bd00fSLiam Girdwood 
6424f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
6434f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL playback substream!\n");
6444f7f9564SGuennadi Liakhovetski 		return -EINVAL;
6454f7f9564SGuennadi Liakhovetski 	}
6464f7f9564SGuennadi Liakhovetski 
64757e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
648868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
649e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
650868bd00fSLiam Girdwood capture:
651868bd00fSLiam Girdwood 	stream = SNDRV_PCM_STREAM_CAPTURE;
652868bd00fSLiam Girdwood 
653868bd00fSLiam Girdwood 	/* do we need to pre-allocate capture audio buffer pages */
654868bd00fSLiam Girdwood 	if (!spcm->pcm.capture)
655868bd00fSLiam Girdwood 		return 0;
656868bd00fSLiam Girdwood 
657868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
658868bd00fSLiam Girdwood 
659868bd00fSLiam Girdwood 	/* pre-allocate capture audio buffer pages */
660ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
661ee1e79b7SRanjani Sridharan 		"spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
662868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
663868bd00fSLiam Girdwood 
6644f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
6654f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL capture substream!\n");
6664f7f9564SGuennadi Liakhovetski 		return -EINVAL;
6674f7f9564SGuennadi Liakhovetski 	}
6684f7f9564SGuennadi Liakhovetski 
66957e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
670868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
671e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
672868bd00fSLiam Girdwood 
673868bd00fSLiam Girdwood 	return 0;
674868bd00fSLiam Girdwood }
675868bd00fSLiam Girdwood 
676c943a586SJaska Uimonen static void ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
677c943a586SJaska Uimonen 					    struct snd_pcm_hw_params *params)
678c943a586SJaska Uimonen {
679c943a586SJaska Uimonen 	struct sof_ipc_dai_config *config;
680c943a586SJaska Uimonen 	struct snd_sof_dai *dai;
681c943a586SJaska Uimonen 	int i;
682c943a586SJaska Uimonen 
683c943a586SJaska Uimonen 	/*
684c943a586SJaska Uimonen 	 * Search for all matching DAIs as we can have both playback and capture DAI
685c943a586SJaska Uimonen 	 * associated with the same link.
686c943a586SJaska Uimonen 	 */
687c943a586SJaska Uimonen 	list_for_each_entry(dai, &sdev->dai_list, list) {
688c943a586SJaska Uimonen 		if (!dai->name || strcmp(link_name, dai->name))
689c943a586SJaska Uimonen 			continue;
690c943a586SJaska Uimonen 		for (i = 0; i < dai->number_configs; i++) {
691c943a586SJaska Uimonen 			config = &dai->dai_config[i];
692c943a586SJaska Uimonen 			if (config->ssp.fsync_rate == params_rate(params)) {
693c943a586SJaska Uimonen 				dev_dbg(sdev->dev, "DAI config %d matches pcm hw params\n", i);
694c943a586SJaska Uimonen 				dai->current_config = i;
695c943a586SJaska Uimonen 				break;
696c943a586SJaska Uimonen 			}
697c943a586SJaska Uimonen 		}
698c943a586SJaska Uimonen 	}
699c943a586SJaska Uimonen }
700c943a586SJaska Uimonen 
701868bd00fSLiam Girdwood /* fixup the BE DAI link to match any values from topology */
702f805e7e0SRanjani Sridharan int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
703868bd00fSLiam Girdwood {
704868bd00fSLiam Girdwood 	struct snd_interval *rate = hw_param_interval(params,
705868bd00fSLiam Girdwood 			SNDRV_PCM_HW_PARAM_RATE);
706868bd00fSLiam Girdwood 	struct snd_interval *channels = hw_param_interval(params,
707868bd00fSLiam Girdwood 						SNDRV_PCM_HW_PARAM_CHANNELS);
708868bd00fSLiam Girdwood 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
709868bd00fSLiam Girdwood 	struct snd_soc_component *component =
710ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
711868bd00fSLiam Girdwood 	struct snd_sof_dai *dai =
712ee1e79b7SRanjani Sridharan 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
713c943a586SJaska Uimonen 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
714fd045558Sranderwang 	struct snd_soc_dpcm *dpcm;
715868bd00fSLiam Girdwood 
716868bd00fSLiam Girdwood 	/* no topology exists for this BE, try a common configuration */
717868bd00fSLiam Girdwood 	if (!dai) {
718ee1e79b7SRanjani Sridharan 		dev_warn(component->dev,
719ee1e79b7SRanjani Sridharan 			 "warning: no topology found for BE DAI %s config\n",
720868bd00fSLiam Girdwood 			 rtd->dai_link->name);
721868bd00fSLiam Girdwood 
722868bd00fSLiam Girdwood 		/*  set 48k, stereo, 16bits by default */
723868bd00fSLiam Girdwood 		rate->min = 48000;
724868bd00fSLiam Girdwood 		rate->max = 48000;
725868bd00fSLiam Girdwood 
726868bd00fSLiam Girdwood 		channels->min = 2;
727868bd00fSLiam Girdwood 		channels->max = 2;
728868bd00fSLiam Girdwood 
729868bd00fSLiam Girdwood 		snd_mask_none(fmt);
730868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
731868bd00fSLiam Girdwood 
732868bd00fSLiam Girdwood 		return 0;
733868bd00fSLiam Girdwood 	}
734868bd00fSLiam Girdwood 
735868bd00fSLiam Girdwood 	/* read format from topology */
736868bd00fSLiam Girdwood 	snd_mask_none(fmt);
737868bd00fSLiam Girdwood 
738868bd00fSLiam Girdwood 	switch (dai->comp_dai.config.frame_fmt) {
739868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S16_LE:
740868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
741868bd00fSLiam Girdwood 		break;
742868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S24_4LE:
743868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
744868bd00fSLiam Girdwood 		break;
745868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S32_LE:
746868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
747868bd00fSLiam Girdwood 		break;
748868bd00fSLiam Girdwood 	default:
749ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: No available DAI format!\n");
750868bd00fSLiam Girdwood 		return -EINVAL;
751868bd00fSLiam Girdwood 	}
752868bd00fSLiam Girdwood 
753868bd00fSLiam Girdwood 	/* read rate and channels from topology */
754868bd00fSLiam Girdwood 	switch (dai->dai_config->type) {
755868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_SSP:
756c943a586SJaska Uimonen 		/* search for config to pcm params match, if not found use default */
757c943a586SJaska Uimonen 		ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);
758c943a586SJaska Uimonen 
759c1c03888SJaska Uimonen 		rate->min = dai->dai_config[dai->current_config].ssp.fsync_rate;
760c1c03888SJaska Uimonen 		rate->max = dai->dai_config[dai->current_config].ssp.fsync_rate;
761c1c03888SJaska Uimonen 		channels->min = dai->dai_config[dai->current_config].ssp.tdm_slots;
762c1c03888SJaska Uimonen 		channels->max = dai->dai_config[dai->current_config].ssp.tdm_slots;
763868bd00fSLiam Girdwood 
764ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
765868bd00fSLiam Girdwood 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
766ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
767868bd00fSLiam Girdwood 			"channels_min: %d channels_max: %d\n",
768868bd00fSLiam Girdwood 			channels->min, channels->max);
769868bd00fSLiam Girdwood 
770868bd00fSLiam Girdwood 		break;
771868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_DMIC:
772868bd00fSLiam Girdwood 		/* DMIC only supports 16 or 32 bit formats */
773868bd00fSLiam Girdwood 		if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
774ee1e79b7SRanjani Sridharan 			dev_err(component->dev,
775868bd00fSLiam Girdwood 				"error: invalid fmt %d for DAI type %d\n",
776868bd00fSLiam Girdwood 				dai->comp_dai.config.frame_fmt,
777868bd00fSLiam Girdwood 				dai->dai_config->type);
778868bd00fSLiam Girdwood 		}
779868bd00fSLiam Girdwood 		break;
780868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_HDA:
781fd045558Sranderwang 		/*
782135ab457SPierre-Louis Bossart 		 * HDAudio does not follow the default trigger
783fd045558Sranderwang 		 * sequence due to firmware implementation
784fd045558Sranderwang 		 */
785fd045558Sranderwang 		for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
786fd045558Sranderwang 			struct snd_soc_pcm_runtime *fe = dpcm->fe;
787fd045558Sranderwang 
788fd045558Sranderwang 			fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
789fd045558Sranderwang 				SND_SOC_DPCM_TRIGGER_POST;
790fd045558Sranderwang 		}
791868bd00fSLiam Girdwood 		break;
7926e3360cdSPierre-Louis Bossart 	case SOF_DAI_INTEL_ALH:
793e1711b1fSRander Wang 		/*
794e1711b1fSRander Wang 		 * Dai could run with different channel count compared with
795e1711b1fSRander Wang 		 * front end, so get dai channel count from topology
796e1711b1fSRander Wang 		 */
797e1711b1fSRander Wang 		channels->min = dai->dai_config->alh.channels;
798e1711b1fSRander Wang 		channels->max = dai->dai_config->alh.channels;
7996e3360cdSPierre-Louis Bossart 		break;
800a4eff5f8SDaniel Baluta 	case SOF_DAI_IMX_ESAI:
80151b0243aSDaniel Baluta 		rate->min = dai->dai_config->esai.fsync_rate;
80251b0243aSDaniel Baluta 		rate->max = dai->dai_config->esai.fsync_rate;
803a4eff5f8SDaniel Baluta 		channels->min = dai->dai_config->esai.tdm_slots;
804a4eff5f8SDaniel Baluta 		channels->max = dai->dai_config->esai.tdm_slots;
805a4eff5f8SDaniel Baluta 
806ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
80751b0243aSDaniel Baluta 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
80851b0243aSDaniel Baluta 		dev_dbg(component->dev,
809a4eff5f8SDaniel Baluta 			"channels_min: %d channels_max: %d\n",
810a4eff5f8SDaniel Baluta 			channels->min, channels->max);
811a4eff5f8SDaniel Baluta 		break;
812d88cbd6fSGuido Roncarolo 	case SOF_DAI_IMX_SAI:
81351b0243aSDaniel Baluta 		rate->min = dai->dai_config->sai.fsync_rate;
81451b0243aSDaniel Baluta 		rate->max = dai->dai_config->sai.fsync_rate;
815d88cbd6fSGuido Roncarolo 		channels->min = dai->dai_config->sai.tdm_slots;
816d88cbd6fSGuido Roncarolo 		channels->max = dai->dai_config->sai.tdm_slots;
817d88cbd6fSGuido Roncarolo 
818d88cbd6fSGuido Roncarolo 		dev_dbg(component->dev,
81951b0243aSDaniel Baluta 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
82051b0243aSDaniel Baluta 		dev_dbg(component->dev,
821d88cbd6fSGuido Roncarolo 			"channels_min: %d channels_max: %d\n",
822d88cbd6fSGuido Roncarolo 			channels->min, channels->max);
823d88cbd6fSGuido Roncarolo 		break;
824868bd00fSLiam Girdwood 	default:
825ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: invalid DAI type %d\n",
826868bd00fSLiam Girdwood 			dai->dai_config->type);
827868bd00fSLiam Girdwood 		break;
828868bd00fSLiam Girdwood 	}
829868bd00fSLiam Girdwood 
830868bd00fSLiam Girdwood 	return 0;
831868bd00fSLiam Girdwood }
832f3f3af17SPierre-Louis Bossart EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
833868bd00fSLiam Girdwood 
834868bd00fSLiam Girdwood static int sof_pcm_probe(struct snd_soc_component *component)
835868bd00fSLiam Girdwood {
836868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
837868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
838868bd00fSLiam Girdwood 	const char *tplg_filename;
839868bd00fSLiam Girdwood 	int ret;
840868bd00fSLiam Girdwood 
841868bd00fSLiam Girdwood 	/* load the default topology */
842868bd00fSLiam Girdwood 	sdev->component = component;
843868bd00fSLiam Girdwood 
844868bd00fSLiam Girdwood 	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
845868bd00fSLiam Girdwood 				       "%s/%s",
846868bd00fSLiam Girdwood 				       plat_data->tplg_filename_prefix,
847868bd00fSLiam Girdwood 				       plat_data->tplg_filename);
848868bd00fSLiam Girdwood 	if (!tplg_filename)
849868bd00fSLiam Girdwood 		return -ENOMEM;
850868bd00fSLiam Girdwood 
851ee1e79b7SRanjani Sridharan 	ret = snd_sof_load_topology(component, tplg_filename);
852868bd00fSLiam Girdwood 	if (ret < 0) {
853ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: failed to load DSP topology %d\n",
854868bd00fSLiam Girdwood 			ret);
855868bd00fSLiam Girdwood 		return ret;
856868bd00fSLiam Girdwood 	}
857868bd00fSLiam Girdwood 
858868bd00fSLiam Girdwood 	return ret;
859868bd00fSLiam Girdwood }
860868bd00fSLiam Girdwood 
861868bd00fSLiam Girdwood static void sof_pcm_remove(struct snd_soc_component *component)
862868bd00fSLiam Girdwood {
863868bd00fSLiam Girdwood 	/* remove topology */
864a5b8f71cSAmadeusz Sławiński 	snd_soc_tplg_component_remove(component);
865868bd00fSLiam Girdwood }
866868bd00fSLiam Girdwood 
867868bd00fSLiam Girdwood void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
868868bd00fSLiam Girdwood {
869868bd00fSLiam Girdwood 	struct snd_soc_component_driver *pd = &sdev->plat_drv;
870868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
871868bd00fSLiam Girdwood 	const char *drv_name;
872868bd00fSLiam Girdwood 
873868bd00fSLiam Girdwood 	drv_name = plat_data->machine->drv_name;
874868bd00fSLiam Girdwood 
875868bd00fSLiam Girdwood 	pd->name = "sof-audio-component";
876868bd00fSLiam Girdwood 	pd->probe = sof_pcm_probe;
877868bd00fSLiam Girdwood 	pd->remove = sof_pcm_remove;
8781c91d77eSKuninori Morimoto 	pd->open = sof_pcm_open;
8791c91d77eSKuninori Morimoto 	pd->close = sof_pcm_close;
8801c91d77eSKuninori Morimoto 	pd->hw_params = sof_pcm_hw_params;
8811c91d77eSKuninori Morimoto 	pd->prepare = sof_pcm_prepare;
8821c91d77eSKuninori Morimoto 	pd->hw_free = sof_pcm_hw_free;
8831c91d77eSKuninori Morimoto 	pd->trigger = sof_pcm_trigger;
8841c91d77eSKuninori Morimoto 	pd->pointer = sof_pcm_pointer;
8851c91d77eSKuninori Morimoto 
88670368106SCezary Rojewski #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
88739118ce5SKuninori Morimoto 	pd->compress_ops = &sof_probe_compressed_ops;
88870368106SCezary Rojewski #endif
8891c91d77eSKuninori Morimoto 	pd->pcm_construct = sof_pcm_new;
890868bd00fSLiam Girdwood 	pd->ignore_machine = drv_name;
891868bd00fSLiam Girdwood 	pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
892868bd00fSLiam Girdwood 	pd->be_pcm_base = SOF_BE_PCM_BASE;
893868bd00fSLiam Girdwood 	pd->use_dai_pcm_id = true;
894868bd00fSLiam Girdwood 	pd->topology_name_prefix = "sof";
895868bd00fSLiam Girdwood 
896868bd00fSLiam Girdwood 	 /* increment module refcount when a pcm is opened */
897868bd00fSLiam Girdwood 	pd->module_get_upon_open = 1;
898868bd00fSLiam Girdwood }
899