#include <sound/soc-acpi.h>
 #include <sound/soc-dai.h>
 #include <sound/soc-dapm.h>
+#include <sound/sof.h>
 #include <uapi/sound/asound.h>
 #include "../../codecs/rt1011.h"
 #include "../../codecs/rt1015.h"
+#include "../../codecs/rt1308.h"
 #include "sof_realtek_common.h"
 
 /*
 }
 EXPORT_SYMBOL_NS(sof_rt1015_dai_link, SND_SOC_INTEL_SOF_REALTEK_COMMON);
 
+/*
+ * RT1308 audio amplifier
+ */
+static const struct snd_kcontrol_new rt1308_kcontrols[] = {
+       SOC_DAPM_PIN_SWITCH("Speakers"),
+};
+
+static const struct snd_soc_dapm_widget rt1308_dapm_widgets[] = {
+       SND_SOC_DAPM_SPK("Speakers", NULL),
+};
+
+static const struct snd_soc_dapm_route rt1308_dapm_routes[] = {
+       /* speaker */
+       {"Speakers", NULL, "SPOL"},
+       {"Speakers", NULL, "SPOR"},
+};
+
+static struct snd_soc_dai_link_component rt1308_components[] = {
+       {
+               .name = RT1308_DEV0_NAME,
+               .dai_name = RT1308_CODEC_DAI,
+       }
+};
+
+static int rt1308_init(struct snd_soc_pcm_runtime *rtd)
+{
+       struct snd_soc_card *card = rtd->card;
+       int ret;
+
+       ret = snd_soc_dapm_new_controls(&card->dapm, rt1308_dapm_widgets,
+                                       ARRAY_SIZE(rt1308_dapm_widgets));
+       if (ret) {
+               dev_err(rtd->dev, "fail to add dapm controls, ret %d\n", ret);
+               return ret;
+       }
+
+       ret = snd_soc_add_card_controls(card, rt1308_kcontrols,
+                                       ARRAY_SIZE(rt1308_kcontrols));
+       if (ret) {
+               dev_err(rtd->dev, "fail to add card controls, ret %d\n", ret);
+               return ret;
+       }
+
+       ret = snd_soc_dapm_add_routes(&card->dapm, rt1308_dapm_routes,
+                                     ARRAY_SIZE(rt1308_dapm_routes));
+
+       if (ret)
+               dev_err(rtd->dev, "fail to add dapm routes, ret %d\n", ret);
+
+       return ret;
+}
+
+static int rt1308_hw_params(struct snd_pcm_substream *substream,
+                           struct snd_pcm_hw_params *params)
+{
+       struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
+       struct snd_soc_card *card = rtd->card;
+       struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
+       int clk_id, clk_freq, pll_out;
+       int ret;
+
+       clk_id = RT1308_PLL_S_MCLK;
+       /* get the tplg configured mclk. */
+       clk_freq = sof_dai_get_mclk(rtd);
+
+       pll_out = params_rate(params) * 512;
+
+       /* Set rt1308 pll */
+       ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
+       if (ret < 0) {
+               dev_err(card->dev, "Failed to set RT1308 PLL: %d\n", ret);
+               return ret;
+       }
+
+       /* Set rt1308 sysclk */
+       ret = snd_soc_dai_set_sysclk(codec_dai, RT1308_FS_SYS_S_PLL, pll_out,
+                                    SND_SOC_CLOCK_IN);
+       if (ret < 0)
+               dev_err(card->dev, "Failed to set RT1308 SYSCLK: %d\n", ret);
+
+       return ret;
+}
+
+static const struct snd_soc_ops rt1308_ops = {
+       .hw_params = rt1308_hw_params,
+};
+
+void sof_rt1308_dai_link(struct snd_soc_dai_link *link)
+{
+       link->codecs = rt1308_components;
+       link->num_codecs = ARRAY_SIZE(rt1308_components);
+       link->init = rt1308_init;
+       link->ops = &rt1308_ops;
+}
+EXPORT_SYMBOL_NS(sof_rt1308_dai_link, SND_SOC_INTEL_SOF_REALTEK_COMMON);
+
 MODULE_DESCRIPTION("ASoC Intel SOF Realtek helpers");
 MODULE_LICENSE("GPL");
 
 #include <sound/pcm.h>
 #include <sound/pcm_params.h>
 #include <sound/sof.h>
-#include "../../codecs/rt1308.h"
+#include "sof_realtek_common.h"
 
 #define SOF_RT1308_SSP_CODEC(quirk)            ((quirk) & GENMASK(3, 0))
 #define SOF_RT1308_SSP_CODEC_MASK                      (GENMASK(3, 0))
 #define SOF_HDMI_CAPTURE_2_SSP(quirk)  \
        (((quirk) << SOF_HDMI_CAPTURE_2_SSP_SHIFT) & SOF_HDMI_CAPTURE_2_SSP_MASK)
 
