1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright (c) 2020 Intel Corporation 3 4 /* 5 * sof_sdw - ASOC Machine driver for Intel SoundWire platforms 6 */ 7 8 #include <linux/device.h> 9 #include <linux/dmi.h> 10 #include <linux/module.h> 11 #include <linux/soundwire/sdw.h> 12 #include <linux/soundwire/sdw_type.h> 13 #include <sound/soc.h> 14 #include <sound/soc-acpi.h> 15 #include "sof_sdw_common.h" 16 17 unsigned long sof_sdw_quirk = SOF_RT711_JD_SRC_JD1; 18 static int quirk_override = -1; 19 module_param_named(quirk, quirk_override, int, 0444); 20 MODULE_PARM_DESC(quirk, "Board-specific quirk override"); 21 22 #define INC_ID(BE, CPU, LINK) do { (BE)++; (CPU)++; (LINK)++; } while (0) 23 24 static void log_quirks(struct device *dev) 25 { 26 if (SOF_RT711_JDSRC(sof_sdw_quirk)) 27 dev_dbg(dev, "quirk realtek,jack-detect-source %ld\n", 28 SOF_RT711_JDSRC(sof_sdw_quirk)); 29 if (sof_sdw_quirk & SOF_SDW_FOUR_SPK) 30 dev_dbg(dev, "quirk SOF_SDW_FOUR_SPK enabled\n"); 31 if (sof_sdw_quirk & SOF_SDW_TGL_HDMI) 32 dev_dbg(dev, "quirk SOF_SDW_TGL_HDMI enabled\n"); 33 if (sof_sdw_quirk & SOF_SDW_PCH_DMIC) 34 dev_dbg(dev, "quirk SOF_SDW_PCH_DMIC enabled\n"); 35 if (SOF_SSP_GET_PORT(sof_sdw_quirk)) 36 dev_dbg(dev, "SSP port %ld\n", 37 SOF_SSP_GET_PORT(sof_sdw_quirk)); 38 if (sof_sdw_quirk & SOF_RT715_DAI_ID_FIX) 39 dev_dbg(dev, "quirk SOF_RT715_DAI_ID_FIX enabled\n"); 40 if (sof_sdw_quirk & SOF_SDW_NO_AGGREGATION) 41 dev_dbg(dev, "quirk SOF_SDW_NO_AGGREGATION enabled\n"); 42 } 43 44 static int sof_sdw_quirk_cb(const struct dmi_system_id *id) 45 { 46 sof_sdw_quirk = (unsigned long)id->driver_data; 47 return 1; 48 } 49 50 static const struct dmi_system_id sof_sdw_quirk_table[] = { 51 { 52 .callback = sof_sdw_quirk_cb, 53 .matches = { 54 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 55 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A32") 56 }, 57 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 | 58 SOF_RT715_DAI_ID_FIX | 59 SOF_SDW_FOUR_SPK), 60 }, 61 { 62 .callback = sof_sdw_quirk_cb, 63 .matches = { 64 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 65 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A3E") 66 }, 67 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 | 68 SOF_RT715_DAI_ID_FIX), 69 }, 70 { 71 .callback = sof_sdw_quirk_cb, 72 .matches = { 73 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 74 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5E") 75 }, 76 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 | 77 SOF_RT715_DAI_ID_FIX | 78 SOF_SDW_FOUR_SPK), 79 }, 80 { 81 .callback = sof_sdw_quirk_cb, 82 .matches = { 83 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 84 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "09C6") 85 }, 86 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 | 87 SOF_RT715_DAI_ID_FIX), 88 }, 89 { 90 /* early version of SKU 09C6 */ 91 .callback = sof_sdw_quirk_cb, 92 .matches = { 93 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 94 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0983") 95 }, 96 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 | 97 SOF_RT715_DAI_ID_FIX), 98 }, 99 { 100 .callback = sof_sdw_quirk_cb, 101 .matches = { 102 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 103 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "098F"), 104 }, 105 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 | 106 SOF_RT715_DAI_ID_FIX | 107 SOF_SDW_FOUR_SPK), 108 }, 109 { 110 .callback = sof_sdw_quirk_cb, 111 .matches = { 112 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 113 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0990"), 114 }, 115 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 | 116 SOF_RT715_DAI_ID_FIX | 117 SOF_SDW_FOUR_SPK), 118 }, 119 { 120 .callback = sof_sdw_quirk_cb, 121 .matches = { 122 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), 123 DMI_MATCH(DMI_PRODUCT_NAME, 124 "Tiger Lake Client Platform"), 125 }, 126 .driver_data = (void *)(SOF_RT711_JD_SRC_JD1 | 127 SOF_SDW_TGL_HDMI | SOF_SDW_PCH_DMIC | 128 SOF_SSP_PORT(SOF_I2S_SSP2)), 129 }, 130 { 131 .callback = sof_sdw_quirk_cb, 132 .matches = { 133 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), 134 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"), 135 }, 136 .driver_data = (void *)SOF_SDW_PCH_DMIC, 137 }, 138 { 139 .callback = sof_sdw_quirk_cb, 140 .matches = { 141 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), 142 DMI_MATCH(DMI_PRODUCT_NAME, "CometLake Client"), 143 }, 144 .driver_data = (void *)SOF_SDW_PCH_DMIC, 145 }, 146 { 147 .callback = sof_sdw_quirk_cb, 148 .matches = { 149 DMI_MATCH(DMI_SYS_VENDOR, "Google"), 150 DMI_MATCH(DMI_PRODUCT_NAME, "Volteer"), 151 }, 152 .driver_data = (void *)(SOF_SDW_TGL_HDMI | SOF_SDW_PCH_DMIC | 153 SOF_SDW_FOUR_SPK), 154 }, 155 { 156 .callback = sof_sdw_quirk_cb, 157 .matches = { 158 DMI_MATCH(DMI_SYS_VENDOR, "Google"), 159 DMI_MATCH(DMI_PRODUCT_NAME, "Ripto"), 160 }, 161 .driver_data = (void *)(SOF_SDW_TGL_HDMI | SOF_SDW_PCH_DMIC | 162 SOF_SDW_FOUR_SPK), 163 }, 164 165 {} 166 }; 167 168 static struct snd_soc_dai_link_component dmic_component[] = { 169 { 170 .name = "dmic-codec", 171 .dai_name = "dmic-hifi", 172 } 173 }; 174 175 static struct snd_soc_dai_link_component platform_component[] = { 176 { 177 /* name might be overridden during probe */ 178 .name = "0000:00:1f.3" 179 } 180 }; 181 182 /* these wrappers are only needed to avoid typecast compilation errors */ 183 int sdw_startup(struct snd_pcm_substream *substream) 184 { 185 return sdw_startup_stream(substream); 186 } 187 188 int sdw_prepare(struct snd_pcm_substream *substream) 189 { 190 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 191 struct sdw_stream_runtime *sdw_stream; 192 struct snd_soc_dai *dai; 193 194 /* Find stream from first CPU DAI */ 195 dai = asoc_rtd_to_cpu(rtd, 0); 196 197 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream); 198 199 if (IS_ERR(sdw_stream)) { 200 dev_err(rtd->dev, "no stream found for DAI %s", dai->name); 201 return PTR_ERR(sdw_stream); 202 } 203 204 return sdw_prepare_stream(sdw_stream); 205 } 206 207 int sdw_trigger(struct snd_pcm_substream *substream, int cmd) 208 { 209 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 210 struct sdw_stream_runtime *sdw_stream; 211 struct snd_soc_dai *dai; 212 int ret; 213 214 /* Find stream from first CPU DAI */ 215 dai = asoc_rtd_to_cpu(rtd, 0); 216 217 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream); 218 219 if (IS_ERR(sdw_stream)) { 220 dev_err(rtd->dev, "no stream found for DAI %s", dai->name); 221 return PTR_ERR(sdw_stream); 222 } 223 224 switch (cmd) { 225 case SNDRV_PCM_TRIGGER_START: 226 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 227 case SNDRV_PCM_TRIGGER_RESUME: 228 ret = sdw_enable_stream(sdw_stream); 229 break; 230 231 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 232 case SNDRV_PCM_TRIGGER_SUSPEND: 233 case SNDRV_PCM_TRIGGER_STOP: 234 ret = sdw_disable_stream(sdw_stream); 235 break; 236 default: 237 ret = -EINVAL; 238 break; 239 } 240 241 if (ret) 242 dev_err(rtd->dev, "%s trigger %d failed: %d", __func__, cmd, ret); 243 244 return ret; 245 } 246 247 int sdw_hw_free(struct snd_pcm_substream *substream) 248 { 249 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 250 struct sdw_stream_runtime *sdw_stream; 251 struct snd_soc_dai *dai; 252 253 /* Find stream from first CPU DAI */ 254 dai = asoc_rtd_to_cpu(rtd, 0); 255 256 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream); 257 258 if (IS_ERR(sdw_stream)) { 259 dev_err(rtd->dev, "no stream found for DAI %s", dai->name); 260 return PTR_ERR(sdw_stream); 261 } 262 263 return sdw_deprepare_stream(sdw_stream); 264 } 265 266 void sdw_shutdown(struct snd_pcm_substream *substream) 267 { 268 sdw_shutdown_stream(substream); 269 } 270 271 static const struct snd_soc_ops sdw_ops = { 272 .startup = sdw_startup, 273 .prepare = sdw_prepare, 274 .trigger = sdw_trigger, 275 .hw_free = sdw_hw_free, 276 .shutdown = sdw_shutdown, 277 }; 278 279 static struct sof_sdw_codec_info codec_info_list[] = { 280 { 281 .part_id = 0x700, 282 .direction = {true, true}, 283 .dai_name = "rt700-aif1", 284 .init = sof_sdw_rt700_init, 285 }, 286 { 287 .part_id = 0x711, 288 .version_id = 3, 289 .direction = {true, true}, 290 .dai_name = "rt711-sdca-aif1", 291 .init = sof_sdw_rt711_sdca_init, 292 .exit = sof_sdw_rt711_sdca_exit, 293 }, 294 { 295 .part_id = 0x711, 296 .version_id = 2, 297 .direction = {true, true}, 298 .dai_name = "rt711-aif1", 299 .init = sof_sdw_rt711_init, 300 .exit = sof_sdw_rt711_exit, 301 }, 302 { 303 .part_id = 0x1308, 304 .acpi_id = "10EC1308", 305 .direction = {true, false}, 306 .dai_name = "rt1308-aif", 307 .ops = &sof_sdw_rt1308_i2s_ops, 308 .init = sof_sdw_rt1308_init, 309 }, 310 { 311 .part_id = 0x1316, 312 .direction = {true, true}, 313 .dai_name = "rt1316-aif", 314 .init = sof_sdw_rt1316_init, 315 }, 316 { 317 .part_id = 0x714, 318 .version_id = 3, 319 .direction = {false, true}, 320 .dai_name = "rt715-aif2", 321 .init = sof_sdw_rt715_sdca_init, 322 }, 323 { 324 .part_id = 0x715, 325 .version_id = 3, 326 .direction = {false, true}, 327 .dai_name = "rt715-aif2", 328 .init = sof_sdw_rt715_sdca_init, 329 }, 330 { 331 .part_id = 0x714, 332 .version_id = 2, 333 .direction = {false, true}, 334 .dai_name = "rt715-aif2", 335 .init = sof_sdw_rt715_init, 336 }, 337 { 338 .part_id = 0x715, 339 .version_id = 2, 340 .direction = {false, true}, 341 .dai_name = "rt715-aif2", 342 .init = sof_sdw_rt715_init, 343 }, 344 { 345 .part_id = 0x8373, 346 .direction = {true, true}, 347 .dai_name = "max98373-aif1", 348 .init = sof_sdw_mx8373_init, 349 .codec_card_late_probe = sof_sdw_mx8373_late_probe, 350 }, 351 { 352 .part_id = 0x5682, 353 .direction = {true, true}, 354 .dai_name = "rt5682-sdw", 355 .init = sof_sdw_rt5682_init, 356 }, 357 }; 358 359 static inline int find_codec_info_part(u64 adr) 360 { 361 unsigned int part_id, sdw_version; 362 int i; 363 364 part_id = SDW_PART_ID(adr); 365 sdw_version = SDW_VERSION(adr); 366 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) 367 /* 368 * A codec info is for all sdw version with the part id if 369 * version_id is not specified in the codec info. 370 */ 371 if (part_id == codec_info_list[i].part_id && 372 (!codec_info_list[i].version_id || 373 sdw_version == codec_info_list[i].version_id)) 374 return i; 375 376 return -EINVAL; 377 378 } 379 380 static inline int find_codec_info_acpi(const u8 *acpi_id) 381 { 382 int i; 383 384 if (!acpi_id[0]) 385 return -EINVAL; 386 387 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) 388 if (!memcmp(codec_info_list[i].acpi_id, acpi_id, 389 ACPI_ID_LEN)) 390 break; 391 392 if (i == ARRAY_SIZE(codec_info_list)) 393 return -EINVAL; 394 395 return i; 396 } 397 398 /* 399 * get BE dailink number and CPU DAI number based on sdw link adr. 400 * Since some sdw slaves may be aggregated, the CPU DAI number 401 * may be larger than the number of BE dailinks. 402 */ 403 static int get_sdw_dailink_info(const struct snd_soc_acpi_link_adr *links, 404 int *sdw_be_num, int *sdw_cpu_dai_num) 405 { 406 const struct snd_soc_acpi_link_adr *link; 407 bool group_visited[SDW_MAX_GROUPS]; 408 bool no_aggregation; 409 int i; 410 411 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION; 412 *sdw_cpu_dai_num = 0; 413 *sdw_be_num = 0; 414 415 if (!links) 416 return -EINVAL; 417 418 for (i = 0; i < SDW_MAX_GROUPS; i++) 419 group_visited[i] = false; 420 421 for (link = links; link->num_adr; link++) { 422 const struct snd_soc_acpi_endpoint *endpoint; 423 int codec_index; 424 int stream; 425 u64 adr; 426 427 adr = link->adr_d->adr; 428 codec_index = find_codec_info_part(adr); 429 if (codec_index < 0) 430 return codec_index; 431 432 endpoint = link->adr_d->endpoints; 433 434 /* count DAI number for playback and capture */ 435 for_each_pcm_streams(stream) { 436 if (!codec_info_list[codec_index].direction[stream]) 437 continue; 438 439 (*sdw_cpu_dai_num)++; 440 441 /* count BE for each non-aggregated slave or group */ 442 if (!endpoint->aggregated || no_aggregation || 443 !group_visited[endpoint->group_id]) 444 (*sdw_be_num)++; 445 } 446 447 if (endpoint->aggregated) 448 group_visited[endpoint->group_id] = true; 449 } 450 451 return 0; 452 } 453 454 static void init_dai_link(struct snd_soc_dai_link *dai_links, int be_id, 455 char *name, int playback, int capture, 456 struct snd_soc_dai_link_component *cpus, 457 int cpus_num, 458 struct snd_soc_dai_link_component *codecs, 459 int codecs_num, 460 int (*init)(struct snd_soc_pcm_runtime *rtd), 461 const struct snd_soc_ops *ops) 462 { 463 dai_links->id = be_id; 464 dai_links->name = name; 465 dai_links->platforms = platform_component; 466 dai_links->num_platforms = ARRAY_SIZE(platform_component); 467 dai_links->nonatomic = true; 468 dai_links->no_pcm = 1; 469 dai_links->cpus = cpus; 470 dai_links->num_cpus = cpus_num; 471 dai_links->codecs = codecs; 472 dai_links->num_codecs = codecs_num; 473 dai_links->dpcm_playback = playback; 474 dai_links->dpcm_capture = capture; 475 dai_links->init = init; 476 dai_links->ops = ops; 477 } 478 479 static bool is_unique_device(const struct snd_soc_acpi_link_adr *link, 480 unsigned int sdw_version, 481 unsigned int mfg_id, 482 unsigned int part_id, 483 unsigned int class_id, 484 int index_in_link 485 ) 486 { 487 int i; 488 489 for (i = 0; i < link->num_adr; i++) { 490 unsigned int sdw1_version, mfg1_id, part1_id, class1_id; 491 u64 adr; 492 493 /* skip itself */ 494 if (i == index_in_link) 495 continue; 496 497 adr = link->adr_d[i].adr; 498 499 sdw1_version = SDW_VERSION(adr); 500 mfg1_id = SDW_MFG_ID(adr); 501 part1_id = SDW_PART_ID(adr); 502 class1_id = SDW_CLASS_ID(adr); 503 504 if (sdw_version == sdw1_version && 505 mfg_id == mfg1_id && 506 part_id == part1_id && 507 class_id == class1_id) 508 return false; 509 } 510 511 return true; 512 } 513 514 static int create_codec_dai_name(struct device *dev, 515 const struct snd_soc_acpi_link_adr *link, 516 struct snd_soc_dai_link_component *codec, 517 int offset, 518 struct snd_soc_codec_conf *codec_conf, 519 int codec_count, 520 int *codec_conf_index) 521 { 522 int i; 523 524 /* sanity check */ 525 if (*codec_conf_index + link->num_adr > codec_count) { 526 dev_err(dev, "codec_conf: out-of-bounds access requested\n"); 527 return -EINVAL; 528 } 529 530 for (i = 0; i < link->num_adr; i++) { 531 unsigned int sdw_version, unique_id, mfg_id; 532 unsigned int link_id, part_id, class_id; 533 int codec_index, comp_index; 534 char *codec_str; 535 u64 adr; 536 537 adr = link->adr_d[i].adr; 538 539 sdw_version = SDW_VERSION(adr); 540 link_id = SDW_DISCO_LINK_ID(adr); 541 unique_id = SDW_UNIQUE_ID(adr); 542 mfg_id = SDW_MFG_ID(adr); 543 part_id = SDW_PART_ID(adr); 544 class_id = SDW_CLASS_ID(adr); 545 546 comp_index = i + offset; 547 if (is_unique_device(link, sdw_version, mfg_id, part_id, 548 class_id, i)) { 549 codec_str = "sdw:%x:%x:%x:%x"; 550 codec[comp_index].name = 551 devm_kasprintf(dev, GFP_KERNEL, codec_str, 552 link_id, mfg_id, part_id, 553 class_id); 554 } else { 555 codec_str = "sdw:%x:%x:%x:%x:%x"; 556 codec[comp_index].name = 557 devm_kasprintf(dev, GFP_KERNEL, codec_str, 558 link_id, mfg_id, part_id, 559 class_id, unique_id); 560 } 561 562 if (!codec[comp_index].name) 563 return -ENOMEM; 564 565 codec_index = find_codec_info_part(adr); 566 if (codec_index < 0) 567 return codec_index; 568 569 codec[comp_index].dai_name = 570 codec_info_list[codec_index].dai_name; 571 572 codec_conf[*codec_conf_index].dlc = codec[comp_index]; 573 codec_conf[*codec_conf_index].name_prefix = link->adr_d[i].name_prefix; 574 575 ++*codec_conf_index; 576 } 577 578 return 0; 579 } 580 581 static int set_codec_init_func(const struct snd_soc_acpi_link_adr *link, 582 struct snd_soc_dai_link *dai_links, 583 bool playback, int group_id) 584 { 585 int i; 586 587 do { 588 /* 589 * Initialize the codec. If codec is part of an aggregated 590 * group (group_id>0), initialize all codecs belonging to 591 * same group. 592 */ 593 for (i = 0; i < link->num_adr; i++) { 594 int codec_index; 595 596 codec_index = find_codec_info_part(link->adr_d[i].adr); 597 598 if (codec_index < 0) 599 return codec_index; 600 /* The group_id is > 0 iff the codec is aggregated */ 601 if (link->adr_d[i].endpoints->group_id != group_id) 602 continue; 603 if (codec_info_list[codec_index].init) 604 codec_info_list[codec_index].init(link, 605 dai_links, 606 &codec_info_list[codec_index], 607 playback); 608 } 609 link++; 610 } while (link->mask && group_id); 611 612 return 0; 613 } 614 615 /* 616 * check endpoint status in slaves and gather link ID for all slaves in 617 * the same group to generate different CPU DAI. Now only support 618 * one sdw link with all slaves set with only single group id. 619 * 620 * one slave on one sdw link with aggregated = 0 621 * one sdw BE DAI <---> one-cpu DAI <---> one-codec DAI 622 * 623 * two or more slaves on one sdw link with aggregated = 0 624 * one sdw BE DAI <---> one-cpu DAI <---> multi-codec DAIs 625 * 626 * multiple links with multiple slaves with aggregated = 1 627 * one sdw BE DAI <---> 1 .. N CPU DAIs <----> 1 .. N codec DAIs 628 */ 629 static int get_slave_info(const struct snd_soc_acpi_link_adr *adr_link, 630 struct device *dev, int *cpu_dai_id, int *cpu_dai_num, 631 int *codec_num, int *group_id, 632 bool *group_generated) 633 { 634 const struct snd_soc_acpi_adr_device *adr_d; 635 const struct snd_soc_acpi_link_adr *adr_next; 636 bool no_aggregation; 637 int index = 0; 638 639 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION; 640 *codec_num = adr_link->num_adr; 641 adr_d = adr_link->adr_d; 642 643 /* make sure the link mask has a single bit set */ 644 if (!is_power_of_2(adr_link->mask)) 645 return -EINVAL; 646 647 cpu_dai_id[index++] = ffs(adr_link->mask) - 1; 648 if (!adr_d->endpoints->aggregated || no_aggregation) { 649 *cpu_dai_num = 1; 650 *group_id = 0; 651 return 0; 652 } 653 654 *group_id = adr_d->endpoints->group_id; 655 656 /* gather other link ID of slaves in the same group */ 657 for (adr_next = adr_link + 1; adr_next && adr_next->num_adr; 658 adr_next++) { 659 const struct snd_soc_acpi_endpoint *endpoint; 660 661 endpoint = adr_next->adr_d->endpoints; 662 if (!endpoint->aggregated || 663 endpoint->group_id != *group_id) 664 continue; 665 666 /* make sure the link mask has a single bit set */ 667 if (!is_power_of_2(adr_next->mask)) 668 return -EINVAL; 669 670 if (index >= SDW_MAX_CPU_DAIS) { 671 dev_err(dev, " cpu_dai_id array overflows"); 672 return -EINVAL; 673 } 674 675 cpu_dai_id[index++] = ffs(adr_next->mask) - 1; 676 *codec_num += adr_next->num_adr; 677 } 678 679 /* 680 * indicate CPU DAIs for this group have been generated 681 * to avoid generating CPU DAIs for this group again. 682 */ 683 group_generated[*group_id] = true; 684 *cpu_dai_num = index; 685 686 return 0; 687 } 688 689 static int create_sdw_dailink(struct device *dev, int *be_index, 690 struct snd_soc_dai_link *dai_links, 691 int sdw_be_num, int sdw_cpu_dai_num, 692 struct snd_soc_dai_link_component *cpus, 693 const struct snd_soc_acpi_link_adr *link, 694 int *cpu_id, bool *group_generated, 695 struct snd_soc_codec_conf *codec_conf, 696 int codec_count, 697 int *codec_conf_index) 698 { 699 const struct snd_soc_acpi_link_adr *link_next; 700 struct snd_soc_dai_link_component *codecs; 701 int cpu_dai_id[SDW_MAX_CPU_DAIS]; 702 int cpu_dai_num, cpu_dai_index; 703 unsigned int group_id; 704 int codec_idx = 0; 705 int i = 0, j = 0; 706 int codec_index; 707 int codec_num; 708 int stream; 709 int ret; 710 int k; 711 712 ret = get_slave_info(link, dev, cpu_dai_id, &cpu_dai_num, &codec_num, 713 &group_id, group_generated); 714 if (ret) 715 return ret; 716 717 codecs = devm_kcalloc(dev, codec_num, sizeof(*codecs), GFP_KERNEL); 718 if (!codecs) 719 return -ENOMEM; 720 721 /* generate codec name on different links in the same group */ 722 for (link_next = link; link_next && link_next->num_adr && 723 i < cpu_dai_num; link_next++) { 724 const struct snd_soc_acpi_endpoint *endpoints; 725 726 endpoints = link_next->adr_d->endpoints; 727 if (group_id && (!endpoints->aggregated || 728 endpoints->group_id != group_id)) 729 continue; 730 731 /* skip the link excluded by this processed group */ 732 if (cpu_dai_id[i] != ffs(link_next->mask) - 1) 733 continue; 734 735 ret = create_codec_dai_name(dev, link_next, codecs, codec_idx, 736 codec_conf, codec_count, codec_conf_index); 737 if (ret < 0) 738 return ret; 739 740 /* check next link to create codec dai in the processed group */ 741 i++; 742 codec_idx += link_next->num_adr; 743 } 744 745 /* find codec info to create BE DAI */ 746 codec_index = find_codec_info_part(link->adr_d[0].adr); 747 if (codec_index < 0) 748 return codec_index; 749 750 cpu_dai_index = *cpu_id; 751 for_each_pcm_streams(stream) { 752 char *name, *cpu_name; 753 int playback, capture; 754 static const char * const sdw_stream_name[] = { 755 "SDW%d-Playback", 756 "SDW%d-Capture", 757 }; 758 759 if (!codec_info_list[codec_index].direction[stream]) 760 continue; 761 762 /* create stream name according to first link id */ 763 name = devm_kasprintf(dev, GFP_KERNEL, 764 sdw_stream_name[stream], cpu_dai_id[0]); 765 if (!name) 766 return -ENOMEM; 767 768 /* 769 * generate CPU DAI name base on the sdw link ID and 770 * PIN ID with offset of 2 according to sdw dai driver. 771 */ 772 for (k = 0; k < cpu_dai_num; k++) { 773 cpu_name = devm_kasprintf(dev, GFP_KERNEL, 774 "SDW%d Pin%d", cpu_dai_id[k], 775 j + SDW_INTEL_BIDIR_PDI_BASE); 776 if (!cpu_name) 777 return -ENOMEM; 778 779 if (cpu_dai_index >= sdw_cpu_dai_num) { 780 dev_err(dev, "invalid cpu dai index %d", 781 cpu_dai_index); 782 return -EINVAL; 783 } 784 785 cpus[cpu_dai_index++].dai_name = cpu_name; 786 } 787 788 if (*be_index >= sdw_be_num) { 789 dev_err(dev, " invalid be dai index %d", *be_index); 790 return -EINVAL; 791 } 792 793 if (*cpu_id >= sdw_cpu_dai_num) { 794 dev_err(dev, " invalid cpu dai index %d", *cpu_id); 795 return -EINVAL; 796 } 797 798 playback = (stream == SNDRV_PCM_STREAM_PLAYBACK); 799 capture = (stream == SNDRV_PCM_STREAM_CAPTURE); 800 init_dai_link(dai_links + *be_index, *be_index, name, 801 playback, capture, 802 cpus + *cpu_id, cpu_dai_num, 803 codecs, codec_num, 804 NULL, &sdw_ops); 805 806 ret = set_codec_init_func(link, dai_links + (*be_index)++, 807 playback, group_id); 808 if (ret < 0) { 809 dev_err(dev, "failed to init codec %d", codec_index); 810 return ret; 811 } 812 813 *cpu_id += cpu_dai_num; 814 j++; 815 } 816 817 return 0; 818 } 819 820 /* 821 * DAI link ID of SSP & DMIC & HDMI are based on last 822 * link ID used by sdw link. Since be_id may be changed 823 * in init func of sdw codec, it is not equal to be_id 824 */ 825 static inline int get_next_be_id(struct snd_soc_dai_link *links, 826 int be_id) 827 { 828 return links[be_id - 1].id + 1; 829 } 830 831 #define IDISP_CODEC_MASK 0x4 832 833 static int sof_card_codec_conf_alloc(struct device *dev, 834 struct snd_soc_acpi_mach_params *mach_params, 835 struct snd_soc_codec_conf **codec_conf, 836 int *codec_conf_count) 837 { 838 const struct snd_soc_acpi_link_adr *adr_link; 839 struct snd_soc_codec_conf *c_conf; 840 int num_codecs = 0; 841 int i; 842 843 adr_link = mach_params->links; 844 if (!adr_link) 845 return -EINVAL; 846 847 /* generate DAI links by each sdw link */ 848 for (; adr_link->num_adr; adr_link++) { 849 for (i = 0; i < adr_link->num_adr; i++) { 850 if (!adr_link->adr_d[i].name_prefix) { 851 dev_err(dev, "codec 0x%llx does not have a name prefix\n", 852 adr_link->adr_d[i].adr); 853 return -EINVAL; 854 } 855 } 856 num_codecs += adr_link->num_adr; 857 } 858 859 c_conf = devm_kzalloc(dev, num_codecs * sizeof(*c_conf), GFP_KERNEL); 860 if (!c_conf) 861 return -ENOMEM; 862 863 *codec_conf = c_conf; 864 *codec_conf_count = num_codecs; 865 866 return 0; 867 } 868 869 static int sof_card_dai_links_create(struct device *dev, 870 struct snd_soc_acpi_mach *mach, 871 struct snd_soc_card *card) 872 { 873 int ssp_num, sdw_be_num = 0, hdmi_num = 0, dmic_num; 874 struct mc_private *ctx = snd_soc_card_get_drvdata(card); 875 struct snd_soc_dai_link_component *idisp_components; 876 struct snd_soc_dai_link_component *ssp_components; 877 struct snd_soc_acpi_mach_params *mach_params; 878 const struct snd_soc_acpi_link_adr *adr_link; 879 struct snd_soc_dai_link_component *cpus; 880 struct snd_soc_codec_conf *codec_conf; 881 int codec_conf_count; 882 int codec_conf_index = 0; 883 bool group_generated[SDW_MAX_GROUPS]; 884 int ssp_codec_index, ssp_mask; 885 struct snd_soc_dai_link *links; 886 int num_links, link_id = 0; 887 char *name, *cpu_name; 888 int total_cpu_dai_num; 889 int sdw_cpu_dai_num; 890 int i, j, be_id = 0; 891 int cpu_id = 0; 892 int comp_num; 893 int ret; 894 895 mach_params = &mach->mach_params; 896 897 /* allocate codec conf, will be populated when dailinks are created */ 898 ret = sof_card_codec_conf_alloc(dev, mach_params, &codec_conf, &codec_conf_count); 899 if (ret < 0) 900 return ret; 901 902 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */ 903 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) 904 codec_info_list[i].amp_num = 0; 905 906 if (sof_sdw_quirk & SOF_SDW_TGL_HDMI) 907 hdmi_num = SOF_TGL_HDMI_COUNT; 908 else 909 hdmi_num = SOF_PRE_TGL_HDMI_COUNT; 910 911 ssp_mask = SOF_SSP_GET_PORT(sof_sdw_quirk); 912 /* 913 * on generic tgl platform, I2S or sdw mode is supported 914 * based on board rework. A ACPI device is registered in 915 * system only when I2S mode is supported, not sdw mode. 916 * Here check ACPI ID to confirm I2S is supported. 917 */ 918 ssp_codec_index = find_codec_info_acpi(mach->id); 919 ssp_num = ssp_codec_index >= 0 ? hweight_long(ssp_mask) : 0; 920 comp_num = hdmi_num + ssp_num; 921 922 ret = get_sdw_dailink_info(mach_params->links, 923 &sdw_be_num, &sdw_cpu_dai_num); 924 if (ret < 0) { 925 dev_err(dev, "failed to get sdw link info %d", ret); 926 return ret; 927 } 928 929 if (mach_params->codec_mask & IDISP_CODEC_MASK) 930 ctx->idisp_codec = true; 931 932 /* enable dmic01 & dmic16k */ 933 dmic_num = (sof_sdw_quirk & SOF_SDW_PCH_DMIC) ? 2 : 0; 934 comp_num += dmic_num; 935 936 dev_dbg(dev, "sdw %d, ssp %d, dmic %d, hdmi %d", sdw_be_num, ssp_num, 937 dmic_num, ctx->idisp_codec ? hdmi_num : 0); 938 939 /* allocate BE dailinks */ 940 num_links = comp_num + sdw_be_num; 941 links = devm_kcalloc(dev, num_links, sizeof(*links), GFP_KERNEL); 942 943 /* allocated CPU DAIs */ 944 total_cpu_dai_num = comp_num + sdw_cpu_dai_num; 945 cpus = devm_kcalloc(dev, total_cpu_dai_num, sizeof(*cpus), 946 GFP_KERNEL); 947 948 if (!links || !cpus) 949 return -ENOMEM; 950 951 /* SDW */ 952 if (!sdw_be_num) 953 goto SSP; 954 955 adr_link = mach_params->links; 956 if (!adr_link) 957 return -EINVAL; 958 959 /* 960 * SoundWire Slaves aggregated in the same group may be 961 * located on different hardware links. Clear array to indicate 962 * CPU DAIs for this group have not been generated. 963 */ 964 for (i = 0; i < SDW_MAX_GROUPS; i++) 965 group_generated[i] = false; 966 967 /* generate DAI links by each sdw link */ 968 for (; adr_link->num_adr; adr_link++) { 969 const struct snd_soc_acpi_endpoint *endpoint; 970 971 endpoint = adr_link->adr_d->endpoints; 972 if (endpoint->aggregated && !endpoint->group_id) { 973 dev_err(dev, "invalid group id on link %x", 974 adr_link->mask); 975 continue; 976 } 977 978 /* this group has been generated */ 979 if (endpoint->aggregated && 980 group_generated[endpoint->group_id]) 981 continue; 982 983 ret = create_sdw_dailink(dev, &be_id, links, sdw_be_num, 984 sdw_cpu_dai_num, cpus, adr_link, 985 &cpu_id, group_generated, 986 codec_conf, codec_conf_count, 987 &codec_conf_index); 988 if (ret < 0) { 989 dev_err(dev, "failed to create dai link %d", be_id); 990 return -ENOMEM; 991 } 992 } 993 994 /* non-sdw DAI follows sdw DAI */ 995 link_id = be_id; 996 997 /* get BE ID for non-sdw DAI */ 998 be_id = get_next_be_id(links, be_id); 999 1000 SSP: 1001 /* SSP */ 1002 if (!ssp_num) 1003 goto DMIC; 1004 1005 for (i = 0, j = 0; ssp_mask; i++, ssp_mask >>= 1) { 1006 struct sof_sdw_codec_info *info; 1007 int playback, capture; 1008 char *codec_name; 1009 1010 if (!(ssp_mask & 0x1)) 1011 continue; 1012 1013 name = devm_kasprintf(dev, GFP_KERNEL, 1014 "SSP%d-Codec", i); 1015 if (!name) 1016 return -ENOMEM; 1017 1018 cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", i); 1019 if (!cpu_name) 1020 return -ENOMEM; 1021 1022 ssp_components = devm_kzalloc(dev, sizeof(*ssp_components), 1023 GFP_KERNEL); 1024 if (!ssp_components) 1025 return -ENOMEM; 1026 1027 info = &codec_info_list[ssp_codec_index]; 1028 codec_name = devm_kasprintf(dev, GFP_KERNEL, "i2c-%s:0%d", 1029 info->acpi_id, j++); 1030 if (!codec_name) 1031 return -ENOMEM; 1032 1033 ssp_components->name = codec_name; 1034 ssp_components->dai_name = info->dai_name; 1035 cpus[cpu_id].dai_name = cpu_name; 1036 1037 playback = info->direction[SNDRV_PCM_STREAM_PLAYBACK]; 1038 capture = info->direction[SNDRV_PCM_STREAM_CAPTURE]; 1039 init_dai_link(links + link_id, be_id, name, 1040 playback, capture, 1041 cpus + cpu_id, 1, 1042 ssp_components, 1, 1043 NULL, info->ops); 1044 1045 ret = info->init(NULL, links + link_id, info, 0); 1046 if (ret < 0) 1047 return ret; 1048 1049 INC_ID(be_id, cpu_id, link_id); 1050 } 1051 1052 DMIC: 1053 /* dmic */ 1054 if (dmic_num > 0) { 1055 cpus[cpu_id].dai_name = "DMIC01 Pin"; 1056 init_dai_link(links + link_id, be_id, "dmic01", 1057 0, 1, // DMIC only supports capture 1058 cpus + cpu_id, 1, 1059 dmic_component, 1, 1060 sof_sdw_dmic_init, NULL); 1061 INC_ID(be_id, cpu_id, link_id); 1062 1063 cpus[cpu_id].dai_name = "DMIC16k Pin"; 1064 init_dai_link(links + link_id, be_id, "dmic16k", 1065 0, 1, // DMIC only supports capture 1066 cpus + cpu_id, 1, 1067 dmic_component, 1, 1068 /* don't call sof_sdw_dmic_init() twice */ 1069 NULL, NULL); 1070 INC_ID(be_id, cpu_id, link_id); 1071 } 1072 1073 /* HDMI */ 1074 if (hdmi_num > 0) { 1075 idisp_components = devm_kcalloc(dev, hdmi_num, 1076 sizeof(*idisp_components), 1077 GFP_KERNEL); 1078 if (!idisp_components) 1079 return -ENOMEM; 1080 } 1081 1082 for (i = 0; i < hdmi_num; i++) { 1083 name = devm_kasprintf(dev, GFP_KERNEL, 1084 "iDisp%d", i + 1); 1085 if (!name) 1086 return -ENOMEM; 1087 1088 if (ctx->idisp_codec) { 1089 idisp_components[i].name = "ehdaudio0D2"; 1090 idisp_components[i].dai_name = devm_kasprintf(dev, 1091 GFP_KERNEL, 1092 "intel-hdmi-hifi%d", 1093 i + 1); 1094 if (!idisp_components[i].dai_name) 1095 return -ENOMEM; 1096 } else { 1097 idisp_components[i].name = "snd-soc-dummy"; 1098 idisp_components[i].dai_name = "snd-soc-dummy-dai"; 1099 } 1100 1101 cpu_name = devm_kasprintf(dev, GFP_KERNEL, 1102 "iDisp%d Pin", i + 1); 1103 if (!cpu_name) 1104 return -ENOMEM; 1105 1106 cpus[cpu_id].dai_name = cpu_name; 1107 init_dai_link(links + link_id, be_id, name, 1108 1, 0, // HDMI only supports playback 1109 cpus + cpu_id, 1, 1110 idisp_components + i, 1, 1111 sof_sdw_hdmi_init, NULL); 1112 INC_ID(be_id, cpu_id, link_id); 1113 } 1114 1115 card->dai_link = links; 1116 card->num_links = num_links; 1117 1118 card->codec_conf = codec_conf; 1119 card->num_configs = codec_conf_count; 1120 1121 return 0; 1122 } 1123 1124 static int sof_sdw_card_late_probe(struct snd_soc_card *card) 1125 { 1126 int i, ret; 1127 1128 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) { 1129 if (!codec_info_list[i].late_probe) 1130 continue; 1131 1132 ret = codec_info_list[i].codec_card_late_probe(card); 1133 if (ret < 0) 1134 return ret; 1135 } 1136 1137 return sof_sdw_hdmi_card_late_probe(card); 1138 } 1139 1140 /* SoC card */ 1141 static const char sdw_card_long_name[] = "Intel Soundwire SOF"; 1142 1143 static struct snd_soc_card card_sof_sdw = { 1144 .name = "soundwire", 1145 .owner = THIS_MODULE, 1146 .late_probe = sof_sdw_card_late_probe, 1147 }; 1148 1149 static int mc_probe(struct platform_device *pdev) 1150 { 1151 struct snd_soc_card *card = &card_sof_sdw; 1152 struct snd_soc_acpi_mach *mach; 1153 struct mc_private *ctx; 1154 int amp_num = 0, i; 1155 int ret; 1156 1157 dev_dbg(&pdev->dev, "Entry %s\n", __func__); 1158 1159 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); 1160 if (!ctx) 1161 return -ENOMEM; 1162 1163 dmi_check_system(sof_sdw_quirk_table); 1164 1165 if (quirk_override != -1) { 1166 dev_info(&pdev->dev, "Overriding quirk 0x%lx => 0x%x\n", 1167 sof_sdw_quirk, quirk_override); 1168 sof_sdw_quirk = quirk_override; 1169 } 1170 log_quirks(&pdev->dev); 1171 1172 INIT_LIST_HEAD(&ctx->hdmi_pcm_list); 1173 1174 card->dev = &pdev->dev; 1175 snd_soc_card_set_drvdata(card, ctx); 1176 1177 mach = pdev->dev.platform_data; 1178 ret = sof_card_dai_links_create(&pdev->dev, mach, 1179 card); 1180 if (ret < 0) 1181 return ret; 1182 1183 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv; 1184 1185 /* 1186 * the default amp_num is zero for each codec and 1187 * amp_num will only be increased for active amp 1188 * codecs on used platform 1189 */ 1190 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) 1191 amp_num += codec_info_list[i].amp_num; 1192 1193 card->components = devm_kasprintf(card->dev, GFP_KERNEL, 1194 "cfg-spk:%d cfg-amp:%d", 1195 (sof_sdw_quirk & SOF_SDW_FOUR_SPK) 1196 ? 4 : 2, amp_num); 1197 if (!card->components) 1198 return -ENOMEM; 1199 1200 card->long_name = sdw_card_long_name; 1201 1202 /* Register the card */ 1203 ret = devm_snd_soc_register_card(&pdev->dev, card); 1204 if (ret) { 1205 dev_err(card->dev, "snd_soc_register_card failed %d\n", ret); 1206 return ret; 1207 } 1208 1209 platform_set_drvdata(pdev, card); 1210 1211 return ret; 1212 } 1213 1214 static int mc_remove(struct platform_device *pdev) 1215 { 1216 struct snd_soc_card *card = platform_get_drvdata(pdev); 1217 struct snd_soc_dai_link *link; 1218 int ret; 1219 int i, j; 1220 1221 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) { 1222 if (!codec_info_list[i].exit) 1223 continue; 1224 /* 1225 * We don't need to call .exit function if there is no matched 1226 * dai link found. 1227 */ 1228 for_each_card_prelinks(card, j, link) { 1229 if (!strcmp(link->codecs[0].dai_name, 1230 codec_info_list[i].dai_name)) { 1231 ret = codec_info_list[i].exit(&pdev->dev, link); 1232 if (ret) 1233 dev_warn(&pdev->dev, 1234 "codec exit failed %d\n", 1235 ret); 1236 break; 1237 } 1238 } 1239 } 1240 1241 return 0; 1242 } 1243 1244 static struct platform_driver sof_sdw_driver = { 1245 .driver = { 1246 .name = "sof_sdw", 1247 .pm = &snd_soc_pm_ops, 1248 }, 1249 .probe = mc_probe, 1250 .remove = mc_remove, 1251 }; 1252 1253 module_platform_driver(sof_sdw_driver); 1254 1255 MODULE_DESCRIPTION("ASoC SoundWire Generic Machine driver"); 1256 MODULE_AUTHOR("Bard Liao <yung-chuan.liao@linux.intel.com>"); 1257 MODULE_AUTHOR("Rander Wang <rander.wang@linux.intel.com>"); 1258 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>"); 1259 MODULE_LICENSE("GPL v2"); 1260 MODULE_ALIAS("platform:sof_sdw"); 1261