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