1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright(c) 2019 Intel Corporation. 3 4 /* 5 * Intel Cometlake I2S Machine driver for RT1011 + RT5682 codec 6 */ 7 8 #include <linux/input.h> 9 #include <linux/module.h> 10 #include <linux/platform_device.h> 11 #include <linux/clk.h> 12 #include <linux/dmi.h> 13 #include <linux/slab.h> 14 #include <asm/cpu_device_id.h> 15 #include <linux/acpi.h> 16 #include <sound/core.h> 17 #include <sound/jack.h> 18 #include <sound/pcm.h> 19 #include <sound/pcm_params.h> 20 #include <sound/soc.h> 21 #include <sound/rt5682.h> 22 #include <sound/soc-acpi.h> 23 #include "../../codecs/rt1011.h" 24 #include "../../codecs/rt5682.h" 25 #include "../../codecs/hdac_hdmi.h" 26 #include "hda_dsp_common.h" 27 28 /* The platform clock outputs 24Mhz clock to codec as I2S MCLK */ 29 #define CML_PLAT_CLK 24000000 30 #define CML_RT1011_CODEC_DAI "rt1011-aif" 31 #define CML_RT5682_CODEC_DAI "rt5682-aif1" 32 #define NAME_SIZE 32 33 34 static struct snd_soc_jack hdmi_jack[3]; 35 36 struct hdmi_pcm { 37 struct list_head head; 38 struct snd_soc_dai *codec_dai; 39 int device; 40 }; 41 42 struct card_private { 43 char codec_name[SND_ACPI_I2C_ID_LEN]; 44 struct snd_soc_jack headset; 45 struct list_head hdmi_pcm_list; 46 bool common_hdmi_codec_drv; 47 }; 48 49 static const struct snd_kcontrol_new cml_controls[] = { 50 SOC_DAPM_PIN_SWITCH("Headphone Jack"), 51 SOC_DAPM_PIN_SWITCH("Headset Mic"), 52 SOC_DAPM_PIN_SWITCH("TL Ext Spk"), 53 SOC_DAPM_PIN_SWITCH("TR Ext Spk"), 54 SOC_DAPM_PIN_SWITCH("WL Ext Spk"), 55 SOC_DAPM_PIN_SWITCH("WR Ext Spk"), 56 }; 57 58 static const struct snd_soc_dapm_widget cml_rt1011_rt5682_widgets[] = { 59 SND_SOC_DAPM_SPK("TL Ext Spk", NULL), 60 SND_SOC_DAPM_SPK("TR Ext Spk", NULL), 61 SND_SOC_DAPM_SPK("WL Ext Spk", NULL), 62 SND_SOC_DAPM_SPK("WR Ext Spk", NULL), 63 SND_SOC_DAPM_HP("Headphone Jack", NULL), 64 SND_SOC_DAPM_MIC("Headset Mic", NULL), 65 SND_SOC_DAPM_MIC("SoC DMIC", NULL), 66 }; 67 68 static const struct snd_soc_dapm_route cml_rt1011_rt5682_map[] = { 69 /*speaker*/ 70 {"TL Ext Spk", NULL, "TL SPO"}, 71 {"TR Ext Spk", NULL, "TR SPO"}, 72 {"WL Ext Spk", NULL, "WL SPO"}, 73 {"WR Ext Spk", NULL, "WR SPO"}, 74 75 /* HP jack connectors - unknown if we have jack detection */ 76 { "Headphone Jack", NULL, "HPOL" }, 77 { "Headphone Jack", NULL, "HPOR" }, 78 79 /* other jacks */ 80 { "IN1P", NULL, "Headset Mic" }, 81 82 /* DMIC */ 83 {"DMic", NULL, "SoC DMIC"}, 84 }; 85 86 static int cml_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd) 87 { 88 struct card_private *ctx = snd_soc_card_get_drvdata(rtd->card); 89 struct snd_soc_component *component = rtd->codec_dai->component; 90 struct snd_soc_jack *jack; 91 int ret; 92 93 /* need to enable ASRC function for 24MHz mclk rate */ 94 rt5682_sel_asrc_clk_src(component, RT5682_DA_STEREO1_FILTER | 95 RT5682_AD_STEREO1_FILTER, 96 RT5682_CLK_SEL_I2S1_ASRC); 97 98 /* 99 * Headset buttons map to the google Reference headset. 100 * These can be configured by userspace. 101 */ 102 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack", 103 SND_JACK_HEADSET | SND_JACK_BTN_0 | 104 SND_JACK_BTN_1 | SND_JACK_BTN_2 | 105 SND_JACK_BTN_3, 106 &ctx->headset, NULL, 0); 107 if (ret) { 108 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret); 109 return ret; 110 } 111 112 jack = &ctx->headset; 113 114 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 115 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); 116 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP); 117 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); 118 ret = snd_soc_component_set_jack(component, jack, NULL); 119 if (ret) 120 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret); 121 122 return ret; 123 }; 124 125 static int cml_rt5682_hw_params(struct snd_pcm_substream *substream, 126 struct snd_pcm_hw_params *params) 127 { 128 struct snd_soc_pcm_runtime *rtd = substream->private_data; 129 struct snd_soc_dai *codec_dai = rtd->codec_dai; 130 int clk_id, clk_freq, pll_out, ret; 131 132 clk_id = RT5682_PLL1_S_MCLK; 133 clk_freq = CML_PLAT_CLK; 134 135 pll_out = params_rate(params) * 512; 136 137 ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out); 138 if (ret < 0) 139 dev_warn(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret); 140 141 /* Configure sysclk for codec */ 142 ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1, 143 pll_out, SND_SOC_CLOCK_IN); 144 if (ret < 0) 145 dev_warn(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret); 146 147 /* 148 * slot_width should be equal or large than data length, set them 149 * be the same 150 */ 151 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2, 152 params_width(params)); 153 if (ret < 0) 154 dev_warn(rtd->dev, "set TDM slot err:%d\n", ret); 155 return ret; 156 } 157 158 static int cml_rt1011_hw_params(struct snd_pcm_substream *substream, 159 struct snd_pcm_hw_params *params) 160 { 161 struct snd_soc_pcm_runtime *rtd = substream->private_data; 162 struct snd_soc_dai *codec_dai; 163 struct snd_soc_card *card = rtd->card; 164 int srate, i, ret = 0; 165 166 srate = params_rate(params); 167 168 for (i = 0; i < rtd->num_codecs; i++) { 169 codec_dai = rtd->codec_dais[i]; 170 171 /* 100 Fs to drive 24 bit data */ 172 ret = snd_soc_dai_set_pll(codec_dai, 0, RT1011_PLL1_S_BCLK, 173 100 * srate, 256 * srate); 174 if (ret < 0) { 175 dev_err(card->dev, "codec_dai clock not set\n"); 176 return ret; 177 } 178 179 ret = snd_soc_dai_set_sysclk(codec_dai, 180 RT1011_FS_SYS_PRE_S_PLL1, 181 256 * srate, SND_SOC_CLOCK_IN); 182 if (ret < 0) { 183 dev_err(card->dev, "codec_dai clock not set\n"); 184 return ret; 185 } 186 187 /* 188 * Codec TDM is configured as 24 bit capture/ playback. 189 * 2 CH PB is done over 4 codecs - 2 Woofers and 2 Tweeters. 190 * The Left woofer and tweeter plays the Left playback data 191 * and similar by the Right. 192 * Hence 2 codecs (1 T and 1 W pair) share same Rx slot. 193 * The feedback is captured for each codec individually. 194 * Hence all 4 codecs use 1 Tx slot each for feedback. 195 */ 196 if (!strcmp(codec_dai->component->name, "i2c-10EC1011:00")) { 197 ret = snd_soc_dai_set_tdm_slot(codec_dai, 198 0x4, 0x1, 4, 24); 199 if (ret < 0) 200 break; 201 } 202 if (!strcmp(codec_dai->component->name, "i2c-10EC1011:02")) { 203 ret = snd_soc_dai_set_tdm_slot(codec_dai, 204 0x1, 0x1, 4, 24); 205 if (ret < 0) 206 break; 207 } 208 /* TDM Rx slot 2 is used for Right Woofer & Tweeters pair */ 209 if (!strcmp(codec_dai->component->name, "i2c-10EC1011:01")) { 210 ret = snd_soc_dai_set_tdm_slot(codec_dai, 211 0x8, 0x2, 4, 24); 212 if (ret < 0) 213 break; 214 } 215 if (!strcmp(codec_dai->component->name, "i2c-10EC1011:03")) { 216 ret = snd_soc_dai_set_tdm_slot(codec_dai, 217 0x2, 0x2, 4, 24); 218 if (ret < 0) 219 break; 220 } 221 } 222 if (ret < 0) 223 dev_err(rtd->dev, 224 "set codec TDM slot for %s failed with error %d\n", 225 codec_dai->component->name, ret); 226 return ret; 227 } 228 229 static struct snd_soc_ops cml_rt5682_ops = { 230 .hw_params = cml_rt5682_hw_params, 231 }; 232 233 static const struct snd_soc_ops cml_rt1011_ops = { 234 .hw_params = cml_rt1011_hw_params, 235 }; 236 237 static int sof_card_late_probe(struct snd_soc_card *card) 238 { 239 struct card_private *ctx = snd_soc_card_get_drvdata(card); 240 struct snd_soc_component *component = NULL; 241 char jack_name[NAME_SIZE]; 242 struct hdmi_pcm *pcm; 243 int ret, i = 0; 244 245 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct hdmi_pcm, 246 head); 247 component = pcm->codec_dai->component; 248 249 if (ctx->common_hdmi_codec_drv) 250 return hda_dsp_hdmi_build_controls(card, component); 251 252 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) { 253 component = pcm->codec_dai->component; 254 snprintf(jack_name, sizeof(jack_name), 255 "HDMI/DP, pcm=%d Jack", pcm->device); 256 ret = snd_soc_card_jack_new(card, jack_name, 257 SND_JACK_AVOUT, &hdmi_jack[i], 258 NULL, 0); 259 if (ret) 260 return ret; 261 262 ret = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device, 263 &hdmi_jack[i]); 264 if (ret < 0) 265 return ret; 266 267 i++; 268 } 269 if (!component) 270 return -EINVAL; 271 272 return hdac_hdmi_jack_port_init(component, &card->dapm); 273 } 274 275 static int hdmi_init(struct snd_soc_pcm_runtime *rtd) 276 { 277 struct card_private *ctx = snd_soc_card_get_drvdata(rtd->card); 278 struct snd_soc_dai *dai = rtd->codec_dai; 279 struct hdmi_pcm *pcm; 280 281 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL); 282 if (!pcm) 283 return -ENOMEM; 284 285 pcm->device = dai->id; 286 pcm->codec_dai = dai; 287 288 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list); 289 290 return 0; 291 } 292 293 /* Cometlake digital audio interface glue - connects codec <--> CPU */ 294 295 SND_SOC_DAILINK_DEF(ssp0_pin, 296 DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin"))); 297 SND_SOC_DAILINK_DEF(ssp0_codec, 298 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5682:00", 299 CML_RT5682_CODEC_DAI))); 300 301 SND_SOC_DAILINK_DEF(ssp1_pin, 302 DAILINK_COMP_ARRAY(COMP_CPU("SSP1 Pin"))); 303 SND_SOC_DAILINK_DEF(ssp1_codec, 304 DAILINK_COMP_ARRAY( 305 /* WL */ COMP_CODEC("i2c-10EC1011:00", CML_RT1011_CODEC_DAI), 306 /* WR */ COMP_CODEC("i2c-10EC1011:01", CML_RT1011_CODEC_DAI), 307 /* TL */ COMP_CODEC("i2c-10EC1011:02", CML_RT1011_CODEC_DAI), 308 /* TR */ COMP_CODEC("i2c-10EC1011:03", CML_RT1011_CODEC_DAI))); 309 310 SND_SOC_DAILINK_DEF(dmic_pin, 311 DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin"))); 312 313 SND_SOC_DAILINK_DEF(dmic16k_pin, 314 DAILINK_COMP_ARRAY(COMP_CPU("DMIC16k Pin"))); 315 316 SND_SOC_DAILINK_DEF(dmic_codec, 317 DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi"))); 318 319 SND_SOC_DAILINK_DEF(idisp1_pin, 320 DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin"))); 321 SND_SOC_DAILINK_DEF(idisp1_codec, 322 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1"))); 323 324 SND_SOC_DAILINK_DEF(idisp2_pin, 325 DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin"))); 326 SND_SOC_DAILINK_DEF(idisp2_codec, 327 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2"))); 328 329 SND_SOC_DAILINK_DEF(idisp3_pin, 330 DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin"))); 331 SND_SOC_DAILINK_DEF(idisp3_codec, 332 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3"))); 333 334 SND_SOC_DAILINK_DEF(platform, 335 DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3"))); 336 337 static struct snd_soc_dai_link cml_rt1011_rt5682_dailink[] = { 338 /* Back End DAI links */ 339 { 340 /* SSP0 - Codec */ 341 .name = "SSP0-Codec", 342 .id = 0, 343 .init = cml_rt5682_codec_init, 344 .ignore_pmdown_time = 1, 345 .ops = &cml_rt5682_ops, 346 .dpcm_playback = 1, 347 .dpcm_capture = 1, 348 .no_pcm = 1, 349 SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform), 350 }, 351 { 352 .name = "dmic01", 353 .id = 1, 354 .ignore_suspend = 1, 355 .dpcm_capture = 1, 356 .no_pcm = 1, 357 SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform), 358 }, 359 { 360 .name = "dmic16k", 361 .id = 2, 362 .ignore_suspend = 1, 363 .dpcm_capture = 1, 364 .no_pcm = 1, 365 SND_SOC_DAILINK_REG(dmic16k_pin, dmic_codec, platform), 366 }, 367 { 368 .name = "iDisp1", 369 .id = 3, 370 .init = hdmi_init, 371 .dpcm_playback = 1, 372 .no_pcm = 1, 373 SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform), 374 }, 375 { 376 .name = "iDisp2", 377 .id = 4, 378 .init = hdmi_init, 379 .dpcm_playback = 1, 380 .no_pcm = 1, 381 SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform), 382 }, 383 { 384 .name = "iDisp3", 385 .id = 5, 386 .init = hdmi_init, 387 .dpcm_playback = 1, 388 .no_pcm = 1, 389 SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform), 390 }, 391 { 392 /* 393 * SSP1 - Codec : added to end of list ensuring 394 * reuse of common topologies for other end points 395 * and changing only SSP1's codec 396 */ 397 .name = "SSP1-Codec", 398 .id = 6, 399 .dpcm_playback = 1, 400 .dpcm_capture = 1, /* Capture stream provides Feedback */ 401 .no_pcm = 1, 402 .ops = &cml_rt1011_ops, 403 SND_SOC_DAILINK_REG(ssp1_pin, ssp1_codec, platform), 404 }, 405 }; 406 407 static struct snd_soc_codec_conf rt1011_conf[] = { 408 { 409 .dev_name = "i2c-10EC1011:00", 410 .name_prefix = "WL", 411 }, 412 { 413 .dev_name = "i2c-10EC1011:01", 414 .name_prefix = "WR", 415 }, 416 { 417 .dev_name = "i2c-10EC1011:02", 418 .name_prefix = "TL", 419 }, 420 { 421 .dev_name = "i2c-10EC1011:03", 422 .name_prefix = "TR", 423 }, 424 }; 425 426 /* Cometlake audio machine driver for RT1011 and RT5682 */ 427 static struct snd_soc_card snd_soc_card_cml = { 428 .name = "cml_rt1011_rt5682", 429 .dai_link = cml_rt1011_rt5682_dailink, 430 .num_links = ARRAY_SIZE(cml_rt1011_rt5682_dailink), 431 .codec_conf = rt1011_conf, 432 .num_configs = ARRAY_SIZE(rt1011_conf), 433 .dapm_widgets = cml_rt1011_rt5682_widgets, 434 .num_dapm_widgets = ARRAY_SIZE(cml_rt1011_rt5682_widgets), 435 .dapm_routes = cml_rt1011_rt5682_map, 436 .num_dapm_routes = ARRAY_SIZE(cml_rt1011_rt5682_map), 437 .controls = cml_controls, 438 .num_controls = ARRAY_SIZE(cml_controls), 439 .fully_routed = true, 440 .late_probe = sof_card_late_probe, 441 }; 442 443 static int snd_cml_rt1011_probe(struct platform_device *pdev) 444 { 445 struct card_private *ctx; 446 struct snd_soc_acpi_mach *mach; 447 const char *platform_name; 448 int ret; 449 450 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_ATOMIC); 451 if (!ctx) 452 return -ENOMEM; 453 454 INIT_LIST_HEAD(&ctx->hdmi_pcm_list); 455 mach = (&pdev->dev)->platform_data; 456 snd_soc_card_cml.dev = &pdev->dev; 457 platform_name = mach->mach_params.platform; 458 459 /* set platform name for each dailink */ 460 ret = snd_soc_fixup_dai_links_platform_name(&snd_soc_card_cml, 461 platform_name); 462 if (ret) 463 return ret; 464 465 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv; 466 467 snd_soc_card_set_drvdata(&snd_soc_card_cml, ctx); 468 469 return devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cml); 470 } 471 472 static struct platform_driver snd_cml_rt1011_rt5682_driver = { 473 .probe = snd_cml_rt1011_probe, 474 .driver = { 475 .name = "cml_rt1011_rt5682", 476 .pm = &snd_soc_pm_ops, 477 }, 478 }; 479 module_platform_driver(snd_cml_rt1011_rt5682_driver); 480 481 /* Module information */ 482 MODULE_DESCRIPTION("Cometlake Audio Machine driver - RT1011 and RT5682 in I2S mode"); 483 MODULE_AUTHOR("Naveen Manohar <naveen.m@intel.com>"); 484 MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>"); 485 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>"); 486 MODULE_LICENSE("GPL v2"); 487 MODULE_ALIAS("platform:cml_rt1011_rt5682"); 488