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