xref: /openbmc/linux/sound/soc/sof/pcm.c (revision 6d0a21dd95c349bbe3663a4870ff7e70ddc6c9b6)
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>
16bcd2cc35SNoah Klayman #include <trace/events/sof.h>
176ace85b9SChunxu Li #include "sof-of-dev.h"
18868bd00fSLiam Girdwood #include "sof-priv.h"
19ee1e79b7SRanjani Sridharan #include "sof-audio.h"
20ee844305SPeter Ujfalusi #include "sof-utils.h"
213dc0d709SPeter Ujfalusi #include "ops.h"
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 
41e2803e61SKeyon Jie /*
42e2803e61SKeyon Jie  * sof pcm period elapse work
43e2803e61SKeyon Jie  */
44858f7a5cSDaniel Baluta static void snd_sof_pcm_period_elapsed_work(struct work_struct *work)
45e2803e61SKeyon Jie {
46e2803e61SKeyon Jie 	struct snd_sof_pcm_stream *sps =
47e2803e61SKeyon Jie 		container_of(work, struct snd_sof_pcm_stream,
48e2803e61SKeyon Jie 			     period_elapsed_work);
49e2803e61SKeyon Jie 
50e2803e61SKeyon Jie 	snd_pcm_period_elapsed(sps->substream);
51e2803e61SKeyon Jie }
52e2803e61SKeyon Jie 
53858f7a5cSDaniel Baluta void snd_sof_pcm_init_elapsed_work(struct work_struct *work)
54858f7a5cSDaniel Baluta {
55858f7a5cSDaniel Baluta 	 INIT_WORK(work, snd_sof_pcm_period_elapsed_work);
56858f7a5cSDaniel Baluta }
57858f7a5cSDaniel Baluta 
58e2803e61SKeyon Jie /*
59e2803e61SKeyon Jie  * sof pcm period elapse, this could be called at irq thread context.
60e2803e61SKeyon Jie  */
61e2803e61SKeyon Jie void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
62e2803e61SKeyon Jie {
631205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
64e2803e61SKeyon Jie 	struct snd_soc_component *component =
65ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
66e2803e61SKeyon Jie 	struct snd_sof_pcm *spcm;
67e2803e61SKeyon Jie 
68ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
69e2803e61SKeyon Jie 	if (!spcm) {
70ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
71e2803e61SKeyon Jie 			"error: period elapsed for unknown stream!\n");
72e2803e61SKeyon Jie 		return;
73e2803e61SKeyon Jie 	}
74e2803e61SKeyon Jie 
75e2803e61SKeyon Jie 	/*
76e2803e61SKeyon Jie 	 * snd_pcm_period_elapsed() can be called in interrupt context
77e2803e61SKeyon Jie 	 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(),
78e2803e61SKeyon Jie 	 * when the PCM is done draining or xrun happened, a STOP IPC will
79e2803e61SKeyon Jie 	 * then be sent and this IPC will hit IPC timeout.
80e2803e61SKeyon Jie 	 * To avoid sending IPC before the previous IPC is handled, we
81e2803e61SKeyon Jie 	 * schedule delayed work here to call the snd_pcm_period_elapsed().
82e2803e61SKeyon Jie 	 */
83e2803e61SKeyon Jie 	schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
84e2803e61SKeyon Jie }
85e2803e61SKeyon Jie EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
86e2803e61SKeyon Jie 
87f18ad9caSRanjani Sridharan static int
88f18ad9caSRanjani Sridharan sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev, struct snd_soc_pcm_runtime *rtd,
8966344c6dSRanjani Sridharan 				struct snd_sof_pcm *spcm, struct snd_pcm_hw_params *params,
9066344c6dSRanjani Sridharan 				struct snd_sof_platform_stream_params *platform_params, int dir)
915fcdbb2dSRanjani Sridharan {
925fcdbb2dSRanjani Sridharan 	struct snd_soc_dai *dai;
935fcdbb2dSRanjani Sridharan 	int ret, j;
945fcdbb2dSRanjani Sridharan 
955fcdbb2dSRanjani Sridharan 	/* query DAPM for list of connected widgets and set them up */
965fcdbb2dSRanjani Sridharan 	for_each_rtd_cpu_dais(rtd, j, dai) {
975fcdbb2dSRanjani Sridharan 		struct snd_soc_dapm_widget_list *list;
985fcdbb2dSRanjani Sridharan 
995fcdbb2dSRanjani Sridharan 		ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list,
1005fcdbb2dSRanjani Sridharan 							     dpcm_end_walk_at_be);
1015fcdbb2dSRanjani Sridharan 		if (ret < 0) {
1025fcdbb2dSRanjani Sridharan 			dev_err(sdev->dev, "error: dai %s has no valid %s path\n", dai->name,
1035fcdbb2dSRanjani Sridharan 				dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
1045fcdbb2dSRanjani Sridharan 			return ret;
1055fcdbb2dSRanjani Sridharan 		}
1065fcdbb2dSRanjani Sridharan 
1075fcdbb2dSRanjani Sridharan 		spcm->stream[dir].list = list;
1085fcdbb2dSRanjani Sridharan 
10966344c6dSRanjani Sridharan 		ret = sof_widget_list_setup(sdev, spcm, params, platform_params, dir);
1105fcdbb2dSRanjani Sridharan 		if (ret < 0) {
1115fcdbb2dSRanjani Sridharan 			dev_err(sdev->dev, "error: failed widget list set up for pcm %d dir %d\n",
1125fcdbb2dSRanjani Sridharan 				spcm->pcm.pcm_id, dir);
1135fcdbb2dSRanjani Sridharan 			spcm->stream[dir].list = NULL;
1145fcdbb2dSRanjani Sridharan 			snd_soc_dapm_dai_free_widgets(&list);
1155fcdbb2dSRanjani Sridharan 			return ret;
1165fcdbb2dSRanjani Sridharan 		}
1175fcdbb2dSRanjani Sridharan 	}
1185fcdbb2dSRanjani Sridharan 
1195fcdbb2dSRanjani Sridharan 	return 0;
1205fcdbb2dSRanjani Sridharan }
1215fcdbb2dSRanjani Sridharan 
1221c91d77eSKuninori Morimoto static int sof_pcm_hw_params(struct snd_soc_component *component,
1231c91d77eSKuninori Morimoto 			     struct snd_pcm_substream *substream,
124868bd00fSLiam Girdwood 			     struct snd_pcm_hw_params *params)
125868bd00fSLiam Girdwood {
126868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
127621fd48cSRanjani Sridharan 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
128148dd6a2SPeter Ujfalusi 	const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
12931f60a0cSPeter Ujfalusi 	struct snd_sof_platform_stream_params platform_params = { 0 };
130621fd48cSRanjani Sridharan 	struct snd_pcm_runtime *runtime = substream->runtime;
131868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
132868bd00fSLiam Girdwood 	int ret;
133868bd00fSLiam Girdwood 
134868bd00fSLiam Girdwood 	/* nothing to do for BE */
135868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
136868bd00fSLiam Girdwood 		return 0;
137868bd00fSLiam Girdwood 
138ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
139868bd00fSLiam Girdwood 	if (!spcm)
140868bd00fSLiam Girdwood 		return -EINVAL;
141868bd00fSLiam Girdwood 
142cfe8191bSKai Vehmanen 	/*
143cfe8191bSKai Vehmanen 	 * Handle repeated calls to hw_params() without free_pcm() in
144cfe8191bSKai Vehmanen 	 * between. At least ALSA OSS emulation depends on this.
145cfe8191bSKai Vehmanen 	 */
146148dd6a2SPeter Ujfalusi 	if (pcm_ops && pcm_ops->hw_free && spcm->prepared[substream->stream]) {
1474123c24bSRanjani Sridharan 		ret = pcm_ops->hw_free(component, substream);
148cfe8191bSKai Vehmanen 		if (ret < 0)
149cfe8191bSKai Vehmanen 			return ret;
150cfe8191bSKai Vehmanen 
1514123c24bSRanjani Sridharan 		spcm->prepared[substream->stream] = false;
1524123c24bSRanjani Sridharan 	}
1534123c24bSRanjani Sridharan 
154ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
155868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream);
156868bd00fSLiam Girdwood 
157f0d31dbbSRanjani Sridharan 	ret = snd_sof_pcm_platform_hw_params(sdev, substream, params, &platform_params);
158f0d31dbbSRanjani Sridharan 	if (ret < 0) {
159f0d31dbbSRanjani Sridharan 		dev_err(component->dev, "platform hw params failed\n");
160f0d31dbbSRanjani Sridharan 		return ret;
161f0d31dbbSRanjani Sridharan 	}
162f0d31dbbSRanjani Sridharan 
1635fcdbb2dSRanjani Sridharan 	/* if this is a repeated hw_params without hw_free, skip setting up widgets */
1645fcdbb2dSRanjani Sridharan 	if (!spcm->stream[substream->stream].list) {
16566344c6dSRanjani Sridharan 		ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, params, &platform_params,
16666344c6dSRanjani Sridharan 						      substream->stream);
1675fcdbb2dSRanjani Sridharan 		if (ret < 0)
1685fcdbb2dSRanjani Sridharan 			return ret;
1695fcdbb2dSRanjani Sridharan 	}
1705fcdbb2dSRanjani Sridharan 
171621fd48cSRanjani Sridharan 	/* create compressed page table for audio firmware */
172621fd48cSRanjani Sridharan 	if (runtime->buffer_changed) {
173621fd48cSRanjani Sridharan 		ret = create_page_table(component, substream, runtime->dma_area,
174621fd48cSRanjani Sridharan 					runtime->dma_bytes);
175621fd48cSRanjani Sridharan 
176621fd48cSRanjani Sridharan 		if (ret < 0)
177868bd00fSLiam Girdwood 			return ret;
178868bd00fSLiam Girdwood 	}
179868bd00fSLiam Girdwood 
180148dd6a2SPeter Ujfalusi 	if (pcm_ops && pcm_ops->hw_params) {
181621fd48cSRanjani Sridharan 		ret = pcm_ops->hw_params(component, substream, params, &platform_params);
182621fd48cSRanjani Sridharan 		if (ret < 0)
183757ce810SPeter Ujfalusi 			return ret;
184757ce810SPeter Ujfalusi 	}
185757ce810SPeter Ujfalusi 
18604c80277SKai Vehmanen 	spcm->prepared[substream->stream] = true;
18704c80277SKai Vehmanen 
188868bd00fSLiam Girdwood 	/* save pcm hw_params */
189868bd00fSLiam Girdwood 	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
190868bd00fSLiam Girdwood 
191621fd48cSRanjani Sridharan 	return 0;
192868bd00fSLiam Girdwood }
193868bd00fSLiam Girdwood 
1941c91d77eSKuninori Morimoto static int sof_pcm_hw_free(struct snd_soc_component *component,
1951c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
196868bd00fSLiam Girdwood {
1971205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
198868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
199148dd6a2SPeter Ujfalusi 	const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
200868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
201e66e52c5SKai Vehmanen 	int ret, err = 0;
202868bd00fSLiam Girdwood 
203868bd00fSLiam Girdwood 	/* nothing to do for BE */
204868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
205868bd00fSLiam Girdwood 		return 0;
206868bd00fSLiam Girdwood 
207ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
208868bd00fSLiam Girdwood 	if (!spcm)
209868bd00fSLiam Girdwood 		return -EINVAL;
210868bd00fSLiam Girdwood 
211ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
212ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
213868bd00fSLiam Girdwood 
214*6d0a21ddSRanjani Sridharan 	if (spcm->prepared[substream->stream]) {
215*6d0a21ddSRanjani Sridharan 		/* stop DMA first if needed */
216*6d0a21ddSRanjani Sridharan 		if (pcm_ops && pcm_ops->platform_stop_during_hw_free)
217*6d0a21ddSRanjani Sridharan 			snd_sof_pcm_platform_trigger(sdev, substream, SNDRV_PCM_TRIGGER_STOP);
218*6d0a21ddSRanjani Sridharan 
2190b639dcdSRanjani Sridharan 		/* free PCM in the DSP */
220*6d0a21ddSRanjani Sridharan 		if (pcm_ops && pcm_ops->hw_free) {
2214123c24bSRanjani Sridharan 			ret = pcm_ops->hw_free(component, substream);
222e66e52c5SKai Vehmanen 			if (ret < 0)
223e66e52c5SKai Vehmanen 				err = ret;
224*6d0a21ddSRanjani Sridharan 		}
225868bd00fSLiam Girdwood 
2264123c24bSRanjani Sridharan 		spcm->prepared[substream->stream] = false;
2274123c24bSRanjani Sridharan 	}
2285fcdbb2dSRanjani Sridharan 
229*6d0a21ddSRanjani Sridharan 	/* reset DMA */
23093146bc2SRanjani Sridharan 	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
231e66e52c5SKai Vehmanen 	if (ret < 0) {
232ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: platform hw free failed\n");
233e66e52c5SKai Vehmanen 		err = ret;
234e66e52c5SKai Vehmanen 	}
23593146bc2SRanjani Sridharan 
2360b639dcdSRanjani Sridharan 	/* free the DAPM widget list */
2370b639dcdSRanjani Sridharan 	ret = sof_widget_list_free(sdev, spcm, substream->stream);
2380b639dcdSRanjani Sridharan 	if (ret < 0)
2390b639dcdSRanjani Sridharan 		err = ret;
2400b639dcdSRanjani Sridharan 
2410b639dcdSRanjani Sridharan 	cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
2420b639dcdSRanjani Sridharan 
243e66e52c5SKai Vehmanen 	return err;
244868bd00fSLiam Girdwood }
245868bd00fSLiam Girdwood 
2461c91d77eSKuninori Morimoto static int sof_pcm_prepare(struct snd_soc_component *component,
2471c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream)
248868bd00fSLiam Girdwood {
2491205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
250868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
251868bd00fSLiam Girdwood 	int ret;
252868bd00fSLiam Girdwood 
253868bd00fSLiam Girdwood 	/* nothing to do for BE */
254868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
255868bd00fSLiam Girdwood 		return 0;
256868bd00fSLiam Girdwood 
257ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
258868bd00fSLiam Girdwood 	if (!spcm)
259868bd00fSLiam Girdwood 		return -EINVAL;
260868bd00fSLiam Girdwood 
26104c80277SKai Vehmanen 	if (spcm->prepared[substream->stream])
262868bd00fSLiam Girdwood 		return 0;
263868bd00fSLiam Girdwood 
264ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
265ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
266868bd00fSLiam Girdwood 
267868bd00fSLiam Girdwood 	/* set hw_params */
2681c91d77eSKuninori Morimoto 	ret = sof_pcm_hw_params(component,
2691c91d77eSKuninori Morimoto 				substream, &spcm->params[substream->stream]);
270868bd00fSLiam Girdwood 	if (ret < 0) {
271ee1e79b7SRanjani Sridharan 		dev_err(component->dev,
272ee1e79b7SRanjani Sridharan 			"error: set pcm hw_params after resume\n");
273868bd00fSLiam Girdwood 		return ret;
274868bd00fSLiam Girdwood 	}
275868bd00fSLiam Girdwood 
276868bd00fSLiam Girdwood 	return 0;
277868bd00fSLiam Girdwood }
278868bd00fSLiam Girdwood 
279868bd00fSLiam Girdwood /*
280868bd00fSLiam Girdwood  * FE dai link trigger actions are always executed in non-atomic context because
281868bd00fSLiam Girdwood  * they involve IPC's.
282868bd00fSLiam Girdwood  */
2831c91d77eSKuninori Morimoto static int sof_pcm_trigger(struct snd_soc_component *component,
2841c91d77eSKuninori Morimoto 			   struct snd_pcm_substream *substream, int cmd)
285868bd00fSLiam Girdwood {
2861205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
287868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
288148dd6a2SPeter Ujfalusi 	const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
289868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
29004c80277SKai Vehmanen 	bool reset_hw_params = false;
2910a1b0834SPan Xiuli 	bool ipc_first = false;
292beac3f4cSRanjani Sridharan 	int ret = 0;
293868bd00fSLiam Girdwood 
294868bd00fSLiam Girdwood 	/* nothing to do for BE */
295868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
296868bd00fSLiam Girdwood 		return 0;
297868bd00fSLiam Girdwood 
298ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
299868bd00fSLiam Girdwood 	if (!spcm)
300868bd00fSLiam Girdwood 		return -EINVAL;
301868bd00fSLiam Girdwood 
302ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
303868bd00fSLiam Girdwood 		spcm->pcm.pcm_id, substream->stream, cmd);
304868bd00fSLiam Girdwood 
305868bd00fSLiam Girdwood 	switch (cmd) {
306868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
3070a1b0834SPan Xiuli 		ipc_first = true;
308868bd00fSLiam Girdwood 		break;
309868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
31051ce3e6eSRanjani Sridharan 		if (pcm_ops && pcm_ops->ipc_first_on_start)
31151ce3e6eSRanjani Sridharan 			ipc_first = true;
312868bd00fSLiam Girdwood 		break;
313868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
314ac8c046fSKeyon Jie 		if (spcm->stream[substream->stream].suspend_ignored) {
315ac8c046fSKeyon Jie 			/*
316ac8c046fSKeyon Jie 			 * This case will be triggered when INFO_RESUME is
317ac8c046fSKeyon Jie 			 * not supported, no need to re-start streams that
318ac8c046fSKeyon Jie 			 * remained enabled in D0ix.
319ac8c046fSKeyon Jie 			 */
320ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = false;
321ac8c046fSKeyon Jie 			return 0;
322ac8c046fSKeyon Jie 		}
32351ce3e6eSRanjani Sridharan 
32451ce3e6eSRanjani Sridharan 		if (pcm_ops && pcm_ops->ipc_first_on_start)
32551ce3e6eSRanjani Sridharan 			ipc_first = true;
326868bd00fSLiam Girdwood 		break;
327868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
328043ae13bSRanjani Sridharan 		if (sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
329ac8c046fSKeyon Jie 		    spcm->stream[substream->stream].d0i3_compatible) {
330ac8c046fSKeyon Jie 			/*
331ac8c046fSKeyon Jie 			 * trap the event, not sending trigger stop to
332ac8c046fSKeyon Jie 			 * prevent the FW pipelines from being stopped,
333ac8c046fSKeyon Jie 			 * and mark the flag to ignore the upcoming DAPM
334ac8c046fSKeyon Jie 			 * PM events.
335ac8c046fSKeyon Jie 			 */
336ac8c046fSKeyon Jie 			spcm->stream[substream->stream].suspend_ignored = true;
337ac8c046fSKeyon Jie 			return 0;
338ac8c046fSKeyon Jie 		}
33928d40e7aSPeter Ujfalusi 
34028d40e7aSPeter Ujfalusi 		/* On suspend the DMA must be stopped in DSPless mode */
34128d40e7aSPeter Ujfalusi 		if (sdev->dspless_mode_selected)
34228d40e7aSPeter Ujfalusi 			reset_hw_params = true;
34328d40e7aSPeter Ujfalusi 
344df561f66SGustavo A. R. Silva 		fallthrough;
345868bd00fSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
3460a1b0834SPan Xiuli 		ipc_first = true;
3477d6f623cSRanjani Sridharan 		if (pcm_ops && pcm_ops->reset_hw_params_during_stop)
34804c80277SKai Vehmanen 			reset_hw_params = true;
349868bd00fSLiam Girdwood 		break;
350868bd00fSLiam Girdwood 	default:
351beac3f4cSRanjani Sridharan 		dev_err(component->dev, "Unhandled trigger cmd %d\n", cmd);
352868bd00fSLiam Girdwood 		return -EINVAL;
353868bd00fSLiam Girdwood 	}
354868bd00fSLiam Girdwood 
3550a1b0834SPan Xiuli 	if (!ipc_first)
356868bd00fSLiam Girdwood 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
357868bd00fSLiam Girdwood 
358148dd6a2SPeter Ujfalusi 	if (pcm_ops && pcm_ops->trigger)
359beac3f4cSRanjani Sridharan 		ret = pcm_ops->trigger(component, substream, cmd);
360868bd00fSLiam Girdwood 
36151ce3e6eSRanjani Sridharan 	switch (cmd) {
36251ce3e6eSRanjani Sridharan 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
36351ce3e6eSRanjani Sridharan 	case SNDRV_PCM_TRIGGER_START:
36451ce3e6eSRanjani Sridharan 		/* invoke platform trigger to start DMA only if pcm_ops is successful */
36551ce3e6eSRanjani Sridharan 		if (ipc_first && !ret)
3660a1b0834SPan Xiuli 			snd_sof_pcm_platform_trigger(sdev, substream, cmd);
36751ce3e6eSRanjani Sridharan 		break;
36851ce3e6eSRanjani Sridharan 	case SNDRV_PCM_TRIGGER_SUSPEND:
36951ce3e6eSRanjani Sridharan 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
37051ce3e6eSRanjani Sridharan 	case SNDRV_PCM_TRIGGER_STOP:
371*6d0a21ddSRanjani Sridharan 		/* invoke platform trigger to stop DMA even if pcm_ops isn't set or if it failed */
372*6d0a21ddSRanjani Sridharan 		if (!pcm_ops || (pcm_ops && !pcm_ops->platform_stop_during_hw_free))
37351ce3e6eSRanjani Sridharan 			snd_sof_pcm_platform_trigger(sdev, substream, cmd);
37451ce3e6eSRanjani Sridharan 		break;
37551ce3e6eSRanjani Sridharan 	default:
37651ce3e6eSRanjani Sridharan 		break;
37751ce3e6eSRanjani Sridharan 	}
3780a1b0834SPan Xiuli 
3790a1b0834SPan Xiuli 	/* free PCM if reset_hw_params is set and the STOP IPC is successful */
3809e116f5aStangmeng 	if (!ret && reset_hw_params)
38182b18242SRanjani Sridharan 		ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, false);
382a49b6871SKai Vehmanen 
383868bd00fSLiam Girdwood 	return ret;
384868bd00fSLiam Girdwood }
385868bd00fSLiam Girdwood 
3861c91d77eSKuninori Morimoto static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
3871c91d77eSKuninori Morimoto 					 struct snd_pcm_substream *substream)
388868bd00fSLiam Girdwood {
3891205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
390868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
391868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
392868bd00fSLiam Girdwood 	snd_pcm_uframes_t host, dai;
393868bd00fSLiam Girdwood 
394868bd00fSLiam Girdwood 	/* nothing to do for BE */
395868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
396868bd00fSLiam Girdwood 		return 0;
397868bd00fSLiam Girdwood 
398868bd00fSLiam Girdwood 	/* use dsp ops pointer callback directly if set */
399868bd00fSLiam Girdwood 	if (sof_ops(sdev)->pcm_pointer)
400868bd00fSLiam Girdwood 		return sof_ops(sdev)->pcm_pointer(sdev, substream);
401868bd00fSLiam Girdwood 
402ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
403868bd00fSLiam Girdwood 	if (!spcm)
404868bd00fSLiam Girdwood 		return -EINVAL;
405868bd00fSLiam Girdwood 
406868bd00fSLiam Girdwood 	/* read position from DSP */
407868bd00fSLiam Girdwood 	host = bytes_to_frames(substream->runtime,
408868bd00fSLiam Girdwood 			       spcm->stream[substream->stream].posn.host_posn);
409868bd00fSLiam Girdwood 	dai = bytes_to_frames(substream->runtime,
410868bd00fSLiam Girdwood 			      spcm->stream[substream->stream].posn.dai_posn);
411868bd00fSLiam Girdwood 
412bcd2cc35SNoah Klayman 	trace_sof_pcm_pointer_position(sdev, spcm, substream, host, dai);
413868bd00fSLiam Girdwood 
414868bd00fSLiam Girdwood 	return host;
415868bd00fSLiam Girdwood }
416868bd00fSLiam Girdwood 
4171c91d77eSKuninori Morimoto static int sof_pcm_open(struct snd_soc_component *component,
4181c91d77eSKuninori Morimoto 			struct snd_pcm_substream *substream)
419868bd00fSLiam Girdwood {
4201205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
421868bd00fSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
422868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
423856601e5SPierre-Louis Bossart 	struct snd_sof_dsp_ops *ops = sof_ops(sdev);
424868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
425868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
426868bd00fSLiam Girdwood 	int ret;
427868bd00fSLiam Girdwood 
428868bd00fSLiam Girdwood 	/* nothing to do for BE */
429868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
430868bd00fSLiam Girdwood 		return 0;
431868bd00fSLiam Girdwood 
432ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
433868bd00fSLiam Girdwood 	if (!spcm)
434868bd00fSLiam Girdwood 		return -EINVAL;
435868bd00fSLiam Girdwood 
436ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
437ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
438868bd00fSLiam Girdwood 
439868bd00fSLiam Girdwood 
440868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[substream->stream];
441868bd00fSLiam Girdwood 
442868bd00fSLiam Girdwood 	/* set runtime config */
44327e322faSPierre-Louis Bossart 	runtime->hw.info = ops->hw_info; /* platform-specific */
44427e322faSPierre-Louis Bossart 
4459983ac49SKai Vehmanen 	/* set any runtime constraints based on topology */
446868bd00fSLiam Girdwood 	runtime->hw.formats = le64_to_cpu(caps->formats);
447868bd00fSLiam Girdwood 	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
448868bd00fSLiam Girdwood 	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
449868bd00fSLiam Girdwood 	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
450868bd00fSLiam Girdwood 	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
451868bd00fSLiam Girdwood 
452868bd00fSLiam Girdwood 	/*
453868bd00fSLiam Girdwood 	 * caps->buffer_size_min is not used since the
454868bd00fSLiam Girdwood 	 * snd_pcm_hardware structure only defines buffer_bytes_max
455868bd00fSLiam Girdwood 	 */
456868bd00fSLiam Girdwood 	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
457868bd00fSLiam Girdwood 
458ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period min %zd max %zd bytes\n",
459868bd00fSLiam Girdwood 		runtime->hw.period_bytes_min,
460868bd00fSLiam Girdwood 		runtime->hw.period_bytes_max);
461ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "period count %d max %d\n",
462868bd00fSLiam Girdwood 		runtime->hw.periods_min,
463868bd00fSLiam Girdwood 		runtime->hw.periods_max);
464ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "buffer max %zd bytes\n",
465868bd00fSLiam Girdwood 		runtime->hw.buffer_bytes_max);
466868bd00fSLiam Girdwood 
467868bd00fSLiam Girdwood 	/* set wait time - TODO: come from topology */
468868bd00fSLiam Girdwood 	substream->wait_time = 500;
469868bd00fSLiam Girdwood 
470868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.host_posn = 0;
471868bd00fSLiam Girdwood 	spcm->stream[substream->stream].posn.dai_posn = 0;
472868bd00fSLiam Girdwood 	spcm->stream[substream->stream].substream = substream;
47304c80277SKai Vehmanen 	spcm->prepared[substream->stream] = false;
474868bd00fSLiam Girdwood 
475868bd00fSLiam Girdwood 	ret = snd_sof_pcm_platform_open(sdev, substream);
47614a2212dSRanjani Sridharan 	if (ret < 0)
477ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm open failed %d\n", ret);
478868bd00fSLiam Girdwood 
479868bd00fSLiam Girdwood 	return ret;
480868bd00fSLiam Girdwood }
481868bd00fSLiam Girdwood 
4821c91d77eSKuninori Morimoto static int sof_pcm_close(struct snd_soc_component *component,
4831c91d77eSKuninori Morimoto 			 struct snd_pcm_substream *substream)
484868bd00fSLiam Girdwood {
4851205300aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
486868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
487868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
488868bd00fSLiam Girdwood 	int err;
489868bd00fSLiam Girdwood 
490868bd00fSLiam Girdwood 	/* nothing to do for BE */
491868bd00fSLiam Girdwood 	if (rtd->dai_link->no_pcm)
492868bd00fSLiam Girdwood 		return 0;
493868bd00fSLiam Girdwood 
494ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
495868bd00fSLiam Girdwood 	if (!spcm)
496868bd00fSLiam Girdwood 		return -EINVAL;
497868bd00fSLiam Girdwood 
498ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
499ee1e79b7SRanjani Sridharan 		spcm->pcm.pcm_id, substream->stream);
500868bd00fSLiam Girdwood 
501868bd00fSLiam Girdwood 	err = snd_sof_pcm_platform_close(sdev, substream);
502868bd00fSLiam Girdwood 	if (err < 0) {
503ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: pcm close failed %d\n",
504868bd00fSLiam Girdwood 			err);
505868bd00fSLiam Girdwood 		/*
506868bd00fSLiam Girdwood 		 * keep going, no point in preventing the close
507868bd00fSLiam Girdwood 		 * from happening
508868bd00fSLiam Girdwood 		 */
509868bd00fSLiam Girdwood 	}
510868bd00fSLiam Girdwood 
511868bd00fSLiam Girdwood 	return 0;
512868bd00fSLiam Girdwood }
513868bd00fSLiam Girdwood 
514868bd00fSLiam Girdwood /*
515868bd00fSLiam Girdwood  * Pre-allocate playback/capture audio buffer pages.
516868bd00fSLiam Girdwood  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
517868bd00fSLiam Girdwood  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
518868bd00fSLiam Girdwood  */
5191c91d77eSKuninori Morimoto static int sof_pcm_new(struct snd_soc_component *component,
5201c91d77eSKuninori Morimoto 		       struct snd_soc_pcm_runtime *rtd)
521868bd00fSLiam Girdwood {
522868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
523868bd00fSLiam Girdwood 	struct snd_sof_pcm *spcm;
524868bd00fSLiam Girdwood 	struct snd_pcm *pcm = rtd->pcm;
525868bd00fSLiam Girdwood 	struct snd_soc_tplg_stream_caps *caps;
526868bd00fSLiam Girdwood 	int stream = SNDRV_PCM_STREAM_PLAYBACK;
527868bd00fSLiam Girdwood 
528868bd00fSLiam Girdwood 	/* find SOF PCM for this RTD */
529ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_dai(component, rtd);
530868bd00fSLiam Girdwood 	if (!spcm) {
531ee1e79b7SRanjani Sridharan 		dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
532868bd00fSLiam Girdwood 			 rtd->dai_link->id);
533868bd00fSLiam Girdwood 		return 0;
534868bd00fSLiam Girdwood 	}
535868bd00fSLiam Girdwood 
536ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
537868bd00fSLiam Girdwood 
538868bd00fSLiam Girdwood 	/* do we need to pre-allocate playback audio buffer pages */
539868bd00fSLiam Girdwood 	if (!spcm->pcm.playback)
540868bd00fSLiam Girdwood 		goto capture;
541868bd00fSLiam Girdwood 
542868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
543868bd00fSLiam Girdwood 
544868bd00fSLiam Girdwood 	/* pre-allocate playback audio buffer pages */
545ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
546ee1e79b7SRanjani Sridharan 		"spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
547868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
548868bd00fSLiam Girdwood 
5494f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
5504f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL playback substream!\n");
5514f7f9564SGuennadi Liakhovetski 		return -EINVAL;
5524f7f9564SGuennadi Liakhovetski 	}
5534f7f9564SGuennadi Liakhovetski 
55457e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
555868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
556e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
557868bd00fSLiam Girdwood capture:
558868bd00fSLiam Girdwood 	stream = SNDRV_PCM_STREAM_CAPTURE;
559868bd00fSLiam Girdwood 
560868bd00fSLiam Girdwood 	/* do we need to pre-allocate capture audio buffer pages */
561868bd00fSLiam Girdwood 	if (!spcm->pcm.capture)
562868bd00fSLiam Girdwood 		return 0;
563868bd00fSLiam Girdwood 
564868bd00fSLiam Girdwood 	caps = &spcm->pcm.caps[stream];
565868bd00fSLiam Girdwood 
566868bd00fSLiam Girdwood 	/* pre-allocate capture audio buffer pages */
567ee1e79b7SRanjani Sridharan 	dev_dbg(component->dev,
568ee1e79b7SRanjani Sridharan 		"spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
569868bd00fSLiam Girdwood 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
570868bd00fSLiam Girdwood 
5714f7f9564SGuennadi Liakhovetski 	if (!pcm->streams[stream].substream) {
5724f7f9564SGuennadi Liakhovetski 		dev_err(component->dev, "error: NULL capture substream!\n");
5734f7f9564SGuennadi Liakhovetski 		return -EINVAL;
5744f7f9564SGuennadi Liakhovetski 	}
5754f7f9564SGuennadi Liakhovetski 
57657e960f0STakashi Iwai 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
577868bd00fSLiam Girdwood 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
578e582f483SKeyon Jie 				   0, le32_to_cpu(caps->buffer_size_max));
579868bd00fSLiam Girdwood 
580868bd00fSLiam Girdwood 	return 0;
581868bd00fSLiam Girdwood }
582868bd00fSLiam Girdwood 
583868bd00fSLiam Girdwood /* fixup the BE DAI link to match any values from topology */
584f805e7e0SRanjani Sridharan int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
585868bd00fSLiam Girdwood {
586868bd00fSLiam Girdwood 	struct snd_interval *rate = hw_param_interval(params,
587868bd00fSLiam Girdwood 			SNDRV_PCM_HW_PARAM_RATE);
588868bd00fSLiam Girdwood 	struct snd_interval *channels = hw_param_interval(params,
589868bd00fSLiam Girdwood 						SNDRV_PCM_HW_PARAM_CHANNELS);
590868bd00fSLiam Girdwood 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
591868bd00fSLiam Girdwood 	struct snd_soc_component *component =
592ee1e79b7SRanjani Sridharan 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
593868bd00fSLiam Girdwood 	struct snd_sof_dai *dai =
594ee1e79b7SRanjani Sridharan 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
595c943a586SJaska Uimonen 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
596148dd6a2SPeter Ujfalusi 	const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
597868bd00fSLiam Girdwood 
598868bd00fSLiam Girdwood 	/* no topology exists for this BE, try a common configuration */
599868bd00fSLiam Girdwood 	if (!dai) {
600ee1e79b7SRanjani Sridharan 		dev_warn(component->dev,
601ee1e79b7SRanjani Sridharan 			 "warning: no topology found for BE DAI %s config\n",
602868bd00fSLiam Girdwood 			 rtd->dai_link->name);
603868bd00fSLiam Girdwood 
604868bd00fSLiam Girdwood 		/*  set 48k, stereo, 16bits by default */
605868bd00fSLiam Girdwood 		rate->min = 48000;
606868bd00fSLiam Girdwood 		rate->max = 48000;
607868bd00fSLiam Girdwood 
608868bd00fSLiam Girdwood 		channels->min = 2;
609868bd00fSLiam Girdwood 		channels->max = 2;
610868bd00fSLiam Girdwood 
611868bd00fSLiam Girdwood 		snd_mask_none(fmt);
612868bd00fSLiam Girdwood 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
613868bd00fSLiam Girdwood 
614868bd00fSLiam Girdwood 		return 0;
615868bd00fSLiam Girdwood 	}
616868bd00fSLiam Girdwood 
617148dd6a2SPeter Ujfalusi 	if (pcm_ops && pcm_ops->dai_link_fixup)
618b243b437SRanjani Sridharan 		return pcm_ops->dai_link_fixup(rtd, params);
619868bd00fSLiam Girdwood 
620868bd00fSLiam Girdwood 	return 0;
621868bd00fSLiam Girdwood }
622f3f3af17SPierre-Louis Bossart EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
623868bd00fSLiam Girdwood 
624868bd00fSLiam Girdwood static int sof_pcm_probe(struct snd_soc_component *component)
625868bd00fSLiam Girdwood {
626868bd00fSLiam Girdwood 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
627868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
628868bd00fSLiam Girdwood 	const char *tplg_filename;
629868bd00fSLiam Girdwood 	int ret;
630868bd00fSLiam Girdwood 
6314ea3bfd1SPierre-Louis Bossart 	/*
6324ea3bfd1SPierre-Louis Bossart 	 * make sure the device is pm_runtime_active before loading the
6334ea3bfd1SPierre-Louis Bossart 	 * topology and initiating IPC or bus transactions
6344ea3bfd1SPierre-Louis Bossart 	 */
6354ea3bfd1SPierre-Louis Bossart 	ret = pm_runtime_resume_and_get(component->dev);
6364ea3bfd1SPierre-Louis Bossart 	if (ret < 0 && ret != -EACCES)
6374ea3bfd1SPierre-Louis Bossart 		return ret;
6384ea3bfd1SPierre-Louis Bossart 
639868bd00fSLiam Girdwood 	/* load the default topology */
640868bd00fSLiam Girdwood 	sdev->component = component;
641868bd00fSLiam Girdwood 
642868bd00fSLiam Girdwood 	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
643868bd00fSLiam Girdwood 				       "%s/%s",
644868bd00fSLiam Girdwood 				       plat_data->tplg_filename_prefix,
645868bd00fSLiam Girdwood 				       plat_data->tplg_filename);
646868bd00fSLiam Girdwood 	if (!tplg_filename)
647868bd00fSLiam Girdwood 		return -ENOMEM;
648868bd00fSLiam Girdwood 
649ee1e79b7SRanjani Sridharan 	ret = snd_sof_load_topology(component, tplg_filename);
650868bd00fSLiam Girdwood 	if (ret < 0) {
651ee1e79b7SRanjani Sridharan 		dev_err(component->dev, "error: failed to load DSP topology %d\n",
652868bd00fSLiam Girdwood 			ret);
653868bd00fSLiam Girdwood 		return ret;
654868bd00fSLiam Girdwood 	}
655868bd00fSLiam Girdwood 
6564ea3bfd1SPierre-Louis Bossart 	pm_runtime_mark_last_busy(component->dev);
6574ea3bfd1SPierre-Louis Bossart 	pm_runtime_put_autosuspend(component->dev);
6584ea3bfd1SPierre-Louis Bossart 
659868bd00fSLiam Girdwood 	return ret;
660868bd00fSLiam Girdwood }
661868bd00fSLiam Girdwood 
662868bd00fSLiam Girdwood static void sof_pcm_remove(struct snd_soc_component *component)
663868bd00fSLiam Girdwood {
664868bd00fSLiam Girdwood 	/* remove topology */
665a5b8f71cSAmadeusz Sławiński 	snd_soc_tplg_component_remove(component);
666868bd00fSLiam Girdwood }
667868bd00fSLiam Girdwood 
6684a39ea3fSRanjani Sridharan static int sof_pcm_ack(struct snd_soc_component *component,
6694a39ea3fSRanjani Sridharan 		       struct snd_pcm_substream *substream)
6704a39ea3fSRanjani Sridharan {
6714a39ea3fSRanjani Sridharan 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
6724a39ea3fSRanjani Sridharan 
6734a39ea3fSRanjani Sridharan 	return snd_sof_pcm_platform_ack(sdev, substream);
6744a39ea3fSRanjani Sridharan }
6754a39ea3fSRanjani Sridharan 
67627c2100bSRander Wang static snd_pcm_sframes_t sof_pcm_delay(struct snd_soc_component *component,
67727c2100bSRander Wang 				       struct snd_pcm_substream *substream)
67827c2100bSRander Wang {
67927c2100bSRander Wang 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
68027c2100bSRander Wang 	const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
68127c2100bSRander Wang 
68227c2100bSRander Wang 	if (pcm_ops && pcm_ops->delay)
68327c2100bSRander Wang 		return pcm_ops->delay(component, substream);
68427c2100bSRander Wang 
68527c2100bSRander Wang 	return 0;
68627c2100bSRander Wang }
68727c2100bSRander Wang 
688868bd00fSLiam Girdwood void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
689868bd00fSLiam Girdwood {
690868bd00fSLiam Girdwood 	struct snd_soc_component_driver *pd = &sdev->plat_drv;
691868bd00fSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
692868bd00fSLiam Girdwood 	const char *drv_name;
693868bd00fSLiam Girdwood 
6946ace85b9SChunxu Li 	if (plat_data->machine)
695868bd00fSLiam Girdwood 		drv_name = plat_data->machine->drv_name;
6966ace85b9SChunxu Li 	else if (plat_data->of_machine)
6976ace85b9SChunxu Li 		drv_name = plat_data->of_machine->drv_name;
6986ace85b9SChunxu Li 	else
6996ace85b9SChunxu Li 		drv_name = NULL;
700868bd00fSLiam Girdwood 
701868bd00fSLiam Girdwood 	pd->name = "sof-audio-component";
702868bd00fSLiam Girdwood 	pd->probe = sof_pcm_probe;
703868bd00fSLiam Girdwood 	pd->remove = sof_pcm_remove;
7041c91d77eSKuninori Morimoto 	pd->open = sof_pcm_open;
7051c91d77eSKuninori Morimoto 	pd->close = sof_pcm_close;
7061c91d77eSKuninori Morimoto 	pd->hw_params = sof_pcm_hw_params;
7071c91d77eSKuninori Morimoto 	pd->prepare = sof_pcm_prepare;
7081c91d77eSKuninori Morimoto 	pd->hw_free = sof_pcm_hw_free;
7091c91d77eSKuninori Morimoto 	pd->trigger = sof_pcm_trigger;
7101c91d77eSKuninori Morimoto 	pd->pointer = sof_pcm_pointer;
7114a39ea3fSRanjani Sridharan 	pd->ack = sof_pcm_ack;
71227c2100bSRander Wang 	pd->delay = sof_pcm_delay;
7131c91d77eSKuninori Morimoto 
71476cdd90bSDaniel Baluta #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
71576cdd90bSDaniel Baluta 	pd->compress_ops = &sof_compressed_ops;
71676cdd90bSDaniel Baluta #endif
71776cdd90bSDaniel Baluta 
7181c91d77eSKuninori Morimoto 	pd->pcm_construct = sof_pcm_new;
719868bd00fSLiam Girdwood 	pd->ignore_machine = drv_name;
720868bd00fSLiam Girdwood 	pd->be_pcm_base = SOF_BE_PCM_BASE;
721868bd00fSLiam Girdwood 	pd->use_dai_pcm_id = true;
722868bd00fSLiam Girdwood 	pd->topology_name_prefix = "sof";
723868bd00fSLiam Girdwood 
724868bd00fSLiam Girdwood 	 /* increment module refcount when a pcm is opened */
725868bd00fSLiam Girdwood 	pd->module_get_upon_open = 1;
726a718ba30SCharles Keepax 
727a718ba30SCharles Keepax 	pd->legacy_dai_naming = 1;
72828d40e7aSPeter Ujfalusi 
72928d40e7aSPeter Ujfalusi 	/*
73028d40e7aSPeter Ujfalusi 	 * The fixup is only needed when the DSP is in use as with the DSPless
73128d40e7aSPeter Ujfalusi 	 * mode we are directly using the audio interface
73228d40e7aSPeter Ujfalusi 	 */
73328d40e7aSPeter Ujfalusi 	if (!sdev->dspless_mode_selected)
73428d40e7aSPeter Ujfalusi 		pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
735868bd00fSLiam Girdwood }
736