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 dev_dbg(dev, "cpu->dai_name:%s\n", cpus->dai_name); 264 if (!cpus->dai_name) 265 return -ENOMEM; 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, 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 ret; 314 315 sdw_platform_component = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), 316 GFP_KERNEL); 317 if (!sdw_platform_component) 318 return -ENOMEM; 319 320 switch (amd_ctx->acp_rev) { 321 case ACP63_PCI_REV: 322 case ACP70_PCI_REV: 323 case ACP71_PCI_REV: 324 case ACP72_PCI_REV: 325 sdw_platform_component->name = "amd_ps_sdw_dma.0"; 326 break; 327 default: 328 return -EINVAL; 329 } 330 331 /* generate DAI links by each sdw link */ 332 while (soc_dais->initialised) { 333 int current_be_id = 0; 334 335 ret = create_sdw_dailink(card, soc_dais, dai_links, 336 ¤t_be_id, codec_conf, sdw_platform_component); 337 if (ret) 338 return ret; 339 340 /* Update the be_id to match the highest ID used for SDW link */ 341 if (*be_id < current_be_id) 342 *be_id = current_be_id; 343 344 soc_dais++; 345 } 346 347 return 0; 348 } 349 350 static int create_dmic_dailinks(struct snd_soc_card *card, 351 struct snd_soc_dai_link **dai_links, int *be_id, int no_pcm) 352 { 353 struct device *dev = card->dev; 354 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); 355 struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private; 356 struct snd_soc_dai_link_component *pdm_cpu; 357 struct snd_soc_dai_link_component *pdm_platform; 358 int ret; 359 360 pdm_cpu = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), GFP_KERNEL); 361 if (!pdm_cpu) 362 return -ENOMEM; 363 364 pdm_platform = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), GFP_KERNEL); 365 if (!pdm_platform) 366 return -ENOMEM; 367 368 switch (amd_ctx->acp_rev) { 369 case ACP63_PCI_REV: 370 case ACP70_PCI_REV: 371 case ACP71_PCI_REV: 372 case ACP72_PCI_REV: 373 pdm_cpu->name = "acp_ps_pdm_dma.0"; 374 pdm_platform->name = "acp_ps_pdm_dma.0"; 375 break; 376 default: 377 return -EINVAL; 378 } 379 380 *be_id = ACP_DMIC_BE_ID; 381 ret = asoc_sdw_init_simple_dai_link(dev, *dai_links, be_id, "acp-dmic-codec", 382 0, 1, // DMIC only supports capture 383 pdm_cpu->name, pdm_platform->name, 384 "dmic-codec.0", "dmic-hifi", no_pcm, 385 asoc_sdw_dmic_init, NULL); 386 if (ret) 387 return ret; 388 389 (*dai_links)++; 390 391 return 0; 392 } 393 394 static int soc_card_dai_links_create(struct snd_soc_card *card) 395 { 396 struct device *dev = card->dev; 397 int sdw_be_num = 0, dmic_num = 0; 398 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); 399 struct snd_soc_aux_dev *soc_aux; 400 struct snd_soc_codec_conf *codec_conf; 401 struct snd_soc_dai_link *dai_links; 402 int num_devs = 0; 403 int num_ends = 0; 404 int num_aux = 0; 405 int num_confs; 406 int num_links; 407 int be_id = 0; 408 int ret; 409 410 ret = asoc_sdw_count_sdw_endpoints(card, &num_devs, &num_ends, &num_aux); 411 if (ret < 0) { 412 dev_err(dev, "failed to count devices/endpoints: %d\n", ret); 413 return ret; 414 } 415 416 num_confs = num_ends; 417 418 /* One per DAI link, worst case is a DAI link for every endpoint */ 419 struct asoc_sdw_dailink *soc_dais __free(kfree) = 420 kzalloc_objs(*soc_dais, num_ends); 421 if (!soc_dais) 422 return -ENOMEM; 423 424 /* One per endpoint, ie. each DAI on each codec/amp */ 425 struct asoc_sdw_endpoint *soc_ends __free(kfree) = 426 kzalloc_objs(*soc_ends, num_ends); 427 if (!soc_ends) 428 return -ENOMEM; 429 430 soc_aux = devm_kcalloc(dev, num_aux, sizeof(*soc_aux), GFP_KERNEL); 431 if (!soc_aux) 432 return -ENOMEM; 433 434 ret = asoc_sdw_parse_sdw_endpoints(card, soc_aux, soc_dais, soc_ends, &num_confs); 435 if (ret < 0) 436 return ret; 437 438 sdw_be_num = ret; 439 440 /* enable dmic */ 441 if (soc_sdw_quirk & ASOC_SDW_ACP_DMIC) 442 dmic_num = 1; 443 444 dev_dbg(dev, "sdw %d, dmic %d", sdw_be_num, dmic_num); 445 446 codec_conf = devm_kcalloc(dev, num_confs, sizeof(*codec_conf), GFP_KERNEL); 447 if (!codec_conf) 448 return -ENOMEM; 449 450 /* allocate BE dailinks */ 451 num_links = sdw_be_num + dmic_num; 452 dai_links = devm_kcalloc(dev, num_links, sizeof(*dai_links), GFP_KERNEL); 453 if (!dai_links) 454 return -ENOMEM; 455 456 card->codec_conf = codec_conf; 457 card->num_configs = num_confs; 458 card->dai_link = dai_links; 459 card->num_links = num_links; 460 card->aux_dev = soc_aux; 461 card->num_aux_devs = num_aux; 462 463 /* SDW */ 464 if (sdw_be_num) { 465 ret = create_sdw_dailinks(card, &dai_links, &be_id, 466 soc_dais, &codec_conf); 467 if (ret) 468 return ret; 469 } 470 471 /* dmic */ 472 if (dmic_num > 0) { 473 if (ctx->ignore_internal_dmic) { 474 dev_warn(dev, "Ignoring ACP DMIC\n"); 475 } else { 476 ret = create_dmic_dailinks(card, &dai_links, &be_id, 0); 477 if (ret) 478 return ret; 479 } 480 } 481 482 WARN_ON(codec_conf != card->codec_conf + card->num_configs); 483 WARN_ON(dai_links != card->dai_link + card->num_links); 484 485 return ret; 486 } 487 488 static int mc_probe(struct platform_device *pdev) 489 { 490 struct snd_soc_acpi_mach *mach = dev_get_platdata(&pdev->dev); 491 struct snd_soc_card *card; 492 struct amd_mc_ctx *amd_ctx; 493 struct asoc_sdw_mc_private *ctx; 494 int amp_num = 0, i; 495 int ret; 496 497 amd_ctx = devm_kzalloc(&pdev->dev, sizeof(*amd_ctx), GFP_KERNEL); 498 if (!amd_ctx) 499 return -ENOMEM; 500 501 amd_ctx->acp_rev = mach->mach_params.subsystem_rev; 502 amd_ctx->max_sdw_links = ACP63_SDW_MAX_LINKS; 503 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); 504 if (!ctx) 505 return -ENOMEM; 506 ctx->codec_info_list_count = asoc_sdw_get_codec_info_list_count(); 507 ctx->private = amd_ctx; 508 card = &ctx->card; 509 card->dev = &pdev->dev; 510 card->name = "amd-soundwire"; 511 card->owner = THIS_MODULE; 512 card->late_probe = asoc_sdw_card_late_probe; 513 514 snd_soc_card_set_drvdata(card, ctx); 515 if (mach->mach_params.subsystem_id_set) 516 snd_soc_card_set_pci_ssid(card, 517 mach->mach_params.subsystem_vendor, 518 mach->mach_params.subsystem_device); 519 520 dmi_check_system(soc_sdw_quirk_table); 521 522 if (quirk_override != -1) { 523 dev_info(card->dev, "Overriding quirk 0x%lx => 0x%x\n", 524 soc_sdw_quirk, quirk_override); 525 soc_sdw_quirk = quirk_override; 526 } 527 528 log_quirks(card->dev); 529 530 ctx->mc_quirk = soc_sdw_quirk; 531 dev_dbg(card->dev, "legacy quirk 0x%lx\n", ctx->mc_quirk); 532 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */ 533 for (i = 0; i < ctx->codec_info_list_count; i++) 534 codec_info_list[i].amp_num = 0; 535 536 ret = soc_card_dai_links_create(card); 537 if (ret < 0) 538 return ret; 539 540 /* 541 * the default amp_num is zero for each codec and 542 * amp_num will only be increased for active amp 543 * codecs on used platform 544 */ 545 for (i = 0; i < ctx->codec_info_list_count; i++) 546 amp_num += codec_info_list[i].amp_num; 547 548 card->components = devm_kasprintf(card->dev, GFP_KERNEL, 549 " cfg-amp:%d", amp_num); 550 if (!card->components) 551 return -ENOMEM; 552 if (soc_sdw_quirk & ASOC_SDW_ACP_DMIC) { 553 card->components = devm_kasprintf(card->dev, GFP_KERNEL, 554 "%s mic:acp-dmic cfg-mics:%d", 555 card->components, 556 1); 557 if (!card->components) 558 return -ENOMEM; 559 } 560 561 /* Register the card */ 562 ret = devm_snd_soc_register_card(card->dev, card); 563 if (ret) { 564 dev_err_probe(card->dev, ret, "snd_soc_register_card failed %d\n", ret); 565 asoc_sdw_mc_dailink_exit_loop(card); 566 return ret; 567 } 568 569 platform_set_drvdata(pdev, card); 570 571 return ret; 572 } 573 574 static void mc_remove(struct platform_device *pdev) 575 { 576 struct snd_soc_card *card = platform_get_drvdata(pdev); 577 578 asoc_sdw_mc_dailink_exit_loop(card); 579 } 580 581 static const struct platform_device_id mc_id_table[] = { 582 { "amd_sdw", }, 583 {} 584 }; 585 MODULE_DEVICE_TABLE(platform, mc_id_table); 586 587 static struct platform_driver soc_sdw_driver = { 588 .driver = { 589 .name = "amd_sdw", 590 .pm = &snd_soc_pm_ops, 591 }, 592 .probe = mc_probe, 593 .remove = mc_remove, 594 .id_table = mc_id_table, 595 }; 596 597 module_platform_driver(soc_sdw_driver); 598 599 MODULE_DESCRIPTION("ASoC AMD SoundWire Legacy Generic Machine driver"); 600 MODULE_AUTHOR("Vijendar Mukunda <Vijendar.Mukunda@amd.com>"); 601 MODULE_LICENSE("GPL"); 602 MODULE_IMPORT_NS("SND_SOC_SDW_UTILS"); 603 MODULE_IMPORT_NS("SND_SOC_AMD_SDW_MACH"); 604