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
log_quirks(struct device * dev)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
sof_sdw_quirk_cb(const struct dmi_system_id * id)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
create_sdw_dailink(struct snd_soc_card * card,struct asoc_sdw_dailink * sof_dai,struct snd_soc_dai_link ** dai_links,int * be_id,struct snd_soc_codec_conf ** codec_conf)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 case ACP72_PCI_REV:
134 ret = get_acp70_cpu_pin_id(ffs(sof_end->link_mask - 1),
135 *be_id, &cpu_pin_id, dev);
136 if (ret)
137 return ret;
138 break;
139 default:
140 return -EINVAL;
141 }
142 /* create stream name according to first link id */
143 if (ctx->append_dai_type) {
144 name = devm_kasprintf(dev, GFP_KERNEL,
145 sdw_stream_name[stream + 2],
146 ffs(sof_end->link_mask) - 1,
147 cpu_pin_id,
148 type_strings[sof_end->dai_info->dai_type]);
149 } else {
150 name = devm_kasprintf(dev, GFP_KERNEL,
151 sdw_stream_name[stream],
152 ffs(sof_end->link_mask) - 1,
153 cpu_pin_id);
154 }
155 if (!name)
156 return -ENOMEM;
157
158 cpus = devm_kcalloc(dev, num_cpus, sizeof(*cpus), GFP_KERNEL);
159 if (!cpus)
160 return -ENOMEM;
161
162 codecs = devm_kcalloc(dev, num_codecs, sizeof(*codecs), GFP_KERNEL);
163 if (!codecs)
164 return -ENOMEM;
165
166 codec_maps = devm_kcalloc(dev, num_codecs, sizeof(*codec_maps), GFP_KERNEL);
167 if (!codec_maps)
168 return -ENOMEM;
169
170 list_for_each_entry(sof_end, &sof_dai->endpoints, list) {
171 if (!sof_end->dai_info->direction[stream])
172 continue;
173
174 int link_num = ffs(sof_end->link_mask) - 1;
175
176 cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
177 "SDW%d Pin%d",
178 link_num, cpu_pin_id);
179 if (!cpus->dai_name)
180 return -ENOMEM;
181 dev_dbg(dev, "cpu->dai_name:%s\n", cpus->dai_name);
182
183 codec_maps[j].cpu = 0;
184 codec_maps[j].codec = j;
185
186 codecs[j].name = sof_end->codec_name;
187 codecs[j].dai_name = sof_end->dai_info->dai_name;
188 j++;
189 }
190
191 WARN_ON(j != num_codecs);
192
193 playback = (stream == SNDRV_PCM_STREAM_PLAYBACK);
194 capture = (stream == SNDRV_PCM_STREAM_CAPTURE);
195
196 asoc_sdw_init_dai_link(dev, *dai_links, be_id, name, playback, capture,
197 cpus, num_cpus, platform_component,
198 ARRAY_SIZE(platform_component), codecs, num_codecs,
199 1, asoc_sdw_rtd_init, &sdw_ops);
200
201 /*
202 * SoundWire DAILINKs use 'stream' functions and Bank Switch operations
203 * based on wait_for_completion(), tag them as 'nonatomic'.
204 */
205 (*dai_links)->nonatomic = true;
206 (*dai_links)->ch_maps = codec_maps;
207
208 list_for_each_entry(sof_end, &sof_dai->endpoints, list) {
209 if (sof_end->dai_info->init)
210 sof_end->dai_info->init(card, *dai_links,
211 sof_end->codec_info,
212 playback);
213 }
214
215 (*dai_links)++;
216 }
217
218 return 0;
219 }
220
create_sdw_dailinks(struct snd_soc_card * card,struct snd_soc_dai_link ** dai_links,int * be_id,struct asoc_sdw_dailink * sof_dais,struct snd_soc_codec_conf ** codec_conf)221 static int create_sdw_dailinks(struct snd_soc_card *card,
222 struct snd_soc_dai_link **dai_links, int *be_id,
223 struct asoc_sdw_dailink *sof_dais,
224 struct snd_soc_codec_conf **codec_conf)
225 {
226 int ret;
227
228 /* generate DAI links by each sdw link */
229 while (sof_dais->initialised) {
230 int current_be_id = 0;
231
232 ret = create_sdw_dailink(card, sof_dais, dai_links,
233 ¤t_be_id, codec_conf);
234 if (ret)
235 return ret;
236
237 /* Update the be_id to match the highest ID used for SDW link */
238 if (*be_id < current_be_id)
239 *be_id = current_be_id;
240
241 sof_dais++;
242 }
243
244 return 0;
245 }
246
create_dmic_dailinks(struct snd_soc_card * card,struct snd_soc_dai_link ** dai_links,int * be_id,int no_pcm)247 static int create_dmic_dailinks(struct snd_soc_card *card,
248 struct snd_soc_dai_link **dai_links, int *be_id, int no_pcm)
249 {
250 struct device *dev = card->dev;
251 int ret;
252
253 ret = asoc_sdw_init_simple_dai_link(dev, *dai_links, be_id, "acp-dmic-codec",
254 0, 1, // DMIC only supports capture
255 "acp-sof-dmic", platform_component->name,
256 "dmic-codec", "dmic-hifi", no_pcm,
257 asoc_sdw_dmic_init, NULL);
258 if (ret)
259 return ret;
260
261 (*dai_links)++;
262
263 return 0;
264 }
265
sof_card_dai_links_create(struct snd_soc_card * card)266 static int sof_card_dai_links_create(struct snd_soc_card *card)
267 {
268 struct device *dev = card->dev;
269 struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
270 int sdw_be_num = 0, dmic_num = 0;
271 struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card);
272 struct snd_soc_acpi_mach_params *mach_params = &mach->mach_params;
273 struct asoc_sdw_endpoint *sof_ends __free(kfree) = NULL;
274 struct asoc_sdw_dailink *sof_dais __free(kfree) = NULL;
275 struct snd_soc_aux_dev *sof_aux;
276 struct snd_soc_codec_conf *codec_conf;
277 struct snd_soc_dai_link *dai_links;
278 int num_devs = 0;
279 int num_ends = 0;
280 int num_aux = 0;
281 int num_links;
282 int be_id = 0;
283 int ret;
284
285 ret = asoc_sdw_count_sdw_endpoints(card, &num_devs, &num_ends, &num_aux);
286 if (ret < 0) {
287 dev_err(dev, "failed to count devices/endpoints: %d\n", ret);
288 return ret;
289 }
290
291 /* One per DAI link, worst case is a DAI link for every endpoint */
292 sof_dais = kcalloc(num_ends, sizeof(*sof_dais), GFP_KERNEL);
293 if (!sof_dais)
294 return -ENOMEM;
295
296 /* One per endpoint, ie. each DAI on each codec/amp */
297 sof_ends = kcalloc(num_ends, sizeof(*sof_ends), GFP_KERNEL);
298 if (!sof_ends)
299 return -ENOMEM;
300
301 sof_aux = devm_kcalloc(dev, num_aux, sizeof(*sof_aux), GFP_KERNEL);
302 if (!sof_aux)
303 return -ENOMEM;
304
305 ret = asoc_sdw_parse_sdw_endpoints(card, sof_aux, sof_dais, sof_ends, &num_devs);
306 if (ret < 0)
307 return ret;
308
309 sdw_be_num = ret;
310
311 /* enable dmic */
312 if (sof_sdw_quirk & ASOC_SDW_ACP_DMIC || mach_params->dmic_num)
313 dmic_num = 1;
314
315 dev_dbg(dev, "sdw %d, dmic %d", sdw_be_num, dmic_num);
316
317 codec_conf = devm_kcalloc(dev, num_devs, sizeof(*codec_conf), GFP_KERNEL);
318 if (!codec_conf)
319 return -ENOMEM;
320
321 /* allocate BE dailinks */
322 num_links = sdw_be_num + dmic_num;
323 dai_links = devm_kcalloc(dev, num_links, sizeof(*dai_links), GFP_KERNEL);
324 if (!dai_links)
325 return -ENOMEM;
326
327 card->codec_conf = codec_conf;
328 card->num_configs = num_devs;
329 card->dai_link = dai_links;
330 card->num_links = num_links;
331 card->aux_dev = sof_aux;
332 card->num_aux_devs = num_aux;
333
334 /* SDW */
335 if (sdw_be_num) {
336 ret = create_sdw_dailinks(card, &dai_links, &be_id,
337 sof_dais, &codec_conf);
338 if (ret)
339 return ret;
340 }
341
342 /* dmic */
343 if (dmic_num > 0) {
344 if (ctx->ignore_internal_dmic) {
345 dev_warn(dev, "Ignoring ACP DMIC\n");
346 } else {
347 ret = create_dmic_dailinks(card, &dai_links, &be_id, 1);
348 if (ret)
349 return ret;
350 }
351 }
352
353 WARN_ON(codec_conf != card->codec_conf + card->num_configs);
354 WARN_ON(dai_links != card->dai_link + card->num_links);
355
356 return ret;
357 }
358
mc_probe(struct platform_device * pdev)359 static int mc_probe(struct platform_device *pdev)
360 {
361 struct snd_soc_acpi_mach *mach = dev_get_platdata(&pdev->dev);
362 struct snd_soc_card *card;
363 struct amd_mc_ctx *amd_ctx;
364 struct asoc_sdw_mc_private *ctx;
365 int amp_num = 0, i;
366 int ret;
367
368 amd_ctx = devm_kzalloc(&pdev->dev, sizeof(*amd_ctx), GFP_KERNEL);
369 if (!amd_ctx)
370 return -ENOMEM;
371
372 amd_ctx->acp_rev = mach->mach_params.subsystem_rev;
373 amd_ctx->max_sdw_links = ACP63_SDW_MAX_LINKS;
374 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
375 if (!ctx)
376 return -ENOMEM;
377 ctx->codec_info_list_count = asoc_sdw_get_codec_info_list_count();
378 ctx->private = amd_ctx;
379 card = &ctx->card;
380 card->dev = &pdev->dev;
381 card->name = "amd-soundwire";
382 card->owner = THIS_MODULE;
383 card->late_probe = asoc_sdw_card_late_probe;
384
385 snd_soc_card_set_drvdata(card, ctx);
386
387 dmi_check_system(sof_sdw_quirk_table);
388
389 if (quirk_override != -1) {
390 dev_info(card->dev, "Overriding quirk 0x%lx => 0x%x\n",
391 sof_sdw_quirk, quirk_override);
392 sof_sdw_quirk = quirk_override;
393 }
394
395 log_quirks(card->dev);
396
397 ctx->mc_quirk = sof_sdw_quirk;
398 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */
399 for (i = 0; i < ctx->codec_info_list_count; i++)
400 codec_info_list[i].amp_num = 0;
401
402 ret = sof_card_dai_links_create(card);
403 if (ret < 0)
404 return ret;
405
406 /*
407 * the default amp_num is zero for each codec and
408 * amp_num will only be increased for active amp
409 * codecs on used platform
410 */
411 for (i = 0; i < ctx->codec_info_list_count; i++)
412 amp_num += codec_info_list[i].amp_num;
413
414 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
415 " cfg-amp:%d", amp_num);
416 if (!card->components)
417 return -ENOMEM;
418
419 /* Register the card */
420 ret = devm_snd_soc_register_card(card->dev, card);
421 if (ret) {
422 dev_err_probe(card->dev, ret, "snd_soc_register_card failed %d\n", ret);
423 asoc_sdw_mc_dailink_exit_loop(card);
424 return ret;
425 }
426
427 platform_set_drvdata(pdev, card);
428
429 return ret;
430 }
431
mc_remove(struct platform_device * pdev)432 static void mc_remove(struct platform_device *pdev)
433 {
434 struct snd_soc_card *card = platform_get_drvdata(pdev);
435
436 asoc_sdw_mc_dailink_exit_loop(card);
437 }
438
439 static const struct platform_device_id mc_id_table[] = {
440 { "amd_sof_sdw", },
441 {}
442 };
443 MODULE_DEVICE_TABLE(platform, mc_id_table);
444
445 static struct platform_driver sof_sdw_driver = {
446 .driver = {
447 .name = "amd_sof_sdw",
448 .pm = &snd_soc_pm_ops,
449 },
450 .probe = mc_probe,
451 .remove = mc_remove,
452 .id_table = mc_id_table,
453 };
454
455 module_platform_driver(sof_sdw_driver);
456
457 MODULE_DESCRIPTION("ASoC AMD SoundWire Generic Machine driver");
458 MODULE_AUTHOR("Vijendar Mukunda <Vijendar.Mukunda@amd.com");
459 MODULE_LICENSE("GPL");
460 MODULE_IMPORT_NS("SND_SOC_SDW_UTILS");
461 MODULE_IMPORT_NS("SND_SOC_AMD_SDW_MACH");
462