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