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 "sof-audio.h"
15 static void sof_reset_route_setup_status(struct snd_sof_dev *sdev, struct snd_sof_widget *widget)
17 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
18 struct snd_sof_route *sroute;
20 list_for_each_entry(sroute, &sdev->route_list, list)
21 if (sroute->src_widget == widget || sroute->sink_widget == widget) {
22 if (sroute->setup && tplg_ops->route_free)
23 tplg_ops->route_free(sdev, sroute);
25 sroute->setup = false;
29 int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
31 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
35 if (!swidget->private)
38 /* only free when use_count is 0 */
39 if (--swidget->use_count)
42 /* reset route setup status for all routes that contain this widget */
43 sof_reset_route_setup_status(sdev, swidget);
45 /* continue to disable core even if IPC fails */
46 if (tplg_ops->widget_free)
47 err = tplg_ops->widget_free(sdev, swidget);
50 * disable widget core. continue to route setup status and complete flag
51 * even if this fails and return the appropriate error
53 ret = snd_sof_dsp_core_put(sdev, swidget->core);
55 dev_err(sdev->dev, "error: failed to disable target core: %d for widget %s\n",
56 swidget->core, swidget->widget->name);
62 * free the scheduler widget (same as pipe_widget) associated with the current swidget.
63 * skip for static pipelines
65 if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) {
66 ret = sof_widget_free(sdev, swidget->pipe_widget);
69 swidget->pipe_widget->complete = 0;
73 dev_dbg(sdev->dev, "widget %s freed\n", swidget->widget->name);
77 EXPORT_SYMBOL(sof_widget_free);
79 int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
81 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
84 /* skip if there is no private data */
85 if (!swidget->private)
88 /* widget already set up */
89 if (++swidget->use_count > 1)
93 * The scheduler widget for a pipeline is not part of the connected DAPM
94 * widget list and it needs to be set up before the widgets in the pipeline
95 * are set up. The use_count for the scheduler widget is incremented for every
96 * widget in a given pipeline to ensure that it is freed only after the last
97 * widget in the pipeline is freed. Skip setting up scheduler widget for static pipelines.
99 if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) {
100 if (!swidget->pipe_widget) {
101 dev_err(sdev->dev, "No scheduler widget set for %s\n",
102 swidget->widget->name);
107 ret = sof_widget_setup(sdev, swidget->pipe_widget);
112 /* enable widget core */
113 ret = snd_sof_dsp_core_get(sdev, swidget->core);
115 dev_err(sdev->dev, "error: failed to enable target core for widget %s\n",
116 swidget->widget->name);
117 goto pipe_widget_free;
120 /* setup widget in the DSP */
121 if (tplg_ops->widget_setup) {
122 ret = tplg_ops->widget_setup(sdev, swidget);
127 /* send config for DAI components */
128 if (WIDGET_IS_DAI(swidget->id)) {
129 unsigned int flags = SOF_DAI_CONFIG_FLAGS_NONE;
131 if (tplg_ops->dai_config) {
132 ret = tplg_ops->dai_config(sdev, swidget, flags, NULL);
138 /* restore kcontrols for widget */
139 if (tplg_ops->control->widget_kcontrol_setup) {
140 ret = tplg_ops->control->widget_kcontrol_setup(sdev, swidget);
145 dev_dbg(sdev->dev, "widget %s setup complete\n", swidget->widget->name);
150 /* widget use_count and core ref_count will both be decremented by sof_widget_free() */
151 sof_widget_free(sdev, swidget);
153 snd_sof_dsp_core_put(sdev, swidget->core);
155 if (swidget->id != snd_soc_dapm_scheduler)
156 sof_widget_free(sdev, swidget->pipe_widget);
158 swidget->use_count--;
161 EXPORT_SYMBOL(sof_widget_setup);
163 int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsource,
164 struct snd_soc_dapm_widget *wsink)
166 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
167 struct snd_sof_widget *src_widget = wsource->dobj.private;
168 struct snd_sof_widget *sink_widget = wsink->dobj.private;
169 struct snd_sof_route *sroute;
170 bool route_found = false;
173 /* ignore routes involving virtual widgets in topology */
174 switch (src_widget->id) {
175 case snd_soc_dapm_out_drv:
176 case snd_soc_dapm_output:
177 case snd_soc_dapm_input:
183 switch (sink_widget->id) {
184 case snd_soc_dapm_out_drv:
185 case snd_soc_dapm_output:
186 case snd_soc_dapm_input:
192 /* find route matching source and sink widgets */
193 list_for_each_entry(sroute, &sdev->route_list, list)
194 if (sroute->src_widget == src_widget && sroute->sink_widget == sink_widget) {
200 dev_err(sdev->dev, "error: cannot find SOF route for source %s -> %s sink\n",
201 wsource->name, wsink->name);
205 /* nothing to do if route is already set up */
209 ret = ipc_tplg_ops->route_setup(sdev, sroute);
213 sroute->setup = true;
217 static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
218 struct snd_soc_dapm_widget_list *list, int dir)
220 struct snd_soc_dapm_widget *widget;
221 struct snd_soc_dapm_path *p;
226 * Set up connections between widgets in the sink/source paths based on direction.
227 * Some non-SOF widgets exist in topology either for compatibility or for the
228 * purpose of connecting a pipeline from a host to a DAI in order to receive the DAPM
229 * events. But they are not handled by the firmware. So ignore them.
231 if (dir == SNDRV_PCM_STREAM_PLAYBACK) {
232 for_each_dapm_widgets(list, i, widget) {
233 if (!widget->dobj.private)
236 snd_soc_dapm_widget_for_each_sink_path(widget, p)
237 if (p->sink->dobj.private) {
238 ret = sof_route_setup(sdev, widget, p->sink);
244 for_each_dapm_widgets(list, i, widget) {
245 if (!widget->dobj.private)
248 snd_soc_dapm_widget_for_each_source_path(widget, p)
249 if (p->source->dobj.private) {
250 ret = sof_route_setup(sdev, p->source, widget);
261 sof_unprepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget)
263 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
264 const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
265 struct snd_sof_widget *swidget = widget->dobj.private;
266 struct snd_soc_dapm_path *p;
268 if (!widget_ops[widget->id].ipc_unprepare || !swidget->prepared)
271 /* unprepare the source widget */
272 widget_ops[widget->id].ipc_unprepare(swidget);
273 swidget->prepared = false;
276 /* unprepare all widgets in the sink paths */
277 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
278 if (!p->walking && p->sink->dobj.private) {
280 sof_unprepare_widgets_in_path(sdev, p->sink);
287 sof_prepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
288 struct snd_pcm_hw_params *fe_params,
289 struct snd_sof_platform_stream_params *platform_params,
290 struct snd_pcm_hw_params *pipeline_params, int dir)
292 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
293 const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
294 struct snd_sof_widget *swidget = widget->dobj.private;
295 struct snd_soc_dapm_path *p;
298 if (!widget_ops[widget->id].ipc_prepare || swidget->prepared)
301 /* prepare the source widget */
302 ret = widget_ops[widget->id].ipc_prepare(swidget, fe_params, platform_params,
303 pipeline_params, dir);
305 dev_err(sdev->dev, "failed to prepare widget %s\n", widget->name);
309 swidget->prepared = true;
312 /* prepare all widgets in the sink paths */
313 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
314 if (!p->walking && p->sink->dobj.private) {
316 ret = sof_prepare_widgets_in_path(sdev, p->sink, fe_params,
317 platform_params, pipeline_params, dir);
320 /* unprepare the source widget */
321 if (widget_ops[widget->id].ipc_unprepare && swidget->prepared) {
322 widget_ops[widget->id].ipc_unprepare(swidget);
323 swidget->prepared = false;
334 * free all widgets in the sink path starting from the source widget
335 * (DAI type for capture, AIF type for playback)
337 static int sof_free_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
340 struct snd_soc_dapm_path *p;
344 /* free all widgets even in case of error to keep use counts balanced */
345 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
346 if (!p->walking && p->sink->dobj.private && widget->dobj.private) {
348 if (WIDGET_IS_AIF_OR_DAI(widget->id)) {
349 err = sof_widget_free(sdev, widget->dobj.private);
354 err = sof_widget_free(sdev, p->sink->dobj.private);
358 err = sof_free_widgets_in_path(sdev, p->sink, dir);
369 * set up all widgets in the sink path starting from the source widget
370 * (DAI type for capture, AIF type for playback).
371 * The error path in this function ensures that all successfully set up widgets getting freed.
373 static int sof_set_up_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
376 struct snd_soc_dapm_path *p;
379 snd_soc_dapm_widget_for_each_sink_path(widget, p) {
380 if (!p->walking && p->sink->dobj.private && widget->dobj.private) {
382 if (WIDGET_IS_AIF_OR_DAI(widget->id)) {
383 ret = sof_widget_setup(sdev, widget->dobj.private);
388 ret = sof_widget_setup(sdev, p->sink->dobj.private);
390 if (WIDGET_IS_AIF_OR_DAI(widget->id))
391 sof_widget_free(sdev, widget->dobj.private);
395 ret = sof_set_up_widgets_in_path(sdev, p->sink, dir);
397 if (WIDGET_IS_AIF_OR_DAI(widget->id))
398 sof_widget_free(sdev, widget->dobj.private);
399 sof_widget_free(sdev, p->sink->dobj.private);
412 sof_walk_widgets_in_order(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget_list *list,
413 struct snd_pcm_hw_params *fe_params,
414 struct snd_sof_platform_stream_params *platform_params, int dir,
415 enum sof_widget_op op)
417 struct snd_soc_dapm_widget *widget;
422 for_each_dapm_widgets(list, i, widget) {
423 /* starting widget for playback is AIF type */
424 if (dir == SNDRV_PCM_STREAM_PLAYBACK && !WIDGET_IS_AIF(widget->id))
427 /* starting widget for capture is DAI type */
428 if (dir == SNDRV_PCM_STREAM_CAPTURE && !WIDGET_IS_DAI(widget->id))
432 case SOF_WIDGET_SETUP:
433 ret = sof_set_up_widgets_in_path(sdev, widget, dir);
436 case SOF_WIDGET_FREE:
437 ret = sof_free_widgets_in_path(sdev, widget, dir);
440 case SOF_WIDGET_PREPARE:
442 struct snd_pcm_hw_params pipeline_params;
446 * When walking the list of connected widgets, the pipeline_params for each
447 * widget is modified by the source widget in the path. Use a local
448 * copy of the runtime params as the pipeline_params so that the runtime
449 * params does not get overwritten.
451 memcpy(&pipeline_params, fe_params, sizeof(*fe_params));
453 ret = sof_prepare_widgets_in_path(sdev, widget, fe_params,
454 platform_params, &pipeline_params, dir);
457 case SOF_WIDGET_UNPREPARE:
458 sof_unprepare_widgets_in_path(sdev, widget);
461 dev_err(sdev->dev, "Invalid widget op %d\n", op);
465 dev_err(sdev->dev, "Failed to %s connected widgets\n", str);
473 int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
474 struct snd_pcm_hw_params *fe_params,
475 struct snd_sof_platform_stream_params *platform_params,
478 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
479 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
480 struct snd_soc_dapm_widget *widget;
483 /* nothing to set up */
488 * Prepare widgets for set up. The prepare step is used to allocate memory, assign
489 * instance ID and pick the widget configuration based on the runtime PCM params.
491 ret = sof_walk_widgets_in_order(sdev, list, fe_params, platform_params,
492 dir, SOF_WIDGET_PREPARE);
496 /* Set up is used to send the IPC to the DSP to create the widget */
497 ret = sof_walk_widgets_in_order(sdev, list, fe_params, platform_params,
498 dir, SOF_WIDGET_SETUP);
500 ret = sof_walk_widgets_in_order(sdev, list, fe_params, platform_params,
501 dir, SOF_WIDGET_UNPREPARE);
506 * error in setting pipeline connections will result in route status being reset for
507 * routes that were successfully set up when the widgets are freed.
509 ret = sof_setup_pipeline_connections(sdev, list, dir);
513 /* complete pipelines */
514 for_each_dapm_widgets(list, i, widget) {
515 struct snd_sof_widget *swidget = widget->dobj.private;
516 struct snd_sof_widget *pipe_widget;
521 pipe_widget = swidget->pipe_widget;
523 dev_err(sdev->dev, "error: no pipeline widget found for %s\n",
524 swidget->widget->name);
529 if (pipe_widget->complete)
532 if (ipc_tplg_ops->pipeline_complete) {
533 pipe_widget->complete = ipc_tplg_ops->pipeline_complete(sdev, pipe_widget);
534 if (pipe_widget->complete < 0) {
535 ret = pipe_widget->complete;
544 sof_walk_widgets_in_order(sdev, list, fe_params, platform_params, dir,
546 sof_walk_widgets_in_order(sdev, list, NULL, NULL, dir, SOF_WIDGET_UNPREPARE);
551 int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir)
553 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
556 /* nothing to free */
560 /* send IPC to free widget in the DSP */
561 ret = sof_walk_widgets_in_order(sdev, list, NULL, NULL, dir, SOF_WIDGET_FREE);
563 /* unprepare the widget */
564 sof_walk_widgets_in_order(sdev, list, NULL, NULL, dir, SOF_WIDGET_UNPREPARE);
566 snd_soc_dapm_dai_free_widgets(&list);
567 spcm->stream[dir].list = NULL;
573 * helper to determine if there are only D0i3 compatible
576 bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev)
578 struct snd_pcm_substream *substream;
579 struct snd_sof_pcm *spcm;
580 bool d0i3_compatible_active = false;
583 list_for_each_entry(spcm, &sdev->pcm_list, list) {
584 for_each_pcm_streams(dir) {
585 substream = spcm->stream[dir].substream;
586 if (!substream || !substream->runtime)
590 * substream->runtime being not NULL indicates
591 * that the stream is open. No need to check the
594 if (!spcm->stream[dir].d0i3_compatible)
597 d0i3_compatible_active = true;
601 return d0i3_compatible_active;
603 EXPORT_SYMBOL(snd_sof_dsp_only_d0i3_compatible_stream_active);
605 bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev)
607 struct snd_sof_pcm *spcm;
609 list_for_each_entry(spcm, &sdev->pcm_list, list) {
610 if (spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].suspend_ignored ||
611 spcm->stream[SNDRV_PCM_STREAM_CAPTURE].suspend_ignored)
618 int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
619 struct snd_sof_pcm *spcm, int dir, bool free_widget_list)
621 const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
624 /* Send PCM_FREE IPC to reset pipeline */
625 if (pcm_ops->hw_free && spcm->prepared[substream->stream]) {
626 ret = pcm_ops->hw_free(sdev->component, substream);
631 spcm->prepared[substream->stream] = false;
634 ret = snd_sof_pcm_platform_hw_free(sdev, substream);
638 /* free widget list */
639 if (free_widget_list) {
640 ret = sof_widget_list_free(sdev, spcm, dir);
642 dev_err(sdev->dev, "failed to free widgets during suspend\n");
649 * Generic object lookup APIs.
652 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp,
655 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
656 struct snd_sof_pcm *spcm;
658 list_for_each_entry(spcm, &sdev->pcm_list, list) {
659 /* match with PCM dai name */
660 if (strcmp(spcm->pcm.dai_name, name) == 0)
663 /* match with playback caps name if set */
664 if (*spcm->pcm.caps[0].name &&
665 !strcmp(spcm->pcm.caps[0].name, name))
668 /* match with capture caps name if set */
669 if (*spcm->pcm.caps[1].name &&
670 !strcmp(spcm->pcm.caps[1].name, name))
677 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp,
678 unsigned int comp_id,
681 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
682 struct snd_sof_pcm *spcm;
685 list_for_each_entry(spcm, &sdev->pcm_list, list) {
686 for_each_pcm_streams(dir) {
687 if (spcm->stream[dir].comp_id == comp_id) {
697 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
700 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
701 struct snd_sof_widget *swidget;
703 list_for_each_entry(swidget, &sdev->widget_list, list) {
704 if (strcmp(name, swidget->widget->name) == 0)
711 /* find widget by stream name and direction */
712 struct snd_sof_widget *
713 snd_sof_find_swidget_sname(struct snd_soc_component *scomp,
714 const char *pcm_name, int dir)
716 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
717 struct snd_sof_widget *swidget;
718 enum snd_soc_dapm_type type;
720 if (dir == SNDRV_PCM_STREAM_PLAYBACK)
721 type = snd_soc_dapm_aif_in;
723 type = snd_soc_dapm_aif_out;
725 list_for_each_entry(swidget, &sdev->widget_list, list) {
726 if (!strcmp(pcm_name, swidget->widget->sname) &&
734 struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp,
737 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
738 struct snd_sof_dai *dai;
740 list_for_each_entry(dai, &sdev->dai_list, list) {
741 if (dai->name && (strcmp(name, dai->name) == 0))
748 static int sof_dai_get_clk(struct snd_soc_pcm_runtime *rtd, int clk_type)
750 struct snd_soc_component *component =
751 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
752 struct snd_sof_dai *dai =
753 snd_sof_find_dai(component, (char *)rtd->dai_link->name);
754 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
755 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
757 /* use the tplg configured mclk if existed */
761 if (tplg_ops->dai_get_clk)
762 return tplg_ops->dai_get_clk(sdev, dai, clk_type);
768 * Helper to get SSP MCLK from a pcm_runtime.
769 * Return 0 if not exist.
771 int sof_dai_get_mclk(struct snd_soc_pcm_runtime *rtd)
773 return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_MCLK);
775 EXPORT_SYMBOL(sof_dai_get_mclk);
778 * Helper to get SSP BCLK from a pcm_runtime.
779 * Return 0 if not exist.
781 int sof_dai_get_bclk(struct snd_soc_pcm_runtime *rtd)
783 return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_BCLK);
785 EXPORT_SYMBOL(sof_dai_get_bclk);
788 * SOF Driver enumeration.
790 int sof_machine_check(struct snd_sof_dev *sdev)
792 struct snd_sof_pdata *sof_pdata = sdev->pdata;
793 const struct sof_dev_desc *desc = sof_pdata->desc;
794 struct snd_soc_acpi_mach *mach;
796 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)) {
799 mach = snd_sof_machine_select(sdev);
801 sof_pdata->machine = mach;
802 snd_sof_set_mach_params(mach, sdev);
806 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)) {
807 dev_err(sdev->dev, "error: no matching ASoC machine driver found - aborting probe\n");
811 dev_warn(sdev->dev, "Force to use nocodec mode\n");
814 /* select nocodec mode */
815 dev_warn(sdev->dev, "Using nocodec machine driver\n");
816 mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL);
820 mach->drv_name = "sof-nocodec";
821 if (!sof_pdata->tplg_filename)
822 sof_pdata->tplg_filename = desc->nocodec_tplg_filename;
824 sof_pdata->machine = mach;
825 snd_sof_set_mach_params(mach, sdev);
829 EXPORT_SYMBOL(sof_machine_check);
831 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
833 struct snd_sof_pdata *plat_data = pdata;
834 const char *drv_name;
838 drv_name = plat_data->machine->drv_name;
839 mach = plat_data->machine;
840 size = sizeof(*plat_data->machine);
842 /* register machine driver, pass machine info as pdata */
843 plat_data->pdev_mach =
844 platform_device_register_data(sdev->dev, drv_name,
845 PLATFORM_DEVID_NONE, mach, size);
846 if (IS_ERR(plat_data->pdev_mach))
847 return PTR_ERR(plat_data->pdev_mach);
849 dev_dbg(sdev->dev, "created machine %s\n",
850 dev_name(&plat_data->pdev_mach->dev));
854 EXPORT_SYMBOL(sof_machine_register);
856 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
858 struct snd_sof_pdata *plat_data = pdata;
860 if (!IS_ERR_OR_NULL(plat_data->pdev_mach))
861 platform_device_unregister(plat_data->pdev_mach);
863 EXPORT_SYMBOL(sof_machine_unregister);