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