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