xref: /linux/sound/soc/soc-core.c (revision a7ea04d1ad39d60da397de75b503062ad5fa562b)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // soc-core.c  --  ALSA SoC Audio Layer
4 //
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
9 //
10 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11 //         with code, comments and ideas from :-
12 //         Richard Purdie <richard@openedhand.com>
13 //
14 //  TODO:
15 //   o Add hw rules to enforce rates, etc.
16 //   o More testing with other codecs/machines.
17 //   o Add more codecs and platforms to ensure good API coverage.
18 //   o Support TDM on PCM and I2S
19 
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/pm.h>
25 #include <linux/bitops.h>
26 #include <linux/debugfs.h>
27 #include <linux/platform_device.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/ctype.h>
30 #include <linux/slab.h>
31 #include <linux/of.h>
32 #include <linux/of_graph.h>
33 #include <linux/dmi.h>
34 #include <linux/acpi.h>
35 #include <linux/string_choices.h>
36 #include <sound/core.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include <sound/soc.h>
40 #include <sound/soc-dpcm.h>
41 #include <sound/soc-topology.h>
42 #include <sound/soc-link.h>
43 #include <sound/initval.h>
44 
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/asoc.h>
47 
48 static DEFINE_MUTEX(client_mutex);
49 static LIST_HEAD(component_list);
50 static LIST_HEAD(unbind_card_list);
51 
52 #define for_each_component(component)			\
53 	list_for_each_entry(component, &component_list, list)
54 
55 /*
56  * This is used if driver don't need to have CPU/Codec/Platform
57  * dai_link. see soc.h
58  */
59 struct snd_soc_dai_link_component null_dailink_component[0];
60 EXPORT_SYMBOL_GPL(null_dailink_component);
61 
62 /*
63  * This is a timeout to do a DAPM powerdown after a stream is closed().
64  * It can be used to eliminate pops between different playback streams, e.g.
65  * between two audio tracks.
66  */
67 static int pmdown_time = 5000;
68 module_param(pmdown_time, int, 0);
69 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70 
71 static ssize_t pmdown_time_show(struct device *dev,
72 				struct device_attribute *attr, char *buf)
73 {
74 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
75 
76 	return sysfs_emit(buf, "%ld\n", rtd->pmdown_time);
77 }
78 
79 static ssize_t pmdown_time_store(struct device *dev,
80 				 struct device_attribute *attr,
81 				 const char *buf, size_t count)
82 {
83 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
84 	int ret;
85 
86 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
87 	if (ret)
88 		return ret;
89 
90 	return count;
91 }
92 
93 static DEVICE_ATTR_RW(pmdown_time);
94 
95 static struct attribute *soc_dev_attrs[] = {
96 	&dev_attr_pmdown_time.attr,
97 	NULL
98 };
99 
100 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
101 				       struct attribute *attr, int idx)
102 {
103 	struct device *dev = kobj_to_dev(kobj);
104 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
105 
106 	if (!rtd)
107 		return 0;
108 
109 	if (attr == &dev_attr_pmdown_time.attr)
110 		return attr->mode; /* always visible */
111 	return rtd->dai_link->num_codecs ? attr->mode : 0; /* enabled only with codec */
112 }
113 
114 static const struct attribute_group soc_dapm_dev_group = {
115 	.attrs = snd_soc_dapm_dev_attrs,
116 	.is_visible = soc_dev_attr_is_visible,
117 };
118 
119 static const struct attribute_group soc_dev_group = {
120 	.attrs = soc_dev_attrs,
121 	.is_visible = soc_dev_attr_is_visible,
122 };
123 
124 static const struct attribute_group *soc_dev_attr_groups[] = {
125 	&soc_dapm_dev_group,
126 	&soc_dev_group,
127 	NULL
128 };
129 
130 #ifdef CONFIG_DEBUG_FS
131 struct dentry *snd_soc_debugfs_root;
132 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
133 
134 static void soc_init_component_debugfs(struct snd_soc_component *component)
135 {
136 	if (!component->card->debugfs_card_root)
137 		return;
138 
139 	if (component->driver->debugfs_prefix) {
140 		char *name;
141 
142 		name = kasprintf(GFP_KERNEL, "%s:%s",
143 			component->driver->debugfs_prefix, component->name);
144 		if (name) {
145 			component->debugfs_root = debugfs_create_dir(name,
146 				component->card->debugfs_card_root);
147 			kfree(name);
148 		}
149 	} else {
150 		component->debugfs_root = debugfs_create_dir(component->name,
151 				component->card->debugfs_card_root);
152 	}
153 
154 	snd_soc_dapm_debugfs_init(snd_soc_component_to_dapm(component),
155 		component->debugfs_root);
156 }
157 
158 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
159 {
160 	if (!component->debugfs_root)
161 		return;
162 	debugfs_remove_recursive(component->debugfs_root);
163 	component->debugfs_root = NULL;
164 }
165 
166 static int dai_list_show(struct seq_file *m, void *v)
167 {
168 	struct snd_soc_component *component;
169 	struct snd_soc_dai *dai;
170 	guard(mutex)(&client_mutex);
171 
172 	for_each_component(component)
173 		for_each_component_dais(component, dai)
174 			seq_printf(m, "%s\n", dai->name);
175 
176 	return 0;
177 }
178 DEFINE_SHOW_ATTRIBUTE(dai_list);
179 
180 static int component_list_show(struct seq_file *m, void *v)
181 {
182 	struct snd_soc_component *component;
183 	guard(mutex)(&client_mutex);
184 
185 	for_each_component(component)
186 		seq_printf(m, "%s\n", component->name);
187 
188 	return 0;
189 }
190 DEFINE_SHOW_ATTRIBUTE(component_list);
191 
192 static void soc_init_card_debugfs(struct snd_soc_card *card)
193 {
194 	card->debugfs_card_root = debugfs_create_dir(card->name,
195 						     snd_soc_debugfs_root);
196 
197 	snd_soc_dapm_debugfs_init(snd_soc_card_to_dapm(card), card->debugfs_card_root);
198 }
199 
200 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
201 {
202 	debugfs_remove_recursive(card->debugfs_card_root);
203 	card->debugfs_card_root = NULL;
204 }
205 
206 static void snd_soc_debugfs_init(void)
207 {
208 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
209 
210 	debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
211 			    &dai_list_fops);
212 
213 	debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
214 			    &component_list_fops);
215 
216 	snd_soc_dapm_debugfs_pop_time(snd_soc_debugfs_root);
217 }
218 
219 static void snd_soc_debugfs_exit(void)
220 {
221 	debugfs_remove_recursive(snd_soc_debugfs_root);
222 }
223 
224 #else
225 
226 static inline void soc_init_component_debugfs(struct snd_soc_component *component) { }
227 static inline void soc_cleanup_component_debugfs(struct snd_soc_component *component) { }
228 static inline void soc_init_card_debugfs(struct snd_soc_card *card) { }
229 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) { }
230 static inline void snd_soc_debugfs_init(void) { }
231 static inline void snd_soc_debugfs_exit(void) { }
232 
233 #endif
234 
235 static int snd_soc_is_match_dai_args(const struct of_phandle_args *args1,
236 				     const struct of_phandle_args *args2)
237 {
238 	if (!args1 || !args2)
239 		return 0;
240 
241 	if (args1->np != args2->np)
242 		return 0;
243 
244 	for (int i = 0; i < args1->args_count; i++)
245 		if (args1->args[i] != args2->args[i])
246 			return 0;
247 
248 	return 1;
249 }
250 
251 static inline int snd_soc_dlc_component_is_empty(struct snd_soc_dai_link_component *dlc)
252 {
253 	return !(dlc->dai_args || dlc->name || dlc->of_node);
254 }
255 
256 static inline int snd_soc_dlc_component_is_invalid(struct snd_soc_dai_link_component *dlc)
257 {
258 	return (dlc->name && dlc->of_node);
259 }
260 
261 static inline int snd_soc_dlc_dai_is_empty(struct snd_soc_dai_link_component *dlc)
262 {
263 	return !(dlc->dai_args || dlc->dai_name);
264 }
265 
266 static int snd_soc_is_matching_dai(const struct snd_soc_dai_link_component *dlc,
267 				   struct snd_soc_dai *dai)
268 {
269 	if (!dlc)
270 		return 0;
271 
272 	if (dlc->dai_args)
273 		return snd_soc_is_match_dai_args(dai->driver->dai_args, dlc->dai_args);
274 
275 	if (!dlc->dai_name)
276 		return 1;
277 
278 	/* see snd_soc_dai_name_get() */
279 
280 	if (dai->driver->name &&
281 	    strcmp(dlc->dai_name, dai->driver->name) == 0)
282 		return 1;
283 
284 	if (strcmp(dlc->dai_name, dai->name) == 0)
285 		return 1;
286 
287 	if (dai->component->name &&
288 	    strcmp(dlc->dai_name, dai->component->name) == 0)
289 		return 1;
290 
291 	return 0;
292 }
293 
294 const char *snd_soc_dai_name_get(const struct snd_soc_dai *dai)
295 {
296 	/* see snd_soc_is_matching_dai() */
297 	if (dai->driver->name)
298 		return dai->driver->name;
299 
300 	if (dai->name)
301 		return dai->name;
302 
303 	if (dai->component->name)
304 		return dai->component->name;
305 
306 	return NULL;
307 }
308 EXPORT_SYMBOL_GPL(snd_soc_dai_name_get);
309 
310 static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
311 				     struct snd_soc_component *component)
312 {
313 	struct snd_soc_component *comp;
314 	int i;
315 
316 	for_each_rtd_components(rtd, i, comp) {
317 		/* already connected */
318 		if (comp == component)
319 			return 0;
320 	}
321 
322 	/* see for_each_rtd_components */
323 	rtd->num_components++; // increment flex array count at first
324 	rtd->components[rtd->num_components - 1] = component;
325 
326 	return 0;
327 }
328 
329 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
330 						const char *driver_name)
331 {
332 	struct snd_soc_component *component;
333 	int i;
334 
335 	if (!driver_name)
336 		return NULL;
337 
338 	/*
339 	 * NOTE
340 	 *
341 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
342 	 * specified driver name.
343 	 * But, if many components which have same driver name are connected
344 	 * to 1 rtd, this function will return 1st found component.
345 	 */
346 	for_each_rtd_components(rtd, i, component) {
347 		const char *component_name = component->driver->name;
348 
349 		if (!component_name)
350 			continue;
351 
352 		if ((component_name == driver_name) ||
353 		    strcmp(component_name, driver_name) == 0)
354 			return component;
355 	}
356 
357 	return NULL;
358 }
359 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
360 
361 struct snd_soc_component
362 *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
363 {
364 	struct snd_soc_component *component;
365 
366 	for_each_component(component) {
367 		if (dev != component->dev)
368 			continue;
369 
370 		if (!driver_name)
371 			return component;
372 
373 		if (!component->driver->name)
374 			continue;
375 
376 		if (component->driver->name == driver_name)
377 			return component;
378 
379 		if (strcmp(component->driver->name, driver_name) == 0)
380 			return component;
381 	}
382 
383 	return NULL;
384 }
385 EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked);
386 
387 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
388 						   const char *driver_name)
389 {
390 	guard(mutex)(&client_mutex);
391 
392 	return snd_soc_lookup_component_nolocked(dev, driver_name);
393 }
394 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
395 
396 struct snd_soc_component *snd_soc_lookup_component_by_name(const char *component_name)
397 {
398 	struct snd_soc_component *component;
399 
400 	guard(mutex)(&client_mutex);
401 	for_each_component(component)
402 		if (strstr(component->name, component_name))
403 			return component;
404 
405 	return NULL;
406 }
407 EXPORT_SYMBOL_GPL(snd_soc_lookup_component_by_name);
408 
409 struct snd_soc_pcm_runtime
410 *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
411 			 struct snd_soc_dai_link *dai_link)
412 {
413 	struct snd_soc_pcm_runtime *rtd;
414 
415 	for_each_card_rtds(card, rtd) {
416 		if (rtd->dai_link == dai_link)
417 			return rtd;
418 	}
419 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
420 	return NULL;
421 }
422 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
423 
424 /*
425  * Power down the audio subsystem pmdown_time msecs after close is called.
426  * This is to ensure there are no pops or clicks in between any music tracks
427  * due to DAPM power cycling.
428  */
429 void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
430 {
431 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
432 	int playback = SNDRV_PCM_STREAM_PLAYBACK;
433 
434 	snd_soc_dpcm_mutex_lock(rtd);
435 
436 	dev_dbg(rtd->dev,
437 		"ASoC: pop wq checking: %s status: %s waiting: %s\n",
438 		codec_dai->driver->playback.stream_name,
439 		snd_soc_dai_stream_active(codec_dai, playback) ?
440 		"active" : "inactive",
441 		str_yes_no(rtd->pop_wait));
442 
443 	/* are we waiting on this codec DAI stream */
444 	if (rtd->pop_wait == 1) {
445 		rtd->pop_wait = 0;
446 		snd_soc_dapm_stream_event(rtd, playback,
447 					  SND_SOC_DAPM_STREAM_STOP);
448 	}
449 
450 	snd_soc_dpcm_mutex_unlock(rtd);
451 }
452 EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
453 
454 static void soc_release_rtd_dev(struct device *dev)
455 {
456 	/* "dev" means "rtd->dev" */
457 	kfree(dev);
458 }
459 
460 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
461 {
462 	if (!rtd)
463 		return;
464 
465 	list_del(&rtd->list);
466 
467 	flush_delayed_work(&rtd->delayed_work);
468 	snd_soc_pcm_component_free(rtd);
469 
470 	/*
471 	 * we don't need to call kfree() for rtd->dev
472 	 * see
473 	 *	soc_release_rtd_dev()
474 	 *
475 	 * We don't need rtd->dev NULL check, because
476 	 * it is alloced *before* rtd.
477 	 * see
478 	 *	soc_new_pcm_runtime()
479 	 *
480 	 * We don't need to mind freeing for rtd,
481 	 * because it was created from dev (= rtd->dev)
482 	 * see
483 	 *	soc_new_pcm_runtime()
484 	 *
485 	 *		rtd = devm_kzalloc(dev, ...);
486 	 *		rtd->dev = dev
487 	 */
488 	device_unregister(rtd->dev);
489 }
490 
491 static void close_delayed_work(struct work_struct *work) {
492 	struct snd_soc_pcm_runtime *rtd =
493 			container_of(work, struct snd_soc_pcm_runtime,
494 				     delayed_work.work);
495 
496 	if (rtd->close_delayed_work_func)
497 		rtd->close_delayed_work_func(rtd);
498 }
499 
500 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
501 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
502 {
503 	struct snd_soc_pcm_runtime *rtd;
504 	struct device *dev;
505 	int ret;
506 	int stream;
507 
508 	/*
509 	 * for rtd->dev
510 	 */
511 	dev = kzalloc_obj(struct device);
512 	if (!dev)
513 		return NULL;
514 
515 	dev->parent	= card->dev;
516 	dev->release	= soc_release_rtd_dev;
517 
518 	dev_set_name(dev, "%s", dai_link->name);
519 
520 	ret = device_register(dev);
521 	if (ret < 0) {
522 		put_device(dev); /* soc_release_rtd_dev */
523 		return NULL;
524 	}
525 
526 	/*
527 	 * for rtd
528 	 */
529 	rtd = devm_kzalloc(dev,
530 			   struct_size(rtd, components,
531 				       dai_link->num_cpus +
532 				       dai_link->num_codecs +
533 				       dai_link->num_platforms),
534 			   GFP_KERNEL);
535 	if (!rtd) {
536 		device_unregister(dev);
537 		return NULL;
538 	}
539 
540 	rtd->dev = dev;
541 	INIT_LIST_HEAD(&rtd->list);
542 	for_each_pcm_streams(stream) {
543 		INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
544 		INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
545 	}
546 	dev_set_drvdata(dev, rtd);
547 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
548 
549 	if ((dai_link->num_cpus + dai_link->num_codecs) == 0) {
550 		dev_err(dev, "ASoC: it has no CPU or codec DAIs\n");
551 		goto free_rtd;
552 	}
553 
554 	/*
555 	 * for rtd->dais
556 	 */
557 	rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
558 					sizeof(struct snd_soc_dai *),
559 					GFP_KERNEL);
560 	if (!rtd->dais)
561 		goto free_rtd;
562 
563 	/*
564 	 * dais = [][][][][][][][][][][][][][][][][][]
565 	 *	  ^cpu_dais         ^codec_dais
566 	 *	  |--- num_cpus ---|--- num_codecs --|
567 	 * see
568 	 *	snd_soc_rtd_to_cpu()
569 	 *	snd_soc_rtd_to_codec()
570 	 */
571 	rtd->card	= card;
572 	rtd->dai_link	= dai_link;
573 	rtd->id		= card->num_rtd++;
574 	rtd->pmdown_time = pmdown_time;			/* default power off timeout */
575 
576 	/* see for_each_card_rtds */
577 	list_add_tail(&rtd->list, &card->rtd_list);
578 
579 	ret = device_add_groups(dev, soc_dev_attr_groups);
580 	if (ret < 0)
581 		goto free_rtd;
582 
583 	return rtd;
584 
585 free_rtd:
586 	soc_free_pcm_runtime(rtd);
587 	return NULL;
588 }
589 
590 static void snd_soc_fill_dummy_dai(struct snd_soc_card *card)
591 {
592 	struct snd_soc_dai_link *dai_link;
593 	int i;
594 
595 	/*
596 	 * COMP_DUMMY() creates size 0 array on dai_link.
597 	 * Fill it as dummy DAI in case of CPU/Codec here.
598 	 * Do nothing for Platform.
599 	 */
600 	for_each_card_prelinks(card, i, dai_link) {
601 		if (dai_link->num_cpus == 0 && dai_link->cpus) {
602 			dai_link->num_cpus	= 1;
603 			dai_link->cpus		= &snd_soc_dummy_dlc;
604 		}
605 		if (dai_link->num_codecs == 0 && dai_link->codecs) {
606 			dai_link->num_codecs	= 1;
607 			dai_link->codecs	= &snd_soc_dummy_dlc;
608 		}
609 	}
610 }
611 
612 static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
613 {
614 	struct snd_soc_pcm_runtime *rtd;
615 
616 	for_each_card_rtds(card, rtd)
617 		flush_delayed_work(&rtd->delayed_work);
618 }
619 
620 #ifdef CONFIG_PM_SLEEP
621 static void soc_playback_digital_mute(struct snd_soc_card *card, int mute)
622 {
623 	struct snd_soc_pcm_runtime *rtd;
624 	struct snd_soc_dai *dai;
625 	int playback = SNDRV_PCM_STREAM_PLAYBACK;
626 	int i;
627 
628 	for_each_card_rtds(card, rtd) {
629 
630 		if (rtd->dai_link->ignore_suspend)
631 			continue;
632 
633 		for_each_rtd_dais(rtd, i, dai) {
634 			if (snd_soc_dai_stream_active(dai, playback))
635 				snd_soc_dai_digital_mute(dai, mute, playback);
636 		}
637 	}
638 }
639 
640 static void soc_dapm_suspend_resume(struct snd_soc_card *card, int event)
641 {
642 	struct snd_soc_pcm_runtime *rtd;
643 	int stream;
644 
645 	for_each_card_rtds(card, rtd) {
646 
647 		if (rtd->dai_link->ignore_suspend)
648 			continue;
649 
650 		for_each_pcm_streams(stream)
651 			snd_soc_dapm_stream_event(rtd, stream, event);
652 	}
653 }
654 
655 /* powers down audio subsystem for suspend */
656 int snd_soc_suspend(struct device *dev)
657 {
658 	struct snd_soc_card *card = dev_get_drvdata(dev);
659 	struct snd_soc_component *component;
660 	struct snd_soc_pcm_runtime *rtd;
661 	int i;
662 
663 	/* If the card is not initialized yet there is nothing to do */
664 	if (!snd_soc_card_is_instantiated(card))
665 		return 0;
666 
667 	/*
668 	 * Due to the resume being scheduled into a workqueue we could
669 	 * suspend before that's finished - wait for it to complete.
670 	 */
671 	snd_power_wait(card->snd_card);
672 
673 	/* we're going to block userspace touching us until resume completes */
674 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
675 
676 	/* mute any active DACs */
677 	soc_playback_digital_mute(card, 1);
678 
679 	/* suspend all pcms */
680 	for_each_card_rtds(card, rtd) {
681 		if (rtd->dai_link->ignore_suspend)
682 			continue;
683 
684 		snd_pcm_suspend_all(rtd->pcm);
685 	}
686 
687 	snd_soc_card_suspend_pre(card);
688 
689 	/* close any waiting streams */
690 	snd_soc_flush_all_delayed_work(card);
691 
692 	soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_SUSPEND);
693 
694 	/* Recheck all endpoints too, their state is affected by suspend */
695 	snd_soc_dapm_mark_endpoints_dirty(card);
696 	snd_soc_dapm_sync(snd_soc_card_to_dapm(card));
697 
698 	/* suspend all COMPONENTs */
699 	for_each_card_rtds(card, rtd) {
700 
701 		if (rtd->dai_link->ignore_suspend)
702 			continue;
703 
704 		for_each_rtd_components(rtd, i, component) {
705 			struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
706 
707 			/*
708 			 * ignore if component was already suspended
709 			 */
710 			if (snd_soc_component_is_suspended(component))
711 				continue;
712 
713 			/*
714 			 * If there are paths active then the COMPONENT will be
715 			 * held with bias _ON and should not be suspended.
716 			 */
717 			switch (snd_soc_dapm_get_bias_level(dapm)) {
718 			case SND_SOC_BIAS_STANDBY:
719 				/*
720 				 * If the COMPONENT is capable of idle
721 				 * bias off then being in STANDBY
722 				 * means it's doing something,
723 				 * otherwise fall through.
724 				 */
725 				if (!snd_soc_dapm_get_idle_bias(dapm)) {
726 					dev_dbg(component->dev,
727 						"ASoC: idle_bias_off CODEC on over suspend\n");
728 					break;
729 				}
730 				fallthrough;
731 
732 			case SND_SOC_BIAS_OFF:
733 				snd_soc_component_suspend(component);
734 				if (component->regmap)
735 					regcache_mark_dirty(component->regmap);
736 				/* deactivate pins to sleep state */
737 				pinctrl_pm_select_sleep_state(component->dev);
738 				break;
739 			default:
740 				dev_dbg(component->dev,
741 					"ASoC: COMPONENT is on over suspend\n");
742 				break;
743 			}
744 		}
745 	}
746 
747 	snd_soc_card_suspend_post(card);
748 
749 	return 0;
750 }
751 EXPORT_SYMBOL_GPL(snd_soc_suspend);
752 
753 /*
754  * deferred resume work, so resume can complete before we finished
755  * setting our codec back up, which can be very slow on I2C
756  */
757 static void soc_resume_deferred(struct work_struct *work)
758 {
759 	struct snd_soc_card *card =
760 			container_of(work, struct snd_soc_card,
761 				     deferred_resume_work);
762 	struct snd_soc_component *component;
763 
764 	/*
765 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
766 	 * so userspace apps are blocked from touching us
767 	 */
768 
769 	dev_dbg(card->dev, "ASoC: starting resume work\n");
770 
771 	/* Bring us up into D2 so that DAPM starts enabling things */
772 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
773 
774 	snd_soc_card_resume_pre(card);
775 
776 	for_each_card_components(card, component) {
777 		if (snd_soc_component_is_suspended(component))
778 			snd_soc_component_resume(component);
779 	}
780 
781 	soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_RESUME);
782 
783 	/* unmute any active DACs */
784 	soc_playback_digital_mute(card, 0);
785 
786 	snd_soc_card_resume_post(card);
787 
788 	dev_dbg(card->dev, "ASoC: resume work completed\n");
789 
790 	/* Recheck all endpoints too, their state is affected by suspend */
791 	snd_soc_dapm_mark_endpoints_dirty(card);
792 	snd_soc_dapm_sync(snd_soc_card_to_dapm(card));
793 
794 	/* userspace can access us now we are back as we were before */
795 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
796 }
797 
798 /* powers up audio subsystem after a suspend */
799 int snd_soc_resume(struct device *dev)
800 {
801 	struct snd_soc_card *card = dev_get_drvdata(dev);
802 	struct snd_soc_component *component;
803 
804 	/* If the card is not initialized yet there is nothing to do */
805 	if (!snd_soc_card_is_instantiated(card))
806 		return 0;
807 
808 	/* activate pins from sleep state */
809 	for_each_card_components(card, component)
810 		if (snd_soc_component_active(component))
811 			pinctrl_pm_select_default_state(component->dev);
812 
813 	dev_dbg(dev, "ASoC: Scheduling resume work\n");
814 	if (!schedule_work(&card->deferred_resume_work))
815 		dev_err(dev, "ASoC: resume work item may be lost\n");
816 
817 	return 0;
818 }
819 EXPORT_SYMBOL_GPL(snd_soc_resume);
820 
821 static void soc_resume_init(struct snd_soc_card *card)
822 {
823 	/* deferred resume work */
824 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
825 }
826 #else
827 #define snd_soc_suspend NULL
828 #define snd_soc_resume NULL
829 static inline void soc_resume_init(struct snd_soc_card *card) { }
830 #endif
831 
832 static struct device_node
833 *soc_component_to_node(struct snd_soc_component *component)
834 {
835 	struct device_node *of_node;
836 
837 	of_node = component->dev->of_node;
838 	if (!of_node && component->dev->parent)
839 		of_node = component->dev->parent->of_node;
840 
841 	return of_node;
842 }
843 
844 struct of_phandle_args *snd_soc_copy_dai_args(struct device *dev,
845 					      const struct of_phandle_args *args)
846 {
847 	struct of_phandle_args *ret = devm_kzalloc(dev, sizeof(*ret), GFP_KERNEL);
848 
849 	if (!ret)
850 		return NULL;
851 
852 	*ret = *args;
853 
854 	return ret;
855 }
856 EXPORT_SYMBOL_GPL(snd_soc_copy_dai_args);
857 
858 static int snd_soc_is_matching_component(
859 	const struct snd_soc_dai_link_component *dlc,
860 	struct snd_soc_component *component)
861 {
862 	struct device_node *component_of_node;
863 
864 	if (!dlc)
865 		return 0;
866 
867 	if (dlc->dai_args) {
868 		struct snd_soc_dai *dai;
869 
870 		for_each_component_dais(component, dai)
871 			if (snd_soc_is_matching_dai(dlc, dai))
872 				return 1;
873 		return 0;
874 	}
875 
876 	component_of_node = soc_component_to_node(component);
877 
878 	if (dlc->of_node && component_of_node != dlc->of_node)
879 		return 0;
880 	if (dlc->name && strcmp(component->name, dlc->name))
881 		return 0;
882 
883 	return 1;
884 }
885 
886 static struct snd_soc_component *soc_find_component(
887 	const struct snd_soc_dai_link_component *dlc)
888 {
889 	struct snd_soc_component *component;
890 
891 	lockdep_assert_held(&client_mutex);
892 
893 	/*
894 	 * NOTE
895 	 *
896 	 * It returns *1st* found component, but some driver
897 	 * has few components by same of_node/name
898 	 * ex)
899 	 *	CPU component and generic DMAEngine component
900 	 */
901 	for_each_component(component)
902 		if (snd_soc_is_matching_component(dlc, component))
903 			return component;
904 
905 	return NULL;
906 }
907 
908 /**
909  * snd_soc_find_dai - Find a registered DAI
910  *
911  * @dlc: name of the DAI or the DAI driver and optional component info to match
912  *
913  * This function will search all registered components and their DAIs to
914  * find the DAI of the same name. The component's of_node and name
915  * should also match if being specified.
916  *
917  * Return: pointer of DAI, or NULL if not found.
918  */
919 struct snd_soc_dai *snd_soc_find_dai(
920 	const struct snd_soc_dai_link_component *dlc)
921 {
922 	struct snd_soc_component *component;
923 	struct snd_soc_dai *dai;
924 
925 	lockdep_assert_held(&client_mutex);
926 
927 	/* Find CPU DAI from registered DAIs */
928 	for_each_component(component)
929 		if (snd_soc_is_matching_component(dlc, component))
930 			for_each_component_dais(component, dai)
931 				if (snd_soc_is_matching_dai(dlc, dai))
932 					return dai;
933 
934 	return NULL;
935 }
936 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
937 
938 struct snd_soc_dai *snd_soc_find_dai_with_mutex(
939 	const struct snd_soc_dai_link_component *dlc)
940 {
941 	guard(mutex)(&client_mutex);
942 
943 	return snd_soc_find_dai(dlc);
944 }
945 EXPORT_SYMBOL_GPL(snd_soc_find_dai_with_mutex);
946 
947 static int soc_dai_link_sanity_check(struct snd_soc_card *card,
948 				     struct snd_soc_dai_link *link)
949 {
950 	int i;
951 	struct snd_soc_dai_link_component *dlc;
952 
953 	/* Codec check */
954 	for_each_link_codecs(link, i, dlc) {
955 		/*
956 		 * Codec must be specified by 1 of name or OF node,
957 		 * not both or neither.
958 		 */
959 		if (snd_soc_dlc_component_is_invalid(dlc))
960 			goto component_invalid;
961 
962 		if (snd_soc_dlc_component_is_empty(dlc))
963 			goto component_empty;
964 
965 		/* Codec DAI name must be specified */
966 		if (snd_soc_dlc_dai_is_empty(dlc))
967 			goto dai_empty;
968 
969 		/*
970 		 * Defer card registration if codec component is not added to
971 		 * component list.
972 		 */
973 		if (!soc_find_component(dlc))
974 			goto component_not_found;
975 	}
976 
977 	/* Platform check */
978 	for_each_link_platforms(link, i, dlc) {
979 		/*
980 		 * Platform may be specified by either name or OF node, but it
981 		 * can be left unspecified, then no components will be inserted
982 		 * in the rtdcom list
983 		 */
984 		if (snd_soc_dlc_component_is_invalid(dlc))
985 			goto component_invalid;
986 
987 		if (snd_soc_dlc_component_is_empty(dlc))
988 			goto component_empty;
989 
990 		/*
991 		 * Defer card registration if platform component is not added to
992 		 * component list.
993 		 */
994 		if (!soc_find_component(dlc))
995 			goto component_not_found;
996 	}
997 
998 	/* CPU check */
999 	for_each_link_cpus(link, i, dlc) {
1000 		/*
1001 		 * CPU device may be specified by either name or OF node, but
1002 		 * can be left unspecified, and will be matched based on DAI
1003 		 * name alone..
1004 		 */
1005 		if (snd_soc_dlc_component_is_invalid(dlc))
1006 			goto component_invalid;
1007 
1008 
1009 		if (snd_soc_dlc_component_is_empty(dlc)) {
1010 			/*
1011 			 * At least one of CPU DAI name or CPU device name/node must be specified
1012 			 */
1013 			if (snd_soc_dlc_dai_is_empty(dlc))
1014 				goto component_dai_empty;
1015 		} else {
1016 			/*
1017 			 * Defer card registration if Component is not added
1018 			 */
1019 			if (!soc_find_component(dlc))
1020 				goto component_not_found;
1021 		}
1022 	}
1023 
1024 	return 0;
1025 
1026 component_invalid:
1027 	dev_err(card->dev, "ASoC: Both Component name/of_node are set for %s\n", link->name);
1028 	return -EINVAL;
1029 
1030 component_empty:
1031 	dev_err(card->dev, "ASoC: Neither Component name/of_node are set for %s\n", link->name);
1032 	return -EINVAL;
1033 
1034 component_not_found:
1035 	dev_dbg(card->dev, "ASoC: Component %s not found for link %s\n", dlc->name, link->name);
1036 	return -EPROBE_DEFER;
1037 
1038 dai_empty:
1039 	dev_err(card->dev, "ASoC: DAI name is not set for %s\n", link->name);
1040 	return -EINVAL;
1041 
1042 component_dai_empty:
1043 	dev_err(card->dev, "ASoC: Neither DAI/Component name/of_node are set for %s\n", link->name);
1044 	return -EINVAL;
1045 }
1046 
1047 #define MAX_DEFAULT_CH_MAP_SIZE 8
1048 static struct snd_soc_dai_link_ch_map default_ch_map_sync[MAX_DEFAULT_CH_MAP_SIZE] = {
1049 	{ .cpu = 0, .codec = 0 },
1050 	{ .cpu = 1, .codec = 1 },
1051 	{ .cpu = 2, .codec = 2 },
1052 	{ .cpu = 3, .codec = 3 },
1053 	{ .cpu = 4, .codec = 4 },
1054 	{ .cpu = 5, .codec = 5 },
1055 	{ .cpu = 6, .codec = 6 },
1056 	{ .cpu = 7, .codec = 7 },
1057 };
1058 static struct snd_soc_dai_link_ch_map default_ch_map_1cpu[MAX_DEFAULT_CH_MAP_SIZE] = {
1059 	{ .cpu = 0, .codec = 0 },
1060 	{ .cpu = 0, .codec = 1 },
1061 	{ .cpu = 0, .codec = 2 },
1062 	{ .cpu = 0, .codec = 3 },
1063 	{ .cpu = 0, .codec = 4 },
1064 	{ .cpu = 0, .codec = 5 },
1065 	{ .cpu = 0, .codec = 6 },
1066 	{ .cpu = 0, .codec = 7 },
1067 };
1068 static struct snd_soc_dai_link_ch_map default_ch_map_1codec[MAX_DEFAULT_CH_MAP_SIZE] = {
1069 	{ .cpu = 0, .codec = 0 },
1070 	{ .cpu = 1, .codec = 0 },
1071 	{ .cpu = 2, .codec = 0 },
1072 	{ .cpu = 3, .codec = 0 },
1073 	{ .cpu = 4, .codec = 0 },
1074 	{ .cpu = 5, .codec = 0 },
1075 	{ .cpu = 6, .codec = 0 },
1076 	{ .cpu = 7, .codec = 0 },
1077 };
1078 static int snd_soc_compensate_channel_connection_map(struct snd_soc_card *card,
1079 						     struct snd_soc_dai_link *dai_link)
1080 {
1081 	struct snd_soc_dai_link_ch_map *ch_maps;
1082 	int i;
1083 
1084 	/*
1085 	 * dai_link->ch_maps indicates how CPU/Codec are connected.
1086 	 * It will be a map seen from a larger number of DAI.
1087 	 * see
1088 	 *	soc.h :: [dai_link->ch_maps Image sample]
1089 	 */
1090 
1091 	/* it should have ch_maps if connection was N:M */
1092 	if (dai_link->num_cpus > 1 && dai_link->num_codecs > 1 &&
1093 	    dai_link->num_cpus != dai_link->num_codecs && !dai_link->ch_maps) {
1094 		dev_err(card->dev, "need to have ch_maps when N:M connection (%s)",
1095 			dai_link->name);
1096 		return -EINVAL;
1097 	}
1098 
1099 	/* do nothing if it has own maps */
1100 	if (dai_link->ch_maps)
1101 		goto sanity_check;
1102 
1103 	/* check default map size */
1104 	if (dai_link->num_cpus   > MAX_DEFAULT_CH_MAP_SIZE ||
1105 	    dai_link->num_codecs > MAX_DEFAULT_CH_MAP_SIZE) {
1106 		dev_err(card->dev, "soc-core.c needs update default_connection_maps");
1107 		return -EINVAL;
1108 	}
1109 
1110 	/* Compensate missing map for ... */
1111 	if (dai_link->num_cpus == dai_link->num_codecs)
1112 		dai_link->ch_maps = default_ch_map_sync;	/* for 1:1 or N:N */
1113 	else if (dai_link->num_cpus <  dai_link->num_codecs)
1114 		dai_link->ch_maps = default_ch_map_1cpu;	/* for 1:N */
1115 	else
1116 		dai_link->ch_maps = default_ch_map_1codec;	/* for N:1 */
1117 
1118 sanity_check:
1119 	dev_dbg(card->dev, "dai_link %s\n", dai_link->stream_name);
1120 	for_each_link_ch_maps(dai_link, i, ch_maps) {
1121 		if ((ch_maps->cpu   >= dai_link->num_cpus) ||
1122 		    (ch_maps->codec >= dai_link->num_codecs)) {
1123 			dev_err(card->dev,
1124 				"unexpected dai_link->ch_maps[%d] index (cpu(%d/%d) codec(%d/%d))",
1125 				i,
1126 				ch_maps->cpu,	dai_link->num_cpus,
1127 				ch_maps->codec,	dai_link->num_codecs);
1128 			return -EINVAL;
1129 		}
1130 
1131 		dev_dbg(card->dev, "  [%d] cpu%d <-> codec%d\n",
1132 			i, ch_maps->cpu, ch_maps->codec);
1133 	}
1134 
1135 	return 0;
1136 }
1137 
1138 /**
1139  * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
1140  * @card: The ASoC card to which the pcm_runtime has
1141  * @rtd: The pcm_runtime to remove
1142  *
1143  * This function removes a pcm_runtime from the ASoC card.
1144  */
1145 void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
1146 				struct snd_soc_pcm_runtime *rtd)
1147 {
1148 	if (!rtd)
1149 		return;
1150 
1151 	lockdep_assert_held(&client_mutex);
1152 
1153 	/*
1154 	 * Notify the machine driver for extra destruction
1155 	 */
1156 	snd_soc_card_remove_dai_link(card, rtd->dai_link);
1157 
1158 	soc_free_pcm_runtime(rtd);
1159 }
1160 EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
1161 
1162 /**
1163  * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
1164  * @card: The ASoC card to which the pcm_runtime is added
1165  * @dai_link: The DAI link to find pcm_runtime
1166  *
1167  * This function adds a pcm_runtime ASoC card by using dai_link.
1168  *
1169  * Note: Topology can use this API to add pcm_runtime when probing the
1170  * topology component. And machine drivers can still define static
1171  * DAI links in dai_link array.
1172  */
1173 static int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
1174 				   struct snd_soc_dai_link *dai_link)
1175 {
1176 	struct snd_soc_pcm_runtime *rtd;
1177 	struct snd_soc_dai_link_component *codec, *platform, *cpu;
1178 	struct snd_soc_component *component;
1179 	int i, id, ret;
1180 
1181 	lockdep_assert_held(&client_mutex);
1182 
1183 	/*
1184 	 * Notify the machine driver for extra initialization
1185 	 */
1186 	ret = snd_soc_card_add_dai_link(card, dai_link);
1187 	if (ret < 0)
1188 		return ret;
1189 
1190 	if (dai_link->ignore)
1191 		return 0;
1192 
1193 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1194 
1195 	ret = soc_dai_link_sanity_check(card, dai_link);
1196 	if (ret < 0)
1197 		return ret;
1198 
1199 	rtd = soc_new_pcm_runtime(card, dai_link);
1200 	if (!rtd)
1201 		return -ENOMEM;
1202 
1203 	for_each_link_cpus(dai_link, i, cpu) {
1204 		snd_soc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
1205 		if (!snd_soc_rtd_to_cpu(rtd, i)) {
1206 			dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1207 				 cpu->dai_name);
1208 			goto _err_defer;
1209 		}
1210 		snd_soc_rtd_add_component(rtd, snd_soc_rtd_to_cpu(rtd, i)->component);
1211 	}
1212 
1213 	/* Find CODEC from registered CODECs */
1214 	for_each_link_codecs(dai_link, i, codec) {
1215 		snd_soc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1216 		if (!snd_soc_rtd_to_codec(rtd, i)) {
1217 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
1218 				 codec->dai_name);
1219 			goto _err_defer;
1220 		}
1221 
1222 		snd_soc_rtd_add_component(rtd, snd_soc_rtd_to_codec(rtd, i)->component);
1223 	}
1224 
1225 	/* Find PLATFORM from registered PLATFORMs */
1226 	for_each_link_platforms(dai_link, i, platform) {
1227 		for_each_component(component) {
1228 			if (!snd_soc_is_matching_component(platform, component))
1229 				continue;
1230 
1231 			if (snd_soc_component_is_dummy(component) && component->num_dai)
1232 				continue;
1233 
1234 			snd_soc_rtd_add_component(rtd, component);
1235 		}
1236 	}
1237 
1238 	/*
1239 	 * Most drivers will register their PCMs using DAI link ordering but
1240 	 * topology based drivers can use the DAI link id field to set PCM
1241 	 * device number and then use rtd + a base offset of the BEs.
1242 	 *
1243 	 * FIXME
1244 	 *
1245 	 * This should be implemented by using "dai_link" feature instead of
1246 	 * "component" feature.
1247 	 */
1248 	id = rtd->id;
1249 	for_each_rtd_components(rtd, i, component) {
1250 		if (!component->driver->use_dai_pcm_id)
1251 			continue;
1252 
1253 		if (rtd->dai_link->no_pcm)
1254 			id += component->driver->be_pcm_base;
1255 		else
1256 			id = rtd->dai_link->id;
1257 	}
1258 	rtd->id = id;
1259 
1260 	return 0;
1261 
1262 _err_defer:
1263 	snd_soc_remove_pcm_runtime(card, rtd);
1264 	return -EPROBE_DEFER;
1265 }
1266 
1267 int snd_soc_add_pcm_runtimes(struct snd_soc_card *card,
1268 			     struct snd_soc_dai_link *dai_link,
1269 			     int num_dai_link)
1270 {
1271 	for (int i = 0; i < num_dai_link; i++) {
1272 		int ret;
1273 
1274 		ret = snd_soc_compensate_channel_connection_map(card, dai_link + i);
1275 		if (ret < 0)
1276 			return ret;
1277 
1278 		ret = snd_soc_add_pcm_runtime(card, dai_link + i);
1279 		if (ret < 0)
1280 			return ret;
1281 	}
1282 
1283 	return 0;
1284 }
1285 EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtimes);
1286 
1287 /**
1288  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1289  * @rtd: The runtime for which the DAI link format should be changed
1290  * @dai_fmt: The new DAI link format
1291  *
1292  * This function updates the DAI link format for all DAIs connected to the DAI
1293  * link for the specified runtime.
1294  *
1295  * Note: For setups with a static format set the dai_fmt field in the
1296  * corresponding snd_dai_link struct instead of using this function.
1297  *
1298  * Returns 0 on success, otherwise a negative error code.
1299  */
1300 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1301 				unsigned int dai_fmt)
1302 {
1303 	struct snd_soc_dai *cpu_dai;
1304 	struct snd_soc_dai *codec_dai;
1305 	unsigned int ext_fmt;
1306 	unsigned int i;
1307 	int ret;
1308 
1309 	if (!dai_fmt)
1310 		return 0;
1311 
1312 	/*
1313 	 * dai_fmt has 4 types
1314 	 *	1. SND_SOC_DAIFMT_FORMAT_MASK
1315 	 *	2. SND_SOC_DAIFMT_CLOCK
1316 	 *	3. SND_SOC_DAIFMT_INV
1317 	 *	4. SND_SOC_DAIFMT_CLOCK_PROVIDER
1318 	 *
1319 	 * 4. CLOCK_PROVIDER is set from Codec perspective in dai_fmt. So it will be flipped
1320 	 * when this function calls set_fmt() for CPU (CBx_CFx -> Bx_Cx). see below.
1321 	 * This mean, we can't set CPU/Codec both are clock consumer for example.
1322 	 * New idea handles 4. in each dai->ext_fmt. It can keep compatibility.
1323 	 *
1324 	 * Legacy
1325 	 *	dai_fmt  includes 1, 2, 3, 4
1326 	 *
1327 	 * New idea
1328 	 *	dai_fmt  includes 1, 2, 3
1329 	 *	ext_fmt  includes 4
1330 	 */
1331 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
1332 		ext_fmt = rtd->dai_link->codecs[i].ext_fmt;
1333 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt | ext_fmt);
1334 		if (ret != 0 && ret != -ENOTSUPP)
1335 			return ret;
1336 	}
1337 
1338 	/* Flip the polarity for the "CPU" end of link */
1339 	/* Will effect only for 4. SND_SOC_DAIFMT_CLOCK_PROVIDER */
1340 	dai_fmt = snd_soc_daifmt_clock_provider_flipped(dai_fmt);
1341 
1342 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1343 		ext_fmt = rtd->dai_link->cpus[i].ext_fmt;
1344 		ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt | ext_fmt);
1345 		if (ret != 0 && ret != -ENOTSUPP)
1346 			return ret;
1347 	}
1348 
1349 	return 0;
1350 }
1351 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1352 
1353 static int soc_init_pcm_runtime(struct snd_soc_card *card,
1354 				struct snd_soc_pcm_runtime *rtd)
1355 {
1356 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1357 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
1358 	int ret;
1359 
1360 	/* do machine specific initialization */
1361 	ret = snd_soc_link_init(rtd);
1362 	if (ret < 0)
1363 		return ret;
1364 
1365 	ret = snd_soc_runtime_set_dai_fmt(rtd, snd_soc_dai_auto_select_format(rtd));
1366 	if (ret)
1367 		goto err;
1368 
1369 	/* add DPCM sysfs entries */
1370 	soc_dpcm_debugfs_add(rtd);
1371 
1372 	/* create compress_device if possible */
1373 	ret = snd_soc_dai_compress_new(cpu_dai, rtd);
1374 	if (ret != -ENOTSUPP)
1375 		goto err;
1376 
1377 	/* create the pcm */
1378 	ret = soc_new_pcm(rtd);
1379 	if (ret < 0) {
1380 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1381 			dai_link->stream_name, ret);
1382 		goto err;
1383 	}
1384 
1385 	ret = snd_soc_pcm_dai_new(rtd);
1386 	if (ret < 0)
1387 		goto err;
1388 
1389 	rtd->initialized = true;
1390 
1391 	return 0;
1392 err:
1393 	snd_soc_link_exit(rtd);
1394 	return ret;
1395 }
1396 
1397 static void soc_set_name_prefix(struct snd_soc_card *card,
1398 				struct snd_soc_component *component)
1399 {
1400 	struct device_node *of_node = soc_component_to_node(component);
1401 	const char *str;
1402 	int ret, i;
1403 
1404 	for (i = 0; i < card->num_configs; i++) {
1405 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1406 
1407 		if (snd_soc_is_matching_component(&map->dlc, component) &&
1408 		    map->name_prefix) {
1409 			component->name_prefix = map->name_prefix;
1410 			return;
1411 		}
1412 	}
1413 
1414 	/*
1415 	 * If there is no configuration table or no match in the table,
1416 	 * check if a prefix is provided in the node
1417 	 */
1418 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1419 	if (ret < 0)
1420 		return;
1421 
1422 	component->name_prefix = str;
1423 }
1424 
1425 static void soc_remove_component(struct snd_soc_component *component,
1426 				 int probed)
1427 {
1428 
1429 	if (!component->card)
1430 		return;
1431 
1432 	if (probed)
1433 		snd_soc_component_remove(component);
1434 
1435 	list_del_init(&component->card_list);
1436 	snd_soc_dapm_free(snd_soc_component_to_dapm(component));
1437 	soc_cleanup_component_debugfs(component);
1438 	component->card = NULL;
1439 	snd_soc_component_module_put_when_remove(component);
1440 }
1441 
1442 static int soc_probe_component(struct snd_soc_card *card,
1443 			       struct snd_soc_component *component)
1444 {
1445 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1446 	struct snd_soc_dai *dai;
1447 	int probed = 0;
1448 	int ret;
1449 
1450 	if (snd_soc_component_is_dummy(component))
1451 		return 0;
1452 
1453 	if (component->card) {
1454 		if (component->card != card) {
1455 			dev_err(component->dev,
1456 				"Trying to bind component \"%s\" to card \"%s\" but is already bound to card \"%s\"\n",
1457 				component->name, card->name, component->card->name);
1458 			return -ENODEV;
1459 		}
1460 		return 0;
1461 	}
1462 
1463 	ret = snd_soc_component_module_get_when_probe(component);
1464 	if (ret < 0)
1465 		return ret;
1466 
1467 	component->card = card;
1468 	soc_set_name_prefix(card, component);
1469 
1470 	soc_init_component_debugfs(component);
1471 
1472 	snd_soc_dapm_init(dapm, card, component);
1473 
1474 	ret = snd_soc_dapm_new_controls(dapm,
1475 					component->driver->dapm_widgets,
1476 					component->driver->num_dapm_widgets);
1477 
1478 	if (ret != 0) {
1479 		dev_err(component->dev,
1480 			"Failed to create new controls %d\n", ret);
1481 		goto err_probe;
1482 	}
1483 
1484 	for_each_component_dais(component, dai) {
1485 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1486 		if (ret != 0) {
1487 			dev_err(component->dev,
1488 				"Failed to create DAI widgets %d\n", ret);
1489 			goto err_probe;
1490 		}
1491 	}
1492 
1493 	ret = snd_soc_component_probe(component);
1494 	if (ret < 0)
1495 		goto err_probe;
1496 
1497 	WARN(!snd_soc_dapm_get_idle_bias(dapm) &&
1498 	     snd_soc_dapm_get_bias_level(dapm) != SND_SOC_BIAS_OFF,
1499 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1500 	     component->name);
1501 	probed = 1;
1502 
1503 	/*
1504 	 * machine specific init
1505 	 * see
1506 	 *	snd_soc_component_set_aux()
1507 	 */
1508 	ret = snd_soc_component_init(component);
1509 	if (ret < 0)
1510 		goto err_probe;
1511 
1512 	ret = snd_soc_add_component_controls(component,
1513 					     component->driver->controls,
1514 					     component->driver->num_controls);
1515 	if (ret < 0)
1516 		goto err_probe;
1517 
1518 	ret = snd_soc_dapm_add_routes(dapm,
1519 				      component->driver->dapm_routes,
1520 				      component->driver->num_dapm_routes);
1521 	if (ret < 0)
1522 		goto err_probe;
1523 
1524 	/* see for_each_card_components */
1525 	list_add(&component->card_list, &card->component_dev_list);
1526 
1527 err_probe:
1528 	if (ret < 0)
1529 		soc_remove_component(component, probed);
1530 
1531 	return ret;
1532 }
1533 
1534 static void soc_remove_link_dais(struct snd_soc_card *card)
1535 {
1536 	struct snd_soc_pcm_runtime *rtd;
1537 	int order;
1538 
1539 	for_each_comp_order(order) {
1540 		for_each_card_rtds(card, rtd) {
1541 			/* remove all rtd connected DAIs in good order */
1542 			snd_soc_pcm_dai_remove(rtd, order);
1543 		}
1544 	}
1545 }
1546 
1547 static int soc_probe_link_dais(struct snd_soc_card *card)
1548 {
1549 	struct snd_soc_pcm_runtime *rtd;
1550 	int order, ret;
1551 
1552 	for_each_comp_order(order) {
1553 		for_each_card_rtds(card, rtd) {
1554 			/* probe all rtd connected DAIs in good order */
1555 			ret = snd_soc_pcm_dai_probe(rtd, order);
1556 			if (ret)
1557 				return ret;
1558 		}
1559 	}
1560 
1561 	return 0;
1562 }
1563 
1564 static void soc_remove_link_components(struct snd_soc_card *card)
1565 {
1566 	struct snd_soc_component *component;
1567 	struct snd_soc_pcm_runtime *rtd;
1568 	int i, order;
1569 
1570 	for_each_comp_order(order) {
1571 		for_each_card_rtds(card, rtd) {
1572 			for_each_rtd_components(rtd, i, component) {
1573 				if (component->driver->remove_order != order)
1574 					continue;
1575 
1576 				soc_remove_component(component, 1);
1577 			}
1578 		}
1579 	}
1580 }
1581 
1582 static int soc_probe_link_components(struct snd_soc_card *card)
1583 {
1584 	struct snd_soc_component *component;
1585 	struct snd_soc_pcm_runtime *rtd;
1586 	int i, ret, order;
1587 
1588 	for_each_comp_order(order) {
1589 		for_each_card_rtds(card, rtd) {
1590 			for_each_rtd_components(rtd, i, component) {
1591 				if (component->driver->probe_order != order)
1592 					continue;
1593 
1594 				ret = soc_probe_component(card, component);
1595 				if (ret < 0)
1596 					return ret;
1597 			}
1598 		}
1599 	}
1600 
1601 	return 0;
1602 }
1603 
1604 static void soc_unbind_aux_dev(struct snd_soc_card *card)
1605 {
1606 	struct snd_soc_component *component, *_component;
1607 
1608 	for_each_card_auxs_safe(card, component, _component) {
1609 		/* for snd_soc_component_init() */
1610 		snd_soc_component_set_aux(component, NULL);
1611 		list_del(&component->card_aux_list);
1612 	}
1613 }
1614 
1615 static int soc_bind_aux_dev(struct snd_soc_card *card)
1616 {
1617 	struct snd_soc_component *component;
1618 	struct snd_soc_aux_dev *aux;
1619 	int i;
1620 
1621 	for_each_card_pre_auxs(card, i, aux) {
1622 		/* codecs, usually analog devices */
1623 		component = soc_find_component(&aux->dlc);
1624 		if (!component)
1625 			return -EPROBE_DEFER;
1626 
1627 		/* for snd_soc_component_init() */
1628 		snd_soc_component_set_aux(component, aux);
1629 		/* see for_each_card_auxs */
1630 		list_add(&component->card_aux_list, &card->aux_comp_list);
1631 	}
1632 	return 0;
1633 }
1634 
1635 static int soc_probe_aux_devices(struct snd_soc_card *card)
1636 {
1637 	struct snd_soc_component *component;
1638 	int order;
1639 	int ret;
1640 
1641 	for_each_comp_order(order) {
1642 		for_each_card_auxs(card, component) {
1643 			if (component->driver->probe_order != order)
1644 				continue;
1645 
1646 			ret = soc_probe_component(card,	component);
1647 			if (ret < 0)
1648 				return ret;
1649 		}
1650 	}
1651 
1652 	return 0;
1653 }
1654 
1655 static void soc_remove_aux_devices(struct snd_soc_card *card)
1656 {
1657 	struct snd_soc_component *comp, *_comp;
1658 	int order;
1659 
1660 	for_each_comp_order(order) {
1661 		for_each_card_auxs_safe(card, comp, _comp) {
1662 			if (comp->driver->remove_order == order)
1663 				soc_remove_component(comp, 1);
1664 		}
1665 	}
1666 }
1667 
1668 #ifdef CONFIG_DMI
1669 /*
1670  * If a DMI filed contain strings in this blacklist (e.g.
1671  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1672  * as invalid and dropped when setting the card long name from DMI info.
1673  */
1674 static const char * const dmi_blacklist[] = {
1675 	"To be filled by OEM",
1676 	"TBD by OEM",
1677 	"Default String",
1678 	"Board Manufacturer",
1679 	"Board Vendor Name",
1680 	"Board Product Name",
1681 	NULL,	/* terminator */
1682 };
1683 
1684 /*
1685  * Trim special characters, and replace '-' with '_' since '-' is used to
1686  * separate different DMI fields in the card long name. Only number and
1687  * alphabet characters and a few separator characters are kept.
1688  */
1689 static void cleanup_dmi_name(char *name)
1690 {
1691 	int i, j = 0;
1692 
1693 	for (i = 0; name[i]; i++) {
1694 		if (isalnum(name[i]) || (name[i] == '.')
1695 		    || (name[i] == '_'))
1696 			name[j++] = name[i];
1697 		else if (name[i] == '-')
1698 			name[j++] = '_';
1699 	}
1700 
1701 	name[j] = '\0';
1702 }
1703 
1704 /*
1705  * Check if a DMI field is valid, i.e. not containing any string
1706  * in the black list and not the empty string.
1707  */
1708 static int is_dmi_valid(const char *field)
1709 {
1710 	int i = 0;
1711 
1712 	if (!field[0])
1713 		return 0;
1714 
1715 	while (dmi_blacklist[i]) {
1716 		if (strstr(field, dmi_blacklist[i]))
1717 			return 0;
1718 		i++;
1719 	}
1720 
1721 	return 1;
1722 }
1723 
1724 /*
1725  * Append a string to dmi_longname with character cleanups.
1726  */
1727 #define DMI_LONGNAME_LEN	80
1728 static void append_dmi_string(char *dst, const char *str)
1729 {
1730 	size_t dst_len = DMI_LONGNAME_LEN;
1731 	size_t len;
1732 
1733 	len = strlen(dst);
1734 	snprintf(dst + len, dst_len - len, "-%s", str);
1735 
1736 	len++;	/* skip the separator "-" */
1737 	if (len < dst_len)
1738 		cleanup_dmi_name(dst + len);
1739 }
1740 
1741 /**
1742  * snd_soc_set_dmi_name() - Register DMI names to card
1743  * @card: The card to register DMI names
1744  *
1745  * An Intel machine driver may be used by many different devices but are
1746  * difficult for userspace to differentiate, since machine drivers usually
1747  * use their own name as the card short name and leave the card long name
1748  * blank. To differentiate such devices and fix bugs due to lack of
1749  * device-specific configurations, this function allows DMI info to be used
1750  * as the sound card long name, in the format of
1751  * "vendor-product-version-board"
1752  * (Character '-' is used to separate different DMI fields here).
1753  * This will help the user space to load the device-specific Use Case Manager
1754  * (UCM) configurations for the card.
1755  *
1756  * Possible card long names may be:
1757  * DellInc.-XPS139343-01-0310JH
1758  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1759  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1760  *
1761  * This function also supports flavoring the card longname to provide
1762  * the extra differentiation, like "vendor-product-version-board-flavor".
1763  *
1764  * We only keep number and alphabet characters and a few separator characters
1765  * in the card long name since UCM in the user space uses the card long names
1766  * as card configuration directory names and AudoConf cannot support special
1767  * characters like SPACE.
1768  *
1769  * Returns 0 on success, otherwise a negative error code.
1770  */
1771 static int snd_soc_set_dmi_name(struct snd_soc_card *card)
1772 {
1773 	const char *vendor, *product, *board;
1774 	char *dmi_longname;
1775 
1776 	if (card->long_name)
1777 		return 0; /* long name already set by driver or from DMI */
1778 
1779 	if (!dmi_available)
1780 		return 0;
1781 
1782 	/* make up dmi long name as: vendor-product-version-board */
1783 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1784 	if (!vendor || !is_dmi_valid(vendor)) {
1785 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1786 		return 0;
1787 	}
1788 
1789 	dmi_longname = devm_kzalloc(card->dev, DMI_LONGNAME_LEN, GFP_KERNEL);
1790 	if (!dmi_longname)
1791 		return -ENOMEM;
1792 
1793 	snprintf(dmi_longname, DMI_LONGNAME_LEN, "%s", vendor);
1794 	cleanup_dmi_name(dmi_longname);
1795 
1796 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
1797 	if (product && is_dmi_valid(product)) {
1798 		const char *product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1799 
1800 		append_dmi_string(dmi_longname, product);
1801 
1802 		/*
1803 		 * some vendors like Lenovo may only put a self-explanatory
1804 		 * name in the product version field
1805 		 */
1806 		if (product_version && is_dmi_valid(product_version))
1807 			append_dmi_string(dmi_longname, product_version);
1808 	}
1809 
1810 	board = dmi_get_system_info(DMI_BOARD_NAME);
1811 	if (board && is_dmi_valid(board)) {
1812 		if (!product || strcasecmp(board, product))
1813 			append_dmi_string(dmi_longname, board);
1814 	} else if (!product) {
1815 		/* fall back to using legacy name */
1816 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1817 		return 0;
1818 	}
1819 
1820 	/* set the card long name */
1821 	card->long_name = dmi_longname;
1822 
1823 	return 0;
1824 }
1825 #else
1826 static inline int snd_soc_set_dmi_name(struct snd_soc_card *card)
1827 {
1828 	return 0;
1829 }
1830 #endif /* CONFIG_DMI */
1831 
1832 static void soc_check_tplg_fes(struct snd_soc_card *card)
1833 {
1834 	struct snd_soc_component *component;
1835 	struct snd_soc_dai_link *dai_link;
1836 	int i;
1837 
1838 	for_each_component(component) {
1839 
1840 		/* does this component override BEs ? */
1841 		if (!component->driver->ignore_machine)
1842 			continue;
1843 
1844 		/* for this machine ? */
1845 		if (!strcmp(component->driver->ignore_machine,
1846 			    card->dev->driver->name))
1847 			goto match;
1848 		if (strcmp(component->driver->ignore_machine,
1849 			   dev_name(card->dev)))
1850 			continue;
1851 match:
1852 		/* machine matches, so override the rtd data */
1853 		for_each_card_prelinks(card, i, dai_link) {
1854 
1855 			/* ignore this FE */
1856 			if (dai_link->dynamic) {
1857 				dai_link->ignore = true;
1858 				continue;
1859 			}
1860 
1861 			dev_dbg(card->dev, "info: override BE DAI link %s\n",
1862 				card->dai_link[i].name);
1863 
1864 			/* override platform component */
1865 			if (!dai_link->platforms) {
1866 				dev_err(card->dev, "init platform error");
1867 				continue;
1868 			}
1869 
1870 			if (component->dev->of_node)
1871 				dai_link->platforms->of_node = component->dev->of_node;
1872 			else
1873 				dai_link->platforms->name = component->name;
1874 
1875 			/* convert non BE into BE */
1876 			dai_link->no_pcm = 1;
1877 
1878 			/*
1879 			 * override any BE fixups
1880 			 * see
1881 			 *	snd_soc_link_be_hw_params_fixup()
1882 			 */
1883 			dai_link->be_hw_params_fixup =
1884 				component->driver->be_hw_params_fixup;
1885 
1886 			/*
1887 			 * most BE links don't set stream name, so set it to
1888 			 * dai link name if it's NULL to help bind widgets.
1889 			 */
1890 			if (!dai_link->stream_name)
1891 				dai_link->stream_name = dai_link->name;
1892 		}
1893 
1894 		/* Inform userspace we are using alternate topology */
1895 		snd_soc_card_set_topology_name(card, component->driver->topology_name_prefix);
1896 	}
1897 }
1898 
1899 #define soc_setup_card_name(card, name, name1, name2) \
1900 	__soc_setup_card_name(card, name, sizeof(name), name1, name2)
1901 static void __soc_setup_card_name(struct snd_soc_card *card,
1902 				  char *name, int len,
1903 				  const char *name1, const char *name2)
1904 {
1905 	const char *src = name1 ? name1 : name2;
1906 	int i;
1907 
1908 	snprintf(name, len, "%s", src);
1909 
1910 	if (name != card->snd_card->driver)
1911 		return;
1912 
1913 	/*
1914 	 * Name normalization (driver field)
1915 	 *
1916 	 * The driver name is somewhat special, as it's used as a key for
1917 	 * searches in the user-space.
1918 	 *
1919 	 * ex)
1920 	 *	"abcd??efg" -> "abcd__efg"
1921 	 */
1922 	for (i = 0; i < len; i++) {
1923 		switch (name[i]) {
1924 		case '_':
1925 		case '-':
1926 		case '\0':
1927 			break;
1928 		default:
1929 			if (!isalnum(name[i]))
1930 				name[i] = '_';
1931 			break;
1932 		}
1933 	}
1934 
1935 	/*
1936 	 * The driver field should contain a valid string from the user view.
1937 	 * The wrapping usually does not work so well here. Set a smaller string
1938 	 * in the specific ASoC driver.
1939 	 */
1940 	if (strlen(src) > len - 1)
1941 		dev_err(card->dev, "ASoC: driver name too long '%s' -> '%s'\n", src, name);
1942 }
1943 
1944 static void soc_cleanup_card_resources(struct snd_soc_card *card)
1945 {
1946 	struct snd_soc_pcm_runtime *rtd, *n;
1947 
1948 	if (card->snd_card)
1949 		snd_card_disconnect_sync(card->snd_card);
1950 
1951 	snd_soc_dapm_shutdown(card);
1952 
1953 	/* release machine specific resources */
1954 	for_each_card_rtds(card, rtd)
1955 		if (rtd->initialized)
1956 			snd_soc_link_exit(rtd);
1957 	/* flush delayed work before removing DAIs and DAPM widgets */
1958 	snd_soc_flush_all_delayed_work(card);
1959 
1960 	/* remove and free each DAI */
1961 	soc_remove_link_dais(card);
1962 	soc_remove_link_components(card);
1963 
1964 	for_each_card_rtds_safe(card, rtd, n)
1965 		snd_soc_remove_pcm_runtime(card, rtd);
1966 
1967 	/* remove auxiliary devices */
1968 	soc_remove_aux_devices(card);
1969 	soc_unbind_aux_dev(card);
1970 
1971 	snd_soc_dapm_free(snd_soc_card_to_dapm(card));
1972 	soc_cleanup_card_debugfs(card);
1973 
1974 	/* remove the card */
1975 	snd_soc_card_remove(card);
1976 
1977 	if (card->snd_card) {
1978 		snd_card_free(card->snd_card);
1979 		card->snd_card = NULL;
1980 	}
1981 }
1982 
1983 static void snd_soc_remove_device_links(struct snd_soc_card *card)
1984 {
1985 	struct snd_soc_component *component;
1986 
1987 	for_each_card_components(card, component) {
1988 		if (component->card_device_link) {
1989 			device_link_del(component->card_device_link);
1990 			component->card_device_link = NULL;
1991 		}
1992 	}
1993 }
1994 
1995 static void snd_soc_unbind_card(struct snd_soc_card *card)
1996 {
1997 	if (snd_soc_card_is_instantiated(card)) {
1998 		card->instantiated = false;
1999 
2000 		snd_soc_remove_device_links(card);
2001 
2002 		soc_cleanup_card_resources(card);
2003 	}
2004 }
2005 
2006 static int snd_soc_bind_card(struct snd_soc_card *card)
2007 {
2008 	struct snd_soc_pcm_runtime *rtd;
2009 	struct snd_soc_component *component;
2010 	struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
2011 	int ret;
2012 
2013 	snd_soc_card_mutex_lock_root(card);
2014 	snd_soc_fill_dummy_dai(card);
2015 
2016 	snd_soc_dapm_init(dapm, card, NULL);
2017 	list_del_init(&card->list);
2018 
2019 	/* check whether any platform is ignore machine FE and using topology */
2020 	soc_check_tplg_fes(card);
2021 
2022 	/* bind aux_devs too */
2023 	ret = soc_bind_aux_dev(card);
2024 	if (ret < 0)
2025 		goto probe_end;
2026 
2027 	/* add predefined DAI links to the list */
2028 	card->num_rtd = 0;
2029 	ret = snd_soc_add_pcm_runtimes(card, card->dai_link, card->num_links);
2030 	if (ret < 0)
2031 		goto probe_end;
2032 
2033 	/* card bind complete so register a sound card */
2034 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2035 			card->owner, 0, &card->snd_card);
2036 	if (ret < 0) {
2037 		dev_err(card->dev,
2038 			"ASoC: can't create sound card for card %s: %d\n",
2039 			card->name, ret);
2040 		goto probe_end;
2041 	}
2042 
2043 	soc_init_card_debugfs(card);
2044 
2045 	soc_resume_init(card);
2046 
2047 	ret = snd_soc_dapm_new_controls(dapm, card->dapm_widgets,
2048 					card->num_dapm_widgets);
2049 	if (ret < 0)
2050 		goto probe_end;
2051 
2052 	ret = snd_soc_dapm_new_controls(dapm, card->of_dapm_widgets,
2053 					card->num_of_dapm_widgets);
2054 	if (ret < 0)
2055 		goto probe_end;
2056 
2057 	/* initialise the sound card only once */
2058 	ret = snd_soc_card_probe(card);
2059 	if (ret < 0)
2060 		goto probe_end;
2061 
2062 	/* probe all components used by DAI links on this card */
2063 	ret = soc_probe_link_components(card);
2064 	if (ret < 0) {
2065 		if (ret != -EPROBE_DEFER) {
2066 			dev_err(card->dev,
2067 				"ASoC: failed to instantiate card %d\n", ret);
2068 		}
2069 		goto probe_end;
2070 	}
2071 
2072 	/* probe auxiliary components */
2073 	ret = soc_probe_aux_devices(card);
2074 	if (ret < 0) {
2075 		dev_err(card->dev,
2076 			"ASoC: failed to probe aux component %d\n", ret);
2077 		goto probe_end;
2078 	}
2079 
2080 	/* probe all DAI links on this card */
2081 	ret = soc_probe_link_dais(card);
2082 	if (ret < 0) {
2083 		dev_err(card->dev,
2084 			"ASoC: failed to instantiate card %d\n", ret);
2085 		goto probe_end;
2086 	}
2087 
2088 	for_each_card_rtds(card, rtd) {
2089 		ret = soc_init_pcm_runtime(card, rtd);
2090 		if (ret < 0)
2091 			goto probe_end;
2092 	}
2093 
2094 	snd_soc_dapm_link_dai_widgets(card);
2095 	snd_soc_dapm_connect_dai_link_widgets(card);
2096 
2097 	ret = snd_soc_add_card_controls(card, card->controls,
2098 					card->num_controls);
2099 	if (ret < 0)
2100 		goto probe_end;
2101 
2102 	ret = snd_soc_dapm_add_routes(dapm, card->dapm_routes,
2103 				      card->num_dapm_routes);
2104 	if (ret < 0)
2105 		goto probe_end;
2106 
2107 	ret = snd_soc_dapm_add_routes(dapm, card->of_dapm_routes,
2108 				      card->num_of_dapm_routes);
2109 	if (ret < 0)
2110 		goto probe_end;
2111 
2112 	/* try to set some sane longname if DMI is available */
2113 	snd_soc_set_dmi_name(card);
2114 
2115 	soc_setup_card_name(card, card->snd_card->shortname,
2116 			    card->name, NULL);
2117 	soc_setup_card_name(card, card->snd_card->longname,
2118 			    card->long_name, card->name);
2119 	soc_setup_card_name(card, card->snd_card->driver,
2120 			    card->driver_name, card->name);
2121 
2122 	if (card->components) {
2123 		/* the current implementation of snd_component_add() accepts */
2124 		/* multiple components in the string separated by space, */
2125 		/* but the string collision (identical string) check might */
2126 		/* not work correctly */
2127 		ret = snd_component_add(card->snd_card, card->components);
2128 		if (ret < 0) {
2129 			dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
2130 				card->name, ret);
2131 			goto probe_end;
2132 		}
2133 	}
2134 
2135 	/*
2136 	 * Add device_link from card to component so that system_suspend
2137 	 * will be done in the correct order. The card must suspend first
2138 	 * to stop audio activity before the components suspend.
2139 	 *
2140 	 * If a driver pair already have a link in the opposite direction
2141 	 * they must manage their own suspend order.
2142 	 */
2143 	for_each_card_components(card, component) {
2144 		if (card->dev == component->dev)
2145 			continue;
2146 
2147 		component->card_device_link = device_link_add(card->dev,
2148 							      component->dev,
2149 							      DL_FLAG_STATELESS);
2150 		if (!component->card_device_link) {
2151 			dev_warn(card->dev, "Could not create device link to %s\n",
2152 				 dev_name(component->dev));
2153 		}
2154 	}
2155 
2156 	ret = snd_soc_card_late_probe(card);
2157 	if (ret < 0)
2158 		goto probe_end;
2159 
2160 	ret = snd_soc_dapm_ignore_suspend_widgets(card);
2161 	if (ret < 0)
2162 		goto probe_end;
2163 
2164 	snd_soc_dapm_new_widgets(card);
2165 	snd_soc_card_fixup_controls(card);
2166 
2167 	ret = snd_card_register(card->snd_card);
2168 	if (ret < 0) {
2169 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2170 				ret);
2171 		goto probe_end;
2172 	}
2173 
2174 	card->instantiated = 1;
2175 	snd_soc_dapm_mark_endpoints_dirty(card);
2176 	snd_soc_dapm_sync(dapm);
2177 
2178 	/* deactivate pins to sleep state */
2179 	for_each_card_components(card, component)
2180 		if (!snd_soc_component_active(component))
2181 			pinctrl_pm_select_sleep_state(component->dev);
2182 
2183 probe_end:
2184 	if (ret < 0) {
2185 		snd_soc_remove_device_links(card);
2186 		soc_cleanup_card_resources(card);
2187 	}
2188 
2189 	if (ret == -EPROBE_DEFER) {
2190 		list_add(&card->list, &unbind_card_list);
2191 		ret = 0;
2192 	}
2193 	snd_soc_card_mutex_unlock(card);
2194 
2195 	return ret;
2196 }
2197 
2198 static void devm_card_bind_release(struct device *dev, void *res)
2199 {
2200 	snd_soc_unregister_card(*(struct snd_soc_card **)res);
2201 }
2202 
2203 static int devm_snd_soc_bind_card(struct device *dev, struct snd_soc_card *card)
2204 {
2205 	struct snd_soc_card **ptr;
2206 	int ret;
2207 
2208 	/* The procedure may be called many times during the lifetime of the card. */
2209 	devres_destroy(dev, devm_card_bind_release, NULL, NULL);
2210 
2211 	ptr = devres_alloc(devm_card_bind_release, sizeof(*ptr), GFP_KERNEL);
2212 	if (!ptr)
2213 		return -ENOMEM;
2214 
2215 	ret = snd_soc_bind_card(card);
2216 	if (ret == 0) {
2217 		*ptr = card;
2218 		devres_add(dev, ptr);
2219 	} else {
2220 		devres_free(ptr);
2221 	}
2222 
2223 	return ret;
2224 }
2225 
2226 static int call_soc_bind_card(struct snd_soc_card *card)
2227 {
2228 	if (card->devres_dev)
2229 		return devm_snd_soc_bind_card(card->devres_dev, card);
2230 	return snd_soc_bind_card(card);
2231 }
2232 
2233 /* probes a new socdev */
2234 static int soc_probe(struct platform_device *pdev)
2235 {
2236 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2237 
2238 	/*
2239 	 * no card, so machine driver should be registering card
2240 	 * we should not be here in that case so ret error
2241 	 */
2242 	if (!card)
2243 		return -EINVAL;
2244 
2245 	dev_warn(&pdev->dev,
2246 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2247 		 card->name);
2248 
2249 	/* Bodge while we unpick instantiation */
2250 	card->dev = &pdev->dev;
2251 
2252 	return devm_snd_soc_register_card(&pdev->dev, card);
2253 }
2254 
2255 int snd_soc_poweroff(struct device *dev)
2256 {
2257 	struct snd_soc_card *card = dev_get_drvdata(dev);
2258 	struct snd_soc_component *component;
2259 
2260 	if (!snd_soc_card_is_instantiated(card))
2261 		return 0;
2262 
2263 	/*
2264 	 * Flush out pmdown_time work - we actually do want to run it
2265 	 * now, we're shutting down so no imminent restart.
2266 	 */
2267 	snd_soc_flush_all_delayed_work(card);
2268 
2269 	snd_soc_dapm_shutdown(card);
2270 
2271 	/* deactivate pins to sleep state */
2272 	for_each_card_components(card, component)
2273 		pinctrl_pm_select_sleep_state(component->dev);
2274 
2275 	return 0;
2276 }
2277 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2278 
2279 const struct dev_pm_ops snd_soc_pm_ops = {
2280 	.suspend = snd_soc_suspend,
2281 	.resume = snd_soc_resume,
2282 	.freeze = snd_soc_suspend,
2283 	.thaw = snd_soc_resume,
2284 	.poweroff = snd_soc_poweroff,
2285 	.restore = snd_soc_resume,
2286 };
2287 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2288 
2289 /* ASoC platform driver */
2290 static struct platform_driver soc_driver = {
2291 	.driver		= {
2292 		.name		= "soc-audio",
2293 		.pm		= &snd_soc_pm_ops,
2294 	},
2295 	.probe		= soc_probe,
2296 };
2297 
2298 /**
2299  * snd_soc_cnew - create new control
2300  * @_template: control template
2301  * @data: control private data
2302  * @long_name: control long name
2303  * @prefix: control name prefix
2304  *
2305  * Create a new mixer control from a template control.
2306  *
2307  * Returns 0 for success, else error.
2308  */
2309 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2310 				  void *data, const char *long_name,
2311 				  const char *prefix)
2312 {
2313 	struct snd_kcontrol_new template;
2314 	struct snd_kcontrol *kcontrol;
2315 	char *name = NULL;
2316 
2317 	memcpy(&template, _template, sizeof(template));
2318 	template.index = 0;
2319 
2320 	if (!long_name)
2321 		long_name = template.name;
2322 
2323 	if (prefix) {
2324 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2325 		if (!name)
2326 			return NULL;
2327 
2328 		template.name = name;
2329 	} else {
2330 		template.name = long_name;
2331 	}
2332 
2333 	kcontrol = snd_ctl_new1(&template, data);
2334 
2335 	kfree(name);
2336 
2337 	return kcontrol;
2338 }
2339 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2340 
2341 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2342 	const struct snd_kcontrol_new *controls, int num_controls,
2343 	const char *prefix, void *data)
2344 {
2345 	int i;
2346 
2347 	for (i = 0; i < num_controls; i++) {
2348 		const struct snd_kcontrol_new *control = &controls[i];
2349 		int err = snd_ctl_add(card, snd_soc_cnew(control, data,
2350 							 control->name, prefix));
2351 		if (err < 0) {
2352 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2353 				control->name, err);
2354 			return err;
2355 		}
2356 	}
2357 
2358 	return 0;
2359 }
2360 
2361 /**
2362  * snd_soc_add_component_controls - Add an array of controls to a component.
2363  *
2364  * @component: Component to add controls to
2365  * @controls: Array of controls to add
2366  * @num_controls: Number of elements in the array
2367  *
2368  * Return: 0 for success, else error.
2369  */
2370 int snd_soc_add_component_controls(struct snd_soc_component *component,
2371 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2372 {
2373 	struct snd_card *card = component->card->snd_card;
2374 
2375 	return snd_soc_add_controls(card, component->dev, controls,
2376 			num_controls, component->name_prefix, component);
2377 }
2378 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2379 
2380 /**
2381  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2382  * Convenience function to add a list of controls.
2383  *
2384  * @soc_card: SoC card to add controls to
2385  * @controls: array of controls to add
2386  * @num_controls: number of elements in the array
2387  *
2388  * Return 0 for success, else error.
2389  */
2390 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2391 	const struct snd_kcontrol_new *controls, int num_controls)
2392 {
2393 	struct snd_card *card = soc_card->snd_card;
2394 
2395 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2396 			NULL, soc_card);
2397 }
2398 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2399 
2400 /**
2401  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2402  * Convenience function to add a list of controls.
2403  *
2404  * @dai: DAI to add controls to
2405  * @controls: array of controls to add
2406  * @num_controls: number of elements in the array
2407  *
2408  * Return 0 for success, else error.
2409  */
2410 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2411 	const struct snd_kcontrol_new *controls, int num_controls)
2412 {
2413 	struct snd_card *card = dai->component->card->snd_card;
2414 
2415 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2416 			NULL, dai);
2417 }
2418 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2419 
2420 /**
2421  * snd_soc_register_card - Register a card with the ASoC core
2422  *
2423  * @card: Card to register
2424  *
2425  */
2426 int snd_soc_register_card(struct snd_soc_card *card)
2427 {
2428 	if (!card->name || !card->dev)
2429 		return -EINVAL;
2430 
2431 	card->dapm = snd_soc_dapm_alloc(card->dev);
2432 	if (!card->dapm)
2433 		return -ENOMEM;
2434 
2435 	dev_set_drvdata(card->dev, card);
2436 
2437 	INIT_LIST_HEAD(&card->widgets);
2438 	INIT_LIST_HEAD(&card->paths);
2439 	INIT_LIST_HEAD(&card->dapm_list);
2440 	INIT_LIST_HEAD(&card->aux_comp_list);
2441 	INIT_LIST_HEAD(&card->component_dev_list);
2442 	INIT_LIST_HEAD(&card->list);
2443 	INIT_LIST_HEAD(&card->rtd_list);
2444 	INIT_LIST_HEAD(&card->dapm_dirty);
2445 
2446 	card->instantiated = 0;
2447 	mutex_init(&card->mutex);
2448 	mutex_init(&card->dapm_mutex);
2449 	mutex_init(&card->pcm_mutex);
2450 
2451 	guard(mutex)(&client_mutex);
2452 
2453 	return call_soc_bind_card(card);
2454 }
2455 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2456 
2457 /**
2458  * snd_soc_unregister_card - Unregister a card with the ASoC core
2459  *
2460  * @card: Card to unregister
2461  *
2462  */
2463 void snd_soc_unregister_card(struct snd_soc_card *card)
2464 {
2465 	guard(mutex)(&client_mutex);
2466 
2467 	snd_soc_unbind_card(card);
2468 	list_del(&card->list);
2469 
2470 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2471 }
2472 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2473 
2474 /*
2475  * Simplify DAI link configuration by removing ".-1" from device names
2476  * and sanitizing names.
2477  */
2478 static char *fmt_single_name(struct device *dev, int *id)
2479 {
2480 	const char *devname = dev_name(dev);
2481 	char *found, *name;
2482 	unsigned int id1, id2;
2483 	int __id;
2484 
2485 	if (devname == NULL)
2486 		return NULL;
2487 
2488 	name = devm_kstrdup(dev, devname, GFP_KERNEL);
2489 	if (!name)
2490 		return NULL;
2491 
2492 	/* are we a "%s.%d" name (platform and SPI components) */
2493 	found = strstr(name, dev->driver->name);
2494 	if (found) {
2495 		/* get ID */
2496 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", &__id) == 1) {
2497 
2498 			/* discard ID from name if ID == -1 */
2499 			if (__id == -1)
2500 				found[strlen(dev->driver->name)] = '\0';
2501 		}
2502 
2503 	/* I2C component devices are named "bus-addr" */
2504 	} else if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2505 
2506 		/* create unique ID number from I2C addr and bus */
2507 		__id = ((id1 & 0xffff) << 16) + id2;
2508 
2509 		devm_kfree(dev, name);
2510 
2511 		/* sanitize component name for DAI link creation */
2512 		name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname);
2513 	} else {
2514 		__id = 0;
2515 	}
2516 
2517 	if (id)
2518 		*id = __id;
2519 
2520 	return name;
2521 }
2522 
2523 /*
2524  * Simplify DAI link naming for single devices with multiple DAIs by removing
2525  * any ".-1" and using the DAI name (instead of device name).
2526  */
2527 static inline char *fmt_multiple_name(struct device *dev,
2528 		struct snd_soc_dai_driver *dai_drv)
2529 {
2530 	if (dai_drv->name == NULL) {
2531 		dev_err(dev,
2532 			"ASoC: error - multiple DAI %s registered with no name\n",
2533 			dev_name(dev));
2534 		return NULL;
2535 	}
2536 
2537 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2538 }
2539 
2540 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2541 {
2542 	lockdep_assert_held(&client_mutex);
2543 
2544 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2545 	list_del(&dai->list);
2546 }
2547 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2548 
2549 /**
2550  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2551  *
2552  * @component: The component the DAIs are registered for
2553  * @dai_drv: DAI driver to use for the DAI
2554  * @legacy_dai_naming: if %true, use legacy single-name format;
2555  * 	if %false, use multiple-name format;
2556  *
2557  * Topology can use this API to register DAIs when probing a component.
2558  * These DAIs's widgets will be freed in the card cleanup and the DAIs
2559  * will be freed in the component cleanup.
2560  */
2561 struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2562 					 struct snd_soc_dai_driver *dai_drv,
2563 					 bool legacy_dai_naming)
2564 {
2565 	struct device *dev = component->dev;
2566 	struct snd_soc_dai *dai;
2567 
2568 	lockdep_assert_held(&client_mutex);
2569 
2570 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2571 	if (dai == NULL)
2572 		return NULL;
2573 
2574 	/*
2575 	 * Back in the old days when we still had component-less DAIs,
2576 	 * instead of having a static name, component-less DAIs would
2577 	 * inherit the name of the parent device so it is possible to
2578 	 * register multiple instances of the DAI. We still need to keep
2579 	 * the same naming style even though those DAIs are not
2580 	 * component-less anymore.
2581 	 */
2582 	if (legacy_dai_naming &&
2583 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
2584 		dai->name = fmt_single_name(dev, &dai->id);
2585 	} else {
2586 		dai->name = fmt_multiple_name(dev, dai_drv);
2587 		if (dai_drv->id)
2588 			dai->id = dai_drv->id;
2589 		else
2590 			dai->id = component->num_dai;
2591 	}
2592 	if (!dai->name)
2593 		return NULL;
2594 
2595 	dai->component = component;
2596 	dai->dev = dev;
2597 	dai->driver = dai_drv;
2598 
2599 	/* see for_each_component_dais */
2600 	list_add_tail(&dai->list, &component->dai_list);
2601 	component->num_dai++;
2602 
2603 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2604 	return dai;
2605 }
2606 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2607 
2608 /**
2609  * snd_soc_unregister_dais - Unregister DAIs from the ASoC core
2610  *
2611  * @component: The component for which the DAIs should be unregistered
2612  */
2613 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2614 {
2615 	struct snd_soc_dai *dai, *_dai;
2616 
2617 	for_each_component_dais_safe(component, dai, _dai)
2618 		snd_soc_unregister_dai(dai);
2619 }
2620 
2621 /**
2622  * snd_soc_register_dais - Register a DAI with the ASoC core
2623  *
2624  * @component: The component the DAIs are registered for
2625  * @dai_drv: DAI driver to use for the DAIs
2626  * @count: Number of DAIs
2627  */
2628 static int snd_soc_register_dais(struct snd_soc_component *component,
2629 				 struct snd_soc_dai_driver *dai_drv,
2630 				 size_t count)
2631 {
2632 	struct snd_soc_dai *dai;
2633 	unsigned int i;
2634 	int ret;
2635 
2636 	for (i = 0; i < count; i++) {
2637 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2638 					   component->driver->legacy_dai_naming);
2639 		if (dai == NULL) {
2640 			ret = -ENOMEM;
2641 			goto err;
2642 		}
2643 	}
2644 
2645 	return 0;
2646 
2647 err:
2648 	snd_soc_unregister_dais(component);
2649 
2650 	return ret;
2651 }
2652 
2653 #define ENDIANNESS_MAP(name) \
2654 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2655 static u64 endianness_format_map[] = {
2656 	ENDIANNESS_MAP(S16_),
2657 	ENDIANNESS_MAP(U16_),
2658 	ENDIANNESS_MAP(S24_),
2659 	ENDIANNESS_MAP(U24_),
2660 	ENDIANNESS_MAP(S32_),
2661 	ENDIANNESS_MAP(U32_),
2662 	ENDIANNESS_MAP(S24_3),
2663 	ENDIANNESS_MAP(U24_3),
2664 	ENDIANNESS_MAP(S20_3),
2665 	ENDIANNESS_MAP(U20_3),
2666 	ENDIANNESS_MAP(S18_3),
2667 	ENDIANNESS_MAP(U18_3),
2668 	ENDIANNESS_MAP(FLOAT_),
2669 	ENDIANNESS_MAP(FLOAT64_),
2670 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2671 };
2672 
2673 /*
2674  * Fix up the DAI formats for endianness: codecs don't actually see
2675  * the endianness of the data but we're using the CPU format
2676  * definitions which do need to include endianness so we ensure that
2677  * codec DAIs always have both big and little endian variants set.
2678  */
2679 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2680 {
2681 	int i;
2682 
2683 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2684 		if (stream->formats & endianness_format_map[i])
2685 			stream->formats |= endianness_format_map[i];
2686 }
2687 
2688 static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2689 {
2690 	struct snd_soc_card *card = component->card;
2691 	bool instantiated;
2692 
2693 	snd_soc_unregister_dais(component);
2694 
2695 	if (card) {
2696 		instantiated = card->instantiated;
2697 		snd_soc_unbind_card(card);
2698 		if (instantiated)
2699 			list_add(&card->list, &unbind_card_list);
2700 	}
2701 
2702 	list_del(&component->list);
2703 }
2704 
2705 int snd_soc_component_initialize(struct snd_soc_component *component,
2706 				 const struct snd_soc_component_driver *driver,
2707 				 struct device *dev)
2708 {
2709 	component->dapm = snd_soc_dapm_alloc(dev);
2710 	if (!component->dapm)
2711 		return -ENOMEM;
2712 
2713 	INIT_LIST_HEAD(&component->dai_list);
2714 	INIT_LIST_HEAD(&component->dobj_list);
2715 	INIT_LIST_HEAD(&component->card_list);
2716 	INIT_LIST_HEAD(&component->list);
2717 	INIT_LIST_HEAD(&component->card_aux_list);
2718 	mutex_init(&component->io_mutex);
2719 
2720 	if (!component->name) {
2721 		component->name = fmt_single_name(dev, NULL);
2722 		if (!component->name) {
2723 			dev_err(dev, "ASoC: Failed to allocate name\n");
2724 			return -ENOMEM;
2725 		}
2726 	}
2727 
2728 	component->dev		= dev;
2729 	component->driver	= driver;
2730 
2731 	return 0;
2732 }
2733 EXPORT_SYMBOL_GPL(snd_soc_component_initialize);
2734 
2735 int snd_soc_add_component(struct snd_soc_component *component,
2736 			  struct snd_soc_dai_driver *dai_drv,
2737 			  int num_dai)
2738 {
2739 	struct snd_soc_card *card, *c;
2740 	int ret;
2741 	int i;
2742 	guard(mutex)(&client_mutex);
2743 
2744 	if (component->driver->endianness) {
2745 		for (i = 0; i < num_dai; i++) {
2746 			convert_endianness_formats(&dai_drv[i].playback);
2747 			convert_endianness_formats(&dai_drv[i].capture);
2748 		}
2749 	}
2750 
2751 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2752 	if (ret < 0) {
2753 		dev_err(component->dev, "ASoC: Failed to register DAIs: %d\n",
2754 			ret);
2755 		goto err_cleanup;
2756 	}
2757 
2758 	if (!component->driver->write && !component->driver->read) {
2759 		if (!component->regmap)
2760 			component->regmap = dev_get_regmap(component->dev,
2761 							   NULL);
2762 	}
2763 
2764 	/* see for_each_component */
2765 	list_add(&component->list, &component_list);
2766 
2767 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2768 		call_soc_bind_card(card);
2769 
2770 err_cleanup:
2771 	if (ret < 0)
2772 		snd_soc_del_component_unlocked(component);
2773 
2774 	return ret;
2775 }
2776 EXPORT_SYMBOL_GPL(snd_soc_add_component);
2777 
2778 int snd_soc_register_component(struct device *dev,
2779 			const struct snd_soc_component_driver *component_driver,
2780 			struct snd_soc_dai_driver *dai_drv,
2781 			int num_dai)
2782 {
2783 	struct snd_soc_component *component;
2784 	int ret;
2785 
2786 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2787 	if (!component)
2788 		return -ENOMEM;
2789 
2790 	ret = snd_soc_component_initialize(component, component_driver, dev);
2791 	if (ret < 0)
2792 		return ret;
2793 
2794 	return snd_soc_add_component(component, dai_drv, num_dai);
2795 }
2796 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2797 
2798 /**
2799  * snd_soc_unregister_component_by_driver - Unregister component using a given driver
2800  * from the ASoC core
2801  *
2802  * @dev: The device to unregister
2803  * @component_driver: The component driver to unregister
2804  */
2805 void snd_soc_unregister_component_by_driver(struct device *dev,
2806 					    const struct snd_soc_component_driver *component_driver)
2807 {
2808 	const char *driver_name = NULL;
2809 
2810 	if (component_driver)
2811 		driver_name = component_driver->name;
2812 
2813 	guard(mutex)(&client_mutex);
2814 
2815 	while (1) {
2816 		struct snd_soc_component *component = snd_soc_lookup_component_nolocked(dev, driver_name);
2817 
2818 		if (!component)
2819 			break;
2820 
2821 		snd_soc_del_component_unlocked(component);
2822 	}
2823 }
2824 EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver);
2825 
2826 /* Retrieve a card's name from device tree */
2827 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2828 			       const char *propname)
2829 {
2830 	struct device_node *np;
2831 	int ret;
2832 
2833 	if (!card->dev) {
2834 		pr_err("card->dev is not set before calling %s\n", __func__);
2835 		return -EINVAL;
2836 	}
2837 
2838 	np = card->dev->of_node;
2839 
2840 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2841 	/*
2842 	 * EINVAL means the property does not exist. This is fine providing
2843 	 * card->name was previously set, which is checked later in
2844 	 * snd_soc_register_card.
2845 	 */
2846 	if (ret < 0 && ret != -EINVAL) {
2847 		dev_err(card->dev,
2848 			"ASoC: Property '%s' could not be read: %d\n",
2849 			propname, ret);
2850 		return ret;
2851 	}
2852 
2853 	return 0;
2854 }
2855 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2856 
2857 static const struct snd_soc_dapm_widget simple_widgets[] = {
2858 	SND_SOC_DAPM_MIC("Microphone", NULL),
2859 	SND_SOC_DAPM_LINE("Line", NULL),
2860 	SND_SOC_DAPM_HP("Headphone", NULL),
2861 	SND_SOC_DAPM_SPK("Speaker", NULL),
2862 };
2863 
2864 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2865 					  const char *propname)
2866 {
2867 	struct device_node *np = card->dev->of_node;
2868 	struct snd_soc_dapm_widget *widgets;
2869 	const char *template, *wname;
2870 	int i, j, num_widgets;
2871 
2872 	num_widgets = of_property_count_strings(np, propname);
2873 	if (num_widgets < 0) {
2874 		dev_err(card->dev,
2875 			"ASoC: Property '%s' does not exist\n",	propname);
2876 		return -EINVAL;
2877 	}
2878 	if (!num_widgets) {
2879 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2880 			propname);
2881 		return -EINVAL;
2882 	}
2883 	if (num_widgets & 1) {
2884 		dev_err(card->dev,
2885 			"ASoC: Property '%s' length is not even\n", propname);
2886 		return -EINVAL;
2887 	}
2888 
2889 	num_widgets /= 2;
2890 
2891 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2892 			       GFP_KERNEL);
2893 	if (!widgets) {
2894 		dev_err(card->dev,
2895 			"ASoC: Could not allocate memory for widgets\n");
2896 		return -ENOMEM;
2897 	}
2898 
2899 	for (i = 0; i < num_widgets; i++) {
2900 		int ret = of_property_read_string_index(np, propname,
2901 							2 * i, &template);
2902 		if (ret) {
2903 			dev_err(card->dev,
2904 				"ASoC: Property '%s' index %d read error:%d\n",
2905 				propname, 2 * i, ret);
2906 			return -EINVAL;
2907 		}
2908 
2909 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2910 			if (!strncmp(template, simple_widgets[j].name,
2911 				     strlen(simple_widgets[j].name))) {
2912 				widgets[i] = simple_widgets[j];
2913 				break;
2914 			}
2915 		}
2916 
2917 		if (j >= ARRAY_SIZE(simple_widgets)) {
2918 			dev_err(card->dev,
2919 				"ASoC: DAPM widget '%s' is not supported\n",
2920 				template);
2921 			return -EINVAL;
2922 		}
2923 
2924 		ret = of_property_read_string_index(np, propname,
2925 						    (2 * i) + 1,
2926 						    &wname);
2927 		if (ret) {
2928 			dev_err(card->dev,
2929 				"ASoC: Property '%s' index %d read error:%d\n",
2930 				propname, (2 * i) + 1, ret);
2931 			return -EINVAL;
2932 		}
2933 
2934 		widgets[i].name = wname;
2935 	}
2936 
2937 	card->of_dapm_widgets = widgets;
2938 	card->num_of_dapm_widgets = num_widgets;
2939 
2940 	return 0;
2941 }
2942 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
2943 
2944 int snd_soc_of_parse_pin_switches(struct snd_soc_card *card, const char *prop)
2945 {
2946 	const unsigned int nb_controls_max = 16;
2947 	const char **strings, *control_name;
2948 	struct snd_kcontrol_new *controls;
2949 	struct device *dev = card->dev;
2950 	unsigned int i, nb_controls;
2951 	int ret;
2952 
2953 	if (!of_property_present(dev->of_node, prop))
2954 		return 0;
2955 
2956 	strings = devm_kcalloc(dev, nb_controls_max,
2957 			       sizeof(*strings), GFP_KERNEL);
2958 	if (!strings)
2959 		return -ENOMEM;
2960 
2961 	ret = of_property_read_string_array(dev->of_node, prop,
2962 					    strings, nb_controls_max);
2963 	if (ret < 0)
2964 		return ret;
2965 
2966 	nb_controls = (unsigned int)ret;
2967 
2968 	controls = devm_kcalloc(dev, nb_controls,
2969 				sizeof(*controls), GFP_KERNEL);
2970 	if (!controls)
2971 		return -ENOMEM;
2972 
2973 	for (i = 0; i < nb_controls; i++) {
2974 		control_name = devm_kasprintf(dev, GFP_KERNEL,
2975 					      "%s Switch", strings[i]);
2976 		if (!control_name)
2977 			return -ENOMEM;
2978 
2979 		controls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2980 		controls[i].name = control_name;
2981 		controls[i].info = snd_soc_dapm_info_pin_switch;
2982 		controls[i].get = snd_soc_dapm_get_pin_switch;
2983 		controls[i].put = snd_soc_dapm_put_pin_switch;
2984 		controls[i].private_value = (unsigned long)strings[i];
2985 	}
2986 
2987 	card->controls = controls;
2988 	card->num_controls = nb_controls;
2989 
2990 	return 0;
2991 }
2992 EXPORT_SYMBOL_GPL(snd_soc_of_parse_pin_switches);
2993 
2994 int snd_soc_of_get_slot_mask(struct device_node *np,
2995 			     const char *prop_name,
2996 			     unsigned int *mask)
2997 {
2998 	u32 val;
2999 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3000 	int i;
3001 
3002 	if (!of_slot_mask)
3003 		return 0;
3004 	val /= sizeof(u32);
3005 	for (i = 0; i < val; i++)
3006 		if (be32_to_cpup(&of_slot_mask[i]))
3007 			*mask |= (1 << i);
3008 
3009 	return val;
3010 }
3011 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
3012 
3013 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3014 			      unsigned int *tx_mask,
3015 			      unsigned int *rx_mask,
3016 			      unsigned int *slots,
3017 			      unsigned int *slot_width)
3018 {
3019 	u32 val;
3020 	int ret;
3021 
3022 	if (tx_mask)
3023 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3024 	if (rx_mask)
3025 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3026 
3027 	ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3028 	if (ret && ret != -EINVAL)
3029 		return ret;
3030 	if (!ret && slots)
3031 		*slots = val;
3032 
3033 	ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3034 	if (ret && ret != -EINVAL)
3035 		return ret;
3036 	if (!ret && slot_width)
3037 		*slot_width = val;
3038 
3039 	return 0;
3040 }
3041 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3042 
3043 void snd_soc_dlc_use_cpu_as_platform(struct snd_soc_dai_link_component *platforms,
3044 				     struct snd_soc_dai_link_component *cpus)
3045 {
3046 	platforms->of_node	= cpus->of_node;
3047 	platforms->dai_args	= cpus->dai_args;
3048 }
3049 EXPORT_SYMBOL_GPL(snd_soc_dlc_use_cpu_as_platform);
3050 
3051 void snd_soc_of_parse_node_prefix(struct device_node *np,
3052 				  struct snd_soc_codec_conf *codec_conf,
3053 				  struct device_node *of_node,
3054 				  const char *propname)
3055 {
3056 	const char *str;
3057 	int ret;
3058 
3059 	ret = of_property_read_string(np, propname, &str);
3060 	if (ret < 0) {
3061 		/* no prefix is not error */
3062 		return;
3063 	}
3064 
3065 	codec_conf->dlc.of_node	= of_node;
3066 	codec_conf->name_prefix	= str;
3067 }
3068 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
3069 
3070 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3071 				   const char *propname)
3072 {
3073 	struct device_node *np = card->dev->of_node;
3074 	int num_routes;
3075 	struct snd_soc_dapm_route *routes;
3076 	int i;
3077 
3078 	num_routes = of_property_count_strings(np, propname);
3079 	if (num_routes < 0 || num_routes & 1) {
3080 		dev_err(card->dev,
3081 			"ASoC: Property '%s' does not exist or its length is not even\n",
3082 			propname);
3083 		return -EINVAL;
3084 	}
3085 	num_routes /= 2;
3086 
3087 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3088 			      GFP_KERNEL);
3089 	if (!routes) {
3090 		dev_err(card->dev,
3091 			"ASoC: Could not allocate DAPM route table\n");
3092 		return -ENOMEM;
3093 	}
3094 
3095 	for (i = 0; i < num_routes; i++) {
3096 		int ret = of_property_read_string_index(np, propname,
3097 							2 * i, &routes[i].sink);
3098 		if (ret) {
3099 			dev_err(card->dev,
3100 				"ASoC: Property '%s' index %d could not be read: %d\n",
3101 				propname, 2 * i, ret);
3102 			return -EINVAL;
3103 		}
3104 		ret = of_property_read_string_index(np, propname,
3105 			(2 * i) + 1, &routes[i].source);
3106 		if (ret) {
3107 			dev_err(card->dev,
3108 				"ASoC: Property '%s' index %d could not be read: %d\n",
3109 				propname, (2 * i) + 1, ret);
3110 			return -EINVAL;
3111 		}
3112 	}
3113 
3114 	card->num_of_dapm_routes = num_routes;
3115 	card->of_dapm_routes = routes;
3116 
3117 	return 0;
3118 }
3119 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3120 
3121 int snd_soc_of_parse_aux_devs(struct snd_soc_card *card, const char *propname)
3122 {
3123 	struct device_node *node = card->dev->of_node;
3124 	struct snd_soc_aux_dev *aux;
3125 	int num, i;
3126 
3127 	num = of_count_phandle_with_args(node, propname, NULL);
3128 	if (num == -ENOENT) {
3129 		return 0;
3130 	} else if (num < 0) {
3131 		dev_err(card->dev, "ASOC: Property '%s' could not be read: %d\n",
3132 			propname, num);
3133 		return num;
3134 	}
3135 
3136 	aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
3137 	if (!aux)
3138 		return -ENOMEM;
3139 	card->aux_dev = aux;
3140 	card->num_aux_devs = num;
3141 
3142 	for_each_card_pre_auxs(card, i, aux) {
3143 		aux->dlc.of_node = of_parse_phandle(node, propname, i);
3144 		if (!aux->dlc.of_node)
3145 			return -EINVAL;
3146 	}
3147 
3148 	return 0;
3149 }
3150 EXPORT_SYMBOL_GPL(snd_soc_of_parse_aux_devs);
3151 
3152 int snd_soc_of_parse_ignore_suspend_widgets(struct snd_soc_card *card,
3153 					    const char *propname)
3154 {
3155 	struct device_node *np = card->dev->of_node;
3156 	int num_widgets;
3157 	const char **widgets;
3158 	int i;
3159 
3160 	num_widgets = of_property_count_strings(np, propname);
3161 	if (num_widgets < 0) {
3162 		dev_err(card->dev,
3163 			"ASoC: Property '%s' does not exist\n", propname);
3164 		return -EINVAL;
3165 	}
3166 
3167 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(char *), GFP_KERNEL);
3168 	if (!widgets)
3169 		return -ENOMEM;
3170 
3171 	for (i = 0; i < num_widgets; i++) {
3172 		const char *name;
3173 		int ret = of_property_read_string_index(np, propname, i, &name);
3174 
3175 		if (ret) {
3176 			dev_err(card->dev,
3177 				"ASoC: Property '%s' could not be read: %d\n",
3178 				propname, ret);
3179 			return -EINVAL;
3180 		}
3181 		widgets[i] = name;
3182 	}
3183 
3184 	card->num_of_ignore_suspend_widgets = num_widgets;
3185 	card->of_ignore_suspend_widgets = widgets;
3186 
3187 	return 0;
3188 }
3189 EXPORT_SYMBOL_GPL(snd_soc_of_parse_ignore_suspend_widgets);
3190 
3191 unsigned int snd_soc_daifmt_clock_provider_flipped(unsigned int dai_fmt)
3192 {
3193 	unsigned int inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
3194 
3195 	switch (dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
3196 	case SND_SOC_DAIFMT_CBP_CFP:
3197 		inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
3198 		break;
3199 	case SND_SOC_DAIFMT_CBP_CFC:
3200 		inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
3201 		break;
3202 	case SND_SOC_DAIFMT_CBC_CFP:
3203 		inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
3204 		break;
3205 	case SND_SOC_DAIFMT_CBC_CFC:
3206 		inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
3207 		break;
3208 	}
3209 
3210 	return inv_dai_fmt;
3211 }
3212 EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_flipped);
3213 
3214 unsigned int snd_soc_daifmt_clock_provider_from_bitmap(unsigned int bit_frame)
3215 {
3216 	/*
3217 	 * bit_frame is return value from
3218 	 *	snd_soc_daifmt_parse_clock_provider_raw()
3219 	 */
3220 
3221 	/* Codec base */
3222 	switch (bit_frame) {
3223 	case 0x11:
3224 		return SND_SOC_DAIFMT_CBP_CFP;
3225 	case 0x10:
3226 		return SND_SOC_DAIFMT_CBP_CFC;
3227 	case 0x01:
3228 		return SND_SOC_DAIFMT_CBC_CFP;
3229 	default:
3230 		return SND_SOC_DAIFMT_CBC_CFC;
3231 	}
3232 
3233 	return 0;
3234 }
3235 EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_from_bitmap);
3236 
3237 unsigned int snd_soc_daifmt_parse_format(struct device_node *np,
3238 					 const char *prefix)
3239 {
3240 	int ret;
3241 	char prop[128];
3242 	unsigned int format = 0;
3243 	int bit, frame;
3244 	const char *str;
3245 	struct {
3246 		char *name;
3247 		unsigned int val;
3248 	} of_fmt_table[] = {
3249 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3250 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3251 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3252 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3253 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3254 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3255 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3256 		{ "msb",	SND_SOC_DAIFMT_MSB },
3257 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3258 	};
3259 
3260 	if (!prefix)
3261 		prefix = "";
3262 
3263 	/*
3264 	 * check "dai-format = xxx"
3265 	 * or    "[prefix]format = xxx"
3266 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3267 	 */
3268 	ret = of_property_read_string(np, "dai-format", &str);
3269 	if (ret < 0) {
3270 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3271 		ret = of_property_read_string(np, prop, &str);
3272 	}
3273 	if (ret == 0) {
3274 		int i;
3275 
3276 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3277 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3278 				format |= of_fmt_table[i].val;
3279 				break;
3280 			}
3281 		}
3282 	}
3283 
3284 	/*
3285 	 * check "[prefix]continuous-clock"
3286 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3287 	 */
3288 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3289 	if (of_property_read_bool(np, prop))
3290 		format |= SND_SOC_DAIFMT_CONT;
3291 	else
3292 		format |= SND_SOC_DAIFMT_GATED;
3293 
3294 	/*
3295 	 * check "[prefix]bitclock-inversion"
3296 	 * check "[prefix]frame-inversion"
3297 	 * SND_SOC_DAIFMT_INV_MASK area
3298 	 */
3299 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3300 	bit = of_property_read_bool(np, prop);
3301 
3302 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3303 	frame = of_property_read_bool(np, prop);
3304 
3305 	switch ((bit << 4) + frame) {
3306 	case 0x11:
3307 		format |= SND_SOC_DAIFMT_IB_IF;
3308 		break;
3309 	case 0x10:
3310 		format |= SND_SOC_DAIFMT_IB_NF;
3311 		break;
3312 	case 0x01:
3313 		format |= SND_SOC_DAIFMT_NB_IF;
3314 		break;
3315 	default:
3316 		/* SND_SOC_DAIFMT_NB_NF is default */
3317 		break;
3318 	}
3319 
3320 	return format;
3321 }
3322 EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_format);
3323 
3324 unsigned int snd_soc_daifmt_parse_clock_provider_raw(struct device_node *np,
3325 						     const char *prefix,
3326 						     struct device_node **bitclkmaster,
3327 						     struct device_node **framemaster)
3328 {
3329 	char prop[128];
3330 	unsigned int bit, frame;
3331 
3332 	if (!np)
3333 		return 0;
3334 
3335 	if (!prefix)
3336 		prefix = "";
3337 
3338 	/*
3339 	 * check "[prefix]bitclock-master"
3340 	 * check "[prefix]frame-master"
3341 	 */
3342 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3343 	bit = of_property_present(np, prop);
3344 	if (bit && bitclkmaster)
3345 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3346 
3347 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3348 	frame = of_property_present(np, prop);
3349 	if (frame && framemaster)
3350 		*framemaster = of_parse_phandle(np, prop, 0);
3351 
3352 	/*
3353 	 * return bitmap.
3354 	 * It will be parameter of
3355 	 *	snd_soc_daifmt_clock_provider_from_bitmap()
3356 	 */
3357 	return (bit << 4) + frame;
3358 }
3359 EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_clock_provider_raw);
3360 
3361 int snd_soc_get_stream_cpu(const struct snd_soc_dai_link *dai_link, int stream)
3362 {
3363 	/*
3364 	 * [Normal]
3365 	 *
3366 	 * Playback
3367 	 *	CPU  : SNDRV_PCM_STREAM_PLAYBACK
3368 	 *	Codec: SNDRV_PCM_STREAM_PLAYBACK
3369 	 *
3370 	 * Capture
3371 	 *	CPU  : SNDRV_PCM_STREAM_CAPTURE
3372 	 *	Codec: SNDRV_PCM_STREAM_CAPTURE
3373 	 */
3374 	if (!dai_link->c2c_params)
3375 		return stream;
3376 
3377 	/*
3378 	 * [Codec2Codec]
3379 	 *
3380 	 * Playback
3381 	 *	CPU  : SNDRV_PCM_STREAM_CAPTURE
3382 	 *	Codec: SNDRV_PCM_STREAM_PLAYBACK
3383 	 *
3384 	 * Capture
3385 	 *	CPU  : SNDRV_PCM_STREAM_PLAYBACK
3386 	 *	Codec: SNDRV_PCM_STREAM_CAPTURE
3387 	 */
3388 	if (stream == SNDRV_PCM_STREAM_CAPTURE)
3389 		return SNDRV_PCM_STREAM_PLAYBACK;
3390 
3391 	return SNDRV_PCM_STREAM_CAPTURE;
3392 }
3393 EXPORT_SYMBOL_GPL(snd_soc_get_stream_cpu);
3394 
3395 int snd_soc_get_dai_id(struct device_node *ep)
3396 {
3397 	struct snd_soc_dai_link_component dlc = {
3398 		.of_node = of_graph_get_port_parent(ep),
3399 	};
3400 	int ret;
3401 
3402 
3403 	/*
3404 	 * For example HDMI case, HDMI has video/sound port,
3405 	 * but ALSA SoC needs sound port number only.
3406 	 * Thus counting HDMI DT port/endpoint doesn't work.
3407 	 * Then, it should have .of_xlate_dai_id
3408 	 */
3409 	ret = -ENOTSUPP;
3410 
3411 	scoped_guard(mutex, &client_mutex) {
3412 		struct snd_soc_component *component = soc_find_component(&dlc);
3413 
3414 		if (component)
3415 			ret = snd_soc_component_of_xlate_dai_id(component, ep);
3416 	}
3417 
3418 	of_node_put(dlc.of_node);
3419 
3420 	return ret;
3421 }
3422 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3423 
3424 int snd_soc_get_dlc(const struct of_phandle_args *args, struct snd_soc_dai_link_component *dlc)
3425 {
3426 	struct snd_soc_component *pos;
3427 	int ret = -EPROBE_DEFER;
3428 	guard(mutex)(&client_mutex);
3429 
3430 	for_each_component(pos) {
3431 		struct device_node *component_of_node = soc_component_to_node(pos);
3432 
3433 		if (component_of_node != args->np || !pos->num_dai)
3434 			continue;
3435 
3436 		ret = snd_soc_component_of_xlate_dai_name(pos, args, &dlc->dai_name);
3437 		if (ret == -ENOTSUPP) {
3438 			struct snd_soc_dai *dai;
3439 			int id = -1;
3440 
3441 			switch (args->args_count) {
3442 			case 0:
3443 				id = 0; /* same as dai_drv[0] */
3444 				break;
3445 			case 1:
3446 				id = args->args[0];
3447 				break;
3448 			default:
3449 				/* not supported */
3450 				break;
3451 			}
3452 
3453 			if (id < 0 || id >= pos->num_dai) {
3454 				ret = -EINVAL;
3455 				continue;
3456 			}
3457 
3458 			ret = 0;
3459 
3460 			/* find target DAI */
3461 			for_each_component_dais(pos, dai) {
3462 				if (id == 0)
3463 					break;
3464 				id--;
3465 			}
3466 
3467 			dlc->dai_name	= snd_soc_dai_name_get(dai);
3468 		} else if (ret) {
3469 			/*
3470 			 * if another error than ENOTSUPP is returned go on and
3471 			 * check if another component is provided with the same
3472 			 * node. This may happen if a device provides several
3473 			 * components
3474 			 */
3475 			continue;
3476 		}
3477 
3478 		break;
3479 	}
3480 
3481 	if (ret == 0)
3482 		dlc->of_node = args->np;
3483 
3484 	return ret;
3485 }
3486 EXPORT_SYMBOL_GPL(snd_soc_get_dlc);
3487 
3488 int snd_soc_of_get_dlc(struct device_node *of_node,
3489 		       struct of_phandle_args *args,
3490 		       struct snd_soc_dai_link_component *dlc,
3491 		       int index)
3492 {
3493 	struct of_phandle_args __args;
3494 	int ret;
3495 
3496 	if (!args)
3497 		args = &__args;
3498 
3499 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
3500 					 "#sound-dai-cells", index, args);
3501 	if (ret)
3502 		return ret;
3503 
3504 	return snd_soc_get_dlc(args, dlc);
3505 }
3506 EXPORT_SYMBOL_GPL(snd_soc_of_get_dlc);
3507 
3508 int snd_soc_get_dai_name(const struct of_phandle_args *args,
3509 			 const char **dai_name)
3510 {
3511 	struct snd_soc_dai_link_component dlc;
3512 	int ret = snd_soc_get_dlc(args, &dlc);
3513 
3514 	if (ret == 0)
3515 		*dai_name = dlc.dai_name;
3516 
3517 	return ret;
3518 }
3519 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3520 
3521 int snd_soc_of_get_dai_name(struct device_node *of_node,
3522 			    const char **dai_name, int index)
3523 {
3524 	struct snd_soc_dai_link_component dlc;
3525 	int ret = snd_soc_of_get_dlc(of_node, NULL, &dlc, index);
3526 
3527 	if (ret == 0)
3528 		*dai_name = dlc.dai_name;
3529 
3530 	return ret;
3531 }
3532 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3533 
3534 struct snd_soc_dai *snd_soc_get_dai_via_args(const struct of_phandle_args *dai_args)
3535 {
3536 	struct snd_soc_dai *dai;
3537 	struct snd_soc_component *component;
3538 	guard(mutex)(&client_mutex);
3539 
3540 	for_each_component(component) {
3541 		for_each_component_dais(component, dai)
3542 			if (snd_soc_is_match_dai_args(dai->driver->dai_args, dai_args))
3543 				return dai;
3544 	}
3545 	return NULL;
3546 }
3547 EXPORT_SYMBOL_GPL(snd_soc_get_dai_via_args);
3548 
3549 static void __snd_soc_of_put_component(struct snd_soc_dai_link_component *component)
3550 {
3551 	if (component->of_node) {
3552 		of_node_put(component->of_node);
3553 		component->of_node = NULL;
3554 	}
3555 }
3556 
3557 static int __snd_soc_of_get_dai_link_component_alloc(
3558 	struct device *dev, struct device_node *of_node,
3559 	struct snd_soc_dai_link_component **ret_component,
3560 	int *ret_num)
3561 {
3562 	struct snd_soc_dai_link_component *component;
3563 	int num;
3564 
3565 	/* Count the number of CPUs/CODECs */
3566 	num = of_count_phandle_with_args(of_node, "sound-dai", "#sound-dai-cells");
3567 	if (num <= 0) {
3568 		if (num == -ENOENT)
3569 			dev_err(dev, "No 'sound-dai' property\n");
3570 		else
3571 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
3572 		return num;
3573 	}
3574 	component = devm_kcalloc(dev, num, sizeof(*component), GFP_KERNEL);
3575 	if (!component)
3576 		return -ENOMEM;
3577 
3578 	*ret_component	= component;
3579 	*ret_num	= num;
3580 
3581 	return 0;
3582 }
3583 
3584 /*
3585  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3586  * @dai_link: DAI link
3587  *
3588  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3589  */
3590 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3591 {
3592 	struct snd_soc_dai_link_component *component;
3593 	int index;
3594 
3595 	for_each_link_codecs(dai_link, index, component)
3596 		__snd_soc_of_put_component(component);
3597 }
3598 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3599 
3600 /*
3601  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3602  * @dev: Card device
3603  * @of_node: Device node
3604  * @dai_link: DAI link
3605  *
3606  * Builds an array of CODEC DAI components from the DAI link property
3607  * 'sound-dai'.
3608  * The array is set in the DAI link and the number of DAIs is set accordingly.
3609  * The device nodes in the array (of_node) must be dereferenced by calling
3610  * snd_soc_of_put_dai_link_codecs() on @dai_link.
3611  *
3612  * Returns 0 for success
3613  */
3614 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3615 				   struct device_node *of_node,
3616 				   struct snd_soc_dai_link *dai_link)
3617 {
3618 	struct snd_soc_dai_link_component *component;
3619 	int index, ret;
3620 
3621 	ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
3622 					 &dai_link->codecs, &dai_link->num_codecs);
3623 	if (ret < 0)
3624 		return ret;
3625 
3626 	/* Parse the list */
3627 	for_each_link_codecs(dai_link, index, component) {
3628 		ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
3629 		if (ret)
3630 			goto err;
3631 	}
3632 	return 0;
3633 err:
3634 	snd_soc_of_put_dai_link_codecs(dai_link);
3635 	dai_link->codecs = NULL;
3636 	dai_link->num_codecs = 0;
3637 	return ret;
3638 }
3639 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3640 
3641 /*
3642  * snd_soc_of_put_dai_link_cpus - Dereference device nodes in the codecs array
3643  * @dai_link: DAI link
3644  *
3645  * Dereference device nodes acquired by snd_soc_of_get_dai_link_cpus().
3646  */
3647 void snd_soc_of_put_dai_link_cpus(struct snd_soc_dai_link *dai_link)
3648 {
3649 	struct snd_soc_dai_link_component *component;
3650 	int index;
3651 
3652 	for_each_link_cpus(dai_link, index, component)
3653 		__snd_soc_of_put_component(component);
3654 }
3655 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_cpus);
3656 
3657 /*
3658  * snd_soc_of_get_dai_link_cpus - Parse a list of CPU DAIs in the devicetree
3659  * @dev: Card device
3660  * @of_node: Device node
3661  * @dai_link: DAI link
3662  *
3663  * Is analogous to snd_soc_of_get_dai_link_codecs but parses a list of CPU DAIs
3664  * instead.
3665  *
3666  * Returns 0 for success
3667  */
3668 int snd_soc_of_get_dai_link_cpus(struct device *dev,
3669 				 struct device_node *of_node,
3670 				 struct snd_soc_dai_link *dai_link)
3671 {
3672 	struct snd_soc_dai_link_component *component;
3673 	int index, ret;
3674 
3675 	/* Count the number of CPUs */
3676 	ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
3677 					 &dai_link->cpus, &dai_link->num_cpus);
3678 	if (ret < 0)
3679 		return ret;
3680 
3681 	/* Parse the list */
3682 	for_each_link_cpus(dai_link, index, component) {
3683 		ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
3684 		if (ret)
3685 			goto err;
3686 	}
3687 	return 0;
3688 err:
3689 	snd_soc_of_put_dai_link_cpus(dai_link);
3690 	dai_link->cpus = NULL;
3691 	dai_link->num_cpus = 0;
3692 	return ret;
3693 }
3694 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_cpus);
3695 
3696 static int __init snd_soc_init(void)
3697 {
3698 	int ret;
3699 
3700 	snd_soc_debugfs_init();
3701 	ret = snd_soc_util_init();
3702 	if (ret)
3703 		goto err_util_init;
3704 
3705 	ret = platform_driver_register(&soc_driver);
3706 	if (ret)
3707 		goto err_register;
3708 	return 0;
3709 
3710 err_register:
3711 	snd_soc_util_exit();
3712 err_util_init:
3713 	snd_soc_debugfs_exit();
3714 	return ret;
3715 }
3716 module_init(snd_soc_init);
3717 
3718 static void __exit snd_soc_exit(void)
3719 {
3720 	snd_soc_util_exit();
3721 	snd_soc_debugfs_exit();
3722 
3723 	platform_driver_unregister(&soc_driver);
3724 }
3725 module_exit(snd_soc_exit);
3726 
3727 /* Module information */
3728 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3729 MODULE_DESCRIPTION("ALSA SoC Core");
3730 MODULE_LICENSE("GPL");
3731 MODULE_ALIAS("platform:soc-audio");
3732