Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ASoC machine driver for Intel Broadwell platforms with RT5650 codec
0004  *
0005  * Copyright 2019, The Chromium OS Authors.  All rights reserved.
0006  */
0007 
0008 #include <linux/delay.h>
0009 #include <linux/gpio/consumer.h>
0010 #include <linux/module.h>
0011 #include <linux/platform_device.h>
0012 #include <sound/core.h>
0013 #include <sound/jack.h>
0014 #include <sound/pcm.h>
0015 #include <sound/pcm_params.h>
0016 #include <sound/soc.h>
0017 #include <sound/soc-acpi.h>
0018 
0019 #include "../../codecs/rt5645.h"
0020 
0021 struct bdw_rt5650_priv {
0022     struct gpio_desc *gpio_hp_en;
0023     struct snd_soc_component *component;
0024 };
0025 
0026 static const struct snd_soc_dapm_widget bdw_rt5650_widgets[] = {
0027     SND_SOC_DAPM_HP("Headphone", NULL),
0028     SND_SOC_DAPM_SPK("Speaker", NULL),
0029     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0030     SND_SOC_DAPM_MIC("DMIC Pair1", NULL),
0031     SND_SOC_DAPM_MIC("DMIC Pair2", NULL),
0032 };
0033 
0034 static const struct snd_soc_dapm_route bdw_rt5650_map[] = {
0035     /* Speakers */
0036     {"Speaker", NULL, "SPOL"},
0037     {"Speaker", NULL, "SPOR"},
0038 
0039     /* Headset jack connectors */
0040     {"Headphone", NULL, "HPOL"},
0041     {"Headphone", NULL, "HPOR"},
0042     {"IN1P", NULL, "Headset Mic"},
0043     {"IN1N", NULL, "Headset Mic"},
0044 
0045     /* Digital MICs
0046      * DMIC Pair1 are the two DMICs connected on the DMICN1 connector.
0047      * DMIC Pair2 are the two DMICs connected on the DMICN2 connector.
0048      * Facing the camera, DMIC Pair1 are on the left side, DMIC Pair2
0049      * are on the right side.
0050      */
0051     {"DMIC L1", NULL, "DMIC Pair1"},
0052     {"DMIC R1", NULL, "DMIC Pair1"},
0053     {"DMIC L2", NULL, "DMIC Pair2"},
0054     {"DMIC R2", NULL, "DMIC Pair2"},
0055 
0056     /* CODEC BE connections */
0057     {"SSP0 CODEC IN", NULL, "AIF1 Capture"},
0058     {"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
0059 };
0060 
0061 static const struct snd_kcontrol_new bdw_rt5650_controls[] = {
0062     SOC_DAPM_PIN_SWITCH("Speaker"),
0063     SOC_DAPM_PIN_SWITCH("Headphone"),
0064     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0065     SOC_DAPM_PIN_SWITCH("DMIC Pair1"),
0066     SOC_DAPM_PIN_SWITCH("DMIC Pair2"),
0067 };
0068 
0069 
0070 static struct snd_soc_jack headphone_jack;
0071 static struct snd_soc_jack mic_jack;
0072 
0073 static struct snd_soc_jack_pin headphone_jack_pin = {
0074     .pin    = "Headphone",
0075     .mask   = SND_JACK_HEADPHONE,
0076 };
0077 
0078 static struct snd_soc_jack_pin mic_jack_pin = {
0079     .pin    = "Headset Mic",
0080     .mask   = SND_JACK_MICROPHONE,
0081 };
0082 
0083 static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
0084             struct snd_pcm_hw_params *params)
0085 {
0086     struct snd_interval *rate = hw_param_interval(params,
0087                               SNDRV_PCM_HW_PARAM_RATE);
0088     struct snd_interval *chan = hw_param_interval(params,
0089                               SNDRV_PCM_HW_PARAM_CHANNELS);
0090 
0091     /* The ADSP will covert the FE rate to 48k, max 4-channels */
0092     rate->min = rate->max = 48000;
0093     chan->min = 2;
0094     chan->max = 4;
0095 
0096     /* set SSP0 to 24 bit */
0097     snd_mask_set_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
0098                 SNDRV_PCM_FORMAT_S24_LE);
0099 
0100     return 0;
0101 }
0102 
0103 static int bdw_rt5650_hw_params(struct snd_pcm_substream *substream,
0104     struct snd_pcm_hw_params *params)
0105 {
0106     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0107     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0108     int ret;
0109 
0110     /* Workaround: set codec PLL to 19.2MHz that PLL source is
0111      * from MCLK(24MHz) to conform 2.4MHz DMIC clock.
0112      */
0113     ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
0114         24000000, 19200000);
0115     if (ret < 0) {
0116         dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
0117         return ret;
0118     }
0119 
0120     /* The actual MCLK freq is 24MHz. The codec is told that MCLK is
0121      * 24.576MHz to satisfy the requirement of rl6231_get_clk_info.
0122      * ASRC is enabled on AD and DA filters to ensure good audio quality.
0123      */
0124     ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1, 24576000,
0125         SND_SOC_CLOCK_IN);
0126     if (ret < 0) {
0127         dev_err(rtd->dev, "can't set codec sysclk configuration\n");
0128         return ret;
0129     }
0130 
0131     return ret;
0132 }
0133 
0134 static struct snd_soc_ops bdw_rt5650_ops = {
0135     .hw_params = bdw_rt5650_hw_params,
0136 };
0137 
0138 static const unsigned int channels[] = {
0139     2, 4,
0140 };
0141 
0142 static const struct snd_pcm_hw_constraint_list constraints_channels = {
0143     .count = ARRAY_SIZE(channels),
0144     .list = channels,
0145     .mask = 0,
0146 };
0147 
0148 static int bdw_rt5650_fe_startup(struct snd_pcm_substream *substream)
0149 {
0150     struct snd_pcm_runtime *runtime = substream->runtime;
0151 
0152     /* Board supports stereo and quad configurations for capture */
0153     if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
0154         return 0;
0155 
0156     runtime->hw.channels_max = 4;
0157     return snd_pcm_hw_constraint_list(runtime, 0,
0158                       SNDRV_PCM_HW_PARAM_CHANNELS,
0159                       &constraints_channels);
0160 }
0161 
0162 static const struct snd_soc_ops bdw_rt5650_fe_ops = {
0163     .startup = bdw_rt5650_fe_startup,
0164 };
0165 
0166 static int bdw_rt5650_init(struct snd_soc_pcm_runtime *rtd)
0167 {
0168     struct bdw_rt5650_priv *bdw_rt5650 =
0169         snd_soc_card_get_drvdata(rtd->card);
0170     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0171     struct snd_soc_component *component = codec_dai->component;
0172     int ret;
0173 
0174     /* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1.
0175      * The ASRC clock source is clk_i2s1_asrc.
0176      */
0177     rt5645_sel_asrc_clk_src(component,
0178                 RT5645_DA_STEREO_FILTER |
0179                 RT5645_DA_MONO_L_FILTER |
0180                 RT5645_DA_MONO_R_FILTER |
0181                 RT5645_AD_STEREO_FILTER |
0182                 RT5645_AD_MONO_L_FILTER |
0183                 RT5645_AD_MONO_R_FILTER,
0184                 RT5645_CLK_SEL_I2S1_ASRC);
0185 
0186     /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
0187     ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
0188 
0189     if (ret < 0) {
0190         dev_err(rtd->dev, "can't set codec TDM slot %d\n", ret);
0191         return ret;
0192     }
0193 
0194     /* Create and initialize headphone jack */
0195     if (snd_soc_card_jack_new_pins(rtd->card, "Headphone Jack",
0196             SND_JACK_HEADPHONE, &headphone_jack,
0197             &headphone_jack_pin, 1)) {
0198         dev_err(component->dev, "Can't create headphone jack\n");
0199     }
0200 
0201     /* Create and initialize mic jack */
0202     if (snd_soc_card_jack_new_pins(rtd->card, "Mic Jack",
0203             SND_JACK_MICROPHONE, &mic_jack, &mic_jack_pin, 1)) {
0204         dev_err(component->dev, "Can't create mic jack\n");
0205     }
0206 
0207     rt5645_set_jack_detect(component, &headphone_jack, &mic_jack, NULL);
0208 
0209     bdw_rt5650->component = component;
0210 
0211     return 0;
0212 }
0213 
0214 /* broadwell digital audio interface glue - connects codec <--> CPU */
0215 SND_SOC_DAILINK_DEF(dummy,
0216     DAILINK_COMP_ARRAY(COMP_DUMMY()));
0217 
0218 SND_SOC_DAILINK_DEF(fe,
0219     DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
0220 
0221 SND_SOC_DAILINK_DEF(platform,
0222     DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio")));
0223 
0224 SND_SOC_DAILINK_DEF(be,
0225     DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5650:00", "rt5645-aif1")));
0226 
0227 SND_SOC_DAILINK_DEF(ssp0_port,
0228         DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
0229 
0230 static struct snd_soc_dai_link bdw_rt5650_dais[] = {
0231     /* Front End DAI links */
0232     {
0233         .name = "System PCM",
0234         .stream_name = "System Playback",
0235         .nonatomic = 1,
0236         .dynamic = 1,
0237         .ops = &bdw_rt5650_fe_ops,
0238         .trigger = {
0239             SND_SOC_DPCM_TRIGGER_POST,
0240             SND_SOC_DPCM_TRIGGER_POST
0241         },
0242         .dpcm_playback = 1,
0243         .dpcm_capture = 1,
0244         SND_SOC_DAILINK_REG(fe, dummy, platform),
0245     },
0246 
0247     /* Back End DAI links */
0248     {
0249         /* SSP0 - Codec */
0250         .name = "Codec",
0251         .id = 0,
0252         .nonatomic = 1,
0253         .no_pcm = 1,
0254         .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF |
0255             SND_SOC_DAIFMT_CBC_CFC,
0256         .ignore_pmdown_time = 1,
0257         .be_hw_params_fixup = broadwell_ssp0_fixup,
0258         .ops = &bdw_rt5650_ops,
0259         .dpcm_playback = 1,
0260         .dpcm_capture = 1,
0261         .init = bdw_rt5650_init,
0262         SND_SOC_DAILINK_REG(ssp0_port, be, platform),
0263     },
0264 };
0265 
0266 /* use space before codec name to simplify card ID, and simplify driver name */
0267 #define SOF_CARD_NAME "bdw rt5650" /* card name will be 'sof-bdw rt5650' */
0268 #define SOF_DRIVER_NAME "SOF"
0269 
0270 #define CARD_NAME "bdw-rt5650"
0271 #define DRIVER_NAME NULL /* card name will be used for driver name */
0272 
0273 /* ASoC machine driver for Broadwell DSP + RT5650 */
0274 static struct snd_soc_card bdw_rt5650_card = {
0275     .name = CARD_NAME,
0276     .driver_name = DRIVER_NAME,
0277     .owner = THIS_MODULE,
0278     .dai_link = bdw_rt5650_dais,
0279     .num_links = ARRAY_SIZE(bdw_rt5650_dais),
0280     .dapm_widgets = bdw_rt5650_widgets,
0281     .num_dapm_widgets = ARRAY_SIZE(bdw_rt5650_widgets),
0282     .dapm_routes = bdw_rt5650_map,
0283     .num_dapm_routes = ARRAY_SIZE(bdw_rt5650_map),
0284     .controls = bdw_rt5650_controls,
0285     .num_controls = ARRAY_SIZE(bdw_rt5650_controls),
0286     .fully_routed = true,
0287 };
0288 
0289 static int bdw_rt5650_probe(struct platform_device *pdev)
0290 {
0291     struct bdw_rt5650_priv *bdw_rt5650;
0292     struct snd_soc_acpi_mach *mach;
0293     int ret;
0294 
0295     bdw_rt5650_card.dev = &pdev->dev;
0296 
0297     /* Allocate driver private struct */
0298     bdw_rt5650 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5650_priv),
0299         GFP_KERNEL);
0300     if (!bdw_rt5650)
0301         return -ENOMEM;
0302 
0303     /* override platform name, if required */
0304     mach = pdev->dev.platform_data;
0305     ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt5650_card,
0306                             mach->mach_params.platform);
0307 
0308     if (ret)
0309         return ret;
0310 
0311     /* set card and driver name */
0312     if (snd_soc_acpi_sof_parent(&pdev->dev)) {
0313         bdw_rt5650_card.name = SOF_CARD_NAME;
0314         bdw_rt5650_card.driver_name = SOF_DRIVER_NAME;
0315     } else {
0316         bdw_rt5650_card.name = CARD_NAME;
0317         bdw_rt5650_card.driver_name = DRIVER_NAME;
0318     }
0319 
0320     snd_soc_card_set_drvdata(&bdw_rt5650_card, bdw_rt5650);
0321 
0322     return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5650_card);
0323 }
0324 
0325 static struct platform_driver bdw_rt5650_audio = {
0326     .probe = bdw_rt5650_probe,
0327     .driver = {
0328         .name = "bdw-rt5650",
0329         .pm = &snd_soc_pm_ops,
0330     },
0331 };
0332 
0333 module_platform_driver(bdw_rt5650_audio)
0334 
0335 /* Module information */
0336 MODULE_AUTHOR("Ben Zhang <benzh@chromium.org>");
0337 MODULE_DESCRIPTION("Intel Broadwell RT5650 machine driver");
0338 MODULE_LICENSE("GPL v2");
0339 MODULE_ALIAS("platform:bdw-rt5650");