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