xref: /linux/sound/soc/soc-core.c (revision a67c554dbc0fdd7e3c5909cb9f0fff41c51b2e9d)
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->debugfs_prefix) {
140 		char *name;
141 
142 		name = kasprintf(GFP_KERNEL, "%s:%s",
143 			component->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 static void snd_soc_runtime_get_dai_fmt(struct snd_soc_pcm_runtime *rtd)
1288 {
1289 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1290 	struct snd_soc_dai *dai, *not_used;
1291 	u64 pos, possible_fmt;
1292 	unsigned int mask = 0, dai_fmt = 0;
1293 	int i, j, priority, pri, until;
1294 
1295 	/*
1296 	 * Get selectable format from each DAIs.
1297 	 *
1298 	 ****************************
1299 	 *            NOTE
1300 	 * Using .auto_selectable_formats is not mandatory,
1301 	 * we can select format manually from Sound Card.
1302 	 * When use it, driver should list well tested format only.
1303 	 ****************************
1304 	 *
1305 	 * ex)
1306 	 *	auto_selectable_formats (= SND_SOC_POSSIBLE_xxx)
1307 	 *		 (A)	 (B)	 (C)
1308 	 *	DAI0_: { 0x000F, 0x00F0, 0x0F00 };
1309 	 *	DAI1 : { 0xF000, 0x0F00 };
1310 	 *		 (X)	 (Y)
1311 	 *
1312 	 * "until" will be 3 in this case (MAX array size from DAI0 and DAI1)
1313 	 * Here is dev_dbg() message and comments
1314 	 *
1315 	 * priority = 1
1316 	 * DAI0: (pri, fmt) = (1, 000000000000000F) // 1st check (A) DAI1 is not selected
1317 	 * DAI1: (pri, fmt) = (0, 0000000000000000) //               Necessary Waste
1318 	 * DAI0: (pri, fmt) = (1, 000000000000000F) // 2nd check (A)
1319 	 * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1320 	 * priority = 2
1321 	 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 3rd check (A) + (B)
1322 	 * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1323 	 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 4th check (A) + (B)
1324 	 * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1325 	 * priority = 3
1326 	 * DAI0: (pri, fmt) = (3, 0000000000000FFF) // 5th check (A) + (B) + (C)
1327 	 * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1328 	 * found auto selected format: 0000000000000F00
1329 	 */
1330 	until = snd_soc_dai_get_fmt_max_priority(rtd);
1331 	for (priority = 1; priority <= until; priority++) {
1332 		for_each_rtd_dais(rtd, j, not_used) {
1333 
1334 			possible_fmt = ULLONG_MAX;
1335 			for_each_rtd_dais(rtd, i, dai) {
1336 				u64 fmt = 0;
1337 
1338 				pri = (j >= i) ? priority : priority - 1;
1339 				fmt = snd_soc_dai_get_fmt(dai, pri);
1340 				possible_fmt &= fmt;
1341 			}
1342 			if (possible_fmt)
1343 				goto found;
1344 		}
1345 	}
1346 	/* Not Found */
1347 	return;
1348 found:
1349 	/*
1350 	 * convert POSSIBLE_DAIFMT to DAIFMT
1351 	 *
1352 	 * Some basic/default settings on each is defined as 0.
1353 	 * see
1354 	 *	SND_SOC_DAIFMT_NB_NF
1355 	 *	SND_SOC_DAIFMT_GATED
1356 	 *
1357 	 * SND_SOC_DAIFMT_xxx_MASK can't notice it if Sound Card specify
1358 	 * these value, and will be overwrite to auto selected value.
1359 	 *
1360 	 * To avoid such issue, loop from 63 to 0 here.
1361 	 * Small number of SND_SOC_POSSIBLE_xxx will be Hi priority.
1362 	 * Basic/Default settings of each part and above are defined
1363 	 * as Hi priority (= small number) of SND_SOC_POSSIBLE_xxx.
1364 	 */
1365 	for (i = 63; i >= 0; i--) {
1366 		pos = 1ULL << i;
1367 		switch (possible_fmt & pos) {
1368 		/*
1369 		 * for format
1370 		 */
1371 		case SND_SOC_POSSIBLE_DAIFMT_I2S:
1372 		case SND_SOC_POSSIBLE_DAIFMT_RIGHT_J:
1373 		case SND_SOC_POSSIBLE_DAIFMT_LEFT_J:
1374 		case SND_SOC_POSSIBLE_DAIFMT_DSP_A:
1375 		case SND_SOC_POSSIBLE_DAIFMT_DSP_B:
1376 		case SND_SOC_POSSIBLE_DAIFMT_AC97:
1377 		case SND_SOC_POSSIBLE_DAIFMT_PDM:
1378 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) | i;
1379 			break;
1380 		/*
1381 		 * for clock
1382 		 */
1383 		case SND_SOC_POSSIBLE_DAIFMT_CONT:
1384 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_CONT;
1385 			break;
1386 		case SND_SOC_POSSIBLE_DAIFMT_GATED:
1387 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_GATED;
1388 			break;
1389 		/*
1390 		 * for clock invert
1391 		 */
1392 		case SND_SOC_POSSIBLE_DAIFMT_NB_NF:
1393 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_NF;
1394 			break;
1395 		case SND_SOC_POSSIBLE_DAIFMT_NB_IF:
1396 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_IF;
1397 			break;
1398 		case SND_SOC_POSSIBLE_DAIFMT_IB_NF:
1399 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_NF;
1400 			break;
1401 		case SND_SOC_POSSIBLE_DAIFMT_IB_IF:
1402 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_IF;
1403 			break;
1404 		/*
1405 		 * for clock provider / consumer
1406 		 */
1407 		case SND_SOC_POSSIBLE_DAIFMT_CBP_CFP:
1408 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFP;
1409 			break;
1410 		case SND_SOC_POSSIBLE_DAIFMT_CBC_CFP:
1411 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFP;
1412 			break;
1413 		case SND_SOC_POSSIBLE_DAIFMT_CBP_CFC:
1414 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFC;
1415 			break;
1416 		case SND_SOC_POSSIBLE_DAIFMT_CBC_CFC:
1417 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFC;
1418 			break;
1419 		}
1420 	}
1421 
1422 	/*
1423 	 * Some driver might have very complex limitation.
1424 	 * In such case, user want to auto-select non-limitation part,
1425 	 * and want to manually specify complex part.
1426 	 *
1427 	 * Or for example, if both CPU and Codec can be clock provider,
1428 	 * but because of its quality, user want to specify it manually.
1429 	 *
1430 	 * Use manually specified settings if sound card did.
1431 	 */
1432 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK))
1433 		mask |= SND_SOC_DAIFMT_FORMAT_MASK;
1434 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_MASK))
1435 		mask |= SND_SOC_DAIFMT_CLOCK_MASK;
1436 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_INV_MASK))
1437 		mask |= SND_SOC_DAIFMT_INV_MASK;
1438 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK))
1439 		mask |= SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
1440 
1441 	dai_link->dai_fmt |= (dai_fmt & mask);
1442 }
1443 
1444 /**
1445  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1446  * @rtd: The runtime for which the DAI link format should be changed
1447  * @dai_fmt: The new DAI link format
1448  *
1449  * This function updates the DAI link format for all DAIs connected to the DAI
1450  * link for the specified runtime.
1451  *
1452  * Note: For setups with a static format set the dai_fmt field in the
1453  * corresponding snd_dai_link struct instead of using this function.
1454  *
1455  * Returns 0 on success, otherwise a negative error code.
1456  */
1457 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1458 				unsigned int dai_fmt)
1459 {
1460 	struct snd_soc_dai *cpu_dai;
1461 	struct snd_soc_dai *codec_dai;
1462 	unsigned int ext_fmt;
1463 	unsigned int i;
1464 	int ret;
1465 
1466 	if (!dai_fmt)
1467 		return 0;
1468 
1469 	/*
1470 	 * dai_fmt has 4 types
1471 	 *	1. SND_SOC_DAIFMT_FORMAT_MASK
1472 	 *	2. SND_SOC_DAIFMT_CLOCK
1473 	 *	3. SND_SOC_DAIFMT_INV
1474 	 *	4. SND_SOC_DAIFMT_CLOCK_PROVIDER
1475 	 *
1476 	 * 4. CLOCK_PROVIDER is set from Codec perspective in dai_fmt. So it will be flipped
1477 	 * when this function calls set_fmt() for CPU (CBx_CFx -> Bx_Cx). see below.
1478 	 * This mean, we can't set CPU/Codec both are clock consumer for example.
1479 	 * New idea handles 4. in each dai->ext_fmt. It can keep compatibility.
1480 	 *
1481 	 * Legacy
1482 	 *	dai_fmt  includes 1, 2, 3, 4
1483 	 *
1484 	 * New idea
1485 	 *	dai_fmt  includes 1, 2, 3
1486 	 *	ext_fmt  includes 4
1487 	 */
1488 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
1489 		ext_fmt = rtd->dai_link->codecs[i].ext_fmt;
1490 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt | ext_fmt);
1491 		if (ret != 0 && ret != -ENOTSUPP)
1492 			return ret;
1493 	}
1494 
1495 	/* Flip the polarity for the "CPU" end of link */
1496 	/* Will effect only for 4. SND_SOC_DAIFMT_CLOCK_PROVIDER */
1497 	dai_fmt = snd_soc_daifmt_clock_provider_flipped(dai_fmt);
1498 
1499 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1500 		ext_fmt = rtd->dai_link->cpus[i].ext_fmt;
1501 		ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt | ext_fmt);
1502 		if (ret != 0 && ret != -ENOTSUPP)
1503 			return ret;
1504 	}
1505 
1506 	return 0;
1507 }
1508 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1509 
1510 static int soc_init_pcm_runtime(struct snd_soc_card *card,
1511 				struct snd_soc_pcm_runtime *rtd)
1512 {
1513 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1514 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
1515 	int ret;
1516 
1517 	/* do machine specific initialization */
1518 	ret = snd_soc_link_init(rtd);
1519 	if (ret < 0)
1520 		return ret;
1521 
1522 	snd_soc_runtime_get_dai_fmt(rtd);
1523 	ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1524 	if (ret)
1525 		goto err;
1526 
1527 	/* add DPCM sysfs entries */
1528 	soc_dpcm_debugfs_add(rtd);
1529 
1530 	/* create compress_device if possible */
1531 	ret = snd_soc_dai_compress_new(cpu_dai, rtd);
1532 	if (ret != -ENOTSUPP)
1533 		goto err;
1534 
1535 	/* create the pcm */
1536 	ret = soc_new_pcm(rtd);
1537 	if (ret < 0) {
1538 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1539 			dai_link->stream_name, ret);
1540 		goto err;
1541 	}
1542 
1543 	ret = snd_soc_pcm_dai_new(rtd);
1544 	if (ret < 0)
1545 		goto err;
1546 
1547 	rtd->initialized = true;
1548 
1549 	return 0;
1550 err:
1551 	snd_soc_link_exit(rtd);
1552 	return ret;
1553 }
1554 
1555 static void soc_set_name_prefix(struct snd_soc_card *card,
1556 				struct snd_soc_component *component)
1557 {
1558 	struct device_node *of_node = soc_component_to_node(component);
1559 	const char *str;
1560 	int ret, i;
1561 
1562 	for (i = 0; i < card->num_configs; i++) {
1563 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1564 
1565 		if (snd_soc_is_matching_component(&map->dlc, component) &&
1566 		    map->name_prefix) {
1567 			component->name_prefix = map->name_prefix;
1568 			return;
1569 		}
1570 	}
1571 
1572 	/*
1573 	 * If there is no configuration table or no match in the table,
1574 	 * check if a prefix is provided in the node
1575 	 */
1576 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1577 	if (ret < 0)
1578 		return;
1579 
1580 	component->name_prefix = str;
1581 }
1582 
1583 static void soc_remove_component(struct snd_soc_component *component,
1584 				 int probed)
1585 {
1586 
1587 	if (!component->card)
1588 		return;
1589 
1590 	if (probed)
1591 		snd_soc_component_remove(component);
1592 
1593 	list_del_init(&component->card_list);
1594 	snd_soc_dapm_free(snd_soc_component_to_dapm(component));
1595 	soc_cleanup_component_debugfs(component);
1596 	component->card = NULL;
1597 	snd_soc_component_module_put_when_remove(component);
1598 }
1599 
1600 static int soc_probe_component(struct snd_soc_card *card,
1601 			       struct snd_soc_component *component)
1602 {
1603 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1604 	struct snd_soc_dai *dai;
1605 	int probed = 0;
1606 	int ret;
1607 
1608 	if (snd_soc_component_is_dummy(component))
1609 		return 0;
1610 
1611 	if (component->card) {
1612 		if (component->card != card) {
1613 			dev_err(component->dev,
1614 				"Trying to bind component \"%s\" to card \"%s\" but is already bound to card \"%s\"\n",
1615 				component->name, card->name, component->card->name);
1616 			return -ENODEV;
1617 		}
1618 		return 0;
1619 	}
1620 
1621 	ret = snd_soc_component_module_get_when_probe(component);
1622 	if (ret < 0)
1623 		return ret;
1624 
1625 	component->card = card;
1626 	soc_set_name_prefix(card, component);
1627 
1628 	soc_init_component_debugfs(component);
1629 
1630 	snd_soc_dapm_init(dapm, card, component);
1631 
1632 	ret = snd_soc_dapm_new_controls(dapm,
1633 					component->driver->dapm_widgets,
1634 					component->driver->num_dapm_widgets);
1635 
1636 	if (ret != 0) {
1637 		dev_err(component->dev,
1638 			"Failed to create new controls %d\n", ret);
1639 		goto err_probe;
1640 	}
1641 
1642 	for_each_component_dais(component, dai) {
1643 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1644 		if (ret != 0) {
1645 			dev_err(component->dev,
1646 				"Failed to create DAI widgets %d\n", ret);
1647 			goto err_probe;
1648 		}
1649 	}
1650 
1651 	ret = snd_soc_component_probe(component);
1652 	if (ret < 0)
1653 		goto err_probe;
1654 
1655 	WARN(!snd_soc_dapm_get_idle_bias(dapm) &&
1656 	     snd_soc_dapm_get_bias_level(dapm) != SND_SOC_BIAS_OFF,
1657 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1658 	     component->name);
1659 	probed = 1;
1660 
1661 	/*
1662 	 * machine specific init
1663 	 * see
1664 	 *	snd_soc_component_set_aux()
1665 	 */
1666 	ret = snd_soc_component_init(component);
1667 	if (ret < 0)
1668 		goto err_probe;
1669 
1670 	ret = snd_soc_add_component_controls(component,
1671 					     component->driver->controls,
1672 					     component->driver->num_controls);
1673 	if (ret < 0)
1674 		goto err_probe;
1675 
1676 	ret = snd_soc_dapm_add_routes(dapm,
1677 				      component->driver->dapm_routes,
1678 				      component->driver->num_dapm_routes);
1679 	if (ret < 0)
1680 		goto err_probe;
1681 
1682 	/* see for_each_card_components */
1683 	list_add(&component->card_list, &card->component_dev_list);
1684 
1685 err_probe:
1686 	if (ret < 0)
1687 		soc_remove_component(component, probed);
1688 
1689 	return ret;
1690 }
1691 
1692 static void soc_remove_link_dais(struct snd_soc_card *card)
1693 {
1694 	struct snd_soc_pcm_runtime *rtd;
1695 	int order;
1696 
1697 	for_each_comp_order(order) {
1698 		for_each_card_rtds(card, rtd) {
1699 			/* remove all rtd connected DAIs in good order */
1700 			snd_soc_pcm_dai_remove(rtd, order);
1701 		}
1702 	}
1703 }
1704 
1705 static int soc_probe_link_dais(struct snd_soc_card *card)
1706 {
1707 	struct snd_soc_pcm_runtime *rtd;
1708 	int order, ret;
1709 
1710 	for_each_comp_order(order) {
1711 		for_each_card_rtds(card, rtd) {
1712 			/* probe all rtd connected DAIs in good order */
1713 			ret = snd_soc_pcm_dai_probe(rtd, order);
1714 			if (ret)
1715 				return ret;
1716 		}
1717 	}
1718 
1719 	return 0;
1720 }
1721 
1722 static void soc_remove_link_components(struct snd_soc_card *card)
1723 {
1724 	struct snd_soc_component *component;
1725 	struct snd_soc_pcm_runtime *rtd;
1726 	int i, order;
1727 
1728 	for_each_comp_order(order) {
1729 		for_each_card_rtds(card, rtd) {
1730 			for_each_rtd_components(rtd, i, component) {
1731 				if (component->driver->remove_order != order)
1732 					continue;
1733 
1734 				soc_remove_component(component, 1);
1735 			}
1736 		}
1737 	}
1738 }
1739 
1740 static int soc_probe_link_components(struct snd_soc_card *card)
1741 {
1742 	struct snd_soc_component *component;
1743 	struct snd_soc_pcm_runtime *rtd;
1744 	int i, ret, order;
1745 
1746 	for_each_comp_order(order) {
1747 		for_each_card_rtds(card, rtd) {
1748 			for_each_rtd_components(rtd, i, component) {
1749 				if (component->driver->probe_order != order)
1750 					continue;
1751 
1752 				ret = soc_probe_component(card, component);
1753 				if (ret < 0)
1754 					return ret;
1755 			}
1756 		}
1757 	}
1758 
1759 	return 0;
1760 }
1761 
1762 static void soc_unbind_aux_dev(struct snd_soc_card *card)
1763 {
1764 	struct snd_soc_component *component, *_component;
1765 
1766 	for_each_card_auxs_safe(card, component, _component) {
1767 		/* for snd_soc_component_init() */
1768 		snd_soc_component_set_aux(component, NULL);
1769 		list_del(&component->card_aux_list);
1770 	}
1771 }
1772 
1773 static int soc_bind_aux_dev(struct snd_soc_card *card)
1774 {
1775 	struct snd_soc_component *component;
1776 	struct snd_soc_aux_dev *aux;
1777 	int i;
1778 
1779 	for_each_card_pre_auxs(card, i, aux) {
1780 		/* codecs, usually analog devices */
1781 		component = soc_find_component(&aux->dlc);
1782 		if (!component)
1783 			return -EPROBE_DEFER;
1784 
1785 		/* for snd_soc_component_init() */
1786 		snd_soc_component_set_aux(component, aux);
1787 		/* see for_each_card_auxs */
1788 		list_add(&component->card_aux_list, &card->aux_comp_list);
1789 	}
1790 	return 0;
1791 }
1792 
1793 static int soc_probe_aux_devices(struct snd_soc_card *card)
1794 {
1795 	struct snd_soc_component *component;
1796 	int order;
1797 	int ret;
1798 
1799 	for_each_comp_order(order) {
1800 		for_each_card_auxs(card, component) {
1801 			if (component->driver->probe_order != order)
1802 				continue;
1803 
1804 			ret = soc_probe_component(card,	component);
1805 			if (ret < 0)
1806 				return ret;
1807 		}
1808 	}
1809 
1810 	return 0;
1811 }
1812 
1813 static void soc_remove_aux_devices(struct snd_soc_card *card)
1814 {
1815 	struct snd_soc_component *comp, *_comp;
1816 	int order;
1817 
1818 	for_each_comp_order(order) {
1819 		for_each_card_auxs_safe(card, comp, _comp) {
1820 			if (comp->driver->remove_order == order)
1821 				soc_remove_component(comp, 1);
1822 		}
1823 	}
1824 }
1825 
1826 #ifdef CONFIG_DMI
1827 /*
1828  * If a DMI filed contain strings in this blacklist (e.g.
1829  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1830  * as invalid and dropped when setting the card long name from DMI info.
1831  */
1832 static const char * const dmi_blacklist[] = {
1833 	"To be filled by OEM",
1834 	"TBD by OEM",
1835 	"Default String",
1836 	"Board Manufacturer",
1837 	"Board Vendor Name",
1838 	"Board Product Name",
1839 	NULL,	/* terminator */
1840 };
1841 
1842 /*
1843  * Trim special characters, and replace '-' with '_' since '-' is used to
1844  * separate different DMI fields in the card long name. Only number and
1845  * alphabet characters and a few separator characters are kept.
1846  */
1847 static void cleanup_dmi_name(char *name)
1848 {
1849 	int i, j = 0;
1850 
1851 	for (i = 0; name[i]; i++) {
1852 		if (isalnum(name[i]) || (name[i] == '.')
1853 		    || (name[i] == '_'))
1854 			name[j++] = name[i];
1855 		else if (name[i] == '-')
1856 			name[j++] = '_';
1857 	}
1858 
1859 	name[j] = '\0';
1860 }
1861 
1862 /*
1863  * Check if a DMI field is valid, i.e. not containing any string
1864  * in the black list and not the empty string.
1865  */
1866 static int is_dmi_valid(const char *field)
1867 {
1868 	int i = 0;
1869 
1870 	if (!field[0])
1871 		return 0;
1872 
1873 	while (dmi_blacklist[i]) {
1874 		if (strstr(field, dmi_blacklist[i]))
1875 			return 0;
1876 		i++;
1877 	}
1878 
1879 	return 1;
1880 }
1881 
1882 /*
1883  * Append a string to card->dmi_longname with character cleanups.
1884  */
1885 static void append_dmi_string(struct snd_soc_card *card, const char *str)
1886 {
1887 	char *dst = card->dmi_longname;
1888 	size_t dst_len = sizeof(card->dmi_longname);
1889 	size_t len;
1890 
1891 	len = strlen(dst);
1892 	snprintf(dst + len, dst_len - len, "-%s", str);
1893 
1894 	len++;	/* skip the separator "-" */
1895 	if (len < dst_len)
1896 		cleanup_dmi_name(dst + len);
1897 }
1898 
1899 /**
1900  * snd_soc_set_dmi_name() - Register DMI names to card
1901  * @card: The card to register DMI names
1902  *
1903  * An Intel machine driver may be used by many different devices but are
1904  * difficult for userspace to differentiate, since machine drivers usually
1905  * use their own name as the card short name and leave the card long name
1906  * blank. To differentiate such devices and fix bugs due to lack of
1907  * device-specific configurations, this function allows DMI info to be used
1908  * as the sound card long name, in the format of
1909  * "vendor-product-version-board"
1910  * (Character '-' is used to separate different DMI fields here).
1911  * This will help the user space to load the device-specific Use Case Manager
1912  * (UCM) configurations for the card.
1913  *
1914  * Possible card long names may be:
1915  * DellInc.-XPS139343-01-0310JH
1916  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1917  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1918  *
1919  * This function also supports flavoring the card longname to provide
1920  * the extra differentiation, like "vendor-product-version-board-flavor".
1921  *
1922  * We only keep number and alphabet characters and a few separator characters
1923  * in the card long name since UCM in the user space uses the card long names
1924  * as card configuration directory names and AudoConf cannot support special
1925  * characters like SPACE.
1926  *
1927  * Returns 0 on success, otherwise a negative error code.
1928  */
1929 static int snd_soc_set_dmi_name(struct snd_soc_card *card)
1930 {
1931 	const char *vendor, *product, *board;
1932 
1933 	if (card->long_name)
1934 		return 0; /* long name already set by driver or from DMI */
1935 
1936 	if (!dmi_available)
1937 		return 0;
1938 
1939 	/* make up dmi long name as: vendor-product-version-board */
1940 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1941 	if (!vendor || !is_dmi_valid(vendor)) {
1942 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1943 		return 0;
1944 	}
1945 
1946 	snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1947 	cleanup_dmi_name(card->dmi_longname);
1948 
1949 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
1950 	if (product && is_dmi_valid(product)) {
1951 		const char *product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1952 
1953 		append_dmi_string(card, product);
1954 
1955 		/*
1956 		 * some vendors like Lenovo may only put a self-explanatory
1957 		 * name in the product version field
1958 		 */
1959 		if (product_version && is_dmi_valid(product_version))
1960 			append_dmi_string(card, product_version);
1961 	}
1962 
1963 	board = dmi_get_system_info(DMI_BOARD_NAME);
1964 	if (board && is_dmi_valid(board)) {
1965 		if (!product || strcasecmp(board, product))
1966 			append_dmi_string(card, board);
1967 	} else if (!product) {
1968 		/* fall back to using legacy name */
1969 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1970 		return 0;
1971 	}
1972 
1973 	/* set the card long name */
1974 	card->long_name = card->dmi_longname;
1975 
1976 	return 0;
1977 }
1978 #else
1979 static inline int snd_soc_set_dmi_name(struct snd_soc_card *card)
1980 {
1981 	return 0;
1982 }
1983 #endif /* CONFIG_DMI */
1984 
1985 static void soc_check_tplg_fes(struct snd_soc_card *card)
1986 {
1987 	struct snd_soc_component *component;
1988 	struct snd_soc_dai_link *dai_link;
1989 	int i;
1990 
1991 	for_each_component(component) {
1992 
1993 		/* does this component override BEs ? */
1994 		if (!component->driver->ignore_machine)
1995 			continue;
1996 
1997 		/* for this machine ? */
1998 		if (!strcmp(component->driver->ignore_machine,
1999 			    card->dev->driver->name))
2000 			goto match;
2001 		if (strcmp(component->driver->ignore_machine,
2002 			   dev_name(card->dev)))
2003 			continue;
2004 match:
2005 		/* machine matches, so override the rtd data */
2006 		for_each_card_prelinks(card, i, dai_link) {
2007 
2008 			/* ignore this FE */
2009 			if (dai_link->dynamic) {
2010 				dai_link->ignore = true;
2011 				continue;
2012 			}
2013 
2014 			dev_dbg(card->dev, "info: override BE DAI link %s\n",
2015 				card->dai_link[i].name);
2016 
2017 			/* override platform component */
2018 			if (!dai_link->platforms) {
2019 				dev_err(card->dev, "init platform error");
2020 				continue;
2021 			}
2022 
2023 			if (component->dev->of_node)
2024 				dai_link->platforms->of_node = component->dev->of_node;
2025 			else
2026 				dai_link->platforms->name = component->name;
2027 
2028 			/* convert non BE into BE */
2029 			dai_link->no_pcm = 1;
2030 
2031 			/*
2032 			 * override any BE fixups
2033 			 * see
2034 			 *	snd_soc_link_be_hw_params_fixup()
2035 			 */
2036 			dai_link->be_hw_params_fixup =
2037 				component->driver->be_hw_params_fixup;
2038 
2039 			/*
2040 			 * most BE links don't set stream name, so set it to
2041 			 * dai link name if it's NULL to help bind widgets.
2042 			 */
2043 			if (!dai_link->stream_name)
2044 				dai_link->stream_name = dai_link->name;
2045 		}
2046 
2047 		/* Inform userspace we are using alternate topology */
2048 		snd_soc_card_set_topology_name(card, component->driver->topology_name_prefix);
2049 	}
2050 }
2051 
2052 #define soc_setup_card_name(card, name, name1, name2) \
2053 	__soc_setup_card_name(card, name, sizeof(name), name1, name2)
2054 static void __soc_setup_card_name(struct snd_soc_card *card,
2055 				  char *name, int len,
2056 				  const char *name1, const char *name2)
2057 {
2058 	const char *src = name1 ? name1 : name2;
2059 	int i;
2060 
2061 	snprintf(name, len, "%s", src);
2062 
2063 	if (name != card->snd_card->driver)
2064 		return;
2065 
2066 	/*
2067 	 * Name normalization (driver field)
2068 	 *
2069 	 * The driver name is somewhat special, as it's used as a key for
2070 	 * searches in the user-space.
2071 	 *
2072 	 * ex)
2073 	 *	"abcd??efg" -> "abcd__efg"
2074 	 */
2075 	for (i = 0; i < len; i++) {
2076 		switch (name[i]) {
2077 		case '_':
2078 		case '-':
2079 		case '\0':
2080 			break;
2081 		default:
2082 			if (!isalnum(name[i]))
2083 				name[i] = '_';
2084 			break;
2085 		}
2086 	}
2087 
2088 	/*
2089 	 * The driver field should contain a valid string from the user view.
2090 	 * The wrapping usually does not work so well here. Set a smaller string
2091 	 * in the specific ASoC driver.
2092 	 */
2093 	if (strlen(src) > len - 1)
2094 		dev_err(card->dev, "ASoC: driver name too long '%s' -> '%s'\n", src, name);
2095 }
2096 
2097 static void soc_cleanup_card_resources(struct snd_soc_card *card)
2098 {
2099 	struct snd_soc_pcm_runtime *rtd, *n;
2100 
2101 	if (card->snd_card)
2102 		snd_card_disconnect_sync(card->snd_card);
2103 
2104 	snd_soc_dapm_shutdown(card);
2105 
2106 	/* release machine specific resources */
2107 	for_each_card_rtds(card, rtd)
2108 		if (rtd->initialized)
2109 			snd_soc_link_exit(rtd);
2110 	/* flush delayed work before removing DAIs and DAPM widgets */
2111 	snd_soc_flush_all_delayed_work(card);
2112 
2113 	/* remove and free each DAI */
2114 	soc_remove_link_dais(card);
2115 	soc_remove_link_components(card);
2116 
2117 	for_each_card_rtds_safe(card, rtd, n)
2118 		snd_soc_remove_pcm_runtime(card, rtd);
2119 
2120 	/* remove auxiliary devices */
2121 	soc_remove_aux_devices(card);
2122 	soc_unbind_aux_dev(card);
2123 
2124 	snd_soc_dapm_free(snd_soc_card_to_dapm(card));
2125 	soc_cleanup_card_debugfs(card);
2126 
2127 	/* remove the card */
2128 	snd_soc_card_remove(card);
2129 
2130 	if (card->snd_card) {
2131 		snd_card_free(card->snd_card);
2132 		card->snd_card = NULL;
2133 	}
2134 }
2135 
2136 static void snd_soc_unbind_card(struct snd_soc_card *card)
2137 {
2138 	if (snd_soc_card_is_instantiated(card)) {
2139 		card->instantiated = false;
2140 		soc_cleanup_card_resources(card);
2141 	}
2142 }
2143 
2144 static int snd_soc_bind_card(struct snd_soc_card *card)
2145 {
2146 	struct snd_soc_pcm_runtime *rtd;
2147 	struct snd_soc_component *component;
2148 	struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
2149 	int ret;
2150 
2151 	snd_soc_card_mutex_lock_root(card);
2152 	snd_soc_fill_dummy_dai(card);
2153 
2154 	snd_soc_dapm_init(dapm, card, NULL);
2155 	list_del_init(&card->list);
2156 
2157 	/* check whether any platform is ignore machine FE and using topology */
2158 	soc_check_tplg_fes(card);
2159 
2160 	/* bind aux_devs too */
2161 	ret = soc_bind_aux_dev(card);
2162 	if (ret < 0)
2163 		goto probe_end;
2164 
2165 	/* add predefined DAI links to the list */
2166 	card->num_rtd = 0;
2167 	ret = snd_soc_add_pcm_runtimes(card, card->dai_link, card->num_links);
2168 	if (ret < 0)
2169 		goto probe_end;
2170 
2171 	/* card bind complete so register a sound card */
2172 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2173 			card->owner, 0, &card->snd_card);
2174 	if (ret < 0) {
2175 		dev_err(card->dev,
2176 			"ASoC: can't create sound card for card %s: %d\n",
2177 			card->name, ret);
2178 		goto probe_end;
2179 	}
2180 
2181 	soc_init_card_debugfs(card);
2182 
2183 	soc_resume_init(card);
2184 
2185 	ret = snd_soc_dapm_new_controls(dapm, card->dapm_widgets,
2186 					card->num_dapm_widgets);
2187 	if (ret < 0)
2188 		goto probe_end;
2189 
2190 	ret = snd_soc_dapm_new_controls(dapm, card->of_dapm_widgets,
2191 					card->num_of_dapm_widgets);
2192 	if (ret < 0)
2193 		goto probe_end;
2194 
2195 	/* initialise the sound card only once */
2196 	ret = snd_soc_card_probe(card);
2197 	if (ret < 0)
2198 		goto probe_end;
2199 
2200 	/* probe all components used by DAI links on this card */
2201 	ret = soc_probe_link_components(card);
2202 	if (ret < 0) {
2203 		if (ret != -EPROBE_DEFER) {
2204 			dev_err(card->dev,
2205 				"ASoC: failed to instantiate card %d\n", ret);
2206 		}
2207 		goto probe_end;
2208 	}
2209 
2210 	/* probe auxiliary components */
2211 	ret = soc_probe_aux_devices(card);
2212 	if (ret < 0) {
2213 		dev_err(card->dev,
2214 			"ASoC: failed to probe aux component %d\n", ret);
2215 		goto probe_end;
2216 	}
2217 
2218 	/* probe all DAI links on this card */
2219 	ret = soc_probe_link_dais(card);
2220 	if (ret < 0) {
2221 		dev_err(card->dev,
2222 			"ASoC: failed to instantiate card %d\n", ret);
2223 		goto probe_end;
2224 	}
2225 
2226 	for_each_card_rtds(card, rtd) {
2227 		ret = soc_init_pcm_runtime(card, rtd);
2228 		if (ret < 0)
2229 			goto probe_end;
2230 	}
2231 
2232 	snd_soc_dapm_link_dai_widgets(card);
2233 	snd_soc_dapm_connect_dai_link_widgets(card);
2234 
2235 	ret = snd_soc_add_card_controls(card, card->controls,
2236 					card->num_controls);
2237 	if (ret < 0)
2238 		goto probe_end;
2239 
2240 	ret = snd_soc_dapm_add_routes(dapm, card->dapm_routes,
2241 				      card->num_dapm_routes);
2242 	if (ret < 0)
2243 		goto probe_end;
2244 
2245 	ret = snd_soc_dapm_add_routes(dapm, card->of_dapm_routes,
2246 				      card->num_of_dapm_routes);
2247 	if (ret < 0)
2248 		goto probe_end;
2249 
2250 	/* try to set some sane longname if DMI is available */
2251 	snd_soc_set_dmi_name(card);
2252 
2253 	soc_setup_card_name(card, card->snd_card->shortname,
2254 			    card->name, NULL);
2255 	soc_setup_card_name(card, card->snd_card->longname,
2256 			    card->long_name, card->name);
2257 	soc_setup_card_name(card, card->snd_card->driver,
2258 			    card->driver_name, card->name);
2259 
2260 	if (card->components) {
2261 		/* the current implementation of snd_component_add() accepts */
2262 		/* multiple components in the string separated by space, */
2263 		/* but the string collision (identical string) check might */
2264 		/* not work correctly */
2265 		ret = snd_component_add(card->snd_card, card->components);
2266 		if (ret < 0) {
2267 			dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
2268 				card->name, ret);
2269 			goto probe_end;
2270 		}
2271 	}
2272 
2273 	ret = snd_soc_card_late_probe(card);
2274 	if (ret < 0)
2275 		goto probe_end;
2276 
2277 	ret = snd_soc_dapm_ignore_suspend_widgets(card);
2278 	if (ret < 0)
2279 		goto probe_end;
2280 
2281 	snd_soc_dapm_new_widgets(card);
2282 	snd_soc_card_fixup_controls(card);
2283 
2284 	ret = snd_card_register(card->snd_card);
2285 	if (ret < 0) {
2286 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2287 				ret);
2288 		goto probe_end;
2289 	}
2290 
2291 	card->instantiated = 1;
2292 	snd_soc_dapm_mark_endpoints_dirty(card);
2293 	snd_soc_dapm_sync(dapm);
2294 
2295 	/* deactivate pins to sleep state */
2296 	for_each_card_components(card, component)
2297 		if (!snd_soc_component_active(component))
2298 			pinctrl_pm_select_sleep_state(component->dev);
2299 
2300 probe_end:
2301 	if (ret < 0)
2302 		soc_cleanup_card_resources(card);
2303 	if (ret == -EPROBE_DEFER) {
2304 		list_add(&card->list, &unbind_card_list);
2305 		ret = 0;
2306 	}
2307 	snd_soc_card_mutex_unlock(card);
2308 
2309 	return ret;
2310 }
2311 
2312 static void devm_card_bind_release(struct device *dev, void *res)
2313 {
2314 	snd_soc_unregister_card(*(struct snd_soc_card **)res);
2315 }
2316 
2317 static int devm_snd_soc_bind_card(struct device *dev, struct snd_soc_card *card)
2318 {
2319 	struct snd_soc_card **ptr;
2320 	int ret;
2321 
2322 	/* The procedure may be called many times during the lifetime of the card. */
2323 	devres_destroy(dev, devm_card_bind_release, NULL, NULL);
2324 
2325 	ptr = devres_alloc(devm_card_bind_release, sizeof(*ptr), GFP_KERNEL);
2326 	if (!ptr)
2327 		return -ENOMEM;
2328 
2329 	ret = snd_soc_bind_card(card);
2330 	if (ret == 0) {
2331 		*ptr = card;
2332 		devres_add(dev, ptr);
2333 	} else {
2334 		devres_free(ptr);
2335 	}
2336 
2337 	return ret;
2338 }
2339 
2340 static int call_soc_bind_card(struct snd_soc_card *card)
2341 {
2342 	if (card->devres_dev)
2343 		return devm_snd_soc_bind_card(card->devres_dev, card);
2344 	return snd_soc_bind_card(card);
2345 }
2346 
2347 /* probes a new socdev */
2348 static int soc_probe(struct platform_device *pdev)
2349 {
2350 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2351 
2352 	/*
2353 	 * no card, so machine driver should be registering card
2354 	 * we should not be here in that case so ret error
2355 	 */
2356 	if (!card)
2357 		return -EINVAL;
2358 
2359 	dev_warn(&pdev->dev,
2360 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2361 		 card->name);
2362 
2363 	/* Bodge while we unpick instantiation */
2364 	card->dev = &pdev->dev;
2365 
2366 	return devm_snd_soc_register_card(&pdev->dev, card);
2367 }
2368 
2369 int snd_soc_poweroff(struct device *dev)
2370 {
2371 	struct snd_soc_card *card = dev_get_drvdata(dev);
2372 	struct snd_soc_component *component;
2373 
2374 	if (!snd_soc_card_is_instantiated(card))
2375 		return 0;
2376 
2377 	/*
2378 	 * Flush out pmdown_time work - we actually do want to run it
2379 	 * now, we're shutting down so no imminent restart.
2380 	 */
2381 	snd_soc_flush_all_delayed_work(card);
2382 
2383 	snd_soc_dapm_shutdown(card);
2384 
2385 	/* deactivate pins to sleep state */
2386 	for_each_card_components(card, component)
2387 		pinctrl_pm_select_sleep_state(component->dev);
2388 
2389 	return 0;
2390 }
2391 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2392 
2393 const struct dev_pm_ops snd_soc_pm_ops = {
2394 	.suspend = snd_soc_suspend,
2395 	.resume = snd_soc_resume,
2396 	.freeze = snd_soc_suspend,
2397 	.thaw = snd_soc_resume,
2398 	.poweroff = snd_soc_poweroff,
2399 	.restore = snd_soc_resume,
2400 };
2401 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2402 
2403 /* ASoC platform driver */
2404 static struct platform_driver soc_driver = {
2405 	.driver		= {
2406 		.name		= "soc-audio",
2407 		.pm		= &snd_soc_pm_ops,
2408 	},
2409 	.probe		= soc_probe,
2410 };
2411 
2412 /**
2413  * snd_soc_cnew - create new control
2414  * @_template: control template
2415  * @data: control private data
2416  * @long_name: control long name
2417  * @prefix: control name prefix
2418  *
2419  * Create a new mixer control from a template control.
2420  *
2421  * Returns 0 for success, else error.
2422  */
2423 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2424 				  void *data, const char *long_name,
2425 				  const char *prefix)
2426 {
2427 	struct snd_kcontrol_new template;
2428 	struct snd_kcontrol *kcontrol;
2429 	char *name = NULL;
2430 
2431 	memcpy(&template, _template, sizeof(template));
2432 	template.index = 0;
2433 
2434 	if (!long_name)
2435 		long_name = template.name;
2436 
2437 	if (prefix) {
2438 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2439 		if (!name)
2440 			return NULL;
2441 
2442 		template.name = name;
2443 	} else {
2444 		template.name = long_name;
2445 	}
2446 
2447 	kcontrol = snd_ctl_new1(&template, data);
2448 
2449 	kfree(name);
2450 
2451 	return kcontrol;
2452 }
2453 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2454 
2455 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2456 	const struct snd_kcontrol_new *controls, int num_controls,
2457 	const char *prefix, void *data)
2458 {
2459 	int i;
2460 
2461 	for (i = 0; i < num_controls; i++) {
2462 		const struct snd_kcontrol_new *control = &controls[i];
2463 		int err = snd_ctl_add(card, snd_soc_cnew(control, data,
2464 							 control->name, prefix));
2465 		if (err < 0) {
2466 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2467 				control->name, err);
2468 			return err;
2469 		}
2470 	}
2471 
2472 	return 0;
2473 }
2474 
2475 /**
2476  * snd_soc_add_component_controls - Add an array of controls to a component.
2477  *
2478  * @component: Component to add controls to
2479  * @controls: Array of controls to add
2480  * @num_controls: Number of elements in the array
2481  *
2482  * Return: 0 for success, else error.
2483  */
2484 int snd_soc_add_component_controls(struct snd_soc_component *component,
2485 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2486 {
2487 	struct snd_card *card = component->card->snd_card;
2488 
2489 	return snd_soc_add_controls(card, component->dev, controls,
2490 			num_controls, component->name_prefix, component);
2491 }
2492 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2493 
2494 /**
2495  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2496  * Convenience function to add a list of controls.
2497  *
2498  * @soc_card: SoC card to add controls to
2499  * @controls: array of controls to add
2500  * @num_controls: number of elements in the array
2501  *
2502  * Return 0 for success, else error.
2503  */
2504 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2505 	const struct snd_kcontrol_new *controls, int num_controls)
2506 {
2507 	struct snd_card *card = soc_card->snd_card;
2508 
2509 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2510 			NULL, soc_card);
2511 }
2512 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2513 
2514 /**
2515  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2516  * Convenience function to add a list of controls.
2517  *
2518  * @dai: DAI to add controls to
2519  * @controls: array of controls to add
2520  * @num_controls: number of elements in the array
2521  *
2522  * Return 0 for success, else error.
2523  */
2524 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2525 	const struct snd_kcontrol_new *controls, int num_controls)
2526 {
2527 	struct snd_card *card = dai->component->card->snd_card;
2528 
2529 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2530 			NULL, dai);
2531 }
2532 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2533 
2534 /**
2535  * snd_soc_register_card - Register a card with the ASoC core
2536  *
2537  * @card: Card to register
2538  *
2539  */
2540 int snd_soc_register_card(struct snd_soc_card *card)
2541 {
2542 	if (!card->name || !card->dev)
2543 		return -EINVAL;
2544 
2545 	card->dapm = snd_soc_dapm_alloc(card->dev);
2546 	if (!card->dapm)
2547 		return -ENOMEM;
2548 
2549 	dev_set_drvdata(card->dev, card);
2550 
2551 	INIT_LIST_HEAD(&card->widgets);
2552 	INIT_LIST_HEAD(&card->paths);
2553 	INIT_LIST_HEAD(&card->dapm_list);
2554 	INIT_LIST_HEAD(&card->aux_comp_list);
2555 	INIT_LIST_HEAD(&card->component_dev_list);
2556 	INIT_LIST_HEAD(&card->list);
2557 	INIT_LIST_HEAD(&card->rtd_list);
2558 	INIT_LIST_HEAD(&card->dapm_dirty);
2559 
2560 	card->instantiated = 0;
2561 	mutex_init(&card->mutex);
2562 	mutex_init(&card->dapm_mutex);
2563 	mutex_init(&card->pcm_mutex);
2564 
2565 	guard(mutex)(&client_mutex);
2566 
2567 	return call_soc_bind_card(card);
2568 }
2569 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2570 
2571 /**
2572  * snd_soc_unregister_card - Unregister a card with the ASoC core
2573  *
2574  * @card: Card to unregister
2575  *
2576  */
2577 void snd_soc_unregister_card(struct snd_soc_card *card)
2578 {
2579 	guard(mutex)(&client_mutex);
2580 
2581 	snd_soc_unbind_card(card);
2582 	list_del(&card->list);
2583 
2584 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2585 }
2586 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2587 
2588 /*
2589  * Simplify DAI link configuration by removing ".-1" from device names
2590  * and sanitizing names.
2591  */
2592 static char *fmt_single_name(struct device *dev, int *id)
2593 {
2594 	const char *devname = dev_name(dev);
2595 	char *found, *name;
2596 	unsigned int id1, id2;
2597 	int __id;
2598 
2599 	if (devname == NULL)
2600 		return NULL;
2601 
2602 	name = devm_kstrdup(dev, devname, GFP_KERNEL);
2603 	if (!name)
2604 		return NULL;
2605 
2606 	/* are we a "%s.%d" name (platform and SPI components) */
2607 	found = strstr(name, dev->driver->name);
2608 	if (found) {
2609 		/* get ID */
2610 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", &__id) == 1) {
2611 
2612 			/* discard ID from name if ID == -1 */
2613 			if (__id == -1)
2614 				found[strlen(dev->driver->name)] = '\0';
2615 		}
2616 
2617 	/* I2C component devices are named "bus-addr" */
2618 	} else if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2619 
2620 		/* create unique ID number from I2C addr and bus */
2621 		__id = ((id1 & 0xffff) << 16) + id2;
2622 
2623 		devm_kfree(dev, name);
2624 
2625 		/* sanitize component name for DAI link creation */
2626 		name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname);
2627 	} else {
2628 		__id = 0;
2629 	}
2630 
2631 	if (id)
2632 		*id = __id;
2633 
2634 	return name;
2635 }
2636 
2637 /*
2638  * Simplify DAI link naming for single devices with multiple DAIs by removing
2639  * any ".-1" and using the DAI name (instead of device name).
2640  */
2641 static inline char *fmt_multiple_name(struct device *dev,
2642 		struct snd_soc_dai_driver *dai_drv)
2643 {
2644 	if (dai_drv->name == NULL) {
2645 		dev_err(dev,
2646 			"ASoC: error - multiple DAI %s registered with no name\n",
2647 			dev_name(dev));
2648 		return NULL;
2649 	}
2650 
2651 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2652 }
2653 
2654 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2655 {
2656 	lockdep_assert_held(&client_mutex);
2657 
2658 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2659 	list_del(&dai->list);
2660 }
2661 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2662 
2663 /**
2664  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2665  *
2666  * @component: The component the DAIs are registered for
2667  * @dai_drv: DAI driver to use for the DAI
2668  * @legacy_dai_naming: if %true, use legacy single-name format;
2669  * 	if %false, use multiple-name format;
2670  *
2671  * Topology can use this API to register DAIs when probing a component.
2672  * These DAIs's widgets will be freed in the card cleanup and the DAIs
2673  * will be freed in the component cleanup.
2674  */
2675 struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2676 					 struct snd_soc_dai_driver *dai_drv,
2677 					 bool legacy_dai_naming)
2678 {
2679 	struct device *dev = component->dev;
2680 	struct snd_soc_dai *dai;
2681 
2682 	lockdep_assert_held(&client_mutex);
2683 
2684 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2685 	if (dai == NULL)
2686 		return NULL;
2687 
2688 	/*
2689 	 * Back in the old days when we still had component-less DAIs,
2690 	 * instead of having a static name, component-less DAIs would
2691 	 * inherit the name of the parent device so it is possible to
2692 	 * register multiple instances of the DAI. We still need to keep
2693 	 * the same naming style even though those DAIs are not
2694 	 * component-less anymore.
2695 	 */
2696 	if (legacy_dai_naming &&
2697 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
2698 		dai->name = fmt_single_name(dev, &dai->id);
2699 	} else {
2700 		dai->name = fmt_multiple_name(dev, dai_drv);
2701 		if (dai_drv->id)
2702 			dai->id = dai_drv->id;
2703 		else
2704 			dai->id = component->num_dai;
2705 	}
2706 	if (!dai->name)
2707 		return NULL;
2708 
2709 	dai->component = component;
2710 	dai->dev = dev;
2711 	dai->driver = dai_drv;
2712 
2713 	/* see for_each_component_dais */
2714 	list_add_tail(&dai->list, &component->dai_list);
2715 	component->num_dai++;
2716 
2717 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2718 	return dai;
2719 }
2720 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2721 
2722 /**
2723  * snd_soc_unregister_dais - Unregister DAIs from the ASoC core
2724  *
2725  * @component: The component for which the DAIs should be unregistered
2726  */
2727 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2728 {
2729 	struct snd_soc_dai *dai, *_dai;
2730 
2731 	for_each_component_dais_safe(component, dai, _dai)
2732 		snd_soc_unregister_dai(dai);
2733 }
2734 
2735 /**
2736  * snd_soc_register_dais - Register a DAI with the ASoC core
2737  *
2738  * @component: The component the DAIs are registered for
2739  * @dai_drv: DAI driver to use for the DAIs
2740  * @count: Number of DAIs
2741  */
2742 static int snd_soc_register_dais(struct snd_soc_component *component,
2743 				 struct snd_soc_dai_driver *dai_drv,
2744 				 size_t count)
2745 {
2746 	struct snd_soc_dai *dai;
2747 	unsigned int i;
2748 	int ret;
2749 
2750 	for (i = 0; i < count; i++) {
2751 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2752 					   component->driver->legacy_dai_naming);
2753 		if (dai == NULL) {
2754 			ret = -ENOMEM;
2755 			goto err;
2756 		}
2757 	}
2758 
2759 	return 0;
2760 
2761 err:
2762 	snd_soc_unregister_dais(component);
2763 
2764 	return ret;
2765 }
2766 
2767 #define ENDIANNESS_MAP(name) \
2768 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2769 static u64 endianness_format_map[] = {
2770 	ENDIANNESS_MAP(S16_),
2771 	ENDIANNESS_MAP(U16_),
2772 	ENDIANNESS_MAP(S24_),
2773 	ENDIANNESS_MAP(U24_),
2774 	ENDIANNESS_MAP(S32_),
2775 	ENDIANNESS_MAP(U32_),
2776 	ENDIANNESS_MAP(S24_3),
2777 	ENDIANNESS_MAP(U24_3),
2778 	ENDIANNESS_MAP(S20_3),
2779 	ENDIANNESS_MAP(U20_3),
2780 	ENDIANNESS_MAP(S18_3),
2781 	ENDIANNESS_MAP(U18_3),
2782 	ENDIANNESS_MAP(FLOAT_),
2783 	ENDIANNESS_MAP(FLOAT64_),
2784 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2785 };
2786 
2787 /*
2788  * Fix up the DAI formats for endianness: codecs don't actually see
2789  * the endianness of the data but we're using the CPU format
2790  * definitions which do need to include endianness so we ensure that
2791  * codec DAIs always have both big and little endian variants set.
2792  */
2793 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2794 {
2795 	int i;
2796 
2797 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2798 		if (stream->formats & endianness_format_map[i])
2799 			stream->formats |= endianness_format_map[i];
2800 }
2801 
2802 static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2803 {
2804 	struct snd_soc_card *card = component->card;
2805 	bool instantiated;
2806 
2807 	snd_soc_unregister_dais(component);
2808 
2809 	if (card) {
2810 		instantiated = card->instantiated;
2811 		snd_soc_unbind_card(card);
2812 		if (instantiated)
2813 			list_add(&card->list, &unbind_card_list);
2814 	}
2815 
2816 	list_del(&component->list);
2817 }
2818 
2819 int snd_soc_component_initialize(struct snd_soc_component *component,
2820 				 const struct snd_soc_component_driver *driver,
2821 				 struct device *dev)
2822 {
2823 	component->dapm = snd_soc_dapm_alloc(dev);
2824 	if (!component->dapm)
2825 		return -ENOMEM;
2826 
2827 	INIT_LIST_HEAD(&component->dai_list);
2828 	INIT_LIST_HEAD(&component->dobj_list);
2829 	INIT_LIST_HEAD(&component->card_list);
2830 	INIT_LIST_HEAD(&component->list);
2831 	INIT_LIST_HEAD(&component->card_aux_list);
2832 	mutex_init(&component->io_mutex);
2833 
2834 	if (!component->name) {
2835 		component->name = fmt_single_name(dev, NULL);
2836 		if (!component->name) {
2837 			dev_err(dev, "ASoC: Failed to allocate name\n");
2838 			return -ENOMEM;
2839 		}
2840 	}
2841 
2842 	component->dev		= dev;
2843 	component->driver	= driver;
2844 
2845 #ifdef CONFIG_DEBUG_FS
2846 	if (!component->debugfs_prefix)
2847 		component->debugfs_prefix = driver->debugfs_prefix;
2848 #endif
2849 
2850 	return 0;
2851 }
2852 EXPORT_SYMBOL_GPL(snd_soc_component_initialize);
2853 
2854 int snd_soc_add_component(struct snd_soc_component *component,
2855 			  struct snd_soc_dai_driver *dai_drv,
2856 			  int num_dai)
2857 {
2858 	struct snd_soc_card *card, *c;
2859 	int ret;
2860 	int i;
2861 	guard(mutex)(&client_mutex);
2862 
2863 	if (component->driver->endianness) {
2864 		for (i = 0; i < num_dai; i++) {
2865 			convert_endianness_formats(&dai_drv[i].playback);
2866 			convert_endianness_formats(&dai_drv[i].capture);
2867 		}
2868 	}
2869 
2870 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2871 	if (ret < 0) {
2872 		dev_err(component->dev, "ASoC: Failed to register DAIs: %d\n",
2873 			ret);
2874 		goto err_cleanup;
2875 	}
2876 
2877 	if (!component->driver->write && !component->driver->read) {
2878 		if (!component->regmap)
2879 			component->regmap = dev_get_regmap(component->dev,
2880 							   NULL);
2881 	}
2882 
2883 	/* see for_each_component */
2884 	list_add(&component->list, &component_list);
2885 
2886 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2887 		call_soc_bind_card(card);
2888 
2889 err_cleanup:
2890 	if (ret < 0)
2891 		snd_soc_del_component_unlocked(component);
2892 
2893 	return ret;
2894 }
2895 EXPORT_SYMBOL_GPL(snd_soc_add_component);
2896 
2897 int snd_soc_register_component(struct device *dev,
2898 			const struct snd_soc_component_driver *component_driver,
2899 			struct snd_soc_dai_driver *dai_drv,
2900 			int num_dai)
2901 {
2902 	struct snd_soc_component *component;
2903 	int ret;
2904 
2905 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2906 	if (!component)
2907 		return -ENOMEM;
2908 
2909 	ret = snd_soc_component_initialize(component, component_driver, dev);
2910 	if (ret < 0)
2911 		return ret;
2912 
2913 	return snd_soc_add_component(component, dai_drv, num_dai);
2914 }
2915 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2916 
2917 /**
2918  * snd_soc_unregister_component_by_driver - Unregister component using a given driver
2919  * from the ASoC core
2920  *
2921  * @dev: The device to unregister
2922  * @component_driver: The component driver to unregister
2923  */
2924 void snd_soc_unregister_component_by_driver(struct device *dev,
2925 					    const struct snd_soc_component_driver *component_driver)
2926 {
2927 	const char *driver_name = NULL;
2928 
2929 	if (component_driver)
2930 		driver_name = component_driver->name;
2931 
2932 	guard(mutex)(&client_mutex);
2933 
2934 	while (1) {
2935 		struct snd_soc_component *component = snd_soc_lookup_component_nolocked(dev, driver_name);
2936 
2937 		if (!component)
2938 			break;
2939 
2940 		snd_soc_del_component_unlocked(component);
2941 	}
2942 }
2943 EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver);
2944 
2945 /* Retrieve a card's name from device tree */
2946 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2947 			       const char *propname)
2948 {
2949 	struct device_node *np;
2950 	int ret;
2951 
2952 	if (!card->dev) {
2953 		pr_err("card->dev is not set before calling %s\n", __func__);
2954 		return -EINVAL;
2955 	}
2956 
2957 	np = card->dev->of_node;
2958 
2959 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2960 	/*
2961 	 * EINVAL means the property does not exist. This is fine providing
2962 	 * card->name was previously set, which is checked later in
2963 	 * snd_soc_register_card.
2964 	 */
2965 	if (ret < 0 && ret != -EINVAL) {
2966 		dev_err(card->dev,
2967 			"ASoC: Property '%s' could not be read: %d\n",
2968 			propname, ret);
2969 		return ret;
2970 	}
2971 
2972 	return 0;
2973 }
2974 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2975 
2976 static const struct snd_soc_dapm_widget simple_widgets[] = {
2977 	SND_SOC_DAPM_MIC("Microphone", NULL),
2978 	SND_SOC_DAPM_LINE("Line", NULL),
2979 	SND_SOC_DAPM_HP("Headphone", NULL),
2980 	SND_SOC_DAPM_SPK("Speaker", NULL),
2981 };
2982 
2983 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2984 					  const char *propname)
2985 {
2986 	struct device_node *np = card->dev->of_node;
2987 	struct snd_soc_dapm_widget *widgets;
2988 	const char *template, *wname;
2989 	int i, j, num_widgets;
2990 
2991 	num_widgets = of_property_count_strings(np, propname);
2992 	if (num_widgets < 0) {
2993 		dev_err(card->dev,
2994 			"ASoC: Property '%s' does not exist\n",	propname);
2995 		return -EINVAL;
2996 	}
2997 	if (!num_widgets) {
2998 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2999 			propname);
3000 		return -EINVAL;
3001 	}
3002 	if (num_widgets & 1) {
3003 		dev_err(card->dev,
3004 			"ASoC: Property '%s' length is not even\n", propname);
3005 		return -EINVAL;
3006 	}
3007 
3008 	num_widgets /= 2;
3009 
3010 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3011 			       GFP_KERNEL);
3012 	if (!widgets) {
3013 		dev_err(card->dev,
3014 			"ASoC: Could not allocate memory for widgets\n");
3015 		return -ENOMEM;
3016 	}
3017 
3018 	for (i = 0; i < num_widgets; i++) {
3019 		int ret = of_property_read_string_index(np, propname,
3020 							2 * i, &template);
3021 		if (ret) {
3022 			dev_err(card->dev,
3023 				"ASoC: Property '%s' index %d read error:%d\n",
3024 				propname, 2 * i, ret);
3025 			return -EINVAL;
3026 		}
3027 
3028 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3029 			if (!strncmp(template, simple_widgets[j].name,
3030 				     strlen(simple_widgets[j].name))) {
3031 				widgets[i] = simple_widgets[j];
3032 				break;
3033 			}
3034 		}
3035 
3036 		if (j >= ARRAY_SIZE(simple_widgets)) {
3037 			dev_err(card->dev,
3038 				"ASoC: DAPM widget '%s' is not supported\n",
3039 				template);
3040 			return -EINVAL;
3041 		}
3042 
3043 		ret = of_property_read_string_index(np, propname,
3044 						    (2 * i) + 1,
3045 						    &wname);
3046 		if (ret) {
3047 			dev_err(card->dev,
3048 				"ASoC: Property '%s' index %d read error:%d\n",
3049 				propname, (2 * i) + 1, ret);
3050 			return -EINVAL;
3051 		}
3052 
3053 		widgets[i].name = wname;
3054 	}
3055 
3056 	card->of_dapm_widgets = widgets;
3057 	card->num_of_dapm_widgets = num_widgets;
3058 
3059 	return 0;
3060 }
3061 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3062 
3063 int snd_soc_of_parse_pin_switches(struct snd_soc_card *card, const char *prop)
3064 {
3065 	const unsigned int nb_controls_max = 16;
3066 	const char **strings, *control_name;
3067 	struct snd_kcontrol_new *controls;
3068 	struct device *dev = card->dev;
3069 	unsigned int i, nb_controls;
3070 	int ret;
3071 
3072 	if (!of_property_present(dev->of_node, prop))
3073 		return 0;
3074 
3075 	strings = devm_kcalloc(dev, nb_controls_max,
3076 			       sizeof(*strings), GFP_KERNEL);
3077 	if (!strings)
3078 		return -ENOMEM;
3079 
3080 	ret = of_property_read_string_array(dev->of_node, prop,
3081 					    strings, nb_controls_max);
3082 	if (ret < 0)
3083 		return ret;
3084 
3085 	nb_controls = (unsigned int)ret;
3086 
3087 	controls = devm_kcalloc(dev, nb_controls,
3088 				sizeof(*controls), GFP_KERNEL);
3089 	if (!controls)
3090 		return -ENOMEM;
3091 
3092 	for (i = 0; i < nb_controls; i++) {
3093 		control_name = devm_kasprintf(dev, GFP_KERNEL,
3094 					      "%s Switch", strings[i]);
3095 		if (!control_name)
3096 			return -ENOMEM;
3097 
3098 		controls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
3099 		controls[i].name = control_name;
3100 		controls[i].info = snd_soc_dapm_info_pin_switch;
3101 		controls[i].get = snd_soc_dapm_get_pin_switch;
3102 		controls[i].put = snd_soc_dapm_put_pin_switch;
3103 		controls[i].private_value = (unsigned long)strings[i];
3104 	}
3105 
3106 	card->controls = controls;
3107 	card->num_controls = nb_controls;
3108 
3109 	return 0;
3110 }
3111 EXPORT_SYMBOL_GPL(snd_soc_of_parse_pin_switches);
3112 
3113 int snd_soc_of_get_slot_mask(struct device_node *np,
3114 			     const char *prop_name,
3115 			     unsigned int *mask)
3116 {
3117 	u32 val;
3118 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3119 	int i;
3120 
3121 	if (!of_slot_mask)
3122 		return 0;
3123 	val /= sizeof(u32);
3124 	for (i = 0; i < val; i++)
3125 		if (be32_to_cpup(&of_slot_mask[i]))
3126 			*mask |= (1 << i);
3127 
3128 	return val;
3129 }
3130 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
3131 
3132 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3133 			      unsigned int *tx_mask,
3134 			      unsigned int *rx_mask,
3135 			      unsigned int *slots,
3136 			      unsigned int *slot_width)
3137 {
3138 	u32 val;
3139 	int ret;
3140 
3141 	if (tx_mask)
3142 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3143 	if (rx_mask)
3144 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3145 
3146 	ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3147 	if (ret && ret != -EINVAL)
3148 		return ret;
3149 	if (!ret && slots)
3150 		*slots = val;
3151 
3152 	ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3153 	if (ret && ret != -EINVAL)
3154 		return ret;
3155 	if (!ret && slot_width)
3156 		*slot_width = val;
3157 
3158 	return 0;
3159 }
3160 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3161 
3162 void snd_soc_dlc_use_cpu_as_platform(struct snd_soc_dai_link_component *platforms,
3163 				     struct snd_soc_dai_link_component *cpus)
3164 {
3165 	platforms->of_node	= cpus->of_node;
3166 	platforms->dai_args	= cpus->dai_args;
3167 }
3168 EXPORT_SYMBOL_GPL(snd_soc_dlc_use_cpu_as_platform);
3169 
3170 void snd_soc_of_parse_node_prefix(struct device_node *np,
3171 				  struct snd_soc_codec_conf *codec_conf,
3172 				  struct device_node *of_node,
3173 				  const char *propname)
3174 {
3175 	const char *str;
3176 	int ret;
3177 
3178 	ret = of_property_read_string(np, propname, &str);
3179 	if (ret < 0) {
3180 		/* no prefix is not error */
3181 		return;
3182 	}
3183 
3184 	codec_conf->dlc.of_node	= of_node;
3185 	codec_conf->name_prefix	= str;
3186 }
3187 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
3188 
3189 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3190 				   const char *propname)
3191 {
3192 	struct device_node *np = card->dev->of_node;
3193 	int num_routes;
3194 	struct snd_soc_dapm_route *routes;
3195 	int i;
3196 
3197 	num_routes = of_property_count_strings(np, propname);
3198 	if (num_routes < 0 || num_routes & 1) {
3199 		dev_err(card->dev,
3200 			"ASoC: Property '%s' does not exist or its length is not even\n",
3201 			propname);
3202 		return -EINVAL;
3203 	}
3204 	num_routes /= 2;
3205 
3206 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3207 			      GFP_KERNEL);
3208 	if (!routes) {
3209 		dev_err(card->dev,
3210 			"ASoC: Could not allocate DAPM route table\n");
3211 		return -ENOMEM;
3212 	}
3213 
3214 	for (i = 0; i < num_routes; i++) {
3215 		int ret = of_property_read_string_index(np, propname,
3216 							2 * i, &routes[i].sink);
3217 		if (ret) {
3218 			dev_err(card->dev,
3219 				"ASoC: Property '%s' index %d could not be read: %d\n",
3220 				propname, 2 * i, ret);
3221 			return -EINVAL;
3222 		}
3223 		ret = of_property_read_string_index(np, propname,
3224 			(2 * i) + 1, &routes[i].source);
3225 		if (ret) {
3226 			dev_err(card->dev,
3227 				"ASoC: Property '%s' index %d could not be read: %d\n",
3228 				propname, (2 * i) + 1, ret);
3229 			return -EINVAL;
3230 		}
3231 	}
3232 
3233 	card->num_of_dapm_routes = num_routes;
3234 	card->of_dapm_routes = routes;
3235 
3236 	return 0;
3237 }
3238 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3239 
3240 int snd_soc_of_parse_aux_devs(struct snd_soc_card *card, const char *propname)
3241 {
3242 	struct device_node *node = card->dev->of_node;
3243 	struct snd_soc_aux_dev *aux;
3244 	int num, i;
3245 
3246 	num = of_count_phandle_with_args(node, propname, NULL);
3247 	if (num == -ENOENT) {
3248 		return 0;
3249 	} else if (num < 0) {
3250 		dev_err(card->dev, "ASOC: Property '%s' could not be read: %d\n",
3251 			propname, num);
3252 		return num;
3253 	}
3254 
3255 	aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
3256 	if (!aux)
3257 		return -ENOMEM;
3258 	card->aux_dev = aux;
3259 	card->num_aux_devs = num;
3260 
3261 	for_each_card_pre_auxs(card, i, aux) {
3262 		aux->dlc.of_node = of_parse_phandle(node, propname, i);
3263 		if (!aux->dlc.of_node)
3264 			return -EINVAL;
3265 	}
3266 
3267 	return 0;
3268 }
3269 EXPORT_SYMBOL_GPL(snd_soc_of_parse_aux_devs);
3270 
3271 int snd_soc_of_parse_ignore_suspend_widgets(struct snd_soc_card *card,
3272 					    const char *propname)
3273 {
3274 	struct device_node *np = card->dev->of_node;
3275 	int num_widgets;
3276 	const char **widgets;
3277 	int i;
3278 
3279 	num_widgets = of_property_count_strings(np, propname);
3280 	if (num_widgets < 0) {
3281 		dev_err(card->dev,
3282 			"ASoC: Property '%s' does not exist\n", propname);
3283 		return -EINVAL;
3284 	}
3285 
3286 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(char *), GFP_KERNEL);
3287 	if (!widgets)
3288 		return -ENOMEM;
3289 
3290 	for (i = 0; i < num_widgets; i++) {
3291 		const char *name;
3292 		int ret = of_property_read_string_index(np, propname, i, &name);
3293 
3294 		if (ret) {
3295 			dev_err(card->dev,
3296 				"ASoC: Property '%s' could not be read: %d\n",
3297 				propname, ret);
3298 			return -EINVAL;
3299 		}
3300 		widgets[i] = name;
3301 	}
3302 
3303 	card->num_of_ignore_suspend_widgets = num_widgets;
3304 	card->of_ignore_suspend_widgets = widgets;
3305 
3306 	return 0;
3307 }
3308 EXPORT_SYMBOL_GPL(snd_soc_of_parse_ignore_suspend_widgets);
3309 
3310 unsigned int snd_soc_daifmt_clock_provider_flipped(unsigned int dai_fmt)
3311 {
3312 	unsigned int inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
3313 
3314 	switch (dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
3315 	case SND_SOC_DAIFMT_CBP_CFP:
3316 		inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
3317 		break;
3318 	case SND_SOC_DAIFMT_CBP_CFC:
3319 		inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
3320 		break;
3321 	case SND_SOC_DAIFMT_CBC_CFP:
3322 		inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
3323 		break;
3324 	case SND_SOC_DAIFMT_CBC_CFC:
3325 		inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
3326 		break;
3327 	}
3328 
3329 	return inv_dai_fmt;
3330 }
3331 EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_flipped);
3332 
3333 unsigned int snd_soc_daifmt_clock_provider_from_bitmap(unsigned int bit_frame)
3334 {
3335 	/*
3336 	 * bit_frame is return value from
3337 	 *	snd_soc_daifmt_parse_clock_provider_raw()
3338 	 */
3339 
3340 	/* Codec base */
3341 	switch (bit_frame) {
3342 	case 0x11:
3343 		return SND_SOC_DAIFMT_CBP_CFP;
3344 	case 0x10:
3345 		return SND_SOC_DAIFMT_CBP_CFC;
3346 	case 0x01:
3347 		return SND_SOC_DAIFMT_CBC_CFP;
3348 	default:
3349 		return SND_SOC_DAIFMT_CBC_CFC;
3350 	}
3351 
3352 	return 0;
3353 }
3354 EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_from_bitmap);
3355 
3356 unsigned int snd_soc_daifmt_parse_format(struct device_node *np,
3357 					 const char *prefix)
3358 {
3359 	int ret;
3360 	char prop[128];
3361 	unsigned int format = 0;
3362 	int bit, frame;
3363 	const char *str;
3364 	struct {
3365 		char *name;
3366 		unsigned int val;
3367 	} of_fmt_table[] = {
3368 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3369 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3370 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3371 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3372 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3373 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3374 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3375 		{ "msb",	SND_SOC_DAIFMT_MSB },
3376 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3377 	};
3378 
3379 	if (!prefix)
3380 		prefix = "";
3381 
3382 	/*
3383 	 * check "dai-format = xxx"
3384 	 * or    "[prefix]format = xxx"
3385 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3386 	 */
3387 	ret = of_property_read_string(np, "dai-format", &str);
3388 	if (ret < 0) {
3389 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3390 		ret = of_property_read_string(np, prop, &str);
3391 	}
3392 	if (ret == 0) {
3393 		int i;
3394 
3395 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3396 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3397 				format |= of_fmt_table[i].val;
3398 				break;
3399 			}
3400 		}
3401 	}
3402 
3403 	/*
3404 	 * check "[prefix]continuous-clock"
3405 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3406 	 */
3407 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3408 	if (of_property_read_bool(np, prop))
3409 		format |= SND_SOC_DAIFMT_CONT;
3410 	else
3411 		format |= SND_SOC_DAIFMT_GATED;
3412 
3413 	/*
3414 	 * check "[prefix]bitclock-inversion"
3415 	 * check "[prefix]frame-inversion"
3416 	 * SND_SOC_DAIFMT_INV_MASK area
3417 	 */
3418 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3419 	bit = of_property_read_bool(np, prop);
3420 
3421 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3422 	frame = of_property_read_bool(np, prop);
3423 
3424 	switch ((bit << 4) + frame) {
3425 	case 0x11:
3426 		format |= SND_SOC_DAIFMT_IB_IF;
3427 		break;
3428 	case 0x10:
3429 		format |= SND_SOC_DAIFMT_IB_NF;
3430 		break;
3431 	case 0x01:
3432 		format |= SND_SOC_DAIFMT_NB_IF;
3433 		break;
3434 	default:
3435 		/* SND_SOC_DAIFMT_NB_NF is default */
3436 		break;
3437 	}
3438 
3439 	return format;
3440 }
3441 EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_format);
3442 
3443 unsigned int snd_soc_daifmt_parse_clock_provider_raw(struct device_node *np,
3444 						     const char *prefix,
3445 						     struct device_node **bitclkmaster,
3446 						     struct device_node **framemaster)
3447 {
3448 	char prop[128];
3449 	unsigned int bit, frame;
3450 
3451 	if (!np)
3452 		return 0;
3453 
3454 	if (!prefix)
3455 		prefix = "";
3456 
3457 	/*
3458 	 * check "[prefix]bitclock-master"
3459 	 * check "[prefix]frame-master"
3460 	 */
3461 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3462 	bit = of_property_present(np, prop);
3463 	if (bit && bitclkmaster)
3464 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3465 
3466 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3467 	frame = of_property_present(np, prop);
3468 	if (frame && framemaster)
3469 		*framemaster = of_parse_phandle(np, prop, 0);
3470 
3471 	/*
3472 	 * return bitmap.
3473 	 * It will be parameter of
3474 	 *	snd_soc_daifmt_clock_provider_from_bitmap()
3475 	 */
3476 	return (bit << 4) + frame;
3477 }
3478 EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_clock_provider_raw);
3479 
3480 int snd_soc_get_stream_cpu(const struct snd_soc_dai_link *dai_link, int stream)
3481 {
3482 	/*
3483 	 * [Normal]
3484 	 *
3485 	 * Playback
3486 	 *	CPU  : SNDRV_PCM_STREAM_PLAYBACK
3487 	 *	Codec: SNDRV_PCM_STREAM_PLAYBACK
3488 	 *
3489 	 * Capture
3490 	 *	CPU  : SNDRV_PCM_STREAM_CAPTURE
3491 	 *	Codec: SNDRV_PCM_STREAM_CAPTURE
3492 	 */
3493 	if (!dai_link->c2c_params)
3494 		return stream;
3495 
3496 	/*
3497 	 * [Codec2Codec]
3498 	 *
3499 	 * Playback
3500 	 *	CPU  : SNDRV_PCM_STREAM_CAPTURE
3501 	 *	Codec: SNDRV_PCM_STREAM_PLAYBACK
3502 	 *
3503 	 * Capture
3504 	 *	CPU  : SNDRV_PCM_STREAM_PLAYBACK
3505 	 *	Codec: SNDRV_PCM_STREAM_CAPTURE
3506 	 */
3507 	if (stream == SNDRV_PCM_STREAM_CAPTURE)
3508 		return SNDRV_PCM_STREAM_PLAYBACK;
3509 
3510 	return SNDRV_PCM_STREAM_CAPTURE;
3511 }
3512 EXPORT_SYMBOL_GPL(snd_soc_get_stream_cpu);
3513 
3514 int snd_soc_get_dai_id(struct device_node *ep)
3515 {
3516 	struct snd_soc_dai_link_component dlc = {
3517 		.of_node = of_graph_get_port_parent(ep),
3518 	};
3519 	int ret;
3520 
3521 
3522 	/*
3523 	 * For example HDMI case, HDMI has video/sound port,
3524 	 * but ALSA SoC needs sound port number only.
3525 	 * Thus counting HDMI DT port/endpoint doesn't work.
3526 	 * Then, it should have .of_xlate_dai_id
3527 	 */
3528 	ret = -ENOTSUPP;
3529 
3530 	scoped_guard(mutex, &client_mutex) {
3531 		struct snd_soc_component *component = soc_find_component(&dlc);
3532 
3533 		if (component)
3534 			ret = snd_soc_component_of_xlate_dai_id(component, ep);
3535 	}
3536 
3537 	of_node_put(dlc.of_node);
3538 
3539 	return ret;
3540 }
3541 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3542 
3543 int snd_soc_get_dlc(const struct of_phandle_args *args, struct snd_soc_dai_link_component *dlc)
3544 {
3545 	struct snd_soc_component *pos;
3546 	int ret = -EPROBE_DEFER;
3547 	guard(mutex)(&client_mutex);
3548 
3549 	for_each_component(pos) {
3550 		struct device_node *component_of_node = soc_component_to_node(pos);
3551 
3552 		if (component_of_node != args->np || !pos->num_dai)
3553 			continue;
3554 
3555 		ret = snd_soc_component_of_xlate_dai_name(pos, args, &dlc->dai_name);
3556 		if (ret == -ENOTSUPP) {
3557 			struct snd_soc_dai *dai;
3558 			int id = -1;
3559 
3560 			switch (args->args_count) {
3561 			case 0:
3562 				id = 0; /* same as dai_drv[0] */
3563 				break;
3564 			case 1:
3565 				id = args->args[0];
3566 				break;
3567 			default:
3568 				/* not supported */
3569 				break;
3570 			}
3571 
3572 			if (id < 0 || id >= pos->num_dai) {
3573 				ret = -EINVAL;
3574 				continue;
3575 			}
3576 
3577 			ret = 0;
3578 
3579 			/* find target DAI */
3580 			for_each_component_dais(pos, dai) {
3581 				if (id == 0)
3582 					break;
3583 				id--;
3584 			}
3585 
3586 			dlc->dai_name	= snd_soc_dai_name_get(dai);
3587 		} else if (ret) {
3588 			/*
3589 			 * if another error than ENOTSUPP is returned go on and
3590 			 * check if another component is provided with the same
3591 			 * node. This may happen if a device provides several
3592 			 * components
3593 			 */
3594 			continue;
3595 		}
3596 
3597 		break;
3598 	}
3599 
3600 	if (ret == 0)
3601 		dlc->of_node = args->np;
3602 
3603 	return ret;
3604 }
3605 EXPORT_SYMBOL_GPL(snd_soc_get_dlc);
3606 
3607 int snd_soc_of_get_dlc(struct device_node *of_node,
3608 		       struct of_phandle_args *args,
3609 		       struct snd_soc_dai_link_component *dlc,
3610 		       int index)
3611 {
3612 	struct of_phandle_args __args;
3613 	int ret;
3614 
3615 	if (!args)
3616 		args = &__args;
3617 
3618 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
3619 					 "#sound-dai-cells", index, args);
3620 	if (ret)
3621 		return ret;
3622 
3623 	return snd_soc_get_dlc(args, dlc);
3624 }
3625 EXPORT_SYMBOL_GPL(snd_soc_of_get_dlc);
3626 
3627 int snd_soc_get_dai_name(const struct of_phandle_args *args,
3628 			 const char **dai_name)
3629 {
3630 	struct snd_soc_dai_link_component dlc;
3631 	int ret = snd_soc_get_dlc(args, &dlc);
3632 
3633 	if (ret == 0)
3634 		*dai_name = dlc.dai_name;
3635 
3636 	return ret;
3637 }
3638 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3639 
3640 int snd_soc_of_get_dai_name(struct device_node *of_node,
3641 			    const char **dai_name, int index)
3642 {
3643 	struct snd_soc_dai_link_component dlc;
3644 	int ret = snd_soc_of_get_dlc(of_node, NULL, &dlc, index);
3645 
3646 	if (ret == 0)
3647 		*dai_name = dlc.dai_name;
3648 
3649 	return ret;
3650 }
3651 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3652 
3653 struct snd_soc_dai *snd_soc_get_dai_via_args(const struct of_phandle_args *dai_args)
3654 {
3655 	struct snd_soc_dai *dai;
3656 	struct snd_soc_component *component;
3657 	guard(mutex)(&client_mutex);
3658 
3659 	for_each_component(component) {
3660 		for_each_component_dais(component, dai)
3661 			if (snd_soc_is_match_dai_args(dai->driver->dai_args, dai_args))
3662 				return dai;
3663 	}
3664 	return NULL;
3665 }
3666 EXPORT_SYMBOL_GPL(snd_soc_get_dai_via_args);
3667 
3668 static void __snd_soc_of_put_component(struct snd_soc_dai_link_component *component)
3669 {
3670 	if (component->of_node) {
3671 		of_node_put(component->of_node);
3672 		component->of_node = NULL;
3673 	}
3674 }
3675 
3676 static int __snd_soc_of_get_dai_link_component_alloc(
3677 	struct device *dev, struct device_node *of_node,
3678 	struct snd_soc_dai_link_component **ret_component,
3679 	int *ret_num)
3680 {
3681 	struct snd_soc_dai_link_component *component;
3682 	int num;
3683 
3684 	/* Count the number of CPUs/CODECs */
3685 	num = of_count_phandle_with_args(of_node, "sound-dai", "#sound-dai-cells");
3686 	if (num <= 0) {
3687 		if (num == -ENOENT)
3688 			dev_err(dev, "No 'sound-dai' property\n");
3689 		else
3690 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
3691 		return num;
3692 	}
3693 	component = devm_kcalloc(dev, num, sizeof(*component), GFP_KERNEL);
3694 	if (!component)
3695 		return -ENOMEM;
3696 
3697 	*ret_component	= component;
3698 	*ret_num	= num;
3699 
3700 	return 0;
3701 }
3702 
3703 /*
3704  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3705  * @dai_link: DAI link
3706  *
3707  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3708  */
3709 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3710 {
3711 	struct snd_soc_dai_link_component *component;
3712 	int index;
3713 
3714 	for_each_link_codecs(dai_link, index, component)
3715 		__snd_soc_of_put_component(component);
3716 }
3717 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3718 
3719 /*
3720  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3721  * @dev: Card device
3722  * @of_node: Device node
3723  * @dai_link: DAI link
3724  *
3725  * Builds an array of CODEC DAI components from the DAI link property
3726  * 'sound-dai'.
3727  * The array is set in the DAI link and the number of DAIs is set accordingly.
3728  * The device nodes in the array (of_node) must be dereferenced by calling
3729  * snd_soc_of_put_dai_link_codecs() on @dai_link.
3730  *
3731  * Returns 0 for success
3732  */
3733 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3734 				   struct device_node *of_node,
3735 				   struct snd_soc_dai_link *dai_link)
3736 {
3737 	struct snd_soc_dai_link_component *component;
3738 	int index, ret;
3739 
3740 	ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
3741 					 &dai_link->codecs, &dai_link->num_codecs);
3742 	if (ret < 0)
3743 		return ret;
3744 
3745 	/* Parse the list */
3746 	for_each_link_codecs(dai_link, index, component) {
3747 		ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
3748 		if (ret)
3749 			goto err;
3750 	}
3751 	return 0;
3752 err:
3753 	snd_soc_of_put_dai_link_codecs(dai_link);
3754 	dai_link->codecs = NULL;
3755 	dai_link->num_codecs = 0;
3756 	return ret;
3757 }
3758 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3759 
3760 /*
3761  * snd_soc_of_put_dai_link_cpus - Dereference device nodes in the codecs array
3762  * @dai_link: DAI link
3763  *
3764  * Dereference device nodes acquired by snd_soc_of_get_dai_link_cpus().
3765  */
3766 void snd_soc_of_put_dai_link_cpus(struct snd_soc_dai_link *dai_link)
3767 {
3768 	struct snd_soc_dai_link_component *component;
3769 	int index;
3770 
3771 	for_each_link_cpus(dai_link, index, component)
3772 		__snd_soc_of_put_component(component);
3773 }
3774 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_cpus);
3775 
3776 /*
3777  * snd_soc_of_get_dai_link_cpus - Parse a list of CPU DAIs in the devicetree
3778  * @dev: Card device
3779  * @of_node: Device node
3780  * @dai_link: DAI link
3781  *
3782  * Is analogous to snd_soc_of_get_dai_link_codecs but parses a list of CPU DAIs
3783  * instead.
3784  *
3785  * Returns 0 for success
3786  */
3787 int snd_soc_of_get_dai_link_cpus(struct device *dev,
3788 				 struct device_node *of_node,
3789 				 struct snd_soc_dai_link *dai_link)
3790 {
3791 	struct snd_soc_dai_link_component *component;
3792 	int index, ret;
3793 
3794 	/* Count the number of CPUs */
3795 	ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
3796 					 &dai_link->cpus, &dai_link->num_cpus);
3797 	if (ret < 0)
3798 		return ret;
3799 
3800 	/* Parse the list */
3801 	for_each_link_cpus(dai_link, index, component) {
3802 		ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
3803 		if (ret)
3804 			goto err;
3805 	}
3806 	return 0;
3807 err:
3808 	snd_soc_of_put_dai_link_cpus(dai_link);
3809 	dai_link->cpus = NULL;
3810 	dai_link->num_cpus = 0;
3811 	return ret;
3812 }
3813 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_cpus);
3814 
3815 static int __init snd_soc_init(void)
3816 {
3817 	int ret;
3818 
3819 	snd_soc_debugfs_init();
3820 	ret = snd_soc_util_init();
3821 	if (ret)
3822 		goto err_util_init;
3823 
3824 	ret = platform_driver_register(&soc_driver);
3825 	if (ret)
3826 		goto err_register;
3827 	return 0;
3828 
3829 err_register:
3830 	snd_soc_util_exit();
3831 err_util_init:
3832 	snd_soc_debugfs_exit();
3833 	return ret;
3834 }
3835 module_init(snd_soc_init);
3836 
3837 static void __exit snd_soc_exit(void)
3838 {
3839 	snd_soc_util_exit();
3840 	snd_soc_debugfs_exit();
3841 
3842 	platform_driver_unregister(&soc_driver);
3843 }
3844 module_exit(snd_soc_exit);
3845 
3846 /* Module information */
3847 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3848 MODULE_DESCRIPTION("ALSA SoC Core");
3849 MODULE_LICENSE("GPL");
3850 MODULE_ALIAS("platform:soc-audio");
3851