Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / sound / soc / soc-core.c
blobf6d4e99b590c708ae185f54e05e4142a59f30ae3
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>
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
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/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/soc-dpcm.h>
39 #include <sound/soc-topology.h>
40 #include <sound/soc-link.h>
41 #include <sound/initval.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/asoc.h>
46 static DEFINE_MUTEX(client_mutex);
47 static LIST_HEAD(component_list);
48 static LIST_HEAD(unbind_card_list);
50 #define for_each_component(component) \
51 list_for_each_entry(component, &component_list, list)
54 * This is used if driver don't need to have CPU/Codec/Platform
55 * dai_link. see soc.h
57 struct snd_soc_dai_link_component null_dailink_component[0];
58 EXPORT_SYMBOL_GPL(null_dailink_component);
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
65 static int pmdown_time = 5000;
66 module_param(pmdown_time, int, 0);
67 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69 static ssize_t pmdown_time_show(struct device *dev,
70 struct device_attribute *attr, char *buf)
72 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
74 return sprintf(buf, "%ld\n", rtd->pmdown_time);
77 static ssize_t pmdown_time_set(struct device *dev,
78 struct device_attribute *attr,
79 const char *buf, size_t count)
81 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
82 int ret;
84 ret = kstrtol(buf, 10, &rtd->pmdown_time);
85 if (ret)
86 return ret;
88 return count;
91 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
93 static struct attribute *soc_dev_attrs[] = {
94 &dev_attr_pmdown_time.attr,
95 NULL
98 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
99 struct attribute *attr, int idx)
101 struct device *dev = kobj_to_dev(kobj);
102 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
104 if (!rtd)
105 return 0;
107 if (attr == &dev_attr_pmdown_time.attr)
108 return attr->mode; /* always visible */
109 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
112 static const struct attribute_group soc_dapm_dev_group = {
113 .attrs = soc_dapm_dev_attrs,
114 .is_visible = soc_dev_attr_is_visible,
117 static const struct attribute_group soc_dev_group = {
118 .attrs = soc_dev_attrs,
119 .is_visible = soc_dev_attr_is_visible,
122 static const struct attribute_group *soc_dev_attr_groups[] = {
123 &soc_dapm_dev_group,
124 &soc_dev_group,
125 NULL
128 #ifdef CONFIG_DEBUG_FS
129 struct dentry *snd_soc_debugfs_root;
130 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
132 static void soc_init_component_debugfs(struct snd_soc_component *component)
134 if (!component->card->debugfs_card_root)
135 return;
137 if (component->debugfs_prefix) {
138 char *name;
140 name = kasprintf(GFP_KERNEL, "%s:%s",
141 component->debugfs_prefix, component->name);
142 if (name) {
143 component->debugfs_root = debugfs_create_dir(name,
144 component->card->debugfs_card_root);
145 kfree(name);
147 } else {
148 component->debugfs_root = debugfs_create_dir(component->name,
149 component->card->debugfs_card_root);
152 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
153 component->debugfs_root);
156 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
158 if (!component->debugfs_root)
159 return;
160 debugfs_remove_recursive(component->debugfs_root);
161 component->debugfs_root = NULL;
164 static int dai_list_show(struct seq_file *m, void *v)
166 struct snd_soc_component *component;
167 struct snd_soc_dai *dai;
169 mutex_lock(&client_mutex);
171 for_each_component(component)
172 for_each_component_dais(component, dai)
173 seq_printf(m, "%s\n", dai->name);
175 mutex_unlock(&client_mutex);
177 return 0;
179 DEFINE_SHOW_ATTRIBUTE(dai_list);
181 static int component_list_show(struct seq_file *m, void *v)
183 struct snd_soc_component *component;
185 mutex_lock(&client_mutex);
187 for_each_component(component)
188 seq_printf(m, "%s\n", component->name);
190 mutex_unlock(&client_mutex);
192 return 0;
194 DEFINE_SHOW_ATTRIBUTE(component_list);
196 static void soc_init_card_debugfs(struct snd_soc_card *card)
198 card->debugfs_card_root = debugfs_create_dir(card->name,
199 snd_soc_debugfs_root);
201 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
202 &card->pop_time);
204 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
207 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
209 debugfs_remove_recursive(card->debugfs_card_root);
210 card->debugfs_card_root = NULL;
213 static void snd_soc_debugfs_init(void)
215 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
217 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
218 &dai_list_fops);
220 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
221 &component_list_fops);
224 static void snd_soc_debugfs_exit(void)
226 debugfs_remove_recursive(snd_soc_debugfs_root);
229 #else
231 static inline void soc_init_component_debugfs(
232 struct snd_soc_component *component)
236 static inline void soc_cleanup_component_debugfs(
237 struct snd_soc_component *component)
241 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
245 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
249 static inline void snd_soc_debugfs_init(void)
253 static inline void snd_soc_debugfs_exit(void)
257 #endif
259 static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
260 struct snd_soc_component *component)
262 struct snd_soc_component *comp;
263 int i;
265 for_each_rtd_components(rtd, i, comp) {
266 /* already connected */
267 if (comp == component)
268 return 0;
271 /* see for_each_rtd_components */
272 rtd->components[rtd->num_components] = component;
273 rtd->num_components++;
275 return 0;
278 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
279 const char *driver_name)
281 struct snd_soc_component *component;
282 int i;
284 if (!driver_name)
285 return NULL;
288 * NOTE
290 * snd_soc_rtdcom_lookup() will find component from rtd by using
291 * specified driver name.
292 * But, if many components which have same driver name are connected
293 * to 1 rtd, this function will return 1st found component.
295 for_each_rtd_components(rtd, i, component) {
296 const char *component_name = component->driver->name;
298 if (!component_name)
299 continue;
301 if ((component_name == driver_name) ||
302 strcmp(component_name, driver_name) == 0)
303 return component;
306 return NULL;
308 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
310 struct snd_soc_component
311 *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
313 struct snd_soc_component *component;
314 struct snd_soc_component *found_component;
316 found_component = NULL;
317 for_each_component(component) {
318 if ((dev == component->dev) &&
319 (!driver_name ||
320 (driver_name == component->driver->name) ||
321 (strcmp(component->driver->name, driver_name) == 0))) {
322 found_component = component;
323 break;
327 return found_component;
329 EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked);
331 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
332 const char *driver_name)
334 struct snd_soc_component *component;
336 mutex_lock(&client_mutex);
337 component = snd_soc_lookup_component_nolocked(dev, driver_name);
338 mutex_unlock(&client_mutex);
340 return component;
342 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
344 struct snd_soc_pcm_runtime
345 *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
346 struct snd_soc_dai_link *dai_link)
348 struct snd_soc_pcm_runtime *rtd;
350 for_each_card_rtds(card, rtd) {
351 if (rtd->dai_link == dai_link)
352 return rtd;
354 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
355 return NULL;
357 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
360 * Power down the audio subsystem pmdown_time msecs after close is called.
361 * This is to ensure there are no pops or clicks in between any music tracks
362 * due to DAPM power cycling.
364 void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
366 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
367 int playback = SNDRV_PCM_STREAM_PLAYBACK;
369 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
371 dev_dbg(rtd->dev,
372 "ASoC: pop wq checking: %s status: %s waiting: %s\n",
373 codec_dai->driver->playback.stream_name,
374 snd_soc_dai_stream_active(codec_dai, playback) ?
375 "active" : "inactive",
376 rtd->pop_wait ? "yes" : "no");
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);
385 mutex_unlock(&rtd->card->pcm_mutex);
387 EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
389 static void soc_release_rtd_dev(struct device *dev)
391 /* "dev" means "rtd->dev" */
392 kfree(dev);
395 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
397 if (!rtd)
398 return;
400 list_del(&rtd->list);
402 if (delayed_work_pending(&rtd->delayed_work))
403 flush_delayed_work(&rtd->delayed_work);
404 snd_soc_pcm_component_free(rtd);
407 * we don't need to call kfree() for rtd->dev
408 * see
409 * soc_release_rtd_dev()
411 * We don't need rtd->dev NULL check, because
412 * it is alloced *before* rtd.
413 * see
414 * soc_new_pcm_runtime()
416 device_unregister(rtd->dev);
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);
424 if (rtd->close_delayed_work_func)
425 rtd->close_delayed_work_func(rtd);
428 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
429 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
431 struct snd_soc_pcm_runtime *rtd;
432 struct snd_soc_component *component;
433 struct device *dev;
434 int ret;
435 int stream;
438 * for rtd->dev
440 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
441 if (!dev)
442 return NULL;
444 dev->parent = card->dev;
445 dev->release = soc_release_rtd_dev;
447 dev_set_name(dev, "%s", dai_link->name);
449 ret = device_register(dev);
450 if (ret < 0) {
451 put_device(dev); /* soc_release_rtd_dev */
452 return NULL;
456 * for rtd
458 rtd = devm_kzalloc(dev,
459 sizeof(*rtd) +
460 sizeof(*component) * (dai_link->num_cpus +
461 dai_link->num_codecs +
462 dai_link->num_platforms),
463 GFP_KERNEL);
464 if (!rtd)
465 goto free_rtd;
467 rtd->dev = dev;
468 INIT_LIST_HEAD(&rtd->list);
469 for_each_pcm_streams(stream) {
470 INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
471 INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
473 dev_set_drvdata(dev, rtd);
474 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
477 * for rtd->dais
479 rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
480 sizeof(struct snd_soc_dai *),
481 GFP_KERNEL);
482 if (!rtd->dais)
483 goto free_rtd;
486 * dais = [][][][][][][][][][][][][][][][][][]
487 * ^cpu_dais ^codec_dais
488 * |--- num_cpus ---|--- num_codecs --|
489 * see
490 * asoc_rtd_to_cpu()
491 * asoc_rtd_to_codec()
493 rtd->num_cpus = dai_link->num_cpus;
494 rtd->num_codecs = dai_link->num_codecs;
495 rtd->card = card;
496 rtd->dai_link = dai_link;
497 rtd->num = card->num_rtd++;
499 /* see for_each_card_rtds */
500 list_add_tail(&rtd->list, &card->rtd_list);
502 ret = device_add_groups(dev, soc_dev_attr_groups);
503 if (ret < 0)
504 goto free_rtd;
506 return rtd;
508 free_rtd:
509 soc_free_pcm_runtime(rtd);
510 return NULL;
513 static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
515 struct snd_soc_pcm_runtime *rtd;
517 for_each_card_rtds(card, rtd)
518 flush_delayed_work(&rtd->delayed_work);
521 #ifdef CONFIG_PM_SLEEP
522 static void soc_playback_digital_mute(struct snd_soc_card *card, int mute)
524 struct snd_soc_pcm_runtime *rtd;
525 struct snd_soc_dai *dai;
526 int playback = SNDRV_PCM_STREAM_PLAYBACK;
527 int i;
529 for_each_card_rtds(card, rtd) {
531 if (rtd->dai_link->ignore_suspend)
532 continue;
534 for_each_rtd_dais(rtd, i, dai) {
535 if (snd_soc_dai_stream_active(dai, playback))
536 snd_soc_dai_digital_mute(dai, mute, playback);
541 static void soc_dapm_suspend_resume(struct snd_soc_card *card, int event)
543 struct snd_soc_pcm_runtime *rtd;
544 int stream;
546 for_each_card_rtds(card, rtd) {
548 if (rtd->dai_link->ignore_suspend)
549 continue;
551 for_each_pcm_streams(stream)
552 snd_soc_dapm_stream_event(rtd, stream, event);
556 /* powers down audio subsystem for suspend */
557 int snd_soc_suspend(struct device *dev)
559 struct snd_soc_card *card = dev_get_drvdata(dev);
560 struct snd_soc_component *component;
561 struct snd_soc_pcm_runtime *rtd;
562 int i;
564 /* If the card is not initialized yet there is nothing to do */
565 if (!card->instantiated)
566 return 0;
569 * Due to the resume being scheduled into a workqueue we could
570 * suspend before that's finished - wait for it to complete.
572 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
574 /* we're going to block userspace touching us until resume completes */
575 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
577 /* mute any active DACs */
578 soc_playback_digital_mute(card, 1);
580 /* suspend all pcms */
581 for_each_card_rtds(card, rtd) {
582 if (rtd->dai_link->ignore_suspend)
583 continue;
585 snd_pcm_suspend_all(rtd->pcm);
588 snd_soc_card_suspend_pre(card);
590 /* close any waiting streams */
591 snd_soc_flush_all_delayed_work(card);
593 soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_SUSPEND);
595 /* Recheck all endpoints too, their state is affected by suspend */
596 dapm_mark_endpoints_dirty(card);
597 snd_soc_dapm_sync(&card->dapm);
599 /* suspend all COMPONENTs */
600 for_each_card_rtds(card, rtd) {
602 if (rtd->dai_link->ignore_suspend)
603 continue;
605 for_each_rtd_components(rtd, i, component) {
606 struct snd_soc_dapm_context *dapm =
607 snd_soc_component_get_dapm(component);
610 * ignore if component was already suspended
612 if (snd_soc_component_is_suspended(component))
613 continue;
616 * If there are paths active then the COMPONENT will be
617 * held with bias _ON and should not be suspended.
619 switch (snd_soc_dapm_get_bias_level(dapm)) {
620 case SND_SOC_BIAS_STANDBY:
622 * If the COMPONENT is capable of idle
623 * bias off then being in STANDBY
624 * means it's doing something,
625 * otherwise fall through.
627 if (dapm->idle_bias_off) {
628 dev_dbg(component->dev,
629 "ASoC: idle_bias_off CODEC on over suspend\n");
630 break;
632 fallthrough;
634 case SND_SOC_BIAS_OFF:
635 snd_soc_component_suspend(component);
636 if (component->regmap)
637 regcache_mark_dirty(component->regmap);
638 /* deactivate pins to sleep state */
639 pinctrl_pm_select_sleep_state(component->dev);
640 break;
641 default:
642 dev_dbg(component->dev,
643 "ASoC: COMPONENT is on over suspend\n");
644 break;
649 snd_soc_card_suspend_post(card);
651 return 0;
653 EXPORT_SYMBOL_GPL(snd_soc_suspend);
656 * deferred resume work, so resume can complete before we finished
657 * setting our codec back up, which can be very slow on I2C
659 static void soc_resume_deferred(struct work_struct *work)
661 struct snd_soc_card *card =
662 container_of(work, struct snd_soc_card,
663 deferred_resume_work);
664 struct snd_soc_component *component;
667 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
668 * so userspace apps are blocked from touching us
671 dev_dbg(card->dev, "ASoC: starting resume work\n");
673 /* Bring us up into D2 so that DAPM starts enabling things */
674 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
676 snd_soc_card_resume_pre(card);
678 for_each_card_components(card, component) {
679 if (snd_soc_component_is_suspended(component))
680 snd_soc_component_resume(component);
683 soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_RESUME);
685 /* unmute any active DACs */
686 soc_playback_digital_mute(card, 0);
688 snd_soc_card_resume_post(card);
690 dev_dbg(card->dev, "ASoC: resume work completed\n");
692 /* Recheck all endpoints too, their state is affected by suspend */
693 dapm_mark_endpoints_dirty(card);
694 snd_soc_dapm_sync(&card->dapm);
696 /* userspace can access us now we are back as we were before */
697 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
700 /* powers up audio subsystem after a suspend */
701 int snd_soc_resume(struct device *dev)
703 struct snd_soc_card *card = dev_get_drvdata(dev);
704 struct snd_soc_component *component;
706 /* If the card is not initialized yet there is nothing to do */
707 if (!card->instantiated)
708 return 0;
710 /* activate pins from sleep state */
711 for_each_card_components(card, component)
712 if (snd_soc_component_active(component))
713 pinctrl_pm_select_default_state(component->dev);
715 dev_dbg(dev, "ASoC: Scheduling resume work\n");
716 if (!schedule_work(&card->deferred_resume_work))
717 dev_err(dev, "ASoC: resume work item may be lost\n");
719 return 0;
721 EXPORT_SYMBOL_GPL(snd_soc_resume);
723 static void soc_resume_init(struct snd_soc_card *card)
725 /* deferred resume work */
726 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
728 #else
729 #define snd_soc_suspend NULL
730 #define snd_soc_resume NULL
731 static inline void soc_resume_init(struct snd_soc_card *card)
734 #endif
736 static struct device_node
737 *soc_component_to_node(struct snd_soc_component *component)
739 struct device_node *of_node;
741 of_node = component->dev->of_node;
742 if (!of_node && component->dev->parent)
743 of_node = component->dev->parent->of_node;
745 return of_node;
748 static int snd_soc_is_matching_component(
749 const struct snd_soc_dai_link_component *dlc,
750 struct snd_soc_component *component)
752 struct device_node *component_of_node;
754 if (!dlc)
755 return 0;
757 component_of_node = soc_component_to_node(component);
759 if (dlc->of_node && component_of_node != dlc->of_node)
760 return 0;
761 if (dlc->name && strcmp(component->name, dlc->name))
762 return 0;
764 return 1;
767 static struct snd_soc_component *soc_find_component(
768 const struct snd_soc_dai_link_component *dlc)
770 struct snd_soc_component *component;
772 lockdep_assert_held(&client_mutex);
775 * NOTE
777 * It returns *1st* found component, but some driver
778 * has few components by same of_node/name
779 * ex)
780 * CPU component and generic DMAEngine component
782 for_each_component(component)
783 if (snd_soc_is_matching_component(dlc, component))
784 return component;
786 return NULL;
790 * snd_soc_find_dai - Find a registered DAI
792 * @dlc: name of the DAI or the DAI driver and optional component info to match
794 * This function will search all registered components and their DAIs to
795 * find the DAI of the same name. The component's of_node and name
796 * should also match if being specified.
798 * Return: pointer of DAI, or NULL if not found.
800 struct snd_soc_dai *snd_soc_find_dai(
801 const struct snd_soc_dai_link_component *dlc)
803 struct snd_soc_component *component;
804 struct snd_soc_dai *dai;
806 lockdep_assert_held(&client_mutex);
808 /* Find CPU DAI from registered DAIs */
809 for_each_component(component) {
810 if (!snd_soc_is_matching_component(dlc, component))
811 continue;
812 for_each_component_dais(component, dai) {
813 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
814 && (!dai->driver->name
815 || strcmp(dai->driver->name, dlc->dai_name)))
816 continue;
818 return dai;
822 return NULL;
824 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
826 struct snd_soc_dai *snd_soc_find_dai_with_mutex(
827 const struct snd_soc_dai_link_component *dlc)
829 struct snd_soc_dai *dai;
831 mutex_lock(&client_mutex);
832 dai = snd_soc_find_dai(dlc);
833 mutex_unlock(&client_mutex);
835 return dai;
837 EXPORT_SYMBOL_GPL(snd_soc_find_dai_with_mutex);
839 static int soc_dai_link_sanity_check(struct snd_soc_card *card,
840 struct snd_soc_dai_link *link)
842 int i;
843 struct snd_soc_dai_link_component *cpu, *codec, *platform;
845 for_each_link_codecs(link, i, codec) {
847 * Codec must be specified by 1 of name or OF node,
848 * not both or neither.
850 if (!!codec->name == !!codec->of_node) {
851 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
852 link->name);
853 return -EINVAL;
856 /* Codec DAI name must be specified */
857 if (!codec->dai_name) {
858 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
859 link->name);
860 return -EINVAL;
864 * Defer card registration if codec component is not added to
865 * component list.
867 if (!soc_find_component(codec)) {
868 dev_dbg(card->dev,
869 "ASoC: codec component %s not found for link %s\n",
870 codec->name, link->name);
871 return -EPROBE_DEFER;
875 for_each_link_platforms(link, i, platform) {
877 * Platform may be specified by either name or OF node, but it
878 * can be left unspecified, then no components will be inserted
879 * in the rtdcom list
881 if (!!platform->name == !!platform->of_node) {
882 dev_err(card->dev,
883 "ASoC: Neither/both platform name/of_node are set for %s\n",
884 link->name);
885 return -EINVAL;
889 * Defer card registration if platform component is not added to
890 * component list.
892 if (!soc_find_component(platform)) {
893 dev_dbg(card->dev,
894 "ASoC: platform component %s not found for link %s\n",
895 platform->name, link->name);
896 return -EPROBE_DEFER;
900 for_each_link_cpus(link, i, cpu) {
902 * CPU device may be specified by either name or OF node, but
903 * can be left unspecified, and will be matched based on DAI
904 * name alone..
906 if (cpu->name && cpu->of_node) {
907 dev_err(card->dev,
908 "ASoC: Neither/both cpu name/of_node are set for %s\n",
909 link->name);
910 return -EINVAL;
914 * Defer card registration if cpu dai component is not added to
915 * component list.
917 if ((cpu->of_node || cpu->name) &&
918 !soc_find_component(cpu)) {
919 dev_dbg(card->dev,
920 "ASoC: cpu component %s not found for link %s\n",
921 cpu->name, link->name);
922 return -EPROBE_DEFER;
926 * At least one of CPU DAI name or CPU device name/node must be
927 * specified
929 if (!cpu->dai_name &&
930 !(cpu->name || cpu->of_node)) {
931 dev_err(card->dev,
932 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
933 link->name);
934 return -EINVAL;
938 return 0;
942 * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
943 * @card: The ASoC card to which the pcm_runtime has
944 * @rtd: The pcm_runtime to remove
946 * This function removes a pcm_runtime from the ASoC card.
948 void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
949 struct snd_soc_pcm_runtime *rtd)
951 lockdep_assert_held(&client_mutex);
953 /* release machine specific resources */
954 snd_soc_link_exit(rtd);
957 * Notify the machine driver for extra destruction
959 snd_soc_card_remove_dai_link(card, rtd->dai_link);
961 soc_free_pcm_runtime(rtd);
963 EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
966 * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
967 * @card: The ASoC card to which the pcm_runtime is added
968 * @dai_link: The DAI link to find pcm_runtime
970 * This function adds a pcm_runtime ASoC card by using dai_link.
972 * Note: Topology can use this API to add pcm_runtime when probing the
973 * topology component. And machine drivers can still define static
974 * DAI links in dai_link array.
976 int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
977 struct snd_soc_dai_link *dai_link)
979 struct snd_soc_pcm_runtime *rtd;
980 struct snd_soc_dai_link_component *codec, *platform, *cpu;
981 struct snd_soc_component *component;
982 int i, ret;
984 lockdep_assert_held(&client_mutex);
987 * Notify the machine driver for extra initialization
989 ret = snd_soc_card_add_dai_link(card, dai_link);
990 if (ret < 0)
991 return ret;
993 if (dai_link->ignore)
994 return 0;
996 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
998 ret = soc_dai_link_sanity_check(card, dai_link);
999 if (ret < 0)
1000 return ret;
1002 rtd = soc_new_pcm_runtime(card, dai_link);
1003 if (!rtd)
1004 return -ENOMEM;
1006 for_each_link_cpus(dai_link, i, cpu) {
1007 asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
1008 if (!asoc_rtd_to_cpu(rtd, i)) {
1009 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1010 cpu->dai_name);
1011 goto _err_defer;
1013 snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component);
1016 /* Find CODEC from registered CODECs */
1017 for_each_link_codecs(dai_link, i, codec) {
1018 asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1019 if (!asoc_rtd_to_codec(rtd, i)) {
1020 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
1021 codec->dai_name);
1022 goto _err_defer;
1025 snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component);
1028 /* Find PLATFORM from registered PLATFORMs */
1029 for_each_link_platforms(dai_link, i, platform) {
1030 for_each_component(component) {
1031 if (!snd_soc_is_matching_component(platform, component))
1032 continue;
1034 snd_soc_rtd_add_component(rtd, component);
1038 return 0;
1040 _err_defer:
1041 snd_soc_remove_pcm_runtime(card, rtd);
1042 return -EPROBE_DEFER;
1044 EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime);
1046 static int soc_init_pcm_runtime(struct snd_soc_card *card,
1047 struct snd_soc_pcm_runtime *rtd)
1049 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1050 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
1051 struct snd_soc_component *component;
1052 int ret, num, i;
1054 /* set default power off timeout */
1055 rtd->pmdown_time = pmdown_time;
1057 /* do machine specific initialization */
1058 ret = snd_soc_link_init(rtd);
1059 if (ret < 0)
1060 return ret;
1062 if (dai_link->dai_fmt) {
1063 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1064 if (ret)
1065 return ret;
1068 /* add DPCM sysfs entries */
1069 soc_dpcm_debugfs_add(rtd);
1071 num = rtd->num;
1074 * most drivers will register their PCMs using DAI link ordering but
1075 * topology based drivers can use the DAI link id field to set PCM
1076 * device number and then use rtd + a base offset of the BEs.
1078 for_each_rtd_components(rtd, i, component) {
1079 if (!component->driver->use_dai_pcm_id)
1080 continue;
1082 if (rtd->dai_link->no_pcm)
1083 num += component->driver->be_pcm_base;
1084 else
1085 num = rtd->dai_link->id;
1088 /* create compress_device if possible */
1089 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1090 if (ret != -ENOTSUPP) {
1091 if (ret < 0)
1092 dev_err(card->dev, "ASoC: can't create compress %s\n",
1093 dai_link->stream_name);
1094 return ret;
1097 /* create the pcm */
1098 ret = soc_new_pcm(rtd, num);
1099 if (ret < 0) {
1100 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1101 dai_link->stream_name, ret);
1102 return ret;
1105 return snd_soc_pcm_dai_new(rtd);
1108 static void soc_set_name_prefix(struct snd_soc_card *card,
1109 struct snd_soc_component *component)
1111 struct device_node *of_node = soc_component_to_node(component);
1112 const char *str;
1113 int ret, i;
1115 for (i = 0; i < card->num_configs; i++) {
1116 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1118 if (snd_soc_is_matching_component(&map->dlc, component) &&
1119 map->name_prefix) {
1120 component->name_prefix = map->name_prefix;
1121 return;
1126 * If there is no configuration table or no match in the table,
1127 * check if a prefix is provided in the node
1129 ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1130 if (ret < 0)
1131 return;
1133 component->name_prefix = str;
1136 static void soc_remove_component(struct snd_soc_component *component,
1137 int probed)
1140 if (!component->card)
1141 return;
1143 if (probed)
1144 snd_soc_component_remove(component);
1146 /* For framework level robustness */
1147 snd_soc_component_set_jack(component, NULL, NULL);
1149 list_del_init(&component->card_list);
1150 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1151 soc_cleanup_component_debugfs(component);
1152 component->card = NULL;
1153 snd_soc_component_module_put_when_remove(component);
1156 static int soc_probe_component(struct snd_soc_card *card,
1157 struct snd_soc_component *component)
1159 struct snd_soc_dapm_context *dapm =
1160 snd_soc_component_get_dapm(component);
1161 struct snd_soc_dai *dai;
1162 int probed = 0;
1163 int ret;
1165 if (!strcmp(component->name, "snd-soc-dummy"))
1166 return 0;
1168 if (component->card) {
1169 if (component->card != card) {
1170 dev_err(component->dev,
1171 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1172 card->name, component->card->name);
1173 return -ENODEV;
1175 return 0;
1178 ret = snd_soc_component_module_get_when_probe(component);
1179 if (ret < 0)
1180 return ret;
1182 component->card = card;
1183 soc_set_name_prefix(card, component);
1185 soc_init_component_debugfs(component);
1187 snd_soc_dapm_init(dapm, card, component);
1189 ret = snd_soc_dapm_new_controls(dapm,
1190 component->driver->dapm_widgets,
1191 component->driver->num_dapm_widgets);
1193 if (ret != 0) {
1194 dev_err(component->dev,
1195 "Failed to create new controls %d\n", ret);
1196 goto err_probe;
1199 for_each_component_dais(component, dai) {
1200 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1201 if (ret != 0) {
1202 dev_err(component->dev,
1203 "Failed to create DAI widgets %d\n", ret);
1204 goto err_probe;
1208 ret = snd_soc_component_probe(component);
1209 if (ret < 0) {
1210 dev_err(component->dev,
1211 "ASoC: failed to probe component %d\n", ret);
1212 goto err_probe;
1214 WARN(dapm->idle_bias_off &&
1215 dapm->bias_level != SND_SOC_BIAS_OFF,
1216 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1217 component->name);
1218 probed = 1;
1221 * machine specific init
1222 * see
1223 * snd_soc_component_set_aux()
1225 ret = snd_soc_component_init(component);
1226 if (ret < 0)
1227 goto err_probe;
1229 ret = snd_soc_add_component_controls(component,
1230 component->driver->controls,
1231 component->driver->num_controls);
1232 if (ret < 0)
1233 goto err_probe;
1235 ret = snd_soc_dapm_add_routes(dapm,
1236 component->driver->dapm_routes,
1237 component->driver->num_dapm_routes);
1238 if (ret < 0) {
1239 if (card->disable_route_checks) {
1240 dev_info(card->dev,
1241 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1242 __func__);
1243 } else {
1244 dev_err(card->dev,
1245 "%s: snd_soc_dapm_add_routes failed: %d\n",
1246 __func__, ret);
1247 goto err_probe;
1251 /* see for_each_card_components */
1252 list_add(&component->card_list, &card->component_dev_list);
1254 err_probe:
1255 if (ret < 0)
1256 soc_remove_component(component, probed);
1258 return ret;
1261 static void soc_remove_link_dais(struct snd_soc_card *card)
1263 struct snd_soc_pcm_runtime *rtd;
1264 int order;
1266 for_each_comp_order(order) {
1267 for_each_card_rtds(card, rtd) {
1268 /* remove all rtd connected DAIs in good order */
1269 snd_soc_pcm_dai_remove(rtd, order);
1274 static int soc_probe_link_dais(struct snd_soc_card *card)
1276 struct snd_soc_pcm_runtime *rtd;
1277 int order, ret;
1279 for_each_comp_order(order) {
1280 for_each_card_rtds(card, rtd) {
1282 dev_dbg(card->dev,
1283 "ASoC: probe %s dai link %d late %d\n",
1284 card->name, rtd->num, order);
1286 /* probe all rtd connected DAIs in good order */
1287 ret = snd_soc_pcm_dai_probe(rtd, order);
1288 if (ret)
1289 return ret;
1293 return 0;
1296 static void soc_remove_link_components(struct snd_soc_card *card)
1298 struct snd_soc_component *component;
1299 struct snd_soc_pcm_runtime *rtd;
1300 int i, order;
1302 for_each_comp_order(order) {
1303 for_each_card_rtds(card, rtd) {
1304 for_each_rtd_components(rtd, i, component) {
1305 if (component->driver->remove_order != order)
1306 continue;
1308 soc_remove_component(component, 1);
1314 static int soc_probe_link_components(struct snd_soc_card *card)
1316 struct snd_soc_component *component;
1317 struct snd_soc_pcm_runtime *rtd;
1318 int i, ret, order;
1320 for_each_comp_order(order) {
1321 for_each_card_rtds(card, rtd) {
1322 for_each_rtd_components(rtd, i, component) {
1323 if (component->driver->probe_order != order)
1324 continue;
1326 ret = soc_probe_component(card, component);
1327 if (ret < 0)
1328 return ret;
1333 return 0;
1336 static void soc_unbind_aux_dev(struct snd_soc_card *card)
1338 struct snd_soc_component *component, *_component;
1340 for_each_card_auxs_safe(card, component, _component) {
1341 /* for snd_soc_component_init() */
1342 snd_soc_component_set_aux(component, NULL);
1343 list_del(&component->card_aux_list);
1347 static int soc_bind_aux_dev(struct snd_soc_card *card)
1349 struct snd_soc_component *component;
1350 struct snd_soc_aux_dev *aux;
1351 int i;
1353 for_each_card_pre_auxs(card, i, aux) {
1354 /* codecs, usually analog devices */
1355 component = soc_find_component(&aux->dlc);
1356 if (!component)
1357 return -EPROBE_DEFER;
1359 /* for snd_soc_component_init() */
1360 snd_soc_component_set_aux(component, aux);
1361 /* see for_each_card_auxs */
1362 list_add(&component->card_aux_list, &card->aux_comp_list);
1364 return 0;
1367 static int soc_probe_aux_devices(struct snd_soc_card *card)
1369 struct snd_soc_component *component;
1370 int order;
1371 int ret;
1373 for_each_comp_order(order) {
1374 for_each_card_auxs(card, component) {
1375 if (component->driver->probe_order != order)
1376 continue;
1378 ret = soc_probe_component(card, component);
1379 if (ret < 0)
1380 return ret;
1384 return 0;
1387 static void soc_remove_aux_devices(struct snd_soc_card *card)
1389 struct snd_soc_component *comp, *_comp;
1390 int order;
1392 for_each_comp_order(order) {
1393 for_each_card_auxs_safe(card, comp, _comp) {
1394 if (comp->driver->remove_order == order)
1395 soc_remove_component(comp, 1);
1401 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1402 * @rtd: The runtime for which the DAI link format should be changed
1403 * @dai_fmt: The new DAI link format
1405 * This function updates the DAI link format for all DAIs connected to the DAI
1406 * link for the specified runtime.
1408 * Note: For setups with a static format set the dai_fmt field in the
1409 * corresponding snd_dai_link struct instead of using this function.
1411 * Returns 0 on success, otherwise a negative error code.
1413 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1414 unsigned int dai_fmt)
1416 struct snd_soc_dai *cpu_dai;
1417 struct snd_soc_dai *codec_dai;
1418 unsigned int inv_dai_fmt;
1419 unsigned int i;
1420 int ret;
1422 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1423 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1424 if (ret != 0 && ret != -ENOTSUPP) {
1425 dev_warn(codec_dai->dev,
1426 "ASoC: Failed to set DAI format: %d\n", ret);
1427 return ret;
1432 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1433 * the component which has non_legacy_dai_naming is Codec
1435 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1436 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1437 case SND_SOC_DAIFMT_CBM_CFM:
1438 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1439 break;
1440 case SND_SOC_DAIFMT_CBM_CFS:
1441 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1442 break;
1443 case SND_SOC_DAIFMT_CBS_CFM:
1444 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1445 break;
1446 case SND_SOC_DAIFMT_CBS_CFS:
1447 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1448 break;
1450 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1451 unsigned int fmt = dai_fmt;
1453 if (cpu_dai->component->driver->non_legacy_dai_naming)
1454 fmt = inv_dai_fmt;
1456 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
1457 if (ret != 0 && ret != -ENOTSUPP) {
1458 dev_warn(cpu_dai->dev,
1459 "ASoC: Failed to set DAI format: %d\n", ret);
1460 return ret;
1464 return 0;
1466 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1468 #ifdef CONFIG_DMI
1470 * If a DMI filed contain strings in this blacklist (e.g.
1471 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1472 * as invalid and dropped when setting the card long name from DMI info.
1474 static const char * const dmi_blacklist[] = {
1475 "To be filled by OEM",
1476 "TBD by OEM",
1477 "Default String",
1478 "Board Manufacturer",
1479 "Board Vendor Name",
1480 "Board Product Name",
1481 NULL, /* terminator */
1485 * Trim special characters, and replace '-' with '_' since '-' is used to
1486 * separate different DMI fields in the card long name. Only number and
1487 * alphabet characters and a few separator characters are kept.
1489 static void cleanup_dmi_name(char *name)
1491 int i, j = 0;
1493 for (i = 0; name[i]; i++) {
1494 if (isalnum(name[i]) || (name[i] == '.')
1495 || (name[i] == '_'))
1496 name[j++] = name[i];
1497 else if (name[i] == '-')
1498 name[j++] = '_';
1501 name[j] = '\0';
1505 * Check if a DMI field is valid, i.e. not containing any string
1506 * in the black list.
1508 static int is_dmi_valid(const char *field)
1510 int i = 0;
1512 while (dmi_blacklist[i]) {
1513 if (strstr(field, dmi_blacklist[i]))
1514 return 0;
1515 i++;
1518 return 1;
1522 * Append a string to card->dmi_longname with character cleanups.
1524 static void append_dmi_string(struct snd_soc_card *card, const char *str)
1526 char *dst = card->dmi_longname;
1527 size_t dst_len = sizeof(card->dmi_longname);
1528 size_t len;
1530 len = strlen(dst);
1531 snprintf(dst + len, dst_len - len, "-%s", str);
1533 len++; /* skip the separator "-" */
1534 if (len < dst_len)
1535 cleanup_dmi_name(dst + len);
1539 * snd_soc_set_dmi_name() - Register DMI names to card
1540 * @card: The card to register DMI names
1541 * @flavour: The flavour "differentiator" for the card amongst its peers.
1543 * An Intel machine driver may be used by many different devices but are
1544 * difficult for userspace to differentiate, since machine drivers ususally
1545 * use their own name as the card short name and leave the card long name
1546 * blank. To differentiate such devices and fix bugs due to lack of
1547 * device-specific configurations, this function allows DMI info to be used
1548 * as the sound card long name, in the format of
1549 * "vendor-product-version-board"
1550 * (Character '-' is used to separate different DMI fields here).
1551 * This will help the user space to load the device-specific Use Case Manager
1552 * (UCM) configurations for the card.
1554 * Possible card long names may be:
1555 * DellInc.-XPS139343-01-0310JH
1556 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1557 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1559 * This function also supports flavoring the card longname to provide
1560 * the extra differentiation, like "vendor-product-version-board-flavor".
1562 * We only keep number and alphabet characters and a few separator characters
1563 * in the card long name since UCM in the user space uses the card long names
1564 * as card configuration directory names and AudoConf cannot support special
1565 * charactors like SPACE.
1567 * Returns 0 on success, otherwise a negative error code.
1569 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1571 const char *vendor, *product, *product_version, *board;
1573 if (card->long_name)
1574 return 0; /* long name already set by driver or from DMI */
1576 /* make up dmi long name as: vendor-product-version-board */
1577 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1578 if (!vendor || !is_dmi_valid(vendor)) {
1579 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1580 return 0;
1583 snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1584 cleanup_dmi_name(card->dmi_longname);
1586 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1587 if (product && is_dmi_valid(product)) {
1588 append_dmi_string(card, product);
1591 * some vendors like Lenovo may only put a self-explanatory
1592 * name in the product version field
1594 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1595 if (product_version && is_dmi_valid(product_version))
1596 append_dmi_string(card, product_version);
1599 board = dmi_get_system_info(DMI_BOARD_NAME);
1600 if (board && is_dmi_valid(board)) {
1601 if (!product || strcasecmp(board, product))
1602 append_dmi_string(card, board);
1603 } else if (!product) {
1604 /* fall back to using legacy name */
1605 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1606 return 0;
1609 /* Add flavour to dmi long name */
1610 if (flavour)
1611 append_dmi_string(card, flavour);
1613 /* set the card long name */
1614 card->long_name = card->dmi_longname;
1616 return 0;
1618 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1619 #endif /* CONFIG_DMI */
1621 static void soc_check_tplg_fes(struct snd_soc_card *card)
1623 struct snd_soc_component *component;
1624 const struct snd_soc_component_driver *comp_drv;
1625 struct snd_soc_dai_link *dai_link;
1626 int i;
1628 for_each_component(component) {
1630 /* does this component override BEs ? */
1631 if (!component->driver->ignore_machine)
1632 continue;
1634 /* for this machine ? */
1635 if (!strcmp(component->driver->ignore_machine,
1636 card->dev->driver->name))
1637 goto match;
1638 if (strcmp(component->driver->ignore_machine,
1639 dev_name(card->dev)))
1640 continue;
1641 match:
1642 /* machine matches, so override the rtd data */
1643 for_each_card_prelinks(card, i, dai_link) {
1645 /* ignore this FE */
1646 if (dai_link->dynamic) {
1647 dai_link->ignore = true;
1648 continue;
1651 dev_dbg(card->dev, "info: override BE DAI link %s\n",
1652 card->dai_link[i].name);
1654 /* override platform component */
1655 if (!dai_link->platforms) {
1656 dev_err(card->dev, "init platform error");
1657 continue;
1659 dai_link->platforms->name = component->name;
1661 /* convert non BE into BE */
1662 if (!dai_link->no_pcm) {
1663 dai_link->no_pcm = 1;
1665 if (dai_link->dpcm_playback)
1666 dev_warn(card->dev,
1667 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_playback=1\n",
1668 dai_link->name);
1669 if (dai_link->dpcm_capture)
1670 dev_warn(card->dev,
1671 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_capture=1\n",
1672 dai_link->name);
1674 /* convert normal link into DPCM one */
1675 if (!(dai_link->dpcm_playback ||
1676 dai_link->dpcm_capture)) {
1677 dai_link->dpcm_playback = !dai_link->capture_only;
1678 dai_link->dpcm_capture = !dai_link->playback_only;
1683 * override any BE fixups
1684 * see
1685 * snd_soc_link_be_hw_params_fixup()
1687 dai_link->be_hw_params_fixup =
1688 component->driver->be_hw_params_fixup;
1691 * most BE links don't set stream name, so set it to
1692 * dai link name if it's NULL to help bind widgets.
1694 if (!dai_link->stream_name)
1695 dai_link->stream_name = dai_link->name;
1698 /* Inform userspace we are using alternate topology */
1699 if (component->driver->topology_name_prefix) {
1701 /* topology shortname created? */
1702 if (!card->topology_shortname_created) {
1703 comp_drv = component->driver;
1705 snprintf(card->topology_shortname, 32, "%s-%s",
1706 comp_drv->topology_name_prefix,
1707 card->name);
1708 card->topology_shortname_created = true;
1711 /* use topology shortname */
1712 card->name = card->topology_shortname;
1717 #define soc_setup_card_name(name, name1, name2, norm) \
1718 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1719 static void __soc_setup_card_name(char *name, int len,
1720 const char *name1, const char *name2,
1721 int normalization)
1723 int i;
1725 snprintf(name, len, "%s", name1 ? name1 : name2);
1727 if (!normalization)
1728 return;
1731 * Name normalization
1733 * The driver name is somewhat special, as it's used as a key for
1734 * searches in the user-space.
1736 * ex)
1737 * "abcd??efg" -> "abcd__efg"
1739 for (i = 0; i < len; i++) {
1740 switch (name[i]) {
1741 case '_':
1742 case '-':
1743 case '\0':
1744 break;
1745 default:
1746 if (!isalnum(name[i]))
1747 name[i] = '_';
1748 break;
1753 static void soc_cleanup_card_resources(struct snd_soc_card *card)
1755 struct snd_soc_pcm_runtime *rtd, *n;
1757 if (card->snd_card)
1758 snd_card_disconnect_sync(card->snd_card);
1760 snd_soc_dapm_shutdown(card);
1762 /* remove and free each DAI */
1763 soc_remove_link_dais(card);
1764 soc_remove_link_components(card);
1766 for_each_card_rtds_safe(card, rtd, n)
1767 snd_soc_remove_pcm_runtime(card, rtd);
1769 /* remove auxiliary devices */
1770 soc_remove_aux_devices(card);
1771 soc_unbind_aux_dev(card);
1773 snd_soc_dapm_free(&card->dapm);
1774 soc_cleanup_card_debugfs(card);
1776 /* remove the card */
1777 snd_soc_card_remove(card);
1779 if (card->snd_card) {
1780 snd_card_free(card->snd_card);
1781 card->snd_card = NULL;
1785 static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
1787 if (card->instantiated) {
1788 card->instantiated = false;
1789 snd_soc_flush_all_delayed_work(card);
1791 soc_cleanup_card_resources(card);
1792 if (!unregister)
1793 list_add(&card->list, &unbind_card_list);
1794 } else {
1795 if (unregister)
1796 list_del(&card->list);
1800 static int snd_soc_bind_card(struct snd_soc_card *card)
1802 struct snd_soc_pcm_runtime *rtd;
1803 struct snd_soc_component *component;
1804 struct snd_soc_dai_link *dai_link;
1805 int ret, i;
1807 mutex_lock(&client_mutex);
1808 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1810 snd_soc_dapm_init(&card->dapm, card, NULL);
1812 /* check whether any platform is ignore machine FE and using topology */
1813 soc_check_tplg_fes(card);
1815 /* bind aux_devs too */
1816 ret = soc_bind_aux_dev(card);
1817 if (ret < 0)
1818 goto probe_end;
1820 /* add predefined DAI links to the list */
1821 card->num_rtd = 0;
1822 for_each_card_prelinks(card, i, dai_link) {
1823 ret = snd_soc_add_pcm_runtime(card, dai_link);
1824 if (ret < 0)
1825 goto probe_end;
1828 /* card bind complete so register a sound card */
1829 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1830 card->owner, 0, &card->snd_card);
1831 if (ret < 0) {
1832 dev_err(card->dev,
1833 "ASoC: can't create sound card for card %s: %d\n",
1834 card->name, ret);
1835 goto probe_end;
1838 soc_init_card_debugfs(card);
1840 soc_resume_init(card);
1842 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1843 card->num_dapm_widgets);
1844 if (ret < 0)
1845 goto probe_end;
1847 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1848 card->num_of_dapm_widgets);
1849 if (ret < 0)
1850 goto probe_end;
1852 /* initialise the sound card only once */
1853 ret = snd_soc_card_probe(card);
1854 if (ret < 0)
1855 goto probe_end;
1857 /* probe all components used by DAI links on this card */
1858 ret = soc_probe_link_components(card);
1859 if (ret < 0) {
1860 dev_err(card->dev,
1861 "ASoC: failed to instantiate card %d\n", ret);
1862 goto probe_end;
1865 /* probe auxiliary components */
1866 ret = soc_probe_aux_devices(card);
1867 if (ret < 0) {
1868 dev_err(card->dev,
1869 "ASoC: failed to probe aux component %d\n", ret);
1870 goto probe_end;
1873 /* probe all DAI links on this card */
1874 ret = soc_probe_link_dais(card);
1875 if (ret < 0) {
1876 dev_err(card->dev,
1877 "ASoC: failed to instantiate card %d\n", ret);
1878 goto probe_end;
1881 for_each_card_rtds(card, rtd) {
1882 ret = soc_init_pcm_runtime(card, rtd);
1883 if (ret < 0)
1884 goto probe_end;
1887 snd_soc_dapm_link_dai_widgets(card);
1888 snd_soc_dapm_connect_dai_link_widgets(card);
1890 ret = snd_soc_add_card_controls(card, card->controls,
1891 card->num_controls);
1892 if (ret < 0)
1893 goto probe_end;
1895 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1896 card->num_dapm_routes);
1897 if (ret < 0) {
1898 if (card->disable_route_checks) {
1899 dev_info(card->dev,
1900 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1901 __func__);
1902 } else {
1903 dev_err(card->dev,
1904 "%s: snd_soc_dapm_add_routes failed: %d\n",
1905 __func__, ret);
1906 goto probe_end;
1910 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1911 card->num_of_dapm_routes);
1912 if (ret < 0)
1913 goto probe_end;
1915 /* try to set some sane longname if DMI is available */
1916 snd_soc_set_dmi_name(card, NULL);
1918 soc_setup_card_name(card->snd_card->shortname,
1919 card->name, NULL, 0);
1920 soc_setup_card_name(card->snd_card->longname,
1921 card->long_name, card->name, 0);
1922 soc_setup_card_name(card->snd_card->driver,
1923 card->driver_name, card->name, 1);
1925 if (card->components) {
1926 /* the current implementation of snd_component_add() accepts */
1927 /* multiple components in the string separated by space, */
1928 /* but the string collision (identical string) check might */
1929 /* not work correctly */
1930 ret = snd_component_add(card->snd_card, card->components);
1931 if (ret < 0) {
1932 dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
1933 card->name, ret);
1934 goto probe_end;
1938 ret = snd_soc_card_late_probe(card);
1939 if (ret < 0)
1940 goto probe_end;
1942 snd_soc_dapm_new_widgets(card);
1944 ret = snd_card_register(card->snd_card);
1945 if (ret < 0) {
1946 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1947 ret);
1948 goto probe_end;
1951 card->instantiated = 1;
1952 dapm_mark_endpoints_dirty(card);
1953 snd_soc_dapm_sync(&card->dapm);
1955 /* deactivate pins to sleep state */
1956 for_each_card_components(card, component)
1957 if (!snd_soc_component_active(component))
1958 pinctrl_pm_select_sleep_state(component->dev);
1960 probe_end:
1961 if (ret < 0)
1962 soc_cleanup_card_resources(card);
1964 mutex_unlock(&card->mutex);
1965 mutex_unlock(&client_mutex);
1967 return ret;
1970 /* probes a new socdev */
1971 static int soc_probe(struct platform_device *pdev)
1973 struct snd_soc_card *card = platform_get_drvdata(pdev);
1976 * no card, so machine driver should be registering card
1977 * we should not be here in that case so ret error
1979 if (!card)
1980 return -EINVAL;
1982 dev_warn(&pdev->dev,
1983 "ASoC: machine %s should use snd_soc_register_card()\n",
1984 card->name);
1986 /* Bodge while we unpick instantiation */
1987 card->dev = &pdev->dev;
1989 return devm_snd_soc_register_card(&pdev->dev, card);
1992 int snd_soc_poweroff(struct device *dev)
1994 struct snd_soc_card *card = dev_get_drvdata(dev);
1995 struct snd_soc_component *component;
1997 if (!card->instantiated)
1998 return 0;
2001 * Flush out pmdown_time work - we actually do want to run it
2002 * now, we're shutting down so no imminent restart.
2004 snd_soc_flush_all_delayed_work(card);
2006 snd_soc_dapm_shutdown(card);
2008 /* deactivate pins to sleep state */
2009 for_each_card_components(card, component)
2010 pinctrl_pm_select_sleep_state(component->dev);
2012 return 0;
2014 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2016 const struct dev_pm_ops snd_soc_pm_ops = {
2017 .suspend = snd_soc_suspend,
2018 .resume = snd_soc_resume,
2019 .freeze = snd_soc_suspend,
2020 .thaw = snd_soc_resume,
2021 .poweroff = snd_soc_poweroff,
2022 .restore = snd_soc_resume,
2024 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2026 /* ASoC platform driver */
2027 static struct platform_driver soc_driver = {
2028 .driver = {
2029 .name = "soc-audio",
2030 .pm = &snd_soc_pm_ops,
2032 .probe = soc_probe,
2036 * snd_soc_cnew - create new control
2037 * @_template: control template
2038 * @data: control private data
2039 * @long_name: control long name
2040 * @prefix: control name prefix
2042 * Create a new mixer control from a template control.
2044 * Returns 0 for success, else error.
2046 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2047 void *data, const char *long_name,
2048 const char *prefix)
2050 struct snd_kcontrol_new template;
2051 struct snd_kcontrol *kcontrol;
2052 char *name = NULL;
2054 memcpy(&template, _template, sizeof(template));
2055 template.index = 0;
2057 if (!long_name)
2058 long_name = template.name;
2060 if (prefix) {
2061 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2062 if (!name)
2063 return NULL;
2065 template.name = name;
2066 } else {
2067 template.name = long_name;
2070 kcontrol = snd_ctl_new1(&template, data);
2072 kfree(name);
2074 return kcontrol;
2076 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2078 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2079 const struct snd_kcontrol_new *controls, int num_controls,
2080 const char *prefix, void *data)
2082 int err, i;
2084 for (i = 0; i < num_controls; i++) {
2085 const struct snd_kcontrol_new *control = &controls[i];
2087 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2088 control->name, prefix));
2089 if (err < 0) {
2090 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2091 control->name, err);
2092 return err;
2096 return 0;
2100 * snd_soc_add_component_controls - Add an array of controls to a component.
2102 * @component: Component to add controls to
2103 * @controls: Array of controls to add
2104 * @num_controls: Number of elements in the array
2106 * Return: 0 for success, else error.
2108 int snd_soc_add_component_controls(struct snd_soc_component *component,
2109 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2111 struct snd_card *card = component->card->snd_card;
2113 return snd_soc_add_controls(card, component->dev, controls,
2114 num_controls, component->name_prefix, component);
2116 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2119 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2120 * Convenience function to add a list of controls.
2122 * @soc_card: SoC card to add controls to
2123 * @controls: array of controls to add
2124 * @num_controls: number of elements in the array
2126 * Return 0 for success, else error.
2128 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2129 const struct snd_kcontrol_new *controls, int num_controls)
2131 struct snd_card *card = soc_card->snd_card;
2133 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2134 NULL, soc_card);
2136 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2139 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2140 * Convienience function to add a list of controls.
2142 * @dai: DAI to add controls to
2143 * @controls: array of controls to add
2144 * @num_controls: number of elements in the array
2146 * Return 0 for success, else error.
2148 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2149 const struct snd_kcontrol_new *controls, int num_controls)
2151 struct snd_card *card = dai->component->card->snd_card;
2153 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2154 NULL, dai);
2156 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2159 * snd_soc_register_card - Register a card with the ASoC core
2161 * @card: Card to register
2164 int snd_soc_register_card(struct snd_soc_card *card)
2166 if (!card->name || !card->dev)
2167 return -EINVAL;
2169 dev_set_drvdata(card->dev, card);
2171 INIT_LIST_HEAD(&card->widgets);
2172 INIT_LIST_HEAD(&card->paths);
2173 INIT_LIST_HEAD(&card->dapm_list);
2174 INIT_LIST_HEAD(&card->aux_comp_list);
2175 INIT_LIST_HEAD(&card->component_dev_list);
2176 INIT_LIST_HEAD(&card->list);
2177 INIT_LIST_HEAD(&card->rtd_list);
2178 INIT_LIST_HEAD(&card->dapm_dirty);
2179 INIT_LIST_HEAD(&card->dobj_list);
2181 card->instantiated = 0;
2182 mutex_init(&card->mutex);
2183 mutex_init(&card->dapm_mutex);
2184 mutex_init(&card->pcm_mutex);
2185 spin_lock_init(&card->dpcm_lock);
2187 return snd_soc_bind_card(card);
2189 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2192 * snd_soc_unregister_card - Unregister a card with the ASoC core
2194 * @card: Card to unregister
2197 int snd_soc_unregister_card(struct snd_soc_card *card)
2199 mutex_lock(&client_mutex);
2200 snd_soc_unbind_card(card, true);
2201 mutex_unlock(&client_mutex);
2202 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2204 return 0;
2206 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2209 * Simplify DAI link configuration by removing ".-1" from device names
2210 * and sanitizing names.
2212 static char *fmt_single_name(struct device *dev, int *id)
2214 const char *devname = dev_name(dev);
2215 char *found, *name;
2216 int id1, id2;
2218 if (devname == NULL)
2219 return NULL;
2221 name = devm_kstrdup(dev, devname, GFP_KERNEL);
2223 /* are we a "%s.%d" name (platform and SPI components) */
2224 found = strstr(name, dev->driver->name);
2225 if (found) {
2226 /* get ID */
2227 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2229 /* discard ID from name if ID == -1 */
2230 if (*id == -1)
2231 found[strlen(dev->driver->name)] = '\0';
2234 /* I2C component devices are named "bus-addr" */
2235 } else if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2237 /* create unique ID number from I2C addr and bus */
2238 *id = ((id1 & 0xffff) << 16) + id2;
2240 devm_kfree(dev, name);
2242 /* sanitize component name for DAI link creation */
2243 name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname);
2244 } else {
2245 *id = 0;
2248 return name;
2252 * Simplify DAI link naming for single devices with multiple DAIs by removing
2253 * any ".-1" and using the DAI name (instead of device name).
2255 static inline char *fmt_multiple_name(struct device *dev,
2256 struct snd_soc_dai_driver *dai_drv)
2258 if (dai_drv->name == NULL) {
2259 dev_err(dev,
2260 "ASoC: error - multiple DAI %s registered with no name\n",
2261 dev_name(dev));
2262 return NULL;
2265 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2268 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2270 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2271 list_del(&dai->list);
2273 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2276 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2278 * @component: The component the DAIs are registered for
2279 * @dai_drv: DAI driver to use for the DAI
2280 * @legacy_dai_naming: if %true, use legacy single-name format;
2281 * if %false, use multiple-name format;
2283 * Topology can use this API to register DAIs when probing a component.
2284 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2285 * will be freed in the component cleanup.
2287 struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2288 struct snd_soc_dai_driver *dai_drv,
2289 bool legacy_dai_naming)
2291 struct device *dev = component->dev;
2292 struct snd_soc_dai *dai;
2294 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2296 lockdep_assert_held(&client_mutex);
2298 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2299 if (dai == NULL)
2300 return NULL;
2303 * Back in the old days when we still had component-less DAIs,
2304 * instead of having a static name, component-less DAIs would
2305 * inherit the name of the parent device so it is possible to
2306 * register multiple instances of the DAI. We still need to keep
2307 * the same naming style even though those DAIs are not
2308 * component-less anymore.
2310 if (legacy_dai_naming &&
2311 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2312 dai->name = fmt_single_name(dev, &dai->id);
2313 } else {
2314 dai->name = fmt_multiple_name(dev, dai_drv);
2315 if (dai_drv->id)
2316 dai->id = dai_drv->id;
2317 else
2318 dai->id = component->num_dai;
2320 if (!dai->name)
2321 return NULL;
2323 dai->component = component;
2324 dai->dev = dev;
2325 dai->driver = dai_drv;
2327 /* see for_each_component_dais */
2328 list_add_tail(&dai->list, &component->dai_list);
2329 component->num_dai++;
2331 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2332 return dai;
2336 * snd_soc_unregister_dais - Unregister DAIs from the ASoC core
2338 * @component: The component for which the DAIs should be unregistered
2340 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2342 struct snd_soc_dai *dai, *_dai;
2344 for_each_component_dais_safe(component, dai, _dai)
2345 snd_soc_unregister_dai(dai);
2349 * snd_soc_register_dais - Register a DAI with the ASoC core
2351 * @component: The component the DAIs are registered for
2352 * @dai_drv: DAI driver to use for the DAIs
2353 * @count: Number of DAIs
2355 static int snd_soc_register_dais(struct snd_soc_component *component,
2356 struct snd_soc_dai_driver *dai_drv,
2357 size_t count)
2359 struct snd_soc_dai *dai;
2360 unsigned int i;
2361 int ret;
2363 for (i = 0; i < count; i++) {
2364 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2365 !component->driver->non_legacy_dai_naming);
2366 if (dai == NULL) {
2367 ret = -ENOMEM;
2368 goto err;
2372 return 0;
2374 err:
2375 snd_soc_unregister_dais(component);
2377 return ret;
2380 #define ENDIANNESS_MAP(name) \
2381 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2382 static u64 endianness_format_map[] = {
2383 ENDIANNESS_MAP(S16_),
2384 ENDIANNESS_MAP(U16_),
2385 ENDIANNESS_MAP(S24_),
2386 ENDIANNESS_MAP(U24_),
2387 ENDIANNESS_MAP(S32_),
2388 ENDIANNESS_MAP(U32_),
2389 ENDIANNESS_MAP(S24_3),
2390 ENDIANNESS_MAP(U24_3),
2391 ENDIANNESS_MAP(S20_3),
2392 ENDIANNESS_MAP(U20_3),
2393 ENDIANNESS_MAP(S18_3),
2394 ENDIANNESS_MAP(U18_3),
2395 ENDIANNESS_MAP(FLOAT_),
2396 ENDIANNESS_MAP(FLOAT64_),
2397 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2401 * Fix up the DAI formats for endianness: codecs don't actually see
2402 * the endianness of the data but we're using the CPU format
2403 * definitions which do need to include endianness so we ensure that
2404 * codec DAIs always have both big and little endian variants set.
2406 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2408 int i;
2410 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2411 if (stream->formats & endianness_format_map[i])
2412 stream->formats |= endianness_format_map[i];
2415 static void snd_soc_try_rebind_card(void)
2417 struct snd_soc_card *card, *c;
2419 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2420 if (!snd_soc_bind_card(card))
2421 list_del(&card->list);
2424 static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2426 struct snd_soc_card *card = component->card;
2428 snd_soc_unregister_dais(component);
2430 if (card)
2431 snd_soc_unbind_card(card, false);
2433 list_del(&component->list);
2436 int snd_soc_component_initialize(struct snd_soc_component *component,
2437 const struct snd_soc_component_driver *driver,
2438 struct device *dev)
2440 INIT_LIST_HEAD(&component->dai_list);
2441 INIT_LIST_HEAD(&component->dobj_list);
2442 INIT_LIST_HEAD(&component->card_list);
2443 mutex_init(&component->io_mutex);
2445 component->name = fmt_single_name(dev, &component->id);
2446 if (!component->name) {
2447 dev_err(dev, "ASoC: Failed to allocate name\n");
2448 return -ENOMEM;
2451 component->dev = dev;
2452 component->driver = driver;
2454 return 0;
2456 EXPORT_SYMBOL_GPL(snd_soc_component_initialize);
2458 int snd_soc_add_component(struct snd_soc_component *component,
2459 struct snd_soc_dai_driver *dai_drv,
2460 int num_dai)
2462 int ret;
2463 int i;
2465 mutex_lock(&client_mutex);
2467 if (component->driver->endianness) {
2468 for (i = 0; i < num_dai; i++) {
2469 convert_endianness_formats(&dai_drv[i].playback);
2470 convert_endianness_formats(&dai_drv[i].capture);
2474 ret = snd_soc_register_dais(component, dai_drv, num_dai);
2475 if (ret < 0) {
2476 dev_err(component->dev, "ASoC: Failed to register DAIs: %d\n",
2477 ret);
2478 goto err_cleanup;
2481 if (!component->driver->write && !component->driver->read) {
2482 if (!component->regmap)
2483 component->regmap = dev_get_regmap(component->dev,
2484 NULL);
2485 if (component->regmap)
2486 snd_soc_component_setup_regmap(component);
2489 /* see for_each_component */
2490 list_add(&component->list, &component_list);
2492 err_cleanup:
2493 if (ret < 0)
2494 snd_soc_del_component_unlocked(component);
2496 mutex_unlock(&client_mutex);
2498 if (ret == 0)
2499 snd_soc_try_rebind_card();
2501 return ret;
2503 EXPORT_SYMBOL_GPL(snd_soc_add_component);
2505 int snd_soc_register_component(struct device *dev,
2506 const struct snd_soc_component_driver *component_driver,
2507 struct snd_soc_dai_driver *dai_drv,
2508 int num_dai)
2510 struct snd_soc_component *component;
2511 int ret;
2513 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2514 if (!component)
2515 return -ENOMEM;
2517 ret = snd_soc_component_initialize(component, component_driver, dev);
2518 if (ret < 0)
2519 return ret;
2521 return snd_soc_add_component(component, dai_drv, num_dai);
2523 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2526 * snd_soc_unregister_component_by_driver - Unregister component using a given driver
2527 * from the ASoC core
2529 * @dev: The device to unregister
2530 * @component_driver: The component driver to unregister
2532 void snd_soc_unregister_component_by_driver(struct device *dev,
2533 const struct snd_soc_component_driver *component_driver)
2535 struct snd_soc_component *component;
2537 if (!component_driver)
2538 return;
2540 mutex_lock(&client_mutex);
2541 component = snd_soc_lookup_component_nolocked(dev, component_driver->name);
2542 if (!component)
2543 goto out;
2545 snd_soc_del_component_unlocked(component);
2547 out:
2548 mutex_unlock(&client_mutex);
2550 EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver);
2553 * snd_soc_unregister_component - Unregister all related component
2554 * from the ASoC core
2556 * @dev: The device to unregister
2558 void snd_soc_unregister_component(struct device *dev)
2560 struct snd_soc_component *component;
2562 mutex_lock(&client_mutex);
2563 while (1) {
2564 component = snd_soc_lookup_component_nolocked(dev, NULL);
2565 if (!component)
2566 break;
2568 snd_soc_del_component_unlocked(component);
2570 mutex_unlock(&client_mutex);
2572 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2574 /* Retrieve a card's name from device tree */
2575 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2576 const char *propname)
2578 struct device_node *np;
2579 int ret;
2581 if (!card->dev) {
2582 pr_err("card->dev is not set before calling %s\n", __func__);
2583 return -EINVAL;
2586 np = card->dev->of_node;
2588 ret = of_property_read_string_index(np, propname, 0, &card->name);
2590 * EINVAL means the property does not exist. This is fine providing
2591 * card->name was previously set, which is checked later in
2592 * snd_soc_register_card.
2594 if (ret < 0 && ret != -EINVAL) {
2595 dev_err(card->dev,
2596 "ASoC: Property '%s' could not be read: %d\n",
2597 propname, ret);
2598 return ret;
2601 return 0;
2603 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2605 static const struct snd_soc_dapm_widget simple_widgets[] = {
2606 SND_SOC_DAPM_MIC("Microphone", NULL),
2607 SND_SOC_DAPM_LINE("Line", NULL),
2608 SND_SOC_DAPM_HP("Headphone", NULL),
2609 SND_SOC_DAPM_SPK("Speaker", NULL),
2612 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2613 const char *propname)
2615 struct device_node *np = card->dev->of_node;
2616 struct snd_soc_dapm_widget *widgets;
2617 const char *template, *wname;
2618 int i, j, num_widgets, ret;
2620 num_widgets = of_property_count_strings(np, propname);
2621 if (num_widgets < 0) {
2622 dev_err(card->dev,
2623 "ASoC: Property '%s' does not exist\n", propname);
2624 return -EINVAL;
2626 if (num_widgets & 1) {
2627 dev_err(card->dev,
2628 "ASoC: Property '%s' length is not even\n", propname);
2629 return -EINVAL;
2632 num_widgets /= 2;
2633 if (!num_widgets) {
2634 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2635 propname);
2636 return -EINVAL;
2639 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2640 GFP_KERNEL);
2641 if (!widgets) {
2642 dev_err(card->dev,
2643 "ASoC: Could not allocate memory for widgets\n");
2644 return -ENOMEM;
2647 for (i = 0; i < num_widgets; i++) {
2648 ret = of_property_read_string_index(np, propname,
2649 2 * i, &template);
2650 if (ret) {
2651 dev_err(card->dev,
2652 "ASoC: Property '%s' index %d read error:%d\n",
2653 propname, 2 * i, ret);
2654 return -EINVAL;
2657 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2658 if (!strncmp(template, simple_widgets[j].name,
2659 strlen(simple_widgets[j].name))) {
2660 widgets[i] = simple_widgets[j];
2661 break;
2665 if (j >= ARRAY_SIZE(simple_widgets)) {
2666 dev_err(card->dev,
2667 "ASoC: DAPM widget '%s' is not supported\n",
2668 template);
2669 return -EINVAL;
2672 ret = of_property_read_string_index(np, propname,
2673 (2 * i) + 1,
2674 &wname);
2675 if (ret) {
2676 dev_err(card->dev,
2677 "ASoC: Property '%s' index %d read error:%d\n",
2678 propname, (2 * i) + 1, ret);
2679 return -EINVAL;
2682 widgets[i].name = wname;
2685 card->of_dapm_widgets = widgets;
2686 card->num_of_dapm_widgets = num_widgets;
2688 return 0;
2690 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
2692 int snd_soc_of_get_slot_mask(struct device_node *np,
2693 const char *prop_name,
2694 unsigned int *mask)
2696 u32 val;
2697 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
2698 int i;
2700 if (!of_slot_mask)
2701 return 0;
2702 val /= sizeof(u32);
2703 for (i = 0; i < val; i++)
2704 if (be32_to_cpup(&of_slot_mask[i]))
2705 *mask |= (1 << i);
2707 return val;
2709 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
2711 int snd_soc_of_parse_tdm_slot(struct device_node *np,
2712 unsigned int *tx_mask,
2713 unsigned int *rx_mask,
2714 unsigned int *slots,
2715 unsigned int *slot_width)
2717 u32 val;
2718 int ret;
2720 if (tx_mask)
2721 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
2722 if (rx_mask)
2723 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
2725 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
2726 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
2727 if (ret)
2728 return ret;
2730 if (slots)
2731 *slots = val;
2734 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
2735 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
2736 if (ret)
2737 return ret;
2739 if (slot_width)
2740 *slot_width = val;
2743 return 0;
2745 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
2747 void snd_soc_of_parse_node_prefix(struct device_node *np,
2748 struct snd_soc_codec_conf *codec_conf,
2749 struct device_node *of_node,
2750 const char *propname)
2752 const char *str;
2753 int ret;
2755 ret = of_property_read_string(np, propname, &str);
2756 if (ret < 0) {
2757 /* no prefix is not error */
2758 return;
2761 codec_conf->dlc.of_node = of_node;
2762 codec_conf->name_prefix = str;
2764 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
2766 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
2767 const char *propname)
2769 struct device_node *np = card->dev->of_node;
2770 int num_routes;
2771 struct snd_soc_dapm_route *routes;
2772 int i, ret;
2774 num_routes = of_property_count_strings(np, propname);
2775 if (num_routes < 0 || num_routes & 1) {
2776 dev_err(card->dev,
2777 "ASoC: Property '%s' does not exist or its length is not even\n",
2778 propname);
2779 return -EINVAL;
2781 num_routes /= 2;
2782 if (!num_routes) {
2783 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2784 propname);
2785 return -EINVAL;
2788 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
2789 GFP_KERNEL);
2790 if (!routes) {
2791 dev_err(card->dev,
2792 "ASoC: Could not allocate DAPM route table\n");
2793 return -EINVAL;
2796 for (i = 0; i < num_routes; i++) {
2797 ret = of_property_read_string_index(np, propname,
2798 2 * i, &routes[i].sink);
2799 if (ret) {
2800 dev_err(card->dev,
2801 "ASoC: Property '%s' index %d could not be read: %d\n",
2802 propname, 2 * i, ret);
2803 return -EINVAL;
2805 ret = of_property_read_string_index(np, propname,
2806 (2 * i) + 1, &routes[i].source);
2807 if (ret) {
2808 dev_err(card->dev,
2809 "ASoC: Property '%s' index %d could not be read: %d\n",
2810 propname, (2 * i) + 1, ret);
2811 return -EINVAL;
2815 card->num_of_dapm_routes = num_routes;
2816 card->of_dapm_routes = routes;
2818 return 0;
2820 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
2822 int snd_soc_of_parse_aux_devs(struct snd_soc_card *card, const char *propname)
2824 struct device_node *node = card->dev->of_node;
2825 struct snd_soc_aux_dev *aux;
2826 int num, i;
2828 num = of_count_phandle_with_args(node, propname, NULL);
2829 if (num == -ENOENT) {
2830 return 0;
2831 } else if (num < 0) {
2832 dev_err(card->dev, "ASOC: Property '%s' could not be read: %d\n",
2833 propname, num);
2834 return num;
2837 aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
2838 if (!aux)
2839 return -ENOMEM;
2840 card->aux_dev = aux;
2841 card->num_aux_devs = num;
2843 for_each_card_pre_auxs(card, i, aux) {
2844 aux->dlc.of_node = of_parse_phandle(node, propname, i);
2845 if (!aux->dlc.of_node)
2846 return -EINVAL;
2849 return 0;
2851 EXPORT_SYMBOL_GPL(snd_soc_of_parse_aux_devs);
2853 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
2854 const char *prefix,
2855 struct device_node **bitclkmaster,
2856 struct device_node **framemaster)
2858 int ret, i;
2859 char prop[128];
2860 unsigned int format = 0;
2861 int bit, frame;
2862 const char *str;
2863 struct {
2864 char *name;
2865 unsigned int val;
2866 } of_fmt_table[] = {
2867 { "i2s", SND_SOC_DAIFMT_I2S },
2868 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
2869 { "left_j", SND_SOC_DAIFMT_LEFT_J },
2870 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
2871 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
2872 { "ac97", SND_SOC_DAIFMT_AC97 },
2873 { "pdm", SND_SOC_DAIFMT_PDM},
2874 { "msb", SND_SOC_DAIFMT_MSB },
2875 { "lsb", SND_SOC_DAIFMT_LSB },
2878 if (!prefix)
2879 prefix = "";
2882 * check "dai-format = xxx"
2883 * or "[prefix]format = xxx"
2884 * SND_SOC_DAIFMT_FORMAT_MASK area
2886 ret = of_property_read_string(np, "dai-format", &str);
2887 if (ret < 0) {
2888 snprintf(prop, sizeof(prop), "%sformat", prefix);
2889 ret = of_property_read_string(np, prop, &str);
2891 if (ret == 0) {
2892 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
2893 if (strcmp(str, of_fmt_table[i].name) == 0) {
2894 format |= of_fmt_table[i].val;
2895 break;
2901 * check "[prefix]continuous-clock"
2902 * SND_SOC_DAIFMT_CLOCK_MASK area
2904 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
2905 if (of_property_read_bool(np, prop))
2906 format |= SND_SOC_DAIFMT_CONT;
2907 else
2908 format |= SND_SOC_DAIFMT_GATED;
2911 * check "[prefix]bitclock-inversion"
2912 * check "[prefix]frame-inversion"
2913 * SND_SOC_DAIFMT_INV_MASK area
2915 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
2916 bit = !!of_get_property(np, prop, NULL);
2918 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
2919 frame = !!of_get_property(np, prop, NULL);
2921 switch ((bit << 4) + frame) {
2922 case 0x11:
2923 format |= SND_SOC_DAIFMT_IB_IF;
2924 break;
2925 case 0x10:
2926 format |= SND_SOC_DAIFMT_IB_NF;
2927 break;
2928 case 0x01:
2929 format |= SND_SOC_DAIFMT_NB_IF;
2930 break;
2931 default:
2932 /* SND_SOC_DAIFMT_NB_NF is default */
2933 break;
2937 * check "[prefix]bitclock-master"
2938 * check "[prefix]frame-master"
2939 * SND_SOC_DAIFMT_MASTER_MASK area
2941 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
2942 bit = !!of_get_property(np, prop, NULL);
2943 if (bit && bitclkmaster)
2944 *bitclkmaster = of_parse_phandle(np, prop, 0);
2946 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
2947 frame = !!of_get_property(np, prop, NULL);
2948 if (frame && framemaster)
2949 *framemaster = of_parse_phandle(np, prop, 0);
2951 switch ((bit << 4) + frame) {
2952 case 0x11:
2953 format |= SND_SOC_DAIFMT_CBM_CFM;
2954 break;
2955 case 0x10:
2956 format |= SND_SOC_DAIFMT_CBM_CFS;
2957 break;
2958 case 0x01:
2959 format |= SND_SOC_DAIFMT_CBS_CFM;
2960 break;
2961 default:
2962 format |= SND_SOC_DAIFMT_CBS_CFS;
2963 break;
2966 return format;
2968 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
2970 int snd_soc_get_dai_id(struct device_node *ep)
2972 struct snd_soc_component *component;
2973 struct snd_soc_dai_link_component dlc;
2974 int ret;
2976 dlc.of_node = of_graph_get_port_parent(ep);
2977 dlc.name = NULL;
2979 * For example HDMI case, HDMI has video/sound port,
2980 * but ALSA SoC needs sound port number only.
2981 * Thus counting HDMI DT port/endpoint doesn't work.
2982 * Then, it should have .of_xlate_dai_id
2984 ret = -ENOTSUPP;
2985 mutex_lock(&client_mutex);
2986 component = soc_find_component(&dlc);
2987 if (component)
2988 ret = snd_soc_component_of_xlate_dai_id(component, ep);
2989 mutex_unlock(&client_mutex);
2991 of_node_put(dlc.of_node);
2993 return ret;
2995 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
2997 int snd_soc_get_dai_name(struct of_phandle_args *args,
2998 const char **dai_name)
3000 struct snd_soc_component *pos;
3001 struct device_node *component_of_node;
3002 int ret = -EPROBE_DEFER;
3004 mutex_lock(&client_mutex);
3005 for_each_component(pos) {
3006 component_of_node = soc_component_to_node(pos);
3008 if (component_of_node != args->np)
3009 continue;
3011 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3012 if (ret == -ENOTSUPP) {
3013 struct snd_soc_dai *dai;
3014 int id = -1;
3016 switch (args->args_count) {
3017 case 0:
3018 id = 0; /* same as dai_drv[0] */
3019 break;
3020 case 1:
3021 id = args->args[0];
3022 break;
3023 default:
3024 /* not supported */
3025 break;
3028 if (id < 0 || id >= pos->num_dai) {
3029 ret = -EINVAL;
3030 continue;
3033 ret = 0;
3035 /* find target DAI */
3036 for_each_component_dais(pos, dai) {
3037 if (id == 0)
3038 break;
3039 id--;
3042 *dai_name = dai->driver->name;
3043 if (!*dai_name)
3044 *dai_name = pos->name;
3045 } else if (ret) {
3047 * if another error than ENOTSUPP is returned go on and
3048 * check if another component is provided with the same
3049 * node. This may happen if a device provides several
3050 * components
3052 continue;
3055 break;
3057 mutex_unlock(&client_mutex);
3058 return ret;
3060 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3062 int snd_soc_of_get_dai_name(struct device_node *of_node,
3063 const char **dai_name)
3065 struct of_phandle_args args;
3066 int ret;
3068 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3069 "#sound-dai-cells", 0, &args);
3070 if (ret)
3071 return ret;
3073 ret = snd_soc_get_dai_name(&args, dai_name);
3075 of_node_put(args.np);
3077 return ret;
3079 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3082 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3083 * @dai_link: DAI link
3085 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3087 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3089 struct snd_soc_dai_link_component *component;
3090 int index;
3092 for_each_link_codecs(dai_link, index, component) {
3093 if (!component->of_node)
3094 break;
3095 of_node_put(component->of_node);
3096 component->of_node = NULL;
3099 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3102 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3103 * @dev: Card device
3104 * @of_node: Device node
3105 * @dai_link: DAI link
3107 * Builds an array of CODEC DAI components from the DAI link property
3108 * 'sound-dai'.
3109 * The array is set in the DAI link and the number of DAIs is set accordingly.
3110 * The device nodes in the array (of_node) must be dereferenced by calling
3111 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3113 * Returns 0 for success
3115 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3116 struct device_node *of_node,
3117 struct snd_soc_dai_link *dai_link)
3119 struct of_phandle_args args;
3120 struct snd_soc_dai_link_component *component;
3121 char *name;
3122 int index, num_codecs, ret;
3124 /* Count the number of CODECs */
3125 name = "sound-dai";
3126 num_codecs = of_count_phandle_with_args(of_node, name,
3127 "#sound-dai-cells");
3128 if (num_codecs <= 0) {
3129 if (num_codecs == -ENOENT)
3130 dev_err(dev, "No 'sound-dai' property\n");
3131 else
3132 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3133 return num_codecs;
3135 component = devm_kcalloc(dev,
3136 num_codecs, sizeof(*component),
3137 GFP_KERNEL);
3138 if (!component)
3139 return -ENOMEM;
3140 dai_link->codecs = component;
3141 dai_link->num_codecs = num_codecs;
3143 /* Parse the list */
3144 for_each_link_codecs(dai_link, index, component) {
3145 ret = of_parse_phandle_with_args(of_node, name,
3146 "#sound-dai-cells",
3147 index, &args);
3148 if (ret)
3149 goto err;
3150 component->of_node = args.np;
3151 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3152 if (ret < 0)
3153 goto err;
3155 return 0;
3156 err:
3157 snd_soc_of_put_dai_link_codecs(dai_link);
3158 dai_link->codecs = NULL;
3159 dai_link->num_codecs = 0;
3160 return ret;
3162 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3164 static int __init snd_soc_init(void)
3166 snd_soc_debugfs_init();
3167 snd_soc_util_init();
3169 return platform_driver_register(&soc_driver);
3171 module_init(snd_soc_init);
3173 static void __exit snd_soc_exit(void)
3175 snd_soc_util_exit();
3176 snd_soc_debugfs_exit();
3178 platform_driver_unregister(&soc_driver);
3180 module_exit(snd_soc_exit);
3182 /* Module information */
3183 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3184 MODULE_DESCRIPTION("ALSA SoC Core");
3185 MODULE_LICENSE("GPL");
3186 MODULE_ALIAS("platform:soc-audio");