xref: /linux-6.15/sound/soc/sof/pcm.c (revision 169ec0a5)
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 	dev_dbg(component->dev, "period min %zd max %zd bytes\n",
540 		runtime->hw.period_bytes_min,
541 		runtime->hw.period_bytes_max);
542 	dev_dbg(component->dev, "period count %d max %d\n",
543 		runtime->hw.periods_min,
544 		runtime->hw.periods_max);
545 	dev_dbg(component->dev, "buffer max %zd bytes\n",
546 		runtime->hw.buffer_bytes_max);
547 
548 	/* set wait time - TODO: come from topology */
549 	substream->wait_time = 500;
550 
551 	spcm->stream[substream->stream].posn.host_posn = 0;
552 	spcm->stream[substream->stream].posn.dai_posn = 0;
553 	spcm->stream[substream->stream].substream = substream;
554 	spcm->prepared[substream->stream] = false;
555 
556 	ret = snd_sof_pcm_platform_open(sdev, substream);
557 	if (ret < 0)
558 		dev_err(component->dev, "error: pcm open failed %d\n", ret);
559 
560 	return ret;
561 }
562 
563 static int sof_pcm_close(struct snd_soc_component *component,
564 			 struct snd_pcm_substream *substream)
565 {
566 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
567 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
568 	struct snd_sof_pcm *spcm;
569 	int err;
570 
571 	/* nothing to do for BE */
572 	if (rtd->dai_link->no_pcm)
573 		return 0;
574 
575 	spcm = snd_sof_find_spcm_dai(component, rtd);
576 	if (!spcm)
577 		return -EINVAL;
578 
579 	dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
580 		spcm->pcm.pcm_id, substream->stream);
581 
582 	err = snd_sof_pcm_platform_close(sdev, substream);
583 	if (err < 0) {
584 		dev_err(component->dev, "error: pcm close failed %d\n",
585 			err);
586 		/*
587 		 * keep going, no point in preventing the close
588 		 * from happening
589 		 */
590 	}
591 
592 	return 0;
593 }
594 
595 /*
596  * Pre-allocate playback/capture audio buffer pages.
597  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
598  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
599  */
600 static int sof_pcm_new(struct snd_soc_component *component,
601 		       struct snd_soc_pcm_runtime *rtd)
602 {
603 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
604 	struct snd_sof_pcm *spcm;
605 	struct snd_pcm *pcm = rtd->pcm;
606 	struct snd_soc_tplg_stream_caps *caps;
607 	int stream = SNDRV_PCM_STREAM_PLAYBACK;
608 
609 	/* find SOF PCM for this RTD */
610 	spcm = snd_sof_find_spcm_dai(component, rtd);
611 	if (!spcm) {
612 		dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
613 			 rtd->dai_link->id);
614 		return 0;
615 	}
616 
617 	dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
618 
619 	/* do we need to pre-allocate playback audio buffer pages */
620 	if (!spcm->pcm.playback)
621 		goto capture;
622 
623 	caps = &spcm->pcm.caps[stream];
624 
625 	/* pre-allocate playback audio buffer pages */
626 	dev_dbg(component->dev,
627 		"spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
628 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
629 
630 	if (!pcm->streams[stream].substream) {
631 		dev_err(component->dev, "error: NULL playback substream!\n");
632 		return -EINVAL;
633 	}
634 
635 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
636 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
637 				   0, le32_to_cpu(caps->buffer_size_max));
638 capture:
639 	stream = SNDRV_PCM_STREAM_CAPTURE;
640 
641 	/* do we need to pre-allocate capture audio buffer pages */
642 	if (!spcm->pcm.capture)
643 		return 0;
644 
645 	caps = &spcm->pcm.caps[stream];
646 
647 	/* pre-allocate capture audio buffer pages */
648 	dev_dbg(component->dev,
649 		"spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
650 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
651 
652 	if (!pcm->streams[stream].substream) {
653 		dev_err(component->dev, "error: NULL capture substream!\n");
654 		return -EINVAL;
655 	}
656 
657 	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
658 				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
659 				   0, le32_to_cpu(caps->buffer_size_max));
660 
661 	return 0;
662 }
663 
664 /* fixup the BE DAI link to match any values from topology */
665 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
666 {
667 	struct snd_interval *rate = hw_param_interval(params,
668 			SNDRV_PCM_HW_PARAM_RATE);
669 	struct snd_interval *channels = hw_param_interval(params,
670 						SNDRV_PCM_HW_PARAM_CHANNELS);
671 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
672 	struct snd_soc_component *component =
673 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
674 	struct snd_sof_dai *dai =
675 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
676 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
677 	const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
678 
679 	/* no topology exists for this BE, try a common configuration */
680 	if (!dai) {
681 		dev_warn(component->dev,
682 			 "warning: no topology found for BE DAI %s config\n",
683 			 rtd->dai_link->name);
684 
685 		/*  set 48k, stereo, 16bits by default */
686 		rate->min = 48000;
687 		rate->max = 48000;
688 
689 		channels->min = 2;
690 		channels->max = 2;
691 
692 		snd_mask_none(fmt);
693 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
694 
695 		return 0;
696 	}
697 
698 	if (pcm_ops && pcm_ops->dai_link_fixup)
699 		return pcm_ops->dai_link_fixup(rtd, params);
700 
701 	return 0;
702 }
703 EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
704 
705 static int sof_pcm_probe(struct snd_soc_component *component)
706 {
707 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
708 	struct snd_sof_pdata *plat_data = sdev->pdata;
709 	const char *tplg_filename;
710 	int ret;
711 
712 	/*
713 	 * make sure the device is pm_runtime_active before loading the
714 	 * topology and initiating IPC or bus transactions
715 	 */
716 	ret = pm_runtime_resume_and_get(component->dev);
717 	if (ret < 0 && ret != -EACCES)
718 		return ret;
719 
720 	/* load the default topology */
721 	sdev->component = component;
722 
723 	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
724 				       "%s/%s",
725 				       plat_data->tplg_filename_prefix,
726 				       plat_data->tplg_filename);
727 	if (!tplg_filename) {
728 		ret = -ENOMEM;
729 		goto pm_error;
730 	}
731 
732 	ret = snd_sof_load_topology(component, tplg_filename);
733 	if (ret < 0)
734 		dev_err(component->dev, "error: failed to load DSP topology %d\n",
735 			ret);
736 
737 pm_error:
738 	pm_runtime_mark_last_busy(component->dev);
739 	pm_runtime_put_autosuspend(component->dev);
740 
741 	return ret;
742 }
743 
744 static void sof_pcm_remove(struct snd_soc_component *component)
745 {
746 	/* remove topology */
747 	snd_soc_tplg_component_remove(component);
748 }
749 
750 static int sof_pcm_ack(struct snd_soc_component *component,
751 		       struct snd_pcm_substream *substream)
752 {
753 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
754 
755 	return snd_sof_pcm_platform_ack(sdev, substream);
756 }
757 
758 static snd_pcm_sframes_t sof_pcm_delay(struct snd_soc_component *component,
759 				       struct snd_pcm_substream *substream)
760 {
761 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
762 	const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
763 
764 	if (pcm_ops && pcm_ops->delay)
765 		return pcm_ops->delay(component, substream);
766 
767 	return 0;
768 }
769 
770 void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
771 {
772 	struct snd_soc_component_driver *pd = &sdev->plat_drv;
773 	struct snd_sof_pdata *plat_data = sdev->pdata;
774 	const char *drv_name;
775 
776 	if (plat_data->machine)
777 		drv_name = plat_data->machine->drv_name;
778 	else if (plat_data->of_machine)
779 		drv_name = plat_data->of_machine->drv_name;
780 	else
781 		drv_name = NULL;
782 
783 	pd->name = "sof-audio-component";
784 	pd->probe = sof_pcm_probe;
785 	pd->remove = sof_pcm_remove;
786 	pd->open = sof_pcm_open;
787 	pd->close = sof_pcm_close;
788 	pd->hw_params = sof_pcm_hw_params;
789 	pd->prepare = sof_pcm_prepare;
790 	pd->hw_free = sof_pcm_hw_free;
791 	pd->trigger = sof_pcm_trigger;
792 	pd->pointer = sof_pcm_pointer;
793 	pd->ack = sof_pcm_ack;
794 	pd->delay = sof_pcm_delay;
795 
796 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
797 	pd->compress_ops = &sof_compressed_ops;
798 #endif
799 
800 	pd->pcm_construct = sof_pcm_new;
801 	pd->ignore_machine = drv_name;
802 	pd->be_pcm_base = SOF_BE_PCM_BASE;
803 	pd->use_dai_pcm_id = true;
804 	pd->topology_name_prefix = "sof";
805 
806 	 /* increment module refcount when a pcm is opened */
807 	pd->module_get_upon_open = 1;
808 
809 	pd->legacy_dai_naming = 1;
810 
811 	/*
812 	 * The fixup is only needed when the DSP is in use as with the DSPless
813 	 * mode we are directly using the audio interface
814 	 */
815 	if (!sdev->dspless_mode_selected)
816 		pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
817 }
818