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