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