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