1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright(c) 2024 Advanced Micro Devices, Inc. 3 4 /* 5 * acp-sdw-sof-mach - ASoC 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 sof_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(sof_sdw_quirk)) 27 dev_dbg(dev, "quirk realtek,jack-detect-source %ld\n", 28 SOC_JACK_JDSRC(sof_sdw_quirk)); 29 if (sof_sdw_quirk & ASOC_SDW_ACP_DMIC) 30 dev_dbg(dev, "quirk SOC_SDW_ACP_DMIC enabled\n"); 31 } 32 33 static int sof_sdw_quirk_cb(const struct dmi_system_id *id) 34 { 35 sof_sdw_quirk = (unsigned long)id->driver_data; 36 return 1; 37 } 38 39 static const struct dmi_system_id sof_sdw_quirk_table[] = { 40 { 41 .callback = sof_sdw_quirk_cb, 42 .matches = { 43 DMI_MATCH(DMI_SYS_VENDOR, "AMD"), 44 DMI_MATCH(DMI_PRODUCT_NAME, "Birman-PHX"), 45 }, 46 .driver_data = (void *)RT711_JD2, 47 }, 48 {} 49 }; 50 51 static struct snd_soc_dai_link_component platform_component[] = { 52 { 53 /* name might be overridden during probe */ 54 .name = "0000:04:00.5", 55 } 56 }; 57 58 static const struct snd_soc_ops sdw_ops = { 59 .startup = asoc_sdw_startup, 60 .prepare = asoc_sdw_prepare, 61 .trigger = asoc_sdw_trigger, 62 .hw_params = asoc_sdw_hw_params, 63 .hw_free = asoc_sdw_hw_free, 64 .shutdown = asoc_sdw_shutdown, 65 }; 66 67 static const char * const type_strings[] = {"SimpleJack", "SmartAmp", "SmartMic"}; 68 69 static int create_sdw_dailink(struct snd_soc_card *card, 70 struct asoc_sdw_dailink *sof_dai, 71 struct snd_soc_dai_link **dai_links, 72 int *be_id, struct snd_soc_codec_conf **codec_conf) 73 { 74 struct device *dev = card->dev; 75 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); 76 struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private; 77 struct asoc_sdw_endpoint *sof_end; 78 int cpu_pin_id; 79 int stream; 80 int ret; 81 82 list_for_each_entry(sof_end, &sof_dai->endpoints, list) { 83 if (sof_end->name_prefix) { 84 (*codec_conf)->dlc.name = sof_end->codec_name; 85 (*codec_conf)->name_prefix = sof_end->name_prefix; 86 (*codec_conf)++; 87 } 88 89 if (sof_end->include_sidecar) { 90 ret = sof_end->codec_info->add_sidecar(card, dai_links, codec_conf); 91 if (ret) 92 return ret; 93 } 94 } 95 96 for_each_pcm_streams(stream) { 97 static const char * const sdw_stream_name[] = { 98 "SDW%d-PIN%d-PLAYBACK", 99 "SDW%d-PIN%d-CAPTURE", 100 "SDW%d-PIN%d-PLAYBACK-%s", 101 "SDW%d-PIN%d-CAPTURE-%s", 102 }; 103 struct snd_soc_dai_link_ch_map *codec_maps; 104 struct snd_soc_dai_link_component *codecs; 105 struct snd_soc_dai_link_component *cpus; 106 int num_cpus = hweight32(sof_dai->link_mask[stream]); 107 int num_codecs = sof_dai->num_devs[stream]; 108 int playback, capture; 109 int j = 0; 110 char *name; 111 112 if (!sof_dai->num_devs[stream]) 113 continue; 114 115 sof_end = list_first_entry(&sof_dai->endpoints, 116 struct asoc_sdw_endpoint, list); 117 118 *be_id = sof_end->dai_info->dailink[stream]; 119 if (*be_id < 0) { 120 dev_err(dev, "Invalid dailink id %d\n", *be_id); 121 return -EINVAL; 122 } 123 124 switch (amd_ctx->acp_rev) { 125 case ACP63_PCI_REV: 126 ret = get_acp63_cpu_pin_id(ffs(sof_end->link_mask - 1), 127 *be_id, &cpu_pin_id, dev); 128 if (ret) 129 return ret; 130 break; 131 case ACP70_PCI_REV: 132 case ACP71_PCI_REV: 133 ret = get_acp70_cpu_pin_id(ffs(sof_end->link_mask - 1), 134 *be_id, &cpu_pin_id, dev); 135 if (ret) 136 return ret; 137 break; 138 default: 139 return -EINVAL; 140 } 141 /* create stream name according to first link id */ 142 if (ctx->append_dai_type) { 143 name = devm_kasprintf(dev, GFP_KERNEL, 144 sdw_stream_name[stream + 2], 145 ffs(sof_end->link_mask) - 1, 146 cpu_pin_id, 147 type_strings[sof_end->dai_info->dai_type]); 148 } else { 149 name = devm_kasprintf(dev, GFP_KERNEL, 150 sdw_stream_name[stream], 151 ffs(sof_end->link_mask) - 1, 152 cpu_pin_id); 153 } 154 if (!name) 155 return -ENOMEM; 156 157 cpus = devm_kcalloc(dev, num_cpus, sizeof(*cpus), GFP_KERNEL); 158 if (!cpus) 159 return -ENOMEM; 160 161 codecs = devm_kcalloc(dev, num_codecs, sizeof(*codecs), GFP_KERNEL); 162 if (!codecs) 163 return -ENOMEM; 164 165 codec_maps = devm_kcalloc(dev, num_codecs, sizeof(*codec_maps), GFP_KERNEL); 166 if (!codec_maps) 167 return -ENOMEM; 168 169 list_for_each_entry(sof_end, &sof_dai->endpoints, list) { 170 if (!sof_end->dai_info->direction[stream]) 171 continue; 172 173 int link_num = ffs(sof_end->link_mask) - 1; 174 175 cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 176 "SDW%d Pin%d", 177 link_num, cpu_pin_id); 178 dev_dbg(dev, "cpu->dai_name:%s\n", cpus->dai_name); 179 if (!cpus->dai_name) 180 return -ENOMEM; 181 182 codec_maps[j].cpu = 0; 183 codec_maps[j].codec = j; 184 185 codecs[j].name = sof_end->codec_name; 186 codecs[j].dai_name = sof_end->dai_info->dai_name; 187 j++; 188 } 189 190 WARN_ON(j != num_codecs); 191 192 playback = (stream == SNDRV_PCM_STREAM_PLAYBACK); 193 capture = (stream == SNDRV_PCM_STREAM_CAPTURE); 194 195 asoc_sdw_init_dai_link(dev, *dai_links, be_id, name, playback, capture, 196 cpus, num_cpus, platform_component, 197 ARRAY_SIZE(platform_component), codecs, num_codecs, 198 1, asoc_sdw_rtd_init, &sdw_ops); 199 200 /* 201 * SoundWire DAILINKs use 'stream' functions and Bank Switch operations 202 * based on wait_for_completion(), tag them as 'nonatomic'. 203 */ 204 (*dai_links)->nonatomic = true; 205 (*dai_links)->ch_maps = codec_maps; 206 207 list_for_each_entry(sof_end, &sof_dai->endpoints, list) { 208 if (sof_end->dai_info->init) 209 sof_end->dai_info->init(card, *dai_links, 210 sof_end->codec_info, 211 playback); 212 } 213 214 (*dai_links)++; 215 } 216 217 return 0; 218 } 219 220 static int create_sdw_dailinks(struct snd_soc_card *card, 221 struct snd_soc_dai_link **dai_links, int *be_id, 222 struct asoc_sdw_dailink *sof_dais, 223 struct snd_soc_codec_conf **codec_conf) 224 { 225 int ret; 226 227 /* generate DAI links by each sdw link */ 228 while (sof_dais->initialised) { 229 int current_be_id = 0; 230 231 ret = create_sdw_dailink(card, sof_dais, dai_links, 232 ¤t_be_id, codec_conf); 233 if (ret) 234 return ret; 235 236 /* Update the be_id to match the highest ID used for SDW link */ 237 if (*be_id < current_be_id) 238 *be_id = current_be_id; 239 240 sof_dais++; 241 } 242 243 return 0; 244 } 245 246 static int create_dmic_dailinks(struct snd_soc_card *card, 247 struct snd_soc_dai_link **dai_links, int *be_id, int no_pcm) 248 { 249 struct device *dev = card->dev; 250 int ret; 251 252 ret = asoc_sdw_init_simple_dai_link(dev, *dai_links, be_id, "acp-dmic-codec", 253 0, 1, // DMIC only supports capture 254 "acp-sof-dmic", platform_component->name, 255 "dmic-codec", "dmic-hifi", no_pcm, 256 asoc_sdw_dmic_init, NULL); 257 if (ret) 258 return ret; 259 260 (*dai_links)++; 261 262 return 0; 263 } 264 265 static int sof_card_dai_links_create(struct snd_soc_card *card) 266 { 267 struct device *dev = card->dev; 268 struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev); 269 int sdw_be_num = 0, dmic_num = 0; 270 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); 271 struct snd_soc_acpi_mach_params *mach_params = &mach->mach_params; 272 struct asoc_sdw_endpoint *sof_ends __free(kfree) = NULL; 273 struct asoc_sdw_dailink *sof_dais __free(kfree) = NULL; 274 struct snd_soc_codec_conf *codec_conf; 275 struct snd_soc_dai_link *dai_links; 276 int num_devs = 0; 277 int num_ends = 0; 278 int num_links; 279 int be_id = 0; 280 int ret; 281 282 ret = asoc_sdw_count_sdw_endpoints(card, &num_devs, &num_ends); 283 if (ret < 0) { 284 dev_err(dev, "failed to count devices/endpoints: %d\n", ret); 285 return ret; 286 } 287 288 /* One per DAI link, worst case is a DAI link for every endpoint */ 289 sof_dais = kcalloc(num_ends, sizeof(*sof_dais), GFP_KERNEL); 290 if (!sof_dais) 291 return -ENOMEM; 292 293 /* One per endpoint, ie. each DAI on each codec/amp */ 294 sof_ends = kcalloc(num_ends, sizeof(*sof_ends), GFP_KERNEL); 295 if (!sof_ends) 296 return -ENOMEM; 297 298 ret = asoc_sdw_parse_sdw_endpoints(card, sof_dais, sof_ends, &num_devs); 299 if (ret < 0) 300 return ret; 301 302 sdw_be_num = ret; 303 304 /* enable dmic */ 305 if (sof_sdw_quirk & ASOC_SDW_ACP_DMIC || mach_params->dmic_num) 306 dmic_num = 1; 307 308 dev_dbg(dev, "sdw %d, dmic %d", sdw_be_num, dmic_num); 309 310 codec_conf = devm_kcalloc(dev, num_devs, sizeof(*codec_conf), GFP_KERNEL); 311 if (!codec_conf) 312 return -ENOMEM; 313 314 /* allocate BE dailinks */ 315 num_links = sdw_be_num + dmic_num; 316 dai_links = devm_kcalloc(dev, num_links, sizeof(*dai_links), GFP_KERNEL); 317 if (!dai_links) 318 return -ENOMEM; 319 320 card->codec_conf = codec_conf; 321 card->num_configs = num_devs; 322 card->dai_link = dai_links; 323 card->num_links = num_links; 324 325 /* SDW */ 326 if (sdw_be_num) { 327 ret = create_sdw_dailinks(card, &dai_links, &be_id, 328 sof_dais, &codec_conf); 329 if (ret) 330 return ret; 331 } 332 333 /* dmic */ 334 if (dmic_num > 0) { 335 if (ctx->ignore_internal_dmic) { 336 dev_warn(dev, "Ignoring ACP DMIC\n"); 337 } else { 338 ret = create_dmic_dailinks(card, &dai_links, &be_id, 1); 339 if (ret) 340 return ret; 341 } 342 } 343 344 WARN_ON(codec_conf != card->codec_conf + card->num_configs); 345 WARN_ON(dai_links != card->dai_link + card->num_links); 346 347 return ret; 348 } 349 350 static int mc_probe(struct platform_device *pdev) 351 { 352 struct snd_soc_acpi_mach *mach = dev_get_platdata(&pdev->dev); 353 struct snd_soc_card *card; 354 struct amd_mc_ctx *amd_ctx; 355 struct asoc_sdw_mc_private *ctx; 356 int amp_num = 0, i; 357 int ret; 358 359 amd_ctx = devm_kzalloc(&pdev->dev, sizeof(*amd_ctx), GFP_KERNEL); 360 if (!amd_ctx) 361 return -ENOMEM; 362 363 amd_ctx->acp_rev = mach->mach_params.subsystem_rev; 364 amd_ctx->max_sdw_links = ACP63_SDW_MAX_LINKS; 365 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); 366 if (!ctx) 367 return -ENOMEM; 368 ctx->codec_info_list_count = asoc_sdw_get_codec_info_list_count(); 369 ctx->private = amd_ctx; 370 card = &ctx->card; 371 card->dev = &pdev->dev; 372 card->name = "amd-soundwire"; 373 card->owner = THIS_MODULE; 374 card->late_probe = asoc_sdw_card_late_probe; 375 376 snd_soc_card_set_drvdata(card, ctx); 377 378 dmi_check_system(sof_sdw_quirk_table); 379 380 if (quirk_override != -1) { 381 dev_info(card->dev, "Overriding quirk 0x%lx => 0x%x\n", 382 sof_sdw_quirk, quirk_override); 383 sof_sdw_quirk = quirk_override; 384 } 385 386 log_quirks(card->dev); 387 388 ctx->mc_quirk = sof_sdw_quirk; 389 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */ 390 for (i = 0; i < ctx->codec_info_list_count; i++) 391 codec_info_list[i].amp_num = 0; 392 393 ret = sof_card_dai_links_create(card); 394 if (ret < 0) 395 return ret; 396 397 /* 398 * the default amp_num is zero for each codec and 399 * amp_num will only be increased for active amp 400 * codecs on used platform 401 */ 402 for (i = 0; i < ctx->codec_info_list_count; i++) 403 amp_num += codec_info_list[i].amp_num; 404 405 card->components = devm_kasprintf(card->dev, GFP_KERNEL, 406 " cfg-amp:%d", amp_num); 407 if (!card->components) 408 return -ENOMEM; 409 410 /* Register the card */ 411 ret = devm_snd_soc_register_card(card->dev, card); 412 if (ret) { 413 dev_err_probe(card->dev, ret, "snd_soc_register_card failed %d\n", ret); 414 asoc_sdw_mc_dailink_exit_loop(card); 415 return ret; 416 } 417 418 platform_set_drvdata(pdev, card); 419 420 return ret; 421 } 422 423 static void mc_remove(struct platform_device *pdev) 424 { 425 struct snd_soc_card *card = platform_get_drvdata(pdev); 426 427 asoc_sdw_mc_dailink_exit_loop(card); 428 } 429 430 static const struct platform_device_id mc_id_table[] = { 431 { "amd_sof_sdw", }, 432 {} 433 }; 434 MODULE_DEVICE_TABLE(platform, mc_id_table); 435 436 static struct platform_driver sof_sdw_driver = { 437 .driver = { 438 .name = "amd_sof_sdw", 439 .pm = &snd_soc_pm_ops, 440 }, 441 .probe = mc_probe, 442 .remove = mc_remove, 443 .id_table = mc_id_table, 444 }; 445 446 module_platform_driver(sof_sdw_driver); 447 448 MODULE_DESCRIPTION("ASoC AMD SoundWire Generic Machine driver"); 449 MODULE_AUTHOR("Vijendar Mukunda <Vijendar.Mukunda@amd.com"); 450 MODULE_LICENSE("GPL"); 451 MODULE_IMPORT_NS("SND_SOC_SDW_UTILS"); 452 MODULE_IMPORT_NS("SND_SOC_AMD_SDW_MACH"); 453