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