1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020 Intel Corporation
3
4 /*
5 * sof_sdw - ASOC Machine driver for Intel SoundWire platforms
6 */
7
8 #include <linux/device.h>
9 #include <linux/dmi.h>
10 #include <linux/module.h>
11 #include <linux/soundwire/sdw.h>
12 #include <linux/soundwire/sdw_type.h>
13 #include <sound/soc.h>
14 #include <sound/soc-acpi.h>
15 #include "sof_sdw_common.h"
16
17 unsigned long sof_sdw_quirk = SOF_RT711_JD_SRC_JD1;
18 static int quirk_override = -1;
19 module_param_named(quirk, quirk_override, int, 0444);
20 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
21
22 #define INC_ID(BE, CPU, LINK) do { (BE)++; (CPU)++; (LINK)++; } while (0)
23
log_quirks(struct device * dev)24 static void log_quirks(struct device *dev)
25 {
26 if (SOF_RT711_JDSRC(sof_sdw_quirk))
27 dev_dbg(dev, "quirk realtek,jack-detect-source %ld\n",
28 SOF_RT711_JDSRC(sof_sdw_quirk));
29 if (sof_sdw_quirk & SOF_SDW_FOUR_SPK)
30 dev_dbg(dev, "quirk SOF_SDW_FOUR_SPK enabled\n");
31 if (sof_sdw_quirk & SOF_SDW_TGL_HDMI)
32 dev_dbg(dev, "quirk SOF_SDW_TGL_HDMI enabled\n");
33 if (sof_sdw_quirk & SOF_SDW_PCH_DMIC)
34 dev_dbg(dev, "quirk SOF_SDW_PCH_DMIC enabled\n");
35 if (SOF_SSP_GET_PORT(sof_sdw_quirk))
36 dev_dbg(dev, "SSP port %ld\n",
37 SOF_SSP_GET_PORT(sof_sdw_quirk));
38 if (sof_sdw_quirk & SOF_RT715_DAI_ID_FIX)
39 dev_dbg(dev, "quirk SOF_RT715_DAI_ID_FIX enabled\n");
40 if (sof_sdw_quirk & SOF_SDW_NO_AGGREGATION)
41 dev_dbg(dev, "quirk SOF_SDW_NO_AGGREGATION enabled\n");
42 }
43
sof_sdw_quirk_cb(const struct dmi_system_id * id)44 static int sof_sdw_quirk_cb(const struct dmi_system_id *id)
45 {
46 sof_sdw_quirk = (unsigned long)id->driver_data;
47 return 1;
48 }
49
50 static const struct dmi_system_id sof_sdw_quirk_table[] = {
51 {
52 .callback = sof_sdw_quirk_cb,
53 .matches = {
54 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
55 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A3E")
56 },
57 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
58 SOF_RT715_DAI_ID_FIX),
59 },
60 {
61 .callback = sof_sdw_quirk_cb,
62 .matches = {
63 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
64 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "09C6")
65 },
66 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
67 SOF_RT715_DAI_ID_FIX),
68 },
69 {
70 /* early version of SKU 09C6 */
71 .callback = sof_sdw_quirk_cb,
72 .matches = {
73 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
74 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0983")
75 },
76 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
77 SOF_RT715_DAI_ID_FIX),
78 },
79 {
80 .callback = sof_sdw_quirk_cb,
81 .matches = {
82 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
83 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "098F"),
84 },
85 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
86 SOF_RT715_DAI_ID_FIX |
87 SOF_SDW_FOUR_SPK),
88 },
89 {
90 .callback = sof_sdw_quirk_cb,
91 .matches = {
92 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
93 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0990"),
94 },
95 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
96 SOF_RT715_DAI_ID_FIX |
97 SOF_SDW_FOUR_SPK),
98 },
99 {
100 .callback = sof_sdw_quirk_cb,
101 .matches = {
102 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
103 DMI_MATCH(DMI_PRODUCT_NAME,
104 "Tiger Lake Client Platform"),
105 },
106 .driver_data = (void *)(SOF_RT711_JD_SRC_JD1 |
107 SOF_SDW_TGL_HDMI | SOF_SDW_PCH_DMIC |
108 SOF_SSP_PORT(SOF_I2S_SSP2)),
109 },
110 {
111 .callback = sof_sdw_quirk_cb,
112 .matches = {
113 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
114 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
115 },
116 .driver_data = (void *)SOF_SDW_PCH_DMIC,
117 },
118 {
119 .callback = sof_sdw_quirk_cb,
120 .matches = {
121 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
122 DMI_MATCH(DMI_PRODUCT_NAME, "CometLake Client"),
123 },
124 .driver_data = (void *)SOF_SDW_PCH_DMIC,
125 },
126 {
127 .callback = sof_sdw_quirk_cb,
128 .matches = {
129 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
130 DMI_MATCH(DMI_PRODUCT_NAME, "Volteer"),
131 },
132 .driver_data = (void *)(SOF_SDW_TGL_HDMI | SOF_SDW_PCH_DMIC |
133 SOF_SDW_FOUR_SPK),
134 },
135 {
136 .callback = sof_sdw_quirk_cb,
137 .matches = {
138 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
139 DMI_MATCH(DMI_PRODUCT_NAME, "Ripto"),
140 },
141 .driver_data = (void *)(SOF_SDW_TGL_HDMI | SOF_SDW_PCH_DMIC |
142 SOF_SDW_FOUR_SPK),
143 },
144
145 {}
146 };
147
148 static struct snd_soc_dai_link_component dmic_component[] = {
149 {
150 .name = "dmic-codec",
151 .dai_name = "dmic-hifi",
152 }
153 };
154
155 static struct snd_soc_dai_link_component platform_component[] = {
156 {
157 /* name might be overridden during probe */
158 .name = "0000:00:1f.3"
159 }
160 };
161
162 /* these wrappers are only needed to avoid typecast compilation errors */
sdw_startup(struct snd_pcm_substream * substream)163 int sdw_startup(struct snd_pcm_substream *substream)
164 {
165 return sdw_startup_stream(substream);
166 }
167
sdw_prepare(struct snd_pcm_substream * substream)168 int sdw_prepare(struct snd_pcm_substream *substream)
169 {
170 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
171 struct sdw_stream_runtime *sdw_stream;
172 struct snd_soc_dai *dai;
173
174 /* Find stream from first CPU DAI */
175 dai = asoc_rtd_to_cpu(rtd, 0);
176
177 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
178
179 if (IS_ERR(sdw_stream)) {
180 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
181 return PTR_ERR(sdw_stream);
182 }
183
184 return sdw_prepare_stream(sdw_stream);
185 }
186
sdw_trigger(struct snd_pcm_substream * substream,int cmd)187 int sdw_trigger(struct snd_pcm_substream *substream, int cmd)
188 {
189 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
190 struct sdw_stream_runtime *sdw_stream;
191 struct snd_soc_dai *dai;
192 int ret;
193
194 /* Find stream from first CPU DAI */
195 dai = asoc_rtd_to_cpu(rtd, 0);
196
197 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
198
199 if (IS_ERR(sdw_stream)) {
200 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
201 return PTR_ERR(sdw_stream);
202 }
203
204 switch (cmd) {
205 case SNDRV_PCM_TRIGGER_START:
206 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
207 case SNDRV_PCM_TRIGGER_RESUME:
208 ret = sdw_enable_stream(sdw_stream);
209 break;
210
211 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
212 case SNDRV_PCM_TRIGGER_SUSPEND:
213 case SNDRV_PCM_TRIGGER_STOP:
214 ret = sdw_disable_stream(sdw_stream);
215 break;
216 default:
217 ret = -EINVAL;
218 break;
219 }
220
221 if (ret)
222 dev_err(rtd->dev, "%s trigger %d failed: %d", __func__, cmd, ret);
223
224 return ret;
225 }
226
sdw_hw_free(struct snd_pcm_substream * substream)227 int sdw_hw_free(struct snd_pcm_substream *substream)
228 {
229 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
230 struct sdw_stream_runtime *sdw_stream;
231 struct snd_soc_dai *dai;
232
233 /* Find stream from first CPU DAI */
234 dai = asoc_rtd_to_cpu(rtd, 0);
235
236 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
237
238 if (IS_ERR(sdw_stream)) {
239 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
240 return PTR_ERR(sdw_stream);
241 }
242
243 return sdw_deprepare_stream(sdw_stream);
244 }
245
sdw_shutdown(struct snd_pcm_substream * substream)246 void sdw_shutdown(struct snd_pcm_substream *substream)
247 {
248 sdw_shutdown_stream(substream);
249 }
250
251 static const struct snd_soc_ops sdw_ops = {
252 .startup = sdw_startup,
253 .prepare = sdw_prepare,
254 .trigger = sdw_trigger,
255 .hw_free = sdw_hw_free,
256 .shutdown = sdw_shutdown,
257 };
258
259 static struct sof_sdw_codec_info codec_info_list[] = {
260 {
261 .part_id = 0x700,
262 .direction = {true, true},
263 .dai_name = "rt700-aif1",
264 .init = sof_sdw_rt700_init,
265 },
266 {
267 .part_id = 0x711,
268 .version_id = 3,
269 .direction = {true, true},
270 .dai_name = "rt711-sdca-aif1",
271 .init = sof_sdw_rt711_sdca_init,
272 .exit = sof_sdw_rt711_sdca_exit,
273 },
274 {
275 .part_id = 0x711,
276 .version_id = 2,
277 .direction = {true, true},
278 .dai_name = "rt711-aif1",
279 .init = sof_sdw_rt711_init,
280 .exit = sof_sdw_rt711_exit,
281 },
282 {
283 .part_id = 0x1308,
284 .acpi_id = "10EC1308",
285 .direction = {true, false},
286 .dai_name = "rt1308-aif",
287 .ops = &sof_sdw_rt1308_i2s_ops,
288 .init = sof_sdw_rt1308_init,
289 },
290 {
291 .part_id = 0x1316,
292 .direction = {true, true},
293 .dai_name = "rt1316-aif",
294 .init = sof_sdw_rt1316_init,
295 },
296 {
297 .part_id = 0x714,
298 .version_id = 3,
299 .direction = {false, true},
300 .dai_name = "rt715-aif2",
301 .init = sof_sdw_rt715_sdca_init,
302 },
303 {
304 .part_id = 0x715,
305 .version_id = 3,
306 .direction = {false, true},
307 .dai_name = "rt715-aif2",
308 .init = sof_sdw_rt715_sdca_init,
309 },
310 {
311 .part_id = 0x714,
312 .version_id = 2,
313 .direction = {false, true},
314 .dai_name = "rt715-aif2",
315 .init = sof_sdw_rt715_init,
316 },
317 {
318 .part_id = 0x715,
319 .version_id = 2,
320 .direction = {false, true},
321 .dai_name = "rt715-aif2",
322 .init = sof_sdw_rt715_init,
323 },
324 {
325 .part_id = 0x8373,
326 .direction = {true, true},
327 .dai_name = "max98373-aif1",
328 .init = sof_sdw_mx8373_init,
329 .codec_card_late_probe = sof_sdw_mx8373_late_probe,
330 },
331 {
332 .part_id = 0x5682,
333 .direction = {true, true},
334 .dai_name = "rt5682-sdw",
335 .init = sof_sdw_rt5682_init,
336 },
337 };
338
find_codec_info_part(u64 adr)339 static inline int find_codec_info_part(u64 adr)
340 {
341 unsigned int part_id, sdw_version;
342 int i;
343
344 part_id = SDW_PART_ID(adr);
345 sdw_version = SDW_VERSION(adr);
346 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
347 /*
348 * A codec info is for all sdw version with the part id if
349 * version_id is not specified in the codec info.
350 */
351 if (part_id == codec_info_list[i].part_id &&
352 (!codec_info_list[i].version_id ||
353 sdw_version == codec_info_list[i].version_id))
354 return i;
355
356 return -EINVAL;
357
358 }
359
find_codec_info_acpi(const u8 * acpi_id)360 static inline int find_codec_info_acpi(const u8 *acpi_id)
361 {
362 int i;
363
364 if (!acpi_id[0])
365 return -EINVAL;
366
367 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
368 if (!memcmp(codec_info_list[i].acpi_id, acpi_id,
369 ACPI_ID_LEN))
370 break;
371
372 if (i == ARRAY_SIZE(codec_info_list))
373 return -EINVAL;
374
375 return i;
376 }
377
378 /*
379 * get BE dailink number and CPU DAI number based on sdw link adr.
380 * Since some sdw slaves may be aggregated, the CPU DAI number
381 * may be larger than the number of BE dailinks.
382 */
get_sdw_dailink_info(const struct snd_soc_acpi_link_adr * links,int * sdw_be_num,int * sdw_cpu_dai_num)383 static int get_sdw_dailink_info(const struct snd_soc_acpi_link_adr *links,
384 int *sdw_be_num, int *sdw_cpu_dai_num)
385 {
386 const struct snd_soc_acpi_link_adr *link;
387 bool group_visited[SDW_MAX_GROUPS];
388 bool no_aggregation;
389 int i;
390
391 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
392 *sdw_cpu_dai_num = 0;
393 *sdw_be_num = 0;
394
395 if (!links)
396 return -EINVAL;
397
398 for (i = 0; i < SDW_MAX_GROUPS; i++)
399 group_visited[i] = false;
400
401 for (link = links; link->num_adr; link++) {
402 const struct snd_soc_acpi_endpoint *endpoint;
403 int codec_index;
404 int stream;
405 u64 adr;
406
407 adr = link->adr_d->adr;
408 codec_index = find_codec_info_part(adr);
409 if (codec_index < 0)
410 return codec_index;
411
412 endpoint = link->adr_d->endpoints;
413
414 /* count DAI number for playback and capture */
415 for_each_pcm_streams(stream) {
416 if (!codec_info_list[codec_index].direction[stream])
417 continue;
418
419 (*sdw_cpu_dai_num)++;
420
421 /* count BE for each non-aggregated slave or group */
422 if (!endpoint->aggregated || no_aggregation ||
423 !group_visited[endpoint->group_id])
424 (*sdw_be_num)++;
425 }
426
427 if (endpoint->aggregated)
428 group_visited[endpoint->group_id] = true;
429 }
430
431 return 0;
432 }
433
init_dai_link(struct snd_soc_dai_link * dai_links,int be_id,char * name,int playback,int capture,struct snd_soc_dai_link_component * cpus,int cpus_num,struct snd_soc_dai_link_component * codecs,int codecs_num,int (* init)(struct snd_soc_pcm_runtime * rtd),const struct snd_soc_ops * ops)434 static void init_dai_link(struct snd_soc_dai_link *dai_links, int be_id,
435 char *name, int playback, int capture,
436 struct snd_soc_dai_link_component *cpus,
437 int cpus_num,
438 struct snd_soc_dai_link_component *codecs,
439 int codecs_num,
440 int (*init)(struct snd_soc_pcm_runtime *rtd),
441 const struct snd_soc_ops *ops)
442 {
443 dai_links->id = be_id;
444 dai_links->name = name;
445 dai_links->platforms = platform_component;
446 dai_links->num_platforms = ARRAY_SIZE(platform_component);
447 dai_links->nonatomic = true;
448 dai_links->no_pcm = 1;
449 dai_links->cpus = cpus;
450 dai_links->num_cpus = cpus_num;
451 dai_links->codecs = codecs;
452 dai_links->num_codecs = codecs_num;
453 dai_links->dpcm_playback = playback;
454 dai_links->dpcm_capture = capture;
455 dai_links->init = init;
456 dai_links->ops = ops;
457 }
458
is_unique_device(const struct snd_soc_acpi_link_adr * link,unsigned int sdw_version,unsigned int mfg_id,unsigned int part_id,unsigned int class_id,int index_in_link)459 static bool is_unique_device(const struct snd_soc_acpi_link_adr *link,
460 unsigned int sdw_version,
461 unsigned int mfg_id,
462 unsigned int part_id,
463 unsigned int class_id,
464 int index_in_link
465 )
466 {
467 int i;
468
469 for (i = 0; i < link->num_adr; i++) {
470 unsigned int sdw1_version, mfg1_id, part1_id, class1_id;
471 u64 adr;
472
473 /* skip itself */
474 if (i == index_in_link)
475 continue;
476
477 adr = link->adr_d[i].adr;
478
479 sdw1_version = SDW_VERSION(adr);
480 mfg1_id = SDW_MFG_ID(adr);
481 part1_id = SDW_PART_ID(adr);
482 class1_id = SDW_CLASS_ID(adr);
483
484 if (sdw_version == sdw1_version &&
485 mfg_id == mfg1_id &&
486 part_id == part1_id &&
487 class_id == class1_id)
488 return false;
489 }
490
491 return true;
492 }
493
create_codec_dai_name(struct device * dev,const struct snd_soc_acpi_link_adr * link,struct snd_soc_dai_link_component * codec,int offset,struct snd_soc_codec_conf * codec_conf,int codec_count,int * codec_conf_index)494 static int create_codec_dai_name(struct device *dev,
495 const struct snd_soc_acpi_link_adr *link,
496 struct snd_soc_dai_link_component *codec,
497 int offset,
498 struct snd_soc_codec_conf *codec_conf,
499 int codec_count,
500 int *codec_conf_index)
501 {
502 int i;
503
504 /* sanity check */
505 if (*codec_conf_index + link->num_adr > codec_count) {
506 dev_err(dev, "codec_conf: out-of-bounds access requested\n");
507 return -EINVAL;
508 }
509
510 for (i = 0; i < link->num_adr; i++) {
511 unsigned int sdw_version, unique_id, mfg_id;
512 unsigned int link_id, part_id, class_id;
513 int codec_index, comp_index;
514 char *codec_str;
515 u64 adr;
516
517 adr = link->adr_d[i].adr;
518
519 sdw_version = SDW_VERSION(adr);
520 link_id = SDW_DISCO_LINK_ID(adr);
521 unique_id = SDW_UNIQUE_ID(adr);
522 mfg_id = SDW_MFG_ID(adr);
523 part_id = SDW_PART_ID(adr);
524 class_id = SDW_CLASS_ID(adr);
525
526 comp_index = i + offset;
527 if (is_unique_device(link, sdw_version, mfg_id, part_id,
528 class_id, i)) {
529 codec_str = "sdw:%x:%x:%x:%x";
530 codec[comp_index].name =
531 devm_kasprintf(dev, GFP_KERNEL, codec_str,
532 link_id, mfg_id, part_id,
533 class_id);
534 } else {
535 codec_str = "sdw:%x:%x:%x:%x:%x";
536 codec[comp_index].name =
537 devm_kasprintf(dev, GFP_KERNEL, codec_str,
538 link_id, mfg_id, part_id,
539 class_id, unique_id);
540 }
541
542 if (!codec[comp_index].name)
543 return -ENOMEM;
544
545 codec_index = find_codec_info_part(adr);
546 if (codec_index < 0)
547 return codec_index;
548
549 codec[comp_index].dai_name =
550 codec_info_list[codec_index].dai_name;
551
552 codec_conf[*codec_conf_index].dlc = codec[comp_index];
553 codec_conf[*codec_conf_index].name_prefix = link->adr_d[i].name_prefix;
554
555 ++*codec_conf_index;
556 }
557
558 return 0;
559 }
560
set_codec_init_func(const struct snd_soc_acpi_link_adr * link,struct snd_soc_dai_link * dai_links,bool playback,int group_id)561 static int set_codec_init_func(const struct snd_soc_acpi_link_adr *link,
562 struct snd_soc_dai_link *dai_links,
563 bool playback, int group_id)
564 {
565 int i;
566
567 do {
568 /*
569 * Initialize the codec. If codec is part of an aggregated
570 * group (group_id>0), initialize all codecs belonging to
571 * same group.
572 */
573 for (i = 0; i < link->num_adr; i++) {
574 int codec_index;
575
576 codec_index = find_codec_info_part(link->adr_d[i].adr);
577
578 if (codec_index < 0)
579 return codec_index;
580 /* The group_id is > 0 iff the codec is aggregated */
581 if (link->adr_d[i].endpoints->group_id != group_id)
582 continue;
583 if (codec_info_list[codec_index].init)
584 codec_info_list[codec_index].init(link,
585 dai_links,
586 &codec_info_list[codec_index],
587 playback);
588 }
589 link++;
590 } while (link->mask && group_id);
591
592 return 0;
593 }
594
595 /*
596 * check endpoint status in slaves and gather link ID for all slaves in
597 * the same group to generate different CPU DAI. Now only support
598 * one sdw link with all slaves set with only single group id.
599 *
600 * one slave on one sdw link with aggregated = 0
601 * one sdw BE DAI <---> one-cpu DAI <---> one-codec DAI
602 *
603 * two or more slaves on one sdw link with aggregated = 0
604 * one sdw BE DAI <---> one-cpu DAI <---> multi-codec DAIs
605 *
606 * multiple links with multiple slaves with aggregated = 1
607 * one sdw BE DAI <---> 1 .. N CPU DAIs <----> 1 .. N codec DAIs
608 */
get_slave_info(const struct snd_soc_acpi_link_adr * adr_link,struct device * dev,int * cpu_dai_id,int * cpu_dai_num,int * codec_num,int * group_id,bool * group_generated)609 static int get_slave_info(const struct snd_soc_acpi_link_adr *adr_link,
610 struct device *dev, int *cpu_dai_id, int *cpu_dai_num,
611 int *codec_num, int *group_id,
612 bool *group_generated)
613 {
614 const struct snd_soc_acpi_adr_device *adr_d;
615 const struct snd_soc_acpi_link_adr *adr_next;
616 bool no_aggregation;
617 int index = 0;
618
619 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
620 *codec_num = adr_link->num_adr;
621 adr_d = adr_link->adr_d;
622
623 /* make sure the link mask has a single bit set */
624 if (!is_power_of_2(adr_link->mask))
625 return -EINVAL;
626
627 cpu_dai_id[index++] = ffs(adr_link->mask) - 1;
628 if (!adr_d->endpoints->aggregated || no_aggregation) {
629 *cpu_dai_num = 1;
630 *group_id = 0;
631 return 0;
632 }
633
634 *group_id = adr_d->endpoints->group_id;
635
636 /* gather other link ID of slaves in the same group */
637 for (adr_next = adr_link + 1; adr_next && adr_next->num_adr;
638 adr_next++) {
639 const struct snd_soc_acpi_endpoint *endpoint;
640
641 endpoint = adr_next->adr_d->endpoints;
642 if (!endpoint->aggregated ||
643 endpoint->group_id != *group_id)
644 continue;
645
646 /* make sure the link mask has a single bit set */
647 if (!is_power_of_2(adr_next->mask))
648 return -EINVAL;
649
650 if (index >= SDW_MAX_CPU_DAIS) {
651 dev_err(dev, " cpu_dai_id array overflows");
652 return -EINVAL;
653 }
654
655 cpu_dai_id[index++] = ffs(adr_next->mask) - 1;
656 *codec_num += adr_next->num_adr;
657 }
658
659 /*
660 * indicate CPU DAIs for this group have been generated
661 * to avoid generating CPU DAIs for this group again.
662 */
663 group_generated[*group_id] = true;
664 *cpu_dai_num = index;
665
666 return 0;
667 }
668
create_sdw_dailink(struct device * dev,int * be_index,struct snd_soc_dai_link * dai_links,int sdw_be_num,int sdw_cpu_dai_num,struct snd_soc_dai_link_component * cpus,const struct snd_soc_acpi_link_adr * link,int * cpu_id,bool * group_generated,struct snd_soc_codec_conf * codec_conf,int codec_count,int * codec_conf_index)669 static int create_sdw_dailink(struct device *dev, int *be_index,
670 struct snd_soc_dai_link *dai_links,
671 int sdw_be_num, int sdw_cpu_dai_num,
672 struct snd_soc_dai_link_component *cpus,
673 const struct snd_soc_acpi_link_adr *link,
674 int *cpu_id, bool *group_generated,
675 struct snd_soc_codec_conf *codec_conf,
676 int codec_count,
677 int *codec_conf_index)
678 {
679 const struct snd_soc_acpi_link_adr *link_next;
680 struct snd_soc_dai_link_component *codecs;
681 int cpu_dai_id[SDW_MAX_CPU_DAIS];
682 int cpu_dai_num, cpu_dai_index;
683 unsigned int group_id;
684 int codec_idx = 0;
685 int i = 0, j = 0;
686 int codec_index;
687 int codec_num;
688 int stream;
689 int ret;
690 int k;
691
692 ret = get_slave_info(link, dev, cpu_dai_id, &cpu_dai_num, &codec_num,
693 &group_id, group_generated);
694 if (ret)
695 return ret;
696
697 codecs = devm_kcalloc(dev, codec_num, sizeof(*codecs), GFP_KERNEL);
698 if (!codecs)
699 return -ENOMEM;
700
701 /* generate codec name on different links in the same group */
702 for (link_next = link; link_next && link_next->num_adr &&
703 i < cpu_dai_num; link_next++) {
704 const struct snd_soc_acpi_endpoint *endpoints;
705
706 endpoints = link_next->adr_d->endpoints;
707 if (group_id && (!endpoints->aggregated ||
708 endpoints->group_id != group_id))
709 continue;
710
711 /* skip the link excluded by this processed group */
712 if (cpu_dai_id[i] != ffs(link_next->mask) - 1)
713 continue;
714
715 ret = create_codec_dai_name(dev, link_next, codecs, codec_idx,
716 codec_conf, codec_count, codec_conf_index);
717 if (ret < 0)
718 return ret;
719
720 /* check next link to create codec dai in the processed group */
721 i++;
722 codec_idx += link_next->num_adr;
723 }
724
725 /* find codec info to create BE DAI */
726 codec_index = find_codec_info_part(link->adr_d[0].adr);
727 if (codec_index < 0)
728 return codec_index;
729
730 cpu_dai_index = *cpu_id;
731 for_each_pcm_streams(stream) {
732 char *name, *cpu_name;
733 int playback, capture;
734 static const char * const sdw_stream_name[] = {
735 "SDW%d-Playback",
736 "SDW%d-Capture",
737 };
738
739 if (!codec_info_list[codec_index].direction[stream])
740 continue;
741
742 /* create stream name according to first link id */
743 name = devm_kasprintf(dev, GFP_KERNEL,
744 sdw_stream_name[stream], cpu_dai_id[0]);
745 if (!name)
746 return -ENOMEM;
747
748 /*
749 * generate CPU DAI name base on the sdw link ID and
750 * PIN ID with offset of 2 according to sdw dai driver.
751 */
752 for (k = 0; k < cpu_dai_num; k++) {
753 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
754 "SDW%d Pin%d", cpu_dai_id[k],
755 j + SDW_INTEL_BIDIR_PDI_BASE);
756 if (!cpu_name)
757 return -ENOMEM;
758
759 if (cpu_dai_index >= sdw_cpu_dai_num) {
760 dev_err(dev, "invalid cpu dai index %d",
761 cpu_dai_index);
762 return -EINVAL;
763 }
764
765 cpus[cpu_dai_index++].dai_name = cpu_name;
766 }
767
768 if (*be_index >= sdw_be_num) {
769 dev_err(dev, " invalid be dai index %d", *be_index);
770 return -EINVAL;
771 }
772
773 if (*cpu_id >= sdw_cpu_dai_num) {
774 dev_err(dev, " invalid cpu dai index %d", *cpu_id);
775 return -EINVAL;
776 }
777
778 playback = (stream == SNDRV_PCM_STREAM_PLAYBACK);
779 capture = (stream == SNDRV_PCM_STREAM_CAPTURE);
780 init_dai_link(dai_links + *be_index, *be_index, name,
781 playback, capture,
782 cpus + *cpu_id, cpu_dai_num,
783 codecs, codec_num,
784 NULL, &sdw_ops);
785
786 ret = set_codec_init_func(link, dai_links + (*be_index)++,
787 playback, group_id);
788 if (ret < 0) {
789 dev_err(dev, "failed to init codec %d", codec_index);
790 return ret;
791 }
792
793 *cpu_id += cpu_dai_num;
794 j++;
795 }
796
797 return 0;
798 }
799
800 /*
801 * DAI link ID of SSP & DMIC & HDMI are based on last
802 * link ID used by sdw link. Since be_id may be changed
803 * in init func of sdw codec, it is not equal to be_id
804 */
get_next_be_id(struct snd_soc_dai_link * links,int be_id)805 static inline int get_next_be_id(struct snd_soc_dai_link *links,
806 int be_id)
807 {
808 return links[be_id - 1].id + 1;
809 }
810
811 #define IDISP_CODEC_MASK 0x4
812
sof_card_codec_conf_alloc(struct device * dev,struct snd_soc_acpi_mach_params * mach_params,struct snd_soc_codec_conf ** codec_conf,int * codec_conf_count)813 static int sof_card_codec_conf_alloc(struct device *dev,
814 struct snd_soc_acpi_mach_params *mach_params,
815 struct snd_soc_codec_conf **codec_conf,
816 int *codec_conf_count)
817 {
818 const struct snd_soc_acpi_link_adr *adr_link;
819 struct snd_soc_codec_conf *c_conf;
820 int num_codecs = 0;
821 int i;
822
823 adr_link = mach_params->links;
824 if (!adr_link)
825 return -EINVAL;
826
827 /* generate DAI links by each sdw link */
828 for (; adr_link->num_adr; adr_link++) {
829 for (i = 0; i < adr_link->num_adr; i++) {
830 if (!adr_link->adr_d[i].name_prefix) {
831 dev_err(dev, "codec 0x%llx does not have a name prefix\n",
832 adr_link->adr_d[i].adr);
833 return -EINVAL;
834 }
835 }
836 num_codecs += adr_link->num_adr;
837 }
838
839 c_conf = devm_kzalloc(dev, num_codecs * sizeof(*c_conf), GFP_KERNEL);
840 if (!c_conf)
841 return -ENOMEM;
842
843 *codec_conf = c_conf;
844 *codec_conf_count = num_codecs;
845
846 return 0;
847 }
848
sof_card_dai_links_create(struct device * dev,struct snd_soc_acpi_mach * mach,struct snd_soc_card * card)849 static int sof_card_dai_links_create(struct device *dev,
850 struct snd_soc_acpi_mach *mach,
851 struct snd_soc_card *card)
852 {
853 int ssp_num, sdw_be_num = 0, hdmi_num = 0, dmic_num;
854 struct mc_private *ctx = snd_soc_card_get_drvdata(card);
855 struct snd_soc_dai_link_component *idisp_components;
856 struct snd_soc_dai_link_component *ssp_components;
857 struct snd_soc_acpi_mach_params *mach_params;
858 const struct snd_soc_acpi_link_adr *adr_link;
859 struct snd_soc_dai_link_component *cpus;
860 struct snd_soc_codec_conf *codec_conf;
861 int codec_conf_count;
862 int codec_conf_index = 0;
863 bool group_generated[SDW_MAX_GROUPS];
864 int ssp_codec_index, ssp_mask;
865 struct snd_soc_dai_link *links;
866 int num_links, link_id = 0;
867 char *name, *cpu_name;
868 int total_cpu_dai_num;
869 int sdw_cpu_dai_num;
870 int i, j, be_id = 0;
871 int cpu_id = 0;
872 int comp_num;
873 int ret;
874
875 mach_params = &mach->mach_params;
876
877 /* allocate codec conf, will be populated when dailinks are created */
878 ret = sof_card_codec_conf_alloc(dev, mach_params, &codec_conf, &codec_conf_count);
879 if (ret < 0)
880 return ret;
881
882 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */
883 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
884 codec_info_list[i].amp_num = 0;
885
886 if (sof_sdw_quirk & SOF_SDW_TGL_HDMI)
887 hdmi_num = SOF_TGL_HDMI_COUNT;
888 else
889 hdmi_num = SOF_PRE_TGL_HDMI_COUNT;
890
891 ssp_mask = SOF_SSP_GET_PORT(sof_sdw_quirk);
892 /*
893 * on generic tgl platform, I2S or sdw mode is supported
894 * based on board rework. A ACPI device is registered in
895 * system only when I2S mode is supported, not sdw mode.
896 * Here check ACPI ID to confirm I2S is supported.
897 */
898 ssp_codec_index = find_codec_info_acpi(mach->id);
899 ssp_num = ssp_codec_index >= 0 ? hweight_long(ssp_mask) : 0;
900 comp_num = hdmi_num + ssp_num;
901
902 ret = get_sdw_dailink_info(mach_params->links,
903 &sdw_be_num, &sdw_cpu_dai_num);
904 if (ret < 0) {
905 dev_err(dev, "failed to get sdw link info %d", ret);
906 return ret;
907 }
908
909 if (mach_params->codec_mask & IDISP_CODEC_MASK)
910 ctx->idisp_codec = true;
911
912 /* enable dmic01 & dmic16k */
913 dmic_num = (sof_sdw_quirk & SOF_SDW_PCH_DMIC) ? 2 : 0;
914 comp_num += dmic_num;
915
916 dev_dbg(dev, "sdw %d, ssp %d, dmic %d, hdmi %d", sdw_be_num, ssp_num,
917 dmic_num, ctx->idisp_codec ? hdmi_num : 0);
918
919 /* allocate BE dailinks */
920 num_links = comp_num + sdw_be_num;
921 links = devm_kcalloc(dev, num_links, sizeof(*links), GFP_KERNEL);
922
923 /* allocated CPU DAIs */
924 total_cpu_dai_num = comp_num + sdw_cpu_dai_num;
925 cpus = devm_kcalloc(dev, total_cpu_dai_num, sizeof(*cpus),
926 GFP_KERNEL);
927
928 if (!links || !cpus)
929 return -ENOMEM;
930
931 /* SDW */
932 if (!sdw_be_num)
933 goto SSP;
934
935 adr_link = mach_params->links;
936 if (!adr_link)
937 return -EINVAL;
938
939 /*
940 * SoundWire Slaves aggregated in the same group may be
941 * located on different hardware links. Clear array to indicate
942 * CPU DAIs for this group have not been generated.
943 */
944 for (i = 0; i < SDW_MAX_GROUPS; i++)
945 group_generated[i] = false;
946
947 /* generate DAI links by each sdw link */
948 for (; adr_link->num_adr; adr_link++) {
949 const struct snd_soc_acpi_endpoint *endpoint;
950
951 endpoint = adr_link->adr_d->endpoints;
952 if (endpoint->aggregated && !endpoint->group_id) {
953 dev_err(dev, "invalid group id on link %x",
954 adr_link->mask);
955 continue;
956 }
957
958 /* this group has been generated */
959 if (endpoint->aggregated &&
960 group_generated[endpoint->group_id])
961 continue;
962
963 ret = create_sdw_dailink(dev, &be_id, links, sdw_be_num,
964 sdw_cpu_dai_num, cpus, adr_link,
965 &cpu_id, group_generated,
966 codec_conf, codec_conf_count,
967 &codec_conf_index);
968 if (ret < 0) {
969 dev_err(dev, "failed to create dai link %d", be_id);
970 return -ENOMEM;
971 }
972 }
973
974 /* non-sdw DAI follows sdw DAI */
975 link_id = be_id;
976
977 /* get BE ID for non-sdw DAI */
978 be_id = get_next_be_id(links, be_id);
979
980 SSP:
981 /* SSP */
982 if (!ssp_num)
983 goto DMIC;
984
985 for (i = 0, j = 0; ssp_mask; i++, ssp_mask >>= 1) {
986 struct sof_sdw_codec_info *info;
987 int playback, capture;
988 char *codec_name;
989
990 if (!(ssp_mask & 0x1))
991 continue;
992
993 name = devm_kasprintf(dev, GFP_KERNEL,
994 "SSP%d-Codec", i);
995 if (!name)
996 return -ENOMEM;
997
998 cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", i);
999 if (!cpu_name)
1000 return -ENOMEM;
1001
1002 ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1003 GFP_KERNEL);
1004 if (!ssp_components)
1005 return -ENOMEM;
1006
1007 info = &codec_info_list[ssp_codec_index];
1008 codec_name = devm_kasprintf(dev, GFP_KERNEL, "i2c-%s:0%d",
1009 info->acpi_id, j++);
1010 if (!codec_name)
1011 return -ENOMEM;
1012
1013 ssp_components->name = codec_name;
1014 ssp_components->dai_name = info->dai_name;
1015 cpus[cpu_id].dai_name = cpu_name;
1016
1017 playback = info->direction[SNDRV_PCM_STREAM_PLAYBACK];
1018 capture = info->direction[SNDRV_PCM_STREAM_CAPTURE];
1019 init_dai_link(links + link_id, be_id, name,
1020 playback, capture,
1021 cpus + cpu_id, 1,
1022 ssp_components, 1,
1023 NULL, info->ops);
1024
1025 ret = info->init(NULL, links + link_id, info, 0);
1026 if (ret < 0)
1027 return ret;
1028
1029 INC_ID(be_id, cpu_id, link_id);
1030 }
1031
1032 DMIC:
1033 /* dmic */
1034 if (dmic_num > 0) {
1035 cpus[cpu_id].dai_name = "DMIC01 Pin";
1036 init_dai_link(links + link_id, be_id, "dmic01",
1037 0, 1, // DMIC only supports capture
1038 cpus + cpu_id, 1,
1039 dmic_component, 1,
1040 sof_sdw_dmic_init, NULL);
1041 INC_ID(be_id, cpu_id, link_id);
1042
1043 cpus[cpu_id].dai_name = "DMIC16k Pin";
1044 init_dai_link(links + link_id, be_id, "dmic16k",
1045 0, 1, // DMIC only supports capture
1046 cpus + cpu_id, 1,
1047 dmic_component, 1,
1048 /* don't call sof_sdw_dmic_init() twice */
1049 NULL, NULL);
1050 INC_ID(be_id, cpu_id, link_id);
1051 }
1052
1053 /* HDMI */
1054 if (hdmi_num > 0) {
1055 idisp_components = devm_kcalloc(dev, hdmi_num,
1056 sizeof(*idisp_components),
1057 GFP_KERNEL);
1058 if (!idisp_components)
1059 return -ENOMEM;
1060 }
1061
1062 for (i = 0; i < hdmi_num; i++) {
1063 name = devm_kasprintf(dev, GFP_KERNEL,
1064 "iDisp%d", i + 1);
1065 if (!name)
1066 return -ENOMEM;
1067
1068 if (ctx->idisp_codec) {
1069 idisp_components[i].name = "ehdaudio0D2";
1070 idisp_components[i].dai_name = devm_kasprintf(dev,
1071 GFP_KERNEL,
1072 "intel-hdmi-hifi%d",
1073 i + 1);
1074 if (!idisp_components[i].dai_name)
1075 return -ENOMEM;
1076 } else {
1077 idisp_components[i].name = "snd-soc-dummy";
1078 idisp_components[i].dai_name = "snd-soc-dummy-dai";
1079 }
1080
1081 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
1082 "iDisp%d Pin", i + 1);
1083 if (!cpu_name)
1084 return -ENOMEM;
1085
1086 cpus[cpu_id].dai_name = cpu_name;
1087 init_dai_link(links + link_id, be_id, name,
1088 1, 0, // HDMI only supports playback
1089 cpus + cpu_id, 1,
1090 idisp_components + i, 1,
1091 sof_sdw_hdmi_init, NULL);
1092 INC_ID(be_id, cpu_id, link_id);
1093 }
1094
1095 card->dai_link = links;
1096 card->num_links = num_links;
1097
1098 card->codec_conf = codec_conf;
1099 card->num_configs = codec_conf_count;
1100
1101 return 0;
1102 }
1103
sof_sdw_card_late_probe(struct snd_soc_card * card)1104 static int sof_sdw_card_late_probe(struct snd_soc_card *card)
1105 {
1106 int i, ret;
1107
1108 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1109 if (!codec_info_list[i].late_probe)
1110 continue;
1111
1112 ret = codec_info_list[i].codec_card_late_probe(card);
1113 if (ret < 0)
1114 return ret;
1115 }
1116
1117 return sof_sdw_hdmi_card_late_probe(card);
1118 }
1119
1120 /* SoC card */
1121 static const char sdw_card_long_name[] = "Intel Soundwire SOF";
1122
1123 static struct snd_soc_card card_sof_sdw = {
1124 .name = "soundwire",
1125 .owner = THIS_MODULE,
1126 .late_probe = sof_sdw_card_late_probe,
1127 };
1128
mc_probe(struct platform_device * pdev)1129 static int mc_probe(struct platform_device *pdev)
1130 {
1131 struct snd_soc_card *card = &card_sof_sdw;
1132 struct snd_soc_acpi_mach *mach;
1133 struct mc_private *ctx;
1134 int amp_num = 0, i;
1135 int ret;
1136
1137 dev_dbg(&pdev->dev, "Entry %s\n", __func__);
1138
1139 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1140 if (!ctx)
1141 return -ENOMEM;
1142
1143 dmi_check_system(sof_sdw_quirk_table);
1144
1145 if (quirk_override != -1) {
1146 dev_info(&pdev->dev, "Overriding quirk 0x%lx => 0x%x\n",
1147 sof_sdw_quirk, quirk_override);
1148 sof_sdw_quirk = quirk_override;
1149 }
1150 log_quirks(&pdev->dev);
1151
1152 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
1153
1154 card->dev = &pdev->dev;
1155 snd_soc_card_set_drvdata(card, ctx);
1156
1157 mach = pdev->dev.platform_data;
1158 ret = sof_card_dai_links_create(&pdev->dev, mach,
1159 card);
1160 if (ret < 0)
1161 return ret;
1162
1163 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
1164
1165 /*
1166 * the default amp_num is zero for each codec and
1167 * amp_num will only be increased for active amp
1168 * codecs on used platform
1169 */
1170 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
1171 amp_num += codec_info_list[i].amp_num;
1172
1173 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1174 "cfg-spk:%d cfg-amp:%d",
1175 (sof_sdw_quirk & SOF_SDW_FOUR_SPK)
1176 ? 4 : 2, amp_num);
1177 if (!card->components)
1178 return -ENOMEM;
1179
1180 card->long_name = sdw_card_long_name;
1181
1182 /* Register the card */
1183 ret = devm_snd_soc_register_card(&pdev->dev, card);
1184 if (ret) {
1185 dev_err(card->dev, "snd_soc_register_card failed %d\n", ret);
1186 return ret;
1187 }
1188
1189 platform_set_drvdata(pdev, card);
1190
1191 return ret;
1192 }
1193
mc_remove(struct platform_device * pdev)1194 static int mc_remove(struct platform_device *pdev)
1195 {
1196 struct snd_soc_card *card = platform_get_drvdata(pdev);
1197 struct snd_soc_dai_link *link;
1198 int ret;
1199 int i, j;
1200
1201 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1202 if (!codec_info_list[i].exit)
1203 continue;
1204 /*
1205 * We don't need to call .exit function if there is no matched
1206 * dai link found.
1207 */
1208 for_each_card_prelinks(card, j, link) {
1209 if (!strcmp(link->codecs[0].dai_name,
1210 codec_info_list[i].dai_name)) {
1211 ret = codec_info_list[i].exit(&pdev->dev, link);
1212 if (ret)
1213 dev_warn(&pdev->dev,
1214 "codec exit failed %d\n",
1215 ret);
1216 break;
1217 }
1218 }
1219 }
1220
1221 return 0;
1222 }
1223
1224 static struct platform_driver sof_sdw_driver = {
1225 .driver = {
1226 .name = "sof_sdw",
1227 .pm = &snd_soc_pm_ops,
1228 },
1229 .probe = mc_probe,
1230 .remove = mc_remove,
1231 };
1232
1233 module_platform_driver(sof_sdw_driver);
1234
1235 MODULE_DESCRIPTION("ASoC SoundWire Generic Machine driver");
1236 MODULE_AUTHOR("Bard Liao <yung-chuan.liao@linux.intel.com>");
1237 MODULE_AUTHOR("Rander Wang <rander.wang@linux.intel.com>");
1238 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
1239 MODULE_LICENSE("GPL v2");
1240 MODULE_ALIAS("platform:sof_sdw");
1241