1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
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.
6 // Copyright(c) 2019 Intel Corporation. All rights reserved.
8 // Author: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
11 #include <linux/bitfield.h>
12 #include <trace/events/sof.h>
13 #include "sof-audio.h"
14 #include "sof-of-dev.h"
17 static bool is_virtual_widget(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
21 case snd_soc_dapm_out_drv:
22 case snd_soc_dapm_output:
23 case snd_soc_dapm_input:
24 dev_dbg(sdev->dev, "%s: %s is a virtual widget\n", func, widget->name);
31 static void sof_reset_route_setup_status(struct snd_sof_dev *sdev, struct snd_sof_widget *widget)
33 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
34 struct snd_sof_route *sroute;
36 list_for_each_entry(sroute, &sdev->route_list, list)
37 if (sroute->src_widget == widget || sroute->sink_widget == widget) {
38 if (sroute->setup && tplg_ops && tplg_ops->route_free)
39 tplg_ops->route_free(sdev, sroute);
41 sroute->setup = false;
45 static int sof_widget_free_unlocked(struct snd_sof_dev *sdev,
46 struct snd_sof_widget *swidget)
48 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
49 struct snd_sof_widget *pipe_widget;
53 if (!swidget->private)
56 trace_sof_widget_free(swidget);
58 /* only free when use_count is 0 */
59 if (--swidget->use_count)
62 pipe_widget = swidget->spipe->pipe_widget;
64 /* reset route setup status for all routes that contain this widget */
65 sof_reset_route_setup_status(sdev, swidget);
67 /* free DAI config and continue to free widget even if it fails */
68 if (WIDGET_IS_DAI(swidget->id)) {
69 struct snd_sof_dai_config_data data;
70 unsigned int flags = SOF_DAI_CONFIG_FLAGS_HW_FREE;
72 data.dai_data = DMA_CHAN_INVALID;
74 if (tplg_ops && tplg_ops->dai_config) {
75 err = tplg_ops->dai_config(sdev, swidget, flags, &data);
77 dev_err(sdev->dev, "failed to free config for widget %s\n",
78 swidget->widget->name);
82 /* continue to disable core even if IPC fails */
83 if (tplg_ops && tplg_ops->widget_free) {
84 ret = tplg_ops->widget_free(sdev, swidget);
90 * disable widget core. continue to route setup status and complete flag
91 * even if this fails and return the appropriate error
93 ret = snd_sof_dsp_core_put(sdev, swidget->core);
95 dev_err(sdev->dev, "error: failed to disable target core: %d for widget %s\n",
96 swidget->core, swidget->widget->name);
102 * free the scheduler widget (same as pipe_widget) associated with the current swidget.
103 * skip for static pipelines
105 if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) {
106 ret = sof_widget_free_unlocked(sdev, pipe_widget);
111 /* clear pipeline complete */
112 if (swidget->id == snd_soc_dapm_scheduler)
113 swidget->spipe->complete = 0;
116 dev_dbg(sdev->dev, "widget %s freed\n", swidget->widget->name);
121 int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
125 mutex_lock(&swidget->setup_mutex);
126 ret = sof_widget_free_unlocked(sdev, swidget);
127 mutex_unlock(&swidget->setup_mutex);
131 EXPORT_SYMBOL(sof_widget_free);
133 static int sof_widget_setup_unlocked(struct snd_sof_dev *sdev,
134 struct snd_sof_widget *swidget)
136 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
137 bool use_count_decremented = false;
140 /* skip if there is no private data */
141 if (!swidget->private)
144 trace_sof_widget_setup(swidget);
146 /* widget already set up */
147 if (++swidget->use_count > 1)
151 * The scheduler widget for a pipeline is not part of the connected DAPM
152 * widget list and it needs to be set up before the widgets in the pipeline
153 * are set up. The use_count for the scheduler widget is incremented for every
154 * widget in a given pipeline to ensure that it is freed only after the last
155 * widget in the pipeline is freed. Skip setting up scheduler widget for static pipelines.
157 if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) {
158 if (!swidget->spipe || !swidget->spipe->pipe_widget) {
159 dev_err(sdev->dev, "No pipeline set for %s\n", swidget->widget->name);
164 ret = sof_widget_setup_unlocked(sdev, swidget->spipe->pipe_widget);
169 /* enable widget core */
170 ret = snd_sof_dsp_core_get(sdev, swidget->core);
172 dev_err(sdev->dev, "error: failed to enable target core for widget %s\n",
173 swidget->widget->name);
174 goto pipe_widget_free;
177 /* setup widget in the DSP */
178 if (tplg_ops && tplg_ops->widget_setup) {
179 ret = tplg_ops->widget_setup(sdev, swidget);
184 /* send config for DAI components */
185 if (WIDGET_IS_DAI(swidget->id)) {
186 unsigned int flags = SOF_DAI_CONFIG_FLAGS_HW_PARAMS;
189 * The config flags saved during BE DAI hw_params will be used for IPC3. IPC4 does
190 * not use the flags argument.
192 if (tplg_ops && tplg_ops->dai_config) {
193 ret = tplg_ops->dai_config(sdev, swidget, flags, NULL);
199 /* restore kcontrols for widget */
200 if (tplg_ops && tplg_ops->control && tplg_ops->control->widget_kcontrol_setup) {
201 ret = tplg_ops->control->widget_kcontrol_setup(sdev, swidget);
206 dev_dbg(sdev->dev, "widget %s setup complete\n", swidget->widget->name);
211 /* widget use_count and core ref_count will both be decremented by sof_widget_free() */
212 sof_widget_free_unlocked(sdev, swidget);
213 use_count_decremented = true;
215 snd_sof_dsp_core_put(sdev, swidget->core);
217 if (swidget->id != snd_soc_dapm_scheduler)
218 sof_widget_free_unlocked(sdev, swidget->spipe->pipe_widget);
220 if (!use_count_decremented)
221 swidget->use_count--;
226 int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
230 mutex_lock(&swidget->setup_mutex);
231 ret = sof_widget_setup_unlocked(sdev, swidget);
232 mutex_unlock(&swidget->setup_mutex);
236 EXPORT_SYMBOL(sof_widget_setup);
238 int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsource,
239 struct snd_soc_dapm_widget *wsink)
241 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
242 struct snd_sof_widget *src_widget = wsource->dobj.private;
243 struct snd_sof_widget *sink_widget = wsink->dobj.private;
244 struct snd_sof_route *sroute;
245 bool route_found = false;
247 /* ignore routes involving virtual widgets in topology */
248 if (is_virtual_widget(sdev, src_widget->widget, __func__) ||
249 is_virtual_widget(sdev, sink_widget->widget, __func__))
252 /* find route matching source and sink widgets */
253 list_for_each_entry(sroute, &sdev->route_list, list)
254 if (sroute->src_widget == src_widget && sroute->sink_widget == sink_widget) {
260 dev_err(sdev->dev, "error: cannot find SOF route for source %s -> %s sink\n",
261 wsource->name, wsink->name);
265 /* nothing to do if route is already set up */
269 if (tplg_ops && tplg_ops->route_setup) {
270 int ret = tplg_ops->route_setup(sdev, sroute);
276 sroute->setup = true;
280 static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
281 struct snd_soc_dapm_widget_list *list, int dir)
283 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
284 struct snd_soc_dapm_widget *widget;
285 struct snd_sof_route *sroute;
286 struct snd_soc_dapm_path *p;
291 * Set up connections between widgets in the sink/source paths based on direction.
292 * Some non-SOF widgets exist in topology either for compatibility or for the
293 * purpose of connecting a pipeline from a host to a DAI in order to receive the DAPM
294 * events. But they are not handled by the firmware. So ignore them.
296 if (dir == SNDRV_PCM_STREAM_PLAYBACK) {
297 for_each_dapm_widgets(list, i, widget) {
298 if (!widget->dobj.private)
301 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
302 if (!widget_in_list(list, p->sink))
305 if (p->sink->dobj.private) {
306 ret = sof_route_setup(sdev, widget, p->sink);
313 for_each_dapm_widgets(list, i, widget) {
314 if (!widget->dobj.private)
317 snd_soc_dapm_widget_for_each_source_path(widget, p) {
318 if (!widget_in_list(list, p->source))
321 if (p->source->dobj.private) {
322 ret = sof_route_setup(sdev, p->source, widget);
331 * The above loop handles connections between widgets that belong to the DAPM widget list.
332 * This is not sufficient to handle loopback cases between pipelines configured with
333 * different directions, e.g. a sidetone or an amplifier feedback connected to a speaker
336 list_for_each_entry(sroute, &sdev->route_list, list) {
337 bool src_widget_in_dapm_list, sink_widget_in_dapm_list;
338 struct snd_sof_widget *swidget;
343 src_widget_in_dapm_list = widget_in_list(list, sroute->src_widget->widget);
344 sink_widget_in_dapm_list = widget_in_list(list, sroute->sink_widget->widget);
347 * if both source and sink are in the DAPM list, the route must already have been
348 * set up above. And if neither are in the DAPM list, the route shouldn't be
351 if (src_widget_in_dapm_list == sink_widget_in_dapm_list)
355 * At this point either the source widget or the sink widget is in the DAPM list
356 * with a route that might need to be set up. Check the use_count of the widget
357 * that is not in the DAPM list to confirm if it is in use currently before setting
360 if (src_widget_in_dapm_list)
361 swidget = sroute->sink_widget;
363 swidget = sroute->src_widget;
365 mutex_lock(&swidget->setup_mutex);
366 if (!swidget->use_count) {
367 mutex_unlock(&swidget->setup_mutex);
371 if (tplg_ops && tplg_ops->route_setup) {
373 * this route will get freed when either the source widget or the sink
374 * widget is freed during hw_free
376 ret = tplg_ops->route_setup(sdev, sroute);
378 sroute->setup = true;
381 mutex_unlock(&swidget->setup_mutex);
391 sof_unprepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
392 struct snd_soc_dapm_widget_list *list)
394 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
395 struct snd_sof_widget *swidget = widget->dobj.private;
396 const struct sof_ipc_tplg_widget_ops *widget_ops;
397 struct snd_soc_dapm_path *p;
399 if (is_virtual_widget(sdev, widget, __func__))
402 /* skip if the widget is in use or if it is already unprepared */
403 if (!swidget || !swidget->prepared || swidget->use_count > 0)
406 widget_ops = tplg_ops ? tplg_ops->widget : NULL;
407 if (widget_ops && widget_ops[widget->id].ipc_unprepare)
408 /* unprepare the source widget */
409 widget_ops[widget->id].ipc_unprepare(swidget);
411 swidget->prepared = false;
414 /* unprepare all widgets in the sink paths */
415 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
416 if (!widget_in_list(list, p->sink))
418 if (!p->walking && p->sink->dobj.private) {
420 sof_unprepare_widgets_in_path(sdev, p->sink, list);
427 sof_prepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
428 struct snd_pcm_hw_params *fe_params,
429 struct snd_sof_platform_stream_params *platform_params,
430 struct snd_pcm_hw_params *pipeline_params, int dir,
431 struct snd_soc_dapm_widget_list *list)
433 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
434 struct snd_sof_widget *swidget = widget->dobj.private;
435 const struct sof_ipc_tplg_widget_ops *widget_ops;
436 struct snd_soc_dapm_path *p;
439 if (is_virtual_widget(sdev, widget, __func__))
442 widget_ops = tplg_ops ? tplg_ops->widget : NULL;
446 if (!swidget || !widget_ops[widget->id].ipc_prepare || swidget->prepared)
449 /* prepare the source widget */
450 ret = widget_ops[widget->id].ipc_prepare(swidget, fe_params, platform_params,
451 pipeline_params, dir);
453 dev_err(sdev->dev, "failed to prepare widget %s\n", widget->name);
457 swidget->prepared = true;
460 /* prepare all widgets in the sink paths */
461 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
462 if (!widget_in_list(list, p->sink))
464 if (!p->walking && p->sink->dobj.private) {
466 ret = sof_prepare_widgets_in_path(sdev, p->sink, fe_params,
467 platform_params, pipeline_params, dir,
471 /* unprepare the source widget */
472 if (widget_ops[widget->id].ipc_unprepare &&
473 swidget && swidget->prepared) {
474 widget_ops[widget->id].ipc_unprepare(swidget);
475 swidget->prepared = false;
486 * free all widgets in the sink path starting from the source widget
487 * (DAI type for capture, AIF type for playback)
489 static int sof_free_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
490 int dir, struct snd_sof_pcm *spcm)
492 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
493 struct snd_soc_dapm_path *p;
497 if (is_virtual_widget(sdev, widget, __func__))
500 if (widget->dobj.private) {
501 err = sof_widget_free(sdev, widget->dobj.private);
506 /* free all widgets in the sink paths even in case of error to keep use counts balanced */
507 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
509 if (!widget_in_list(list, p->sink))
514 err = sof_free_widgets_in_path(sdev, p->sink, dir, spcm);
525 * set up all widgets in the sink path starting from the source widget
526 * (DAI type for capture, AIF type for playback).
527 * The error path in this function ensures that all successfully set up widgets getting freed.
529 static int sof_set_up_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
530 int dir, struct snd_sof_pcm *spcm)
532 struct snd_sof_pcm_stream_pipeline_list *pipeline_list = &spcm->stream[dir].pipeline_list;
533 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
534 struct snd_sof_widget *swidget = widget->dobj.private;
535 struct snd_sof_pipeline *spipe;
536 struct snd_soc_dapm_path *p;
539 if (is_virtual_widget(sdev, widget, __func__))
545 ret = sof_widget_setup(sdev, widget->dobj.private);
549 /* skip populating the pipe_widgets array if it is NULL */
550 if (!pipeline_list->pipelines)
554 * Add the widget's pipe_widget to the list of pipelines to be triggered if not
555 * already in the list. This will result in the pipelines getting added in the
556 * order source to sink.
558 for (i = 0; i < pipeline_list->count; i++) {
559 spipe = pipeline_list->pipelines[i];
560 if (spipe == swidget->spipe)
564 if (i == pipeline_list->count) {
565 pipeline_list->count++;
566 pipeline_list->pipelines[i] = swidget->spipe;
571 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
573 if (!widget_in_list(list, p->sink))
578 ret = sof_set_up_widgets_in_path(sdev, p->sink, dir, spcm);
582 sof_widget_free(sdev, swidget);
592 sof_walk_widgets_in_order(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
593 struct snd_pcm_hw_params *fe_params,
594 struct snd_sof_platform_stream_params *platform_params, int dir,
595 enum sof_widget_op op)
597 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
598 struct snd_soc_dapm_widget *widget;
606 for_each_dapm_widgets(list, i, widget) {
607 if (is_virtual_widget(sdev, widget, __func__))
610 /* starting widget for playback is AIF type */
611 if (dir == SNDRV_PCM_STREAM_PLAYBACK && widget->id != snd_soc_dapm_aif_in)
614 /* starting widget for capture is DAI type */
615 if (dir == SNDRV_PCM_STREAM_CAPTURE && widget->id != snd_soc_dapm_dai_out)
619 case SOF_WIDGET_SETUP:
620 ret = sof_set_up_widgets_in_path(sdev, widget, dir, spcm);
623 case SOF_WIDGET_FREE:
624 ret = sof_free_widgets_in_path(sdev, widget, dir, spcm);
627 case SOF_WIDGET_PREPARE:
629 struct snd_pcm_hw_params pipeline_params;
633 * When walking the list of connected widgets, the pipeline_params for each
634 * widget is modified by the source widget in the path. Use a local
635 * copy of the runtime params as the pipeline_params so that the runtime
636 * params does not get overwritten.
638 memcpy(&pipeline_params, fe_params, sizeof(*fe_params));
640 ret = sof_prepare_widgets_in_path(sdev, widget, fe_params, platform_params,
641 &pipeline_params, dir, list);
644 case SOF_WIDGET_UNPREPARE:
645 sof_unprepare_widgets_in_path(sdev, widget, list);
648 dev_err(sdev->dev, "Invalid widget op %d\n", op);
652 dev_err(sdev->dev, "Failed to %s connected widgets\n", str);
660 int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
661 struct snd_pcm_hw_params *fe_params,
662 struct snd_sof_platform_stream_params *platform_params,
665 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
666 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
667 struct snd_soc_dapm_widget *widget;
670 /* nothing to set up */
675 * Prepare widgets for set up. The prepare step is used to allocate memory, assign
676 * instance ID and pick the widget configuration based on the runtime PCM params.
678 ret = sof_walk_widgets_in_order(sdev, spcm, fe_params, platform_params,
679 dir, SOF_WIDGET_PREPARE);
683 /* Set up is used to send the IPC to the DSP to create the widget */
684 ret = sof_walk_widgets_in_order(sdev, spcm, fe_params, platform_params,
685 dir, SOF_WIDGET_SETUP);
687 sof_walk_widgets_in_order(sdev, spcm, fe_params, platform_params,
688 dir, SOF_WIDGET_UNPREPARE);
693 * error in setting pipeline connections will result in route status being reset for
694 * routes that were successfully set up when the widgets are freed.
696 ret = sof_setup_pipeline_connections(sdev, list, dir);
700 /* complete pipelines */
701 for_each_dapm_widgets(list, i, widget) {
702 struct snd_sof_widget *swidget = widget->dobj.private;
703 struct snd_sof_widget *pipe_widget;
704 struct snd_sof_pipeline *spipe;
706 if (!swidget || sdev->dspless_mode_selected)
709 spipe = swidget->spipe;
711 dev_err(sdev->dev, "no pipeline found for %s\n",
712 swidget->widget->name);
717 pipe_widget = spipe->pipe_widget;
719 dev_err(sdev->dev, "error: no pipeline widget found for %s\n",
720 swidget->widget->name);
728 if (tplg_ops && tplg_ops->pipeline_complete) {
729 spipe->complete = tplg_ops->pipeline_complete(sdev, pipe_widget);
730 if (spipe->complete < 0) {
731 ret = spipe->complete;
740 sof_walk_widgets_in_order(sdev, spcm, fe_params, platform_params, dir,
742 sof_walk_widgets_in_order(sdev, spcm, NULL, NULL, dir, SOF_WIDGET_UNPREPARE);
747 int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir)
749 struct snd_sof_pcm_stream_pipeline_list *pipeline_list = &spcm->stream[dir].pipeline_list;
750 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
753 /* nothing to free */
757 /* send IPC to free widget in the DSP */
758 ret = sof_walk_widgets_in_order(sdev, spcm, NULL, NULL, dir, SOF_WIDGET_FREE);
760 /* unprepare the widget */
761 sof_walk_widgets_in_order(sdev, spcm, NULL, NULL, dir, SOF_WIDGET_UNPREPARE);
763 snd_soc_dapm_dai_free_widgets(&list);
764 spcm->stream[dir].list = NULL;
766 pipeline_list->count = 0;
772 * helper to determine if there are only D0i3 compatible
775 bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev)
777 struct snd_pcm_substream *substream;
778 struct snd_sof_pcm *spcm;
779 bool d0i3_compatible_active = false;
782 list_for_each_entry(spcm, &sdev->pcm_list, list) {
783 for_each_pcm_streams(dir) {
784 substream = spcm->stream[dir].substream;
785 if (!substream || !substream->runtime)
789 * substream->runtime being not NULL indicates
790 * that the stream is open. No need to check the
793 if (!spcm->stream[dir].d0i3_compatible)
796 d0i3_compatible_active = true;
800 return d0i3_compatible_active;
802 EXPORT_SYMBOL(snd_sof_dsp_only_d0i3_compatible_stream_active);
804 bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev)
806 struct snd_sof_pcm *spcm;
808 list_for_each_entry(spcm, &sdev->pcm_list, list) {
809 if (spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].suspend_ignored ||
810 spcm->stream[SNDRV_PCM_STREAM_CAPTURE].suspend_ignored)
817 int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
818 struct snd_sof_pcm *spcm, int dir, bool free_widget_list)
820 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
823 if (spcm->prepared[substream->stream]) {
824 /* stop DMA first if needed */
825 if (pcm_ops && pcm_ops->platform_stop_during_hw_free)
826 snd_sof_pcm_platform_trigger(sdev, substream, SNDRV_PCM_TRIGGER_STOP);
828 /* Send PCM_FREE IPC to reset pipeline */
829 if (pcm_ops && pcm_ops->hw_free) {
830 ret = pcm_ops->hw_free(sdev->component, substream);
835 spcm->prepared[substream->stream] = false;
839 ret = snd_sof_pcm_platform_hw_free(sdev, substream);
843 /* free widget list */
844 if (free_widget_list) {
845 ret = sof_widget_list_free(sdev, spcm, dir);
847 dev_err(sdev->dev, "failed to free widgets during suspend\n");
854 * Generic object lookup APIs.
857 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp,
860 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
861 struct snd_sof_pcm *spcm;
863 list_for_each_entry(spcm, &sdev->pcm_list, list) {
864 /* match with PCM dai name */
865 if (strcmp(spcm->pcm.dai_name, name) == 0)
868 /* match with playback caps name if set */
869 if (*spcm->pcm.caps[0].name &&
870 !strcmp(spcm->pcm.caps[0].name, name))
873 /* match with capture caps name if set */
874 if (*spcm->pcm.caps[1].name &&
875 !strcmp(spcm->pcm.caps[1].name, name))
882 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp,
883 unsigned int comp_id,
886 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
887 struct snd_sof_pcm *spcm;
890 list_for_each_entry(spcm, &sdev->pcm_list, list) {
891 for_each_pcm_streams(dir) {
892 if (spcm->stream[dir].comp_id == comp_id) {
902 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
905 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
906 struct snd_sof_widget *swidget;
908 list_for_each_entry(swidget, &sdev->widget_list, list) {
909 if (strcmp(name, swidget->widget->name) == 0)
916 /* find widget by stream name and direction */
917 struct snd_sof_widget *
918 snd_sof_find_swidget_sname(struct snd_soc_component *scomp,
919 const char *pcm_name, int dir)
921 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
922 struct snd_sof_widget *swidget;
923 enum snd_soc_dapm_type type;
925 if (dir == SNDRV_PCM_STREAM_PLAYBACK)
926 type = snd_soc_dapm_aif_in;
928 type = snd_soc_dapm_aif_out;
930 list_for_each_entry(swidget, &sdev->widget_list, list) {
931 if (!strcmp(pcm_name, swidget->widget->sname) &&
939 struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp,
942 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
943 struct snd_sof_dai *dai;
945 list_for_each_entry(dai, &sdev->dai_list, list) {
946 if (dai->name && (strcmp(name, dai->name) == 0))
953 static int sof_dai_get_clk(struct snd_soc_pcm_runtime *rtd, int clk_type)
955 struct snd_soc_component *component =
956 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
957 struct snd_sof_dai *dai =
958 snd_sof_find_dai(component, (char *)rtd->dai_link->name);
959 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
960 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
962 /* use the tplg configured mclk if existed */
966 if (tplg_ops && tplg_ops->dai_get_clk)
967 return tplg_ops->dai_get_clk(sdev, dai, clk_type);
973 * Helper to get SSP MCLK from a pcm_runtime.
974 * Return 0 if not exist.
976 int sof_dai_get_mclk(struct snd_soc_pcm_runtime *rtd)
978 return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_MCLK);
980 EXPORT_SYMBOL(sof_dai_get_mclk);
983 * Helper to get SSP BCLK from a pcm_runtime.
984 * Return 0 if not exist.
986 int sof_dai_get_bclk(struct snd_soc_pcm_runtime *rtd)
988 return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_BCLK);
990 EXPORT_SYMBOL(sof_dai_get_bclk);
992 static struct snd_sof_of_mach *sof_of_machine_select(struct snd_sof_dev *sdev)
994 struct snd_sof_pdata *sof_pdata = sdev->pdata;
995 const struct sof_dev_desc *desc = sof_pdata->desc;
996 struct snd_sof_of_mach *mach = desc->of_machines;
1001 for (; mach->compatible; mach++) {
1002 if (of_machine_is_compatible(mach->compatible)) {
1003 sof_pdata->tplg_filename = mach->sof_tplg_filename;
1004 if (mach->fw_filename)
1005 sof_pdata->fw_filename = mach->fw_filename;
1015 * SOF Driver enumeration.
1017 int sof_machine_check(struct snd_sof_dev *sdev)
1019 struct snd_sof_pdata *sof_pdata = sdev->pdata;
1020 const struct sof_dev_desc *desc = sof_pdata->desc;
1021 struct snd_soc_acpi_mach *mach;
1023 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)) {
1024 const struct snd_sof_of_mach *of_mach;
1026 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
1027 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
1031 mach = snd_sof_machine_select(sdev);
1033 sof_pdata->machine = mach;
1034 snd_sof_set_mach_params(mach, sdev);
1038 of_mach = sof_of_machine_select(sdev);
1040 sof_pdata->of_machine = of_mach;
1044 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)) {
1045 dev_err(sdev->dev, "error: no matching ASoC machine driver found - aborting probe\n");
1049 dev_warn(sdev->dev, "Force to use nocodec mode\n");
1053 /* select nocodec mode */
1054 dev_warn(sdev->dev, "Using nocodec machine driver\n");
1055 mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL);
1059 mach->drv_name = "sof-nocodec";
1060 if (!sof_pdata->tplg_filename)
1061 sof_pdata->tplg_filename = desc->nocodec_tplg_filename;
1063 sof_pdata->machine = mach;
1064 snd_sof_set_mach_params(mach, sdev);
1068 EXPORT_SYMBOL(sof_machine_check);
1070 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
1072 struct snd_sof_pdata *plat_data = pdata;
1073 const char *drv_name;
1077 drv_name = plat_data->machine->drv_name;
1078 mach = plat_data->machine;
1079 size = sizeof(*plat_data->machine);
1081 /* register machine driver, pass machine info as pdata */
1082 plat_data->pdev_mach =
1083 platform_device_register_data(sdev->dev, drv_name,
1084 PLATFORM_DEVID_NONE, mach, size);
1085 if (IS_ERR(plat_data->pdev_mach))
1086 return PTR_ERR(plat_data->pdev_mach);
1088 dev_dbg(sdev->dev, "created machine %s\n",
1089 dev_name(&plat_data->pdev_mach->dev));
1093 EXPORT_SYMBOL(sof_machine_register);
1095 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
1097 struct snd_sof_pdata *plat_data = pdata;
1099 if (!IS_ERR_OR_NULL(plat_data->pdev_mach))
1100 platform_device_unregister(plat_data->pdev_mach);
1102 EXPORT_SYMBOL(sof_machine_unregister);