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 }; 100 101 static const struct snd_soc_ops sdw_ops = { 102 .startup = asoc_sdw_startup, 103 .prepare = asoc_sdw_prepare, 104 .trigger = asoc_sdw_trigger, 105 .hw_params = asoc_sdw_hw_params, 106 .hw_free = asoc_sdw_hw_free, 107 .shutdown = asoc_sdw_shutdown, 108 }; 109 110 static const char * const type_strings[] = {"SimpleJack", "SmartAmp", "SmartMic"}; 111 112 static int create_sdw_dailink(struct snd_soc_card *card, 113 struct asoc_sdw_dailink *soc_dai, 114 struct snd_soc_dai_link **dai_links, 115 int *be_id, struct snd_soc_codec_conf **codec_conf, 116 struct snd_soc_dai_link_component *sdw_platform_component) 117 { 118 struct device *dev = card->dev; 119 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); 120 struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private; 121 struct asoc_sdw_endpoint *soc_end; 122 int cpu_pin_id; 123 int stream; 124 int ret; 125 126 list_for_each_entry(soc_end, &soc_dai->endpoints, list) { 127 if (soc_end->name_prefix) { 128 (*codec_conf)->dlc.name = soc_end->codec_name; 129 (*codec_conf)->name_prefix = soc_end->name_prefix; 130 (*codec_conf)++; 131 } 132 133 if (soc_end->include_sidecar) { 134 ret = soc_end->codec_info->add_sidecar(card, dai_links, codec_conf); 135 if (ret) 136 return ret; 137 } 138 } 139 140 for_each_pcm_streams(stream) { 141 static const char * const sdw_stream_name[] = { 142 "SDW%d-PIN%d-PLAYBACK", 143 "SDW%d-PIN%d-CAPTURE", 144 "SDW%d-PIN%d-PLAYBACK-%s", 145 "SDW%d-PIN%d-CAPTURE-%s", 146 }; 147 struct snd_soc_dai_link_ch_map *codec_maps; 148 struct snd_soc_dai_link_component *codecs; 149 struct snd_soc_dai_link_component *cpus; 150 int num_cpus = hweight32(soc_dai->link_mask[stream]); 151 int num_codecs = soc_dai->num_devs[stream]; 152 int playback, capture; 153 int j = 0; 154 char *name; 155 156 if (!soc_dai->num_devs[stream]) 157 continue; 158 159 soc_end = list_first_entry(&soc_dai->endpoints, 160 struct asoc_sdw_endpoint, list); 161 162 *be_id = soc_end->dai_info->dailink[stream]; 163 if (*be_id < 0) { 164 dev_err(dev, "Invalid dailink id %d\n", *be_id); 165 return -EINVAL; 166 } 167 168 switch (amd_ctx->acp_rev) { 169 case ACP63_PCI_REV: 170 ret = get_acp63_cpu_pin_id(ffs(soc_end->link_mask - 1), 171 *be_id, &cpu_pin_id, dev); 172 if (ret) 173 return ret; 174 break; 175 case ACP70_PCI_REV: 176 case ACP71_PCI_REV: 177 case ACP72_PCI_REV: 178 ret = get_acp70_cpu_pin_id(ffs(soc_end->link_mask - 1), 179 *be_id, &cpu_pin_id, dev); 180 if (ret) 181 return ret; 182 break; 183 default: 184 return -EINVAL; 185 } 186 /* create stream name according to first link id */ 187 if (ctx->append_dai_type) { 188 name = devm_kasprintf(dev, GFP_KERNEL, 189 sdw_stream_name[stream + 2], 190 ffs(soc_end->link_mask) - 1, 191 cpu_pin_id, 192 type_strings[soc_end->dai_info->dai_type]); 193 } else { 194 name = devm_kasprintf(dev, GFP_KERNEL, 195 sdw_stream_name[stream], 196 ffs(soc_end->link_mask) - 1, 197 cpu_pin_id); 198 } 199 if (!name) 200 return -ENOMEM; 201 202 cpus = devm_kcalloc(dev, num_cpus, sizeof(*cpus), GFP_KERNEL); 203 if (!cpus) 204 return -ENOMEM; 205 206 codecs = devm_kcalloc(dev, num_codecs, sizeof(*codecs), GFP_KERNEL); 207 if (!codecs) 208 return -ENOMEM; 209 210 codec_maps = devm_kcalloc(dev, num_codecs, sizeof(*codec_maps), GFP_KERNEL); 211 if (!codec_maps) 212 return -ENOMEM; 213 214 list_for_each_entry(soc_end, &soc_dai->endpoints, list) { 215 if (!soc_end->dai_info->direction[stream]) 216 continue; 217 218 int link_num = ffs(soc_end->link_mask) - 1; 219 220 cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 221 "SDW%d Pin%d", 222 link_num, cpu_pin_id); 223 dev_dbg(dev, "cpu->dai_name:%s\n", cpus->dai_name); 224 if (!cpus->dai_name) 225 return -ENOMEM; 226 227 codec_maps[j].cpu = 0; 228 codec_maps[j].codec = j; 229 230 codecs[j].name = soc_end->codec_name; 231 codecs[j].dai_name = soc_end->dai_info->dai_name; 232 j++; 233 } 234 235 WARN_ON(j != num_codecs); 236 237 playback = (stream == SNDRV_PCM_STREAM_PLAYBACK); 238 capture = (stream == SNDRV_PCM_STREAM_CAPTURE); 239 240 asoc_sdw_init_dai_link(dev, *dai_links, be_id, name, playback, capture, 241 cpus, num_cpus, sdw_platform_component, 242 1, codecs, num_codecs, 243 0, asoc_sdw_rtd_init, &sdw_ops); 244 /* 245 * SoundWire DAILINKs use 'stream' functions and Bank Switch operations 246 * based on wait_for_completion(), tag them as 'nonatomic'. 247 */ 248 (*dai_links)->nonatomic = true; 249 (*dai_links)->ch_maps = codec_maps; 250 251 list_for_each_entry(soc_end, &soc_dai->endpoints, list) { 252 if (soc_end->dai_info->init) 253 soc_end->dai_info->init(card, *dai_links, 254 soc_end->codec_info, 255 playback); 256 } 257 258 (*dai_links)++; 259 } 260 261 return 0; 262 } 263 264 static int create_sdw_dailinks(struct snd_soc_card *card, 265 struct snd_soc_dai_link **dai_links, int *be_id, 266 struct asoc_sdw_dailink *soc_dais, 267 struct snd_soc_codec_conf **codec_conf) 268 { 269 struct device *dev = card->dev; 270 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); 271 struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private; 272 struct snd_soc_dai_link_component *sdw_platform_component; 273 int ret; 274 275 sdw_platform_component = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), 276 GFP_KERNEL); 277 if (!sdw_platform_component) 278 return -ENOMEM; 279 280 switch (amd_ctx->acp_rev) { 281 case ACP63_PCI_REV: 282 case ACP70_PCI_REV: 283 case ACP71_PCI_REV: 284 case ACP72_PCI_REV: 285 sdw_platform_component->name = "amd_ps_sdw_dma.0"; 286 break; 287 default: 288 return -EINVAL; 289 } 290 291 /* generate DAI links by each sdw link */ 292 while (soc_dais->initialised) { 293 int current_be_id = 0; 294 295 ret = create_sdw_dailink(card, soc_dais, dai_links, 296 ¤t_be_id, codec_conf, sdw_platform_component); 297 if (ret) 298 return ret; 299 300 /* Update the be_id to match the highest ID used for SDW link */ 301 if (*be_id < current_be_id) 302 *be_id = current_be_id; 303 304 soc_dais++; 305 } 306 307 return 0; 308 } 309 310 static int create_dmic_dailinks(struct snd_soc_card *card, 311 struct snd_soc_dai_link **dai_links, int *be_id, int no_pcm) 312 { 313 struct device *dev = card->dev; 314 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); 315 struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private; 316 struct snd_soc_dai_link_component *pdm_cpu; 317 struct snd_soc_dai_link_component *pdm_platform; 318 int ret; 319 320 pdm_cpu = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), GFP_KERNEL); 321 if (!pdm_cpu) 322 return -ENOMEM; 323 324 pdm_platform = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), GFP_KERNEL); 325 if (!pdm_platform) 326 return -ENOMEM; 327 328 switch (amd_ctx->acp_rev) { 329 case ACP63_PCI_REV: 330 case ACP70_PCI_REV: 331 case ACP71_PCI_REV: 332 case ACP72_PCI_REV: 333 pdm_cpu->name = "acp_ps_pdm_dma.0"; 334 pdm_platform->name = "acp_ps_pdm_dma.0"; 335 break; 336 default: 337 return -EINVAL; 338 } 339 340 *be_id = ACP_DMIC_BE_ID; 341 ret = asoc_sdw_init_simple_dai_link(dev, *dai_links, be_id, "acp-dmic-codec", 342 0, 1, // DMIC only supports capture 343 pdm_cpu->name, pdm_platform->name, 344 "dmic-codec.0", "dmic-hifi", no_pcm, 345 asoc_sdw_dmic_init, NULL); 346 if (ret) 347 return ret; 348 349 (*dai_links)++; 350 351 return 0; 352 } 353 354 static int soc_card_dai_links_create(struct snd_soc_card *card) 355 { 356 struct device *dev = card->dev; 357 struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev); 358 int sdw_be_num = 0, dmic_num = 0; 359 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); 360 struct snd_soc_acpi_mach_params *mach_params = &mach->mach_params; 361 struct asoc_sdw_endpoint *soc_ends __free(kfree) = NULL; 362 struct asoc_sdw_dailink *soc_dais __free(kfree) = NULL; 363 struct snd_soc_aux_dev *soc_aux; 364 struct snd_soc_codec_conf *codec_conf; 365 struct snd_soc_dai_link *dai_links; 366 int num_devs = 0; 367 int num_ends = 0; 368 int num_aux = 0; 369 int num_confs; 370 int num_links; 371 int be_id = 0; 372 int ret; 373 374 ret = asoc_sdw_count_sdw_endpoints(card, &num_devs, &num_ends, &num_aux); 375 if (ret < 0) { 376 dev_err(dev, "failed to count devices/endpoints: %d\n", ret); 377 return ret; 378 } 379 380 num_confs = num_ends; 381 382 /* One per DAI link, worst case is a DAI link for every endpoint */ 383 soc_dais = kcalloc(num_ends, sizeof(*soc_dais), GFP_KERNEL); 384 if (!soc_dais) 385 return -ENOMEM; 386 387 /* One per endpoint, ie. each DAI on each codec/amp */ 388 soc_ends = kcalloc(num_ends, sizeof(*soc_ends), GFP_KERNEL); 389 if (!soc_ends) 390 return -ENOMEM; 391 392 soc_aux = devm_kcalloc(dev, num_aux, sizeof(*soc_aux), GFP_KERNEL); 393 if (!soc_aux) 394 return -ENOMEM; 395 396 ret = asoc_sdw_parse_sdw_endpoints(card, soc_aux, soc_dais, soc_ends, &num_confs); 397 if (ret < 0) 398 return ret; 399 400 sdw_be_num = ret; 401 402 /* enable dmic */ 403 if (soc_sdw_quirk & ASOC_SDW_ACP_DMIC || mach_params->dmic_num) 404 dmic_num = 1; 405 406 dev_dbg(dev, "sdw %d, dmic %d", sdw_be_num, dmic_num); 407 408 codec_conf = devm_kcalloc(dev, num_confs, sizeof(*codec_conf), GFP_KERNEL); 409 if (!codec_conf) 410 return -ENOMEM; 411 412 /* allocate BE dailinks */ 413 num_links = sdw_be_num + dmic_num; 414 dai_links = devm_kcalloc(dev, num_links, sizeof(*dai_links), GFP_KERNEL); 415 if (!dai_links) 416 return -ENOMEM; 417 418 card->codec_conf = codec_conf; 419 card->num_configs = num_confs; 420 card->dai_link = dai_links; 421 card->num_links = num_links; 422 card->aux_dev = soc_aux; 423 card->num_aux_devs = num_aux; 424 425 /* SDW */ 426 if (sdw_be_num) { 427 ret = create_sdw_dailinks(card, &dai_links, &be_id, 428 soc_dais, &codec_conf); 429 if (ret) 430 return ret; 431 } 432 433 /* dmic */ 434 if (dmic_num > 0) { 435 if (ctx->ignore_internal_dmic) { 436 dev_warn(dev, "Ignoring ACP DMIC\n"); 437 } else { 438 ret = create_dmic_dailinks(card, &dai_links, &be_id, 0); 439 if (ret) 440 return ret; 441 } 442 } 443 444 WARN_ON(codec_conf != card->codec_conf + card->num_configs); 445 WARN_ON(dai_links != card->dai_link + card->num_links); 446 447 return ret; 448 } 449 450 static int mc_probe(struct platform_device *pdev) 451 { 452 struct snd_soc_acpi_mach *mach = dev_get_platdata(&pdev->dev); 453 struct snd_soc_card *card; 454 struct amd_mc_ctx *amd_ctx; 455 struct asoc_sdw_mc_private *ctx; 456 int amp_num = 0, i; 457 int ret; 458 459 amd_ctx = devm_kzalloc(&pdev->dev, sizeof(*amd_ctx), GFP_KERNEL); 460 if (!amd_ctx) 461 return -ENOMEM; 462 463 amd_ctx->acp_rev = mach->mach_params.subsystem_rev; 464 amd_ctx->max_sdw_links = ACP63_SDW_MAX_LINKS; 465 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); 466 if (!ctx) 467 return -ENOMEM; 468 ctx->codec_info_list_count = asoc_sdw_get_codec_info_list_count(); 469 ctx->private = amd_ctx; 470 card = &ctx->card; 471 card->dev = &pdev->dev; 472 card->name = "amd-soundwire"; 473 card->owner = THIS_MODULE; 474 card->late_probe = asoc_sdw_card_late_probe; 475 476 snd_soc_card_set_drvdata(card, ctx); 477 if (mach->mach_params.subsystem_id_set) 478 snd_soc_card_set_pci_ssid(card, 479 mach->mach_params.subsystem_vendor, 480 mach->mach_params.subsystem_device); 481 482 dmi_check_system(soc_sdw_quirk_table); 483 484 if (quirk_override != -1) { 485 dev_info(card->dev, "Overriding quirk 0x%lx => 0x%x\n", 486 soc_sdw_quirk, quirk_override); 487 soc_sdw_quirk = quirk_override; 488 } 489 490 log_quirks(card->dev); 491 492 ctx->mc_quirk = soc_sdw_quirk; 493 dev_dbg(card->dev, "legacy quirk 0x%lx\n", ctx->mc_quirk); 494 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */ 495 for (i = 0; i < ctx->codec_info_list_count; i++) 496 codec_info_list[i].amp_num = 0; 497 498 ret = soc_card_dai_links_create(card); 499 if (ret < 0) 500 return ret; 501 502 /* 503 * the default amp_num is zero for each codec and 504 * amp_num will only be increased for active amp 505 * codecs on used platform 506 */ 507 for (i = 0; i < ctx->codec_info_list_count; i++) 508 amp_num += codec_info_list[i].amp_num; 509 510 card->components = devm_kasprintf(card->dev, GFP_KERNEL, 511 " cfg-amp:%d", amp_num); 512 if (!card->components) 513 return -ENOMEM; 514 if (mach->mach_params.dmic_num) { 515 card->components = devm_kasprintf(card->dev, GFP_KERNEL, 516 "%s mic:dmic cfg-mics:%d", 517 card->components, 518 mach->mach_params.dmic_num); 519 if (!card->components) 520 return -ENOMEM; 521 } 522 523 /* Register the card */ 524 ret = devm_snd_soc_register_card(card->dev, card); 525 if (ret) { 526 dev_err_probe(card->dev, ret, "snd_soc_register_card failed %d\n", ret); 527 asoc_sdw_mc_dailink_exit_loop(card); 528 return ret; 529 } 530 531 platform_set_drvdata(pdev, card); 532 533 return ret; 534 } 535 536 static void mc_remove(struct platform_device *pdev) 537 { 538 struct snd_soc_card *card = platform_get_drvdata(pdev); 539 540 asoc_sdw_mc_dailink_exit_loop(card); 541 } 542 543 static const struct platform_device_id mc_id_table[] = { 544 { "amd_sdw", }, 545 {} 546 }; 547 MODULE_DEVICE_TABLE(platform, mc_id_table); 548 549 static struct platform_driver soc_sdw_driver = { 550 .driver = { 551 .name = "amd_sdw", 552 .pm = &snd_soc_pm_ops, 553 }, 554 .probe = mc_probe, 555 .remove = mc_remove, 556 .id_table = mc_id_table, 557 }; 558 559 module_platform_driver(soc_sdw_driver); 560 561 MODULE_DESCRIPTION("ASoC AMD SoundWire Legacy Generic Machine driver"); 562 MODULE_AUTHOR("Vijendar Mukunda <Vijendar.Mukunda@amd.com>"); 563 MODULE_LICENSE("GPL"); 564 MODULE_IMPORT_NS("SND_SOC_SDW_UTILS"); 565 MODULE_IMPORT_NS("SND_SOC_AMD_SDW_MACH"); 566