xref: /openbmc/linux/sound/soc/sof/pcm.c (revision f3f3af1743350fdc7c373772fb3740dc223db8e5)
1e149ca29SPierre-Louis Bossart // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2868bd00fSLiam Girdwood //
3868bd00fSLiam Girdwood // This file is provided under a dual BSD/GPLv2 license.  When using or
4868bd00fSLiam Girdwood // redistributing this file, you may do so under either license.
5868bd00fSLiam Girdwood //
6868bd00fSLiam Girdwood // Copyright(c) 2018 Intel Corporation. All rights reserved.
7868bd00fSLiam Girdwood //
8868bd00fSLiam Girdwood // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9868bd00fSLiam Girdwood //
10868bd00fSLiam Girdwood // PCM Layer, interface between ALSA and IPC.
11868bd00fSLiam Girdwood //
12868bd00fSLiam Girdwood 
13868bd00fSLiam Girdwood #include <linux/pm_runtime.h>
14868bd00fSLiam Girdwood #include <sound/pcm_params.h>
15868bd00fSLiam Girdwood #include <sound/sof.h>
16868bd00fSLiam Girdwood #include "sof-priv.h"
17ee1e79b7SRanjani Sridharan #include "sof-audio.h"
18868bd00fSLiam Girdwood #include "ops.h"
1970368106SCezary Rojewski #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
2070368106SCezary Rojewski #include "compress.h"
2170368106SCezary Rojewski #endif
22868bd00fSLiam Girdwood 
23868bd00fSLiam Girdwood /* Create DMA buffer page table for DSP */
241c91d77eSKuninori Morimoto static int create_page_table(struct snd_soc_component *component,
251c91d77eSKuninori Morimoto 			     struct snd_pcm_substream *substream,
26868bd00fSLiam Girdwood 			     unsigned char *dma_area, size_t size)
27868bd00fSLiam Girdwood {
281205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
29868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
30868bd00fSLiam Girdwood 	struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
31868bd00fSLiam Girdwood 	int stream = substream->stream;
32868bd00fSLiam Girdwood 
33ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
34868bd00fSLiam Girdwood 	if (!spcm)
35868bd00fSLiam Girdwood 		return -EINVAL;
36868bd00fSLiam Girdwood 
37ee1e79b7SRanjani Sridharan 	return snd_sof_create_page_table(component->dev, dmab,
38868bd00fSLiam Girdwood 		spcm->stream[stream].page_table.area, size);
39868bd00fSLiam Girdwood }
40868bd00fSLiam Girdwood 
41868bd00fSLiam Girdwood static int sof_pcm_dsp_params(struct snd_sof_pcm *spcm, struct snd_pcm_substream *substream,
42868bd00fSLiam Girdwood 			      const struct sof_ipc_pcm_params_reply *reply)
43868bd00fSLiam Girdwood {
44ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = spcm->scomp;
45ee1e79b7SRanjani Sridharan 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
46ee1e79b7SRanjani Sridharan 
47868bd00fSLiam Girdwood 	/* validate offset */
48868bd00fSLiam Girdwood 	int ret = snd_sof_ipc_pcm_params(sdev, substream, reply);
49868bd00fSLiam Girdwood 
50868bd00fSLiam Girdwood 	if (ret < 0)
51ee1e79b7SRanjani Sridharan 		dev_err(scomp->dev, "error: got wrong reply for PCM %d\n",
52868bd00fSLiam Girdwood 			spcm->pcm.pcm_id);
53868bd00fSLiam Girdwood 
54868bd00fSLiam Girdwood 	return ret;
55868bd00fSLiam Girdwood }
56868bd00fSLiam Girdwood 
57e2803e61SKeyon Jie /*
58e2803e61SKeyon Jie  * sof pcm period elapse work
59e2803e61SKeyon Jie  */
609ef91cadSGuennadi Liakhovetski void snd_sof_pcm_period_elapsed_work(struct work_struct *work)
61e2803e61SKeyon Jie {
62e2803e61SKeyon Jie 	struct snd_sof_pcm_stream *sps =
63e2803e61SKeyon Jie 		container_of(work, struct snd_sof_pcm_stream,
64e2803e61SKeyon Jie 			     period_elapsed_work);
65e2803e61SKeyon Jie 
66e2803e61SKeyon Jie 	snd_pcm_period_elapsed(sps->substream);
67e2803e61SKeyon Jie }
68e2803e61SKeyon Jie 
69e2803e61SKeyon Jie /*
70e2803e61SKeyon Jie  * sof pcm period elapse, this could be called at irq thread context.
71e2803e61SKeyon Jie  */
72e2803e61SKeyon Jie void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
73e2803e61SKeyon Jie {
741205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
75e2803e61SKeyon Jie 	struct snd_soc_component *component =
76ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
77e2803e61SKeyon Jie 	struct snd_sof_pcm *spcm;
78e2803e61SKeyon Jie 
79ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
80e2803e61SKeyon Jie 	if (!spcm) {
81ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
82e2803e61SKeyon Jie 			"error: period elapsed for unknown stream!\n");
83e2803e61SKeyon Jie 		return;
84e2803e61SKeyon Jie 	}
85e2803e61SKeyon Jie 
86e2803e61SKeyon Jie 	/*
87e2803e61SKeyon Jie 	 * snd_pcm_period_elapsed() can be called in interrupt context
88e2803e61SKeyon Jie 	 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(),
89e2803e61SKeyon Jie 	 * when the PCM is done draining or xrun happened, a STOP IPC will
90e2803e61SKeyon Jie 	 * then be sent and this IPC will hit IPC timeout.
91e2803e61SKeyon Jie 	 * To avoid sending IPC before the previous IPC is handled, we
92e2803e61SKeyon Jie 	 * schedule delayed work here to call the snd_pcm_period_elapsed().
93e2803e61SKeyon Jie 	 */
94e2803e61SKeyon Jie 	schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
95e2803e61SKeyon Jie }
96e2803e61SKeyon Jie EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
97e2803e61SKeyon Jie 
98cfe8191bSKai Vehmanen static int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream,
99cfe8191bSKai Vehmanen 				struct snd_sof_dev *sdev,
100cfe8191bSKai Vehmanen 				struct snd_sof_pcm *spcm)
101cfe8191bSKai Vehmanen {
102cfe8191bSKai Vehmanen 	struct sof_ipc_stream stream;
103cfe8191bSKai Vehmanen 	struct sof_ipc_reply reply;
104cfe8191bSKai Vehmanen 	int ret;
105cfe8191bSKai Vehmanen 
106cfe8191bSKai Vehmanen 	stream.hdr.size = sizeof(stream);
107cfe8191bSKai Vehmanen 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
108cfe8191bSKai Vehmanen 	stream.comp_id = spcm->stream[substream->stream].comp_id;
109cfe8191bSKai Vehmanen 
110cfe8191bSKai Vehmanen 	/* send IPC to the DSP */
111cfe8191bSKai Vehmanen 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
112cfe8191bSKai Vehmanen 				 sizeof(stream), &reply, sizeof(reply));
113cfe8191bSKai Vehmanen 	if (!ret)
114cfe8191bSKai Vehmanen 		spcm->prepared[substream->stream] = false;
115cfe8191bSKai Vehmanen 
116cfe8191bSKai Vehmanen 	return ret;
117cfe8191bSKai Vehmanen }
118cfe8191bSKai Vehmanen 
1191c91d77eSKuninori Morimoto static int sof_pcm_hw_params(struct snd_soc_component *component,
1201c91d77eSKuninori Morimoto 			     struct snd_pcm_substream *substream,
121868bd00fSLiam Girdwood 			     struct snd_pcm_hw_params *params)
122868bd00fSLiam Girdwood {
1231205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
124868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
125868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
126868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
127868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params pcm;
128868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params_reply ipc_params_reply;
129868bd00fSLiam Girdwood 	int ret;
130868bd00fSLiam Girdwood 
131868bd00fSLiam Girdwood 	/* nothing to do for BE */
132868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
133868bd00fSLiam Girdwood 		return 0;
134868bd00fSLiam Girdwood 
135ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
136868bd00fSLiam Girdwood 	if (!spcm)
137868bd00fSLiam Girdwood 		return -EINVAL;
138868bd00fSLiam Girdwood 
139cfe8191bSKai Vehmanen 	/*
140cfe8191bSKai Vehmanen 	 * Handle repeated calls to hw_params() without free_pcm() in
141cfe8191bSKai Vehmanen 	 * between. At least ALSA OSS emulation depends on this.
142cfe8191bSKai Vehmanen 	 */
143cfe8191bSKai Vehmanen 	if (spcm->prepared[substream->stream]) {
144cfe8191bSKai Vehmanen 		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
145cfe8191bSKai Vehmanen 		if (ret < 0)
146cfe8191bSKai Vehmanen 			return ret;
147cfe8191bSKai Vehmanen 	}
148cfe8191bSKai Vehmanen 
149ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
150868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream);
151868bd00fSLiam Girdwood 
152868bd00fSLiam Girdwood 	memset(&pcm, 0, sizeof(pcm));
153868bd00fSLiam Girdwood 
15457e960f0STakashi Iwai 	/* create compressed page table for audio firmware */
15557e960f0STakashi Iwai 	if (runtime->buffer_changed) {
1561c91d77eSKuninori Morimoto 		ret = create_page_table(component, substream, runtime->dma_area,
157868bd00fSLiam Girdwood 					runtime->dma_bytes);
158868bd00fSLiam Girdwood 		if (ret < 0)
159868bd00fSLiam Girdwood 			return ret;
160868bd00fSLiam Girdwood 	}
161868bd00fSLiam Girdwood 
162868bd00fSLiam Girdwood 	/* number of pages should be rounded up */
163868bd00fSLiam Girdwood 	pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
164868bd00fSLiam Girdwood 
165868bd00fSLiam Girdwood 	/* set IPC PCM parameters */
166868bd00fSLiam Girdwood 	pcm.hdr.size = sizeof(pcm);
167868bd00fSLiam Girdwood 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
168868bd00fSLiam Girdwood 	pcm.comp_id = spcm->stream[substream->stream].comp_id;
169868bd00fSLiam Girdwood 	pcm.params.hdr.size = sizeof(pcm.params);
170868bd00fSLiam Girdwood 	pcm.params.buffer.phy_addr =
171868bd00fSLiam Girdwood 		spcm->stream[substream->stream].page_table.addr;
172868bd00fSLiam Girdwood 	pcm.params.buffer.size = runtime->dma_bytes;
173868bd00fSLiam Girdwood 	pcm.params.direction = substream->stream;
174868bd00fSLiam Girdwood 	pcm.params.sample_valid_bytes = params_width(params) >> 3;
175868bd00fSLiam Girdwood 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
176868bd00fSLiam Girdwood 	pcm.params.rate = params_rate(params);
177868bd00fSLiam Girdwood 	pcm.params.channels = params_channels(params);
178868bd00fSLiam Girdwood 	pcm.params.host_period_bytes = params_period_bytes(params);
179868bd00fSLiam Girdwood 
180868bd00fSLiam Girdwood 	/* container size */
181868bd00fSLiam Girdwood 	ret = snd_pcm_format_physical_width(params_format(params));
182868bd00fSLiam Girdwood 	if (ret < 0)
183868bd00fSLiam Girdwood 		return ret;
184868bd00fSLiam Girdwood 	pcm.params.sample_container_bytes = ret >> 3;
185868bd00fSLiam Girdwood 
186868bd00fSLiam Girdwood 	/* format */
187868bd00fSLiam Girdwood 	switch (params_format(params)) {
188868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S16:
189868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
190868bd00fSLiam Girdwood 		break;
191868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S24:
192868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
193868bd00fSLiam Girdwood 		break;
194868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S32:
195868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
196868bd00fSLiam Girdwood 		break;
197868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_FLOAT:
198868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
199868bd00fSLiam Girdwood 		break;
200868bd00fSLiam Girdwood 	default:
201868bd00fSLiam Girdwood 		return -EINVAL;
202868bd00fSLiam Girdwood 	}
203868bd00fSLiam Girdwood 
204868bd00fSLiam Girdwood 	/* firmware already configured host stream */
205868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_hw_params(sdev,
206868bd00fSLiam Girdwood 					     substream,
207868bd00fSLiam Girdwood 					     params,
208868bd00fSLiam Girdwood 					     &pcm.params);
209868bd00fSLiam Girdwood 	if (ret < 0) {
210ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw params failed\n");
211868bd00fSLiam Girdwood 		return ret;
212868bd00fSLiam Girdwood 	}
213868bd00fSLiam Girdwood 
214ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag);
215868bd00fSLiam Girdwood 
216868bd00fSLiam Girdwood 	/* send IPC to the DSP */
217868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
218868bd00fSLiam Girdwood 				 &ipc_params_reply, sizeof(ipc_params_reply));
219868bd00fSLiam Girdwood 	if (ret < 0) {
220ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: hw params ipc failed for stream %d\n",
221868bd00fSLiam Girdwood 			pcm.params.stream_tag);
222868bd00fSLiam Girdwood 		return ret;
223868bd00fSLiam Girdwood 	}
224868bd00fSLiam Girdwood 
225868bd00fSLiam Girdwood 	ret = sof_pcm_dsp_params(spcm, substream, &ipc_params_reply);
226868bd00fSLiam Girdwood 	if (ret < 0)
227868bd00fSLiam Girdwood 		return ret;
228868bd00fSLiam Girdwood 
22904c80277SKai Vehmanen 	spcm->prepared[substream->stream] = true;
23004c80277SKai Vehmanen 
231868bd00fSLiam Girdwood 	/* save pcm hw_params */
232868bd00fSLiam Girdwood 	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
233868bd00fSLiam Girdwood 
234868bd00fSLiam Girdwood 	return ret;
235868bd00fSLiam Girdwood }
236868bd00fSLiam Girdwood 
2371c91d77eSKuninori Morimoto static int sof_pcm_hw_free(struct snd_soc_component *component,
2381c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
239868bd00fSLiam Girdwood {
2401205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
241868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
242868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
243e66e52c5SKai Vehmanen 	int ret, err = 0;
244868bd00fSLiam Girdwood 
245868bd00fSLiam Girdwood 	/* nothing to do for BE */
246868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
247868bd00fSLiam Girdwood 		return 0;
248868bd00fSLiam Girdwood 
249ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
250868bd00fSLiam Girdwood 	if (!spcm)
251868bd00fSLiam Girdwood 		return -EINVAL;
252868bd00fSLiam Girdwood 
253ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
254ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
255868bd00fSLiam Girdwood 
256e66e52c5SKai Vehmanen 	if (spcm->prepared[substream->stream]) {
257a49b6871SKai Vehmanen 		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
258e66e52c5SKai Vehmanen 		if (ret < 0)
259e66e52c5SKai Vehmanen 			err = ret;
260e66e52c5SKai Vehmanen 	}
261868bd00fSLiam Girdwood 
262e2803e61SKeyon Jie 	cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
263e2803e61SKeyon Jie 
26493146bc2SRanjani Sridharan 	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
265e66e52c5SKai Vehmanen 	if (ret < 0) {
266ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw free failed\n");
267e66e52c5SKai Vehmanen 		err = ret;
268e66e52c5SKai Vehmanen 	}
26993146bc2SRanjani Sridharan 
270e66e52c5SKai Vehmanen 	return err;
271868bd00fSLiam Girdwood }
272868bd00fSLiam Girdwood 
2731c91d77eSKuninori Morimoto static int sof_pcm_prepare(struct snd_soc_component *component,
2741c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
275868bd00fSLiam Girdwood {
2761205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
277868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
278868bd00fSLiam Girdwood 	int ret;
279868bd00fSLiam Girdwood 
280868bd00fSLiam Girdwood 	/* nothing to do for BE */
281868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
282868bd00fSLiam Girdwood 		return 0;
283868bd00fSLiam Girdwood 
284ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
285868bd00fSLiam Girdwood 	if (!spcm)
286868bd00fSLiam Girdwood 		return -EINVAL;
287868bd00fSLiam Girdwood 
28804c80277SKai Vehmanen 	if (spcm->prepared[substream->stream])
289868bd00fSLiam Girdwood 		return 0;
290868bd00fSLiam Girdwood 
291ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
292ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
293868bd00fSLiam Girdwood 
294868bd00fSLiam Girdwood 	/* set hw_params */
2951c91d77eSKuninori Morimoto 	ret = sof_pcm_hw_params(component,
2961c91d77eSKuninori Morimoto 				substream, &spcm->params[substream->stream]);
297868bd00fSLiam Girdwood 	if (ret < 0) {
298ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
299ee1e79b7SRanjani Sridharan 			"error: set pcm hw_params after resume\n");
300868bd00fSLiam Girdwood 		return ret;
301868bd00fSLiam Girdwood 	}
302868bd00fSLiam Girdwood 
303868bd00fSLiam Girdwood 	return 0;
304868bd00fSLiam Girdwood }
305868bd00fSLiam Girdwood 
306868bd00fSLiam Girdwood /*
307868bd00fSLiam Girdwood  * FE dai link trigger actions are always executed in non-atomic context because
308868bd00fSLiam Girdwood  * they involve IPC's.
309868bd00fSLiam Girdwood  */
3101c91d77eSKuninori Morimoto static int sof_pcm_trigger(struct snd_soc_component *component,
3111c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream, int cmd)
312868bd00fSLiam Girdwood {
3131205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
314868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
315868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
316868bd00fSLiam Girdwood 	struct sof_ipc_stream stream;
317868bd00fSLiam Girdwood 	struct sof_ipc_reply reply;
31804c80277SKai Vehmanen 	bool reset_hw_params = false;
3190a1b0834SPan Xiuli 	bool ipc_first = false;
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 
330ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
331868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream, cmd);
332868bd00fSLiam Girdwood 
333868bd00fSLiam Girdwood 	stream.hdr.size = sizeof(stream);
334868bd00fSLiam Girdwood 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
335868bd00fSLiam Girdwood 	stream.comp_id = spcm->stream[substream->stream].comp_id;
336868bd00fSLiam Girdwood 
337868bd00fSLiam Girdwood 	switch (cmd) {
338868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
339868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
3400a1b0834SPan Xiuli 		ipc_first = true;
341868bd00fSLiam Girdwood 		break;
342868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
343868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
344868bd00fSLiam Girdwood 		break;
345868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
346ac8c046fSKeyon Jie 		if (spcm->stream[substream->stream].suspend_ignored) {
347ac8c046fSKeyon Jie 			/*
348ac8c046fSKeyon Jie 			 * this case will be triggered when INFO_RESUME is
349ac8c046fSKeyon Jie 			 * supported, no need to resume streams that remained
350ac8c046fSKeyon Jie 			 * enabled in D0ix.
351ac8c046fSKeyon Jie 			 */
352ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = false;
353ac8c046fSKeyon Jie 			return 0;
354ac8c046fSKeyon Jie 		}
355ac8c046fSKeyon Jie 
356868bd00fSLiam Girdwood 		/* set up hw_params */
3571c91d77eSKuninori Morimoto 		ret = sof_pcm_prepare(component, substream);
358868bd00fSLiam Girdwood 		if (ret < 0) {
359ee1e79b7SRanjani Sridharan 			dev_err(component->dev,
360868bd00fSLiam Girdwood 				"error: failed to set up hw_params upon resume\n");
361868bd00fSLiam Girdwood 			return ret;
362868bd00fSLiam Girdwood 		}
363868bd00fSLiam Girdwood 
364df561f66SGustavo A. R. Silva 		fallthrough;
365868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
366ac8c046fSKeyon Jie 		if (spcm->stream[substream->stream].suspend_ignored) {
367ac8c046fSKeyon Jie 			/*
368ac8c046fSKeyon Jie 			 * This case will be triggered when INFO_RESUME is
369ac8c046fSKeyon Jie 			 * not supported, no need to re-start streams that
370ac8c046fSKeyon Jie 			 * remained enabled in D0ix.
371ac8c046fSKeyon Jie 			 */
372ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = false;
373ac8c046fSKeyon Jie 			return 0;
374ac8c046fSKeyon Jie 		}
375868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
376868bd00fSLiam Girdwood 		break;
377868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
378043ae13bSRanjani Sridharan 		if (sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
379ac8c046fSKeyon Jie 		    spcm->stream[substream->stream].d0i3_compatible) {
380ac8c046fSKeyon Jie 			/*
381ac8c046fSKeyon Jie 			 * trap the event, not sending trigger stop to
382ac8c046fSKeyon Jie 			 * prevent the FW pipelines from being stopped,
383ac8c046fSKeyon Jie 			 * and mark the flag to ignore the upcoming DAPM
384ac8c046fSKeyon Jie 			 * PM events.
385ac8c046fSKeyon Jie 			 */
386ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = true;
387ac8c046fSKeyon Jie 			return 0;
388ac8c046fSKeyon Jie 		}
389df561f66SGustavo A. R. Silva 		fallthrough;
390868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
391868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
3920a1b0834SPan Xiuli 		ipc_first = true;
39304c80277SKai Vehmanen 		reset_hw_params = true;
394868bd00fSLiam Girdwood 		break;
395868bd00fSLiam Girdwood 	default:
396ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: unhandled trigger cmd %d\n",
397ee1e79b7SRanjani Sridharan 			cmd);
398868bd00fSLiam Girdwood 		return -EINVAL;
399868bd00fSLiam Girdwood 	}
400868bd00fSLiam Girdwood 
4010a1b0834SPan Xiuli 	/*
4020a1b0834SPan Xiuli 	 * DMA and IPC sequence is different for start and stop. Need to send
4030a1b0834SPan Xiuli 	 * STOP IPC before stop DMA
4040a1b0834SPan Xiuli 	 */
4050a1b0834SPan Xiuli 	if (!ipc_first)
406868bd00fSLiam Girdwood 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
407868bd00fSLiam Girdwood 
408868bd00fSLiam Girdwood 	/* send IPC to the DSP */
409868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
410868bd00fSLiam Girdwood 				 sizeof(stream), &reply, sizeof(reply));
411868bd00fSLiam Girdwood 
4120a1b0834SPan Xiuli 	/* need to STOP DMA even if STOP IPC failed */
4130a1b0834SPan Xiuli 	if (ipc_first)
4140a1b0834SPan Xiuli 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
4150a1b0834SPan Xiuli 
4160a1b0834SPan Xiuli 	/* free PCM if reset_hw_params is set and the STOP IPC is successful */
417a49b6871SKai Vehmanen 	if (!ret && reset_hw_params)
418a49b6871SKai Vehmanen 		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
419a49b6871SKai Vehmanen 
420868bd00fSLiam Girdwood 	return ret;
421868bd00fSLiam Girdwood }
422868bd00fSLiam Girdwood 
4231c91d77eSKuninori Morimoto static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
4241c91d77eSKuninori Morimoto 					 struct snd_pcm_substream *substream)
425868bd00fSLiam Girdwood {
4261205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
427868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
428868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
429868bd00fSLiam Girdwood 	snd_pcm_uframes_t host, dai;
430868bd00fSLiam Girdwood 
431868bd00fSLiam Girdwood 	/* nothing to do for BE */
432868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
433868bd00fSLiam Girdwood 		return 0;
434868bd00fSLiam Girdwood 
435868bd00fSLiam Girdwood 	/* use dsp ops pointer callback directly if set */
436868bd00fSLiam Girdwood 	if (sof_ops(sdev)->pcm_pointer)
437868bd00fSLiam Girdwood 		return sof_ops(sdev)->pcm_pointer(sdev, substream);
438868bd00fSLiam Girdwood 
439ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
440868bd00fSLiam Girdwood 	if (!spcm)
441868bd00fSLiam Girdwood 		return -EINVAL;
442868bd00fSLiam Girdwood 
443868bd00fSLiam Girdwood 	/* read position from DSP */
444868bd00fSLiam Girdwood 	host = bytes_to_frames(substream->runtime,
445868bd00fSLiam Girdwood 			       spcm->stream[substream->stream].posn.host_posn);
446868bd00fSLiam Girdwood 	dai = bytes_to_frames(substream->runtime,
447868bd00fSLiam Girdwood 			      spcm->stream[substream->stream].posn.dai_posn);
448868bd00fSLiam Girdwood 
449277ff236SPierre-Louis Bossart 	dev_vdbg(component->dev,
450ee1e79b7SRanjani Sridharan 		 "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
451868bd00fSLiam Girdwood 		 spcm->pcm.pcm_id, substream->stream, host, dai);
452868bd00fSLiam Girdwood 
453868bd00fSLiam Girdwood 	return host;
454868bd00fSLiam Girdwood }
455868bd00fSLiam Girdwood 
4561c91d77eSKuninori Morimoto static int sof_pcm_open(struct snd_soc_component *component,
4571c91d77eSKuninori Morimoto 			struct snd_pcm_substream *substream)
458868bd00fSLiam Girdwood {
4591205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
460868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
461868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
46227e322faSPierre-Louis Bossart 	const struct snd_sof_dsp_ops *ops = sof_ops(sdev);
463868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
464868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
465868bd00fSLiam Girdwood 	int ret;
466868bd00fSLiam Girdwood 
467868bd00fSLiam Girdwood 	/* nothing to do for BE */
468868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
469868bd00fSLiam Girdwood 		return 0;
470868bd00fSLiam Girdwood 
471ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
472868bd00fSLiam Girdwood 	if (!spcm)
473868bd00fSLiam Girdwood 		return -EINVAL;
474868bd00fSLiam Girdwood 
475ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
476ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
477868bd00fSLiam Girdwood 
478868bd00fSLiam Girdwood 
479868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[substream->stream];
480868bd00fSLiam Girdwood 
481868bd00fSLiam Girdwood 	/* set runtime config */
48227e322faSPierre-Louis Bossart 	runtime->hw.info = ops->hw_info; /* platform-specific */
48327e322faSPierre-Louis Bossart 
4849983ac49SKai Vehmanen 	/* set any runtime constraints based on topology */
485868bd00fSLiam Girdwood 	runtime->hw.formats = le64_to_cpu(caps->formats);
486868bd00fSLiam Girdwood 	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
487868bd00fSLiam Girdwood 	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
488868bd00fSLiam Girdwood 	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
489868bd00fSLiam Girdwood 	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
490868bd00fSLiam Girdwood 
491868bd00fSLiam Girdwood 	/*
492868bd00fSLiam Girdwood 	 * caps->buffer_size_min is not used since the
493868bd00fSLiam Girdwood 	 * snd_pcm_hardware structure only defines buffer_bytes_max
494868bd00fSLiam Girdwood 	 */
495868bd00fSLiam Girdwood 	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
496868bd00fSLiam Girdwood 
497ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period min %zd max %zd bytes\n",
498868bd00fSLiam Girdwood 		runtime->hw.period_bytes_min,
499868bd00fSLiam Girdwood 		runtime->hw.period_bytes_max);
500ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period count %d max %d\n",
501868bd00fSLiam Girdwood 		runtime->hw.periods_min,
502868bd00fSLiam Girdwood 		runtime->hw.periods_max);
503ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "buffer max %zd bytes\n",
504868bd00fSLiam Girdwood 		runtime->hw.buffer_bytes_max);
505868bd00fSLiam Girdwood 
506868bd00fSLiam Girdwood 	/* set wait time - TODO: come from topology */
507868bd00fSLiam Girdwood 	substream->wait_time = 500;
508868bd00fSLiam Girdwood 
509868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.host_posn = 0;
510868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.dai_posn = 0;
511868bd00fSLiam Girdwood 	spcm->stream[substream->stream].substream = substream;
51204c80277SKai Vehmanen 	spcm->prepared[substream->stream] = false;
513868bd00fSLiam Girdwood 
514868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_open(sdev, substream);
51514a2212dSRanjani Sridharan 	if (ret < 0)
516ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm open failed %d\n", ret);
517868bd00fSLiam Girdwood 
518868bd00fSLiam Girdwood 	return ret;
519868bd00fSLiam Girdwood }
520868bd00fSLiam Girdwood 
5211c91d77eSKuninori Morimoto static int sof_pcm_close(struct snd_soc_component *component,
5221c91d77eSKuninori Morimoto 			 struct snd_pcm_substream *substream)
523868bd00fSLiam Girdwood {
5241205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
525868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
526868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
527868bd00fSLiam Girdwood 	int err;
528868bd00fSLiam Girdwood 
529868bd00fSLiam Girdwood 	/* nothing to do for BE */
530868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
531868bd00fSLiam Girdwood 		return 0;
532868bd00fSLiam Girdwood 
533ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
534868bd00fSLiam Girdwood 	if (!spcm)
535868bd00fSLiam Girdwood 		return -EINVAL;
536868bd00fSLiam Girdwood 
537ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
538ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
539868bd00fSLiam Girdwood 
540868bd00fSLiam Girdwood 	err = snd_sof_pcm_platform_close(sdev, substream);
541868bd00fSLiam Girdwood 	if (err < 0) {
542ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm close failed %d\n",
543868bd00fSLiam Girdwood 			err);
544868bd00fSLiam Girdwood 		/*
545868bd00fSLiam Girdwood 		 * keep going, no point in preventing the close
546868bd00fSLiam Girdwood 		 * from happening
547868bd00fSLiam Girdwood 		 */
548868bd00fSLiam Girdwood 	}
549868bd00fSLiam Girdwood 
550868bd00fSLiam Girdwood 	return 0;
551868bd00fSLiam Girdwood }
552868bd00fSLiam Girdwood 
553868bd00fSLiam Girdwood /*
554868bd00fSLiam Girdwood  * Pre-allocate playback/capture audio buffer pages.
555868bd00fSLiam Girdwood  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
556868bd00fSLiam Girdwood  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
557868bd00fSLiam Girdwood  */
5581c91d77eSKuninori Morimoto static int sof_pcm_new(struct snd_soc_component *component,
5591c91d77eSKuninori Morimoto 		       struct snd_soc_pcm_runtime *rtd)
560868bd00fSLiam Girdwood {
561868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
562868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
563868bd00fSLiam Girdwood 	struct snd_pcm *pcm = rtd->pcm;
564868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
565868bd00fSLiam Girdwood 	int stream = SNDRV_PCM_STREAM_PLAYBACK;
566868bd00fSLiam Girdwood 
567868bd00fSLiam Girdwood 	/* find SOF PCM for this RTD */
568ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
569868bd00fSLiam Girdwood 	if (!spcm) {
570ee1e79b7SRanjani Sridharan 		dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
571868bd00fSLiam Girdwood 			 rtd->dai_link->id);
572868bd00fSLiam Girdwood 		return 0;
573868bd00fSLiam Girdwood 	}
574868bd00fSLiam Girdwood 
575ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
576868bd00fSLiam Girdwood 
577868bd00fSLiam Girdwood 	/* do we need to pre-allocate playback audio buffer pages */
578868bd00fSLiam Girdwood 	if (!spcm->pcm.playback)
579868bd00fSLiam Girdwood 		goto capture;
580868bd00fSLiam Girdwood 
581868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
582868bd00fSLiam Girdwood 
583868bd00fSLiam Girdwood 	/* pre-allocate playback audio buffer pages */
584ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
585ee1e79b7SRanjani Sridharan 		"spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
586868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
587868bd00fSLiam Girdwood 
5884f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
5894f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL playback substream!\n");
5904f7f9564SGuennadi Liakhovetski 		return -EINVAL;
5914f7f9564SGuennadi Liakhovetski 	}
5924f7f9564SGuennadi Liakhovetski 
59357e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
594868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
595e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
596868bd00fSLiam Girdwood capture:
597868bd00fSLiam Girdwood 	stream = SNDRV_PCM_STREAM_CAPTURE;
598868bd00fSLiam Girdwood 
599868bd00fSLiam Girdwood 	/* do we need to pre-allocate capture audio buffer pages */
600868bd00fSLiam Girdwood 	if (!spcm->pcm.capture)
601868bd00fSLiam Girdwood 		return 0;
602868bd00fSLiam Girdwood 
603868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
604868bd00fSLiam Girdwood 
605868bd00fSLiam Girdwood 	/* pre-allocate capture audio buffer pages */
606ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
607ee1e79b7SRanjani Sridharan 		"spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
608868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
609868bd00fSLiam Girdwood 
6104f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
6114f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL capture substream!\n");
6124f7f9564SGuennadi Liakhovetski 		return -EINVAL;
6134f7f9564SGuennadi Liakhovetski 	}
6144f7f9564SGuennadi Liakhovetski 
61557e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
616868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
617e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
618868bd00fSLiam Girdwood 
619868bd00fSLiam Girdwood 	return 0;
620868bd00fSLiam Girdwood }
621868bd00fSLiam Girdwood 
622c943a586SJaska Uimonen static void ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
623c943a586SJaska Uimonen 					    struct snd_pcm_hw_params *params)
624c943a586SJaska Uimonen {
625c943a586SJaska Uimonen 	struct sof_ipc_dai_config *config;
626c943a586SJaska Uimonen 	struct snd_sof_dai *dai;
627c943a586SJaska Uimonen 	int i;
628c943a586SJaska Uimonen 
629c943a586SJaska Uimonen 	/*
630c943a586SJaska Uimonen 	 * Search for all matching DAIs as we can have both playback and capture DAI
631c943a586SJaska Uimonen 	 * associated with the same link.
632c943a586SJaska Uimonen 	 */
633c943a586SJaska Uimonen 	list_for_each_entry(dai, &sdev->dai_list, list) {
634c943a586SJaska Uimonen 		if (!dai->name || strcmp(link_name, dai->name))
635c943a586SJaska Uimonen 			continue;
636c943a586SJaska Uimonen 		for (i = 0; i < dai->number_configs; i++) {
637c943a586SJaska Uimonen 			config = &dai->dai_config[i];
638c943a586SJaska Uimonen 			if (config->ssp.fsync_rate == params_rate(params)) {
639c943a586SJaska Uimonen 				dev_dbg(sdev->dev, "DAI config %d matches pcm hw params\n", i);
640c943a586SJaska Uimonen 				dai->current_config = i;
641c943a586SJaska Uimonen 				break;
642c943a586SJaska Uimonen 			}
643c943a586SJaska Uimonen 		}
644c943a586SJaska Uimonen 	}
645c943a586SJaska Uimonen }
646c943a586SJaska Uimonen 
647868bd00fSLiam Girdwood /* fixup the BE DAI link to match any values from topology */
648f805e7e0SRanjani Sridharan int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
649868bd00fSLiam Girdwood {
650868bd00fSLiam Girdwood 	struct snd_interval *rate = hw_param_interval(params,
651868bd00fSLiam Girdwood 			SNDRV_PCM_HW_PARAM_RATE);
652868bd00fSLiam Girdwood 	struct snd_interval *channels = hw_param_interval(params,
653868bd00fSLiam Girdwood 						SNDRV_PCM_HW_PARAM_CHANNELS);
654868bd00fSLiam Girdwood 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
655868bd00fSLiam Girdwood 	struct snd_soc_component *component =
656ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
657868bd00fSLiam Girdwood 	struct snd_sof_dai *dai =
658ee1e79b7SRanjani Sridharan 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
659c943a586SJaska Uimonen 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
660fd045558Sranderwang 	struct snd_soc_dpcm *dpcm;
661868bd00fSLiam Girdwood 
662868bd00fSLiam Girdwood 	/* no topology exists for this BE, try a common configuration */
663868bd00fSLiam Girdwood 	if (!dai) {
664ee1e79b7SRanjani Sridharan 		dev_warn(component->dev,
665ee1e79b7SRanjani Sridharan 			 "warning: no topology found for BE DAI %s config\n",
666868bd00fSLiam Girdwood 			 rtd->dai_link->name);
667868bd00fSLiam Girdwood 
668868bd00fSLiam Girdwood 		/*  set 48k, stereo, 16bits by default */
669868bd00fSLiam Girdwood 		rate->min = 48000;
670868bd00fSLiam Girdwood 		rate->max = 48000;
671868bd00fSLiam Girdwood 
672868bd00fSLiam Girdwood 		channels->min = 2;
673868bd00fSLiam Girdwood 		channels->max = 2;
674868bd00fSLiam Girdwood 
675868bd00fSLiam Girdwood 		snd_mask_none(fmt);
676868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
677868bd00fSLiam Girdwood 
678868bd00fSLiam Girdwood 		return 0;
679868bd00fSLiam Girdwood 	}
680868bd00fSLiam Girdwood 
681868bd00fSLiam Girdwood 	/* read format from topology */
682868bd00fSLiam Girdwood 	snd_mask_none(fmt);
683868bd00fSLiam Girdwood 
684868bd00fSLiam Girdwood 	switch (dai->comp_dai.config.frame_fmt) {
685868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S16_LE:
686868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
687868bd00fSLiam Girdwood 		break;
688868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S24_4LE:
689868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
690868bd00fSLiam Girdwood 		break;
691868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S32_LE:
692868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
693868bd00fSLiam Girdwood 		break;
694868bd00fSLiam Girdwood 	default:
695ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: No available DAI format!\n");
696868bd00fSLiam Girdwood 		return -EINVAL;
697868bd00fSLiam Girdwood 	}
698868bd00fSLiam Girdwood 
699868bd00fSLiam Girdwood 	/* read rate and channels from topology */
700868bd00fSLiam Girdwood 	switch (dai->dai_config->type) {
701868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_SSP:
702c943a586SJaska Uimonen 		/* search for config to pcm params match, if not found use default */
703c943a586SJaska Uimonen 		ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);
704c943a586SJaska Uimonen 
705c1c03888SJaska Uimonen 		rate->min = dai->dai_config[dai->current_config].ssp.fsync_rate;
706c1c03888SJaska Uimonen 		rate->max = dai->dai_config[dai->current_config].ssp.fsync_rate;
707c1c03888SJaska Uimonen 		channels->min = dai->dai_config[dai->current_config].ssp.tdm_slots;
708c1c03888SJaska Uimonen 		channels->max = dai->dai_config[dai->current_config].ssp.tdm_slots;
709868bd00fSLiam Girdwood 
710ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
711868bd00fSLiam Girdwood 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
712ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
713868bd00fSLiam Girdwood 			"channels_min: %d channels_max: %d\n",
714868bd00fSLiam Girdwood 			channels->min, channels->max);
715868bd00fSLiam Girdwood 
716868bd00fSLiam Girdwood 		break;
717868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_DMIC:
718868bd00fSLiam Girdwood 		/* DMIC only supports 16 or 32 bit formats */
719868bd00fSLiam Girdwood 		if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
720ee1e79b7SRanjani Sridharan 			dev_err(component->dev,
721868bd00fSLiam Girdwood 				"error: invalid fmt %d for DAI type %d\n",
722868bd00fSLiam Girdwood 				dai->comp_dai.config.frame_fmt,
723868bd00fSLiam Girdwood 				dai->dai_config->type);
724868bd00fSLiam Girdwood 		}
725868bd00fSLiam Girdwood 		break;
726868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_HDA:
727fd045558Sranderwang 		/*
728135ab457SPierre-Louis Bossart 		 * HDAudio does not follow the default trigger
729fd045558Sranderwang 		 * sequence due to firmware implementation
730fd045558Sranderwang 		 */
731fd045558Sranderwang 		for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
732fd045558Sranderwang 			struct snd_soc_pcm_runtime *fe = dpcm->fe;
733fd045558Sranderwang 
734fd045558Sranderwang 			fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
735fd045558Sranderwang 				SND_SOC_DPCM_TRIGGER_POST;
736fd045558Sranderwang 		}
737868bd00fSLiam Girdwood 		break;
7386e3360cdSPierre-Louis Bossart 	case SOF_DAI_INTEL_ALH:
739e1711b1fSRander Wang 		/*
740e1711b1fSRander Wang 		 * Dai could run with different channel count compared with
741e1711b1fSRander Wang 		 * front end, so get dai channel count from topology
742e1711b1fSRander Wang 		 */
743e1711b1fSRander Wang 		channels->min = dai->dai_config->alh.channels;
744e1711b1fSRander Wang 		channels->max = dai->dai_config->alh.channels;
7456e3360cdSPierre-Louis Bossart 		break;
746a4eff5f8SDaniel Baluta 	case SOF_DAI_IMX_ESAI:
74751b0243aSDaniel Baluta 		rate->min = dai->dai_config->esai.fsync_rate;
74851b0243aSDaniel Baluta 		rate->max = dai->dai_config->esai.fsync_rate;
749a4eff5f8SDaniel Baluta 		channels->min = dai->dai_config->esai.tdm_slots;
750a4eff5f8SDaniel Baluta 		channels->max = dai->dai_config->esai.tdm_slots;
751a4eff5f8SDaniel Baluta 
752ee1e79b7SRanjani Sridharan 		dev_dbg(component->dev,
75351b0243aSDaniel Baluta 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
75451b0243aSDaniel Baluta 		dev_dbg(component->dev,
755a4eff5f8SDaniel Baluta 			"channels_min: %d channels_max: %d\n",
756a4eff5f8SDaniel Baluta 			channels->min, channels->max);
757a4eff5f8SDaniel Baluta 		break;
758d88cbd6fSGuido Roncarolo 	case SOF_DAI_IMX_SAI:
75951b0243aSDaniel Baluta 		rate->min = dai->dai_config->sai.fsync_rate;
76051b0243aSDaniel Baluta 		rate->max = dai->dai_config->sai.fsync_rate;
761d88cbd6fSGuido Roncarolo 		channels->min = dai->dai_config->sai.tdm_slots;
762d88cbd6fSGuido Roncarolo 		channels->max = dai->dai_config->sai.tdm_slots;
763d88cbd6fSGuido Roncarolo 
764d88cbd6fSGuido Roncarolo 		dev_dbg(component->dev,
76551b0243aSDaniel Baluta 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
76651b0243aSDaniel Baluta 		dev_dbg(component->dev,
767d88cbd6fSGuido Roncarolo 			"channels_min: %d channels_max: %d\n",
768d88cbd6fSGuido Roncarolo 			channels->min, channels->max);
769d88cbd6fSGuido Roncarolo 		break;
770868bd00fSLiam Girdwood 	default:
771ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: invalid DAI type %d\n",
772868bd00fSLiam Girdwood 			dai->dai_config->type);
773868bd00fSLiam Girdwood 		break;
774868bd00fSLiam Girdwood 	}
775868bd00fSLiam Girdwood 
776868bd00fSLiam Girdwood 	return 0;
777868bd00fSLiam Girdwood }
778*f3f3af17SPierre-Louis Bossart EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
779868bd00fSLiam Girdwood 
780868bd00fSLiam Girdwood static int sof_pcm_probe(struct snd_soc_component *component)
781868bd00fSLiam Girdwood {
782868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
783868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
784868bd00fSLiam Girdwood 	const char *tplg_filename;
785868bd00fSLiam Girdwood 	int ret;
786868bd00fSLiam Girdwood 
787868bd00fSLiam Girdwood 	/* load the default topology */
788868bd00fSLiam Girdwood 	sdev->component = component;
789868bd00fSLiam Girdwood 
790868bd00fSLiam Girdwood 	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
791868bd00fSLiam Girdwood 				       "%s/%s",
792868bd00fSLiam Girdwood 				       plat_data->tplg_filename_prefix,
793868bd00fSLiam Girdwood 				       plat_data->tplg_filename);
794868bd00fSLiam Girdwood 	if (!tplg_filename)
795868bd00fSLiam Girdwood 		return -ENOMEM;
796868bd00fSLiam Girdwood 
797ee1e79b7SRanjani Sridharan 	ret = snd_sof_load_topology(component, tplg_filename);
798868bd00fSLiam Girdwood 	if (ret < 0) {
799ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: failed to load DSP topology %d\n",
800868bd00fSLiam Girdwood 			ret);
801868bd00fSLiam Girdwood 		return ret;
802868bd00fSLiam Girdwood 	}
803868bd00fSLiam Girdwood 
804868bd00fSLiam Girdwood 	return ret;
805868bd00fSLiam Girdwood }
806868bd00fSLiam Girdwood 
807868bd00fSLiam Girdwood static void sof_pcm_remove(struct snd_soc_component *component)
808868bd00fSLiam Girdwood {
809868bd00fSLiam Girdwood 	/* remove topology */
810a5b8f71cSAmadeusz Sławiński 	snd_soc_tplg_component_remove(component);
811868bd00fSLiam Girdwood }
812868bd00fSLiam Girdwood 
813868bd00fSLiam Girdwood void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
814868bd00fSLiam Girdwood {
815868bd00fSLiam Girdwood 	struct snd_soc_component_driver *pd = &sdev->plat_drv;
816868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
817868bd00fSLiam Girdwood 	const char *drv_name;
818868bd00fSLiam Girdwood 
819868bd00fSLiam Girdwood 	drv_name = plat_data->machine->drv_name;
820868bd00fSLiam Girdwood 
821868bd00fSLiam Girdwood 	pd->name = "sof-audio-component";
822868bd00fSLiam Girdwood 	pd->probe = sof_pcm_probe;
823868bd00fSLiam Girdwood 	pd->remove = sof_pcm_remove;
8241c91d77eSKuninori Morimoto 	pd->open = sof_pcm_open;
8251c91d77eSKuninori Morimoto 	pd->close = sof_pcm_close;
8261c91d77eSKuninori Morimoto 	pd->hw_params = sof_pcm_hw_params;
8271c91d77eSKuninori Morimoto 	pd->prepare = sof_pcm_prepare;
8281c91d77eSKuninori Morimoto 	pd->hw_free = sof_pcm_hw_free;
8291c91d77eSKuninori Morimoto 	pd->trigger = sof_pcm_trigger;
8301c91d77eSKuninori Morimoto 	pd->pointer = sof_pcm_pointer;
8311c91d77eSKuninori Morimoto 
832868bd00fSLiam Girdwood #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
83339118ce5SKuninori Morimoto 	pd->compress_ops = &sof_compressed_ops;
834868bd00fSLiam Girdwood #endif
83570368106SCezary Rojewski #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
83670368106SCezary Rojewski 	/* override cops when probe support is enabled */
83739118ce5SKuninori Morimoto 	pd->compress_ops = &sof_probe_compressed_ops;
83870368106SCezary Rojewski #endif
8391c91d77eSKuninori Morimoto 	pd->pcm_construct = sof_pcm_new;
840868bd00fSLiam Girdwood 	pd->ignore_machine = drv_name;
841868bd00fSLiam Girdwood 	pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
842868bd00fSLiam Girdwood 	pd->be_pcm_base = SOF_BE_PCM_BASE;
843868bd00fSLiam Girdwood 	pd->use_dai_pcm_id = true;
844868bd00fSLiam Girdwood 	pd->topology_name_prefix = "sof";
845868bd00fSLiam Girdwood 
846868bd00fSLiam Girdwood 	 /* increment module refcount when a pcm is opened */
847868bd00fSLiam Girdwood 	pd->module_get_upon_open = 1;
848868bd00fSLiam Girdwood }
849