xref: /linux-6.15/sound/soc/sof/pcm.c (revision 4d2ea165)
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
7 //
8 // Author: Liam Girdwood <[email protected]>
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 = snd_soc_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 = snd_soc_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 				snd_pcm_direction_name(dir));
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 = snd_soc_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_stream_free(struct snd_sof_dev *sdev,
195 			       struct snd_pcm_substream *substream,
196 			       struct snd_sof_pcm *spcm, int dir,
197 			       bool free_widget_list)
198 {
199 	const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
200 	int ret;
201 	int err = 0;
202 
203 	if (spcm->prepared[substream->stream]) {
204 		/* stop DMA first if needed */
205 		if (pcm_ops && pcm_ops->platform_stop_during_hw_free)
206 			snd_sof_pcm_platform_trigger(sdev, substream,
207 						     SNDRV_PCM_TRIGGER_STOP);
208 
209 		/* free PCM in the DSP */
210 		if (pcm_ops && pcm_ops->hw_free) {
211 			ret = pcm_ops->hw_free(sdev->component, substream);
212 			if (ret < 0) {
213 				dev_err(sdev->dev, "%s: pcm_ops hw_free failed %d\n",
214 					__func__, ret);
215 				err = ret;
216 			}
217 		}
218 
219 		spcm->prepared[substream->stream] = false;
220 		spcm->pending_stop[substream->stream] = false;
221 	}
222 
223 	/* reset the DMA */
224 	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
225 	if (ret < 0) {
226 		dev_err(sdev->dev, "%s: platform hw free failed %d\n",
227 			__func__, ret);
228 		if (!err)
229 			err = ret;
230 	}
231 
232 	/* free widget list */
233 	if (free_widget_list) {
234 		ret = sof_widget_list_free(sdev, spcm, dir);
235 		if (ret < 0) {
236 			dev_err(sdev->dev, "%s: sof_widget_list_free failed %d\n",
237 				__func__, ret);
238 			if (!err)
239 				err = ret;
240 		}
241 	}
242 
243 	return err;
244 }
245 
246 int sof_pcm_free_all_streams(struct snd_sof_dev *sdev)
247 {
248 	struct snd_pcm_substream *substream;
249 	struct snd_sof_pcm *spcm;
250 	int dir, ret;
251 
252 	list_for_each_entry(spcm, &sdev->pcm_list, list) {
253 		for_each_pcm_streams(dir) {
254 			substream = spcm->stream[dir].substream;
255 
256 			if (!substream || !substream->runtime ||
257 			    spcm->stream[dir].suspend_ignored)
258 				continue;
259 
260 			if (spcm->stream[dir].list) {
261 				ret = sof_pcm_stream_free(sdev, substream, spcm,
262 							  dir, true);
263 				if (ret < 0)
264 					return ret;
265 			}
266 		}
267 	}
268 
269 	return 0;
270 }
271 
272 static int sof_pcm_hw_free(struct snd_soc_component *component,
273 			   struct snd_pcm_substream *substream)
274 {
275 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
276 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
277 	struct snd_sof_pcm *spcm;
278 	int ret;
279 
280 	/* nothing to do for BE */
281 	if (rtd->dai_link->no_pcm)
282 		return 0;
283 
284 	spcm = snd_sof_find_spcm_dai(component, rtd);
285 	if (!spcm)
286 		return -EINVAL;
287 
288 	dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
289 		spcm->pcm.pcm_id, substream->stream);
290 
291 	ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, true);
292 
293 	cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
294 
295 	return ret;
296 }
297 
298 static int sof_pcm_prepare(struct snd_soc_component *component,
299 			   struct snd_pcm_substream *substream)
300 {
301 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
302 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
303 	struct snd_sof_pcm *spcm;
304 	int ret;
305 
306 	/* nothing to do for BE */
307 	if (rtd->dai_link->no_pcm)
308 		return 0;
309 
310 	spcm = snd_sof_find_spcm_dai(component, rtd);
311 	if (!spcm)
312 		return -EINVAL;
313 
314 	if (spcm->prepared[substream->stream]) {
315 		if (!spcm->pending_stop[substream->stream])
316 			return 0;
317 
318 		/*
319 		 * this case should be reached in case of xruns where we absolutely
320 		 * want to free-up and reset all PCM/DMA resources
321 		 */
322 		ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, true);
323 		if (ret < 0)
324 			return ret;
325 	}
326 
327 	dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
328 		spcm->pcm.pcm_id, substream->stream);
329 
330 	/* set hw_params */
331 	ret = sof_pcm_hw_params(component,
332 				substream, &spcm->params[substream->stream]);
333 	if (ret < 0) {
334 		dev_err(component->dev,
335 			"error: set pcm hw_params after resume\n");
336 		return ret;
337 	}
338 
339 	return 0;
340 }
341 
342 /*
343  * FE dai link trigger actions are always executed in non-atomic context because
344  * they involve IPC's.
345  */
346 static int sof_pcm_trigger(struct snd_soc_component *component,
347 			   struct snd_pcm_substream *substream, int cmd)
348 {
349 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
350 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
351 	const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
352 	struct snd_sof_pcm *spcm;
353 	bool reset_hw_params = false;
354 	bool ipc_first = false;
355 	int ret = 0;
356 
357 	/* nothing to do for BE */
358 	if (rtd->dai_link->no_pcm)
359 		return 0;
360 
361 	spcm = snd_sof_find_spcm_dai(component, rtd);
362 	if (!spcm)
363 		return -EINVAL;
364 
365 	dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
366 		spcm->pcm.pcm_id, substream->stream, cmd);
367 
368 	spcm->pending_stop[substream->stream] = false;
369 
370 	switch (cmd) {
371 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
372 		ipc_first = true;
373 		break;
374 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
375 		if (pcm_ops && pcm_ops->ipc_first_on_start)
376 			ipc_first = true;
377 		break;
378 	case SNDRV_PCM_TRIGGER_START:
379 		if (spcm->stream[substream->stream].suspend_ignored) {
380 			/*
381 			 * This case will be triggered when INFO_RESUME is
382 			 * not supported, no need to re-start streams that
383 			 * remained enabled in D0ix.
384 			 */
385 			spcm->stream[substream->stream].suspend_ignored = false;
386 			return 0;
387 		}
388 
389 		if (pcm_ops && pcm_ops->ipc_first_on_start)
390 			ipc_first = true;
391 		break;
392 	case SNDRV_PCM_TRIGGER_SUSPEND:
393 		/*
394 		 * If DSP D0I3 is allowed during S0iX, set the suspend_ignored flag for
395 		 * D0I3-compatible streams to keep the firmware pipeline running
396 		 */
397 		if (pcm_ops && pcm_ops->d0i3_supported_in_s0ix &&
398 		    sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
399 		    spcm->stream[substream->stream].d0i3_compatible) {
400 			spcm->stream[substream->stream].suspend_ignored = true;
401 			return 0;
402 		}
403 
404 		/* On suspend the DMA must be stopped in DSPless mode */
405 		if (sdev->dspless_mode_selected)
406 			reset_hw_params = true;
407 
408 		fallthrough;
409 	case SNDRV_PCM_TRIGGER_STOP:
410 		ipc_first = true;
411 		if (pcm_ops && pcm_ops->reset_hw_params_during_stop)
412 			reset_hw_params = true;
413 		break;
414 	default:
415 		dev_err(component->dev, "Unhandled trigger cmd %d\n", cmd);
416 		return -EINVAL;
417 	}
418 
419 	if (!ipc_first)
420 		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
421 
422 	if (pcm_ops && pcm_ops->trigger)
423 		ret = pcm_ops->trigger(component, substream, cmd);
424 
425 	switch (cmd) {
426 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
427 	case SNDRV_PCM_TRIGGER_START:
428 		/* invoke platform trigger to start DMA only if pcm_ops is successful */
429 		if (ipc_first && !ret)
430 			snd_sof_pcm_platform_trigger(sdev, substream, cmd);
431 		break;
432 	case SNDRV_PCM_TRIGGER_SUSPEND:
433 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
434 	case SNDRV_PCM_TRIGGER_STOP:
435 		/* invoke platform trigger to stop DMA even if pcm_ops isn't set or if it failed */
436 		if (!pcm_ops || !pcm_ops->platform_stop_during_hw_free)
437 			snd_sof_pcm_platform_trigger(sdev, substream, cmd);
438 
439 		/*
440 		 * set the pending_stop flag to indicate that pipeline stop has been delayed.
441 		 * This will be used later to stop the pipelines during prepare when recovering
442 		 * from xruns.
443 		 */
444 		if (pcm_ops && pcm_ops->platform_stop_during_hw_free &&
445 		    cmd == SNDRV_PCM_TRIGGER_STOP)
446 			spcm->pending_stop[substream->stream] = true;
447 		break;
448 	default:
449 		break;
450 	}
451 
452 	/* free PCM if reset_hw_params is set and the STOP IPC is successful */
453 	if (!ret && reset_hw_params)
454 		ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, false);
455 
456 	return ret;
457 }
458 
459 static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
460 					 struct snd_pcm_substream *substream)
461 {
462 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
463 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
464 	const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
465 	struct snd_sof_pcm *spcm;
466 	snd_pcm_uframes_t host, dai;
467 	int ret = -EOPNOTSUPP;
468 
469 	/* nothing to do for BE */
470 	if (rtd->dai_link->no_pcm)
471 		return 0;
472 
473 	if (pcm_ops && pcm_ops->pointer)
474 		ret = pcm_ops->pointer(component, substream, &host);
475 
476 	if (ret != -EOPNOTSUPP)
477 		return ret ? ret : host;
478 
479 	/* use dsp ops pointer callback directly if set */
480 	if (sof_ops(sdev)->pcm_pointer)
481 		return sof_ops(sdev)->pcm_pointer(sdev, substream);
482 
483 	spcm = snd_sof_find_spcm_dai(component, rtd);
484 	if (!spcm)
485 		return -EINVAL;
486 
487 	/* read position from DSP */
488 	host = bytes_to_frames(substream->runtime,
489 			       spcm->stream[substream->stream].posn.host_posn);
490 	dai = bytes_to_frames(substream->runtime,
491 			      spcm->stream[substream->stream].posn.dai_posn);
492 
493 	trace_sof_pcm_pointer_position(sdev, spcm, substream, host, dai);
494 
495 	return host;
496 }
497 
498 static int sof_pcm_open(struct snd_soc_component *component,
499 			struct snd_pcm_substream *substream)
500 {
501 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
502 	struct snd_pcm_runtime *runtime = substream->runtime;
503 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
504 	const struct snd_sof_dsp_ops *ops = sof_ops(sdev);
505 	struct snd_sof_pcm *spcm;
506 	struct snd_soc_tplg_stream_caps *caps;
507 	int ret;
508 
509 	/* nothing to do for BE */
510 	if (rtd->dai_link->no_pcm)
511 		return 0;
512 
513 	spcm = snd_sof_find_spcm_dai(component, rtd);
514 	if (!spcm)
515 		return -EINVAL;
516 
517 	dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
518 		spcm->pcm.pcm_id, substream->stream);
519 
520 
521 	caps = &spcm->pcm.caps[substream->stream];
522 
523 	/* set runtime config */
524 	runtime->hw.info = ops->hw_info; /* platform-specific */
525 
526 	/* set any runtime constraints based on topology */
527 	runtime->hw.formats = le64_to_cpu(caps->formats);
528 	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
529 	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
530 	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
531 	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
532 
533 	/*
534 	 * caps->buffer_size_min is not used since the
535 	 * snd_pcm_hardware structure only defines buffer_bytes_max
536 	 */
537 	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
538 
539 	/* set wait time - TODO: come from topology */
540 	substream->wait_time = 500;
541 
542 	spcm->stream[substream->stream].posn.host_posn = 0;
543 	spcm->stream[substream->stream].posn.dai_posn = 0;
544 	spcm->stream[substream->stream].substream = substream;
545 	spcm->prepared[substream->stream] = false;
546 
547 	ret = snd_sof_pcm_platform_open(sdev, substream);
548 	if (ret < 0) {
549 		dev_err(component->dev, "error: pcm open failed %d\n", ret);
550 		return ret;
551 	}
552 
553 	dev_dbg(component->dev, "period bytes min %zd, max %zd\n",
554 		runtime->hw.period_bytes_min,
555 		runtime->hw.period_bytes_max);
556 	dev_dbg(component->dev, "period count min %d, max %d\n",
557 		runtime->hw.periods_min,
558 		runtime->hw.periods_max);
559 	dev_dbg(component->dev, "buffer bytes max %zd\n",
560 		runtime->hw.buffer_bytes_max);
561 
562 	return 0;
563 }
564 
565 static int sof_pcm_close(struct snd_soc_component *component,
566 			 struct snd_pcm_substream *substream)
567 {
568 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
569 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
570 	struct snd_sof_pcm *spcm;
571 	int err;
572 
573 	/* nothing to do for BE */
574 	if (rtd->dai_link->no_pcm)
575 		return 0;
576 
577 	spcm = snd_sof_find_spcm_dai(component, rtd);
578 	if (!spcm)
579 		return -EINVAL;
580 
581 	dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
582 		spcm->pcm.pcm_id, substream->stream);
583 
584 	err = snd_sof_pcm_platform_close(sdev, substream);
585 	if (err < 0) {
586 		dev_err(component->dev, "error: pcm close failed %d\n",
587 			err);
588 		/*
589 		 * keep going, no point in preventing the close
590 		 * from happening
591 		 */
592 	}
593 
594 	return 0;
595 }
596 
597 /*
598  * Pre-allocate playback/capture audio buffer pages.
599  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
600  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
601  */
602 static int sof_pcm_new(struct snd_soc_component *component,
603 		       struct snd_soc_pcm_runtime *rtd)
604 {
605 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
606 	struct snd_sof_pcm *spcm;
607 	struct snd_pcm *pcm = rtd->pcm;
608 	struct snd_soc_tplg_stream_caps *caps;
609 	int stream = SNDRV_PCM_STREAM_PLAYBACK;
610 
611 	/* find SOF PCM for this RTD */
612 	spcm = snd_sof_find_spcm_dai(component, rtd);
613 	if (!spcm) {
614 		dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
615 			 rtd->dai_link->id);
616 		return 0;
617 	}
618 
619 	dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
620 
621 	/* do we need to pre-allocate playback audio buffer pages */
622 	if (!spcm->pcm.playback)
623 		goto capture;
624 
625 	caps = &spcm->pcm.caps[stream];
626 
627 	/* pre-allocate playback audio buffer pages */
628 	dev_dbg(component->dev,
629 		"spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
630 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
631 
632 	if (!pcm->streams[stream].substream) {
633 		dev_err(component->dev, "error: NULL playback substream!\n");
634 		return -EINVAL;
635 	}
636 
637 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
638 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
639 				   0, le32_to_cpu(caps->buffer_size_max));
640 capture:
641 	stream = SNDRV_PCM_STREAM_CAPTURE;
642 
643 	/* do we need to pre-allocate capture audio buffer pages */
644 	if (!spcm->pcm.capture)
645 		return 0;
646 
647 	caps = &spcm->pcm.caps[stream];
648 
649 	/* pre-allocate capture audio buffer pages */
650 	dev_dbg(component->dev,
651 		"spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
652 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
653 
654 	if (!pcm->streams[stream].substream) {
655 		dev_err(component->dev, "error: NULL capture substream!\n");
656 		return -EINVAL;
657 	}
658 
659 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
660 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
661 				   0, le32_to_cpu(caps->buffer_size_max));
662 
663 	return 0;
664 }
665 
666 /* fixup the BE DAI link to match any values from topology */
667 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
668 {
669 	struct snd_interval *rate = hw_param_interval(params,
670 			SNDRV_PCM_HW_PARAM_RATE);
671 	struct snd_interval *channels = hw_param_interval(params,
672 						SNDRV_PCM_HW_PARAM_CHANNELS);
673 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
674 	struct snd_soc_component *component =
675 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
676 	struct snd_sof_dai *dai =
677 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
678 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
679 	const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
680 
681 	/* no topology exists for this BE, try a common configuration */
682 	if (!dai) {
683 		dev_warn(component->dev,
684 			 "warning: no topology found for BE DAI %s config\n",
685 			 rtd->dai_link->name);
686 
687 		/*  set 48k, stereo, 16bits by default */
688 		rate->min = 48000;
689 		rate->max = 48000;
690 
691 		channels->min = 2;
692 		channels->max = 2;
693 
694 		snd_mask_none(fmt);
695 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
696 
697 		return 0;
698 	}
699 
700 	if (pcm_ops && pcm_ops->dai_link_fixup)
701 		return pcm_ops->dai_link_fixup(rtd, params);
702 
703 	return 0;
704 }
705 EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
706 
707 static int sof_pcm_probe(struct snd_soc_component *component)
708 {
709 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
710 	struct snd_sof_pdata *plat_data = sdev->pdata;
711 	const char *tplg_filename;
712 	int ret;
713 
714 	/*
715 	 * make sure the device is pm_runtime_active before loading the
716 	 * topology and initiating IPC or bus transactions
717 	 */
718 	ret = pm_runtime_resume_and_get(component->dev);
719 	if (ret < 0 && ret != -EACCES)
720 		return ret;
721 
722 	/* load the default topology */
723 	sdev->component = component;
724 
725 	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
726 				       "%s/%s",
727 				       plat_data->tplg_filename_prefix,
728 				       plat_data->tplg_filename);
729 	if (!tplg_filename) {
730 		ret = -ENOMEM;
731 		goto pm_error;
732 	}
733 
734 	ret = snd_sof_load_topology(component, tplg_filename);
735 	if (ret < 0)
736 		dev_err(component->dev, "error: failed to load DSP topology %d\n",
737 			ret);
738 
739 pm_error:
740 	pm_runtime_mark_last_busy(component->dev);
741 	pm_runtime_put_autosuspend(component->dev);
742 
743 	return ret;
744 }
745 
746 static void sof_pcm_remove(struct snd_soc_component *component)
747 {
748 	/* remove topology */
749 	snd_soc_tplg_component_remove(component);
750 }
751 
752 static int sof_pcm_ack(struct snd_soc_component *component,
753 		       struct snd_pcm_substream *substream)
754 {
755 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
756 
757 	return snd_sof_pcm_platform_ack(sdev, substream);
758 }
759 
760 static snd_pcm_sframes_t sof_pcm_delay(struct snd_soc_component *component,
761 				       struct snd_pcm_substream *substream)
762 {
763 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
764 	const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
765 
766 	if (pcm_ops && pcm_ops->delay)
767 		return pcm_ops->delay(component, substream);
768 
769 	return 0;
770 }
771 
772 void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
773 {
774 	struct snd_soc_component_driver *pd = &sdev->plat_drv;
775 	struct snd_sof_pdata *plat_data = sdev->pdata;
776 	const char *drv_name;
777 
778 	if (plat_data->machine)
779 		drv_name = plat_data->machine->drv_name;
780 	else if (plat_data->of_machine)
781 		drv_name = plat_data->of_machine->drv_name;
782 	else
783 		drv_name = NULL;
784 
785 	pd->name = "sof-audio-component";
786 	pd->probe = sof_pcm_probe;
787 	pd->remove = sof_pcm_remove;
788 	pd->open = sof_pcm_open;
789 	pd->close = sof_pcm_close;
790 	pd->hw_params = sof_pcm_hw_params;
791 	pd->prepare = sof_pcm_prepare;
792 	pd->hw_free = sof_pcm_hw_free;
793 	pd->trigger = sof_pcm_trigger;
794 	pd->pointer = sof_pcm_pointer;
795 	pd->ack = sof_pcm_ack;
796 	pd->delay = sof_pcm_delay;
797 
798 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
799 	pd->compress_ops = &sof_compressed_ops;
800 #endif
801 
802 	pd->pcm_construct = sof_pcm_new;
803 	pd->ignore_machine = drv_name;
804 	pd->be_pcm_base = SOF_BE_PCM_BASE;
805 	pd->use_dai_pcm_id = true;
806 	pd->topology_name_prefix = "sof";
807 
808 	 /* increment module refcount when a pcm is opened */
809 	pd->module_get_upon_open = 1;
810 
811 	pd->legacy_dai_naming = 1;
812 
813 	/*
814 	 * The fixup is only needed when the DSP is in use as with the DSPless
815 	 * mode we are directly using the audio interface
816 	 */
817 	if (!sdev->dspless_mode_selected)
818 		pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
819 }
820