Debian Codegraph @

sound/soc/amd/acp/acp-sdw-legacy-mach.c

raw
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,
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 &current_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 struct snd_soc_acpi_mach *mach = dev_get_platdata(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_acpi_mach_params *mach_params = &mach->mach_params;
401 struct snd_soc_aux_dev *soc_aux;
402 struct snd_soc_codec_conf *codec_conf;
403 struct snd_soc_dai_link *dai_links;
404 int num_devs = 0;
405 int num_ends = 0;
406 int num_aux = 0;
407 int num_confs;
408 int num_links;
409 int be_id = 0;
410 int ret;
411
412 ret = asoc_sdw_count_sdw_endpoints(card, &num_devs, &num_ends, &num_aux);
413 if (ret < 0) {
414 dev_err(dev, "failed to count devices/endpoints: %d\n", ret);
415 return ret;
416 }
417
418 num_confs = num_ends;
419
420 /* One per DAI link, worst case is a DAI link for every endpoint */
421 struct asoc_sdw_dailink *soc_dais __free(kfree) =
422 kzalloc_objs(*soc_dais, num_ends);
423 if (!soc_dais)
424 return -ENOMEM;
425
426 /* One per endpoint, ie. each DAI on each codec/amp */
427 struct asoc_sdw_endpoint *soc_ends __free(kfree) =
428 kzalloc_objs(*soc_ends, num_ends);
429 if (!soc_ends)
430 return -ENOMEM;
431
432 soc_aux = devm_kcalloc(dev, num_aux, sizeof(*soc_aux), GFP_KERNEL);
433 if (!soc_aux)
434 return -ENOMEM;
435
436 ret = asoc_sdw_parse_sdw_endpoints(card, soc_aux, soc_dais, soc_ends, &num_confs);
437 if (ret < 0)
438 return ret;
439
440 sdw_be_num = ret;
441
442 /* enable dmic */
443 if (soc_sdw_quirk & ASOC_SDW_ACP_DMIC || mach_params->dmic_num)
444 dmic_num = 1;
445
446 dev_dbg(dev, "sdw %d, dmic %d", sdw_be_num, dmic_num);
447
448 codec_conf = devm_kcalloc(dev, num_confs, sizeof(*codec_conf), GFP_KERNEL);
449 if (!codec_conf)
450 return -ENOMEM;
451
452 /* allocate BE dailinks */
453 num_links = sdw_be_num + dmic_num;
454 dai_links = devm_kcalloc(dev, num_links, sizeof(*dai_links), GFP_KERNEL);
455 if (!dai_links)
456 return -ENOMEM;
457
458 card->codec_conf = codec_conf;
459 card->num_configs = num_confs;
460 card->dai_link = dai_links;
461 card->num_links = num_links;
462 card->aux_dev = soc_aux;
463 card->num_aux_devs = num_aux;
464
465 /* SDW */
466 if (sdw_be_num) {
467 ret = create_sdw_dailinks(card, &dai_links, &be_id,
468 soc_dais, &codec_conf);
469 if (ret)
470 return ret;
471 }
472
473 /* dmic */
474 if (dmic_num > 0) {
475 if (ctx->ignore_internal_dmic) {
476 dev_warn(dev, "Ignoring ACP DMIC\n");
477 } else {
478 ret = create_dmic_dailinks(card, &dai_links, &be_id, 0);
479 if (ret)
480 return ret;
481 }
482 }
483
484 WARN_ON(codec_conf != card->codec_conf + card->num_configs);
485 WARN_ON(dai_links != card->dai_link + card->num_links);
486
487 return ret;
488 }
489
490 static int mc_probe(struct platform_device *pdev)
491 {
492 struct snd_soc_acpi_mach *mach = dev_get_platdata(&pdev->dev);
493 struct snd_soc_card *card;
494 struct amd_mc_ctx *amd_ctx;
495 struct asoc_sdw_mc_private *ctx;
496 int amp_num = 0, i;
497 int ret;
498
499 amd_ctx = devm_kzalloc(&pdev->dev, sizeof(*amd_ctx), GFP_KERNEL);
500 if (!amd_ctx)
501 return -ENOMEM;
502
503 amd_ctx->acp_rev = mach->mach_params.subsystem_rev;
504 amd_ctx->max_sdw_links = ACP63_SDW_MAX_LINKS;
505 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
506 if (!ctx)
507 return -ENOMEM;
508 ctx->codec_info_list_count = asoc_sdw_get_codec_info_list_count();
509 ctx->private = amd_ctx;
510 card = &ctx->card;
511 card->dev = &pdev->dev;
512 card->name = "amd-soundwire";
513 card->owner = THIS_MODULE;
514 card->late_probe = asoc_sdw_card_late_probe;
515
516 snd_soc_card_set_drvdata(card, ctx);
517 if (mach->mach_params.subsystem_id_set)
518 snd_soc_card_set_pci_ssid(card,
519 mach->mach_params.subsystem_vendor,
520 mach->mach_params.subsystem_device);
521
522 dmi_check_system(soc_sdw_quirk_table);
523
524 if (quirk_override != -1) {
525 dev_info(card->dev, "Overriding quirk 0x%lx => 0x%x\n",
526 soc_sdw_quirk, quirk_override);
527 soc_sdw_quirk = quirk_override;
528 }
529
530 log_quirks(card->dev);
531
532 ctx->mc_quirk = soc_sdw_quirk;
533 dev_dbg(card->dev, "legacy quirk 0x%lx\n", ctx->mc_quirk);
534 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */
535 for (i = 0; i < ctx->codec_info_list_count; i++)
536 codec_info_list[i].amp_num = 0;
537
538 ret = soc_card_dai_links_create(card);
539 if (ret < 0)
540 return ret;
541
542 /*
543 * the default amp_num is zero for each codec and
544 * amp_num will only be increased for active amp
545 * codecs on used platform
546 */
547 for (i = 0; i < ctx->codec_info_list_count; i++)
548 amp_num += codec_info_list[i].amp_num;
549
550 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
551 " cfg-amp:%d", amp_num);
552 if (!card->components)
553 return -ENOMEM;
554 if (soc_sdw_quirk & ASOC_SDW_ACP_DMIC) {
555 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
556 "%s mic:dmic cfg-mics:%d",
557 card->components,
558 1);
559 if (!card->components)
560 return -ENOMEM;
561 }
562
563 /* Register the card */
564 ret = devm_snd_soc_register_card(card->dev, card);
565 if (ret) {
566 dev_err_probe(card->dev, ret, "snd_soc_register_card failed %d\n", ret);
567 asoc_sdw_mc_dailink_exit_loop(card);
568 return ret;
569 }
570
571 platform_set_drvdata(pdev, card);
572
573 return ret;
574 }
575
576 static void mc_remove(struct platform_device *pdev)
577 {
578 struct snd_soc_card *card = platform_get_drvdata(pdev);
579
580 asoc_sdw_mc_dailink_exit_loop(card);
581 }
582
583 static const struct platform_device_id mc_id_table[] = {
584 { "amd_sdw", },
585 {}
586 };
587 MODULE_DEVICE_TABLE(platform, mc_id_table);
588
589 static struct platform_driver soc_sdw_driver = {
590 .driver = {
591 .name = "amd_sdw",
592 .pm = &snd_soc_pm_ops,
593 },
594 .probe = mc_probe,
595 .remove = mc_remove,
596 .id_table = mc_id_table,
597 };
598
599 module_platform_driver(soc_sdw_driver);
600
601 MODULE_DESCRIPTION("ASoC AMD SoundWire Legacy Generic Machine driver");
602 MODULE_AUTHOR("Vijendar Mukunda <[email protected]>");
603 MODULE_LICENSE("GPL");
604 MODULE_IMPORT_NS("SND_SOC_SDW_UTILS");
605 MODULE_IMPORT_NS("SND_SOC_AMD_SDW_MACH");