@@ -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,10 @@ 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 );
300305static int sof_setup_pipeline_connections (struct snd_sof_dev * sdev ,
301306 struct snd_soc_dapm_widget_list * list , int dir )
302307{
303- const struct sof_ipc_tplg_ops * tplg_ops = sof_ipc_get_ops (sdev , tplg );
304308 struct snd_soc_dapm_widget * widget ;
305309 struct snd_sof_route * sroute ;
306310 struct snd_soc_dapm_path * p ;
@@ -315,14 +319,25 @@ static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
315319 */
316320 if (dir == SNDRV_PCM_STREAM_PLAYBACK ) {
317321 for_each_dapm_widgets (list , i , widget ) {
322+ struct snd_sof_widget * src_widget , * sink_widget ;
323+
318324 if (!widget -> dobj .private )
319325 continue ;
320326
321327 snd_soc_dapm_widget_for_each_sink_path (widget , p ) {
322328 if (!widget_in_list (list , p -> sink ))
323329 continue ;
330+ src_widget = widget -> dobj .private ;
324331
325332 if (p -> sink -> dobj .private ) {
333+ sink_widget = p -> sink -> dobj .private ;
334+
335+ /*
336+ * skip if source and sink are in different directions
337+ * (ex. playback and echo ref). These will be set up later.
338+ */
339+ if (!sof_widget_in_same_direction (sink_widget , src_widget -> spipe -> direction ))
340+ continue ;
326341 ret = sof_route_setup (sdev , widget , p -> sink );
327342 if (ret < 0 )
328343 return ret ;
@@ -331,14 +346,27 @@ static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
331346 }
332347 } else {
333348 for_each_dapm_widgets (list , i , widget ) {
349+ struct snd_sof_widget * src_widget , * sink_widget ;
350+
334351 if (!widget -> dobj .private )
335352 continue ;
336353
337354 snd_soc_dapm_widget_for_each_source_path (widget , p ) {
338355 if (!widget_in_list (list , p -> source ))
339356 continue ;
340357
358+ sink_widget = widget -> dobj .private ;
359+
341360 if (p -> source -> dobj .private ) {
361+ src_widget = p -> source -> dobj .private ;
362+
363+ /*
364+ * skip if source and sink are in different directions
365+ * (ex. playback and echo ref). These will be set up later.
366+ */
367+ if (!sof_widget_in_same_direction (sink_widget , src_widget -> spipe -> direction ))
368+ continue ;
369+
342370 ret = sof_route_setup (sdev , p -> source , widget );
343371 if (ret < 0 )
344372 return ret ;
@@ -355,7 +383,6 @@ static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
355383 */
356384 list_for_each_entry (sroute , & sdev -> route_list , list ) {
357385 bool src_widget_in_dapm_list , sink_widget_in_dapm_list ;
358- struct snd_sof_widget * swidget ;
359386
360387 if (sroute -> setup )
361388 continue ;
@@ -364,63 +391,62 @@ static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
364391 sink_widget_in_dapm_list = widget_in_list (list , sroute -> sink_widget -> widget );
365392
366393 /*
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.
394+ * no need to set up the route if both the source and sink widgets are not in the
395+ * DAPM list
370396 */
371- if (src_widget_in_dapm_list == sink_widget_in_dapm_list )
397+ if (! src_widget_in_dapm_list && ! sink_widget_in_dapm_list )
372398 continue ;
373399
374400 /*
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.
401+ * set up the route only if both the source and sink widgets are in the DAPM list
402+ * but are in different directions. The ones in the same direction would already have
403+ * been set up in the previous loop.
379404 */
380- if (src_widget_in_dapm_list )
381- swidget = sroute -> sink_widget ;
382- else
383- swidget = sroute -> src_widget ;
405+ if (src_widget_in_dapm_list && sink_widget_in_dapm_list ) {
406+ struct snd_sof_widget * src_widget , * sink_widget ;
384407
385- scoped_guard (mutex , & swidget -> setup_mutex ) {
386- if (!swidget -> use_count )
387- continue ;
408+ src_widget = sroute -> src_widget -> widget -> dobj .private ;
409+ sink_widget = sroute -> sink_widget -> widget -> dobj .private ;
388410
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- }
411+ if (src_widget && sink_widget && sof_widget_in_same_direction (sink_widget , src_widget -> spipe -> direction ))
412+ continue ;
399413 }
400414
415+ ret = sof_route_setup (sdev , sroute -> src_widget -> widget ,
416+ sroute -> sink_widget -> widget );
417+
401418 if (ret < 0 )
402419 return ret ;
403420 }
404421
405422 return 0 ;
406423}
407424
425+ static bool sof_widget_in_same_direction (struct snd_sof_widget * swidget , int dir )
426+ {
427+ if (swidget -> spipe -> direction == dir )
428+ return true;
429+
430+ return false;
431+ }
432+
408433static void
409434sof_unprepare_widgets_in_path (struct snd_sof_dev * sdev , struct snd_soc_dapm_widget * widget ,
410- struct snd_soc_dapm_widget_list * list )
435+ struct snd_soc_dapm_widget_list * list , int dir )
411436{
412437 const struct sof_ipc_tplg_ops * tplg_ops = sof_ipc_get_ops (sdev , tplg );
413438 struct snd_sof_widget * swidget = widget -> dobj .private ;
414439 const struct sof_ipc_tplg_widget_ops * widget_ops ;
415440 struct snd_soc_dapm_path * p ;
416441
417- /* skip if the widget is in use or if it is already unprepared */
418- if (!swidget || !swidget -> prepared || swidget -> use_count > 0 ||
419- is_virtual_widget (sdev , widget , __func__ ))
442+ if (!swidget || is_virtual_widget (sdev , widget , __func__ ))
420443 goto sink_unprepare ;
421444
422- /* skip aggregated DAIs */
423- if (is_aggregated_dai (swidget ))
445+ if (!sof_widget_in_same_direction (swidget , dir ))
446+ return ;
447+
448+ /* skip widgets in use, those already unprepared or aggregated DAIs */
449+ if (!swidget -> prepared || swidget -> use_count > 0 || is_aggregated_dai (swidget ))
424450 goto sink_unprepare ;
425451
426452 widget_ops = tplg_ops ? tplg_ops -> widget : NULL ;
@@ -435,9 +461,10 @@ sof_unprepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widg
435461 snd_soc_dapm_widget_for_each_sink_path (widget , p ) {
436462 if (!widget_in_list (list , p -> sink ))
437463 continue ;
464+
438465 if (!p -> walking && p -> sink -> dobj .private ) {
439466 p -> walking = true;
440- sof_unprepare_widgets_in_path (sdev , p -> sink , list );
467+ sof_unprepare_widgets_in_path (sdev , p -> sink , list , dir );
441468 p -> walking = false;
442469 }
443470 }
@@ -456,18 +483,21 @@ sof_prepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget
456483 struct snd_soc_dapm_path * p ;
457484 int ret ;
458485
459- if (is_virtual_widget (sdev , widget , __func__ ))
486+ dev_dbg (sdev -> dev , "%s: preparing widget %s id %d\n" ,
487+ __func__ , widget -> name , widget -> id );
488+
489+ if (is_virtual_widget (sdev , widget , __func__ ) || !swidget )
460490 goto sink_prepare ;
461491
462492 widget_ops = tplg_ops ? tplg_ops -> widget : NULL ;
463493 if (!widget_ops )
464494 return 0 ;
465495
466- if (!swidget || ! widget_ops [ widget -> id ]. ipc_prepare || swidget -> prepared )
467- goto sink_prepare ;
496+ if (!sof_widget_in_same_direction ( swidget , dir ) )
497+ return 0 ;
468498
469- /* skip aggregated DAIs */
470- if ( is_aggregated_dai (swidget ))
499+ if (! widget_ops [ widget -> id ]. ipc_prepare || swidget -> prepared ||
500+ is_aggregated_dai (swidget ))
471501 goto sink_prepare ;
472502
473503 /* prepare the source widget */
@@ -485,6 +515,7 @@ sof_prepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget
485515 snd_soc_dapm_widget_for_each_sink_path (widget , p ) {
486516 if (!widget_in_list (list , p -> sink ))
487517 continue ;
518+
488519 if (!p -> walking && p -> sink -> dobj .private ) {
489520 p -> walking = true;
490521 ret = sof_prepare_widgets_in_path (sdev , p -> sink , fe_params ,
@@ -514,23 +545,25 @@ static int sof_free_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dap
514545 int dir , struct snd_sof_pcm * spcm )
515546{
516547 struct snd_soc_dapm_widget_list * list = spcm -> stream [dir ].list ;
517- struct snd_sof_widget * swidget ;
548+ struct snd_sof_widget * swidget = widget -> dobj . private ;
518549 struct snd_soc_dapm_path * p ;
519550 int err ;
520551 int ret = 0 ;
521552
522- if (is_virtual_widget (sdev , widget , __func__ ))
553+ if (is_virtual_widget (sdev , widget , __func__ ) || ! swidget )
523554 goto sink_free ;
524555
525- swidget = widget -> dobj .private ;
556+ /* skip aggregated DAIs */
557+ if (!sof_widget_in_same_direction (swidget , dir ))
558+ return 0 ;
526559
527- /* no need to free aggregated DAI widgets */
528- if (swidget && !is_aggregated_dai (swidget )) {
529- err = sof_widget_free (sdev , swidget );
530- if (err < 0 )
531- ret = err ;
532- }
560+ if (is_aggregated_dai (swidget ))
561+ goto sink_free ;
533562
563+ err = sof_widget_free (sdev , widget -> dobj .private );
564+ if (err < 0 )
565+ ret = err ;
566+ sink_free :
534567 /* free all widgets in the sink paths even in case of error to keep use counts balanced */
535568 snd_soc_dapm_widget_for_each_sink_path (widget , p ) {
536569 if (!p -> walking ) {
@@ -570,6 +603,10 @@ static int sof_set_up_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_d
570603 if (swidget ) {
571604 int i ;
572605
606+ /* skip aggregated DAIs */
607+ if (!sof_widget_in_same_direction (swidget , dir ))
608+ return 0 ;
609+
573610 if (is_aggregated_dai (swidget ))
574611 goto sink_setup ;
575612
@@ -635,15 +672,14 @@ sof_walk_widgets_in_order(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
635672 return 0 ;
636673
637674 for_each_dapm_widgets (list , i , widget ) {
638- if (is_virtual_widget (sdev , widget , __func__ ))
639- continue ;
640-
641- /* starting widget for playback is AIF type */
642- if (dir == SNDRV_PCM_STREAM_PLAYBACK && widget -> id != snd_soc_dapm_aif_in )
675+ /* starting widget for playback is of AIF or snd_soc_dapm_input type */
676+ if (dir == SNDRV_PCM_STREAM_PLAYBACK && (widget -> id != snd_soc_dapm_aif_in &&
677+ widget -> id != snd_soc_dapm_input ))
643678 continue ;
644679
645680 /* starting widget for capture is DAI type */
646- if (dir == SNDRV_PCM_STREAM_CAPTURE && widget -> id != snd_soc_dapm_dai_out )
681+ if (dir == SNDRV_PCM_STREAM_CAPTURE && widget -> id != snd_soc_dapm_dai_out &&
682+ widget -> id != snd_soc_dapm_output )
647683 continue ;
648684
649685 switch (op ) {
@@ -673,7 +709,7 @@ sof_walk_widgets_in_order(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
673709 break ;
674710 }
675711 case SOF_WIDGET_UNPREPARE :
676- sof_unprepare_widgets_in_path (sdev , widget , list );
712+ sof_unprepare_widgets_in_path (sdev , widget , list , dir );
677713 break ;
678714 default :
679715 dev_err (sdev -> dev , "Invalid widget op %d\n" , op );
@@ -920,29 +956,41 @@ struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
920956 return NULL ;
921957}
922958
923- /* find widget by stream name and direction */
924- struct snd_sof_widget *
925- snd_sof_find_swidget_sname (struct snd_soc_component * scomp ,
926- const char * pcm_name , int dir )
959+ static struct snd_sof_widget * snd_sof_find_swidget_sname_type (struct snd_soc_component * scomp ,
960+ const char * sname ,
961+ enum snd_soc_dapm_type type )
927962{
928963 struct snd_sof_dev * sdev = snd_soc_component_get_drvdata (scomp );
929964 struct snd_sof_widget * swidget ;
930- enum snd_soc_dapm_type type ;
931-
932- if (dir == SNDRV_PCM_STREAM_PLAYBACK )
933- type = snd_soc_dapm_aif_in ;
934- else
935- type = snd_soc_dapm_aif_out ;
936965
937966 list_for_each_entry (swidget , & sdev -> widget_list , list ) {
938- if (!strcmp (pcm_name , swidget -> widget -> sname ) &&
939- swidget -> id == type )
967+ if (!strcmp (sname , swidget -> widget -> sname ) && swidget -> id == type )
940968 return swidget ;
941969 }
942970
943971 return NULL ;
944972}
945973
974+ /* find widget by stream name and direction */
975+ struct snd_sof_widget *
976+ snd_sof_find_swidget_sname (struct snd_soc_component * scomp ,
977+ const char * pcm_name , int dir )
978+ {
979+ if (dir == SNDRV_PCM_STREAM_PLAYBACK ) {
980+ struct snd_sof_widget * swidget ;
981+
982+ /* first look for an aif_in type widget */
983+ swidget = snd_sof_find_swidget_sname_type (scomp , pcm_name , snd_soc_dapm_aif_in );
984+ if (swidget )
985+ return swidget ;
986+
987+ /* if not found, look for an input type widget */
988+ return snd_sof_find_swidget_sname_type (scomp , pcm_name , snd_soc_dapm_input );
989+ }
990+
991+ return snd_sof_find_swidget_sname_type (scomp , pcm_name , snd_soc_dapm_aif_out );
992+ }
993+
946994struct snd_sof_dai * snd_sof_find_dai (struct snd_soc_component * scomp ,
947995 const char * name )
948996{
0 commit comments