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