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