ASoC: mediatek: common: revise SOF common code
authorTrevor Wu <trevor.wu@mediatek.com>
Fri, 25 Aug 2023 02:49:34 +0000 (10:49 +0800)
committerMark Brown <broonie@kernel.org>
Mon, 11 Sep 2023 00:18:55 +0000 (01:18 +0100)
Originally, normal dai link fixup callback is overwritten by sof fixup
callback on mtk_sof_card_late_probe and it relies on the mapping defined
on struct sof_conn_stream.

It's not flexible. When a new hardware connection is adopted, user needs
to update struct sof_conn_stream defined in machine driver which is used
to specify the mapping relationship of normal BE and SOF BE.

In the patch, mtk_sof_check_tplg_be_dai_link_fixup() is introduced for
all normal BEs. In mtk_sof_late_probe, back up normal BE fixup if it
exists and then overwrite be_hw_params_fixup by the new callback.

There are two cases for FE and BE connection.

case 1:
SOF FE -> normal BE
       -> SOF_BE

case 2:
normal FE -> normal BE

In the new fixup callback, it tries to find SOF_BE which connects to the
same FE, and then reuses the fixup of SOF_BE. If no SOF_BE exists,
it must be case 2, so rollback to the original fixup if it exists.

As a result, the predefined relation is not needed anymore. Hardware
connection can be controlled by the mixer control for AFE interconn.
Then, DPCM finds the BE mapping at runtime.

Signed-off-by: Trevor Wu <trevor.wu@mediatek.com>
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Link: https://lore.kernel.org/r/20230825024935.10878-3-trevor.wu@mediatek.com
Signed-off-by: Mark Brown <broonie@kernel.org>
sound/soc/mediatek/common/mtk-dsp-sof-common.c
sound/soc/mediatek/common/mtk-dsp-sof-common.h

index 6fef16306f74ff941f4b796583c06cc568f6ce71..f3894010f6563a0f949ec2b52307546eef648a81 100644 (file)
@@ -54,6 +54,8 @@ int mtk_sof_card_probe(struct snd_soc_card *card)
 {
        int i;
        struct snd_soc_dai_link *dai_link;
+       struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(card);
+       struct mtk_sof_priv *sof_priv = soc_card_data->sof_priv;
 
        /* Set stream_name to help sof bind widgets */
        for_each_card_prelinks(card, i, dai_link) {
@@ -61,10 +63,81 @@ int mtk_sof_card_probe(struct snd_soc_card *card)
                        dai_link->stream_name = dai_link->name;
        }
 
+       INIT_LIST_HEAD(&sof_priv->dai_link_list);
+
        return 0;
 }
 EXPORT_SYMBOL_GPL(mtk_sof_card_probe);
 
+static struct snd_soc_pcm_runtime *mtk_sof_find_tplg_be(struct snd_soc_pcm_runtime *rtd)
+{
+       struct snd_soc_card *card = rtd->card;
+       struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(card);
+       struct mtk_sof_priv *sof_priv = soc_card_data->sof_priv;
+       struct snd_soc_pcm_runtime *fe;
+       struct snd_soc_pcm_runtime *be;
+       struct snd_soc_dpcm *dpcm;
+       int i, stream;
+
+       for_each_pcm_streams(stream) {
+               fe = NULL;
+               for_each_dpcm_fe(rtd, stream, dpcm) {
+                       fe = dpcm->fe;
+                       if (fe)
+                               break;
+               }
+
+               if (!fe)
+                       continue;
+
+               for_each_dpcm_be(fe, stream, dpcm) {
+                       be = dpcm->be;
+                       if (be == rtd)
+                               continue;
+
+                       for (i = 0; i < sof_priv->num_streams; i++) {
+                               const struct sof_conn_stream *conn = &sof_priv->conn_streams[i];
+
+                               if (!strcmp(be->dai_link->name, conn->sof_link))
+                                       return be;
+                       }
+               }
+       }
+
+       return NULL;
+}
+
+/* fixup the BE DAI link to match any values from topology */
+static int mtk_sof_check_tplg_be_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
+                                               struct snd_pcm_hw_params *params)
+{
+       struct snd_soc_card *card = rtd->card;
+       struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(card);
+       struct mtk_sof_priv *sof_priv = soc_card_data->sof_priv;
+       struct snd_soc_pcm_runtime *sof_be;
+       struct mtk_dai_link *dai_link;
+       int ret = 0;
+
+       sof_be = mtk_sof_find_tplg_be(rtd);
+       if (sof_be) {
+               if (sof_priv->sof_dai_link_fixup)
+                       ret = sof_priv->sof_dai_link_fixup(rtd, params);
+               else if (sof_be->dai_link->be_hw_params_fixup)
+                       ret = sof_be->dai_link->be_hw_params_fixup(sof_be, params);
+       } else {
+               list_for_each_entry(dai_link, &sof_priv->dai_link_list, list) {
+                       if (strcmp(dai_link->name, rtd->dai_link->name) == 0) {
+                               if (dai_link->be_hw_params_fixup)
+                                       ret = dai_link->be_hw_params_fixup(rtd, params);
+
+                               break;
+                       }
+               }
+       }
+
+       return ret;
+}
+
 int mtk_sof_card_late_probe(struct snd_soc_card *card)
 {
        struct snd_soc_pcm_runtime *rtd;
@@ -72,6 +145,8 @@ int mtk_sof_card_late_probe(struct snd_soc_card *card)
        struct mtk_soc_card_data *soc_card_data =
                snd_soc_card_get_drvdata(card);
        struct mtk_sof_priv *sof_priv = soc_card_data->sof_priv;
+       struct snd_soc_dai_link *dai_link;
+       struct mtk_dai_link *mtk_dai_link;
        int i;
 
        /* 1. find sof component */
@@ -86,25 +161,37 @@ int mtk_sof_card_late_probe(struct snd_soc_card *card)
                return 0;
        }
 
