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