1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-core.c -- ALSA SoC Audio Layer
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
10 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11 // with code, comments and ideas from :-
12 // Richard Purdie <richard@openedhand.com>
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>
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>
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>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/asoc.h>
48 static DEFINE_MUTEX(client_mutex
);
49 static LIST_HEAD(component_list
);
50 static LIST_HEAD(unbind_card_list
);
52 #define for_each_component(component) \
53 list_for_each_entry(component, &component_list, list)
56 * This is used if driver don't need to have CPU/Codec/Platform
59 struct snd_soc_dai_link_component null_dailink_component
[0];
60 EXPORT_SYMBOL_GPL(null_dailink_component
);
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.
67 static int pmdown_time
= 5000;
68 module_param(pmdown_time
, int, 0);
69 MODULE_PARM_DESC(pmdown_time
, "DAPM stream powerdown time (msecs)");
71 static ssize_t
pmdown_time_show(struct device
*dev
,
72 struct device_attribute
*attr
, char *buf
)
74 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
76 return sprintf(buf
, "%ld\n", rtd
->pmdown_time
);
79 static ssize_t
pmdown_time_set(struct device
*dev
,
80 struct device_attribute
*attr
,
81 const char *buf
, size_t count
)
83 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
86 ret
= kstrtol(buf
, 10, &rtd
->pmdown_time
);
93 static DEVICE_ATTR(pmdown_time
, 0644, pmdown_time_show
, pmdown_time_set
);
95 static struct attribute
*soc_dev_attrs
[] = {
96 &dev_attr_pmdown_time
.attr
,
100 static umode_t
soc_dev_attr_is_visible(struct kobject
*kobj
,
101 struct attribute
*attr
, int idx
)
103 struct device
*dev
= kobj_to_dev(kobj
);
104 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
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 */
114 static const struct attribute_group soc_dapm_dev_group
= {
115 .attrs
= soc_dapm_dev_attrs
,
116 .is_visible
= soc_dev_attr_is_visible
,
119 static const struct attribute_group soc_dev_group
= {
120 .attrs
= soc_dev_attrs
,
121 .is_visible
= soc_dev_attr_is_visible
,
124 static const struct attribute_group
*soc_dev_attr_groups
[] = {
130 #ifdef CONFIG_DEBUG_FS
131 struct dentry
*snd_soc_debugfs_root
;
132 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root
);
134 static void soc_init_component_debugfs(struct snd_soc_component
*component
)
136 if (!component
->card
->debugfs_card_root
)
139 if (component
->debugfs_prefix
) {
142 name
= kasprintf(GFP_KERNEL
, "%s:%s",
143 component
->debugfs_prefix
, component
->name
);
145 component
->debugfs_root
= debugfs_create_dir(name
,
146 component
->card
->debugfs_card_root
);
150 component
->debugfs_root
= debugfs_create_dir(component
->name
,
151 component
->card
->debugfs_card_root
);
154 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component
),
155 component
->debugfs_root
);
158 static void soc_cleanup_component_debugfs(struct snd_soc_component
*component
)
160 if (!component
->debugfs_root
)
162 debugfs_remove_recursive(component
->debugfs_root
);
163 component
->debugfs_root
= NULL
;
166 static int dai_list_show(struct seq_file
*m
, void *v
)
168 struct snd_soc_component
*component
;
169 struct snd_soc_dai
*dai
;
171 mutex_lock(&client_mutex
);
173 for_each_component(component
)
174 for_each_component_dais(component
, dai
)
175 seq_printf(m
, "%s\n", dai
->name
);
177 mutex_unlock(&client_mutex
);
181 DEFINE_SHOW_ATTRIBUTE(dai_list
);
183 static int component_list_show(struct seq_file
*m
, void *v
)
185 struct snd_soc_component
*component
;
187 mutex_lock(&client_mutex
);
189 for_each_component(component
)
190 seq_printf(m
, "%s\n", component
->name
);
192 mutex_unlock(&client_mutex
);
196 DEFINE_SHOW_ATTRIBUTE(component_list
);
198 static void soc_init_card_debugfs(struct snd_soc_card
*card
)
200 card
->debugfs_card_root
= debugfs_create_dir(card
->name
,
201 snd_soc_debugfs_root
);
203 debugfs_create_u32("dapm_pop_time", 0644, card
->debugfs_card_root
,
206 snd_soc_dapm_debugfs_init(&card
->dapm
, card
->debugfs_card_root
);
209 static void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
211 debugfs_remove_recursive(card
->debugfs_card_root
);
212 card
->debugfs_card_root
= NULL
;
215 static void snd_soc_debugfs_init(void)
217 snd_soc_debugfs_root
= debugfs_create_dir("asoc", NULL
);
219 debugfs_create_file("dais", 0444, snd_soc_debugfs_root
, NULL
,
222 debugfs_create_file("components", 0444, snd_soc_debugfs_root
, NULL
,
223 &component_list_fops
);
226 static void snd_soc_debugfs_exit(void)
228 debugfs_remove_recursive(snd_soc_debugfs_root
);
233 static inline void soc_init_component_debugfs(
234 struct snd_soc_component
*component
)
238 static inline void soc_cleanup_component_debugfs(
239 struct snd_soc_component
*component
)
243 static inline void soc_init_card_debugfs(struct snd_soc_card
*card
)
247 static inline void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
251 static inline void snd_soc_debugfs_init(void)
255 static inline void snd_soc_debugfs_exit(void)
261 static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime
*rtd
,
262 struct snd_soc_component
*component
)
264 struct snd_soc_component
*comp
;
267 for_each_rtd_components(rtd
, i
, comp
) {
268 /* already connected */
269 if (comp
== component
)
273 /* see for_each_rtd_components */
274 rtd
->components
[rtd
->num_components
] = component
;
275 rtd
->num_components
++;
280 struct snd_soc_component
*snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime
*rtd
,
281 const char *driver_name
)
283 struct snd_soc_component
*component
;
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.
297 for_each_rtd_components(rtd
, i
, component
) {
298 const char *component_name
= component
->driver
->name
;
303 if ((component_name
== driver_name
) ||
304 strcmp(component_name
, driver_name
) == 0)
310 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup
);
312 static struct snd_soc_component
313 *snd_soc_lookup_component_nolocked(struct device
*dev
, const char *driver_name
)
315 struct snd_soc_component
*component
;
316 struct snd_soc_component
*found_component
;
318 found_component
= NULL
;
319 for_each_component(component
) {
320 if ((dev
== component
->dev
) &&
322 (driver_name
== component
->driver
->name
) ||
323 (strcmp(component
->driver
->name
, driver_name
) == 0))) {
324 found_component
= component
;
329 return found_component
;
332 struct snd_soc_component
*snd_soc_lookup_component(struct device
*dev
,
333 const char *driver_name
)
335 struct snd_soc_component
*component
;
337 mutex_lock(&client_mutex
);
338 component
= snd_soc_lookup_component_nolocked(dev
, driver_name
);
339 mutex_unlock(&client_mutex
);
343 EXPORT_SYMBOL_GPL(snd_soc_lookup_component
);
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
)
349 struct snd_soc_pcm_runtime
*rtd
;
351 for_each_card_rtds(card
, rtd
) {
352 if (rtd
->dai_link
== dai_link
)
355 dev_dbg(card
->dev
, "ASoC: failed to find rtd %s\n", dai_link
->name
);
358 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime
);
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.
365 void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime
*rtd
)
367 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
369 mutex_lock_nested(&rtd
->card
->pcm_mutex
, rtd
->card
->pcm_subclass
);
372 "ASoC: pop wq checking: %s status: %s waiting: %s\n",
373 codec_dai
->driver
->playback
.stream_name
,
374 codec_dai
->playback_active
? "active" : "inactive",
375 rtd
->pop_wait
? "yes" : "no");
377 /* are we waiting on this codec DAI stream */
378 if (rtd
->pop_wait
== 1) {
380 snd_soc_dapm_stream_event(rtd
, SNDRV_PCM_STREAM_PLAYBACK
,
381 SND_SOC_DAPM_STREAM_STOP
);
384 mutex_unlock(&rtd
->card
->pcm_mutex
);
386 EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work
);
388 static void soc_release_rtd_dev(struct device
*dev
)
390 /* "dev" means "rtd->dev" */
394 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime
*rtd
)
399 list_del(&rtd
->list
);
401 if (delayed_work_pending(&rtd
->delayed_work
))
402 flush_delayed_work(&rtd
->delayed_work
);
403 snd_soc_pcm_component_free(rtd
);
406 * we don't need to call kfree() for rtd->dev
408 * soc_release_rtd_dev()
410 * We don't need rtd->dev NULL check, because
411 * it is alloced *before* rtd.
413 * soc_new_pcm_runtime()
415 device_unregister(rtd
->dev
);
418 static void close_delayed_work(struct work_struct
*work
) {
419 struct snd_soc_pcm_runtime
*rtd
=
420 container_of(work
, struct snd_soc_pcm_runtime
,
423 if (rtd
->close_delayed_work_func
)
424 rtd
->close_delayed_work_func(rtd
);
427 static struct snd_soc_pcm_runtime
*soc_new_pcm_runtime(
428 struct snd_soc_card
*card
, struct snd_soc_dai_link
*dai_link
)
430 struct snd_soc_pcm_runtime
*rtd
;
431 struct snd_soc_component
*component
;
438 dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
442 dev
->parent
= card
->dev
;
443 dev
->release
= soc_release_rtd_dev
;
444 dev
->groups
= soc_dev_attr_groups
;
446 dev_set_name(dev
, "%s", dai_link
->name
);
448 ret
= device_register(dev
);
450 put_device(dev
); /* soc_release_rtd_dev */
457 rtd
= devm_kzalloc(dev
,
459 sizeof(*component
) * (dai_link
->num_cpus
+
460 dai_link
->num_codecs
+
461 dai_link
->num_platforms
),
467 INIT_LIST_HEAD(&rtd
->list
);
468 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_PLAYBACK
].be_clients
);
469 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_CAPTURE
].be_clients
);
470 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_PLAYBACK
].fe_clients
);
471 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_CAPTURE
].fe_clients
);
472 dev_set_drvdata(dev
, rtd
);
473 INIT_DELAYED_WORK(&rtd
->delayed_work
, close_delayed_work
);
476 * for rtd->codec_dais
478 rtd
->codec_dais
= devm_kcalloc(dev
, dai_link
->num_codecs
,
479 sizeof(struct snd_soc_dai
*),
481 if (!rtd
->codec_dais
)
485 * rtd remaining settings
488 rtd
->dai_link
= dai_link
;
490 /* see for_each_card_rtds */
491 list_add_tail(&rtd
->list
, &card
->rtd_list
);
492 rtd
->num
= card
->num_rtd
;
498 soc_free_pcm_runtime(rtd
);
502 static void snd_soc_flush_all_delayed_work(struct snd_soc_card
*card
)
504 struct snd_soc_pcm_runtime
*rtd
;
506 for_each_card_rtds(card
, rtd
)
507 flush_delayed_work(&rtd
->delayed_work
);
510 #ifdef CONFIG_PM_SLEEP
511 /* powers down audio subsystem for suspend */
512 int snd_soc_suspend(struct device
*dev
)
514 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
515 struct snd_soc_component
*component
;
516 struct snd_soc_pcm_runtime
*rtd
;
519 /* If the card is not initialized yet there is nothing to do */
520 if (!card
->instantiated
)
524 * Due to the resume being scheduled into a workqueue we could
525 * suspend before that's finished - wait for it to complete.
527 snd_power_wait(card
->snd_card
, SNDRV_CTL_POWER_D0
);
529 /* we're going to block userspace touching us until resume completes */
530 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D3hot
);
532 /* mute any active DACs */
533 for_each_card_rtds(card
, rtd
) {
534 struct snd_soc_dai
*dai
;
536 if (rtd
->dai_link
->ignore_suspend
)
539 for_each_rtd_codec_dai(rtd
, i
, dai
) {
540 if (dai
->playback_active
)
541 snd_soc_dai_digital_mute(dai
, 1,
542 SNDRV_PCM_STREAM_PLAYBACK
);
546 /* suspend all pcms */
547 for_each_card_rtds(card
, rtd
) {
548 if (rtd
->dai_link
->ignore_suspend
)
551 snd_pcm_suspend_all(rtd
->pcm
);
554 if (card
->suspend_pre
)
555 card
->suspend_pre(card
);
557 /* close any waiting streams */
558 snd_soc_flush_all_delayed_work(card
);
560 for_each_card_rtds(card
, rtd
) {
562 if (rtd
->dai_link
->ignore_suspend
)
565 snd_soc_dapm_stream_event(rtd
,
566 SNDRV_PCM_STREAM_PLAYBACK
,
567 SND_SOC_DAPM_STREAM_SUSPEND
);
569 snd_soc_dapm_stream_event(rtd
,
570 SNDRV_PCM_STREAM_CAPTURE
,
571 SND_SOC_DAPM_STREAM_SUSPEND
);
574 /* Recheck all endpoints too, their state is affected by suspend */
575 dapm_mark_endpoints_dirty(card
);
576 snd_soc_dapm_sync(&card
->dapm
);
578 /* suspend all COMPONENTs */
579 for_each_card_rtds(card
, rtd
) {
581 if (rtd
->dai_link
->ignore_suspend
)
584 for_each_rtd_components(rtd
, i
, component
) {
585 struct snd_soc_dapm_context
*dapm
=
586 snd_soc_component_get_dapm(component
);
589 * ignore if component was already suspended
591 if (snd_soc_component_is_suspended(component
))
595 * If there are paths active then the COMPONENT will be
596 * held with bias _ON and should not be suspended.
598 switch (snd_soc_dapm_get_bias_level(dapm
)) {
599 case SND_SOC_BIAS_STANDBY
:
601 * If the COMPONENT is capable of idle
602 * bias off then being in STANDBY
603 * means it's doing something,
604 * otherwise fall through.
606 if (dapm
->idle_bias_off
) {
607 dev_dbg(component
->dev
,
608 "ASoC: idle_bias_off CODEC on over suspend\n");
613 case SND_SOC_BIAS_OFF
:
614 snd_soc_component_suspend(component
);
615 if (component
->regmap
)
616 regcache_mark_dirty(component
->regmap
);
617 /* deactivate pins to sleep state */
618 pinctrl_pm_select_sleep_state(component
->dev
);
621 dev_dbg(component
->dev
,
622 "ASoC: COMPONENT is on over suspend\n");
628 if (card
->suspend_post
)
629 card
->suspend_post(card
);
633 EXPORT_SYMBOL_GPL(snd_soc_suspend
);
636 * deferred resume work, so resume can complete before we finished
637 * setting our codec back up, which can be very slow on I2C
639 static void soc_resume_deferred(struct work_struct
*work
)
641 struct snd_soc_card
*card
=
642 container_of(work
, struct snd_soc_card
,
643 deferred_resume_work
);
644 struct snd_soc_pcm_runtime
*rtd
;
645 struct snd_soc_component
*component
;
649 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
650 * so userspace apps are blocked from touching us
653 dev_dbg(card
->dev
, "ASoC: starting resume work\n");
655 /* Bring us up into D2 so that DAPM starts enabling things */
656 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D2
);
658 if (card
->resume_pre
)
659 card
->resume_pre(card
);
661 for_each_card_components(card
, component
) {
662 if (snd_soc_component_is_suspended(component
))
663 snd_soc_component_resume(component
);
666 for_each_card_rtds(card
, rtd
) {
668 if (rtd
->dai_link
->ignore_suspend
)
671 snd_soc_dapm_stream_event(rtd
,
672 SNDRV_PCM_STREAM_PLAYBACK
,
673 SND_SOC_DAPM_STREAM_RESUME
);
675 snd_soc_dapm_stream_event(rtd
,
676 SNDRV_PCM_STREAM_CAPTURE
,
677 SND_SOC_DAPM_STREAM_RESUME
);
680 /* unmute any active DACs */
681 for_each_card_rtds(card
, rtd
) {
682 struct snd_soc_dai
*dai
;
684 if (rtd
->dai_link
->ignore_suspend
)
687 for_each_rtd_codec_dai(rtd
, i
, dai
) {
688 if (dai
->playback_active
)
689 snd_soc_dai_digital_mute(dai
, 0,
690 SNDRV_PCM_STREAM_PLAYBACK
);
694 if (card
->resume_post
)
695 card
->resume_post(card
);
697 dev_dbg(card
->dev
, "ASoC: resume work completed\n");
699 /* Recheck all endpoints too, their state is affected by suspend */
700 dapm_mark_endpoints_dirty(card
);
701 snd_soc_dapm_sync(&card
->dapm
);
703 /* userspace can access us now we are back as we were before */
704 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D0
);
707 /* powers up audio subsystem after a suspend */
708 int snd_soc_resume(struct device
*dev
)
710 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
711 struct snd_soc_component
*component
;
713 /* If the card is not initialized yet there is nothing to do */
714 if (!card
->instantiated
)
717 /* activate pins from sleep state */
718 for_each_card_components(card
, component
)
719 if (component
->active
)
720 pinctrl_pm_select_default_state(component
->dev
);
722 dev_dbg(dev
, "ASoC: Scheduling resume work\n");
723 if (!schedule_work(&card
->deferred_resume_work
))
724 dev_err(dev
, "ASoC: resume work item may be lost\n");
728 EXPORT_SYMBOL_GPL(snd_soc_resume
);
730 static void soc_resume_init(struct snd_soc_card
*card
)
732 /* deferred resume work */
733 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
736 #define snd_soc_suspend NULL
737 #define snd_soc_resume NULL
738 static inline void soc_resume_init(struct snd_soc_card
*card
)
743 static const struct snd_soc_dai_ops null_dai_ops
= {
746 static struct device_node
747 *soc_component_to_node(struct snd_soc_component
*component
)
749 struct device_node
*of_node
;
751 of_node
= component
->dev
->of_node
;
752 if (!of_node
&& component
->dev
->parent
)
753 of_node
= component
->dev
->parent
->of_node
;
758 static int snd_soc_is_matching_component(
759 const struct snd_soc_dai_link_component
*dlc
,
760 struct snd_soc_component
*component
)
762 struct device_node
*component_of_node
;
767 component_of_node
= soc_component_to_node(component
);
769 if (dlc
->of_node
&& component_of_node
!= dlc
->of_node
)
771 if (dlc
->name
&& strcmp(component
->name
, dlc
->name
))
777 static struct snd_soc_component
*soc_find_component(
778 const struct snd_soc_dai_link_component
*dlc
)
780 struct snd_soc_component
*component
;
782 lockdep_assert_held(&client_mutex
);
787 * It returns *1st* found component, but some driver
788 * has few components by same of_node/name
790 * CPU component and generic DMAEngine component
792 for_each_component(component
)
793 if (snd_soc_is_matching_component(dlc
, component
))
800 * snd_soc_find_dai - Find a registered DAI
802 * @dlc: name of the DAI or the DAI driver and optional component info to match
804 * This function will search all registered components and their DAIs to
805 * find the DAI of the same name. The component's of_node and name
806 * should also match if being specified.
808 * Return: pointer of DAI, or NULL if not found.
810 struct snd_soc_dai
*snd_soc_find_dai(
811 const struct snd_soc_dai_link_component
*dlc
)
813 struct snd_soc_component
*component
;
814 struct snd_soc_dai
*dai
;
816 lockdep_assert_held(&client_mutex
);
818 /* Find CPU DAI from registered DAIs */
819 for_each_component(component
) {
820 if (!snd_soc_is_matching_component(dlc
, component
))
822 for_each_component_dais(component
, dai
) {
823 if (dlc
->dai_name
&& strcmp(dai
->name
, dlc
->dai_name
)
824 && (!dai
->driver
->name
825 || strcmp(dai
->driver
->name
, dlc
->dai_name
)))
834 EXPORT_SYMBOL_GPL(snd_soc_find_dai
);
836 static int soc_dai_link_sanity_check(struct snd_soc_card
*card
,
837 struct snd_soc_dai_link
*link
)
840 struct snd_soc_dai_link_component
*codec
, *platform
;
842 for_each_link_codecs(link
, i
, codec
) {
844 * Codec must be specified by 1 of name or OF node,
845 * not both or neither.
847 if (!!codec
->name
== !!codec
->of_node
) {
848 dev_err(card
->dev
, "ASoC: Neither/both codec name/of_node are set for %s\n",
853 /* Codec DAI name must be specified */
854 if (!codec
->dai_name
) {
855 dev_err(card
->dev
, "ASoC: codec_dai_name not set for %s\n",
861 * Defer card registration if codec component is not added to
864 if (!soc_find_component(codec
))
865 return -EPROBE_DEFER
;
868 for_each_link_platforms(link
, i
, platform
) {
870 * Platform may be specified by either name or OF node, but it
871 * can be left unspecified, then no components will be inserted
874 if (!!platform
->name
== !!platform
->of_node
) {
876 "ASoC: Neither/both platform name/of_node are set for %s\n",
882 * Defer card registration if platform component is not added to
885 if (!soc_find_component(platform
))
886 return -EPROBE_DEFER
;
890 if (link
->num_cpus
> 1) {
892 "ASoC: multi cpu is not yet supported %s\n",
898 * CPU device may be specified by either name or OF node, but
899 * can be left unspecified, and will be matched based on DAI
902 if (link
->cpus
->name
&& link
->cpus
->of_node
) {
904 "ASoC: Neither/both cpu name/of_node are set for %s\n",
910 * Defer card registration if cpu dai component is not added to
913 if ((link
->cpus
->of_node
|| link
->cpus
->name
) &&
914 !soc_find_component(link
->cpus
))
915 return -EPROBE_DEFER
;
918 * At least one of CPU DAI name or CPU device name/node must be
921 if (!link
->cpus
->dai_name
&&
922 !(link
->cpus
->name
|| link
->cpus
->of_node
)) {
924 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
933 * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
934 * @card: The ASoC card to which the pcm_runtime has
935 * @rtd: The pcm_runtime to remove
937 * This function removes a pcm_runtime from the ASoC card.
939 void snd_soc_remove_pcm_runtime(struct snd_soc_card
*card
,
940 struct snd_soc_pcm_runtime
*rtd
)
942 lockdep_assert_held(&client_mutex
);
945 * Notify the machine driver for extra destruction
947 if (card
->remove_dai_link
)
948 card
->remove_dai_link(card
, rtd
->dai_link
);
950 soc_free_pcm_runtime(rtd
);
952 EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime
);
955 * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
956 * @card: The ASoC card to which the pcm_runtime is added
957 * @dai_link: The DAI link to find pcm_runtime
959 * This function adds a pcm_runtime ASoC card by using dai_link.
961 * Note: Topology can use this API to add pcm_runtime when probing the
962 * topology component. And machine drivers can still define static
963 * DAI links in dai_link array.
965 int snd_soc_add_pcm_runtime(struct snd_soc_card
*card
,
966 struct snd_soc_dai_link
*dai_link
)
968 struct snd_soc_pcm_runtime
*rtd
;
969 struct snd_soc_dai_link_component
*codec
, *platform
;
970 struct snd_soc_component
*component
;
973 lockdep_assert_held(&client_mutex
);
976 * Notify the machine driver for extra initialization
978 if (card
->add_dai_link
)
979 card
->add_dai_link(card
, dai_link
);
981 if (dai_link
->ignore
)
984 dev_dbg(card
->dev
, "ASoC: binding %s\n", dai_link
->name
);
986 ret
= soc_dai_link_sanity_check(card
, dai_link
);
990 rtd
= soc_new_pcm_runtime(card
, dai_link
);
994 /* FIXME: we need multi CPU support in the future */
995 rtd
->cpu_dai
= snd_soc_find_dai(dai_link
->cpus
);
997 dev_info(card
->dev
, "ASoC: CPU DAI %s not registered\n",
998 dai_link
->cpus
->dai_name
);
1001 snd_soc_rtd_add_component(rtd
, rtd
->cpu_dai
->component
);
1003 /* Find CODEC from registered CODECs */
1004 rtd
->num_codecs
= dai_link
->num_codecs
;
1005 for_each_link_codecs(dai_link
, i
, codec
) {
1006 rtd
->codec_dais
[i
] = snd_soc_find_dai(codec
);
1007 if (!rtd
->codec_dais
[i
]) {
1008 dev_info(card
->dev
, "ASoC: CODEC DAI %s not registered\n",
1013 snd_soc_rtd_add_component(rtd
, rtd
->codec_dais
[i
]->component
);
1016 /* Single codec links expect codec and codec_dai in runtime data */
1017 rtd
->codec_dai
= rtd
->codec_dais
[0];
1019 /* Find PLATFORM from registered PLATFORMs */
1020 for_each_link_platforms(dai_link
, i
, platform
) {
1021 for_each_component(component
) {
1022 if (!snd_soc_is_matching_component(platform
, component
))
1025 snd_soc_rtd_add_component(rtd
, component
);
1032 snd_soc_remove_pcm_runtime(card
, rtd
);
1033 return -EPROBE_DEFER
;
1035 EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime
);
1037 static int soc_dai_pcm_new(struct snd_soc_dai
**dais
, int num_dais
,
1038 struct snd_soc_pcm_runtime
*rtd
)
1042 for (i
= 0; i
< num_dais
; ++i
) {
1043 struct snd_soc_dai_driver
*drv
= dais
[i
]->driver
;
1046 ret
= drv
->pcm_new(rtd
, dais
[i
]);
1048 dev_err(dais
[i
]->dev
,
1049 "ASoC: Failed to bind %s with pcm device\n",
1058 static int soc_init_pcm_runtime(struct snd_soc_card
*card
,
1059 struct snd_soc_pcm_runtime
*rtd
)
1061 struct snd_soc_dai_link
*dai_link
= rtd
->dai_link
;
1062 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1063 struct snd_soc_component
*component
;
1066 /* set default power off timeout */
1067 rtd
->pmdown_time
= pmdown_time
;
1069 /* do machine specific initialization */
1070 if (dai_link
->init
) {
1071 ret
= dai_link
->init(rtd
);
1073 dev_err(card
->dev
, "ASoC: failed to init %s: %d\n",
1074 dai_link
->name
, ret
);
1079 if (dai_link
->dai_fmt
) {
1080 ret
= snd_soc_runtime_set_dai_fmt(rtd
, dai_link
->dai_fmt
);
1085 /* add DPCM sysfs entries */
1086 soc_dpcm_debugfs_add(rtd
);
1091 * most drivers will register their PCMs using DAI link ordering but
1092 * topology based drivers can use the DAI link id field to set PCM
1093 * device number and then use rtd + a base offset of the BEs.
1095 for_each_rtd_components(rtd
, i
, component
) {
1096 if (!component
->driver
->use_dai_pcm_id
)
1099 if (rtd
->dai_link
->no_pcm
)
1100 num
+= component
->driver
->be_pcm_base
;
1102 num
= rtd
->dai_link
->id
;
1105 /* create compress_device if possible */
1106 ret
= snd_soc_dai_compress_new(cpu_dai
, rtd
, num
);
1107 if (ret
!= -ENOTSUPP
) {
1109 dev_err(card
->dev
, "ASoC: can't create compress %s\n",
1110 dai_link
->stream_name
);
1114 /* create the pcm */
1115 ret
= soc_new_pcm(rtd
, num
);
1117 dev_err(card
->dev
, "ASoC: can't create pcm %s :%d\n",
1118 dai_link
->stream_name
, ret
);
1121 ret
= soc_dai_pcm_new(&cpu_dai
, 1, rtd
);
1124 ret
= soc_dai_pcm_new(rtd
->codec_dais
,
1125 rtd
->num_codecs
, rtd
);
1129 static void soc_set_name_prefix(struct snd_soc_card
*card
,
1130 struct snd_soc_component
*component
)
1132 struct device_node
*of_node
= soc_component_to_node(component
);
1136 for (i
= 0; i
< card
->num_configs
; i
++) {
1137 struct snd_soc_codec_conf
*map
= &card
->codec_conf
[i
];
1139 if (snd_soc_is_matching_component(&map
->dlc
, component
)) {
1140 component
->name_prefix
= map
->name_prefix
;
1146 * If there is no configuration table or no match in the table,
1147 * check if a prefix is provided in the node
1149 ret
= of_property_read_string(of_node
, "sound-name-prefix", &str
);
1153 component
->name_prefix
= str
;
1156 static void soc_remove_component(struct snd_soc_component
*component
,
1160 if (!component
->card
)
1164 snd_soc_component_remove(component
);
1166 /* For framework level robustness */
1167 snd_soc_component_set_jack(component
, NULL
, NULL
);
1169 list_del_init(&component
->card_list
);
1170 snd_soc_dapm_free(snd_soc_component_get_dapm(component
));
1171 soc_cleanup_component_debugfs(component
);
1172 component
->card
= NULL
;
1173 snd_soc_component_module_put_when_remove(component
);
1176 static int soc_probe_component(struct snd_soc_card
*card
,
1177 struct snd_soc_component
*component
)
1179 struct snd_soc_dapm_context
*dapm
=
1180 snd_soc_component_get_dapm(component
);
1181 struct snd_soc_dai
*dai
;
1185 if (!strcmp(component
->name
, "snd-soc-dummy"))
1188 if (component
->card
) {
1189 if (component
->card
!= card
) {
1190 dev_err(component
->dev
,
1191 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1192 card
->name
, component
->card
->name
);
1198 ret
= snd_soc_component_module_get_when_probe(component
);
1202 component
->card
= card
;
1203 soc_set_name_prefix(card
, component
);
1205 soc_init_component_debugfs(component
);
1207 snd_soc_dapm_init(dapm
, card
, component
);
1209 ret
= snd_soc_dapm_new_controls(dapm
,
1210 component
->driver
->dapm_widgets
,
1211 component
->driver
->num_dapm_widgets
);
1214 dev_err(component
->dev
,
1215 "Failed to create new controls %d\n", ret
);
1219 for_each_component_dais(component
, dai
) {
1220 ret
= snd_soc_dapm_new_dai_widgets(dapm
, dai
);
1222 dev_err(component
->dev
,
1223 "Failed to create DAI widgets %d\n", ret
);
1228 ret
= snd_soc_component_probe(component
);
1230 dev_err(component
->dev
,
1231 "ASoC: failed to probe component %d\n", ret
);
1234 WARN(dapm
->idle_bias_off
&&
1235 dapm
->bias_level
!= SND_SOC_BIAS_OFF
,
1236 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1240 /* machine specific init */
1241 if (component
->init
) {
1242 ret
= component
->init(component
);
1244 dev_err(component
->dev
,
1245 "Failed to do machine specific init %d\n", ret
);
1250 ret
= snd_soc_add_component_controls(component
,
1251 component
->driver
->controls
,
1252 component
->driver
->num_controls
);
1256 ret
= snd_soc_dapm_add_routes(dapm
,
1257 component
->driver
->dapm_routes
,
1258 component
->driver
->num_dapm_routes
);
1262 /* see for_each_card_components */
1263 list_add(&component
->card_list
, &card
->component_dev_list
);
1267 soc_remove_component(component
, probed
);
1272 static void soc_remove_dai(struct snd_soc_dai
*dai
, int order
)
1276 if (!dai
|| !dai
->probed
|| !dai
->driver
||
1277 dai
->driver
->remove_order
!= order
)
1280 err
= snd_soc_dai_remove(dai
);
1283 "ASoC: failed to remove %s: %d\n",
1289 static int soc_probe_dai(struct snd_soc_dai
*dai
, int order
)
1294 dai
->driver
->probe_order
!= order
)
1297 ret
= snd_soc_dai_probe(dai
);
1299 dev_err(dai
->dev
, "ASoC: failed to probe DAI %s: %d\n",
1309 static void soc_remove_link_dais(struct snd_soc_card
*card
)
1312 struct snd_soc_dai
*codec_dai
;
1313 struct snd_soc_pcm_runtime
*rtd
;
1316 for_each_comp_order(order
) {
1317 for_each_card_rtds(card
, rtd
) {
1318 /* remove the CODEC DAI */
1319 for_each_rtd_codec_dai(rtd
, i
, codec_dai
)
1320 soc_remove_dai(codec_dai
, order
);
1322 soc_remove_dai(rtd
->cpu_dai
, order
);
1327 static int soc_probe_link_dais(struct snd_soc_card
*card
)
1329 struct snd_soc_dai
*codec_dai
;
1330 struct snd_soc_pcm_runtime
*rtd
;
1333 for_each_comp_order(order
) {
1334 for_each_card_rtds(card
, rtd
) {
1337 "ASoC: probe %s dai link %d late %d\n",
1338 card
->name
, rtd
->num
, order
);
1340 ret
= soc_probe_dai(rtd
->cpu_dai
, order
);
1344 /* probe the CODEC DAI */
1345 for_each_rtd_codec_dai(rtd
, i
, codec_dai
) {
1346 ret
= soc_probe_dai(codec_dai
, order
);
1356 static void soc_remove_link_components(struct snd_soc_card
*card
)
1358 struct snd_soc_component
*component
;
1359 struct snd_soc_pcm_runtime
*rtd
;
1362 for_each_comp_order(order
) {
1363 for_each_card_rtds(card
, rtd
) {
1364 for_each_rtd_components(rtd
, i
, component
) {
1365 if (component
->driver
->remove_order
!= order
)
1368 soc_remove_component(component
, 1);
1374 static int soc_probe_link_components(struct snd_soc_card
*card
)
1376 struct snd_soc_component
*component
;
1377 struct snd_soc_pcm_runtime
*rtd
;
1380 for_each_comp_order(order
) {
1381 for_each_card_rtds(card
, rtd
) {
1382 for_each_rtd_components(rtd
, i
, component
) {
1383 if (component
->driver
->probe_order
!= order
)
1386 ret
= soc_probe_component(card
, component
);
1396 static void soc_unbind_aux_dev(struct snd_soc_card
*card
)
1398 struct snd_soc_component
*component
, *_component
;
1400 for_each_card_auxs_safe(card
, component
, _component
) {
1401 component
->init
= NULL
;
1402 list_del(&component
->card_aux_list
);
1406 static int soc_bind_aux_dev(struct snd_soc_card
*card
)
1408 struct snd_soc_component
*component
;
1409 struct snd_soc_aux_dev
*aux
;
1412 for_each_card_pre_auxs(card
, i
, aux
) {
1413 /* codecs, usually analog devices */
1414 component
= soc_find_component(&aux
->dlc
);
1416 return -EPROBE_DEFER
;
1418 component
->init
= aux
->init
;
1419 /* see for_each_card_auxs */
1420 list_add(&component
->card_aux_list
, &card
->aux_comp_list
);
1425 static int soc_probe_aux_devices(struct snd_soc_card
*card
)
1427 struct snd_soc_component
*component
;
1431 for_each_comp_order(order
) {
1432 for_each_card_auxs(card
, component
) {
1433 if (component
->driver
->probe_order
!= order
)
1436 ret
= soc_probe_component(card
, component
);
1445 static void soc_remove_aux_devices(struct snd_soc_card
*card
)
1447 struct snd_soc_component
*comp
, *_comp
;
1450 for_each_comp_order(order
) {
1451 for_each_card_auxs_safe(card
, comp
, _comp
) {
1452 if (comp
->driver
->remove_order
== order
)
1453 soc_remove_component(comp
, 1);
1459 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1460 * @rtd: The runtime for which the DAI link format should be changed
1461 * @dai_fmt: The new DAI link format
1463 * This function updates the DAI link format for all DAIs connected to the DAI
1464 * link for the specified runtime.
1466 * Note: For setups with a static format set the dai_fmt field in the
1467 * corresponding snd_dai_link struct instead of using this function.
1469 * Returns 0 on success, otherwise a negative error code.
1471 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime
*rtd
,
1472 unsigned int dai_fmt
)
1474 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1475 struct snd_soc_dai
*codec_dai
;
1479 for_each_rtd_codec_dai(rtd
, i
, codec_dai
) {
1480 ret
= snd_soc_dai_set_fmt(codec_dai
, dai_fmt
);
1481 if (ret
!= 0 && ret
!= -ENOTSUPP
) {
1482 dev_warn(codec_dai
->dev
,
1483 "ASoC: Failed to set DAI format: %d\n", ret
);
1489 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1490 * the component which has non_legacy_dai_naming is Codec
1492 if (cpu_dai
->component
->driver
->non_legacy_dai_naming
) {
1493 unsigned int inv_dai_fmt
;
1495 inv_dai_fmt
= dai_fmt
& ~SND_SOC_DAIFMT_MASTER_MASK
;
1496 switch (dai_fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1497 case SND_SOC_DAIFMT_CBM_CFM
:
1498 inv_dai_fmt
|= SND_SOC_DAIFMT_CBS_CFS
;
1500 case SND_SOC_DAIFMT_CBM_CFS
:
1501 inv_dai_fmt
|= SND_SOC_DAIFMT_CBS_CFM
;
1503 case SND_SOC_DAIFMT_CBS_CFM
:
1504 inv_dai_fmt
|= SND_SOC_DAIFMT_CBM_CFS
;
1506 case SND_SOC_DAIFMT_CBS_CFS
:
1507 inv_dai_fmt
|= SND_SOC_DAIFMT_CBM_CFM
;
1511 dai_fmt
= inv_dai_fmt
;
1514 ret
= snd_soc_dai_set_fmt(cpu_dai
, dai_fmt
);
1515 if (ret
!= 0 && ret
!= -ENOTSUPP
) {
1516 dev_warn(cpu_dai
->dev
,
1517 "ASoC: Failed to set DAI format: %d\n", ret
);
1523 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt
);
1527 * If a DMI filed contain strings in this blacklist (e.g.
1528 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1529 * as invalid and dropped when setting the card long name from DMI info.
1531 static const char * const dmi_blacklist
[] = {
1532 "To be filled by OEM",
1535 "Board Manufacturer",
1536 "Board Vendor Name",
1537 "Board Product Name",
1538 NULL
, /* terminator */
1542 * Trim special characters, and replace '-' with '_' since '-' is used to
1543 * separate different DMI fields in the card long name. Only number and
1544 * alphabet characters and a few separator characters are kept.
1546 static void cleanup_dmi_name(char *name
)
1550 for (i
= 0; name
[i
]; i
++) {
1551 if (isalnum(name
[i
]) || (name
[i
] == '.')
1552 || (name
[i
] == '_'))
1553 name
[j
++] = name
[i
];
1554 else if (name
[i
] == '-')
1562 * Check if a DMI field is valid, i.e. not containing any string
1563 * in the black list.
1565 static int is_dmi_valid(const char *field
)
1569 while (dmi_blacklist
[i
]) {
1570 if (strstr(field
, dmi_blacklist
[i
]))
1579 * Append a string to card->dmi_longname with character cleanups.
1581 static void append_dmi_string(struct snd_soc_card
*card
, const char *str
)
1583 char *dst
= card
->dmi_longname
;
1584 size_t dst_len
= sizeof(card
->dmi_longname
);
1588 snprintf(dst
+ len
, dst_len
- len
, "-%s", str
);
1590 len
++; /* skip the separator "-" */
1592 cleanup_dmi_name(dst
+ len
);
1596 * snd_soc_set_dmi_name() - Register DMI names to card
1597 * @card: The card to register DMI names
1598 * @flavour: The flavour "differentiator" for the card amongst its peers.
1600 * An Intel machine driver may be used by many different devices but are
1601 * difficult for userspace to differentiate, since machine drivers ususally
1602 * use their own name as the card short name and leave the card long name
1603 * blank. To differentiate such devices and fix bugs due to lack of
1604 * device-specific configurations, this function allows DMI info to be used
1605 * as the sound card long name, in the format of
1606 * "vendor-product-version-board"
1607 * (Character '-' is used to separate different DMI fields here).
1608 * This will help the user space to load the device-specific Use Case Manager
1609 * (UCM) configurations for the card.
1611 * Possible card long names may be:
1612 * DellInc.-XPS139343-01-0310JH
1613 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1614 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1616 * This function also supports flavoring the card longname to provide
1617 * the extra differentiation, like "vendor-product-version-board-flavor".
1619 * We only keep number and alphabet characters and a few separator characters
1620 * in the card long name since UCM in the user space uses the card long names
1621 * as card configuration directory names and AudoConf cannot support special
1622 * charactors like SPACE.
1624 * Returns 0 on success, otherwise a negative error code.
1626 int snd_soc_set_dmi_name(struct snd_soc_card
*card
, const char *flavour
)
1628 const char *vendor
, *product
, *product_version
, *board
;
1630 if (card
->long_name
)
1631 return 0; /* long name already set by driver or from DMI */
1633 /* make up dmi long name as: vendor-product-version-board */
1634 vendor
= dmi_get_system_info(DMI_BOARD_VENDOR
);
1635 if (!vendor
|| !is_dmi_valid(vendor
)) {
1636 dev_warn(card
->dev
, "ASoC: no DMI vendor name!\n");
1640 snprintf(card
->dmi_longname
, sizeof(card
->dmi_longname
), "%s", vendor
);
1641 cleanup_dmi_name(card
->dmi_longname
);
1643 product
= dmi_get_system_info(DMI_PRODUCT_NAME
);
1644 if (product
&& is_dmi_valid(product
)) {
1645 append_dmi_string(card
, product
);
1648 * some vendors like Lenovo may only put a self-explanatory
1649 * name in the product version field
1651 product_version
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
1652 if (product_version
&& is_dmi_valid(product_version
))
1653 append_dmi_string(card
, product_version
);
1656 board
= dmi_get_system_info(DMI_BOARD_NAME
);
1657 if (board
&& is_dmi_valid(board
)) {
1658 if (!product
|| strcasecmp(board
, product
))
1659 append_dmi_string(card
, board
);
1660 } else if (!product
) {
1661 /* fall back to using legacy name */
1662 dev_warn(card
->dev
, "ASoC: no DMI board/product name!\n");
1666 /* Add flavour to dmi long name */
1668 append_dmi_string(card
, flavour
);
1670 /* set the card long name */
1671 card
->long_name
= card
->dmi_longname
;
1675 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name
);
1676 #endif /* CONFIG_DMI */
1678 static void soc_check_tplg_fes(struct snd_soc_card
*card
)
1680 struct snd_soc_component
*component
;
1681 const struct snd_soc_component_driver
*comp_drv
;
1682 struct snd_soc_dai_link
*dai_link
;
1685 for_each_component(component
) {
1687 /* does this component override BEs ? */
1688 if (!component
->driver
->ignore_machine
)
1691 /* for this machine ? */
1692 if (!strcmp(component
->driver
->ignore_machine
,
1693 card
->dev
->driver
->name
))
1695 if (strcmp(component
->driver
->ignore_machine
,
1696 dev_name(card
->dev
)))
1699 /* machine matches, so override the rtd data */
1700 for_each_card_prelinks(card
, i
, dai_link
) {
1702 /* ignore this FE */
1703 if (dai_link
->dynamic
) {
1704 dai_link
->ignore
= true;
1708 dev_info(card
->dev
, "info: override BE DAI link %s\n",
1709 card
->dai_link
[i
].name
);
1711 /* override platform component */
1712 if (!dai_link
->platforms
) {
1713 dev_err(card
->dev
, "init platform error");
1716 dai_link
->platforms
->name
= component
->name
;
1718 /* convert non BE into BE */
1719 dai_link
->no_pcm
= 1;
1720 dai_link
->dpcm_playback
= 1;
1721 dai_link
->dpcm_capture
= 1;
1723 /* override any BE fixups */
1724 dai_link
->be_hw_params_fixup
=
1725 component
->driver
->be_hw_params_fixup
;
1728 * most BE links don't set stream name, so set it to
1729 * dai link name if it's NULL to help bind widgets.
1731 if (!dai_link
->stream_name
)
1732 dai_link
->stream_name
= dai_link
->name
;
1735 /* Inform userspace we are using alternate topology */
1736 if (component
->driver
->topology_name_prefix
) {
1738 /* topology shortname created? */
1739 if (!card
->topology_shortname_created
) {
1740 comp_drv
= component
->driver
;
1742 snprintf(card
->topology_shortname
, 32, "%s-%s",
1743 comp_drv
->topology_name_prefix
,
1745 card
->topology_shortname_created
= true;
1748 /* use topology shortname */
1749 card
->name
= card
->topology_shortname
;
1754 #define soc_setup_card_name(name, name1, name2, norm) \
1755 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1756 static void __soc_setup_card_name(char *name
, int len
,
1757 const char *name1
, const char *name2
,
1762 snprintf(name
, len
, "%s", name1
? name1
: name2
);
1768 * Name normalization
1770 * The driver name is somewhat special, as it's used as a key for
1771 * searches in the user-space.
1774 * "abcd??efg" -> "abcd__efg"
1776 for (i
= 0; i
< len
; i
++) {
1783 if (!isalnum(name
[i
]))
1790 static void soc_cleanup_card_resources(struct snd_soc_card
*card
,
1793 struct snd_soc_pcm_runtime
*rtd
, *n
;
1796 snd_card_disconnect_sync(card
->snd_card
);
1798 snd_soc_dapm_shutdown(card
);
1800 /* remove and free each DAI */
1801 soc_remove_link_dais(card
);
1802 soc_remove_link_components(card
);
1804 for_each_card_rtds_safe(card
, rtd
, n
)
1805 snd_soc_remove_pcm_runtime(card
, rtd
);
1807 /* remove auxiliary devices */
1808 soc_remove_aux_devices(card
);
1809 soc_unbind_aux_dev(card
);
1811 snd_soc_dapm_free(&card
->dapm
);
1812 soc_cleanup_card_debugfs(card
);
1814 /* remove the card */
1815 if (card_probed
&& card
->remove
)
1818 if (card
->snd_card
) {
1819 snd_card_free(card
->snd_card
);
1820 card
->snd_card
= NULL
;
1824 static void snd_soc_unbind_card(struct snd_soc_card
*card
, bool unregister
)
1826 if (card
->instantiated
) {
1827 int card_probed
= 1;
1829 card
->instantiated
= false;
1830 snd_soc_flush_all_delayed_work(card
);
1832 soc_cleanup_card_resources(card
, card_probed
);
1834 list_add(&card
->list
, &unbind_card_list
);
1837 list_del(&card
->list
);
1841 static int snd_soc_bind_card(struct snd_soc_card
*card
)
1843 struct snd_soc_pcm_runtime
*rtd
;
1844 struct snd_soc_component
*component
;
1845 struct snd_soc_dai_link
*dai_link
;
1846 int ret
, i
, card_probed
= 0;
1848 mutex_lock(&client_mutex
);
1849 mutex_lock_nested(&card
->mutex
, SND_SOC_CARD_CLASS_INIT
);
1851 snd_soc_dapm_init(&card
->dapm
, card
, NULL
);
1853 /* check whether any platform is ignore machine FE and using topology */
1854 soc_check_tplg_fes(card
);
1856 /* bind aux_devs too */
1857 ret
= soc_bind_aux_dev(card
);
1861 /* add predefined DAI links to the list */
1863 for_each_card_prelinks(card
, i
, dai_link
) {
1864 ret
= snd_soc_add_pcm_runtime(card
, dai_link
);
1869 /* card bind complete so register a sound card */
1870 ret
= snd_card_new(card
->dev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
1871 card
->owner
, 0, &card
->snd_card
);
1874 "ASoC: can't create sound card for card %s: %d\n",
1879 soc_init_card_debugfs(card
);
1881 soc_resume_init(card
);
1883 ret
= snd_soc_dapm_new_controls(&card
->dapm
, card
->dapm_widgets
,
1884 card
->num_dapm_widgets
);
1888 ret
= snd_soc_dapm_new_controls(&card
->dapm
, card
->of_dapm_widgets
,
1889 card
->num_of_dapm_widgets
);
1893 /* initialise the sound card only once */
1895 ret
= card
->probe(card
);
1901 /* probe all components used by DAI links on this card */
1902 ret
= soc_probe_link_components(card
);
1905 "ASoC: failed to instantiate card %d\n", ret
);
1909 /* probe auxiliary components */
1910 ret
= soc_probe_aux_devices(card
);
1913 "ASoC: failed to probe aux component %d\n", ret
);
1917 /* probe all DAI links on this card */
1918 ret
= soc_probe_link_dais(card
);
1921 "ASoC: failed to instantiate card %d\n", ret
);
1925 for_each_card_rtds(card
, rtd
) {
1926 ret
= soc_init_pcm_runtime(card
, rtd
);
1931 snd_soc_dapm_link_dai_widgets(card
);
1932 snd_soc_dapm_connect_dai_link_widgets(card
);
1934 ret
= snd_soc_add_card_controls(card
, card
->controls
,
1935 card
->num_controls
);
1939 ret
= snd_soc_dapm_add_routes(&card
->dapm
, card
->dapm_routes
,
1940 card
->num_dapm_routes
);
1944 ret
= snd_soc_dapm_add_routes(&card
->dapm
, card
->of_dapm_routes
,
1945 card
->num_of_dapm_routes
);
1949 /* try to set some sane longname if DMI is available */
1950 snd_soc_set_dmi_name(card
, NULL
);
1952 soc_setup_card_name(card
->snd_card
->shortname
,
1953 card
->name
, NULL
, 0);
1954 soc_setup_card_name(card
->snd_card
->longname
,
1955 card
->long_name
, card
->name
, 0);
1956 soc_setup_card_name(card
->snd_card
->driver
,
1957 card
->driver_name
, card
->name
, 1);
1959 if (card
->components
) {
1960 /* the current implementation of snd_component_add() accepts */
1961 /* multiple components in the string separated by space, */
1962 /* but the string collision (identical string) check might */
1963 /* not work correctly */
1964 ret
= snd_component_add(card
->snd_card
, card
->components
);
1966 dev_err(card
->dev
, "ASoC: %s snd_component_add() failed: %d\n",
1972 if (card
->late_probe
) {
1973 ret
= card
->late_probe(card
);
1975 dev_err(card
->dev
, "ASoC: %s late_probe() failed: %d\n",
1982 snd_soc_dapm_new_widgets(card
);
1984 ret
= snd_card_register(card
->snd_card
);
1986 dev_err(card
->dev
, "ASoC: failed to register soundcard %d\n",
1991 card
->instantiated
= 1;
1992 dapm_mark_endpoints_dirty(card
);
1993 snd_soc_dapm_sync(&card
->dapm
);
1995 /* deactivate pins to sleep state */
1996 for_each_card_components(card
, component
)
1997 if (!component
->active
)
1998 pinctrl_pm_select_sleep_state(component
->dev
);
2002 soc_cleanup_card_resources(card
, card_probed
);
2004 mutex_unlock(&card
->mutex
);
2005 mutex_unlock(&client_mutex
);
2010 /* probes a new socdev */
2011 static int soc_probe(struct platform_device
*pdev
)
2013 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
2016 * no card, so machine driver should be registering card
2017 * we should not be here in that case so ret error
2022 dev_warn(&pdev
->dev
,
2023 "ASoC: machine %s should use snd_soc_register_card()\n",
2026 /* Bodge while we unpick instantiation */
2027 card
->dev
= &pdev
->dev
;
2029 return snd_soc_register_card(card
);
2032 /* removes a socdev */
2033 static int soc_remove(struct platform_device
*pdev
)
2035 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
2037 snd_soc_unregister_card(card
);
2041 int snd_soc_poweroff(struct device
*dev
)
2043 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
2044 struct snd_soc_component
*component
;
2046 if (!card
->instantiated
)
2050 * Flush out pmdown_time work - we actually do want to run it
2051 * now, we're shutting down so no imminent restart.
2053 snd_soc_flush_all_delayed_work(card
);
2055 snd_soc_dapm_shutdown(card
);
2057 /* deactivate pins to sleep state */
2058 for_each_card_components(card
, component
)
2059 pinctrl_pm_select_sleep_state(component
->dev
);
2063 EXPORT_SYMBOL_GPL(snd_soc_poweroff
);
2065 const struct dev_pm_ops snd_soc_pm_ops
= {
2066 .suspend
= snd_soc_suspend
,
2067 .resume
= snd_soc_resume
,
2068 .freeze
= snd_soc_suspend
,
2069 .thaw
= snd_soc_resume
,
2070 .poweroff
= snd_soc_poweroff
,
2071 .restore
= snd_soc_resume
,
2073 EXPORT_SYMBOL_GPL(snd_soc_pm_ops
);
2075 /* ASoC platform driver */
2076 static struct platform_driver soc_driver
= {
2078 .name
= "soc-audio",
2079 .pm
= &snd_soc_pm_ops
,
2082 .remove
= soc_remove
,
2086 * snd_soc_cnew - create new control
2087 * @_template: control template
2088 * @data: control private data
2089 * @long_name: control long name
2090 * @prefix: control name prefix
2092 * Create a new mixer control from a template control.
2094 * Returns 0 for success, else error.
2096 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
2097 void *data
, const char *long_name
,
2100 struct snd_kcontrol_new
template;
2101 struct snd_kcontrol
*kcontrol
;
2104 memcpy(&template, _template
, sizeof(template));
2108 long_name
= template.name
;
2111 name
= kasprintf(GFP_KERNEL
, "%s %s", prefix
, long_name
);
2115 template.name
= name
;
2117 template.name
= long_name
;
2120 kcontrol
= snd_ctl_new1(&template, data
);
2126 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
2128 static int snd_soc_add_controls(struct snd_card
*card
, struct device
*dev
,
2129 const struct snd_kcontrol_new
*controls
, int num_controls
,
2130 const char *prefix
, void *data
)
2134 for (i
= 0; i
< num_controls
; i
++) {
2135 const struct snd_kcontrol_new
*control
= &controls
[i
];
2137 err
= snd_ctl_add(card
, snd_soc_cnew(control
, data
,
2138 control
->name
, prefix
));
2140 dev_err(dev
, "ASoC: Failed to add %s: %d\n",
2141 control
->name
, err
);
2149 struct snd_kcontrol
*snd_soc_card_get_kcontrol(struct snd_soc_card
*soc_card
,
2152 struct snd_card
*card
= soc_card
->snd_card
;
2153 struct snd_kcontrol
*kctl
;
2155 if (unlikely(!name
))
2158 list_for_each_entry(kctl
, &card
->controls
, list
)
2159 if (!strncmp(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
)))
2163 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol
);
2166 * snd_soc_add_component_controls - Add an array of controls to a component.
2168 * @component: Component to add controls to
2169 * @controls: Array of controls to add
2170 * @num_controls: Number of elements in the array
2172 * Return: 0 for success, else error.
2174 int snd_soc_add_component_controls(struct snd_soc_component
*component
,
2175 const struct snd_kcontrol_new
*controls
, unsigned int num_controls
)
2177 struct snd_card
*card
= component
->card
->snd_card
;
2179 return snd_soc_add_controls(card
, component
->dev
, controls
,
2180 num_controls
, component
->name_prefix
, component
);
2182 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls
);
2185 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2186 * Convenience function to add a list of controls.
2188 * @soc_card: SoC card to add controls to
2189 * @controls: array of controls to add
2190 * @num_controls: number of elements in the array
2192 * Return 0 for success, else error.
2194 int snd_soc_add_card_controls(struct snd_soc_card
*soc_card
,
2195 const struct snd_kcontrol_new
*controls
, int num_controls
)
2197 struct snd_card
*card
= soc_card
->snd_card
;
2199 return snd_soc_add_controls(card
, soc_card
->dev
, controls
, num_controls
,
2202 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls
);
2205 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2206 * Convienience function to add a list of controls.
2208 * @dai: DAI to add controls to
2209 * @controls: array of controls to add
2210 * @num_controls: number of elements in the array
2212 * Return 0 for success, else error.
2214 int snd_soc_add_dai_controls(struct snd_soc_dai
*dai
,
2215 const struct snd_kcontrol_new
*controls
, int num_controls
)
2217 struct snd_card
*card
= dai
->component
->card
->snd_card
;
2219 return snd_soc_add_controls(card
, dai
->dev
, controls
, num_controls
,
2222 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls
);
2225 * snd_soc_register_card - Register a card with the ASoC core
2227 * @card: Card to register
2230 int snd_soc_register_card(struct snd_soc_card
*card
)
2232 if (!card
->name
|| !card
->dev
)
2235 dev_set_drvdata(card
->dev
, card
);
2237 INIT_LIST_HEAD(&card
->widgets
);
2238 INIT_LIST_HEAD(&card
->paths
);
2239 INIT_LIST_HEAD(&card
->dapm_list
);
2240 INIT_LIST_HEAD(&card
->aux_comp_list
);
2241 INIT_LIST_HEAD(&card
->component_dev_list
);
2242 INIT_LIST_HEAD(&card
->list
);
2243 INIT_LIST_HEAD(&card
->rtd_list
);
2244 INIT_LIST_HEAD(&card
->dapm_dirty
);
2245 INIT_LIST_HEAD(&card
->dobj_list
);
2247 card
->instantiated
= 0;
2248 mutex_init(&card
->mutex
);
2249 mutex_init(&card
->dapm_mutex
);
2250 mutex_init(&card
->pcm_mutex
);
2251 spin_lock_init(&card
->dpcm_lock
);
2253 return snd_soc_bind_card(card
);
2255 EXPORT_SYMBOL_GPL(snd_soc_register_card
);
2258 * snd_soc_unregister_card - Unregister a card with the ASoC core
2260 * @card: Card to unregister
2263 int snd_soc_unregister_card(struct snd_soc_card
*card
)
2265 mutex_lock(&client_mutex
);
2266 snd_soc_unbind_card(card
, true);
2267 mutex_unlock(&client_mutex
);
2268 dev_dbg(card
->dev
, "ASoC: Unregistered card '%s'\n", card
->name
);
2272 EXPORT_SYMBOL_GPL(snd_soc_unregister_card
);
2275 * Simplify DAI link configuration by removing ".-1" from device names
2276 * and sanitizing names.
2278 static char *fmt_single_name(struct device
*dev
, int *id
)
2280 char *found
, name
[NAME_SIZE
];
2283 if (dev_name(dev
) == NULL
)
2286 strlcpy(name
, dev_name(dev
), NAME_SIZE
);
2288 /* are we a "%s.%d" name (platform and SPI components) */
2289 found
= strstr(name
, dev
->driver
->name
);
2292 if (sscanf(&found
[strlen(dev
->driver
->name
)], ".%d", id
) == 1) {
2294 /* discard ID from name if ID == -1 */
2296 found
[strlen(dev
->driver
->name
)] = '\0';
2300 /* I2C component devices are named "bus-addr" */
2301 if (sscanf(name
, "%x-%x", &id1
, &id2
) == 2) {
2302 char tmp
[NAME_SIZE
];
2304 /* create unique ID number from I2C addr and bus */
2305 *id
= ((id1
& 0xffff) << 16) + id2
;
2307 /* sanitize component name for DAI link creation */
2308 snprintf(tmp
, NAME_SIZE
, "%s.%s", dev
->driver
->name
,
2310 strlcpy(name
, tmp
, NAME_SIZE
);
2315 return devm_kstrdup(dev
, name
, GFP_KERNEL
);
2319 * Simplify DAI link naming for single devices with multiple DAIs by removing
2320 * any ".-1" and using the DAI name (instead of device name).
2322 static inline char *fmt_multiple_name(struct device
*dev
,
2323 struct snd_soc_dai_driver
*dai_drv
)
2325 if (dai_drv
->name
== NULL
) {
2327 "ASoC: error - multiple DAI %s registered with no name\n",
2332 return devm_kstrdup(dev
, dai_drv
->name
, GFP_KERNEL
);
2335 void snd_soc_unregister_dai(struct snd_soc_dai
*dai
)
2337 dev_dbg(dai
->dev
, "ASoC: Unregistered DAI '%s'\n", dai
->name
);
2338 list_del(&dai
->list
);
2340 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai
);
2343 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2345 * @component: The component the DAIs are registered for
2346 * @dai_drv: DAI driver to use for the DAI
2347 * @legacy_dai_naming: if %true, use legacy single-name format;
2348 * if %false, use multiple-name format;
2350 * Topology can use this API to register DAIs when probing a component.
2351 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2352 * will be freed in the component cleanup.
2354 struct snd_soc_dai
*snd_soc_register_dai(struct snd_soc_component
*component
,
2355 struct snd_soc_dai_driver
*dai_drv
,
2356 bool legacy_dai_naming
)
2358 struct device
*dev
= component
->dev
;
2359 struct snd_soc_dai
*dai
;
2361 dev_dbg(dev
, "ASoC: dynamically register DAI %s\n", dev_name(dev
));
2363 lockdep_assert_held(&client_mutex
);
2365 dai
= devm_kzalloc(dev
, sizeof(*dai
), GFP_KERNEL
);
2370 * Back in the old days when we still had component-less DAIs,
2371 * instead of having a static name, component-less DAIs would
2372 * inherit the name of the parent device so it is possible to
2373 * register multiple instances of the DAI. We still need to keep
2374 * the same naming style even though those DAIs are not
2375 * component-less anymore.
2377 if (legacy_dai_naming
&&
2378 (dai_drv
->id
== 0 || dai_drv
->name
== NULL
)) {
2379 dai
->name
= fmt_single_name(dev
, &dai
->id
);
2381 dai
->name
= fmt_multiple_name(dev
, dai_drv
);
2383 dai
->id
= dai_drv
->id
;
2385 dai
->id
= component
->num_dai
;
2390 dai
->component
= component
;
2392 dai
->driver
= dai_drv
;
2393 if (!dai
->driver
->ops
)
2394 dai
->driver
->ops
= &null_dai_ops
;
2396 /* see for_each_component_dais */
2397 list_add_tail(&dai
->list
, &component
->dai_list
);
2398 component
->num_dai
++;
2400 dev_dbg(dev
, "ASoC: Registered DAI '%s'\n", dai
->name
);
2405 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2407 * @component: The component for which the DAIs should be unregistered
2409 static void snd_soc_unregister_dais(struct snd_soc_component
*component
)
2411 struct snd_soc_dai
*dai
, *_dai
;
2413 for_each_component_dais_safe(component
, dai
, _dai
)
2414 snd_soc_unregister_dai(dai
);
2418 * snd_soc_register_dais - Register a DAI with the ASoC core
2420 * @component: The component the DAIs are registered for
2421 * @dai_drv: DAI driver to use for the DAIs
2422 * @count: Number of DAIs
2424 static int snd_soc_register_dais(struct snd_soc_component
*component
,
2425 struct snd_soc_dai_driver
*dai_drv
,
2428 struct snd_soc_dai
*dai
;
2432 for (i
= 0; i
< count
; i
++) {
2433 dai
= snd_soc_register_dai(component
, dai_drv
+ i
, count
== 1 &&
2434 !component
->driver
->non_legacy_dai_naming
);
2444 snd_soc_unregister_dais(component
);
2449 static int snd_soc_component_initialize(struct snd_soc_component
*component
,
2450 const struct snd_soc_component_driver
*driver
, struct device
*dev
)
2452 INIT_LIST_HEAD(&component
->dai_list
);
2453 INIT_LIST_HEAD(&component
->dobj_list
);
2454 INIT_LIST_HEAD(&component
->card_list
);
2455 mutex_init(&component
->io_mutex
);
2457 component
->name
= fmt_single_name(dev
, &component
->id
);
2458 if (!component
->name
) {
2459 dev_err(dev
, "ASoC: Failed to allocate name\n");
2463 component
->dev
= dev
;
2464 component
->driver
= driver
;
2469 static void snd_soc_component_setup_regmap(struct snd_soc_component
*component
)
2471 int val_bytes
= regmap_get_val_bytes(component
->regmap
);
2473 /* Errors are legitimate for non-integer byte multiples */
2475 component
->val_bytes
= val_bytes
;
2478 #ifdef CONFIG_REGMAP
2481 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2483 * @component: The component for which to initialize the regmap instance
2484 * @regmap: The regmap instance that should be used by the component
2486 * This function allows deferred assignment of the regmap instance that is
2487 * associated with the component. Only use this if the regmap instance is not
2488 * yet ready when the component is registered. The function must also be called
2489 * before the first IO attempt of the component.
2491 void snd_soc_component_init_regmap(struct snd_soc_component
*component
,
2492 struct regmap
*regmap
)
2494 component
->regmap
= regmap
;
2495 snd_soc_component_setup_regmap(component
);
2497 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap
);
2500 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2502 * @component: The component for which to de-initialize the regmap instance
2504 * Calls regmap_exit() on the regmap instance associated to the component and
2505 * removes the regmap instance from the component.
2507 * This function should only be used if snd_soc_component_init_regmap() was used
2508 * to initialize the regmap instance.
2510 void snd_soc_component_exit_regmap(struct snd_soc_component
*component
)
2512 regmap_exit(component
->regmap
);
2513 component
->regmap
= NULL
;
2515 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap
);
2519 #define ENDIANNESS_MAP(name) \
2520 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2521 static u64 endianness_format_map
[] = {
2522 ENDIANNESS_MAP(S16_
),
2523 ENDIANNESS_MAP(U16_
),
2524 ENDIANNESS_MAP(S24_
),
2525 ENDIANNESS_MAP(U24_
),
2526 ENDIANNESS_MAP(S32_
),
2527 ENDIANNESS_MAP(U32_
),
2528 ENDIANNESS_MAP(S24_3
),
2529 ENDIANNESS_MAP(U24_3
),
2530 ENDIANNESS_MAP(S20_3
),
2531 ENDIANNESS_MAP(U20_3
),
2532 ENDIANNESS_MAP(S18_3
),
2533 ENDIANNESS_MAP(U18_3
),
2534 ENDIANNESS_MAP(FLOAT_
),
2535 ENDIANNESS_MAP(FLOAT64_
),
2536 ENDIANNESS_MAP(IEC958_SUBFRAME_
),
2540 * Fix up the DAI formats for endianness: codecs don't actually see
2541 * the endianness of the data but we're using the CPU format
2542 * definitions which do need to include endianness so we ensure that
2543 * codec DAIs always have both big and little endian variants set.
2545 static void convert_endianness_formats(struct snd_soc_pcm_stream
*stream
)
2549 for (i
= 0; i
< ARRAY_SIZE(endianness_format_map
); i
++)
2550 if (stream
->formats
& endianness_format_map
[i
])
2551 stream
->formats
|= endianness_format_map
[i
];
2554 static void snd_soc_try_rebind_card(void)
2556 struct snd_soc_card
*card
, *c
;
2558 list_for_each_entry_safe(card
, c
, &unbind_card_list
, list
)
2559 if (!snd_soc_bind_card(card
))
2560 list_del(&card
->list
);
2563 static void snd_soc_del_component_unlocked(struct snd_soc_component
*component
)
2565 struct snd_soc_card
*card
= component
->card
;
2567 snd_soc_unregister_dais(component
);
2570 snd_soc_unbind_card(card
, false);
2572 list_del(&component
->list
);
2575 int snd_soc_add_component(struct device
*dev
,
2576 struct snd_soc_component
*component
,
2577 const struct snd_soc_component_driver
*component_driver
,
2578 struct snd_soc_dai_driver
*dai_drv
,
2584 mutex_lock(&client_mutex
);
2586 ret
= snd_soc_component_initialize(component
, component_driver
, dev
);
2590 if (component_driver
->endianness
) {
2591 for (i
= 0; i
< num_dai
; i
++) {
2592 convert_endianness_formats(&dai_drv
[i
].playback
);
2593 convert_endianness_formats(&dai_drv
[i
].capture
);
2597 ret
= snd_soc_register_dais(component
, dai_drv
, num_dai
);
2599 dev_err(dev
, "ASoC: Failed to register DAIs: %d\n", ret
);
2603 if (!component
->driver
->write
&& !component
->driver
->read
) {
2604 if (!component
->regmap
)
2605 component
->regmap
= dev_get_regmap(component
->dev
,
2607 if (component
->regmap
)
2608 snd_soc_component_setup_regmap(component
);
2611 /* see for_each_component */
2612 list_add(&component
->list
, &component_list
);
2616 snd_soc_del_component_unlocked(component
);
2618 mutex_unlock(&client_mutex
);
2621 snd_soc_try_rebind_card();
2625 EXPORT_SYMBOL_GPL(snd_soc_add_component
);
2627 int snd_soc_register_component(struct device
*dev
,
2628 const struct snd_soc_component_driver
*component_driver
,
2629 struct snd_soc_dai_driver
*dai_drv
,
2632 struct snd_soc_component
*component
;
2634 component
= devm_kzalloc(dev
, sizeof(*component
), GFP_KERNEL
);
2638 return snd_soc_add_component(dev
, component
, component_driver
,
2641 EXPORT_SYMBOL_GPL(snd_soc_register_component
);
2644 * snd_soc_unregister_component - Unregister all related component
2645 * from the ASoC core
2647 * @dev: The device to unregister
2649 void snd_soc_unregister_component(struct device
*dev
)
2651 struct snd_soc_component
*component
;
2653 mutex_lock(&client_mutex
);
2655 component
= snd_soc_lookup_component_nolocked(dev
, NULL
);
2659 snd_soc_del_component_unlocked(component
);
2661 mutex_unlock(&client_mutex
);
2663 EXPORT_SYMBOL_GPL(snd_soc_unregister_component
);
2665 /* Retrieve a card's name from device tree */
2666 int snd_soc_of_parse_card_name(struct snd_soc_card
*card
,
2667 const char *propname
)
2669 struct device_node
*np
;
2673 pr_err("card->dev is not set before calling %s\n", __func__
);
2677 np
= card
->dev
->of_node
;
2679 ret
= of_property_read_string_index(np
, propname
, 0, &card
->name
);
2681 * EINVAL means the property does not exist. This is fine providing
2682 * card->name was previously set, which is checked later in
2683 * snd_soc_register_card.
2685 if (ret
< 0 && ret
!= -EINVAL
) {
2687 "ASoC: Property '%s' could not be read: %d\n",
2694 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name
);
2696 static const struct snd_soc_dapm_widget simple_widgets
[] = {
2697 SND_SOC_DAPM_MIC("Microphone", NULL
),
2698 SND_SOC_DAPM_LINE("Line", NULL
),
2699 SND_SOC_DAPM_HP("Headphone", NULL
),
2700 SND_SOC_DAPM_SPK("Speaker", NULL
),
2703 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card
*card
,
2704 const char *propname
)
2706 struct device_node
*np
= card
->dev
->of_node
;
2707 struct snd_soc_dapm_widget
*widgets
;
2708 const char *template, *wname
;
2709 int i
, j
, num_widgets
, ret
;
2711 num_widgets
= of_property_count_strings(np
, propname
);
2712 if (num_widgets
< 0) {
2714 "ASoC: Property '%s' does not exist\n", propname
);
2717 if (num_widgets
& 1) {
2719 "ASoC: Property '%s' length is not even\n", propname
);
2725 dev_err(card
->dev
, "ASoC: Property '%s's length is zero\n",
2730 widgets
= devm_kcalloc(card
->dev
, num_widgets
, sizeof(*widgets
),
2734 "ASoC: Could not allocate memory for widgets\n");
2738 for (i
= 0; i
< num_widgets
; i
++) {
2739 ret
= of_property_read_string_index(np
, propname
,
2743 "ASoC: Property '%s' index %d read error:%d\n",
2744 propname
, 2 * i
, ret
);
2748 for (j
= 0; j
< ARRAY_SIZE(simple_widgets
); j
++) {
2749 if (!strncmp(template, simple_widgets
[j
].name
,
2750 strlen(simple_widgets
[j
].name
))) {
2751 widgets
[i
] = simple_widgets
[j
];
2756 if (j
>= ARRAY_SIZE(simple_widgets
)) {
2758 "ASoC: DAPM widget '%s' is not supported\n",
2763 ret
= of_property_read_string_index(np
, propname
,
2768 "ASoC: Property '%s' index %d read error:%d\n",
2769 propname
, (2 * i
) + 1, ret
);
2773 widgets
[i
].name
= wname
;
2776 card
->of_dapm_widgets
= widgets
;
2777 card
->num_of_dapm_widgets
= num_widgets
;
2781 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets
);
2783 int snd_soc_of_get_slot_mask(struct device_node
*np
,
2784 const char *prop_name
,
2788 const __be32
*of_slot_mask
= of_get_property(np
, prop_name
, &val
);
2794 for (i
= 0; i
< val
; i
++)
2795 if (be32_to_cpup(&of_slot_mask
[i
]))
2800 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask
);
2802 int snd_soc_of_parse_tdm_slot(struct device_node
*np
,
2803 unsigned int *tx_mask
,
2804 unsigned int *rx_mask
,
2805 unsigned int *slots
,
2806 unsigned int *slot_width
)
2812 snd_soc_of_get_slot_mask(np
, "dai-tdm-slot-tx-mask", tx_mask
);
2814 snd_soc_of_get_slot_mask(np
, "dai-tdm-slot-rx-mask", rx_mask
);
2816 if (of_property_read_bool(np
, "dai-tdm-slot-num")) {
2817 ret
= of_property_read_u32(np
, "dai-tdm-slot-num", &val
);
2825 if (of_property_read_bool(np
, "dai-tdm-slot-width")) {
2826 ret
= of_property_read_u32(np
, "dai-tdm-slot-width", &val
);
2836 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot
);
2838 void snd_soc_of_parse_node_prefix(struct device_node
*np
,
2839 struct snd_soc_codec_conf
*codec_conf
,
2840 struct device_node
*of_node
,
2841 const char *propname
)
2846 ret
= of_property_read_string(np
, propname
, &str
);
2848 /* no prefix is not error */
2852 codec_conf
->dlc
.of_node
= of_node
;
2853 codec_conf
->name_prefix
= str
;
2855 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix
);
2857 int snd_soc_of_parse_audio_routing(struct snd_soc_card
*card
,
2858 const char *propname
)
2860 struct device_node
*np
= card
->dev
->of_node
;
2862 struct snd_soc_dapm_route
*routes
;
2865 num_routes
= of_property_count_strings(np
, propname
);
2866 if (num_routes
< 0 || num_routes
& 1) {
2868 "ASoC: Property '%s' does not exist or its length is not even\n",
2874 dev_err(card
->dev
, "ASoC: Property '%s's length is zero\n",
2879 routes
= devm_kcalloc(card
->dev
, num_routes
, sizeof(*routes
),
2883 "ASoC: Could not allocate DAPM route table\n");
2887 for (i
= 0; i
< num_routes
; i
++) {
2888 ret
= of_property_read_string_index(np
, propname
,
2889 2 * i
, &routes
[i
].sink
);
2892 "ASoC: Property '%s' index %d could not be read: %d\n",
2893 propname
, 2 * i
, ret
);
2896 ret
= of_property_read_string_index(np
, propname
,
2897 (2 * i
) + 1, &routes
[i
].source
);
2900 "ASoC: Property '%s' index %d could not be read: %d\n",
2901 propname
, (2 * i
) + 1, ret
);
2906 card
->num_of_dapm_routes
= num_routes
;
2907 card
->of_dapm_routes
= routes
;
2911 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing
);
2913 unsigned int snd_soc_of_parse_daifmt(struct device_node
*np
,
2915 struct device_node
**bitclkmaster
,
2916 struct device_node
**framemaster
)
2920 unsigned int format
= 0;
2926 } of_fmt_table
[] = {
2927 { "i2s", SND_SOC_DAIFMT_I2S
},
2928 { "right_j", SND_SOC_DAIFMT_RIGHT_J
},
2929 { "left_j", SND_SOC_DAIFMT_LEFT_J
},
2930 { "dsp_a", SND_SOC_DAIFMT_DSP_A
},
2931 { "dsp_b", SND_SOC_DAIFMT_DSP_B
},
2932 { "ac97", SND_SOC_DAIFMT_AC97
},
2933 { "pdm", SND_SOC_DAIFMT_PDM
},
2934 { "msb", SND_SOC_DAIFMT_MSB
},
2935 { "lsb", SND_SOC_DAIFMT_LSB
},
2942 * check "dai-format = xxx"
2943 * or "[prefix]format = xxx"
2944 * SND_SOC_DAIFMT_FORMAT_MASK area
2946 ret
= of_property_read_string(np
, "dai-format", &str
);
2948 snprintf(prop
, sizeof(prop
), "%sformat", prefix
);
2949 ret
= of_property_read_string(np
, prop
, &str
);
2952 for (i
= 0; i
< ARRAY_SIZE(of_fmt_table
); i
++) {
2953 if (strcmp(str
, of_fmt_table
[i
].name
) == 0) {
2954 format
|= of_fmt_table
[i
].val
;
2961 * check "[prefix]continuous-clock"
2962 * SND_SOC_DAIFMT_CLOCK_MASK area
2964 snprintf(prop
, sizeof(prop
), "%scontinuous-clock", prefix
);
2965 if (of_property_read_bool(np
, prop
))
2966 format
|= SND_SOC_DAIFMT_CONT
;
2968 format
|= SND_SOC_DAIFMT_GATED
;
2971 * check "[prefix]bitclock-inversion"
2972 * check "[prefix]frame-inversion"
2973 * SND_SOC_DAIFMT_INV_MASK area
2975 snprintf(prop
, sizeof(prop
), "%sbitclock-inversion", prefix
);
2976 bit
= !!of_get_property(np
, prop
, NULL
);
2978 snprintf(prop
, sizeof(prop
), "%sframe-inversion", prefix
);
2979 frame
= !!of_get_property(np
, prop
, NULL
);
2981 switch ((bit
<< 4) + frame
) {
2983 format
|= SND_SOC_DAIFMT_IB_IF
;
2986 format
|= SND_SOC_DAIFMT_IB_NF
;
2989 format
|= SND_SOC_DAIFMT_NB_IF
;
2992 /* SND_SOC_DAIFMT_NB_NF is default */
2997 * check "[prefix]bitclock-master"
2998 * check "[prefix]frame-master"
2999 * SND_SOC_DAIFMT_MASTER_MASK area
3001 snprintf(prop
, sizeof(prop
), "%sbitclock-master", prefix
);
3002 bit
= !!of_get_property(np
, prop
, NULL
);
3003 if (bit
&& bitclkmaster
)
3004 *bitclkmaster
= of_parse_phandle(np
, prop
, 0);
3006 snprintf(prop
, sizeof(prop
), "%sframe-master", prefix
);
3007 frame
= !!of_get_property(np
, prop
, NULL
);
3008 if (frame
&& framemaster
)
3009 *framemaster
= of_parse_phandle(np
, prop
, 0);
3011 switch ((bit
<< 4) + frame
) {
3013 format
|= SND_SOC_DAIFMT_CBM_CFM
;
3016 format
|= SND_SOC_DAIFMT_CBM_CFS
;
3019 format
|= SND_SOC_DAIFMT_CBS_CFM
;
3022 format
|= SND_SOC_DAIFMT_CBS_CFS
;
3028 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt
);
3030 int snd_soc_get_dai_id(struct device_node
*ep
)
3032 struct snd_soc_component
*component
;
3033 struct snd_soc_dai_link_component dlc
;
3036 dlc
.of_node
= of_graph_get_port_parent(ep
);
3039 * For example HDMI case, HDMI has video/sound port,
3040 * but ALSA SoC needs sound port number only.
3041 * Thus counting HDMI DT port/endpoint doesn't work.
3042 * Then, it should have .of_xlate_dai_id
3045 mutex_lock(&client_mutex
);
3046 component
= soc_find_component(&dlc
);
3048 ret
= snd_soc_component_of_xlate_dai_id(component
, ep
);
3049 mutex_unlock(&client_mutex
);
3051 of_node_put(dlc
.of_node
);
3055 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id
);
3057 int snd_soc_get_dai_name(struct of_phandle_args
*args
,
3058 const char **dai_name
)
3060 struct snd_soc_component
*pos
;
3061 struct device_node
*component_of_node
;
3062 int ret
= -EPROBE_DEFER
;
3064 mutex_lock(&client_mutex
);
3065 for_each_component(pos
) {
3066 component_of_node
= soc_component_to_node(pos
);
3068 if (component_of_node
!= args
->np
)
3071 ret
= snd_soc_component_of_xlate_dai_name(pos
, args
, dai_name
);
3072 if (ret
== -ENOTSUPP
) {
3073 struct snd_soc_dai
*dai
;
3076 switch (args
->args_count
) {
3078 id
= 0; /* same as dai_drv[0] */
3088 if (id
< 0 || id
>= pos
->num_dai
) {
3095 /* find target DAI */
3096 for_each_component_dais(pos
, dai
) {
3102 *dai_name
= dai
->driver
->name
;
3104 *dai_name
= pos
->name
;
3109 mutex_unlock(&client_mutex
);
3112 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name
);
3114 int snd_soc_of_get_dai_name(struct device_node
*of_node
,
3115 const char **dai_name
)
3117 struct of_phandle_args args
;
3120 ret
= of_parse_phandle_with_args(of_node
, "sound-dai",
3121 "#sound-dai-cells", 0, &args
);
3125 ret
= snd_soc_get_dai_name(&args
, dai_name
);
3127 of_node_put(args
.np
);
3131 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name
);
3134 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3135 * @dai_link: DAI link
3137 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3139 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link
*dai_link
)
3141 struct snd_soc_dai_link_component
*component
;
3144 for_each_link_codecs(dai_link
, index
, component
) {
3145 if (!component
->of_node
)
3147 of_node_put(component
->of_node
);
3148 component
->of_node
= NULL
;
3151 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs
);
3154 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3156 * @of_node: Device node
3157 * @dai_link: DAI link
3159 * Builds an array of CODEC DAI components from the DAI link property
3161 * The array is set in the DAI link and the number of DAIs is set accordingly.
3162 * The device nodes in the array (of_node) must be dereferenced by calling
3163 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3165 * Returns 0 for success
3167 int snd_soc_of_get_dai_link_codecs(struct device
*dev
,
3168 struct device_node
*of_node
,
3169 struct snd_soc_dai_link
*dai_link
)
3171 struct of_phandle_args args
;
3172 struct snd_soc_dai_link_component
*component
;
3174 int index
, num_codecs
, ret
;
3176 /* Count the number of CODECs */
3178 num_codecs
= of_count_phandle_with_args(of_node
, name
,
3179 "#sound-dai-cells");
3180 if (num_codecs
<= 0) {
3181 if (num_codecs
== -ENOENT
)
3182 dev_err(dev
, "No 'sound-dai' property\n");
3184 dev_err(dev
, "Bad phandle in 'sound-dai'\n");
3187 component
= devm_kcalloc(dev
,
3188 num_codecs
, sizeof(*component
),
3192 dai_link
->codecs
= component
;
3193 dai_link
->num_codecs
= num_codecs
;
3195 /* Parse the list */
3196 for_each_link_codecs(dai_link
, index
, component
) {
3197 ret
= of_parse_phandle_with_args(of_node
, name
,
3202 component
->of_node
= args
.np
;
3203 ret
= snd_soc_get_dai_name(&args
, &component
->dai_name
);
3209 snd_soc_of_put_dai_link_codecs(dai_link
);
3210 dai_link
->codecs
= NULL
;
3211 dai_link
->num_codecs
= 0;
3214 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs
);
3216 static int __init
snd_soc_init(void)
3218 snd_soc_debugfs_init();
3219 snd_soc_util_init();
3221 return platform_driver_register(&soc_driver
);
3223 module_init(snd_soc_init
);
3225 static void __exit
snd_soc_exit(void)
3227 snd_soc_util_exit();
3228 snd_soc_debugfs_exit();
3230 platform_driver_unregister(&soc_driver
);
3232 module_exit(snd_soc_exit
);
3234 /* Module information */
3235 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3236 MODULE_DESCRIPTION("ALSA SoC Core");
3237 MODULE_LICENSE("GPL");
3238 MODULE_ALIAS("platform:soc-audio");