xref: /openbmc/linux/sound/soc/sof/pcm.c (revision ac8c046f19f94ec419c60d7a073af75a71386e97)
1868bd00fSLiam Girdwood // SPDX-License-Identifier: (GPL-2.0 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"
17868bd00fSLiam Girdwood #include "ops.h"
18868bd00fSLiam Girdwood 
19868bd00fSLiam Girdwood #define DRV_NAME	"sof-audio-component"
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 {
26868bd00fSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
27868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
28868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
29868bd00fSLiam Girdwood 	struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
30868bd00fSLiam Girdwood 	int stream = substream->stream;
31868bd00fSLiam Girdwood 
32868bd00fSLiam Girdwood 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
33868bd00fSLiam Girdwood 	if (!spcm)
34868bd00fSLiam Girdwood 		return -EINVAL;
35868bd00fSLiam Girdwood 
36868bd00fSLiam Girdwood 	return snd_sof_create_page_table(sdev, dmab,
37868bd00fSLiam Girdwood 		spcm->stream[stream].page_table.area, size);
38868bd00fSLiam Girdwood }
39868bd00fSLiam Girdwood 
40868bd00fSLiam Girdwood static int sof_pcm_dsp_params(struct snd_sof_pcm *spcm, struct snd_pcm_substream *substream,
41868bd00fSLiam Girdwood 			      const struct sof_ipc_pcm_params_reply *reply)
42868bd00fSLiam Girdwood {
43868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = spcm->sdev;
44868bd00fSLiam Girdwood 	/* validate offset */
45868bd00fSLiam Girdwood 	int ret = snd_sof_ipc_pcm_params(sdev, substream, reply);
46868bd00fSLiam Girdwood 
47868bd00fSLiam Girdwood 	if (ret < 0)
48868bd00fSLiam Girdwood 		dev_err(sdev->dev, "error: got wrong reply for PCM %d\n",
49868bd00fSLiam Girdwood 			spcm->pcm.pcm_id);
50868bd00fSLiam Girdwood 
51868bd00fSLiam Girdwood 	return ret;
52868bd00fSLiam Girdwood }
53868bd00fSLiam Girdwood 
54e2803e61SKeyon Jie /*
55e2803e61SKeyon Jie  * sof pcm period elapse work
56e2803e61SKeyon Jie  */
57e2803e61SKeyon Jie static void sof_pcm_period_elapsed_work(struct work_struct *work)
58e2803e61SKeyon Jie {
59e2803e61SKeyon Jie 	struct snd_sof_pcm_stream *sps =
60e2803e61SKeyon Jie 		container_of(work, struct snd_sof_pcm_stream,
61e2803e61SKeyon Jie 			     period_elapsed_work);
62e2803e61SKeyon Jie 
63e2803e61SKeyon Jie 	snd_pcm_period_elapsed(sps->substream);
64e2803e61SKeyon Jie }
65e2803e61SKeyon Jie 
66e2803e61SKeyon Jie /*
67e2803e61SKeyon Jie  * sof pcm period elapse, this could be called at irq thread context.
68e2803e61SKeyon Jie  */
69e2803e61SKeyon Jie void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
70e2803e61SKeyon Jie {
71e2803e61SKeyon Jie 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
72e2803e61SKeyon Jie 	struct snd_soc_component *component =
73e2803e61SKeyon Jie 		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
74e2803e61SKeyon Jie 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
75e2803e61SKeyon Jie 	struct snd_sof_pcm *spcm;
76e2803e61SKeyon Jie 
77e2803e61SKeyon Jie 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
78e2803e61SKeyon Jie 	if (!spcm) {
79e2803e61SKeyon Jie 		dev_err(sdev->dev,
80e2803e61SKeyon Jie 			"error: period elapsed for unknown stream!\n");
81e2803e61SKeyon Jie 		return;
82e2803e61SKeyon Jie 	}
83e2803e61SKeyon Jie 
84e2803e61SKeyon Jie 	/*
85e2803e61SKeyon Jie 	 * snd_pcm_period_elapsed() can be called in interrupt context
86e2803e61SKeyon Jie 	 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(),
87e2803e61SKeyon Jie 	 * when the PCM is done draining or xrun happened, a STOP IPC will
88e2803e61SKeyon Jie 	 * then be sent and this IPC will hit IPC timeout.
89e2803e61SKeyon Jie 	 * To avoid sending IPC before the previous IPC is handled, we
90e2803e61SKeyon Jie 	 * schedule delayed work here to call the snd_pcm_period_elapsed().
91e2803e61SKeyon Jie 	 */
92e2803e61SKeyon Jie 	schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
93e2803e61SKeyon Jie }
94e2803e61SKeyon Jie EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
95e2803e61SKeyon Jie 
96868bd00fSLiam Girdwood /* this may get called several times by oss emulation */
971c91d77eSKuninori Morimoto static int sof_pcm_hw_params(struct snd_soc_component *component,
981c91d77eSKuninori Morimoto 			     struct snd_pcm_substream *substream,
99868bd00fSLiam Girdwood 			     struct snd_pcm_hw_params *params)
100868bd00fSLiam Girdwood {
101868bd00fSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
102868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
103868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
104868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
105868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params pcm;
106868bd00fSLiam Girdwood 	struct sof_ipc_pcm_params_reply ipc_params_reply;
107868bd00fSLiam Girdwood 	int ret;
108868bd00fSLiam Girdwood 
109868bd00fSLiam Girdwood 	/* nothing to do for BE */
110868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
111868bd00fSLiam Girdwood 		return 0;
112868bd00fSLiam Girdwood 
113868bd00fSLiam Girdwood 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
114868bd00fSLiam Girdwood 	if (!spcm)
115868bd00fSLiam Girdwood 		return -EINVAL;
116868bd00fSLiam Girdwood 
117868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "pcm: hw params stream %d dir %d\n",
118868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream);
119868bd00fSLiam Girdwood 
120868bd00fSLiam Girdwood 	memset(&pcm, 0, sizeof(pcm));
121868bd00fSLiam Girdwood 
122868bd00fSLiam Girdwood 	/* allocate audio buffer pages */
123868bd00fSLiam Girdwood 	ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
124868bd00fSLiam Girdwood 	if (ret < 0) {
125868bd00fSLiam Girdwood 		dev_err(sdev->dev, "error: could not allocate %d bytes for PCM %d\n",
126868bd00fSLiam Girdwood 			params_buffer_bytes(params), spcm->pcm.pcm_id);
127868bd00fSLiam Girdwood 		return ret;
128868bd00fSLiam Girdwood 	}
129868bd00fSLiam Girdwood 	if (ret) {
130868bd00fSLiam Girdwood 		/*
131868bd00fSLiam Girdwood 		 * ret == 1 means the buffer is changed
132868bd00fSLiam Girdwood 		 * create compressed page table for audio firmware
133868bd00fSLiam Girdwood 		 * ret == 0 means the buffer is not changed
134868bd00fSLiam Girdwood 		 * so no need to regenerate the page table
135868bd00fSLiam Girdwood 		 */
1361c91d77eSKuninori Morimoto 		ret = create_page_table(component, substream, runtime->dma_area,
137868bd00fSLiam Girdwood 					runtime->dma_bytes);
138868bd00fSLiam Girdwood 		if (ret < 0)
139868bd00fSLiam Girdwood 			return ret;
140868bd00fSLiam Girdwood 	}
141868bd00fSLiam Girdwood 
142868bd00fSLiam Girdwood 	/* number of pages should be rounded up */
143868bd00fSLiam Girdwood 	pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
144868bd00fSLiam Girdwood 
145868bd00fSLiam Girdwood 	/* set IPC PCM parameters */
146868bd00fSLiam Girdwood 	pcm.hdr.size = sizeof(pcm);
147868bd00fSLiam Girdwood 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
148868bd00fSLiam Girdwood 	pcm.comp_id = spcm->stream[substream->stream].comp_id;
149868bd00fSLiam Girdwood 	pcm.params.hdr.size = sizeof(pcm.params);
150868bd00fSLiam Girdwood 	pcm.params.buffer.phy_addr =
151868bd00fSLiam Girdwood 		spcm->stream[substream->stream].page_table.addr;
152868bd00fSLiam Girdwood 	pcm.params.buffer.size = runtime->dma_bytes;
153868bd00fSLiam Girdwood 	pcm.params.direction = substream->stream;
154868bd00fSLiam Girdwood 	pcm.params.sample_valid_bytes = params_width(params) >> 3;
155868bd00fSLiam Girdwood 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
156868bd00fSLiam Girdwood 	pcm.params.rate = params_rate(params);
157868bd00fSLiam Girdwood 	pcm.params.channels = params_channels(params);
158868bd00fSLiam Girdwood 	pcm.params.host_period_bytes = params_period_bytes(params);
159868bd00fSLiam Girdwood 
160868bd00fSLiam Girdwood 	/* container size */
161868bd00fSLiam Girdwood 	ret = snd_pcm_format_physical_width(params_format(params));
162868bd00fSLiam Girdwood 	if (ret < 0)
163868bd00fSLiam Girdwood 		return ret;
164868bd00fSLiam Girdwood 	pcm.params.sample_container_bytes = ret >> 3;
165868bd00fSLiam Girdwood 
166868bd00fSLiam Girdwood 	/* format */
167868bd00fSLiam Girdwood 	switch (params_format(params)) {
168868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S16:
169868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
170868bd00fSLiam Girdwood 		break;
171868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S24:
172868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
173868bd00fSLiam Girdwood 		break;
174868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_S32:
175868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
176868bd00fSLiam Girdwood 		break;
177868bd00fSLiam Girdwood 	case SNDRV_PCM_FORMAT_FLOAT:
178868bd00fSLiam Girdwood 		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
179868bd00fSLiam Girdwood 		break;
180868bd00fSLiam Girdwood 	default:
181868bd00fSLiam Girdwood 		return -EINVAL;
182868bd00fSLiam Girdwood 	}
183868bd00fSLiam Girdwood 
184868bd00fSLiam Girdwood 	/* firmware already configured host stream */
185868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_hw_params(sdev,
186868bd00fSLiam Girdwood 					     substream,
187868bd00fSLiam Girdwood 					     params,
188868bd00fSLiam Girdwood 					     &pcm.params);
189868bd00fSLiam Girdwood 	if (ret < 0) {
190868bd00fSLiam Girdwood 		dev_err(sdev->dev, "error: platform hw params failed\n");
191868bd00fSLiam Girdwood 		return ret;
192868bd00fSLiam Girdwood 	}
193868bd00fSLiam Girdwood 
194868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "stream_tag %d", pcm.params.stream_tag);
195868bd00fSLiam Girdwood 
196868bd00fSLiam Girdwood 	/* send IPC to the DSP */
197868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
198868bd00fSLiam Girdwood 				 &ipc_params_reply, sizeof(ipc_params_reply));
199868bd00fSLiam Girdwood 	if (ret < 0) {
200868bd00fSLiam Girdwood 		dev_err(sdev->dev, "error: hw params ipc failed for stream %d\n",
201868bd00fSLiam Girdwood 			pcm.params.stream_tag);
202868bd00fSLiam Girdwood 		return ret;
203868bd00fSLiam Girdwood 	}
204868bd00fSLiam Girdwood 
205868bd00fSLiam Girdwood 	ret = sof_pcm_dsp_params(spcm, substream, &ipc_params_reply);
206868bd00fSLiam Girdwood 	if (ret < 0)
207868bd00fSLiam Girdwood 		return ret;
208868bd00fSLiam Girdwood 
20904c80277SKai Vehmanen 	spcm->prepared[substream->stream] = true;
21004c80277SKai Vehmanen 
211868bd00fSLiam Girdwood 	/* save pcm hw_params */
212868bd00fSLiam Girdwood 	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
213868bd00fSLiam Girdwood 
214868bd00fSLiam Girdwood 	return ret;
215868bd00fSLiam Girdwood }
216868bd00fSLiam Girdwood 
217a49b6871SKai Vehmanen static int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream,
218a49b6871SKai Vehmanen 				struct snd_sof_dev *sdev,
219a49b6871SKai Vehmanen 				struct snd_sof_pcm *spcm)
220a49b6871SKai Vehmanen {
221a49b6871SKai Vehmanen 	struct sof_ipc_stream stream;
222a49b6871SKai Vehmanen 	struct sof_ipc_reply reply;
223a49b6871SKai Vehmanen 	int ret;
224a49b6871SKai Vehmanen 
225a49b6871SKai Vehmanen 	stream.hdr.size = sizeof(stream);
226a49b6871SKai Vehmanen 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
227a49b6871SKai Vehmanen 	stream.comp_id = spcm->stream[substream->stream].comp_id;
228a49b6871SKai Vehmanen 
229a49b6871SKai Vehmanen 	/* send IPC to the DSP */
230a49b6871SKai Vehmanen 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
231a49b6871SKai Vehmanen 				 sizeof(stream), &reply, sizeof(reply));
232a49b6871SKai Vehmanen 	if (!ret)
233a49b6871SKai Vehmanen 		spcm->prepared[substream->stream] = false;
234a49b6871SKai Vehmanen 
235a49b6871SKai Vehmanen 	return ret;
236a49b6871SKai Vehmanen }
237a49b6871SKai Vehmanen 
2381c91d77eSKuninori Morimoto static int sof_pcm_hw_free(struct snd_soc_component *component,
2391c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
240868bd00fSLiam Girdwood {
241868bd00fSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
242868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
243868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
244e66e52c5SKai Vehmanen 	int ret, err = 0;
245868bd00fSLiam Girdwood 
246868bd00fSLiam Girdwood 	/* nothing to do for BE */
247868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
248868bd00fSLiam Girdwood 		return 0;
249868bd00fSLiam Girdwood 
250868bd00fSLiam Girdwood 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
251868bd00fSLiam Girdwood 	if (!spcm)
252868bd00fSLiam Girdwood 		return -EINVAL;
253868bd00fSLiam Girdwood 
254868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "pcm: free stream %d dir %d\n", spcm->pcm.pcm_id,
255868bd00fSLiam Girdwood 		substream->stream);
256868bd00fSLiam Girdwood 
257e66e52c5SKai Vehmanen 	if (spcm->prepared[substream->stream]) {
258a49b6871SKai Vehmanen 		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
259e66e52c5SKai Vehmanen 		if (ret < 0)
260e66e52c5SKai Vehmanen 			err = ret;
261e66e52c5SKai Vehmanen 	}
262868bd00fSLiam Girdwood 
263868bd00fSLiam Girdwood 	snd_pcm_lib_free_pages(substream);
264e2803e61SKeyon Jie 
265e2803e61SKeyon Jie 	cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
266e2803e61SKeyon Jie 
26793146bc2SRanjani Sridharan 	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
268e66e52c5SKai Vehmanen 	if (ret < 0) {
26993146bc2SRanjani Sridharan 		dev_err(sdev->dev, "error: platform hw free failed\n");
270e66e52c5SKai Vehmanen 		err = ret;
271e66e52c5SKai Vehmanen 	}
27293146bc2SRanjani Sridharan 
273e66e52c5SKai Vehmanen 	return err;
274868bd00fSLiam Girdwood }
275868bd00fSLiam Girdwood 
2761c91d77eSKuninori Morimoto static int sof_pcm_prepare(struct snd_soc_component *component,
2771c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
278868bd00fSLiam Girdwood {
279868bd00fSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
280868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
281868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
282868bd00fSLiam Girdwood 	int ret;
283868bd00fSLiam Girdwood 
284868bd00fSLiam Girdwood 	/* nothing to do for BE */
285868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
286868bd00fSLiam Girdwood 		return 0;
287868bd00fSLiam Girdwood 
288868bd00fSLiam Girdwood 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
289868bd00fSLiam Girdwood 	if (!spcm)
290868bd00fSLiam Girdwood 		return -EINVAL;
291868bd00fSLiam Girdwood 
29204c80277SKai Vehmanen 	if (spcm->prepared[substream->stream])
293868bd00fSLiam Girdwood 		return 0;
294868bd00fSLiam Girdwood 
295868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "pcm: prepare stream %d dir %d\n", spcm->pcm.pcm_id,
296868bd00fSLiam Girdwood 		substream->stream);
297868bd00fSLiam Girdwood 
298868bd00fSLiam Girdwood 	/* set hw_params */
2991c91d77eSKuninori Morimoto 	ret = sof_pcm_hw_params(component,
3001c91d77eSKuninori Morimoto 				substream, &spcm->params[substream->stream]);
301868bd00fSLiam Girdwood 	if (ret < 0) {
302868bd00fSLiam Girdwood 		dev_err(sdev->dev, "error: set pcm hw_params after resume\n");
303868bd00fSLiam Girdwood 		return ret;
304868bd00fSLiam Girdwood 	}
305868bd00fSLiam Girdwood 
306868bd00fSLiam Girdwood 	return 0;
307868bd00fSLiam Girdwood }
308868bd00fSLiam Girdwood 
309868bd00fSLiam Girdwood /*
310868bd00fSLiam Girdwood  * FE dai link trigger actions are always executed in non-atomic context because
311868bd00fSLiam Girdwood  * they involve IPC's.
312868bd00fSLiam Girdwood  */
3131c91d77eSKuninori Morimoto static int sof_pcm_trigger(struct snd_soc_component *component,
3141c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream, int cmd)
315868bd00fSLiam Girdwood {
316868bd00fSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
317868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
318868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
319868bd00fSLiam Girdwood 	struct sof_ipc_stream stream;
320868bd00fSLiam Girdwood 	struct sof_ipc_reply reply;
32104c80277SKai Vehmanen 	bool reset_hw_params = false;
3220a1b0834SPan Xiuli 	bool ipc_first = false;
323868bd00fSLiam Girdwood 	int ret;
324868bd00fSLiam Girdwood 
325868bd00fSLiam Girdwood 	/* nothing to do for BE */
326868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
327868bd00fSLiam Girdwood 		return 0;
328868bd00fSLiam Girdwood 
329868bd00fSLiam Girdwood 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
330868bd00fSLiam Girdwood 	if (!spcm)
331868bd00fSLiam Girdwood 		return -EINVAL;
332868bd00fSLiam Girdwood 
333868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "pcm: trigger stream %d dir %d cmd %d\n",
334868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream, cmd);
335868bd00fSLiam Girdwood 
336868bd00fSLiam Girdwood 	stream.hdr.size = sizeof(stream);
337868bd00fSLiam Girdwood 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
338868bd00fSLiam Girdwood 	stream.comp_id = spcm->stream[substream->stream].comp_id;
339868bd00fSLiam Girdwood 
340868bd00fSLiam Girdwood 	switch (cmd) {
341868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
342868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
3430a1b0834SPan Xiuli 		ipc_first = true;
344868bd00fSLiam Girdwood 		break;
345868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
346868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
347868bd00fSLiam Girdwood 		break;
348868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
349*ac8c046fSKeyon Jie 		if (spcm->stream[substream->stream].suspend_ignored) {
350*ac8c046fSKeyon Jie 			/*
351*ac8c046fSKeyon Jie 			 * this case will be triggered when INFO_RESUME is
352*ac8c046fSKeyon Jie 			 * supported, no need to resume streams that remained
353*ac8c046fSKeyon Jie 			 * enabled in D0ix.
354*ac8c046fSKeyon Jie 			 */
355*ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = false;
356*ac8c046fSKeyon Jie 			return 0;
357*ac8c046fSKeyon Jie 		}
358*ac8c046fSKeyon Jie 
359868bd00fSLiam Girdwood 		/* set up hw_params */
3601c91d77eSKuninori Morimoto 		ret = sof_pcm_prepare(component, substream);
361868bd00fSLiam Girdwood 		if (ret < 0) {
362868bd00fSLiam Girdwood 			dev_err(sdev->dev,
363868bd00fSLiam Girdwood 				"error: failed to set up hw_params upon resume\n");
364868bd00fSLiam Girdwood 			return ret;
365868bd00fSLiam Girdwood 		}
366868bd00fSLiam Girdwood 
367868bd00fSLiam Girdwood 		/* fallthrough */
368868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
369*ac8c046fSKeyon Jie 		if (spcm->stream[substream->stream].suspend_ignored) {
370*ac8c046fSKeyon Jie 			/*
371*ac8c046fSKeyon Jie 			 * This case will be triggered when INFO_RESUME is
372*ac8c046fSKeyon Jie 			 * not supported, no need to re-start streams that
373*ac8c046fSKeyon Jie 			 * remained enabled in D0ix.
374*ac8c046fSKeyon Jie 			 */
375*ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = false;
376*ac8c046fSKeyon Jie 			return 0;
377*ac8c046fSKeyon Jie 		}
378868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
379868bd00fSLiam Girdwood 		break;
380868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
381*ac8c046fSKeyon Jie 		if (sdev->s0_suspend &&
382*ac8c046fSKeyon Jie 		    spcm->stream[substream->stream].d0i3_compatible) {
383*ac8c046fSKeyon Jie 			/*
384*ac8c046fSKeyon Jie 			 * trap the event, not sending trigger stop to
385*ac8c046fSKeyon Jie 			 * prevent the FW pipelines from being stopped,
386*ac8c046fSKeyon Jie 			 * and mark the flag to ignore the upcoming DAPM
387*ac8c046fSKeyon Jie 			 * PM events.
388*ac8c046fSKeyon Jie 			 */
389*ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = true;
390*ac8c046fSKeyon Jie 			return 0;
391*ac8c046fSKeyon Jie 		}
392*ac8c046fSKeyon Jie 		/* fallthrough */
393868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
394868bd00fSLiam Girdwood 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
3950a1b0834SPan Xiuli 		ipc_first = true;
39604c80277SKai Vehmanen 		reset_hw_params = true;
397868bd00fSLiam Girdwood 		break;
398868bd00fSLiam Girdwood 	default:
399868bd00fSLiam Girdwood 		dev_err(sdev->dev, "error: unhandled trigger cmd %d\n", cmd);
400868bd00fSLiam Girdwood 		return -EINVAL;
401868bd00fSLiam Girdwood 	}
402868bd00fSLiam Girdwood 
4030a1b0834SPan Xiuli 	/*
4040a1b0834SPan Xiuli 	 * DMA and IPC sequence is different for start and stop. Need to send
4050a1b0834SPan Xiuli 	 * STOP IPC before stop DMA
4060a1b0834SPan Xiuli 	 */
4070a1b0834SPan Xiuli 	if (!ipc_first)
408868bd00fSLiam Girdwood 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
409868bd00fSLiam Girdwood 
410868bd00fSLiam Girdwood 	/* send IPC to the DSP */
411868bd00fSLiam Girdwood 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
412868bd00fSLiam Girdwood 				 sizeof(stream), &reply, sizeof(reply));
413868bd00fSLiam Girdwood 
4140a1b0834SPan Xiuli 	/* need to STOP DMA even if STOP IPC failed */
4150a1b0834SPan Xiuli 	if (ipc_first)
4160a1b0834SPan Xiuli 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
4170a1b0834SPan Xiuli 
4180a1b0834SPan Xiuli 	/* free PCM if reset_hw_params is set and the STOP IPC is successful */
419a49b6871SKai Vehmanen 	if (!ret && reset_hw_params)
420a49b6871SKai Vehmanen 		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
421a49b6871SKai Vehmanen 
422868bd00fSLiam Girdwood 	return ret;
423868bd00fSLiam Girdwood }
424868bd00fSLiam Girdwood 
4251c91d77eSKuninori Morimoto static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
4261c91d77eSKuninori Morimoto 					 struct snd_pcm_substream *substream)
427868bd00fSLiam Girdwood {
428868bd00fSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
429868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
430868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
431868bd00fSLiam Girdwood 	snd_pcm_uframes_t host, dai;
432868bd00fSLiam Girdwood 
433868bd00fSLiam Girdwood 	/* nothing to do for BE */
434868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
435868bd00fSLiam Girdwood 		return 0;
436868bd00fSLiam Girdwood 
437868bd00fSLiam Girdwood 	/* use dsp ops pointer callback directly if set */
438868bd00fSLiam Girdwood 	if (sof_ops(sdev)->pcm_pointer)
439868bd00fSLiam Girdwood 		return sof_ops(sdev)->pcm_pointer(sdev, substream);
440868bd00fSLiam Girdwood 
441868bd00fSLiam Girdwood 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
442868bd00fSLiam Girdwood 	if (!spcm)
443868bd00fSLiam Girdwood 		return -EINVAL;
444868bd00fSLiam Girdwood 
445868bd00fSLiam Girdwood 	/* read position from DSP */
446868bd00fSLiam Girdwood 	host = bytes_to_frames(substream->runtime,
447868bd00fSLiam Girdwood 			       spcm->stream[substream->stream].posn.host_posn);
448868bd00fSLiam Girdwood 	dai = bytes_to_frames(substream->runtime,
449868bd00fSLiam Girdwood 			      spcm->stream[substream->stream].posn.dai_posn);
450868bd00fSLiam Girdwood 
451868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
452868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream, host, dai);
453868bd00fSLiam Girdwood 
454868bd00fSLiam Girdwood 	return host;
455868bd00fSLiam Girdwood }
456868bd00fSLiam Girdwood 
4571c91d77eSKuninori Morimoto #ifdef CONFIG_SND_DMA_SGBUF
4581c91d77eSKuninori Morimoto static struct page *sof_pcm_page(struct snd_soc_component *component,
4591c91d77eSKuninori Morimoto 				 struct snd_pcm_substream *substream,
4601c91d77eSKuninori Morimoto 				 unsigned long offset)
4611c91d77eSKuninori Morimoto {
4621c91d77eSKuninori Morimoto 	return snd_pcm_sgbuf_ops_page(substream, offset);
4631c91d77eSKuninori Morimoto }
4641c91d77eSKuninori Morimoto #else
4651c91d77eSKuninori Morimoto #define sof_pcm_page	NULL
4661c91d77eSKuninori Morimoto #endif /* CONFIG_SND_DMA_SGBUF */
4671c91d77eSKuninori Morimoto 
4681c91d77eSKuninori Morimoto static int sof_pcm_open(struct snd_soc_component *component,
4691c91d77eSKuninori Morimoto 			struct snd_pcm_substream *substream)
470868bd00fSLiam Girdwood {
471868bd00fSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
472868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
473868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
47427e322faSPierre-Louis Bossart 	const struct snd_sof_dsp_ops *ops = sof_ops(sdev);
475868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
476868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
477868bd00fSLiam Girdwood 	int ret;
478868bd00fSLiam Girdwood 
479868bd00fSLiam Girdwood 	/* nothing to do for BE */
480868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
481868bd00fSLiam Girdwood 		return 0;
482868bd00fSLiam Girdwood 
483868bd00fSLiam Girdwood 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
484868bd00fSLiam Girdwood 	if (!spcm)
485868bd00fSLiam Girdwood 		return -EINVAL;
486868bd00fSLiam Girdwood 
487868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "pcm: open stream %d dir %d\n", spcm->pcm.pcm_id,
488868bd00fSLiam Girdwood 		substream->stream);
489868bd00fSLiam Girdwood 
490fab4edf4SPierre-Louis Bossart 	INIT_WORK(&spcm->stream[substream->stream].period_elapsed_work,
491fab4edf4SPierre-Louis Bossart 		  sof_pcm_period_elapsed_work);
492868bd00fSLiam Girdwood 
493868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[substream->stream];
494868bd00fSLiam Girdwood 
495868bd00fSLiam Girdwood 	/* set any runtime constraints based on topology */
496868bd00fSLiam Girdwood 	snd_pcm_hw_constraint_step(substream->runtime, 0,
497868bd00fSLiam Girdwood 				   SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
498868bd00fSLiam Girdwood 				   le32_to_cpu(caps->period_size_min));
499868bd00fSLiam Girdwood 	snd_pcm_hw_constraint_step(substream->runtime, 0,
500868bd00fSLiam Girdwood 				   SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
501868bd00fSLiam Girdwood 				   le32_to_cpu(caps->period_size_min));
502868bd00fSLiam Girdwood 
503868bd00fSLiam Girdwood 	/* set runtime config */
50427e322faSPierre-Louis Bossart 	runtime->hw.info = ops->hw_info; /* platform-specific */
50527e322faSPierre-Louis Bossart 
506868bd00fSLiam Girdwood 	runtime->hw.formats = le64_to_cpu(caps->formats);
507868bd00fSLiam Girdwood 	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
508868bd00fSLiam Girdwood 	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
509868bd00fSLiam Girdwood 	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
510868bd00fSLiam Girdwood 	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
511868bd00fSLiam Girdwood 
512868bd00fSLiam Girdwood 	/*
513868bd00fSLiam Girdwood 	 * caps->buffer_size_min is not used since the
514868bd00fSLiam Girdwood 	 * snd_pcm_hardware structure only defines buffer_bytes_max
515868bd00fSLiam Girdwood 	 */
516868bd00fSLiam Girdwood 	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
517868bd00fSLiam Girdwood 
518868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "period min %zd max %zd bytes\n",
519868bd00fSLiam Girdwood 		runtime->hw.period_bytes_min,
520868bd00fSLiam Girdwood 		runtime->hw.period_bytes_max);
521868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "period count %d max %d\n",
522868bd00fSLiam Girdwood 		runtime->hw.periods_min,
523868bd00fSLiam Girdwood 		runtime->hw.periods_max);
524868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "buffer max %zd bytes\n",
525868bd00fSLiam Girdwood 		runtime->hw.buffer_bytes_max);
526868bd00fSLiam Girdwood 
527868bd00fSLiam Girdwood 	/* set wait time - TODO: come from topology */
528868bd00fSLiam Girdwood 	substream->wait_time = 500;
529868bd00fSLiam Girdwood 
530868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.host_posn = 0;
531868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.dai_posn = 0;
532868bd00fSLiam Girdwood 	spcm->stream[substream->stream].substream = substream;
53304c80277SKai Vehmanen 	spcm->prepared[substream->stream] = false;
534868bd00fSLiam Girdwood 
535868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_open(sdev, substream);
53614a2212dSRanjani Sridharan 	if (ret < 0)
53714a2212dSRanjani Sridharan 		dev_err(sdev->dev, "error: pcm open failed %d\n", ret);
538868bd00fSLiam Girdwood 
539868bd00fSLiam Girdwood 	return ret;
540868bd00fSLiam Girdwood }
541868bd00fSLiam Girdwood 
5421c91d77eSKuninori Morimoto static int sof_pcm_close(struct snd_soc_component *component,
5431c91d77eSKuninori Morimoto 			 struct snd_pcm_substream *substream)
544868bd00fSLiam Girdwood {
545868bd00fSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
546868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
547868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
548868bd00fSLiam Girdwood 	int err;
549868bd00fSLiam Girdwood 
550868bd00fSLiam Girdwood 	/* nothing to do for BE */
551868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
552868bd00fSLiam Girdwood 		return 0;
553868bd00fSLiam Girdwood 
554868bd00fSLiam Girdwood 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
555868bd00fSLiam Girdwood 	if (!spcm)
556868bd00fSLiam Girdwood 		return -EINVAL;
557868bd00fSLiam Girdwood 
558868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "pcm: close stream %d dir %d\n", spcm->pcm.pcm_id,
559868bd00fSLiam Girdwood 		substream->stream);
560868bd00fSLiam Girdwood 
561868bd00fSLiam Girdwood 	err = snd_sof_pcm_platform_close(sdev, substream);
562868bd00fSLiam Girdwood 	if (err < 0) {
563868bd00fSLiam Girdwood 		dev_err(sdev->dev, "error: pcm close failed %d\n",
564868bd00fSLiam Girdwood 			err);
565868bd00fSLiam Girdwood 		/*
566868bd00fSLiam Girdwood 		 * keep going, no point in preventing the close
567868bd00fSLiam Girdwood 		 * from happening
568868bd00fSLiam Girdwood 		 */
569868bd00fSLiam Girdwood 	}
570868bd00fSLiam Girdwood 
571868bd00fSLiam Girdwood 	return 0;
572868bd00fSLiam Girdwood }
573868bd00fSLiam Girdwood 
574868bd00fSLiam Girdwood /*
575868bd00fSLiam Girdwood  * Pre-allocate playback/capture audio buffer pages.
576868bd00fSLiam Girdwood  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
577868bd00fSLiam Girdwood  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
578868bd00fSLiam Girdwood  */
5791c91d77eSKuninori Morimoto static int sof_pcm_new(struct snd_soc_component *component,
5801c91d77eSKuninori Morimoto 		       struct snd_soc_pcm_runtime *rtd)
581868bd00fSLiam Girdwood {
582868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
583868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
584868bd00fSLiam Girdwood 	struct snd_pcm *pcm = rtd->pcm;
585868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
586868bd00fSLiam Girdwood 	int stream = SNDRV_PCM_STREAM_PLAYBACK;
587868bd00fSLiam Girdwood 
588868bd00fSLiam Girdwood 	/* find SOF PCM for this RTD */
589868bd00fSLiam Girdwood 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
590868bd00fSLiam Girdwood 	if (!spcm) {
591868bd00fSLiam Girdwood 		dev_warn(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
592868bd00fSLiam Girdwood 			 rtd->dai_link->id);
593868bd00fSLiam Girdwood 		return 0;
594868bd00fSLiam Girdwood 	}
595868bd00fSLiam Girdwood 
596868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
597868bd00fSLiam Girdwood 
598868bd00fSLiam Girdwood 	/* do we need to pre-allocate playback audio buffer pages */
599868bd00fSLiam Girdwood 	if (!spcm->pcm.playback)
600868bd00fSLiam Girdwood 		goto capture;
601868bd00fSLiam Girdwood 
602868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
603868bd00fSLiam Girdwood 
604868bd00fSLiam Girdwood 	/* pre-allocate playback audio buffer pages */
605868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
606868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
607868bd00fSLiam Girdwood 
608868bd00fSLiam Girdwood 	snd_pcm_lib_preallocate_pages(pcm->streams[stream].substream,
609868bd00fSLiam Girdwood 				      SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
610868bd00fSLiam Girdwood 				      le32_to_cpu(caps->buffer_size_min),
611868bd00fSLiam Girdwood 				      le32_to_cpu(caps->buffer_size_max));
612868bd00fSLiam Girdwood capture:
613868bd00fSLiam Girdwood 	stream = SNDRV_PCM_STREAM_CAPTURE;
614868bd00fSLiam Girdwood 
615868bd00fSLiam Girdwood 	/* do we need to pre-allocate capture audio buffer pages */
616868bd00fSLiam Girdwood 	if (!spcm->pcm.capture)
617868bd00fSLiam Girdwood 		return 0;
618868bd00fSLiam Girdwood 
619868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
620868bd00fSLiam Girdwood 
621868bd00fSLiam Girdwood 	/* pre-allocate capture audio buffer pages */
622868bd00fSLiam Girdwood 	dev_dbg(sdev->dev, "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
623868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
624868bd00fSLiam Girdwood 
625868bd00fSLiam Girdwood 	snd_pcm_lib_preallocate_pages(pcm->streams[stream].substream,
626868bd00fSLiam Girdwood 				      SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
627868bd00fSLiam Girdwood 				      le32_to_cpu(caps->buffer_size_min),
628868bd00fSLiam Girdwood 				      le32_to_cpu(caps->buffer_size_max));
629868bd00fSLiam Girdwood 
630868bd00fSLiam Girdwood 	return 0;
631868bd00fSLiam Girdwood }
632868bd00fSLiam Girdwood 
633868bd00fSLiam Girdwood /* fixup the BE DAI link to match any values from topology */
634868bd00fSLiam Girdwood static int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
635868bd00fSLiam Girdwood 				  struct snd_pcm_hw_params *params)
636868bd00fSLiam Girdwood {
637868bd00fSLiam Girdwood 	struct snd_interval *rate = hw_param_interval(params,
638868bd00fSLiam Girdwood 			SNDRV_PCM_HW_PARAM_RATE);
639868bd00fSLiam Girdwood 	struct snd_interval *channels = hw_param_interval(params,
640868bd00fSLiam Girdwood 						SNDRV_PCM_HW_PARAM_CHANNELS);
641868bd00fSLiam Girdwood 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
642868bd00fSLiam Girdwood 	struct snd_soc_component *component =
643868bd00fSLiam Girdwood 		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
644868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
645868bd00fSLiam Girdwood 	struct snd_sof_dai *dai =
646868bd00fSLiam Girdwood 		snd_sof_find_dai(sdev, (char *)rtd->dai_link->name);
647868bd00fSLiam Girdwood 
648868bd00fSLiam Girdwood 	/* no topology exists for this BE, try a common configuration */
649868bd00fSLiam Girdwood 	if (!dai) {
650868bd00fSLiam Girdwood 		dev_warn(sdev->dev, "warning: no topology found for BE DAI %s config\n",
651868bd00fSLiam Girdwood 			 rtd->dai_link->name);
652868bd00fSLiam Girdwood 
653868bd00fSLiam Girdwood 		/*  set 48k, stereo, 16bits by default */
654868bd00fSLiam Girdwood 		rate->min = 48000;
655868bd00fSLiam Girdwood 		rate->max = 48000;
656868bd00fSLiam Girdwood 
657868bd00fSLiam Girdwood 		channels->min = 2;
658868bd00fSLiam Girdwood 		channels->max = 2;
659868bd00fSLiam Girdwood 
660868bd00fSLiam Girdwood 		snd_mask_none(fmt);
661868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
662868bd00fSLiam Girdwood 
663868bd00fSLiam Girdwood 		return 0;
664868bd00fSLiam Girdwood 	}
665868bd00fSLiam Girdwood 
666868bd00fSLiam Girdwood 	/* read format from topology */
667868bd00fSLiam Girdwood 	snd_mask_none(fmt);
668868bd00fSLiam Girdwood 
669868bd00fSLiam Girdwood 	switch (dai->comp_dai.config.frame_fmt) {
670868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S16_LE:
671868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
672868bd00fSLiam Girdwood 		break;
673868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S24_4LE:
674868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
675868bd00fSLiam Girdwood 		break;
676868bd00fSLiam Girdwood 	case SOF_IPC_FRAME_S32_LE:
677868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
678868bd00fSLiam Girdwood 		break;
679868bd00fSLiam Girdwood 	default:
680868bd00fSLiam Girdwood 		dev_err(sdev->dev, "error: No available DAI format!\n");
681868bd00fSLiam Girdwood 		return -EINVAL;
682868bd00fSLiam Girdwood 	}
683868bd00fSLiam Girdwood 
684868bd00fSLiam Girdwood 	/* read rate and channels from topology */
685868bd00fSLiam Girdwood 	switch (dai->dai_config->type) {
686868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_SSP:
687868bd00fSLiam Girdwood 		rate->min = dai->dai_config->ssp.fsync_rate;
688868bd00fSLiam Girdwood 		rate->max = dai->dai_config->ssp.fsync_rate;
689868bd00fSLiam Girdwood 		channels->min = dai->dai_config->ssp.tdm_slots;
690868bd00fSLiam Girdwood 		channels->max = dai->dai_config->ssp.tdm_slots;
691868bd00fSLiam Girdwood 
692868bd00fSLiam Girdwood 		dev_dbg(sdev->dev,
693868bd00fSLiam Girdwood 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
694868bd00fSLiam Girdwood 		dev_dbg(sdev->dev,
695868bd00fSLiam Girdwood 			"channels_min: %d channels_max: %d\n",
696868bd00fSLiam Girdwood 			channels->min, channels->max);
697868bd00fSLiam Girdwood 
698868bd00fSLiam Girdwood 		break;
699868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_DMIC:
700868bd00fSLiam Girdwood 		/* DMIC only supports 16 or 32 bit formats */
701868bd00fSLiam Girdwood 		if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
702868bd00fSLiam Girdwood 			dev_err(sdev->dev,
703868bd00fSLiam Girdwood 				"error: invalid fmt %d for DAI type %d\n",
704868bd00fSLiam Girdwood 				dai->comp_dai.config.frame_fmt,
705868bd00fSLiam Girdwood 				dai->dai_config->type);
706868bd00fSLiam Girdwood 		}
707868bd00fSLiam Girdwood 		break;
708868bd00fSLiam Girdwood 	case SOF_DAI_INTEL_HDA:
709868bd00fSLiam Girdwood 		/* do nothing for HDA dai_link */
710868bd00fSLiam Girdwood 		break;
7116e3360cdSPierre-Louis Bossart 	case SOF_DAI_INTEL_ALH:
7126e3360cdSPierre-Louis Bossart 		/* do nothing for ALH dai_link */
7136e3360cdSPierre-Louis Bossart 		break;
714a4eff5f8SDaniel Baluta 	case SOF_DAI_IMX_ESAI:
715a4eff5f8SDaniel Baluta 		channels->min = dai->dai_config->esai.tdm_slots;
716a4eff5f8SDaniel Baluta 		channels->max = dai->dai_config->esai.tdm_slots;
717a4eff5f8SDaniel Baluta 
718a4eff5f8SDaniel Baluta 		dev_dbg(sdev->dev,
719a4eff5f8SDaniel Baluta 			"channels_min: %d channels_max: %d\n",
720a4eff5f8SDaniel Baluta 			channels->min, channels->max);
721a4eff5f8SDaniel Baluta 		break;
722868bd00fSLiam Girdwood 	default:
723868bd00fSLiam Girdwood 		dev_err(sdev->dev, "error: invalid DAI type %d\n",
724868bd00fSLiam Girdwood 			dai->dai_config->type);
725868bd00fSLiam Girdwood 		break;
726868bd00fSLiam Girdwood 	}
727868bd00fSLiam Girdwood 
728868bd00fSLiam Girdwood 	return 0;
729868bd00fSLiam Girdwood }
730868bd00fSLiam Girdwood 
731868bd00fSLiam Girdwood static int sof_pcm_probe(struct snd_soc_component *component)
732868bd00fSLiam Girdwood {
733868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
734868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
735868bd00fSLiam Girdwood 	const char *tplg_filename;
736868bd00fSLiam Girdwood 	int ret;
737868bd00fSLiam Girdwood 
738868bd00fSLiam Girdwood 	/* load the default topology */
739868bd00fSLiam Girdwood 	sdev->component = component;
740868bd00fSLiam Girdwood 
741868bd00fSLiam Girdwood 	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
742868bd00fSLiam Girdwood 				       "%s/%s",
743868bd00fSLiam Girdwood 				       plat_data->tplg_filename_prefix,
744868bd00fSLiam Girdwood 				       plat_data->tplg_filename);
745868bd00fSLiam Girdwood 	if (!tplg_filename)
746868bd00fSLiam Girdwood 		return -ENOMEM;
747868bd00fSLiam Girdwood 
748868bd00fSLiam Girdwood 	ret = snd_sof_load_topology(sdev, tplg_filename);
749868bd00fSLiam Girdwood 	if (ret < 0) {
750868bd00fSLiam Girdwood 		dev_err(sdev->dev, "error: failed to load DSP topology %d\n",
751868bd00fSLiam Girdwood 			ret);
752868bd00fSLiam Girdwood 		return ret;
753868bd00fSLiam Girdwood 	}
754868bd00fSLiam Girdwood 
755868bd00fSLiam Girdwood 	/*
756868bd00fSLiam Girdwood 	 * Some platforms in SOF, ex: BYT, may not have their platform PM
757868bd00fSLiam Girdwood 	 * callbacks set. Increment the usage count so as to
758868bd00fSLiam Girdwood 	 * prevent the device from entering runtime suspend.
759868bd00fSLiam Girdwood 	 */
760868bd00fSLiam Girdwood 	if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume)
761868bd00fSLiam Girdwood 		pm_runtime_get_noresume(sdev->dev);
762868bd00fSLiam Girdwood 
763868bd00fSLiam Girdwood 	return ret;
764868bd00fSLiam Girdwood }
765868bd00fSLiam Girdwood 
766868bd00fSLiam Girdwood static void sof_pcm_remove(struct snd_soc_component *component)
767868bd00fSLiam Girdwood {
768868bd00fSLiam Girdwood 	/* remove topology */
769868bd00fSLiam Girdwood 	snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
770868bd00fSLiam Girdwood }
771868bd00fSLiam Girdwood 
772868bd00fSLiam Girdwood void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
773868bd00fSLiam Girdwood {
774868bd00fSLiam Girdwood 	struct snd_soc_component_driver *pd = &sdev->plat_drv;
775868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
776868bd00fSLiam Girdwood 	const char *drv_name;
777868bd00fSLiam Girdwood 
778868bd00fSLiam Girdwood 	drv_name = plat_data->machine->drv_name;
779868bd00fSLiam Girdwood 
780868bd00fSLiam Girdwood 	pd->name = "sof-audio-component";
781868bd00fSLiam Girdwood 	pd->probe = sof_pcm_probe;
782868bd00fSLiam Girdwood 	pd->remove = sof_pcm_remove;
7831c91d77eSKuninori Morimoto 	pd->open = sof_pcm_open;
7841c91d77eSKuninori Morimoto 	pd->close = sof_pcm_close;
7851c91d77eSKuninori Morimoto 	pd->ioctl = snd_soc_pcm_lib_ioctl;
7861c91d77eSKuninori Morimoto 	pd->hw_params = sof_pcm_hw_params;
7871c91d77eSKuninori Morimoto 	pd->prepare = sof_pcm_prepare;
7881c91d77eSKuninori Morimoto 	pd->hw_free = sof_pcm_hw_free;
7891c91d77eSKuninori Morimoto 	pd->trigger = sof_pcm_trigger;
7901c91d77eSKuninori Morimoto 	pd->pointer = sof_pcm_pointer;
7911c91d77eSKuninori Morimoto 	pd->page = sof_pcm_page;
7921c91d77eSKuninori Morimoto 
793868bd00fSLiam Girdwood #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
794868bd00fSLiam Girdwood 	pd->compr_ops = &sof_compressed_ops;
795868bd00fSLiam Girdwood #endif
7961c91d77eSKuninori Morimoto 	pd->pcm_construct = sof_pcm_new;
797868bd00fSLiam Girdwood 	pd->ignore_machine = drv_name;
798868bd00fSLiam Girdwood 	pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
799868bd00fSLiam Girdwood 	pd->be_pcm_base = SOF_BE_PCM_BASE;
800868bd00fSLiam Girdwood 	pd->use_dai_pcm_id = true;
801868bd00fSLiam Girdwood 	pd->topology_name_prefix = "sof";
802868bd00fSLiam Girdwood 
803868bd00fSLiam Girdwood 	 /* increment module refcount when a pcm is opened */
804868bd00fSLiam Girdwood 	pd->module_get_upon_open = 1;
805868bd00fSLiam Girdwood }
806