+#define SOF_RT1308_SPEAKER_AMP_PRESENT         BIT(13)
+
 /* Default: SSP2  */
 static unsigned long sof_rt1308_quirk = SOF_RT1308_SSP_CODEC(2);
 
 static const struct snd_soc_dapm_widget sof_rt1308_dapm_widgets[] = {
-       SND_SOC_DAPM_SPK("Speakers", NULL),
        SND_SOC_DAPM_MIC("SoC DMIC", NULL),
 };
 
-static const struct snd_kcontrol_new sof_rt1308_controls[] = {
-       SOC_DAPM_PIN_SWITCH("Speakers"),
-};
-
 static const struct snd_soc_dapm_route sof_rt1308_dapm_routes[] = {
-       { "Speakers", NULL, "SPOL" },
-       { "Speakers", NULL, "SPOR" },
-
        /* digital mics */
        {"DMic", NULL, "SoC DMIC"},
 };
 static struct snd_soc_card sof_rt1308_card = {
        .name         = "rt1308",
        .owner        = THIS_MODULE,
-       .controls = sof_rt1308_controls,
-       .num_controls = ARRAY_SIZE(sof_rt1308_controls),
        .dapm_widgets = sof_rt1308_dapm_widgets,
        .num_dapm_widgets = ARRAY_SIZE(sof_rt1308_dapm_widgets),
        .dapm_routes = sof_rt1308_dapm_routes,
        .fully_routed = true,
 };
 
-static int sof_rt1308_hw_params(struct snd_pcm_substream *substream,
-                               struct snd_pcm_hw_params *params)
-{
-       struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
-       struct snd_soc_card *card = rtd->card;
-       struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
-       int clk_id, clk_freq, pll_out;
-       int ret;
-
-       clk_id = RT1308_PLL_S_MCLK;
-       /* get the tplg configured mclk. */
-       clk_freq = sof_dai_get_mclk(rtd);
-
-       pll_out = params_rate(params) * 512;
-
-       /* Set rt1308 pll */
-       ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
-       if (ret < 0) {
-               dev_err(card->dev, "Failed to set RT1308 PLL: %d\n", ret);
-               return ret;
-       }
-
-       /* Set rt1308 sysclk */
-       ret = snd_soc_dai_set_sysclk(codec_dai, RT1308_FS_SYS_S_PLL, pll_out,
-                                    SND_SOC_CLOCK_IN);
-       if (ret < 0)
-               dev_err(card->dev, "Failed to set RT1308 SYSCLK: %d\n", ret);
-
-       return ret;
-}
-
 static struct snd_soc_dai_link_component platform_component[] = {
        {
                /* name might be overridden during probe */
        }
 };
 
-static struct snd_soc_dai_link_component rt1308_component[] = {
-       {
-               .name = "i2c-10EC1308:00",
-               .dai_name = "rt1308-aif",
-       }
-};
-
 static struct snd_soc_dai_link_component dmic_component[] = {
        {
                .name = "dmic-codec",
        }
 };
 
-/* machine stream operations */
-static const struct snd_soc_ops sof_rt1308_ops = {
-       .hw_params = sof_rt1308_hw_params,
-};
-
 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
                                                          int ssp_codec,
                                                          int dmic_be_num)
                return NULL;
 
        links[id].id = id;
-       links[id].codecs = rt1308_component;
-       links[id].num_codecs = ARRAY_SIZE(rt1308_component);
+       if (sof_rt1308_quirk & SOF_RT1308_SPEAKER_AMP_PRESENT) {
+               sof_rt1308_dai_link(&links[id]);
+       }
        links[id].platforms = platform_component;
        links[id].num_platforms = ARRAY_SIZE(platform_component);
-       links[id].ops = &sof_rt1308_ops;
        links[id].dpcm_playback = 1;
        links[id].no_pcm = 1;
        links[id].cpus = &cpus[id];
                                        SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
                                        SOF_HDMI_CAPTURE_1_SSP(1) |
                                        SOF_HDMI_CAPTURE_2_SSP(5) |
-                                       SOF_SSP_HDMI_CAPTURE_PRESENT),
+                                       SOF_SSP_HDMI_CAPTURE_PRESENT |
+                                       SOF_RT1308_SPEAKER_AMP_PRESENT),
        },
        { }
 };
 module_platform_driver(sof_rt1308_driver);
 
 MODULE_DESCRIPTION("ASoC Intel(R) SOF + RT1308 Machine driver");
+MODULE_AUTHOR("balamurugan.c <balamurugan.c@intel.com>");
+MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
 MODULE_LICENSE("GPL");
-MODULE_ALIAS("platform:sof_rt1308");
+MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);