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 <sound/core.h>
38 #include <sound/jack.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
42 #include <sound/soc-dpcm.h>
43 #include <sound/soc-topology.h>
44 #include <sound/initval.h>
46 #define CREATE_TRACE_POINTS
47 #include <trace/events/asoc.h>
51 #ifdef CONFIG_DEBUG_FS
52 struct dentry
*snd_soc_debugfs_root
;
53 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root
);
56 static DEFINE_MUTEX(client_mutex
);
57 static LIST_HEAD(platform_list
);
58 static LIST_HEAD(codec_list
);
59 static LIST_HEAD(component_list
);
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
66 static int pmdown_time
= 5000;
67 module_param(pmdown_time
, int, 0);
68 MODULE_PARM_DESC(pmdown_time
, "DAPM stream powerdown time (msecs)");
70 /* returns the minimum number of bytes needed to represent
71 * a particular given value */
72 static int min_bytes_needed(unsigned long val
)
77 for (i
= (sizeof val
* 8) - 1; i
>= 0; --i
, ++c
)
80 c
= (sizeof val
* 8) - c
;
88 /* fill buf which is 'len' bytes with a formatted
89 * string of the form 'reg: value\n' */
90 static int format_register_str(struct snd_soc_codec
*codec
,
91 unsigned int reg
, char *buf
, size_t len
)
93 int wordsize
= min_bytes_needed(codec
->driver
->reg_cache_size
) * 2;
94 int regsize
= codec
->driver
->reg_word_size
* 2;
97 /* +2 for ': ' and + 1 for '\n' */
98 if (wordsize
+ regsize
+ 2 + 1 != len
)
101 sprintf(buf
, "%.*x: ", wordsize
, reg
);
104 ret
= snd_soc_read(codec
, reg
);
106 memset(buf
, 'X', regsize
);
108 sprintf(buf
, "%.*x", regsize
, ret
);
110 /* no NUL-termination needed */
114 /* codec register dump */
115 static ssize_t
soc_codec_reg_show(struct snd_soc_codec
*codec
, char *buf
,
116 size_t count
, loff_t pos
)
119 int wordsize
, regsize
;
124 wordsize
= min_bytes_needed(codec
->driver
->reg_cache_size
) * 2;
125 regsize
= codec
->driver
->reg_word_size
* 2;
127 len
= wordsize
+ regsize
+ 2 + 1;
129 if (!codec
->driver
->reg_cache_size
)
132 if (codec
->driver
->reg_cache_step
)
133 step
= codec
->driver
->reg_cache_step
;
135 for (i
= 0; i
< codec
->driver
->reg_cache_size
; i
+= step
) {
136 /* only support larger than PAGE_SIZE bytes debugfs
137 * entries for the default case */
139 if (total
+ len
>= count
- 1)
141 format_register_str(codec
, i
, buf
+ total
, len
);
147 total
= min(total
, count
- 1);
152 static ssize_t
codec_reg_show(struct device
*dev
,
153 struct device_attribute
*attr
, char *buf
)
155 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
157 return soc_codec_reg_show(rtd
->codec
, buf
, PAGE_SIZE
, 0);
160 static DEVICE_ATTR(codec_reg
, 0444, codec_reg_show
, NULL
);
162 static ssize_t
pmdown_time_show(struct device
*dev
,
163 struct device_attribute
*attr
, char *buf
)
165 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
167 return sprintf(buf
, "%ld\n", rtd
->pmdown_time
);
170 static ssize_t
pmdown_time_set(struct device
*dev
,
171 struct device_attribute
*attr
,
172 const char *buf
, size_t count
)
174 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
177 ret
= kstrtol(buf
, 10, &rtd
->pmdown_time
);
184 static DEVICE_ATTR(pmdown_time
, 0644, pmdown_time_show
, pmdown_time_set
);
186 static struct attribute
*soc_dev_attrs
[] = {
187 &dev_attr_codec_reg
.attr
,
188 &dev_attr_pmdown_time
.attr
,
192 static umode_t
soc_dev_attr_is_visible(struct kobject
*kobj
,
193 struct attribute
*attr
, int idx
)
195 struct device
*dev
= kobj_to_dev(kobj
);
196 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
198 if (attr
== &dev_attr_pmdown_time
.attr
)
199 return attr
->mode
; /* always visible */
200 return rtd
->codec
? attr
->mode
: 0; /* enabled only with codec */
203 static const struct attribute_group soc_dapm_dev_group
= {
204 .attrs
= soc_dapm_dev_attrs
,
205 .is_visible
= soc_dev_attr_is_visible
,
208 static const struct attribute_group soc_dev_roup
= {
209 .attrs
= soc_dev_attrs
,
210 .is_visible
= soc_dev_attr_is_visible
,
213 static const struct attribute_group
*soc_dev_attr_groups
[] = {
219 #ifdef CONFIG_DEBUG_FS
220 static ssize_t
codec_reg_read_file(struct file
*file
, char __user
*user_buf
,
221 size_t count
, loff_t
*ppos
)
224 struct snd_soc_codec
*codec
= file
->private_data
;
227 if (*ppos
< 0 || !count
)
230 buf
= kmalloc(count
, GFP_KERNEL
);
234 ret
= soc_codec_reg_show(codec
, buf
, count
, *ppos
);
236 if (copy_to_user(user_buf
, buf
, ret
)) {
247 static ssize_t
codec_reg_write_file(struct file
*file
,
248 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
253 unsigned long reg
, value
;
254 struct snd_soc_codec
*codec
= file
->private_data
;
257 buf_size
= min(count
, (sizeof(buf
)-1));
258 if (copy_from_user(buf
, user_buf
, buf_size
))
262 while (*start
== ' ')
264 reg
= simple_strtoul(start
, &start
, 16);
265 while (*start
== ' ')
267 ret
= kstrtoul(start
, 16, &value
);
271 /* Userspace has been fiddling around behind the kernel's back */
272 add_taint(TAINT_USER
, LOCKDEP_NOW_UNRELIABLE
);
274 snd_soc_write(codec
, reg
, value
);
278 static const struct file_operations codec_reg_fops
= {
280 .read
= codec_reg_read_file
,
281 .write
= codec_reg_write_file
,
282 .llseek
= default_llseek
,
285 static void soc_init_component_debugfs(struct snd_soc_component
*component
)
287 if (!component
->card
->debugfs_card_root
)
290 if (component
->debugfs_prefix
) {
293 name
= kasprintf(GFP_KERNEL
, "%s:%s",
294 component
->debugfs_prefix
, component
->name
);
296 component
->debugfs_root
= debugfs_create_dir(name
,
297 component
->card
->debugfs_card_root
);
301 component
->debugfs_root
= debugfs_create_dir(component
->name
,
302 component
->card
->debugfs_card_root
);
305 if (!component
->debugfs_root
) {
306 dev_warn(component
->dev
,
307 "ASoC: Failed to create component debugfs directory\n");
311 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component
),
312 component
->debugfs_root
);
314 if (component
->init_debugfs
)
315 component
->init_debugfs(component
);
318 static void soc_cleanup_component_debugfs(struct snd_soc_component
*component
)
320 debugfs_remove_recursive(component
->debugfs_root
);
323 static void soc_init_codec_debugfs(struct snd_soc_component
*component
)
325 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
327 codec
->debugfs_reg
= debugfs_create_file("codec_reg", 0644,
328 codec
->component
.debugfs_root
,
329 codec
, &codec_reg_fops
);
330 if (!codec
->debugfs_reg
)
332 "ASoC: Failed to create codec register debugfs file\n");
335 static ssize_t
codec_list_read_file(struct file
*file
, char __user
*user_buf
,
336 size_t count
, loff_t
*ppos
)
338 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
339 ssize_t len
, ret
= 0;
340 struct snd_soc_codec
*codec
;
345 mutex_lock(&client_mutex
);
347 list_for_each_entry(codec
, &codec_list
, list
) {
348 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
349 codec
->component
.name
);
352 if (ret
> PAGE_SIZE
) {
358 mutex_unlock(&client_mutex
);
361 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
368 static const struct file_operations codec_list_fops
= {
369 .read
= codec_list_read_file
,
370 .llseek
= default_llseek
,/* read accesses f_pos */
373 static ssize_t
dai_list_read_file(struct file
*file
, char __user
*user_buf
,
374 size_t count
, loff_t
*ppos
)
376 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
377 ssize_t len
, ret
= 0;
378 struct snd_soc_component
*component
;
379 struct snd_soc_dai
*dai
;
384 mutex_lock(&client_mutex
);
386 list_for_each_entry(component
, &component_list
, list
) {
387 list_for_each_entry(dai
, &component
->dai_list
, list
) {
388 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
392 if (ret
> PAGE_SIZE
) {
399 mutex_unlock(&client_mutex
);
401 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
408 static const struct file_operations dai_list_fops
= {
409 .read
= dai_list_read_file
,
410 .llseek
= default_llseek
,/* read accesses f_pos */
413 static ssize_t
platform_list_read_file(struct file
*file
,
414 char __user
*user_buf
,
415 size_t count
, loff_t
*ppos
)
417 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
418 ssize_t len
, ret
= 0;
419 struct snd_soc_platform
*platform
;
424 mutex_lock(&client_mutex
);
426 list_for_each_entry(platform
, &platform_list
, list
) {
427 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
428 platform
->component
.name
);
431 if (ret
> PAGE_SIZE
) {
437 mutex_unlock(&client_mutex
);
439 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
446 static const struct file_operations platform_list_fops
= {
447 .read
= platform_list_read_file
,
448 .llseek
= default_llseek
,/* read accesses f_pos */
451 static void soc_init_card_debugfs(struct snd_soc_card
*card
)
453 if (!snd_soc_debugfs_root
)
456 card
->debugfs_card_root
= debugfs_create_dir(card
->name
,
457 snd_soc_debugfs_root
);
458 if (!card
->debugfs_card_root
) {
460 "ASoC: Failed to create card debugfs directory\n");
464 card
->debugfs_pop_time
= debugfs_create_u32("dapm_pop_time", 0644,
465 card
->debugfs_card_root
,
467 if (!card
->debugfs_pop_time
)
469 "ASoC: Failed to create pop time debugfs file\n");
472 static void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
474 debugfs_remove_recursive(card
->debugfs_card_root
);
478 static void snd_soc_debugfs_init(void)
480 snd_soc_debugfs_root
= debugfs_create_dir("asoc", NULL
);
481 if (IS_ERR(snd_soc_debugfs_root
) || !snd_soc_debugfs_root
) {
482 pr_warn("ASoC: Failed to create debugfs directory\n");
483 snd_soc_debugfs_root
= NULL
;
487 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root
, NULL
,
489 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
491 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root
, NULL
,
493 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
495 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root
, NULL
,
496 &platform_list_fops
))
497 pr_warn("ASoC: Failed to create platform list debugfs file\n");
500 static void snd_soc_debugfs_exit(void)
502 debugfs_remove_recursive(snd_soc_debugfs_root
);
507 #define soc_init_codec_debugfs NULL
509 static inline void soc_init_component_debugfs(
510 struct snd_soc_component
*component
)
514 static inline void soc_cleanup_component_debugfs(
515 struct snd_soc_component
*component
)
519 static inline void soc_init_card_debugfs(struct snd_soc_card
*card
)
523 static inline void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
527 static inline void snd_soc_debugfs_init(void)
531 static inline void snd_soc_debugfs_exit(void)
537 struct snd_pcm_substream
*snd_soc_get_dai_substream(struct snd_soc_card
*card
,
538 const char *dai_link
, int stream
)
540 struct snd_soc_pcm_runtime
*rtd
;
542 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
543 if (rtd
->dai_link
->no_pcm
&&
544 !strcmp(rtd
->dai_link
->name
, dai_link
))
545 return rtd
->pcm
->streams
[stream
].substream
;
547 dev_dbg(card
->dev
, "ASoC: failed to find dai link %s\n", dai_link
);
550 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream
);
552 static struct snd_soc_pcm_runtime
*soc_new_pcm_runtime(
553 struct snd_soc_card
*card
, struct snd_soc_dai_link
*dai_link
)
555 struct snd_soc_pcm_runtime
*rtd
;
557 rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
), GFP_KERNEL
);
562 rtd
->dai_link
= dai_link
;
563 rtd
->codec_dais
= kzalloc(sizeof(struct snd_soc_dai
*) *
564 dai_link
->num_codecs
,
566 if (!rtd
->codec_dais
) {
574 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime
*rtd
)
576 if (rtd
&& rtd
->codec_dais
)
577 kfree(rtd
->codec_dais
);
581 static void soc_add_pcm_runtime(struct snd_soc_card
*card
,
582 struct snd_soc_pcm_runtime
*rtd
)
584 list_add_tail(&rtd
->list
, &card
->rtd_list
);
585 rtd
->num
= card
->num_rtd
;
589 static void soc_remove_pcm_runtimes(struct snd_soc_card
*card
)
591 struct snd_soc_pcm_runtime
*rtd
, *_rtd
;
593 list_for_each_entry_safe(rtd
, _rtd
, &card
->rtd_list
, list
) {
594 list_del(&rtd
->list
);
595 soc_free_pcm_runtime(rtd
);
601 struct snd_soc_pcm_runtime
*snd_soc_get_pcm_runtime(struct snd_soc_card
*card
,
602 const char *dai_link
)
604 struct snd_soc_pcm_runtime
*rtd
;
606 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
607 if (!strcmp(rtd
->dai_link
->name
, dai_link
))
610 dev_dbg(card
->dev
, "ASoC: failed to find rtd %s\n", dai_link
);
613 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime
);
615 static void codec2codec_close_delayed_work(struct work_struct
*work
)
617 /* Currently nothing to do for c2c links
618 * Since c2c links are internal nodes in the DAPM graph and
619 * don't interface with the outside world or application layer
620 * we don't have to do any special handling on close.
624 #ifdef CONFIG_PM_SLEEP
625 /* powers down audio subsystem for suspend */
626 int snd_soc_suspend(struct device
*dev
)
628 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
629 struct snd_soc_codec
*codec
;
630 struct snd_soc_pcm_runtime
*rtd
;
633 /* If the card is not initialized yet there is nothing to do */
634 if (!card
->instantiated
)
637 /* Due to the resume being scheduled into a workqueue we could
638 * suspend before that's finished - wait for it to complete.
640 snd_power_lock(card
->snd_card
);
641 snd_power_wait(card
->snd_card
, SNDRV_CTL_POWER_D0
);
642 snd_power_unlock(card
->snd_card
);
644 /* we're going to block userspace touching us until resume completes */
645 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D3hot
);
647 /* mute any active DACs */
648 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
650 if (rtd
->dai_link
->ignore_suspend
)
653 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
654 struct snd_soc_dai
*dai
= rtd
->codec_dais
[i
];
655 struct snd_soc_dai_driver
*drv
= dai
->driver
;
657 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
658 drv
->ops
->digital_mute(dai
, 1);
662 /* suspend all pcms */
663 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
664 if (rtd
->dai_link
->ignore_suspend
)
667 snd_pcm_suspend_all(rtd
->pcm
);
670 if (card
->suspend_pre
)
671 card
->suspend_pre(card
);
673 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
674 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
676 if (rtd
->dai_link
->ignore_suspend
)
679 if (cpu_dai
->driver
->suspend
&& !cpu_dai
->driver
->bus_control
)
680 cpu_dai
->driver
->suspend(cpu_dai
);
683 /* close any waiting streams */
684 list_for_each_entry(rtd
, &card
->rtd_list
, list
)
685 flush_delayed_work(&rtd
->delayed_work
);
687 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
689 if (rtd
->dai_link
->ignore_suspend
)
692 snd_soc_dapm_stream_event(rtd
,
693 SNDRV_PCM_STREAM_PLAYBACK
,
694 SND_SOC_DAPM_STREAM_SUSPEND
);
696 snd_soc_dapm_stream_event(rtd
,
697 SNDRV_PCM_STREAM_CAPTURE
,
698 SND_SOC_DAPM_STREAM_SUSPEND
);
701 /* Recheck all endpoints too, their state is affected by suspend */
702 dapm_mark_endpoints_dirty(card
);
703 snd_soc_dapm_sync(&card
->dapm
);
705 /* suspend all CODECs */
706 list_for_each_entry(codec
, &card
->codec_dev_list
, card_list
) {
707 struct snd_soc_dapm_context
*dapm
= snd_soc_codec_get_dapm(codec
);
709 /* If there are paths active then the CODEC will be held with
710 * bias _ON and should not be suspended. */
711 if (!codec
->suspended
) {
712 switch (snd_soc_dapm_get_bias_level(dapm
)) {
713 case SND_SOC_BIAS_STANDBY
:
715 * If the CODEC is capable of idle
716 * bias off then being in STANDBY
717 * means it's doing something,
718 * otherwise fall through.
720 if (dapm
->idle_bias_off
) {
722 "ASoC: idle_bias_off CODEC on over suspend\n");
726 case SND_SOC_BIAS_OFF
:
727 if (codec
->driver
->suspend
)
728 codec
->driver
->suspend(codec
);
729 codec
->suspended
= 1;
730 if (codec
->component
.regmap
)
731 regcache_mark_dirty(codec
->component
.regmap
);
732 /* deactivate pins to sleep state */
733 pinctrl_pm_select_sleep_state(codec
->dev
);
737 "ASoC: CODEC is on over suspend\n");
743 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
744 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
746 if (rtd
->dai_link
->ignore_suspend
)
749 if (cpu_dai
->driver
->suspend
&& cpu_dai
->driver
->bus_control
)
750 cpu_dai
->driver
->suspend(cpu_dai
);
752 /* deactivate pins to sleep state */
753 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
756 if (card
->suspend_post
)
757 card
->suspend_post(card
);
761 EXPORT_SYMBOL_GPL(snd_soc_suspend
);
763 /* deferred resume work, so resume can complete before we finished
764 * setting our codec back up, which can be very slow on I2C
766 static void soc_resume_deferred(struct work_struct
*work
)
768 struct snd_soc_card
*card
=
769 container_of(work
, struct snd_soc_card
, deferred_resume_work
);
770 struct snd_soc_pcm_runtime
*rtd
;
771 struct snd_soc_codec
*codec
;
774 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
775 * so userspace apps are blocked from touching us
778 dev_dbg(card
->dev
, "ASoC: starting resume work\n");
780 /* Bring us up into D2 so that DAPM starts enabling things */
781 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D2
);
783 if (card
->resume_pre
)
784 card
->resume_pre(card
);
786 /* resume control bus DAIs */
787 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
788 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
790 if (rtd
->dai_link
->ignore_suspend
)
793 if (cpu_dai
->driver
->resume
&& cpu_dai
->driver
->bus_control
)
794 cpu_dai
->driver
->resume(cpu_dai
);
797 list_for_each_entry(codec
, &card
->codec_dev_list
, card_list
) {
798 if (codec
->suspended
) {
799 if (codec
->driver
->resume
)
800 codec
->driver
->resume(codec
);
801 codec
->suspended
= 0;
805 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
807 if (rtd
->dai_link
->ignore_suspend
)
810 snd_soc_dapm_stream_event(rtd
,
811 SNDRV_PCM_STREAM_PLAYBACK
,
812 SND_SOC_DAPM_STREAM_RESUME
);
814 snd_soc_dapm_stream_event(rtd
,
815 SNDRV_PCM_STREAM_CAPTURE
,
816 SND_SOC_DAPM_STREAM_RESUME
);
819 /* unmute any active DACs */
820 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
822 if (rtd
->dai_link
->ignore_suspend
)
825 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
826 struct snd_soc_dai
*dai
= rtd
->codec_dais
[i
];
827 struct snd_soc_dai_driver
*drv
= dai
->driver
;
829 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
830 drv
->ops
->digital_mute(dai
, 0);
834 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
835 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
837 if (rtd
->dai_link
->ignore_suspend
)
840 if (cpu_dai
->driver
->resume
&& !cpu_dai
->driver
->bus_control
)
841 cpu_dai
->driver
->resume(cpu_dai
);
844 if (card
->resume_post
)
845 card
->resume_post(card
);
847 dev_dbg(card
->dev
, "ASoC: resume work completed\n");
849 /* Recheck all endpoints too, their state is affected by suspend */
850 dapm_mark_endpoints_dirty(card
);
851 snd_soc_dapm_sync(&card
->dapm
);
853 /* userspace can access us now we are back as we were before */
854 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D0
);
857 /* powers up audio subsystem after a suspend */
858 int snd_soc_resume(struct device
*dev
)
860 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
861 bool bus_control
= false;
862 struct snd_soc_pcm_runtime
*rtd
;
864 /* If the card is not initialized yet there is nothing to do */
865 if (!card
->instantiated
)
868 /* activate pins from sleep state */
869 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
870 struct snd_soc_dai
**codec_dais
= rtd
->codec_dais
;
871 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
875 pinctrl_pm_select_default_state(cpu_dai
->dev
);
877 for (j
= 0; j
< rtd
->num_codecs
; j
++) {
878 struct snd_soc_dai
*codec_dai
= codec_dais
[j
];
879 if (codec_dai
->active
)
880 pinctrl_pm_select_default_state(codec_dai
->dev
);
885 * DAIs that also act as the control bus master might have other drivers
886 * hanging off them so need to resume immediately. Other drivers don't
887 * have that problem and may take a substantial amount of time to resume
888 * due to I/O costs and anti-pop so handle them out of line.
890 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
891 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
892 bus_control
|= cpu_dai
->driver
->bus_control
;
895 dev_dbg(dev
, "ASoC: Resuming control bus master immediately\n");
896 soc_resume_deferred(&card
->deferred_resume_work
);
898 dev_dbg(dev
, "ASoC: Scheduling resume work\n");
899 if (!schedule_work(&card
->deferred_resume_work
))
900 dev_err(dev
, "ASoC: resume work item may be lost\n");
905 EXPORT_SYMBOL_GPL(snd_soc_resume
);
907 #define snd_soc_suspend NULL
908 #define snd_soc_resume NULL
911 static const struct snd_soc_dai_ops null_dai_ops
= {
914 static struct snd_soc_component
*soc_find_component(
915 const struct device_node
*of_node
, const char *name
)
917 struct snd_soc_component
*component
;
919 lockdep_assert_held(&client_mutex
);
921 list_for_each_entry(component
, &component_list
, list
) {
923 if (component
->dev
->of_node
== of_node
)
925 } else if (strcmp(component
->name
, name
) == 0) {
933 static struct snd_soc_dai
*snd_soc_find_dai(
934 const struct snd_soc_dai_link_component
*dlc
)
936 struct snd_soc_component
*component
;
937 struct snd_soc_dai
*dai
;
938 struct device_node
*component_of_node
;
940 lockdep_assert_held(&client_mutex
);
942 /* Find CPU DAI from registered DAIs*/
943 list_for_each_entry(component
, &component_list
, list
) {
944 component_of_node
= component
->dev
->of_node
;
945 if (!component_of_node
&& component
->dev
->parent
)
946 component_of_node
= component
->dev
->parent
->of_node
;
948 if (dlc
->of_node
&& component_of_node
!= dlc
->of_node
)
950 if (dlc
->name
&& strcmp(component
->name
, dlc
->name
))
952 list_for_each_entry(dai
, &component
->dai_list
, list
) {
953 if (dlc
->dai_name
&& strcmp(dai
->name
, dlc
->dai_name
))
963 static int soc_bind_dai_link(struct snd_soc_card
*card
, int num
)
965 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
966 struct snd_soc_pcm_runtime
*rtd
;
967 struct snd_soc_dai_link_component
*codecs
= dai_link
->codecs
;
968 struct snd_soc_dai_link_component cpu_dai_component
;
969 struct snd_soc_dai
**codec_dais
;
970 struct snd_soc_platform
*platform
;
971 const char *platform_name
;
974 dev_dbg(card
->dev
, "ASoC: binding %s at idx %d\n", dai_link
->name
, num
);
976 rtd
= soc_new_pcm_runtime(card
, dai_link
);
980 cpu_dai_component
.name
= dai_link
->cpu_name
;
981 cpu_dai_component
.of_node
= dai_link
->cpu_of_node
;
982 cpu_dai_component
.dai_name
= dai_link
->cpu_dai_name
;
983 rtd
->cpu_dai
= snd_soc_find_dai(&cpu_dai_component
);
985 dev_err(card
->dev
, "ASoC: CPU DAI %s not registered\n",
986 dai_link
->cpu_dai_name
);
990 rtd
->num_codecs
= dai_link
->num_codecs
;
992 /* Find CODEC from registered CODECs */
993 codec_dais
= rtd
->codec_dais
;
994 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
995 codec_dais
[i
] = snd_soc_find_dai(&codecs
[i
]);
996 if (!codec_dais
[i
]) {
997 dev_err(card
->dev
, "ASoC: CODEC DAI %s not registered\n",
1003 /* Single codec links expect codec and codec_dai in runtime data */
1004 rtd
->codec_dai
= codec_dais
[0];
1005 rtd
->codec
= rtd
->codec_dai
->codec
;
1007 /* if there's no platform we match on the empty platform */
1008 platform_name
= dai_link
->platform_name
;
1009 if (!platform_name
&& !dai_link
->platform_of_node
)
1010 platform_name
= "snd-soc-dummy";
1012 /* find one from the set of registered platforms */
1013 list_for_each_entry(platform
, &platform_list
, list
) {
1014 if (dai_link
->platform_of_node
) {
1015 if (platform
->dev
->of_node
!=
1016 dai_link
->platform_of_node
)
1019 if (strcmp(platform
->component
.name
, platform_name
))
1023 rtd
->platform
= platform
;
1025 if (!rtd
->platform
) {
1026 dev_err(card
->dev
, "ASoC: platform %s not registered\n",
1027 dai_link
->platform_name
);
1028 return -EPROBE_DEFER
;
1031 soc_add_pcm_runtime(card
, rtd
);
1035 soc_free_pcm_runtime(rtd
);
1036 return -EPROBE_DEFER
;
1039 static void soc_remove_component(struct snd_soc_component
*component
)
1041 if (!component
->card
)
1044 /* This is a HACK and will be removed soon */
1045 if (component
->codec
)
1046 list_del(&component
->codec
->card_list
);
1048 if (component
->remove
)
1049 component
->remove(component
);
1051 snd_soc_dapm_free(snd_soc_component_get_dapm(component
));
1053 soc_cleanup_component_debugfs(component
);
1054 component
->card
= NULL
;
1055 module_put(component
->dev
->driver
->owner
);
1058 static void soc_remove_dai(struct snd_soc_dai
*dai
, int order
)
1062 if (dai
&& dai
->probed
&&
1063 dai
->driver
->remove_order
== order
) {
1064 if (dai
->driver
->remove
) {
1065 err
= dai
->driver
->remove(dai
);
1068 "ASoC: failed to remove %s: %d\n",
1075 static void soc_remove_link_dais(struct snd_soc_card
*card
,
1076 struct snd_soc_pcm_runtime
*rtd
, int order
)
1080 /* unregister the rtd device */
1081 if (rtd
->dev_registered
) {
1082 device_unregister(rtd
->dev
);
1083 rtd
->dev_registered
= 0;
1086 /* remove the CODEC DAI */
1087 for (i
= 0; i
< rtd
->num_codecs
; i
++)
1088 soc_remove_dai(rtd
->codec_dais
[i
], order
);
1090 soc_remove_dai(rtd
->cpu_dai
, order
);
1093 static void soc_remove_link_components(struct snd_soc_card
*card
,
1094 struct snd_soc_pcm_runtime
*rtd
, int order
)
1096 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1097 struct snd_soc_platform
*platform
= rtd
->platform
;
1098 struct snd_soc_component
*component
;
1101 /* remove the platform */
1102 if (platform
&& platform
->component
.driver
->remove_order
== order
)
1103 soc_remove_component(&platform
->component
);
1105 /* remove the CODEC-side CODEC */
1106 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1107 component
= rtd
->codec_dais
[i
]->component
;
1108 if (component
->driver
->remove_order
== order
)
1109 soc_remove_component(component
);
1112 /* remove any CPU-side CODEC */
1114 if (cpu_dai
->component
->driver
->remove_order
== order
)
1115 soc_remove_component(cpu_dai
->component
);
1119 static void soc_remove_dai_links(struct snd_soc_card
*card
)
1122 struct snd_soc_pcm_runtime
*rtd
;
1124 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1126 list_for_each_entry(rtd
, &card
->rtd_list
, list
)
1127 soc_remove_link_dais(card
, rtd
, order
);
1130 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1132 list_for_each_entry(rtd
, &card
->rtd_list
, list
)
1133 soc_remove_link_components(card
, rtd
, order
);
1137 static void soc_set_name_prefix(struct snd_soc_card
*card
,
1138 struct snd_soc_component
*component
)
1142 if (card
->codec_conf
== NULL
)
1145 for (i
= 0; i
< card
->num_configs
; i
++) {
1146 struct snd_soc_codec_conf
*map
= &card
->codec_conf
[i
];
1147 if (map
->of_node
&& component
->dev
->of_node
!= map
->of_node
)
1149 if (map
->dev_name
&& strcmp(component
->name
, map
->dev_name
))
1151 component
->name_prefix
= map
->name_prefix
;
1156 static int soc_probe_component(struct snd_soc_card
*card
,
1157 struct snd_soc_component
*component
)
1159 struct snd_soc_dapm_context
*dapm
= snd_soc_component_get_dapm(component
);
1160 struct snd_soc_dai
*dai
;
1163 if (!strcmp(component
->name
, "snd-soc-dummy"))
1166 if (component
->card
) {
1167 if (component
->card
!= card
) {
1168 dev_err(component
->dev
,
1169 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1170 card
->name
, component
->card
->name
);
1176 if (!try_module_get(component
->dev
->driver
->owner
))
1179 component
->card
= card
;
1181 soc_set_name_prefix(card
, component
);
1183 soc_init_component_debugfs(component
);
1185 if (component
->dapm_widgets
) {
1186 ret
= snd_soc_dapm_new_controls(dapm
, component
->dapm_widgets
,
1187 component
->num_dapm_widgets
);
1190 dev_err(component
->dev
,
1191 "Failed to create new controls %d\n", ret
);
1196 list_for_each_entry(dai
, &component
->dai_list
, list
) {
1197 ret
= snd_soc_dapm_new_dai_widgets(dapm
, dai
);
1199 dev_err(component
->dev
,
1200 "Failed to create DAI widgets %d\n", ret
);
1205 if (component
->probe
) {
1206 ret
= component
->probe(component
);
1208 dev_err(component
->dev
,
1209 "ASoC: failed to probe component %d\n", ret
);
1213 WARN(dapm
->idle_bias_off
&&
1214 dapm
->bias_level
!= SND_SOC_BIAS_OFF
,
1215 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1219 if (component
->controls
)
1220 snd_soc_add_component_controls(component
, component
->controls
,
1221 component
->num_controls
);
1222 if (component
->dapm_routes
)
1223 snd_soc_dapm_add_routes(dapm
, component
->dapm_routes
,
1224 component
->num_dapm_routes
);
1226 list_add(&dapm
->list
, &card
->dapm_list
);
1228 /* This is a HACK and will be removed soon */
1229 if (component
->codec
)
1230 list_add(&component
->codec
->card_list
, &card
->codec_dev_list
);
1235 soc_cleanup_component_debugfs(component
);
1236 component
->card
= NULL
;
1237 module_put(component
->dev
->driver
->owner
);
1242 static void rtd_release(struct device
*dev
)
1247 static int soc_post_component_init(struct snd_soc_pcm_runtime
*rtd
,
1252 /* register the rtd device */
1253 rtd
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
1256 device_initialize(rtd
->dev
);
1257 rtd
->dev
->parent
= rtd
->card
->dev
;
1258 rtd
->dev
->release
= rtd_release
;
1259 rtd
->dev
->groups
= soc_dev_attr_groups
;
1260 dev_set_name(rtd
->dev
, "%s", name
);
1261 dev_set_drvdata(rtd
->dev
, rtd
);
1262 mutex_init(&rtd
->pcm_mutex
);
1263 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_PLAYBACK
].be_clients
);
1264 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_CAPTURE
].be_clients
);
1265 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_PLAYBACK
].fe_clients
);
1266 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_CAPTURE
].fe_clients
);
1267 ret
= device_add(rtd
->dev
);
1269 /* calling put_device() here to free the rtd->dev */
1270 put_device(rtd
->dev
);
1271 dev_err(rtd
->card
->dev
,
1272 "ASoC: failed to register runtime device: %d\n", ret
);
1275 rtd
->dev_registered
= 1;
1279 static int soc_probe_link_components(struct snd_soc_card
*card
,
1280 struct snd_soc_pcm_runtime
*rtd
,
1283 struct snd_soc_platform
*platform
= rtd
->platform
;
1284 struct snd_soc_component
*component
;
1287 /* probe the CPU-side component, if it is a CODEC */
1288 component
= rtd
->cpu_dai
->component
;
1289 if (component
->driver
->probe_order
== order
) {
1290 ret
= soc_probe_component(card
, component
);
1295 /* probe the CODEC-side components */
1296 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1297 component
= rtd
->codec_dais
[i
]->component
;
1298 if (component
->driver
->probe_order
== order
) {
1299 ret
= soc_probe_component(card
, component
);
1305 /* probe the platform */
1306 if (platform
->component
.driver
->probe_order
== order
) {
1307 ret
= soc_probe_component(card
, &platform
->component
);
1315 static int soc_probe_dai(struct snd_soc_dai
*dai
, int order
)
1319 if (!dai
->probed
&& dai
->driver
->probe_order
== order
) {
1320 if (dai
->driver
->probe
) {
1321 ret
= dai
->driver
->probe(dai
);
1324 "ASoC: failed to probe DAI %s: %d\n",
1336 static int soc_link_dai_widgets(struct snd_soc_card
*card
,
1337 struct snd_soc_dai_link
*dai_link
,
1338 struct snd_soc_pcm_runtime
*rtd
)
1340 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1341 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
1342 struct snd_soc_dapm_widget
*play_w
, *capture_w
;
1345 if (rtd
->num_codecs
> 1)
1346 dev_warn(card
->dev
, "ASoC: Multiple codecs not supported yet\n");
1348 /* link the DAI widgets */
1349 play_w
= codec_dai
->playback_widget
;
1350 capture_w
= cpu_dai
->capture_widget
;
1351 if (play_w
&& capture_w
) {
1352 ret
= snd_soc_dapm_new_pcm(card
, dai_link
->params
,
1353 dai_link
->num_params
, capture_w
,
1356 dev_err(card
->dev
, "ASoC: Can't link %s to %s: %d\n",
1357 play_w
->name
, capture_w
->name
, ret
);
1362 play_w
= cpu_dai
->playback_widget
;
1363 capture_w
= codec_dai
->capture_widget
;
1364 if (play_w
&& capture_w
) {
1365 ret
= snd_soc_dapm_new_pcm(card
, dai_link
->params
,
1366 dai_link
->num_params
, capture_w
,
1369 dev_err(card
->dev
, "ASoC: Can't link %s to %s: %d\n",
1370 play_w
->name
, capture_w
->name
, ret
);
1378 static int soc_probe_link_dais(struct snd_soc_card
*card
,
1379 struct snd_soc_pcm_runtime
*rtd
, int order
)
1381 struct snd_soc_dai_link
*dai_link
= rtd
->dai_link
;
1382 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1385 dev_dbg(card
->dev
, "ASoC: probe %s dai link %d late %d\n",
1386 card
->name
, rtd
->num
, order
);
1388 /* set default power off timeout */
1389 rtd
->pmdown_time
= pmdown_time
;
1391 ret
= soc_probe_dai(cpu_dai
, order
);
1395 /* probe the CODEC DAI */
1396 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1397 ret
= soc_probe_dai(rtd
->codec_dais
[i
], order
);
1402 /* complete DAI probe during last probe */
1403 if (order
!= SND_SOC_COMP_ORDER_LAST
)
1406 /* do machine specific initialization */
1407 if (dai_link
->init
) {
1408 ret
= dai_link
->init(rtd
);
1410 dev_err(card
->dev
, "ASoC: failed to init %s: %d\n",
1411 dai_link
->name
, ret
);
1416 if (dai_link
->dai_fmt
)
1417 snd_soc_runtime_set_dai_fmt(rtd
, dai_link
->dai_fmt
);
1419 ret
= soc_post_component_init(rtd
, dai_link
->name
);
1423 #ifdef CONFIG_DEBUG_FS
1424 /* add DPCM sysfs entries */
1425 if (dai_link
->dynamic
)
1426 soc_dpcm_debugfs_add(rtd
);
1429 if (cpu_dai
->driver
->compress_new
) {
1430 /*create compress_device"*/
1431 ret
= cpu_dai
->driver
->compress_new(rtd
, rtd
->num
);
1433 dev_err(card
->dev
, "ASoC: can't create compress %s\n",
1434 dai_link
->stream_name
);
1439 if (!dai_link
->params
) {
1440 /* create the pcm */
1441 ret
= soc_new_pcm(rtd
, rtd
->num
);
1443 dev_err(card
->dev
, "ASoC: can't create pcm %s :%d\n",
1444 dai_link
->stream_name
, ret
);
1448 INIT_DELAYED_WORK(&rtd
->delayed_work
,
1449 codec2codec_close_delayed_work
);
1451 /* link the DAI widgets */
1452 ret
= soc_link_dai_widgets(card
, dai_link
, rtd
);
1461 static int soc_bind_aux_dev(struct snd_soc_card
*card
, int num
)
1463 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd_aux
[num
];
1464 struct snd_soc_aux_dev
*aux_dev
= &card
->aux_dev
[num
];
1465 const char *name
= aux_dev
->codec_name
;
1467 rtd
->component
= soc_find_component(aux_dev
->codec_of_node
, name
);
1468 if (!rtd
->component
) {
1469 if (aux_dev
->codec_of_node
)
1470 name
= of_node_full_name(aux_dev
->codec_of_node
);
1472 dev_err(card
->dev
, "ASoC: %s not registered\n", name
);
1473 return -EPROBE_DEFER
;
1477 * Some places still reference rtd->codec, so we have to keep that
1478 * initialized if the component is a CODEC. Once all those references
1479 * have been removed, this code can be removed as well.
1481 rtd
->codec
= rtd
->component
->codec
;
1486 static int soc_probe_aux_dev(struct snd_soc_card
*card
, int num
)
1488 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd_aux
[num
];
1489 struct snd_soc_aux_dev
*aux_dev
= &card
->aux_dev
[num
];
1492 ret
= soc_probe_component(card
, rtd
->component
);
1496 /* do machine specific initialization */
1497 if (aux_dev
->init
) {
1498 ret
= aux_dev
->init(rtd
->component
);
1500 dev_err(card
->dev
, "ASoC: failed to init %s: %d\n",
1501 aux_dev
->name
, ret
);
1506 return soc_post_component_init(rtd
, aux_dev
->name
);
1509 static void soc_remove_aux_dev(struct snd_soc_card
*card
, int num
)
1511 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd_aux
[num
];
1512 struct snd_soc_component
*component
= rtd
->component
;
1514 /* unregister the rtd device */
1515 if (rtd
->dev_registered
) {
1516 device_unregister(rtd
->dev
);
1517 rtd
->dev_registered
= 0;
1521 soc_remove_component(component
);
1524 static int snd_soc_init_codec_cache(struct snd_soc_codec
*codec
)
1528 if (codec
->cache_init
)
1531 ret
= snd_soc_cache_init(codec
);
1534 "ASoC: Failed to set cache compression type: %d\n",
1538 codec
->cache_init
= 1;
1543 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1544 * @rtd: The runtime for which the DAI link format should be changed
1545 * @dai_fmt: The new DAI link format
1547 * This function updates the DAI link format for all DAIs connected to the DAI
1548 * link for the specified runtime.
1550 * Note: For setups with a static format set the dai_fmt field in the
1551 * corresponding snd_dai_link struct instead of using this function.
1553 * Returns 0 on success, otherwise a negative error code.
1555 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime
*rtd
,
1556 unsigned int dai_fmt
)
1558 struct snd_soc_dai
**codec_dais
= rtd
->codec_dais
;
1559 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1563 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1564 struct snd_soc_dai
*codec_dai
= codec_dais
[i
];
1566 ret
= snd_soc_dai_set_fmt(codec_dai
, dai_fmt
);
1567 if (ret
!= 0 && ret
!= -ENOTSUPP
) {
1568 dev_warn(codec_dai
->dev
,
1569 "ASoC: Failed to set DAI format: %d\n", ret
);
1574 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1575 if (cpu_dai
->codec
) {
1576 unsigned int inv_dai_fmt
;
1578 inv_dai_fmt
= dai_fmt
& ~SND_SOC_DAIFMT_MASTER_MASK
;
1579 switch (dai_fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1580 case SND_SOC_DAIFMT_CBM_CFM
:
1581 inv_dai_fmt
|= SND_SOC_DAIFMT_CBS_CFS
;
1583 case SND_SOC_DAIFMT_CBM_CFS
:
1584 inv_dai_fmt
|= SND_SOC_DAIFMT_CBS_CFM
;
1586 case SND_SOC_DAIFMT_CBS_CFM
:
1587 inv_dai_fmt
|= SND_SOC_DAIFMT_CBM_CFS
;
1589 case SND_SOC_DAIFMT_CBS_CFS
:
1590 inv_dai_fmt
|= SND_SOC_DAIFMT_CBM_CFM
;
1594 dai_fmt
= inv_dai_fmt
;
1597 ret
= snd_soc_dai_set_fmt(cpu_dai
, dai_fmt
);
1598 if (ret
!= 0 && ret
!= -ENOTSUPP
) {
1599 dev_warn(cpu_dai
->dev
,
1600 "ASoC: Failed to set DAI format: %d\n", ret
);
1606 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt
);
1608 static int snd_soc_instantiate_card(struct snd_soc_card
*card
)
1610 struct snd_soc_codec
*codec
;
1611 struct snd_soc_pcm_runtime
*rtd
;
1614 mutex_lock(&client_mutex
);
1615 mutex_lock_nested(&card
->mutex
, SND_SOC_CARD_CLASS_INIT
);
1618 for (i
= 0; i
< card
->num_links
; i
++) {
1619 ret
= soc_bind_dai_link(card
, i
);
1624 /* bind aux_devs too */
1625 for (i
= 0; i
< card
->num_aux_devs
; i
++) {
1626 ret
= soc_bind_aux_dev(card
, i
);
1631 /* initialize the register cache for each available codec */
1632 list_for_each_entry(codec
, &codec_list
, list
) {
1633 if (codec
->cache_init
)
1635 ret
= snd_soc_init_codec_cache(codec
);
1640 /* card bind complete so register a sound card */
1641 ret
= snd_card_new(card
->dev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
1642 card
->owner
, 0, &card
->snd_card
);
1645 "ASoC: can't create sound card for card %s: %d\n",
1650 soc_init_card_debugfs(card
);
1652 card
->dapm
.bias_level
= SND_SOC_BIAS_OFF
;
1653 card
->dapm
.dev
= card
->dev
;
1654 card
->dapm
.card
= card
;
1655 list_add(&card
->dapm
.list
, &card
->dapm_list
);
1657 #ifdef CONFIG_DEBUG_FS
1658 snd_soc_dapm_debugfs_init(&card
->dapm
, card
->debugfs_card_root
);
1661 #ifdef CONFIG_PM_SLEEP
1662 /* deferred resume work */
1663 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
1666 if (card
->dapm_widgets
)
1667 snd_soc_dapm_new_controls(&card
->dapm
, card
->dapm_widgets
,
1668 card
->num_dapm_widgets
);
1670 if (card
->of_dapm_widgets
)
1671 snd_soc_dapm_new_controls(&card
->dapm
, card
->of_dapm_widgets
,
1672 card
->num_of_dapm_widgets
);
1674 /* initialise the sound card only once */
1676 ret
= card
->probe(card
);
1678 goto card_probe_error
;
1681 /* probe all components used by DAI links on this card */
1682 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1684 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
1685 ret
= soc_probe_link_components(card
, rtd
, order
);
1688 "ASoC: failed to instantiate card %d\n",
1695 /* probe all DAI links on this card */
1696 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1698 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
1699 ret
= soc_probe_link_dais(card
, rtd
, order
);
1702 "ASoC: failed to instantiate card %d\n",
1709 for (i
= 0; i
< card
->num_aux_devs
; i
++) {
1710 ret
= soc_probe_aux_dev(card
, i
);
1713 "ASoC: failed to add auxiliary devices %d\n",
1715 goto probe_aux_dev_err
;
1719 snd_soc_dapm_link_dai_widgets(card
);
1720 snd_soc_dapm_connect_dai_link_widgets(card
);
1723 snd_soc_add_card_controls(card
, card
->controls
, card
->num_controls
);
1725 if (card
->dapm_routes
)
1726 snd_soc_dapm_add_routes(&card
->dapm
, card
->dapm_routes
,
1727 card
->num_dapm_routes
);
1729 if (card
->of_dapm_routes
)
1730 snd_soc_dapm_add_routes(&card
->dapm
, card
->of_dapm_routes
,
1731 card
->num_of_dapm_routes
);
1733 snprintf(card
->snd_card
->shortname
, sizeof(card
->snd_card
->shortname
),
1735 snprintf(card
->snd_card
->longname
, sizeof(card
->snd_card
->longname
),
1736 "%s", card
->long_name
? card
->long_name
: card
->name
);
1737 snprintf(card
->snd_card
->driver
, sizeof(card
->snd_card
->driver
),
1738 "%s", card
->driver_name
? card
->driver_name
: card
->name
);
1739 for (i
= 0; i
< ARRAY_SIZE(card
->snd_card
->driver
); i
++) {
1740 switch (card
->snd_card
->driver
[i
]) {
1746 if (!isalnum(card
->snd_card
->driver
[i
]))
1747 card
->snd_card
->driver
[i
] = '_';
1752 if (card
->late_probe
) {
1753 ret
= card
->late_probe(card
);
1755 dev_err(card
->dev
, "ASoC: %s late_probe() failed: %d\n",
1757 goto probe_aux_dev_err
;
1761 snd_soc_dapm_new_widgets(card
);
1763 ret
= snd_card_register(card
->snd_card
);
1765 dev_err(card
->dev
, "ASoC: failed to register soundcard %d\n",
1767 goto probe_aux_dev_err
;
1770 card
->instantiated
= 1;
1771 snd_soc_dapm_sync(&card
->dapm
);
1772 mutex_unlock(&card
->mutex
);
1773 mutex_unlock(&client_mutex
);
1778 for (i
= 0; i
< card
->num_aux_devs
; i
++)
1779 soc_remove_aux_dev(card
, i
);
1782 soc_remove_dai_links(card
);
1788 snd_soc_dapm_free(&card
->dapm
);
1789 soc_cleanup_card_debugfs(card
);
1790 snd_card_free(card
->snd_card
);
1793 soc_remove_pcm_runtimes(card
);
1794 mutex_unlock(&card
->mutex
);
1795 mutex_unlock(&client_mutex
);
1800 /* probes a new socdev */
1801 static int soc_probe(struct platform_device
*pdev
)
1803 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1806 * no card, so machine driver should be registering card
1807 * we should not be here in that case so ret error
1812 dev_warn(&pdev
->dev
,
1813 "ASoC: machine %s should use snd_soc_register_card()\n",
1816 /* Bodge while we unpick instantiation */
1817 card
->dev
= &pdev
->dev
;
1819 return snd_soc_register_card(card
);
1822 static int soc_cleanup_card_resources(struct snd_soc_card
*card
)
1824 struct snd_soc_pcm_runtime
*rtd
;
1827 /* make sure any delayed work runs */
1828 list_for_each_entry(rtd
, &card
->rtd_list
, list
)
1829 flush_delayed_work(&rtd
->delayed_work
);
1831 /* remove auxiliary devices */
1832 for (i
= 0; i
< card
->num_aux_devs
; i
++)
1833 soc_remove_aux_dev(card
, i
);
1835 /* remove and free each DAI */
1836 soc_remove_dai_links(card
);
1837 soc_remove_pcm_runtimes(card
);
1839 soc_cleanup_card_debugfs(card
);
1841 /* remove the card */
1845 snd_soc_dapm_free(&card
->dapm
);
1847 snd_card_free(card
->snd_card
);
1852 /* removes a socdev */
1853 static int soc_remove(struct platform_device
*pdev
)
1855 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1857 snd_soc_unregister_card(card
);
1861 int snd_soc_poweroff(struct device
*dev
)
1863 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
1864 struct snd_soc_pcm_runtime
*rtd
;
1866 if (!card
->instantiated
)
1869 /* Flush out pmdown_time work - we actually do want to run it
1870 * now, we're shutting down so no imminent restart. */
1871 list_for_each_entry(rtd
, &card
->rtd_list
, list
)
1872 flush_delayed_work(&rtd
->delayed_work
);
1874 snd_soc_dapm_shutdown(card
);
1876 /* deactivate pins to sleep state */
1877 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
1878 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1881 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
1882 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1883 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[i
];
1884 pinctrl_pm_select_sleep_state(codec_dai
->dev
);
1890 EXPORT_SYMBOL_GPL(snd_soc_poweroff
);
1892 const struct dev_pm_ops snd_soc_pm_ops
= {
1893 .suspend
= snd_soc_suspend
,
1894 .resume
= snd_soc_resume
,
1895 .freeze
= snd_soc_suspend
,
1896 .thaw
= snd_soc_resume
,
1897 .poweroff
= snd_soc_poweroff
,
1898 .restore
= snd_soc_resume
,
1900 EXPORT_SYMBOL_GPL(snd_soc_pm_ops
);
1902 /* ASoC platform driver */
1903 static struct platform_driver soc_driver
= {
1905 .name
= "soc-audio",
1906 .pm
= &snd_soc_pm_ops
,
1909 .remove
= soc_remove
,
1913 * snd_soc_cnew - create new control
1914 * @_template: control template
1915 * @data: control private data
1916 * @long_name: control long name
1917 * @prefix: control name prefix
1919 * Create a new mixer control from a template control.
1921 * Returns 0 for success, else error.
1923 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
1924 void *data
, const char *long_name
,
1927 struct snd_kcontrol_new
template;
1928 struct snd_kcontrol
*kcontrol
;
1931 memcpy(&template, _template
, sizeof(template));
1935 long_name
= template.name
;
1938 name
= kasprintf(GFP_KERNEL
, "%s %s", prefix
, long_name
);
1942 template.name
= name
;
1944 template.name
= long_name
;
1947 kcontrol
= snd_ctl_new1(&template, data
);
1953 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
1955 static int snd_soc_add_controls(struct snd_card
*card
, struct device
*dev
,
1956 const struct snd_kcontrol_new
*controls
, int num_controls
,
1957 const char *prefix
, void *data
)
1961 for (i
= 0; i
< num_controls
; i
++) {
1962 const struct snd_kcontrol_new
*control
= &controls
[i
];
1963 err
= snd_ctl_add(card
, snd_soc_cnew(control
, data
,
1964 control
->name
, prefix
));
1966 dev_err(dev
, "ASoC: Failed to add %s: %d\n",
1967 control
->name
, err
);
1975 struct snd_kcontrol
*snd_soc_card_get_kcontrol(struct snd_soc_card
*soc_card
,
1978 struct snd_card
*card
= soc_card
->snd_card
;
1979 struct snd_kcontrol
*kctl
;
1981 if (unlikely(!name
))
1984 list_for_each_entry(kctl
, &card
->controls
, list
)
1985 if (!strncmp(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
)))
1989 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol
);
1992 * snd_soc_add_component_controls - Add an array of controls to a component.
1994 * @component: Component to add controls to
1995 * @controls: Array of controls to add
1996 * @num_controls: Number of elements in the array
1998 * Return: 0 for success, else error.
2000 int snd_soc_add_component_controls(struct snd_soc_component
*component
,
2001 const struct snd_kcontrol_new
*controls
, unsigned int num_controls
)
2003 struct snd_card
*card
= component
->card
->snd_card
;
2005 return snd_soc_add_controls(card
, component
->dev
, controls
,
2006 num_controls
, component
->name_prefix
, component
);
2008 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls
);
2011 * snd_soc_add_codec_controls - add an array of controls to a codec.
2012 * Convenience function to add a list of controls. Many codecs were
2013 * duplicating this code.
2015 * @codec: codec to add controls to
2016 * @controls: array of controls to add
2017 * @num_controls: number of elements in the array
2019 * Return 0 for success, else error.
2021 int snd_soc_add_codec_controls(struct snd_soc_codec
*codec
,
2022 const struct snd_kcontrol_new
*controls
, unsigned int num_controls
)
2024 return snd_soc_add_component_controls(&codec
->component
, controls
,
2027 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls
);
2030 * snd_soc_add_platform_controls - add an array of controls to a platform.
2031 * Convenience function to add a list of controls.
2033 * @platform: platform to add controls to
2034 * @controls: array of controls to add
2035 * @num_controls: number of elements in the array
2037 * Return 0 for success, else error.
2039 int snd_soc_add_platform_controls(struct snd_soc_platform
*platform
,
2040 const struct snd_kcontrol_new
*controls
, unsigned int num_controls
)
2042 return snd_soc_add_component_controls(&platform
->component
, controls
,
2045 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls
);
2048 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2049 * Convenience function to add a list of controls.
2051 * @soc_card: SoC card to add controls to
2052 * @controls: array of controls to add
2053 * @num_controls: number of elements in the array
2055 * Return 0 for success, else error.
2057 int snd_soc_add_card_controls(struct snd_soc_card
*soc_card
,
2058 const struct snd_kcontrol_new
*controls
, int num_controls
)
2060 struct snd_card
*card
= soc_card
->snd_card
;
2062 return snd_soc_add_controls(card
, soc_card
->dev
, controls
, num_controls
,
2065 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls
);
2068 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2069 * Convienience function to add a list of controls.
2071 * @dai: DAI to add controls to
2072 * @controls: array of controls to add
2073 * @num_controls: number of elements in the array
2075 * Return 0 for success, else error.
2077 int snd_soc_add_dai_controls(struct snd_soc_dai
*dai
,
2078 const struct snd_kcontrol_new
*controls
, int num_controls
)
2080 struct snd_card
*card
= dai
->component
->card
->snd_card
;
2082 return snd_soc_add_controls(card
, dai
->dev
, controls
, num_controls
,
2085 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls
);
2088 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2090 * @clk_id: DAI specific clock ID
2091 * @freq: new clock frequency in Hz
2092 * @dir: new clock direction - input/output.
2094 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2096 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
2097 unsigned int freq
, int dir
)
2099 if (dai
->driver
&& dai
->driver
->ops
->set_sysclk
)
2100 return dai
->driver
->ops
->set_sysclk(dai
, clk_id
, freq
, dir
);
2101 else if (dai
->codec
&& dai
->codec
->driver
->set_sysclk
)
2102 return dai
->codec
->driver
->set_sysclk(dai
->codec
, clk_id
, 0,
2107 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
2110 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2112 * @clk_id: DAI specific clock ID
2113 * @source: Source for the clock
2114 * @freq: new clock frequency in Hz
2115 * @dir: new clock direction - input/output.
2117 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2119 int snd_soc_codec_set_sysclk(struct snd_soc_codec
*codec
, int clk_id
,
2120 int source
, unsigned int freq
, int dir
)
2122 if (codec
->driver
->set_sysclk
)
2123 return codec
->driver
->set_sysclk(codec
, clk_id
, source
,
2128 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk
);
2131 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2133 * @div_id: DAI specific clock divider ID
2134 * @div: new clock divisor.
2136 * Configures the clock dividers. This is used to derive the best DAI bit and
2137 * frame clocks from the system or master clock. It's best to set the DAI bit
2138 * and frame clocks as low as possible to save system power.
2140 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
2141 int div_id
, int div
)
2143 if (dai
->driver
&& dai
->driver
->ops
->set_clkdiv
)
2144 return dai
->driver
->ops
->set_clkdiv(dai
, div_id
, div
);
2148 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
2151 * snd_soc_dai_set_pll - configure DAI PLL.
2153 * @pll_id: DAI specific PLL ID
2154 * @source: DAI specific source for the PLL
2155 * @freq_in: PLL input clock frequency in Hz
2156 * @freq_out: requested PLL output clock frequency in Hz
2158 * Configures and enables PLL to generate output clock based on input clock.
2160 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
2161 unsigned int freq_in
, unsigned int freq_out
)
2163 if (dai
->driver
&& dai
->driver
->ops
->set_pll
)
2164 return dai
->driver
->ops
->set_pll(dai
, pll_id
, source
,
2166 else if (dai
->codec
&& dai
->codec
->driver
->set_pll
)
2167 return dai
->codec
->driver
->set_pll(dai
->codec
, pll_id
, source
,
2172 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
2175 * snd_soc_codec_set_pll - configure codec PLL.
2177 * @pll_id: DAI specific PLL ID
2178 * @source: DAI specific source for the PLL
2179 * @freq_in: PLL input clock frequency in Hz
2180 * @freq_out: requested PLL output clock frequency in Hz
2182 * Configures and enables PLL to generate output clock based on input clock.
2184 int snd_soc_codec_set_pll(struct snd_soc_codec
*codec
, int pll_id
, int source
,
2185 unsigned int freq_in
, unsigned int freq_out
)
2187 if (codec
->driver
->set_pll
)
2188 return codec
->driver
->set_pll(codec
, pll_id
, source
,
2193 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll
);
2196 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2198 * @ratio: Ratio of BCLK to Sample rate.
2200 * Configures the DAI for a preset BCLK to sample rate ratio.
2202 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai
*dai
, unsigned int ratio
)
2204 if (dai
->driver
&& dai
->driver
->ops
->set_bclk_ratio
)
2205 return dai
->driver
->ops
->set_bclk_ratio(dai
, ratio
);
2209 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio
);
2212 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2214 * @fmt: SND_SOC_DAIFMT_ format value.
2216 * Configures the DAI hardware format and clocking.
2218 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
2220 if (dai
->driver
== NULL
)
2222 if (dai
->driver
->ops
->set_fmt
== NULL
)
2224 return dai
->driver
->ops
->set_fmt(dai
, fmt
);
2226 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
2229 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2230 * @slots: Number of slots in use.
2231 * @tx_mask: bitmask representing active TX slots.
2232 * @rx_mask: bitmask representing active RX slots.
2234 * Generates the TDM tx and rx slot default masks for DAI.
2236 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots
,
2237 unsigned int *tx_mask
,
2238 unsigned int *rx_mask
)
2240 if (*tx_mask
|| *rx_mask
)
2246 *tx_mask
= (1 << slots
) - 1;
2247 *rx_mask
= (1 << slots
) - 1;
2253 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2254 * @dai: The DAI to configure
2255 * @tx_mask: bitmask representing active TX slots.
2256 * @rx_mask: bitmask representing active RX slots.
2257 * @slots: Number of slots in use.
2258 * @slot_width: Width in bits for each slot.
2260 * This function configures the specified DAI for TDM operation. @slot contains
2261 * the total number of slots of the TDM stream and @slot_with the width of each
2262 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2263 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2264 * DAI should write to or read from. If a bit is set the corresponding slot is
2265 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2266 * the first slot, bit 1 to the second slot and so on. The first active slot
2267 * maps to the first channel of the DAI, the second active slot to the second
2268 * channel and so on.
2270 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2271 * @rx_mask and @slot_width will be ignored.
2273 * Returns 0 on success, a negative error code otherwise.
2275 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
2276 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
2278 if (dai
->driver
&& dai
->driver
->ops
->xlate_tdm_slot_mask
)
2279 dai
->driver
->ops
->xlate_tdm_slot_mask(slots
,
2280 &tx_mask
, &rx_mask
);
2282 snd_soc_xlate_tdm_slot_mask(slots
, &tx_mask
, &rx_mask
);
2284 dai
->tx_mask
= tx_mask
;
2285 dai
->rx_mask
= rx_mask
;
2287 if (dai
->driver
&& dai
->driver
->ops
->set_tdm_slot
)
2288 return dai
->driver
->ops
->set_tdm_slot(dai
, tx_mask
, rx_mask
,
2293 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
2296 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2298 * @tx_num: how many TX channels
2299 * @tx_slot: pointer to an array which imply the TX slot number channel
2301 * @rx_num: how many RX channels
2302 * @rx_slot: pointer to an array which imply the RX slot number channel
2305 * configure the relationship between channel number and TDM slot number.
2307 int snd_soc_dai_set_channel_map(struct snd_soc_dai
*dai
,
2308 unsigned int tx_num
, unsigned int *tx_slot
,
2309 unsigned int rx_num
, unsigned int *rx_slot
)
2311 if (dai
->driver
&& dai
->driver
->ops
->set_channel_map
)
2312 return dai
->driver
->ops
->set_channel_map(dai
, tx_num
, tx_slot
,
2317 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map
);
2320 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2322 * @tristate: tristate enable
2324 * Tristates the DAI so that others can use it.
2326 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
2328 if (dai
->driver
&& dai
->driver
->ops
->set_tristate
)
2329 return dai
->driver
->ops
->set_tristate(dai
, tristate
);
2333 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
2336 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2338 * @mute: mute enable
2339 * @direction: stream to mute
2341 * Mutes the DAI DAC.
2343 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
,
2349 if (dai
->driver
->ops
->mute_stream
)
2350 return dai
->driver
->ops
->mute_stream(dai
, mute
, direction
);
2351 else if (direction
== SNDRV_PCM_STREAM_PLAYBACK
&&
2352 dai
->driver
->ops
->digital_mute
)
2353 return dai
->driver
->ops
->digital_mute(dai
, mute
);
2357 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
2359 static int snd_soc_init_multicodec(struct snd_soc_card
*card
,
2360 struct snd_soc_dai_link
*dai_link
)
2362 /* Legacy codec/codec_dai link is a single entry in multicodec */
2363 if (dai_link
->codec_name
|| dai_link
->codec_of_node
||
2364 dai_link
->codec_dai_name
) {
2365 dai_link
->num_codecs
= 1;
2367 dai_link
->codecs
= devm_kzalloc(card
->dev
,
2368 sizeof(struct snd_soc_dai_link_component
),
2370 if (!dai_link
->codecs
)
2373 dai_link
->codecs
[0].name
= dai_link
->codec_name
;
2374 dai_link
->codecs
[0].of_node
= dai_link
->codec_of_node
;
2375 dai_link
->codecs
[0].dai_name
= dai_link
->codec_dai_name
;
2378 if (!dai_link
->codecs
) {
2379 dev_err(card
->dev
, "ASoC: DAI link has no CODECs\n");
2387 * snd_soc_register_card - Register a card with the ASoC core
2389 * @card: Card to register
2392 int snd_soc_register_card(struct snd_soc_card
*card
)
2395 struct snd_soc_pcm_runtime
*rtd
;
2397 if (!card
->name
|| !card
->dev
)
2400 for (i
= 0; i
< card
->num_links
; i
++) {
2401 struct snd_soc_dai_link
*link
= &card
->dai_link
[i
];
2403 ret
= snd_soc_init_multicodec(card
, link
);
2405 dev_err(card
->dev
, "ASoC: failed to init multicodec\n");
2409 for (j
= 0; j
< link
->num_codecs
; j
++) {
2411 * Codec must be specified by 1 of name or OF node,
2412 * not both or neither.
2414 if (!!link
->codecs
[j
].name
==
2415 !!link
->codecs
[j
].of_node
) {
2416 dev_err(card
->dev
, "ASoC: Neither/both codec name/of_node are set for %s\n",
2420 /* Codec DAI name must be specified */
2421 if (!link
->codecs
[j
].dai_name
) {
2422 dev_err(card
->dev
, "ASoC: codec_dai_name not set for %s\n",
2429 * Platform may be specified by either name or OF node, but
2430 * can be left unspecified, and a dummy platform will be used.
2432 if (link
->platform_name
&& link
->platform_of_node
) {
2434 "ASoC: Both platform name/of_node are set for %s\n",
2440 * CPU device may be specified by either name or OF node, but
2441 * can be left unspecified, and will be matched based on DAI
2444 if (link
->cpu_name
&& link
->cpu_of_node
) {
2446 "ASoC: Neither/both cpu name/of_node are set for %s\n",
2451 * At least one of CPU DAI name or CPU device name/node must be
2454 if (!link
->cpu_dai_name
&&
2455 !(link
->cpu_name
|| link
->cpu_of_node
)) {
2457 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
2463 dev_set_drvdata(card
->dev
, card
);
2465 snd_soc_initialize_card_lists(card
);
2467 INIT_LIST_HEAD(&card
->rtd_list
);
2470 card
->rtd_aux
= devm_kzalloc(card
->dev
,
2471 sizeof(struct snd_soc_pcm_runtime
) *
2474 if (card
->rtd_aux
== NULL
)
2477 for (i
= 0; i
< card
->num_aux_devs
; i
++)
2478 card
->rtd_aux
[i
].card
= card
;
2480 INIT_LIST_HEAD(&card
->dapm_dirty
);
2481 INIT_LIST_HEAD(&card
->dobj_list
);
2482 card
->instantiated
= 0;
2483 mutex_init(&card
->mutex
);
2484 mutex_init(&card
->dapm_mutex
);
2486 ret
= snd_soc_instantiate_card(card
);
2490 /* deactivate pins to sleep state */
2491 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
2492 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
2495 for (j
= 0; j
< rtd
->num_codecs
; j
++) {
2496 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[j
];
2497 if (!codec_dai
->active
)
2498 pinctrl_pm_select_sleep_state(codec_dai
->dev
);
2501 if (!cpu_dai
->active
)
2502 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
2507 EXPORT_SYMBOL_GPL(snd_soc_register_card
);
2510 * snd_soc_unregister_card - Unregister a card with the ASoC core
2512 * @card: Card to unregister
2515 int snd_soc_unregister_card(struct snd_soc_card
*card
)
2517 if (card
->instantiated
) {
2518 card
->instantiated
= false;
2519 snd_soc_dapm_shutdown(card
);
2520 soc_cleanup_card_resources(card
);
2521 dev_dbg(card
->dev
, "ASoC: Unregistered card '%s'\n", card
->name
);
2526 EXPORT_SYMBOL_GPL(snd_soc_unregister_card
);
2529 * Simplify DAI link configuration by removing ".-1" from device names
2530 * and sanitizing names.
2532 static char *fmt_single_name(struct device
*dev
, int *id
)
2534 char *found
, name
[NAME_SIZE
];
2537 if (dev_name(dev
) == NULL
)
2540 strlcpy(name
, dev_name(dev
), NAME_SIZE
);
2542 /* are we a "%s.%d" name (platform and SPI components) */
2543 found
= strstr(name
, dev
->driver
->name
);
2546 if (sscanf(&found
[strlen(dev
->driver
->name
)], ".%d", id
) == 1) {
2548 /* discard ID from name if ID == -1 */
2550 found
[strlen(dev
->driver
->name
)] = '\0';
2554 /* I2C component devices are named "bus-addr" */
2555 if (sscanf(name
, "%x-%x", &id1
, &id2
) == 2) {
2556 char tmp
[NAME_SIZE
];
2558 /* create unique ID number from I2C addr and bus */
2559 *id
= ((id1
& 0xffff) << 16) + id2
;
2561 /* sanitize component name for DAI link creation */
2562 snprintf(tmp
, NAME_SIZE
, "%s.%s", dev
->driver
->name
, name
);
2563 strlcpy(name
, tmp
, NAME_SIZE
);
2568 return kstrdup(name
, GFP_KERNEL
);
2572 * Simplify DAI link naming for single devices with multiple DAIs by removing
2573 * any ".-1" and using the DAI name (instead of device name).
2575 static inline char *fmt_multiple_name(struct device
*dev
,
2576 struct snd_soc_dai_driver
*dai_drv
)
2578 if (dai_drv
->name
== NULL
) {
2580 "ASoC: error - multiple DAI %s registered with no name\n",
2585 return kstrdup(dai_drv
->name
, GFP_KERNEL
);
2589 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2591 * @component: The component for which the DAIs should be unregistered
2593 static void snd_soc_unregister_dais(struct snd_soc_component
*component
)
2595 struct snd_soc_dai
*dai
, *_dai
;
2597 list_for_each_entry_safe(dai
, _dai
, &component
->dai_list
, list
) {
2598 dev_dbg(component
->dev
, "ASoC: Unregistered DAI '%s'\n",
2600 list_del(&dai
->list
);
2607 * snd_soc_register_dais - Register a DAI with the ASoC core
2609 * @component: The component the DAIs are registered for
2610 * @dai_drv: DAI driver to use for the DAIs
2611 * @count: Number of DAIs
2612 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2615 static int snd_soc_register_dais(struct snd_soc_component
*component
,
2616 struct snd_soc_dai_driver
*dai_drv
, size_t count
,
2617 bool legacy_dai_naming
)
2619 struct device
*dev
= component
->dev
;
2620 struct snd_soc_dai
*dai
;
2624 dev_dbg(dev
, "ASoC: dai register %s #%Zu\n", dev_name(dev
), count
);
2626 component
->dai_drv
= dai_drv
;
2627 component
->num_dai
= count
;
2629 for (i
= 0; i
< count
; i
++) {
2631 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
2638 * Back in the old days when we still had component-less DAIs,
2639 * instead of having a static name, component-less DAIs would
2640 * inherit the name of the parent device so it is possible to
2641 * register multiple instances of the DAI. We still need to keep
2642 * the same naming style even though those DAIs are not
2643 * component-less anymore.
2645 if (count
== 1 && legacy_dai_naming
&&
2646 (dai_drv
[i
].id
== 0 || dai_drv
[i
].name
== NULL
)) {
2647 dai
->name
= fmt_single_name(dev
, &dai
->id
);
2649 dai
->name
= fmt_multiple_name(dev
, &dai_drv
[i
]);
2651 dai
->id
= dai_drv
[i
].id
;
2655 if (dai
->name
== NULL
) {
2661 dai
->component
= component
;
2663 dai
->driver
= &dai_drv
[i
];
2664 if (!dai
->driver
->ops
)
2665 dai
->driver
->ops
= &null_dai_ops
;
2667 list_add(&dai
->list
, &component
->dai_list
);
2669 dev_dbg(dev
, "ASoC: Registered DAI '%s'\n", dai
->name
);
2675 snd_soc_unregister_dais(component
);
2680 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context
*dapm
,
2681 enum snd_soc_dapm_type type
, int subseq
)
2683 struct snd_soc_component
*component
= dapm
->component
;
2685 component
->driver
->seq_notifier(component
, type
, subseq
);
2688 static int snd_soc_component_stream_event(struct snd_soc_dapm_context
*dapm
,
2691 struct snd_soc_component
*component
= dapm
->component
;
2693 return component
->driver
->stream_event(component
, event
);
2696 static int snd_soc_component_initialize(struct snd_soc_component
*component
,
2697 const struct snd_soc_component_driver
*driver
, struct device
*dev
)
2699 struct snd_soc_dapm_context
*dapm
;
2701 component
->name
= fmt_single_name(dev
, &component
->id
);
2702 if (!component
->name
) {
2703 dev_err(dev
, "ASoC: Failed to allocate name\n");
2707 component
->dev
= dev
;
2708 component
->driver
= driver
;
2709 component
->probe
= component
->driver
->probe
;
2710 component
->remove
= component
->driver
->remove
;
2712 dapm
= &component
->dapm
;
2714 dapm
->component
= component
;
2715 dapm
->bias_level
= SND_SOC_BIAS_OFF
;
2716 dapm
->idle_bias_off
= true;
2717 if (driver
->seq_notifier
)
2718 dapm
->seq_notifier
= snd_soc_component_seq_notifier
;
2719 if (driver
->stream_event
)
2720 dapm
->stream_event
= snd_soc_component_stream_event
;
2722 component
->controls
= driver
->controls
;
2723 component
->num_controls
= driver
->num_controls
;
2724 component
->dapm_widgets
= driver
->dapm_widgets
;
2725 component
->num_dapm_widgets
= driver
->num_dapm_widgets
;
2726 component
->dapm_routes
= driver
->dapm_routes
;
2727 component
->num_dapm_routes
= driver
->num_dapm_routes
;
2729 INIT_LIST_HEAD(&component
->dai_list
);
2730 mutex_init(&component
->io_mutex
);
2735 static void snd_soc_component_setup_regmap(struct snd_soc_component
*component
)
2737 int val_bytes
= regmap_get_val_bytes(component
->regmap
);
2739 /* Errors are legitimate for non-integer byte multiples */
2741 component
->val_bytes
= val_bytes
;
2744 #ifdef CONFIG_REGMAP
2747 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
2748 * @component: The component for which to initialize the regmap instance
2749 * @regmap: The regmap instance that should be used by the component
2751 * This function allows deferred assignment of the regmap instance that is
2752 * associated with the component. Only use this if the regmap instance is not
2753 * yet ready when the component is registered. The function must also be called
2754 * before the first IO attempt of the component.
2756 void snd_soc_component_init_regmap(struct snd_soc_component
*component
,
2757 struct regmap
*regmap
)
2759 component
->regmap
= regmap
;
2760 snd_soc_component_setup_regmap(component
);
2762 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap
);
2765 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
2766 * @component: The component for which to de-initialize the regmap instance
2768 * Calls regmap_exit() on the regmap instance associated to the component and
2769 * removes the regmap instance from the component.
2771 * This function should only be used if snd_soc_component_init_regmap() was used
2772 * to initialize the regmap instance.
2774 void snd_soc_component_exit_regmap(struct snd_soc_component
*component
)
2776 regmap_exit(component
->regmap
);
2777 component
->regmap
= NULL
;
2779 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap
);
2783 static void snd_soc_component_add_unlocked(struct snd_soc_component
*component
)
2785 if (!component
->write
&& !component
->read
) {
2786 if (!component
->regmap
)
2787 component
->regmap
= dev_get_regmap(component
->dev
, NULL
);
2788 if (component
->regmap
)
2789 snd_soc_component_setup_regmap(component
);
2792 list_add(&component
->list
, &component_list
);
2793 INIT_LIST_HEAD(&component
->dobj_list
);
2796 static void snd_soc_component_add(struct snd_soc_component
*component
)
2798 mutex_lock(&client_mutex
);
2799 snd_soc_component_add_unlocked(component
);
2800 mutex_unlock(&client_mutex
);
2803 static void snd_soc_component_cleanup(struct snd_soc_component
*component
)
2805 snd_soc_unregister_dais(component
);
2806 kfree(component
->name
);
2809 static void snd_soc_component_del_unlocked(struct snd_soc_component
*component
)
2811 list_del(&component
->list
);
2814 int snd_soc_register_component(struct device
*dev
,
2815 const struct snd_soc_component_driver
*cmpnt_drv
,
2816 struct snd_soc_dai_driver
*dai_drv
,
2819 struct snd_soc_component
*cmpnt
;
2822 cmpnt
= kzalloc(sizeof(*cmpnt
), GFP_KERNEL
);
2824 dev_err(dev
, "ASoC: Failed to allocate memory\n");
2828 ret
= snd_soc_component_initialize(cmpnt
, cmpnt_drv
, dev
);
2832 cmpnt
->ignore_pmdown_time
= true;
2833 cmpnt
->registered_as_component
= true;
2835 ret
= snd_soc_register_dais(cmpnt
, dai_drv
, num_dai
, true);
2837 dev_err(dev
, "ASoC: Failed to register DAIs: %d\n", ret
);
2841 snd_soc_component_add(cmpnt
);
2846 snd_soc_component_cleanup(cmpnt
);
2851 EXPORT_SYMBOL_GPL(snd_soc_register_component
);
2854 * snd_soc_unregister_component - Unregister a component from the ASoC core
2856 * @dev: The device to unregister
2858 void snd_soc_unregister_component(struct device
*dev
)
2860 struct snd_soc_component
*cmpnt
;
2862 mutex_lock(&client_mutex
);
2863 list_for_each_entry(cmpnt
, &component_list
, list
) {
2864 if (dev
== cmpnt
->dev
&& cmpnt
->registered_as_component
)
2867 mutex_unlock(&client_mutex
);
2871 snd_soc_tplg_component_remove(cmpnt
, SND_SOC_TPLG_INDEX_ALL
);
2872 snd_soc_component_del_unlocked(cmpnt
);
2873 mutex_unlock(&client_mutex
);
2874 snd_soc_component_cleanup(cmpnt
);
2877 EXPORT_SYMBOL_GPL(snd_soc_unregister_component
);
2879 static int snd_soc_platform_drv_probe(struct snd_soc_component
*component
)
2881 struct snd_soc_platform
*platform
= snd_soc_component_to_platform(component
);
2883 return platform
->driver
->probe(platform
);
2886 static void snd_soc_platform_drv_remove(struct snd_soc_component
*component
)
2888 struct snd_soc_platform
*platform
= snd_soc_component_to_platform(component
);
2890 platform
->driver
->remove(platform
);
2894 * snd_soc_add_platform - Add a platform to the ASoC core
2895 * @dev: The parent device for the platform
2896 * @platform: The platform to add
2897 * @platform_drv: The driver for the platform
2899 int snd_soc_add_platform(struct device
*dev
, struct snd_soc_platform
*platform
,
2900 const struct snd_soc_platform_driver
*platform_drv
)
2904 ret
= snd_soc_component_initialize(&platform
->component
,
2905 &platform_drv
->component_driver
, dev
);
2909 platform
->dev
= dev
;
2910 platform
->driver
= platform_drv
;
2912 if (platform_drv
->probe
)
2913 platform
->component
.probe
= snd_soc_platform_drv_probe
;
2914 if (platform_drv
->remove
)
2915 platform
->component
.remove
= snd_soc_platform_drv_remove
;
2917 #ifdef CONFIG_DEBUG_FS
2918 platform
->component
.debugfs_prefix
= "platform";
2921 mutex_lock(&client_mutex
);
2922 snd_soc_component_add_unlocked(&platform
->component
);
2923 list_add(&platform
->list
, &platform_list
);
2924 mutex_unlock(&client_mutex
);
2926 dev_dbg(dev
, "ASoC: Registered platform '%s'\n",
2927 platform
->component
.name
);
2931 EXPORT_SYMBOL_GPL(snd_soc_add_platform
);
2934 * snd_soc_register_platform - Register a platform with the ASoC core
2936 * @dev: The device for the platform
2937 * @platform_drv: The driver for the platform
2939 int snd_soc_register_platform(struct device
*dev
,
2940 const struct snd_soc_platform_driver
*platform_drv
)
2942 struct snd_soc_platform
*platform
;
2945 dev_dbg(dev
, "ASoC: platform register %s\n", dev_name(dev
));
2947 platform
= kzalloc(sizeof(struct snd_soc_platform
), GFP_KERNEL
);
2948 if (platform
== NULL
)
2951 ret
= snd_soc_add_platform(dev
, platform
, platform_drv
);
2957 EXPORT_SYMBOL_GPL(snd_soc_register_platform
);
2960 * snd_soc_remove_platform - Remove a platform from the ASoC core
2961 * @platform: the platform to remove
2963 void snd_soc_remove_platform(struct snd_soc_platform
*platform
)
2966 mutex_lock(&client_mutex
);
2967 list_del(&platform
->list
);
2968 snd_soc_component_del_unlocked(&platform
->component
);
2969 mutex_unlock(&client_mutex
);
2971 dev_dbg(platform
->dev
, "ASoC: Unregistered platform '%s'\n",
2972 platform
->component
.name
);
2974 snd_soc_component_cleanup(&platform
->component
);
2976 EXPORT_SYMBOL_GPL(snd_soc_remove_platform
);
2978 struct snd_soc_platform
*snd_soc_lookup_platform(struct device
*dev
)
2980 struct snd_soc_platform
*platform
;
2982 mutex_lock(&client_mutex
);
2983 list_for_each_entry(platform
, &platform_list
, list
) {
2984 if (dev
== platform
->dev
) {
2985 mutex_unlock(&client_mutex
);
2989 mutex_unlock(&client_mutex
);
2993 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform
);
2996 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2998 * @dev: platform to unregister
3000 void snd_soc_unregister_platform(struct device
*dev
)
3002 struct snd_soc_platform
*platform
;
3004 platform
= snd_soc_lookup_platform(dev
);
3008 snd_soc_remove_platform(platform
);
3011 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform
);
3013 static u64 codec_format_map
[] = {
3014 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
,
3015 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_U16_BE
,
3016 SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S24_BE
,
3017 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_U24_BE
,
3018 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
,
3019 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_U32_BE
,
3020 SNDRV_PCM_FMTBIT_S24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3021 SNDRV_PCM_FMTBIT_U24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3022 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S20_3BE
,
3023 SNDRV_PCM_FMTBIT_U20_3LE
| SNDRV_PCM_FMTBIT_U20_3BE
,
3024 SNDRV_PCM_FMTBIT_S18_3LE
| SNDRV_PCM_FMTBIT_S18_3BE
,
3025 SNDRV_PCM_FMTBIT_U18_3LE
| SNDRV_PCM_FMTBIT_U18_3BE
,
3026 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
,
3027 SNDRV_PCM_FMTBIT_FLOAT64_LE
| SNDRV_PCM_FMTBIT_FLOAT64_BE
,
3028 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3029 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
,
3032 /* Fix up the DAI formats for endianness: codecs don't actually see
3033 * the endianness of the data but we're using the CPU format
3034 * definitions which do need to include endianness so we ensure that
3035 * codec DAIs always have both big and little endian variants set.
3037 static void fixup_codec_formats(struct snd_soc_pcm_stream
*stream
)
3041 for (i
= 0; i
< ARRAY_SIZE(codec_format_map
); i
++)
3042 if (stream
->formats
& codec_format_map
[i
])
3043 stream
->formats
|= codec_format_map
[i
];
3046 static int snd_soc_codec_drv_probe(struct snd_soc_component
*component
)
3048 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
3050 return codec
->driver
->probe(codec
);
3053 static void snd_soc_codec_drv_remove(struct snd_soc_component
*component
)
3055 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
3057 codec
->driver
->remove(codec
);
3060 static int snd_soc_codec_drv_write(struct snd_soc_component
*component
,
3061 unsigned int reg
, unsigned int val
)
3063 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
3065 return codec
->driver
->write(codec
, reg
, val
);
3068 static int snd_soc_codec_drv_read(struct snd_soc_component
*component
,
3069 unsigned int reg
, unsigned int *val
)
3071 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
3073 *val
= codec
->driver
->read(codec
, reg
);
3078 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context
*dapm
,
3079 enum snd_soc_bias_level level
)
3081 struct snd_soc_codec
*codec
= snd_soc_dapm_to_codec(dapm
);
3083 return codec
->driver
->set_bias_level(codec
, level
);
3087 * snd_soc_register_codec - Register a codec with the ASoC core
3089 * @dev: The parent device for this codec
3090 * @codec_drv: Codec driver
3091 * @dai_drv: The associated DAI driver
3092 * @num_dai: Number of DAIs
3094 int snd_soc_register_codec(struct device
*dev
,
3095 const struct snd_soc_codec_driver
*codec_drv
,
3096 struct snd_soc_dai_driver
*dai_drv
,
3099 struct snd_soc_dapm_context
*dapm
;
3100 struct snd_soc_codec
*codec
;
3101 struct snd_soc_dai
*dai
;
3104 dev_dbg(dev
, "codec register %s\n", dev_name(dev
));
3106 codec
= kzalloc(sizeof(struct snd_soc_codec
), GFP_KERNEL
);
3110 codec
->component
.codec
= codec
;
3112 ret
= snd_soc_component_initialize(&codec
->component
,
3113 &codec_drv
->component_driver
, dev
);
3117 if (codec_drv
->controls
) {
3118 codec
->component
.controls
= codec_drv
->controls
;
3119 codec
->component
.num_controls
= codec_drv
->num_controls
;
3121 if (codec_drv
->dapm_widgets
) {
3122 codec
->component
.dapm_widgets
= codec_drv
->dapm_widgets
;
3123 codec
->component
.num_dapm_widgets
= codec_drv
->num_dapm_widgets
;
3125 if (codec_drv
->dapm_routes
) {
3126 codec
->component
.dapm_routes
= codec_drv
->dapm_routes
;
3127 codec
->component
.num_dapm_routes
= codec_drv
->num_dapm_routes
;
3130 if (codec_drv
->probe
)
3131 codec
->component
.probe
= snd_soc_codec_drv_probe
;
3132 if (codec_drv
->remove
)
3133 codec
->component
.remove
= snd_soc_codec_drv_remove
;
3134 if (codec_drv
->write
)
3135 codec
->component
.write
= snd_soc_codec_drv_write
;
3136 if (codec_drv
->read
)
3137 codec
->component
.read
= snd_soc_codec_drv_read
;
3138 codec
->component
.ignore_pmdown_time
= codec_drv
->ignore_pmdown_time
;
3140 dapm
= snd_soc_codec_get_dapm(codec
);
3141 dapm
->idle_bias_off
= codec_drv
->idle_bias_off
;
3142 dapm
->suspend_bias_off
= codec_drv
->suspend_bias_off
;
3143 if (codec_drv
->seq_notifier
)
3144 dapm
->seq_notifier
= codec_drv
->seq_notifier
;
3145 if (codec_drv
->set_bias_level
)
3146 dapm
->set_bias_level
= snd_soc_codec_set_bias_level
;
3148 codec
->driver
= codec_drv
;
3149 codec
->component
.val_bytes
= codec_drv
->reg_word_size
;
3151 #ifdef CONFIG_DEBUG_FS
3152 codec
->component
.init_debugfs
= soc_init_codec_debugfs
;
3153 codec
->component
.debugfs_prefix
= "codec";
3156 if (codec_drv
->get_regmap
)
3157 codec
->component
.regmap
= codec_drv
->get_regmap(dev
);
3159 for (i
= 0; i
< num_dai
; i
++) {
3160 fixup_codec_formats(&dai_drv
[i
].playback
);
3161 fixup_codec_formats(&dai_drv
[i
].capture
);
3164 ret
= snd_soc_register_dais(&codec
->component
, dai_drv
, num_dai
, false);
3166 dev_err(dev
, "ASoC: Failed to register DAIs: %d\n", ret
);
3170 list_for_each_entry(dai
, &codec
->component
.dai_list
, list
)
3173 mutex_lock(&client_mutex
);
3174 snd_soc_component_add_unlocked(&codec
->component
);
3175 list_add(&codec
->list
, &codec_list
);
3176 mutex_unlock(&client_mutex
);
3178 dev_dbg(codec
->dev
, "ASoC: Registered codec '%s'\n",
3179 codec
->component
.name
);
3183 snd_soc_component_cleanup(&codec
->component
);
3188 EXPORT_SYMBOL_GPL(snd_soc_register_codec
);
3191 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3193 * @dev: codec to unregister
3195 void snd_soc_unregister_codec(struct device
*dev
)
3197 struct snd_soc_codec
*codec
;
3199 mutex_lock(&client_mutex
);
3200 list_for_each_entry(codec
, &codec_list
, list
) {
3201 if (dev
== codec
->dev
)
3204 mutex_unlock(&client_mutex
);
3208 list_del(&codec
->list
);
3209 snd_soc_component_del_unlocked(&codec
->component
);
3210 mutex_unlock(&client_mutex
);
3212 dev_dbg(codec
->dev
, "ASoC: Unregistered codec '%s'\n",
3213 codec
->component
.name
);
3215 snd_soc_component_cleanup(&codec
->component
);
3216 snd_soc_cache_exit(codec
);
3219 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec
);
3221 /* Retrieve a card's name from device tree */
3222 int snd_soc_of_parse_card_name(struct snd_soc_card
*card
,
3223 const char *propname
)
3225 struct device_node
*np
;
3229 pr_err("card->dev is not set before calling %s\n", __func__
);
3233 np
= card
->dev
->of_node
;
3235 ret
= of_property_read_string_index(np
, propname
, 0, &card
->name
);
3237 * EINVAL means the property does not exist. This is fine providing
3238 * card->name was previously set, which is checked later in
3239 * snd_soc_register_card.
3241 if (ret
< 0 && ret
!= -EINVAL
) {
3243 "ASoC: Property '%s' could not be read: %d\n",
3250 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name
);
3252 static const struct snd_soc_dapm_widget simple_widgets
[] = {
3253 SND_SOC_DAPM_MIC("Microphone", NULL
),
3254 SND_SOC_DAPM_LINE("Line", NULL
),
3255 SND_SOC_DAPM_HP("Headphone", NULL
),
3256 SND_SOC_DAPM_SPK("Speaker", NULL
),
3259 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card
*card
,
3260 const char *propname
)
3262 struct device_node
*np
= card
->dev
->of_node
;
3263 struct snd_soc_dapm_widget
*widgets
;
3264 const char *template, *wname
;
3265 int i
, j
, num_widgets
, ret
;
3267 num_widgets
= of_property_count_strings(np
, propname
);
3268 if (num_widgets
< 0) {
3270 "ASoC: Property '%s' does not exist\n", propname
);
3273 if (num_widgets
& 1) {
3275 "ASoC: Property '%s' length is not even\n", propname
);
3281 dev_err(card
->dev
, "ASoC: Property '%s's length is zero\n",
3286 widgets
= devm_kcalloc(card
->dev
, num_widgets
, sizeof(*widgets
),
3290 "ASoC: Could not allocate memory for widgets\n");
3294 for (i
= 0; i
< num_widgets
; i
++) {
3295 ret
= of_property_read_string_index(np
, propname
,
3299 "ASoC: Property '%s' index %d read error:%d\n",
3300 propname
, 2 * i
, ret
);
3304 for (j
= 0; j
< ARRAY_SIZE(simple_widgets
); j
++) {
3305 if (!strncmp(template, simple_widgets
[j
].name
,
3306 strlen(simple_widgets
[j
].name
))) {
3307 widgets
[i
] = simple_widgets
[j
];
3312 if (j
>= ARRAY_SIZE(simple_widgets
)) {
3314 "ASoC: DAPM widget '%s' is not supported\n",
3319 ret
= of_property_read_string_index(np
, propname
,
3324 "ASoC: Property '%s' index %d read error:%d\n",
3325 propname
, (2 * i
) + 1, ret
);
3329 widgets
[i
].name
= wname
;
3332 card
->of_dapm_widgets
= widgets
;
3333 card
->num_of_dapm_widgets
= num_widgets
;
3337 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets
);
3339 static int snd_soc_of_get_slot_mask(struct device_node
*np
,
3340 const char *prop_name
,
3344 const __be32
*of_slot_mask
= of_get_property(np
, prop_name
, &val
);
3350 for (i
= 0; i
< val
; i
++)
3351 if (be32_to_cpup(&of_slot_mask
[i
]))
3357 int snd_soc_of_parse_tdm_slot(struct device_node
*np
,
3358 unsigned int *tx_mask
,
3359 unsigned int *rx_mask
,
3360 unsigned int *slots
,
3361 unsigned int *slot_width
)
3367 snd_soc_of_get_slot_mask(np
, "dai-tdm-slot-tx-mask", tx_mask
);
3369 snd_soc_of_get_slot_mask(np
, "dai-tdm-slot-rx-mask", rx_mask
);
3371 if (of_property_read_bool(np
, "dai-tdm-slot-num")) {
3372 ret
= of_property_read_u32(np
, "dai-tdm-slot-num", &val
);
3380 if (of_property_read_bool(np
, "dai-tdm-slot-width")) {
3381 ret
= of_property_read_u32(np
, "dai-tdm-slot-width", &val
);
3391 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot
);
3393 void snd_soc_of_parse_audio_prefix(struct snd_soc_card
*card
,
3394 struct snd_soc_codec_conf
*codec_conf
,
3395 struct device_node
*of_node
,
3396 const char *propname
)
3398 struct device_node
*np
= card
->dev
->of_node
;
3402 ret
= of_property_read_string(np
, propname
, &str
);
3404 /* no prefix is not error */
3408 codec_conf
->of_node
= of_node
;
3409 codec_conf
->name_prefix
= str
;
3411 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix
);
3413 int snd_soc_of_parse_audio_routing(struct snd_soc_card
*card
,
3414 const char *propname
)
3416 struct device_node
*np
= card
->dev
->of_node
;
3418 struct snd_soc_dapm_route
*routes
;
3421 num_routes
= of_property_count_strings(np
, propname
);
3422 if (num_routes
< 0 || num_routes
& 1) {
3424 "ASoC: Property '%s' does not exist or its length is not even\n",
3430 dev_err(card
->dev
, "ASoC: Property '%s's length is zero\n",
3435 routes
= devm_kzalloc(card
->dev
, num_routes
* sizeof(*routes
),
3439 "ASoC: Could not allocate DAPM route table\n");
3443 for (i
= 0; i
< num_routes
; i
++) {
3444 ret
= of_property_read_string_index(np
, propname
,
3445 2 * i
, &routes
[i
].sink
);
3448 "ASoC: Property '%s' index %d could not be read: %d\n",
3449 propname
, 2 * i
, ret
);
3452 ret
= of_property_read_string_index(np
, propname
,
3453 (2 * i
) + 1, &routes
[i
].source
);
3456 "ASoC: Property '%s' index %d could not be read: %d\n",
3457 propname
, (2 * i
) + 1, ret
);
3462 card
->num_of_dapm_routes
= num_routes
;
3463 card
->of_dapm_routes
= routes
;
3467 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing
);
3469 unsigned int snd_soc_of_parse_daifmt(struct device_node
*np
,
3471 struct device_node
**bitclkmaster
,
3472 struct device_node
**framemaster
)
3476 unsigned int format
= 0;
3482 } of_fmt_table
[] = {
3483 { "i2s", SND_SOC_DAIFMT_I2S
},
3484 { "right_j", SND_SOC_DAIFMT_RIGHT_J
},
3485 { "left_j", SND_SOC_DAIFMT_LEFT_J
},
3486 { "dsp_a", SND_SOC_DAIFMT_DSP_A
},
3487 { "dsp_b", SND_SOC_DAIFMT_DSP_B
},
3488 { "ac97", SND_SOC_DAIFMT_AC97
},
3489 { "pdm", SND_SOC_DAIFMT_PDM
},
3490 { "msb", SND_SOC_DAIFMT_MSB
},
3491 { "lsb", SND_SOC_DAIFMT_LSB
},
3498 * check "[prefix]format = xxx"
3499 * SND_SOC_DAIFMT_FORMAT_MASK area
3501 snprintf(prop
, sizeof(prop
), "%sformat", prefix
);
3502 ret
= of_property_read_string(np
, prop
, &str
);
3504 for (i
= 0; i
< ARRAY_SIZE(of_fmt_table
); i
++) {
3505 if (strcmp(str
, of_fmt_table
[i
].name
) == 0) {
3506 format
|= of_fmt_table
[i
].val
;
3513 * check "[prefix]continuous-clock"
3514 * SND_SOC_DAIFMT_CLOCK_MASK area
3516 snprintf(prop
, sizeof(prop
), "%scontinuous-clock", prefix
);
3517 if (of_get_property(np
, prop
, NULL
))
3518 format
|= SND_SOC_DAIFMT_CONT
;
3520 format
|= SND_SOC_DAIFMT_GATED
;
3523 * check "[prefix]bitclock-inversion"
3524 * check "[prefix]frame-inversion"
3525 * SND_SOC_DAIFMT_INV_MASK area
3527 snprintf(prop
, sizeof(prop
), "%sbitclock-inversion", prefix
);
3528 bit
= !!of_get_property(np
, prop
, NULL
);
3530 snprintf(prop
, sizeof(prop
), "%sframe-inversion", prefix
);
3531 frame
= !!of_get_property(np
, prop
, NULL
);
3533 switch ((bit
<< 4) + frame
) {
3535 format
|= SND_SOC_DAIFMT_IB_IF
;
3538 format
|= SND_SOC_DAIFMT_IB_NF
;
3541 format
|= SND_SOC_DAIFMT_NB_IF
;
3544 /* SND_SOC_DAIFMT_NB_NF is default */
3549 * check "[prefix]bitclock-master"
3550 * check "[prefix]frame-master"
3551 * SND_SOC_DAIFMT_MASTER_MASK area
3553 snprintf(prop
, sizeof(prop
), "%sbitclock-master", prefix
);
3554 bit
= !!of_get_property(np
, prop
, NULL
);
3555 if (bit
&& bitclkmaster
)
3556 *bitclkmaster
= of_parse_phandle(np
, prop
, 0);
3558 snprintf(prop
, sizeof(prop
), "%sframe-master", prefix
);
3559 frame
= !!of_get_property(np
, prop
, NULL
);
3560 if (frame
&& framemaster
)
3561 *framemaster
= of_parse_phandle(np
, prop
, 0);
3563 switch ((bit
<< 4) + frame
) {
3565 format
|= SND_SOC_DAIFMT_CBM_CFM
;
3568 format
|= SND_SOC_DAIFMT_CBM_CFS
;
3571 format
|= SND_SOC_DAIFMT_CBS_CFM
;
3574 format
|= SND_SOC_DAIFMT_CBS_CFS
;
3580 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt
);
3582 static int snd_soc_get_dai_name(struct of_phandle_args
*args
,
3583 const char **dai_name
)
3585 struct snd_soc_component
*pos
;
3586 struct device_node
*component_of_node
;
3587 int ret
= -EPROBE_DEFER
;
3589 mutex_lock(&client_mutex
);
3590 list_for_each_entry(pos
, &component_list
, list
) {
3591 component_of_node
= pos
->dev
->of_node
;
3592 if (!component_of_node
&& pos
->dev
->parent
)
3593 component_of_node
= pos
->dev
->parent
->of_node
;
3595 if (component_of_node
!= args
->np
)
3598 if (pos
->driver
->of_xlate_dai_name
) {
3599 ret
= pos
->driver
->of_xlate_dai_name(pos
,
3605 switch (args
->args_count
) {
3607 id
= 0; /* same as dai_drv[0] */
3617 if (id
< 0 || id
>= pos
->num_dai
) {
3624 *dai_name
= pos
->dai_drv
[id
].name
;
3626 *dai_name
= pos
->name
;
3631 mutex_unlock(&client_mutex
);
3635 int snd_soc_of_get_dai_name(struct device_node
*of_node
,
3636 const char **dai_name
)
3638 struct of_phandle_args args
;
3641 ret
= of_parse_phandle_with_args(of_node
, "sound-dai",
3642 "#sound-dai-cells", 0, &args
);
3646 ret
= snd_soc_get_dai_name(&args
, dai_name
);
3648 of_node_put(args
.np
);
3652 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name
);
3655 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3657 * @of_node: Device node
3658 * @dai_link: DAI link
3660 * Builds an array of CODEC DAI components from the DAI link property
3662 * The array is set in the DAI link and the number of DAIs is set accordingly.
3663 * The device nodes in the array (of_node) must be dereferenced by the caller.
3665 * Returns 0 for success
3667 int snd_soc_of_get_dai_link_codecs(struct device
*dev
,
3668 struct device_node
*of_node
,
3669 struct snd_soc_dai_link
*dai_link
)
3671 struct of_phandle_args args
;
3672 struct snd_soc_dai_link_component
*component
;
3674 int index
, num_codecs
, ret
;
3676 /* Count the number of CODECs */
3678 num_codecs
= of_count_phandle_with_args(of_node
, name
,
3679 "#sound-dai-cells");
3680 if (num_codecs
<= 0) {
3681 if (num_codecs
== -ENOENT
)
3682 dev_err(dev
, "No 'sound-dai' property\n");
3684 dev_err(dev
, "Bad phandle in 'sound-dai'\n");
3687 component
= devm_kzalloc(dev
,
3688 sizeof *component
* num_codecs
,
3692 dai_link
->codecs
= component
;
3693 dai_link
->num_codecs
= num_codecs
;
3695 /* Parse the list */
3696 for (index
= 0, component
= dai_link
->codecs
;
3697 index
< dai_link
->num_codecs
;
3698 index
++, component
++) {
3699 ret
= of_parse_phandle_with_args(of_node
, name
,
3704 component
->of_node
= args
.np
;
3705 ret
= snd_soc_get_dai_name(&args
, &component
->dai_name
);
3711 for (index
= 0, component
= dai_link
->codecs
;
3712 index
< dai_link
->num_codecs
;
3713 index
++, component
++) {
3714 if (!component
->of_node
)
3716 of_node_put(component
->of_node
);
3717 component
->of_node
= NULL
;
3719 dai_link
->codecs
= NULL
;
3720 dai_link
->num_codecs
= 0;
3723 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs
);
3725 static int __init
snd_soc_init(void)
3727 snd_soc_debugfs_init();
3728 snd_soc_util_init();
3730 return platform_driver_register(&soc_driver
);
3732 module_init(snd_soc_init
);
3734 static void __exit
snd_soc_exit(void)
3736 snd_soc_util_exit();
3737 snd_soc_debugfs_exit();
3739 #ifdef CONFIG_DEBUG_FS
3741 platform_driver_unregister(&soc_driver
);
3743 module_exit(snd_soc_exit
);
3745 /* Module information */
3746 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3747 MODULE_DESCRIPTION("ALSA SoC Core");
3748 MODULE_LICENSE("GPL");
3749 MODULE_ALIAS("platform:soc-audio");