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_pipeline *spipe = swidget->spipe;
50 struct snd_sof_widget *pipe_widget;
54 if (!swidget->private)
57 trace_sof_widget_free(swidget);
59 /* only free when use_count is 0 */
60 if (--swidget->use_count)
63 pipe_widget = swidget->spipe->pipe_widget;
65 /* reset route setup status for all routes that contain this widget */
66 sof_reset_route_setup_status(sdev, swidget);
68 /* free DAI config and continue to free widget even if it fails */
69 if (WIDGET_IS_DAI(swidget->id)) {
70 struct snd_sof_dai_config_data data;
71 unsigned int flags = SOF_DAI_CONFIG_FLAGS_HW_FREE;
73 data.dai_data = DMA_CHAN_INVALID;
75 if (tplg_ops && tplg_ops->dai_config) {
76 err = tplg_ops->dai_config(sdev, swidget, flags, &data);
78 dev_err(sdev->dev, "failed to free config for widget %s\n",
79 swidget->widget->name);
83 /* continue to disable core even if IPC fails */
84 if (tplg_ops && tplg_ops->widget_free) {
85 ret = tplg_ops->widget_free(sdev, swidget);
91 * decrement ref count for cores associated with all modules in the pipeline and clear
94 if (swidget->id == snd_soc_dapm_scheduler) {
97 for_each_set_bit(i, &spipe->core_mask, sdev->num_cores) {
98 ret = snd_sof_dsp_core_put(sdev, i);
100 dev_err(sdev->dev, "failed to disable target core: %d for pipeline %s\n",
101 i, swidget->widget->name);
106 swidget->spipe->complete = 0;
110 * free the scheduler widget (same as pipe_widget) associated with the current swidget.
111 * skip for static pipelines
113 if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) {
114 ret = sof_widget_free_unlocked(sdev, pipe_widget);
120 dev_dbg(sdev->dev, "widget %s freed\n", swidget->widget->name);
125 int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
129 mutex_lock(&swidget->setup_mutex);
130 ret = sof_widget_free_unlocked(sdev, swidget);
131 mutex_unlock(&swidget->setup_mutex);
135 EXPORT_SYMBOL(sof_widget_free);
137 static int sof_widget_setup_unlocked(struct snd_sof_dev *sdev,
138 struct snd_sof_widget *swidget)
140 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
141 struct snd_sof_pipeline *spipe = swidget->spipe;
142 bool use_count_decremented = false;
146 /* skip if there is no private data */
147 if (!swidget->private)
150 trace_sof_widget_setup(swidget);
152 /* widget already set up */
153 if (++swidget->use_count > 1)
157 * The scheduler widget for a pipeline is not part of the connected DAPM
158 * widget list and it needs to be set up before the widgets in the pipeline
159 * are set up. The use_count for the scheduler widget is incremented for every
160 * widget in a given pipeline to ensure that it is freed only after the last
161 * widget in the pipeline is freed. Skip setting up scheduler widget for static pipelines.
163 if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) {
164 if (!swidget->spipe || !swidget->spipe->pipe_widget) {
165 dev_err(sdev->dev, "No pipeline set for %s\n", swidget->widget->name);
170 ret = sof_widget_setup_unlocked(sdev, swidget->spipe->pipe_widget);
175 /* update ref count for cores associated with all modules in the pipeline */
176 if (swidget->id == snd_soc_dapm_scheduler) {
177 for_each_set_bit(i, &spipe->core_mask, sdev->num_cores) {
178 ret = snd_sof_dsp_core_get(sdev, i);
180 dev_err(sdev->dev, "failed to enable target core %d for pipeline %s\n",
181 i, swidget->widget->name);
182 goto pipe_widget_free;
187 /* setup widget in the DSP */
188 if (tplg_ops && tplg_ops->widget_setup) {
189 ret = tplg_ops->widget_setup(sdev, swidget);
191 goto pipe_widget_free;
194 /* send config for DAI components */
195 if (WIDGET_IS_DAI(swidget->id)) {
196 unsigned int flags = SOF_DAI_CONFIG_FLAGS_HW_PARAMS;
199 * The config flags saved during BE DAI hw_params will be used for IPC3. IPC4 does
200 * not use the flags argument.
202 if (tplg_ops && tplg_ops->dai_config) {
203 ret = tplg_ops->dai_config(sdev, swidget, flags, NULL);
209 /* restore kcontrols for widget */
210 if (tplg_ops && tplg_ops->control && tplg_ops->control->widget_kcontrol_setup) {
211 ret = tplg_ops->control->widget_kcontrol_setup(sdev, swidget);
216 dev_dbg(sdev->dev, "widget %s setup complete\n", swidget->widget->name);
221 /* widget use_count will be decremented by sof_widget_free() */
222 sof_widget_free_unlocked(sdev, swidget);
223 use_count_decremented = true;
225 if (swidget->id != snd_soc_dapm_scheduler) {
226 sof_widget_free_unlocked(sdev, swidget->spipe->pipe_widget);
230 /* decrement ref count for all cores that were updated previously */
231 for_each_set_bit(j, &spipe->core_mask, sdev->num_cores) {
234 snd_sof_dsp_core_put(sdev, j);
238 if (!use_count_decremented)
239 swidget->use_count--;
244 int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
248 mutex_lock(&swidget->setup_mutex);
249 ret = sof_widget_setup_unlocked(sdev, swidget);
250 mutex_unlock(&swidget->setup_mutex);
254 EXPORT_SYMBOL(sof_widget_setup);
256 int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsource,
257 struct snd_soc_dapm_widget *wsink)
259 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
260 struct snd_sof_widget *src_widget = wsource->dobj.private;
261 struct snd_sof_widget *sink_widget = wsink->dobj.private;
262 struct snd_sof_route *sroute;
263 bool route_found = false;
265 /* ignore routes involving virtual widgets in topology */
266 if (is_virtual_widget(sdev, src_widget->widget, __func__) ||
267 is_virtual_widget(sdev, sink_widget->widget, __func__))
270 /* find route matching source and sink widgets */
271 list_for_each_entry(sroute, &sdev->route_list, list)
272 if (sroute->src_widget == src_widget && sroute->sink_widget == sink_widget) {
278 dev_err(sdev->dev, "error: cannot find SOF route for source %s -> %s sink\n",
279 wsource->name, wsink->name);
283 /* nothing to do if route is already set up */
287 if (tplg_ops && tplg_ops->route_setup) {
288 int ret = tplg_ops->route_setup(sdev, sroute);
294 sroute->setup = true;
298 static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
299 struct snd_soc_dapm_widget_list *list, int dir)
301 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
302 struct snd_soc_dapm_widget *widget;
303 struct snd_sof_route *sroute;
304 struct snd_soc_dapm_path *p;
309 * Set up connections between widgets in the sink/source paths based on direction.
310 * Some non-SOF widgets exist in topology either for compatibility or for the
311 * purpose of connecting a pipeline from a host to a DAI in order to receive the DAPM
312 * events. But they are not handled by the firmware. So ignore them.
314 if (dir == SNDRV_PCM_STREAM_PLAYBACK) {
315 for_each_dapm_widgets(list, i, widget) {
316 if (!widget->dobj.private)
319 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
320 if (!widget_in_list(list, p->sink))
323 if (p->sink->dobj.private) {
324 ret = sof_route_setup(sdev, widget, p->sink);
331 for_each_dapm_widgets(list, i, widget) {
332 if (!widget->dobj.private)
335 snd_soc_dapm_widget_for_each_source_path(widget, p) {
336 if (!widget_in_list(list, p->source))
339 if (p->source->dobj.private) {
340 ret = sof_route_setup(sdev, p->source, widget);
349 * The above loop handles connections between widgets that belong to the DAPM widget list.
350 * This is not sufficient to handle loopback cases between pipelines configured with
351 * different directions, e.g. a sidetone or an amplifier feedback connected to a speaker
354 list_for_each_entry(sroute, &sdev->route_list, list) {
355 bool src_widget_in_dapm_list, sink_widget_in_dapm_list;
356 struct snd_sof_widget *swidget;
361 src_widget_in_dapm_list = widget_in_list(list, sroute->src_widget->widget);
362 sink_widget_in_dapm_list = widget_in_list(list, sroute->sink_widget->widget);
365 * if both source and sink are in the DAPM list, the route must already have been
366 * set up above. And if neither are in the DAPM list, the route shouldn't be
369 if (src_widget_in_dapm_list == sink_widget_in_dapm_list)
373 * At this point either the source widget or the sink widget is in the DAPM list
374 * with a route that might need to be set up. Check the use_count of the widget
375 * that is not in the DAPM list to confirm if it is in use currently before setting
378 if (src_widget_in_dapm_list)
379 swidget = sroute->sink_widget;
381 swidget = sroute->src_widget;
383 mutex_lock(&swidget->setup_mutex);
384 if (!swidget->use_count) {
385 mutex_unlock(&swidget->setup_mutex);
389 if (tplg_ops && tplg_ops->route_setup) {
391 * this route will get freed when either the source widget or the sink
392 * widget is freed during hw_free
394 ret = tplg_ops->route_setup(sdev, sroute);
396 sroute->setup = true;
399 mutex_unlock(&swidget->setup_mutex);
409 sof_unprepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
410 struct snd_soc_dapm_widget_list *list)
412 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
413 struct snd_sof_widget *swidget = widget->dobj.private;
414 const struct sof_ipc_tplg_widget_ops *widget_ops;
415 struct snd_soc_dapm_path *p;
417 if (is_virtual_widget(sdev, widget, __func__))
420 /* skip if the widget is in use or if it is already unprepared */
421 if (!swidget || !swidget->prepared || swidget->use_count > 0)
424 widget_ops = tplg_ops ? tplg_ops->widget : NULL;
425 if (widget_ops && widget_ops[widget->id].ipc_unprepare)
426 /* unprepare the source widget */
427 widget_ops[widget->id].ipc_unprepare(swidget);
429 swidget->prepared = false;
432 /* unprepare all widgets in the sink paths */
433 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
434 if (!widget_in_list(list, p->sink))
436 if (!p->walking && p->sink->dobj.private) {
438 sof_unprepare_widgets_in_path(sdev, p->sink, list);
445 sof_prepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
446 struct snd_pcm_hw_params *fe_params,
447 struct snd_sof_platform_stream_params *platform_params,
448 struct snd_pcm_hw_params *pipeline_params, int dir,
449 struct snd_soc_dapm_widget_list *list)
451 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
452 struct snd_sof_widget *swidget = widget->dobj.private;
453 const struct sof_ipc_tplg_widget_ops *widget_ops;
454 struct snd_soc_dapm_path *p;
457 if (is_virtual_widget(sdev, widget, __func__))
460 widget_ops = tplg_ops ? tplg_ops->widget : NULL;
464 if (!swidget || !widget_ops[widget->id].ipc_prepare || swidget->prepared)
467 /* prepare the source widget */
468 ret = widget_ops[widget->id].ipc_prepare(swidget, fe_params, platform_params,
469 pipeline_params, dir);
471 dev_err(sdev->dev, "failed to prepare widget %s\n", widget->name);
475 swidget->prepared = true;
478 /* prepare all widgets in the sink paths */
479 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
480 if (!widget_in_list(list, p->sink))
482 if (!p->walking && p->sink->dobj.private) {
484 ret = sof_prepare_widgets_in_path(sdev, p->sink, fe_params,
485 platform_params, pipeline_params, dir,
489 /* unprepare the source widget */
490 if (widget_ops[widget->id].ipc_unprepare &&
491 swidget && swidget->prepared) {
492 widget_ops[widget->id].ipc_unprepare(swidget);
493 swidget->prepared = false;
504 * free all widgets in the sink path starting from the source widget
505 * (DAI type for capture, AIF type for playback)
507 static int sof_free_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
508 int dir, struct snd_sof_pcm *spcm)
510 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
511 struct snd_soc_dapm_path *p;
515 if (is_virtual_widget(sdev, widget, __func__))
518 if (widget->dobj.private) {
519 err = sof_widget_free(sdev, widget->dobj.private);
524 /* free all widgets in the sink paths even in case of error to keep use counts balanced */
525 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
527 if (!widget_in_list(list, p->sink))
532 err = sof_free_widgets_in_path(sdev, p->sink, dir, spcm);
543 * set up all widgets in the sink path starting from the source widget
544 * (DAI type for capture, AIF type for playback).
545 * The error path in this function ensures that all successfully set up widgets getting freed.
547 static int sof_set_up_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
548 int dir, struct snd_sof_pcm *spcm)
550 struct snd_sof_pcm_stream_pipeline_list *pipeline_list = &spcm->stream[dir].pipeline_list;
551 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
552 struct snd_sof_widget *swidget = widget->dobj.private;
553 struct snd_sof_pipeline *spipe;
554 struct snd_soc_dapm_path *p;
557 if (is_virtual_widget(sdev, widget, __func__))
563 ret = sof_widget_setup(sdev, widget->dobj.private);
567 /* skip populating the pipe_widgets array if it is NULL */
568 if (!pipeline_list->pipelines)
572 * Add the widget's pipe_widget to the list of pipelines to be triggered if not
573 * already in the list. This will result in the pipelines getting added in the
574 * order source to sink.
576 for (i = 0; i < pipeline_list->count; i++) {
577 spipe = pipeline_list->pipelines[i];
578 if (spipe == swidget->spipe)
582 if (i == pipeline_list->count) {
583 pipeline_list->count++;
584 pipeline_list->pipelines[i] = swidget->spipe;
589 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
591 if (!widget_in_list(list, p->sink))
596 ret = sof_set_up_widgets_in_path(sdev, p->sink, dir, spcm);
600 sof_widget_free(sdev, swidget);
610 sof_walk_widgets_in_order(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
611 struct snd_pcm_hw_params *fe_params,
612 struct snd_sof_platform_stream_params *platform_params, int dir,
613 enum sof_widget_op op)
615 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
616 struct snd_soc_dapm_widget *widget;
624 for_each_dapm_widgets(list, i, widget) {
625 if (is_virtual_widget(sdev, widget, __func__))
628 /* starting widget for playback is AIF type */
629 if (dir == SNDRV_PCM_STREAM_PLAYBACK && widget->id != snd_soc_dapm_aif_in)
632 /* starting widget for capture is DAI type */
633 if (dir == SNDRV_PCM_STREAM_CAPTURE && widget->id != snd_soc_dapm_dai_out)
637 case SOF_WIDGET_SETUP:
638 ret = sof_set_up_widgets_in_path(sdev, widget, dir, spcm);
641 case SOF_WIDGET_FREE:
642 ret = sof_free_widgets_in_path(sdev, widget, dir, spcm);
645 case SOF_WIDGET_PREPARE:
647 struct snd_pcm_hw_params pipeline_params;
651 * When walking the list of connected widgets, the pipeline_params for each
652 * widget is modified by the source widget in the path. Use a local
653 * copy of the runtime params as the pipeline_params so that the runtime
654 * params does not get overwritten.
656 memcpy(&pipeline_params, fe_params, sizeof(*fe_params));
658 ret = sof_prepare_widgets_in_path(sdev, widget, fe_params, platform_params,
659 &pipeline_params, dir, list);
662 case SOF_WIDGET_UNPREPARE:
663 sof_unprepare_widgets_in_path(sdev, widget, list);
666 dev_err(sdev->dev, "Invalid widget op %d\n", op);
670 dev_err(sdev->dev, "Failed to %s connected widgets\n", str);
678 int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
679 struct snd_pcm_hw_params *fe_params,
680 struct snd_sof_platform_stream_params *platform_params,
683 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
684 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
685 struct snd_soc_dapm_widget *widget;
688 /* nothing to set up */
693 * Prepare widgets for set up. The prepare step is used to allocate memory, assign
694 * instance ID and pick the widget configuration based on the runtime PCM params.
696 ret = sof_walk_widgets_in_order(sdev, spcm, fe_params, platform_params,
697 dir, SOF_WIDGET_PREPARE);
701 /* Set up is used to send the IPC to the DSP to create the widget */
702 ret = sof_walk_widgets_in_order(sdev, spcm, fe_params, platform_params,
703 dir, SOF_WIDGET_SETUP);
705 sof_walk_widgets_in_order(sdev, spcm, fe_params, platform_params,
706 dir, SOF_WIDGET_UNPREPARE);
711 * error in setting pipeline connections will result in route status being reset for
712 * routes that were successfully set up when the widgets are freed.
714 ret = sof_setup_pipeline_connections(sdev, list, dir);
718 /* complete pipelines */
719 for_each_dapm_widgets(list, i, widget) {
720 struct snd_sof_widget *swidget = widget->dobj.private;
721 struct snd_sof_widget *pipe_widget;
722 struct snd_sof_pipeline *spipe;
724 if (!swidget || sdev->dspless_mode_selected)
727 spipe = swidget->spipe;
729 dev_err(sdev->dev, "no pipeline found for %s\n",
730 swidget->widget->name);
735 pipe_widget = spipe->pipe_widget;
737 dev_err(sdev->dev, "error: no pipeline widget found for %s\n",
738 swidget->widget->name);
746 if (tplg_ops && tplg_ops->pipeline_complete) {
747 spipe->complete = tplg_ops->pipeline_complete(sdev, pipe_widget);
748 if (spipe->complete < 0) {
749 ret = spipe->complete;
758 sof_walk_widgets_in_order(sdev, spcm, fe_params, platform_params, dir,
760 sof_walk_widgets_in_order(sdev, spcm, NULL, NULL, dir, SOF_WIDGET_UNPREPARE);
765 int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir)
767 struct snd_sof_pcm_stream_pipeline_list *pipeline_list = &spcm->stream[dir].pipeline_list;
768 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
771 /* nothing to free */
775 /* send IPC to free widget in the DSP */
776 ret = sof_walk_widgets_in_order(sdev, spcm, NULL, NULL, dir, SOF_WIDGET_FREE);
778 /* unprepare the widget */
779 sof_walk_widgets_in_order(sdev, spcm, NULL, NULL, dir, SOF_WIDGET_UNPREPARE);
781 snd_soc_dapm_dai_free_widgets(&list);
782 spcm->stream[dir].list = NULL;
784 pipeline_list->count = 0;
790 * helper to determine if there are only D0i3 compatible
793 bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev)
795 struct snd_pcm_substream *substream;
796 struct snd_sof_pcm *spcm;
797 bool d0i3_compatible_active = false;
800 list_for_each_entry(spcm, &sdev->pcm_list, list) {
801 for_each_pcm_streams(dir) {
802 substream = spcm->stream[dir].substream;
803 if (!substream || !substream->runtime)
807 * substream->runtime being not NULL indicates
808 * that the stream is open. No need to check the
811 if (!spcm->stream[dir].d0i3_compatible)
814 d0i3_compatible_active = true;
818 return d0i3_compatible_active;
820 EXPORT_SYMBOL(snd_sof_dsp_only_d0i3_compatible_stream_active);
822 bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev)
824 struct snd_sof_pcm *spcm;
826 list_for_each_entry(spcm, &sdev->pcm_list, list) {
827 if (spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].suspend_ignored ||
828 spcm->stream[SNDRV_PCM_STREAM_CAPTURE].suspend_ignored)
835 int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
836 struct snd_sof_pcm *spcm, int dir, bool free_widget_list)
838 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
841 if (spcm->prepared[substream->stream]) {
842 /* stop DMA first if needed */
843 if (pcm_ops && pcm_ops->platform_stop_during_hw_free)
844 snd_sof_pcm_platform_trigger(sdev, substream, SNDRV_PCM_TRIGGER_STOP);
846 /* Send PCM_FREE IPC to reset pipeline */
847 if (pcm_ops && pcm_ops->hw_free) {
848 ret = pcm_ops->hw_free(sdev->component, substream);
853 spcm->prepared[substream->stream] = false;
857 ret = snd_sof_pcm_platform_hw_free(sdev, substream);
861 /* free widget list */
862 if (free_widget_list) {
863 ret = sof_widget_list_free(sdev, spcm, dir);
865 dev_err(sdev->dev, "failed to free widgets during suspend\n");
872 * Generic object lookup APIs.
875 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp,
878 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
879 struct snd_sof_pcm *spcm;
881 list_for_each_entry(spcm, &sdev->pcm_list, list) {
882 /* match with PCM dai name */
883 if (strcmp(spcm->pcm.dai_name, name) == 0)
886 /* match with playback caps name if set */
887 if (*spcm->pcm.caps[0].name &&
888 !strcmp(spcm->pcm.caps[0].name, name))
891 /* match with capture caps name if set */
892 if (*spcm->pcm.caps[1].name &&
893 !strcmp(spcm->pcm.caps[1].name, name))
900 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp,
901 unsigned int comp_id,
904 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
905 struct snd_sof_pcm *spcm;
908 list_for_each_entry(spcm, &sdev->pcm_list, list) {
909 for_each_pcm_streams(dir) {
910 if (spcm->stream[dir].comp_id == comp_id) {
920 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
923 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
924 struct snd_sof_widget *swidget;
926 list_for_each_entry(swidget, &sdev->widget_list, list) {
927 if (strcmp(name, swidget->widget->name) == 0)
934 /* find widget by stream name and direction */
935 struct snd_sof_widget *
936 snd_sof_find_swidget_sname(struct snd_soc_component *scomp,
937 const char *pcm_name, int dir)
939 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
940 struct snd_sof_widget *swidget;
941 enum snd_soc_dapm_type type;
943 if (dir == SNDRV_PCM_STREAM_PLAYBACK)
944 type = snd_soc_dapm_aif_in;
946 type = snd_soc_dapm_aif_out;
948 list_for_each_entry(swidget, &sdev->widget_list, list) {
949 if (!strcmp(pcm_name, swidget->widget->sname) &&
957 struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp,
960 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
961 struct snd_sof_dai *dai;
963 list_for_each_entry(dai, &sdev->dai_list, list) {
964 if (dai->name && (strcmp(name, dai->name) == 0))
971 static int sof_dai_get_clk(struct snd_soc_pcm_runtime *rtd, int clk_type)
973 struct snd_soc_component *component =
974 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
975 struct snd_sof_dai *dai =
976 snd_sof_find_dai(component, (char *)rtd->dai_link->name);
977 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
978 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
980 /* use the tplg configured mclk if existed */
984 if (tplg_ops && tplg_ops->dai_get_clk)
985 return tplg_ops->dai_get_clk(sdev, dai, clk_type);
991 * Helper to get SSP MCLK from a pcm_runtime.
992 * Return 0 if not exist.
994 int sof_dai_get_mclk(struct snd_soc_pcm_runtime *rtd)
996 return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_MCLK);
998 EXPORT_SYMBOL(sof_dai_get_mclk);
1001 * Helper to get SSP BCLK from a pcm_runtime.
1002 * Return 0 if not exist.
1004 int sof_dai_get_bclk(struct snd_soc_pcm_runtime *rtd)
1006 return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_BCLK);
1008 EXPORT_SYMBOL(sof_dai_get_bclk);
1010 static struct snd_sof_of_mach *sof_of_machine_select(struct snd_sof_dev *sdev)
1012 struct snd_sof_pdata *sof_pdata = sdev->pdata;
1013 const struct sof_dev_desc *desc = sof_pdata->desc;
1014 struct snd_sof_of_mach *mach = desc->of_machines;
1019 for (; mach->compatible; mach++) {
1020 if (of_machine_is_compatible(mach->compatible)) {
1021 sof_pdata->tplg_filename = mach->sof_tplg_filename;
1022 if (mach->fw_filename)
1023 sof_pdata->fw_filename = mach->fw_filename;
1033 * SOF Driver enumeration.
1035 int sof_machine_check(struct snd_sof_dev *sdev)
1037 struct snd_sof_pdata *sof_pdata = sdev->pdata;
1038 const struct sof_dev_desc *desc = sof_pdata->desc;
1039 struct snd_soc_acpi_mach *mach;
1041 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)) {
1042 const struct snd_sof_of_mach *of_mach;
1044 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
1045 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
1049 mach = snd_sof_machine_select(sdev);
1051 sof_pdata->machine = mach;
1053 if (sof_pdata->subsystem_id_set) {
1054 mach->mach_params.subsystem_vendor = sof_pdata->subsystem_vendor;
1055 mach->mach_params.subsystem_device = sof_pdata->subsystem_device;
1056 mach->mach_params.subsystem_id_set = true;
1059 snd_sof_set_mach_params(mach, sdev);
1063 of_mach = sof_of_machine_select(sdev);
1065 sof_pdata->of_machine = of_mach;
1069 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)) {
1070 dev_err(sdev->dev, "error: no matching ASoC machine driver found - aborting probe\n");
1074 dev_warn(sdev->dev, "Force to use nocodec mode\n");
1078 /* select nocodec mode */
1079 dev_warn(sdev->dev, "Using nocodec machine driver\n");
1080 mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL);
1084 mach->drv_name = "sof-nocodec";
1085 if (!sof_pdata->tplg_filename)
1086 sof_pdata->tplg_filename = desc->nocodec_tplg_filename;
1088 sof_pdata->machine = mach;
1089 snd_sof_set_mach_params(mach, sdev);
1093 EXPORT_SYMBOL(sof_machine_check);
1095 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
1097 struct snd_sof_pdata *plat_data = pdata;
1098 const char *drv_name;
1102 drv_name = plat_data->machine->drv_name;
1103 mach = plat_data->machine;
1104 size = sizeof(*plat_data->machine);
1106 /* register machine driver, pass machine info as pdata */
1107 plat_data->pdev_mach =
1108 platform_device_register_data(sdev->dev, drv_name,
1109 PLATFORM_DEVID_NONE, mach, size);
1110 if (IS_ERR(plat_data->pdev_mach))
1111 return PTR_ERR(plat_data->pdev_mach);
1113 dev_dbg(sdev->dev, "created machine %s\n",
1114 dev_name(&plat_data->pdev_mach->dev));
1118 EXPORT_SYMBOL(sof_machine_register);
1120 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
1122 struct snd_sof_pdata *plat_data = pdata;
1124 if (!IS_ERR_OR_NULL(plat_data->pdev_mach))
1125 platform_device_unregister(plat_data->pdev_mach);
1127 EXPORT_SYMBOL(sof_machine_unregister);