2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/pinctrl/consumer.h>
34 #include <linux/ctype.h>
35 #include <linux/slab.h>
37 #include <linux/dmi.h>
38 #include <sound/core.h>
39 #include <sound/jack.h>
40 #include <sound/pcm.h>
41 #include <sound/pcm_params.h>
42 #include <sound/soc.h>
43 #include <sound/soc-dpcm.h>
44 #include <sound/soc-topology.h>
45 #include <sound/initval.h>
47 #define CREATE_TRACE_POINTS
48 #include <trace/events/asoc.h>
52 #ifdef CONFIG_DEBUG_FS
53 struct dentry
*snd_soc_debugfs_root
;
54 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root
);
57 static DEFINE_MUTEX(client_mutex
);
58 static LIST_HEAD(platform_list
);
59 static LIST_HEAD(codec_list
);
60 static LIST_HEAD(component_list
);
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 /* returns the minimum number of bytes needed to represent
72 * a particular given value */
73 static int min_bytes_needed(unsigned long val
)
78 for (i
= (sizeof val
* 8) - 1; i
>= 0; --i
, ++c
)
81 c
= (sizeof val
* 8) - c
;
89 /* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91 static int format_register_str(struct snd_soc_codec
*codec
,
92 unsigned int reg
, char *buf
, size_t len
)
94 int wordsize
= min_bytes_needed(codec
->driver
->reg_cache_size
) * 2;
95 int regsize
= codec
->driver
->reg_word_size
* 2;
98 /* +2 for ': ' and + 1 for '\n' */
99 if (wordsize
+ regsize
+ 2 + 1 != len
)
102 sprintf(buf
, "%.*x: ", wordsize
, reg
);
105 ret
= snd_soc_read(codec
, reg
);
107 memset(buf
, 'X', regsize
);
109 sprintf(buf
, "%.*x", regsize
, ret
);
111 /* no NUL-termination needed */
115 /* codec register dump */
116 static ssize_t
soc_codec_reg_show(struct snd_soc_codec
*codec
, char *buf
,
117 size_t count
, loff_t pos
)
120 int wordsize
, regsize
;
125 wordsize
= min_bytes_needed(codec
->driver
->reg_cache_size
) * 2;
126 regsize
= codec
->driver
->reg_word_size
* 2;
128 len
= wordsize
+ regsize
+ 2 + 1;
130 if (!codec
->driver
->reg_cache_size
)
133 if (codec
->driver
->reg_cache_step
)
134 step
= codec
->driver
->reg_cache_step
;
136 for (i
= 0; i
< codec
->driver
->reg_cache_size
; i
+= step
) {
137 /* only support larger than PAGE_SIZE bytes debugfs
138 * entries for the default case */
140 if (total
+ len
>= count
- 1)
142 format_register_str(codec
, i
, buf
+ total
, len
);
148 total
= min(total
, count
- 1);
153 static ssize_t
codec_reg_show(struct device
*dev
,
154 struct device_attribute
*attr
, char *buf
)
156 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
158 return soc_codec_reg_show(rtd
->codec
, buf
, PAGE_SIZE
, 0);
161 static DEVICE_ATTR(codec_reg
, 0444, codec_reg_show
, NULL
);
163 static ssize_t
pmdown_time_show(struct device
*dev
,
164 struct device_attribute
*attr
, char *buf
)
166 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
168 return sprintf(buf
, "%ld\n", rtd
->pmdown_time
);
171 static ssize_t
pmdown_time_set(struct device
*dev
,
172 struct device_attribute
*attr
,
173 const char *buf
, size_t count
)
175 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
178 ret
= kstrtol(buf
, 10, &rtd
->pmdown_time
);
185 static DEVICE_ATTR(pmdown_time
, 0644, pmdown_time_show
, pmdown_time_set
);
187 static struct attribute
*soc_dev_attrs
[] = {
188 &dev_attr_codec_reg
.attr
,
189 &dev_attr_pmdown_time
.attr
,
193 static umode_t
soc_dev_attr_is_visible(struct kobject
*kobj
,
194 struct attribute
*attr
, int idx
)
196 struct device
*dev
= kobj_to_dev(kobj
);
197 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
199 if (attr
== &dev_attr_pmdown_time
.attr
)
200 return attr
->mode
; /* always visible */
201 return rtd
->codec
? attr
->mode
: 0; /* enabled only with codec */
204 static const struct attribute_group soc_dapm_dev_group
= {
205 .attrs
= soc_dapm_dev_attrs
,
206 .is_visible
= soc_dev_attr_is_visible
,
209 static const struct attribute_group soc_dev_roup
= {
210 .attrs
= soc_dev_attrs
,
211 .is_visible
= soc_dev_attr_is_visible
,
214 static const struct attribute_group
*soc_dev_attr_groups
[] = {
220 #ifdef CONFIG_DEBUG_FS
221 static ssize_t
codec_reg_read_file(struct file
*file
, char __user
*user_buf
,
222 size_t count
, loff_t
*ppos
)
225 struct snd_soc_codec
*codec
= file
->private_data
;
228 if (*ppos
< 0 || !count
)
231 buf
= kmalloc(count
, GFP_KERNEL
);
235 ret
= soc_codec_reg_show(codec
, buf
, count
, *ppos
);
237 if (copy_to_user(user_buf
, buf
, ret
)) {
248 static ssize_t
codec_reg_write_file(struct file
*file
,
249 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
254 unsigned long reg
, value
;
255 struct snd_soc_codec
*codec
= file
->private_data
;
258 buf_size
= min(count
, (sizeof(buf
)-1));
259 if (copy_from_user(buf
, user_buf
, buf_size
))
263 while (*start
== ' ')
265 reg
= simple_strtoul(start
, &start
, 16);
266 while (*start
== ' ')
268 ret
= kstrtoul(start
, 16, &value
);
272 /* Userspace has been fiddling around behind the kernel's back */
273 add_taint(TAINT_USER
, LOCKDEP_NOW_UNRELIABLE
);
275 snd_soc_write(codec
, reg
, value
);
279 static const struct file_operations codec_reg_fops
= {
281 .read
= codec_reg_read_file
,
282 .write
= codec_reg_write_file
,
283 .llseek
= default_llseek
,
286 static void soc_init_component_debugfs(struct snd_soc_component
*component
)
288 if (!component
->card
->debugfs_card_root
)
291 if (component
->debugfs_prefix
) {
294 name
= kasprintf(GFP_KERNEL
, "%s:%s",
295 component
->debugfs_prefix
, component
->name
);
297 component
->debugfs_root
= debugfs_create_dir(name
,
298 component
->card
->debugfs_card_root
);
302 component
->debugfs_root
= debugfs_create_dir(component
->name
,
303 component
->card
->debugfs_card_root
);
306 if (!component
->debugfs_root
) {
307 dev_warn(component
->dev
,
308 "ASoC: Failed to create component debugfs directory\n");
312 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component
),
313 component
->debugfs_root
);
315 if (component
->init_debugfs
)
316 component
->init_debugfs(component
);
319 static void soc_cleanup_component_debugfs(struct snd_soc_component
*component
)
321 debugfs_remove_recursive(component
->debugfs_root
);
324 static void soc_init_codec_debugfs(struct snd_soc_component
*component
)
326 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
328 codec
->debugfs_reg
= debugfs_create_file("codec_reg", 0644,
329 codec
->component
.debugfs_root
,
330 codec
, &codec_reg_fops
);
331 if (!codec
->debugfs_reg
)
333 "ASoC: Failed to create codec register debugfs file\n");
336 static ssize_t
codec_list_read_file(struct file
*file
, char __user
*user_buf
,
337 size_t count
, loff_t
*ppos
)
339 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
340 ssize_t len
, ret
= 0;
341 struct snd_soc_codec
*codec
;
346 mutex_lock(&client_mutex
);
348 list_for_each_entry(codec
, &codec_list
, list
) {
349 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
350 codec
->component
.name
);
353 if (ret
> PAGE_SIZE
) {
359 mutex_unlock(&client_mutex
);
362 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
369 static const struct file_operations codec_list_fops
= {
370 .read
= codec_list_read_file
,
371 .llseek
= default_llseek
,/* read accesses f_pos */
374 static ssize_t
dai_list_read_file(struct file
*file
, char __user
*user_buf
,
375 size_t count
, loff_t
*ppos
)
377 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
378 ssize_t len
, ret
= 0;
379 struct snd_soc_component
*component
;
380 struct snd_soc_dai
*dai
;
385 mutex_lock(&client_mutex
);
387 list_for_each_entry(component
, &component_list
, list
) {
388 list_for_each_entry(dai
, &component
->dai_list
, list
) {
389 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
393 if (ret
> PAGE_SIZE
) {
400 mutex_unlock(&client_mutex
);
402 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
409 static const struct file_operations dai_list_fops
= {
410 .read
= dai_list_read_file
,
411 .llseek
= default_llseek
,/* read accesses f_pos */
414 static ssize_t
platform_list_read_file(struct file
*file
,
415 char __user
*user_buf
,
416 size_t count
, loff_t
*ppos
)
418 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
419 ssize_t len
, ret
= 0;
420 struct snd_soc_platform
*platform
;
425 mutex_lock(&client_mutex
);
427 list_for_each_entry(platform
, &platform_list
, list
) {
428 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
429 platform
->component
.name
);
432 if (ret
> PAGE_SIZE
) {
438 mutex_unlock(&client_mutex
);
440 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
447 static const struct file_operations platform_list_fops
= {
448 .read
= platform_list_read_file
,
449 .llseek
= default_llseek
,/* read accesses f_pos */
452 static void soc_init_card_debugfs(struct snd_soc_card
*card
)
454 if (!snd_soc_debugfs_root
)
457 card
->debugfs_card_root
= debugfs_create_dir(card
->name
,
458 snd_soc_debugfs_root
);
459 if (!card
->debugfs_card_root
) {
461 "ASoC: Failed to create card debugfs directory\n");
465 card
->debugfs_pop_time
= debugfs_create_u32("dapm_pop_time", 0644,
466 card
->debugfs_card_root
,
468 if (!card
->debugfs_pop_time
)
470 "ASoC: Failed to create pop time debugfs file\n");
473 static void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
475 debugfs_remove_recursive(card
->debugfs_card_root
);
479 static void snd_soc_debugfs_init(void)
481 snd_soc_debugfs_root
= debugfs_create_dir("asoc", NULL
);
482 if (IS_ERR(snd_soc_debugfs_root
) || !snd_soc_debugfs_root
) {
483 pr_warn("ASoC: Failed to create debugfs directory\n");
484 snd_soc_debugfs_root
= NULL
;
488 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root
, NULL
,
490 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
492 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root
, NULL
,
494 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
496 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root
, NULL
,
497 &platform_list_fops
))
498 pr_warn("ASoC: Failed to create platform list debugfs file\n");
501 static void snd_soc_debugfs_exit(void)
503 debugfs_remove_recursive(snd_soc_debugfs_root
);
508 #define soc_init_codec_debugfs NULL
510 static inline void soc_init_component_debugfs(
511 struct snd_soc_component
*component
)
515 static inline void soc_cleanup_component_debugfs(
516 struct snd_soc_component
*component
)
520 static inline void soc_init_card_debugfs(struct snd_soc_card
*card
)
524 static inline void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
528 static inline void snd_soc_debugfs_init(void)
532 static inline void snd_soc_debugfs_exit(void)
538 struct snd_pcm_substream
*snd_soc_get_dai_substream(struct snd_soc_card
*card
,
539 const char *dai_link
, int stream
)
541 struct snd_soc_pcm_runtime
*rtd
;
543 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
544 if (rtd
->dai_link
->no_pcm
&&
545 !strcmp(rtd
->dai_link
->name
, dai_link
))
546 return rtd
->pcm
->streams
[stream
].substream
;
548 dev_dbg(card
->dev
, "ASoC: failed to find dai link %s\n", dai_link
);
551 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream
);
553 static struct snd_soc_pcm_runtime
*soc_new_pcm_runtime(
554 struct snd_soc_card
*card
, struct snd_soc_dai_link
*dai_link
)
556 struct snd_soc_pcm_runtime
*rtd
;
558 rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
), GFP_KERNEL
);
563 rtd
->dai_link
= dai_link
;
564 rtd
->codec_dais
= kzalloc(sizeof(struct snd_soc_dai
*) *
565 dai_link
->num_codecs
,
567 if (!rtd
->codec_dais
) {
575 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime
*rtd
)
577 if (rtd
&& rtd
->codec_dais
)
578 kfree(rtd
->codec_dais
);
582 static void soc_add_pcm_runtime(struct snd_soc_card
*card
,
583 struct snd_soc_pcm_runtime
*rtd
)
585 list_add_tail(&rtd
->list
, &card
->rtd_list
);
586 rtd
->num
= card
->num_rtd
;
590 static void soc_remove_pcm_runtimes(struct snd_soc_card
*card
)
592 struct snd_soc_pcm_runtime
*rtd
, *_rtd
;
594 list_for_each_entry_safe(rtd
, _rtd
, &card
->rtd_list
, list
) {
595 list_del(&rtd
->list
);
596 soc_free_pcm_runtime(rtd
);
602 struct snd_soc_pcm_runtime
*snd_soc_get_pcm_runtime(struct snd_soc_card
*card
,
603 const char *dai_link
)
605 struct snd_soc_pcm_runtime
*rtd
;
607 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
608 if (!strcmp(rtd
->dai_link
->name
, dai_link
))
611 dev_dbg(card
->dev
, "ASoC: failed to find rtd %s\n", dai_link
);
614 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime
);
616 static void codec2codec_close_delayed_work(struct work_struct
*work
)
618 /* Currently nothing to do for c2c links
619 * Since c2c links are internal nodes in the DAPM graph and
620 * don't interface with the outside world or application layer
621 * we don't have to do any special handling on close.
625 #ifdef CONFIG_PM_SLEEP
626 /* powers down audio subsystem for suspend */
627 int snd_soc_suspend(struct device
*dev
)
629 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
630 struct snd_soc_component
*component
;
631 struct snd_soc_pcm_runtime
*rtd
;
634 /* If the card is not initialized yet there is nothing to do */
635 if (!card
->instantiated
)
638 /* Due to the resume being scheduled into a workqueue we could
639 * suspend before that's finished - wait for it to complete.
641 snd_power_lock(card
->snd_card
);
642 snd_power_wait(card
->snd_card
, SNDRV_CTL_POWER_D0
);
643 snd_power_unlock(card
->snd_card
);
645 /* we're going to block userspace touching us until resume completes */
646 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D3hot
);
648 /* mute any active DACs */
649 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
651 if (rtd
->dai_link
->ignore_suspend
)
654 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
655 struct snd_soc_dai
*dai
= rtd
->codec_dais
[i
];
656 struct snd_soc_dai_driver
*drv
= dai
->driver
;
658 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
659 drv
->ops
->digital_mute(dai
, 1);
663 /* suspend all pcms */
664 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
665 if (rtd
->dai_link
->ignore_suspend
)
668 snd_pcm_suspend_all(rtd
->pcm
);
671 if (card
->suspend_pre
)
672 card
->suspend_pre(card
);
674 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
675 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
677 if (rtd
->dai_link
->ignore_suspend
)
680 if (cpu_dai
->driver
->suspend
&& !cpu_dai
->driver
->bus_control
)
681 cpu_dai
->driver
->suspend(cpu_dai
);
684 /* close any waiting streams */
685 list_for_each_entry(rtd
, &card
->rtd_list
, list
)
686 flush_delayed_work(&rtd
->delayed_work
);
688 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
690 if (rtd
->dai_link
->ignore_suspend
)
693 snd_soc_dapm_stream_event(rtd
,
694 SNDRV_PCM_STREAM_PLAYBACK
,
695 SND_SOC_DAPM_STREAM_SUSPEND
);
697 snd_soc_dapm_stream_event(rtd
,
698 SNDRV_PCM_STREAM_CAPTURE
,
699 SND_SOC_DAPM_STREAM_SUSPEND
);
702 /* Recheck all endpoints too, their state is affected by suspend */
703 dapm_mark_endpoints_dirty(card
);
704 snd_soc_dapm_sync(&card
->dapm
);
706 /* suspend all COMPONENTs */
707 list_for_each_entry(component
, &card
->component_dev_list
, card_list
) {
708 struct snd_soc_dapm_context
*dapm
= snd_soc_component_get_dapm(component
);
710 /* If there are paths active then the COMPONENT will be held with
711 * bias _ON and should not be suspended. */
712 if (!component
->suspended
) {
713 switch (snd_soc_dapm_get_bias_level(dapm
)) {
714 case SND_SOC_BIAS_STANDBY
:
716 * If the COMPONENT is capable of idle
717 * bias off then being in STANDBY
718 * means it's doing something,
719 * otherwise fall through.
721 if (dapm
->idle_bias_off
) {
722 dev_dbg(component
->dev
,
723 "ASoC: idle_bias_off CODEC on over suspend\n");
727 case SND_SOC_BIAS_OFF
:
728 if (component
->suspend
)
729 component
->suspend(component
);
730 component
->suspended
= 1;
731 if (component
->regmap
)
732 regcache_mark_dirty(component
->regmap
);
733 /* deactivate pins to sleep state */
734 pinctrl_pm_select_sleep_state(component
->dev
);
737 dev_dbg(component
->dev
,
738 "ASoC: COMPONENT is on over suspend\n");
744 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
745 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
747 if (rtd
->dai_link
->ignore_suspend
)
750 if (cpu_dai
->driver
->suspend
&& cpu_dai
->driver
->bus_control
)
751 cpu_dai
->driver
->suspend(cpu_dai
);
753 /* deactivate pins to sleep state */
754 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
757 if (card
->suspend_post
)
758 card
->suspend_post(card
);
762 EXPORT_SYMBOL_GPL(snd_soc_suspend
);
764 /* deferred resume work, so resume can complete before we finished
765 * setting our codec back up, which can be very slow on I2C
767 static void soc_resume_deferred(struct work_struct
*work
)
769 struct snd_soc_card
*card
=
770 container_of(work
, struct snd_soc_card
, deferred_resume_work
);
771 struct snd_soc_pcm_runtime
*rtd
;
772 struct snd_soc_component
*component
;
775 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
776 * so userspace apps are blocked from touching us
779 dev_dbg(card
->dev
, "ASoC: starting resume work\n");
781 /* Bring us up into D2 so that DAPM starts enabling things */
782 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D2
);
784 if (card
->resume_pre
)
785 card
->resume_pre(card
);
787 /* resume control bus DAIs */
788 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
789 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
791 if (rtd
->dai_link
->ignore_suspend
)
794 if (cpu_dai
->driver
->resume
&& cpu_dai
->driver
->bus_control
)
795 cpu_dai
->driver
->resume(cpu_dai
);
798 list_for_each_entry(component
, &card
->component_dev_list
, card_list
) {
799 if (component
->suspended
) {
800 if (component
->resume
)
801 component
->resume(component
);
802 component
->suspended
= 0;
806 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
808 if (rtd
->dai_link
->ignore_suspend
)
811 snd_soc_dapm_stream_event(rtd
,
812 SNDRV_PCM_STREAM_PLAYBACK
,
813 SND_SOC_DAPM_STREAM_RESUME
);
815 snd_soc_dapm_stream_event(rtd
,
816 SNDRV_PCM_STREAM_CAPTURE
,
817 SND_SOC_DAPM_STREAM_RESUME
);
820 /* unmute any active DACs */
821 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
823 if (rtd
->dai_link
->ignore_suspend
)
826 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
827 struct snd_soc_dai
*dai
= rtd
->codec_dais
[i
];
828 struct snd_soc_dai_driver
*drv
= dai
->driver
;
830 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
831 drv
->ops
->digital_mute(dai
, 0);
835 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
836 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
838 if (rtd
->dai_link
->ignore_suspend
)
841 if (cpu_dai
->driver
->resume
&& !cpu_dai
->driver
->bus_control
)
842 cpu_dai
->driver
->resume(cpu_dai
);
845 if (card
->resume_post
)
846 card
->resume_post(card
);
848 dev_dbg(card
->dev
, "ASoC: resume work completed\n");
850 /* Recheck all endpoints too, their state is affected by suspend */
851 dapm_mark_endpoints_dirty(card
);
852 snd_soc_dapm_sync(&card
->dapm
);
854 /* userspace can access us now we are back as we were before */
855 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D0
);
858 /* powers up audio subsystem after a suspend */
859 int snd_soc_resume(struct device
*dev
)
861 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
862 bool bus_control
= false;
863 struct snd_soc_pcm_runtime
*rtd
;
865 /* If the card is not initialized yet there is nothing to do */
866 if (!card
->instantiated
)
869 /* activate pins from sleep state */
870 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
871 struct snd_soc_dai
**codec_dais
= rtd
->codec_dais
;
872 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
876 pinctrl_pm_select_default_state(cpu_dai
->dev
);
878 for (j
= 0; j
< rtd
->num_codecs
; j
++) {
879 struct snd_soc_dai
*codec_dai
= codec_dais
[j
];
880 if (codec_dai
->active
)
881 pinctrl_pm_select_default_state(codec_dai
->dev
);
886 * DAIs that also act as the control bus master might have other drivers
887 * hanging off them so need to resume immediately. Other drivers don't
888 * have that problem and may take a substantial amount of time to resume
889 * due to I/O costs and anti-pop so handle them out of line.
891 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
892 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
893 bus_control
|= cpu_dai
->driver
->bus_control
;
896 dev_dbg(dev
, "ASoC: Resuming control bus master immediately\n");
897 soc_resume_deferred(&card
->deferred_resume_work
);
899 dev_dbg(dev
, "ASoC: Scheduling resume work\n");
900 if (!schedule_work(&card
->deferred_resume_work
))
901 dev_err(dev
, "ASoC: resume work item may be lost\n");
906 EXPORT_SYMBOL_GPL(snd_soc_resume
);
908 #define snd_soc_suspend NULL
909 #define snd_soc_resume NULL
912 static const struct snd_soc_dai_ops null_dai_ops
= {
915 static struct snd_soc_component
*soc_find_component(
916 const struct device_node
*of_node
, const char *name
)
918 struct snd_soc_component
*component
;
920 lockdep_assert_held(&client_mutex
);
922 list_for_each_entry(component
, &component_list
, list
) {
924 if (component
->dev
->of_node
== of_node
)
926 } else if (strcmp(component
->name
, name
) == 0) {
935 * snd_soc_find_dai - Find a registered DAI
937 * @dlc: name of the DAI and optional component info to match
939 * This function will search all regsitered components and their DAIs to
940 * find the DAI of the same name. The component's of_node and name
941 * should also match if being specified.
943 * Return: pointer of DAI, or NULL if not found.
945 struct snd_soc_dai
*snd_soc_find_dai(
946 const struct snd_soc_dai_link_component
*dlc
)
948 struct snd_soc_component
*component
;
949 struct snd_soc_dai
*dai
;
950 struct device_node
*component_of_node
;
952 lockdep_assert_held(&client_mutex
);
954 /* Find CPU DAI from registered DAIs*/
955 list_for_each_entry(component
, &component_list
, list
) {
956 component_of_node
= component
->dev
->of_node
;
957 if (!component_of_node
&& component
->dev
->parent
)
958 component_of_node
= component
->dev
->parent
->of_node
;
960 if (dlc
->of_node
&& component_of_node
!= dlc
->of_node
)
962 if (dlc
->name
&& strcmp(component
->name
, dlc
->name
))
964 list_for_each_entry(dai
, &component
->dai_list
, list
) {
965 if (dlc
->dai_name
&& strcmp(dai
->name
, dlc
->dai_name
))
974 EXPORT_SYMBOL_GPL(snd_soc_find_dai
);
978 * snd_soc_find_dai_link - Find a DAI link
981 * @id: DAI link ID to match
982 * @name: DAI link name to match, optional
983 * @stream name: DAI link stream name to match, optional
985 * This function will search all existing DAI links of the soc card to
986 * find the link of the same ID. Since DAI links may not have their
987 * unique ID, so name and stream name should also match if being
990 * Return: pointer of DAI link, or NULL if not found.
992 struct snd_soc_dai_link
*snd_soc_find_dai_link(struct snd_soc_card
*card
,
993 int id
, const char *name
,
994 const char *stream_name
)
996 struct snd_soc_dai_link
*link
, *_link
;
998 lockdep_assert_held(&client_mutex
);
1000 list_for_each_entry_safe(link
, _link
, &card
->dai_link_list
, list
) {
1004 if (name
&& (!link
->name
|| strcmp(name
, link
->name
)))
1007 if (stream_name
&& (!link
->stream_name
1008 || strcmp(stream_name
, link
->stream_name
)))
1016 EXPORT_SYMBOL_GPL(snd_soc_find_dai_link
);
1018 static bool soc_is_dai_link_bound(struct snd_soc_card
*card
,
1019 struct snd_soc_dai_link
*dai_link
)
1021 struct snd_soc_pcm_runtime
*rtd
;
1023 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
1024 if (rtd
->dai_link
== dai_link
)
1031 static int soc_bind_dai_link(struct snd_soc_card
*card
,
1032 struct snd_soc_dai_link
*dai_link
)
1034 struct snd_soc_pcm_runtime
*rtd
;
1035 struct snd_soc_dai_link_component
*codecs
= dai_link
->codecs
;
1036 struct snd_soc_dai_link_component cpu_dai_component
;
1037 struct snd_soc_dai
**codec_dais
;
1038 struct snd_soc_platform
*platform
;
1039 struct device_node
*platform_of_node
;
1040 const char *platform_name
;
1043 dev_dbg(card
->dev
, "ASoC: binding %s\n", dai_link
->name
);
1045 if (soc_is_dai_link_bound(card
, dai_link
)) {
1046 dev_dbg(card
->dev
, "ASoC: dai link %s already bound\n",
1051 rtd
= soc_new_pcm_runtime(card
, dai_link
);
1055 cpu_dai_component
.name
= dai_link
->cpu_name
;
1056 cpu_dai_component
.of_node
= dai_link
->cpu_of_node
;
1057 cpu_dai_component
.dai_name
= dai_link
->cpu_dai_name
;
1058 rtd
->cpu_dai
= snd_soc_find_dai(&cpu_dai_component
);
1059 if (!rtd
->cpu_dai
) {
1060 dev_err(card
->dev
, "ASoC: CPU DAI %s not registered\n",
1061 dai_link
->cpu_dai_name
);
1065 rtd
->num_codecs
= dai_link
->num_codecs
;
1067 /* Find CODEC from registered CODECs */
1068 codec_dais
= rtd
->codec_dais
;
1069 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1070 codec_dais
[i
] = snd_soc_find_dai(&codecs
[i
]);
1071 if (!codec_dais
[i
]) {
1072 dev_err(card
->dev
, "ASoC: CODEC DAI %s not registered\n",
1073 codecs
[i
].dai_name
);
1078 /* Single codec links expect codec and codec_dai in runtime data */
1079 rtd
->codec_dai
= codec_dais
[0];
1080 rtd
->codec
= rtd
->codec_dai
->codec
;
1082 /* if there's no platform we match on the empty platform */
1083 platform_name
= dai_link
->platform_name
;
1084 if (!platform_name
&& !dai_link
->platform_of_node
)
1085 platform_name
= "snd-soc-dummy";
1087 /* find one from the set of registered platforms */
1088 list_for_each_entry(platform
, &platform_list
, list
) {
1089 platform_of_node
= platform
->dev
->of_node
;
1090 if (!platform_of_node
&& platform
->dev
->parent
->of_node
)
1091 platform_of_node
= platform
->dev
->parent
->of_node
;
1093 if (dai_link
->platform_of_node
) {
1094 if (platform_of_node
!= dai_link
->platform_of_node
)
1097 if (strcmp(platform
->component
.name
, platform_name
))
1101 rtd
->platform
= platform
;
1103 if (!rtd
->platform
) {
1104 dev_err(card
->dev
, "ASoC: platform %s not registered\n",
1105 dai_link
->platform_name
);
1109 soc_add_pcm_runtime(card
, rtd
);
1113 soc_free_pcm_runtime(rtd
);
1114 return -EPROBE_DEFER
;
1117 static void soc_remove_component(struct snd_soc_component
*component
)
1119 if (!component
->card
)
1122 list_del(&component
->card_list
);
1124 if (component
->remove
)
1125 component
->remove(component
);
1127 snd_soc_dapm_free(snd_soc_component_get_dapm(component
));
1129 soc_cleanup_component_debugfs(component
);
1130 component
->card
= NULL
;
1131 module_put(component
->dev
->driver
->owner
);
1134 static void soc_remove_dai(struct snd_soc_dai
*dai
, int order
)
1138 if (dai
&& dai
->probed
&&
1139 dai
->driver
->remove_order
== order
) {
1140 if (dai
->driver
->remove
) {
1141 err
= dai
->driver
->remove(dai
);
1144 "ASoC: failed to remove %s: %d\n",
1151 static void soc_remove_link_dais(struct snd_soc_card
*card
,
1152 struct snd_soc_pcm_runtime
*rtd
, int order
)
1156 /* unregister the rtd device */
1157 if (rtd
->dev_registered
) {
1158 device_unregister(rtd
->dev
);
1159 rtd
->dev_registered
= 0;
1162 /* remove the CODEC DAI */
1163 for (i
= 0; i
< rtd
->num_codecs
; i
++)
1164 soc_remove_dai(rtd
->codec_dais
[i
], order
);
1166 soc_remove_dai(rtd
->cpu_dai
, order
);
1169 static void soc_remove_link_components(struct snd_soc_card
*card
,
1170 struct snd_soc_pcm_runtime
*rtd
, int order
)
1172 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1173 struct snd_soc_platform
*platform
= rtd
->platform
;
1174 struct snd_soc_component
*component
;
1177 /* remove the platform */
1178 if (platform
&& platform
->component
.driver
->remove_order
== order
)
1179 soc_remove_component(&platform
->component
);
1181 /* remove the CODEC-side CODEC */
1182 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1183 component
= rtd
->codec_dais
[i
]->component
;
1184 if (component
->driver
->remove_order
== order
)
1185 soc_remove_component(component
);
1188 /* remove any CPU-side CODEC */
1190 if (cpu_dai
->component
->driver
->remove_order
== order
)
1191 soc_remove_component(cpu_dai
->component
);
1195 static void soc_remove_dai_links(struct snd_soc_card
*card
)
1198 struct snd_soc_pcm_runtime
*rtd
;
1199 struct snd_soc_dai_link
*link
, *_link
;
1201 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1203 list_for_each_entry(rtd
, &card
->rtd_list
, list
)
1204 soc_remove_link_dais(card
, rtd
, order
);
1207 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1209 list_for_each_entry(rtd
, &card
->rtd_list
, list
)
1210 soc_remove_link_components(card
, rtd
, order
);
1213 list_for_each_entry_safe(link
, _link
, &card
->dai_link_list
, list
) {
1214 if (link
->dobj
.type
== SND_SOC_DOBJ_DAI_LINK
)
1215 dev_warn(card
->dev
, "Topology forgot to remove link %s?\n",
1218 list_del(&link
->list
);
1219 card
->num_dai_links
--;
1223 static int snd_soc_init_multicodec(struct snd_soc_card
*card
,
1224 struct snd_soc_dai_link
*dai_link
)
1226 /* Legacy codec/codec_dai link is a single entry in multicodec */
1227 if (dai_link
->codec_name
|| dai_link
->codec_of_node
||
1228 dai_link
->codec_dai_name
) {
1229 dai_link
->num_codecs
= 1;
1231 dai_link
->codecs
= devm_kzalloc(card
->dev
,
1232 sizeof(struct snd_soc_dai_link_component
),
1234 if (!dai_link
->codecs
)
1237 dai_link
->codecs
[0].name
= dai_link
->codec_name
;
1238 dai_link
->codecs
[0].of_node
= dai_link
->codec_of_node
;
1239 dai_link
->codecs
[0].dai_name
= dai_link
->codec_dai_name
;
1242 if (!dai_link
->codecs
) {
1243 dev_err(card
->dev
, "ASoC: DAI link has no CODECs\n");
1250 static int soc_init_dai_link(struct snd_soc_card
*card
,
1251 struct snd_soc_dai_link
*link
)
1255 ret
= snd_soc_init_multicodec(card
, link
);
1257 dev_err(card
->dev
, "ASoC: failed to init multicodec\n");
1261 for (i
= 0; i
< link
->num_codecs
; i
++) {
1263 * Codec must be specified by 1 of name or OF node,
1264 * not both or neither.
1266 if (!!link
->codecs
[i
].name
==
1267 !!link
->codecs
[i
].of_node
) {
1268 dev_err(card
->dev
, "ASoC: Neither/both codec name/of_node are set for %s\n",
1272 /* Codec DAI name must be specified */
1273 if (!link
->codecs
[i
].dai_name
) {
1274 dev_err(card
->dev
, "ASoC: codec_dai_name not set for %s\n",
1281 * Platform may be specified by either name or OF node, but
1282 * can be left unspecified, and a dummy platform will be used.
1284 if (link
->platform_name
&& link
->platform_of_node
) {
1286 "ASoC: Both platform name/of_node are set for %s\n",
1292 * CPU device may be specified by either name or OF node, but
1293 * can be left unspecified, and will be matched based on DAI
1296 if (link
->cpu_name
&& link
->cpu_of_node
) {
1298 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1303 * At least one of CPU DAI name or CPU device name/node must be
1306 if (!link
->cpu_dai_name
&&
1307 !(link
->cpu_name
|| link
->cpu_of_node
)) {
1309 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1318 * snd_soc_add_dai_link - Add a DAI link dynamically
1319 * @card: The ASoC card to which the DAI link is added
1320 * @dai_link: The new DAI link to add
1322 * This function adds a DAI link to the ASoC card's link list.
1324 * Note: Topology can use this API to add DAI links when probing the
1325 * topology component. And machine drivers can still define static
1326 * DAI links in dai_link array.
1328 int snd_soc_add_dai_link(struct snd_soc_card
*card
,
1329 struct snd_soc_dai_link
*dai_link
)
1331 if (dai_link
->dobj
.type
1332 && dai_link
->dobj
.type
!= SND_SOC_DOBJ_DAI_LINK
) {
1333 dev_err(card
->dev
, "Invalid dai link type %d\n",
1334 dai_link
->dobj
.type
);
1338 lockdep_assert_held(&client_mutex
);
1339 /* Notify the machine driver for extra initialization
1340 * on the link created by topology.
1342 if (dai_link
->dobj
.type
&& card
->add_dai_link
)
1343 card
->add_dai_link(card
, dai_link
);
1345 list_add_tail(&dai_link
->list
, &card
->dai_link_list
);
1346 card
->num_dai_links
++;
1350 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link
);
1353 * snd_soc_remove_dai_link - Remove a DAI link from the list
1354 * @card: The ASoC card that owns the link
1355 * @dai_link: The DAI link to remove
1357 * This function removes a DAI link from the ASoC card's link list.
1359 * For DAI links previously added by topology, topology should
1360 * remove them by using the dobj embedded in the link.
1362 void snd_soc_remove_dai_link(struct snd_soc_card
*card
,
1363 struct snd_soc_dai_link
*dai_link
)
1365 struct snd_soc_dai_link
*link
, *_link
;
1367 if (dai_link
->dobj
.type
1368 && dai_link
->dobj
.type
!= SND_SOC_DOBJ_DAI_LINK
) {
1369 dev_err(card
->dev
, "Invalid dai link type %d\n",
1370 dai_link
->dobj
.type
);
1374 lockdep_assert_held(&client_mutex
);
1375 /* Notify the machine driver for extra destruction
1376 * on the link created by topology.
1378 if (dai_link
->dobj
.type
&& card
->remove_dai_link
)
1379 card
->remove_dai_link(card
, dai_link
);
1381 list_for_each_entry_safe(link
, _link
, &card
->dai_link_list
, list
) {
1382 if (link
== dai_link
) {
1383 list_del(&link
->list
);
1384 card
->num_dai_links
--;
1389 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link
);
1391 static void soc_set_name_prefix(struct snd_soc_card
*card
,
1392 struct snd_soc_component
*component
)
1396 if (card
->codec_conf
== NULL
)
1399 for (i
= 0; i
< card
->num_configs
; i
++) {
1400 struct snd_soc_codec_conf
*map
= &card
->codec_conf
[i
];
1401 if (map
->of_node
&& component
->dev
->of_node
!= map
->of_node
)
1403 if (map
->dev_name
&& strcmp(component
->name
, map
->dev_name
))
1405 component
->name_prefix
= map
->name_prefix
;
1410 static int soc_probe_component(struct snd_soc_card
*card
,
1411 struct snd_soc_component
*component
)
1413 struct snd_soc_dapm_context
*dapm
= snd_soc_component_get_dapm(component
);
1414 struct snd_soc_dai
*dai
;
1417 if (!strcmp(component
->name
, "snd-soc-dummy"))
1420 if (component
->card
) {
1421 if (component
->card
!= card
) {
1422 dev_err(component
->dev
,
1423 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1424 card
->name
, component
->card
->name
);
1430 if (!try_module_get(component
->dev
->driver
->owner
))
1433 component
->card
= card
;
1435 soc_set_name_prefix(card
, component
);
1437 soc_init_component_debugfs(component
);
1439 if (component
->dapm_widgets
) {
1440 ret
= snd_soc_dapm_new_controls(dapm
, component
->dapm_widgets
,
1441 component
->num_dapm_widgets
);
1444 dev_err(component
->dev
,
1445 "Failed to create new controls %d\n", ret
);
1450 list_for_each_entry(dai
, &component
->dai_list
, list
) {
1451 ret
= snd_soc_dapm_new_dai_widgets(dapm
, dai
);
1453 dev_err(component
->dev
,
1454 "Failed to create DAI widgets %d\n", ret
);
1459 if (component
->probe
) {
1460 ret
= component
->probe(component
);
1462 dev_err(component
->dev
,
1463 "ASoC: failed to probe component %d\n", ret
);
1467 WARN(dapm
->idle_bias_off
&&
1468 dapm
->bias_level
!= SND_SOC_BIAS_OFF
,
1469 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1473 /* machine specific init */
1474 if (component
->init
) {
1475 ret
= component
->init(component
);
1477 dev_err(component
->dev
,
1478 "Failed to do machine specific init %d\n", ret
);
1483 if (component
->controls
)
1484 snd_soc_add_component_controls(component
, component
->controls
,
1485 component
->num_controls
);
1486 if (component
->dapm_routes
)
1487 snd_soc_dapm_add_routes(dapm
, component
->dapm_routes
,
1488 component
->num_dapm_routes
);
1490 list_add(&dapm
->list
, &card
->dapm_list
);
1491 list_add(&component
->card_list
, &card
->component_dev_list
);
1496 soc_cleanup_component_debugfs(component
);
1497 component
->card
= NULL
;
1498 module_put(component
->dev
->driver
->owner
);
1503 static void rtd_release(struct device
*dev
)
1508 static int soc_post_component_init(struct snd_soc_pcm_runtime
*rtd
,
1513 /* register the rtd device */
1514 rtd
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
1517 device_initialize(rtd
->dev
);
1518 rtd
->dev
->parent
= rtd
->card
->dev
;
1519 rtd
->dev
->release
= rtd_release
;
1520 rtd
->dev
->groups
= soc_dev_attr_groups
;
1521 dev_set_name(rtd
->dev
, "%s", name
);
1522 dev_set_drvdata(rtd
->dev
, rtd
);
1523 mutex_init(&rtd
->pcm_mutex
);
1524 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_PLAYBACK
].be_clients
);
1525 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_CAPTURE
].be_clients
);
1526 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_PLAYBACK
].fe_clients
);
1527 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_CAPTURE
].fe_clients
);
1528 ret
= device_add(rtd
->dev
);
1530 /* calling put_device() here to free the rtd->dev */
1531 put_device(rtd
->dev
);
1532 dev_err(rtd
->card
->dev
,
1533 "ASoC: failed to register runtime device: %d\n", ret
);
1536 rtd
->dev_registered
= 1;
1540 static int soc_probe_link_components(struct snd_soc_card
*card
,
1541 struct snd_soc_pcm_runtime
*rtd
,
1544 struct snd_soc_platform
*platform
= rtd
->platform
;
1545 struct snd_soc_component
*component
;
1548 /* probe the CPU-side component, if it is a CODEC */
1549 component
= rtd
->cpu_dai
->component
;
1550 if (component
->driver
->probe_order
== order
) {
1551 ret
= soc_probe_component(card
, component
);
1556 /* probe the CODEC-side components */
1557 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1558 component
= rtd
->codec_dais
[i
]->component
;
1559 if (component
->driver
->probe_order
== order
) {
1560 ret
= soc_probe_component(card
, component
);
1566 /* probe the platform */
1567 if (platform
->component
.driver
->probe_order
== order
) {
1568 ret
= soc_probe_component(card
, &platform
->component
);
1576 static int soc_probe_dai(struct snd_soc_dai
*dai
, int order
)
1580 if (!dai
->probed
&& dai
->driver
->probe_order
== order
) {
1581 if (dai
->driver
->probe
) {
1582 ret
= dai
->driver
->probe(dai
);
1585 "ASoC: failed to probe DAI %s: %d\n",
1597 static int soc_link_dai_widgets(struct snd_soc_card
*card
,
1598 struct snd_soc_dai_link
*dai_link
,
1599 struct snd_soc_pcm_runtime
*rtd
)
1601 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1602 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
1603 struct snd_soc_dapm_widget
*sink
, *source
;
1606 if (rtd
->num_codecs
> 1)
1607 dev_warn(card
->dev
, "ASoC: Multiple codecs not supported yet\n");
1609 /* link the DAI widgets */
1610 sink
= codec_dai
->playback_widget
;
1611 source
= cpu_dai
->capture_widget
;
1612 if (sink
&& source
) {
1613 ret
= snd_soc_dapm_new_pcm(card
, dai_link
->params
,
1614 dai_link
->num_params
,
1617 dev_err(card
->dev
, "ASoC: Can't link %s to %s: %d\n",
1618 sink
->name
, source
->name
, ret
);
1623 sink
= cpu_dai
->playback_widget
;
1624 source
= codec_dai
->capture_widget
;
1625 if (sink
&& source
) {
1626 ret
= snd_soc_dapm_new_pcm(card
, dai_link
->params
,
1627 dai_link
->num_params
,
1630 dev_err(card
->dev
, "ASoC: Can't link %s to %s: %d\n",
1631 sink
->name
, source
->name
, ret
);
1639 static int soc_probe_link_dais(struct snd_soc_card
*card
,
1640 struct snd_soc_pcm_runtime
*rtd
, int order
)
1642 struct snd_soc_dai_link
*dai_link
= rtd
->dai_link
;
1643 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1646 dev_dbg(card
->dev
, "ASoC: probe %s dai link %d late %d\n",
1647 card
->name
, rtd
->num
, order
);
1649 /* set default power off timeout */
1650 rtd
->pmdown_time
= pmdown_time
;
1652 ret
= soc_probe_dai(cpu_dai
, order
);
1656 /* probe the CODEC DAI */
1657 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1658 ret
= soc_probe_dai(rtd
->codec_dais
[i
], order
);
1663 /* complete DAI probe during last probe */
1664 if (order
!= SND_SOC_COMP_ORDER_LAST
)
1667 /* do machine specific initialization */
1668 if (dai_link
->init
) {
1669 ret
= dai_link
->init(rtd
);
1671 dev_err(card
->dev
, "ASoC: failed to init %s: %d\n",
1672 dai_link
->name
, ret
);
1677 if (dai_link
->dai_fmt
)
1678 snd_soc_runtime_set_dai_fmt(rtd
, dai_link
->dai_fmt
);
1680 ret
= soc_post_component_init(rtd
, dai_link
->name
);
1684 #ifdef CONFIG_DEBUG_FS
1685 /* add DPCM sysfs entries */
1686 if (dai_link
->dynamic
)
1687 soc_dpcm_debugfs_add(rtd
);
1690 if (cpu_dai
->driver
->compress_new
) {
1691 /*create compress_device"*/
1692 ret
= cpu_dai
->driver
->compress_new(rtd
, rtd
->num
);
1694 dev_err(card
->dev
, "ASoC: can't create compress %s\n",
1695 dai_link
->stream_name
);
1700 if (!dai_link
->params
) {
1701 /* create the pcm */
1702 ret
= soc_new_pcm(rtd
, rtd
->num
);
1704 dev_err(card
->dev
, "ASoC: can't create pcm %s :%d\n",
1705 dai_link
->stream_name
, ret
);
1709 INIT_DELAYED_WORK(&rtd
->delayed_work
,
1710 codec2codec_close_delayed_work
);
1712 /* link the DAI widgets */
1713 ret
= soc_link_dai_widgets(card
, dai_link
, rtd
);
1722 static int soc_bind_aux_dev(struct snd_soc_card
*card
, int num
)
1724 struct snd_soc_aux_dev
*aux_dev
= &card
->aux_dev
[num
];
1725 struct snd_soc_component
*component
;
1727 struct device_node
*codec_of_node
;
1729 if (aux_dev
->codec_of_node
|| aux_dev
->codec_name
) {
1730 /* codecs, usually analog devices */
1731 name
= aux_dev
->codec_name
;
1732 codec_of_node
= aux_dev
->codec_of_node
;
1733 component
= soc_find_component(codec_of_node
, name
);
1736 name
= of_node_full_name(codec_of_node
);
1739 } else if (aux_dev
->name
) {
1740 /* generic components */
1741 name
= aux_dev
->name
;
1742 component
= soc_find_component(NULL
, name
);
1746 dev_err(card
->dev
, "ASoC: Invalid auxiliary device\n");
1750 component
->init
= aux_dev
->init
;
1751 component
->auxiliary
= 1;
1752 list_add(&component
->card_aux_list
, &card
->aux_comp_list
);
1757 dev_err(card
->dev
, "ASoC: %s not registered\n", name
);
1758 return -EPROBE_DEFER
;
1761 static int soc_probe_aux_devices(struct snd_soc_card
*card
)
1763 struct snd_soc_component
*comp
, *tmp
;
1767 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1769 list_for_each_entry_safe(comp
, tmp
, &card
->aux_comp_list
,
1771 if (comp
->driver
->probe_order
== order
) {
1772 ret
= soc_probe_component(card
, comp
);
1775 "ASoC: failed to probe aux component %s %d\n",
1779 list_del(&comp
->card_aux_list
);
1787 static void soc_remove_aux_devices(struct snd_soc_card
*card
)
1789 struct snd_soc_component
*comp
, *_comp
;
1792 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1794 list_for_each_entry_safe(comp
, _comp
,
1795 &card
->component_dev_list
, card_list
) {
1797 if (!comp
->auxiliary
)
1800 if (comp
->driver
->remove_order
== order
) {
1801 soc_remove_component(comp
);
1802 comp
->auxiliary
= 0;
1808 static int snd_soc_init_codec_cache(struct snd_soc_codec
*codec
)
1812 if (codec
->cache_init
)
1815 ret
= snd_soc_cache_init(codec
);
1818 "ASoC: Failed to set cache compression type: %d\n",
1822 codec
->cache_init
= 1;
1827 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1828 * @rtd: The runtime for which the DAI link format should be changed
1829 * @dai_fmt: The new DAI link format
1831 * This function updates the DAI link format for all DAIs connected to the DAI
1832 * link for the specified runtime.
1834 * Note: For setups with a static format set the dai_fmt field in the
1835 * corresponding snd_dai_link struct instead of using this function.
1837 * Returns 0 on success, otherwise a negative error code.
1839 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime
*rtd
,
1840 unsigned int dai_fmt
)
1842 struct snd_soc_dai
**codec_dais
= rtd
->codec_dais
;
1843 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1847 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1848 struct snd_soc_dai
*codec_dai
= codec_dais
[i
];
1850 ret
= snd_soc_dai_set_fmt(codec_dai
, dai_fmt
);
1851 if (ret
!= 0 && ret
!= -ENOTSUPP
) {
1852 dev_warn(codec_dai
->dev
,
1853 "ASoC: Failed to set DAI format: %d\n", ret
);
1858 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1859 if (cpu_dai
->codec
) {
1860 unsigned int inv_dai_fmt
;
1862 inv_dai_fmt
= dai_fmt
& ~SND_SOC_DAIFMT_MASTER_MASK
;
1863 switch (dai_fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1864 case SND_SOC_DAIFMT_CBM_CFM
:
1865 inv_dai_fmt
|= SND_SOC_DAIFMT_CBS_CFS
;
1867 case SND_SOC_DAIFMT_CBM_CFS
:
1868 inv_dai_fmt
|= SND_SOC_DAIFMT_CBS_CFM
;
1870 case SND_SOC_DAIFMT_CBS_CFM
:
1871 inv_dai_fmt
|= SND_SOC_DAIFMT_CBM_CFS
;
1873 case SND_SOC_DAIFMT_CBS_CFS
:
1874 inv_dai_fmt
|= SND_SOC_DAIFMT_CBM_CFM
;
1878 dai_fmt
= inv_dai_fmt
;
1881 ret
= snd_soc_dai_set_fmt(cpu_dai
, dai_fmt
);
1882 if (ret
!= 0 && ret
!= -ENOTSUPP
) {
1883 dev_warn(cpu_dai
->dev
,
1884 "ASoC: Failed to set DAI format: %d\n", ret
);
1890 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt
);
1893 /* Trim special characters, and replace '-' with '_' since '-' is used to
1894 * separate different DMI fields in the card long name. Only number and
1895 * alphabet characters and a few separator characters are kept.
1897 static void cleanup_dmi_name(char *name
)
1901 for (i
= 0; name
[i
]; i
++) {
1902 if (isalnum(name
[i
]) || (name
[i
] == '.')
1903 || (name
[i
] == '_'))
1904 name
[j
++] = name
[i
];
1905 else if (name
[i
] == '-')
1913 * snd_soc_set_dmi_name() - Register DMI names to card
1914 * @card: The card to register DMI names
1915 * @flavour: The flavour "differentiator" for the card amongst its peers.
1917 * An Intel machine driver may be used by many different devices but are
1918 * difficult for userspace to differentiate, since machine drivers ususally
1919 * use their own name as the card short name and leave the card long name
1920 * blank. To differentiate such devices and fix bugs due to lack of
1921 * device-specific configurations, this function allows DMI info to be used
1922 * as the sound card long name, in the format of
1923 * "vendor-product-version-board"
1924 * (Character '-' is used to separate different DMI fields here).
1925 * This will help the user space to load the device-specific Use Case Manager
1926 * (UCM) configurations for the card.
1928 * Possible card long names may be:
1929 * DellInc.-XPS139343-01-0310JH
1930 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1931 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1933 * This function also supports flavoring the card longname to provide
1934 * the extra differentiation, like "vendor-product-version-board-flavor".
1936 * We only keep number and alphabet characters and a few separator characters
1937 * in the card long name since UCM in the user space uses the card long names
1938 * as card configuration directory names and AudoConf cannot support special
1939 * charactors like SPACE.
1941 * Returns 0 on success, otherwise a negative error code.
1943 int snd_soc_set_dmi_name(struct snd_soc_card
*card
, const char *flavour
)
1945 const char *vendor
, *product
, *product_version
, *board
;
1946 size_t longname_buf_size
= sizeof(card
->snd_card
->longname
);
1949 if (card
->long_name
)
1950 return 0; /* long name already set by driver or from DMI */
1952 /* make up dmi long name as: vendor.product.version.board */
1953 vendor
= dmi_get_system_info(DMI_BOARD_VENDOR
);
1955 dev_warn(card
->dev
, "ASoC: no DMI vendor name!\n");
1959 snprintf(card
->dmi_longname
, sizeof(card
->snd_card
->longname
),
1961 cleanup_dmi_name(card
->dmi_longname
);
1963 product
= dmi_get_system_info(DMI_PRODUCT_NAME
);
1965 len
= strlen(card
->dmi_longname
);
1966 snprintf(card
->dmi_longname
+ len
,
1967 longname_buf_size
- len
,
1970 len
++; /* skip the separator "-" */
1971 if (len
< longname_buf_size
)
1972 cleanup_dmi_name(card
->dmi_longname
+ len
);
1974 /* some vendors like Lenovo may only put a self-explanatory
1975 * name in the product version field
1977 product_version
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
1978 if (product_version
) {
1979 len
= strlen(card
->dmi_longname
);
1980 snprintf(card
->dmi_longname
+ len
,
1981 longname_buf_size
- len
,
1982 "-%s", product_version
);
1985 if (len
< longname_buf_size
)
1986 cleanup_dmi_name(card
->dmi_longname
+ len
);
1990 board
= dmi_get_system_info(DMI_BOARD_NAME
);
1992 len
= strlen(card
->dmi_longname
);
1993 snprintf(card
->dmi_longname
+ len
,
1994 longname_buf_size
- len
,
1998 if (len
< longname_buf_size
)
1999 cleanup_dmi_name(card
->dmi_longname
+ len
);
2000 } else if (!product
) {
2001 /* fall back to using legacy name */
2002 dev_warn(card
->dev
, "ASoC: no DMI board/product name!\n");
2006 /* Add flavour to dmi long name */
2008 len
= strlen(card
->dmi_longname
);
2009 snprintf(card
->dmi_longname
+ len
,
2010 longname_buf_size
- len
,
2014 if (len
< longname_buf_size
)
2015 cleanup_dmi_name(card
->dmi_longname
+ len
);
2018 /* set the card long name */
2019 card
->long_name
= card
->dmi_longname
;
2023 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name
);
2025 static int snd_soc_instantiate_card(struct snd_soc_card
*card
)
2027 struct snd_soc_codec
*codec
;
2028 struct snd_soc_pcm_runtime
*rtd
;
2029 struct snd_soc_dai_link
*dai_link
;
2032 mutex_lock(&client_mutex
);
2033 mutex_lock_nested(&card
->mutex
, SND_SOC_CARD_CLASS_INIT
);
2036 for (i
= 0; i
< card
->num_links
; i
++) {
2037 ret
= soc_bind_dai_link(card
, &card
->dai_link
[i
]);
2042 /* bind aux_devs too */
2043 for (i
= 0; i
< card
->num_aux_devs
; i
++) {
2044 ret
= soc_bind_aux_dev(card
, i
);
2049 /* add predefined DAI links to the list */
2050 for (i
= 0; i
< card
->num_links
; i
++)
2051 snd_soc_add_dai_link(card
, card
->dai_link
+i
);
2053 /* initialize the register cache for each available codec */
2054 list_for_each_entry(codec
, &codec_list
, list
) {
2055 if (codec
->cache_init
)
2057 ret
= snd_soc_init_codec_cache(codec
);
2062 /* card bind complete so register a sound card */
2063 ret
= snd_card_new(card
->dev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
2064 card
->owner
, 0, &card
->snd_card
);
2067 "ASoC: can't create sound card for card %s: %d\n",
2072 soc_init_card_debugfs(card
);
2074 card
->dapm
.bias_level
= SND_SOC_BIAS_OFF
;
2075 card
->dapm
.dev
= card
->dev
;
2076 card
->dapm
.card
= card
;
2077 list_add(&card
->dapm
.list
, &card
->dapm_list
);
2079 #ifdef CONFIG_DEBUG_FS
2080 snd_soc_dapm_debugfs_init(&card
->dapm
, card
->debugfs_card_root
);
2083 #ifdef CONFIG_PM_SLEEP
2084 /* deferred resume work */
2085 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
2088 if (card
->dapm_widgets
)
2089 snd_soc_dapm_new_controls(&card
->dapm
, card
->dapm_widgets
,
2090 card
->num_dapm_widgets
);
2092 if (card
->of_dapm_widgets
)
2093 snd_soc_dapm_new_controls(&card
->dapm
, card
->of_dapm_widgets
,
2094 card
->num_of_dapm_widgets
);
2096 /* initialise the sound card only once */
2098 ret
= card
->probe(card
);
2100 goto card_probe_error
;
2103 /* probe all components used by DAI links on this card */
2104 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
2106 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
2107 ret
= soc_probe_link_components(card
, rtd
, order
);
2110 "ASoC: failed to instantiate card %d\n",
2117 /* probe auxiliary components */
2118 ret
= soc_probe_aux_devices(card
);
2122 /* Find new DAI links added during probing components and bind them.
2123 * Components with topology may bring new DAIs and DAI links.
2125 list_for_each_entry(dai_link
, &card
->dai_link_list
, list
) {
2126 if (soc_is_dai_link_bound(card
, dai_link
))
2129 ret
= soc_init_dai_link(card
, dai_link
);
2132 ret
= soc_bind_dai_link(card
, dai_link
);
2137 /* probe all DAI links on this card */
2138 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
2140 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
2141 ret
= soc_probe_link_dais(card
, rtd
, order
);
2144 "ASoC: failed to instantiate card %d\n",
2151 snd_soc_dapm_link_dai_widgets(card
);
2152 snd_soc_dapm_connect_dai_link_widgets(card
);
2155 snd_soc_add_card_controls(card
, card
->controls
, card
->num_controls
);
2157 if (card
->dapm_routes
)
2158 snd_soc_dapm_add_routes(&card
->dapm
, card
->dapm_routes
,
2159 card
->num_dapm_routes
);
2161 if (card
->of_dapm_routes
)
2162 snd_soc_dapm_add_routes(&card
->dapm
, card
->of_dapm_routes
,
2163 card
->num_of_dapm_routes
);
2165 snprintf(card
->snd_card
->shortname
, sizeof(card
->snd_card
->shortname
),
2167 snprintf(card
->snd_card
->longname
, sizeof(card
->snd_card
->longname
),
2168 "%s", card
->long_name
? card
->long_name
: card
->name
);
2169 snprintf(card
->snd_card
->driver
, sizeof(card
->snd_card
->driver
),
2170 "%s", card
->driver_name
? card
->driver_name
: card
->name
);
2171 for (i
= 0; i
< ARRAY_SIZE(card
->snd_card
->driver
); i
++) {
2172 switch (card
->snd_card
->driver
[i
]) {
2178 if (!isalnum(card
->snd_card
->driver
[i
]))
2179 card
->snd_card
->driver
[i
] = '_';
2184 if (card
->late_probe
) {
2185 ret
= card
->late_probe(card
);
2187 dev_err(card
->dev
, "ASoC: %s late_probe() failed: %d\n",
2189 goto probe_aux_dev_err
;
2193 snd_soc_dapm_new_widgets(card
);
2195 ret
= snd_card_register(card
->snd_card
);
2197 dev_err(card
->dev
, "ASoC: failed to register soundcard %d\n",
2199 goto probe_aux_dev_err
;
2202 card
->instantiated
= 1;
2203 snd_soc_dapm_sync(&card
->dapm
);
2204 mutex_unlock(&card
->mutex
);
2205 mutex_unlock(&client_mutex
);
2210 soc_remove_aux_devices(card
);
2213 soc_remove_dai_links(card
);
2219 snd_soc_dapm_free(&card
->dapm
);
2220 soc_cleanup_card_debugfs(card
);
2221 snd_card_free(card
->snd_card
);
2224 soc_remove_pcm_runtimes(card
);
2225 mutex_unlock(&card
->mutex
);
2226 mutex_unlock(&client_mutex
);
2231 /* probes a new socdev */
2232 static int soc_probe(struct platform_device
*pdev
)
2234 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
2237 * no card, so machine driver should be registering card
2238 * we should not be here in that case so ret error
2243 dev_warn(&pdev
->dev
,
2244 "ASoC: machine %s should use snd_soc_register_card()\n",
2247 /* Bodge while we unpick instantiation */
2248 card
->dev
= &pdev
->dev
;
2250 return snd_soc_register_card(card
);
2253 static int soc_cleanup_card_resources(struct snd_soc_card
*card
)
2255 struct snd_soc_pcm_runtime
*rtd
;
2257 /* make sure any delayed work runs */
2258 list_for_each_entry(rtd
, &card
->rtd_list
, list
)
2259 flush_delayed_work(&rtd
->delayed_work
);
2261 /* remove and free each DAI */
2262 soc_remove_dai_links(card
);
2263 soc_remove_pcm_runtimes(card
);
2265 /* remove auxiliary devices */
2266 soc_remove_aux_devices(card
);
2268 snd_soc_dapm_free(&card
->dapm
);
2269 soc_cleanup_card_debugfs(card
);
2271 /* remove the card */
2275 snd_card_free(card
->snd_card
);
2280 /* removes a socdev */
2281 static int soc_remove(struct platform_device
*pdev
)
2283 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
2285 snd_soc_unregister_card(card
);
2289 int snd_soc_poweroff(struct device
*dev
)
2291 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
2292 struct snd_soc_pcm_runtime
*rtd
;
2294 if (!card
->instantiated
)
2297 /* Flush out pmdown_time work - we actually do want to run it
2298 * now, we're shutting down so no imminent restart. */
2299 list_for_each_entry(rtd
, &card
->rtd_list
, list
)
2300 flush_delayed_work(&rtd
->delayed_work
);
2302 snd_soc_dapm_shutdown(card
);
2304 /* deactivate pins to sleep state */
2305 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
2306 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
2309 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
2310 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
2311 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[i
];
2312 pinctrl_pm_select_sleep_state(codec_dai
->dev
);
2318 EXPORT_SYMBOL_GPL(snd_soc_poweroff
);
2320 const struct dev_pm_ops snd_soc_pm_ops
= {
2321 .suspend
= snd_soc_suspend
,
2322 .resume
= snd_soc_resume
,
2323 .freeze
= snd_soc_suspend
,
2324 .thaw
= snd_soc_resume
,
2325 .poweroff
= snd_soc_poweroff
,
2326 .restore
= snd_soc_resume
,
2328 EXPORT_SYMBOL_GPL(snd_soc_pm_ops
);
2330 /* ASoC platform driver */
2331 static struct platform_driver soc_driver
= {
2333 .name
= "soc-audio",
2334 .pm
= &snd_soc_pm_ops
,
2337 .remove
= soc_remove
,
2341 * snd_soc_cnew - create new control
2342 * @_template: control template
2343 * @data: control private data
2344 * @long_name: control long name
2345 * @prefix: control name prefix
2347 * Create a new mixer control from a template control.
2349 * Returns 0 for success, else error.
2351 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
2352 void *data
, const char *long_name
,
2355 struct snd_kcontrol_new
template;
2356 struct snd_kcontrol
*kcontrol
;
2359 memcpy(&template, _template
, sizeof(template));
2363 long_name
= template.name
;
2366 name
= kasprintf(GFP_KERNEL
, "%s %s", prefix
, long_name
);
2370 template.name
= name
;
2372 template.name
= long_name
;
2375 kcontrol
= snd_ctl_new1(&template, data
);
2381 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
2383 static int snd_soc_add_controls(struct snd_card
*card
, struct device
*dev
,
2384 const struct snd_kcontrol_new
*controls
, int num_controls
,
2385 const char *prefix
, void *data
)
2389 for (i
= 0; i
< num_controls
; i
++) {
2390 const struct snd_kcontrol_new
*control
= &controls
[i
];
2391 err
= snd_ctl_add(card
, snd_soc_cnew(control
, data
,
2392 control
->name
, prefix
));
2394 dev_err(dev
, "ASoC: Failed to add %s: %d\n",
2395 control
->name
, err
);
2403 struct snd_kcontrol
*snd_soc_card_get_kcontrol(struct snd_soc_card
*soc_card
,
2406 struct snd_card
*card
= soc_card
->snd_card
;
2407 struct snd_kcontrol
*kctl
;
2409 if (unlikely(!name
))
2412 list_for_each_entry(kctl
, &card
->controls
, list
)
2413 if (!strncmp(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
)))
2417 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol
);
2420 * snd_soc_add_component_controls - Add an array of controls to a component.
2422 * @component: Component to add controls to
2423 * @controls: Array of controls to add
2424 * @num_controls: Number of elements in the array
2426 * Return: 0 for success, else error.
2428 int snd_soc_add_component_controls(struct snd_soc_component
*component
,
2429 const struct snd_kcontrol_new
*controls
, unsigned int num_controls
)
2431 struct snd_card
*card
= component
->card
->snd_card
;
2433 return snd_soc_add_controls(card
, component
->dev
, controls
,
2434 num_controls
, component
->name_prefix
, component
);
2436 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls
);
2439 * snd_soc_add_codec_controls - add an array of controls to a codec.
2440 * Convenience function to add a list of controls. Many codecs were
2441 * duplicating this code.
2443 * @codec: codec to add controls to
2444 * @controls: array of controls to add
2445 * @num_controls: number of elements in the array
2447 * Return 0 for success, else error.
2449 int snd_soc_add_codec_controls(struct snd_soc_codec
*codec
,
2450 const struct snd_kcontrol_new
*controls
, unsigned int num_controls
)
2452 return snd_soc_add_component_controls(&codec
->component
, controls
,
2455 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls
);
2458 * snd_soc_add_platform_controls - add an array of controls to a platform.
2459 * Convenience function to add a list of controls.
2461 * @platform: platform to add controls to
2462 * @controls: array of controls to add
2463 * @num_controls: number of elements in the array
2465 * Return 0 for success, else error.
2467 int snd_soc_add_platform_controls(struct snd_soc_platform
*platform
,
2468 const struct snd_kcontrol_new
*controls
, unsigned int num_controls
)
2470 return snd_soc_add_component_controls(&platform
->component
, controls
,
2473 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls
);
2476 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2477 * Convenience function to add a list of controls.
2479 * @soc_card: SoC card to add controls to
2480 * @controls: array of controls to add
2481 * @num_controls: number of elements in the array
2483 * Return 0 for success, else error.
2485 int snd_soc_add_card_controls(struct snd_soc_card
*soc_card
,
2486 const struct snd_kcontrol_new
*controls
, int num_controls
)
2488 struct snd_card
*card
= soc_card
->snd_card
;
2490 return snd_soc_add_controls(card
, soc_card
->dev
, controls
, num_controls
,
2493 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls
);
2496 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2497 * Convienience function to add a list of controls.
2499 * @dai: DAI to add controls to
2500 * @controls: array of controls to add
2501 * @num_controls: number of elements in the array
2503 * Return 0 for success, else error.
2505 int snd_soc_add_dai_controls(struct snd_soc_dai
*dai
,
2506 const struct snd_kcontrol_new
*controls
, int num_controls
)
2508 struct snd_card
*card
= dai
->component
->card
->snd_card
;
2510 return snd_soc_add_controls(card
, dai
->dev
, controls
, num_controls
,
2513 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls
);
2516 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2518 * @clk_id: DAI specific clock ID
2519 * @freq: new clock frequency in Hz
2520 * @dir: new clock direction - input/output.
2522 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2524 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
2525 unsigned int freq
, int dir
)
2527 if (dai
->driver
&& dai
->driver
->ops
->set_sysclk
)
2528 return dai
->driver
->ops
->set_sysclk(dai
, clk_id
, freq
, dir
);
2529 else if (dai
->codec
&& dai
->codec
->driver
->set_sysclk
)
2530 return dai
->codec
->driver
->set_sysclk(dai
->codec
, clk_id
, 0,
2535 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
2538 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2540 * @clk_id: DAI specific clock ID
2541 * @source: Source for the clock
2542 * @freq: new clock frequency in Hz
2543 * @dir: new clock direction - input/output.
2545 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2547 int snd_soc_codec_set_sysclk(struct snd_soc_codec
*codec
, int clk_id
,
2548 int source
, unsigned int freq
, int dir
)
2550 if (codec
->driver
->set_sysclk
)
2551 return codec
->driver
->set_sysclk(codec
, clk_id
, source
,
2556 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk
);
2559 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2561 * @div_id: DAI specific clock divider ID
2562 * @div: new clock divisor.
2564 * Configures the clock dividers. This is used to derive the best DAI bit and
2565 * frame clocks from the system or master clock. It's best to set the DAI bit
2566 * and frame clocks as low as possible to save system power.
2568 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
2569 int div_id
, int div
)
2571 if (dai
->driver
&& dai
->driver
->ops
->set_clkdiv
)
2572 return dai
->driver
->ops
->set_clkdiv(dai
, div_id
, div
);
2576 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
2579 * snd_soc_dai_set_pll - configure DAI PLL.
2581 * @pll_id: DAI specific PLL ID
2582 * @source: DAI specific source for the PLL
2583 * @freq_in: PLL input clock frequency in Hz
2584 * @freq_out: requested PLL output clock frequency in Hz
2586 * Configures and enables PLL to generate output clock based on input clock.
2588 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
2589 unsigned int freq_in
, unsigned int freq_out
)
2591 if (dai
->driver
&& dai
->driver
->ops
->set_pll
)
2592 return dai
->driver
->ops
->set_pll(dai
, pll_id
, source
,
2594 else if (dai
->codec
&& dai
->codec
->driver
->set_pll
)
2595 return dai
->codec
->driver
->set_pll(dai
->codec
, pll_id
, source
,
2600 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
2603 * snd_soc_codec_set_pll - configure codec PLL.
2605 * @pll_id: DAI specific PLL ID
2606 * @source: DAI specific source for the PLL
2607 * @freq_in: PLL input clock frequency in Hz
2608 * @freq_out: requested PLL output clock frequency in Hz
2610 * Configures and enables PLL to generate output clock based on input clock.
2612 int snd_soc_codec_set_pll(struct snd_soc_codec
*codec
, int pll_id
, int source
,
2613 unsigned int freq_in
, unsigned int freq_out
)
2615 if (codec
->driver
->set_pll
)
2616 return codec
->driver
->set_pll(codec
, pll_id
, source
,
2621 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll
);
2624 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2626 * @ratio: Ratio of BCLK to Sample rate.
2628 * Configures the DAI for a preset BCLK to sample rate ratio.
2630 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai
*dai
, unsigned int ratio
)
2632 if (dai
->driver
&& dai
->driver
->ops
->set_bclk_ratio
)
2633 return dai
->driver
->ops
->set_bclk_ratio(dai
, ratio
);
2637 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio
);
2640 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2642 * @fmt: SND_SOC_DAIFMT_ format value.
2644 * Configures the DAI hardware format and clocking.
2646 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
2648 if (dai
->driver
== NULL
)
2650 if (dai
->driver
->ops
->set_fmt
== NULL
)
2652 return dai
->driver
->ops
->set_fmt(dai
, fmt
);
2654 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
2657 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2658 * @slots: Number of slots in use.
2659 * @tx_mask: bitmask representing active TX slots.
2660 * @rx_mask: bitmask representing active RX slots.
2662 * Generates the TDM tx and rx slot default masks for DAI.
2664 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots
,
2665 unsigned int *tx_mask
,
2666 unsigned int *rx_mask
)
2668 if (*tx_mask
|| *rx_mask
)
2674 *tx_mask
= (1 << slots
) - 1;
2675 *rx_mask
= (1 << slots
) - 1;
2681 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2682 * @dai: The DAI to configure
2683 * @tx_mask: bitmask representing active TX slots.
2684 * @rx_mask: bitmask representing active RX slots.
2685 * @slots: Number of slots in use.
2686 * @slot_width: Width in bits for each slot.
2688 * This function configures the specified DAI for TDM operation. @slot contains
2689 * the total number of slots of the TDM stream and @slot_with the width of each
2690 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2691 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2692 * DAI should write to or read from. If a bit is set the corresponding slot is
2693 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2694 * the first slot, bit 1 to the second slot and so on. The first active slot
2695 * maps to the first channel of the DAI, the second active slot to the second
2696 * channel and so on.
2698 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2699 * @rx_mask and @slot_width will be ignored.
2701 * Returns 0 on success, a negative error code otherwise.
2703 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
2704 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
2706 if (dai
->driver
&& dai
->driver
->ops
->xlate_tdm_slot_mask
)
2707 dai
->driver
->ops
->xlate_tdm_slot_mask(slots
,
2708 &tx_mask
, &rx_mask
);
2710 snd_soc_xlate_tdm_slot_mask(slots
, &tx_mask
, &rx_mask
);
2712 dai
->tx_mask
= tx_mask
;
2713 dai
->rx_mask
= rx_mask
;
2715 if (dai
->driver
&& dai
->driver
->ops
->set_tdm_slot
)
2716 return dai
->driver
->ops
->set_tdm_slot(dai
, tx_mask
, rx_mask
,
2721 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
2724 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2726 * @tx_num: how many TX channels
2727 * @tx_slot: pointer to an array which imply the TX slot number channel
2729 * @rx_num: how many RX channels
2730 * @rx_slot: pointer to an array which imply the RX slot number channel
2733 * configure the relationship between channel number and TDM slot number.
2735 int snd_soc_dai_set_channel_map(struct snd_soc_dai
*dai
,
2736 unsigned int tx_num
, unsigned int *tx_slot
,
2737 unsigned int rx_num
, unsigned int *rx_slot
)
2739 if (dai
->driver
&& dai
->driver
->ops
->set_channel_map
)
2740 return dai
->driver
->ops
->set_channel_map(dai
, tx_num
, tx_slot
,
2745 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map
);
2748 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2750 * @tristate: tristate enable
2752 * Tristates the DAI so that others can use it.
2754 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
2756 if (dai
->driver
&& dai
->driver
->ops
->set_tristate
)
2757 return dai
->driver
->ops
->set_tristate(dai
, tristate
);
2761 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
2764 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2766 * @mute: mute enable
2767 * @direction: stream to mute
2769 * Mutes the DAI DAC.
2771 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
,
2777 if (dai
->driver
->ops
->mute_stream
)
2778 return dai
->driver
->ops
->mute_stream(dai
, mute
, direction
);
2779 else if (direction
== SNDRV_PCM_STREAM_PLAYBACK
&&
2780 dai
->driver
->ops
->digital_mute
)
2781 return dai
->driver
->ops
->digital_mute(dai
, mute
);
2785 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
2788 * snd_soc_register_card - Register a card with the ASoC core
2790 * @card: Card to register
2793 int snd_soc_register_card(struct snd_soc_card
*card
)
2796 struct snd_soc_pcm_runtime
*rtd
;
2798 if (!card
->name
|| !card
->dev
)
2801 for (i
= 0; i
< card
->num_links
; i
++) {
2802 struct snd_soc_dai_link
*link
= &card
->dai_link
[i
];
2804 ret
= soc_init_dai_link(card
, link
);
2806 dev_err(card
->dev
, "ASoC: failed to init link %s\n",
2812 dev_set_drvdata(card
->dev
, card
);
2814 snd_soc_initialize_card_lists(card
);
2816 INIT_LIST_HEAD(&card
->dai_link_list
);
2817 card
->num_dai_links
= 0;
2819 INIT_LIST_HEAD(&card
->rtd_list
);
2822 INIT_LIST_HEAD(&card
->dapm_dirty
);
2823 INIT_LIST_HEAD(&card
->dobj_list
);
2824 card
->instantiated
= 0;
2825 mutex_init(&card
->mutex
);
2826 mutex_init(&card
->dapm_mutex
);
2828 ret
= snd_soc_instantiate_card(card
);
2832 /* deactivate pins to sleep state */
2833 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
2834 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
2837 for (j
= 0; j
< rtd
->num_codecs
; j
++) {
2838 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[j
];
2839 if (!codec_dai
->active
)
2840 pinctrl_pm_select_sleep_state(codec_dai
->dev
);
2843 if (!cpu_dai
->active
)
2844 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
2849 EXPORT_SYMBOL_GPL(snd_soc_register_card
);
2852 * snd_soc_unregister_card - Unregister a card with the ASoC core
2854 * @card: Card to unregister
2857 int snd_soc_unregister_card(struct snd_soc_card
*card
)
2859 if (card
->instantiated
) {
2860 card
->instantiated
= false;
2861 snd_soc_dapm_shutdown(card
);
2862 soc_cleanup_card_resources(card
);
2863 dev_dbg(card
->dev
, "ASoC: Unregistered card '%s'\n", card
->name
);
2868 EXPORT_SYMBOL_GPL(snd_soc_unregister_card
);
2871 * Simplify DAI link configuration by removing ".-1" from device names
2872 * and sanitizing names.
2874 static char *fmt_single_name(struct device
*dev
, int *id
)
2876 char *found
, name
[NAME_SIZE
];
2879 if (dev_name(dev
) == NULL
)
2882 strlcpy(name
, dev_name(dev
), NAME_SIZE
);
2884 /* are we a "%s.%d" name (platform and SPI components) */
2885 found
= strstr(name
, dev
->driver
->name
);
2888 if (sscanf(&found
[strlen(dev
->driver
->name
)], ".%d", id
) == 1) {
2890 /* discard ID from name if ID == -1 */
2892 found
[strlen(dev
->driver
->name
)] = '\0';
2896 /* I2C component devices are named "bus-addr" */
2897 if (sscanf(name
, "%x-%x", &id1
, &id2
) == 2) {
2898 char tmp
[NAME_SIZE
];
2900 /* create unique ID number from I2C addr and bus */
2901 *id
= ((id1
& 0xffff) << 16) + id2
;
2903 /* sanitize component name for DAI link creation */
2904 snprintf(tmp
, NAME_SIZE
, "%s.%s", dev
->driver
->name
, name
);
2905 strlcpy(name
, tmp
, NAME_SIZE
);
2910 return kstrdup(name
, GFP_KERNEL
);
2914 * Simplify DAI link naming for single devices with multiple DAIs by removing
2915 * any ".-1" and using the DAI name (instead of device name).
2917 static inline char *fmt_multiple_name(struct device
*dev
,
2918 struct snd_soc_dai_driver
*dai_drv
)
2920 if (dai_drv
->name
== NULL
) {
2922 "ASoC: error - multiple DAI %s registered with no name\n",
2927 return kstrdup(dai_drv
->name
, GFP_KERNEL
);
2931 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2933 * @component: The component for which the DAIs should be unregistered
2935 static void snd_soc_unregister_dais(struct snd_soc_component
*component
)
2937 struct snd_soc_dai
*dai
, *_dai
;
2939 list_for_each_entry_safe(dai
, _dai
, &component
->dai_list
, list
) {
2940 dev_dbg(component
->dev
, "ASoC: Unregistered DAI '%s'\n",
2942 list_del(&dai
->list
);
2948 /* Create a DAI and add it to the component's DAI list */
2949 static struct snd_soc_dai
*soc_add_dai(struct snd_soc_component
*component
,
2950 struct snd_soc_dai_driver
*dai_drv
,
2951 bool legacy_dai_naming
)
2953 struct device
*dev
= component
->dev
;
2954 struct snd_soc_dai
*dai
;
2956 dev_dbg(dev
, "ASoC: dynamically register DAI %s\n", dev_name(dev
));
2958 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
2963 * Back in the old days when we still had component-less DAIs,
2964 * instead of having a static name, component-less DAIs would
2965 * inherit the name of the parent device so it is possible to
2966 * register multiple instances of the DAI. We still need to keep
2967 * the same naming style even though those DAIs are not
2968 * component-less anymore.
2970 if (legacy_dai_naming
&&
2971 (dai_drv
->id
== 0 || dai_drv
->name
== NULL
)) {
2972 dai
->name
= fmt_single_name(dev
, &dai
->id
);
2974 dai
->name
= fmt_multiple_name(dev
, dai_drv
);
2976 dai
->id
= dai_drv
->id
;
2978 dai
->id
= component
->num_dai
;
2980 if (dai
->name
== NULL
) {
2985 dai
->component
= component
;
2987 dai
->driver
= dai_drv
;
2988 if (!dai
->driver
->ops
)
2989 dai
->driver
->ops
= &null_dai_ops
;
2991 list_add(&dai
->list
, &component
->dai_list
);
2992 component
->num_dai
++;
2994 dev_dbg(dev
, "ASoC: Registered DAI '%s'\n", dai
->name
);
2999 * snd_soc_register_dais - Register a DAI with the ASoC core
3001 * @component: The component the DAIs are registered for
3002 * @dai_drv: DAI driver to use for the DAIs
3003 * @count: Number of DAIs
3004 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3007 static int snd_soc_register_dais(struct snd_soc_component
*component
,
3008 struct snd_soc_dai_driver
*dai_drv
, size_t count
,
3009 bool legacy_dai_naming
)
3011 struct device
*dev
= component
->dev
;
3012 struct snd_soc_dai
*dai
;
3016 dev_dbg(dev
, "ASoC: dai register %s #%Zu\n", dev_name(dev
), count
);
3018 component
->dai_drv
= dai_drv
;
3020 for (i
= 0; i
< count
; i
++) {
3022 dai
= soc_add_dai(component
, dai_drv
+ i
,
3023 count
== 1 && legacy_dai_naming
);
3033 snd_soc_unregister_dais(component
);
3039 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3041 * @component: The component the DAIs are registered for
3042 * @dai_drv: DAI driver to use for the DAI
3044 * Topology can use this API to register DAIs when probing a component.
3045 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3046 * will be freed in the component cleanup.
3048 int snd_soc_register_dai(struct snd_soc_component
*component
,
3049 struct snd_soc_dai_driver
*dai_drv
)
3051 struct snd_soc_dapm_context
*dapm
=
3052 snd_soc_component_get_dapm(component
);
3053 struct snd_soc_dai
*dai
;
3056 if (dai_drv
->dobj
.type
!= SND_SOC_DOBJ_PCM
) {
3057 dev_err(component
->dev
, "Invalid dai type %d\n",
3058 dai_drv
->dobj
.type
);
3062 lockdep_assert_held(&client_mutex
);
3063 dai
= soc_add_dai(component
, dai_drv
, false);
3067 /* Create the DAI widgets here. After adding DAIs, topology may
3068 * also add routes that need these widgets as source or sink.
3070 ret
= snd_soc_dapm_new_dai_widgets(dapm
, dai
);
3072 dev_err(component
->dev
,
3073 "Failed to create DAI widgets %d\n", ret
);
3078 EXPORT_SYMBOL_GPL(snd_soc_register_dai
);
3080 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context
*dapm
,
3081 enum snd_soc_dapm_type type
, int subseq
)
3083 struct snd_soc_component
*component
= dapm
->component
;
3085 component
->driver
->seq_notifier(component
, type
, subseq
);
3088 static int snd_soc_component_stream_event(struct snd_soc_dapm_context
*dapm
,
3091 struct snd_soc_component
*component
= dapm
->component
;
3093 return component
->driver
->stream_event(component
, event
);
3096 static int snd_soc_component_initialize(struct snd_soc_component
*component
,
3097 const struct snd_soc_component_driver
*driver
, struct device
*dev
)
3099 struct snd_soc_dapm_context
*dapm
;
3101 component
->name
= fmt_single_name(dev
, &component
->id
);
3102 if (!component
->name
) {
3103 dev_err(dev
, "ASoC: Failed to allocate name\n");
3107 component
->dev
= dev
;
3108 component
->driver
= driver
;
3109 component
->probe
= component
->driver
->probe
;
3110 component
->remove
= component
->driver
->remove
;
3111 component
->suspend
= component
->driver
->suspend
;
3112 component
->resume
= component
->driver
->resume
;
3113 component
->pcm_new
= component
->driver
->pcm_new
;
3114 component
->pcm_free
= component
->driver
->pcm_free
;
3116 dapm
= &component
->dapm
;
3118 dapm
->component
= component
;
3119 dapm
->bias_level
= SND_SOC_BIAS_OFF
;
3120 dapm
->idle_bias_off
= true;
3121 if (driver
->seq_notifier
)
3122 dapm
->seq_notifier
= snd_soc_component_seq_notifier
;
3123 if (driver
->stream_event
)
3124 dapm
->stream_event
= snd_soc_component_stream_event
;
3126 component
->controls
= driver
->controls
;
3127 component
->num_controls
= driver
->num_controls
;
3128 component
->dapm_widgets
= driver
->dapm_widgets
;
3129 component
->num_dapm_widgets
= driver
->num_dapm_widgets
;
3130 component
->dapm_routes
= driver
->dapm_routes
;
3131 component
->num_dapm_routes
= driver
->num_dapm_routes
;
3133 INIT_LIST_HEAD(&component
->dai_list
);
3134 mutex_init(&component
->io_mutex
);
3139 static void snd_soc_component_setup_regmap(struct snd_soc_component
*component
)
3141 int val_bytes
= regmap_get_val_bytes(component
->regmap
);
3143 /* Errors are legitimate for non-integer byte multiples */
3145 component
->val_bytes
= val_bytes
;
3148 #ifdef CONFIG_REGMAP
3151 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3152 * @component: The component for which to initialize the regmap instance
3153 * @regmap: The regmap instance that should be used by the component
3155 * This function allows deferred assignment of the regmap instance that is
3156 * associated with the component. Only use this if the regmap instance is not
3157 * yet ready when the component is registered. The function must also be called
3158 * before the first IO attempt of the component.
3160 void snd_soc_component_init_regmap(struct snd_soc_component
*component
,
3161 struct regmap
*regmap
)
3163 component
->regmap
= regmap
;
3164 snd_soc_component_setup_regmap(component
);
3166 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap
);
3169 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3170 * @component: The component for which to de-initialize the regmap instance
3172 * Calls regmap_exit() on the regmap instance associated to the component and
3173 * removes the regmap instance from the component.
3175 * This function should only be used if snd_soc_component_init_regmap() was used
3176 * to initialize the regmap instance.
3178 void snd_soc_component_exit_regmap(struct snd_soc_component
*component
)
3180 regmap_exit(component
->regmap
);
3181 component
->regmap
= NULL
;
3183 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap
);
3187 static void snd_soc_component_add_unlocked(struct snd_soc_component
*component
)
3189 if (!component
->write
&& !component
->read
) {
3190 if (!component
->regmap
)
3191 component
->regmap
= dev_get_regmap(component
->dev
, NULL
);
3192 if (component
->regmap
)
3193 snd_soc_component_setup_regmap(component
);
3196 list_add(&component
->list
, &component_list
);
3197 INIT_LIST_HEAD(&component
->dobj_list
);
3200 static void snd_soc_component_add(struct snd_soc_component
*component
)
3202 mutex_lock(&client_mutex
);
3203 snd_soc_component_add_unlocked(component
);
3204 mutex_unlock(&client_mutex
);
3207 static void snd_soc_component_cleanup(struct snd_soc_component
*component
)
3209 snd_soc_unregister_dais(component
);
3210 kfree(component
->name
);
3213 static void snd_soc_component_del_unlocked(struct snd_soc_component
*component
)
3215 list_del(&component
->list
);
3218 int snd_soc_register_component(struct device
*dev
,
3219 const struct snd_soc_component_driver
*cmpnt_drv
,
3220 struct snd_soc_dai_driver
*dai_drv
,
3223 struct snd_soc_component
*cmpnt
;
3226 cmpnt
= kzalloc(sizeof(*cmpnt
), GFP_KERNEL
);
3228 dev_err(dev
, "ASoC: Failed to allocate memory\n");
3232 ret
= snd_soc_component_initialize(cmpnt
, cmpnt_drv
, dev
);
3236 cmpnt
->ignore_pmdown_time
= true;
3237 cmpnt
->registered_as_component
= true;
3239 ret
= snd_soc_register_dais(cmpnt
, dai_drv
, num_dai
, true);
3241 dev_err(dev
, "ASoC: Failed to register DAIs: %d\n", ret
);
3245 snd_soc_component_add(cmpnt
);
3250 snd_soc_component_cleanup(cmpnt
);
3255 EXPORT_SYMBOL_GPL(snd_soc_register_component
);
3258 * snd_soc_unregister_component - Unregister a component from the ASoC core
3260 * @dev: The device to unregister
3262 void snd_soc_unregister_component(struct device
*dev
)
3264 struct snd_soc_component
*cmpnt
;
3266 mutex_lock(&client_mutex
);
3267 list_for_each_entry(cmpnt
, &component_list
, list
) {
3268 if (dev
== cmpnt
->dev
&& cmpnt
->registered_as_component
)
3271 mutex_unlock(&client_mutex
);
3275 snd_soc_tplg_component_remove(cmpnt
, SND_SOC_TPLG_INDEX_ALL
);
3276 snd_soc_component_del_unlocked(cmpnt
);
3277 mutex_unlock(&client_mutex
);
3278 snd_soc_component_cleanup(cmpnt
);
3281 EXPORT_SYMBOL_GPL(snd_soc_unregister_component
);
3283 static int snd_soc_platform_drv_probe(struct snd_soc_component
*component
)
3285 struct snd_soc_platform
*platform
= snd_soc_component_to_platform(component
);
3287 return platform
->driver
->probe(platform
);
3290 static void snd_soc_platform_drv_remove(struct snd_soc_component
*component
)
3292 struct snd_soc_platform
*platform
= snd_soc_component_to_platform(component
);
3294 platform
->driver
->remove(platform
);
3297 static int snd_soc_platform_drv_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
3299 struct snd_soc_platform
*platform
= rtd
->platform
;
3301 return platform
->driver
->pcm_new(rtd
);
3304 static void snd_soc_platform_drv_pcm_free(struct snd_pcm
*pcm
)
3306 struct snd_soc_pcm_runtime
*rtd
= pcm
->private_data
;
3307 struct snd_soc_platform
*platform
= rtd
->platform
;
3309 platform
->driver
->pcm_free(pcm
);
3313 * snd_soc_add_platform - Add a platform to the ASoC core
3314 * @dev: The parent device for the platform
3315 * @platform: The platform to add
3316 * @platform_drv: The driver for the platform
3318 int snd_soc_add_platform(struct device
*dev
, struct snd_soc_platform
*platform
,
3319 const struct snd_soc_platform_driver
*platform_drv
)
3323 ret
= snd_soc_component_initialize(&platform
->component
,
3324 &platform_drv
->component_driver
, dev
);
3328 platform
->dev
= dev
;
3329 platform
->driver
= platform_drv
;
3331 if (platform_drv
->probe
)
3332 platform
->component
.probe
= snd_soc_platform_drv_probe
;
3333 if (platform_drv
->remove
)
3334 platform
->component
.remove
= snd_soc_platform_drv_remove
;
3335 if (platform_drv
->pcm_new
)
3336 platform
->component
.pcm_new
= snd_soc_platform_drv_pcm_new
;
3337 if (platform_drv
->pcm_free
)
3338 platform
->component
.pcm_free
= snd_soc_platform_drv_pcm_free
;
3340 #ifdef CONFIG_DEBUG_FS
3341 platform
->component
.debugfs_prefix
= "platform";
3344 mutex_lock(&client_mutex
);
3345 snd_soc_component_add_unlocked(&platform
->component
);
3346 list_add(&platform
->list
, &platform_list
);
3347 mutex_unlock(&client_mutex
);
3349 dev_dbg(dev
, "ASoC: Registered platform '%s'\n",
3350 platform
->component
.name
);
3354 EXPORT_SYMBOL_GPL(snd_soc_add_platform
);
3357 * snd_soc_register_platform - Register a platform with the ASoC core
3359 * @dev: The device for the platform
3360 * @platform_drv: The driver for the platform
3362 int snd_soc_register_platform(struct device
*dev
,
3363 const struct snd_soc_platform_driver
*platform_drv
)
3365 struct snd_soc_platform
*platform
;
3368 dev_dbg(dev
, "ASoC: platform register %s\n", dev_name(dev
));
3370 platform
= kzalloc(sizeof(struct snd_soc_platform
), GFP_KERNEL
);
3371 if (platform
== NULL
)
3374 ret
= snd_soc_add_platform(dev
, platform
, platform_drv
);
3380 EXPORT_SYMBOL_GPL(snd_soc_register_platform
);
3383 * snd_soc_remove_platform - Remove a platform from the ASoC core
3384 * @platform: the platform to remove
3386 void snd_soc_remove_platform(struct snd_soc_platform
*platform
)
3389 mutex_lock(&client_mutex
);
3390 list_del(&platform
->list
);
3391 snd_soc_component_del_unlocked(&platform
->component
);
3392 mutex_unlock(&client_mutex
);
3394 dev_dbg(platform
->dev
, "ASoC: Unregistered platform '%s'\n",
3395 platform
->component
.name
);
3397 snd_soc_component_cleanup(&platform
->component
);
3399 EXPORT_SYMBOL_GPL(snd_soc_remove_platform
);
3401 struct snd_soc_platform
*snd_soc_lookup_platform(struct device
*dev
)
3403 struct snd_soc_platform
*platform
;
3405 mutex_lock(&client_mutex
);
3406 list_for_each_entry(platform
, &platform_list
, list
) {
3407 if (dev
== platform
->dev
) {
3408 mutex_unlock(&client_mutex
);
3412 mutex_unlock(&client_mutex
);
3416 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform
);
3419 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3421 * @dev: platform to unregister
3423 void snd_soc_unregister_platform(struct device
*dev
)
3425 struct snd_soc_platform
*platform
;
3427 platform
= snd_soc_lookup_platform(dev
);
3431 snd_soc_remove_platform(platform
);
3434 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform
);
3436 static u64 codec_format_map
[] = {
3437 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
,
3438 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_U16_BE
,
3439 SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S24_BE
,
3440 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_U24_BE
,
3441 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
,
3442 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_U32_BE
,
3443 SNDRV_PCM_FMTBIT_S24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3444 SNDRV_PCM_FMTBIT_U24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3445 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S20_3BE
,
3446 SNDRV_PCM_FMTBIT_U20_3LE
| SNDRV_PCM_FMTBIT_U20_3BE
,
3447 SNDRV_PCM_FMTBIT_S18_3LE
| SNDRV_PCM_FMTBIT_S18_3BE
,
3448 SNDRV_PCM_FMTBIT_U18_3LE
| SNDRV_PCM_FMTBIT_U18_3BE
,
3449 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
,
3450 SNDRV_PCM_FMTBIT_FLOAT64_LE
| SNDRV_PCM_FMTBIT_FLOAT64_BE
,
3451 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3452 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
,
3455 /* Fix up the DAI formats for endianness: codecs don't actually see
3456 * the endianness of the data but we're using the CPU format
3457 * definitions which do need to include endianness so we ensure that
3458 * codec DAIs always have both big and little endian variants set.
3460 static void fixup_codec_formats(struct snd_soc_pcm_stream
*stream
)
3464 for (i
= 0; i
< ARRAY_SIZE(codec_format_map
); i
++)
3465 if (stream
->formats
& codec_format_map
[i
])
3466 stream
->formats
|= codec_format_map
[i
];
3469 static int snd_soc_codec_drv_probe(struct snd_soc_component
*component
)
3471 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
3473 return codec
->driver
->probe(codec
);
3476 static void snd_soc_codec_drv_remove(struct snd_soc_component
*component
)
3478 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
3480 codec
->driver
->remove(codec
);
3483 static int snd_soc_codec_drv_suspend(struct snd_soc_component
*component
)
3485 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
3487 return codec
->driver
->suspend(codec
);
3490 static int snd_soc_codec_drv_resume(struct snd_soc_component
*component
)
3492 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
3494 return codec
->driver
->resume(codec
);
3497 static int snd_soc_codec_drv_write(struct snd_soc_component
*component
,
3498 unsigned int reg
, unsigned int val
)
3500 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
3502 return codec
->driver
->write(codec
, reg
, val
);
3505 static int snd_soc_codec_drv_read(struct snd_soc_component
*component
,
3506 unsigned int reg
, unsigned int *val
)
3508 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
3510 *val
= codec
->driver
->read(codec
, reg
);
3515 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context
*dapm
,
3516 enum snd_soc_bias_level level
)
3518 struct snd_soc_codec
*codec
= snd_soc_dapm_to_codec(dapm
);
3520 return codec
->driver
->set_bias_level(codec
, level
);
3524 * snd_soc_register_codec - Register a codec with the ASoC core
3526 * @dev: The parent device for this codec
3527 * @codec_drv: Codec driver
3528 * @dai_drv: The associated DAI driver
3529 * @num_dai: Number of DAIs
3531 int snd_soc_register_codec(struct device
*dev
,
3532 const struct snd_soc_codec_driver
*codec_drv
,
3533 struct snd_soc_dai_driver
*dai_drv
,
3536 struct snd_soc_dapm_context
*dapm
;
3537 struct snd_soc_codec
*codec
;
3538 struct snd_soc_dai
*dai
;
3541 dev_dbg(dev
, "codec register %s\n", dev_name(dev
));
3543 codec
= kzalloc(sizeof(struct snd_soc_codec
), GFP_KERNEL
);
3547 codec
->component
.codec
= codec
;
3549 ret
= snd_soc_component_initialize(&codec
->component
,
3550 &codec_drv
->component_driver
, dev
);
3554 if (codec_drv
->probe
)
3555 codec
->component
.probe
= snd_soc_codec_drv_probe
;
3556 if (codec_drv
->remove
)
3557 codec
->component
.remove
= snd_soc_codec_drv_remove
;
3558 if (codec_drv
->suspend
)
3559 codec
->component
.suspend
= snd_soc_codec_drv_suspend
;
3560 if (codec_drv
->resume
)
3561 codec
->component
.resume
= snd_soc_codec_drv_resume
;
3562 if (codec_drv
->write
)
3563 codec
->component
.write
= snd_soc_codec_drv_write
;
3564 if (codec_drv
->read
)
3565 codec
->component
.read
= snd_soc_codec_drv_read
;
3566 codec
->component
.ignore_pmdown_time
= codec_drv
->ignore_pmdown_time
;
3568 dapm
= snd_soc_codec_get_dapm(codec
);
3569 dapm
->idle_bias_off
= codec_drv
->idle_bias_off
;
3570 dapm
->suspend_bias_off
= codec_drv
->suspend_bias_off
;
3571 if (codec_drv
->seq_notifier
)
3572 dapm
->seq_notifier
= codec_drv
->seq_notifier
;
3573 if (codec_drv
->set_bias_level
)
3574 dapm
->set_bias_level
= snd_soc_codec_set_bias_level
;
3576 codec
->driver
= codec_drv
;
3577 codec
->component
.val_bytes
= codec_drv
->reg_word_size
;
3579 #ifdef CONFIG_DEBUG_FS
3580 codec
->component
.init_debugfs
= soc_init_codec_debugfs
;
3581 codec
->component
.debugfs_prefix
= "codec";
3584 if (codec_drv
->get_regmap
)
3585 codec
->component
.regmap
= codec_drv
->get_regmap(dev
);
3587 for (i
= 0; i
< num_dai
; i
++) {
3588 fixup_codec_formats(&dai_drv
[i
].playback
);
3589 fixup_codec_formats(&dai_drv
[i
].capture
);
3592 ret
= snd_soc_register_dais(&codec
->component
, dai_drv
, num_dai
, false);
3594 dev_err(dev
, "ASoC: Failed to register DAIs: %d\n", ret
);
3598 list_for_each_entry(dai
, &codec
->component
.dai_list
, list
)
3601 mutex_lock(&client_mutex
);
3602 snd_soc_component_add_unlocked(&codec
->component
);
3603 list_add(&codec
->list
, &codec_list
);
3604 mutex_unlock(&client_mutex
);
3606 dev_dbg(codec
->dev
, "ASoC: Registered codec '%s'\n",
3607 codec
->component
.name
);
3611 snd_soc_component_cleanup(&codec
->component
);
3616 EXPORT_SYMBOL_GPL(snd_soc_register_codec
);
3619 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3621 * @dev: codec to unregister
3623 void snd_soc_unregister_codec(struct device
*dev
)
3625 struct snd_soc_codec
*codec
;
3627 mutex_lock(&client_mutex
);
3628 list_for_each_entry(codec
, &codec_list
, list
) {
3629 if (dev
== codec
->dev
)
3632 mutex_unlock(&client_mutex
);
3636 list_del(&codec
->list
);
3637 snd_soc_component_del_unlocked(&codec
->component
);
3638 mutex_unlock(&client_mutex
);
3640 dev_dbg(codec
->dev
, "ASoC: Unregistered codec '%s'\n",
3641 codec
->component
.name
);
3643 snd_soc_component_cleanup(&codec
->component
);
3644 snd_soc_cache_exit(codec
);
3647 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec
);
3649 /* Retrieve a card's name from device tree */
3650 int snd_soc_of_parse_card_name_from_node(struct snd_soc_card
*card
,
3651 struct device_node
*np
,
3652 const char *propname
)
3657 pr_err("card->dev is not set before calling %s\n", __func__
);
3662 np
= card
->dev
->of_node
;
3664 ret
= of_property_read_string_index(np
, propname
, 0, &card
->name
);
3666 * EINVAL means the property does not exist. This is fine providing
3667 * card->name was previously set, which is checked later in
3668 * snd_soc_register_card.
3670 if (ret
< 0 && ret
!= -EINVAL
) {
3672 "ASoC: Property '%s' could not be read: %d\n",
3679 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name_from_node
);
3681 static const struct snd_soc_dapm_widget simple_widgets
[] = {
3682 SND_SOC_DAPM_MIC("Microphone", NULL
),
3683 SND_SOC_DAPM_LINE("Line", NULL
),
3684 SND_SOC_DAPM_HP("Headphone", NULL
),
3685 SND_SOC_DAPM_SPK("Speaker", NULL
),
3688 int snd_soc_of_parse_audio_simple_widgets_from_node(struct snd_soc_card
*card
,
3689 struct device_node
*np
,
3690 const char *propname
)
3692 struct snd_soc_dapm_widget
*widgets
;
3693 const char *template, *wname
;
3694 int i
, j
, num_widgets
, ret
;
3697 np
= card
->dev
->of_node
;
3699 num_widgets
= of_property_count_strings(np
, propname
);
3700 if (num_widgets
< 0) {
3702 "ASoC: Property '%s' does not exist\n", propname
);
3705 if (num_widgets
& 1) {
3707 "ASoC: Property '%s' length is not even\n", propname
);
3713 dev_err(card
->dev
, "ASoC: Property '%s's length is zero\n",
3718 widgets
= devm_kcalloc(card
->dev
, num_widgets
, sizeof(*widgets
),
3722 "ASoC: Could not allocate memory for widgets\n");
3726 for (i
= 0; i
< num_widgets
; i
++) {
3727 ret
= of_property_read_string_index(np
, propname
,
3731 "ASoC: Property '%s' index %d read error:%d\n",
3732 propname
, 2 * i
, ret
);
3736 for (j
= 0; j
< ARRAY_SIZE(simple_widgets
); j
++) {
3737 if (!strncmp(template, simple_widgets
[j
].name
,
3738 strlen(simple_widgets
[j
].name
))) {
3739 widgets
[i
] = simple_widgets
[j
];
3744 if (j
>= ARRAY_SIZE(simple_widgets
)) {
3746 "ASoC: DAPM widget '%s' is not supported\n",
3751 ret
= of_property_read_string_index(np
, propname
,
3756 "ASoC: Property '%s' index %d read error:%d\n",
3757 propname
, (2 * i
) + 1, ret
);
3761 widgets
[i
].name
= wname
;
3764 card
->of_dapm_widgets
= widgets
;
3765 card
->num_of_dapm_widgets
= num_widgets
;
3769 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets_from_node
);
3771 static int snd_soc_of_get_slot_mask(struct device_node
*np
,
3772 const char *prop_name
,
3776 const __be32
*of_slot_mask
= of_get_property(np
, prop_name
, &val
);
3782 for (i
= 0; i
< val
; i
++)
3783 if (be32_to_cpup(&of_slot_mask
[i
]))
3789 int snd_soc_of_parse_tdm_slot(struct device_node
*np
,
3790 unsigned int *tx_mask
,
3791 unsigned int *rx_mask
,
3792 unsigned int *slots
,
3793 unsigned int *slot_width
)
3799 snd_soc_of_get_slot_mask(np
, "dai-tdm-slot-tx-mask", tx_mask
);
3801 snd_soc_of_get_slot_mask(np
, "dai-tdm-slot-rx-mask", rx_mask
);
3803 if (of_property_read_bool(np
, "dai-tdm-slot-num")) {
3804 ret
= of_property_read_u32(np
, "dai-tdm-slot-num", &val
);
3812 if (of_property_read_bool(np
, "dai-tdm-slot-width")) {
3813 ret
= of_property_read_u32(np
, "dai-tdm-slot-width", &val
);
3823 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot
);
3825 void snd_soc_of_parse_audio_prefix_from_node(struct snd_soc_card
*card
,
3826 struct device_node
*np
,
3827 struct snd_soc_codec_conf
*codec_conf
,
3828 struct device_node
*of_node
,
3829 const char *propname
)
3835 np
= card
->dev
->of_node
;
3837 ret
= of_property_read_string(np
, propname
, &str
);
3839 /* no prefix is not error */
3843 codec_conf
->of_node
= of_node
;
3844 codec_conf
->name_prefix
= str
;
3846 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix_from_node
);
3848 int snd_soc_of_parse_audio_routing_from_node(struct snd_soc_card
*card
,
3849 struct device_node
*np
,
3850 const char *propname
)
3853 struct snd_soc_dapm_route
*routes
;
3857 np
= card
->dev
->of_node
;
3859 num_routes
= of_property_count_strings(np
, propname
);
3860 if (num_routes
< 0 || num_routes
& 1) {
3862 "ASoC: Property '%s' does not exist or its length is not even\n",
3868 dev_err(card
->dev
, "ASoC: Property '%s's length is zero\n",
3873 routes
= devm_kzalloc(card
->dev
, num_routes
* sizeof(*routes
),
3877 "ASoC: Could not allocate DAPM route table\n");
3881 for (i
= 0; i
< num_routes
; i
++) {
3882 ret
= of_property_read_string_index(np
, propname
,
3883 2 * i
, &routes
[i
].sink
);
3886 "ASoC: Property '%s' index %d could not be read: %d\n",
3887 propname
, 2 * i
, ret
);
3890 ret
= of_property_read_string_index(np
, propname
,
3891 (2 * i
) + 1, &routes
[i
].source
);
3894 "ASoC: Property '%s' index %d could not be read: %d\n",
3895 propname
, (2 * i
) + 1, ret
);
3900 card
->num_of_dapm_routes
= num_routes
;
3901 card
->of_dapm_routes
= routes
;
3905 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing_from_node
);
3907 unsigned int snd_soc_of_parse_daifmt(struct device_node
*np
,
3909 struct device_node
**bitclkmaster
,
3910 struct device_node
**framemaster
)
3914 unsigned int format
= 0;
3920 } of_fmt_table
[] = {
3921 { "i2s", SND_SOC_DAIFMT_I2S
},
3922 { "right_j", SND_SOC_DAIFMT_RIGHT_J
},
3923 { "left_j", SND_SOC_DAIFMT_LEFT_J
},
3924 { "dsp_a", SND_SOC_DAIFMT_DSP_A
},
3925 { "dsp_b", SND_SOC_DAIFMT_DSP_B
},
3926 { "ac97", SND_SOC_DAIFMT_AC97
},
3927 { "pdm", SND_SOC_DAIFMT_PDM
},
3928 { "msb", SND_SOC_DAIFMT_MSB
},
3929 { "lsb", SND_SOC_DAIFMT_LSB
},
3936 * check "[prefix]format = xxx"
3937 * SND_SOC_DAIFMT_FORMAT_MASK area
3939 snprintf(prop
, sizeof(prop
), "%sformat", prefix
);
3940 ret
= of_property_read_string(np
, prop
, &str
);
3942 for (i
= 0; i
< ARRAY_SIZE(of_fmt_table
); i
++) {
3943 if (strcmp(str
, of_fmt_table
[i
].name
) == 0) {
3944 format
|= of_fmt_table
[i
].val
;
3951 * check "[prefix]continuous-clock"
3952 * SND_SOC_DAIFMT_CLOCK_MASK area
3954 snprintf(prop
, sizeof(prop
), "%scontinuous-clock", prefix
);
3955 if (of_property_read_bool(np
, prop
))
3956 format
|= SND_SOC_DAIFMT_CONT
;
3958 format
|= SND_SOC_DAIFMT_GATED
;
3961 * check "[prefix]bitclock-inversion"
3962 * check "[prefix]frame-inversion"
3963 * SND_SOC_DAIFMT_INV_MASK area
3965 snprintf(prop
, sizeof(prop
), "%sbitclock-inversion", prefix
);
3966 bit
= !!of_get_property(np
, prop
, NULL
);
3968 snprintf(prop
, sizeof(prop
), "%sframe-inversion", prefix
);
3969 frame
= !!of_get_property(np
, prop
, NULL
);
3971 switch ((bit
<< 4) + frame
) {
3973 format
|= SND_SOC_DAIFMT_IB_IF
;
3976 format
|= SND_SOC_DAIFMT_IB_NF
;
3979 format
|= SND_SOC_DAIFMT_NB_IF
;
3982 /* SND_SOC_DAIFMT_NB_NF is default */
3987 * check "[prefix]bitclock-master"
3988 * check "[prefix]frame-master"
3989 * SND_SOC_DAIFMT_MASTER_MASK area
3991 snprintf(prop
, sizeof(prop
), "%sbitclock-master", prefix
);
3992 bit
= !!of_get_property(np
, prop
, NULL
);
3993 if (bit
&& bitclkmaster
)
3994 *bitclkmaster
= of_parse_phandle(np
, prop
, 0);
3996 snprintf(prop
, sizeof(prop
), "%sframe-master", prefix
);
3997 frame
= !!of_get_property(np
, prop
, NULL
);
3998 if (frame
&& framemaster
)
3999 *framemaster
= of_parse_phandle(np
, prop
, 0);
4001 switch ((bit
<< 4) + frame
) {
4003 format
|= SND_SOC_DAIFMT_CBM_CFM
;
4006 format
|= SND_SOC_DAIFMT_CBM_CFS
;
4009 format
|= SND_SOC_DAIFMT_CBS_CFM
;
4012 format
|= SND_SOC_DAIFMT_CBS_CFS
;
4018 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt
);
4020 int snd_soc_get_dai_name(struct of_phandle_args
*args
,
4021 const char **dai_name
)
4023 struct snd_soc_component
*pos
;
4024 struct device_node
*component_of_node
;
4025 int ret
= -EPROBE_DEFER
;
4027 mutex_lock(&client_mutex
);
4028 list_for_each_entry(pos
, &component_list
, list
) {
4029 component_of_node
= pos
->dev
->of_node
;
4030 if (!component_of_node
&& pos
->dev
->parent
)
4031 component_of_node
= pos
->dev
->parent
->of_node
;
4033 if (component_of_node
!= args
->np
)
4036 if (pos
->driver
->of_xlate_dai_name
) {
4037 ret
= pos
->driver
->of_xlate_dai_name(pos
,
4043 switch (args
->args_count
) {
4045 id
= 0; /* same as dai_drv[0] */
4055 if (id
< 0 || id
>= pos
->num_dai
) {
4062 *dai_name
= pos
->dai_drv
[id
].name
;
4064 *dai_name
= pos
->name
;
4069 mutex_unlock(&client_mutex
);
4072 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name
);
4074 int snd_soc_of_get_dai_name(struct device_node
*of_node
,
4075 const char **dai_name
)
4077 struct of_phandle_args args
;
4080 ret
= of_parse_phandle_with_args(of_node
, "sound-dai",
4081 "#sound-dai-cells", 0, &args
);
4085 ret
= snd_soc_get_dai_name(&args
, dai_name
);
4087 of_node_put(args
.np
);
4091 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name
);
4094 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
4096 * @of_node: Device node
4097 * @dai_link: DAI link
4099 * Builds an array of CODEC DAI components from the DAI link property
4101 * The array is set in the DAI link and the number of DAIs is set accordingly.
4102 * The device nodes in the array (of_node) must be dereferenced by the caller.
4104 * Returns 0 for success
4106 int snd_soc_of_get_dai_link_codecs(struct device
*dev
,
4107 struct device_node
*of_node
,
4108 struct snd_soc_dai_link
*dai_link
)
4110 struct of_phandle_args args
;
4111 struct snd_soc_dai_link_component
*component
;
4113 int index
, num_codecs
, ret
;
4115 /* Count the number of CODECs */
4117 num_codecs
= of_count_phandle_with_args(of_node
, name
,
4118 "#sound-dai-cells");
4119 if (num_codecs
<= 0) {
4120 if (num_codecs
== -ENOENT
)
4121 dev_err(dev
, "No 'sound-dai' property\n");
4123 dev_err(dev
, "Bad phandle in 'sound-dai'\n");
4126 component
= devm_kzalloc(dev
,
4127 sizeof *component
* num_codecs
,
4131 dai_link
->codecs
= component
;
4132 dai_link
->num_codecs
= num_codecs
;
4134 /* Parse the list */
4135 for (index
= 0, component
= dai_link
->codecs
;
4136 index
< dai_link
->num_codecs
;
4137 index
++, component
++) {
4138 ret
= of_parse_phandle_with_args(of_node
, name
,
4143 component
->of_node
= args
.np
;
4144 ret
= snd_soc_get_dai_name(&args
, &component
->dai_name
);
4150 for (index
= 0, component
= dai_link
->codecs
;
4151 index
< dai_link
->num_codecs
;
4152 index
++, component
++) {
4153 if (!component
->of_node
)
4155 of_node_put(component
->of_node
);
4156 component
->of_node
= NULL
;
4158 dai_link
->codecs
= NULL
;
4159 dai_link
->num_codecs
= 0;
4162 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs
);
4164 static int __init
snd_soc_init(void)
4166 snd_soc_debugfs_init();
4167 snd_soc_util_init();
4169 return platform_driver_register(&soc_driver
);
4171 module_init(snd_soc_init
);
4173 static void __exit
snd_soc_exit(void)
4175 snd_soc_util_exit();
4176 snd_soc_debugfs_exit();
4178 #ifdef CONFIG_DEBUG_FS
4180 platform_driver_unregister(&soc_driver
);
4182 module_exit(snd_soc_exit
);
4184 /* Module information */
4185 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4186 MODULE_DESCRIPTION("ALSA SoC Core");
4187 MODULE_LICENSE("GPL");
4188 MODULE_ALIAS("platform:soc-audio");