1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright(c) 2019-2020 Intel Corporation. 3 4 /* 5 * Intel SOF Machine Driver with Realtek rt5682 Codec 6 * and speaker codec MAX98357A or RT1015. 7 */ 8 #include <linux/i2c.h> 9 #include <linux/input.h> 10 #include <linux/module.h> 11 #include <linux/platform_device.h> 12 #include <linux/clk.h> 13 #include <linux/dmi.h> 14 #include <sound/core.h> 15 #include <sound/jack.h> 16 #include <sound/pcm.h> 17 #include <sound/pcm_params.h> 18 #include <sound/soc.h> 19 #include <sound/sof.h> 20 #include <sound/rt5682.h> 21 #include <sound/rt5682s.h> 22 #include <sound/soc-acpi.h> 23 #include "../../codecs/rt5682.h" 24 #include "../../codecs/rt5682s.h" 25 #include "../../codecs/hdac_hdmi.h" 26 #include "../common/soc-intel-quirks.h" 27 #include "hda_dsp_common.h" 28 #include "sof_maxim_common.h" 29 #include "sof_realtek_common.h" 30 31 #define NAME_SIZE 32 32 33 #define SOF_RT5682_SSP_CODEC(quirk) ((quirk) & GENMASK(2, 0)) 34 #define SOF_RT5682_SSP_CODEC_MASK (GENMASK(2, 0)) 35 #define SOF_RT5682_MCLK_EN BIT(3) 36 #define SOF_RT5682_MCLK_24MHZ BIT(4) 37 #define SOF_SPEAKER_AMP_PRESENT BIT(5) 38 #define SOF_RT5682_SSP_AMP_SHIFT 6 39 #define SOF_RT5682_SSP_AMP_MASK (GENMASK(8, 6)) 40 #define SOF_RT5682_SSP_AMP(quirk) \ 41 (((quirk) << SOF_RT5682_SSP_AMP_SHIFT) & SOF_RT5682_SSP_AMP_MASK) 42 #define SOF_RT5682_MCLK_BYTCHT_EN BIT(9) 43 #define SOF_RT5682_NUM_HDMIDEV_SHIFT 10 44 #define SOF_RT5682_NUM_HDMIDEV_MASK (GENMASK(12, 10)) 45 #define SOF_RT5682_NUM_HDMIDEV(quirk) \ 46 ((quirk << SOF_RT5682_NUM_HDMIDEV_SHIFT) & SOF_RT5682_NUM_HDMIDEV_MASK) 47 #define SOF_RT1011_SPEAKER_AMP_PRESENT BIT(13) 48 #define SOF_RT1015_SPEAKER_AMP_PRESENT BIT(14) 49 #define SOF_RT1015_SPEAKER_AMP_100FS BIT(15) 50 #define SOF_RT1015P_SPEAKER_AMP_PRESENT BIT(16) 51 #define SOF_MAX98373_SPEAKER_AMP_PRESENT BIT(17) 52 #define SOF_MAX98360A_SPEAKER_AMP_PRESENT BIT(18) 53 54 /* BT audio offload: reserve 3 bits for future */ 55 #define SOF_BT_OFFLOAD_SSP_SHIFT 19 56 #define SOF_BT_OFFLOAD_SSP_MASK (GENMASK(21, 19)) 57 #define SOF_BT_OFFLOAD_SSP(quirk) \ 58 (((quirk) << SOF_BT_OFFLOAD_SSP_SHIFT) & SOF_BT_OFFLOAD_SSP_MASK) 59 #define SOF_SSP_BT_OFFLOAD_PRESENT BIT(22) 60 #define SOF_RT5682S_HEADPHONE_CODEC_PRESENT BIT(23) 61 #define SOF_MAX98390_SPEAKER_AMP_PRESENT BIT(24) 62 #define SOF_MAX98390_TWEETER_SPEAKER_PRESENT BIT(25) 63 64 65 /* Default: MCLK on, MCLK 19.2M, SSP0 */ 66 static unsigned long sof_rt5682_quirk = SOF_RT5682_MCLK_EN | 67 SOF_RT5682_SSP_CODEC(0); 68 69 static int is_legacy_cpu; 70 71 static struct snd_soc_jack sof_hdmi[3]; 72 73 struct sof_hdmi_pcm { 74 struct list_head head; 75 struct snd_soc_dai *codec_dai; 76 int device; 77 }; 78 79 struct sof_card_private { 80 struct clk *mclk; 81 struct snd_soc_jack sof_headset; 82 struct list_head hdmi_pcm_list; 83 bool common_hdmi_codec_drv; 84 bool idisp_codec; 85 }; 86 87 static int sof_rt5682_quirk_cb(const struct dmi_system_id *id) 88 { 89 sof_rt5682_quirk = (unsigned long)id->driver_data; 90 return 1; 91 } 92 93 static const struct dmi_system_id sof_rt5682_quirk_table[] = { 94 { 95 .callback = sof_rt5682_quirk_cb, 96 .matches = { 97 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"), 98 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max"), 99 }, 100 .driver_data = (void *)(SOF_RT5682_SSP_CODEC(2)), 101 }, 102 { 103 .callback = sof_rt5682_quirk_cb, 104 .matches = { 105 DMI_MATCH(DMI_SYS_VENDOR, "AAEON"), 106 DMI_MATCH(DMI_PRODUCT_NAME, "UP-CHT01"), 107 }, 108 .driver_data = (void *)(SOF_RT5682_SSP_CODEC(2)), 109 }, 110 { 111 .callback = sof_rt5682_quirk_cb, 112 .matches = { 113 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), 114 DMI_MATCH(DMI_PRODUCT_NAME, "WhiskeyLake Client"), 115 }, 116 .driver_data = (void *)(SOF_RT5682_MCLK_EN | 117 SOF_RT5682_MCLK_24MHZ | 118 SOF_RT5682_SSP_CODEC(1)), 119 }, 120 { 121 /* 122 * Dooly is hatch family but using rt1015 amp so it 123 * requires a quirk before "Google_Hatch". 124 */ 125 .callback = sof_rt5682_quirk_cb, 126 .matches = { 127 DMI_MATCH(DMI_SYS_VENDOR, "HP"), 128 DMI_MATCH(DMI_PRODUCT_NAME, "Dooly"), 129 }, 130 .driver_data = (void *)(SOF_RT5682_MCLK_EN | 131 SOF_RT5682_MCLK_24MHZ | 132 SOF_RT5682_SSP_CODEC(0) | 133 SOF_SPEAKER_AMP_PRESENT | 134 SOF_RT1015_SPEAKER_AMP_PRESENT | 135 SOF_RT1015_SPEAKER_AMP_100FS | 136 SOF_RT5682_SSP_AMP(1)), 137 }, 138 { 139 .callback = sof_rt5682_quirk_cb, 140 .matches = { 141 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Hatch"), 142 }, 143 .driver_data = (void *)(SOF_RT5682_MCLK_EN | 144 SOF_RT5682_MCLK_24MHZ | 145 SOF_RT5682_SSP_CODEC(0) | 146 SOF_SPEAKER_AMP_PRESENT | 147 SOF_RT5682_SSP_AMP(1)), 148 }, 149 { 150 .callback = sof_rt5682_quirk_cb, 151 .matches = { 152 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), 153 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"), 154 }, 155 .driver_data = (void *)(SOF_RT5682_MCLK_EN | 156 SOF_RT5682_SSP_CODEC(0)), 157 }, 158 { 159 .callback = sof_rt5682_quirk_cb, 160 .matches = { 161 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Volteer"), 162 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98373_ALC5682I_I2S_UP4"), 163 }, 164 .driver_data = (void *)(SOF_RT5682_MCLK_EN | 165 SOF_RT5682_SSP_CODEC(0) | 166 SOF_SPEAKER_AMP_PRESENT | 167 SOF_MAX98373_SPEAKER_AMP_PRESENT | 168 SOF_RT5682_SSP_AMP(2) | 169 SOF_RT5682_NUM_HDMIDEV(4)), 170 }, 171 { 172 .callback = sof_rt5682_quirk_cb, 173 .matches = { 174 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), 175 DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"), 176 DMI_MATCH(DMI_OEM_STRING, "AUDIO-ADL_MAX98373_ALC5682I_I2S"), 177 }, 178 .driver_data = (void *)(SOF_RT5682_MCLK_EN | 179 SOF_RT5682_SSP_CODEC(0) | 180 SOF_SPEAKER_AMP_PRESENT | 181 SOF_MAX98373_SPEAKER_AMP_PRESENT | 182 SOF_RT5682_SSP_AMP(2) | 183 SOF_RT5682_NUM_HDMIDEV(4)), 184 }, 185 { 186 .callback = sof_rt5682_quirk_cb, 187 .matches = { 188 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"), 189 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98390_ALC5682I_I2S"), 190 }, 191 .driver_data = (void *)(SOF_RT5682_MCLK_EN | 192 SOF_RT5682_SSP_CODEC(0) | 193 SOF_SPEAKER_AMP_PRESENT | 194 SOF_MAX98390_SPEAKER_AMP_PRESENT | 195 SOF_RT5682_SSP_AMP(2) | 196 SOF_RT5682_NUM_HDMIDEV(4)), 197 }, 198 { 199 .callback = sof_rt5682_quirk_cb, 200 .matches = { 201 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"), 202 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98390_ALC5682I_I2S_4SPK"), 203 }, 204 .driver_data = (void *)(SOF_RT5682_MCLK_EN | 205 SOF_RT5682_SSP_CODEC(0) | 206 SOF_SPEAKER_AMP_PRESENT | 207 SOF_MAX98390_SPEAKER_AMP_PRESENT | 208 SOF_MAX98390_TWEETER_SPEAKER_PRESENT | 209 SOF_RT5682_SSP_AMP(1) | 210 SOF_RT5682_NUM_HDMIDEV(4) | 211 SOF_BT_OFFLOAD_SSP(2) | 212 SOF_SSP_BT_OFFLOAD_PRESENT), 213 214 }, 215 {} 216 }; 217 218 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd) 219 { 220 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card); 221 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0); 222 struct sof_hdmi_pcm *pcm; 223 224 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL); 225 if (!pcm) 226 return -ENOMEM; 227 228 /* dai_link id is 1:1 mapped to the PCM device */ 229 pcm->device = rtd->dai_link->id; 230 pcm->codec_dai = dai; 231 232 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list); 233 234 return 0; 235 } 236 237 static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd) 238 { 239 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card); 240 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component; 241 struct snd_soc_jack *jack; 242 int ret; 243 244 /* need to enable ASRC function for 24MHz mclk rate */ 245 if ((sof_rt5682_quirk & SOF_RT5682_MCLK_EN) && 246 (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)) { 247 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) 248 rt5682s_sel_asrc_clk_src(component, 249 RT5682S_DA_STEREO1_FILTER | 250 RT5682S_AD_STEREO1_FILTER, 251 RT5682S_CLK_SEL_I2S1_ASRC); 252 else 253 rt5682_sel_asrc_clk_src(component, 254 RT5682_DA_STEREO1_FILTER | 255 RT5682_AD_STEREO1_FILTER, 256 RT5682_CLK_SEL_I2S1_ASRC); 257 } 258 259 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) { 260 /* 261 * The firmware might enable the clock at 262 * boot (this information may or may not 263 * be reflected in the enable clock register). 264 * To change the rate we must disable the clock 265 * first to cover these cases. Due to common 266 * clock framework restrictions that do not allow 267 * to disable a clock that has not been enabled, 268 * we need to enable the clock first. 269 */ 270 ret = clk_prepare_enable(ctx->mclk); 271 if (!ret) 272 clk_disable_unprepare(ctx->mclk); 273 274 ret = clk_set_rate(ctx->mclk, 19200000); 275 276 if (ret) 277 dev_err(rtd->dev, "unable to set MCLK rate\n"); 278 } 279 280 /* 281 * Headset buttons map to the google Reference headset. 282 * These can be configured by userspace. 283 */ 284 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack", 285 SND_JACK_HEADSET | SND_JACK_BTN_0 | 286 SND_JACK_BTN_1 | SND_JACK_BTN_2 | 287 SND_JACK_BTN_3, 288 &ctx->sof_headset, NULL, 0); 289 if (ret) { 290 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret); 291 return ret; 292 } 293 294 jack = &ctx->sof_headset; 295 296 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 297 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); 298 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP); 299 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); 300 ret = snd_soc_component_set_jack(component, jack, NULL); 301 302 if (ret) { 303 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret); 304 return ret; 305 } 306 307 return ret; 308 }; 309 310 static void sof_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd) 311 { 312 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component; 313 314 snd_soc_component_set_jack(component, NULL, NULL); 315 } 316 317 static int sof_rt5682_hw_params(struct snd_pcm_substream *substream, 318 struct snd_pcm_hw_params *params) 319 { 320 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 321 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card); 322 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 323 int pll_id, pll_source, pll_in, pll_out, clk_id, ret; 324 325 if (sof_rt5682_quirk & SOF_RT5682_MCLK_EN) { 326 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) { 327 ret = clk_prepare_enable(ctx->mclk); 328 if (ret < 0) { 329 dev_err(rtd->dev, 330 "could not configure MCLK state"); 331 return ret; 332 } 333 } 334 335 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) 336 pll_source = RT5682S_PLL_S_MCLK; 337 else 338 pll_source = RT5682_PLL1_S_MCLK; 339 340 /* get the tplg configured mclk. */ 341 pll_in = sof_dai_get_mclk(rtd); 342 343 /* mclk from the quirk is the first choice */ 344 if (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ) { 345 if (pll_in != 24000000) 346 dev_warn(rtd->dev, "configure wrong mclk in tplg, please use 24MHz.\n"); 347 pll_in = 24000000; 348 } else if (pll_in == 0) { 349 /* use default mclk if not specified correct in topology */ 350 pll_in = 19200000; 351 } else if (pll_in < 0) { 352 return pll_in; 353 } 354 } else { 355 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) 356 pll_source = RT5682S_PLL_S_BCLK1; 357 else 358 pll_source = RT5682_PLL1_S_BCLK1; 359 360 pll_in = params_rate(params) * 50; 361 } 362 363 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) { 364 pll_id = RT5682S_PLL2; 365 clk_id = RT5682S_SCLK_S_PLL2; 366 } else { 367 pll_id = RT5682_PLL1; 368 clk_id = RT5682_SCLK_S_PLL1; 369 } 370 371 pll_out = params_rate(params) * 512; 372 373 /* when MCLK is 512FS, no need to set PLL configuration additionally. */ 374 if (pll_in == pll_out) 375 clk_id = RT5682S_SCLK_S_MCLK; 376 else { 377 /* Configure pll for codec */ 378 ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source, pll_in, 379 pll_out); 380 if (ret < 0) 381 dev_err(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret); 382 } 383 384 /* Configure sysclk for codec */ 385 ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, 386 pll_out, SND_SOC_CLOCK_IN); 387 if (ret < 0) 388 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret); 389 390 /* 391 * slot_width should equal or large than data length, set them 392 * be the same 393 */ 394 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2, 395 params_width(params)); 396 if (ret < 0) { 397 dev_err(rtd->dev, "set TDM slot err:%d\n", ret); 398 return ret; 399 } 400 401 return ret; 402 } 403 404 static struct snd_soc_ops sof_rt5682_ops = { 405 .hw_params = sof_rt5682_hw_params, 406 }; 407 408 static struct snd_soc_dai_link_component platform_component[] = { 409 { 410 /* name might be overridden during probe */ 411 .name = "0000:00:1f.3" 412 } 413 }; 414 415 static int sof_card_late_probe(struct snd_soc_card *card) 416 { 417 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card); 418 struct snd_soc_component *component = NULL; 419 struct snd_soc_dapm_context *dapm = &card->dapm; 420 char jack_name[NAME_SIZE]; 421 struct sof_hdmi_pcm *pcm; 422 int err; 423 int i = 0; 424 425 /* HDMI is not supported by SOF on Baytrail/CherryTrail */ 426 if (is_legacy_cpu || !ctx->idisp_codec) 427 return 0; 428 429 if (list_empty(&ctx->hdmi_pcm_list)) 430 return -EINVAL; 431 432 if (ctx->common_hdmi_codec_drv) { 433 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm, 434 head); 435 component = pcm->codec_dai->component; 436 return hda_dsp_hdmi_build_controls(card, component); 437 } 438 439 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) { 440 component = pcm->codec_dai->component; 441 snprintf(jack_name, sizeof(jack_name), 442 "HDMI/DP, pcm=%d Jack", pcm->device); 443 err = snd_soc_card_jack_new(card, jack_name, 444 SND_JACK_AVOUT, &sof_hdmi[i], 445 NULL, 0); 446 447 if (err) 448 return err; 449 450 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device, 451 &sof_hdmi[i]); 452 if (err < 0) 453 return err; 454 455 i++; 456 } 457 458 if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT) { 459 /* Disable Left and Right Spk pin after boot */ 460 snd_soc_dapm_disable_pin(dapm, "Left Spk"); 461 snd_soc_dapm_disable_pin(dapm, "Right Spk"); 462 err = snd_soc_dapm_sync(dapm); 463 if (err < 0) 464 return err; 465 } 466 467 return hdac_hdmi_jack_port_init(component, &card->dapm); 468 } 469 470 static const struct snd_kcontrol_new sof_controls[] = { 471 SOC_DAPM_PIN_SWITCH("Headphone Jack"), 472 SOC_DAPM_PIN_SWITCH("Headset Mic"), 473 SOC_DAPM_PIN_SWITCH("Left Spk"), 474 SOC_DAPM_PIN_SWITCH("Right Spk"), 475 476 }; 477 478 static const struct snd_soc_dapm_widget sof_widgets[] = { 479 SND_SOC_DAPM_HP("Headphone Jack", NULL), 480 SND_SOC_DAPM_MIC("Headset Mic", NULL), 481 SND_SOC_DAPM_SPK("Left Spk", NULL), 482 SND_SOC_DAPM_SPK("Right Spk", NULL), 483 }; 484 485 static const struct snd_soc_dapm_widget dmic_widgets[] = { 486 SND_SOC_DAPM_MIC("SoC DMIC", NULL), 487 }; 488 489 static const struct snd_soc_dapm_route sof_map[] = { 490 /* HP jack connectors - unknown if we have jack detection */ 491 { "Headphone Jack", NULL, "HPOL" }, 492 { "Headphone Jack", NULL, "HPOR" }, 493 494 /* other jacks */ 495 { "IN1P", NULL, "Headset Mic" }, 496 }; 497 498 static const struct snd_soc_dapm_route dmic_map[] = { 499 /* digital mics */ 500 {"DMic", NULL, "SoC DMIC"}, 501 }; 502 503 static int dmic_init(struct snd_soc_pcm_runtime *rtd) 504 { 505 struct snd_soc_card *card = rtd->card; 506 int ret; 507 508 ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets, 509 ARRAY_SIZE(dmic_widgets)); 510 if (ret) { 511 dev_err(card->dev, "DMic widget addition failed: %d\n", ret); 512 /* Don't need to add routes if widget addition failed */ 513 return ret; 514 } 515 516 ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map, 517 ARRAY_SIZE(dmic_map)); 518 519 if (ret) 520 dev_err(card->dev, "DMic map addition failed: %d\n", ret); 521 522 return ret; 523 } 524 525 /* sof audio machine driver for rt5682 codec */ 526 static struct snd_soc_card sof_audio_card_rt5682 = { 527 .name = "rt5682", /* the sof- prefix is added by the core */ 528 .owner = THIS_MODULE, 529 .controls = sof_controls, 530 .num_controls = ARRAY_SIZE(sof_controls), 531 .dapm_widgets = sof_widgets, 532 .num_dapm_widgets = ARRAY_SIZE(sof_widgets), 533 .dapm_routes = sof_map, 534 .num_dapm_routes = ARRAY_SIZE(sof_map), 535 .fully_routed = true, 536 .late_probe = sof_card_late_probe, 537 }; 538 539 static struct snd_soc_dai_link_component rt5682_component[] = { 540 { 541 .name = "i2c-10EC5682:00", 542 .dai_name = "rt5682-aif1", 543 } 544 }; 545 546 static struct snd_soc_dai_link_component rt5682s_component[] = { 547 { 548 .name = "i2c-RTL5682:00", 549 .dai_name = "rt5682s-aif1", 550 } 551 }; 552 553 static struct snd_soc_dai_link_component dmic_component[] = { 554 { 555 .name = "dmic-codec", 556 .dai_name = "dmic-hifi", 557 } 558 }; 559 560 static struct snd_soc_dai_link_component dummy_component[] = { 561 { 562 .name = "snd-soc-dummy", 563 .dai_name = "snd-soc-dummy-dai", 564 } 565 }; 566 567 #define IDISP_CODEC_MASK 0x4 568 569 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev, 570 int ssp_codec, 571 int ssp_amp, 572 int dmic_be_num, 573 int hdmi_num, 574 bool idisp_codec) 575 { 576 struct snd_soc_dai_link_component *idisp_components; 577 struct snd_soc_dai_link_component *cpus; 578 struct snd_soc_dai_link *links; 579 int i, id = 0; 580 581 links = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link) * 582 sof_audio_card_rt5682.num_links, GFP_KERNEL); 583 cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component) * 584 sof_audio_card_rt5682.num_links, GFP_KERNEL); 585 if (!links || !cpus) 586 goto devm_err; 587 588 /* codec SSP */ 589 links[id].name = devm_kasprintf(dev, GFP_KERNEL, 590 "SSP%d-Codec", ssp_codec); 591 if (!links[id].name) 592 goto devm_err; 593 594 links[id].id = id; 595 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) { 596 links[id].codecs = rt5682s_component; 597 links[id].num_codecs = ARRAY_SIZE(rt5682s_component); 598 } else { 599 links[id].codecs = rt5682_component; 600 links[id].num_codecs = ARRAY_SIZE(rt5682_component); 601 } 602 links[id].platforms = platform_component; 603 links[id].num_platforms = ARRAY_SIZE(platform_component); 604 links[id].init = sof_rt5682_codec_init; 605 links[id].exit = sof_rt5682_codec_exit; 606 links[id].ops = &sof_rt5682_ops; 607 links[id].dpcm_playback = 1; 608 links[id].dpcm_capture = 1; 609 links[id].no_pcm = 1; 610 links[id].cpus = &cpus[id]; 611 links[id].num_cpus = 1; 612 if (is_legacy_cpu) { 613 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 614 "ssp%d-port", 615 ssp_codec); 616 if (!links[id].cpus->dai_name) 617 goto devm_err; 618 } else { 619 /* 620 * Currently, On SKL+ platforms MCLK will be turned off in sof 621 * runtime suspended, and it will go into runtime suspended 622 * right after playback is stop. However, rt5682 will output 623 * static noise if sysclk turns off during playback. Set 624 * ignore_pmdown_time to power down rt5682 immediately and 625 * avoid the noise. 626 * It can be removed once we can control MCLK by driver. 627 */ 628 links[id].ignore_pmdown_time = 1; 629 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 630 "SSP%d Pin", 631 ssp_codec); 632 if (!links[id].cpus->dai_name) 633 goto devm_err; 634 } 635 id++; 636 637 /* dmic */ 638 if (dmic_be_num > 0) { 639 /* at least we have dmic01 */ 640 links[id].name = "dmic01"; 641 links[id].cpus = &cpus[id]; 642 links[id].cpus->dai_name = "DMIC01 Pin"; 643 links[id].init = dmic_init; 644 if (dmic_be_num > 1) { 645 /* set up 2 BE links at most */ 646 links[id + 1].name = "dmic16k"; 647 links[id + 1].cpus = &cpus[id + 1]; 648 links[id + 1].cpus->dai_name = "DMIC16k Pin"; 649 dmic_be_num = 2; 650 } 651 } 652 653 for (i = 0; i < dmic_be_num; i++) { 654 links[id].id = id; 655 links[id].num_cpus = 1; 656 links[id].codecs = dmic_component; 657 links[id].num_codecs = ARRAY_SIZE(dmic_component); 658 links[id].platforms = platform_component; 659 links[id].num_platforms = ARRAY_SIZE(platform_component); 660 links[id].ignore_suspend = 1; 661 links[id].dpcm_capture = 1; 662 links[id].no_pcm = 1; 663 id++; 664 } 665 666 /* HDMI */ 667 if (hdmi_num > 0) { 668 idisp_components = devm_kzalloc(dev, 669 sizeof(struct snd_soc_dai_link_component) * 670 hdmi_num, GFP_KERNEL); 671 if (!idisp_components) 672 goto devm_err; 673 } 674 for (i = 1; i <= hdmi_num; i++) { 675 links[id].name = devm_kasprintf(dev, GFP_KERNEL, 676 "iDisp%d", i); 677 if (!links[id].name) 678 goto devm_err; 679 680 links[id].id = id; 681 links[id].cpus = &cpus[id]; 682 links[id].num_cpus = 1; 683 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 684 "iDisp%d Pin", i); 685 if (!links[id].cpus->dai_name) 686 goto devm_err; 687 688 if (idisp_codec) { 689 idisp_components[i - 1].name = "ehdaudio0D2"; 690 idisp_components[i - 1].dai_name = devm_kasprintf(dev, 691 GFP_KERNEL, 692 "intel-hdmi-hifi%d", 693 i); 694 if (!idisp_components[i - 1].dai_name) 695 goto devm_err; 696 } else { 697 idisp_components[i - 1].name = "snd-soc-dummy"; 698 idisp_components[i - 1].dai_name = "snd-soc-dummy-dai"; 699 } 700 701 links[id].codecs = &idisp_components[i - 1]; 702 links[id].num_codecs = 1; 703 links[id].platforms = platform_component; 704 links[id].num_platforms = ARRAY_SIZE(platform_component); 705 links[id].init = sof_hdmi_init; 706 links[id].dpcm_playback = 1; 707 links[id].no_pcm = 1; 708 id++; 709 } 710 711 /* speaker amp */ 712 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) { 713 links[id].name = devm_kasprintf(dev, GFP_KERNEL, 714 "SSP%d-Codec", ssp_amp); 715 if (!links[id].name) 716 goto devm_err; 717 718 links[id].id = id; 719 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT) { 720 sof_rt1015_dai_link(&links[id], (sof_rt5682_quirk & 721 SOF_RT1015_SPEAKER_AMP_100FS) ? 100 : 64); 722 } else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT) { 723 sof_rt1015p_dai_link(&links[id]); 724 } else if (sof_rt5682_quirk & 725 SOF_MAX98373_SPEAKER_AMP_PRESENT) { 726 links[id].codecs = max_98373_components; 727 links[id].num_codecs = ARRAY_SIZE(max_98373_components); 728 links[id].init = max_98373_spk_codec_init; 729 links[id].ops = &max_98373_ops; 730 /* feedback stream */ 731 links[id].dpcm_capture = 1; 732 } else if (sof_rt5682_quirk & 733 SOF_MAX98360A_SPEAKER_AMP_PRESENT) { 734 max_98360a_dai_link(&links[id]); 735 } else if (sof_rt5682_quirk & 736 SOF_RT1011_SPEAKER_AMP_PRESENT) { 737 sof_rt1011_dai_link(&links[id]); 738 } else if (sof_rt5682_quirk & 739 SOF_MAX98390_SPEAKER_AMP_PRESENT) { 740 if (sof_rt5682_quirk & 741 SOF_MAX98390_TWEETER_SPEAKER_PRESENT) { 742 links[id].codecs = max_98390_4spk_components; 743 links[id].num_codecs = ARRAY_SIZE(max_98390_4spk_components); 744 } else { 745 links[id].codecs = max_98390_components; 746 links[id].num_codecs = ARRAY_SIZE(max_98390_components); 747 } 748 links[id].init = max_98390_spk_codec_init; 749 links[id].ops = &max_98390_ops; 750 links[id].dpcm_capture = 1; 751 752 } else { 753 max_98357a_dai_link(&links[id]); 754 } 755 links[id].platforms = platform_component; 756 links[id].num_platforms = ARRAY_SIZE(platform_component); 757 links[id].dpcm_playback = 1; 758 links[id].no_pcm = 1; 759 links[id].cpus = &cpus[id]; 760 links[id].num_cpus = 1; 761 if (is_legacy_cpu) { 762 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 763 "ssp%d-port", 764 ssp_amp); 765 if (!links[id].cpus->dai_name) 766 goto devm_err; 767 768 } else { 769 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 770 "SSP%d Pin", 771 ssp_amp); 772 if (!links[id].cpus->dai_name) 773 goto devm_err; 774 } 775 id++; 776 } 777 778 /* BT audio offload */ 779 if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) { 780 int port = (sof_rt5682_quirk & SOF_BT_OFFLOAD_SSP_MASK) >> 781 SOF_BT_OFFLOAD_SSP_SHIFT; 782 783 links[id].id = id; 784 links[id].cpus = &cpus[id]; 785 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 786 "SSP%d Pin", port); 787 if (!links[id].cpus->dai_name) 788 goto devm_err; 789 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port); 790 if (!links[id].name) 791 goto devm_err; 792 links[id].codecs = dummy_component; 793 links[id].num_codecs = ARRAY_SIZE(dummy_component); 794 links[id].platforms = platform_component; 795 links[id].num_platforms = ARRAY_SIZE(platform_component); 796 links[id].dpcm_playback = 1; 797 links[id].dpcm_capture = 1; 798 links[id].no_pcm = 1; 799 links[id].num_cpus = 1; 800 } 801 802 return links; 803 devm_err: 804 return NULL; 805 } 806 807 static int sof_audio_probe(struct platform_device *pdev) 808 { 809 struct snd_soc_dai_link *dai_links; 810 struct snd_soc_acpi_mach *mach; 811 struct sof_card_private *ctx; 812 int dmic_be_num, hdmi_num; 813 int ret, ssp_amp, ssp_codec; 814 815 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); 816 if (!ctx) 817 return -ENOMEM; 818 819 if (pdev->id_entry && pdev->id_entry->driver_data) 820 sof_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data; 821 822 dmi_check_system(sof_rt5682_quirk_table); 823 824 mach = pdev->dev.platform_data; 825 826 /* A speaker amp might not be present when the quirk claims one is. 827 * Detect this via whether the machine driver match includes quirk_data. 828 */ 829 if ((sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) && !mach->quirk_data) 830 sof_rt5682_quirk &= ~SOF_SPEAKER_AMP_PRESENT; 831 832 /* Detect the headset codec variant */ 833 if (acpi_dev_present("RTL5682", NULL, -1)) 834 sof_rt5682_quirk |= SOF_RT5682S_HEADPHONE_CODEC_PRESENT; 835 836 /* Detect the headset codec variant to support machines in DMI quirk */ 837 if (acpi_dev_present("RTL5682", NULL, -1)) 838 sof_rt5682_quirk |= SOF_RT5682S_HEADPHONE_CODEC_PRESENT; 839 840 if (soc_intel_is_byt() || soc_intel_is_cht()) { 841 is_legacy_cpu = 1; 842 dmic_be_num = 0; 843 hdmi_num = 0; 844 /* default quirk for legacy cpu */ 845 sof_rt5682_quirk = SOF_RT5682_MCLK_EN | 846 SOF_RT5682_MCLK_BYTCHT_EN | 847 SOF_RT5682_SSP_CODEC(2); 848 } else { 849 dmic_be_num = 2; 850 hdmi_num = (sof_rt5682_quirk & SOF_RT5682_NUM_HDMIDEV_MASK) >> 851 SOF_RT5682_NUM_HDMIDEV_SHIFT; 852 /* default number of HDMI DAI's */ 853 if (!hdmi_num) 854 hdmi_num = 3; 855 856 if (mach->mach_params.codec_mask & IDISP_CODEC_MASK) 857 ctx->idisp_codec = true; 858 } 859 860 /* need to get main clock from pmc */ 861 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) { 862 ctx->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3"); 863 if (IS_ERR(ctx->mclk)) { 864 ret = PTR_ERR(ctx->mclk); 865 866 dev_err(&pdev->dev, 867 "Failed to get MCLK from pmc_plt_clk_3: %d\n", 868 ret); 869 return ret; 870 } 871 872 ret = clk_prepare_enable(ctx->mclk); 873 if (ret < 0) { 874 dev_err(&pdev->dev, 875 "could not configure MCLK state"); 876 return ret; 877 } 878 } 879 880 dev_dbg(&pdev->dev, "sof_rt5682_quirk = %lx\n", sof_rt5682_quirk); 881 882 ssp_amp = (sof_rt5682_quirk & SOF_RT5682_SSP_AMP_MASK) >> 883 SOF_RT5682_SSP_AMP_SHIFT; 884 885 ssp_codec = sof_rt5682_quirk & SOF_RT5682_SSP_CODEC_MASK; 886 887 /* compute number of dai links */ 888 sof_audio_card_rt5682.num_links = 1 + dmic_be_num + hdmi_num; 889 890 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) 891 sof_audio_card_rt5682.num_links++; 892 893 if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT) 894 max_98373_set_codec_conf(&sof_audio_card_rt5682); 895 else if (sof_rt5682_quirk & SOF_RT1011_SPEAKER_AMP_PRESENT) 896 sof_rt1011_codec_conf(&sof_audio_card_rt5682); 897 else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT) 898 sof_rt1015p_codec_conf(&sof_audio_card_rt5682); 899 else if (sof_rt5682_quirk & SOF_MAX98390_SPEAKER_AMP_PRESENT) { 900 if (sof_rt5682_quirk & SOF_MAX98390_TWEETER_SPEAKER_PRESENT) 901 max_98390_set_codec_conf(&sof_audio_card_rt5682, 902 ARRAY_SIZE(max_98390_4spk_components)); 903 else 904 max_98390_set_codec_conf(&sof_audio_card_rt5682, 905 ARRAY_SIZE(max_98390_components)); 906 } 907 908 if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) 909 sof_audio_card_rt5682.num_links++; 910 911 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp, 912 dmic_be_num, hdmi_num, ctx->idisp_codec); 913 if (!dai_links) 914 return -ENOMEM; 915 916 sof_audio_card_rt5682.dai_link = dai_links; 917 918 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT) 919 sof_rt1015_codec_conf(&sof_audio_card_rt5682); 920 921 INIT_LIST_HEAD(&ctx->hdmi_pcm_list); 922 923 sof_audio_card_rt5682.dev = &pdev->dev; 924 925 /* set platform name for each dailink */ 926 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_rt5682, 927 mach->mach_params.platform); 928 if (ret) 929 return ret; 930 931 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv; 932 933 snd_soc_card_set_drvdata(&sof_audio_card_rt5682, ctx); 934 935 return devm_snd_soc_register_card(&pdev->dev, 936 &sof_audio_card_rt5682); 937 } 938 939 static const struct platform_device_id board_ids[] = { 940 { 941 .name = "sof_rt5682", 942 }, 943 { 944 .name = "tgl_mx98357_rt5682", 945 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN | 946 SOF_RT5682_SSP_CODEC(0) | 947 SOF_SPEAKER_AMP_PRESENT | 948 SOF_RT5682_SSP_AMP(1) | 949 SOF_RT5682_NUM_HDMIDEV(4) | 950 SOF_BT_OFFLOAD_SSP(2) | 951 SOF_SSP_BT_OFFLOAD_PRESENT), 952 }, 953 { 954 .name = "jsl_rt5682_rt1015", 955 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN | 956 SOF_RT5682_MCLK_24MHZ | 957 SOF_RT5682_SSP_CODEC(0) | 958 SOF_SPEAKER_AMP_PRESENT | 959 SOF_RT1015_SPEAKER_AMP_PRESENT | 960 SOF_RT5682_SSP_AMP(1)), 961 }, 962 { 963 .name = "tgl_mx98373_rt5682", 964 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN | 965 SOF_RT5682_SSP_CODEC(0) | 966 SOF_SPEAKER_AMP_PRESENT | 967 SOF_MAX98373_SPEAKER_AMP_PRESENT | 968 SOF_RT5682_SSP_AMP(1) | 969 SOF_RT5682_NUM_HDMIDEV(4) | 970 SOF_BT_OFFLOAD_SSP(2) | 971 SOF_SSP_BT_OFFLOAD_PRESENT), 972 }, 973 { 974 .name = "jsl_rt5682_mx98360", 975 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN | 976 SOF_RT5682_MCLK_24MHZ | 977 SOF_RT5682_SSP_CODEC(0) | 978 SOF_SPEAKER_AMP_PRESENT | 979 SOF_MAX98360A_SPEAKER_AMP_PRESENT | 980 SOF_RT5682_SSP_AMP(1)), 981 }, 982 { 983 .name = "cml_rt1015_rt5682", 984 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN | 985 SOF_RT5682_MCLK_24MHZ | 986 SOF_RT5682_SSP_CODEC(0) | 987 SOF_SPEAKER_AMP_PRESENT | 988 SOF_RT1015_SPEAKER_AMP_PRESENT | 989 SOF_RT1015_SPEAKER_AMP_100FS | 990 SOF_RT5682_SSP_AMP(1)), 991 }, 992 { 993 .name = "tgl_rt1011_rt5682", 994 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN | 995 SOF_RT5682_SSP_CODEC(0) | 996 SOF_SPEAKER_AMP_PRESENT | 997 SOF_RT1011_SPEAKER_AMP_PRESENT | 998 SOF_RT5682_SSP_AMP(1) | 999 SOF_RT5682_NUM_HDMIDEV(4) | 1000 SOF_BT_OFFLOAD_SSP(2) | 1001 SOF_SSP_BT_OFFLOAD_PRESENT), 1002 }, 1003 { 1004 .name = "jsl_rt5682_rt1015p", 1005 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN | 1006 SOF_RT5682_MCLK_24MHZ | 1007 SOF_RT5682_SSP_CODEC(0) | 1008 SOF_SPEAKER_AMP_PRESENT | 1009 SOF_RT1015P_SPEAKER_AMP_PRESENT | 1010 SOF_RT5682_SSP_AMP(1)), 1011 }, 1012 { 1013 .name = "adl_mx98373_rt5682", 1014 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN | 1015 SOF_RT5682_SSP_CODEC(0) | 1016 SOF_SPEAKER_AMP_PRESENT | 1017 SOF_MAX98373_SPEAKER_AMP_PRESENT | 1018 SOF_RT5682_SSP_AMP(1) | 1019 SOF_RT5682_NUM_HDMIDEV(4) | 1020 SOF_BT_OFFLOAD_SSP(2) | 1021 SOF_SSP_BT_OFFLOAD_PRESENT), 1022 }, 1023 { 1024 .name = "adl_mx98357_rt5682", 1025 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN | 1026 SOF_RT5682_SSP_CODEC(0) | 1027 SOF_SPEAKER_AMP_PRESENT | 1028 SOF_RT5682_SSP_AMP(2) | 1029 SOF_RT5682_NUM_HDMIDEV(4)), 1030 }, 1031 { 1032 .name = "adl_max98390_rt5682", 1033 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN | 1034 SOF_RT5682_SSP_CODEC(0) | 1035 SOF_SPEAKER_AMP_PRESENT | 1036 SOF_MAX98390_SPEAKER_AMP_PRESENT | 1037 SOF_RT5682_SSP_AMP(1) | 1038 SOF_RT5682_NUM_HDMIDEV(4) | 1039 SOF_BT_OFFLOAD_SSP(2) | 1040 SOF_SSP_BT_OFFLOAD_PRESENT), 1041 }, 1042 { 1043 .name = "adl_mx98360_rt5682", 1044 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN | 1045 SOF_RT5682_SSP_CODEC(0) | 1046 SOF_SPEAKER_AMP_PRESENT | 1047 SOF_MAX98360A_SPEAKER_AMP_PRESENT | 1048 SOF_RT5682_SSP_AMP(1) | 1049 SOF_RT5682_NUM_HDMIDEV(4) | 1050 SOF_BT_OFFLOAD_SSP(2) | 1051 SOF_SSP_BT_OFFLOAD_PRESENT), 1052 }, 1053 { 1054 .name = "adl_rt5682", 1055 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN | 1056 SOF_RT5682_SSP_CODEC(0) | 1057 SOF_RT5682_NUM_HDMIDEV(4) | 1058 SOF_BT_OFFLOAD_SSP(2) | 1059 SOF_SSP_BT_OFFLOAD_PRESENT), 1060 }, 1061 { } 1062 }; 1063 MODULE_DEVICE_TABLE(platform, board_ids); 1064 1065 static struct platform_driver sof_audio = { 1066 .probe = sof_audio_probe, 1067 .driver = { 1068 .name = "sof_rt5682", 1069 .pm = &snd_soc_pm_ops, 1070 }, 1071 .id_table = board_ids, 1072 }; 1073 module_platform_driver(sof_audio) 1074 1075 /* Module information */ 1076 MODULE_DESCRIPTION("SOF Audio Machine driver"); 1077 MODULE_AUTHOR("Bard Liao <bard.liao@intel.com>"); 1078 MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>"); 1079 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>"); 1080 MODULE_AUTHOR("Mac Chiang <mac.chiang@intel.com>"); 1081 MODULE_LICENSE("GPL v2"); 1082 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON); 1083 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON); 1084 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON); 1085