xref: /openbmc/linux/sound/soc/sof/pcm.c (revision 2dec9e09)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license.  When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
7 //
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 //
10 // PCM Layer, interface between ALSA and IPC.
11 //
12 
13 #include <linux/pm_runtime.h>
14 #include <sound/pcm_params.h>
15 #include <sound/sof.h>
16 #include "sof-of-dev.h"
17 #include "sof-priv.h"
18 #include "sof-audio.h"
19 #include "sof-utils.h"
20 #include "ops.h"
21 
22 /* Create DMA buffer page table for DSP */
23 static int create_page_table(struct snd_soc_component *component,
24 			     struct snd_pcm_substream *substream,
25 			     unsigned char *dma_area, size_t size)
26 {
27 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
28 	struct snd_sof_pcm *spcm;
29 	struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
30 	int stream = substream->stream;
31 
32 	spcm = snd_sof_find_spcm_dai(component, rtd);
33 	if (!spcm)
34 		return -EINVAL;
35 
36 	return snd_sof_create_page_table(component->dev, dmab,
37 		spcm->stream[stream].page_table.area, size);
38 }
39 
40 /*
41  * sof pcm period elapse work
42  */
43 static void snd_sof_pcm_period_elapsed_work(struct work_struct *work)
44 {
45 	struct snd_sof_pcm_stream *sps =
46 		container_of(work, struct snd_sof_pcm_stream,
47 			     period_elapsed_work);
48 
49 	snd_pcm_period_elapsed(sps->substream);
50 }
51 
52 void snd_sof_pcm_init_elapsed_work(struct work_struct *work)
53 {
54 	 INIT_WORK(work, snd_sof_pcm_period_elapsed_work);
55 }
56 
57 /*
58  * sof pcm period elapse, this could be called at irq thread context.
59  */
60 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
61 {
62 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
63 	struct snd_soc_component *component =
64 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
65 	struct snd_sof_pcm *spcm;
66 
67 	spcm = snd_sof_find_spcm_dai(component, rtd);
68 	if (!spcm) {
69 		dev_err(component->dev,
70 			"error: period elapsed for unknown stream!\n");
71 		return;
72 	}
73 
74 	/*
75 	 * snd_pcm_period_elapsed() can be called in interrupt context
76 	 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(),
77 	 * when the PCM is done draining or xrun happened, a STOP IPC will
78 	 * then be sent and this IPC will hit IPC timeout.
79 	 * To avoid sending IPC before the previous IPC is handled, we
80 	 * schedule delayed work here to call the snd_pcm_period_elapsed().
81 	 */
82 	schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
83 }
84 EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
85 
86 static int
87 sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev, struct snd_soc_pcm_runtime *rtd,
88 				struct snd_sof_pcm *spcm, struct snd_pcm_hw_params *params,
89 				struct snd_sof_platform_stream_params *platform_params, int dir)
90 {
91 	struct snd_soc_dai *dai;
92 	int ret, j;
93 
94 	/* query DAPM for list of connected widgets and set them up */
95 	for_each_rtd_cpu_dais(rtd, j, dai) {
96 		struct snd_soc_dapm_widget_list *list;
97 
98 		ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list,
99 							     dpcm_end_walk_at_be);
100 		if (ret < 0) {
101 			dev_err(sdev->dev, "error: dai %s has no valid %s path\n", dai->name,
102 				dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
103 			return ret;
104 		}
105 
106 		spcm->stream[dir].list = list;
107 
108 		ret = sof_widget_list_setup(sdev, spcm, params, platform_params, dir);
109 		if (ret < 0) {
110 			dev_err(sdev->dev, "error: failed widget list set up for pcm %d dir %d\n",
111 				spcm->pcm.pcm_id, dir);
112 			spcm->stream[dir].list = NULL;
113 			snd_soc_dapm_dai_free_widgets(&list);
114 			return ret;
115 		}
116 	}
117 
118 	return 0;
119 }
120 
121 static int sof_pcm_hw_params(struct snd_soc_component *component,
122 			     struct snd_pcm_substream *substream,
123 			     struct snd_pcm_hw_params *params)
124 {
125 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
126 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
127 	struct snd_sof_platform_stream_params platform_params = { 0 };
128 	const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
129 	struct snd_pcm_runtime *runtime = substream->runtime;
130 	struct snd_sof_pcm *spcm;
131 	int ret;
132 
133 	/* nothing to do for BE */
134 	if (rtd->dai_link->no_pcm)
135 		return 0;
136 
137 	spcm = snd_sof_find_spcm_dai(component, rtd);
138 	if (!spcm)
139 		return -EINVAL;
140 
141 	/*
142 	 * Handle repeated calls to hw_params() without free_pcm() in
143 	 * between. At least ALSA OSS emulation depends on this.
144 	 */
145 	if (pcm_ops->hw_free && spcm->prepared[substream->stream]) {
146 		ret = pcm_ops->hw_free(component, substream);
147 		if (ret < 0)
148 			return ret;
149 
150 		spcm->prepared[substream->stream] = false;
151 	}
152 
153 	dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
154 		spcm->pcm.pcm_id, substream->stream);
155 
156 	ret = snd_sof_pcm_platform_hw_params(sdev, substream, params, &platform_params);
157 	if (ret < 0) {
158 		dev_err(component->dev, "platform hw params failed\n");
159 		return ret;
160 	}
161 
162 	/* if this is a repeated hw_params without hw_free, skip setting up widgets */
163 	if (!spcm->stream[substream->stream].list) {
164 		ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, params, &platform_params,
165 						      substream->stream);
166 		if (ret < 0)
167 			return ret;
168 	}
169 
170 	/* create compressed page table for audio firmware */
171 	if (runtime->buffer_changed) {
172 		ret = create_page_table(component, substream, runtime->dma_area,
173 					runtime->dma_bytes);
174 
175 		if (ret < 0)
176 			return ret;
177 	}
178 
179 	if (pcm_ops->hw_params) {
180 		ret = pcm_ops->hw_params(component, substream, params, &platform_params);
181 		if (ret < 0)
182 			return ret;
183 	}
184 
185 	spcm->prepared[substream->stream] = true;
186 
187 	/* save pcm hw_params */
188 	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
189 
190 	return 0;
191 }
192 
193 static int sof_pcm_hw_free(struct snd_soc_component *component,
194 			   struct snd_pcm_substream *substream)
195 {
196 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
197 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
198 	const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
199 	struct snd_sof_pcm *spcm;
200 	int ret, err = 0;
201 
202 	/* nothing to do for BE */
203 	if (rtd->dai_link->no_pcm)
204 		return 0;
205 
206 	spcm = snd_sof_find_spcm_dai(component, rtd);
207 	if (!spcm)
208 		return -EINVAL;
209 
210 	dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
211 		spcm->pcm.pcm_id, substream->stream);
212 
213 	/* free PCM in the DSP */
214 	if (pcm_ops->hw_free && spcm->prepared[substream->stream]) {
215 		ret = pcm_ops->hw_free(component, substream);
216 		if (ret < 0)
217 			err = ret;
218 
219 		spcm->prepared[substream->stream] = false;
220 	}
221 
222 	/* stop DMA */
223 	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
224 	if (ret < 0) {
225 		dev_err(component->dev, "error: platform hw free failed\n");
226 		err = ret;
227 	}
228 
229 	/* free the DAPM widget list */
230 	ret = sof_widget_list_free(sdev, spcm, substream->stream);
231 	if (ret < 0)
232 		err = ret;
233 
234 	cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
235 
236 	return err;
237 }
238 
239 static int sof_pcm_prepare(struct snd_soc_component *component,
240 			   struct snd_pcm_substream *substream)
241 {
242 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
243 	struct snd_sof_pcm *spcm;
244 	int ret;
245 
246 	/* nothing to do for BE */
247 	if (rtd->dai_link->no_pcm)
248 		return 0;
249 
250 	spcm = snd_sof_find_spcm_dai(component, rtd);
251 	if (!spcm)
252 		return -EINVAL;
253 
254 	if (spcm->prepared[substream->stream])
255 		return 0;
256 
257 	dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
258 		spcm->pcm.pcm_id, substream->stream);
259 
260 	/* set hw_params */
261 	ret = sof_pcm_hw_params(component,
262 				substream, &spcm->params[substream->stream]);
263 	if (ret < 0) {
264 		dev_err(component->dev,
265 			"error: set pcm hw_params after resume\n");
266 		return ret;
267 	}
268 
269 	return 0;
270 }
271 
272 /*
273  * FE dai link trigger actions are always executed in non-atomic context because
274  * they involve IPC's.
275  */
276 static int sof_pcm_trigger(struct snd_soc_component *component,
277 			   struct snd_pcm_substream *substream, int cmd)
278 {
279 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
280 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
281 	const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
282 	struct snd_sof_pcm *spcm;
283 	bool reset_hw_params = false;
284 	bool free_widget_list = false;
285 	bool ipc_first = false;
286 	int ret = 0;
287 
288 	/* nothing to do for BE */
289 	if (rtd->dai_link->no_pcm)
290 		return 0;
291 
292 	spcm = snd_sof_find_spcm_dai(component, rtd);
293 	if (!spcm)
294 		return -EINVAL;
295 
296 	dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
297 		spcm->pcm.pcm_id, substream->stream, cmd);
298 
299 	switch (cmd) {
300 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
301 		ipc_first = true;
302 		break;
303 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
304 		break;
305 	case SNDRV_PCM_TRIGGER_START:
306 		if (spcm->stream[substream->stream].suspend_ignored) {
307 			/*
308 			 * This case will be triggered when INFO_RESUME is
309 			 * not supported, no need to re-start streams that
310 			 * remained enabled in D0ix.
311 			 */
312 			spcm->stream[substream->stream].suspend_ignored = false;
313 			return 0;
314 		}
315 		break;
316 	case SNDRV_PCM_TRIGGER_SUSPEND:
317 		if (sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
318 		    spcm->stream[substream->stream].d0i3_compatible) {
319 			/*
320 			 * trap the event, not sending trigger stop to
321 			 * prevent the FW pipelines from being stopped,
322 			 * and mark the flag to ignore the upcoming DAPM
323 			 * PM events.
324 			 */
325 			spcm->stream[substream->stream].suspend_ignored = true;
326 			return 0;
327 		}
328 		free_widget_list = true;
329 		fallthrough;
330 	case SNDRV_PCM_TRIGGER_STOP:
331 		ipc_first = true;
332 		reset_hw_params = true;
333 		break;
334 	default:
335 		dev_err(component->dev, "Unhandled trigger cmd %d\n", cmd);
336 		return -EINVAL;
337 	}
338 
339 	/*
340 	 * DMA and IPC sequence is different for start and stop. Need to send
341 	 * STOP IPC before stop DMA
342 	 */
343 	if (!ipc_first)
344 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
345 
346 	if (pcm_ops->trigger)
347 		ret = pcm_ops->trigger(component, substream, cmd);
348 
349 	/* need to STOP DMA even if trigger IPC failed */
350 	if (ipc_first)
351 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
352 
353 	/* free PCM if reset_hw_params is set and the STOP IPC is successful */
354 	if (!ret && reset_hw_params)
355 		ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream,
356 					  free_widget_list);
357 
358 	return ret;
359 }
360 
361 static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
362 					 struct snd_pcm_substream *substream)
363 {
364 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
365 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
366 	struct snd_sof_pcm *spcm;
367 	snd_pcm_uframes_t host, dai;
368 
369 	/* nothing to do for BE */
370 	if (rtd->dai_link->no_pcm)
371 		return 0;
372 
373 	/* use dsp ops pointer callback directly if set */
374 	if (sof_ops(sdev)->pcm_pointer)
375 		return sof_ops(sdev)->pcm_pointer(sdev, substream);
376 
377 	spcm = snd_sof_find_spcm_dai(component, rtd);
378 	if (!spcm)
379 		return -EINVAL;
380 
381 	/* read position from DSP */
382 	host = bytes_to_frames(substream->runtime,
383 			       spcm->stream[substream->stream].posn.host_posn);
384 	dai = bytes_to_frames(substream->runtime,
385 			      spcm->stream[substream->stream].posn.dai_posn);
386 
387 	dev_vdbg(component->dev,
388 		 "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
389 		 spcm->pcm.pcm_id, substream->stream, host, dai);
390 
391 	return host;
392 }
393 
394 static int sof_pcm_open(struct snd_soc_component *component,
395 			struct snd_pcm_substream *substream)
396 {
397 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
398 	struct snd_pcm_runtime *runtime = substream->runtime;
399 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
400 	struct snd_sof_dsp_ops *ops = sof_ops(sdev);
401 	struct snd_sof_pcm *spcm;
402 	struct snd_soc_tplg_stream_caps *caps;
403 	int ret;
404 
405 	/* nothing to do for BE */
406 	if (rtd->dai_link->no_pcm)
407 		return 0;
408 
409 	spcm = snd_sof_find_spcm_dai(component, rtd);
410 	if (!spcm)
411 		return -EINVAL;
412 
413 	dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
414 		spcm->pcm.pcm_id, substream->stream);
415 
416 
417 	caps = &spcm->pcm.caps[substream->stream];
418 
419 	/* set runtime config */
420 	runtime->hw.info = ops->hw_info; /* platform-specific */
421 
422 	/* set any runtime constraints based on topology */
423 	runtime->hw.formats = le64_to_cpu(caps->formats);
424 	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
425 	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
426 	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
427 	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
428 
429 	/*
430 	 * caps->buffer_size_min is not used since the
431 	 * snd_pcm_hardware structure only defines buffer_bytes_max
432 	 */
433 	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
434 
435 	dev_dbg(component->dev, "period min %zd max %zd bytes\n",
436 		runtime->hw.period_bytes_min,
437 		runtime->hw.period_bytes_max);
438 	dev_dbg(component->dev, "period count %d max %d\n",
439 		runtime->hw.periods_min,
440 		runtime->hw.periods_max);
441 	dev_dbg(component->dev, "buffer max %zd bytes\n",
442 		runtime->hw.buffer_bytes_max);
443 
444 	/* set wait time - TODO: come from topology */
445 	substream->wait_time = 500;
446 
447 	spcm->stream[substream->stream].posn.host_posn = 0;
448 	spcm->stream[substream->stream].posn.dai_posn = 0;
449 	spcm->stream[substream->stream].substream = substream;
450 	spcm->prepared[substream->stream] = false;
451 
452 	ret = snd_sof_pcm_platform_open(sdev, substream);
453 	if (ret < 0)
454 		dev_err(component->dev, "error: pcm open failed %d\n", ret);
455 
456 	return ret;
457 }
458 
459 static int sof_pcm_close(struct snd_soc_component *component,
460 			 struct snd_pcm_substream *substream)
461 {
462 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
463 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
464 	struct snd_sof_pcm *spcm;
465 	int err;
466 
467 	/* nothing to do for BE */
468 	if (rtd->dai_link->no_pcm)
469 		return 0;
470 
471 	spcm = snd_sof_find_spcm_dai(component, rtd);
472 	if (!spcm)
473 		return -EINVAL;
474 
475 	dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
476 		spcm->pcm.pcm_id, substream->stream);
477 
478 	err = snd_sof_pcm_platform_close(sdev, substream);
479 	if (err < 0) {
480 		dev_err(component->dev, "error: pcm close failed %d\n",
481 			err);
482 		/*
483 		 * keep going, no point in preventing the close
484 		 * from happening
485 		 */
486 	}
487 
488 	return 0;
489 }
490 
491 /*
492  * Pre-allocate playback/capture audio buffer pages.
493  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
494  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
495  */
496 static int sof_pcm_new(struct snd_soc_component *component,
497 		       struct snd_soc_pcm_runtime *rtd)
498 {
499 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
500 	struct snd_sof_pcm *spcm;
501 	struct snd_pcm *pcm = rtd->pcm;
502 	struct snd_soc_tplg_stream_caps *caps;
503 	int stream = SNDRV_PCM_STREAM_PLAYBACK;
504 
505 	/* find SOF PCM for this RTD */
506 	spcm = snd_sof_find_spcm_dai(component, rtd);
507 	if (!spcm) {
508 		dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
509 			 rtd->dai_link->id);
510 		return 0;
511 	}
512 
513 	dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
514 
515 	/* do we need to pre-allocate playback audio buffer pages */
516 	if (!spcm->pcm.playback)
517 		goto capture;
518 
519 	caps = &spcm->pcm.caps[stream];
520 
521 	/* pre-allocate playback audio buffer pages */
522 	dev_dbg(component->dev,
523 		"spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
524 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
525 
526 	if (!pcm->streams[stream].substream) {
527 		dev_err(component->dev, "error: NULL playback substream!\n");
528 		return -EINVAL;
529 	}
530 
531 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
532 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
533 				   0, le32_to_cpu(caps->buffer_size_max));
534 capture:
535 	stream = SNDRV_PCM_STREAM_CAPTURE;
536 
537 	/* do we need to pre-allocate capture audio buffer pages */
538 	if (!spcm->pcm.capture)
539 		return 0;
540 
541 	caps = &spcm->pcm.caps[stream];
542 
543 	/* pre-allocate capture audio buffer pages */
544 	dev_dbg(component->dev,
545 		"spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
546 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
547 
548 	if (!pcm->streams[stream].substream) {
549 		dev_err(component->dev, "error: NULL capture substream!\n");
550 		return -EINVAL;
551 	}
552 
553 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
554 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
555 				   0, le32_to_cpu(caps->buffer_size_max));
556 
557 	return 0;
558 }
559 
560 /* fixup the BE DAI link to match any values from topology */
561 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
562 {
563 	struct snd_interval *rate = hw_param_interval(params,
564 			SNDRV_PCM_HW_PARAM_RATE);
565 	struct snd_interval *channels = hw_param_interval(params,
566 						SNDRV_PCM_HW_PARAM_CHANNELS);
567 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
568 	struct snd_soc_component *component =
569 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
570 	struct snd_sof_dai *dai =
571 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
572 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
573 	const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
574 
575 	/* no topology exists for this BE, try a common configuration */
576 	if (!dai) {
577 		dev_warn(component->dev,
578 			 "warning: no topology found for BE DAI %s config\n",
579 			 rtd->dai_link->name);
580 
581 		/*  set 48k, stereo, 16bits by default */
582 		rate->min = 48000;
583 		rate->max = 48000;
584 
585 		channels->min = 2;
586 		channels->max = 2;
587 
588 		snd_mask_none(fmt);
589 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
590 
591 		return 0;
592 	}
593 
594 	if (pcm_ops->dai_link_fixup)
595 		return pcm_ops->dai_link_fixup(rtd, params);
596 
597 	return 0;
598 }
599 EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
600 
601 static int sof_pcm_probe(struct snd_soc_component *component)
602 {
603 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
604 	struct snd_sof_pdata *plat_data = sdev->pdata;
605 	const char *tplg_filename;
606 	int ret;
607 
608 	/*
609 	 * make sure the device is pm_runtime_active before loading the
610 	 * topology and initiating IPC or bus transactions
611 	 */
612 	ret = pm_runtime_resume_and_get(component->dev);
613 	if (ret < 0 && ret != -EACCES)
614 		return ret;
615 
616 	/* load the default topology */
617 	sdev->component = component;
618 
619 	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
620 				       "%s/%s",
621 				       plat_data->tplg_filename_prefix,
622 				       plat_data->tplg_filename);
623 	if (!tplg_filename)
624 		return -ENOMEM;
625 
626 	ret = snd_sof_load_topology(component, tplg_filename);
627 	if (ret < 0) {
628 		dev_err(component->dev, "error: failed to load DSP topology %d\n",
629 			ret);
630 		return ret;
631 	}
632 
633 	pm_runtime_mark_last_busy(component->dev);
634 	pm_runtime_put_autosuspend(component->dev);
635 
636 	return ret;
637 }
638 
639 static void sof_pcm_remove(struct snd_soc_component *component)
640 {
641 	/* remove topology */
642 	snd_soc_tplg_component_remove(component);
643 }
644 
645 static int sof_pcm_ack(struct snd_soc_component *component,
646 		       struct snd_pcm_substream *substream)
647 {
648 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
649 
650 	return snd_sof_pcm_platform_ack(sdev, substream);
651 }
652 
653 void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
654 {
655 	struct snd_soc_component_driver *pd = &sdev->plat_drv;
656 	struct snd_sof_pdata *plat_data = sdev->pdata;
657 	const char *drv_name;
658 
659 	if (plat_data->machine)
660 		drv_name = plat_data->machine->drv_name;
661 	else if (plat_data->of_machine)
662 		drv_name = plat_data->of_machine->drv_name;
663 	else
664 		drv_name = NULL;
665 
666 	pd->name = "sof-audio-component";
667 	pd->probe = sof_pcm_probe;
668 	pd->remove = sof_pcm_remove;
669 	pd->open = sof_pcm_open;
670 	pd->close = sof_pcm_close;
671 	pd->hw_params = sof_pcm_hw_params;
672 	pd->prepare = sof_pcm_prepare;
673 	pd->hw_free = sof_pcm_hw_free;
674 	pd->trigger = sof_pcm_trigger;
675 	pd->pointer = sof_pcm_pointer;
676 	pd->ack = sof_pcm_ack;
677 
678 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
679 	pd->compress_ops = &sof_compressed_ops;
680 #endif
681 
682 	pd->pcm_construct = sof_pcm_new;
683 	pd->ignore_machine = drv_name;
684 	pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
685 	pd->be_pcm_base = SOF_BE_PCM_BASE;
686 	pd->use_dai_pcm_id = true;
687 	pd->topology_name_prefix = "sof";
688 
689 	 /* increment module refcount when a pcm is opened */
690 	pd->module_get_upon_open = 1;
691 
692 	pd->legacy_dai_naming = 1;
693 }
694