Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 // Copyright(c) 2019 Intel Corporation.
0003 
0004 /*
0005  * Intel SOF Machine driver for DA7219 + MAX98373/MAX98360A codec
0006  */
0007 
0008 #include <linux/input.h>
0009 #include <linux/module.h>
0010 #include <sound/pcm.h>
0011 #include <sound/pcm_params.h>
0012 #include <linux/platform_device.h>
0013 #include <sound/soc.h>
0014 #include <sound/soc-acpi.h>
0015 #include "../../codecs/da7219.h"
0016 #include "../../codecs/da7219-aad.h"
0017 #include "hda_dsp_common.h"
0018 
0019 #define DIALOG_CODEC_DAI    "da7219-hifi"
0020 #define MAX98373_CODEC_DAI  "max98373-aif1"
0021 #define MAXIM_DEV0_NAME     "i2c-MX98373:00"
0022 #define MAXIM_DEV1_NAME     "i2c-MX98373:01"
0023 
0024 struct hdmi_pcm {
0025     struct list_head head;
0026     struct snd_soc_dai *codec_dai;
0027     int device;
0028 };
0029 
0030 struct card_private {
0031     struct snd_soc_jack headset;
0032     struct list_head hdmi_pcm_list;
0033     struct snd_soc_jack hdmi[3];
0034 };
0035 
0036 static int platform_clock_control(struct snd_soc_dapm_widget *w,
0037                   struct snd_kcontrol *k, int  event)
0038 {
0039     struct snd_soc_dapm_context *dapm = w->dapm;
0040     struct snd_soc_card *card = dapm->card;
0041     struct snd_soc_dai *codec_dai;
0042     int ret = 0;
0043 
0044     codec_dai = snd_soc_card_get_codec_dai(card, DIALOG_CODEC_DAI);
0045     if (!codec_dai) {
0046         dev_err(card->dev, "Codec dai not found; Unable to set/unset codec pll\n");
0047         return -EIO;
0048     }
0049 
0050     if (SND_SOC_DAPM_EVENT_OFF(event)) {
0051         ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK,
0052                       0, 0);
0053         if (ret)
0054             dev_err(card->dev, "failed to stop PLL: %d\n", ret);
0055     } else if (SND_SOC_DAPM_EVENT_ON(event)) {
0056         ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL_SRM,
0057                       0, DA7219_PLL_FREQ_OUT_98304);
0058         if (ret)
0059             dev_err(card->dev, "failed to start PLL: %d\n", ret);
0060     }
0061 
0062     return ret;
0063 }
0064 
0065 static const struct snd_kcontrol_new controls[] = {
0066     SOC_DAPM_PIN_SWITCH("Headphone Jack"),
0067     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0068     SOC_DAPM_PIN_SWITCH("Left Spk"),
0069     SOC_DAPM_PIN_SWITCH("Right Spk"),
0070 };
0071 
0072 static const struct snd_kcontrol_new m98360a_controls[] = {
0073     SOC_DAPM_PIN_SWITCH("Headphone Jack"),
0074     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0075     SOC_DAPM_PIN_SWITCH("Spk"),
0076 };
0077 
0078 /* For MAX98373 amp */
0079 static const struct snd_soc_dapm_widget widgets[] = {
0080     SND_SOC_DAPM_HP("Headphone Jack", NULL),
0081     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0082 
0083     SND_SOC_DAPM_SPK("Left Spk", NULL),
0084     SND_SOC_DAPM_SPK("Right Spk", NULL),
0085 
0086     SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
0087                 platform_clock_control, SND_SOC_DAPM_POST_PMD |
0088                 SND_SOC_DAPM_PRE_PMU),
0089 
0090     SND_SOC_DAPM_MIC("SoC DMIC", NULL),
0091 };
0092 
0093 static const struct snd_soc_dapm_route audio_map[] = {
0094     { "Headphone Jack", NULL, "HPL" },
0095     { "Headphone Jack", NULL, "HPR" },
0096 
0097     { "MIC", NULL, "Headset Mic" },
0098 
0099     { "Headphone Jack", NULL, "Platform Clock" },
0100     { "Headset Mic", NULL, "Platform Clock" },
0101 
0102     { "Left Spk", NULL, "Left BE_OUT" },
0103     { "Right Spk", NULL, "Right BE_OUT" },
0104 
0105     /* digital mics */
0106     {"DMic", NULL, "SoC DMIC"},
0107 };
0108 
0109 /* For MAX98360A amp */
0110 static const struct snd_soc_dapm_widget max98360a_widgets[] = {
0111     SND_SOC_DAPM_HP("Headphone Jack", NULL),
0112     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0113 
0114     SND_SOC_DAPM_SPK("Spk", NULL),
0115 
0116     SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
0117                 platform_clock_control, SND_SOC_DAPM_POST_PMD |
0118                 SND_SOC_DAPM_PRE_PMU),
0119 
0120     SND_SOC_DAPM_MIC("SoC DMIC", NULL),
0121 };
0122 
0123 static const struct snd_soc_dapm_route max98360a_map[] = {
0124     { "Headphone Jack", NULL, "HPL" },
0125     { "Headphone Jack", NULL, "HPR" },
0126 
0127     { "MIC", NULL, "Headset Mic" },
0128 
0129     { "Headphone Jack", NULL, "Platform Clock" },
0130     { "Headset Mic", NULL, "Platform Clock" },
0131 
0132     {"Spk", NULL, "Speaker"},
0133 
0134     /* digital mics */
0135     {"DMic", NULL, "SoC DMIC"},
0136 };
0137 
0138 static struct snd_soc_jack_pin jack_pins[] = {
0139     {
0140         .pin    = "Headphone Jack",
0141         .mask   = SND_JACK_HEADPHONE,
0142     },
0143     {
0144         .pin    = "Headset Mic",
0145         .mask   = SND_JACK_MICROPHONE,
0146     },
0147 };
0148 
0149 static struct snd_soc_jack headset;
0150 
0151 static int da7219_codec_init(struct snd_soc_pcm_runtime *rtd)
0152 {
0153     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0154     struct snd_soc_component *component = codec_dai->component;
0155     struct snd_soc_jack *jack;
0156     int ret;
0157 
0158     /* Configure sysclk for codec */
0159     ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, 24000000,
0160                      SND_SOC_CLOCK_IN);
0161     if (ret) {
0162         dev_err(rtd->dev, "can't set codec sysclk configuration\n");
0163         return ret;
0164     }
0165 
0166     /*
0167      * Headset buttons map to the google Reference headset.
0168      * These can be configured by userspace.
0169      */
0170     ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
0171                      SND_JACK_HEADSET | SND_JACK_BTN_0 |
0172                      SND_JACK_BTN_1 | SND_JACK_BTN_2 |
0173                      SND_JACK_BTN_3 | SND_JACK_LINEOUT,
0174                      &headset,
0175                      jack_pins,
0176                      ARRAY_SIZE(jack_pins));
0177     if (ret) {
0178         dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
0179         return ret;
0180     }
0181 
0182     jack = &headset;
0183     snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
0184     snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
0185     snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
0186     snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
0187     da7219_aad_jack_det(component, jack);
0188 
0189     return ret;
0190 }
0191 
0192 static int ssp1_hw_params(struct snd_pcm_substream *substream,
0193                   struct snd_pcm_hw_params *params)
0194 {
0195     struct snd_soc_pcm_runtime *runtime = asoc_substream_to_rtd(substream);
0196     int ret, j;
0197 
0198     for (j = 0; j < runtime->num_codecs; j++) {
0199         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(runtime, j);
0200 
0201         if (!strcmp(codec_dai->component->name, MAXIM_DEV0_NAME)) {
0202             /* vmon_slot_no = 0 imon_slot_no = 1 for TX slots */
0203             ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 3, 4, 16);
0204             if (ret < 0) {
0205                 dev_err(runtime->dev, "DEV0 TDM slot err:%d\n", ret);
0206                 return ret;
0207             }
0208         }
0209         if (!strcmp(codec_dai->component->name, MAXIM_DEV1_NAME)) {
0210             /* vmon_slot_no = 2 imon_slot_no = 3 for TX slots */
0211             ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC, 3, 4, 16);
0212             if (ret < 0) {
0213                 dev_err(runtime->dev, "DEV1 TDM slot err:%d\n", ret);
0214                 return ret;
0215             }
0216         }
0217     }
0218 
0219     return 0;
0220 }
0221 
0222 static struct snd_soc_ops ssp1_ops = {
0223     .hw_params = ssp1_hw_params,
0224 };
0225 
0226 static struct snd_soc_codec_conf max98373_codec_conf[] = {
0227     {
0228         .dlc = COMP_CODEC_CONF(MAXIM_DEV0_NAME),
0229         .name_prefix = "Right",
0230     },
0231     {
0232         .dlc = COMP_CODEC_CONF(MAXIM_DEV1_NAME),
0233         .name_prefix = "Left",
0234     },
0235 };
0236 
0237 static int hdmi_init(struct snd_soc_pcm_runtime *rtd)
0238 {
0239     struct card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
0240     struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
0241     struct hdmi_pcm *pcm;
0242 
0243     pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
0244     if (!pcm)
0245         return -ENOMEM;
0246 
0247     pcm->device = dai->id;
0248     pcm->codec_dai = dai;
0249 
0250     list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
0251 
0252     return 0;
0253 }
0254 
0255 static int card_late_probe(struct snd_soc_card *card)
0256 {
0257     struct card_private *ctx = snd_soc_card_get_drvdata(card);
0258     struct snd_soc_acpi_mach *mach = (card->dev)->platform_data;
0259     struct hdmi_pcm *pcm;
0260 
0261     if (mach->mach_params.common_hdmi_codec_drv) {
0262         pcm = list_first_entry(&ctx->hdmi_pcm_list, struct hdmi_pcm,
0263                        head);
0264         return hda_dsp_hdmi_build_controls(card,
0265                            pcm->codec_dai->component);
0266     }
0267 
0268     return -EINVAL;
0269 }
0270 
0271 SND_SOC_DAILINK_DEF(ssp0_pin,
0272     DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin")));
0273 SND_SOC_DAILINK_DEF(ssp0_codec,
0274     DAILINK_COMP_ARRAY(COMP_CODEC("i2c-DLGS7219:00", DIALOG_CODEC_DAI)));
0275 
0276 SND_SOC_DAILINK_DEF(ssp1_pin,
0277     DAILINK_COMP_ARRAY(COMP_CPU("SSP1 Pin")));
0278 SND_SOC_DAILINK_DEF(ssp1_amps,
0279     DAILINK_COMP_ARRAY(
0280     /* Left */  COMP_CODEC(MAXIM_DEV0_NAME, MAX98373_CODEC_DAI),
0281     /* Right */ COMP_CODEC(MAXIM_DEV1_NAME, MAX98373_CODEC_DAI)));
0282 
0283 SND_SOC_DAILINK_DEF(ssp1_m98360a,
0284     DAILINK_COMP_ARRAY(COMP_CODEC("MX98360A:00", "HiFi")));
0285 
0286 SND_SOC_DAILINK_DEF(dmic_pin,
0287     DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin")));
0288 SND_SOC_DAILINK_DEF(dmic_codec,
0289     DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
0290 
0291 SND_SOC_DAILINK_DEF(dmic16k_pin,
0292     DAILINK_COMP_ARRAY(COMP_CPU("DMIC16k Pin")));
0293 
0294 SND_SOC_DAILINK_DEF(idisp1_pin,
0295     DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
0296 SND_SOC_DAILINK_DEF(idisp1_codec,
0297     DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1")));
0298 
0299 SND_SOC_DAILINK_DEF(idisp2_pin,
0300     DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
0301 SND_SOC_DAILINK_DEF(idisp2_codec,
0302     DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));
0303 
0304 SND_SOC_DAILINK_DEF(idisp3_pin,
0305     DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin")));
0306 SND_SOC_DAILINK_DEF(idisp3_codec,
0307     DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3")));
0308 
0309 SND_SOC_DAILINK_DEF(platform, /* subject to be overridden during probe */
0310     DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3")));
0311 
0312 static struct snd_soc_dai_link dais[] = {
0313     /* Back End DAI links */
0314     {
0315         .name = "SSP1-Codec",
0316         .id = 0,
0317         .ignore_pmdown_time = 1,
0318         .no_pcm = 1,
0319         .dpcm_playback = 1,
0320         .dpcm_capture = 1, /* IV feedback */
0321         .ops = &ssp1_ops,
0322         SND_SOC_DAILINK_REG(ssp1_pin, ssp1_amps, platform),
0323     },
0324     {
0325         .name = "SSP0-Codec",
0326         .id = 1,
0327         .no_pcm = 1,
0328         .init = da7219_codec_init,
0329         .ignore_pmdown_time = 1,
0330         .dpcm_playback = 1,
0331         .dpcm_capture = 1,
0332         SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform),
0333     },
0334     {
0335         .name = "dmic01",
0336         .id = 2,
0337         .ignore_suspend = 1,
0338         .dpcm_capture = 1,
0339         .no_pcm = 1,
0340         SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform),
0341     },
0342     {
0343         .name = "iDisp1",
0344         .id = 3,
0345         .init = hdmi_init,
0346         .dpcm_playback = 1,
0347         .no_pcm = 1,
0348         SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
0349     },
0350     {
0351         .name = "iDisp2",
0352         .id = 4,
0353         .init = hdmi_init,
0354         .dpcm_playback = 1,
0355         .no_pcm = 1,
0356         SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
0357     },
0358     {
0359         .name = "iDisp3",
0360         .id = 5,
0361         .init = hdmi_init,
0362         .dpcm_playback = 1,
0363         .no_pcm = 1,
0364         SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
0365     },
0366     {
0367         .name = "dmic16k",
0368         .id = 6,
0369         .ignore_suspend = 1,
0370         .dpcm_capture = 1,
0371         .no_pcm = 1,
0372         SND_SOC_DAILINK_REG(dmic16k_pin, dmic_codec, platform),
0373     }
0374 };
0375 
0376 static struct snd_soc_card card_da7219_m98373 = {
0377     .name = "da7219max",
0378     .owner = THIS_MODULE,
0379     .dai_link = dais,
0380     .num_links = ARRAY_SIZE(dais),
0381     .controls = controls,
0382     .num_controls = ARRAY_SIZE(controls),
0383     .dapm_widgets = widgets,
0384     .num_dapm_widgets = ARRAY_SIZE(widgets),
0385     .dapm_routes = audio_map,
0386     .num_dapm_routes = ARRAY_SIZE(audio_map),
0387     .codec_conf = max98373_codec_conf,
0388     .num_configs = ARRAY_SIZE(max98373_codec_conf),
0389     .fully_routed = true,
0390     .late_probe = card_late_probe,
0391 };
0392 
0393 static struct snd_soc_card card_da7219_m98360a = {
0394     .name = "da7219max98360a",
0395     .owner = THIS_MODULE,
0396     .dai_link = dais,
0397     .num_links = ARRAY_SIZE(dais),
0398     .controls = m98360a_controls,
0399     .num_controls = ARRAY_SIZE(m98360a_controls),
0400     .dapm_widgets = max98360a_widgets,
0401     .num_dapm_widgets = ARRAY_SIZE(max98360a_widgets),
0402     .dapm_routes = max98360a_map,
0403     .num_dapm_routes = ARRAY_SIZE(max98360a_map),
0404     .fully_routed = true,
0405     .late_probe = card_late_probe,
0406 };
0407 
0408 static int audio_probe(struct platform_device *pdev)
0409 {
0410     static struct snd_soc_card *card;
0411     struct snd_soc_acpi_mach *mach;
0412     struct card_private *ctx;
0413     int ret;
0414 
0415     ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
0416     if (!ctx)
0417         return -ENOMEM;
0418 
0419     /* By default dais[0] is configured for max98373 */
0420     if (!strcmp(pdev->name, "sof_da7219_mx98360a")) {
0421         dais[0] = (struct snd_soc_dai_link) {
0422             .name = "SSP1-Codec",
0423             .id = 0,
0424             .no_pcm = 1,
0425             .dpcm_playback = 1,
0426             .ignore_pmdown_time = 1,
0427             SND_SOC_DAILINK_REG(ssp1_pin, ssp1_m98360a, platform) };
0428     }
0429 
0430     INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
0431     card = (struct snd_soc_card *)pdev->id_entry->driver_data;
0432     card->dev = &pdev->dev;
0433 
0434     mach = pdev->dev.platform_data;
0435     ret = snd_soc_fixup_dai_links_platform_name(card,
0436                             mach->mach_params.platform);
0437     if (ret)
0438         return ret;
0439 
0440     snd_soc_card_set_drvdata(card, ctx);
0441 
0442     return devm_snd_soc_register_card(&pdev->dev, card);
0443 }
0444 
0445 static const struct platform_device_id board_ids[] = {
0446     {
0447         .name = "sof_da7219_mx98373",
0448         .driver_data = (kernel_ulong_t)&card_da7219_m98373,
0449     },
0450     {
0451         .name = "sof_da7219_mx98360a",
0452         .driver_data = (kernel_ulong_t)&card_da7219_m98360a,
0453     },
0454     { }
0455 };
0456 MODULE_DEVICE_TABLE(platform, board_ids);
0457 
0458 static struct platform_driver audio = {
0459     .probe = audio_probe,
0460     .driver = {
0461         .name = "sof_da7219_max98_360a_373",
0462         .pm = &snd_soc_pm_ops,
0463     },
0464     .id_table = board_ids,
0465 };
0466 module_platform_driver(audio)
0467 
0468 /* Module information */
0469 MODULE_DESCRIPTION("ASoC Intel(R) SOF Machine driver");
0470 MODULE_AUTHOR("Yong Zhi <yong.zhi@intel.com>");
0471 MODULE_LICENSE("GPL v2");
0472 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);