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