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