Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ASoC machine driver for Intel Broadwell platforms with RT5677 codec
0004  *
0005  * Copyright (c) 2014, The Chromium OS Authors.  All rights reserved.
0006  */
0007 
0008 #include <linux/acpi.h>
0009 #include <linux/module.h>
0010 #include <linux/platform_device.h>
0011 #include <linux/gpio/consumer.h>
0012 #include <linux/delay.h>
0013 #include <sound/core.h>
0014 #include <sound/pcm.h>
0015 #include <sound/soc.h>
0016 #include <sound/pcm_params.h>
0017 #include <sound/jack.h>
0018 #include <sound/soc-acpi.h>
0019 
0020 #include "../../codecs/rt5677.h"
0021 
0022 struct bdw_rt5677_priv {
0023     struct gpio_desc *gpio_hp_en;
0024     struct snd_soc_component *component;
0025 };
0026 
0027 static int bdw_rt5677_event_hp(struct snd_soc_dapm_widget *w,
0028             struct snd_kcontrol *k, int event)
0029 {
0030     struct snd_soc_dapm_context *dapm = w->dapm;
0031     struct snd_soc_card *card = dapm->card;
0032     struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
0033 
0034     if (SND_SOC_DAPM_EVENT_ON(event))
0035         msleep(70);
0036 
0037     gpiod_set_value_cansleep(bdw_rt5677->gpio_hp_en,
0038         SND_SOC_DAPM_EVENT_ON(event));
0039 
0040     return 0;
0041 }
0042 
0043 static const struct snd_soc_dapm_widget bdw_rt5677_widgets[] = {
0044     SND_SOC_DAPM_HP("Headphone", bdw_rt5677_event_hp),
0045     SND_SOC_DAPM_SPK("Speaker", NULL),
0046     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0047     SND_SOC_DAPM_MIC("Local DMICs", NULL),
0048     SND_SOC_DAPM_MIC("Remote DMICs", NULL),
0049 };
0050 
0051 static const struct snd_soc_dapm_route bdw_rt5677_map[] = {
0052     /* Speakers */
0053     {"Speaker", NULL, "PDM1L"},
0054     {"Speaker", NULL, "PDM1R"},
0055 
0056     /* Headset jack connectors */
0057     {"Headphone", NULL, "LOUT1"},
0058     {"Headphone", NULL, "LOUT2"},
0059     {"IN1P", NULL, "Headset Mic"},
0060     {"IN1N", NULL, "Headset Mic"},
0061 
0062     /* Digital MICs
0063      * Local DMICs: the two DMICs on the mainboard
0064      * Remote DMICs: the two DMICs on the camera module
0065      */
0066     {"DMIC L1", NULL, "Remote DMICs"},
0067     {"DMIC R1", NULL, "Remote DMICs"},
0068     {"DMIC L2", NULL, "Local DMICs"},
0069     {"DMIC R2", NULL, "Local DMICs"},
0070 
0071     /* CODEC BE connections */
0072     {"SSP0 CODEC IN", NULL, "AIF1 Capture"},
0073     {"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
0074     {"DSP Capture", NULL, "DSP Buffer"},
0075 
0076     /* DSP Clock Connections */
0077     { "DSP Buffer", NULL, "SSP0 CODEC IN" },
0078     { "SSP0 CODEC IN", NULL, "DSPTX" },
0079 };
0080 
0081 static const struct snd_kcontrol_new bdw_rt5677_controls[] = {
0082     SOC_DAPM_PIN_SWITCH("Speaker"),
0083     SOC_DAPM_PIN_SWITCH("Headphone"),
0084     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0085     SOC_DAPM_PIN_SWITCH("Local DMICs"),
0086     SOC_DAPM_PIN_SWITCH("Remote DMICs"),
0087 };
0088 
0089 
0090 static struct snd_soc_jack headphone_jack;
0091 static struct snd_soc_jack mic_jack;
0092 
0093 static struct snd_soc_jack_pin headphone_jack_pin = {
0094     .pin    = "Headphone",
0095     .mask   = SND_JACK_HEADPHONE,
0096 };
0097 
0098 static struct snd_soc_jack_pin mic_jack_pin = {
0099     .pin    = "Headset Mic",
0100     .mask   = SND_JACK_MICROPHONE,
0101 };
0102 
0103 static struct snd_soc_jack_gpio headphone_jack_gpio = {
0104     .name           = "plug-det",
0105     .report         = SND_JACK_HEADPHONE,
0106     .debounce_time      = 200,
0107 };
0108 
0109 static struct snd_soc_jack_gpio mic_jack_gpio = {
0110     .name           = "mic-present",
0111     .report         = SND_JACK_MICROPHONE,
0112     .debounce_time      = 200,
0113     .invert         = 1,
0114 };
0115 
0116 /* GPIO indexes defined by ACPI */
0117 enum {
0118     RT5677_GPIO_PLUG_DET        = 0,
0119     RT5677_GPIO_MIC_PRESENT_L   = 1,
0120     RT5677_GPIO_HOTWORD_DET_L   = 2,
0121     RT5677_GPIO_DSP_INT     = 3,
0122     RT5677_GPIO_HP_AMP_SHDN_L   = 4,
0123 };
0124 
0125 static const struct acpi_gpio_params plug_det_gpio = { RT5677_GPIO_PLUG_DET, 0, false };
0126 static const struct acpi_gpio_params mic_present_gpio = { RT5677_GPIO_MIC_PRESENT_L, 0, false };
0127 static const struct acpi_gpio_params headphone_enable_gpio = { RT5677_GPIO_HP_AMP_SHDN_L, 0, false };
0128 
0129 static const struct acpi_gpio_mapping bdw_rt5677_gpios[] = {
0130     { "plug-det-gpios", &plug_det_gpio, 1 },
0131     { "mic-present-gpios", &mic_present_gpio, 1 },
0132     { "headphone-enable-gpios", &headphone_enable_gpio, 1 },
0133     { NULL },
0134 };
0135 
0136 static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
0137             struct snd_pcm_hw_params *params)
0138 {
0139     struct snd_interval *rate = hw_param_interval(params,
0140                               SNDRV_PCM_HW_PARAM_RATE);
0141     struct snd_interval *chan = hw_param_interval(params,
0142                               SNDRV_PCM_HW_PARAM_CHANNELS);
0143 
0144     /* The ADSP will covert the FE rate to 48k, stereo */
0145     rate->min = rate->max = 48000;
0146     chan->min = chan->max = 2;
0147 
0148     /* set SSP0 to 16 bit */
0149     params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
0150     return 0;
0151 }
0152 
0153 static int bdw_rt5677_hw_params(struct snd_pcm_substream *substream,
0154     struct snd_pcm_hw_params *params)
0155 {
0156     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0157     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0158     int ret;
0159 
0160     ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_MCLK, 24576000,
0161         SND_SOC_CLOCK_IN);
0162     if (ret < 0) {
0163         dev_err(rtd->dev, "can't set codec sysclk configuration\n");
0164         return ret;
0165     }
0166 
0167     return ret;
0168 }
0169 
0170 static int bdw_rt5677_dsp_hw_params(struct snd_pcm_substream *substream,
0171     struct snd_pcm_hw_params *params)
0172 {
0173     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0174     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0175     int ret;
0176 
0177     ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_PLL1, 24576000,
0178         SND_SOC_CLOCK_IN);
0179     if (ret < 0) {
0180         dev_err(rtd->dev, "can't set codec sysclk configuration\n");
0181         return ret;
0182     }
0183     ret = snd_soc_dai_set_pll(codec_dai, 0, RT5677_PLL1_S_MCLK,
0184         24000000, 24576000);
0185     if (ret < 0) {
0186         dev_err(rtd->dev, "can't set codec pll configuration\n");
0187         return ret;
0188     }
0189 
0190     return 0;
0191 }
0192 
0193 static const struct snd_soc_ops bdw_rt5677_ops = {
0194     .hw_params = bdw_rt5677_hw_params,
0195 };
0196 
0197 static const struct snd_soc_ops bdw_rt5677_dsp_ops = {
0198     .hw_params = bdw_rt5677_dsp_hw_params,
0199 };
0200 
0201 static const unsigned int channels[] = {
0202     2,
0203 };
0204 
0205 static const struct snd_pcm_hw_constraint_list constraints_channels = {
0206     .count = ARRAY_SIZE(channels),
0207     .list = channels,
0208     .mask = 0,
0209 };
0210 
0211 static int bdw_rt5677_fe_startup(struct snd_pcm_substream *substream)
0212 {
0213     struct snd_pcm_runtime *runtime = substream->runtime;
0214 
0215     /* Board supports stereo configuration only */
0216     runtime->hw.channels_max = 2;
0217     return snd_pcm_hw_constraint_list(runtime, 0,
0218                       SNDRV_PCM_HW_PARAM_CHANNELS,
0219                       &constraints_channels);
0220 }
0221 
0222 static const struct snd_soc_ops bdw_rt5677_fe_ops = {
0223     .startup = bdw_rt5677_fe_startup,
0224 };
0225 
0226 static int bdw_rt5677_init(struct snd_soc_pcm_runtime *rtd)
0227 {
0228     struct bdw_rt5677_priv *bdw_rt5677 =
0229             snd_soc_card_get_drvdata(rtd->card);
0230     struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
0231     struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
0232     int ret;
0233 
0234     ret = devm_acpi_dev_add_driver_gpios(component->dev, bdw_rt5677_gpios);
0235     if (ret)
0236         dev_warn(component->dev, "Failed to add driver gpios\n");
0237 
0238     /* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1.
0239      * The ASRC clock source is clk_i2s1_asrc.
0240      */
0241     rt5677_sel_asrc_clk_src(component, RT5677_DA_STEREO_FILTER |
0242             RT5677_AD_STEREO1_FILTER | RT5677_I2S1_SOURCE,
0243             RT5677_CLK_SEL_I2S1_ASRC);
0244     /* Enable codec ASRC function for Mono ADC L.
0245      * The ASRC clock source is clk_sys2_asrc.
0246      */
0247     rt5677_sel_asrc_clk_src(component, RT5677_AD_MONO_L_FILTER,
0248             RT5677_CLK_SEL_SYS2);
0249 
0250     /* Request rt5677 GPIO for headphone amp control */
0251     bdw_rt5677->gpio_hp_en = gpiod_get(component->dev, "headphone-enable",
0252                        GPIOD_OUT_LOW);
0253     if (IS_ERR(bdw_rt5677->gpio_hp_en)) {
0254         dev_err(component->dev, "Can't find HP_AMP_SHDN_L gpio\n");
0255         return PTR_ERR(bdw_rt5677->gpio_hp_en);
0256     }
0257 
0258     /* Create and initialize headphone jack */
0259     if (!snd_soc_card_jack_new_pins(rtd->card, "Headphone Jack",
0260             SND_JACK_HEADPHONE, &headphone_jack,
0261             &headphone_jack_pin, 1)) {
0262         headphone_jack_gpio.gpiod_dev = component->dev;
0263         if (snd_soc_jack_add_gpios(&headphone_jack, 1,
0264                 &headphone_jack_gpio))
0265             dev_err(component->dev, "Can't add headphone jack gpio\n");
0266     } else {
0267         dev_err(component->dev, "Can't create headphone jack\n");
0268     }
0269 
0270     /* Create and initialize mic jack */
0271     if (!snd_soc_card_jack_new_pins(rtd->card, "Mic Jack",
0272             SND_JACK_MICROPHONE, &mic_jack,
0273             &mic_jack_pin, 1)) {
0274         mic_jack_gpio.gpiod_dev = component->dev;
0275         if (snd_soc_jack_add_gpios(&mic_jack, 1, &mic_jack_gpio))
0276             dev_err(component->dev, "Can't add mic jack gpio\n");
0277     } else {
0278         dev_err(component->dev, "Can't create mic jack\n");
0279     }
0280     bdw_rt5677->component = component;
0281 
0282     snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
0283     return 0;
0284 }
0285 
0286 static void bdw_rt5677_exit(struct snd_soc_pcm_runtime *rtd)
0287 {
0288     struct bdw_rt5677_priv *bdw_rt5677 =
0289             snd_soc_card_get_drvdata(rtd->card);
0290 
0291     /*
0292      * The .exit() can be reached without going through the .init()
0293      * so explicitly test if the gpiod is valid
0294      */
0295     if (!IS_ERR_OR_NULL(bdw_rt5677->gpio_hp_en))
0296         gpiod_put(bdw_rt5677->gpio_hp_en);
0297 }
0298 
0299 /* broadwell digital audio interface glue - connects codec <--> CPU */
0300 SND_SOC_DAILINK_DEF(dummy,
0301     DAILINK_COMP_ARRAY(COMP_DUMMY()));
0302 
0303 SND_SOC_DAILINK_DEF(fe,
0304     DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
0305 
0306 SND_SOC_DAILINK_DEF(platform,
0307     DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio")));
0308 
0309 SND_SOC_DAILINK_DEF(be,
0310     DAILINK_COMP_ARRAY(COMP_CODEC("i2c-RT5677CE:00", "rt5677-aif1")));
0311 
0312 SND_SOC_DAILINK_DEF(ssp0_port,
0313         DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
0314 
0315 /* Wake on voice interface */
0316 SND_SOC_DAILINK_DEFS(dsp,
0317     DAILINK_COMP_ARRAY(COMP_CPU("spi-RT5677AA:00")),
0318     DAILINK_COMP_ARRAY(COMP_CODEC("i2c-RT5677CE:00", "rt5677-dspbuffer")),
0319     DAILINK_COMP_ARRAY(COMP_PLATFORM("spi-RT5677AA:00")));
0320 
0321 static struct snd_soc_dai_link bdw_rt5677_dais[] = {
0322     /* Front End DAI links */
0323     {
0324         .name = "System PCM",
0325         .stream_name = "System Playback/Capture",
0326         .nonatomic = 1,
0327         .dynamic = 1,
0328         .trigger = {
0329             SND_SOC_DPCM_TRIGGER_POST,
0330             SND_SOC_DPCM_TRIGGER_POST
0331         },
0332         .dpcm_capture = 1,
0333         .dpcm_playback = 1,
0334         .ops = &bdw_rt5677_fe_ops,
0335         SND_SOC_DAILINK_REG(fe, dummy, platform),
0336     },
0337 
0338     /* Non-DPCM links */
0339     {
0340         .name = "Codec DSP",
0341         .stream_name = "Wake on Voice",
0342         .capture_only = 1,
0343         .ops = &bdw_rt5677_dsp_ops,
0344         SND_SOC_DAILINK_REG(dsp),
0345     },
0346 
0347     /* Back End DAI links */
0348     {
0349         /* SSP0 - Codec */
0350         .name = "Codec",
0351         .id = 0,
0352         .nonatomic = 1,
0353         .no_pcm = 1,
0354         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
0355             SND_SOC_DAIFMT_CBC_CFC,
0356         .ignore_pmdown_time = 1,
0357         .be_hw_params_fixup = broadwell_ssp0_fixup,
0358         .ops = &bdw_rt5677_ops,
0359         .dpcm_playback = 1,
0360         .dpcm_capture = 1,
0361         .init = bdw_rt5677_init,
0362         .exit = bdw_rt5677_exit,
0363         SND_SOC_DAILINK_REG(ssp0_port, be, platform),
0364     },
0365 };
0366 
0367 static int bdw_rt5677_suspend_pre(struct snd_soc_card *card)
0368 {
0369     struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
0370     struct snd_soc_dapm_context *dapm;
0371 
0372     if (bdw_rt5677->component) {
0373         dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
0374         snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
0375     }
0376     return 0;
0377 }
0378 
0379 static int bdw_rt5677_resume_post(struct snd_soc_card *card)
0380 {
0381     struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
0382     struct snd_soc_dapm_context *dapm;
0383 
0384     if (bdw_rt5677->component) {
0385         dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
0386         snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
0387     }
0388     return 0;
0389 }
0390 
0391 /* use space before codec name to simplify card ID, and simplify driver name */
0392 #define SOF_CARD_NAME "bdw rt5677" /* card name will be 'sof-bdw rt5677' */
0393 #define SOF_DRIVER_NAME "SOF"
0394 
0395 #define CARD_NAME "bdw-rt5677"
0396 #define DRIVER_NAME NULL /* card name will be used for driver name */
0397 
0398 /* ASoC machine driver for Broadwell DSP + RT5677 */
0399 static struct snd_soc_card bdw_rt5677_card = {
0400     .name = CARD_NAME,
0401     .driver_name = DRIVER_NAME,
0402     .owner = THIS_MODULE,
0403     .dai_link = bdw_rt5677_dais,
0404     .num_links = ARRAY_SIZE(bdw_rt5677_dais),
0405     .dapm_widgets = bdw_rt5677_widgets,
0406     .num_dapm_widgets = ARRAY_SIZE(bdw_rt5677_widgets),
0407     .dapm_routes = bdw_rt5677_map,
0408     .num_dapm_routes = ARRAY_SIZE(bdw_rt5677_map),
0409     .controls = bdw_rt5677_controls,
0410     .num_controls = ARRAY_SIZE(bdw_rt5677_controls),
0411     .fully_routed = true,
0412     .suspend_pre = bdw_rt5677_suspend_pre,
0413     .resume_post = bdw_rt5677_resume_post,
0414 };
0415 
0416 static int bdw_rt5677_probe(struct platform_device *pdev)
0417 {
0418     struct bdw_rt5677_priv *bdw_rt5677;
0419     struct snd_soc_acpi_mach *mach;
0420     int ret;
0421 
0422     bdw_rt5677_card.dev = &pdev->dev;
0423 
0424     /* Allocate driver private struct */
0425     bdw_rt5677 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5677_priv),
0426         GFP_KERNEL);
0427     if (!bdw_rt5677)
0428         return -ENOMEM;
0429 
0430     /* override platform name, if required */
0431     mach = pdev->dev.platform_data;
0432     ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt5677_card,
0433                             mach->mach_params.platform);
0434     if (ret)
0435         return ret;
0436 
0437     /* set card and driver name */
0438     if (snd_soc_acpi_sof_parent(&pdev->dev)) {
0439         bdw_rt5677_card.name = SOF_CARD_NAME;
0440         bdw_rt5677_card.driver_name = SOF_DRIVER_NAME;
0441     } else {
0442         bdw_rt5677_card.name = CARD_NAME;
0443         bdw_rt5677_card.driver_name = DRIVER_NAME;
0444     }
0445 
0446     snd_soc_card_set_drvdata(&bdw_rt5677_card, bdw_rt5677);
0447 
0448     return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5677_card);
0449 }
0450 
0451 static struct platform_driver bdw_rt5677_audio = {
0452     .probe = bdw_rt5677_probe,
0453     .driver = {
0454         .name = "bdw-rt5677",
0455         .pm = &snd_soc_pm_ops
0456     },
0457 };
0458 
0459 module_platform_driver(bdw_rt5677_audio)
0460 
0461 /* Module information */
0462 MODULE_AUTHOR("Ben Zhang");
0463 MODULE_DESCRIPTION("Intel Broadwell RT5677 machine driver");
0464 MODULE_LICENSE("GPL v2");
0465 MODULE_ALIAS("platform:bdw-rt5677");