Skip to content

Commit 57524a2

Browse files
committed
ASoC: SOF: sof-audio: Add support for loopback capture
An example of a DAI-less loopback pipeline would be the echo reference capture in the speaker playback path. This pipeline is set up as follows: Host(Playback) -> mixin -> mixout -> gain -> module-copier -> DAI | V Host(Capture) <- Process module <- virtual DAI In the above example, the virtual DAI exploits the concept of an aggregated DAI (one with a non-zero DAI ID) in topology to enable this pipeline to work with DPCM. A virtual DAI is a DAI widget with a non-zero DAI ID and hence is skipped when traversing the list of DAPM widgets during widget prepare/set/up/free/unprepare. The process module in the above pipeline generates 0's that are captured by the echo reference PCM. When the playback path is active, the process module acts as a passthrough module to allow the playback samples to be passthrough to the capture host. In order for these pipelines to work properly, the logic for setting/preparing/freeing/unpreparing the widgets needs to be amended to make sure that only the widgets that are in the pipeline in the same direction as the PCM being started are set up. For example, when the playback PCM is started, the capture pipeline widgets also show up in the list of connected DAPM widgets but they shouldn't be set up yet because the echo reference capture PCM hasn't been started yet. Alternatively, when the echo reference capture PCM is started, the playback pipeline widgets should not be setup. Finally, the last step needed to put this all together is the set the routes for widgets connecting the playback and the capture pipelines when both are active. Signed-off-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
1 parent 31870fc commit 57524a2

File tree

1 file changed

+99
-52
lines changed

1 file changed

+99
-52
lines changed

sound/soc/sof/sof-audio.c

Lines changed: 99 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsourc
269269
is_virtual_widget(sdev, sink_widget->widget, __func__))
270270
return 0;
271271

272+
/* skip route if source/sink widget is not set up */
273+
if (!src_widget->use_count || !sink_widget->use_count)
274+
return 0;
275+
272276
/* find route matching source and sink widgets */
273277
list_for_each_entry(sroute, &sdev->route_list, list)
274278
if (sroute->src_widget == src_widget && sroute->sink_widget == sink_widget) {
@@ -297,10 +301,37 @@ int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsourc
297301
return 0;
298302
}
299303

