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
log_quirks(struct device * dev)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
soc_sdw_quirk_cb(const struct dmi_system_id * id)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
create_sdw_dailink(struct snd_soc_card * card,struct asoc_sdw_dailink * soc_dai,struct snd_soc_dai_link ** dai_links,int * be_id,struct snd_soc_codec_conf ** codec_conf,struct snd_soc_dai_link_component * sdw_platform_component)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
create_sdw_dailinks(struct snd_soc_card * card,struct snd_soc_dai_link ** dai_links,int * be_id,struct asoc_sdw_dailink * soc_dais,struct snd_soc_codec_conf ** codec_conf)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
create_dmic_dailinks(struct snd_soc_card * card,struct snd_soc_dai_link ** dai_links,int * be_id,int no_pcm)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
soc_card_dai_links_create(struct snd_soc_card * card)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_codec_conf *codec_conf;
364 struct snd_soc_dai_link *dai_links;
365 int num_devs = 0;
366 int num_ends = 0;
367 int num_links;
368 int be_id = 0;
369 int ret;
370
371 ret = asoc_sdw_count_sdw_endpoints(card, &num_devs, &num_ends);
372 if (ret < 0) {
373 dev_err(dev, "failed to count devices/endpoints: %d\n", ret);
374 return ret;
375 }
376
377 /* One per DAI link, worst case is a DAI link for every endpoint */
378 soc_dais = kcalloc(num_ends, sizeof(*soc_dais), GFP_KERNEL);
379 if (!soc_dais)
380 return -ENOMEM;
381
382 /* One per endpoint, ie. each DAI on each codec/amp */
383 soc_ends = kcalloc(num_ends, sizeof(*soc_ends), GFP_KERNEL);
384 if (!soc_ends)
385 return -ENOMEM;
386
387 ret = asoc_sdw_parse_sdw_endpoints(card, soc_dais, soc_ends, &num_devs);
388 if (ret < 0)
389 return ret;
390
391 sdw_be_num = ret;
392
393 /* enable dmic */
394 if (soc_sdw_quirk & ASOC_SDW_ACP_DMIC || mach_params->dmic_num)
395 dmic_num = 1;
396
397 dev_dbg(dev, "sdw %d, dmic %d", sdw_be_num, dmic_num);
398
399 codec_conf = devm_kcalloc(dev, num_devs, sizeof(*codec_conf), GFP_KERNEL);
400 if (!codec_conf)
401 return -ENOMEM;
402
403 /* allocate BE dailinks */
404 num_links = sdw_be_num + dmic_num;
405 dai_links = devm_kcalloc(dev, num_links, sizeof(*dai_links), GFP_KERNEL);
406 if (!dai_links)
407 return -ENOMEM;
408
409 card->codec_conf = codec_conf;
410 card->num_configs = num_devs;
411 card->dai_link = dai_links;
412 card->num_links = num_links;
413
414 /* SDW */
415 if (sdw_be_num) {
416 ret = create_sdw_dailinks(card, &dai_links, &be_id,
417 soc_dais, &codec_conf);
418 if (ret)
419 return ret;
420 }
421
422 /* dmic */
423 if (dmic_num > 0) {
424 if (ctx->ignore_internal_dmic) {
425 dev_warn(dev, "Ignoring ACP DMIC\n");
426 } else {
427 ret = create_dmic_dailinks(card, &dai_links, &be_id, 0);
428 if (ret)
429 return ret;
430 }
431 }
432
433 WARN_ON(codec_conf != card->codec_conf + card->num_configs);
434 WARN_ON(dai_links != card->dai_link + card->num_links);
435
436 return ret;
437 }
438
mc_probe(struct platform_device * pdev)439 static int mc_probe(struct platform_device *pdev)
440 {
441 struct snd_soc_acpi_mach *mach = dev_get_platdata(&pdev->dev);
442 struct snd_soc_card *card;
443 struct amd_mc_ctx *amd_ctx;
444 struct asoc_sdw_mc_private *ctx;
445 int amp_num = 0, i;
446 int ret;
447
448 amd_ctx = devm_kzalloc(&pdev->dev, sizeof(*amd_ctx), GFP_KERNEL);
449 if (!amd_ctx)
450 return -ENOMEM;
451
452 amd_ctx->acp_rev = mach->mach_params.subsystem_rev;
453 amd_ctx->max_sdw_links = ACP63_SDW_MAX_LINKS;
454 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
455 if (!ctx)
456 return -ENOMEM;
457 ctx->codec_info_list_count = asoc_sdw_get_codec_info_list_count();
458 ctx->private = amd_ctx;
459 card = &ctx->card;
460 card->dev = &pdev->dev;
461 card->name = "amd-soundwire";
462 card->owner = THIS_MODULE;
463 card->late_probe = asoc_sdw_card_late_probe;
464
465 snd_soc_card_set_drvdata(card, ctx);
466
467 dmi_check_system(soc_sdw_quirk_table);
468
469 if (quirk_override != -1) {
470 dev_info(card->dev, "Overriding quirk 0x%lx => 0x%x\n",
471 soc_sdw_quirk, quirk_override);
472 soc_sdw_quirk = quirk_override;
473 }
474
475 log_quirks(card->dev);
476
477 ctx->mc_quirk = soc_sdw_quirk;
478 dev_dbg(card->dev, "legacy quirk 0x%lx\n", ctx->mc_quirk);
479 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */
480 for (i = 0; i < ctx->codec_info_list_count; i++)
481 codec_info_list[i].amp_num = 0;
482
483 ret = soc_card_dai_links_create(card);
484 if (ret < 0)
485 return ret;
486
487 /*
488 * the default amp_num is zero for each codec and
489 * amp_num will only be increased for active amp
490 * codecs on used platform
491 */
492 for (i = 0; i < ctx->codec_info_list_count; i++)
493 amp_num += codec_info_list[i].amp_num;
494
495 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
496 " cfg-amp:%d", amp_num);
497 if (!card->components)
498 return -ENOMEM;
499 if (mach->mach_params.dmic_num) {
500 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
501 "%s mic:dmic cfg-mics:%d",
502 card->components,
503 mach->mach_params.dmic_num);
504 if (!card->components)
505 return -ENOMEM;
506 }
507
508 /* Register the card */
509 ret = devm_snd_soc_register_card(card->dev, card);
510 if (ret) {
511 dev_err_probe(card->dev, ret, "snd_soc_register_card failed %d\n", ret);
512 asoc_sdw_mc_dailink_exit_loop(card);
513 return ret;
514 }
515
516 platform_set_drvdata(pdev, card);
517
518 return ret;
519 }
520
mc_remove(struct platform_device * pdev)521 static void mc_remove(struct platform_device *pdev)
522 {
523 struct snd_soc_card *card = platform_get_drvdata(pdev);
524
525 asoc_sdw_mc_dailink_exit_loop(card);
526 }
527
528 static const struct platform_device_id mc_id_table[] = {
529 { "amd_sdw", },
530 {}
531 };
532 MODULE_DEVICE_TABLE(platform, mc_id_table);
533
534 static struct platform_driver soc_sdw_driver = {
535 .driver = {
536 .name = "amd_sdw",
537 .pm = &snd_soc_pm_ops,
538 },
539 .probe = mc_probe,
540 .remove = mc_remove,
541 .id_table = mc_id_table,
542 };
543
544 module_platform_driver(soc_sdw_driver);
545
546 MODULE_DESCRIPTION("ASoC AMD SoundWire Legacy Generic Machine driver");
547 MODULE_AUTHOR("Vijendar Mukunda <Vijendar.Mukunda@amd.com>");
548 MODULE_LICENSE("GPL");
549 MODULE_IMPORT_NS("SND_SOC_SDW_UTILS");
550 MODULE_IMPORT_NS("SND_SOC_AMD_SDW_MACH");
551