Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 // Copyright(c) 2021 Intel Corporation.
0003 
0004 /*
0005  * Intel SOF Machine Driver with es8336 Codec
0006  */
0007 
0008 #include <linux/device.h>
0009 #include <linux/dmi.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/gpio/machine.h>
0012 #include <linux/i2c.h>
0013 #include <linux/input.h>
0014 #include <linux/module.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/slab.h>
0017 #include <sound/jack.h>
0018 #include <sound/pcm.h>
0019 #include <sound/pcm_params.h>
0020 #include <sound/soc.h>
0021 #include <sound/soc-acpi.h>
0022 #include "hda_dsp_common.h"
0023 
0024 /* jd-inv + terminating entry */
0025 #define MAX_NO_PROPS 2
0026 
0027 #define SOF_ES8336_SSP_CODEC(quirk)     ((quirk) & GENMASK(3, 0))
0028 #define SOF_ES8336_SSP_CODEC_MASK       (GENMASK(3, 0))
0029 
0030 #define SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK  BIT(4)
0031 
0032 /* HDMI capture*/
0033 #define SOF_SSP_HDMI_CAPTURE_PRESENT        BIT(14)
0034 #define SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT        15
0035 #define SOF_NO_OF_HDMI_CAPTURE_SSP_MASK     (GENMASK(16, 15))
0036 #define SOF_NO_OF_HDMI_CAPTURE_SSP(quirk)   \
0037     (((quirk) << SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT) & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK)
0038 
0039 #define SOF_HDMI_CAPTURE_1_SSP_SHIFT        7
0040 #define SOF_HDMI_CAPTURE_1_SSP_MASK     (GENMASK(9, 7))
0041 #define SOF_HDMI_CAPTURE_1_SSP(quirk)   \
0042     (((quirk) << SOF_HDMI_CAPTURE_1_SSP_SHIFT) & SOF_HDMI_CAPTURE_1_SSP_MASK)
0043 
0044 #define SOF_HDMI_CAPTURE_2_SSP_SHIFT        10
0045 #define SOF_HDMI_CAPTURE_2_SSP_MASK     (GENMASK(12, 10))
0046 #define SOF_HDMI_CAPTURE_2_SSP(quirk)   \
0047     (((quirk) << SOF_HDMI_CAPTURE_2_SSP_SHIFT) & SOF_HDMI_CAPTURE_2_SSP_MASK)
0048 
0049 #define SOF_ES8336_ENABLE_DMIC          BIT(5)
0050 #define SOF_ES8336_JD_INVERTED          BIT(6)
0051 #define SOF_ES8336_HEADPHONE_GPIO       BIT(7)
0052 #define SOC_ES8336_HEADSET_MIC1         BIT(8)
0053 
0054 static unsigned long quirk;
0055 
0056 static int quirk_override = -1;
0057 module_param_named(quirk, quirk_override, int, 0444);
0058 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
0059 
0060 struct sof_es8336_private {
0061     struct device *codec_dev;
0062     struct gpio_desc *gpio_speakers, *gpio_headphone;
0063     struct snd_soc_jack jack;
0064     struct list_head hdmi_pcm_list;
0065     bool speaker_en;
0066 };
0067 
0068 struct sof_hdmi_pcm {
0069     struct list_head head;
0070     struct snd_soc_dai *codec_dai;
0071     int device;
0072 };
0073 
0074 static const struct acpi_gpio_params enable_gpio0 = { 0, 0, true };
0075 static const struct acpi_gpio_params enable_gpio1 = { 1, 0, true };
0076 
0077 static const struct acpi_gpio_mapping acpi_speakers_enable_gpio0[] = {
0078     { "speakers-enable-gpios", &enable_gpio0, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
0079     { }
0080 };
0081 
0082 static const struct acpi_gpio_mapping acpi_speakers_enable_gpio1[] = {
0083     { "speakers-enable-gpios", &enable_gpio1, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
0084 };
0085 
0086 static const struct acpi_gpio_mapping acpi_enable_both_gpios[] = {
0087     { "speakers-enable-gpios", &enable_gpio0, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
0088     { "headphone-enable-gpios", &enable_gpio1, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
0089     { }
0090 };
0091 
0092 static const struct acpi_gpio_mapping acpi_enable_both_gpios_rev_order[] = {
0093     { "speakers-enable-gpios", &enable_gpio1, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
0094     { "headphone-enable-gpios", &enable_gpio0, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
0095     { }
0096 };
0097 
0098 static void log_quirks(struct device *dev)
0099 {
0100     dev_info(dev, "quirk mask %#lx\n", quirk);
0101     dev_info(dev, "quirk SSP%ld\n",  SOF_ES8336_SSP_CODEC(quirk));
0102     if (quirk & SOF_ES8336_ENABLE_DMIC)
0103         dev_info(dev, "quirk DMIC enabled\n");
0104     if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK)
0105         dev_info(dev, "Speakers GPIO1 quirk enabled\n");
0106     if (quirk & SOF_ES8336_HEADPHONE_GPIO)
0107         dev_info(dev, "quirk headphone GPIO enabled\n");
0108     if (quirk & SOF_ES8336_JD_INVERTED)
0109         dev_info(dev, "quirk JD inverted enabled\n");
0110     if (quirk & SOC_ES8336_HEADSET_MIC1)
0111         dev_info(dev, "quirk headset at mic1 port enabled\n");
0112 }
0113 
0114 static int sof_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
0115                       struct snd_kcontrol *kcontrol, int event)
0116 {
0117     struct snd_soc_card *card = w->dapm->card;
0118     struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
0119 
0120     if (priv->speaker_en == !SND_SOC_DAPM_EVENT_ON(event))
0121         return 0;
0122 
0123     priv->speaker_en = !SND_SOC_DAPM_EVENT_ON(event);
0124 
0125     if (SND_SOC_DAPM_EVENT_ON(event))
0126         msleep(70);
0127 
0128     gpiod_set_value_cansleep(priv->gpio_speakers, priv->speaker_en);
0129 
0130     if (!(quirk & SOF_ES8336_HEADPHONE_GPIO))
0131         return 0;
0132 
0133     if (SND_SOC_DAPM_EVENT_ON(event))
0134         msleep(70);
0135 
0136     gpiod_set_value_cansleep(priv->gpio_headphone, priv->speaker_en);
0137 
0138     return 0;
0139 }
0140 
0141 static const struct snd_soc_dapm_widget sof_es8316_widgets[] = {
0142     SND_SOC_DAPM_SPK("Speaker", NULL),
0143     SND_SOC_DAPM_HP("Headphone", NULL),
0144     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0145     SND_SOC_DAPM_MIC("Internal Mic", NULL),
0146 
0147     SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
0148                 sof_es8316_speaker_power_event,
0149                 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
0150 };
0151 
0152 static const struct snd_soc_dapm_widget dmic_widgets[] = {
0153     SND_SOC_DAPM_MIC("SoC DMIC", NULL),
0154 };
0155 
0156 static const struct snd_soc_dapm_route sof_es8316_audio_map[] = {
0157     {"Headphone", NULL, "HPOL"},
0158     {"Headphone", NULL, "HPOR"},
0159 
0160     /*
0161      * There is no separate speaker output instead the speakers are muxed to
0162      * the HP outputs. The mux is controlled Speaker and/or headphone switch.
0163      */
0164     {"Speaker", NULL, "HPOL"},
0165     {"Speaker", NULL, "HPOR"},
0166     {"Speaker", NULL, "Speaker Power"},
0167 };
0168 
0169 static const struct snd_soc_dapm_route sof_es8316_headset_mic2_map[] = {
0170     {"MIC1", NULL, "Internal Mic"},
0171     {"MIC2", NULL, "Headset Mic"},
0172 };
0173 
0174 static const struct snd_soc_dapm_route sof_es8316_headset_mic1_map[] = {
0175     {"MIC2", NULL, "Internal Mic"},
0176     {"MIC1", NULL, "Headset Mic"},
0177 };
0178 
0179 static const struct snd_soc_dapm_route dmic_map[] = {
0180     /* digital mics */
0181     {"DMic", NULL, "SoC DMIC"},
0182 };
0183 
0184 static const struct snd_kcontrol_new sof_es8316_controls[] = {
0185     SOC_DAPM_PIN_SWITCH("Speaker"),
0186     SOC_DAPM_PIN_SWITCH("Headphone"),
0187     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0188     SOC_DAPM_PIN_SWITCH("Internal Mic"),
0189 };
0190 
0191 static struct snd_soc_jack_pin sof_es8316_jack_pins[] = {
0192     {
0193         .pin    = "Headphone",
0194         .mask   = SND_JACK_HEADPHONE,
0195     },
0196     {
0197         .pin    = "Headset Mic",
0198         .mask   = SND_JACK_MICROPHONE,
0199     },
0200 };
0201 
0202 static int dmic_init(struct snd_soc_pcm_runtime *runtime)
0203 {
0204     struct snd_soc_card *card = runtime->card;
0205     int ret;
0206 
0207     ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
0208                     ARRAY_SIZE(dmic_widgets));
0209     if (ret) {
0210         dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
0211         return ret;
0212     }
0213 
0214     ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
0215                       ARRAY_SIZE(dmic_map));
0216     if (ret)
0217         dev_err(card->dev, "DMic map addition failed: %d\n", ret);
0218 
0219     return ret;
0220 }
0221 
0222 static int sof_hdmi_init(struct snd_soc_pcm_runtime *runtime)
0223 {
0224     struct sof_es8336_private *priv = snd_soc_card_get_drvdata(runtime->card);
0225     struct snd_soc_dai *dai = asoc_rtd_to_codec(runtime, 0);
0226     struct sof_hdmi_pcm *pcm;
0227 
0228     pcm = devm_kzalloc(runtime->card->dev, sizeof(*pcm), GFP_KERNEL);
0229     if (!pcm)
0230         return -ENOMEM;
0231 
0232     /* dai_link id is 1:1 mapped to the PCM device */
0233     pcm->device = runtime->dai_link->id;
0234     pcm->codec_dai = dai;
0235 
0236     list_add_tail(&pcm->head, &priv->hdmi_pcm_list);
0237 
0238     return 0;
0239 }
0240 
0241 static int sof_es8316_init(struct snd_soc_pcm_runtime *runtime)
0242 {
0243     struct snd_soc_component *codec = asoc_rtd_to_codec(runtime, 0)->component;
0244     struct snd_soc_card *card = runtime->card;
0245     struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
0246     const struct snd_soc_dapm_route *custom_map;
0247     int num_routes;
0248     int ret;
0249 
0250     card->dapm.idle_bias_off = true;
0251 
0252     if (quirk & SOC_ES8336_HEADSET_MIC1) {
0253         custom_map = sof_es8316_headset_mic1_map;
0254         num_routes = ARRAY_SIZE(sof_es8316_headset_mic1_map);
0255     } else {
0256         custom_map = sof_es8316_headset_mic2_map;
0257         num_routes = ARRAY_SIZE(sof_es8316_headset_mic2_map);
0258     }
0259 
0260     ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
0261     if (ret)
0262         return ret;
0263 
0264     ret = snd_soc_card_jack_new_pins(card, "Headset",
0265                      SND_JACK_HEADSET | SND_JACK_BTN_0,
0266                      &priv->jack, sof_es8316_jack_pins,
0267                      ARRAY_SIZE(sof_es8316_jack_pins));
0268     if (ret) {
0269         dev_err(card->dev, "jack creation failed %d\n", ret);
0270         return ret;
0271     }
0272 
0273     snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
0274 
0275     snd_soc_component_set_jack(codec, &priv->jack, NULL);
0276 
0277     return 0;
0278 }
0279 
0280 static void sof_es8316_exit(struct snd_soc_pcm_runtime *rtd)
0281 {
0282     struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
0283 
0284     snd_soc_component_set_jack(component, NULL, NULL);
0285 }
0286 
0287 static int sof_es8336_quirk_cb(const struct dmi_system_id *id)
0288 {
0289     quirk = (unsigned long)id->driver_data;
0290 
0291     return 1;
0292 }
0293 
0294 /*
0295  * this table should only be used to add GPIO or jack-detection quirks
0296  * that cannot be detected from ACPI tables. The SSP and DMIC
0297  * information are providing by the platform driver and are aligned
0298  * with the topology used.
0299  *
0300  * If the GPIO support is missing, the quirk parameter can be used to
0301  * enable speakers. In that case it's recommended to keep the SSP and DMIC
0302  * information consistent, overriding the SSP and DMIC can only be done
0303  * if the topology file is modified as well.
0304  */
0305 static const struct dmi_system_id sof_es8336_quirk_table[] = {
0306     {
0307         .callback = sof_es8336_quirk_cb,
0308         .matches = {
0309             DMI_MATCH(DMI_SYS_VENDOR, "IP3 tech"),
0310             DMI_MATCH(DMI_BOARD_NAME, "WN1"),
0311         },
0312         .driver_data = (void *)(SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK)
0313     },
0314     {
0315         .callback = sof_es8336_quirk_cb,
0316         .matches = {
0317             DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
0318             DMI_MATCH(DMI_BOARD_NAME, "BOHB-WAX9-PCB-B2"),
0319         },
0320         .driver_data = (void *)(SOF_ES8336_HEADPHONE_GPIO |
0321                     SOC_ES8336_HEADSET_MIC1)
0322     },
0323     {}
0324 };
0325 
0326 static int sof_es8336_hw_params(struct snd_pcm_substream *substream,
0327                 struct snd_pcm_hw_params *params)
0328 {
0329     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0330     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0331     const int sysclk = 19200000;
0332     int ret;
0333 
0334     ret = snd_soc_dai_set_sysclk(codec_dai, 1, sysclk, SND_SOC_CLOCK_OUT);
0335     if (ret < 0) {
0336         dev_err(rtd->dev, "%s, Failed to set ES8336 SYSCLK: %d\n",
0337             __func__, ret);
0338         return ret;
0339     }
0340 
0341     return 0;
0342 }
0343 
0344 /* machine stream operations */
0345 static struct snd_soc_ops sof_es8336_ops = {
0346     .hw_params = sof_es8336_hw_params,
0347 };
0348 
0349 static struct snd_soc_dai_link_component platform_component[] = {
0350     {
0351         /* name might be overridden during probe */
0352         .name = "0000:00:1f.3"
0353     }
0354 };
0355 
0356 SND_SOC_DAILINK_DEF(es8336_codec,
0357     DAILINK_COMP_ARRAY(COMP_CODEC("i2c-ESSX8336:00", "ES8316 HiFi")));
0358 
0359 static struct snd_soc_dai_link_component dmic_component[] = {
0360     {
0361         .name = "dmic-codec",
0362         .dai_name = "dmic-hifi",
0363     }
0364 };
0365 
0366 static struct snd_soc_dai_link_component dummy_component[] = {
0367     {
0368         .name = "snd-soc-dummy",
0369         .dai_name = "snd-soc-dummy-dai",
0370     }
0371 };
0372 
0373 static int sof_es8336_late_probe(struct snd_soc_card *card)
0374 {
0375     struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
0376     struct sof_hdmi_pcm *pcm;
0377 
0378     if (list_empty(&priv->hdmi_pcm_list))
0379         return -ENOENT;
0380 
0381     pcm = list_first_entry(&priv->hdmi_pcm_list, struct sof_hdmi_pcm, head);
0382 
0383     return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
0384 }
0385 
0386 /* SoC card */
0387 static struct snd_soc_card sof_es8336_card = {
0388     .name = "essx8336", /* sof- prefix added automatically */
0389     .owner = THIS_MODULE,
0390     .dapm_widgets = sof_es8316_widgets,
0391     .num_dapm_widgets = ARRAY_SIZE(sof_es8316_widgets),
0392     .dapm_routes = sof_es8316_audio_map,
0393     .num_dapm_routes = ARRAY_SIZE(sof_es8316_audio_map),
0394     .controls = sof_es8316_controls,
0395     .num_controls = ARRAY_SIZE(sof_es8316_controls),
0396     .fully_routed = true,
0397     .late_probe = sof_es8336_late_probe,
0398     .num_links = 1,
0399 };
0400 
0401 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
0402                               int ssp_codec,
0403                               int dmic_be_num,
0404                               int hdmi_num)
0405 {
0406     struct snd_soc_dai_link_component *cpus;
0407     struct snd_soc_dai_link *links;
0408     struct snd_soc_dai_link_component *idisp_components;
0409     int hdmi_id_offset = 0;
0410     int id = 0;
0411     int i;
0412 
0413     links = devm_kcalloc(dev, sof_es8336_card.num_links,
0414                  sizeof(struct snd_soc_dai_link), GFP_KERNEL);
0415     cpus = devm_kcalloc(dev, sof_es8336_card.num_links,
0416                 sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
0417     if (!links || !cpus)
0418         goto devm_err;
0419 
0420     /* codec SSP */
0421     links[id].name = devm_kasprintf(dev, GFP_KERNEL,
0422                     "SSP%d-Codec", ssp_codec);
0423     if (!links[id].name)
0424         goto devm_err;
0425 
0426     links[id].id = id;
0427     links[id].codecs = es8336_codec;
0428     links[id].num_codecs = ARRAY_SIZE(es8336_codec);
0429     links[id].platforms = platform_component;
0430     links[id].num_platforms = ARRAY_SIZE(platform_component);
0431     links[id].init = sof_es8316_init;
0432     links[id].exit = sof_es8316_exit;
0433     links[id].ops = &sof_es8336_ops;
0434     links[id].nonatomic = true;
0435     links[id].dpcm_playback = 1;
0436     links[id].dpcm_capture = 1;
0437     links[id].no_pcm = 1;
0438     links[id].cpus = &cpus[id];
0439     links[id].num_cpus = 1;
0440 
0441     links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
0442                           "SSP%d Pin",
0443                           ssp_codec);
0444     if (!links[id].cpus->dai_name)
0445         goto devm_err;
0446 
0447     id++;
0448 
0449     /* dmic */
0450     if (dmic_be_num > 0) {
0451         /* at least we have dmic01 */
0452         links[id].name = "dmic01";
0453         links[id].cpus = &cpus[id];
0454         links[id].cpus->dai_name = "DMIC01 Pin";
0455         links[id].init = dmic_init;
0456         if (dmic_be_num > 1) {
0457             /* set up 2 BE links at most */
0458             links[id + 1].name = "dmic16k";
0459             links[id + 1].cpus = &cpus[id + 1];
0460             links[id + 1].cpus->dai_name = "DMIC16k Pin";
0461             dmic_be_num = 2;
0462         }
0463     } else {
0464         /* HDMI dai link starts at 3 according to current topology settings */
0465         hdmi_id_offset = 2;
0466     }
0467 
0468     for (i = 0; i < dmic_be_num; i++) {
0469         links[id].id = id;
0470         links[id].num_cpus = 1;
0471         links[id].codecs = dmic_component;
0472         links[id].num_codecs = ARRAY_SIZE(dmic_component);
0473         links[id].platforms = platform_component;
0474         links[id].num_platforms = ARRAY_SIZE(platform_component);
0475         links[id].ignore_suspend = 1;
0476         links[id].dpcm_capture = 1;
0477         links[id].no_pcm = 1;
0478 
0479         id++;
0480     }
0481 
0482     /* HDMI */
0483     if (hdmi_num > 0) {
0484         idisp_components = devm_kzalloc(dev,
0485                         sizeof(struct snd_soc_dai_link_component) *
0486                         hdmi_num, GFP_KERNEL);
0487         if (!idisp_components)
0488             goto devm_err;
0489     }
0490 
0491     for (i = 1; i <= hdmi_num; i++) {
0492         links[id].name = devm_kasprintf(dev, GFP_KERNEL,
0493                         "iDisp%d", i);
0494         if (!links[id].name)
0495             goto devm_err;
0496 
0497         links[id].id = id + hdmi_id_offset;
0498         links[id].cpus = &cpus[id];
0499         links[id].num_cpus = 1;
0500         links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
0501                               "iDisp%d Pin", i);
0502         if (!links[id].cpus->dai_name)
0503             goto devm_err;
0504 
0505         idisp_components[i - 1].name = "ehdaudio0D2";
0506         idisp_components[i - 1].dai_name = devm_kasprintf(dev,
0507                                   GFP_KERNEL,
0508                                   "intel-hdmi-hifi%d",
0509                                   i);
0510         if (!idisp_components[i - 1].dai_name)
0511             goto devm_err;
0512 
0513         links[id].codecs = &idisp_components[i - 1];
0514         links[id].num_codecs = 1;
0515         links[id].platforms = platform_component;
0516         links[id].num_platforms = ARRAY_SIZE(platform_component);
0517         links[id].init = sof_hdmi_init;
0518         links[id].dpcm_playback = 1;
0519         links[id].no_pcm = 1;
0520 
0521         id++;
0522     }
0523 
0524     /* HDMI-In SSP */
0525     if (quirk & SOF_SSP_HDMI_CAPTURE_PRESENT) {
0526         int num_of_hdmi_ssp = (quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
0527                 SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
0528 
0529         for (i = 1; i <= num_of_hdmi_ssp; i++) {
0530             int port = (i == 1 ? (quirk & SOF_HDMI_CAPTURE_1_SSP_MASK) >>
0531                         SOF_HDMI_CAPTURE_1_SSP_SHIFT :
0532                         (quirk & SOF_HDMI_CAPTURE_2_SSP_MASK) >>
0533                         SOF_HDMI_CAPTURE_2_SSP_SHIFT);
0534 
0535             links[id].cpus = &cpus[id];
0536             links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
0537                                   "SSP%d Pin", port);
0538             if (!links[id].cpus->dai_name)
0539                 return NULL;
0540             links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-HDMI", port);
0541             if (!links[id].name)
0542                 return NULL;
0543             links[id].id = id + hdmi_id_offset;
0544             links[id].codecs = dummy_component;
0545             links[id].num_codecs = ARRAY_SIZE(dummy_component);
0546             links[id].platforms = platform_component;
0547             links[id].num_platforms = ARRAY_SIZE(platform_component);
0548             links[id].dpcm_capture = 1;
0549             links[id].no_pcm = 1;
0550             links[id].num_cpus = 1;
0551             id++;
0552         }
0553     }
0554 
0555     return links;
0556 
0557 devm_err:
0558     return NULL;
0559 }
0560 
0561 static char soc_components[30];
0562 
0563  /* i2c-<HID>:00 with HID being 8 chars */
0564 static char codec_name[SND_ACPI_I2C_ID_LEN];
0565 
0566 static int sof_es8336_probe(struct platform_device *pdev)
0567 {
0568     struct device *dev = &pdev->dev;
0569     struct snd_soc_card *card;
0570     struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
0571     struct property_entry props[MAX_NO_PROPS] = {};
0572     struct sof_es8336_private *priv;
0573     struct fwnode_handle *fwnode;
0574     struct acpi_device *adev;
0575     struct snd_soc_dai_link *dai_links;
0576     struct device *codec_dev;
0577     const struct acpi_gpio_mapping *gpio_mapping;
0578     unsigned int cnt = 0;
0579     int dmic_be_num = 0;
0580     int hdmi_num = 3;
0581     int ret;
0582 
0583     priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0584     if (!priv)
0585         return -ENOMEM;
0586 
0587     card = &sof_es8336_card;
0588     card->dev = dev;
0589 
0590     if (pdev->id_entry && pdev->id_entry->driver_data)
0591         quirk = (unsigned long)pdev->id_entry->driver_data;
0592 
0593     /* check GPIO DMI quirks */
0594     dmi_check_system(sof_es8336_quirk_table);
0595 
0596     /* Use NHLT configuration only for Non-HDMI capture use case.
0597      * Because more than one SSP will be enabled for HDMI capture hence wrong codec
0598      * SSP will be set.
0599      */
0600     if (mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER) {
0601         if (!mach->mach_params.i2s_link_mask) {
0602             dev_warn(dev, "No I2S link information provided, using SSP0. This may need to be modified with the quirk module parameter\n");
0603         } else {
0604             /*
0605              * Set configuration based on platform NHLT.
0606              * In this machine driver, we can only support one SSP for the
0607              * ES8336 link.
0608              * In some cases multiple SSPs can be reported by NHLT, starting MSB-first
0609              * seems to pick the right connection.
0610              */
0611             unsigned long ssp;
0612 
0613             /* fls returns 1-based results, SSPs indices are 0-based */
0614             ssp = fls(mach->mach_params.i2s_link_mask) - 1;
0615 
0616             quirk |= ssp;
0617         }
0618     }
0619 
0620     if (mach->mach_params.dmic_num)
0621         quirk |= SOF_ES8336_ENABLE_DMIC;
0622 
0623     if (quirk_override != -1) {
0624         dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
0625              quirk, quirk_override);
0626         quirk = quirk_override;
0627     }
0628     log_quirks(dev);
0629 
0630     if (quirk & SOF_ES8336_ENABLE_DMIC)
0631         dmic_be_num = 2;
0632 
0633     /* compute number of dai links */
0634     sof_es8336_card.num_links = 1 + dmic_be_num + hdmi_num;
0635 
0636     if (quirk & SOF_SSP_HDMI_CAPTURE_PRESENT)
0637         sof_es8336_card.num_links += (quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
0638                 SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
0639 
0640     dai_links = sof_card_dai_links_create(dev,
0641                           SOF_ES8336_SSP_CODEC(quirk),
0642                           dmic_be_num, hdmi_num);
0643     if (!dai_links)
0644         return -ENOMEM;
0645 
0646     sof_es8336_card.dai_link = dai_links;
0647 
0648     /* fixup codec name based on HID */
0649     adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
0650     if (adev) {
0651         snprintf(codec_name, sizeof(codec_name),
0652              "i2c-%s", acpi_dev_name(adev));
0653         put_device(&adev->dev);
0654         dai_links[0].codecs->name = codec_name;
0655 
0656         /* also fixup codec dai name if relevant */
0657         if (!strncmp(mach->id, "ESSX8326", SND_ACPI_I2C_ID_LEN))
0658             dai_links[0].codecs->dai_name = "ES8326 HiFi";
0659     } else {
0660         dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
0661         return -ENXIO;
0662     }
0663 
0664     ret = snd_soc_fixup_dai_links_platform_name(&sof_es8336_card,
0665                             mach->mach_params.platform);
0666     if (ret)
0667         return ret;
0668 
0669     codec_dev = acpi_get_first_physical_node(adev);
0670     if (!codec_dev)
0671         return -EPROBE_DEFER;
0672     priv->codec_dev = get_device(codec_dev);
0673 
0674     if (quirk & SOF_ES8336_JD_INVERTED)
0675         props[cnt++] = PROPERTY_ENTRY_BOOL("everest,jack-detect-inverted");
0676 
0677     if (cnt) {
0678         fwnode = fwnode_create_software_node(props, NULL);
0679         if (IS_ERR(fwnode)) {
0680             put_device(codec_dev);
0681             return PTR_ERR(fwnode);
0682         }
0683 
0684         ret = device_add_software_node(codec_dev, to_software_node(fwnode));
0685 
0686         fwnode_handle_put(fwnode);
0687 
0688         if (ret) {
0689             put_device(codec_dev);
0690             return ret;
0691         }
0692     }
0693 
0694     /* get speaker enable GPIO */
0695     if (quirk & SOF_ES8336_HEADPHONE_GPIO) {
0696         if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK)
0697             gpio_mapping = acpi_enable_both_gpios;
0698         else
0699             gpio_mapping = acpi_enable_both_gpios_rev_order;
0700     } else if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK) {
0701         gpio_mapping = acpi_speakers_enable_gpio1;
0702     } else {
0703         gpio_mapping = acpi_speakers_enable_gpio0;
0704     }
0705 
0706     ret = devm_acpi_dev_add_driver_gpios(codec_dev, gpio_mapping);
0707     if (ret)
0708         dev_warn(codec_dev, "unable to add GPIO mapping table\n");
0709 
0710     priv->gpio_speakers = gpiod_get_optional(codec_dev, "speakers-enable", GPIOD_OUT_LOW);
0711     if (IS_ERR(priv->gpio_speakers)) {
0712         ret = dev_err_probe(dev, PTR_ERR(priv->gpio_speakers),
0713                     "could not get speakers-enable GPIO\n");
0714         goto err_put_codec;
0715     }
0716 
0717     priv->gpio_headphone = gpiod_get_optional(codec_dev, "headphone-enable", GPIOD_OUT_LOW);
0718     if (IS_ERR(priv->gpio_headphone)) {
0719         ret = dev_err_probe(dev, PTR_ERR(priv->gpio_headphone),
0720                     "could not get headphone-enable GPIO\n");
0721         goto err_put_codec;
0722     }
0723 
0724     INIT_LIST_HEAD(&priv->hdmi_pcm_list);
0725 
0726     snd_soc_card_set_drvdata(card, priv);
0727 
0728     if (mach->mach_params.dmic_num > 0) {
0729         snprintf(soc_components, sizeof(soc_components),
0730              "cfg-dmics:%d", mach->mach_params.dmic_num);
0731         card->components = soc_components;
0732     }
0733 
0734     ret = devm_snd_soc_register_card(dev, card);
0735     if (ret) {
0736         gpiod_put(priv->gpio_speakers);
0737         dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
0738         goto err_put_codec;
0739     }
0740     platform_set_drvdata(pdev, &sof_es8336_card);
0741     return 0;
0742 
0743 err_put_codec:
0744     device_remove_software_node(priv->codec_dev);
0745     put_device(codec_dev);
0746     return ret;
0747 }
0748 
0749 static int sof_es8336_remove(struct platform_device *pdev)
0750 {
0751     struct snd_soc_card *card = platform_get_drvdata(pdev);
0752     struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
0753 
0754     gpiod_put(priv->gpio_speakers);
0755     device_remove_software_node(priv->codec_dev);
0756     put_device(priv->codec_dev);
0757 
0758     return 0;
0759 }
0760 
0761 static const struct platform_device_id board_ids[] = {
0762     {
0763         .name = "sof-essx8336", /* default quirk == 0 */
0764     },
0765     {
0766         .name = "adl_es83x6_c1_h02",
0767         .driver_data = (kernel_ulong_t)(SOF_ES8336_SSP_CODEC(1) |
0768                     SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
0769                     SOF_HDMI_CAPTURE_1_SSP(0) |
0770                     SOF_HDMI_CAPTURE_2_SSP(2) |
0771                     SOF_SSP_HDMI_CAPTURE_PRESENT |
0772                     SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK |
0773                     SOF_ES8336_JD_INVERTED),
0774     },
0775     { }
0776 };
0777 MODULE_DEVICE_TABLE(platform, board_ids);
0778 
0779 static struct platform_driver sof_es8336_driver = {
0780     .driver = {
0781         .name = "sof-essx8336",
0782         .pm = &snd_soc_pm_ops,
0783     },
0784     .probe = sof_es8336_probe,
0785     .remove = sof_es8336_remove,
0786     .id_table = board_ids,
0787 };
0788 module_platform_driver(sof_es8336_driver);
0789 
0790 MODULE_DESCRIPTION("ASoC Intel(R) SOF + ES8336 Machine driver");
0791 MODULE_LICENSE("GPL");
0792 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);