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/ctype.h>
34 #include <linux/slab.h>
36 #include <sound/ac97_codec.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/initval.h>
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/asoc.h>
49 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq
);
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(card_list
);
58 static LIST_HEAD(dai_list
);
59 static LIST_HEAD(platform_list
);
60 static LIST_HEAD(codec_list
);
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
67 static int pmdown_time
= 5000;
68 module_param(pmdown_time
, int, 0);
69 MODULE_PARM_DESC(pmdown_time
, "DAPM stream powerdown time (msecs)");
71 /* returns the minimum number of bytes needed to represent
72 * a particular given value */
73 static int min_bytes_needed(unsigned long val
)
78 for (i
= (sizeof val
* 8) - 1; i
>= 0; --i
, ++c
)
81 c
= (sizeof val
* 8) - c
;
89 /* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91 static int format_register_str(struct snd_soc_codec
*codec
,
92 unsigned int reg
, char *buf
, size_t len
)
94 int wordsize
= min_bytes_needed(codec
->driver
->reg_cache_size
) * 2;
95 int regsize
= codec
->driver
->reg_word_size
* 2;
98 char regbuf
[regsize
+ 1];
100 /* since tmpbuf is allocated on the stack, warn the callers if they
101 * try to abuse this function */
104 /* +2 for ': ' and + 1 for '\n' */
105 if (wordsize
+ regsize
+ 2 + 1 != len
)
108 ret
= snd_soc_read(codec
, reg
);
110 memset(regbuf
, 'X', regsize
);
111 regbuf
[regsize
] = '\0';
113 snprintf(regbuf
, regsize
+ 1, "%.*x", regsize
, ret
);
116 /* prepare the buffer */
117 snprintf(tmpbuf
, len
+ 1, "%.*x: %s\n", wordsize
, reg
, regbuf
);
118 /* copy it back to the caller without the '\0' */
119 memcpy(buf
, tmpbuf
, len
);
124 /* codec register dump */
125 static ssize_t
soc_codec_reg_show(struct snd_soc_codec
*codec
, char *buf
,
126 size_t count
, loff_t pos
)
129 int wordsize
, regsize
;
134 wordsize
= min_bytes_needed(codec
->driver
->reg_cache_size
) * 2;
135 regsize
= codec
->driver
->reg_word_size
* 2;
137 len
= wordsize
+ regsize
+ 2 + 1;
139 if (!codec
->driver
->reg_cache_size
)
142 if (codec
->driver
->reg_cache_step
)
143 step
= codec
->driver
->reg_cache_step
;
145 for (i
= 0; i
< codec
->driver
->reg_cache_size
; i
+= step
) {
146 if (!snd_soc_codec_readable_register(codec
, i
))
148 if (codec
->driver
->display_register
) {
149 count
+= codec
->driver
->display_register(codec
, buf
+ count
,
150 PAGE_SIZE
- count
, i
);
152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
155 if (total
+ len
>= count
- 1)
157 format_register_str(codec
, i
, buf
+ total
, len
);
164 total
= min(total
, count
- 1);
169 static ssize_t
codec_reg_show(struct device
*dev
,
170 struct device_attribute
*attr
, char *buf
)
172 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
174 return soc_codec_reg_show(rtd
->codec
, buf
, PAGE_SIZE
, 0);
177 static DEVICE_ATTR(codec_reg
, 0444, codec_reg_show
, NULL
);
179 static ssize_t
pmdown_time_show(struct device
*dev
,
180 struct device_attribute
*attr
, char *buf
)
182 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
184 return sprintf(buf
, "%ld\n", rtd
->pmdown_time
);
187 static ssize_t
pmdown_time_set(struct device
*dev
,
188 struct device_attribute
*attr
,
189 const char *buf
, size_t count
)
191 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
194 ret
= strict_strtol(buf
, 10, &rtd
->pmdown_time
);
201 static DEVICE_ATTR(pmdown_time
, 0644, pmdown_time_show
, pmdown_time_set
);
203 #ifdef CONFIG_DEBUG_FS
204 static int codec_reg_open_file(struct inode
*inode
, struct file
*file
)
206 file
->private_data
= inode
->i_private
;
210 static ssize_t
codec_reg_read_file(struct file
*file
, char __user
*user_buf
,
211 size_t count
, loff_t
*ppos
)
214 struct snd_soc_codec
*codec
= file
->private_data
;
217 if (*ppos
< 0 || !count
)
220 buf
= kmalloc(count
, GFP_KERNEL
);
224 ret
= soc_codec_reg_show(codec
, buf
, count
, *ppos
);
226 if (copy_to_user(user_buf
, buf
, ret
)) {
237 static ssize_t
codec_reg_write_file(struct file
*file
,
238 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
243 unsigned long reg
, value
;
244 struct snd_soc_codec
*codec
= file
->private_data
;
246 buf_size
= min(count
, (sizeof(buf
)-1));
247 if (copy_from_user(buf
, user_buf
, buf_size
))
251 while (*start
== ' ')
253 reg
= simple_strtoul(start
, &start
, 16);
254 while (*start
== ' ')
256 if (strict_strtoul(start
, 16, &value
))
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER
);
262 snd_soc_write(codec
, reg
, value
);
266 static const struct file_operations codec_reg_fops
= {
267 .open
= codec_reg_open_file
,
268 .read
= codec_reg_read_file
,
269 .write
= codec_reg_write_file
,
270 .llseek
= default_llseek
,
273 static void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
275 struct dentry
*debugfs_card_root
= codec
->card
->debugfs_card_root
;
277 codec
->debugfs_codec_root
= debugfs_create_dir(codec
->name
,
279 if (!codec
->debugfs_codec_root
) {
281 "ASoC: Failed to create codec debugfs directory\n");
285 debugfs_create_bool("cache_sync", 0444, codec
->debugfs_codec_root
,
287 debugfs_create_bool("cache_only", 0444, codec
->debugfs_codec_root
,
290 codec
->debugfs_reg
= debugfs_create_file("codec_reg", 0644,
291 codec
->debugfs_codec_root
,
292 codec
, &codec_reg_fops
);
293 if (!codec
->debugfs_reg
)
295 "ASoC: Failed to create codec register debugfs file\n");
297 snd_soc_dapm_debugfs_init(&codec
->dapm
, codec
->debugfs_codec_root
);
300 static void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
302 debugfs_remove_recursive(codec
->debugfs_codec_root
);
305 static ssize_t
codec_list_read_file(struct file
*file
, char __user
*user_buf
,
306 size_t count
, loff_t
*ppos
)
308 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
309 ssize_t len
, ret
= 0;
310 struct snd_soc_codec
*codec
;
315 list_for_each_entry(codec
, &codec_list
, list
) {
316 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
320 if (ret
> PAGE_SIZE
) {
327 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
334 static const struct file_operations codec_list_fops
= {
335 .read
= codec_list_read_file
,
336 .llseek
= default_llseek
,/* read accesses f_pos */
339 static ssize_t
dai_list_read_file(struct file
*file
, char __user
*user_buf
,
340 size_t count
, loff_t
*ppos
)
342 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
343 ssize_t len
, ret
= 0;
344 struct snd_soc_dai
*dai
;
349 list_for_each_entry(dai
, &dai_list
, list
) {
350 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n", dai
->name
);
353 if (ret
> PAGE_SIZE
) {
359 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
366 static const struct file_operations dai_list_fops
= {
367 .read
= dai_list_read_file
,
368 .llseek
= default_llseek
,/* read accesses f_pos */
371 static ssize_t
platform_list_read_file(struct file
*file
,
372 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_platform
*platform
;
382 list_for_each_entry(platform
, &platform_list
, list
) {
383 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
387 if (ret
> PAGE_SIZE
) {
393 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
400 static const struct file_operations platform_list_fops
= {
401 .read
= platform_list_read_file
,
402 .llseek
= default_llseek
,/* read accesses f_pos */
405 static void soc_init_card_debugfs(struct snd_soc_card
*card
)
407 card
->debugfs_card_root
= debugfs_create_dir(card
->name
,
408 snd_soc_debugfs_root
);
409 if (!card
->debugfs_card_root
) {
411 "ASoC: Failed to create card debugfs directory\n");
415 card
->debugfs_pop_time
= debugfs_create_u32("dapm_pop_time", 0644,
416 card
->debugfs_card_root
,
418 if (!card
->debugfs_pop_time
)
420 "Failed to create pop time debugfs file\n");
423 static void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
425 debugfs_remove_recursive(card
->debugfs_card_root
);
430 static inline void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
434 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
438 static inline void soc_init_card_debugfs(struct snd_soc_card
*card
)
442 static inline void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
447 #ifdef CONFIG_SND_SOC_AC97_BUS
448 /* unregister ac97 codec */
449 static int soc_ac97_dev_unregister(struct snd_soc_codec
*codec
)
451 if (codec
->ac97
->dev
.bus
)
452 device_unregister(&codec
->ac97
->dev
);
456 /* stop no dev release warning */
457 static void soc_ac97_device_release(struct device
*dev
){}
459 /* register ac97 codec to bus */
460 static int soc_ac97_dev_register(struct snd_soc_codec
*codec
)
464 codec
->ac97
->dev
.bus
= &ac97_bus_type
;
465 codec
->ac97
->dev
.parent
= codec
->card
->dev
;
466 codec
->ac97
->dev
.release
= soc_ac97_device_release
;
468 dev_set_name(&codec
->ac97
->dev
, "%d-%d:%s",
469 codec
->card
->snd_card
->number
, 0, codec
->name
);
470 err
= device_register(&codec
->ac97
->dev
);
472 snd_printk(KERN_ERR
"Can't register ac97 bus\n");
473 codec
->ac97
->dev
.bus
= NULL
;
480 #ifdef CONFIG_PM_SLEEP
481 /* powers down audio subsystem for suspend */
482 int snd_soc_suspend(struct device
*dev
)
484 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
485 struct snd_soc_codec
*codec
;
488 /* If the initialization of this soc device failed, there is no codec
489 * associated with it. Just bail out in this case.
491 if (list_empty(&card
->codec_dev_list
))
494 /* Due to the resume being scheduled into a workqueue we could
495 * suspend before that's finished - wait for it to complete.
497 snd_power_lock(card
->snd_card
);
498 snd_power_wait(card
->snd_card
, SNDRV_CTL_POWER_D0
);
499 snd_power_unlock(card
->snd_card
);
501 /* we're going to block userspace touching us until resume completes */
502 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D3hot
);
504 /* mute any active DACs */
505 for (i
= 0; i
< card
->num_rtd
; i
++) {
506 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
507 struct snd_soc_dai_driver
*drv
= dai
->driver
;
509 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
512 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
513 drv
->ops
->digital_mute(dai
, 1);
516 /* suspend all pcms */
517 for (i
= 0; i
< card
->num_rtd
; i
++) {
518 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
521 snd_pcm_suspend_all(card
->rtd
[i
].pcm
);
524 if (card
->suspend_pre
)
525 card
->suspend_pre(card
);
527 for (i
= 0; i
< card
->num_rtd
; i
++) {
528 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
529 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
531 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
534 if (cpu_dai
->driver
->suspend
&& !cpu_dai
->driver
->ac97_control
)
535 cpu_dai
->driver
->suspend(cpu_dai
);
536 if (platform
->driver
->suspend
&& !platform
->suspended
) {
537 platform
->driver
->suspend(cpu_dai
);
538 platform
->suspended
= 1;
542 /* close any waiting streams and save state */
543 for (i
= 0; i
< card
->num_rtd
; i
++) {
544 flush_delayed_work_sync(&card
->rtd
[i
].delayed_work
);
545 card
->rtd
[i
].codec
->dapm
.suspend_bias_level
= card
->rtd
[i
].codec
->dapm
.bias_level
;
548 for (i
= 0; i
< card
->num_rtd
; i
++) {
549 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
551 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
554 if (driver
->playback
.stream_name
!= NULL
)
555 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
556 SND_SOC_DAPM_STREAM_SUSPEND
);
558 if (driver
->capture
.stream_name
!= NULL
)
559 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
560 SND_SOC_DAPM_STREAM_SUSPEND
);
563 /* suspend all CODECs */
564 list_for_each_entry(codec
, &card
->codec_dev_list
, card_list
) {
565 /* If there are paths active then the CODEC will be held with
566 * bias _ON and should not be suspended. */
567 if (!codec
->suspended
&& codec
->driver
->suspend
) {
568 switch (codec
->dapm
.bias_level
) {
569 case SND_SOC_BIAS_STANDBY
:
571 * If the CODEC is capable of idle
572 * bias off then being in STANDBY
573 * means it's doing something,
574 * otherwise fall through.
576 if (codec
->dapm
.idle_bias_off
) {
578 "idle_bias_off CODEC on over suspend\n");
581 case SND_SOC_BIAS_OFF
:
582 codec
->driver
->suspend(codec
);
583 codec
->suspended
= 1;
584 codec
->cache_sync
= 1;
587 dev_dbg(codec
->dev
, "CODEC is on over suspend\n");
593 for (i
= 0; i
< card
->num_rtd
; i
++) {
594 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
596 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
599 if (cpu_dai
->driver
->suspend
&& cpu_dai
->driver
->ac97_control
)
600 cpu_dai
->driver
->suspend(cpu_dai
);
603 if (card
->suspend_post
)
604 card
->suspend_post(card
);
608 EXPORT_SYMBOL_GPL(snd_soc_suspend
);
610 /* deferred resume work, so resume can complete before we finished
611 * setting our codec back up, which can be very slow on I2C
613 static void soc_resume_deferred(struct work_struct
*work
)
615 struct snd_soc_card
*card
=
616 container_of(work
, struct snd_soc_card
, deferred_resume_work
);
617 struct snd_soc_codec
*codec
;
620 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
621 * so userspace apps are blocked from touching us
624 dev_dbg(card
->dev
, "starting resume work\n");
626 /* Bring us up into D2 so that DAPM starts enabling things */
627 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D2
);
629 if (card
->resume_pre
)
630 card
->resume_pre(card
);
632 /* resume AC97 DAIs */
633 for (i
= 0; i
< card
->num_rtd
; i
++) {
634 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
636 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
639 if (cpu_dai
->driver
->resume
&& cpu_dai
->driver
->ac97_control
)
640 cpu_dai
->driver
->resume(cpu_dai
);
643 list_for_each_entry(codec
, &card
->codec_dev_list
, card_list
) {
644 /* If the CODEC was idle over suspend then it will have been
645 * left with bias OFF or STANDBY and suspended so we must now
646 * resume. Otherwise the suspend was suppressed.
648 if (codec
->driver
->resume
&& codec
->suspended
) {
649 switch (codec
->dapm
.bias_level
) {
650 case SND_SOC_BIAS_STANDBY
:
651 case SND_SOC_BIAS_OFF
:
652 codec
->driver
->resume(codec
);
653 codec
->suspended
= 0;
656 dev_dbg(codec
->dev
, "CODEC was on over suspend\n");
662 for (i
= 0; i
< card
->num_rtd
; i
++) {
663 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
665 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
668 if (driver
->playback
.stream_name
!= NULL
)
669 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
670 SND_SOC_DAPM_STREAM_RESUME
);
672 if (driver
->capture
.stream_name
!= NULL
)
673 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
674 SND_SOC_DAPM_STREAM_RESUME
);
677 /* unmute any active DACs */
678 for (i
= 0; i
< card
->num_rtd
; i
++) {
679 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
680 struct snd_soc_dai_driver
*drv
= dai
->driver
;
682 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
685 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
686 drv
->ops
->digital_mute(dai
, 0);
689 for (i
= 0; i
< card
->num_rtd
; i
++) {
690 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
691 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
693 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
696 if (cpu_dai
->driver
->resume
&& !cpu_dai
->driver
->ac97_control
)
697 cpu_dai
->driver
->resume(cpu_dai
);
698 if (platform
->driver
->resume
&& platform
->suspended
) {
699 platform
->driver
->resume(cpu_dai
);
700 platform
->suspended
= 0;
704 if (card
->resume_post
)
705 card
->resume_post(card
);
707 dev_dbg(card
->dev
, "resume work completed\n");
709 /* userspace can access us now we are back as we were before */
710 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D0
);
713 /* powers up audio subsystem after a suspend */
714 int snd_soc_resume(struct device
*dev
)
716 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
717 int i
, ac97_control
= 0;
719 /* If the initialization of this soc device failed, there is no codec
720 * associated with it. Just bail out in this case.
722 if (list_empty(&card
->codec_dev_list
))
725 /* AC97 devices might have other drivers hanging off them so
726 * need to resume immediately. Other drivers don't have that
727 * problem and may take a substantial amount of time to resume
728 * due to I/O costs and anti-pop so handle them out of line.
730 for (i
= 0; i
< card
->num_rtd
; i
++) {
731 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
732 ac97_control
|= cpu_dai
->driver
->ac97_control
;
735 dev_dbg(dev
, "Resuming AC97 immediately\n");
736 soc_resume_deferred(&card
->deferred_resume_work
);
738 dev_dbg(dev
, "Scheduling resume work\n");
739 if (!schedule_work(&card
->deferred_resume_work
))
740 dev_err(dev
, "resume work item may be lost\n");
745 EXPORT_SYMBOL_GPL(snd_soc_resume
);
747 #define snd_soc_suspend NULL
748 #define snd_soc_resume NULL
751 static const struct snd_soc_dai_ops null_dai_ops
= {
754 static int soc_bind_dai_link(struct snd_soc_card
*card
, int num
)
756 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
757 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
758 struct snd_soc_codec
*codec
;
759 struct snd_soc_platform
*platform
;
760 struct snd_soc_dai
*codec_dai
, *cpu_dai
;
761 const char *platform_name
;
765 dev_dbg(card
->dev
, "binding %s at idx %d\n", dai_link
->name
, num
);
767 /* do we already have the CPU DAI for this link ? */
771 /* no, then find CPU DAI from registered DAIs*/
772 list_for_each_entry(cpu_dai
, &dai_list
, list
) {
773 if (dai_link
->cpu_dai_of_node
) {
774 if (cpu_dai
->dev
->of_node
!= dai_link
->cpu_dai_of_node
)
777 if (strcmp(cpu_dai
->name
, dai_link
->cpu_dai_name
))
781 rtd
->cpu_dai
= cpu_dai
;
784 dev_dbg(card
->dev
, "CPU DAI %s not registered\n",
785 dai_link
->cpu_dai_name
);
788 /* do we already have the CODEC for this link ? */
793 /* no, then find CODEC from registered CODECs*/
794 list_for_each_entry(codec
, &codec_list
, list
) {
795 if (dai_link
->codec_of_node
) {
796 if (codec
->dev
->of_node
!= dai_link
->codec_of_node
)
799 if (strcmp(codec
->name
, dai_link
->codec_name
))
806 * CODEC found, so find CODEC DAI from registered DAIs from
809 list_for_each_entry(codec_dai
, &dai_list
, list
) {
810 if (codec
->dev
== codec_dai
->dev
&&
811 !strcmp(codec_dai
->name
,
812 dai_link
->codec_dai_name
)) {
814 rtd
->codec_dai
= codec_dai
;
818 dev_dbg(card
->dev
, "CODEC DAI %s not registered\n",
819 dai_link
->codec_dai_name
);
823 dev_dbg(card
->dev
, "CODEC %s not registered\n",
824 dai_link
->codec_name
);
827 /* do we need a platform? */
831 /* if there's no platform we match on the empty platform */
832 platform_name
= dai_link
->platform_name
;
833 if (!platform_name
&& !dai_link
->platform_of_node
)
834 platform_name
= "snd-soc-dummy";
836 /* no, then find one from the set of registered platforms */
837 list_for_each_entry(platform
, &platform_list
, list
) {
838 if (dai_link
->platform_of_node
) {
839 if (platform
->dev
->of_node
!=
840 dai_link
->platform_of_node
)
843 if (strcmp(platform
->name
, platform_name
))
847 rtd
->platform
= platform
;
851 dev_dbg(card
->dev
, "platform %s not registered\n",
852 dai_link
->platform_name
);
856 /* mark rtd as complete if we found all 4 of our client devices */
857 if (rtd
->codec
&& rtd
->codec_dai
&& rtd
->platform
&& rtd
->cpu_dai
) {
864 static void soc_remove_codec(struct snd_soc_codec
*codec
)
868 if (codec
->driver
->remove
) {
869 err
= codec
->driver
->remove(codec
);
872 "asoc: failed to remove %s: %d\n",
876 /* Make sure all DAPM widgets are freed */
877 snd_soc_dapm_free(&codec
->dapm
);
879 soc_cleanup_codec_debugfs(codec
);
881 list_del(&codec
->card_list
);
882 module_put(codec
->dev
->driver
->owner
);
885 static void soc_remove_dai_link(struct snd_soc_card
*card
, int num
, int order
)
887 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
888 struct snd_soc_codec
*codec
= rtd
->codec
;
889 struct snd_soc_platform
*platform
= rtd
->platform
;
890 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
893 /* unregister the rtd device */
894 if (rtd
->dev_registered
) {
895 device_remove_file(rtd
->dev
, &dev_attr_pmdown_time
);
896 device_remove_file(rtd
->dev
, &dev_attr_codec_reg
);
897 device_unregister(rtd
->dev
);
898 rtd
->dev_registered
= 0;
901 /* remove the CODEC DAI */
902 if (codec_dai
&& codec_dai
->probed
&&
903 codec_dai
->driver
->remove_order
== order
) {
904 if (codec_dai
->driver
->remove
) {
905 err
= codec_dai
->driver
->remove(codec_dai
);
907 printk(KERN_ERR
"asoc: failed to remove %s\n", codec_dai
->name
);
909 codec_dai
->probed
= 0;
910 list_del(&codec_dai
->card_list
);
913 /* remove the platform */
914 if (platform
&& platform
->probed
&&
915 platform
->driver
->remove_order
== order
) {
916 if (platform
->driver
->remove
) {
917 err
= platform
->driver
->remove(platform
);
919 printk(KERN_ERR
"asoc: failed to remove %s\n", platform
->name
);
922 /* Make sure all DAPM widgets are freed */
923 snd_soc_dapm_free(&platform
->dapm
);
925 platform
->probed
= 0;
926 list_del(&platform
->card_list
);
927 module_put(platform
->dev
->driver
->owner
);
930 /* remove the CODEC */
931 if (codec
&& codec
->probed
&&
932 codec
->driver
->remove_order
== order
)
933 soc_remove_codec(codec
);
935 /* remove the cpu_dai */
936 if (cpu_dai
&& cpu_dai
->probed
&&
937 cpu_dai
->driver
->remove_order
== order
) {
938 if (cpu_dai
->driver
->remove
) {
939 err
= cpu_dai
->driver
->remove(cpu_dai
);
941 printk(KERN_ERR
"asoc: failed to remove %s\n", cpu_dai
->name
);
944 list_del(&cpu_dai
->card_list
);
945 module_put(cpu_dai
->dev
->driver
->owner
);
949 static void soc_remove_dai_links(struct snd_soc_card
*card
)
953 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
955 for (dai
= 0; dai
< card
->num_rtd
; dai
++)
956 soc_remove_dai_link(card
, dai
, order
);
961 static void soc_set_name_prefix(struct snd_soc_card
*card
,
962 struct snd_soc_codec
*codec
)
966 if (card
->codec_conf
== NULL
)
969 for (i
= 0; i
< card
->num_configs
; i
++) {
970 struct snd_soc_codec_conf
*map
= &card
->codec_conf
[i
];
971 if (map
->dev_name
&& !strcmp(codec
->name
, map
->dev_name
)) {
972 codec
->name_prefix
= map
->name_prefix
;
978 static int soc_probe_codec(struct snd_soc_card
*card
,
979 struct snd_soc_codec
*codec
)
982 const struct snd_soc_codec_driver
*driver
= codec
->driver
;
985 codec
->dapm
.card
= card
;
986 soc_set_name_prefix(card
, codec
);
988 if (!try_module_get(codec
->dev
->driver
->owner
))
991 soc_init_codec_debugfs(codec
);
993 if (driver
->dapm_widgets
)
994 snd_soc_dapm_new_controls(&codec
->dapm
, driver
->dapm_widgets
,
995 driver
->num_dapm_widgets
);
997 codec
->dapm
.idle_bias_off
= driver
->idle_bias_off
;
1000 ret
= driver
->probe(codec
);
1003 "asoc: failed to probe CODEC %s: %d\n",
1009 if (driver
->controls
)
1010 snd_soc_add_controls(codec
, driver
->controls
,
1011 driver
->num_controls
);
1012 if (driver
->dapm_routes
)
1013 snd_soc_dapm_add_routes(&codec
->dapm
, driver
->dapm_routes
,
1014 driver
->num_dapm_routes
);
1016 /* mark codec as probed and add to card codec list */
1018 list_add(&codec
->card_list
, &card
->codec_dev_list
);
1019 list_add(&codec
->dapm
.list
, &card
->dapm_list
);
1024 soc_cleanup_codec_debugfs(codec
);
1025 module_put(codec
->dev
->driver
->owner
);
1030 static int soc_probe_platform(struct snd_soc_card
*card
,
1031 struct snd_soc_platform
*platform
)
1034 const struct snd_soc_platform_driver
*driver
= platform
->driver
;
1036 platform
->card
= card
;
1037 platform
->dapm
.card
= card
;
1039 if (!try_module_get(platform
->dev
->driver
->owner
))
1042 if (driver
->dapm_widgets
)
1043 snd_soc_dapm_new_controls(&platform
->dapm
,
1044 driver
->dapm_widgets
, driver
->num_dapm_widgets
);
1046 if (driver
->probe
) {
1047 ret
= driver
->probe(platform
);
1049 dev_err(platform
->dev
,
1050 "asoc: failed to probe platform %s: %d\n",
1051 platform
->name
, ret
);
1056 if (driver
->controls
)
1057 snd_soc_add_platform_controls(platform
, driver
->controls
,
1058 driver
->num_controls
);
1059 if (driver
->dapm_routes
)
1060 snd_soc_dapm_add_routes(&platform
->dapm
, driver
->dapm_routes
,
1061 driver
->num_dapm_routes
);
1063 /* mark platform as probed and add to card platform list */
1064 platform
->probed
= 1;
1065 list_add(&platform
->card_list
, &card
->platform_dev_list
);
1066 list_add(&platform
->dapm
.list
, &card
->dapm_list
);
1071 module_put(platform
->dev
->driver
->owner
);
1076 static void rtd_release(struct device
*dev
)
1081 static int soc_post_component_init(struct snd_soc_card
*card
,
1082 struct snd_soc_codec
*codec
,
1083 int num
, int dailess
)
1085 struct snd_soc_dai_link
*dai_link
= NULL
;
1086 struct snd_soc_aux_dev
*aux_dev
= NULL
;
1087 struct snd_soc_pcm_runtime
*rtd
;
1088 const char *temp
, *name
;
1092 dai_link
= &card
->dai_link
[num
];
1093 rtd
= &card
->rtd
[num
];
1094 name
= dai_link
->name
;
1096 aux_dev
= &card
->aux_dev
[num
];
1097 rtd
= &card
->rtd_aux
[num
];
1098 name
= aux_dev
->name
;
1102 /* Make sure all DAPM widgets are instantiated */
1103 snd_soc_dapm_new_widgets(&codec
->dapm
);
1105 /* machine controls, routes and widgets are not prefixed */
1106 temp
= codec
->name_prefix
;
1107 codec
->name_prefix
= NULL
;
1109 /* do machine specific initialization */
1110 if (!dailess
&& dai_link
->init
)
1111 ret
= dai_link
->init(rtd
);
1112 else if (dailess
&& aux_dev
->init
)
1113 ret
= aux_dev
->init(&codec
->dapm
);
1115 dev_err(card
->dev
, "asoc: failed to init %s: %d\n", name
, ret
);
1118 codec
->name_prefix
= temp
;
1120 /* register the rtd device */
1123 rtd
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
1126 device_initialize(rtd
->dev
);
1127 rtd
->dev
->parent
= card
->dev
;
1128 rtd
->dev
->release
= rtd_release
;
1129 rtd
->dev
->init_name
= name
;
1130 dev_set_drvdata(rtd
->dev
, rtd
);
1131 mutex_init(&rtd
->pcm_mutex
);
1132 ret
= device_add(rtd
->dev
);
1135 "asoc: failed to register runtime device: %d\n", ret
);
1138 rtd
->dev_registered
= 1;
1140 /* add DAPM sysfs entries for this codec */
1141 ret
= snd_soc_dapm_sys_add(rtd
->dev
);
1144 "asoc: failed to add codec dapm sysfs entries: %d\n",
1147 /* add codec sysfs entries */
1148 ret
= device_create_file(rtd
->dev
, &dev_attr_codec_reg
);
1151 "asoc: failed to add codec sysfs files: %d\n", ret
);
1156 static int soc_probe_dai_link(struct snd_soc_card
*card
, int num
, int order
)
1158 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1159 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1160 struct snd_soc_codec
*codec
= rtd
->codec
;
1161 struct snd_soc_platform
*platform
= rtd
->platform
;
1162 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1165 dev_dbg(card
->dev
, "probe %s dai link %d late %d\n",
1166 card
->name
, num
, order
);
1168 /* config components */
1169 codec_dai
->codec
= codec
;
1170 cpu_dai
->platform
= platform
;
1171 codec_dai
->card
= card
;
1172 cpu_dai
->card
= card
;
1174 /* set default power off timeout */
1175 rtd
->pmdown_time
= pmdown_time
;
1177 /* probe the cpu_dai */
1178 if (!cpu_dai
->probed
&&
1179 cpu_dai
->driver
->probe_order
== order
) {
1180 if (!try_module_get(cpu_dai
->dev
->driver
->owner
))
1183 if (cpu_dai
->driver
->probe
) {
1184 ret
= cpu_dai
->driver
->probe(cpu_dai
);
1186 printk(KERN_ERR
"asoc: failed to probe CPU DAI %s\n",
1188 module_put(cpu_dai
->dev
->driver
->owner
);
1192 cpu_dai
->probed
= 1;
1193 /* mark cpu_dai as probed and add to card dai list */
1194 list_add(&cpu_dai
->card_list
, &card
->dai_dev_list
);
1197 /* probe the CODEC */
1198 if (!codec
->probed
&&
1199 codec
->driver
->probe_order
== order
) {
1200 ret
= soc_probe_codec(card
, codec
);
1205 /* probe the platform */
1206 if (!platform
->probed
&&
1207 platform
->driver
->probe_order
== order
) {
1208 ret
= soc_probe_platform(card
, platform
);
1213 /* probe the CODEC DAI */
1214 if (!codec_dai
->probed
&& codec_dai
->driver
->probe_order
== order
) {
1215 if (codec_dai
->driver
->probe
) {
1216 ret
= codec_dai
->driver
->probe(codec_dai
);
1218 printk(KERN_ERR
"asoc: failed to probe CODEC DAI %s\n",
1224 /* mark codec_dai as probed and add to card dai list */
1225 codec_dai
->probed
= 1;
1226 list_add(&codec_dai
->card_list
, &card
->dai_dev_list
);
1229 /* complete DAI probe during last probe */
1230 if (order
!= SND_SOC_COMP_ORDER_LAST
)
1233 ret
= soc_post_component_init(card
, codec
, num
, 0);
1237 ret
= device_create_file(rtd
->dev
, &dev_attr_pmdown_time
);
1239 printk(KERN_WARNING
"asoc: failed to add pmdown_time sysfs\n");
1241 /* create the pcm */
1242 ret
= soc_new_pcm(rtd
, num
);
1244 printk(KERN_ERR
"asoc: can't create pcm %s\n", dai_link
->stream_name
);
1248 /* add platform data for AC97 devices */
1249 if (rtd
->codec_dai
->driver
->ac97_control
)
1250 snd_ac97_dev_add_pdata(codec
->ac97
, rtd
->cpu_dai
->ac97_pdata
);
1255 #ifdef CONFIG_SND_SOC_AC97_BUS
1256 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime
*rtd
)
1260 /* Only instantiate AC97 if not already done by the adaptor
1261 * for the generic AC97 subsystem.
1263 if (rtd
->codec_dai
->driver
->ac97_control
&& !rtd
->codec
->ac97_registered
) {
1265 * It is possible that the AC97 device is already registered to
1266 * the device subsystem. This happens when the device is created
1267 * via snd_ac97_mixer(). Currently only SoC codec that does so
1268 * is the generic AC97 glue but others migh emerge.
1270 * In those cases we don't try to register the device again.
1272 if (!rtd
->codec
->ac97_created
)
1275 ret
= soc_ac97_dev_register(rtd
->codec
);
1277 printk(KERN_ERR
"asoc: AC97 device register failed\n");
1281 rtd
->codec
->ac97_registered
= 1;
1286 static void soc_unregister_ac97_dai_link(struct snd_soc_codec
*codec
)
1288 if (codec
->ac97_registered
) {
1289 soc_ac97_dev_unregister(codec
);
1290 codec
->ac97_registered
= 0;
1295 static int soc_probe_aux_dev(struct snd_soc_card
*card
, int num
)
1297 struct snd_soc_aux_dev
*aux_dev
= &card
->aux_dev
[num
];
1298 struct snd_soc_codec
*codec
;
1301 /* find CODEC from registered CODECs*/
1302 list_for_each_entry(codec
, &codec_list
, list
) {
1303 if (!strcmp(codec
->name
, aux_dev
->codec_name
)) {
1304 if (codec
->probed
) {
1306 "asoc: codec already probed");
1313 /* codec not found */
1314 dev_err(card
->dev
, "asoc: codec %s not found", aux_dev
->codec_name
);
1318 ret
= soc_probe_codec(card
, codec
);
1322 ret
= soc_post_component_init(card
, codec
, num
, 1);
1328 static void soc_remove_aux_dev(struct snd_soc_card
*card
, int num
)
1330 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd_aux
[num
];
1331 struct snd_soc_codec
*codec
= rtd
->codec
;
1333 /* unregister the rtd device */
1334 if (rtd
->dev_registered
) {
1335 device_remove_file(rtd
->dev
, &dev_attr_codec_reg
);
1336 device_del(rtd
->dev
);
1337 rtd
->dev_registered
= 0;
1340 if (codec
&& codec
->probed
)
1341 soc_remove_codec(codec
);
1344 static int snd_soc_init_codec_cache(struct snd_soc_codec
*codec
,
1345 enum snd_soc_compress_type compress_type
)
1349 if (codec
->cache_init
)
1352 /* override the compress_type if necessary */
1353 if (compress_type
&& codec
->compress_type
!= compress_type
)
1354 codec
->compress_type
= compress_type
;
1355 ret
= snd_soc_cache_init(codec
);
1357 dev_err(codec
->dev
, "Failed to set cache compression type: %d\n",
1361 codec
->cache_init
= 1;
1365 static void snd_soc_instantiate_card(struct snd_soc_card
*card
)
1367 struct snd_soc_codec
*codec
;
1368 struct snd_soc_codec_conf
*codec_conf
;
1369 enum snd_soc_compress_type compress_type
;
1370 struct snd_soc_dai_link
*dai_link
;
1373 mutex_lock(&card
->mutex
);
1375 if (card
->instantiated
) {
1376 mutex_unlock(&card
->mutex
);
1381 for (i
= 0; i
< card
->num_links
; i
++)
1382 soc_bind_dai_link(card
, i
);
1384 /* bind completed ? */
1385 if (card
->num_rtd
!= card
->num_links
) {
1386 mutex_unlock(&card
->mutex
);
1390 /* initialize the register cache for each available codec */
1391 list_for_each_entry(codec
, &codec_list
, list
) {
1392 if (codec
->cache_init
)
1394 /* by default we don't override the compress_type */
1396 /* check to see if we need to override the compress_type */
1397 for (i
= 0; i
< card
->num_configs
; ++i
) {
1398 codec_conf
= &card
->codec_conf
[i
];
1399 if (!strcmp(codec
->name
, codec_conf
->dev_name
)) {
1400 compress_type
= codec_conf
->compress_type
;
1401 if (compress_type
&& compress_type
1402 != codec
->compress_type
)
1406 ret
= snd_soc_init_codec_cache(codec
, compress_type
);
1408 mutex_unlock(&card
->mutex
);
1413 /* card bind complete so register a sound card */
1414 ret
= snd_card_create(SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
1415 card
->owner
, 0, &card
->snd_card
);
1417 printk(KERN_ERR
"asoc: can't create sound card for card %s\n",
1419 mutex_unlock(&card
->mutex
);
1422 card
->snd_card
->dev
= card
->dev
;
1424 card
->dapm
.bias_level
= SND_SOC_BIAS_OFF
;
1425 card
->dapm
.dev
= card
->dev
;
1426 card
->dapm
.card
= card
;
1427 list_add(&card
->dapm
.list
, &card
->dapm_list
);
1429 #ifdef CONFIG_DEBUG_FS
1430 snd_soc_dapm_debugfs_init(&card
->dapm
, card
->debugfs_card_root
);
1433 #ifdef CONFIG_PM_SLEEP
1434 /* deferred resume work */
1435 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
1438 if (card
->dapm_widgets
)
1439 snd_soc_dapm_new_controls(&card
->dapm
, card
->dapm_widgets
,
1440 card
->num_dapm_widgets
);
1442 /* initialise the sound card only once */
1444 ret
= card
->probe(card
);
1446 goto card_probe_error
;
1449 /* early DAI link probe */
1450 for (order
= SND_SOC_COMP_ORDER_FIRST
; order
<= SND_SOC_COMP_ORDER_LAST
;
1452 for (i
= 0; i
< card
->num_links
; i
++) {
1453 ret
= soc_probe_dai_link(card
, i
, order
);
1455 pr_err("asoc: failed to instantiate card %s: %d\n",
1462 for (i
= 0; i
< card
->num_aux_devs
; i
++) {
1463 ret
= soc_probe_aux_dev(card
, i
);
1465 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1467 goto probe_aux_dev_err
;
1471 /* We should have a non-codec control add function but we don't */
1473 snd_soc_add_controls(list_first_entry(&card
->codec_dev_list
,
1474 struct snd_soc_codec
,
1477 card
->num_controls
);
1479 if (card
->dapm_routes
)
1480 snd_soc_dapm_add_routes(&card
->dapm
, card
->dapm_routes
,
1481 card
->num_dapm_routes
);
1483 snd_soc_dapm_new_widgets(&card
->dapm
);
1485 for (i
= 0; i
< card
->num_links
; i
++) {
1486 dai_link
= &card
->dai_link
[i
];
1488 if (dai_link
->dai_fmt
) {
1489 ret
= snd_soc_dai_set_fmt(card
->rtd
[i
].codec_dai
,
1492 dev_warn(card
->rtd
[i
].codec_dai
->dev
,
1493 "Failed to set DAI format: %d\n",
1496 ret
= snd_soc_dai_set_fmt(card
->rtd
[i
].cpu_dai
,
1499 dev_warn(card
->rtd
[i
].cpu_dai
->dev
,
1500 "Failed to set DAI format: %d\n",
1505 snprintf(card
->snd_card
->shortname
, sizeof(card
->snd_card
->shortname
),
1507 snprintf(card
->snd_card
->longname
, sizeof(card
->snd_card
->longname
),
1508 "%s", card
->long_name
? card
->long_name
: card
->name
);
1509 snprintf(card
->snd_card
->driver
, sizeof(card
->snd_card
->driver
),
1510 "%s", card
->driver_name
? card
->driver_name
: card
->name
);
1511 for (i
= 0; i
< ARRAY_SIZE(card
->snd_card
->driver
); i
++) {
1512 switch (card
->snd_card
->driver
[i
]) {
1518 if (!isalnum(card
->snd_card
->driver
[i
]))
1519 card
->snd_card
->driver
[i
] = '_';
1524 if (card
->late_probe
) {
1525 ret
= card
->late_probe(card
);
1527 dev_err(card
->dev
, "%s late_probe() failed: %d\n",
1529 goto probe_aux_dev_err
;
1533 snd_soc_dapm_new_widgets(&card
->dapm
);
1535 if (card
->fully_routed
)
1536 list_for_each_entry(codec
, &card
->codec_dev_list
, card_list
)
1537 snd_soc_dapm_auto_nc_codec_pins(codec
);
1539 ret
= snd_card_register(card
->snd_card
);
1541 printk(KERN_ERR
"asoc: failed to register soundcard for %s\n", card
->name
);
1542 goto probe_aux_dev_err
;
1545 #ifdef CONFIG_SND_SOC_AC97_BUS
1546 /* register any AC97 codecs */
1547 for (i
= 0; i
< card
->num_rtd
; i
++) {
1548 ret
= soc_register_ac97_dai_link(&card
->rtd
[i
]);
1550 printk(KERN_ERR
"asoc: failed to register AC97 %s\n", card
->name
);
1552 soc_unregister_ac97_dai_link(card
->rtd
[i
].codec
);
1553 goto probe_aux_dev_err
;
1558 card
->instantiated
= 1;
1559 snd_soc_dapm_sync(&card
->dapm
);
1560 mutex_unlock(&card
->mutex
);
1564 for (i
= 0; i
< card
->num_aux_devs
; i
++)
1565 soc_remove_aux_dev(card
, i
);
1568 soc_remove_dai_links(card
);
1574 snd_card_free(card
->snd_card
);
1576 mutex_unlock(&card
->mutex
);
1580 * Attempt to initialise any uninitialised cards. Must be called with
1583 static void snd_soc_instantiate_cards(void)
1585 struct snd_soc_card
*card
;
1586 list_for_each_entry(card
, &card_list
, list
)
1587 snd_soc_instantiate_card(card
);
1590 /* probes a new socdev */
1591 static int soc_probe(struct platform_device
*pdev
)
1593 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1597 * no card, so machine driver should be registering card
1598 * we should not be here in that case so ret error
1603 /* Bodge while we unpick instantiation */
1604 card
->dev
= &pdev
->dev
;
1606 ret
= snd_soc_register_card(card
);
1608 dev_err(&pdev
->dev
, "Failed to register card\n");
1615 static int soc_cleanup_card_resources(struct snd_soc_card
*card
)
1619 /* make sure any delayed work runs */
1620 for (i
= 0; i
< card
->num_rtd
; i
++) {
1621 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1622 flush_delayed_work_sync(&rtd
->delayed_work
);
1625 /* remove auxiliary devices */
1626 for (i
= 0; i
< card
->num_aux_devs
; i
++)
1627 soc_remove_aux_dev(card
, i
);
1629 /* remove and free each DAI */
1630 soc_remove_dai_links(card
);
1632 soc_cleanup_card_debugfs(card
);
1634 /* remove the card */
1638 snd_soc_dapm_free(&card
->dapm
);
1641 snd_card_free(card
->snd_card
);
1646 /* removes a socdev */
1647 static int soc_remove(struct platform_device
*pdev
)
1649 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1651 snd_soc_unregister_card(card
);
1655 int snd_soc_poweroff(struct device
*dev
)
1657 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
1660 if (!card
->instantiated
)
1663 /* Flush out pmdown_time work - we actually do want to run it
1664 * now, we're shutting down so no imminent restart. */
1665 for (i
= 0; i
< card
->num_rtd
; i
++) {
1666 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1667 flush_delayed_work_sync(&rtd
->delayed_work
);
1670 snd_soc_dapm_shutdown(card
);
1674 EXPORT_SYMBOL_GPL(snd_soc_poweroff
);
1676 const struct dev_pm_ops snd_soc_pm_ops
= {
1677 .suspend
= snd_soc_suspend
,
1678 .resume
= snd_soc_resume
,
1679 .poweroff
= snd_soc_poweroff
,
1681 EXPORT_SYMBOL_GPL(snd_soc_pm_ops
);
1683 /* ASoC platform driver */
1684 static struct platform_driver soc_driver
= {
1686 .name
= "soc-audio",
1687 .owner
= THIS_MODULE
,
1688 .pm
= &snd_soc_pm_ops
,
1691 .remove
= soc_remove
,
1695 * snd_soc_codec_volatile_register: Report if a register is volatile.
1697 * @codec: CODEC to query.
1698 * @reg: Register to query.
1700 * Boolean function indiciating if a CODEC register is volatile.
1702 int snd_soc_codec_volatile_register(struct snd_soc_codec
*codec
,
1705 if (codec
->volatile_register
)
1706 return codec
->volatile_register(codec
, reg
);
1710 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register
);
1713 * snd_soc_codec_readable_register: Report if a register is readable.
1715 * @codec: CODEC to query.
1716 * @reg: Register to query.
1718 * Boolean function indicating if a CODEC register is readable.
1720 int snd_soc_codec_readable_register(struct snd_soc_codec
*codec
,
1723 if (codec
->readable_register
)
1724 return codec
->readable_register(codec
, reg
);
1728 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register
);
1731 * snd_soc_codec_writable_register: Report if a register is writable.
1733 * @codec: CODEC to query.
1734 * @reg: Register to query.
1736 * Boolean function indicating if a CODEC register is writable.
1738 int snd_soc_codec_writable_register(struct snd_soc_codec
*codec
,
1741 if (codec
->writable_register
)
1742 return codec
->writable_register(codec
, reg
);
1746 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register
);
1748 int snd_soc_platform_read(struct snd_soc_platform
*platform
,
1753 if (!platform
->driver
->read
) {
1754 dev_err(platform
->dev
, "platform has no read back\n");
1758 ret
= platform
->driver
->read(platform
, reg
);
1759 dev_dbg(platform
->dev
, "read %x => %x\n", reg
, ret
);
1760 trace_snd_soc_preg_read(platform
, reg
, ret
);
1764 EXPORT_SYMBOL_GPL(snd_soc_platform_read
);
1766 int snd_soc_platform_write(struct snd_soc_platform
*platform
,
1767 unsigned int reg
, unsigned int val
)
1769 if (!platform
->driver
->write
) {
1770 dev_err(platform
->dev
, "platform has no write back\n");
1774 dev_dbg(platform
->dev
, "write %x = %x\n", reg
, val
);
1775 trace_snd_soc_preg_write(platform
, reg
, val
);
1776 return platform
->driver
->write(platform
, reg
, val
);
1778 EXPORT_SYMBOL_GPL(snd_soc_platform_write
);
1781 * snd_soc_new_ac97_codec - initailise AC97 device
1782 * @codec: audio codec
1783 * @ops: AC97 bus operations
1784 * @num: AC97 codec number
1786 * Initialises AC97 codec resources for use by ad-hoc devices only.
1788 int snd_soc_new_ac97_codec(struct snd_soc_codec
*codec
,
1789 struct snd_ac97_bus_ops
*ops
, int num
)
1791 mutex_lock(&codec
->mutex
);
1793 codec
->ac97
= kzalloc(sizeof(struct snd_ac97
), GFP_KERNEL
);
1794 if (codec
->ac97
== NULL
) {
1795 mutex_unlock(&codec
->mutex
);
1799 codec
->ac97
->bus
= kzalloc(sizeof(struct snd_ac97_bus
), GFP_KERNEL
);
1800 if (codec
->ac97
->bus
== NULL
) {
1803 mutex_unlock(&codec
->mutex
);
1807 codec
->ac97
->bus
->ops
= ops
;
1808 codec
->ac97
->num
= num
;
1811 * Mark the AC97 device to be created by us. This way we ensure that the
1812 * device will be registered with the device subsystem later on.
1814 codec
->ac97_created
= 1;
1816 mutex_unlock(&codec
->mutex
);
1819 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec
);
1822 * snd_soc_free_ac97_codec - free AC97 codec device
1823 * @codec: audio codec
1825 * Frees AC97 codec device resources.
1827 void snd_soc_free_ac97_codec(struct snd_soc_codec
*codec
)
1829 mutex_lock(&codec
->mutex
);
1830 #ifdef CONFIG_SND_SOC_AC97_BUS
1831 soc_unregister_ac97_dai_link(codec
);
1833 kfree(codec
->ac97
->bus
);
1836 codec
->ac97_created
= 0;
1837 mutex_unlock(&codec
->mutex
);
1839 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec
);
1841 unsigned int snd_soc_read(struct snd_soc_codec
*codec
, unsigned int reg
)
1845 ret
= codec
->read(codec
, reg
);
1846 dev_dbg(codec
->dev
, "read %x => %x\n", reg
, ret
);
1847 trace_snd_soc_reg_read(codec
, reg
, ret
);
1851 EXPORT_SYMBOL_GPL(snd_soc_read
);
1853 unsigned int snd_soc_write(struct snd_soc_codec
*codec
,
1854 unsigned int reg
, unsigned int val
)
1856 dev_dbg(codec
->dev
, "write %x = %x\n", reg
, val
);
1857 trace_snd_soc_reg_write(codec
, reg
, val
);
1858 return codec
->write(codec
, reg
, val
);
1860 EXPORT_SYMBOL_GPL(snd_soc_write
);
1862 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec
*codec
,
1863 unsigned int reg
, const void *data
, size_t len
)
1865 return codec
->bulk_write_raw(codec
, reg
, data
, len
);
1867 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw
);
1870 * snd_soc_update_bits - update codec register bits
1871 * @codec: audio codec
1872 * @reg: codec register
1873 * @mask: register mask
1876 * Writes new register value.
1878 * Returns 1 for change, 0 for no change, or negative error code.
1880 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1881 unsigned int mask
, unsigned int value
)
1884 unsigned int old
, new;
1887 ret
= snd_soc_read(codec
, reg
);
1892 new = (old
& ~mask
) | (value
& mask
);
1893 change
= old
!= new;
1895 ret
= snd_soc_write(codec
, reg
, new);
1902 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
1905 * snd_soc_update_bits_locked - update codec register bits
1906 * @codec: audio codec
1907 * @reg: codec register
1908 * @mask: register mask
1911 * Writes new register value, and takes the codec mutex.
1913 * Returns 1 for change else 0.
1915 int snd_soc_update_bits_locked(struct snd_soc_codec
*codec
,
1916 unsigned short reg
, unsigned int mask
,
1921 mutex_lock(&codec
->mutex
);
1922 change
= snd_soc_update_bits(codec
, reg
, mask
, value
);
1923 mutex_unlock(&codec
->mutex
);
1927 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked
);
1930 * snd_soc_test_bits - test register for change
1931 * @codec: audio codec
1932 * @reg: codec register
1933 * @mask: register mask
1936 * Tests a register with a new value and checks if the new value is
1937 * different from the old value.
1939 * Returns 1 for change else 0.
1941 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1942 unsigned int mask
, unsigned int value
)
1945 unsigned int old
, new;
1947 old
= snd_soc_read(codec
, reg
);
1948 new = (old
& ~mask
) | value
;
1949 change
= old
!= new;
1953 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
1956 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1957 * @substream: the pcm substream
1958 * @hw: the hardware parameters
1960 * Sets the substream runtime hardware parameters.
1962 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
1963 const struct snd_pcm_hardware
*hw
)
1965 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1966 runtime
->hw
.info
= hw
->info
;
1967 runtime
->hw
.formats
= hw
->formats
;
1968 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
1969 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
1970 runtime
->hw
.periods_min
= hw
->periods_min
;
1971 runtime
->hw
.periods_max
= hw
->periods_max
;
1972 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
1973 runtime
->hw
.fifo_size
= hw
->fifo_size
;
1976 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
1979 * snd_soc_cnew - create new control
1980 * @_template: control template
1981 * @data: control private data
1982 * @long_name: control long name
1983 * @prefix: control name prefix
1985 * Create a new mixer control from a template control.
1987 * Returns 0 for success, else error.
1989 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
1990 void *data
, char *long_name
,
1993 struct snd_kcontrol_new
template;
1994 struct snd_kcontrol
*kcontrol
;
1998 memcpy(&template, _template
, sizeof(template));
2002 long_name
= template.name
;
2005 name_len
= strlen(long_name
) + strlen(prefix
) + 2;
2006 name
= kmalloc(name_len
, GFP_KERNEL
);
2010 snprintf(name
, name_len
, "%s %s", prefix
, long_name
);
2012 template.name
= name
;
2014 template.name
= long_name
;
2017 kcontrol
= snd_ctl_new1(&template, data
);
2023 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
2026 * snd_soc_add_controls - add an array of controls to a codec.
2027 * Convienience function to add a list of controls. Many codecs were
2028 * duplicating this code.
2030 * @codec: codec to add controls to
2031 * @controls: array of controls to add
2032 * @num_controls: number of elements in the array
2034 * Return 0 for success, else error.
2036 int snd_soc_add_controls(struct snd_soc_codec
*codec
,
2037 const struct snd_kcontrol_new
*controls
, int num_controls
)
2039 struct snd_card
*card
= codec
->card
->snd_card
;
2042 for (i
= 0; i
< num_controls
; i
++) {
2043 const struct snd_kcontrol_new
*control
= &controls
[i
];
2044 err
= snd_ctl_add(card
, snd_soc_cnew(control
, codec
,
2046 codec
->name_prefix
));
2048 dev_err(codec
->dev
, "%s: Failed to add %s: %d\n",
2049 codec
->name
, control
->name
, err
);
2056 EXPORT_SYMBOL_GPL(snd_soc_add_controls
);
2059 * snd_soc_add_platform_controls - add an array of controls to a platform.
2060 * Convienience function to add a list of controls.
2062 * @platform: platform to add controls to
2063 * @controls: array of controls to add
2064 * @num_controls: number of elements in the array
2066 * Return 0 for success, else error.
2068 int snd_soc_add_platform_controls(struct snd_soc_platform
*platform
,
2069 const struct snd_kcontrol_new
*controls
, int num_controls
)
2071 struct snd_card
*card
= platform
->card
->snd_card
;
2074 for (i
= 0; i
< num_controls
; i
++) {
2075 const struct snd_kcontrol_new
*control
= &controls
[i
];
2076 err
= snd_ctl_add(card
, snd_soc_cnew(control
, platform
,
2077 control
->name
, NULL
));
2079 dev_err(platform
->dev
, "Failed to add %s %d\n",control
->name
, err
);
2086 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls
);
2089 * snd_soc_info_enum_double - enumerated double mixer info callback
2090 * @kcontrol: mixer control
2091 * @uinfo: control element information
2093 * Callback to provide information about a double enumerated
2096 * Returns 0 for success.
2098 int snd_soc_info_enum_double(struct snd_kcontrol
*kcontrol
,
2099 struct snd_ctl_elem_info
*uinfo
)
2101 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2103 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2104 uinfo
->count
= e
->shift_l
== e
->shift_r
? 1 : 2;
2105 uinfo
->value
.enumerated
.items
= e
->max
;
2107 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2108 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2109 strcpy(uinfo
->value
.enumerated
.name
,
2110 e
->texts
[uinfo
->value
.enumerated
.item
]);
2113 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double
);
2116 * snd_soc_get_enum_double - enumerated double mixer get callback
2117 * @kcontrol: mixer control
2118 * @ucontrol: control element information
2120 * Callback to get the value of a double enumerated mixer.
2122 * Returns 0 for success.
2124 int snd_soc_get_enum_double(struct snd_kcontrol
*kcontrol
,
2125 struct snd_ctl_elem_value
*ucontrol
)
2127 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2128 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2129 unsigned int val
, bitmask
;
2131 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2133 val
= snd_soc_read(codec
, e
->reg
);
2134 ucontrol
->value
.enumerated
.item
[0]
2135 = (val
>> e
->shift_l
) & (bitmask
- 1);
2136 if (e
->shift_l
!= e
->shift_r
)
2137 ucontrol
->value
.enumerated
.item
[1] =
2138 (val
>> e
->shift_r
) & (bitmask
- 1);
2142 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double
);
2145 * snd_soc_put_enum_double - enumerated double mixer put callback
2146 * @kcontrol: mixer control
2147 * @ucontrol: control element information
2149 * Callback to set the value of a double enumerated mixer.
2151 * Returns 0 for success.
2153 int snd_soc_put_enum_double(struct snd_kcontrol
*kcontrol
,
2154 struct snd_ctl_elem_value
*ucontrol
)
2156 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2157 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2159 unsigned int mask
, bitmask
;
2161 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2163 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2165 val
= ucontrol
->value
.enumerated
.item
[0] << e
->shift_l
;
2166 mask
= (bitmask
- 1) << e
->shift_l
;
2167 if (e
->shift_l
!= e
->shift_r
) {
2168 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2170 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2171 mask
|= (bitmask
- 1) << e
->shift_r
;
2174 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2176 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double
);
2179 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2180 * @kcontrol: mixer control
2181 * @ucontrol: control element information
2183 * Callback to get the value of a double semi enumerated mixer.
2185 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2186 * used for handling bitfield coded enumeration for example.
2188 * Returns 0 for success.
2190 int snd_soc_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2191 struct snd_ctl_elem_value
*ucontrol
)
2193 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2194 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2195 unsigned int reg_val
, val
, mux
;
2197 reg_val
= snd_soc_read(codec
, e
->reg
);
2198 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2199 for (mux
= 0; mux
< e
->max
; mux
++) {
2200 if (val
== e
->values
[mux
])
2203 ucontrol
->value
.enumerated
.item
[0] = mux
;
2204 if (e
->shift_l
!= e
->shift_r
) {
2205 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2206 for (mux
= 0; mux
< e
->max
; mux
++) {
2207 if (val
== e
->values
[mux
])
2210 ucontrol
->value
.enumerated
.item
[1] = mux
;
2215 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double
);
2218 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2219 * @kcontrol: mixer control
2220 * @ucontrol: control element information
2222 * Callback to set the value of a double semi enumerated mixer.
2224 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2225 * used for handling bitfield coded enumeration for example.
2227 * Returns 0 for success.
2229 int snd_soc_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2230 struct snd_ctl_elem_value
*ucontrol
)
2232 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2233 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2237 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2239 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2240 mask
= e
->mask
<< e
->shift_l
;
2241 if (e
->shift_l
!= e
->shift_r
) {
2242 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2244 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2245 mask
|= e
->mask
<< e
->shift_r
;
2248 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2250 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double
);
2253 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2254 * @kcontrol: mixer control
2255 * @uinfo: control element information
2257 * Callback to provide information about an external enumerated
2260 * Returns 0 for success.
2262 int snd_soc_info_enum_ext(struct snd_kcontrol
*kcontrol
,
2263 struct snd_ctl_elem_info
*uinfo
)
2265 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2267 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2269 uinfo
->value
.enumerated
.items
= e
->max
;
2271 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2272 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2273 strcpy(uinfo
->value
.enumerated
.name
,
2274 e
->texts
[uinfo
->value
.enumerated
.item
]);
2277 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext
);
2280 * snd_soc_info_volsw_ext - external single mixer info callback
2281 * @kcontrol: mixer control
2282 * @uinfo: control element information
2284 * Callback to provide information about a single external mixer control.
2286 * Returns 0 for success.
2288 int snd_soc_info_volsw_ext(struct snd_kcontrol
*kcontrol
,
2289 struct snd_ctl_elem_info
*uinfo
)
2291 int max
= kcontrol
->private_value
;
2293 if (max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2294 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2296 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2299 uinfo
->value
.integer
.min
= 0;
2300 uinfo
->value
.integer
.max
= max
;
2303 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext
);
2306 * snd_soc_info_volsw - single mixer info callback
2307 * @kcontrol: mixer control
2308 * @uinfo: control element information
2310 * Callback to provide information about a single mixer control, or a double
2311 * mixer control that spans 2 registers.
2313 * Returns 0 for success.
2315 int snd_soc_info_volsw(struct snd_kcontrol
*kcontrol
,
2316 struct snd_ctl_elem_info
*uinfo
)
2318 struct soc_mixer_control
*mc
=
2319 (struct soc_mixer_control
*)kcontrol
->private_value
;
2322 if (!mc
->platform_max
)
2323 mc
->platform_max
= mc
->max
;
2324 platform_max
= mc
->platform_max
;
2326 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2327 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2329 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2331 uinfo
->count
= snd_soc_volsw_is_stereo(mc
) ? 2 : 1;
2332 uinfo
->value
.integer
.min
= 0;
2333 uinfo
->value
.integer
.max
= platform_max
;
2336 EXPORT_SYMBOL_GPL(snd_soc_info_volsw
);
2339 * snd_soc_get_volsw - single mixer get callback
2340 * @kcontrol: mixer control
2341 * @ucontrol: control element information
2343 * Callback to get the value of a single mixer control, or a double mixer
2344 * control that spans 2 registers.
2346 * Returns 0 for success.
2348 int snd_soc_get_volsw(struct snd_kcontrol
*kcontrol
,
2349 struct snd_ctl_elem_value
*ucontrol
)
2351 struct soc_mixer_control
*mc
=
2352 (struct soc_mixer_control
*)kcontrol
->private_value
;
2353 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2354 unsigned int reg
= mc
->reg
;
2355 unsigned int reg2
= mc
->rreg
;
2356 unsigned int shift
= mc
->shift
;
2357 unsigned int rshift
= mc
->rshift
;
2359 unsigned int mask
= (1 << fls(max
)) - 1;
2360 unsigned int invert
= mc
->invert
;
2362 ucontrol
->value
.integer
.value
[0] =
2363 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2365 ucontrol
->value
.integer
.value
[0] =
2366 max
- ucontrol
->value
.integer
.value
[0];
2368 if (snd_soc_volsw_is_stereo(mc
)) {
2370 ucontrol
->value
.integer
.value
[1] =
2371 (snd_soc_read(codec
, reg
) >> rshift
) & mask
;
2373 ucontrol
->value
.integer
.value
[1] =
2374 (snd_soc_read(codec
, reg2
) >> shift
) & mask
;
2376 ucontrol
->value
.integer
.value
[1] =
2377 max
- ucontrol
->value
.integer
.value
[1];
2382 EXPORT_SYMBOL_GPL(snd_soc_get_volsw
);
2385 * snd_soc_put_volsw - single mixer put callback
2386 * @kcontrol: mixer control
2387 * @ucontrol: control element information
2389 * Callback to set the value of a single mixer control, or a double mixer
2390 * control that spans 2 registers.
2392 * Returns 0 for success.
2394 int snd_soc_put_volsw(struct snd_kcontrol
*kcontrol
,
2395 struct snd_ctl_elem_value
*ucontrol
)
2397 struct soc_mixer_control
*mc
=
2398 (struct soc_mixer_control
*)kcontrol
->private_value
;
2399 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2400 unsigned int reg
= mc
->reg
;
2401 unsigned int reg2
= mc
->rreg
;
2402 unsigned int shift
= mc
->shift
;
2403 unsigned int rshift
= mc
->rshift
;
2405 unsigned int mask
= (1 << fls(max
)) - 1;
2406 unsigned int invert
= mc
->invert
;
2409 unsigned int val2
= 0;
2410 unsigned int val
, val_mask
;
2412 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2415 val_mask
= mask
<< shift
;
2417 if (snd_soc_volsw_is_stereo(mc
)) {
2418 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2422 val_mask
|= mask
<< rshift
;
2423 val
|= val2
<< rshift
;
2425 val2
= val2
<< shift
;
2429 err
= snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2434 err
= snd_soc_update_bits_locked(codec
, reg2
, val_mask
, val2
);
2438 EXPORT_SYMBOL_GPL(snd_soc_put_volsw
);
2441 * snd_soc_info_volsw_s8 - signed mixer info callback
2442 * @kcontrol: mixer control
2443 * @uinfo: control element information
2445 * Callback to provide information about a signed mixer control.
2447 * Returns 0 for success.
2449 int snd_soc_info_volsw_s8(struct snd_kcontrol
*kcontrol
,
2450 struct snd_ctl_elem_info
*uinfo
)
2452 struct soc_mixer_control
*mc
=
2453 (struct soc_mixer_control
*)kcontrol
->private_value
;
2457 if (!mc
->platform_max
)
2458 mc
->platform_max
= mc
->max
;
2459 platform_max
= mc
->platform_max
;
2461 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2463 uinfo
->value
.integer
.min
= 0;
2464 uinfo
->value
.integer
.max
= platform_max
- min
;
2467 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8
);
2470 * snd_soc_get_volsw_s8 - signed mixer get callback
2471 * @kcontrol: mixer control
2472 * @ucontrol: control element information
2474 * Callback to get the value of a signed mixer control.
2476 * Returns 0 for success.
2478 int snd_soc_get_volsw_s8(struct snd_kcontrol
*kcontrol
,
2479 struct snd_ctl_elem_value
*ucontrol
)
2481 struct soc_mixer_control
*mc
=
2482 (struct soc_mixer_control
*)kcontrol
->private_value
;
2483 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2484 unsigned int reg
= mc
->reg
;
2486 int val
= snd_soc_read(codec
, reg
);
2488 ucontrol
->value
.integer
.value
[0] =
2489 ((signed char)(val
& 0xff))-min
;
2490 ucontrol
->value
.integer
.value
[1] =
2491 ((signed char)((val
>> 8) & 0xff))-min
;
2494 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8
);
2497 * snd_soc_put_volsw_sgn - signed mixer put callback
2498 * @kcontrol: mixer control
2499 * @ucontrol: control element information
2501 * Callback to set the value of a signed mixer control.
2503 * Returns 0 for success.
2505 int snd_soc_put_volsw_s8(struct snd_kcontrol
*kcontrol
,
2506 struct snd_ctl_elem_value
*ucontrol
)
2508 struct soc_mixer_control
*mc
=
2509 (struct soc_mixer_control
*)kcontrol
->private_value
;
2510 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2511 unsigned int reg
= mc
->reg
;
2515 val
= (ucontrol
->value
.integer
.value
[0]+min
) & 0xff;
2516 val
|= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff) << 8;
2518 return snd_soc_update_bits_locked(codec
, reg
, 0xffff, val
);
2520 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8
);
2523 * snd_soc_limit_volume - Set new limit to an existing volume control.
2525 * @codec: where to look for the control
2526 * @name: Name of the control
2527 * @max: new maximum limit
2529 * Return 0 for success, else error.
2531 int snd_soc_limit_volume(struct snd_soc_codec
*codec
,
2532 const char *name
, int max
)
2534 struct snd_card
*card
= codec
->card
->snd_card
;
2535 struct snd_kcontrol
*kctl
;
2536 struct soc_mixer_control
*mc
;
2540 /* Sanity check for name and max */
2541 if (unlikely(!name
|| max
<= 0))
2544 list_for_each_entry(kctl
, &card
->controls
, list
) {
2545 if (!strncmp(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
))) {
2551 mc
= (struct soc_mixer_control
*)kctl
->private_value
;
2552 if (max
<= mc
->max
) {
2553 mc
->platform_max
= max
;
2559 EXPORT_SYMBOL_GPL(snd_soc_limit_volume
);
2562 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2563 * mixer info callback
2564 * @kcontrol: mixer control
2565 * @uinfo: control element information
2567 * Returns 0 for success.
2569 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2570 struct snd_ctl_elem_info
*uinfo
)
2572 struct soc_mixer_control
*mc
=
2573 (struct soc_mixer_control
*)kcontrol
->private_value
;
2577 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2579 uinfo
->value
.integer
.min
= 0;
2580 uinfo
->value
.integer
.max
= max
-min
;
2584 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx
);
2587 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2588 * mixer get callback
2589 * @kcontrol: mixer control
2590 * @uinfo: control element information
2592 * Returns 0 for success.
2594 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2595 struct snd_ctl_elem_value
*ucontrol
)
2597 struct soc_mixer_control
*mc
=
2598 (struct soc_mixer_control
*)kcontrol
->private_value
;
2599 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2600 unsigned int mask
= (1<<mc
->shift
)-1;
2602 int val
= snd_soc_read(codec
, mc
->reg
) & mask
;
2603 int valr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2605 ucontrol
->value
.integer
.value
[0] = ((val
& 0xff)-min
) & mask
;
2606 ucontrol
->value
.integer
.value
[1] = ((valr
& 0xff)-min
) & mask
;
2609 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx
);
2612 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2613 * mixer put callback
2614 * @kcontrol: mixer control
2615 * @uinfo: control element information
2617 * Returns 0 for success.
2619 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2620 struct snd_ctl_elem_value
*ucontrol
)
2622 struct soc_mixer_control
*mc
=
2623 (struct soc_mixer_control
*)kcontrol
->private_value
;
2624 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2625 unsigned int mask
= (1<<mc
->shift
)-1;
2628 unsigned int val
, valr
, oval
, ovalr
;
2630 val
= ((ucontrol
->value
.integer
.value
[0]+min
) & 0xff);
2632 valr
= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff);
2635 oval
= snd_soc_read(codec
, mc
->reg
) & mask
;
2636 ovalr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2640 ret
= snd_soc_write(codec
, mc
->reg
, val
);
2644 if (ovalr
!= valr
) {
2645 ret
= snd_soc_write(codec
, mc
->rreg
, valr
);
2652 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx
);
2655 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2657 * @clk_id: DAI specific clock ID
2658 * @freq: new clock frequency in Hz
2659 * @dir: new clock direction - input/output.
2661 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2663 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
2664 unsigned int freq
, int dir
)
2666 if (dai
->driver
&& dai
->driver
->ops
->set_sysclk
)
2667 return dai
->driver
->ops
->set_sysclk(dai
, clk_id
, freq
, dir
);
2668 else if (dai
->codec
&& dai
->codec
->driver
->set_sysclk
)
2669 return dai
->codec
->driver
->set_sysclk(dai
->codec
, clk_id
, 0,
2674 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
2677 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2679 * @clk_id: DAI specific clock ID
2680 * @source: Source for the clock
2681 * @freq: new clock frequency in Hz
2682 * @dir: new clock direction - input/output.
2684 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2686 int snd_soc_codec_set_sysclk(struct snd_soc_codec
*codec
, int clk_id
,
2687 int source
, unsigned int freq
, int dir
)
2689 if (codec
->driver
->set_sysclk
)
2690 return codec
->driver
->set_sysclk(codec
, clk_id
, source
,
2695 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk
);
2698 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2700 * @div_id: DAI specific clock divider ID
2701 * @div: new clock divisor.
2703 * Configures the clock dividers. This is used to derive the best DAI bit and
2704 * frame clocks from the system or master clock. It's best to set the DAI bit
2705 * and frame clocks as low as possible to save system power.
2707 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
2708 int div_id
, int div
)
2710 if (dai
->driver
&& dai
->driver
->ops
->set_clkdiv
)
2711 return dai
->driver
->ops
->set_clkdiv(dai
, div_id
, div
);
2715 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
2718 * snd_soc_dai_set_pll - configure DAI PLL.
2720 * @pll_id: DAI specific PLL ID
2721 * @source: DAI specific source for the PLL
2722 * @freq_in: PLL input clock frequency in Hz
2723 * @freq_out: requested PLL output clock frequency in Hz
2725 * Configures and enables PLL to generate output clock based on input clock.
2727 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
2728 unsigned int freq_in
, unsigned int freq_out
)
2730 if (dai
->driver
&& dai
->driver
->ops
->set_pll
)
2731 return dai
->driver
->ops
->set_pll(dai
, pll_id
, source
,
2733 else if (dai
->codec
&& dai
->codec
->driver
->set_pll
)
2734 return dai
->codec
->driver
->set_pll(dai
->codec
, pll_id
, source
,
2739 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
2742 * snd_soc_codec_set_pll - configure codec PLL.
2744 * @pll_id: DAI specific PLL ID
2745 * @source: DAI specific source for the PLL
2746 * @freq_in: PLL input clock frequency in Hz
2747 * @freq_out: requested PLL output clock frequency in Hz
2749 * Configures and enables PLL to generate output clock based on input clock.
2751 int snd_soc_codec_set_pll(struct snd_soc_codec
*codec
, int pll_id
, int source
,
2752 unsigned int freq_in
, unsigned int freq_out
)
2754 if (codec
->driver
->set_pll
)
2755 return codec
->driver
->set_pll(codec
, pll_id
, source
,
2760 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll
);
2763 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2765 * @fmt: SND_SOC_DAIFMT_ format value.
2767 * Configures the DAI hardware format and clocking.
2769 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
2771 if (dai
->driver
&& dai
->driver
->ops
->set_fmt
)
2772 return dai
->driver
->ops
->set_fmt(dai
, fmt
);
2776 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
2779 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2781 * @tx_mask: bitmask representing active TX slots.
2782 * @rx_mask: bitmask representing active RX slots.
2783 * @slots: Number of slots in use.
2784 * @slot_width: Width in bits for each slot.
2786 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2789 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
2790 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
2792 if (dai
->driver
&& dai
->driver
->ops
->set_tdm_slot
)
2793 return dai
->driver
->ops
->set_tdm_slot(dai
, tx_mask
, rx_mask
,
2798 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
2801 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2803 * @tx_num: how many TX channels
2804 * @tx_slot: pointer to an array which imply the TX slot number channel
2806 * @rx_num: how many RX channels
2807 * @rx_slot: pointer to an array which imply the RX slot number channel
2810 * configure the relationship between channel number and TDM slot number.
2812 int snd_soc_dai_set_channel_map(struct snd_soc_dai
*dai
,
2813 unsigned int tx_num
, unsigned int *tx_slot
,
2814 unsigned int rx_num
, unsigned int *rx_slot
)
2816 if (dai
->driver
&& dai
->driver
->ops
->set_channel_map
)
2817 return dai
->driver
->ops
->set_channel_map(dai
, tx_num
, tx_slot
,
2822 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map
);
2825 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2827 * @tristate: tristate enable
2829 * Tristates the DAI so that others can use it.
2831 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
2833 if (dai
->driver
&& dai
->driver
->ops
->set_tristate
)
2834 return dai
->driver
->ops
->set_tristate(dai
, tristate
);
2838 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
2841 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2843 * @mute: mute enable
2845 * Mutes the DAI DAC.
2847 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
)
2849 if (dai
->driver
&& dai
->driver
->ops
->digital_mute
)
2850 return dai
->driver
->ops
->digital_mute(dai
, mute
);
2854 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
2857 * snd_soc_register_card - Register a card with the ASoC core
2859 * @card: Card to register
2862 int snd_soc_register_card(struct snd_soc_card
*card
)
2866 if (!card
->name
|| !card
->dev
)
2869 for (i
= 0; i
< card
->num_links
; i
++) {
2870 struct snd_soc_dai_link
*link
= &card
->dai_link
[i
];
2873 * Codec must be specified by 1 of name or OF node,
2874 * not both or neither.
2876 if (!!link
->codec_name
== !!link
->codec_of_node
) {
2878 "Neither/both codec name/of_node are set\n");
2883 * Platform may be specified by either name or OF node, but
2884 * can be left unspecified, and a dummy platform will be used.
2886 if (link
->platform_name
&& link
->platform_of_node
) {
2888 "Both platform name/of_node are set\n");
2893 * CPU DAI must be specified by 1 of name or OF node,
2894 * not both or neither.
2896 if (!!link
->cpu_dai_name
== !!link
->cpu_dai_of_node
) {
2898 "Neither/both cpu_dai name/of_node are set\n");
2903 dev_set_drvdata(card
->dev
, card
);
2905 snd_soc_initialize_card_lists(card
);
2907 soc_init_card_debugfs(card
);
2909 card
->rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
) *
2910 (card
->num_links
+ card
->num_aux_devs
),
2912 if (card
->rtd
== NULL
)
2914 card
->rtd_aux
= &card
->rtd
[card
->num_links
];
2916 for (i
= 0; i
< card
->num_links
; i
++)
2917 card
->rtd
[i
].dai_link
= &card
->dai_link
[i
];
2919 INIT_LIST_HEAD(&card
->list
);
2920 INIT_LIST_HEAD(&card
->dapm_dirty
);
2921 card
->instantiated
= 0;
2922 mutex_init(&card
->mutex
);
2924 mutex_lock(&client_mutex
);
2925 list_add(&card
->list
, &card_list
);
2926 snd_soc_instantiate_cards();
2927 mutex_unlock(&client_mutex
);
2929 dev_dbg(card
->dev
, "Registered card '%s'\n", card
->name
);
2933 EXPORT_SYMBOL_GPL(snd_soc_register_card
);
2936 * snd_soc_unregister_card - Unregister a card with the ASoC core
2938 * @card: Card to unregister
2941 int snd_soc_unregister_card(struct snd_soc_card
*card
)
2943 if (card
->instantiated
)
2944 soc_cleanup_card_resources(card
);
2945 mutex_lock(&client_mutex
);
2946 list_del(&card
->list
);
2947 mutex_unlock(&client_mutex
);
2948 dev_dbg(card
->dev
, "Unregistered card '%s'\n", card
->name
);
2952 EXPORT_SYMBOL_GPL(snd_soc_unregister_card
);
2955 * Simplify DAI link configuration by removing ".-1" from device names
2956 * and sanitizing names.
2958 static char *fmt_single_name(struct device
*dev
, int *id
)
2960 char *found
, name
[NAME_SIZE
];
2963 if (dev_name(dev
) == NULL
)
2966 strlcpy(name
, dev_name(dev
), NAME_SIZE
);
2968 /* are we a "%s.%d" name (platform and SPI components) */
2969 found
= strstr(name
, dev
->driver
->name
);
2972 if (sscanf(&found
[strlen(dev
->driver
->name
)], ".%d", id
) == 1) {
2974 /* discard ID from name if ID == -1 */
2976 found
[strlen(dev
->driver
->name
)] = '\0';
2980 /* I2C component devices are named "bus-addr" */
2981 if (sscanf(name
, "%x-%x", &id1
, &id2
) == 2) {
2982 char tmp
[NAME_SIZE
];
2984 /* create unique ID number from I2C addr and bus */
2985 *id
= ((id1
& 0xffff) << 16) + id2
;
2987 /* sanitize component name for DAI link creation */
2988 snprintf(tmp
, NAME_SIZE
, "%s.%s", dev
->driver
->name
, name
);
2989 strlcpy(name
, tmp
, NAME_SIZE
);
2994 return kstrdup(name
, GFP_KERNEL
);
2998 * Simplify DAI link naming for single devices with multiple DAIs by removing
2999 * any ".-1" and using the DAI name (instead of device name).
3001 static inline char *fmt_multiple_name(struct device
*dev
,
3002 struct snd_soc_dai_driver
*dai_drv
)
3004 if (dai_drv
->name
== NULL
) {
3005 printk(KERN_ERR
"asoc: error - multiple DAI %s registered with no name\n",
3010 return kstrdup(dai_drv
->name
, GFP_KERNEL
);
3014 * snd_soc_register_dai - Register a DAI with the ASoC core
3016 * @dai: DAI to register
3018 int snd_soc_register_dai(struct device
*dev
,
3019 struct snd_soc_dai_driver
*dai_drv
)
3021 struct snd_soc_dai
*dai
;
3023 dev_dbg(dev
, "dai register %s\n", dev_name(dev
));
3025 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
3029 /* create DAI component name */
3030 dai
->name
= fmt_single_name(dev
, &dai
->id
);
3031 if (dai
->name
== NULL
) {
3037 dai
->driver
= dai_drv
;
3038 if (!dai
->driver
->ops
)
3039 dai
->driver
->ops
= &null_dai_ops
;
3041 mutex_lock(&client_mutex
);
3042 list_add(&dai
->list
, &dai_list
);
3043 snd_soc_instantiate_cards();
3044 mutex_unlock(&client_mutex
);
3046 pr_debug("Registered DAI '%s'\n", dai
->name
);
3050 EXPORT_SYMBOL_GPL(snd_soc_register_dai
);
3053 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3055 * @dai: DAI to unregister
3057 void snd_soc_unregister_dai(struct device
*dev
)
3059 struct snd_soc_dai
*dai
;
3061 list_for_each_entry(dai
, &dai_list
, list
) {
3062 if (dev
== dai
->dev
)
3068 mutex_lock(&client_mutex
);
3069 list_del(&dai
->list
);
3070 mutex_unlock(&client_mutex
);
3072 pr_debug("Unregistered DAI '%s'\n", dai
->name
);
3076 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai
);
3079 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3081 * @dai: Array of DAIs to register
3082 * @count: Number of DAIs
3084 int snd_soc_register_dais(struct device
*dev
,
3085 struct snd_soc_dai_driver
*dai_drv
, size_t count
)
3087 struct snd_soc_dai
*dai
;
3090 dev_dbg(dev
, "dai register %s #%Zu\n", dev_name(dev
), count
);
3092 for (i
= 0; i
< count
; i
++) {
3094 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
3100 /* create DAI component name */
3101 dai
->name
= fmt_multiple_name(dev
, &dai_drv
[i
]);
3102 if (dai
->name
== NULL
) {
3109 dai
->driver
= &dai_drv
[i
];
3110 if (dai
->driver
->id
)
3111 dai
->id
= dai
->driver
->id
;
3114 if (!dai
->driver
->ops
)
3115 dai
->driver
->ops
= &null_dai_ops
;
3117 mutex_lock(&client_mutex
);
3118 list_add(&dai
->list
, &dai_list
);
3119 mutex_unlock(&client_mutex
);
3121 pr_debug("Registered DAI '%s'\n", dai
->name
);
3124 mutex_lock(&client_mutex
);
3125 snd_soc_instantiate_cards();
3126 mutex_unlock(&client_mutex
);
3130 for (i
--; i
>= 0; i
--)
3131 snd_soc_unregister_dai(dev
);
3135 EXPORT_SYMBOL_GPL(snd_soc_register_dais
);
3138 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3140 * @dai: Array of DAIs to unregister
3141 * @count: Number of DAIs
3143 void snd_soc_unregister_dais(struct device
*dev
, size_t count
)
3147 for (i
= 0; i
< count
; i
++)
3148 snd_soc_unregister_dai(dev
);
3150 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais
);
3153 * snd_soc_register_platform - Register a platform with the ASoC core
3155 * @platform: platform to register
3157 int snd_soc_register_platform(struct device
*dev
,
3158 struct snd_soc_platform_driver
*platform_drv
)
3160 struct snd_soc_platform
*platform
;
3162 dev_dbg(dev
, "platform register %s\n", dev_name(dev
));
3164 platform
= kzalloc(sizeof(struct snd_soc_platform
), GFP_KERNEL
);
3165 if (platform
== NULL
)
3168 /* create platform component name */
3169 platform
->name
= fmt_single_name(dev
, &platform
->id
);
3170 if (platform
->name
== NULL
) {
3175 platform
->dev
= dev
;
3176 platform
->driver
= platform_drv
;
3177 platform
->dapm
.dev
= dev
;
3178 platform
->dapm
.platform
= platform
;
3179 platform
->dapm
.stream_event
= platform_drv
->stream_event
;
3181 mutex_lock(&client_mutex
);
3182 list_add(&platform
->list
, &platform_list
);
3183 snd_soc_instantiate_cards();
3184 mutex_unlock(&client_mutex
);
3186 pr_debug("Registered platform '%s'\n", platform
->name
);
3190 EXPORT_SYMBOL_GPL(snd_soc_register_platform
);
3193 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3195 * @platform: platform to unregister
3197 void snd_soc_unregister_platform(struct device
*dev
)
3199 struct snd_soc_platform
*platform
;
3201 list_for_each_entry(platform
, &platform_list
, list
) {
3202 if (dev
== platform
->dev
)
3208 mutex_lock(&client_mutex
);
3209 list_del(&platform
->list
);
3210 mutex_unlock(&client_mutex
);
3212 pr_debug("Unregistered platform '%s'\n", platform
->name
);
3213 kfree(platform
->name
);
3216 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform
);
3218 static u64 codec_format_map
[] = {
3219 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
,
3220 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_U16_BE
,
3221 SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S24_BE
,
3222 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_U24_BE
,
3223 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
,
3224 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_U32_BE
,
3225 SNDRV_PCM_FMTBIT_S24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3226 SNDRV_PCM_FMTBIT_U24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3227 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S20_3BE
,
3228 SNDRV_PCM_FMTBIT_U20_3LE
| SNDRV_PCM_FMTBIT_U20_3BE
,
3229 SNDRV_PCM_FMTBIT_S18_3LE
| SNDRV_PCM_FMTBIT_S18_3BE
,
3230 SNDRV_PCM_FMTBIT_U18_3LE
| SNDRV_PCM_FMTBIT_U18_3BE
,
3231 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
,
3232 SNDRV_PCM_FMTBIT_FLOAT64_LE
| SNDRV_PCM_FMTBIT_FLOAT64_BE
,
3233 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3234 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
,
3237 /* Fix up the DAI formats for endianness: codecs don't actually see
3238 * the endianness of the data but we're using the CPU format
3239 * definitions which do need to include endianness so we ensure that
3240 * codec DAIs always have both big and little endian variants set.
3242 static void fixup_codec_formats(struct snd_soc_pcm_stream
*stream
)
3246 for (i
= 0; i
< ARRAY_SIZE(codec_format_map
); i
++)
3247 if (stream
->formats
& codec_format_map
[i
])
3248 stream
->formats
|= codec_format_map
[i
];
3252 * snd_soc_register_codec - Register a codec with the ASoC core
3254 * @codec: codec to register
3256 int snd_soc_register_codec(struct device
*dev
,
3257 const struct snd_soc_codec_driver
*codec_drv
,
3258 struct snd_soc_dai_driver
*dai_drv
,
3262 struct snd_soc_codec
*codec
;
3265 dev_dbg(dev
, "codec register %s\n", dev_name(dev
));
3267 codec
= kzalloc(sizeof(struct snd_soc_codec
), GFP_KERNEL
);
3271 /* create CODEC component name */
3272 codec
->name
= fmt_single_name(dev
, &codec
->id
);
3273 if (codec
->name
== NULL
) {
3278 if (codec_drv
->compress_type
)
3279 codec
->compress_type
= codec_drv
->compress_type
;
3281 codec
->compress_type
= SND_SOC_FLAT_COMPRESSION
;
3283 codec
->write
= codec_drv
->write
;
3284 codec
->read
= codec_drv
->read
;
3285 codec
->volatile_register
= codec_drv
->volatile_register
;
3286 codec
->readable_register
= codec_drv
->readable_register
;
3287 codec
->writable_register
= codec_drv
->writable_register
;
3288 codec
->dapm
.bias_level
= SND_SOC_BIAS_OFF
;
3289 codec
->dapm
.dev
= dev
;
3290 codec
->dapm
.codec
= codec
;
3291 codec
->dapm
.seq_notifier
= codec_drv
->seq_notifier
;
3292 codec
->dapm
.stream_event
= codec_drv
->stream_event
;
3294 codec
->driver
= codec_drv
;
3295 codec
->num_dai
= num_dai
;
3296 mutex_init(&codec
->mutex
);
3298 /* allocate CODEC register cache */
3299 if (codec_drv
->reg_cache_size
&& codec_drv
->reg_word_size
) {
3300 reg_size
= codec_drv
->reg_cache_size
* codec_drv
->reg_word_size
;
3301 codec
->reg_size
= reg_size
;
3302 /* it is necessary to make a copy of the default register cache
3303 * because in the case of using a compression type that requires
3304 * the default register cache to be marked as __devinitconst the
3305 * kernel might have freed the array by the time we initialize
3308 if (codec_drv
->reg_cache_default
) {
3309 codec
->reg_def_copy
= kmemdup(codec_drv
->reg_cache_default
,
3310 reg_size
, GFP_KERNEL
);
3311 if (!codec
->reg_def_copy
) {
3318 if (codec_drv
->reg_access_size
&& codec_drv
->reg_access_default
) {
3319 if (!codec
->volatile_register
)
3320 codec
->volatile_register
= snd_soc_default_volatile_register
;
3321 if (!codec
->readable_register
)
3322 codec
->readable_register
= snd_soc_default_readable_register
;
3323 if (!codec
->writable_register
)
3324 codec
->writable_register
= snd_soc_default_writable_register
;
3327 for (i
= 0; i
< num_dai
; i
++) {
3328 fixup_codec_formats(&dai_drv
[i
].playback
);
3329 fixup_codec_formats(&dai_drv
[i
].capture
);
3332 /* register any DAIs */
3334 ret
= snd_soc_register_dais(dev
, dai_drv
, num_dai
);
3339 mutex_lock(&client_mutex
);
3340 list_add(&codec
->list
, &codec_list
);
3341 snd_soc_instantiate_cards();
3342 mutex_unlock(&client_mutex
);
3344 pr_debug("Registered codec '%s'\n", codec
->name
);
3348 kfree(codec
->reg_def_copy
);
3349 codec
->reg_def_copy
= NULL
;
3354 EXPORT_SYMBOL_GPL(snd_soc_register_codec
);
3357 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3359 * @codec: codec to unregister
3361 void snd_soc_unregister_codec(struct device
*dev
)
3363 struct snd_soc_codec
*codec
;
3366 list_for_each_entry(codec
, &codec_list
, list
) {
3367 if (dev
== codec
->dev
)
3374 for (i
= 0; i
< codec
->num_dai
; i
++)
3375 snd_soc_unregister_dai(dev
);
3377 mutex_lock(&client_mutex
);
3378 list_del(&codec
->list
);
3379 mutex_unlock(&client_mutex
);
3381 pr_debug("Unregistered codec '%s'\n", codec
->name
);
3383 snd_soc_cache_exit(codec
);
3384 kfree(codec
->reg_def_copy
);
3388 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec
);
3390 /* Retrieve a card's name from device tree */
3391 int snd_soc_of_parse_card_name(struct snd_soc_card
*card
,
3392 const char *propname
)
3394 struct device_node
*np
= card
->dev
->of_node
;
3397 ret
= of_property_read_string_index(np
, propname
, 0, &card
->name
);
3399 * EINVAL means the property does not exist. This is fine providing
3400 * card->name was previously set, which is checked later in
3401 * snd_soc_register_card.
3403 if (ret
< 0 && ret
!= -EINVAL
) {
3405 "Property '%s' could not be read: %d\n",
3412 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name
);
3414 int snd_soc_of_parse_audio_routing(struct snd_soc_card
*card
,
3415 const char *propname
)
3417 struct device_node
*np
= card
->dev
->of_node
;
3419 struct snd_soc_dapm_route
*routes
;
3422 num_routes
= of_property_count_strings(np
, propname
);
3423 if (num_routes
< 0 || num_routes
& 1) {
3425 "Property '%s' does not exist or its length is not even\n",
3432 "Property '%s's length is zero\n",
3437 routes
= devm_kzalloc(card
->dev
, num_routes
* sizeof(*routes
),
3441 "Could not allocate DAPM route table\n");
3445 for (i
= 0; i
< num_routes
; i
++) {
3446 ret
= of_property_read_string_index(np
, propname
,
3447 2 * i
, &routes
[i
].sink
);
3450 "Property '%s' index %d could not be read: %d\n",
3451 propname
, 2 * i
, ret
);
3454 ret
= of_property_read_string_index(np
, propname
,
3455 (2 * i
) + 1, &routes
[i
].source
);
3458 "Property '%s' index %d could not be read: %d\n",
3459 propname
, (2 * i
) + 1, ret
);
3464 card
->num_dapm_routes
= num_routes
;
3465 card
->dapm_routes
= routes
;
3469 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing
);
3471 static int __init
snd_soc_init(void)
3473 #ifdef CONFIG_DEBUG_FS
3474 snd_soc_debugfs_root
= debugfs_create_dir("asoc", NULL
);
3475 if (IS_ERR(snd_soc_debugfs_root
) || !snd_soc_debugfs_root
) {
3477 "ASoC: Failed to create debugfs directory\n");
3478 snd_soc_debugfs_root
= NULL
;
3481 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root
, NULL
,
3483 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3485 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root
, NULL
,
3487 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3489 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root
, NULL
,
3490 &platform_list_fops
))
3491 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3494 snd_soc_util_init();
3496 return platform_driver_register(&soc_driver
);
3498 module_init(snd_soc_init
);
3500 static void __exit
snd_soc_exit(void)
3502 snd_soc_util_exit();
3504 #ifdef CONFIG_DEBUG_FS
3505 debugfs_remove_recursive(snd_soc_debugfs_root
);
3507 platform_driver_unregister(&soc_driver
);
3509 module_exit(snd_soc_exit
);
3511 /* Module information */
3512 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3513 MODULE_DESCRIPTION("ALSA SoC Core");
3514 MODULE_LICENSE("GPL");
3515 MODULE_ALIAS("platform:soc-audio");