summaryrefslogtreecommitdiff
path: root/sound/soc/sof/sof-audio.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc/sof/sof-audio.c')
-rw-r--r--sound/soc/sof/sof-audio.c398
1 files changed, 258 insertions, 140 deletions
diff --git a/sound/soc/sof/sof-audio.c b/sound/soc/sof/sof-audio.c
index b2f009a0c5b7..8d740635a4bb 100644
--- a/sound/soc/sof/sof-audio.c
+++ b/sound/soc/sof/sof-audio.c
@@ -12,61 +12,18 @@
#include "sof-audio.h"
#include "ops.h"
-static int sof_kcontrol_setup(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
-{
- int ret;
-
- /* reset readback offset for scontrol */
- scontrol->readback_offset = 0;
-
- ret = snd_sof_ipc_set_get_comp_data(scontrol, true);
- if (ret < 0)
- dev_err(sdev->dev, "error: failed kcontrol value set for widget: %d\n",
- scontrol->comp_id);
-
- return ret;
-}
-
-static int sof_widget_kcontrol_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
-{
- struct snd_sof_control *scontrol;
- int ret;
-
- /* set up all controls for the widget */
- list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
- if (scontrol->comp_id == swidget->comp_id) {
- /* set kcontrol data in DSP */
- ret = sof_kcontrol_setup(sdev, scontrol);
- if (ret < 0) {
- dev_err(sdev->dev, "error: fail to set up kcontrols for widget %s\n",
- swidget->widget->name);
- return ret;
- }
-
- /*
- * Read back the data from the DSP for static widgets. This is particularly
- * useful for binary kcontrols associated with static pipeline widgets to
- * initialize the data size to match that in the DSP.
- */
- if (swidget->dynamic_pipeline_widget)
- continue;
-
- ret = snd_sof_ipc_set_get_comp_data(scontrol, false);
- if (ret < 0)
- dev_warn(sdev->dev, "Failed kcontrol get for control in widget %s\n",
- swidget->widget->name);
- }
-
- return 0;
-}
-
static void sof_reset_route_setup_status(struct snd_sof_dev *sdev, struct snd_sof_widget *widget)
{
+ const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
struct snd_sof_route *sroute;
list_for_each_entry(sroute, &sdev->route_list, list)
- if (sroute->src_widget == widget || sroute->sink_widget == widget)
+ if (sroute->src_widget == widget || sroute->sink_widget == widget) {
+ if (sroute->setup && tplg_ops->route_free)
+ tplg_ops->route_free(sdev, sroute);
+
sroute->setup = false;
+ }
}
int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
@@ -82,6 +39,9 @@ int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
if (--swidget->use_count)
return 0;
+ /* reset route setup status for all routes that contain this widget */
+ sof_reset_route_setup_status(sdev, swidget);
+
/* continue to disable core even if IPC fails */
if (tplg_ops->widget_free)
err = tplg_ops->widget_free(sdev, swidget);
@@ -98,10 +58,6 @@ int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
err = ret;
}
- /* reset route setup status for all routes that contain this widget */
- sof_reset_route_setup_status(sdev, swidget);
- swidget->complete = 0;
-
/*
* free the scheduler widget (same as pipe_widget) associated with the current swidget.
* skip for static pipelines
@@ -110,6 +66,7 @@ int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
ret = sof_widget_free(sdev, swidget->pipe_widget);
if (ret < 0 && !err)
err = ret;
+ swidget->pipe_widget->complete = 0;
}
if (!err)
@@ -179,11 +136,10 @@ int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
}
/* restore kcontrols for widget */
- ret = sof_widget_kcontrol_setup(sdev, swidget);
- if (ret < 0) {
- dev_err(sdev->dev, "error: failed to restore kcontrols for widget %s\n",
- swidget->widget->name);
- goto widget_free;
+ if (tplg_ops->control->widget_kcontrol_setup) {
+ ret = tplg_ops->control->widget_kcontrol_setup(sdev, swidget);
+ if (ret < 0)
+ goto widget_free;
}
dev_dbg(sdev->dev, "widget %s setup complete\n", swidget->widget->name);
@@ -301,28 +257,249 @@ static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
return 0;
}
-int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir)
+static void
+sof_unprepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget)
+{
+ const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
+ const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
+ struct snd_sof_widget *swidget = widget->dobj.private;
+ struct snd_soc_dapm_path *p;
+
+ if (!widget_ops[widget->id].ipc_unprepare || !swidget->prepared)
+ goto sink_unprepare;
+
+ /* unprepare the source widget */
+ widget_ops[widget->id].ipc_unprepare(swidget);
+ swidget->prepared = false;
+
+sink_unprepare:
+ /* unprepare all widgets in the sink paths */
+ snd_soc_dapm_widget_for_each_sink_path(widget, p) {
+ if (!p->walking && p->sink->dobj.private) {
+ p->walking = true;
+ sof_unprepare_widgets_in_path(sdev, p->sink);
+ p->walking = false;
+ }
+ }
+}
+
+static int
+sof_prepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
+ struct snd_pcm_hw_params *fe_params,
+ struct snd_sof_platform_stream_params *platform_params,
+ struct snd_pcm_hw_params *pipeline_params, int dir)
+{
+ const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
+ const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
+ struct snd_sof_widget *swidget = widget->dobj.private;
+ struct snd_soc_dapm_path *p;
+ int ret;
+
+ if (!widget_ops[widget->id].ipc_prepare || swidget->prepared)
+ goto sink_prepare;
+
+ /* prepare the source widget */
+ ret = widget_ops[widget->id].ipc_prepare(swidget, fe_params, platform_params,
+ pipeline_params, dir);
+ if (ret < 0) {
+ dev_err(sdev->dev, "failed to prepare widget %s\n", widget->name);
+ return ret;
+ }
+
+ swidget->prepared = true;
+
+sink_prepare:
+ /* prepare all widgets in the sink paths */
+ snd_soc_dapm_widget_for_each_sink_path(widget, p) {
+ if (!p->walking && p->sink->dobj.private) {
+ p->walking = true;
+ ret = sof_prepare_widgets_in_path(sdev, p->sink, fe_params,
+ platform_params, pipeline_params, dir);
+ p->walking = false;
+ if (ret < 0) {
+ /* unprepare the source widget */
+ if (!widget_ops[widget->id].ipc_unprepare && swidget->prepared) {
+ widget_ops[widget->id].ipc_unprepare(swidget);
+ swidget->prepared = false;
+ }
+ return ret;
+ }
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * free all widgets in the sink path starting from the source widget
+ * (DAI type for capture, AIF type for playback)
+ */
+static int sof_free_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
+ int dir)
+{
+ struct snd_soc_dapm_path *p;
+ int err;
+ int ret = 0;
+
+ /* free all widgets even in case of error to keep use counts balanced */
+ snd_soc_dapm_widget_for_each_sink_path(widget, p) {
+ if (!p->walking && p->sink->dobj.private && widget->dobj.private) {
+ p->walking = true;
+ if (WIDGET_IS_AIF_OR_DAI(widget->id)) {
+ err = sof_widget_free(sdev, widget->dobj.private);
+ if (err < 0)
+ ret = err;
+ }
+
+ err = sof_widget_free(sdev, p->sink->dobj.private);
+ if (err < 0)
+ ret = err;
+
+ err = sof_free_widgets_in_path(sdev, p->sink, dir);
+ if (err < 0)
+ ret = err;
+ p->walking = false;
+ }
+ }
+
+ return ret;
+}
+
+/*
+ * set up all widgets in the sink path starting from the source widget
+ * (DAI type for capture, AIF type for playback).
+ * The error path in this function ensures that all successfully set up widgets getting freed.
+ */
+static int sof_set_up_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
+ int dir)
+{
+ struct snd_soc_dapm_path *p;
+ int ret;
+
+ snd_soc_dapm_widget_for_each_sink_path(widget, p) {
+ if (!p->walking && p->sink->dobj.private && widget->dobj.private) {
+ p->walking = true;
+ if (WIDGET_IS_AIF_OR_DAI(widget->id)) {
+ ret = sof_widget_setup(sdev, widget->dobj.private);
+ if (ret < 0)
+ goto out;
+ }
+
+ ret = sof_widget_setup(sdev, p->sink->dobj.private);
+ if (ret < 0) {
+ if (WIDGET_IS_AIF_OR_DAI(widget->id))
+ sof_widget_free(sdev, widget->dobj.private);
+ goto out;
+ }
+
+ ret = sof_set_up_widgets_in_path(sdev, p->sink, dir);
+ if (ret < 0) {
+ if (WIDGET_IS_AIF_OR_DAI(widget->id))
+ sof_widget_free(sdev, widget->dobj.private);
+ sof_widget_free(sdev, p->sink->dobj.private);
+ }
+out:
+ p->walking = false;
+ if (ret < 0)
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+static int
+sof_walk_widgets_in_order(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget_list *list,
+ struct snd_pcm_hw_params *fe_params,
+ struct snd_sof_platform_stream_params *platform_params, int dir,
+ enum sof_widget_op op)
+{
+ struct snd_soc_dapm_widget *widget;
+ char *str;
+ int ret = 0;
+ int i;
+
+ for_each_dapm_widgets(list, i, widget) {
+ /* starting widget for playback is AIF type */
+ if (dir == SNDRV_PCM_STREAM_PLAYBACK && !WIDGET_IS_AIF(widget->id))
+ continue;
+
+ /* starting widget for capture is DAI type */
+ if (dir == SNDRV_PCM_STREAM_CAPTURE && !WIDGET_IS_DAI(widget->id))
+ continue;
+
+ switch (op) {
+ case SOF_WIDGET_SETUP:
+ ret = sof_set_up_widgets_in_path(sdev, widget, dir);
+ str = "set up";
+ break;
+ case SOF_WIDGET_FREE:
+ ret = sof_free_widgets_in_path(sdev, widget, dir);
+ str = "free";
+ break;
+ case SOF_WIDGET_PREPARE:
+ {
+ struct snd_pcm_hw_params pipeline_params;
+
+ str = "prepare";
+ /*
+ * When walking the list of connected widgets, the pipeline_params for each
+ * widget is modified by the source widget in the path. Use a local
+ * copy of the runtime params as the pipeline_params so that the runtime
+ * params does not get overwritten.
+ */
+ memcpy(&pipeline_params, fe_params, sizeof(*fe_params));
+
+ ret = sof_prepare_widgets_in_path(sdev, widget, fe_params,
+ platform_params, &pipeline_params, dir);
+ break;
+ }
+ case SOF_WIDGET_UNPREPARE:
+ sof_unprepare_widgets_in_path(sdev, widget);
+ break;
+ default:
+ dev_err(sdev->dev, "Invalid widget op %d\n", op);
+ return -EINVAL;
+ }
+ if (ret < 0) {
+ dev_err(sdev->dev, "Failed to %s connected widgets\n", str);
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
+ struct snd_pcm_hw_params *fe_params,
+ struct snd_sof_platform_stream_params *platform_params,
+ int dir)
{
const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
struct snd_soc_dapm_widget *widget;
- int i, ret, num_widgets;
+ int i, ret;
/* nothing to set up */
if (!list)
return 0;
- /* set up widgets in the list */
- for_each_dapm_widgets(list, num_widgets, widget) {
- struct snd_sof_widget *swidget = widget->dobj.private;
-
- if (!swidget)
- continue;
+ /*
+ * Prepare widgets for set up. The prepare step is used to allocate memory, assign
+ * instance ID and pick the widget configuration based on the runtime PCM params.
+ */
+ ret = sof_walk_widgets_in_order(sdev, list, fe_params, platform_params,
+ dir, SOF_WIDGET_PREPARE);
+ if (ret < 0)
+ return ret;
- /* set up the widget */
- ret = sof_widget_setup(sdev, swidget);
- if (ret < 0)
- goto widget_free;
+ /* Set up is used to send the IPC to the DSP to create the widget */
+ ret = sof_walk_widgets_in_order(sdev, list, fe_params, platform_params,
+ dir, SOF_WIDGET_SETUP);
+ if (ret < 0) {
+ ret = sof_walk_widgets_in_order(sdev, list, fe_params, platform_params,
+ dir, SOF_WIDGET_UNPREPARE);
+ return ret;
}
/*
@@ -364,18 +541,9 @@ int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, in
return 0;
widget_free:
- /* free all widgets that have been set up successfully */
- for_each_dapm_widgets(list, i, widget) {
- struct snd_sof_widget *swidget = widget->dobj.private;
-
- if (!swidget)
- continue;
-
- if (!num_widgets--)
- break;
-
- sof_widget_free(sdev, swidget);
- }
+ sof_walk_widgets_in_order(sdev, list, fe_params, platform_params, dir,
+ SOF_WIDGET_FREE);
+ sof_walk_widgets_in_order(sdev, list, NULL, NULL, dir, SOF_WIDGET_UNPREPARE);
return ret;
}
@@ -383,37 +551,22 @@ widget_free:
int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir)
{
struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
- struct snd_soc_dapm_widget *widget;
- int i, ret;
- int ret1 = 0;
+ int ret;
/* nothing to free */
if (!list)
return 0;
- /*
- * Free widgets in the list. This can fail but continue freeing other widgets to keep
- * use_counts balanced.
- */
- for_each_dapm_widgets(list, i, widget) {
- struct snd_sof_widget *swidget = widget->dobj.private;
-
- if (!swidget)
- continue;
+ /* send IPC to free widget in the DSP */
+ ret = sof_walk_widgets_in_order(sdev, list, NULL, NULL, dir, SOF_WIDGET_FREE);
- /*
- * free widget and its pipe_widget. Either of these can fail, but free as many as
- * possible before freeing the list and returning the error.
- */
- ret = sof_widget_free(sdev, swidget);
- if (ret < 0)
- ret1 = ret;
- }
+ /* unprepare the widget */
+ sof_walk_widgets_in_order(sdev, list, NULL, NULL, dir, SOF_WIDGET_UNPREPARE);
snd_soc_dapm_dai_free_widgets(&list);
spcm->stream[dir].list = NULL;
- return ret1;
+ return ret;
}
/*
@@ -462,42 +615,6 @@ bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev)
return false;
}
-int sof_set_hw_params_upon_resume(struct device *dev)
-{
- struct snd_sof_dev *sdev = dev_get_drvdata(dev);
- struct snd_pcm_substream *substream;
- struct snd_sof_pcm *spcm;
- snd_pcm_state_t state;
- int dir;
-
- /*
- * SOF requires hw_params to be set-up internally upon resume.
- * So, set the flag to indicate this for those streams that
- * have been suspended.
- */
- list_for_each_entry(spcm, &sdev->pcm_list, list) {
- for_each_pcm_streams(dir) {
- /*
- * do not reset hw_params upon resume for streams that
- * were kept running during suspend
- */
- if (spcm->stream[dir].suspend_ignored)
- continue;
-
- substream = spcm->stream[dir].substream;
- if (!substream || !substream->runtime)
- continue;
-
- state = substream->runtime->status->state;
- if (state == SNDRV_PCM_STATE_SUSPENDED)
- spcm->prepared[dir] = false;
- }
- }
-
- /* set internal flag for BE */
- return snd_sof_dsp_hw_params_upon_resume(sdev);
-}
-
int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
struct snd_sof_pcm *spcm, int dir, bool free_widget_list)
{
@@ -701,7 +818,8 @@ int sof_machine_check(struct snd_sof_dev *sdev)
return -ENOMEM;
mach->drv_name = "sof-nocodec";
- sof_pdata->tplg_filename = desc->nocodec_tplg_filename;
+ if (!sof_pdata->tplg_filename)
+ sof_pdata->tplg_filename = desc->nocodec_tplg_filename;
sof_pdata->machine = mach;
snd_sof_set_mach_params(mach, sdev);