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