1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ASoC machine driver for Intel Broadwell platforms with RT5650 codec 4 * 5 * Copyright 2019, The Chromium OS Authors. All rights reserved. 6 */ 7 8 #include <linux/delay.h> 9 #include <linux/gpio/consumer.h> 10 #include <linux/module.h> 11 #include <linux/platform_device.h> 12 #include <sound/core.h> 13 #include <sound/jack.h> 14 #include <sound/pcm.h> 15 #include <sound/pcm_params.h> 16 #include <sound/soc.h> 17 #include <sound/soc-acpi.h> 18 19 #include "../common/sst-dsp.h" 20 #include "../haswell/sst-haswell-ipc.h" 21 22 #include "../../codecs/rt5645.h" 23 24 struct bdw_rt5650_priv { 25 struct gpio_desc *gpio_hp_en; 26 struct snd_soc_component *component; 27 }; 28 29 static const struct snd_soc_dapm_widget bdw_rt5650_widgets[] = { 30 SND_SOC_DAPM_HP("Headphone", NULL), 31 SND_SOC_DAPM_SPK("Speaker", NULL), 32 SND_SOC_DAPM_MIC("Headset Mic", NULL), 33 SND_SOC_DAPM_MIC("DMIC Pair1", NULL), 34 SND_SOC_DAPM_MIC("DMIC Pair2", NULL), 35 }; 36 37 static const struct snd_soc_dapm_route bdw_rt5650_map[] = { 38 /* Speakers */ 39 {"Speaker", NULL, "SPOL"}, 40 {"Speaker", NULL, "SPOR"}, 41 42 /* Headset jack connectors */ 43 {"Headphone", NULL, "HPOL"}, 44 {"Headphone", NULL, "HPOR"}, 45 {"IN1P", NULL, "Headset Mic"}, 46 {"IN1N", NULL, "Headset Mic"}, 47 48 /* Digital MICs 49 * DMIC Pair1 are the two DMICs connected on the DMICN1 connector. 50 * DMIC Pair2 are the two DMICs connected on the DMICN2 connector. 51 * Facing the camera, DMIC Pair1 are on the left side, DMIC Pair2 52 * are on the right side. 53 */ 54 {"DMIC L1", NULL, "DMIC Pair1"}, 55 {"DMIC R1", NULL, "DMIC Pair1"}, 56 {"DMIC L2", NULL, "DMIC Pair2"}, 57 {"DMIC R2", NULL, "DMIC Pair2"}, 58 59 /* CODEC BE connections */ 60 {"SSP0 CODEC IN", NULL, "AIF1 Capture"}, 61 {"AIF1 Playback", NULL, "SSP0 CODEC OUT"}, 62 }; 63 64 static const struct snd_kcontrol_new bdw_rt5650_controls[] = { 65 SOC_DAPM_PIN_SWITCH("Speaker"), 66 SOC_DAPM_PIN_SWITCH("Headphone"), 67 SOC_DAPM_PIN_SWITCH("Headset Mic"), 68 SOC_DAPM_PIN_SWITCH("DMIC Pair1"), 69 SOC_DAPM_PIN_SWITCH("DMIC Pair2"), 70 }; 71 72 73 static struct snd_soc_jack headphone_jack; 74 static struct snd_soc_jack mic_jack; 75 76 static struct snd_soc_jack_pin headphone_jack_pin = { 77 .pin = "Headphone", 78 .mask = SND_JACK_HEADPHONE, 79 }; 80 81 static struct snd_soc_jack_pin mic_jack_pin = { 82 .pin = "Headset Mic", 83 .mask = SND_JACK_MICROPHONE, 84 }; 85 86 static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd, 87 struct snd_pcm_hw_params *params) 88 { 89 struct snd_interval *rate = hw_param_interval(params, 90 SNDRV_PCM_HW_PARAM_RATE); 91 struct snd_interval *channels = hw_param_interval(params, 92 SNDRV_PCM_HW_PARAM_CHANNELS); 93 94 /* The ADSP will covert the FE rate to 48k, max 4-channels */ 95 rate->min = rate->max = 48000; 96 channels->min = 2; 97 channels->max = 4; 98 99 /* set SSP0 to 24 bit */ 100 snd_mask_set_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), 101 SNDRV_PCM_FORMAT_S24_LE); 102 103 return 0; 104 } 105 106 static int bdw_rt5650_hw_params(struct snd_pcm_substream *substream, 107 struct snd_pcm_hw_params *params) 108 { 109 struct snd_soc_pcm_runtime *rtd = substream->private_data; 110 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 111 int ret; 112 113 /* Workaround: set codec PLL to 19.2MHz that PLL source is 114 * from MCLK(24MHz) to conform 2.4MHz DMIC clock. 115 */ 116 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK, 117 24000000, 19200000); 118 if (ret < 0) { 119 dev_err(rtd->dev, "can't set codec pll: %d\n", ret); 120 return ret; 121 } 122 123 /* The actual MCLK freq is 24MHz. The codec is told that MCLK is 124 * 24.576MHz to satisfy the requirement of rl6231_get_clk_info. 125 * ASRC is enabled on AD and DA filters to ensure good audio quality. 126 */ 127 ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1, 24576000, 128 SND_SOC_CLOCK_IN); 129 if (ret < 0) { 130 dev_err(rtd->dev, "can't set codec sysclk configuration\n"); 131 return ret; 132 } 133 134 return ret; 135 } 136 137 static struct snd_soc_ops bdw_rt5650_ops = { 138 .hw_params = bdw_rt5650_hw_params, 139 }; 140 141 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL) 142 static int bdw_rt5650_rtd_init(struct snd_soc_pcm_runtime *rtd) 143 { 144 struct snd_soc_component *component = 145 snd_soc_rtdcom_lookup(rtd, DRV_NAME); 146 struct sst_pdata *pdata = dev_get_platdata(component->dev); 147 struct sst_hsw *broadwell = pdata->dsp; 148 int ret; 149 150 /* Set ADSP SSP port settings 151 * clock_divider = 4 means BCLK = MCLK/5 = 24MHz/5 = 4.8MHz 152 */ 153 ret = sst_hsw_device_set_config(broadwell, SST_HSW_DEVICE_SSP_0, 154 SST_HSW_DEVICE_MCLK_FREQ_24_MHZ, 155 SST_HSW_DEVICE_TDM_CLOCK_MASTER, 4); 156 if (ret < 0) { 157 dev_err(rtd->dev, "error: failed to set device config\n"); 158 return ret; 159 } 160 161 return 0; 162 } 163 #endif 164 165 static const unsigned int channels[] = { 166 2, 4, 167 }; 168 169 static const struct snd_pcm_hw_constraint_list constraints_channels = { 170 .count = ARRAY_SIZE(channels), 171 .list = channels, 172 .mask = 0, 173 }; 174 175 static int bdw_rt5650_fe_startup(struct snd_pcm_substream *substream) 176 { 177 struct snd_pcm_runtime *runtime = substream->runtime; 178 179 /* Board supports stereo and quad configurations for capture */ 180 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE) 181 return 0; 182 183 runtime->hw.channels_max = 4; 184 return snd_pcm_hw_constraint_list(runtime, 0, 185 SNDRV_PCM_HW_PARAM_CHANNELS, 186 &constraints_channels); 187 } 188 189 static const struct snd_soc_ops bdw_rt5650_fe_ops = { 190 .startup = bdw_rt5650_fe_startup, 191 }; 192 193 static int bdw_rt5650_init(struct snd_soc_pcm_runtime *rtd) 194 { 195 struct bdw_rt5650_priv *bdw_rt5650 = 196 snd_soc_card_get_drvdata(rtd->card); 197 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 198 struct snd_soc_component *component = codec_dai->component; 199 int ret; 200 201 /* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1. 202 * The ASRC clock source is clk_i2s1_asrc. 203 */ 204 rt5645_sel_asrc_clk_src(component, 205 RT5645_DA_STEREO_FILTER | 206 RT5645_DA_MONO_L_FILTER | 207 RT5645_DA_MONO_R_FILTER | 208 RT5645_AD_STEREO_FILTER | 209 RT5645_AD_MONO_L_FILTER | 210 RT5645_AD_MONO_R_FILTER, 211 RT5645_CLK_SEL_I2S1_ASRC); 212 213 /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */ 214 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24); 215 216 if (ret < 0) { 217 dev_err(rtd->dev, "can't set codec TDM slot %d\n", ret); 218 return ret; 219 } 220 221 /* Create and initialize headphone jack */ 222 if (snd_soc_card_jack_new(rtd->card, "Headphone Jack", 223 SND_JACK_HEADPHONE, &headphone_jack, 224 &headphone_jack_pin, 1)) { 225 dev_err(component->dev, "Can't create headphone jack\n"); 226 } 227 228 /* Create and initialize mic jack */ 229 if (snd_soc_card_jack_new(rtd->card, "Mic Jack", SND_JACK_MICROPHONE, 230 &mic_jack, &mic_jack_pin, 1)) { 231 dev_err(component->dev, "Can't create mic jack\n"); 232 } 233 234 rt5645_set_jack_detect(component, &headphone_jack, &mic_jack, NULL); 235 236 bdw_rt5650->component = component; 237 238 return 0; 239 } 240 241 /* broadwell digital audio interface glue - connects codec <--> CPU */ 242 SND_SOC_DAILINK_DEF(dummy, 243 DAILINK_COMP_ARRAY(COMP_DUMMY())); 244 245 SND_SOC_DAILINK_DEF(fe, 246 DAILINK_COMP_ARRAY(COMP_CPU("System Pin"))); 247 248 SND_SOC_DAILINK_DEF(platform, 249 DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio"))); 250 251 SND_SOC_DAILINK_DEF(be, 252 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5650:00", "rt5645-aif1"))); 253 254 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL) 255 SND_SOC_DAILINK_DEF(ssp0_port, 256 DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port"))); 257 #endif 258 259 static struct snd_soc_dai_link bdw_rt5650_dais[] = { 260 /* Front End DAI links */ 261 { 262 .name = "System PCM", 263 .stream_name = "System Playback", 264 .dynamic = 1, 265 .ops = &bdw_rt5650_fe_ops, 266 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL) 267 .init = bdw_rt5650_rtd_init, 268 #endif 269 .trigger = { 270 SND_SOC_DPCM_TRIGGER_POST, 271 SND_SOC_DPCM_TRIGGER_POST 272 }, 273 .dpcm_playback = 1, 274 .dpcm_capture = 1, 275 SND_SOC_DAILINK_REG(fe, dummy, platform), 276 }, 277 278 /* Back End DAI links */ 279 { 280 /* SSP0 - Codec */ 281 .name = "Codec", 282 .id = 0, 283 .no_pcm = 1, 284 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF | 285 SND_SOC_DAIFMT_CBS_CFS, 286 .ignore_pmdown_time = 1, 287 .be_hw_params_fixup = broadwell_ssp0_fixup, 288 .ops = &bdw_rt5650_ops, 289 .dpcm_playback = 1, 290 .dpcm_capture = 1, 291 .init = bdw_rt5650_init, 292 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL) 293 SND_SOC_DAILINK_REG(dummy, be, dummy), 294 #else 295 SND_SOC_DAILINK_REG(ssp0_port, be, platform), 296 #endif 297 }, 298 }; 299 300 /* ASoC machine driver for Broadwell DSP + RT5650 */ 301 static struct snd_soc_card bdw_rt5650_card = { 302 .name = "bdw-rt5650", 303 .owner = THIS_MODULE, 304 .dai_link = bdw_rt5650_dais, 305 .num_links = ARRAY_SIZE(bdw_rt5650_dais), 306 .dapm_widgets = bdw_rt5650_widgets, 307 .num_dapm_widgets = ARRAY_SIZE(bdw_rt5650_widgets), 308 .dapm_routes = bdw_rt5650_map, 309 .num_dapm_routes = ARRAY_SIZE(bdw_rt5650_map), 310 .controls = bdw_rt5650_controls, 311 .num_controls = ARRAY_SIZE(bdw_rt5650_controls), 312 .fully_routed = true, 313 }; 314 315 static int bdw_rt5650_probe(struct platform_device *pdev) 316 { 317 struct bdw_rt5650_priv *bdw_rt5650; 318 struct snd_soc_acpi_mach *mach; 319 int ret; 320 321 bdw_rt5650_card.dev = &pdev->dev; 322 323 /* Allocate driver private struct */ 324 bdw_rt5650 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5650_priv), 325 GFP_KERNEL); 326 if (!bdw_rt5650) 327 return -ENOMEM; 328 329 /* override plaform name, if required */ 330 mach = pdev->dev.platform_data; 331 ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt5650_card, 332 mach->mach_params.platform); 333 334 if (ret) 335 return ret; 336 337 snd_soc_card_set_drvdata(&bdw_rt5650_card, bdw_rt5650); 338 339 return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5650_card); 340 } 341 342 static struct platform_driver bdw_rt5650_audio = { 343 .probe = bdw_rt5650_probe, 344 .driver = { 345 .name = "bdw-rt5650", 346 .pm = &snd_soc_pm_ops, 347 }, 348 }; 349 350 module_platform_driver(bdw_rt5650_audio) 351 352 /* Module information */ 353 MODULE_AUTHOR("Ben Zhang <benzh@chromium.org>"); 354 MODULE_DESCRIPTION("Intel Broadwell RT5650 machine driver"); 355 MODULE_LICENSE("GPL v2"); 356 MODULE_ALIAS("platform:bdw-rt5650"); 357