Merge tag 'asoc-v6.10' of https://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[sfrench/cifs-2.6.git] / sound / soc / intel / boards / sof_realtek_common.c
index 80c8687cd1da5b7348b741a378f958e75191e797..dda346e0f7374ee6f2c9edc99aa58ee48e0379e2 100644 (file)
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 //
-// Copyright(c) 2020 Intel Corporation. All rights reserved.
+// Copyright(c) 2020 Intel Corporation
 
 #include <linux/device.h>
 #include <linux/kernel.h>
 #include "../../codecs/rt1011.h"
 #include "../../codecs/rt1015.h"
 #include "../../codecs/rt1308.h"
+#include "../common/soc-intel-quirks.h"
 #include "sof_realtek_common.h"
 
 /*
- * Current only 2-amp configuration is supported for rt1011
+ * Common structures and functions
+ */
+static const struct snd_kcontrol_new realtek_2spk_kcontrols[] = {
+       SOC_DAPM_PIN_SWITCH("Left Spk"),
+       SOC_DAPM_PIN_SWITCH("Right Spk"),
+
+};
+
+static const struct snd_soc_dapm_widget realtek_2spk_widgets[] = {
+       SND_SOC_DAPM_SPK("Left Spk", NULL),
+       SND_SOC_DAPM_SPK("Right Spk", NULL),
+};
+
+static const struct snd_kcontrol_new realtek_4spk_kcontrols[] = {
+       SOC_DAPM_PIN_SWITCH("WL Ext Spk"),
+       SOC_DAPM_PIN_SWITCH("WR Ext Spk"),
+       SOC_DAPM_PIN_SWITCH("TL Ext Spk"),
+       SOC_DAPM_PIN_SWITCH("TR Ext Spk"),
+};
+
+static const struct snd_soc_dapm_widget realtek_4spk_widgets[] = {
+       SND_SOC_DAPM_SPK("WL Ext Spk", NULL),
+       SND_SOC_DAPM_SPK("WR Ext Spk", NULL),
+       SND_SOC_DAPM_SPK("TL Ext Spk", NULL),
+       SND_SOC_DAPM_SPK("TR Ext Spk", NULL),
+};
+
+/* helper function to get the number of specific codec */
+static unsigned int get_num_codecs(const char *hid)
+{
+       struct acpi_device *adev;
+       unsigned int dev_num = 0;
+
+       for_each_acpi_dev_match(adev, hid, NULL, -1)
+               dev_num++;
+
+       return dev_num;
+}
+
+/*
+ * Realtek ALC1011
  */
 static const struct snd_soc_dapm_route speaker_map_lr[] = {
        /* speaker */
@@ -26,16 +67,14 @@ static const struct snd_soc_dapm_route speaker_map_lr[] = {
        { "Right Spk", NULL, "Right SPO" },
 };
 
-/*
- * Make sure device's Unique ID follows this configuration:
- *
- * Two speakers:
- *         0: left, 1: right
- * Four speakers:
- *         0: Woofer left, 1: Woofer right
- *         2: Tweeter left, 3: Tweeter right
- */
-static struct snd_soc_codec_conf rt1011_codec_confs[] = {
+static const struct snd_soc_dapm_route rt1011_4spk_routes[] = {
+       {"WL Ext Spk", NULL, "WL SPO" },
+       {"WR Ext Spk", NULL, "WR SPO" },
+       {"TL Ext Spk", NULL, "TL SPO" },
+       {"TR Ext Spk", NULL, "TR SPO" },
+};
+
+static struct snd_soc_codec_conf rt1011_2spk_codec_confs[] = {
        {
                .dlc = COMP_CODEC_CONF(RT1011_DEV0_NAME),
                .name_prefix = "Left",
@@ -46,6 +85,25 @@ static struct snd_soc_codec_conf rt1011_codec_confs[] = {
        },
 };
 
+static struct snd_soc_codec_conf rt1011_4spk_codec_confs[] = {
+       {
+               .dlc = COMP_CODEC_CONF(RT1011_DEV0_NAME),
+               .name_prefix = "WL",
+       },
+       {
+               .dlc = COMP_CODEC_CONF(RT1011_DEV1_NAME),
+               .name_prefix = "WR",
+       },
+       {
+               .dlc = COMP_CODEC_CONF(RT1011_DEV2_NAME),
+               .name_prefix = "TL",
+       },
+       {
+               .dlc = COMP_CODEC_CONF(RT1011_DEV3_NAME),
+               .name_prefix = "TR",
+       },
+};
+
 static struct snd_soc_dai_link_component rt1011_dai_link_components[] = {
        {
                .name = RT1011_DEV0_NAME,
@@ -55,6 +113,14 @@ static struct snd_soc_dai_link_component rt1011_dai_link_components[] = {
                .name = RT1011_DEV1_NAME,
                .dai_name = RT1011_CODEC_DAI,
        },
+       {
+               .name = RT1011_DEV2_NAME,
+               .dai_name = RT1011_CODEC_DAI,
+       },
+       {
+               .name = RT1011_DEV3_NAME,
+               .dai_name = RT1011_CODEC_DAI,
+       },
 };
 
 static const struct {
@@ -63,6 +129,8 @@ static const struct {
 } rt1011_tdm_mask[] = {
        {.tx = 0x4, .rx = 0x1},
        {.tx = 0x8, .rx = 0x2},
+       {.tx = 0x1, .rx = 0x1},
+       {.tx = 0x2, .rx = 0x2},
 };
 
 static int rt1011_hw_params(struct snd_pcm_substream *substream,
@@ -118,28 +186,125 @@ static const struct snd_soc_ops rt1011_ops = {
 static int rt1011_init(struct snd_soc_pcm_runtime *rtd)
 {
        struct snd_soc_card *card = rtd->card;
+       unsigned int num_codecs = get_num_codecs(RT1011_ACPI_HID);
        int ret;
 
-       ret = snd_soc_dapm_add_routes(&card->dapm, speaker_map_lr,
-                                     ARRAY_SIZE(speaker_map_lr));
-       if (ret)
-               dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
+       switch (num_codecs) {
+       case 2:
+               if (!soc_intel_is_cml()) {
+                       ret = snd_soc_dapm_new_controls(&card->dapm, realtek_2spk_widgets,
+                                                       ARRAY_SIZE(realtek_2spk_widgets));
+                       if (ret) {
+                               dev_err(rtd->dev, "fail to add rt1011 widgets, ret %d\n",
+                                       ret);
+                               return ret;
+                       }
+
+                       ret = snd_soc_add_card_controls(card, realtek_2spk_kcontrols,
+                                                       ARRAY_SIZE(realtek_2spk_kcontrols));
+                       if (ret) {
+                               dev_err(rtd->dev, "fail to add rt1011 kcontrols, ret %d\n",
+                                       ret);
+                               return ret;
+                       }
+
+                       ret = snd_soc_dapm_add_routes(&card->dapm, speaker_map_lr,
+                                                     ARRAY_SIZE(speaker_map_lr));
+                       if (ret) {
+                               dev_err(rtd->dev, "fail to add rt1011 routes, ret %d\n",
+                                       ret);
+                               return ret;
+                       }
+
+                       break;
+               }
+
+               /*
+                * register speaker widgets "WL Ext Spk" and "WR Ext Spk" to
+                * keep backward compatible with cml devices
+                */
+               fallthrough;
+       case 4:
+               ret = snd_soc_dapm_new_controls(&card->dapm, realtek_4spk_widgets,
+                                               num_codecs);
+               if (ret) {
+                       dev_err(rtd->dev, "fail to add rt1011 widgets, ret %d\n",
+                               ret);
+                       return ret;
+               }
+
+               ret = snd_soc_add_card_controls(card, realtek_4spk_kcontrols,
+                                               num_codecs);
+               if (ret) {
+                       dev_err(rtd->dev, "fail to add rt1011 controls, ret %d\n",
+                               ret);
+                       return ret;
+               }
+
+               ret = snd_soc_dapm_add_routes(&card->dapm, rt1011_4spk_routes,
+                                             num_codecs);
+               if (ret) {
+                       dev_err(rtd->dev, "fail to add rt1011 routes, ret %d\n",
+                               ret);
+                       return ret;
+               }
+               break;
+       default:
+               dev_err(rtd->dev, "rt1011: invalid num_codecs %d\n", num_codecs);
+               return -EINVAL;
+       }
+
        return ret;
 }
 
-void sof_rt1011_dai_link(struct snd_soc_dai_link *link)
+void sof_rt1011_dai_link(struct device *dev, struct snd_soc_dai_link *link)
 {
+       unsigned int num_codecs = get_num_codecs(RT1011_ACPI_HID);
+
        link->codecs = rt1011_dai_link_components;
-       link->num_codecs = ARRAY_SIZE(rt1011_dai_link_components);
+
+       switch (num_codecs) {
+       case 2:
+       case 4:
+               link->num_codecs = num_codecs;
+               break;
+       default:
+               dev_err(dev, "rt1011: invalid num_codecs %d\n", num_codecs);
+               break;
+       }
+
        link->init = rt1011_init;
        link->ops = &rt1011_ops;
 }
 EXPORT_SYMBOL_NS(sof_rt1011_dai_link, SND_SOC_INTEL_SOF_REALTEK_COMMON);
 
-void sof_rt1011_codec_conf(struct snd_soc_card *card)
+void sof_rt1011_codec_conf(struct device *dev, struct snd_soc_card *card)
 {
-       card->codec_conf = rt1011_codec_confs;
-       card->num_configs = ARRAY_SIZE(rt1011_codec_confs);
+       unsigned int num_codecs = get_num_codecs(RT1011_ACPI_HID);
+
+       switch (num_codecs) {
+       case 2:
+               if (soc_intel_is_cml()) {
+                       /*
+                        * use name prefix 'WL' and 'WR' for speaker widgets to
+                        * keep backward compatible with cml devices
+                        */
+                       card->codec_conf = rt1011_4spk_codec_confs;
+               } else {
+                       card->codec_conf = rt1011_2spk_codec_confs;
+               }
+
+               card->num_configs = num_codecs;
+               break;
+       case 4:
+               card->codec_conf = rt1011_4spk_codec_confs;
+               card->num_configs = ARRAY_SIZE(rt1011_4spk_codec_confs);
+               break;
+       default:
+               dev_err(dev, "rt1011: invalid num_codecs %d\n", num_codecs);
+               break;
+       }
+
 }
 EXPORT_SYMBOL_NS(sof_rt1011_codec_conf, SND_SOC_INTEL_SOF_REALTEK_COMMON);
 
@@ -149,59 +314,21 @@ EXPORT_SYMBOL_NS(sof_rt1011_codec_conf, SND_SOC_INTEL_SOF_REALTEK_COMMON);
  *
  * For stereo output, there are always two amplifiers on the board.
  * However, the ACPI implements only one device instance (UID=0) if they
- * are sharing the same enable pin. The code will detect the number of
- * device instance and use corresponding DAPM structures for
- * initialization.
+ * are sharing the same enable pin. This is the case of rt1015p.
  */
-static const struct snd_soc_dapm_route rt1015p_1dev_dapm_routes[] = {
+static const struct snd_soc_dapm_route rt1015p_dapm_routes[] = {
        /* speaker */
        { "Left Spk", NULL, "Speaker" },
        { "Right Spk", NULL, "Speaker" },
 };
 
-static const struct snd_soc_dapm_route rt1015p_2dev_dapm_routes[] = {
-       /* speaker */
-       { "Left Spk", NULL, "Left Speaker" },
-       { "Right Spk", NULL, "Right Speaker" },
-};
-
-static struct snd_soc_codec_conf rt1015p_codec_confs[] = {
-       {
-               .dlc = COMP_CODEC_CONF(RT1015P_DEV0_NAME),
-               .name_prefix = "Left",
-       },
-       {
-               .dlc = COMP_CODEC_CONF(RT1015P_DEV1_NAME),
-               .name_prefix = "Right",
-       },
-};
-
 static struct snd_soc_dai_link_component rt1015p_dai_link_components[] = {
        {
                .name = RT1015P_DEV0_NAME,
                .dai_name = RT1015P_CODEC_DAI,
        },
-       {
-               .name = RT1015P_DEV1_NAME,
-               .dai_name = RT1015P_CODEC_DAI,
-       },
 };
 
-static int rt1015p_get_num_codecs(void)
-{
-       static int dev_num;
-
-       if (dev_num)
-               return dev_num;
-
-       if (!acpi_dev_present("RTL1015", "1", -1))
-               dev_num = 1;
-       else
-               dev_num = 2;
-
-       return dev_num;
-}
-
 static int rt1015p_hw_params(struct snd_pcm_substream *substream,
                             struct snd_pcm_hw_params *params)
 {
@@ -219,12 +346,22 @@ static int rt1015p_init(struct snd_soc_pcm_runtime *rtd)
        struct snd_soc_card *card = rtd->card;
        int ret;
 
-       if (rt1015p_get_num_codecs() == 1)
-               ret = snd_soc_dapm_add_routes(&card->dapm, rt1015p_1dev_dapm_routes,
-                                             ARRAY_SIZE(rt1015p_1dev_dapm_routes));
-       else
-               ret = snd_soc_dapm_add_routes(&card->dapm, rt1015p_2dev_dapm_routes,
-                                             ARRAY_SIZE(rt1015p_2dev_dapm_routes));
+       ret = snd_soc_dapm_new_controls(&card->dapm, realtek_2spk_widgets,
+                                       ARRAY_SIZE(realtek_2spk_widgets));
+       if (ret) {
+               dev_err(rtd->dev, "fail to add rt1015p widgets, ret %d\n", ret);
+               return ret;
+       }
+
+       ret = snd_soc_add_card_controls(card, realtek_2spk_kcontrols,
+                                       ARRAY_SIZE(realtek_2spk_kcontrols));
+       if (ret) {
+               dev_err(rtd->dev, "fail to add rt1015p kcontrols, ret %d\n", ret);
+               return ret;
+       }
+
+       ret = snd_soc_dapm_add_routes(&card->dapm, rt1015p_dapm_routes,
+                                     ARRAY_SIZE(rt1015p_dapm_routes));
        if (ret)
                dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
        return ret;
@@ -233,7 +370,7 @@ static int rt1015p_init(struct snd_soc_pcm_runtime *rtd)
 void sof_rt1015p_dai_link(struct snd_soc_dai_link *link)
 {
        link->codecs = rt1015p_dai_link_components;
-       link->num_codecs = rt1015p_get_num_codecs();
+       link->num_codecs = ARRAY_SIZE(rt1015p_dai_link_components);
        link->init = rt1015p_init;
        link->ops = &rt1015p_ops;
 }
@@ -241,11 +378,6 @@ EXPORT_SYMBOL_NS(sof_rt1015p_dai_link, SND_SOC_INTEL_SOF_REALTEK_COMMON);
 
 void sof_rt1015p_codec_conf(struct snd_soc_card *card)
 {
-       if (rt1015p_get_num_codecs() == 1)
-               return;
-
-       card->codec_conf = rt1015p_codec_confs;
-       card->num_configs = ARRAY_SIZE(rt1015p_codec_confs);
 }
 EXPORT_SYMBOL_NS(sof_rt1015p_codec_conf, SND_SOC_INTEL_SOF_REALTEK_COMMON);
 
@@ -348,8 +480,42 @@ static struct snd_soc_dai_link_component rt1015_components[] = {
 
 static int speaker_codec_init_lr(struct snd_soc_pcm_runtime *rtd)
 {
-       return snd_soc_dapm_add_routes(&rtd->card->dapm, speaker_map_lr,
-                                       ARRAY_SIZE(speaker_map_lr));
+       struct snd_soc_card *card = rtd->card;
+       unsigned int num_codecs = get_num_codecs(RT1015_ACPI_HID);
+       int ret;
+
+       switch (num_codecs) {
+       case 2:
+               ret = snd_soc_dapm_new_controls(&card->dapm, realtek_2spk_widgets,
+                                               ARRAY_SIZE(realtek_2spk_widgets));
+               if (ret) {
+                       dev_err(rtd->dev, "fail to add rt1015 widgets, ret %d\n",
+                               ret);
+                       return ret;
+               }
+
+               ret = snd_soc_add_card_controls(card, realtek_2spk_kcontrols,
+                                               ARRAY_SIZE(realtek_2spk_kcontrols));
+               if (ret) {
+                       dev_err(rtd->dev, "fail to add rt1015 kcontrols, ret %d\n",
+                               ret);
+                       return ret;
+               }
+
+               ret = snd_soc_dapm_add_routes(&rtd->card->dapm, speaker_map_lr,
+                                             ARRAY_SIZE(speaker_map_lr));
+               if (ret) {
+                       dev_err(rtd->dev, "fail to add rt1015 routes, ret %d\n",
+                               ret);
+                       return ret;
+               }
+               break;
+       default:
+               dev_err(rtd->dev, "rt1015: invalid num_codecs %d\n", num_codecs);
+               return -EINVAL;
+       }
+
+       return ret;
 }
 
 void sof_rt1015_codec_conf(struct snd_soc_card *card)
@@ -486,6 +652,20 @@ static int rt1019p_init(struct snd_soc_pcm_runtime *rtd)
        struct snd_soc_card *card = rtd->card;
        int ret;
 
+       ret = snd_soc_dapm_new_controls(&card->dapm, realtek_2spk_widgets,
+                                       ARRAY_SIZE(realtek_2spk_widgets));
+       if (ret) {
+               dev_err(rtd->dev, "fail to add rt1019p widgets, ret %d\n", ret);
+               return ret;
+       }
+
+       ret = snd_soc_add_card_controls(card, realtek_2spk_kcontrols,
+                                       ARRAY_SIZE(realtek_2spk_kcontrols));
+       if (ret) {
+               dev_err(rtd->dev, "fail to add rt1019p kcontrols, ret %d\n", ret);
+               return ret;
+       }
+
        ret = snd_soc_dapm_add_routes(&card->dapm, rt1019p_dapm_routes,
                                      ARRAY_SIZE(rt1019p_dapm_routes));
        if (ret) {