xref: /openbmc/linux/sound/soc/sof/pcm.c (revision 757ce8103c9e5b83cf18a669fe38484b0be3cfaf)
1e149ca29SPierre-Louis Bossart // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2868bd00fSLiam Girdwood //
3868bd00fSLiam Girdwood // This file is provided under a dual BSD/GPLv2 license.  When using or
4868bd00fSLiam Girdwood // redistributing this file, you may do so under either license.
5868bd00fSLiam Girdwood //
6868bd00fSLiam Girdwood // Copyright(c) 2018 Intel Corporation. All rights reserved.
7868bd00fSLiam Girdwood //
8868bd00fSLiam Girdwood // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9868bd00fSLiam Girdwood //
10868bd00fSLiam Girdwood // PCM Layer, interface between ALSA and IPC.
11868bd00fSLiam Girdwood //
12868bd00fSLiam Girdwood 
13868bd00fSLiam Girdwood #include <linux/pm_runtime.h>
14868bd00fSLiam Girdwood #include <sound/pcm_params.h>
15868bd00fSLiam Girdwood #include <sound/sof.h>
16868bd00fSLiam Girdwood #include "sof-priv.h"
17ee1e79b7SRanjani Sridharan #include "sof-audio.h"
18ee844305SPeter Ujfalusi #include "sof-utils.h"
193dc0d709SPeter Ujfalusi #include "ops.h"
20868bd00fSLiam Girdwood 
21868bd00fSLiam Girdwood /* Create DMA buffer page table for DSP */
221c91d77eSKuninori Morimoto static int create_page_table(struct snd_soc_component *component,
231c91d77eSKuninori Morimoto 			     struct snd_pcm_substream *substream,
24868bd00fSLiam Girdwood 			     unsigned char *dma_area, size_t size)
25868bd00fSLiam Girdwood {
261205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
27868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
28868bd00fSLiam Girdwood 	struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
29868bd00fSLiam Girdwood 	int stream = substream->stream;
30868bd00fSLiam Girdwood 
31ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
32868bd00fSLiam Girdwood 	if (!spcm)
33868bd00fSLiam Girdwood 		return -EINVAL;
34868bd00fSLiam Girdwood 
35ee1e79b7SRanjani Sridharan 	return snd_sof_create_page_table(component->dev, dmab,
36868bd00fSLiam Girdwood 		spcm->stream[stream].page_table.area, size);
37868bd00fSLiam Girdwood }
38868bd00fSLiam Girdwood 
39e2803e61SKeyon Jie /*
40e2803e61SKeyon Jie  * sof pcm period elapse work
41e2803e61SKeyon Jie  */
42858f7a5cSDaniel Baluta static void snd_sof_pcm_period_elapsed_work(struct work_struct *work)
43e2803e61SKeyon Jie {
44e2803e61SKeyon Jie 	struct snd_sof_pcm_stream *sps =
45e2803e61SKeyon Jie 		container_of(work, struct snd_sof_pcm_stream,
46e2803e61SKeyon Jie 			     period_elapsed_work);
47e2803e61SKeyon Jie 
48e2803e61SKeyon Jie 	snd_pcm_period_elapsed(sps->substream);
49e2803e61SKeyon Jie }
50e2803e61SKeyon Jie 
51858f7a5cSDaniel Baluta void snd_sof_pcm_init_elapsed_work(struct work_struct *work)
52858f7a5cSDaniel Baluta {
53858f7a5cSDaniel Baluta 	 INIT_WORK(work, snd_sof_pcm_period_elapsed_work);
54858f7a5cSDaniel Baluta }
55858f7a5cSDaniel Baluta 
56e2803e61SKeyon Jie /*
57e2803e61SKeyon Jie  * sof pcm period elapse, this could be called at irq thread context.
58e2803e61SKeyon Jie  */
59e2803e61SKeyon Jie void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
60e2803e61SKeyon Jie {
611205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
62e2803e61SKeyon Jie 	struct snd_soc_component *component =
63ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
64e2803e61SKeyon Jie 	struct snd_sof_pcm *spcm;
65e2803e61SKeyon Jie 
66ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
67e2803e61SKeyon Jie 	if (!spcm) {
68ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
69e2803e61SKeyon Jie 			"error: period elapsed for unknown stream!\n");
70e2803e61SKeyon Jie 		return;
71e2803e61SKeyon Jie 	}
72e2803e61SKeyon Jie 
73e2803e61SKeyon Jie 	/*
74e2803e61SKeyon Jie 	 * snd_pcm_period_elapsed() can be called in interrupt context
75e2803e61SKeyon Jie 	 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(),
76e2803e61SKeyon Jie 	 * when the PCM is done draining or xrun happened, a STOP IPC will
77e2803e61SKeyon Jie 	 * then be sent and this IPC will hit IPC timeout.
78e2803e61SKeyon Jie 	 * To avoid sending IPC before the previous IPC is handled, we
79e2803e61SKeyon Jie 	 * schedule delayed work here to call the snd_pcm_period_elapsed().
80e2803e61SKeyon Jie 	 */
81e2803e61SKeyon Jie 	schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
82e2803e61SKeyon Jie }
83e2803e61SKeyon Jie EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
84e2803e61SKeyon Jie 
8596da1740SRanjani Sridharan int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream, struct snd_sof_dev *sdev,
86cfe8191bSKai Vehmanen 			 struct snd_sof_pcm *spcm)
87cfe8191bSKai Vehmanen {
88cfe8191bSKai Vehmanen 	struct sof_ipc_stream stream;
89cfe8191bSKai Vehmanen 	struct sof_ipc_reply reply;
90cfe8191bSKai Vehmanen 	int ret;
91cfe8191bSKai Vehmanen 
9285d7acd0SRanjani Sridharan 	if (!spcm->prepared[substream->stream])
9385d7acd0SRanjani Sridharan 		return 0;
9485d7acd0SRanjani Sridharan 
95cfe8191bSKai Vehmanen 	stream.hdr.size = sizeof(stream);
96cfe8191bSKai Vehmanen 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
97cfe8191bSKai Vehmanen 	stream.comp_id = spcm->stream[substream->stream].comp_id;
98cfe8191bSKai Vehmanen 
99cfe8191bSKai Vehmanen 	/* send IPC to the DSP */
100cfe8191bSKai Vehmanen 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
101cfe8191bSKai Vehmanen 				 sizeof(stream), &reply, sizeof(reply));
102cfe8191bSKai Vehmanen 	if (!ret)
103cfe8191bSKai Vehmanen 		spcm->prepared[substream->stream] = false;
104cfe8191bSKai Vehmanen 
105cfe8191bSKai Vehmanen 	return ret;
106cfe8191bSKai Vehmanen }
107cfe8191bSKai Vehmanen 
1085fcdbb2dSRanjani Sridharan static int sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev,
1095fcdbb2dSRanjani Sridharan 					   struct snd_soc_pcm_runtime *rtd,
1105fcdbb2dSRanjani Sridharan 					   struct snd_sof_pcm *spcm, int dir)
1115fcdbb2dSRanjani Sridharan {
1125fcdbb2dSRanjani Sridharan 	struct snd_soc_dai *dai;
1135fcdbb2dSRanjani Sridharan 	int ret, j;
1145fcdbb2dSRanjani Sridharan 
1155fcdbb2dSRanjani Sridharan 	/* query DAPM for list of connected widgets and set them up */
1165fcdbb2dSRanjani Sridharan 	for_each_rtd_cpu_dais(rtd, j, dai) {
1175fcdbb2dSRanjani Sridharan 		struct snd_soc_dapm_widget_list *list;
1185fcdbb2dSRanjani Sridharan 
1195fcdbb2dSRanjani Sridharan 		ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list,
1205fcdbb2dSRanjani Sridharan 							     dpcm_end_walk_at_be);
1215fcdbb2dSRanjani Sridharan 		if (ret < 0) {
1225fcdbb2dSRanjani Sridharan 			dev_err(sdev->dev, "error: dai %s has no valid %s path\n", dai->name,
1235fcdbb2dSRanjani Sridharan 				dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
1245fcdbb2dSRanjani Sridharan 			return ret;
1255fcdbb2dSRanjani Sridharan 		}
1265fcdbb2dSRanjani Sridharan 
1275fcdbb2dSRanjani Sridharan 		spcm->stream[dir].list = list;
1285fcdbb2dSRanjani Sridharan 
1295fcdbb2dSRanjani Sridharan 		ret = sof_widget_list_setup(sdev, spcm, dir);
1305fcdbb2dSRanjani Sridharan 		if (ret < 0) {
1315fcdbb2dSRanjani Sridharan 			dev_err(sdev->dev, "error: failed widget list set up for pcm %d dir %d\n",
1325fcdbb2dSRanjani Sridharan 				spcm->pcm.pcm_id, dir);
1335fcdbb2dSRanjani Sridharan 			spcm->stream[dir].list = NULL;
1345fcdbb2dSRanjani Sridharan 			snd_soc_dapm_dai_free_widgets(&list);
1355fcdbb2dSRanjani Sridharan 			return ret;
1365fcdbb2dSRanjani Sridharan 		}
1375fcdbb2dSRanjani Sridharan 	}
1385fcdbb2dSRanjani Sridharan 
1395fcdbb2dSRanjani Sridharan 	return 0;
1405fcdbb2dSRanjani Sridharan }
1415fcdbb2dSRanjani Sridharan 
1421c91d77eSKuninori Morimoto static int sof_pcm_hw_params(struct snd_soc_component *component,
1431c91d77eSKuninori Morimoto 			     struct snd_pcm_substream *substream,
144868bd00fSLiam Girdwood 			     struct snd_pcm_hw_params *params)
145868bd00fSLiam Girdwood {
1461205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
147868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
148868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
14931f60a0cSPeter Ujfalusi 	struct snd_sof_platform_stream_params platform_params = { 0 };
15031f60a0cSPeter Ujfalusi 	struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
151868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
152868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params pcm;
153868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params_reply ipc_params_reply;
154868bd00fSLiam Girdwood 	int ret;
155868bd00fSLiam Girdwood 
156868bd00fSLiam Girdwood 	/* nothing to do for BE */
157868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
158868bd00fSLiam Girdwood 		return 0;
159868bd00fSLiam Girdwood 
160ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
161868bd00fSLiam Girdwood 	if (!spcm)
162868bd00fSLiam Girdwood 		return -EINVAL;
163868bd00fSLiam Girdwood 
164cfe8191bSKai Vehmanen 	/*
165cfe8191bSKai Vehmanen 	 * Handle repeated calls to hw_params() without free_pcm() in
166cfe8191bSKai Vehmanen 	 * between. At least ALSA OSS emulation depends on this.
167cfe8191bSKai Vehmanen 	 */
168cfe8191bSKai Vehmanen 	ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
169cfe8191bSKai Vehmanen 	if (ret < 0)
170cfe8191bSKai Vehmanen 		return ret;
171cfe8191bSKai Vehmanen 
172ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
173868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream);
174868bd00fSLiam Girdwood 
175868bd00fSLiam Girdwood 	memset(&pcm, 0, sizeof(pcm));
176868bd00fSLiam Girdwood 
17757e960f0STakashi Iwai 	/* create compressed page table for audio firmware */
17857e960f0STakashi Iwai 	if (runtime->buffer_changed) {
1791c91d77eSKuninori Morimoto 		ret = create_page_table(component, substream, runtime->dma_area,
180868bd00fSLiam Girdwood 					runtime->dma_bytes);
181868bd00fSLiam Girdwood 		if (ret < 0)
182868bd00fSLiam Girdwood 			return ret;
183868bd00fSLiam Girdwood 	}
184868bd00fSLiam Girdwood 
185868bd00fSLiam Girdwood 	/* number of pages should be rounded up */
186868bd00fSLiam Girdwood 	pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
187868bd00fSLiam Girdwood 
188868bd00fSLiam Girdwood 	/* set IPC PCM parameters */
189868bd00fSLiam Girdwood 	pcm.hdr.size = sizeof(pcm);
190868bd00fSLiam Girdwood 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
191868bd00fSLiam Girdwood 	pcm.comp_id = spcm->stream[substream->stream].comp_id;
192868bd00fSLiam Girdwood 	pcm.params.hdr.size = sizeof(pcm.params);
193868bd00fSLiam Girdwood 	pcm.params.buffer.phy_addr =
194868bd00fSLiam Girdwood 		spcm->stream[substream->stream].page_table.addr;
195868bd00fSLiam Girdwood 	pcm.params.buffer.size = runtime->dma_bytes;
196868bd00fSLiam Girdwood 	pcm.params.direction = substream->stream;
197868bd00fSLiam Girdwood 	pcm.params.sample_valid_bytes = params_width(params) >> 3;
198868bd00fSLiam Girdwood 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
199868bd00fSLiam Girdwood 	pcm.params.rate = params_rate(params);
200868bd00fSLiam Girdwood 	pcm.params.channels = params_channels(params);
201868bd00fSLiam Girdwood 	pcm.params.host_period_bytes = params_period_bytes(params);
202868bd00fSLiam Girdwood 
203868bd00fSLiam Girdwood 	/* container size */
204868bd00fSLiam Girdwood 	ret = snd_pcm_format_physical_width(params_format(params));
205868bd00fSLiam Girdwood 	if (ret < 0)
206868bd00fSLiam Girdwood 		return ret;
207868bd00fSLiam Girdwood 	pcm.params.sample_container_bytes = ret >> 3;
208868bd00fSLiam Girdwood 
209868bd00fSLiam Girdwood 	/* format */
210868bd00fSLiam Girdwood 	switch (params_format(params)) {
211868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S16:
212868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
213868bd00fSLiam Girdwood 		break;
214868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S24:
215868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
216868bd00fSLiam Girdwood 		break;
217868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S32:
218868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
219868bd00fSLiam Girdwood 		break;
220868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_FLOAT:
221868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
222868bd00fSLiam Girdwood 		break;
223868bd00fSLiam Girdwood 	default:
224868bd00fSLiam Girdwood 		return -EINVAL;
225868bd00fSLiam Girdwood 	}
226868bd00fSLiam Girdwood 
227868bd00fSLiam Girdwood 	/* firmware already configured host stream */
228868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_hw_params(sdev,
229868bd00fSLiam Girdwood 					     substream,
230868bd00fSLiam Girdwood 					     params,
23131f60a0cSPeter Ujfalusi 					     &platform_params);
232868bd00fSLiam Girdwood 	if (ret < 0) {
233ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw params failed\n");
234868bd00fSLiam Girdwood 		return ret;
235868bd00fSLiam Girdwood 	}
236868bd00fSLiam Girdwood 
23731f60a0cSPeter Ujfalusi 	/* Update the IPC message with information from the platform */
23831f60a0cSPeter Ujfalusi 	pcm.params.stream_tag = platform_params.stream_tag;
23931f60a0cSPeter Ujfalusi 
24031f60a0cSPeter Ujfalusi 	if (platform_params.use_phy_address)
24131f60a0cSPeter Ujfalusi 		pcm.params.buffer.phy_addr = platform_params.phy_addr;
24231f60a0cSPeter Ujfalusi 
24331f60a0cSPeter Ujfalusi 	if (platform_params.no_ipc_position) {
24431f60a0cSPeter Ujfalusi 		/* For older ABIs set host_period_bytes to zero to inform
24531f60a0cSPeter Ujfalusi 		 * FW we don't want position updates. Newer versions use
24631f60a0cSPeter Ujfalusi 		 * no_stream_position for this purpose.
24731f60a0cSPeter Ujfalusi 		 */
24831f60a0cSPeter Ujfalusi 		if (v->abi_version < SOF_ABI_VER(3, 10, 0))
24931f60a0cSPeter Ujfalusi 			pcm.params.host_period_bytes = 0;
25031f60a0cSPeter Ujfalusi 		else
25131f60a0cSPeter Ujfalusi 			pcm.params.no_stream_position = 1;
25231f60a0cSPeter Ujfalusi 	}
25331f60a0cSPeter Ujfalusi 
254ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag);
255868bd00fSLiam Girdwood 
2565fcdbb2dSRanjani Sridharan 	/* if this is a repeated hw_params without hw_free, skip setting up widgets */
2575fcdbb2dSRanjani Sridharan 	if (!spcm->stream[substream->stream].list) {
2585fcdbb2dSRanjani Sridharan 		ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, substream->stream);
2595fcdbb2dSRanjani Sridharan 		if (ret < 0)
2605fcdbb2dSRanjani Sridharan 			return ret;
2615fcdbb2dSRanjani Sridharan 	}
2625fcdbb2dSRanjani Sridharan 
2635fcdbb2dSRanjani Sridharan 	/* send hw_params IPC to the DSP */
264868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
265868bd00fSLiam Girdwood 				 &ipc_params_reply, sizeof(ipc_params_reply));
266868bd00fSLiam Girdwood 	if (ret < 0) {
267ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: hw params ipc failed for stream %d\n",
268868bd00fSLiam Girdwood 			pcm.params.stream_tag);
269868bd00fSLiam Girdwood 		return ret;
270868bd00fSLiam Girdwood 	}
271868bd00fSLiam Girdwood 
272d1b1146fSPeter Ujfalusi 	ret = snd_sof_ipc_pcm_params(sdev, substream, &ipc_params_reply);
273d1b1146fSPeter Ujfalusi 	if (ret < 0) {
274d1b1146fSPeter Ujfalusi 		dev_err(component->dev, "%s: got wrong reply for PCM %d\n",
275d1b1146fSPeter Ujfalusi 			__func__, spcm->pcm.pcm_id);
276868bd00fSLiam Girdwood 		return ret;
277d1b1146fSPeter Ujfalusi 	}
278868bd00fSLiam Girdwood 
279*757ce810SPeter Ujfalusi 	ret = snd_sof_set_stream_data_offset(sdev, substream,
280*757ce810SPeter Ujfalusi 					     ipc_params_reply.posn_offset);
281*757ce810SPeter Ujfalusi 	if (ret < 0) {
282*757ce810SPeter Ujfalusi 		dev_err(component->dev, "%s: invalid stream data offset for PCM %d\n",
283*757ce810SPeter Ujfalusi 			__func__, spcm->pcm.pcm_id);
284*757ce810SPeter Ujfalusi 		return ret;
285*757ce810SPeter Ujfalusi 	}
286*757ce810SPeter Ujfalusi 
28704c80277SKai Vehmanen 	spcm->prepared[substream->stream] = true;
28804c80277SKai Vehmanen 
289868bd00fSLiam Girdwood 	/* save pcm hw_params */
290868bd00fSLiam Girdwood 	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
291868bd00fSLiam Girdwood 
292868bd00fSLiam Girdwood 	return ret;
293868bd00fSLiam Girdwood }
294868bd00fSLiam Girdwood 
2951c91d77eSKuninori Morimoto static int sof_pcm_hw_free(struct snd_soc_component *component,
2961c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
297868bd00fSLiam Girdwood {
2981205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
299868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
300868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
301e66e52c5SKai Vehmanen 	int ret, err = 0;
302868bd00fSLiam Girdwood 
303868bd00fSLiam Girdwood 	/* nothing to do for BE */
304868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
305868bd00fSLiam Girdwood 		return 0;
306868bd00fSLiam Girdwood 
307ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
308868bd00fSLiam Girdwood 	if (!spcm)
309868bd00fSLiam Girdwood 		return -EINVAL;
310868bd00fSLiam Girdwood 
311ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
312ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
313868bd00fSLiam Girdwood 
3140b639dcdSRanjani Sridharan 	/* free PCM in the DSP */
315a49b6871SKai Vehmanen 	ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
316e66e52c5SKai Vehmanen 	if (ret < 0)
317e66e52c5SKai Vehmanen 		err = ret;
318868bd00fSLiam Girdwood 
3195fcdbb2dSRanjani Sridharan 
3200b639dcdSRanjani Sridharan 	/* stop DMA */
32193146bc2SRanjani Sridharan 	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
322e66e52c5SKai Vehmanen 	if (ret < 0) {
323ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw free failed\n");
324e66e52c5SKai Vehmanen 		err = ret;
325e66e52c5SKai Vehmanen 	}
32693146bc2SRanjani Sridharan 
3270b639dcdSRanjani Sridharan 	/* free the DAPM widget list */
3280b639dcdSRanjani Sridharan 	ret = sof_widget_list_free(sdev, spcm, substream->stream);
3290b639dcdSRanjani Sridharan 	if (ret < 0)
3300b639dcdSRanjani Sridharan 		err = ret;
3310b639dcdSRanjani Sridharan 
3320b639dcdSRanjani Sridharan 	cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
3330b639dcdSRanjani Sridharan 
334e66e52c5SKai Vehmanen 	return err;
335868bd00fSLiam Girdwood }
336868bd00fSLiam Girdwood 
3371c91d77eSKuninori Morimoto static int sof_pcm_prepare(struct snd_soc_component *component,
3381c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
339868bd00fSLiam Girdwood {
3401205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
341868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
342868bd00fSLiam Girdwood 	int ret;
343868bd00fSLiam Girdwood 
344868bd00fSLiam Girdwood 	/* nothing to do for BE */
345868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
346868bd00fSLiam Girdwood 		return 0;
347868bd00fSLiam Girdwood 
348ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
349868bd00fSLiam Girdwood 	if (!spcm)
350868bd00fSLiam Girdwood 		return -EINVAL;
351868bd00fSLiam Girdwood 
35204c80277SKai Vehmanen 	if (spcm->prepared[substream->stream])
353868bd00fSLiam Girdwood 		return 0;
354868bd00fSLiam Girdwood 
355ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
356ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
357868bd00fSLiam Girdwood 
358868bd00fSLiam Girdwood 	/* set hw_params */
3591c91d77eSKuninori Morimoto 	ret = sof_pcm_hw_params(component,
3601c91d77eSKuninori Morimoto 				substream, &spcm->params[substream->stream]);
361868bd00fSLiam Girdwood 	if (ret < 0) {
362ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
363ee1e79b7SRanjani Sridharan 			"error: set pcm hw_params after resume\n");
364868bd00fSLiam Girdwood 		return ret;
365868bd00fSLiam Girdwood 	}
366868bd00fSLiam Girdwood 
367868bd00fSLiam Girdwood 	return 0;
368868bd00fSLiam Girdwood }
369868bd00fSLiam Girdwood 
370868bd00fSLiam Girdwood /*
371868bd00fSLiam Girdwood  * FE dai link trigger actions are always executed in non-atomic context because
372868bd00fSLiam Girdwood  * they involve IPC's.
373868bd00fSLiam Girdwood  */
3741c91d77eSKuninori Morimoto static int sof_pcm_trigger(struct snd_soc_component *component,
3751c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream, int cmd)
376868bd00fSLiam Girdwood {
3771205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
378868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
379868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
380868bd00fSLiam Girdwood 	struct sof_ipc_stream stream;
381868bd00fSLiam Girdwood 	struct sof_ipc_reply reply;
38204c80277SKai Vehmanen 	bool reset_hw_params = false;
3835fcdbb2dSRanjani Sridharan 	bool free_widget_list = false;
3840a1b0834SPan Xiuli 	bool ipc_first = false;
385868bd00fSLiam Girdwood 	int ret;
386868bd00fSLiam Girdwood 
387868bd00fSLiam Girdwood 	/* nothing to do for BE */
388868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
389868bd00fSLiam Girdwood 		return 0;
390868bd00fSLiam Girdwood 
391ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
392868bd00fSLiam Girdwood 	if (!spcm)
393868bd00fSLiam Girdwood 		return -EINVAL;
394868bd00fSLiam Girdwood 
395ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
396868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream, cmd);
397868bd00fSLiam Girdwood 
398868bd00fSLiam Girdwood 	stream.hdr.size = sizeof(stream);
399868bd00fSLiam Girdwood 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
400868bd00fSLiam Girdwood 	stream.comp_id = spcm->stream[substream->stream].comp_id;
401868bd00fSLiam Girdwood 
402868bd00fSLiam Girdwood 	switch (cmd) {
403868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
404868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
4050a1b0834SPan Xiuli 		ipc_first = true;
406868bd00fSLiam Girdwood 		break;
407868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
408868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
409868bd00fSLiam Girdwood 		break;
410868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
411ac8c046fSKeyon Jie 		if (spcm->stream[substream->stream].suspend_ignored) {
412ac8c046fSKeyon Jie 			/*
413ac8c046fSKeyon Jie 			 * This case will be triggered when INFO_RESUME is
414ac8c046fSKeyon Jie 			 * not supported, no need to re-start streams that
415ac8c046fSKeyon Jie 			 * remained enabled in D0ix.
416ac8c046fSKeyon Jie 			 */
417ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = false;
418ac8c046fSKeyon Jie 			return 0;
419ac8c046fSKeyon Jie 		}
420868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
421868bd00fSLiam Girdwood 		break;
422868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
423043ae13bSRanjani Sridharan 		if (sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
424ac8c046fSKeyon Jie 		    spcm->stream[substream->stream].d0i3_compatible) {
425ac8c046fSKeyon Jie 			/*
426ac8c046fSKeyon Jie 			 * trap the event, not sending trigger stop to
427ac8c046fSKeyon Jie 			 * prevent the FW pipelines from being stopped,
428ac8c046fSKeyon Jie 			 * and mark the flag to ignore the upcoming DAPM
429ac8c046fSKeyon Jie 			 * PM events.
430ac8c046fSKeyon Jie 			 */
431ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = true;
432ac8c046fSKeyon Jie 			return 0;
433ac8c046fSKeyon Jie 		}
4345fcdbb2dSRanjani Sridharan 		free_widget_list = true;
435df561f66SGustavo A. R. Silva 		fallthrough;
436868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
437868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
4380a1b0834SPan Xiuli 		ipc_first = true;
43904c80277SKai Vehmanen 		reset_hw_params = true;
440868bd00fSLiam Girdwood 		break;
441868bd00fSLiam Girdwood 	default:
442ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: unhandled trigger cmd %d\n",
443ee1e79b7SRanjani Sridharan 			cmd);
444868bd00fSLiam Girdwood 		return -EINVAL;
445868bd00fSLiam Girdwood 	}
446868bd00fSLiam Girdwood 
4470a1b0834SPan Xiuli 	/*
4480a1b0834SPan Xiuli 	 * DMA and IPC sequence is different for start and stop. Need to send
4490a1b0834SPan Xiuli 	 * STOP IPC before stop DMA
4500a1b0834SPan Xiuli 	 */
4510a1b0834SPan Xiuli 	if (!ipc_first)
452868bd00fSLiam Girdwood 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
453868bd00fSLiam Girdwood 
454868bd00fSLiam Girdwood 	/* send IPC to the DSP */
455868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
456868bd00fSLiam Girdwood 				 sizeof(stream), &reply, sizeof(reply));
457868bd00fSLiam Girdwood 
4580a1b0834SPan Xiuli 	/* need to STOP DMA even if STOP IPC failed */
4590a1b0834SPan Xiuli 	if (ipc_first)
4600a1b0834SPan Xiuli 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
4610a1b0834SPan Xiuli 
4620a1b0834SPan Xiuli 	/* free PCM if reset_hw_params is set and the STOP IPC is successful */
4635fcdbb2dSRanjani Sridharan 	if (!ret && reset_hw_params) {
464d9a72465SRanjani Sridharan 		ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream,
465d9a72465SRanjani Sridharan 					  free_widget_list);
4665fcdbb2dSRanjani Sridharan 		if (ret < 0)
4675fcdbb2dSRanjani Sridharan 			return ret;
4685fcdbb2dSRanjani Sridharan 	}
469a49b6871SKai Vehmanen 
470868bd00fSLiam Girdwood 	return ret;
471868bd00fSLiam Girdwood }
472868bd00fSLiam Girdwood 
4731c91d77eSKuninori Morimoto static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
4741c91d77eSKuninori Morimoto 					 struct snd_pcm_substream *substream)
475868bd00fSLiam Girdwood {
4761205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
477868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
478868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
479868bd00fSLiam Girdwood 	snd_pcm_uframes_t host, dai;
480868bd00fSLiam Girdwood 
481868bd00fSLiam Girdwood 	/* nothing to do for BE */
482868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
483868bd00fSLiam Girdwood 		return 0;
484868bd00fSLiam Girdwood 
485868bd00fSLiam Girdwood 	/* use dsp ops pointer callback directly if set */
486868bd00fSLiam Girdwood 	if (sof_ops(sdev)->pcm_pointer)
487868bd00fSLiam Girdwood 		return sof_ops(sdev)->pcm_pointer(sdev, substream);
488868bd00fSLiam Girdwood 
489ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
490868bd00fSLiam Girdwood 	if (!spcm)
491868bd00fSLiam Girdwood 		return -EINVAL;
492868bd00fSLiam Girdwood 
493868bd00fSLiam Girdwood 	/* read position from DSP */
494868bd00fSLiam Girdwood 	host = bytes_to_frames(substream->runtime,
495868bd00fSLiam Girdwood 			       spcm->stream[substream->stream].posn.host_posn);
496868bd00fSLiam Girdwood 	dai = bytes_to_frames(substream->runtime,
497868bd00fSLiam Girdwood 			      spcm->stream[substream->stream].posn.dai_posn);
498868bd00fSLiam Girdwood 
499277ff236SPierre-Louis Bossart 	dev_vdbg(component->dev,
500ee1e79b7SRanjani Sridharan 		 "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
501868bd00fSLiam Girdwood 		 spcm->pcm.pcm_id, substream->stream, host, dai);
502868bd00fSLiam Girdwood 
503868bd00fSLiam Girdwood 	return host;
504868bd00fSLiam Girdwood }
505868bd00fSLiam Girdwood 
5061c91d77eSKuninori Morimoto static int sof_pcm_open(struct snd_soc_component *component,
5071c91d77eSKuninori Morimoto 			struct snd_pcm_substream *substream)
508868bd00fSLiam Girdwood {
5091205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
510868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
511868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
51227e322faSPierre-Louis Bossart 	const struct snd_sof_dsp_ops *ops = sof_ops(sdev);
513868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
514868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
515868bd00fSLiam Girdwood 	int ret;
516868bd00fSLiam Girdwood 
517868bd00fSLiam Girdwood 	/* nothing to do for BE */
518868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
519868bd00fSLiam Girdwood 		return 0;
520868bd00fSLiam Girdwood 
521ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
522868bd00fSLiam Girdwood 	if (!spcm)
523868bd00fSLiam Girdwood 		return -EINVAL;
524868bd00fSLiam Girdwood 
525ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
526ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
527868bd00fSLiam Girdwood 
528868bd00fSLiam Girdwood 
529868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[substream->stream];
530868bd00fSLiam Girdwood 
531868bd00fSLiam Girdwood 	/* set runtime config */
53227e322faSPierre-Louis Bossart 	runtime->hw.info = ops->hw_info; /* platform-specific */
53327e322faSPierre-Louis Bossart 
5349983ac49SKai Vehmanen 	/* set any runtime constraints based on topology */
535868bd00fSLiam Girdwood 	runtime->hw.formats = le64_to_cpu(caps->formats);
536868bd00fSLiam Girdwood 	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
537868bd00fSLiam Girdwood 	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
538868bd00fSLiam Girdwood 	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
539868bd00fSLiam Girdwood 	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
540868bd00fSLiam Girdwood 
541868bd00fSLiam Girdwood 	/*
542868bd00fSLiam Girdwood 	 * caps->buffer_size_min is not used since the
543868bd00fSLiam Girdwood 	 * snd_pcm_hardware structure only defines buffer_bytes_max
544868bd00fSLiam Girdwood 	 */
545868bd00fSLiam Girdwood 	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
546868bd00fSLiam Girdwood 
547ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period min %zd max %zd bytes\n",
548868bd00fSLiam Girdwood 		runtime->hw.period_bytes_min,
549868bd00fSLiam Girdwood 		runtime->hw.period_bytes_max);
550ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period count %d max %d\n",
551868bd00fSLiam Girdwood 		runtime->hw.periods_min,
552868bd00fSLiam Girdwood 		runtime->hw.periods_max);
553ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "buffer max %zd bytes\n",
554868bd00fSLiam Girdwood 		runtime->hw.buffer_bytes_max);
555868bd00fSLiam Girdwood 
556868bd00fSLiam Girdwood 	/* set wait time - TODO: come from topology */
557868bd00fSLiam Girdwood 	substream->wait_time = 500;
558868bd00fSLiam Girdwood 
559868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.host_posn = 0;
560868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.dai_posn = 0;
561868bd00fSLiam Girdwood 	spcm->stream[substream->stream].substream = substream;
56204c80277SKai Vehmanen 	spcm->prepared[substream->stream] = false;
563868bd00fSLiam Girdwood 
564868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_open(sdev, substream);
56514a2212dSRanjani Sridharan 	if (ret < 0)
566ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm open failed %d\n", ret);
567868bd00fSLiam Girdwood 
568868bd00fSLiam Girdwood 	return ret;
569868bd00fSLiam Girdwood }
570868bd00fSLiam Girdwood 
5711c91d77eSKuninori Morimoto static int sof_pcm_close(struct snd_soc_component *component,
5721c91d77eSKuninori Morimoto 			 struct snd_pcm_substream *substream)
573868bd00fSLiam Girdwood {
5741205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
575868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
576868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
577868bd00fSLiam Girdwood 	int err;
578868bd00fSLiam Girdwood 
579868bd00fSLiam Girdwood 	/* nothing to do for BE */
580868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
581868bd00fSLiam Girdwood 		return 0;
582868bd00fSLiam Girdwood 
583ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
584868bd00fSLiam Girdwood 	if (!spcm)
585868bd00fSLiam Girdwood 		return -EINVAL;
586868bd00fSLiam Girdwood 
587ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
588ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
589868bd00fSLiam Girdwood 
590868bd00fSLiam Girdwood 	err = snd_sof_pcm_platform_close(sdev, substream);
591868bd00fSLiam Girdwood 	if (err < 0) {
592ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm close failed %d\n",
593868bd00fSLiam Girdwood 			err);
594868bd00fSLiam Girdwood 		/*
595868bd00fSLiam Girdwood 		 * keep going, no point in preventing the close
596868bd00fSLiam Girdwood 		 * from happening
597868bd00fSLiam Girdwood 		 */
598868bd00fSLiam Girdwood 	}
599868bd00fSLiam Girdwood 
600868bd00fSLiam Girdwood 	return 0;
601868bd00fSLiam Girdwood }
602868bd00fSLiam Girdwood 
603868bd00fSLiam Girdwood /*
604868bd00fSLiam Girdwood  * Pre-allocate playback/capture audio buffer pages.
605868bd00fSLiam Girdwood  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
606868bd00fSLiam Girdwood  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
607868bd00fSLiam Girdwood  */
6081c91d77eSKuninori Morimoto static int sof_pcm_new(struct snd_soc_component *component,
6091c91d77eSKuninori Morimoto 		       struct snd_soc_pcm_runtime *rtd)
610868bd00fSLiam Girdwood {
611868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
612868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
613868bd00fSLiam Girdwood 	struct snd_pcm *pcm = rtd->pcm;
614868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
615868bd00fSLiam Girdwood 	int stream = SNDRV_PCM_STREAM_PLAYBACK;
616868bd00fSLiam Girdwood 
617868bd00fSLiam Girdwood 	/* find SOF PCM for this RTD */
618ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
619868bd00fSLiam Girdwood 	if (!spcm) {
620ee1e79b7SRanjani Sridharan 		dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
621868bd00fSLiam Girdwood 			 rtd->dai_link->id);
622868bd00fSLiam Girdwood 		return 0;
623868bd00fSLiam Girdwood 	}
624868bd00fSLiam Girdwood 
625ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
626868bd00fSLiam Girdwood 
627868bd00fSLiam Girdwood 	/* do we need to pre-allocate playback audio buffer pages */
628868bd00fSLiam Girdwood 	if (!spcm->pcm.playback)
629868bd00fSLiam Girdwood 		goto capture;
630868bd00fSLiam Girdwood 
631868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
632868bd00fSLiam Girdwood 
633868bd00fSLiam Girdwood 	/* pre-allocate playback audio buffer pages */
634ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
635ee1e79b7SRanjani Sridharan 		"spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
636868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
637868bd00fSLiam Girdwood 
6384f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
6394f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL playback substream!\n");
6404f7f9564SGuennadi Liakhovetski 		return -EINVAL;
6414f7f9564SGuennadi Liakhovetski 	}
6424f7f9564SGuennadi Liakhovetski 
64357e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
644868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
645e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
646868bd00fSLiam Girdwood capture:
647868bd00fSLiam Girdwood 	stream = SNDRV_PCM_STREAM_CAPTURE;
648868bd00fSLiam Girdwood 
649868bd00fSLiam Girdwood 	/* do we need to pre-allocate capture audio buffer pages */
650868bd00fSLiam Girdwood 	if (!spcm->pcm.capture)
651868bd00fSLiam Girdwood 		return 0;
652868bd00fSLiam Girdwood 
653868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
654868bd00fSLiam Girdwood 
655868bd00fSLiam Girdwood 	/* pre-allocate capture audio buffer pages */
656ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
657ee1e79b7SRanjani Sridharan 		"spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
658868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
659868bd00fSLiam Girdwood 
6604f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
6614f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL capture substream!\n");
6624f7f9564SGuennadi Liakhovetski 		return -EINVAL;
6634f7f9564SGuennadi Liakhovetski 	}
6644f7f9564SGuennadi Liakhovetski 
66557e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
666868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
667e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
668868bd00fSLiam Girdwood 
669868bd00fSLiam Girdwood 	return 0;
670868bd00fSLiam Girdwood }
671868bd00fSLiam Girdwood 
672c943a586SJaska Uimonen static void ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
673c943a586SJaska Uimonen 					    struct snd_pcm_hw_params *params)
674c943a586SJaska Uimonen {
675c943a586SJaska Uimonen 	struct sof_ipc_dai_config *config;
676c943a586SJaska Uimonen 	struct snd_sof_dai *dai;
677c943a586SJaska Uimonen 	int i;
678c943a586SJaska Uimonen 
679c943a586SJaska Uimonen 	/*
680c943a586SJaska Uimonen 	 * Search for all matching DAIs as we can have both playback and capture DAI
681c943a586SJaska Uimonen 	 * associated with the same link.
682c943a586SJaska Uimonen 	 */
683c943a586SJaska Uimonen 	list_for_each_entry(dai, &sdev->dai_list, list) {
684c943a586SJaska Uimonen 		if (!dai->name || strcmp(link_name, dai->name))
685c943a586SJaska Uimonen 			continue;
686c943a586SJaska Uimonen 		for (i = 0; i < dai->number_configs; i++) {
687839e484fSRanjani Sridharan 			struct sof_dai_private_data *private = dai->private;
688839e484fSRanjani Sridharan 
689839e484fSRanjani Sridharan 			config = &private->dai_config[i];
690c943a586SJaska Uimonen 			if (config->ssp.fsync_rate == params_rate(params)) {
691c943a586SJaska Uimonen 				dev_dbg(sdev->dev, "DAI config %d matches pcm hw params\n", i);
692c943a586SJaska Uimonen 				dai->current_config = i;
693c943a586SJaska Uimonen 				break;
694c943a586SJaska Uimonen 			}
695c943a586SJaska Uimonen 		}
696c943a586SJaska Uimonen 	}
697c943a586SJaska Uimonen }
698c943a586SJaska Uimonen 
699868bd00fSLiam Girdwood /* fixup the BE DAI link to match any values from topology */
700f805e7e0SRanjani Sridharan int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
701868bd00fSLiam Girdwood {
702868bd00fSLiam Girdwood 	struct snd_interval *rate = hw_param_interval(params,
703868bd00fSLiam Girdwood 			SNDRV_PCM_HW_PARAM_RATE);
704868bd00fSLiam Girdwood 	struct snd_interval *channels = hw_param_interval(params,
705868bd00fSLiam Girdwood 						SNDRV_PCM_HW_PARAM_CHANNELS);
706868bd00fSLiam Girdwood 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
707868bd00fSLiam Girdwood 	struct snd_soc_component *component =
708ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
709868bd00fSLiam Girdwood 	struct snd_sof_dai *dai =
710ee1e79b7SRanjani Sridharan 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
711c943a586SJaska Uimonen 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
712839e484fSRanjani Sridharan 	struct sof_dai_private_data *private = dai->private;
713fd045558Sranderwang 	struct snd_soc_dpcm *dpcm;
714868bd00fSLiam Girdwood 
715868bd00fSLiam Girdwood 	/* no topology exists for this BE, try a common configuration */
716868bd00fSLiam Girdwood 	if (!dai) {
717ee1e79b7SRanjani Sridharan 		dev_warn(component->dev,
718ee1e79b7SRanjani Sridharan 			 "warning: no topology found for BE DAI %s config\n",
719868bd00fSLiam Girdwood 			 rtd->dai_link->name);
720868bd00fSLiam Girdwood 
721868bd00fSLiam Girdwood 		/*  set 48k, stereo, 16bits by default */
722868bd00fSLiam Girdwood 		rate->min = 48000;
723868bd00fSLiam Girdwood 		rate->max = 48000;
724868bd00fSLiam Girdwood 
725868bd00fSLiam Girdwood 		channels->min = 2;
726868bd00fSLiam Girdwood 		channels->max = 2;
727868bd00fSLiam Girdwood 
728868bd00fSLiam Girdwood 		snd_mask_none(fmt);
729868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
730868bd00fSLiam Girdwood 
731868bd00fSLiam Girdwood 		return 0;
732868bd00fSLiam Girdwood 	}
733868bd00fSLiam Girdwood 
734868bd00fSLiam Girdwood 	/* read format from topology */
735868bd00fSLiam Girdwood 	snd_mask_none(fmt);
736868bd00fSLiam Girdwood 
737839e484fSRanjani Sridharan 	switch (private->comp_dai->config.frame_fmt) {
738868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S16_LE:
739868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
740868bd00fSLiam Girdwood 		break;
741868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S24_4LE:
742868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
743868bd00fSLiam Girdwood 		break;
744868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S32_LE:
745868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
746868bd00fSLiam Girdwood 		break;
747868bd00fSLiam Girdwood 	default:
748ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: No available DAI format!\n");
749868bd00fSLiam Girdwood 		return -EINVAL;
750868bd00fSLiam Girdwood 	}
751868bd00fSLiam Girdwood 
752868bd00fSLiam Girdwood 	/* read rate and channels from topology */
753839e484fSRanjani Sridharan 	switch (private->dai_config->type) {
754868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_SSP:
755c943a586SJaska Uimonen 		/* search for config to pcm params match, if not found use default */
756c943a586SJaska Uimonen 		ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);
757c943a586SJaska Uimonen 
758839e484fSRanjani Sridharan 		rate->min = private->dai_config[dai->current_config].ssp.fsync_rate;
759839e484fSRanjani Sridharan 		rate->max = private->dai_config[dai->current_config].ssp.fsync_rate;
760839e484fSRanjani Sridharan 		channels->min = private->dai_config[dai->current_config].ssp.tdm_slots;
761839e484fSRanjani Sridharan 		channels->max = private->dai_config[dai->current_config].ssp.tdm_slots;
762868bd00fSLiam Girdwood 
763ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
764868bd00fSLiam Girdwood 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
765ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
766868bd00fSLiam Girdwood 			"channels_min: %d channels_max: %d\n",
767868bd00fSLiam Girdwood 			channels->min, channels->max);
768868bd00fSLiam Girdwood 
769868bd00fSLiam Girdwood 		break;
770868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_DMIC:
771868bd00fSLiam Girdwood 		/* DMIC only supports 16 or 32 bit formats */
772839e484fSRanjani Sridharan 		if (private->comp_dai->config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
773ee1e79b7SRanjani Sridharan 			dev_err(component->dev,
774868bd00fSLiam Girdwood 				"error: invalid fmt %d for DAI type %d\n",
775839e484fSRanjani Sridharan 				private->comp_dai->config.frame_fmt,
776839e484fSRanjani Sridharan 				private->dai_config->type);
777868bd00fSLiam Girdwood 		}
778868bd00fSLiam Girdwood 		break;
779868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_HDA:
780fd045558Sranderwang 		/*
781135ab457SPierre-Louis Bossart 		 * HDAudio does not follow the default trigger
782fd045558Sranderwang 		 * sequence due to firmware implementation
783fd045558Sranderwang 		 */
784fd045558Sranderwang 		for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
785fd045558Sranderwang 			struct snd_soc_pcm_runtime *fe = dpcm->fe;
786fd045558Sranderwang 
787fd045558Sranderwang 			fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
788fd045558Sranderwang 				SND_SOC_DPCM_TRIGGER_POST;
789fd045558Sranderwang 		}
790868bd00fSLiam Girdwood 		break;
7916e3360cdSPierre-Louis Bossart 	case SOF_DAI_INTEL_ALH:
792e1711b1fSRander Wang 		/*
793e1711b1fSRander Wang 		 * Dai could run with different channel count compared with
794e1711b1fSRander Wang 		 * front end, so get dai channel count from topology
795e1711b1fSRander Wang 		 */
796839e484fSRanjani Sridharan 		channels->min = private->dai_config->alh.channels;
797839e484fSRanjani Sridharan 		channels->max = private->dai_config->alh.channels;
7986e3360cdSPierre-Louis Bossart 		break;
799a4eff5f8SDaniel Baluta 	case SOF_DAI_IMX_ESAI:
800839e484fSRanjani Sridharan 		rate->min = private->dai_config->esai.fsync_rate;
801839e484fSRanjani Sridharan 		rate->max = private->dai_config->esai.fsync_rate;
802839e484fSRanjani Sridharan 		channels->min = private->dai_config->esai.tdm_slots;
803839e484fSRanjani Sridharan 		channels->max = private->dai_config->esai.tdm_slots;
804a4eff5f8SDaniel Baluta 
805ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
80651b0243aSDaniel Baluta 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
80751b0243aSDaniel Baluta 		dev_dbg(component->dev,
808a4eff5f8SDaniel Baluta 			"channels_min: %d channels_max: %d\n",
809a4eff5f8SDaniel Baluta 			channels->min, channels->max);
810a4eff5f8SDaniel Baluta 		break;
811b72bfcffSYC Hung 	case SOF_DAI_MEDIATEK_AFE:
812839e484fSRanjani Sridharan 		rate->min = private->dai_config->afe.rate;
813839e484fSRanjani Sridharan 		rate->max = private->dai_config->afe.rate;
814839e484fSRanjani Sridharan 		channels->min = private->dai_config->afe.channels;
815839e484fSRanjani Sridharan 		channels->max = private->dai_config->afe.channels;
816b72bfcffSYC Hung 
817b72bfcffSYC Hung 		dev_dbg(component->dev,
818b72bfcffSYC Hung 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
819b72bfcffSYC Hung 		dev_dbg(component->dev,
820b72bfcffSYC Hung 			"channels_min: %d channels_max: %d\n",
821b72bfcffSYC Hung 			channels->min, channels->max);
822b72bfcffSYC Hung 		break;
823d88cbd6fSGuido Roncarolo 	case SOF_DAI_IMX_SAI:
824839e484fSRanjani Sridharan 		rate->min = private->dai_config->sai.fsync_rate;
825839e484fSRanjani Sridharan 		rate->max = private->dai_config->sai.fsync_rate;
826839e484fSRanjani Sridharan 		channels->min = private->dai_config->sai.tdm_slots;
827839e484fSRanjani Sridharan 		channels->max = private->dai_config->sai.tdm_slots;
828d88cbd6fSGuido Roncarolo 
829d88cbd6fSGuido Roncarolo 		dev_dbg(component->dev,
83051b0243aSDaniel Baluta 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
83151b0243aSDaniel Baluta 		dev_dbg(component->dev,
832d88cbd6fSGuido Roncarolo 			"channels_min: %d channels_max: %d\n",
833d88cbd6fSGuido Roncarolo 			channels->min, channels->max);
834d88cbd6fSGuido Roncarolo 		break;
835efb931cdSAjit Kumar Pandey 	case SOF_DAI_AMD_BT:
836839e484fSRanjani Sridharan 		rate->min = private->dai_config->acpbt.fsync_rate;
837839e484fSRanjani Sridharan 		rate->max = private->dai_config->acpbt.fsync_rate;
838839e484fSRanjani Sridharan 		channels->min = private->dai_config->acpbt.tdm_slots;
839839e484fSRanjani Sridharan 		channels->max = private->dai_config->acpbt.tdm_slots;
840efb931cdSAjit Kumar Pandey 
841efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
842efb931cdSAjit Kumar Pandey 			"AMD_BT rate_min: %d rate_max: %d\n", rate->min, rate->max);
843efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
844efb931cdSAjit Kumar Pandey 			"AMD_BT channels_min: %d channels_max: %d\n",
845efb931cdSAjit Kumar Pandey 			channels->min, channels->max);
846efb931cdSAjit Kumar Pandey 		break;
847efb931cdSAjit Kumar Pandey 	case SOF_DAI_AMD_SP:
848839e484fSRanjani Sridharan 		rate->min = private->dai_config->acpsp.fsync_rate;
849839e484fSRanjani Sridharan 		rate->max = private->dai_config->acpsp.fsync_rate;
850839e484fSRanjani Sridharan 		channels->min = private->dai_config->acpsp.tdm_slots;
851839e484fSRanjani Sridharan 		channels->max = private->dai_config->acpsp.tdm_slots;
852efb931cdSAjit Kumar Pandey 
853efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
854efb931cdSAjit Kumar Pandey 			"AMD_SP rate_min: %d rate_max: %d\n", rate->min, rate->max);
855efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
856efb931cdSAjit Kumar Pandey 			"AMD_SP channels_min: %d channels_max: %d\n",
857efb931cdSAjit Kumar Pandey 			channels->min, channels->max);
858efb931cdSAjit Kumar Pandey 		break;
859efb931cdSAjit Kumar Pandey 	case SOF_DAI_AMD_DMIC:
860839e484fSRanjani Sridharan 		rate->min = private->dai_config->acpdmic.fsync_rate;
861839e484fSRanjani Sridharan 		rate->max = private->dai_config->acpdmic.fsync_rate;
862839e484fSRanjani Sridharan 		channels->min = private->dai_config->acpdmic.tdm_slots;
863839e484fSRanjani Sridharan 		channels->max = private->dai_config->acpdmic.tdm_slots;
864efb931cdSAjit Kumar Pandey 
865efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
866efb931cdSAjit Kumar Pandey 			"AMD_DMIC rate_min: %d rate_max: %d\n", rate->min, rate->max);
867efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
868efb931cdSAjit Kumar Pandey 			"AMD_DMIC channels_min: %d channels_max: %d\n",
869efb931cdSAjit Kumar Pandey 			channels->min, channels->max);
870efb931cdSAjit Kumar Pandey 		break;
871868bd00fSLiam Girdwood 	default:
872ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: invalid DAI type %d\n",
873839e484fSRanjani Sridharan 			private->dai_config->type);
874868bd00fSLiam Girdwood 		break;
875868bd00fSLiam Girdwood 	}
876868bd00fSLiam Girdwood 
877868bd00fSLiam Girdwood 	return 0;
878868bd00fSLiam Girdwood }
879f3f3af17SPierre-Louis Bossart EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
880868bd00fSLiam Girdwood 
881868bd00fSLiam Girdwood static int sof_pcm_probe(struct snd_soc_component *component)
882868bd00fSLiam Girdwood {
883868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
884868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
885868bd00fSLiam Girdwood 	const char *tplg_filename;
886868bd00fSLiam Girdwood 	int ret;
887868bd00fSLiam Girdwood 
888868bd00fSLiam Girdwood 	/* load the default topology */
889868bd00fSLiam Girdwood 	sdev->component = component;
890868bd00fSLiam Girdwood 
891868bd00fSLiam Girdwood 	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
892868bd00fSLiam Girdwood 				       "%s/%s",
893868bd00fSLiam Girdwood 				       plat_data->tplg_filename_prefix,
894868bd00fSLiam Girdwood 				       plat_data->tplg_filename);
895868bd00fSLiam Girdwood 	if (!tplg_filename)
896868bd00fSLiam Girdwood 		return -ENOMEM;
897868bd00fSLiam Girdwood 
898ee1e79b7SRanjani Sridharan 	ret = snd_sof_load_topology(component, tplg_filename);
899868bd00fSLiam Girdwood 	if (ret < 0) {
900ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: failed to load DSP topology %d\n",
901868bd00fSLiam Girdwood 			ret);
902868bd00fSLiam Girdwood 		return ret;
903868bd00fSLiam Girdwood 	}
904868bd00fSLiam Girdwood 
905868bd00fSLiam Girdwood 	return ret;
906868bd00fSLiam Girdwood }
907868bd00fSLiam Girdwood 
908868bd00fSLiam Girdwood static void sof_pcm_remove(struct snd_soc_component *component)
909868bd00fSLiam Girdwood {
910868bd00fSLiam Girdwood 	/* remove topology */
911a5b8f71cSAmadeusz Sławiński 	snd_soc_tplg_component_remove(component);
912868bd00fSLiam Girdwood }
913868bd00fSLiam Girdwood 
9144a39ea3fSRanjani Sridharan static int sof_pcm_ack(struct snd_soc_component *component,
9154a39ea3fSRanjani Sridharan 		       struct snd_pcm_substream *substream)
9164a39ea3fSRanjani Sridharan {
9174a39ea3fSRanjani Sridharan 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
9184a39ea3fSRanjani Sridharan 
9194a39ea3fSRanjani Sridharan 	return snd_sof_pcm_platform_ack(sdev, substream);
9204a39ea3fSRanjani Sridharan }
9214a39ea3fSRanjani Sridharan 
922868bd00fSLiam Girdwood void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
923868bd00fSLiam Girdwood {
924868bd00fSLiam Girdwood 	struct snd_soc_component_driver *pd = &sdev->plat_drv;
925868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
926868bd00fSLiam Girdwood 	const char *drv_name;
927868bd00fSLiam Girdwood 
928868bd00fSLiam Girdwood 	drv_name = plat_data->machine->drv_name;
929868bd00fSLiam Girdwood 
930868bd00fSLiam Girdwood 	pd->name = "sof-audio-component";
931868bd00fSLiam Girdwood 	pd->probe = sof_pcm_probe;
932868bd00fSLiam Girdwood 	pd->remove = sof_pcm_remove;
9331c91d77eSKuninori Morimoto 	pd->open = sof_pcm_open;
9341c91d77eSKuninori Morimoto 	pd->close = sof_pcm_close;
9351c91d77eSKuninori Morimoto 	pd->hw_params = sof_pcm_hw_params;
9361c91d77eSKuninori Morimoto 	pd->prepare = sof_pcm_prepare;
9371c91d77eSKuninori Morimoto 	pd->hw_free = sof_pcm_hw_free;
9381c91d77eSKuninori Morimoto 	pd->trigger = sof_pcm_trigger;
9391c91d77eSKuninori Morimoto 	pd->pointer = sof_pcm_pointer;
9404a39ea3fSRanjani Sridharan 	pd->ack = sof_pcm_ack;
9411c91d77eSKuninori Morimoto 
94276cdd90bSDaniel Baluta #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
94376cdd90bSDaniel Baluta 	pd->compress_ops = &sof_compressed_ops;
94476cdd90bSDaniel Baluta #endif
94576cdd90bSDaniel Baluta 
9461c91d77eSKuninori Morimoto 	pd->pcm_construct = sof_pcm_new;
947868bd00fSLiam Girdwood 	pd->ignore_machine = drv_name;
948868bd00fSLiam Girdwood 	pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
949868bd00fSLiam Girdwood 	pd->be_pcm_base = SOF_BE_PCM_BASE;
950868bd00fSLiam Girdwood 	pd->use_dai_pcm_id = true;
951868bd00fSLiam Girdwood 	pd->topology_name_prefix = "sof";
952868bd00fSLiam Girdwood 
953868bd00fSLiam Girdwood 	 /* increment module refcount when a pcm is opened */
954868bd00fSLiam Girdwood 	pd->module_get_upon_open = 1;
955868bd00fSLiam Girdwood }
956