-       /* 2. add route path and fixup callback */
+       /* 2. overwrite all BE fixups, and backup the existing fixup */
+       for_each_card_prelinks(card, i, dai_link) {
+               if (dai_link->be_hw_params_fixup) {
+                       mtk_dai_link = devm_kzalloc(card->dev,
+                                                   sizeof(*mtk_dai_link),
+                                                   GFP_KERNEL);
+                       if (!mtk_dai_link)
+                               return -ENOMEM;
+
+                       mtk_dai_link->be_hw_params_fixup = dai_link->be_hw_params_fixup;
+                       mtk_dai_link->name = dai_link->name;
+
+                       list_add(&mtk_dai_link->list, &sof_priv->dai_link_list);
+               }
+
+               if (dai_link->no_pcm)
+                       dai_link->be_hw_params_fixup = mtk_sof_check_tplg_be_dai_link_fixup;
+       }
+
+       /* 3. add route path and SOF_BE fixup callback */
        for (i = 0; i < sof_priv->num_streams; i++) {
                const struct sof_conn_stream *conn = &sof_priv->conn_streams[i];
                struct snd_soc_pcm_runtime *sof_rtd = NULL;
-               struct snd_soc_pcm_runtime *normal_rtd = NULL;
 
                for_each_card_rtds(card, rtd) {
                        if (!strcmp(rtd->dai_link->name, conn->sof_link)) {
                                sof_rtd = rtd;
-                               continue;
-                       }
-                       if (!strcmp(rtd->dai_link->name, conn->normal_link)) {
-                               normal_rtd = rtd;
-                               continue;
-                       }
-                       if (normal_rtd && sof_rtd)
                                break;
+                       }
                }
-               if (normal_rtd && sof_rtd) {
+               if (sof_rtd) {
                        int j;
                        struct snd_soc_dai *cpu_dai;
 
@@ -131,13 +218,9 @@ int mtk_sof_card_late_probe(struct snd_soc_card *card)
                                }
                        }
 
+                       /* overwrite SOF BE fixup */
                        sof_rtd->dai_link->be_hw_params_fixup =
                                sof_comp->driver->be_hw_params_fixup;
-                       if (sof_priv->sof_dai_link_fixup)
-                               normal_rtd->dai_link->be_hw_params_fixup =
-                                       sof_priv->sof_dai_link_fixup;
-                       else
-                               normal_rtd->dai_link->be_hw_params_fixup = mtk_sof_dai_link_fixup;
                }
        }
 
index dd38c4a93574f4e4bcd74200d66e7878d076a234..4bc5e1c0c8ed9b016b48e64e74afe9de747e7a99 100644 (file)
@@ -18,11 +18,19 @@ struct sof_conn_stream {
        int stream_dir;
 };
 
+struct mtk_dai_link {
+       const char *name;
+       int (*be_hw_params_fixup)(struct snd_soc_pcm_runtime *rtd,
+                                 struct snd_pcm_hw_params *params);
+       struct list_head list;
+};
+
 struct mtk_sof_priv {
        const struct sof_conn_stream *conn_streams;
        int num_streams;
        int (*sof_dai_link_fixup)(struct snd_soc_pcm_runtime *rtd,
                                  struct snd_pcm_hw_params *params);
+       struct list_head dai_link_list;
 };
 
 int mtk_sof_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,