304+
static bool sof_widget_in_same_direction(struct snd_sof_widget *swidget, int dir)
305+
{
306+
if (swidget->spipe->direction == dir)
307+
return true;
308+
309+
return false;
310+
}
311+
312+
static int sof_set_up_same_dir_widget_routes(struct snd_sof_dev *sdev,
313+
struct snd_soc_dapm_widget *wsource,
314+
struct snd_soc_dapm_widget *wsink)
315+
{
316+
struct snd_sof_widget *src_widget = wsource->dobj.private;
317+
struct snd_sof_widget *sink_widget = wsink->dobj.private;
318+
319+
/*
320+
* skip setting up route if source and sink are in different directions (ex. playback and
321+
* echo ref) if the direction is set in topology. These will be set up later. It is enough
322+
* to check if the direction_valid is set for one of the widgets as all widgets will have
323+
* the direction set in topology if one is set.
324+
*/
325+
if (sink_widget->spipe->direction_valid &&
326+
!sof_widget_in_same_direction(sink_widget, src_widget->spipe->direction))
327+
return 0;
328+
329+
return sof_route_setup(sdev, wsource, wsink);
330+
}
331+
300332
static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
301333
struct snd_soc_dapm_widget_list *list, int dir)
302334
{
303-
const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
304335
struct snd_soc_dapm_widget *widget;
305336
struct snd_sof_route *sroute;
306337
struct snd_soc_dapm_path *p;
@@ -323,7 +354,7 @@ static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
323354
continue;
324355

325356
if (p->sink->dobj.private) {
326-
ret = sof_route_setup(sdev, widget, p->sink);
357+
ret = sof_set_up_same_dir_widget_routes(sdev, widget, p->sink);
327358
if (ret < 0)
328359
return ret;
329360
}
@@ -339,7 +370,7 @@ static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
339370
continue;
340371

341372
if (p->source->dobj.private) {
342-
ret = sof_route_setup(sdev, p->source, widget);
373+
ret = sof_set_up_same_dir_widget_routes(sdev, p->source, widget);
343374
if (ret < 0)
344375
return ret;
345376
}
@@ -355,7 +386,6 @@ static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
355386
*/
356387
list_for_each_entry(sroute, &sdev->route_list, list) {
357388
bool src_widget_in_dapm_list, sink_widget_in_dapm_list;
358-
struct snd_sof_widget *swidget;
359389

360390
if (sroute->setup)
361391
continue;
@@ -364,50 +394,49 @@ static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
364394
sink_widget_in_dapm_list = widget_in_list(list, sroute->sink_widget->widget);
365395

366396
/*
367-
* if both source and sink are in the DAPM list, the route must already have been
368-
* set up above. And if neither are in the DAPM list, the route shouldn't be
369-
* handled now.
397+
* no need to set up the route if both the source and sink widgets are not in the
398+
* DAPM list
370399
*/
371-
if (src_widget_in_dapm_list == sink_widget_in_dapm_list)
400+
if (!src_widget_in_dapm_list && !sink_widget_in_dapm_list)
372401
continue;
373402

374403
/*
375-
* At this point either the source widget or the sink widget is in the DAPM list
376-
* with a route that might need to be set up. Check the use_count of the widget
377-
* that is not in the DAPM list to confirm if it is in use currently before setting
378-
* up the route.
404+
* set up the route only if both the source and sink widgets are in the DAPM list
405+
* but are in different directions. The ones in the same direction would already
406+
* have been set up in the previous loop.
379407
*/
380-
if (src_widget_in_dapm_list)
381-
swidget = sroute->sink_widget;
382-
else
383-
swidget = sroute->src_widget;
408+
if (src_widget_in_dapm_list && sink_widget_in_dapm_list) {
409+
struct snd_sof_widget *src_widget, *sink_widget;
384410

385-
scoped_guard(mutex, &swidget->setup_mutex) {
386-
if (!swidget->use_count)
387-
continue;
411+
src_widget = sroute->src_widget->widget->dobj.private;
412+
sink_widget = sroute->sink_widget->widget->dobj.private;
388413

389-
if (tplg_ops && tplg_ops->route_setup) {
390-
/*
391-
* this route will get freed when either the
392-
* source widget or the sink widget is freed
393-
* during hw_free
394-
*/
395-
ret = tplg_ops->route_setup(sdev, sroute);
396-
if (!ret)
397-
sroute->setup = true;
398-
}
414+
/*
415+
* it is enough to check if the direction_valid is set for one of the
416+
* widgets as all widgets will have the direction set in topology if one
417+
* is set.
418+
*/
419+
if (src_widget && sink_widget &&
420+
src_widget->spipe->direction_valid &&
421+
sof_widget_in_same_direction(sink_widget, src_widget->spipe->direction))
422+
continue;
399423
}
400424

425+
ret = sof_route_setup(sdev, sroute->src_widget->widget,
426+
sroute->sink_widget->widget);
427+
401428
if (ret < 0)
402429
return ret;
403430
}
404431

405432
return 0;
406433
}
407434

435+
436+
408437
static void
409438
sof_unprepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
410-
struct snd_soc_dapm_widget_list *list)
439+
struct snd_soc_dapm_widget_list *list, int dir)
411440
{
412441
const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
413442
struct snd_sof_widget *swidget = widget->dobj.private;
@@ -417,9 +446,14 @@ sof_unprepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widg
417446
if (is_virtual_widget(sdev, widget, __func__))
418447
return;
419448

420-
/* skip if the widget in use or already unprepared or is an aggregated DAI */
421-
if (!swidget || !swidget->prepared || swidget->use_count > 0 ||
422-
is_aggregated_dai(swidget))
449+
if (!swidget)
450+
goto sink_unprepare;
451+
452+
if (swidget->spipe->direction_valid && !sof_widget_in_same_direction(swidget, dir))
453+
return;
454+
455+
/* skip widgets in use, those already unprepared or aggregated DAIs */
456+
if (!swidget->prepared || swidget->use_count > 0 || is_aggregated_dai(swidget))
423457
goto sink_unprepare;
424458

425459
widget_ops = tplg_ops ? tplg_ops->widget : NULL;
@@ -434,9 +468,10 @@ sof_unprepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widg
434468
snd_soc_dapm_widget_for_each_sink_path(widget, p) {
435469
if (!widget_in_list(list, p->sink))
436470
continue;
471+
437472
if (!p->walking && p->sink->dobj.private) {
438473
p->walking = true;
439-
sof_unprepare_widgets_in_path(sdev, p->sink, list);
474+
sof_unprepare_widgets_in_path(sdev, p->sink, list, dir);
440475
p->walking = false;
441476
}
442477
}
@@ -458,15 +493,19 @@ sof_prepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget
458493
if (is_virtual_widget(sdev, widget, __func__))
459494
return 0;
460495

496+
if (!swidget)
497+
goto sink_prepare;
498+
461499
widget_ops = tplg_ops ? tplg_ops->widget : NULL;
462500
if (!widget_ops)
463501
return 0;
464502

465-
if (!swidget || !widget_ops[widget->id].ipc_prepare || swidget->prepared)
466-
goto sink_prepare;
503+
if (swidget->spipe->direction_valid && !sof_widget_in_same_direction(swidget, dir))
504+
return 0;
467505

468-
/* skip aggregated DAIs */
469-
if (is_aggregated_dai(swidget))
506+
/* skip widgets already prepared or aggregated DAI widgets*/
507+
if (!widget_ops[widget->id].ipc_prepare || swidget->prepared ||
508+
is_aggregated_dai(swidget))
470509
goto sink_prepare;
471510

472511
/* prepare the source widget */
@@ -484,6 +523,7 @@ sof_prepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget
484523
snd_soc_dapm_widget_for_each_sink_path(widget, p) {
485524
if (!widget_in_list(list, p->sink))
486525
continue;
526+
487527
if (!p->walking && p->sink->dobj.private) {
488528
p->walking = true;
489529
ret = sof_prepare_widgets_in_path(sdev, p->sink, fe_params,
@@ -513,23 +553,28 @@ static int sof_free_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dap
513553
int dir, struct snd_sof_pcm *spcm)
514554
{
515555
struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
516-
struct snd_sof_widget *swidget;
556+
struct snd_sof_widget *swidget = widget->dobj.private;
517557
struct snd_soc_dapm_path *p;
518558
int err;
519559
int ret = 0;
520560

521561
if (is_virtual_widget(sdev, widget, __func__))
522562
return 0;
563+
564+
if (!swidget)
565+
goto sink_free;
523566

524-
swidget = widget->dobj.private;
567+
if (swidget->spipe->direction_valid && !sof_widget_in_same_direction(swidget, dir))
568+
return 0;
525569

526-
/* no need to free aggregated DAI widgets */
527-
if (swidget && !is_aggregated_dai(swidget)) {
528-
err = sof_widget_free(sdev, swidget);
529-
if (err < 0)
530-
ret = err;
531-
}
570+
/* skip aggregated DAIs */
571+
if (is_aggregated_dai(swidget))
572+
goto sink_free;
532573

574+
err = sof_widget_free(sdev, widget->dobj.private);
575+
if (err < 0)
576+
ret = err;
577+
sink_free:
533578
/* free all widgets in the sink paths even in case of error to keep use counts balanced */
534579
snd_soc_dapm_widget_for_each_sink_path(widget, p) {
535580
if (!p->walking) {
@@ -569,6 +614,10 @@ static int sof_set_up_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_d
569614
if (swidget) {
570615
int i;
571616

617+
if (swidget->spipe->direction_valid && !sof_widget_in_same_direction(swidget, dir))
618+
return 0;
619+
620+
/* skip aggregated DAIs */
572621
if (is_aggregated_dai(swidget))
573622
goto sink_setup;
574623

@@ -634,15 +683,13 @@ sof_walk_widgets_in_order(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
634683
return 0;
635684

636685
for_each_dapm_widgets(list, i, widget) {
637-
if (is_virtual_widget(sdev, widget, __func__))
638-
continue;
639-
640-
/* starting widget for playback is AIF type */
686+
/* starting widget for playback is of AIF type */
641687
if (dir == SNDRV_PCM_STREAM_PLAYBACK && widget->id != snd_soc_dapm_aif_in)
642688
continue;
643689

644690
/* starting widget for capture is DAI type */
645-
if (dir == SNDRV_PCM_STREAM_CAPTURE && widget->id != snd_soc_dapm_dai_out)
691+
if (dir == SNDRV_PCM_STREAM_CAPTURE && widget->id != snd_soc_dapm_dai_out &&
692+
widget->id != snd_soc_dapm_output)
646693
continue;
647694

648695
switch (op) {
@@ -672,7 +719,7 @@ sof_walk_widgets_in_order(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
672719
break;
673720
}
674721
case SOF_WIDGET_UNPREPARE:
675-
sof_unprepare_widgets_in_path(sdev, widget, list);
722+
sof_unprepare_widgets_in_path(sdev, widget, list, dir);
676723
break;
677724
default:
678725
dev_err(sdev->dev, "Invalid widget op %d\n", op);

0 commit comments

Comments
 (0)