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/initval.h>
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/asoc.h>
50 #ifdef CONFIG_DEBUG_FS
51 struct dentry
*snd_soc_debugfs_root
;
52 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root
);
55 static DEFINE_MUTEX(client_mutex
);
56 static LIST_HEAD(platform_list
);
57 static LIST_HEAD(codec_list
);
58 static LIST_HEAD(component_list
);
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
65 static int pmdown_time
= 5000;
66 module_param(pmdown_time
, int, 0);
67 MODULE_PARM_DESC(pmdown_time
, "DAPM stream powerdown time (msecs)");
69 /* returns the minimum number of bytes needed to represent
70 * a particular given value */
71 static int min_bytes_needed(unsigned long val
)
76 for (i
= (sizeof val
* 8) - 1; i
>= 0; --i
, ++c
)
79 c
= (sizeof val
* 8) - c
;
87 /* fill buf which is 'len' bytes with a formatted
88 * string of the form 'reg: value\n' */
89 static int format_register_str(struct snd_soc_codec
*codec
,
90 unsigned int reg
, char *buf
, size_t len
)
92 int wordsize
= min_bytes_needed(codec
->driver
->reg_cache_size
) * 2;
93 int regsize
= codec
->driver
->reg_word_size
* 2;
96 /* +2 for ': ' and + 1 for '\n' */
97 if (wordsize
+ regsize
+ 2 + 1 != len
)
100 sprintf(buf
, "%.*x: ", wordsize
, reg
);
103 ret
= snd_soc_read(codec
, reg
);
105 memset(buf
, 'X', regsize
);
107 sprintf(buf
, "%.*x", regsize
, ret
);
109 /* no NUL-termination needed */
113 /* codec register dump */
114 static ssize_t
soc_codec_reg_show(struct snd_soc_codec
*codec
, char *buf
,
115 size_t count
, loff_t pos
)
118 int wordsize
, regsize
;
123 wordsize
= min_bytes_needed(codec
->driver
->reg_cache_size
) * 2;
124 regsize
= codec
->driver
->reg_word_size
* 2;
126 len
= wordsize
+ regsize
+ 2 + 1;
128 if (!codec
->driver
->reg_cache_size
)
131 if (codec
->driver
->reg_cache_step
)
132 step
= codec
->driver
->reg_cache_step
;
134 for (i
= 0; i
< codec
->driver
->reg_cache_size
; i
+= step
) {
135 /* only support larger than PAGE_SIZE bytes debugfs
136 * entries for the default case */
138 if (total
+ len
>= count
- 1)
140 format_register_str(codec
, i
, buf
+ total
, len
);
146 total
= min(total
, count
- 1);
151 static ssize_t
codec_reg_show(struct device
*dev
,
152 struct device_attribute
*attr
, char *buf
)
154 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
156 return soc_codec_reg_show(rtd
->codec
, buf
, PAGE_SIZE
, 0);
159 static DEVICE_ATTR(codec_reg
, 0444, codec_reg_show
, NULL
);
161 static ssize_t
pmdown_time_show(struct device
*dev
,
162 struct device_attribute
*attr
, char *buf
)
164 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
166 return sprintf(buf
, "%ld\n", rtd
->pmdown_time
);
169 static ssize_t
pmdown_time_set(struct device
*dev
,
170 struct device_attribute
*attr
,
171 const char *buf
, size_t count
)
173 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
176 ret
= kstrtol(buf
, 10, &rtd
->pmdown_time
);
183 static DEVICE_ATTR(pmdown_time
, 0644, pmdown_time_show
, pmdown_time_set
);
185 static struct attribute
*soc_dev_attrs
[] = {
186 &dev_attr_codec_reg
.attr
,
187 &dev_attr_pmdown_time
.attr
,
191 static umode_t
soc_dev_attr_is_visible(struct kobject
*kobj
,
192 struct attribute
*attr
, int idx
)
194 struct device
*dev
= kobj_to_dev(kobj
);
195 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
197 if (attr
== &dev_attr_pmdown_time
.attr
)
198 return attr
->mode
; /* always visible */
199 return rtd
->codec
? attr
->mode
: 0; /* enabled only with codec */
202 static const struct attribute_group soc_dapm_dev_group
= {
203 .attrs
= soc_dapm_dev_attrs
,
204 .is_visible
= soc_dev_attr_is_visible
,
207 static const struct attribute_group soc_dev_roup
= {
208 .attrs
= soc_dev_attrs
,
209 .is_visible
= soc_dev_attr_is_visible
,
212 static const struct attribute_group
*soc_dev_attr_groups
[] = {
218 #ifdef CONFIG_DEBUG_FS
219 static ssize_t
codec_reg_read_file(struct file
*file
, char __user
*user_buf
,
220 size_t count
, loff_t
*ppos
)
223 struct snd_soc_codec
*codec
= file
->private_data
;
226 if (*ppos
< 0 || !count
)
229 buf
= kmalloc(count
, GFP_KERNEL
);
233 ret
= soc_codec_reg_show(codec
, buf
, count
, *ppos
);
235 if (copy_to_user(user_buf
, buf
, ret
)) {
246 static ssize_t
codec_reg_write_file(struct file
*file
,
247 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
252 unsigned long reg
, value
;
253 struct snd_soc_codec
*codec
= file
->private_data
;
256 buf_size
= min(count
, (sizeof(buf
)-1));
257 if (copy_from_user(buf
, user_buf
, buf_size
))
261 while (*start
== ' ')
263 reg
= simple_strtoul(start
, &start
, 16);
264 while (*start
== ' ')
266 ret
= kstrtoul(start
, 16, &value
);
270 /* Userspace has been fiddling around behind the kernel's back */
271 add_taint(TAINT_USER
, LOCKDEP_NOW_UNRELIABLE
);
273 snd_soc_write(codec
, reg
, value
);
277 static const struct file_operations codec_reg_fops
= {
279 .read
= codec_reg_read_file
,
280 .write
= codec_reg_write_file
,
281 .llseek
= default_llseek
,
284 static void soc_init_component_debugfs(struct snd_soc_component
*component
)
286 if (!component
->card
->debugfs_card_root
)
289 if (component
->debugfs_prefix
) {
292 name
= kasprintf(GFP_KERNEL
, "%s:%s",
293 component
->debugfs_prefix
, component
->name
);
295 component
->debugfs_root
= debugfs_create_dir(name
,
296 component
->card
->debugfs_card_root
);
300 component
->debugfs_root
= debugfs_create_dir(component
->name
,
301 component
->card
->debugfs_card_root
);
304 if (!component
->debugfs_root
) {
305 dev_warn(component
->dev
,
306 "ASoC: Failed to create component debugfs directory\n");
310 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component
),
311 component
->debugfs_root
);
313 if (component
->init_debugfs
)
314 component
->init_debugfs(component
);
317 static void soc_cleanup_component_debugfs(struct snd_soc_component
*component
)
319 debugfs_remove_recursive(component
->debugfs_root
);
322 static void soc_init_codec_debugfs(struct snd_soc_component
*component
)
324 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
326 codec
->debugfs_reg
= debugfs_create_file("codec_reg", 0644,
327 codec
->component
.debugfs_root
,
328 codec
, &codec_reg_fops
);
329 if (!codec
->debugfs_reg
)
331 "ASoC: Failed to create codec register debugfs file\n");
334 static ssize_t
codec_list_read_file(struct file
*file
, char __user
*user_buf
,
335 size_t count
, loff_t
*ppos
)
337 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
338 ssize_t len
, ret
= 0;
339 struct snd_soc_codec
*codec
;
344 mutex_lock(&client_mutex
);
346 list_for_each_entry(codec
, &codec_list
, list
) {
347 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
348 codec
->component
.name
);
351 if (ret
> PAGE_SIZE
) {
357 mutex_unlock(&client_mutex
);
360 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
367 static const struct file_operations codec_list_fops
= {
368 .read
= codec_list_read_file
,
369 .llseek
= default_llseek
,/* read accesses f_pos */
372 static ssize_t
dai_list_read_file(struct file
*file
, char __user
*user_buf
,
373 size_t count
, loff_t
*ppos
)
375 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
376 ssize_t len
, ret
= 0;
377 struct snd_soc_component
*component
;
378 struct snd_soc_dai
*dai
;
383 mutex_lock(&client_mutex
);
385 list_for_each_entry(component
, &component_list
, list
) {
386 list_for_each_entry(dai
, &component
->dai_list
, list
) {
387 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
391 if (ret
> PAGE_SIZE
) {
398 mutex_unlock(&client_mutex
);
400 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
407 static const struct file_operations dai_list_fops
= {
408 .read
= dai_list_read_file
,
409 .llseek
= default_llseek
,/* read accesses f_pos */
412 static ssize_t
platform_list_read_file(struct file
*file
,
413 char __user
*user_buf
,
414 size_t count
, loff_t
*ppos
)
416 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
417 ssize_t len
, ret
= 0;
418 struct snd_soc_platform
*platform
;
423 mutex_lock(&client_mutex
);
425 list_for_each_entry(platform
, &platform_list
, list
) {
426 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
427 platform
->component
.name
);
430 if (ret
> PAGE_SIZE
) {
436 mutex_unlock(&client_mutex
);
438 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
445 static const struct file_operations platform_list_fops
= {
446 .read
= platform_list_read_file
,
447 .llseek
= default_llseek
,/* read accesses f_pos */
450 static void soc_init_card_debugfs(struct snd_soc_card
*card
)
452 if (!snd_soc_debugfs_root
)
455 card
->debugfs_card_root
= debugfs_create_dir(card
->name
,
456 snd_soc_debugfs_root
);
457 if (!card
->debugfs_card_root
) {
459 "ASoC: Failed to create card debugfs directory\n");
463 card
->debugfs_pop_time
= debugfs_create_u32("dapm_pop_time", 0644,
464 card
->debugfs_card_root
,
466 if (!card
->debugfs_pop_time
)
468 "ASoC: Failed to create pop time debugfs file\n");
471 static void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
473 debugfs_remove_recursive(card
->debugfs_card_root
);
477 static void snd_soc_debugfs_init(void)
479 snd_soc_debugfs_root
= debugfs_create_dir("asoc", NULL
);
480 if (IS_ERR(snd_soc_debugfs_root
) || !snd_soc_debugfs_root
) {
481 pr_warn("ASoC: Failed to create debugfs directory\n");
482 snd_soc_debugfs_root
= NULL
;
486 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root
, NULL
,
488 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
490 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root
, NULL
,
492 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
494 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root
, NULL
,
495 &platform_list_fops
))
496 pr_warn("ASoC: Failed to create platform list debugfs file\n");
499 static void snd_soc_debugfs_exit(void)
501 debugfs_remove_recursive(snd_soc_debugfs_root
);
506 #define soc_init_codec_debugfs NULL
508 static inline void soc_init_component_debugfs(
509 struct snd_soc_component
*component
)
513 static inline void soc_cleanup_component_debugfs(
514 struct snd_soc_component
*component
)
518 static inline void soc_init_card_debugfs(struct snd_soc_card
*card
)
522 static inline void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
526 static inline void snd_soc_debugfs_init(void)
530 static inline void snd_soc_debugfs_exit(void)
536 struct snd_pcm_substream
*snd_soc_get_dai_substream(struct snd_soc_card
*card
,
537 const char *dai_link
, int stream
)
541 for (i
= 0; i
< card
->num_links
; i
++) {
542 if (card
->rtd
[i
].dai_link
->no_pcm
&&
543 !strcmp(card
->rtd
[i
].dai_link
->name
, dai_link
))
544 return card
->rtd
[i
].pcm
->streams
[stream
].substream
;
546 dev_dbg(card
->dev
, "ASoC: failed to find dai link %s\n", dai_link
);
549 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream
);
551 struct snd_soc_pcm_runtime
*snd_soc_get_pcm_runtime(struct snd_soc_card
*card
,
552 const char *dai_link
)
556 for (i
= 0; i
< card
->num_links
; i
++) {
557 if (!strcmp(card
->rtd
[i
].dai_link
->name
, dai_link
))
558 return &card
->rtd
[i
];
560 dev_dbg(card
->dev
, "ASoC: failed to find rtd %s\n", dai_link
);
563 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime
);
565 static void codec2codec_close_delayed_work(struct work_struct
*work
)
567 /* Currently nothing to do for c2c links
568 * Since c2c links are internal nodes in the DAPM graph and
569 * don't interface with the outside world or application layer
570 * we don't have to do any special handling on close.
574 #ifdef CONFIG_PM_SLEEP
575 /* powers down audio subsystem for suspend */
576 int snd_soc_suspend(struct device
*dev
)
578 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
579 struct snd_soc_codec
*codec
;
582 /* If the card is not initialized yet there is nothing to do */
583 if (!card
->instantiated
)
586 /* Due to the resume being scheduled into a workqueue we could
587 * suspend before that's finished - wait for it to complete.
589 snd_power_lock(card
->snd_card
);
590 snd_power_wait(card
->snd_card
, SNDRV_CTL_POWER_D0
);
591 snd_power_unlock(card
->snd_card
);
593 /* we're going to block userspace touching us until resume completes */
594 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D3hot
);
596 /* mute any active DACs */
597 for (i
= 0; i
< card
->num_rtd
; i
++) {
599 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
602 for (j
= 0; j
< card
->rtd
[i
].num_codecs
; j
++) {
603 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dais
[j
];
604 struct snd_soc_dai_driver
*drv
= dai
->driver
;
606 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
607 drv
->ops
->digital_mute(dai
, 1);
611 /* suspend all pcms */
612 for (i
= 0; i
< card
->num_rtd
; i
++) {
613 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
616 snd_pcm_suspend_all(card
->rtd
[i
].pcm
);
619 if (card
->suspend_pre
)
620 card
->suspend_pre(card
);
622 for (i
= 0; i
< card
->num_rtd
; i
++) {
623 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
625 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
628 if (cpu_dai
->driver
->suspend
&& !cpu_dai
->driver
->bus_control
)
629 cpu_dai
->driver
->suspend(cpu_dai
);
632 /* close any waiting streams */
633 for (i
= 0; i
< card
->num_rtd
; i
++)
634 flush_delayed_work(&card
->rtd
[i
].delayed_work
);
636 for (i
= 0; i
< card
->num_rtd
; i
++) {
638 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
641 snd_soc_dapm_stream_event(&card
->rtd
[i
],
642 SNDRV_PCM_STREAM_PLAYBACK
,
643 SND_SOC_DAPM_STREAM_SUSPEND
);
645 snd_soc_dapm_stream_event(&card
->rtd
[i
],
646 SNDRV_PCM_STREAM_CAPTURE
,
647 SND_SOC_DAPM_STREAM_SUSPEND
);
650 /* Recheck all endpoints too, their state is affected by suspend */
651 dapm_mark_endpoints_dirty(card
);
652 snd_soc_dapm_sync(&card
->dapm
);
654 /* suspend all CODECs */
655 list_for_each_entry(codec
, &card
->codec_dev_list
, card_list
) {
656 /* If there are paths active then the CODEC will be held with
657 * bias _ON and should not be suspended. */
658 if (!codec
->suspended
) {
659 switch (codec
->dapm
.bias_level
) {
660 case SND_SOC_BIAS_STANDBY
:
662 * If the CODEC is capable of idle
663 * bias off then being in STANDBY
664 * means it's doing something,
665 * otherwise fall through.
667 if (codec
->dapm
.idle_bias_off
) {
669 "ASoC: idle_bias_off CODEC on over suspend\n");
673 case SND_SOC_BIAS_OFF
:
674 if (codec
->driver
->suspend
)
675 codec
->driver
->suspend(codec
);
676 codec
->suspended
= 1;
677 if (codec
->component
.regmap
)
678 regcache_mark_dirty(codec
->component
.regmap
);
679 /* deactivate pins to sleep state */
680 pinctrl_pm_select_sleep_state(codec
->dev
);
684 "ASoC: CODEC is on over suspend\n");
690 for (i
= 0; i
< card
->num_rtd
; i
++) {
691 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
693 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
696 if (cpu_dai
->driver
->suspend
&& cpu_dai
->driver
->bus_control
)
697 cpu_dai
->driver
->suspend(cpu_dai
);
699 /* deactivate pins to sleep state */
700 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
703 if (card
->suspend_post
)
704 card
->suspend_post(card
);
708 EXPORT_SYMBOL_GPL(snd_soc_suspend
);
710 /* deferred resume work, so resume can complete before we finished
711 * setting our codec back up, which can be very slow on I2C
713 static void soc_resume_deferred(struct work_struct
*work
)
715 struct snd_soc_card
*card
=
716 container_of(work
, struct snd_soc_card
, deferred_resume_work
);
717 struct snd_soc_codec
*codec
;
720 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
721 * so userspace apps are blocked from touching us
724 dev_dbg(card
->dev
, "ASoC: starting resume work\n");
726 /* Bring us up into D2 so that DAPM starts enabling things */
727 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D2
);
729 if (card
->resume_pre
)
730 card
->resume_pre(card
);
732 /* resume control bus DAIs */
733 for (i
= 0; i
< card
->num_rtd
; i
++) {
734 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
736 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
739 if (cpu_dai
->driver
->resume
&& cpu_dai
->driver
->bus_control
)
740 cpu_dai
->driver
->resume(cpu_dai
);
743 list_for_each_entry(codec
, &card
->codec_dev_list
, card_list
) {
744 if (codec
->suspended
) {
745 if (codec
->driver
->resume
)
746 codec
->driver
->resume(codec
);
747 codec
->suspended
= 0;
751 for (i
= 0; i
< card
->num_rtd
; i
++) {
753 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
756 snd_soc_dapm_stream_event(&card
->rtd
[i
],
757 SNDRV_PCM_STREAM_PLAYBACK
,
758 SND_SOC_DAPM_STREAM_RESUME
);
760 snd_soc_dapm_stream_event(&card
->rtd
[i
],
761 SNDRV_PCM_STREAM_CAPTURE
,
762 SND_SOC_DAPM_STREAM_RESUME
);
765 /* unmute any active DACs */
766 for (i
= 0; i
< card
->num_rtd
; i
++) {
768 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
771 for (j
= 0; j
< card
->rtd
[i
].num_codecs
; j
++) {
772 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dais
[j
];
773 struct snd_soc_dai_driver
*drv
= dai
->driver
;
775 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
776 drv
->ops
->digital_mute(dai
, 0);
780 for (i
= 0; i
< card
->num_rtd
; i
++) {
781 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
783 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
786 if (cpu_dai
->driver
->resume
&& !cpu_dai
->driver
->bus_control
)
787 cpu_dai
->driver
->resume(cpu_dai
);
790 if (card
->resume_post
)
791 card
->resume_post(card
);
793 dev_dbg(card
->dev
, "ASoC: resume work completed\n");
795 /* userspace can access us now we are back as we were before */
796 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D0
);
798 /* Recheck all endpoints too, their state is affected by suspend */
799 dapm_mark_endpoints_dirty(card
);
800 snd_soc_dapm_sync(&card
->dapm
);
803 /* powers up audio subsystem after a suspend */
804 int snd_soc_resume(struct device
*dev
)
806 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
807 bool bus_control
= false;
810 /* If the card is not initialized yet there is nothing to do */
811 if (!card
->instantiated
)
814 /* activate pins from sleep state */
815 for (i
= 0; i
< card
->num_rtd
; i
++) {
816 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
817 struct snd_soc_dai
**codec_dais
= rtd
->codec_dais
;
818 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
822 pinctrl_pm_select_default_state(cpu_dai
->dev
);
824 for (j
= 0; j
< rtd
->num_codecs
; j
++) {
825 struct snd_soc_dai
*codec_dai
= codec_dais
[j
];
826 if (codec_dai
->active
)
827 pinctrl_pm_select_default_state(codec_dai
->dev
);
832 * DAIs that also act as the control bus master might have other drivers
833 * hanging off them so need to resume immediately. Other drivers don't
834 * have that problem and may take a substantial amount of time to resume
835 * due to I/O costs and anti-pop so handle them out of line.
837 for (i
= 0; i
< card
->num_rtd
; i
++) {
838 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
839 bus_control
|= cpu_dai
->driver
->bus_control
;
842 dev_dbg(dev
, "ASoC: Resuming control bus master immediately\n");
843 soc_resume_deferred(&card
->deferred_resume_work
);
845 dev_dbg(dev
, "ASoC: Scheduling resume work\n");
846 if (!schedule_work(&card
->deferred_resume_work
))
847 dev_err(dev
, "ASoC: resume work item may be lost\n");
852 EXPORT_SYMBOL_GPL(snd_soc_resume
);
854 #define snd_soc_suspend NULL
855 #define snd_soc_resume NULL
858 static const struct snd_soc_dai_ops null_dai_ops
= {
861 static struct snd_soc_component
*soc_find_component(
862 const struct device_node
*of_node
, const char *name
)
864 struct snd_soc_component
*component
;
866 lockdep_assert_held(&client_mutex
);
868 list_for_each_entry(component
, &component_list
, list
) {
870 if (component
->dev
->of_node
== of_node
)
872 } else if (strcmp(component
->name
, name
) == 0) {
880 static struct snd_soc_dai
*snd_soc_find_dai(
881 const struct snd_soc_dai_link_component
*dlc
)
883 struct snd_soc_component
*component
;
884 struct snd_soc_dai
*dai
;
885 struct device_node
*component_of_node
;
887 lockdep_assert_held(&client_mutex
);
889 /* Find CPU DAI from registered DAIs*/
890 list_for_each_entry(component
, &component_list
, list
) {
891 component_of_node
= component
->dev
->of_node
;
892 if (!component_of_node
&& component
->dev
->parent
)
893 component_of_node
= component
->dev
->parent
->of_node
;
895 if (dlc
->of_node
&& component_of_node
!= dlc
->of_node
)
897 if (dlc
->name
&& strcmp(component
->name
, dlc
->name
))
899 list_for_each_entry(dai
, &component
->dai_list
, list
) {
900 if (dlc
->dai_name
&& strcmp(dai
->name
, dlc
->dai_name
))
910 static int soc_bind_dai_link(struct snd_soc_card
*card
, int num
)
912 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
913 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
914 struct snd_soc_dai_link_component
*codecs
= dai_link
->codecs
;
915 struct snd_soc_dai_link_component cpu_dai_component
;
916 struct snd_soc_dai
**codec_dais
= rtd
->codec_dais
;
917 struct snd_soc_platform
*platform
;
918 const char *platform_name
;
921 dev_dbg(card
->dev
, "ASoC: binding %s at idx %d\n", dai_link
->name
, num
);
923 cpu_dai_component
.name
= dai_link
->cpu_name
;
924 cpu_dai_component
.of_node
= dai_link
->cpu_of_node
;
925 cpu_dai_component
.dai_name
= dai_link
->cpu_dai_name
;
926 rtd
->cpu_dai
= snd_soc_find_dai(&cpu_dai_component
);
928 dev_err(card
->dev
, "ASoC: CPU DAI %s not registered\n",
929 dai_link
->cpu_dai_name
);
930 return -EPROBE_DEFER
;
933 rtd
->num_codecs
= dai_link
->num_codecs
;
935 /* Find CODEC from registered CODECs */
936 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
937 codec_dais
[i
] = snd_soc_find_dai(&codecs
[i
]);
938 if (!codec_dais
[i
]) {
939 dev_err(card
->dev
, "ASoC: CODEC DAI %s not registered\n",
941 return -EPROBE_DEFER
;
945 /* Single codec links expect codec and codec_dai in runtime data */
946 rtd
->codec_dai
= codec_dais
[0];
947 rtd
->codec
= rtd
->codec_dai
->codec
;
949 /* if there's no platform we match on the empty platform */
950 platform_name
= dai_link
->platform_name
;
951 if (!platform_name
&& !dai_link
->platform_of_node
)
952 platform_name
= "snd-soc-dummy";
954 /* find one from the set of registered platforms */
955 list_for_each_entry(platform
, &platform_list
, list
) {
956 if (dai_link
->platform_of_node
) {
957 if (platform
->dev
->of_node
!=
958 dai_link
->platform_of_node
)
961 if (strcmp(platform
->component
.name
, platform_name
))
965 rtd
->platform
= platform
;
967 if (!rtd
->platform
) {
968 dev_err(card
->dev
, "ASoC: platform %s not registered\n",
969 dai_link
->platform_name
);
970 return -EPROBE_DEFER
;
978 static void soc_remove_component(struct snd_soc_component
*component
)
980 if (!component
->probed
)
983 /* This is a HACK and will be removed soon */
984 if (component
->codec
)
985 list_del(&component
->codec
->card_list
);
987 if (component
->remove
)
988 component
->remove(component
);
990 snd_soc_dapm_free(snd_soc_component_get_dapm(component
));
992 soc_cleanup_component_debugfs(component
);
993 component
->probed
= 0;
994 module_put(component
->dev
->driver
->owner
);
997 static void soc_remove_dai(struct snd_soc_dai
*dai
, int order
)
1001 if (dai
&& dai
->probed
&&
1002 dai
->driver
->remove_order
== order
) {
1003 if (dai
->driver
->remove
) {
1004 err
= dai
->driver
->remove(dai
);
1007 "ASoC: failed to remove %s: %d\n",
1014 static void soc_remove_link_dais(struct snd_soc_card
*card
, int num
, int order
)
1016 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1019 /* unregister the rtd device */
1020 if (rtd
->dev_registered
) {
1021 device_unregister(rtd
->dev
);
1022 rtd
->dev_registered
= 0;
1025 /* remove the CODEC DAI */
1026 for (i
= 0; i
< rtd
->num_codecs
; i
++)
1027 soc_remove_dai(rtd
->codec_dais
[i
], order
);
1029 soc_remove_dai(rtd
->cpu_dai
, order
);
1032 static void soc_remove_link_components(struct snd_soc_card
*card
, int num
,
1035 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1036 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1037 struct snd_soc_platform
*platform
= rtd
->platform
;
1038 struct snd_soc_component
*component
;
1041 /* remove the platform */
1042 if (platform
&& platform
->component
.driver
->remove_order
== order
)
1043 soc_remove_component(&platform
->component
);
1045 /* remove the CODEC-side CODEC */
1046 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1047 component
= rtd
->codec_dais
[i
]->component
;
1048 if (component
->driver
->remove_order
== order
)
1049 soc_remove_component(component
);
1052 /* remove any CPU-side CODEC */
1054 if (cpu_dai
->component
->driver
->remove_order
== order
)
1055 soc_remove_component(cpu_dai
->component
);
1059 static void soc_remove_dai_links(struct snd_soc_card
*card
)
1063 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1065 for (dai
= 0; dai
< card
->num_rtd
; dai
++)
1066 soc_remove_link_dais(card
, dai
, order
);
1069 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1071 for (dai
= 0; dai
< card
->num_rtd
; dai
++)
1072 soc_remove_link_components(card
, dai
, order
);
1078 static void soc_set_name_prefix(struct snd_soc_card
*card
,
1079 struct snd_soc_component
*component
)
1083 if (card
->codec_conf
== NULL
)
1086 for (i
= 0; i
< card
->num_configs
; i
++) {
1087 struct snd_soc_codec_conf
*map
= &card
->codec_conf
[i
];
1088 if (map
->of_node
&& component
->dev
->of_node
!= map
->of_node
)
1090 if (map
->dev_name
&& strcmp(component
->name
, map
->dev_name
))
1092 component
->name_prefix
= map
->name_prefix
;
1097 static int soc_probe_component(struct snd_soc_card
*card
,
1098 struct snd_soc_component
*component
)
1100 struct snd_soc_dapm_context
*dapm
= snd_soc_component_get_dapm(component
);
1101 struct snd_soc_dai
*dai
;
1104 if (component
->probed
)
1107 component
->card
= card
;
1109 soc_set_name_prefix(card
, component
);
1111 if (!try_module_get(component
->dev
->driver
->owner
))
1114 soc_init_component_debugfs(component
);
1116 if (component
->dapm_widgets
) {
1117 ret
= snd_soc_dapm_new_controls(dapm
, component
->dapm_widgets
,
1118 component
->num_dapm_widgets
);
1121 dev_err(component
->dev
,
1122 "Failed to create new controls %d\n", ret
);
1127 list_for_each_entry(dai
, &component
->dai_list
, list
) {
1128 ret
= snd_soc_dapm_new_dai_widgets(dapm
, dai
);
1130 dev_err(component
->dev
,
1131 "Failed to create DAI widgets %d\n", ret
);
1136 if (component
->probe
) {
1137 ret
= component
->probe(component
);
1139 dev_err(component
->dev
,
1140 "ASoC: failed to probe component %d\n", ret
);
1144 WARN(dapm
->idle_bias_off
&&
1145 dapm
->bias_level
!= SND_SOC_BIAS_OFF
,
1146 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1150 if (component
->controls
)
1151 snd_soc_add_component_controls(component
, component
->controls
,
1152 component
->num_controls
);
1153 if (component
->dapm_routes
)
1154 snd_soc_dapm_add_routes(dapm
, component
->dapm_routes
,
1155 component
->num_dapm_routes
);
1157 component
->probed
= 1;
1158 list_add(&dapm
->list
, &card
->dapm_list
);
1160 /* This is a HACK and will be removed soon */
1161 if (component
->codec
)
1162 list_add(&component
->codec
->card_list
, &card
->codec_dev_list
);
1167 soc_cleanup_component_debugfs(component
);
1168 module_put(component
->dev
->driver
->owner
);
1173 static void rtd_release(struct device
*dev
)
1178 static int soc_post_component_init(struct snd_soc_pcm_runtime
*rtd
,
1183 /* register the rtd device */
1184 rtd
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
1187 device_initialize(rtd
->dev
);
1188 rtd
->dev
->parent
= rtd
->card
->dev
;
1189 rtd
->dev
->release
= rtd_release
;
1190 rtd
->dev
->groups
= soc_dev_attr_groups
;
1191 dev_set_name(rtd
->dev
, "%s", name
);
1192 dev_set_drvdata(rtd
->dev
, rtd
);
1193 mutex_init(&rtd
->pcm_mutex
);
1194 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_PLAYBACK
].be_clients
);
1195 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_CAPTURE
].be_clients
);
1196 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_PLAYBACK
].fe_clients
);
1197 INIT_LIST_HEAD(&rtd
->dpcm
[SNDRV_PCM_STREAM_CAPTURE
].fe_clients
);
1198 ret
= device_add(rtd
->dev
);
1200 /* calling put_device() here to free the rtd->dev */
1201 put_device(rtd
->dev
);
1202 dev_err(rtd
->card
->dev
,
1203 "ASoC: failed to register runtime device: %d\n", ret
);
1206 rtd
->dev_registered
= 1;
1210 static int soc_probe_link_components(struct snd_soc_card
*card
, int num
,
1213 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1214 struct snd_soc_platform
*platform
= rtd
->platform
;
1215 struct snd_soc_component
*component
;
1218 /* probe the CPU-side component, if it is a CODEC */
1219 component
= rtd
->cpu_dai
->component
;
1220 if (component
->driver
->probe_order
== order
) {
1221 ret
= soc_probe_component(card
, component
);
1226 /* probe the CODEC-side components */
1227 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1228 component
= rtd
->codec_dais
[i
]->component
;
1229 if (component
->driver
->probe_order
== order
) {
1230 ret
= soc_probe_component(card
, component
);
1236 /* probe the platform */
1237 if (platform
->component
.driver
->probe_order
== order
) {
1238 ret
= soc_probe_component(card
, &platform
->component
);
1246 static int soc_probe_dai(struct snd_soc_dai
*dai
, int order
)
1250 if (!dai
->probed
&& dai
->driver
->probe_order
== order
) {
1251 if (dai
->driver
->probe
) {
1252 ret
= dai
->driver
->probe(dai
);
1255 "ASoC: failed to probe DAI %s: %d\n",
1267 static int soc_link_dai_widgets(struct snd_soc_card
*card
,
1268 struct snd_soc_dai_link
*dai_link
,
1269 struct snd_soc_pcm_runtime
*rtd
)
1271 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1272 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
1273 struct snd_soc_dapm_widget
*play_w
, *capture_w
;
1276 if (rtd
->num_codecs
> 1)
1277 dev_warn(card
->dev
, "ASoC: Multiple codecs not supported yet\n");
1279 /* link the DAI widgets */
1280 play_w
= codec_dai
->playback_widget
;
1281 capture_w
= cpu_dai
->capture_widget
;
1282 if (play_w
&& capture_w
) {
1283 ret
= snd_soc_dapm_new_pcm(card
, dai_link
->params
,
1284 dai_link
->num_params
, capture_w
,
1287 dev_err(card
->dev
, "ASoC: Can't link %s to %s: %d\n",
1288 play_w
->name
, capture_w
->name
, ret
);
1293 play_w
= cpu_dai
->playback_widget
;
1294 capture_w
= codec_dai
->capture_widget
;
1295 if (play_w
&& capture_w
) {
1296 ret
= snd_soc_dapm_new_pcm(card
, dai_link
->params
,
1297 dai_link
->num_params
, capture_w
,
1300 dev_err(card
->dev
, "ASoC: Can't link %s to %s: %d\n",
1301 play_w
->name
, capture_w
->name
, ret
);
1309 static int soc_probe_link_dais(struct snd_soc_card
*card
, int num
, int order
)
1311 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1312 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1313 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1316 dev_dbg(card
->dev
, "ASoC: probe %s dai link %d late %d\n",
1317 card
->name
, num
, order
);
1319 /* set default power off timeout */
1320 rtd
->pmdown_time
= pmdown_time
;
1322 ret
= soc_probe_dai(cpu_dai
, order
);
1326 /* probe the CODEC DAI */
1327 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1328 ret
= soc_probe_dai(rtd
->codec_dais
[i
], order
);
1333 /* complete DAI probe during last probe */
1334 if (order
!= SND_SOC_COMP_ORDER_LAST
)
1337 /* do machine specific initialization */
1338 if (dai_link
->init
) {
1339 ret
= dai_link
->init(rtd
);
1341 dev_err(card
->dev
, "ASoC: failed to init %s: %d\n",
1342 dai_link
->name
, ret
);
1347 if (dai_link
->dai_fmt
)
1348 snd_soc_runtime_set_dai_fmt(rtd
, dai_link
->dai_fmt
);
1350 ret
= soc_post_component_init(rtd
, dai_link
->name
);
1354 #ifdef CONFIG_DEBUG_FS
1355 /* add DPCM sysfs entries */
1356 if (dai_link
->dynamic
)
1357 soc_dpcm_debugfs_add(rtd
);
1360 if (cpu_dai
->driver
->compress_dai
) {
1361 /*create compress_device"*/
1362 ret
= soc_new_compress(rtd
, num
);
1364 dev_err(card
->dev
, "ASoC: can't create compress %s\n",
1365 dai_link
->stream_name
);
1370 if (!dai_link
->params
) {
1371 /* create the pcm */
1372 ret
= soc_new_pcm(rtd
, num
);
1374 dev_err(card
->dev
, "ASoC: can't create pcm %s :%d\n",
1375 dai_link
->stream_name
, ret
);
1379 INIT_DELAYED_WORK(&rtd
->delayed_work
,
1380 codec2codec_close_delayed_work
);
1382 /* link the DAI widgets */
1383 ret
= soc_link_dai_widgets(card
, dai_link
, rtd
);
1392 static int soc_bind_aux_dev(struct snd_soc_card
*card
, int num
)
1394 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd_aux
[num
];
1395 struct snd_soc_aux_dev
*aux_dev
= &card
->aux_dev
[num
];
1396 const char *name
= aux_dev
->codec_name
;
1398 rtd
->component
= soc_find_component(aux_dev
->codec_of_node
, name
);
1399 if (!rtd
->component
) {
1400 if (aux_dev
->codec_of_node
)
1401 name
= of_node_full_name(aux_dev
->codec_of_node
);
1403 dev_err(card
->dev
, "ASoC: %s not registered\n", name
);
1404 return -EPROBE_DEFER
;
1408 * Some places still reference rtd->codec, so we have to keep that
1409 * initialized if the component is a CODEC. Once all those references
1410 * have been removed, this code can be removed as well.
1412 rtd
->codec
= rtd
->component
->codec
;
1417 static int soc_probe_aux_dev(struct snd_soc_card
*card
, int num
)
1419 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd_aux
[num
];
1420 struct snd_soc_aux_dev
*aux_dev
= &card
->aux_dev
[num
];
1423 ret
= soc_probe_component(card
, rtd
->component
);
1427 /* do machine specific initialization */
1428 if (aux_dev
->init
) {
1429 ret
= aux_dev
->init(rtd
->component
);
1431 dev_err(card
->dev
, "ASoC: failed to init %s: %d\n",
1432 aux_dev
->name
, ret
);
1437 return soc_post_component_init(rtd
, aux_dev
->name
);
1440 static void soc_remove_aux_dev(struct snd_soc_card
*card
, int num
)
1442 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd_aux
[num
];
1443 struct snd_soc_component
*component
= rtd
->component
;
1445 /* unregister the rtd device */
1446 if (rtd
->dev_registered
) {
1447 device_unregister(rtd
->dev
);
1448 rtd
->dev_registered
= 0;
1451 if (component
&& component
->probed
)
1452 soc_remove_component(component
);
1455 static int snd_soc_init_codec_cache(struct snd_soc_codec
*codec
)
1459 if (codec
->cache_init
)
1462 ret
= snd_soc_cache_init(codec
);
1465 "ASoC: Failed to set cache compression type: %d\n",
1469 codec
->cache_init
= 1;
1474 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1475 * @rtd: The runtime for which the DAI link format should be changed
1476 * @dai_fmt: The new DAI link format
1478 * This function updates the DAI link format for all DAIs connected to the DAI
1479 * link for the specified runtime.
1481 * Note: For setups with a static format set the dai_fmt field in the
1482 * corresponding snd_dai_link struct instead of using this function.
1484 * Returns 0 on success, otherwise a negative error code.
1486 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime
*rtd
,
1487 unsigned int dai_fmt
)
1489 struct snd_soc_dai
**codec_dais
= rtd
->codec_dais
;
1490 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1494 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1495 struct snd_soc_dai
*codec_dai
= codec_dais
[i
];
1497 ret
= snd_soc_dai_set_fmt(codec_dai
, dai_fmt
);
1498 if (ret
!= 0 && ret
!= -ENOTSUPP
) {
1499 dev_warn(codec_dai
->dev
,
1500 "ASoC: Failed to set DAI format: %d\n", ret
);
1505 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1506 if (cpu_dai
->codec
) {
1507 unsigned int inv_dai_fmt
;
1509 inv_dai_fmt
= dai_fmt
& ~SND_SOC_DAIFMT_MASTER_MASK
;
1510 switch (dai_fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1511 case SND_SOC_DAIFMT_CBM_CFM
:
1512 inv_dai_fmt
|= SND_SOC_DAIFMT_CBS_CFS
;
1514 case SND_SOC_DAIFMT_CBM_CFS
:
1515 inv_dai_fmt
|= SND_SOC_DAIFMT_CBS_CFM
;
1517 case SND_SOC_DAIFMT_CBS_CFM
:
1518 inv_dai_fmt
|= SND_SOC_DAIFMT_CBM_CFS
;
1520 case SND_SOC_DAIFMT_CBS_CFS
:
1521 inv_dai_fmt
|= SND_SOC_DAIFMT_CBM_CFM
;
1525 dai_fmt
= inv_dai_fmt
;
1528 ret
= snd_soc_dai_set_fmt(cpu_dai
, dai_fmt
);
1529 if (ret
!= 0 && ret
!= -ENOTSUPP
) {
1530 dev_warn(cpu_dai
->dev
,
1531 "ASoC: Failed to set DAI format: %d\n", ret
);
1537 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt
);
1539 static int snd_soc_instantiate_card(struct snd_soc_card
*card
)
1541 struct snd_soc_codec
*codec
;
1544 mutex_lock(&client_mutex
);
1545 mutex_lock_nested(&card
->mutex
, SND_SOC_CARD_CLASS_INIT
);
1548 for (i
= 0; i
< card
->num_links
; i
++) {
1549 ret
= soc_bind_dai_link(card
, i
);
1554 /* bind aux_devs too */
1555 for (i
= 0; i
< card
->num_aux_devs
; i
++) {
1556 ret
= soc_bind_aux_dev(card
, i
);
1561 /* initialize the register cache for each available codec */
1562 list_for_each_entry(codec
, &codec_list
, list
) {
1563 if (codec
->cache_init
)
1565 ret
= snd_soc_init_codec_cache(codec
);
1570 /* card bind complete so register a sound card */
1571 ret
= snd_card_new(card
->dev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
1572 card
->owner
, 0, &card
->snd_card
);
1575 "ASoC: can't create sound card for card %s: %d\n",
1580 soc_init_card_debugfs(card
);
1582 card
->dapm
.bias_level
= SND_SOC_BIAS_OFF
;
1583 card
->dapm
.dev
= card
->dev
;
1584 card
->dapm
.card
= card
;
1585 list_add(&card
->dapm
.list
, &card
->dapm_list
);
1587 #ifdef CONFIG_DEBUG_FS
1588 snd_soc_dapm_debugfs_init(&card
->dapm
, card
->debugfs_card_root
);
1591 #ifdef CONFIG_PM_SLEEP
1592 /* deferred resume work */
1593 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
1596 if (card
->dapm_widgets
)
1597 snd_soc_dapm_new_controls(&card
->dapm
, card
->dapm_widgets
,
1598 card
->num_dapm_widgets
);
1600 if (card
->of_dapm_widgets
)
1601 snd_soc_dapm_new_controls(&card
->dapm
, card
->of_dapm_widgets
,
1602 card
->num_of_dapm_widgets
);
1604 /* initialise the sound card only once */
1606 ret
= card
->probe(card
);
1608 goto card_probe_error
;
1611 /* probe all components used by DAI links on this card */
1612 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1614 for (i
= 0; i
< card
->num_links
; i
++) {
1615 ret
= soc_probe_link_components(card
, i
, order
);
1618 "ASoC: failed to instantiate card %d\n",
1625 /* probe all DAI links on this card */
1626 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1628 for (i
= 0; i
< card
->num_links
; i
++) {
1629 ret
= soc_probe_link_dais(card
, i
, order
);
1632 "ASoC: failed to instantiate card %d\n",
1639 for (i
= 0; i
< card
->num_aux_devs
; i
++) {
1640 ret
= soc_probe_aux_dev(card
, i
);
1643 "ASoC: failed to add auxiliary devices %d\n",
1645 goto probe_aux_dev_err
;
1649 snd_soc_dapm_link_dai_widgets(card
);
1650 snd_soc_dapm_connect_dai_link_widgets(card
);
1653 snd_soc_add_card_controls(card
, card
->controls
, card
->num_controls
);
1655 if (card
->dapm_routes
)
1656 snd_soc_dapm_add_routes(&card
->dapm
, card
->dapm_routes
,
1657 card
->num_dapm_routes
);
1659 if (card
->of_dapm_routes
)
1660 snd_soc_dapm_add_routes(&card
->dapm
, card
->of_dapm_routes
,
1661 card
->num_of_dapm_routes
);
1663 snprintf(card
->snd_card
->shortname
, sizeof(card
->snd_card
->shortname
),
1665 snprintf(card
->snd_card
->longname
, sizeof(card
->snd_card
->longname
),
1666 "%s", card
->long_name
? card
->long_name
: card
->name
);
1667 snprintf(card
->snd_card
->driver
, sizeof(card
->snd_card
->driver
),
1668 "%s", card
->driver_name
? card
->driver_name
: card
->name
);
1669 for (i
= 0; i
< ARRAY_SIZE(card
->snd_card
->driver
); i
++) {
1670 switch (card
->snd_card
->driver
[i
]) {
1676 if (!isalnum(card
->snd_card
->driver
[i
]))
1677 card
->snd_card
->driver
[i
] = '_';
1682 if (card
->late_probe
) {
1683 ret
= card
->late_probe(card
);
1685 dev_err(card
->dev
, "ASoC: %s late_probe() failed: %d\n",
1687 goto probe_aux_dev_err
;
1691 snd_soc_dapm_new_widgets(card
);
1693 ret
= snd_card_register(card
->snd_card
);
1695 dev_err(card
->dev
, "ASoC: failed to register soundcard %d\n",
1697 goto probe_aux_dev_err
;
1700 card
->instantiated
= 1;
1701 snd_soc_dapm_sync(&card
->dapm
);
1702 mutex_unlock(&card
->mutex
);
1703 mutex_unlock(&client_mutex
);
1708 for (i
= 0; i
< card
->num_aux_devs
; i
++)
1709 soc_remove_aux_dev(card
, i
);
1712 soc_remove_dai_links(card
);
1718 soc_cleanup_card_debugfs(card
);
1719 snd_card_free(card
->snd_card
);
1722 mutex_unlock(&card
->mutex
);
1723 mutex_unlock(&client_mutex
);
1728 /* probes a new socdev */
1729 static int soc_probe(struct platform_device
*pdev
)
1731 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1734 * no card, so machine driver should be registering card
1735 * we should not be here in that case so ret error
1740 dev_warn(&pdev
->dev
,
1741 "ASoC: machine %s should use snd_soc_register_card()\n",
1744 /* Bodge while we unpick instantiation */
1745 card
->dev
= &pdev
->dev
;
1747 return snd_soc_register_card(card
);
1750 static int soc_cleanup_card_resources(struct snd_soc_card
*card
)
1754 /* make sure any delayed work runs */
1755 for (i
= 0; i
< card
->num_rtd
; i
++) {
1756 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1757 flush_delayed_work(&rtd
->delayed_work
);
1760 /* remove auxiliary devices */
1761 for (i
= 0; i
< card
->num_aux_devs
; i
++)
1762 soc_remove_aux_dev(card
, i
);
1764 /* remove and free each DAI */
1765 soc_remove_dai_links(card
);
1767 soc_cleanup_card_debugfs(card
);
1769 /* remove the card */
1773 snd_soc_dapm_free(&card
->dapm
);
1775 snd_card_free(card
->snd_card
);
1780 /* removes a socdev */
1781 static int soc_remove(struct platform_device
*pdev
)
1783 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1785 snd_soc_unregister_card(card
);
1789 int snd_soc_poweroff(struct device
*dev
)
1791 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
1794 if (!card
->instantiated
)
1797 /* Flush out pmdown_time work - we actually do want to run it
1798 * now, we're shutting down so no imminent restart. */
1799 for (i
= 0; i
< card
->num_rtd
; i
++) {
1800 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1801 flush_delayed_work(&rtd
->delayed_work
);
1804 snd_soc_dapm_shutdown(card
);
1806 /* deactivate pins to sleep state */
1807 for (i
= 0; i
< card
->num_rtd
; i
++) {
1808 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1809 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1812 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
1813 for (j
= 0; j
< rtd
->num_codecs
; j
++) {
1814 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[j
];
1815 pinctrl_pm_select_sleep_state(codec_dai
->dev
);
1821 EXPORT_SYMBOL_GPL(snd_soc_poweroff
);
1823 const struct dev_pm_ops snd_soc_pm_ops
= {
1824 .suspend
= snd_soc_suspend
,
1825 .resume
= snd_soc_resume
,
1826 .freeze
= snd_soc_suspend
,
1827 .thaw
= snd_soc_resume
,
1828 .poweroff
= snd_soc_poweroff
,
1829 .restore
= snd_soc_resume
,
1831 EXPORT_SYMBOL_GPL(snd_soc_pm_ops
);
1833 /* ASoC platform driver */
1834 static struct platform_driver soc_driver
= {
1836 .name
= "soc-audio",
1837 .pm
= &snd_soc_pm_ops
,
1840 .remove
= soc_remove
,
1844 * snd_soc_cnew - create new control
1845 * @_template: control template
1846 * @data: control private data
1847 * @long_name: control long name
1848 * @prefix: control name prefix
1850 * Create a new mixer control from a template control.
1852 * Returns 0 for success, else error.
1854 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
1855 void *data
, const char *long_name
,
1858 struct snd_kcontrol_new
template;
1859 struct snd_kcontrol
*kcontrol
;
1862 memcpy(&template, _template
, sizeof(template));
1866 long_name
= template.name
;
1869 name
= kasprintf(GFP_KERNEL
, "%s %s", prefix
, long_name
);
1873 template.name
= name
;
1875 template.name
= long_name
;
1878 kcontrol
= snd_ctl_new1(&template, data
);
1884 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
1886 static int snd_soc_add_controls(struct snd_card
*card
, struct device
*dev
,
1887 const struct snd_kcontrol_new
*controls
, int num_controls
,
1888 const char *prefix
, void *data
)
1892 for (i
= 0; i
< num_controls
; i
++) {
1893 const struct snd_kcontrol_new
*control
= &controls
[i
];
1894 err
= snd_ctl_add(card
, snd_soc_cnew(control
, data
,
1895 control
->name
, prefix
));
1897 dev_err(dev
, "ASoC: Failed to add %s: %d\n",
1898 control
->name
, err
);
1906 struct snd_kcontrol
*snd_soc_card_get_kcontrol(struct snd_soc_card
*soc_card
,
1909 struct snd_card
*card
= soc_card
->snd_card
;
1910 struct snd_kcontrol
*kctl
;
1912 if (unlikely(!name
))
1915 list_for_each_entry(kctl
, &card
->controls
, list
)
1916 if (!strncmp(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
)))
1920 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol
);
1923 * snd_soc_add_component_controls - Add an array of controls to a component.
1925 * @component: Component to add controls to
1926 * @controls: Array of controls to add
1927 * @num_controls: Number of elements in the array
1929 * Return: 0 for success, else error.
1931 int snd_soc_add_component_controls(struct snd_soc_component
*component
,
1932 const struct snd_kcontrol_new
*controls
, unsigned int num_controls
)
1934 struct snd_card
*card
= component
->card
->snd_card
;
1936 return snd_soc_add_controls(card
, component
->dev
, controls
,
1937 num_controls
, component
->name_prefix
, component
);
1939 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls
);
1942 * snd_soc_add_codec_controls - add an array of controls to a codec.
1943 * Convenience function to add a list of controls. Many codecs were
1944 * duplicating this code.
1946 * @codec: codec to add controls to
1947 * @controls: array of controls to add
1948 * @num_controls: number of elements in the array
1950 * Return 0 for success, else error.
1952 int snd_soc_add_codec_controls(struct snd_soc_codec
*codec
,
1953 const struct snd_kcontrol_new
*controls
, unsigned int num_controls
)
1955 return snd_soc_add_component_controls(&codec
->component
, controls
,
1958 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls
);
1961 * snd_soc_add_platform_controls - add an array of controls to a platform.
1962 * Convenience function to add a list of controls.
1964 * @platform: platform to add controls to
1965 * @controls: array of controls to add
1966 * @num_controls: number of elements in the array
1968 * Return 0 for success, else error.
1970 int snd_soc_add_platform_controls(struct snd_soc_platform
*platform
,
1971 const struct snd_kcontrol_new
*controls
, unsigned int num_controls
)
1973 return snd_soc_add_component_controls(&platform
->component
, controls
,
1976 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls
);
1979 * snd_soc_add_card_controls - add an array of controls to a SoC card.
1980 * Convenience function to add a list of controls.
1982 * @soc_card: SoC card to add controls to
1983 * @controls: array of controls to add
1984 * @num_controls: number of elements in the array
1986 * Return 0 for success, else error.
1988 int snd_soc_add_card_controls(struct snd_soc_card
*soc_card
,
1989 const struct snd_kcontrol_new
*controls
, int num_controls
)
1991 struct snd_card
*card
= soc_card
->snd_card
;
1993 return snd_soc_add_controls(card
, soc_card
->dev
, controls
, num_controls
,
1996 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls
);
1999 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2000 * Convienience function to add a list of controls.
2002 * @dai: DAI to add controls to
2003 * @controls: array of controls to add
2004 * @num_controls: number of elements in the array
2006 * Return 0 for success, else error.
2008 int snd_soc_add_dai_controls(struct snd_soc_dai
*dai
,
2009 const struct snd_kcontrol_new
*controls
, int num_controls
)
2011 struct snd_card
*card
= dai
->component
->card
->snd_card
;
2013 return snd_soc_add_controls(card
, dai
->dev
, controls
, num_controls
,
2016 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls
);
2019 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2021 * @clk_id: DAI specific clock ID
2022 * @freq: new clock frequency in Hz
2023 * @dir: new clock direction - input/output.
2025 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2027 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
2028 unsigned int freq
, int dir
)
2030 if (dai
->driver
&& dai
->driver
->ops
->set_sysclk
)
2031 return dai
->driver
->ops
->set_sysclk(dai
, clk_id
, freq
, dir
);
2032 else if (dai
->codec
&& dai
->codec
->driver
->set_sysclk
)
2033 return dai
->codec
->driver
->set_sysclk(dai
->codec
, clk_id
, 0,
2038 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
2041 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2043 * @clk_id: DAI specific clock ID
2044 * @source: Source for the clock
2045 * @freq: new clock frequency in Hz
2046 * @dir: new clock direction - input/output.
2048 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2050 int snd_soc_codec_set_sysclk(struct snd_soc_codec
*codec
, int clk_id
,
2051 int source
, unsigned int freq
, int dir
)
2053 if (codec
->driver
->set_sysclk
)
2054 return codec
->driver
->set_sysclk(codec
, clk_id
, source
,
2059 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk
);
2062 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2064 * @div_id: DAI specific clock divider ID
2065 * @div: new clock divisor.
2067 * Configures the clock dividers. This is used to derive the best DAI bit and
2068 * frame clocks from the system or master clock. It's best to set the DAI bit
2069 * and frame clocks as low as possible to save system power.
2071 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
2072 int div_id
, int div
)
2074 if (dai
->driver
&& dai
->driver
->ops
->set_clkdiv
)
2075 return dai
->driver
->ops
->set_clkdiv(dai
, div_id
, div
);
2079 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
2082 * snd_soc_dai_set_pll - configure DAI PLL.
2084 * @pll_id: DAI specific PLL ID
2085 * @source: DAI specific source for the PLL
2086 * @freq_in: PLL input clock frequency in Hz
2087 * @freq_out: requested PLL output clock frequency in Hz
2089 * Configures and enables PLL to generate output clock based on input clock.
2091 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
2092 unsigned int freq_in
, unsigned int freq_out
)
2094 if (dai
->driver
&& dai
->driver
->ops
->set_pll
)
2095 return dai
->driver
->ops
->set_pll(dai
, pll_id
, source
,
2097 else if (dai
->codec
&& dai
->codec
->driver
->set_pll
)
2098 return dai
->codec
->driver
->set_pll(dai
->codec
, pll_id
, source
,
2103 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
2106 * snd_soc_codec_set_pll - configure codec PLL.
2108 * @pll_id: DAI specific PLL ID
2109 * @source: DAI specific source for the PLL
2110 * @freq_in: PLL input clock frequency in Hz
2111 * @freq_out: requested PLL output clock frequency in Hz
2113 * Configures and enables PLL to generate output clock based on input clock.
2115 int snd_soc_codec_set_pll(struct snd_soc_codec
*codec
, int pll_id
, int source
,
2116 unsigned int freq_in
, unsigned int freq_out
)
2118 if (codec
->driver
->set_pll
)
2119 return codec
->driver
->set_pll(codec
, pll_id
, source
,
2124 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll
);
2127 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2129 * @ratio Ratio of BCLK to Sample rate.
2131 * Configures the DAI for a preset BCLK to sample rate ratio.
2133 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai
*dai
, unsigned int ratio
)
2135 if (dai
->driver
&& dai
->driver
->ops
->set_bclk_ratio
)
2136 return dai
->driver
->ops
->set_bclk_ratio(dai
, ratio
);
2140 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio
);
2143 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2145 * @fmt: SND_SOC_DAIFMT_ format value.
2147 * Configures the DAI hardware format and clocking.
2149 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
2151 if (dai
->driver
== NULL
)
2153 if (dai
->driver
->ops
->set_fmt
== NULL
)
2155 return dai
->driver
->ops
->set_fmt(dai
, fmt
);
2157 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
2160 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2161 * @slots: Number of slots in use.
2162 * @tx_mask: bitmask representing active TX slots.
2163 * @rx_mask: bitmask representing active RX slots.
2165 * Generates the TDM tx and rx slot default masks for DAI.
2167 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots
,
2168 unsigned int *tx_mask
,
2169 unsigned int *rx_mask
)
2171 if (*tx_mask
|| *rx_mask
)
2177 *tx_mask
= (1 << slots
) - 1;
2178 *rx_mask
= (1 << slots
) - 1;
2184 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2185 * @dai: The DAI to configure
2186 * @tx_mask: bitmask representing active TX slots.
2187 * @rx_mask: bitmask representing active RX slots.
2188 * @slots: Number of slots in use.
2189 * @slot_width: Width in bits for each slot.
2191 * This function configures the specified DAI for TDM operation. @slot contains
2192 * the total number of slots of the TDM stream and @slot_with the width of each
2193 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2194 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2195 * DAI should write to or read from. If a bit is set the corresponding slot is
2196 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2197 * the first slot, bit 1 to the second slot and so on. The first active slot
2198 * maps to the first channel of the DAI, the second active slot to the second
2199 * channel and so on.
2201 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2202 * @rx_mask and @slot_width will be ignored.
2204 * Returns 0 on success, a negative error code otherwise.
2206 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
2207 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
2209 if (dai
->driver
&& dai
->driver
->ops
->xlate_tdm_slot_mask
)
2210 dai
->driver
->ops
->xlate_tdm_slot_mask(slots
,
2211 &tx_mask
, &rx_mask
);
2213 snd_soc_xlate_tdm_slot_mask(slots
, &tx_mask
, &rx_mask
);
2215 dai
->tx_mask
= tx_mask
;
2216 dai
->rx_mask
= rx_mask
;
2218 if (dai
->driver
&& dai
->driver
->ops
->set_tdm_slot
)
2219 return dai
->driver
->ops
->set_tdm_slot(dai
, tx_mask
, rx_mask
,
2224 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
2227 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2229 * @tx_num: how many TX channels
2230 * @tx_slot: pointer to an array which imply the TX slot number channel
2232 * @rx_num: how many RX channels
2233 * @rx_slot: pointer to an array which imply the RX slot number channel
2236 * configure the relationship between channel number and TDM slot number.
2238 int snd_soc_dai_set_channel_map(struct snd_soc_dai
*dai
,
2239 unsigned int tx_num
, unsigned int *tx_slot
,
2240 unsigned int rx_num
, unsigned int *rx_slot
)
2242 if (dai
->driver
&& dai
->driver
->ops
->set_channel_map
)
2243 return dai
->driver
->ops
->set_channel_map(dai
, tx_num
, tx_slot
,
2248 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map
);
2251 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2253 * @tristate: tristate enable
2255 * Tristates the DAI so that others can use it.
2257 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
2259 if (dai
->driver
&& dai
->driver
->ops
->set_tristate
)
2260 return dai
->driver
->ops
->set_tristate(dai
, tristate
);
2264 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
2267 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2269 * @mute: mute enable
2270 * @direction: stream to mute
2272 * Mutes the DAI DAC.
2274 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
,
2280 if (dai
->driver
->ops
->mute_stream
)
2281 return dai
->driver
->ops
->mute_stream(dai
, mute
, direction
);
2282 else if (direction
== SNDRV_PCM_STREAM_PLAYBACK
&&
2283 dai
->driver
->ops
->digital_mute
)
2284 return dai
->driver
->ops
->digital_mute(dai
, mute
);
2288 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
2290 static int snd_soc_init_multicodec(struct snd_soc_card
*card
,
2291 struct snd_soc_dai_link
*dai_link
)
2293 /* Legacy codec/codec_dai link is a single entry in multicodec */
2294 if (dai_link
->codec_name
|| dai_link
->codec_of_node
||
2295 dai_link
->codec_dai_name
) {
2296 dai_link
->num_codecs
= 1;
2298 dai_link
->codecs
= devm_kzalloc(card
->dev
,
2299 sizeof(struct snd_soc_dai_link_component
),
2301 if (!dai_link
->codecs
)
2304 dai_link
->codecs
[0].name
= dai_link
->codec_name
;
2305 dai_link
->codecs
[0].of_node
= dai_link
->codec_of_node
;
2306 dai_link
->codecs
[0].dai_name
= dai_link
->codec_dai_name
;
2309 if (!dai_link
->codecs
) {
2310 dev_err(card
->dev
, "ASoC: DAI link has no CODECs\n");
2318 * snd_soc_register_card - Register a card with the ASoC core
2320 * @card: Card to register
2323 int snd_soc_register_card(struct snd_soc_card
*card
)
2327 if (!card
->name
|| !card
->dev
)
2330 for (i
= 0; i
< card
->num_links
; i
++) {
2331 struct snd_soc_dai_link
*link
= &card
->dai_link
[i
];
2333 ret
= snd_soc_init_multicodec(card
, link
);
2335 dev_err(card
->dev
, "ASoC: failed to init multicodec\n");
2339 for (j
= 0; j
< link
->num_codecs
; j
++) {
2341 * Codec must be specified by 1 of name or OF node,
2342 * not both or neither.
2344 if (!!link
->codecs
[j
].name
==
2345 !!link
->codecs
[j
].of_node
) {
2346 dev_err(card
->dev
, "ASoC: Neither/both codec name/of_node are set for %s\n",
2350 /* Codec DAI name must be specified */
2351 if (!link
->codecs
[j
].dai_name
) {
2352 dev_err(card
->dev
, "ASoC: codec_dai_name not set for %s\n",
2359 * Platform may be specified by either name or OF node, but
2360 * can be left unspecified, and a dummy platform will be used.
2362 if (link
->platform_name
&& link
->platform_of_node
) {
2364 "ASoC: Both platform name/of_node are set for %s\n",
2370 * CPU device may be specified by either name or OF node, but
2371 * can be left unspecified, and will be matched based on DAI
2374 if (link
->cpu_name
&& link
->cpu_of_node
) {
2376 "ASoC: Neither/both cpu name/of_node are set for %s\n",
2381 * At least one of CPU DAI name or CPU device name/node must be
2384 if (!link
->cpu_dai_name
&&
2385 !(link
->cpu_name
|| link
->cpu_of_node
)) {
2387 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
2393 dev_set_drvdata(card
->dev
, card
);
2395 snd_soc_initialize_card_lists(card
);
2397 card
->rtd
= devm_kzalloc(card
->dev
,
2398 sizeof(struct snd_soc_pcm_runtime
) *
2399 (card
->num_links
+ card
->num_aux_devs
),
2401 if (card
->rtd
== NULL
)
2404 card
->rtd_aux
= &card
->rtd
[card
->num_links
];
2406 for (i
= 0; i
< card
->num_links
; i
++) {
2407 card
->rtd
[i
].card
= card
;
2408 card
->rtd
[i
].dai_link
= &card
->dai_link
[i
];
2409 card
->rtd
[i
].codec_dais
= devm_kzalloc(card
->dev
,
2410 sizeof(struct snd_soc_dai
*) *
2411 (card
->rtd
[i
].dai_link
->num_codecs
),
2413 if (card
->rtd
[i
].codec_dais
== NULL
)
2417 for (i
= 0; i
< card
->num_aux_devs
; i
++)
2418 card
->rtd_aux
[i
].card
= card
;
2420 INIT_LIST_HEAD(&card
->dapm_dirty
);
2421 card
->instantiated
= 0;
2422 mutex_init(&card
->mutex
);
2423 mutex_init(&card
->dapm_mutex
);
2425 ret
= snd_soc_instantiate_card(card
);
2429 /* deactivate pins to sleep state */
2430 for (i
= 0; i
< card
->num_rtd
; i
++) {
2431 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
2432 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
2435 for (j
= 0; j
< rtd
->num_codecs
; j
++) {
2436 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[j
];
2437 if (!codec_dai
->active
)
2438 pinctrl_pm_select_sleep_state(codec_dai
->dev
);
2441 if (!cpu_dai
->active
)
2442 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
2447 EXPORT_SYMBOL_GPL(snd_soc_register_card
);
2450 * snd_soc_unregister_card - Unregister a card with the ASoC core
2452 * @card: Card to unregister
2455 int snd_soc_unregister_card(struct snd_soc_card
*card
)
2457 if (card
->instantiated
) {
2458 card
->instantiated
= false;
2459 snd_soc_dapm_shutdown(card
);
2460 soc_cleanup_card_resources(card
);
2461 dev_dbg(card
->dev
, "ASoC: Unregistered card '%s'\n", card
->name
);
2466 EXPORT_SYMBOL_GPL(snd_soc_unregister_card
);
2469 * Simplify DAI link configuration by removing ".-1" from device names
2470 * and sanitizing names.
2472 static char *fmt_single_name(struct device
*dev
, int *id
)
2474 char *found
, name
[NAME_SIZE
];
2477 if (dev_name(dev
) == NULL
)
2480 strlcpy(name
, dev_name(dev
), NAME_SIZE
);
2482 /* are we a "%s.%d" name (platform and SPI components) */
2483 found
= strstr(name
, dev
->driver
->name
);
2486 if (sscanf(&found
[strlen(dev
->driver
->name
)], ".%d", id
) == 1) {
2488 /* discard ID from name if ID == -1 */
2490 found
[strlen(dev
->driver
->name
)] = '\0';
2494 /* I2C component devices are named "bus-addr" */
2495 if (sscanf(name
, "%x-%x", &id1
, &id2
) == 2) {
2496 char tmp
[NAME_SIZE
];
2498 /* create unique ID number from I2C addr and bus */
2499 *id
= ((id1
& 0xffff) << 16) + id2
;
2501 /* sanitize component name for DAI link creation */
2502 snprintf(tmp
, NAME_SIZE
, "%s.%s", dev
->driver
->name
, name
);
2503 strlcpy(name
, tmp
, NAME_SIZE
);
2508 return kstrdup(name
, GFP_KERNEL
);
2512 * Simplify DAI link naming for single devices with multiple DAIs by removing
2513 * any ".-1" and using the DAI name (instead of device name).
2515 static inline char *fmt_multiple_name(struct device
*dev
,
2516 struct snd_soc_dai_driver
*dai_drv
)
2518 if (dai_drv
->name
== NULL
) {
2520 "ASoC: error - multiple DAI %s registered with no name\n",
2525 return kstrdup(dai_drv
->name
, GFP_KERNEL
);
2529 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2531 * @component: The component for which the DAIs should be unregistered
2533 static void snd_soc_unregister_dais(struct snd_soc_component
*component
)
2535 struct snd_soc_dai
*dai
, *_dai
;
2537 list_for_each_entry_safe(dai
, _dai
, &component
->dai_list
, list
) {
2538 dev_dbg(component
->dev
, "ASoC: Unregistered DAI '%s'\n",
2540 list_del(&dai
->list
);
2547 * snd_soc_register_dais - Register a DAI with the ASoC core
2549 * @component: The component the DAIs are registered for
2550 * @dai_drv: DAI driver to use for the DAIs
2551 * @count: Number of DAIs
2552 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2555 static int snd_soc_register_dais(struct snd_soc_component
*component
,
2556 struct snd_soc_dai_driver
*dai_drv
, size_t count
,
2557 bool legacy_dai_naming
)
2559 struct device
*dev
= component
->dev
;
2560 struct snd_soc_dai
*dai
;
2564 dev_dbg(dev
, "ASoC: dai register %s #%Zu\n", dev_name(dev
), count
);
2566 component
->dai_drv
= dai_drv
;
2567 component
->num_dai
= count
;
2569 for (i
= 0; i
< count
; i
++) {
2571 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
2578 * Back in the old days when we still had component-less DAIs,
2579 * instead of having a static name, component-less DAIs would
2580 * inherit the name of the parent device so it is possible to
2581 * register multiple instances of the DAI. We still need to keep
2582 * the same naming style even though those DAIs are not
2583 * component-less anymore.
2585 if (count
== 1 && legacy_dai_naming
&&
2586 (dai_drv
[i
].id
== 0 || dai_drv
[i
].name
== NULL
)) {
2587 dai
->name
= fmt_single_name(dev
, &dai
->id
);
2589 dai
->name
= fmt_multiple_name(dev
, &dai_drv
[i
]);
2591 dai
->id
= dai_drv
[i
].id
;
2595 if (dai
->name
== NULL
) {
2601 dai
->component
= component
;
2603 dai
->driver
= &dai_drv
[i
];
2604 if (!dai
->driver
->ops
)
2605 dai
->driver
->ops
= &null_dai_ops
;
2607 list_add(&dai
->list
, &component
->dai_list
);
2609 dev_dbg(dev
, "ASoC: Registered DAI '%s'\n", dai
->name
);
2615 snd_soc_unregister_dais(component
);
2620 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context
*dapm
,
2621 enum snd_soc_dapm_type type
, int subseq
)
2623 struct snd_soc_component
*component
= dapm
->component
;
2625 component
->driver
->seq_notifier(component
, type
, subseq
);
2628 static int snd_soc_component_stream_event(struct snd_soc_dapm_context
*dapm
,
2631 struct snd_soc_component
*component
= dapm
->component
;
2633 return component
->driver
->stream_event(component
, event
);
2636 static int snd_soc_component_initialize(struct snd_soc_component
*component
,
2637 const struct snd_soc_component_driver
*driver
, struct device
*dev
)
2639 struct snd_soc_dapm_context
*dapm
;
2641 component
->name
= fmt_single_name(dev
, &component
->id
);
2642 if (!component
->name
) {
2643 dev_err(dev
, "ASoC: Failed to allocate name\n");
2647 component
->dev
= dev
;
2648 component
->driver
= driver
;
2649 component
->probe
= component
->driver
->probe
;
2650 component
->remove
= component
->driver
->remove
;
2652 if (!component
->dapm_ptr
)
2653 component
->dapm_ptr
= &component
->dapm
;
2655 dapm
= component
->dapm_ptr
;
2657 dapm
->component
= component
;
2658 dapm
->bias_level
= SND_SOC_BIAS_OFF
;
2659 dapm
->idle_bias_off
= true;
2660 if (driver
->seq_notifier
)
2661 dapm
->seq_notifier
= snd_soc_component_seq_notifier
;
2662 if (driver
->stream_event
)
2663 dapm
->stream_event
= snd_soc_component_stream_event
;
2665 component
->controls
= driver
->controls
;
2666 component
->num_controls
= driver
->num_controls
;
2667 component
->dapm_widgets
= driver
->dapm_widgets
;
2668 component
->num_dapm_widgets
= driver
->num_dapm_widgets
;
2669 component
->dapm_routes
= driver
->dapm_routes
;
2670 component
->num_dapm_routes
= driver
->num_dapm_routes
;
2672 INIT_LIST_HEAD(&component
->dai_list
);
2673 mutex_init(&component
->io_mutex
);
2678 static void snd_soc_component_setup_regmap(struct snd_soc_component
*component
)
2680 int val_bytes
= regmap_get_val_bytes(component
->regmap
);
2682 /* Errors are legitimate for non-integer byte multiples */
2684 component
->val_bytes
= val_bytes
;
2687 #ifdef CONFIG_REGMAP
2690 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
2691 * @component: The component for which to initialize the regmap instance
2692 * @regmap: The regmap instance that should be used by the component
2694 * This function allows deferred assignment of the regmap instance that is
2695 * associated with the component. Only use this if the regmap instance is not
2696 * yet ready when the component is registered. The function must also be called
2697 * before the first IO attempt of the component.
2699 void snd_soc_component_init_regmap(struct snd_soc_component
*component
,
2700 struct regmap
*regmap
)
2702 component
->regmap
= regmap
;
2703 snd_soc_component_setup_regmap(component
);
2705 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap
);
2708 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
2709 * @component: The component for which to de-initialize the regmap instance
2711 * Calls regmap_exit() on the regmap instance associated to the component and
2712 * removes the regmap instance from the component.
2714 * This function should only be used if snd_soc_component_init_regmap() was used
2715 * to initialize the regmap instance.
2717 void snd_soc_component_exit_regmap(struct snd_soc_component
*component
)
2719 regmap_exit(component
->regmap
);
2720 component
->regmap
= NULL
;
2722 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap
);
2726 static void snd_soc_component_add_unlocked(struct snd_soc_component
*component
)
2728 if (!component
->write
&& !component
->read
) {
2729 if (!component
->regmap
)
2730 component
->regmap
= dev_get_regmap(component
->dev
, NULL
);
2731 if (component
->regmap
)
2732 snd_soc_component_setup_regmap(component
);
2735 list_add(&component
->list
, &component_list
);
2738 static void snd_soc_component_add(struct snd_soc_component
*component
)
2740 mutex_lock(&client_mutex
);
2741 snd_soc_component_add_unlocked(component
);
2742 mutex_unlock(&client_mutex
);
2745 static void snd_soc_component_cleanup(struct snd_soc_component
*component
)
2747 snd_soc_unregister_dais(component
);
2748 kfree(component
->name
);
2751 static void snd_soc_component_del_unlocked(struct snd_soc_component
*component
)
2753 list_del(&component
->list
);
2756 int snd_soc_register_component(struct device
*dev
,
2757 const struct snd_soc_component_driver
*cmpnt_drv
,
2758 struct snd_soc_dai_driver
*dai_drv
,
2761 struct snd_soc_component
*cmpnt
;
2764 cmpnt
= kzalloc(sizeof(*cmpnt
), GFP_KERNEL
);
2766 dev_err(dev
, "ASoC: Failed to allocate memory\n");
2770 ret
= snd_soc_component_initialize(cmpnt
, cmpnt_drv
, dev
);
2774 cmpnt
->ignore_pmdown_time
= true;
2775 cmpnt
->registered_as_component
= true;
2777 ret
= snd_soc_register_dais(cmpnt
, dai_drv
, num_dai
, true);
2779 dev_err(dev
, "ASoC: Failed to register DAIs: %d\n", ret
);
2783 snd_soc_component_add(cmpnt
);
2788 snd_soc_component_cleanup(cmpnt
);
2793 EXPORT_SYMBOL_GPL(snd_soc_register_component
);
2796 * snd_soc_unregister_component - Unregister a component from the ASoC core
2799 void snd_soc_unregister_component(struct device
*dev
)
2801 struct snd_soc_component
*cmpnt
;
2803 mutex_lock(&client_mutex
);
2804 list_for_each_entry(cmpnt
, &component_list
, list
) {
2805 if (dev
== cmpnt
->dev
&& cmpnt
->registered_as_component
)
2808 mutex_unlock(&client_mutex
);
2812 snd_soc_component_del_unlocked(cmpnt
);
2813 mutex_unlock(&client_mutex
);
2814 snd_soc_component_cleanup(cmpnt
);
2817 EXPORT_SYMBOL_GPL(snd_soc_unregister_component
);
2819 static int snd_soc_platform_drv_probe(struct snd_soc_component
*component
)
2821 struct snd_soc_platform
*platform
= snd_soc_component_to_platform(component
);
2823 return platform
->driver
->probe(platform
);
2826 static void snd_soc_platform_drv_remove(struct snd_soc_component
*component
)
2828 struct snd_soc_platform
*platform
= snd_soc_component_to_platform(component
);
2830 platform
->driver
->remove(platform
);
2834 * snd_soc_add_platform - Add a platform to the ASoC core
2835 * @dev: The parent device for the platform
2836 * @platform: The platform to add
2837 * @platform_driver: The driver for the platform
2839 int snd_soc_add_platform(struct device
*dev
, struct snd_soc_platform
*platform
,
2840 const struct snd_soc_platform_driver
*platform_drv
)
2844 ret
= snd_soc_component_initialize(&platform
->component
,
2845 &platform_drv
->component_driver
, dev
);
2849 platform
->dev
= dev
;
2850 platform
->driver
= platform_drv
;
2852 if (platform_drv
->probe
)
2853 platform
->component
.probe
= snd_soc_platform_drv_probe
;
2854 if (platform_drv
->remove
)
2855 platform
->component
.remove
= snd_soc_platform_drv_remove
;
2857 #ifdef CONFIG_DEBUG_FS
2858 platform
->component
.debugfs_prefix
= "platform";
2861 mutex_lock(&client_mutex
);
2862 snd_soc_component_add_unlocked(&platform
->component
);
2863 list_add(&platform
->list
, &platform_list
);
2864 mutex_unlock(&client_mutex
);
2866 dev_dbg(dev
, "ASoC: Registered platform '%s'\n",
2867 platform
->component
.name
);
2871 EXPORT_SYMBOL_GPL(snd_soc_add_platform
);
2874 * snd_soc_register_platform - Register a platform with the ASoC core
2876 * @platform: platform to register
2878 int snd_soc_register_platform(struct device
*dev
,
2879 const struct snd_soc_platform_driver
*platform_drv
)
2881 struct snd_soc_platform
*platform
;
2884 dev_dbg(dev
, "ASoC: platform register %s\n", dev_name(dev
));
2886 platform
= kzalloc(sizeof(struct snd_soc_platform
), GFP_KERNEL
);
2887 if (platform
== NULL
)
2890 ret
= snd_soc_add_platform(dev
, platform
, platform_drv
);
2896 EXPORT_SYMBOL_GPL(snd_soc_register_platform
);
2899 * snd_soc_remove_platform - Remove a platform from the ASoC core
2900 * @platform: the platform to remove
2902 void snd_soc_remove_platform(struct snd_soc_platform
*platform
)
2905 mutex_lock(&client_mutex
);
2906 list_del(&platform
->list
);
2907 snd_soc_component_del_unlocked(&platform
->component
);
2908 mutex_unlock(&client_mutex
);
2910 dev_dbg(platform
->dev
, "ASoC: Unregistered platform '%s'\n",
2911 platform
->component
.name
);
2913 snd_soc_component_cleanup(&platform
->component
);
2915 EXPORT_SYMBOL_GPL(snd_soc_remove_platform
);
2917 struct snd_soc_platform
*snd_soc_lookup_platform(struct device
*dev
)
2919 struct snd_soc_platform
*platform
;
2921 mutex_lock(&client_mutex
);
2922 list_for_each_entry(platform
, &platform_list
, list
) {
2923 if (dev
== platform
->dev
) {
2924 mutex_unlock(&client_mutex
);
2928 mutex_unlock(&client_mutex
);
2932 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform
);
2935 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2937 * @platform: platform to unregister
2939 void snd_soc_unregister_platform(struct device
*dev
)
2941 struct snd_soc_platform
*platform
;
2943 platform
= snd_soc_lookup_platform(dev
);
2947 snd_soc_remove_platform(platform
);
2950 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform
);
2952 static u64 codec_format_map
[] = {
2953 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
,
2954 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_U16_BE
,
2955 SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S24_BE
,
2956 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_U24_BE
,
2957 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
,
2958 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_U32_BE
,
2959 SNDRV_PCM_FMTBIT_S24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
2960 SNDRV_PCM_FMTBIT_U24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
2961 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S20_3BE
,
2962 SNDRV_PCM_FMTBIT_U20_3LE
| SNDRV_PCM_FMTBIT_U20_3BE
,
2963 SNDRV_PCM_FMTBIT_S18_3LE
| SNDRV_PCM_FMTBIT_S18_3BE
,
2964 SNDRV_PCM_FMTBIT_U18_3LE
| SNDRV_PCM_FMTBIT_U18_3BE
,
2965 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
,
2966 SNDRV_PCM_FMTBIT_FLOAT64_LE
| SNDRV_PCM_FMTBIT_FLOAT64_BE
,
2967 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2968 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
,
2971 /* Fix up the DAI formats for endianness: codecs don't actually see
2972 * the endianness of the data but we're using the CPU format
2973 * definitions which do need to include endianness so we ensure that
2974 * codec DAIs always have both big and little endian variants set.
2976 static void fixup_codec_formats(struct snd_soc_pcm_stream
*stream
)
2980 for (i
= 0; i
< ARRAY_SIZE(codec_format_map
); i
++)
2981 if (stream
->formats
& codec_format_map
[i
])
2982 stream
->formats
|= codec_format_map
[i
];
2985 static int snd_soc_codec_drv_probe(struct snd_soc_component
*component
)
2987 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
2989 return codec
->driver
->probe(codec
);
2992 static void snd_soc_codec_drv_remove(struct snd_soc_component
*component
)
2994 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
2996 codec
->driver
->remove(codec
);
2999 static int snd_soc_codec_drv_write(struct snd_soc_component
*component
,
3000 unsigned int reg
, unsigned int val
)
3002 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
3004 return codec
->driver
->write(codec
, reg
, val
);
3007 static int snd_soc_codec_drv_read(struct snd_soc_component
*component
,
3008 unsigned int reg
, unsigned int *val
)
3010 struct snd_soc_codec
*codec
= snd_soc_component_to_codec(component
);
3012 *val
= codec
->driver
->read(codec
, reg
);
3017 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context
*dapm
,
3018 enum snd_soc_bias_level level
)
3020 struct snd_soc_codec
*codec
= snd_soc_dapm_to_codec(dapm
);
3022 return codec
->driver
->set_bias_level(codec
, level
);
3026 * snd_soc_register_codec - Register a codec with the ASoC core
3028 * @codec: codec to register
3030 int snd_soc_register_codec(struct device
*dev
,
3031 const struct snd_soc_codec_driver
*codec_drv
,
3032 struct snd_soc_dai_driver
*dai_drv
,
3035 struct snd_soc_codec
*codec
;
3036 struct snd_soc_dai
*dai
;
3039 dev_dbg(dev
, "codec register %s\n", dev_name(dev
));
3041 codec
= kzalloc(sizeof(struct snd_soc_codec
), GFP_KERNEL
);
3045 codec
->component
.dapm_ptr
= &codec
->dapm
;
3046 codec
->component
.codec
= codec
;
3048 ret
= snd_soc_component_initialize(&codec
->component
,
3049 &codec_drv
->component_driver
, dev
);
3053 if (codec_drv
->controls
) {
3054 codec
->component
.controls
= codec_drv
->controls
;
3055 codec
->component
.num_controls
= codec_drv
->num_controls
;
3057 if (codec_drv
->dapm_widgets
) {
3058 codec
->component
.dapm_widgets
= codec_drv
->dapm_widgets
;
3059 codec
->component
.num_dapm_widgets
= codec_drv
->num_dapm_widgets
;
3061 if (codec_drv
->dapm_routes
) {
3062 codec
->component
.dapm_routes
= codec_drv
->dapm_routes
;
3063 codec
->component
.num_dapm_routes
= codec_drv
->num_dapm_routes
;
3066 if (codec_drv
->probe
)
3067 codec
->component
.probe
= snd_soc_codec_drv_probe
;
3068 if (codec_drv
->remove
)
3069 codec
->component
.remove
= snd_soc_codec_drv_remove
;
3070 if (codec_drv
->write
)
3071 codec
->component
.write
= snd_soc_codec_drv_write
;
3072 if (codec_drv
->read
)
3073 codec
->component
.read
= snd_soc_codec_drv_read
;
3074 codec
->component
.ignore_pmdown_time
= codec_drv
->ignore_pmdown_time
;
3075 codec
->dapm
.idle_bias_off
= codec_drv
->idle_bias_off
;
3076 codec
->dapm
.suspend_bias_off
= codec_drv
->suspend_bias_off
;
3077 if (codec_drv
->seq_notifier
)
3078 codec
->dapm
.seq_notifier
= codec_drv
->seq_notifier
;
3079 if (codec_drv
->set_bias_level
)
3080 codec
->dapm
.set_bias_level
= snd_soc_codec_set_bias_level
;
3082 codec
->driver
= codec_drv
;
3083 codec
->component
.val_bytes
= codec_drv
->reg_word_size
;
3085 #ifdef CONFIG_DEBUG_FS
3086 codec
->component
.init_debugfs
= soc_init_codec_debugfs
;
3087 codec
->component
.debugfs_prefix
= "codec";
3090 if (codec_drv
->get_regmap
)
3091 codec
->component
.regmap
= codec_drv
->get_regmap(dev
);
3093 for (i
= 0; i
< num_dai
; i
++) {
3094 fixup_codec_formats(&dai_drv
[i
].playback
);
3095 fixup_codec_formats(&dai_drv
[i
].capture
);
3098 ret
= snd_soc_register_dais(&codec
->component
, dai_drv
, num_dai
, false);
3100 dev_err(dev
, "ASoC: Failed to register DAIs: %d\n", ret
);
3104 list_for_each_entry(dai
, &codec
->component
.dai_list
, list
)
3107 mutex_lock(&client_mutex
);
3108 snd_soc_component_add_unlocked(&codec
->component
);
3109 list_add(&codec
->list
, &codec_list
);
3110 mutex_unlock(&client_mutex
);
3112 dev_dbg(codec
->dev
, "ASoC: Registered codec '%s'\n",
3113 codec
->component
.name
);
3117 snd_soc_component_cleanup(&codec
->component
);
3122 EXPORT_SYMBOL_GPL(snd_soc_register_codec
);
3125 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3127 * @codec: codec to unregister
3129 void snd_soc_unregister_codec(struct device
*dev
)
3131 struct snd_soc_codec
*codec
;
3133 mutex_lock(&client_mutex
);
3134 list_for_each_entry(codec
, &codec_list
, list
) {
3135 if (dev
== codec
->dev
)
3138 mutex_unlock(&client_mutex
);
3142 list_del(&codec
->list
);
3143 snd_soc_component_del_unlocked(&codec
->component
);
3144 mutex_unlock(&client_mutex
);
3146 dev_dbg(codec
->dev
, "ASoC: Unregistered codec '%s'\n",
3147 codec
->component
.name
);
3149 snd_soc_component_cleanup(&codec
->component
);
3150 snd_soc_cache_exit(codec
);
3153 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec
);
3155 /* Retrieve a card's name from device tree */
3156 int snd_soc_of_parse_card_name(struct snd_soc_card
*card
,
3157 const char *propname
)
3159 struct device_node
*np
;
3163 pr_err("card->dev is not set before calling %s\n", __func__
);
3167 np
= card
->dev
->of_node
;
3169 ret
= of_property_read_string_index(np
, propname
, 0, &card
->name
);
3171 * EINVAL means the property does not exist. This is fine providing
3172 * card->name was previously set, which is checked later in
3173 * snd_soc_register_card.
3175 if (ret
< 0 && ret
!= -EINVAL
) {
3177 "ASoC: Property '%s' could not be read: %d\n",
3184 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name
);
3186 static const struct snd_soc_dapm_widget simple_widgets
[] = {
3187 SND_SOC_DAPM_MIC("Microphone", NULL
),
3188 SND_SOC_DAPM_LINE("Line", NULL
),
3189 SND_SOC_DAPM_HP("Headphone", NULL
),
3190 SND_SOC_DAPM_SPK("Speaker", NULL
),
3193 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card
*card
,
3194 const char *propname
)
3196 struct device_node
*np
= card
->dev
->of_node
;
3197 struct snd_soc_dapm_widget
*widgets
;
3198 const char *template, *wname
;
3199 int i
, j
, num_widgets
, ret
;
3201 num_widgets
= of_property_count_strings(np
, propname
);
3202 if (num_widgets
< 0) {
3204 "ASoC: Property '%s' does not exist\n", propname
);
3207 if (num_widgets
& 1) {
3209 "ASoC: Property '%s' length is not even\n", propname
);
3215 dev_err(card
->dev
, "ASoC: Property '%s's length is zero\n",
3220 widgets
= devm_kcalloc(card
->dev
, num_widgets
, sizeof(*widgets
),
3224 "ASoC: Could not allocate memory for widgets\n");
3228 for (i
= 0; i
< num_widgets
; i
++) {
3229 ret
= of_property_read_string_index(np
, propname
,
3233 "ASoC: Property '%s' index %d read error:%d\n",
3234 propname
, 2 * i
, ret
);
3238 for (j
= 0; j
< ARRAY_SIZE(simple_widgets
); j
++) {
3239 if (!strncmp(template, simple_widgets
[j
].name
,
3240 strlen(simple_widgets
[j
].name
))) {
3241 widgets
[i
] = simple_widgets
[j
];
3246 if (j
>= ARRAY_SIZE(simple_widgets
)) {
3248 "ASoC: DAPM widget '%s' is not supported\n",
3253 ret
= of_property_read_string_index(np
, propname
,
3258 "ASoC: Property '%s' index %d read error:%d\n",
3259 propname
, (2 * i
) + 1, ret
);
3263 widgets
[i
].name
= wname
;
3266 card
->of_dapm_widgets
= widgets
;
3267 card
->num_of_dapm_widgets
= num_widgets
;
3271 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets
);
3273 int snd_soc_of_parse_tdm_slot(struct device_node
*np
,
3274 unsigned int *slots
,
3275 unsigned int *slot_width
)
3280 if (of_property_read_bool(np
, "dai-tdm-slot-num")) {
3281 ret
= of_property_read_u32(np
, "dai-tdm-slot-num", &val
);
3289 if (of_property_read_bool(np
, "dai-tdm-slot-width")) {
3290 ret
= of_property_read_u32(np
, "dai-tdm-slot-width", &val
);
3300 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot
);
3302 int snd_soc_of_parse_audio_routing(struct snd_soc_card
*card
,
3303 const char *propname
)
3305 struct device_node
*np
= card
->dev
->of_node
;
3307 struct snd_soc_dapm_route
*routes
;
3310 num_routes
= of_property_count_strings(np
, propname
);
3311 if (num_routes
< 0 || num_routes
& 1) {
3313 "ASoC: Property '%s' does not exist or its length is not even\n",
3319 dev_err(card
->dev
, "ASoC: Property '%s's length is zero\n",
3324 routes
= devm_kzalloc(card
->dev
, num_routes
* sizeof(*routes
),
3328 "ASoC: Could not allocate DAPM route table\n");
3332 for (i
= 0; i
< num_routes
; i
++) {
3333 ret
= of_property_read_string_index(np
, propname
,
3334 2 * i
, &routes
[i
].sink
);
3337 "ASoC: Property '%s' index %d could not be read: %d\n",
3338 propname
, 2 * i
, ret
);
3341 ret
= of_property_read_string_index(np
, propname
,
3342 (2 * i
) + 1, &routes
[i
].source
);
3345 "ASoC: Property '%s' index %d could not be read: %d\n",
3346 propname
, (2 * i
) + 1, ret
);
3351 card
->num_of_dapm_routes
= num_routes
;
3352 card
->of_dapm_routes
= routes
;
3356 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing
);
3358 unsigned int snd_soc_of_parse_daifmt(struct device_node
*np
,
3360 struct device_node
**bitclkmaster
,
3361 struct device_node
**framemaster
)
3365 unsigned int format
= 0;
3371 } of_fmt_table
[] = {
3372 { "i2s", SND_SOC_DAIFMT_I2S
},
3373 { "right_j", SND_SOC_DAIFMT_RIGHT_J
},
3374 { "left_j", SND_SOC_DAIFMT_LEFT_J
},
3375 { "dsp_a", SND_SOC_DAIFMT_DSP_A
},
3376 { "dsp_b", SND_SOC_DAIFMT_DSP_B
},
3377 { "ac97", SND_SOC_DAIFMT_AC97
},
3378 { "pdm", SND_SOC_DAIFMT_PDM
},
3379 { "msb", SND_SOC_DAIFMT_MSB
},
3380 { "lsb", SND_SOC_DAIFMT_LSB
},
3387 * check "[prefix]format = xxx"
3388 * SND_SOC_DAIFMT_FORMAT_MASK area
3390 snprintf(prop
, sizeof(prop
), "%sformat", prefix
);
3391 ret
= of_property_read_string(np
, prop
, &str
);
3393 for (i
= 0; i
< ARRAY_SIZE(of_fmt_table
); i
++) {
3394 if (strcmp(str
, of_fmt_table
[i
].name
) == 0) {
3395 format
|= of_fmt_table
[i
].val
;
3402 * check "[prefix]continuous-clock"
3403 * SND_SOC_DAIFMT_CLOCK_MASK area
3405 snprintf(prop
, sizeof(prop
), "%scontinuous-clock", prefix
);
3406 if (of_get_property(np
, prop
, NULL
))
3407 format
|= SND_SOC_DAIFMT_CONT
;
3409 format
|= SND_SOC_DAIFMT_GATED
;
3412 * check "[prefix]bitclock-inversion"
3413 * check "[prefix]frame-inversion"
3414 * SND_SOC_DAIFMT_INV_MASK area
3416 snprintf(prop
, sizeof(prop
), "%sbitclock-inversion", prefix
);
3417 bit
= !!of_get_property(np
, prop
, NULL
);
3419 snprintf(prop
, sizeof(prop
), "%sframe-inversion", prefix
);
3420 frame
= !!of_get_property(np
, prop
, NULL
);
3422 switch ((bit
<< 4) + frame
) {
3424 format
|= SND_SOC_DAIFMT_IB_IF
;
3427 format
|= SND_SOC_DAIFMT_IB_NF
;
3430 format
|= SND_SOC_DAIFMT_NB_IF
;
3433 /* SND_SOC_DAIFMT_NB_NF is default */
3438 * check "[prefix]bitclock-master"
3439 * check "[prefix]frame-master"
3440 * SND_SOC_DAIFMT_MASTER_MASK area
3442 snprintf(prop
, sizeof(prop
), "%sbitclock-master", prefix
);
3443 bit
= !!of_get_property(np
, prop
, NULL
);
3444 if (bit
&& bitclkmaster
)
3445 *bitclkmaster
= of_parse_phandle(np
, prop
, 0);
3447 snprintf(prop
, sizeof(prop
), "%sframe-master", prefix
);
3448 frame
= !!of_get_property(np
, prop
, NULL
);
3449 if (frame
&& framemaster
)
3450 *framemaster
= of_parse_phandle(np
, prop
, 0);
3452 switch ((bit
<< 4) + frame
) {
3454 format
|= SND_SOC_DAIFMT_CBM_CFM
;
3457 format
|= SND_SOC_DAIFMT_CBM_CFS
;
3460 format
|= SND_SOC_DAIFMT_CBS_CFM
;
3463 format
|= SND_SOC_DAIFMT_CBS_CFS
;
3469 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt
);
3471 static int snd_soc_get_dai_name(struct of_phandle_args
*args
,
3472 const char **dai_name
)
3474 struct snd_soc_component
*pos
;
3475 struct device_node
*component_of_node
;
3476 int ret
= -EPROBE_DEFER
;
3478 mutex_lock(&client_mutex
);
3479 list_for_each_entry(pos
, &component_list
, list
) {
3480 component_of_node
= pos
->dev
->of_node
;
3481 if (!component_of_node
&& pos
->dev
->parent
)
3482 component_of_node
= pos
->dev
->parent
->of_node
;
3484 if (component_of_node
!= args
->np
)
3487 if (pos
->driver
->of_xlate_dai_name
) {
3488 ret
= pos
->driver
->of_xlate_dai_name(pos
,
3494 switch (args
->args_count
) {
3496 id
= 0; /* same as dai_drv[0] */
3506 if (id
< 0 || id
>= pos
->num_dai
) {
3513 *dai_name
= pos
->dai_drv
[id
].name
;
3515 *dai_name
= pos
->name
;
3520 mutex_unlock(&client_mutex
);
3524 int snd_soc_of_get_dai_name(struct device_node
*of_node
,
3525 const char **dai_name
)
3527 struct of_phandle_args args
;
3530 ret
= of_parse_phandle_with_args(of_node
, "sound-dai",
3531 "#sound-dai-cells", 0, &args
);
3535 ret
= snd_soc_get_dai_name(&args
, dai_name
);
3537 of_node_put(args
.np
);
3541 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name
);
3544 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3546 * @of_node: Device node
3547 * @dai_link: DAI link
3549 * Builds an array of CODEC DAI components from the DAI link property
3551 * The array is set in the DAI link and the number of DAIs is set accordingly.
3552 * The device nodes in the array (of_node) must be dereferenced by the caller.
3554 * Returns 0 for success
3556 int snd_soc_of_get_dai_link_codecs(struct device
*dev
,
3557 struct device_node
*of_node
,
3558 struct snd_soc_dai_link
*dai_link
)
3560 struct of_phandle_args args
;
3561 struct snd_soc_dai_link_component
*component
;
3563 int index
, num_codecs
, ret
;
3565 /* Count the number of CODECs */
3567 num_codecs
= of_count_phandle_with_args(of_node
, name
,
3568 "#sound-dai-cells");
3569 if (num_codecs
<= 0) {
3570 if (num_codecs
== -ENOENT
)
3571 dev_err(dev
, "No 'sound-dai' property\n");
3573 dev_err(dev
, "Bad phandle in 'sound-dai'\n");
3576 component
= devm_kzalloc(dev
,
3577 sizeof *component
* num_codecs
,
3581 dai_link
->codecs
= component
;
3582 dai_link
->num_codecs
= num_codecs
;
3584 /* Parse the list */
3585 for (index
= 0, component
= dai_link
->codecs
;
3586 index
< dai_link
->num_codecs
;
3587 index
++, component
++) {
3588 ret
= of_parse_phandle_with_args(of_node
, name
,
3593 component
->of_node
= args
.np
;
3594 ret
= snd_soc_get_dai_name(&args
, &component
->dai_name
);
3600 for (index
= 0, component
= dai_link
->codecs
;
3601 index
< dai_link
->num_codecs
;
3602 index
++, component
++) {
3603 if (!component
->of_node
)
3605 of_node_put(component
->of_node
);
3606 component
->of_node
= NULL
;
3608 dai_link
->codecs
= NULL
;
3609 dai_link
->num_codecs
= 0;
3612 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs
);
3614 static int __init
snd_soc_init(void)
3616 snd_soc_debugfs_init();
3617 snd_soc_util_init();
3619 return platform_driver_register(&soc_driver
);
3621 module_init(snd_soc_init
);
3623 static void __exit
snd_soc_exit(void)
3625 snd_soc_util_exit();
3626 snd_soc_debugfs_exit();
3628 #ifdef CONFIG_DEBUG_FS
3630 platform_driver_unregister(&soc_driver
);
3632 module_exit(snd_soc_exit
);
3634 /* Module information */
3635 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3636 MODULE_DESCRIPTION("ALSA SoC Core");
3637 MODULE_LICENSE("GPL");
3638 MODULE_ALIAS("platform:soc-audio");