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