1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright(c) 2024 Advanced Micro Devices, Inc. 3 4 /* 5 * acp-sdw-legacy-mach - ASoC legacy Machine driver for AMD SoundWire platforms 6 */ 7 8 #include <linux/bitmap.h> 9 #include <linux/device.h> 10 #include <linux/dmi.h> 11 #include <linux/module.h> 12 #include <linux/soundwire/sdw.h> 13 #include <linux/soundwire/sdw_type.h> 14 #include <sound/soc.h> 15 #include <sound/soc-acpi.h> 16 #include "soc_amd_sdw_common.h" 17 #include "../../codecs/rt711.h" 18 19 static unsigned long soc_sdw_quirk = RT711_JD1; 20 static int quirk_override = -1; 21 module_param_named(quirk, quirk_override, int, 0444); 22 MODULE_PARM_DESC(quirk, "Board-specific quirk override"); 23 24 static void log_quirks(struct device *dev) 25 { 26 if (SOC_JACK_JDSRC(soc_sdw_quirk)) 27 dev_dbg(dev, "quirk realtek,jack-detect-source %ld\n", 28 SOC_JACK_JDSRC(soc_sdw_quirk)); 29 if (soc_sdw_quirk & ASOC_SDW_ACP_DMIC) 30 dev_dbg(dev, "quirk SOC_SDW_ACP_DMIC enabled\n"); 31 if (soc_sdw_quirk & ASOC_SDW_CODEC_SPKR) 32 dev_dbg(dev, "quirk ASOC_SDW_CODEC_SPKR enabled\n"); 33 } 34 35 static int soc_sdw_quirk_cb(const struct dmi_system_id *id) 36 { 37 soc_sdw_quirk = (unsigned long)id->driver_data; 38 return 1; 39 } 40 41 static const struct dmi_system_id soc_sdw_quirk_table[] = { 42 { 43 .callback = soc_sdw_quirk_cb, 44 .matches = { 45 DMI_MATCH(DMI_SYS_VENDOR, "AMD"), 46 DMI_MATCH(DMI_PRODUCT_NAME, "Birman-PHX"), 47 }, 48 .driver_data = (void *)RT711_JD2, 49 }, 50 { 51 .callback = soc_sdw_quirk_cb, 52 .matches = { 53 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 54 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0D80"), 55 }, 56 .driver_data = (void *)(ASOC_SDW_CODEC_SPKR), 57 }, 58 { 59 .callback = soc_sdw_quirk_cb, 60 .matches = { 61 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 62 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0D81"), 63 }, 64 .driver_data = (void *)(ASOC_SDW_CODEC_SPKR), 65 }, 66 { 67 .callback = soc_sdw_quirk_cb, 68 .matches = { 69 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 70 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0D82"), 71 }, 72 .driver_data = (void *)(ASOC_SDW_CODEC_SPKR), 73 }, 74 { 75 .callback = soc_sdw_quirk_cb, 76 .matches = { 77 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 78 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0D83"), 79 }, 80 .driver_data = (void *)(ASOC_SDW_CODEC_SPKR), 81 }, 82 { 83 .callback = soc_sdw_quirk_cb, 84 .matches = { 85 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 86 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0DD3"), 87 }, 88 .driver_data = (void *)(ASOC_SDW_CODEC_SPKR), 89 }, 90 { 91 .callback = soc_sdw_quirk_cb, 92 .matches = { 93 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"), 94 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0DD4"), 95 }, 96 .driver_data = (void *)(ASOC_SDW_CODEC_SPKR), 97 }, 98 { 99 .callback = soc_sdw_quirk_cb, 100 .matches = { 101 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 102 DMI_MATCH(DMI_PRODUCT_SKU, "21YW"), 103 }, 104 .driver_data = (void *)((ASOC_SDW_CODEC_SPKR) | (ASOC_SDW_ACP_DMIC)), 105 }, 106 { 107 .callback = soc_sdw_quirk_cb, 108 .matches = { 109 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 110 DMI_MATCH(DMI_PRODUCT_SKU, "21YX"), 111 }, 112 .driver_data = (void *)((ASOC_SDW_CODEC_SPKR) | (ASOC_SDW_ACP_DMIC)), 113 }, 114 { 115 .callback = soc_sdw_quirk_cb, 116 .matches = { /* Lenovo P16s G5 AMD */ 117 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 118 DMI_MATCH(DMI_PRODUCT_SKU, "21XG"), 119 }, 120 .driver_data = (void *)(ASOC_SDW_ACP_DMIC), 121 }, 122 { 123 .callback = soc_sdw_quirk_cb, 124 .matches = { /* Lenovo P16s G5 AMD */ 125 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 126 DMI_MATCH(DMI_PRODUCT_SKU, "21XH"), 127 }, 128 .driver_data = (void *)(ASOC_SDW_ACP_DMIC), 129 }, 130 { 131 .callback = soc_sdw_quirk_cb, 132 .matches = { 133 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK COMPUTER INC."), 134 DMI_MATCH(DMI_PRODUCT_NAME, "HN7306EA"), 135 }, 136 .driver_data = (void *)(ASOC_SDW_ACP_DMIC), 137 }, 138 {} 139 }; 140 141 static const struct snd_soc_ops sdw_ops = { 142 .startup = asoc_sdw_startup, 143 .prepare = asoc_sdw_prepare, 144 .trigger = asoc_sdw_trigger, 145 .hw_params = asoc_sdw_hw_params, 146 .hw_free = asoc_sdw_hw_free, 147 .shutdown = asoc_sdw_shutdown, 148 }; 149 150 static const char * const type_strings[] = {"SimpleJack", "SmartAmp", "SmartMic"}; 151 152 static int create_sdw_dailink(struct snd_soc_card *card, 153 struct asoc_sdw_dailink *soc_dai, 154 struct snd_soc_dai_link **dai_links, 155 int *be_id, struct snd_soc_codec_conf **codec_conf, 156 struct snd_soc_dai_link_component *sdw_platform_component) 157 { 158 struct device *dev = card->dev; 159 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); 160 struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private; 161 struct asoc_sdw_endpoint *soc_end; 162 int cpu_pin_id; 163 int stream; 164 int ret; 165 166 list_for_each_entry(soc_end, &soc_dai->endpoints, list) { 167 if (soc_end->name_prefix) { 168 (*codec_conf)->dlc.name = soc_end->codec_name; 169 (*codec_conf)->name_prefix = soc_end->name_prefix; 170 (*codec_conf)++; 171 } 172 173 if (soc_end->include_sidecar) { 174 ret = soc_end->codec_info->add_sidecar(card, dai_links, codec_conf); 175 if (ret) 176 return ret; 177 } 178 } 179 180 for_each_pcm_streams(stream) { 181 static const char * const sdw_stream_name[] = { 182 "SDW%d-PIN%d-PLAYBACK", 183 "SDW%d-PIN%d-CAPTURE", 184 "SDW%d-PIN%d-PLAYBACK-%s", 185 "SDW%d-PIN%d-CAPTURE-%s", 186 }; 187 struct snd_soc_dai_link_ch_map *codec_maps; 188 struct snd_soc_dai_link_component *codecs; 189 struct snd_soc_dai_link_component *cpus; 190 int num_cpus = hweight32(soc_dai->link_mask[stream]); 191 int num_codecs = soc_dai->num_devs[stream]; 192 int playback, capture; 193 int j = 0; 194 char *name; 195 196 if (!soc_dai->num_devs[stream]) 197 continue; 198 199 soc_end = list_first_entry(&soc_dai->endpoints, 200 struct asoc_sdw_endpoint, list); 201 202 *be_id = soc_end->dai_info->dailink[stream]; 203 if (*be_id < 0) { 204 dev_err(dev, "Invalid dailink id %d\n", *be_id); 205 return -EINVAL; 206 } 207 208 switch (amd_ctx->acp_rev) { 209 case ACP63_PCI_REV: 210 ret = get_acp63_cpu_pin_id(ffs(soc_end->link_mask - 1), 211 *be_id, &cpu_pin_id, dev); 212 if (ret) 213 return ret; 214 break; 215 case ACP70_PCI_REV: 216 case ACP71_PCI_REV: 217 case ACP72_PCI_REV: 218 ret = get_acp70_cpu_pin_id(ffs(soc_end->link_mask - 1), 219 *be_id, &cpu_pin_id, dev); 220 if (ret) 221 return ret; 222 break; 223 default: 224 return -EINVAL; 225 } 226 /* create stream name according to first link id */ 227 if (ctx->append_dai_type) { 228 name = devm_kasprintf(dev, GFP_KERNEL, 229 sdw_stream_name[stream + 2], 230 ffs(soc_end->link_mask) - 1, 231 cpu_pin_id, 232 type_strings[soc_end->dai_info->dai_type]); 233 } else { 234 name = devm_kasprintf(dev, GFP_KERNEL, 235 sdw_stream_name[stream], 236 ffs(soc_end->link_mask) - 1, 237 cpu_pin_id); 238 } 239 if (!name) 240 return -ENOMEM; 241 242 cpus = devm_kcalloc(dev, num_cpus, sizeof(*cpus), GFP_KERNEL); 243 if (!cpus) 244 return -ENOMEM; 245 246 codecs = devm_kcalloc(dev, num_codecs, sizeof(*codecs), GFP_KERNEL); 247 if (!codecs) 248 return -ENOMEM; 249 250 codec_maps = devm_kcalloc(dev, num_codecs, sizeof(*codec_maps), GFP_KERNEL); 251 if (!codec_maps) 252 return -ENOMEM; 253 254 list_for_each_entry(soc_end, &soc_dai->endpoints, list) { 255 if (!soc_end->dai_info->direction[stream]) 256 continue; 257 258 int link_num = ffs(soc_end->link_mask) - 1; 259 260 cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 261 "SDW%d Pin%d", 262 link_num, cpu_pin_id); 263 if (!cpus->dai_name) 264 return -ENOMEM; 265 dev_dbg(dev, "cpu->dai_name:%s\n", cpus->dai_name); 266 267 codec_maps[j].cpu = 0; 268 codec_maps[j].codec = j; 269 270 codecs[j].name = soc_end->codec_name; 271 codecs[j].dai_name = soc_end->dai_info->dai_name; 272 j++; 273 } 274 275 WARN_ON(j != num_codecs); 276 277 playback = (stream == SNDRV_PCM_STREAM_PLAYBACK); 278 capture = (stream == SNDRV_PCM_STREAM_CAPTURE); 279 280 asoc_sdw_init_dai_link(dev, *dai_links, be_id, name, playback, capture, 281 cpus, num_cpus, sdw_platform_component, 282 1, codecs, num_codecs, 283 0, asoc_sdw_rtd_init, &sdw_ops); 284 /* 285 * SoundWire DAILINKs use 'stream' functions and Bank Switch operations 286 * based on wait_for_completion(), tag them as 'nonatomic'. 287 */ 288 (*dai_links)->nonatomic = true; 289 (*dai_links)->ch_maps = codec_maps; 290 291 list_for_each_entry(soc_end, &soc_dai->endpoints, list) { 292 if (soc_end->dai_info->init) 293 soc_end->dai_info->init(card, *dai_links, 294 soc_end->codec_info, 295 playback); 296 } 297 298 (*dai_links)++; 299 } 300 301 return 0; 302 } 303 304 static int create_sdw_dailinks(struct snd_soc_card *card, 305 struct snd_soc_dai_link **dai_links, int *be_id, 306 struct asoc_sdw_dailink *soc_dais, int num_dais, 307 struct snd_soc_codec_conf **codec_conf) 308 { 309 struct device *dev = card->dev; 310 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); 311 struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private; 312 struct snd_soc_dai_link_component *sdw_platform_component; 313 int i; 314 int ret; 315 316 sdw_platform_component = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), 317 GFP_KERNEL); 318 if (!sdw_platform_component) 319 return -ENOMEM; 320 321 switch (amd_ctx->acp_rev) { 322 case ACP63_PCI_REV: 323 case ACP70_PCI_REV: 324 case ACP71_PCI_REV: 325 case ACP72_PCI_REV: 326 sdw_platform_component->name = "amd_ps_sdw_dma.0"; 327 break; 328 default: 329 return -EINVAL; 330 } 331 332 /* generate DAI links by each sdw link */ 333 for (i = 0; i < num_dais && soc_dais->initialised; i++) { 334 int current_be_id = 0; 335 336 ret = create_sdw_dailink(card, soc_dais, dai_links, 337 ¤t_be_id, codec_conf, sdw_platform_component); 338 if (ret) 339 return ret; 340 341 /* Update the be_id to match the highest ID used for SDW link */ 342 if (*be_id < current_be_id) 343 *be_id = current_be_id; 344 345 soc_dais++; 346 } 347 348 return 0; 349 } 350 351 static int create_dmic_dailinks(struct snd_soc_card *card, 352 struct snd_soc_dai_link **dai_links, int *be_id, int no_pcm) 353 { 354 struct device *dev = card->dev; 355 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); 356 struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private; 357 struct snd_soc_dai_link_component *pdm_cpu; 358 struct snd_soc_dai_link_component *pdm_platform; 359 int ret; 360 361 pdm_cpu = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), GFP_KERNEL); 362 if (!pdm_cpu) 363 return -ENOMEM; 364 365 pdm_platform = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), GFP_KERNEL); 366 if (!pdm_platform) 367 return -ENOMEM; 368 369 switch (amd_ctx->acp_rev) { 370 case ACP63_PCI_REV: 371 case ACP70_PCI_REV: 372 case ACP71_PCI_REV: 373 case ACP72_PCI_REV: 374 pdm_cpu->name = "acp_ps_pdm_dma.0"; 375 pdm_platform->name = "acp_ps_pdm_dma.0"; 376 break; 377 default: 378 return -EINVAL; 379 } 380 381 *be_id = ACP_DMIC_BE_ID; 382 ret = asoc_sdw_init_simple_dai_link(dev, *dai_links, be_id, "acp-dmic-codec", 383 0, 1, // DMIC only supports capture 384 pdm_cpu->name, pdm_platform->name, 385 "dmic-codec.0", "dmic-hifi", no_pcm, 386 asoc_sdw_dmic_init, NULL); 387 if (ret) 388 return ret; 389 390 (*dai_links)++; 391 392 return 0; 393 } 394 395 static int soc_card_dai_links_create(struct snd_soc_card *card) 396 { 397 struct device *dev = card->dev; 398 int sdw_be_num = 0, dmic_num = 0; 399 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); 400 struct snd_soc_aux_dev *soc_aux; 401 struct snd_soc_codec_conf *codec_conf; 402 struct snd_soc_dai_link *dai_links; 403 int num_devs = 0; 404 int num_ends = 0; 405 int num_aux = 0; 406 int num_confs; 407 int num_links; 408 int be_id = 0; 409 int ret; 410 411 ret = asoc_sdw_count_sdw_endpoints(card, &num_devs, &num_ends, &num_aux); 412 if (ret < 0) { 413 dev_err(dev, "failed to count devices/endpoints: %d\n", ret); 414 return ret; 415 } 416 417 num_confs = num_ends; 418 419 /* One per DAI link, worst case is a DAI link for every endpoint */ 420 struct asoc_sdw_dailink *soc_dais __free(kfree) = 421 kzalloc_objs(*soc_dais, num_ends); 422 if (!soc_dais) 423 return -ENOMEM; 424 425 /* One per endpoint, ie. each DAI on each codec/amp */ 426 struct asoc_sdw_endpoint *soc_ends __free(kfree) = 427 kzalloc_objs(*soc_ends, num_ends); 428 if (!soc_ends) 429 return -ENOMEM; 430 431 soc_aux = devm_kcalloc(dev, num_aux, sizeof(*soc_aux), GFP_KERNEL); 432 if (!soc_aux) 433 return -ENOMEM; 434 435 ret = asoc_sdw_parse_sdw_endpoints(card, soc_aux, soc_dais, soc_ends, &num_confs); 436 if (ret < 0) 437 return ret; 438 439 sdw_be_num = ret; 440 441 /* enable dmic */ 442 if (soc_sdw_quirk & ASOC_SDW_ACP_DMIC) 443 dmic_num = 1; 444 445 dev_dbg(dev, "sdw %d, dmic %d", sdw_be_num, dmic_num); 446 447 codec_conf = devm_kcalloc(dev, num_confs, sizeof(*codec_conf), GFP_KERNEL); 448 if (!codec_conf) 449 return -ENOMEM; 450 451 /* allocate BE dailinks */ 452 num_links = sdw_be_num + dmic_num; 453 dai_links = devm_kcalloc(dev, num_links, sizeof(*dai_links), GFP_KERNEL); 454 if (!dai_links) 455 return -ENOMEM; 456 457 card->codec_conf = codec_conf; 458 card->num_configs = num_confs; 459 card->dai_link = dai_links; 460 card->num_links = num_links; 461 card->aux_dev = soc_aux; 462 card->num_aux_devs = num_aux; 463 464 /* SDW */ 465 if (sdw_be_num) { 466 ret = create_sdw_dailinks(card, &dai_links, &be_id, 467 soc_dais, num_ends, &codec_conf); 468 if (ret) 469 return ret; 470 } 471 472 /* dmic */ 473 if (dmic_num > 0) { 474 if (ctx->ignore_internal_dmic) { 475 dev_warn(dev, "Ignoring ACP DMIC\n"); 476 } else { 477 ret = create_dmic_dailinks(card, &dai_links, &be_id, 0); 478 if (ret) 479 return ret; 480 } 481 } 482 483 WARN_ON(codec_conf != card->codec_conf + card->num_configs); 484 WARN_ON(dai_links != card->dai_link + card->num_links); 485 486 return ret; 487 } 488 489 static int mc_probe(struct platform_device *pdev) 490 { 491 struct snd_soc_acpi_mach *mach = dev_get_platdata(&pdev->dev); 492 struct snd_soc_card *card; 493 struct amd_mc_ctx *amd_ctx; 494 struct asoc_sdw_mc_private *ctx; 495 int amp_num = 0, i; 496 int ret; 497 498 amd_ctx = devm_kzalloc(&pdev->dev, sizeof(*amd_ctx), GFP_KERNEL); 499 if (!amd_ctx) 500 return -ENOMEM; 501 502 amd_ctx->acp_rev = mach->mach_params.subsystem_rev; 503 amd_ctx->max_sdw_links = ACP63_SDW_MAX_LINKS; 504 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); 505 if (!ctx) 506 return -ENOMEM; 507 ctx->codec_info_list_count = asoc_sdw_get_codec_info_list_count(); 508 ctx->private = amd_ctx; 509 card = &ctx->card; 510 card->dev = &pdev->dev; 511 card->name = "amd-soundwire"; 512 card->owner = THIS_MODULE; 513 card->late_probe = asoc_sdw_card_late_probe; 514 515 snd_soc_card_set_drvdata(card, ctx); 516 if (mach->mach_params.subsystem_id_set) 517 snd_soc_card_set_pci_ssid(card, 518 mach->mach_params.subsystem_vendor, 519 mach->mach_params.subsystem_device); 520 521 dmi_check_system(soc_sdw_quirk_table); 522 523 if (quirk_override != -1) { 524 dev_info(card->dev, "Overriding quirk 0x%lx => 0x%x\n", 525 soc_sdw_quirk, quirk_override); 526 soc_sdw_quirk = quirk_override; 527 } 528 529 log_quirks(card->dev); 530 531 ctx->mc_quirk = soc_sdw_quirk; 532 dev_dbg(card->dev, "legacy quirk 0x%lx\n", ctx->mc_quirk); 533 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */ 534 for (i = 0; i < ctx->codec_info_list_count; i++) 535 codec_info_list[i].amp_num = 0; 536 537 ret = soc_card_dai_links_create(card); 538 if (ret < 0) 539 return ret; 540 541 /* 542 * the default amp_num is zero for each codec and 543 * amp_num will only be increased for active amp 544 * codecs on used platform 545 */ 546 for (i = 0; i < ctx->codec_info_list_count; i++) 547 amp_num += codec_info_list[i].amp_num; 548 549 card->components = devm_kasprintf(card->dev, GFP_KERNEL, 550 " cfg-amp:%d", amp_num); 551 if (!card->components) 552 return -ENOMEM; 553 if (soc_sdw_quirk & ASOC_SDW_ACP_DMIC) { 554 card->components = devm_kasprintf(card->dev, GFP_KERNEL, 555 "%s mic:acp-dmic cfg-mics:%d", 556 card->components, 557 1); 558 if (!card->components) 559 return -ENOMEM; 560 } 561 562 /* Register the card */ 563 ret = devm_snd_soc_register_card(card->dev, card); 564 if (ret) { 565 dev_err_probe(card->dev, ret, "snd_soc_register_card failed %d\n", ret); 566 asoc_sdw_mc_dailink_exit_loop(card); 567 return ret; 568 } 569 570 platform_set_drvdata(pdev, card); 571 572 return ret; 573 } 574 575 static void mc_remove(struct platform_device *pdev) 576 { 577 struct snd_soc_card *card = platform_get_drvdata(pdev); 578 579 asoc_sdw_mc_dailink_exit_loop(card); 580 } 581 582 static const struct platform_device_id mc_id_table[] = { 583 { "amd_sdw", }, 584 {} 585 }; 586 MODULE_DEVICE_TABLE(platform, mc_id_table); 587 588 static struct platform_driver soc_sdw_driver = { 589 .driver = { 590 .name = "amd_sdw", 591 .pm = &snd_soc_pm_ops, 592 }, 593 .probe = mc_probe, 594 .remove = mc_remove, 595 .id_table = mc_id_table, 596 }; 597 598 module_platform_driver(soc_sdw_driver); 599 600 MODULE_DESCRIPTION("ASoC AMD SoundWire Legacy Generic Machine driver"); 601 MODULE_AUTHOR("Vijendar Mukunda <Vijendar.Mukunda@amd.com>"); 602 MODULE_LICENSE("GPL"); 603 MODULE_IMPORT_NS("SND_SOC_SDW_UTILS"); 604 MODULE_IMPORT_NS("SND_SOC_AMD_SDW_MACH"); 605