Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  bytcr_rt5651.c - ASoc Machine driver for Intel Byt CR platform
0004  *  (derived from bytcr_rt5640.c)
0005  *
0006  *  Copyright (C) 2015 Intel Corp
0007  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0008  *
0009  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0010  */
0011 
0012 #include <linux/init.h>
0013 #include <linux/i2c.h>
0014 #include <linux/module.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/property.h>
0017 #include <linux/acpi.h>
0018 #include <linux/clk.h>
0019 #include <linux/device.h>
0020 #include <linux/dmi.h>
0021 #include <linux/input.h>
0022 #include <linux/gpio/consumer.h>
0023 #include <linux/gpio/machine.h>
0024 #include <linux/slab.h>
0025 #include <sound/pcm.h>
0026 #include <sound/pcm_params.h>
0027 #include <sound/soc.h>
0028 #include <sound/jack.h>
0029 #include <sound/soc-acpi.h>
0030 #include "../../codecs/rt5651.h"
0031 #include "../atom/sst-atom-controls.h"
0032 #include "../common/soc-intel-quirks.h"
0033 
0034 enum {
0035     BYT_RT5651_DMIC_MAP,
0036     BYT_RT5651_IN1_MAP,
0037     BYT_RT5651_IN2_MAP,
0038     BYT_RT5651_IN1_IN2_MAP,
0039 };
0040 
0041 enum {
0042     BYT_RT5651_JD_NULL  = (RT5651_JD_NULL << 4),
0043     BYT_RT5651_JD1_1    = (RT5651_JD1_1 << 4),
0044     BYT_RT5651_JD1_2    = (RT5651_JD1_2 << 4),
0045     BYT_RT5651_JD2      = (RT5651_JD2 << 4),
0046 };
0047 
0048 enum {
0049     BYT_RT5651_OVCD_TH_600UA  = (6 << 8),
0050     BYT_RT5651_OVCD_TH_1500UA = (15 << 8),
0051     BYT_RT5651_OVCD_TH_2000UA = (20 << 8),
0052 };
0053 
0054 enum {
0055     BYT_RT5651_OVCD_SF_0P5  = (RT5651_OVCD_SF_0P5 << 13),
0056     BYT_RT5651_OVCD_SF_0P75 = (RT5651_OVCD_SF_0P75 << 13),
0057     BYT_RT5651_OVCD_SF_1P0  = (RT5651_OVCD_SF_1P0 << 13),
0058     BYT_RT5651_OVCD_SF_1P5  = (RT5651_OVCD_SF_1P5 << 13),
0059 };
0060 
0061 #define BYT_RT5651_MAP(quirk)       ((quirk) & GENMASK(3, 0))
0062 #define BYT_RT5651_JDSRC(quirk)     (((quirk) & GENMASK(7, 4)) >> 4)
0063 #define BYT_RT5651_OVCD_TH(quirk)   (((quirk) & GENMASK(12, 8)) >> 8)
0064 #define BYT_RT5651_OVCD_SF(quirk)   (((quirk) & GENMASK(14, 13)) >> 13)
0065 #define BYT_RT5651_DMIC_EN      BIT(16)
0066 #define BYT_RT5651_MCLK_EN      BIT(17)
0067 #define BYT_RT5651_MCLK_25MHZ       BIT(18)
0068 #define BYT_RT5651_SSP2_AIF2        BIT(19) /* default is using AIF1  */
0069 #define BYT_RT5651_SSP0_AIF1        BIT(20)
0070 #define BYT_RT5651_SSP0_AIF2        BIT(21)
0071 #define BYT_RT5651_HP_LR_SWAPPED    BIT(22)
0072 #define BYT_RT5651_MONO_SPEAKER     BIT(23)
0073 #define BYT_RT5651_JD_NOT_INV       BIT(24)
0074 
0075 #define BYT_RT5651_DEFAULT_QUIRKS   (BYT_RT5651_MCLK_EN | \
0076                      BYT_RT5651_JD1_1   | \
0077                      BYT_RT5651_OVCD_TH_2000UA | \
0078                      BYT_RT5651_OVCD_SF_0P75)
0079 
0080 /* jack-detect-source + inv + dmic-en + ovcd-th + -sf + terminating entry */
0081 #define MAX_NO_PROPS 6
0082 
0083 struct byt_rt5651_private {
0084     struct clk *mclk;
0085     struct gpio_desc *ext_amp_gpio;
0086     struct gpio_desc *hp_detect;
0087     struct snd_soc_jack jack;
0088     struct device *codec_dev;
0089 };
0090 
0091 static const struct acpi_gpio_mapping *byt_rt5651_gpios;
0092 
0093 /* Default: jack-detect on JD1_1, internal mic on in2, headsetmic on in3 */
0094 static unsigned long byt_rt5651_quirk = BYT_RT5651_DEFAULT_QUIRKS |
0095                     BYT_RT5651_IN2_MAP;
0096 
0097 static int quirk_override = -1;
0098 module_param_named(quirk, quirk_override, int, 0444);
0099 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
0100 
0101 static void log_quirks(struct device *dev)
0102 {
0103     if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_DMIC_MAP)
0104         dev_info(dev, "quirk DMIC_MAP enabled");
0105     if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_MAP)
0106         dev_info(dev, "quirk IN1_MAP enabled");
0107     if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN2_MAP)
0108         dev_info(dev, "quirk IN2_MAP enabled");
0109     if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_IN2_MAP)
0110         dev_info(dev, "quirk IN1_IN2_MAP enabled");
0111     if (BYT_RT5651_JDSRC(byt_rt5651_quirk)) {
0112         dev_info(dev, "quirk realtek,jack-detect-source %ld\n",
0113              BYT_RT5651_JDSRC(byt_rt5651_quirk));
0114         dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n",
0115              BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
0116         dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n",
0117              BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
0118     }
0119     if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
0120         dev_info(dev, "quirk DMIC enabled");
0121     if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
0122         dev_info(dev, "quirk MCLK_EN enabled");
0123     if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
0124         dev_info(dev, "quirk MCLK_25MHZ enabled");
0125     if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2)
0126         dev_info(dev, "quirk SSP2_AIF2 enabled\n");
0127     if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1)
0128         dev_info(dev, "quirk SSP0_AIF1 enabled\n");
0129     if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)
0130         dev_info(dev, "quirk SSP0_AIF2 enabled\n");
0131     if (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER)
0132         dev_info(dev, "quirk MONO_SPEAKER enabled\n");
0133     if (byt_rt5651_quirk & BYT_RT5651_JD_NOT_INV)
0134         dev_info(dev, "quirk JD_NOT_INV enabled\n");
0135 }
0136 
0137 #define BYT_CODEC_DAI1  "rt5651-aif1"
0138 #define BYT_CODEC_DAI2  "rt5651-aif2"
0139 
0140 static int byt_rt5651_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
0141                           int rate, int bclk_ratio)
0142 {
0143     int clk_id, clk_freq, ret;
0144 
0145     /* Configure the PLL before selecting it */
0146     if (!(byt_rt5651_quirk & BYT_RT5651_MCLK_EN)) {
0147         clk_id = RT5651_PLL1_S_BCLK1;
0148         clk_freq = rate * bclk_ratio;
0149     } else {
0150         clk_id = RT5651_PLL1_S_MCLK;
0151         if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
0152             clk_freq = 25000000;
0153         else
0154             clk_freq = 19200000;
0155     }
0156     ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, rate * 512);
0157     if (ret < 0) {
0158         dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
0159         return ret;
0160     }
0161 
0162     ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1,
0163                      rate * 512, SND_SOC_CLOCK_IN);
0164     if (ret < 0) {
0165         dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
0166         return ret;
0167     }
0168 
0169     return 0;
0170 }
0171 
0172 static int platform_clock_control(struct snd_soc_dapm_widget *w,
0173                   struct snd_kcontrol *k, int  event)
0174 {
0175     struct snd_soc_dapm_context *dapm = w->dapm;
0176     struct snd_soc_card *card = dapm->card;
0177     struct snd_soc_dai *codec_dai;
0178     struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
0179     int ret;
0180 
0181     codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
0182     if (!codec_dai)
0183         codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
0184     if (!codec_dai) {
0185         dev_err(card->dev,
0186             "Codec dai not found; Unable to set platform clock\n");
0187         return -EIO;
0188     }
0189 
0190     if (SND_SOC_DAPM_EVENT_ON(event)) {
0191         ret = clk_prepare_enable(priv->mclk);
0192         if (ret < 0) {
0193             dev_err(card->dev, "could not configure MCLK state");
0194             return ret;
0195         }
0196         ret = byt_rt5651_prepare_and_enable_pll1(codec_dai, 48000, 50);
0197     } else {
0198         /*
0199          * Set codec clock source to internal clock before
0200          * turning off the platform clock. Codec needs clock
0201          * for Jack detection and button press
0202          */
0203         ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_RCCLK,
0204                          48000 * 512,
0205                          SND_SOC_CLOCK_IN);
0206         if (!ret)
0207             clk_disable_unprepare(priv->mclk);
0208     }
0209 
0210     if (ret < 0) {
0211         dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
0212         return ret;
0213     }
0214 
0215     return 0;
0216 }
0217 
0218 static int rt5651_ext_amp_power_event(struct snd_soc_dapm_widget *w,
0219     struct snd_kcontrol *kcontrol, int event)
0220 {
0221     struct snd_soc_card *card = w->dapm->card;
0222     struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
0223 
0224     if (SND_SOC_DAPM_EVENT_ON(event))
0225         gpiod_set_value_cansleep(priv->ext_amp_gpio, 1);
0226     else
0227         gpiod_set_value_cansleep(priv->ext_amp_gpio, 0);
0228 
0229     return 0;
0230 }
0231 
0232 static const struct snd_soc_dapm_widget byt_rt5651_widgets[] = {
0233     SND_SOC_DAPM_HP("Headphone", NULL),
0234     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0235     SND_SOC_DAPM_MIC("Internal Mic", NULL),
0236     SND_SOC_DAPM_SPK("Speaker", NULL),
0237     SND_SOC_DAPM_LINE("Line In", NULL),
0238     SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
0239                 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
0240                 SND_SOC_DAPM_POST_PMD),
0241     SND_SOC_DAPM_SUPPLY("Ext Amp Power", SND_SOC_NOPM, 0, 0,
0242                 rt5651_ext_amp_power_event,
0243                 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
0244 };
0245 
0246 static const struct snd_soc_dapm_route byt_rt5651_audio_map[] = {
0247     {"Headphone", NULL, "Platform Clock"},
0248     {"Headset Mic", NULL, "Platform Clock"},
0249     {"Internal Mic", NULL, "Platform Clock"},
0250     {"Speaker", NULL, "Platform Clock"},
0251     {"Speaker", NULL, "Ext Amp Power"},
0252     {"Line In", NULL, "Platform Clock"},
0253 
0254     {"Headset Mic", NULL, "micbias1"}, /* lowercase for rt5651 */
0255     {"Headphone", NULL, "HPOL"},
0256     {"Headphone", NULL, "HPOR"},
0257     {"Speaker", NULL, "LOUTL"},
0258     {"Speaker", NULL, "LOUTR"},
0259     {"IN2P", NULL, "Line In"},
0260     {"IN2N", NULL, "Line In"},
0261 
0262 };
0263 
0264 static const struct snd_soc_dapm_route byt_rt5651_intmic_dmic_map[] = {
0265     {"DMIC L1", NULL, "Internal Mic"},
0266     {"DMIC R1", NULL, "Internal Mic"},
0267     {"IN2P", NULL, "Headset Mic"},
0268 };
0269 
0270 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_map[] = {
0271     {"Internal Mic", NULL, "micbias1"},
0272     {"IN1P", NULL, "Internal Mic"},
0273     {"IN3P", NULL, "Headset Mic"},
0274 };
0275 
0276 static const struct snd_soc_dapm_route byt_rt5651_intmic_in2_map[] = {
0277     {"Internal Mic", NULL, "micbias1"},
0278     {"IN2P", NULL, "Internal Mic"},
0279     {"IN3P", NULL, "Headset Mic"},
0280 };
0281 
0282 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_in2_map[] = {
0283     {"Internal Mic", NULL, "micbias1"},
0284     {"IN1P", NULL, "Internal Mic"},
0285     {"IN2P", NULL, "Internal Mic"},
0286     {"IN3P", NULL, "Headset Mic"},
0287 };
0288 
0289 static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif1_map[] = {
0290     {"ssp0 Tx", NULL, "modem_out"},
0291     {"modem_in", NULL, "ssp0 Rx"},
0292 
0293     {"AIF1 Playback", NULL, "ssp0 Tx"},
0294     {"ssp0 Rx", NULL, "AIF1 Capture"},
0295 };
0296 
0297 static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif2_map[] = {
0298     {"ssp0 Tx", NULL, "modem_out"},
0299     {"modem_in", NULL, "ssp0 Rx"},
0300 
0301     {"AIF2 Playback", NULL, "ssp0 Tx"},
0302     {"ssp0 Rx", NULL, "AIF2 Capture"},
0303 };
0304 
0305 static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif1_map[] = {
0306     {"ssp2 Tx", NULL, "codec_out0"},
0307     {"ssp2 Tx", NULL, "codec_out1"},
0308     {"codec_in0", NULL, "ssp2 Rx"},
0309     {"codec_in1", NULL, "ssp2 Rx"},
0310 
0311     {"AIF1 Playback", NULL, "ssp2 Tx"},
0312     {"ssp2 Rx", NULL, "AIF1 Capture"},
0313 };
0314 
0315 static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif2_map[] = {
0316     {"ssp2 Tx", NULL, "codec_out0"},
0317     {"ssp2 Tx", NULL, "codec_out1"},
0318     {"codec_in0", NULL, "ssp2 Rx"},
0319     {"codec_in1", NULL, "ssp2 Rx"},
0320 
0321     {"AIF2 Playback", NULL, "ssp2 Tx"},
0322     {"ssp2 Rx", NULL, "AIF2 Capture"},
0323 };
0324 
0325 static const struct snd_kcontrol_new byt_rt5651_controls[] = {
0326     SOC_DAPM_PIN_SWITCH("Headphone"),
0327     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0328     SOC_DAPM_PIN_SWITCH("Internal Mic"),
0329     SOC_DAPM_PIN_SWITCH("Speaker"),
0330     SOC_DAPM_PIN_SWITCH("Line In"),
0331 };
0332 
0333 static struct snd_soc_jack_pin bytcr_jack_pins[] = {
0334     {
0335         .pin    = "Headphone",
0336         .mask   = SND_JACK_HEADPHONE,
0337     },
0338     {
0339         .pin    = "Headset Mic",
0340         .mask   = SND_JACK_MICROPHONE,
0341     },
0342 };
0343 
0344 static int byt_rt5651_aif1_hw_params(struct snd_pcm_substream *substream,
0345                     struct snd_pcm_hw_params *params)
0346 {
0347     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0348     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0349     snd_pcm_format_t format = params_format(params);
0350     int rate = params_rate(params);
0351     int bclk_ratio;
0352 
0353     if (format == SNDRV_PCM_FORMAT_S16_LE)
0354         bclk_ratio = 32;
0355     else
0356         bclk_ratio = 50;
0357 
0358     return byt_rt5651_prepare_and_enable_pll1(codec_dai, rate, bclk_ratio);
0359 }
0360 
0361 static const struct acpi_gpio_params pov_p1006w_hp_detect = { 1, 0, false };
0362 static const struct acpi_gpio_params pov_p1006w_ext_amp_en = { 2, 0, true };
0363 
0364 static const struct acpi_gpio_mapping byt_rt5651_pov_p1006w_gpios[] = {
0365     { "hp-detect-gpios", &pov_p1006w_hp_detect, 1, },
0366     { "ext-amp-enable-gpios", &pov_p1006w_ext_amp_en, 1, },
0367     { },
0368 };
0369 
0370 static int byt_rt5651_pov_p1006w_quirk_cb(const struct dmi_system_id *id)
0371 {
0372     byt_rt5651_quirk = (unsigned long)id->driver_data;
0373     byt_rt5651_gpios = byt_rt5651_pov_p1006w_gpios;
0374     return 1;
0375 }
0376 
0377 static int byt_rt5651_quirk_cb(const struct dmi_system_id *id)
0378 {
0379     byt_rt5651_quirk = (unsigned long)id->driver_data;
0380     return 1;
0381 }
0382 
0383 static const struct dmi_system_id byt_rt5651_quirk_table[] = {
0384     {
0385         /* Chuwi Hi8 Pro (CWI513) */
0386         .callback = byt_rt5651_quirk_cb,
0387         .matches = {
0388             DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
0389             DMI_MATCH(DMI_PRODUCT_NAME, "X1D3_C806N"),
0390         },
0391         .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
0392                     BYT_RT5651_IN2_MAP |
0393                     BYT_RT5651_HP_LR_SWAPPED |
0394                     BYT_RT5651_MONO_SPEAKER),
0395     },
0396     {
0397         /* Chuwi Vi8 Plus (CWI519) */
0398         .callback = byt_rt5651_quirk_cb,
0399         .matches = {
0400             DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
0401             DMI_MATCH(DMI_PRODUCT_NAME, "D2D3_Vi8A1"),
0402         },
0403         .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
0404                     BYT_RT5651_IN2_MAP |
0405                     BYT_RT5651_HP_LR_SWAPPED |
0406                     BYT_RT5651_MONO_SPEAKER),
0407     },
0408     {
0409         /* Complet Electro Serv MY8307 */
0410         .callback = byt_rt5651_quirk_cb,
0411         .matches = {
0412             DMI_MATCH(DMI_SYS_VENDOR, "Complet Electro Serv"),
0413             DMI_MATCH(DMI_PRODUCT_NAME, "MY8307"),
0414         },
0415         .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
0416                     BYT_RT5651_IN2_MAP |
0417                     BYT_RT5651_MONO_SPEAKER |
0418                     BYT_RT5651_JD_NOT_INV),
0419     },
0420     {
0421         /* I.T.Works TW701, Ployer Momo7w and Trekstor ST70416-6
0422          * (these all use the same mainboard) */
0423         .callback = byt_rt5651_quirk_cb,
0424         .matches = {
0425             DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."),
0426             /* Partial match for all of itWORKS.G.WI71C.JGBMRBA,
0427              * TREK.G.WI71C.JGBMRBA0x and MOMO.G.WI71C.MABMRBA02 */
0428             DMI_MATCH(DMI_BIOS_VERSION, ".G.WI71C."),
0429         },
0430         .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
0431                     BYT_RT5651_IN2_MAP |
0432                     BYT_RT5651_SSP0_AIF1 |
0433                     BYT_RT5651_MONO_SPEAKER),
0434     },
0435     {
0436         /* Jumper EZpad 7 */
0437         .callback = byt_rt5651_quirk_cb,
0438         .matches = {
0439             DMI_MATCH(DMI_SYS_VENDOR, "Jumper"),
0440             DMI_MATCH(DMI_PRODUCT_NAME, "EZpad"),
0441             /* Jumper12x.WJ2012.bsBKRCP05 with the version dropped */
0442             DMI_MATCH(DMI_BIOS_VERSION, "Jumper12x.WJ2012.bsBKRCP"),
0443         },
0444         .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
0445                     BYT_RT5651_IN2_MAP |
0446                     BYT_RT5651_JD_NOT_INV),
0447     },
0448     {
0449         /* KIANO SlimNote 14.2 */
0450         .callback = byt_rt5651_quirk_cb,
0451         .matches = {
0452             DMI_MATCH(DMI_SYS_VENDOR, "KIANO"),
0453             DMI_MATCH(DMI_PRODUCT_NAME, "KIANO SlimNote 14.2"),
0454         },
0455         .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
0456                     BYT_RT5651_IN1_IN2_MAP),
0457     },
0458     {
0459         /* Minnowboard Max B3 */
0460         .callback = byt_rt5651_quirk_cb,
0461         .matches = {
0462             DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
0463             DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
0464         },
0465         .driver_data = (void *)(BYT_RT5651_IN1_MAP),
0466     },
0467     {
0468         /* Minnowboard Turbot */
0469         .callback = byt_rt5651_quirk_cb,
0470         .matches = {
0471             DMI_MATCH(DMI_SYS_VENDOR, "ADI"),
0472             DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Turbot"),
0473         },
0474         .driver_data = (void *)(BYT_RT5651_MCLK_EN |
0475                     BYT_RT5651_IN1_MAP),
0476     },
0477     {
0478         /* Point of View mobii wintab p1006w (v1.0) */
0479         .callback = byt_rt5651_pov_p1006w_quirk_cb,
0480         .matches = {
0481             DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
0482             DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "BayTrail"),
0483             /* Note 105b is Foxcon's USB/PCI vendor id */
0484             DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "105B"),
0485             DMI_EXACT_MATCH(DMI_BOARD_NAME, "0E57"),
0486         },
0487         .driver_data = (void *)(BYT_RT5651_DMIC_MAP |
0488                     BYT_RT5651_OVCD_TH_2000UA |
0489                     BYT_RT5651_OVCD_SF_0P75 |
0490                     BYT_RT5651_DMIC_EN |
0491                     BYT_RT5651_MCLK_EN |
0492                     BYT_RT5651_SSP0_AIF1),
0493     },
0494     {
0495         /* VIOS LTH17 */
0496         .callback = byt_rt5651_quirk_cb,
0497         .matches = {
0498             DMI_MATCH(DMI_SYS_VENDOR, "VIOS"),
0499             DMI_MATCH(DMI_PRODUCT_NAME, "LTH17"),
0500         },
0501         .driver_data = (void *)(BYT_RT5651_IN1_IN2_MAP |
0502                     BYT_RT5651_JD1_1 |
0503                     BYT_RT5651_OVCD_TH_2000UA |
0504                     BYT_RT5651_OVCD_SF_1P0 |
0505                     BYT_RT5651_MCLK_EN),
0506     },
0507     {
0508         /* Yours Y8W81 (and others using the same mainboard) */
0509         .callback = byt_rt5651_quirk_cb,
0510         .matches = {
0511             DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."),
0512             /* Partial match for all devs with a W86C mainboard */
0513             DMI_MATCH(DMI_BIOS_VERSION, ".F.W86C."),
0514         },
0515         .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
0516                     BYT_RT5651_IN2_MAP |
0517                     BYT_RT5651_SSP0_AIF1 |
0518                     BYT_RT5651_MONO_SPEAKER),
0519     },
0520     {}
0521 };
0522 
0523 /*
0524  * Note this MUST be called before snd_soc_register_card(), so that the props
0525  * are in place before the codec component driver's probe function parses them.
0526  */
0527 static int byt_rt5651_add_codec_device_props(struct device *i2c_dev,
0528                          struct byt_rt5651_private *priv)
0529 {
0530     struct property_entry props[MAX_NO_PROPS] = {};
0531     struct fwnode_handle *fwnode;
0532     int cnt = 0;
0533     int ret;
0534 
0535     props[cnt++] = PROPERTY_ENTRY_U32("realtek,jack-detect-source",
0536                 BYT_RT5651_JDSRC(byt_rt5651_quirk));
0537 
0538     props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-threshold-microamp",
0539                 BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
0540 
0541     props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-scale-factor",
0542                 BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
0543 
0544     if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
0545         props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,dmic-en");
0546 
0547     if (byt_rt5651_quirk & BYT_RT5651_JD_NOT_INV)
0548         props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,jack-detect-not-inverted");
0549 
0550     fwnode = fwnode_create_software_node(props, NULL);
0551     if (IS_ERR(fwnode)) {
0552         /* put_device(i2c_dev) is handled in caller */
0553         return PTR_ERR(fwnode);
0554     }
0555 
0556     ret = device_add_software_node(i2c_dev, to_software_node(fwnode));
0557 
0558     fwnode_handle_put(fwnode);
0559 
0560     return ret;
0561 }
0562 
0563 static int byt_rt5651_init(struct snd_soc_pcm_runtime *runtime)
0564 {
0565     struct snd_soc_card *card = runtime->card;
0566     struct snd_soc_component *codec = asoc_rtd_to_codec(runtime, 0)->component;
0567     struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
0568     const struct snd_soc_dapm_route *custom_map;
0569     int num_routes;
0570     int report;
0571     int ret;
0572 
0573     card->dapm.idle_bias_off = true;
0574 
0575     /* Start with RC clk for jack-detect (we disable MCLK below) */
0576     if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
0577         snd_soc_component_update_bits(codec, RT5651_GLB_CLK,
0578             RT5651_SCLK_SRC_MASK, RT5651_SCLK_SRC_RCCLK);
0579 
0580     switch (BYT_RT5651_MAP(byt_rt5651_quirk)) {
0581     case BYT_RT5651_IN1_MAP:
0582         custom_map = byt_rt5651_intmic_in1_map;
0583         num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_map);
0584         break;
0585     case BYT_RT5651_IN2_MAP:
0586         custom_map = byt_rt5651_intmic_in2_map;
0587         num_routes = ARRAY_SIZE(byt_rt5651_intmic_in2_map);
0588         break;
0589     case BYT_RT5651_IN1_IN2_MAP:
0590         custom_map = byt_rt5651_intmic_in1_in2_map;
0591         num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_in2_map);
0592         break;
0593     default:
0594         custom_map = byt_rt5651_intmic_dmic_map;
0595         num_routes = ARRAY_SIZE(byt_rt5651_intmic_dmic_map);
0596     }
0597     ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
0598     if (ret)
0599         return ret;
0600 
0601     if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) {
0602         ret = snd_soc_dapm_add_routes(&card->dapm,
0603                     byt_rt5651_ssp2_aif2_map,
0604                     ARRAY_SIZE(byt_rt5651_ssp2_aif2_map));
0605     } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) {
0606         ret = snd_soc_dapm_add_routes(&card->dapm,
0607                     byt_rt5651_ssp0_aif1_map,
0608                     ARRAY_SIZE(byt_rt5651_ssp0_aif1_map));
0609     } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2) {
0610         ret = snd_soc_dapm_add_routes(&card->dapm,
0611                     byt_rt5651_ssp0_aif2_map,
0612                     ARRAY_SIZE(byt_rt5651_ssp0_aif2_map));
0613     } else {
0614         ret = snd_soc_dapm_add_routes(&card->dapm,
0615                     byt_rt5651_ssp2_aif1_map,
0616                     ARRAY_SIZE(byt_rt5651_ssp2_aif1_map));
0617     }
0618     if (ret)
0619         return ret;
0620 
0621     ret = snd_soc_add_card_controls(card, byt_rt5651_controls,
0622                     ARRAY_SIZE(byt_rt5651_controls));
0623     if (ret) {
0624         dev_err(card->dev, "unable to add card controls\n");
0625         return ret;
0626     }
0627 
0628     /*
0629      * The firmware might enable the clock at boot (this information
0630      * may or may not be reflected in the enable clock register).
0631      * To change the rate we must disable the clock first to cover
0632      * these cases. Due to common clock framework restrictions that
0633      * do not allow to disable a clock that has not been enabled,
0634      * we need to enable the clock first.
0635      */
0636     ret = clk_prepare_enable(priv->mclk);
0637     if (!ret)
0638         clk_disable_unprepare(priv->mclk);
0639 
0640     if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
0641         ret = clk_set_rate(priv->mclk, 25000000);
0642     else
0643         ret = clk_set_rate(priv->mclk, 19200000);
0644 
0645     if (ret)
0646         dev_err(card->dev, "unable to set MCLK rate\n");
0647 
0648     report = 0;
0649     if (BYT_RT5651_JDSRC(byt_rt5651_quirk))
0650         report = SND_JACK_HEADSET | SND_JACK_BTN_0;
0651     else if (priv->hp_detect)
0652         report = SND_JACK_HEADSET;
0653 
0654     if (report) {
0655         ret = snd_soc_card_jack_new_pins(runtime->card, "Headset",
0656                          report, &priv->jack,
0657                          bytcr_jack_pins,
0658                          ARRAY_SIZE(bytcr_jack_pins));
0659         if (ret) {
0660             dev_err(runtime->dev, "jack creation failed %d\n", ret);
0661             return ret;
0662         }
0663 
0664         if (report & SND_JACK_BTN_0)
0665             snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
0666                      KEY_PLAYPAUSE);
0667 
0668         ret = snd_soc_component_set_jack(codec, &priv->jack,
0669                          priv->hp_detect);
0670         if (ret)
0671             return ret;
0672     }
0673 
0674     return 0;
0675 }
0676 
0677 static int byt_rt5651_codec_fixup(struct snd_soc_pcm_runtime *rtd,
0678                 struct snd_pcm_hw_params *params)
0679 {
0680     struct snd_interval *rate = hw_param_interval(params,
0681             SNDRV_PCM_HW_PARAM_RATE);
0682     struct snd_interval *channels = hw_param_interval(params,
0683                         SNDRV_PCM_HW_PARAM_CHANNELS);
0684     int ret, bits;
0685 
0686     /* The DSP will covert the FE rate to 48k, stereo */
0687     rate->min = rate->max = 48000;
0688     channels->min = channels->max = 2;
0689 
0690     if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
0691         (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
0692         /* set SSP0 to 16-bit */
0693         params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
0694         bits = 16;
0695     } else {
0696         /* set SSP2 to 24-bit */
0697         params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
0698         bits = 24;
0699     }
0700 
0701     /*
0702      * Default mode for SSP configuration is TDM 4 slot, override config
0703      * with explicit setting to I2S 2ch. The word length is set with
0704      * dai_set_tdm_slot() since there is no other API exposed
0705      */
0706     ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
0707                   SND_SOC_DAIFMT_I2S     |
0708                   SND_SOC_DAIFMT_NB_NF   |
0709                   SND_SOC_DAIFMT_BP_FP
0710                   );
0711 
0712     if (ret < 0) {
0713         dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
0714         return ret;
0715     }
0716 
0717     ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
0718     if (ret < 0) {
0719         dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
0720         return ret;
0721     }
0722 
0723     return 0;
0724 }
0725 
0726 static const unsigned int rates_48000[] = {
0727     48000,
0728 };
0729 
0730 static const struct snd_pcm_hw_constraint_list constraints_48000 = {
0731     .count = ARRAY_SIZE(rates_48000),
0732     .list  = rates_48000,
0733 };
0734 
0735 static int byt_rt5651_aif1_startup(struct snd_pcm_substream *substream)
0736 {
0737     return snd_pcm_hw_constraint_list(substream->runtime, 0,
0738             SNDRV_PCM_HW_PARAM_RATE,
0739             &constraints_48000);
0740 }
0741 
0742 static const struct snd_soc_ops byt_rt5651_aif1_ops = {
0743     .startup = byt_rt5651_aif1_startup,
0744 };
0745 
0746 static const struct snd_soc_ops byt_rt5651_be_ssp2_ops = {
0747     .hw_params = byt_rt5651_aif1_hw_params,
0748 };
0749 
0750 SND_SOC_DAILINK_DEF(dummy,
0751     DAILINK_COMP_ARRAY(COMP_DUMMY()));
0752 
0753 SND_SOC_DAILINK_DEF(media,
0754     DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
0755 
0756 SND_SOC_DAILINK_DEF(deepbuffer,
0757     DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
0758 
0759 SND_SOC_DAILINK_DEF(ssp2_port,
0760     DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
0761 SND_SOC_DAILINK_DEF(ssp2_codec,
0762     DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5651:00", "rt5651-aif1")));
0763 
0764 SND_SOC_DAILINK_DEF(platform,
0765     DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
0766 
0767 static struct snd_soc_dai_link byt_rt5651_dais[] = {
0768     [MERR_DPCM_AUDIO] = {
0769         .name = "Audio Port",
0770         .stream_name = "Audio",
0771         .nonatomic = true,
0772         .dynamic = 1,
0773         .dpcm_playback = 1,
0774         .dpcm_capture = 1,
0775         .ops = &byt_rt5651_aif1_ops,
0776         SND_SOC_DAILINK_REG(media, dummy, platform),
0777     },
0778     [MERR_DPCM_DEEP_BUFFER] = {
0779         .name = "Deep-Buffer Audio Port",
0780         .stream_name = "Deep-Buffer Audio",
0781         .nonatomic = true,
0782         .dynamic = 1,
0783         .dpcm_playback = 1,
0784         .ops = &byt_rt5651_aif1_ops,
0785         SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
0786     },
0787     /* CODEC<->CODEC link */
0788     /* back ends */
0789     {
0790         .name = "SSP2-Codec",
0791         .id = 0,
0792         .no_pcm = 1,
0793         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
0794                         | SND_SOC_DAIFMT_CBC_CFC,
0795         .be_hw_params_fixup = byt_rt5651_codec_fixup,
0796         .dpcm_playback = 1,
0797         .dpcm_capture = 1,
0798         .init = byt_rt5651_init,
0799         .ops = &byt_rt5651_be_ssp2_ops,
0800         SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
0801     },
0802 };
0803 
0804 /* SoC card */
0805 static char byt_rt5651_codec_name[SND_ACPI_I2C_ID_LEN];
0806 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
0807 static char byt_rt5651_long_name[50]; /* = "bytcr-rt5651-*-spk-*-mic[-swapped-hp]" */
0808 #endif
0809 static char byt_rt5651_components[50]; /* = "cfg-spk:* cfg-mic:*" */
0810 
0811 static int byt_rt5651_suspend(struct snd_soc_card *card)
0812 {
0813     struct snd_soc_component *component;
0814 
0815     if (!BYT_RT5651_JDSRC(byt_rt5651_quirk))
0816         return 0;
0817 
0818     for_each_card_components(card, component) {
0819         if (!strcmp(component->name, byt_rt5651_codec_name)) {
0820             dev_dbg(component->dev, "disabling jack detect before suspend\n");
0821             snd_soc_component_set_jack(component, NULL, NULL);
0822             break;
0823         }
0824     }
0825 
0826     return 0;
0827 }
0828 
0829 static int byt_rt5651_resume(struct snd_soc_card *card)
0830 {
0831     struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
0832     struct snd_soc_component *component;
0833 
0834     if (!BYT_RT5651_JDSRC(byt_rt5651_quirk))
0835         return 0;
0836 
0837     for_each_card_components(card, component) {
0838         if (!strcmp(component->name, byt_rt5651_codec_name)) {
0839             dev_dbg(component->dev, "re-enabling jack detect after resume\n");
0840             snd_soc_component_set_jack(component, &priv->jack,
0841                            priv->hp_detect);
0842             break;
0843         }
0844     }
0845 
0846     return 0;
0847 }
0848 
0849 /* use space before codec name to simplify card ID, and simplify driver name */
0850 #define SOF_CARD_NAME "bytcht rt5651" /* card name will be 'sof-bytcht rt5651' */
0851 #define SOF_DRIVER_NAME "SOF"
0852 
0853 #define CARD_NAME "bytcr-rt5651"
0854 #define DRIVER_NAME NULL /* card name will be used for driver name */
0855 
0856 static struct snd_soc_card byt_rt5651_card = {
0857     .name = CARD_NAME,
0858     .driver_name = DRIVER_NAME,
0859     .owner = THIS_MODULE,
0860     .dai_link = byt_rt5651_dais,
0861     .num_links = ARRAY_SIZE(byt_rt5651_dais),
0862     .dapm_widgets = byt_rt5651_widgets,
0863     .num_dapm_widgets = ARRAY_SIZE(byt_rt5651_widgets),
0864     .dapm_routes = byt_rt5651_audio_map,
0865     .num_dapm_routes = ARRAY_SIZE(byt_rt5651_audio_map),
0866     .fully_routed = true,
0867     .suspend_pre = byt_rt5651_suspend,
0868     .resume_post = byt_rt5651_resume,
0869 };
0870 
0871 static const struct acpi_gpio_params ext_amp_enable_gpios = { 0, 0, false };
0872 
0873 static const struct acpi_gpio_mapping cht_rt5651_gpios[] = {
0874     /*
0875      * Some boards have I2cSerialBusV2, GpioIo, GpioInt as ACPI resources,
0876      * other boards may  have I2cSerialBusV2, GpioInt, GpioIo instead.
0877      * We want the GpioIo one for the ext-amp-enable-gpio.
0878      */
0879     { "ext-amp-enable-gpios", &ext_amp_enable_gpios, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
0880     { },
0881 };
0882 
0883 struct acpi_chan_package {   /* ACPICA seems to require 64 bit integers */
0884     u64 aif_value;       /* 1: AIF1, 2: AIF2 */
0885     u64 mclock_value;    /* usually 25MHz (0x17d7940), ignored */
0886 };
0887 
0888 static int snd_byt_rt5651_mc_probe(struct platform_device *pdev)
0889 {
0890     struct device *dev = &pdev->dev;
0891     static const char * const mic_name[] = { "dmic", "in1", "in2", "in12" };
0892     struct snd_soc_acpi_mach *mach = dev_get_platdata(dev);
0893     struct byt_rt5651_private *priv;
0894     const char *platform_name;
0895     struct acpi_device *adev;
0896     struct device *codec_dev;
0897     bool sof_parent;
0898     bool is_bytcr = false;
0899     int ret_val = 0;
0900     int dai_index = 0;
0901     int i;
0902 
0903     priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0904     if (!priv)
0905         return -ENOMEM;
0906 
0907     /* register the soc card */
0908     byt_rt5651_card.dev = dev;
0909     snd_soc_card_set_drvdata(&byt_rt5651_card, priv);
0910 
0911     /* fix index of codec dai */
0912     for (i = 0; i < ARRAY_SIZE(byt_rt5651_dais); i++) {
0913         if (!strcmp(byt_rt5651_dais[i].codecs->name,
0914                 "i2c-10EC5651:00")) {
0915             dai_index = i;
0916             break;
0917         }
0918     }
0919 
0920     /* fixup codec name based on HID */
0921     adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
0922     if (adev) {
0923         snprintf(byt_rt5651_codec_name, sizeof(byt_rt5651_codec_name),
0924              "i2c-%s", acpi_dev_name(adev));
0925         put_device(&adev->dev);
0926         byt_rt5651_dais[dai_index].codecs->name = byt_rt5651_codec_name;
0927     } else {
0928         dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
0929         return -ENXIO;
0930     }
0931 
0932     codec_dev = acpi_get_first_physical_node(adev);
0933     if (!codec_dev)
0934         return -EPROBE_DEFER;
0935     priv->codec_dev = get_device(codec_dev);
0936 
0937     /*
0938      * swap SSP0 if bytcr is detected
0939      * (will be overridden if DMI quirk is detected)
0940      */
0941     if (soc_intel_is_byt()) {
0942         if (mach->mach_params.acpi_ipc_irq_index == 0)
0943             is_bytcr = true;
0944     }
0945 
0946     if (is_bytcr) {
0947         /*
0948          * Baytrail CR platforms may have CHAN package in BIOS, try
0949          * to find relevant routing quirk based as done on Windows
0950          * platforms. We have to read the information directly from the
0951          * BIOS, at this stage the card is not created and the links
0952          * with the codec driver/pdata are non-existent
0953          */
0954 
0955         struct acpi_chan_package chan_package = { 0 };
0956 
0957         /* format specified: 2 64-bit integers */
0958         struct acpi_buffer format = {sizeof("NN"), "NN"};
0959         struct acpi_buffer state = {0, NULL};
0960         struct snd_soc_acpi_package_context pkg_ctx;
0961         bool pkg_found = false;
0962 
0963         state.length = sizeof(chan_package);
0964         state.pointer = &chan_package;
0965 
0966         pkg_ctx.name = "CHAN";
0967         pkg_ctx.length = 2;
0968         pkg_ctx.format = &format;
0969         pkg_ctx.state = &state;
0970         pkg_ctx.data_valid = false;
0971 
0972         pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
0973                                    &pkg_ctx);
0974         if (pkg_found) {
0975             if (chan_package.aif_value == 1) {
0976                 dev_info(dev, "BIOS Routing: AIF1 connected\n");
0977                 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF1;
0978             } else  if (chan_package.aif_value == 2) {
0979                 dev_info(dev, "BIOS Routing: AIF2 connected\n");
0980                 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
0981             } else {
0982                 dev_info(dev, "BIOS Routing isn't valid, ignored\n");
0983                 pkg_found = false;
0984             }
0985         }
0986 
0987         if (!pkg_found) {
0988             /* no BIOS indications, assume SSP0-AIF2 connection */
0989             byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
0990         }
0991     }
0992 
0993     /* check quirks before creating card */
0994     dmi_check_system(byt_rt5651_quirk_table);
0995 
0996     if (quirk_override != -1) {
0997         dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
0998              byt_rt5651_quirk, quirk_override);
0999         byt_rt5651_quirk = quirk_override;
1000     }
1001 
1002     /* Must be called before register_card, also see declaration comment. */
1003     ret_val = byt_rt5651_add_codec_device_props(codec_dev, priv);
1004     if (ret_val)
1005         goto err_device;
1006 
1007     /* Cherry Trail devices use an external amplifier enable gpio */
1008     if (soc_intel_is_cht() && !byt_rt5651_gpios)
1009         byt_rt5651_gpios = cht_rt5651_gpios;
1010 
1011     if (byt_rt5651_gpios) {
1012         devm_acpi_dev_add_driver_gpios(codec_dev, byt_rt5651_gpios);
1013         priv->ext_amp_gpio = devm_fwnode_gpiod_get(dev, codec_dev->fwnode,
1014                                "ext-amp-enable",
1015                                GPIOD_OUT_LOW,
1016                                "speaker-amp");
1017         if (IS_ERR(priv->ext_amp_gpio)) {
1018             ret_val = PTR_ERR(priv->ext_amp_gpio);
1019             switch (ret_val) {
1020             case -ENOENT:
1021                 priv->ext_amp_gpio = NULL;
1022                 break;
1023             default:
1024                 dev_err(dev, "Failed to get ext-amp-enable GPIO: %d\n", ret_val);
1025                 fallthrough;
1026             case -EPROBE_DEFER:
1027                 goto err;
1028             }
1029         }
1030         priv->hp_detect = devm_fwnode_gpiod_get(dev, codec_dev->fwnode,
1031                             "hp-detect",
1032                             GPIOD_IN,
1033                             "hp-detect");
1034         if (IS_ERR(priv->hp_detect)) {
1035             ret_val = PTR_ERR(priv->hp_detect);
1036             switch (ret_val) {
1037             case -ENOENT:
1038                 priv->hp_detect = NULL;
1039                 break;
1040             default:
1041                 dev_err(dev, "Failed to get hp-detect GPIO: %d\n", ret_val);
1042                 fallthrough;
1043             case -EPROBE_DEFER:
1044                 goto err;
1045             }
1046         }
1047     }
1048 
1049     log_quirks(dev);
1050 
1051     if ((byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) ||
1052         (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2))
1053         byt_rt5651_dais[dai_index].codecs->dai_name = "rt5651-aif2";
1054 
1055     if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
1056         (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2))
1057         byt_rt5651_dais[dai_index].cpus->dai_name = "ssp0-port";
1058 
1059     if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
1060         priv->mclk = devm_clk_get_optional(dev, "pmc_plt_clk_3");
1061         if (IS_ERR(priv->mclk)) {
1062             ret_val = dev_err_probe(dev, PTR_ERR(priv->mclk),
1063                         "Failed to get MCLK from pmc_plt_clk_3\n");
1064             goto err;
1065         }
1066         /*
1067          * Fall back to bit clock usage when clock is not
1068          * available likely due to missing dependencies.
1069          */
1070         if (!priv->mclk)
1071             byt_rt5651_quirk &= ~BYT_RT5651_MCLK_EN;
1072     }
1073 
1074     snprintf(byt_rt5651_components, sizeof(byt_rt5651_components),
1075          "cfg-spk:%s cfg-mic:%s%s",
1076          (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER) ? "1" : "2",
1077          mic_name[BYT_RT5651_MAP(byt_rt5651_quirk)],
1078          (byt_rt5651_quirk & BYT_RT5651_HP_LR_SWAPPED) ?
1079             " cfg-hp:lrswap" : "");
1080     byt_rt5651_card.components = byt_rt5651_components;
1081 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1082     snprintf(byt_rt5651_long_name, sizeof(byt_rt5651_long_name),
1083          "bytcr-rt5651-%s-spk-%s-mic%s",
1084          (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER) ?
1085             "mono" : "stereo",
1086          mic_name[BYT_RT5651_MAP(byt_rt5651_quirk)],
1087          (byt_rt5651_quirk & BYT_RT5651_HP_LR_SWAPPED) ?
1088             "-hp-swapped" : "");
1089     byt_rt5651_card.long_name = byt_rt5651_long_name;
1090 #endif
1091 
1092     /* override platform name, if required */
1093     platform_name = mach->mach_params.platform;
1094 
1095     ret_val = snd_soc_fixup_dai_links_platform_name(&byt_rt5651_card,
1096                             platform_name);
1097     if (ret_val)
1098         goto err;
1099 
1100     sof_parent = snd_soc_acpi_sof_parent(dev);
1101 
1102     /* set card and driver name */
1103     if (sof_parent) {
1104         byt_rt5651_card.name = SOF_CARD_NAME;
1105         byt_rt5651_card.driver_name = SOF_DRIVER_NAME;
1106     } else {
1107         byt_rt5651_card.name = CARD_NAME;
1108         byt_rt5651_card.driver_name = DRIVER_NAME;
1109     }
1110 
1111     /* set pm ops */
1112     if (sof_parent)
1113         dev->driver->pm = &snd_soc_pm_ops;
1114 
1115     ret_val = devm_snd_soc_register_card(dev, &byt_rt5651_card);
1116     if (ret_val) {
1117         dev_err(dev, "devm_snd_soc_register_card failed %d\n", ret_val);
1118         goto err;
1119     }
1120     platform_set_drvdata(pdev, &byt_rt5651_card);
1121     return ret_val;
1122 
1123 err:
1124     device_remove_software_node(priv->codec_dev);
1125 err_device:
1126     put_device(priv->codec_dev);
1127     return ret_val;
1128 }
1129 
1130 static int snd_byt_rt5651_mc_remove(struct platform_device *pdev)
1131 {
1132     struct snd_soc_card *card = platform_get_drvdata(pdev);
1133     struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
1134 
1135     device_remove_software_node(priv->codec_dev);
1136     put_device(priv->codec_dev);
1137     return 0;
1138 }
1139 
1140 static struct platform_driver snd_byt_rt5651_mc_driver = {
1141     .driver = {
1142         .name = "bytcr_rt5651",
1143     },
1144     .probe = snd_byt_rt5651_mc_probe,
1145     .remove = snd_byt_rt5651_mc_remove,
1146 };
1147 
1148 module_platform_driver(snd_byt_rt5651_mc_driver);
1149 
1150 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver for RT5651");
1151 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
1152 MODULE_LICENSE("GPL v2");
1153 MODULE_ALIAS("platform:bytcr_rt5651");