xref: /openbmc/linux/sound/soc/sof/pcm.c (revision 4123c24bd13caa8ff633d9e17fa2089d53b1f766)
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 
85442c7128SRanjani Sridharan int sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev, struct snd_soc_pcm_runtime *rtd,
865fcdbb2dSRanjani Sridharan 				    struct snd_sof_pcm *spcm, int dir)
875fcdbb2dSRanjani Sridharan {
885fcdbb2dSRanjani Sridharan 	struct snd_soc_dai *dai;
895fcdbb2dSRanjani Sridharan 	int ret, j;
905fcdbb2dSRanjani Sridharan 
915fcdbb2dSRanjani Sridharan 	/* query DAPM for list of connected widgets and set them up */
925fcdbb2dSRanjani Sridharan 	for_each_rtd_cpu_dais(rtd, j, dai) {
935fcdbb2dSRanjani Sridharan 		struct snd_soc_dapm_widget_list *list;
945fcdbb2dSRanjani Sridharan 
955fcdbb2dSRanjani Sridharan 		ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list,
965fcdbb2dSRanjani Sridharan 							     dpcm_end_walk_at_be);
975fcdbb2dSRanjani Sridharan 		if (ret < 0) {
985fcdbb2dSRanjani Sridharan 			dev_err(sdev->dev, "error: dai %s has no valid %s path\n", dai->name,
995fcdbb2dSRanjani Sridharan 				dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
1005fcdbb2dSRanjani Sridharan 			return ret;
1015fcdbb2dSRanjani Sridharan 		}
1025fcdbb2dSRanjani Sridharan 
1035fcdbb2dSRanjani Sridharan 		spcm->stream[dir].list = list;
1045fcdbb2dSRanjani Sridharan 
1055fcdbb2dSRanjani Sridharan 		ret = sof_widget_list_setup(sdev, spcm, dir);
1065fcdbb2dSRanjani Sridharan 		if (ret < 0) {
1075fcdbb2dSRanjani Sridharan 			dev_err(sdev->dev, "error: failed widget list set up for pcm %d dir %d\n",
1085fcdbb2dSRanjani Sridharan 				spcm->pcm.pcm_id, dir);
1095fcdbb2dSRanjani Sridharan 			spcm->stream[dir].list = NULL;
1105fcdbb2dSRanjani Sridharan 			snd_soc_dapm_dai_free_widgets(&list);
1115fcdbb2dSRanjani Sridharan 			return ret;
1125fcdbb2dSRanjani Sridharan 		}
1135fcdbb2dSRanjani Sridharan 	}
1145fcdbb2dSRanjani Sridharan 
1155fcdbb2dSRanjani Sridharan 	return 0;
1165fcdbb2dSRanjani Sridharan }
1175fcdbb2dSRanjani Sridharan 
1181c91d77eSKuninori Morimoto static int sof_pcm_hw_params(struct snd_soc_component *component,
1191c91d77eSKuninori Morimoto 			     struct snd_pcm_substream *substream,
120868bd00fSLiam Girdwood 			     struct snd_pcm_hw_params *params)
121868bd00fSLiam Girdwood {
1221205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
123868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
124868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
125*4123c24bSRanjani Sridharan 	const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
12631f60a0cSPeter Ujfalusi 	struct snd_sof_platform_stream_params platform_params = { 0 };
12731f60a0cSPeter Ujfalusi 	struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
128868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
129868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params pcm;
130868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params_reply ipc_params_reply;
131868bd00fSLiam Girdwood 	int ret;
132868bd00fSLiam Girdwood 
133868bd00fSLiam Girdwood 	/* nothing to do for BE */
134868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
135868bd00fSLiam Girdwood 		return 0;
136868bd00fSLiam Girdwood 
137ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
138868bd00fSLiam Girdwood 	if (!spcm)
139868bd00fSLiam Girdwood 		return -EINVAL;
140868bd00fSLiam Girdwood 
141cfe8191bSKai Vehmanen 	/*
142cfe8191bSKai Vehmanen 	 * Handle repeated calls to hw_params() without free_pcm() in
143cfe8191bSKai Vehmanen 	 * between. At least ALSA OSS emulation depends on this.
144cfe8191bSKai Vehmanen 	 */
145*4123c24bSRanjani Sridharan 	if (pcm_ops->hw_free && spcm->prepared[substream->stream]) {
146*4123c24bSRanjani Sridharan 		ret = pcm_ops->hw_free(component, substream);
147cfe8191bSKai Vehmanen 		if (ret < 0)
148cfe8191bSKai Vehmanen 			return ret;
149cfe8191bSKai Vehmanen 
150*4123c24bSRanjani Sridharan 		spcm->prepared[substream->stream] = false;
151*4123c24bSRanjani Sridharan 	}
152*4123c24bSRanjani Sridharan 
153ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
154868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream);
155868bd00fSLiam Girdwood 
156868bd00fSLiam Girdwood 	memset(&pcm, 0, sizeof(pcm));
157868bd00fSLiam Girdwood 
15857e960f0STakashi Iwai 	/* create compressed page table for audio firmware */
15957e960f0STakashi Iwai 	if (runtime->buffer_changed) {
1601c91d77eSKuninori Morimoto 		ret = create_page_table(component, substream, runtime->dma_area,
161868bd00fSLiam Girdwood 					runtime->dma_bytes);
162868bd00fSLiam Girdwood 		if (ret < 0)
163868bd00fSLiam Girdwood 			return ret;
164868bd00fSLiam Girdwood 	}
165868bd00fSLiam Girdwood 
166868bd00fSLiam Girdwood 	/* number of pages should be rounded up */
167868bd00fSLiam Girdwood 	pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
168868bd00fSLiam Girdwood 
169868bd00fSLiam Girdwood 	/* set IPC PCM parameters */
170868bd00fSLiam Girdwood 	pcm.hdr.size = sizeof(pcm);
171868bd00fSLiam Girdwood 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
172868bd00fSLiam Girdwood 	pcm.comp_id = spcm->stream[substream->stream].comp_id;
173868bd00fSLiam Girdwood 	pcm.params.hdr.size = sizeof(pcm.params);
174868bd00fSLiam Girdwood 	pcm.params.buffer.phy_addr =
175868bd00fSLiam Girdwood 		spcm->stream[substream->stream].page_table.addr;
176868bd00fSLiam Girdwood 	pcm.params.buffer.size = runtime->dma_bytes;
177868bd00fSLiam Girdwood 	pcm.params.direction = substream->stream;
178868bd00fSLiam Girdwood 	pcm.params.sample_valid_bytes = params_width(params) >> 3;
179868bd00fSLiam Girdwood 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
180868bd00fSLiam Girdwood 	pcm.params.rate = params_rate(params);
181868bd00fSLiam Girdwood 	pcm.params.channels = params_channels(params);
182868bd00fSLiam Girdwood 	pcm.params.host_period_bytes = params_period_bytes(params);
183868bd00fSLiam Girdwood 
184868bd00fSLiam Girdwood 	/* container size */
185868bd00fSLiam Girdwood 	ret = snd_pcm_format_physical_width(params_format(params));
186868bd00fSLiam Girdwood 	if (ret < 0)
187868bd00fSLiam Girdwood 		return ret;
188868bd00fSLiam Girdwood 	pcm.params.sample_container_bytes = ret >> 3;
189868bd00fSLiam Girdwood 
190868bd00fSLiam Girdwood 	/* format */
191868bd00fSLiam Girdwood 	switch (params_format(params)) {
192868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S16:
193868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
194868bd00fSLiam Girdwood 		break;
195868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S24:
196868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
197868bd00fSLiam Girdwood 		break;
198868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S32:
199868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
200868bd00fSLiam Girdwood 		break;
201868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_FLOAT:
202868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
203868bd00fSLiam Girdwood 		break;
204868bd00fSLiam Girdwood 	default:
205868bd00fSLiam Girdwood 		return -EINVAL;
206868bd00fSLiam Girdwood 	}
207868bd00fSLiam Girdwood 
208868bd00fSLiam Girdwood 	/* firmware already configured host stream */
209868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_hw_params(sdev,
210868bd00fSLiam Girdwood 					     substream,
211868bd00fSLiam Girdwood 					     params,
21231f60a0cSPeter Ujfalusi 					     &platform_params);
213868bd00fSLiam Girdwood 	if (ret < 0) {
214ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw params failed\n");
215868bd00fSLiam Girdwood 		return ret;
216868bd00fSLiam Girdwood 	}
217868bd00fSLiam Girdwood 
21831f60a0cSPeter Ujfalusi 	/* Update the IPC message with information from the platform */
21931f60a0cSPeter Ujfalusi 	pcm.params.stream_tag = platform_params.stream_tag;
22031f60a0cSPeter Ujfalusi 
22131f60a0cSPeter Ujfalusi 	if (platform_params.use_phy_address)
22231f60a0cSPeter Ujfalusi 		pcm.params.buffer.phy_addr = platform_params.phy_addr;
22331f60a0cSPeter Ujfalusi 
22431f60a0cSPeter Ujfalusi 	if (platform_params.no_ipc_position) {
22531f60a0cSPeter Ujfalusi 		/* For older ABIs set host_period_bytes to zero to inform
22631f60a0cSPeter Ujfalusi 		 * FW we don't want position updates. Newer versions use
22731f60a0cSPeter Ujfalusi 		 * no_stream_position for this purpose.
22831f60a0cSPeter Ujfalusi 		 */
22931f60a0cSPeter Ujfalusi 		if (v->abi_version < SOF_ABI_VER(3, 10, 0))
23031f60a0cSPeter Ujfalusi 			pcm.params.host_period_bytes = 0;
23131f60a0cSPeter Ujfalusi 		else
23231f60a0cSPeter Ujfalusi 			pcm.params.no_stream_position = 1;
23331f60a0cSPeter Ujfalusi 	}
23431f60a0cSPeter Ujfalusi 
235ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag);
236868bd00fSLiam Girdwood 
2375fcdbb2dSRanjani Sridharan 	/* if this is a repeated hw_params without hw_free, skip setting up widgets */
2385fcdbb2dSRanjani Sridharan 	if (!spcm->stream[substream->stream].list) {
2395fcdbb2dSRanjani Sridharan 		ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, substream->stream);
2405fcdbb2dSRanjani Sridharan 		if (ret < 0)
2415fcdbb2dSRanjani Sridharan 			return ret;
2425fcdbb2dSRanjani Sridharan 	}
2435fcdbb2dSRanjani Sridharan 
2445fcdbb2dSRanjani Sridharan 	/* send hw_params IPC to the DSP */
245868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
246868bd00fSLiam Girdwood 				 &ipc_params_reply, sizeof(ipc_params_reply));
247868bd00fSLiam Girdwood 	if (ret < 0) {
248ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: hw params ipc failed for stream %d\n",
249868bd00fSLiam Girdwood 			pcm.params.stream_tag);
250868bd00fSLiam Girdwood 		return ret;
251868bd00fSLiam Girdwood 	}
252868bd00fSLiam Girdwood 
253757ce810SPeter Ujfalusi 	ret = snd_sof_set_stream_data_offset(sdev, substream,
254757ce810SPeter Ujfalusi 					     ipc_params_reply.posn_offset);
255757ce810SPeter Ujfalusi 	if (ret < 0) {
256757ce810SPeter Ujfalusi 		dev_err(component->dev, "%s: invalid stream data offset for PCM %d\n",
257757ce810SPeter Ujfalusi 			__func__, spcm->pcm.pcm_id);
258757ce810SPeter Ujfalusi 		return ret;
259757ce810SPeter Ujfalusi 	}
260757ce810SPeter Ujfalusi 
26104c80277SKai Vehmanen 	spcm->prepared[substream->stream] = true;
26204c80277SKai Vehmanen 
263868bd00fSLiam Girdwood 	/* save pcm hw_params */
264868bd00fSLiam Girdwood 	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
265868bd00fSLiam Girdwood 
266868bd00fSLiam Girdwood 	return ret;
267868bd00fSLiam Girdwood }
268868bd00fSLiam Girdwood 
2691c91d77eSKuninori Morimoto static int sof_pcm_hw_free(struct snd_soc_component *component,
2701c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
271868bd00fSLiam Girdwood {
2721205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
273868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
274*4123c24bSRanjani Sridharan 	const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
275868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
276e66e52c5SKai Vehmanen 	int ret, err = 0;
277868bd00fSLiam Girdwood 
278868bd00fSLiam Girdwood 	/* nothing to do for BE */
279868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
280868bd00fSLiam Girdwood 		return 0;
281868bd00fSLiam Girdwood 
282ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
283868bd00fSLiam Girdwood 	if (!spcm)
284868bd00fSLiam Girdwood 		return -EINVAL;
285868bd00fSLiam Girdwood 
286ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
287ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
288868bd00fSLiam Girdwood 
2890b639dcdSRanjani Sridharan 	/* free PCM in the DSP */
290*4123c24bSRanjani Sridharan 	if (pcm_ops->hw_free && spcm->prepared[substream->stream]) {
291*4123c24bSRanjani Sridharan 		ret = pcm_ops->hw_free(component, substream);
292e66e52c5SKai Vehmanen 		if (ret < 0)
293e66e52c5SKai Vehmanen 			err = ret;
294868bd00fSLiam Girdwood 
295*4123c24bSRanjani Sridharan 		spcm->prepared[substream->stream] = false;
296*4123c24bSRanjani Sridharan 	}
2975fcdbb2dSRanjani Sridharan 
2980b639dcdSRanjani Sridharan 	/* stop DMA */
29993146bc2SRanjani Sridharan 	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
300e66e52c5SKai Vehmanen 	if (ret < 0) {
301ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw free failed\n");
302e66e52c5SKai Vehmanen 		err = ret;
303e66e52c5SKai Vehmanen 	}
30493146bc2SRanjani Sridharan 
3050b639dcdSRanjani Sridharan 	/* free the DAPM widget list */
3060b639dcdSRanjani Sridharan 	ret = sof_widget_list_free(sdev, spcm, substream->stream);
3070b639dcdSRanjani Sridharan 	if (ret < 0)
3080b639dcdSRanjani Sridharan 		err = ret;
3090b639dcdSRanjani Sridharan 
3100b639dcdSRanjani Sridharan 	cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
3110b639dcdSRanjani Sridharan 
312e66e52c5SKai Vehmanen 	return err;
313868bd00fSLiam Girdwood }
314868bd00fSLiam Girdwood 
3151c91d77eSKuninori Morimoto static int sof_pcm_prepare(struct snd_soc_component *component,
3161c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
317868bd00fSLiam Girdwood {
3181205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
319868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
320868bd00fSLiam Girdwood 	int ret;
321868bd00fSLiam Girdwood 
322868bd00fSLiam Girdwood 	/* nothing to do for BE */
323868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
324868bd00fSLiam Girdwood 		return 0;
325868bd00fSLiam Girdwood 
326ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
327868bd00fSLiam Girdwood 	if (!spcm)
328868bd00fSLiam Girdwood 		return -EINVAL;
329868bd00fSLiam Girdwood 
33004c80277SKai Vehmanen 	if (spcm->prepared[substream->stream])
331868bd00fSLiam Girdwood 		return 0;
332868bd00fSLiam Girdwood 
333ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
334ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
335868bd00fSLiam Girdwood 
336868bd00fSLiam Girdwood 	/* set hw_params */
3371c91d77eSKuninori Morimoto 	ret = sof_pcm_hw_params(component,
3381c91d77eSKuninori Morimoto 				substream, &spcm->params[substream->stream]);
339868bd00fSLiam Girdwood 	if (ret < 0) {
340ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
341ee1e79b7SRanjani Sridharan 			"error: set pcm hw_params after resume\n");
342868bd00fSLiam Girdwood 		return ret;
343868bd00fSLiam Girdwood 	}
344868bd00fSLiam Girdwood 
345868bd00fSLiam Girdwood 	return 0;
346868bd00fSLiam Girdwood }
347868bd00fSLiam Girdwood 
348868bd00fSLiam Girdwood /*
349868bd00fSLiam Girdwood  * FE dai link trigger actions are always executed in non-atomic context because
350868bd00fSLiam Girdwood  * they involve IPC's.
351868bd00fSLiam Girdwood  */
3521c91d77eSKuninori Morimoto static int sof_pcm_trigger(struct snd_soc_component *component,
3531c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream, int cmd)
354868bd00fSLiam Girdwood {
3551205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
356868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
357868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
358868bd00fSLiam Girdwood 	struct sof_ipc_stream stream;
359868bd00fSLiam Girdwood 	struct sof_ipc_reply reply;
36004c80277SKai Vehmanen 	bool reset_hw_params = false;
3615fcdbb2dSRanjani Sridharan 	bool free_widget_list = false;
3620a1b0834SPan Xiuli 	bool ipc_first = false;
363868bd00fSLiam Girdwood 	int ret;
364868bd00fSLiam Girdwood 
365868bd00fSLiam Girdwood 	/* nothing to do for BE */
366868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
367868bd00fSLiam Girdwood 		return 0;
368868bd00fSLiam Girdwood 
369ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
370868bd00fSLiam Girdwood 	if (!spcm)
371868bd00fSLiam Girdwood 		return -EINVAL;
372868bd00fSLiam Girdwood 
373ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
374868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream, cmd);
375868bd00fSLiam Girdwood 
376868bd00fSLiam Girdwood 	stream.hdr.size = sizeof(stream);
377868bd00fSLiam Girdwood 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
378868bd00fSLiam Girdwood 	stream.comp_id = spcm->stream[substream->stream].comp_id;
379868bd00fSLiam Girdwood 
380868bd00fSLiam Girdwood 	switch (cmd) {
381868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
382868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
3830a1b0834SPan Xiuli 		ipc_first = true;
384868bd00fSLiam Girdwood 		break;
385868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
386868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
387868bd00fSLiam Girdwood 		break;
388868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
389ac8c046fSKeyon Jie 		if (spcm->stream[substream->stream].suspend_ignored) {
390ac8c046fSKeyon Jie 			/*
391ac8c046fSKeyon Jie 			 * This case will be triggered when INFO_RESUME is
392ac8c046fSKeyon Jie 			 * not supported, no need to re-start streams that
393ac8c046fSKeyon Jie 			 * remained enabled in D0ix.
394ac8c046fSKeyon Jie 			 */
395ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = false;
396ac8c046fSKeyon Jie 			return 0;
397ac8c046fSKeyon Jie 		}
398868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
399868bd00fSLiam Girdwood 		break;
400868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
401043ae13bSRanjani Sridharan 		if (sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
402ac8c046fSKeyon Jie 		    spcm->stream[substream->stream].d0i3_compatible) {
403ac8c046fSKeyon Jie 			/*
404ac8c046fSKeyon Jie 			 * trap the event, not sending trigger stop to
405ac8c046fSKeyon Jie 			 * prevent the FW pipelines from being stopped,
406ac8c046fSKeyon Jie 			 * and mark the flag to ignore the upcoming DAPM
407ac8c046fSKeyon Jie 			 * PM events.
408ac8c046fSKeyon Jie 			 */
409ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = true;
410ac8c046fSKeyon Jie 			return 0;
411ac8c046fSKeyon Jie 		}
4125fcdbb2dSRanjani Sridharan 		free_widget_list = true;
413df561f66SGustavo A. R. Silva 		fallthrough;
414868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
415868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
4160a1b0834SPan Xiuli 		ipc_first = true;
41704c80277SKai Vehmanen 		reset_hw_params = true;
418868bd00fSLiam Girdwood 		break;
419868bd00fSLiam Girdwood 	default:
420ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: unhandled trigger cmd %d\n",
421ee1e79b7SRanjani Sridharan 			cmd);
422868bd00fSLiam Girdwood 		return -EINVAL;
423868bd00fSLiam Girdwood 	}
424868bd00fSLiam Girdwood 
4250a1b0834SPan Xiuli 	/*
4260a1b0834SPan Xiuli 	 * DMA and IPC sequence is different for start and stop. Need to send
4270a1b0834SPan Xiuli 	 * STOP IPC before stop DMA
4280a1b0834SPan Xiuli 	 */
4290a1b0834SPan Xiuli 	if (!ipc_first)
430868bd00fSLiam Girdwood 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
431868bd00fSLiam Girdwood 
432868bd00fSLiam Girdwood 	/* send IPC to the DSP */
433868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
434868bd00fSLiam Girdwood 				 sizeof(stream), &reply, sizeof(reply));
435868bd00fSLiam Girdwood 
4360a1b0834SPan Xiuli 	/* need to STOP DMA even if STOP IPC failed */
4370a1b0834SPan Xiuli 	if (ipc_first)
4380a1b0834SPan Xiuli 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
4390a1b0834SPan Xiuli 
4400a1b0834SPan Xiuli 	/* free PCM if reset_hw_params is set and the STOP IPC is successful */
4415fcdbb2dSRanjani Sridharan 	if (!ret && reset_hw_params) {
442d9a72465SRanjani Sridharan 		ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream,
443d9a72465SRanjani Sridharan 					  free_widget_list);
4445fcdbb2dSRanjani Sridharan 		if (ret < 0)
4455fcdbb2dSRanjani Sridharan 			return ret;
4465fcdbb2dSRanjani Sridharan 	}
447a49b6871SKai Vehmanen 
448868bd00fSLiam Girdwood 	return ret;
449868bd00fSLiam Girdwood }
450868bd00fSLiam Girdwood 
4511c91d77eSKuninori Morimoto static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
4521c91d77eSKuninori Morimoto 					 struct snd_pcm_substream *substream)
453868bd00fSLiam Girdwood {
4541205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
455868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
456868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
457868bd00fSLiam Girdwood 	snd_pcm_uframes_t host, dai;
458868bd00fSLiam Girdwood 
459868bd00fSLiam Girdwood 	/* nothing to do for BE */
460868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
461868bd00fSLiam Girdwood 		return 0;
462868bd00fSLiam Girdwood 
463868bd00fSLiam Girdwood 	/* use dsp ops pointer callback directly if set */
464868bd00fSLiam Girdwood 	if (sof_ops(sdev)->pcm_pointer)
465868bd00fSLiam Girdwood 		return sof_ops(sdev)->pcm_pointer(sdev, substream);
466868bd00fSLiam Girdwood 
467ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
468868bd00fSLiam Girdwood 	if (!spcm)
469868bd00fSLiam Girdwood 		return -EINVAL;
470868bd00fSLiam Girdwood 
471868bd00fSLiam Girdwood 	/* read position from DSP */
472868bd00fSLiam Girdwood 	host = bytes_to_frames(substream->runtime,
473868bd00fSLiam Girdwood 			       spcm->stream[substream->stream].posn.host_posn);
474868bd00fSLiam Girdwood 	dai = bytes_to_frames(substream->runtime,
475868bd00fSLiam Girdwood 			      spcm->stream[substream->stream].posn.dai_posn);
476868bd00fSLiam Girdwood 
477277ff236SPierre-Louis Bossart 	dev_vdbg(component->dev,
478ee1e79b7SRanjani Sridharan 		 "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
479868bd00fSLiam Girdwood 		 spcm->pcm.pcm_id, substream->stream, host, dai);
480868bd00fSLiam Girdwood 
481868bd00fSLiam Girdwood 	return host;
482868bd00fSLiam Girdwood }
483868bd00fSLiam Girdwood 
4841c91d77eSKuninori Morimoto static int sof_pcm_open(struct snd_soc_component *component,
4851c91d77eSKuninori Morimoto 			struct snd_pcm_substream *substream)
486868bd00fSLiam Girdwood {
4871205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
488868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
489868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
49027e322faSPierre-Louis Bossart 	const struct snd_sof_dsp_ops *ops = sof_ops(sdev);
491868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
492868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
493868bd00fSLiam Girdwood 	int ret;
494868bd00fSLiam Girdwood 
495868bd00fSLiam Girdwood 	/* nothing to do for BE */
496868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
497868bd00fSLiam Girdwood 		return 0;
498868bd00fSLiam Girdwood 
499ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
500868bd00fSLiam Girdwood 	if (!spcm)
501868bd00fSLiam Girdwood 		return -EINVAL;
502868bd00fSLiam Girdwood 
503ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
504ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
505868bd00fSLiam Girdwood 
506868bd00fSLiam Girdwood 
507868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[substream->stream];
508868bd00fSLiam Girdwood 
509868bd00fSLiam Girdwood 	/* set runtime config */
51027e322faSPierre-Louis Bossart 	runtime->hw.info = ops->hw_info; /* platform-specific */
51127e322faSPierre-Louis Bossart 
5129983ac49SKai Vehmanen 	/* set any runtime constraints based on topology */
513868bd00fSLiam Girdwood 	runtime->hw.formats = le64_to_cpu(caps->formats);
514868bd00fSLiam Girdwood 	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
515868bd00fSLiam Girdwood 	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
516868bd00fSLiam Girdwood 	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
517868bd00fSLiam Girdwood 	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
518868bd00fSLiam Girdwood 
519868bd00fSLiam Girdwood 	/*
520868bd00fSLiam Girdwood 	 * caps->buffer_size_min is not used since the
521868bd00fSLiam Girdwood 	 * snd_pcm_hardware structure only defines buffer_bytes_max
522868bd00fSLiam Girdwood 	 */
523868bd00fSLiam Girdwood 	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
524868bd00fSLiam Girdwood 
525ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period min %zd max %zd bytes\n",
526868bd00fSLiam Girdwood 		runtime->hw.period_bytes_min,
527868bd00fSLiam Girdwood 		runtime->hw.period_bytes_max);
528ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period count %d max %d\n",
529868bd00fSLiam Girdwood 		runtime->hw.periods_min,
530868bd00fSLiam Girdwood 		runtime->hw.periods_max);
531ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "buffer max %zd bytes\n",
532868bd00fSLiam Girdwood 		runtime->hw.buffer_bytes_max);
533868bd00fSLiam Girdwood 
534868bd00fSLiam Girdwood 	/* set wait time - TODO: come from topology */
535868bd00fSLiam Girdwood 	substream->wait_time = 500;
536868bd00fSLiam Girdwood 
537868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.host_posn = 0;
538868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.dai_posn = 0;
539868bd00fSLiam Girdwood 	spcm->stream[substream->stream].substream = substream;
54004c80277SKai Vehmanen 	spcm->prepared[substream->stream] = false;
541868bd00fSLiam Girdwood 
542868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_open(sdev, substream);
54314a2212dSRanjani Sridharan 	if (ret < 0)
544ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm open failed %d\n", ret);
545868bd00fSLiam Girdwood 
546868bd00fSLiam Girdwood 	return ret;
547868bd00fSLiam Girdwood }
548868bd00fSLiam Girdwood 
5491c91d77eSKuninori Morimoto static int sof_pcm_close(struct snd_soc_component *component,
5501c91d77eSKuninori Morimoto 			 struct snd_pcm_substream *substream)
551868bd00fSLiam Girdwood {
5521205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
553868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
554868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
555868bd00fSLiam Girdwood 	int err;
556868bd00fSLiam Girdwood 
557868bd00fSLiam Girdwood 	/* nothing to do for BE */
558868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
559868bd00fSLiam Girdwood 		return 0;
560868bd00fSLiam Girdwood 
561ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
562868bd00fSLiam Girdwood 	if (!spcm)
563868bd00fSLiam Girdwood 		return -EINVAL;
564868bd00fSLiam Girdwood 
565ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
566ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
567868bd00fSLiam Girdwood 
568868bd00fSLiam Girdwood 	err = snd_sof_pcm_platform_close(sdev, substream);
569868bd00fSLiam Girdwood 	if (err < 0) {
570ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm close failed %d\n",
571868bd00fSLiam Girdwood 			err);
572868bd00fSLiam Girdwood 		/*
573868bd00fSLiam Girdwood 		 * keep going, no point in preventing the close
574868bd00fSLiam Girdwood 		 * from happening
575868bd00fSLiam Girdwood 		 */
576868bd00fSLiam Girdwood 	}
577868bd00fSLiam Girdwood 
578868bd00fSLiam Girdwood 	return 0;
579868bd00fSLiam Girdwood }
580868bd00fSLiam Girdwood 
581868bd00fSLiam Girdwood /*
582868bd00fSLiam Girdwood  * Pre-allocate playback/capture audio buffer pages.
583868bd00fSLiam Girdwood  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
584868bd00fSLiam Girdwood  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
585868bd00fSLiam Girdwood  */
5861c91d77eSKuninori Morimoto static int sof_pcm_new(struct snd_soc_component *component,
5871c91d77eSKuninori Morimoto 		       struct snd_soc_pcm_runtime *rtd)
588868bd00fSLiam Girdwood {
589868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
590868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
591868bd00fSLiam Girdwood 	struct snd_pcm *pcm = rtd->pcm;
592868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
593868bd00fSLiam Girdwood 	int stream = SNDRV_PCM_STREAM_PLAYBACK;
594868bd00fSLiam Girdwood 
595868bd00fSLiam Girdwood 	/* find SOF PCM for this RTD */
596ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
597868bd00fSLiam Girdwood 	if (!spcm) {
598ee1e79b7SRanjani Sridharan 		dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
599868bd00fSLiam Girdwood 			 rtd->dai_link->id);
600868bd00fSLiam Girdwood 		return 0;
601868bd00fSLiam Girdwood 	}
602868bd00fSLiam Girdwood 
603ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
604868bd00fSLiam Girdwood 
605868bd00fSLiam Girdwood 	/* do we need to pre-allocate playback audio buffer pages */
606868bd00fSLiam Girdwood 	if (!spcm->pcm.playback)
607868bd00fSLiam Girdwood 		goto capture;
608868bd00fSLiam Girdwood 
609868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
610868bd00fSLiam Girdwood 
611868bd00fSLiam Girdwood 	/* pre-allocate playback audio buffer pages */
612ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
613ee1e79b7SRanjani Sridharan 		"spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
614868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
615868bd00fSLiam Girdwood 
6164f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
6174f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL playback substream!\n");
6184f7f9564SGuennadi Liakhovetski 		return -EINVAL;
6194f7f9564SGuennadi Liakhovetski 	}
6204f7f9564SGuennadi Liakhovetski 
62157e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
622868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
623e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
624868bd00fSLiam Girdwood capture:
625868bd00fSLiam Girdwood 	stream = SNDRV_PCM_STREAM_CAPTURE;
626868bd00fSLiam Girdwood 
627868bd00fSLiam Girdwood 	/* do we need to pre-allocate capture audio buffer pages */
628868bd00fSLiam Girdwood 	if (!spcm->pcm.capture)
629868bd00fSLiam Girdwood 		return 0;
630868bd00fSLiam Girdwood 
631868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
632868bd00fSLiam Girdwood 
633868bd00fSLiam Girdwood 	/* pre-allocate capture audio buffer pages */
634ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
635ee1e79b7SRanjani Sridharan 		"spcm: allocate %s capture 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 capture 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 
647868bd00fSLiam Girdwood 	return 0;
648868bd00fSLiam Girdwood }
649868bd00fSLiam Girdwood 
650c943a586SJaska Uimonen static void ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
651c943a586SJaska Uimonen 					    struct snd_pcm_hw_params *params)
652c943a586SJaska Uimonen {
653c943a586SJaska Uimonen 	struct sof_ipc_dai_config *config;
654c943a586SJaska Uimonen 	struct snd_sof_dai *dai;
655c943a586SJaska Uimonen 	int i;
656c943a586SJaska Uimonen 
657c943a586SJaska Uimonen 	/*
658c943a586SJaska Uimonen 	 * Search for all matching DAIs as we can have both playback and capture DAI
659c943a586SJaska Uimonen 	 * associated with the same link.
660c943a586SJaska Uimonen 	 */
661c943a586SJaska Uimonen 	list_for_each_entry(dai, &sdev->dai_list, list) {
662c943a586SJaska Uimonen 		if (!dai->name || strcmp(link_name, dai->name))
663c943a586SJaska Uimonen 			continue;
664c943a586SJaska Uimonen 		for (i = 0; i < dai->number_configs; i++) {
665839e484fSRanjani Sridharan 			struct sof_dai_private_data *private = dai->private;
666839e484fSRanjani Sridharan 
667839e484fSRanjani Sridharan 			config = &private->dai_config[i];
668c943a586SJaska Uimonen 			if (config->ssp.fsync_rate == params_rate(params)) {
669c943a586SJaska Uimonen 				dev_dbg(sdev->dev, "DAI config %d matches pcm hw params\n", i);
670c943a586SJaska Uimonen 				dai->current_config = i;
671c943a586SJaska Uimonen 				break;
672c943a586SJaska Uimonen 			}
673c943a586SJaska Uimonen 		}
674c943a586SJaska Uimonen 	}
675c943a586SJaska Uimonen }
676c943a586SJaska Uimonen 
677868bd00fSLiam Girdwood /* fixup the BE DAI link to match any values from topology */
678f805e7e0SRanjani Sridharan int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
679868bd00fSLiam Girdwood {
680868bd00fSLiam Girdwood 	struct snd_interval *rate = hw_param_interval(params,
681868bd00fSLiam Girdwood 			SNDRV_PCM_HW_PARAM_RATE);
682868bd00fSLiam Girdwood 	struct snd_interval *channels = hw_param_interval(params,
683868bd00fSLiam Girdwood 						SNDRV_PCM_HW_PARAM_CHANNELS);
684868bd00fSLiam Girdwood 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
685868bd00fSLiam Girdwood 	struct snd_soc_component *component =
686ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
687868bd00fSLiam Girdwood 	struct snd_sof_dai *dai =
688ee1e79b7SRanjani Sridharan 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
689c943a586SJaska Uimonen 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
690839e484fSRanjani Sridharan 	struct sof_dai_private_data *private = dai->private;
691fd045558Sranderwang 	struct snd_soc_dpcm *dpcm;
692868bd00fSLiam Girdwood 
693868bd00fSLiam Girdwood 	/* no topology exists for this BE, try a common configuration */
694868bd00fSLiam Girdwood 	if (!dai) {
695ee1e79b7SRanjani Sridharan 		dev_warn(component->dev,
696ee1e79b7SRanjani Sridharan 			 "warning: no topology found for BE DAI %s config\n",
697868bd00fSLiam Girdwood 			 rtd->dai_link->name);
698868bd00fSLiam Girdwood 
699868bd00fSLiam Girdwood 		/*  set 48k, stereo, 16bits by default */
700868bd00fSLiam Girdwood 		rate->min = 48000;
701868bd00fSLiam Girdwood 		rate->max = 48000;
702868bd00fSLiam Girdwood 
703868bd00fSLiam Girdwood 		channels->min = 2;
704868bd00fSLiam Girdwood 		channels->max = 2;
705868bd00fSLiam Girdwood 
706868bd00fSLiam Girdwood 		snd_mask_none(fmt);
707868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
708868bd00fSLiam Girdwood 
709868bd00fSLiam Girdwood 		return 0;
710868bd00fSLiam Girdwood 	}
711868bd00fSLiam Girdwood 
712868bd00fSLiam Girdwood 	/* read format from topology */
713868bd00fSLiam Girdwood 	snd_mask_none(fmt);
714868bd00fSLiam Girdwood 
715839e484fSRanjani Sridharan 	switch (private->comp_dai->config.frame_fmt) {
716868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S16_LE:
717868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
718868bd00fSLiam Girdwood 		break;
719868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S24_4LE:
720868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
721868bd00fSLiam Girdwood 		break;
722868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S32_LE:
723868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
724868bd00fSLiam Girdwood 		break;
725868bd00fSLiam Girdwood 	default:
726ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: No available DAI format!\n");
727868bd00fSLiam Girdwood 		return -EINVAL;
728868bd00fSLiam Girdwood 	}
729868bd00fSLiam Girdwood 
730868bd00fSLiam Girdwood 	/* read rate and channels from topology */
731839e484fSRanjani Sridharan 	switch (private->dai_config->type) {
732868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_SSP:
733c943a586SJaska Uimonen 		/* search for config to pcm params match, if not found use default */
734c943a586SJaska Uimonen 		ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);
735c943a586SJaska Uimonen 
736839e484fSRanjani Sridharan 		rate->min = private->dai_config[dai->current_config].ssp.fsync_rate;
737839e484fSRanjani Sridharan 		rate->max = private->dai_config[dai->current_config].ssp.fsync_rate;
738839e484fSRanjani Sridharan 		channels->min = private->dai_config[dai->current_config].ssp.tdm_slots;
739839e484fSRanjani Sridharan 		channels->max = private->dai_config[dai->current_config].ssp.tdm_slots;
740868bd00fSLiam Girdwood 
741ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
742868bd00fSLiam Girdwood 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
743ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
744868bd00fSLiam Girdwood 			"channels_min: %d channels_max: %d\n",
745868bd00fSLiam Girdwood 			channels->min, channels->max);
746868bd00fSLiam Girdwood 
747868bd00fSLiam Girdwood 		break;
748868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_DMIC:
749868bd00fSLiam Girdwood 		/* DMIC only supports 16 or 32 bit formats */
750839e484fSRanjani Sridharan 		if (private->comp_dai->config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
751ee1e79b7SRanjani Sridharan 			dev_err(component->dev,
752868bd00fSLiam Girdwood 				"error: invalid fmt %d for DAI type %d\n",
753839e484fSRanjani Sridharan 				private->comp_dai->config.frame_fmt,
754839e484fSRanjani Sridharan 				private->dai_config->type);
755868bd00fSLiam Girdwood 		}
756868bd00fSLiam Girdwood 		break;
757868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_HDA:
758fd045558Sranderwang 		/*
759135ab457SPierre-Louis Bossart 		 * HDAudio does not follow the default trigger
760fd045558Sranderwang 		 * sequence due to firmware implementation
761fd045558Sranderwang 		 */
762fd045558Sranderwang 		for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
763fd045558Sranderwang 			struct snd_soc_pcm_runtime *fe = dpcm->fe;
764fd045558Sranderwang 
765fd045558Sranderwang 			fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
766fd045558Sranderwang 				SND_SOC_DPCM_TRIGGER_POST;
767fd045558Sranderwang 		}
768868bd00fSLiam Girdwood 		break;
7696e3360cdSPierre-Louis Bossart 	case SOF_DAI_INTEL_ALH:
770e1711b1fSRander Wang 		/*
771e1711b1fSRander Wang 		 * Dai could run with different channel count compared with
772e1711b1fSRander Wang 		 * front end, so get dai channel count from topology
773e1711b1fSRander Wang 		 */
774839e484fSRanjani Sridharan 		channels->min = private->dai_config->alh.channels;
775839e484fSRanjani Sridharan 		channels->max = private->dai_config->alh.channels;
7766e3360cdSPierre-Louis Bossart 		break;
777a4eff5f8SDaniel Baluta 	case SOF_DAI_IMX_ESAI:
778839e484fSRanjani Sridharan 		rate->min = private->dai_config->esai.fsync_rate;
779839e484fSRanjani Sridharan 		rate->max = private->dai_config->esai.fsync_rate;
780839e484fSRanjani Sridharan 		channels->min = private->dai_config->esai.tdm_slots;
781839e484fSRanjani Sridharan 		channels->max = private->dai_config->esai.tdm_slots;
782a4eff5f8SDaniel Baluta 
783ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
78451b0243aSDaniel Baluta 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
78551b0243aSDaniel Baluta 		dev_dbg(component->dev,
786a4eff5f8SDaniel Baluta 			"channels_min: %d channels_max: %d\n",
787a4eff5f8SDaniel Baluta 			channels->min, channels->max);
788a4eff5f8SDaniel Baluta 		break;
789b72bfcffSYC Hung 	case SOF_DAI_MEDIATEK_AFE:
790839e484fSRanjani Sridharan 		rate->min = private->dai_config->afe.rate;
791839e484fSRanjani Sridharan 		rate->max = private->dai_config->afe.rate;
792839e484fSRanjani Sridharan 		channels->min = private->dai_config->afe.channels;
793839e484fSRanjani Sridharan 		channels->max = private->dai_config->afe.channels;
794b72bfcffSYC Hung 
795b72bfcffSYC Hung 		dev_dbg(component->dev,
796b72bfcffSYC Hung 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
797b72bfcffSYC Hung 		dev_dbg(component->dev,
798b72bfcffSYC Hung 			"channels_min: %d channels_max: %d\n",
799b72bfcffSYC Hung 			channels->min, channels->max);
800b72bfcffSYC Hung 		break;
801d88cbd6fSGuido Roncarolo 	case SOF_DAI_IMX_SAI:
802839e484fSRanjani Sridharan 		rate->min = private->dai_config->sai.fsync_rate;
803839e484fSRanjani Sridharan 		rate->max = private->dai_config->sai.fsync_rate;
804839e484fSRanjani Sridharan 		channels->min = private->dai_config->sai.tdm_slots;
805839e484fSRanjani Sridharan 		channels->max = private->dai_config->sai.tdm_slots;
806d88cbd6fSGuido Roncarolo 
807d88cbd6fSGuido Roncarolo 		dev_dbg(component->dev,
80851b0243aSDaniel Baluta 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
80951b0243aSDaniel Baluta 		dev_dbg(component->dev,
810d88cbd6fSGuido Roncarolo 			"channels_min: %d channels_max: %d\n",
811d88cbd6fSGuido Roncarolo 			channels->min, channels->max);
812d88cbd6fSGuido Roncarolo 		break;
813efb931cdSAjit Kumar Pandey 	case SOF_DAI_AMD_BT:
814839e484fSRanjani Sridharan 		rate->min = private->dai_config->acpbt.fsync_rate;
815839e484fSRanjani Sridharan 		rate->max = private->dai_config->acpbt.fsync_rate;
816839e484fSRanjani Sridharan 		channels->min = private->dai_config->acpbt.tdm_slots;
817839e484fSRanjani Sridharan 		channels->max = private->dai_config->acpbt.tdm_slots;
818efb931cdSAjit Kumar Pandey 
819efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
820efb931cdSAjit Kumar Pandey 			"AMD_BT rate_min: %d rate_max: %d\n", rate->min, rate->max);
821efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
822efb931cdSAjit Kumar Pandey 			"AMD_BT channels_min: %d channels_max: %d\n",
823efb931cdSAjit Kumar Pandey 			channels->min, channels->max);
824efb931cdSAjit Kumar Pandey 		break;
825efb931cdSAjit Kumar Pandey 	case SOF_DAI_AMD_SP:
826839e484fSRanjani Sridharan 		rate->min = private->dai_config->acpsp.fsync_rate;
827839e484fSRanjani Sridharan 		rate->max = private->dai_config->acpsp.fsync_rate;
828839e484fSRanjani Sridharan 		channels->min = private->dai_config->acpsp.tdm_slots;
829839e484fSRanjani Sridharan 		channels->max = private->dai_config->acpsp.tdm_slots;
830efb931cdSAjit Kumar Pandey 
831efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
832efb931cdSAjit Kumar Pandey 			"AMD_SP rate_min: %d rate_max: %d\n", rate->min, rate->max);
833efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
834efb931cdSAjit Kumar Pandey 			"AMD_SP channels_min: %d channels_max: %d\n",
835efb931cdSAjit Kumar Pandey 			channels->min, channels->max);
836efb931cdSAjit Kumar Pandey 		break;
837efb931cdSAjit Kumar Pandey 	case SOF_DAI_AMD_DMIC:
838839e484fSRanjani Sridharan 		rate->min = private->dai_config->acpdmic.fsync_rate;
839839e484fSRanjani Sridharan 		rate->max = private->dai_config->acpdmic.fsync_rate;
840839e484fSRanjani Sridharan 		channels->min = private->dai_config->acpdmic.tdm_slots;
841839e484fSRanjani Sridharan 		channels->max = private->dai_config->acpdmic.tdm_slots;
842efb931cdSAjit Kumar Pandey 
843efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
844efb931cdSAjit Kumar Pandey 			"AMD_DMIC rate_min: %d rate_max: %d\n", rate->min, rate->max);
845efb931cdSAjit Kumar Pandey 		dev_dbg(component->dev,
846efb931cdSAjit Kumar Pandey 			"AMD_DMIC channels_min: %d channels_max: %d\n",
847efb931cdSAjit Kumar Pandey 			channels->min, channels->max);
848efb931cdSAjit Kumar Pandey 		break;
849868bd00fSLiam Girdwood 	default:
850ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: invalid DAI type %d\n",
851839e484fSRanjani Sridharan 			private->dai_config->type);
852868bd00fSLiam Girdwood 		break;
853868bd00fSLiam Girdwood 	}
854868bd00fSLiam Girdwood 
855868bd00fSLiam Girdwood 	return 0;
856868bd00fSLiam Girdwood }
857f3f3af17SPierre-Louis Bossart EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
858868bd00fSLiam Girdwood 
859868bd00fSLiam Girdwood static int sof_pcm_probe(struct snd_soc_component *component)
860868bd00fSLiam Girdwood {
861868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
862868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
863868bd00fSLiam Girdwood 	const char *tplg_filename;
864868bd00fSLiam Girdwood 	int ret;
865868bd00fSLiam Girdwood 
866868bd00fSLiam Girdwood 	/* load the default topology */
867868bd00fSLiam Girdwood 	sdev->component = component;
868868bd00fSLiam Girdwood 
869868bd00fSLiam Girdwood 	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
870868bd00fSLiam Girdwood 				       "%s/%s",
871868bd00fSLiam Girdwood 				       plat_data->tplg_filename_prefix,
872868bd00fSLiam Girdwood 				       plat_data->tplg_filename);
873868bd00fSLiam Girdwood 	if (!tplg_filename)
874868bd00fSLiam Girdwood 		return -ENOMEM;
875868bd00fSLiam Girdwood 
876ee1e79b7SRanjani Sridharan 	ret = snd_sof_load_topology(component, tplg_filename);
877868bd00fSLiam Girdwood 	if (ret < 0) {
878ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: failed to load DSP topology %d\n",
879868bd00fSLiam Girdwood 			ret);
880868bd00fSLiam Girdwood 		return ret;
881868bd00fSLiam Girdwood 	}
882868bd00fSLiam Girdwood 
883868bd00fSLiam Girdwood 	return ret;
884868bd00fSLiam Girdwood }
885868bd00fSLiam Girdwood 
886868bd00fSLiam Girdwood static void sof_pcm_remove(struct snd_soc_component *component)
887868bd00fSLiam Girdwood {
888868bd00fSLiam Girdwood 	/* remove topology */
889a5b8f71cSAmadeusz Sławiński 	snd_soc_tplg_component_remove(component);
890868bd00fSLiam Girdwood }
891868bd00fSLiam Girdwood 
8924a39ea3fSRanjani Sridharan static int sof_pcm_ack(struct snd_soc_component *component,
8934a39ea3fSRanjani Sridharan 		       struct snd_pcm_substream *substream)
8944a39ea3fSRanjani Sridharan {
8954a39ea3fSRanjani Sridharan 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
8964a39ea3fSRanjani Sridharan 
8974a39ea3fSRanjani Sridharan 	return snd_sof_pcm_platform_ack(sdev, substream);
8984a39ea3fSRanjani Sridharan }
8994a39ea3fSRanjani Sridharan 
900868bd00fSLiam Girdwood void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
901868bd00fSLiam Girdwood {
902868bd00fSLiam Girdwood 	struct snd_soc_component_driver *pd = &sdev->plat_drv;
903868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
904868bd00fSLiam Girdwood 	const char *drv_name;
905868bd00fSLiam Girdwood 
906868bd00fSLiam Girdwood 	drv_name = plat_data->machine->drv_name;
907868bd00fSLiam Girdwood 
908868bd00fSLiam Girdwood 	pd->name = "sof-audio-component";
909868bd00fSLiam Girdwood 	pd->probe = sof_pcm_probe;
910868bd00fSLiam Girdwood 	pd->remove = sof_pcm_remove;
9111c91d77eSKuninori Morimoto 	pd->open = sof_pcm_open;
9121c91d77eSKuninori Morimoto 	pd->close = sof_pcm_close;
9131c91d77eSKuninori Morimoto 	pd->hw_params = sof_pcm_hw_params;
9141c91d77eSKuninori Morimoto 	pd->prepare = sof_pcm_prepare;
9151c91d77eSKuninori Morimoto 	pd->hw_free = sof_pcm_hw_free;
9161c91d77eSKuninori Morimoto 	pd->trigger = sof_pcm_trigger;
9171c91d77eSKuninori Morimoto 	pd->pointer = sof_pcm_pointer;
9184a39ea3fSRanjani Sridharan 	pd->ack = sof_pcm_ack;
9191c91d77eSKuninori Morimoto 
92076cdd90bSDaniel Baluta #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
92176cdd90bSDaniel Baluta 	pd->compress_ops = &sof_compressed_ops;
92276cdd90bSDaniel Baluta #endif
92376cdd90bSDaniel Baluta 
9241c91d77eSKuninori Morimoto 	pd->pcm_construct = sof_pcm_new;
925868bd00fSLiam Girdwood 	pd->ignore_machine = drv_name;
926868bd00fSLiam Girdwood 	pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
927868bd00fSLiam Girdwood 	pd->be_pcm_base = SOF_BE_PCM_BASE;
928868bd00fSLiam Girdwood 	pd->use_dai_pcm_id = true;
929868bd00fSLiam Girdwood 	pd->topology_name_prefix = "sof";
930868bd00fSLiam Girdwood 
931868bd00fSLiam Girdwood 	 /* increment module refcount when a pcm is opened */
932868bd00fSLiam Girdwood 	pd->module_get_upon_open = 1;
933868bd00fSLiam Girdwood }
934