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