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/slab.h>
34 #include <sound/ac97_codec.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/soc-dapm.h>
40 #include <sound/initval.h>
44 static DEFINE_MUTEX(pcm_mutex
);
45 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq
);
47 #ifdef CONFIG_DEBUG_FS
48 static struct dentry
*debugfs_root
;
51 static DEFINE_MUTEX(client_mutex
);
52 static LIST_HEAD(card_list
);
53 static LIST_HEAD(dai_list
);
54 static LIST_HEAD(platform_list
);
55 static LIST_HEAD(codec_list
);
57 static int snd_soc_register_card(struct snd_soc_card
*card
);
58 static int snd_soc_unregister_card(struct snd_soc_card
*card
);
59 static int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
);
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
66 static int pmdown_time
= 5000;
67 module_param(pmdown_time
, int, 0);
68 MODULE_PARM_DESC(pmdown_time
, "DAPM stream powerdown time (msecs)");
71 * This function forces any delayed work to be queued and run.
73 static int run_delayed_work(struct delayed_work
*dwork
)
77 /* cancel any work waiting to be queued. */
78 ret
= cancel_delayed_work(dwork
);
80 /* if there was any work waiting then we run it now and
81 * wait for it's completion */
83 schedule_delayed_work(dwork
, 0);
84 flush_scheduled_work();
89 /* codec register dump */
90 static ssize_t
soc_codec_reg_show(struct snd_soc_codec
*codec
, char *buf
)
92 int ret
, i
, step
= 1, count
= 0;
94 if (!codec
->driver
->reg_cache_size
)
97 if (codec
->driver
->reg_cache_step
)
98 step
= codec
->driver
->reg_cache_step
;
100 count
+= sprintf(buf
, "%s registers\n", codec
->name
);
101 for (i
= 0; i
< codec
->driver
->reg_cache_size
; i
+= step
) {
102 if (codec
->driver
->readable_register
&& !codec
->driver
->readable_register(i
))
105 count
+= sprintf(buf
+ count
, "%2x: ", i
);
106 if (count
>= PAGE_SIZE
- 1)
109 if (codec
->driver
->display_register
) {
110 count
+= codec
->driver
->display_register(codec
, buf
+ count
,
111 PAGE_SIZE
- count
, i
);
113 /* If the read fails it's almost certainly due to
114 * the register being volatile and the device being
117 ret
= codec
->driver
->read(codec
, i
);
119 count
+= snprintf(buf
+ count
,
123 count
+= snprintf(buf
+ count
,
125 "<no data: %d>", ret
);
128 if (count
>= PAGE_SIZE
- 1)
131 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
132 if (count
>= PAGE_SIZE
- 1)
136 /* Truncate count; min() would cause a warning */
137 if (count
>= PAGE_SIZE
)
138 count
= PAGE_SIZE
- 1;
142 static ssize_t
codec_reg_show(struct device
*dev
,
143 struct device_attribute
*attr
, char *buf
)
145 struct snd_soc_pcm_runtime
*rtd
=
146 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
148 return soc_codec_reg_show(rtd
->codec
, buf
);
151 static DEVICE_ATTR(codec_reg
, 0444, codec_reg_show
, NULL
);
153 static ssize_t
pmdown_time_show(struct device
*dev
,
154 struct device_attribute
*attr
, char *buf
)
156 struct snd_soc_pcm_runtime
*rtd
=
157 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
159 return sprintf(buf
, "%ld\n", rtd
->pmdown_time
);
162 static ssize_t
pmdown_time_set(struct device
*dev
,
163 struct device_attribute
*attr
,
164 const char *buf
, size_t count
)
166 struct snd_soc_pcm_runtime
*rtd
=
167 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
170 ret
= strict_strtol(buf
, 10, &rtd
->pmdown_time
);
177 static DEVICE_ATTR(pmdown_time
, 0644, pmdown_time_show
, pmdown_time_set
);
179 #ifdef CONFIG_DEBUG_FS
180 static int codec_reg_open_file(struct inode
*inode
, struct file
*file
)
182 file
->private_data
= inode
->i_private
;
186 static ssize_t
codec_reg_read_file(struct file
*file
, char __user
*user_buf
,
187 size_t count
, loff_t
*ppos
)
190 struct snd_soc_codec
*codec
= file
->private_data
;
191 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
194 ret
= soc_codec_reg_show(codec
, buf
);
196 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
201 static ssize_t
codec_reg_write_file(struct file
*file
,
202 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
207 unsigned long reg
, value
;
209 struct snd_soc_codec
*codec
= file
->private_data
;
211 buf_size
= min(count
, (sizeof(buf
)-1));
212 if (copy_from_user(buf
, user_buf
, buf_size
))
216 if (codec
->driver
->reg_cache_step
)
217 step
= codec
->driver
->reg_cache_step
;
219 while (*start
== ' ')
221 reg
= simple_strtoul(start
, &start
, 16);
222 if ((reg
>= codec
->driver
->reg_cache_size
) || (reg
% step
))
224 while (*start
== ' ')
226 if (strict_strtoul(start
, 16, &value
))
228 codec
->driver
->write(codec
, reg
, value
);
232 static const struct file_operations codec_reg_fops
= {
233 .open
= codec_reg_open_file
,
234 .read
= codec_reg_read_file
,
235 .write
= codec_reg_write_file
,
236 .llseek
= default_llseek
,
239 static void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
241 codec
->debugfs_codec_root
= debugfs_create_dir(codec
->name
,
243 if (!codec
->debugfs_codec_root
) {
245 "ASoC: Failed to create codec debugfs directory\n");
249 codec
->debugfs_reg
= debugfs_create_file("codec_reg", 0644,
250 codec
->debugfs_codec_root
,
251 codec
, &codec_reg_fops
);
252 if (!codec
->debugfs_reg
)
254 "ASoC: Failed to create codec register debugfs file\n");
256 codec
->debugfs_pop_time
= debugfs_create_u32("dapm_pop_time", 0644,
257 codec
->debugfs_codec_root
,
258 &codec
->dapm
.pop_time
);
259 if (!codec
->debugfs_pop_time
)
261 "Failed to create pop time debugfs file\n");
263 codec
->dapm
.debugfs_dapm
= debugfs_create_dir("dapm",
264 codec
->debugfs_codec_root
);
265 if (!codec
->dapm
.debugfs_dapm
)
267 "Failed to create DAPM debugfs directory\n");
269 snd_soc_dapm_debugfs_init(&codec
->dapm
);
272 static void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
274 debugfs_remove_recursive(codec
->debugfs_codec_root
);
277 static ssize_t
codec_list_read_file(struct file
*file
, char __user
*user_buf
,
278 size_t count
, loff_t
*ppos
)
280 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
281 ssize_t len
, ret
= 0;
282 struct snd_soc_codec
*codec
;
287 list_for_each_entry(codec
, &codec_list
, list
) {
288 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
292 if (ret
> PAGE_SIZE
) {
299 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
306 static const struct file_operations codec_list_fops
= {
307 .read
= codec_list_read_file
,
308 .llseek
= default_llseek
,/* read accesses f_pos */
311 static ssize_t
dai_list_read_file(struct file
*file
, char __user
*user_buf
,
312 size_t count
, loff_t
*ppos
)
314 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
315 ssize_t len
, ret
= 0;
316 struct snd_soc_dai
*dai
;
321 list_for_each_entry(dai
, &dai_list
, list
) {
322 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n", dai
->name
);
325 if (ret
> PAGE_SIZE
) {
331 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
338 static const struct file_operations dai_list_fops
= {
339 .read
= dai_list_read_file
,
340 .llseek
= default_llseek
,/* read accesses f_pos */
343 static ssize_t
platform_list_read_file(struct file
*file
,
344 char __user
*user_buf
,
345 size_t count
, loff_t
*ppos
)
347 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
348 ssize_t len
, ret
= 0;
349 struct snd_soc_platform
*platform
;
354 list_for_each_entry(platform
, &platform_list
, list
) {
355 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
359 if (ret
> PAGE_SIZE
) {
365 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
372 static const struct file_operations platform_list_fops
= {
373 .read
= platform_list_read_file
,
374 .llseek
= default_llseek
,/* read accesses f_pos */
377 static void soc_init_card_debugfs(struct snd_soc_card
*card
)
379 card
->debugfs_card_root
= debugfs_create_dir(card
->name
,
381 if (!card
->debugfs_card_root
)
383 "ASoC: Failed to create codec debugfs directory\n");
386 static void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
388 debugfs_remove_recursive(card
->debugfs_card_root
);
393 static inline void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
397 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
402 #ifdef CONFIG_SND_SOC_AC97_BUS
403 /* unregister ac97 codec */
404 static int soc_ac97_dev_unregister(struct snd_soc_codec
*codec
)
406 if (codec
->ac97
->dev
.bus
)
407 device_unregister(&codec
->ac97
->dev
);
411 /* stop no dev release warning */
412 static void soc_ac97_device_release(struct device
*dev
){}
414 /* register ac97 codec to bus */
415 static int soc_ac97_dev_register(struct snd_soc_codec
*codec
)
419 codec
->ac97
->dev
.bus
= &ac97_bus_type
;
420 codec
->ac97
->dev
.parent
= codec
->card
->dev
;
421 codec
->ac97
->dev
.release
= soc_ac97_device_release
;
423 dev_set_name(&codec
->ac97
->dev
, "%d-%d:%s",
424 codec
->card
->snd_card
->number
, 0, codec
->name
);
425 err
= device_register(&codec
->ac97
->dev
);
427 snd_printk(KERN_ERR
"Can't register ac97 bus\n");
428 codec
->ac97
->dev
.bus
= NULL
;
435 static int soc_pcm_apply_symmetry(struct snd_pcm_substream
*substream
)
437 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
438 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
439 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
442 if (codec_dai
->driver
->symmetric_rates
|| cpu_dai
->driver
->symmetric_rates
||
443 rtd
->dai_link
->symmetric_rates
) {
444 dev_dbg(&rtd
->dev
, "Symmetry forces %dHz rate\n",
447 ret
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
448 SNDRV_PCM_HW_PARAM_RATE
,
453 "Unable to apply rate symmetry constraint: %d\n", ret
);
462 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
463 * then initialized and any private data can be allocated. This also calls
464 * startup for the cpu DAI, platform, machine and codec DAI.
466 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
468 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
469 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
470 struct snd_soc_platform
*platform
= rtd
->platform
;
471 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
472 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
473 struct snd_soc_dai_driver
*cpu_dai_drv
= cpu_dai
->driver
;
474 struct snd_soc_dai_driver
*codec_dai_drv
= codec_dai
->driver
;
477 mutex_lock(&pcm_mutex
);
479 /* startup the audio subsystem */
480 if (cpu_dai
->driver
->ops
->startup
) {
481 ret
= cpu_dai
->driver
->ops
->startup(substream
, cpu_dai
);
483 printk(KERN_ERR
"asoc: can't open interface %s\n",
489 if (platform
->driver
->ops
->open
) {
490 ret
= platform
->driver
->ops
->open(substream
);
492 printk(KERN_ERR
"asoc: can't open platform %s\n", platform
->name
);
497 if (codec_dai
->driver
->ops
->startup
) {
498 ret
= codec_dai
->driver
->ops
->startup(substream
, codec_dai
);
500 printk(KERN_ERR
"asoc: can't open codec %s\n",
506 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->startup
) {
507 ret
= rtd
->dai_link
->ops
->startup(substream
);
509 printk(KERN_ERR
"asoc: %s startup failed\n", rtd
->dai_link
->name
);
514 /* Check that the codec and cpu DAI's are compatible */
515 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
516 runtime
->hw
.rate_min
=
517 max(codec_dai_drv
->playback
.rate_min
,
518 cpu_dai_drv
->playback
.rate_min
);
519 runtime
->hw
.rate_max
=
520 min(codec_dai_drv
->playback
.rate_max
,
521 cpu_dai_drv
->playback
.rate_max
);
522 runtime
->hw
.channels_min
=
523 max(codec_dai_drv
->playback
.channels_min
,
524 cpu_dai_drv
->playback
.channels_min
);
525 runtime
->hw
.channels_max
=
526 min(codec_dai_drv
->playback
.channels_max
,
527 cpu_dai_drv
->playback
.channels_max
);
528 runtime
->hw
.formats
=
529 codec_dai_drv
->playback
.formats
& cpu_dai_drv
->playback
.formats
;
531 codec_dai_drv
->playback
.rates
& cpu_dai_drv
->playback
.rates
;
532 if (codec_dai_drv
->playback
.rates
533 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
534 runtime
->hw
.rates
|= cpu_dai_drv
->playback
.rates
;
535 if (cpu_dai_drv
->playback
.rates
536 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
537 runtime
->hw
.rates
|= codec_dai_drv
->playback
.rates
;
539 runtime
->hw
.rate_min
=
540 max(codec_dai_drv
->capture
.rate_min
,
541 cpu_dai_drv
->capture
.rate_min
);
542 runtime
->hw
.rate_max
=
543 min(codec_dai_drv
->capture
.rate_max
,
544 cpu_dai_drv
->capture
.rate_max
);
545 runtime
->hw
.channels_min
=
546 max(codec_dai_drv
->capture
.channels_min
,
547 cpu_dai_drv
->capture
.channels_min
);
548 runtime
->hw
.channels_max
=
549 min(codec_dai_drv
->capture
.channels_max
,
550 cpu_dai_drv
->capture
.channels_max
);
551 runtime
->hw
.formats
=
552 codec_dai_drv
->capture
.formats
& cpu_dai_drv
->capture
.formats
;
554 codec_dai_drv
->capture
.rates
& cpu_dai_drv
->capture
.rates
;
555 if (codec_dai_drv
->capture
.rates
556 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
557 runtime
->hw
.rates
|= cpu_dai_drv
->capture
.rates
;
558 if (cpu_dai_drv
->capture
.rates
559 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
560 runtime
->hw
.rates
|= codec_dai_drv
->capture
.rates
;
563 snd_pcm_limit_hw_rates(runtime
);
564 if (!runtime
->hw
.rates
) {
565 printk(KERN_ERR
"asoc: %s <-> %s No matching rates\n",
566 codec_dai
->name
, cpu_dai
->name
);
569 if (!runtime
->hw
.formats
) {
570 printk(KERN_ERR
"asoc: %s <-> %s No matching formats\n",
571 codec_dai
->name
, cpu_dai
->name
);
574 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
) {
575 printk(KERN_ERR
"asoc: %s <-> %s No matching channels\n",
576 codec_dai
->name
, cpu_dai
->name
);
580 /* Symmetry only applies if we've already got an active stream. */
581 if (cpu_dai
->active
|| codec_dai
->active
) {
582 ret
= soc_pcm_apply_symmetry(substream
);
587 pr_debug("asoc: %s <-> %s info:\n",
588 codec_dai
->name
, cpu_dai
->name
);
589 pr_debug("asoc: rate mask 0x%x\n", runtime
->hw
.rates
);
590 pr_debug("asoc: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
591 runtime
->hw
.channels_max
);
592 pr_debug("asoc: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
593 runtime
->hw
.rate_max
);
595 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
596 cpu_dai
->playback_active
++;
597 codec_dai
->playback_active
++;
599 cpu_dai
->capture_active
++;
600 codec_dai
->capture_active
++;
604 rtd
->codec
->active
++;
605 mutex_unlock(&pcm_mutex
);
609 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
610 rtd
->dai_link
->ops
->shutdown(substream
);
613 if (codec_dai
->driver
->ops
->shutdown
)
614 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
617 if (platform
->driver
->ops
->close
)
618 platform
->driver
->ops
->close(substream
);
621 if (cpu_dai
->driver
->ops
->shutdown
)
622 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
624 mutex_unlock(&pcm_mutex
);
629 * Power down the audio subsystem pmdown_time msecs after close is called.
630 * This is to ensure there are no pops or clicks in between any music tracks
631 * due to DAPM power cycling.
633 static void close_delayed_work(struct work_struct
*work
)
635 struct snd_soc_pcm_runtime
*rtd
=
636 container_of(work
, struct snd_soc_pcm_runtime
, delayed_work
.work
);
637 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
639 mutex_lock(&pcm_mutex
);
641 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
642 codec_dai
->driver
->playback
.stream_name
,
643 codec_dai
->playback_active
? "active" : "inactive",
644 codec_dai
->pop_wait
? "yes" : "no");
646 /* are we waiting on this codec DAI stream */
647 if (codec_dai
->pop_wait
== 1) {
648 codec_dai
->pop_wait
= 0;
649 snd_soc_dapm_stream_event(rtd
,
650 codec_dai
->driver
->playback
.stream_name
,
651 SND_SOC_DAPM_STREAM_STOP
);
654 mutex_unlock(&pcm_mutex
);
658 * Called by ALSA when a PCM substream is closed. Private data can be
659 * freed here. The cpu DAI, codec DAI, machine and platform are also
662 static int soc_codec_close(struct snd_pcm_substream
*substream
)
664 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
665 struct snd_soc_platform
*platform
= rtd
->platform
;
666 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
667 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
668 struct snd_soc_codec
*codec
= rtd
->codec
;
670 mutex_lock(&pcm_mutex
);
672 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
673 cpu_dai
->playback_active
--;
674 codec_dai
->playback_active
--;
676 cpu_dai
->capture_active
--;
677 codec_dai
->capture_active
--;
684 /* Muting the DAC suppresses artifacts caused during digital
685 * shutdown, for example from stopping clocks.
687 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
688 snd_soc_dai_digital_mute(codec_dai
, 1);
690 if (cpu_dai
->driver
->ops
->shutdown
)
691 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
693 if (codec_dai
->driver
->ops
->shutdown
)
694 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
696 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
697 rtd
->dai_link
->ops
->shutdown(substream
);
699 if (platform
->driver
->ops
->close
)
700 platform
->driver
->ops
->close(substream
);
701 cpu_dai
->runtime
= NULL
;
703 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
704 /* start delayed pop wq here for playback streams */
705 codec_dai
->pop_wait
= 1;
706 schedule_delayed_work(&rtd
->delayed_work
,
707 msecs_to_jiffies(rtd
->pmdown_time
));
709 /* capture streams can be powered down now */
710 snd_soc_dapm_stream_event(rtd
,
711 codec_dai
->driver
->capture
.stream_name
,
712 SND_SOC_DAPM_STREAM_STOP
);
715 mutex_unlock(&pcm_mutex
);
720 * Called by ALSA when the PCM substream is prepared, can set format, sample
721 * rate, etc. This function is non atomic and can be called multiple times,
722 * it can refer to the runtime info.
724 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
726 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
727 struct snd_soc_platform
*platform
= rtd
->platform
;
728 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
729 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
732 mutex_lock(&pcm_mutex
);
734 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->prepare
) {
735 ret
= rtd
->dai_link
->ops
->prepare(substream
);
737 printk(KERN_ERR
"asoc: machine prepare error\n");
742 if (platform
->driver
->ops
->prepare
) {
743 ret
= platform
->driver
->ops
->prepare(substream
);
745 printk(KERN_ERR
"asoc: platform prepare error\n");
750 if (codec_dai
->driver
->ops
->prepare
) {
751 ret
= codec_dai
->driver
->ops
->prepare(substream
, codec_dai
);
753 printk(KERN_ERR
"asoc: codec DAI prepare error\n");
758 if (cpu_dai
->driver
->ops
->prepare
) {
759 ret
= cpu_dai
->driver
->ops
->prepare(substream
, cpu_dai
);
761 printk(KERN_ERR
"asoc: cpu DAI prepare error\n");
766 /* cancel any delayed stream shutdown that is pending */
767 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
768 codec_dai
->pop_wait
) {
769 codec_dai
->pop_wait
= 0;
770 cancel_delayed_work(&rtd
->delayed_work
);
773 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
774 snd_soc_dapm_stream_event(rtd
,
775 codec_dai
->driver
->playback
.stream_name
,
776 SND_SOC_DAPM_STREAM_START
);
778 snd_soc_dapm_stream_event(rtd
,
779 codec_dai
->driver
->capture
.stream_name
,
780 SND_SOC_DAPM_STREAM_START
);
782 snd_soc_dai_digital_mute(codec_dai
, 0);
785 mutex_unlock(&pcm_mutex
);
790 * Called by ALSA when the hardware params are set by application. This
791 * function can also be called multiple times and can allocate buffers
792 * (using snd_pcm_lib_* ). It's non-atomic.
794 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
795 struct snd_pcm_hw_params
*params
)
797 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
798 struct snd_soc_platform
*platform
= rtd
->platform
;
799 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
800 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
803 mutex_lock(&pcm_mutex
);
805 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_params
) {
806 ret
= rtd
->dai_link
->ops
->hw_params(substream
, params
);
808 printk(KERN_ERR
"asoc: machine hw_params failed\n");
813 if (codec_dai
->driver
->ops
->hw_params
) {
814 ret
= codec_dai
->driver
->ops
->hw_params(substream
, params
, codec_dai
);
816 printk(KERN_ERR
"asoc: can't set codec %s hw params\n",
822 if (cpu_dai
->driver
->ops
->hw_params
) {
823 ret
= cpu_dai
->driver
->ops
->hw_params(substream
, params
, cpu_dai
);
825 printk(KERN_ERR
"asoc: interface %s hw params failed\n",
831 if (platform
->driver
->ops
->hw_params
) {
832 ret
= platform
->driver
->ops
->hw_params(substream
, params
);
834 printk(KERN_ERR
"asoc: platform %s hw params failed\n",
840 rtd
->rate
= params_rate(params
);
843 mutex_unlock(&pcm_mutex
);
847 if (cpu_dai
->driver
->ops
->hw_free
)
848 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
851 if (codec_dai
->driver
->ops
->hw_free
)
852 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
855 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
856 rtd
->dai_link
->ops
->hw_free(substream
);
858 mutex_unlock(&pcm_mutex
);
863 * Free's resources allocated by hw_params, can be called multiple times
865 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
867 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
868 struct snd_soc_platform
*platform
= rtd
->platform
;
869 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
870 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
871 struct snd_soc_codec
*codec
= rtd
->codec
;
873 mutex_lock(&pcm_mutex
);
875 /* apply codec digital mute */
877 snd_soc_dai_digital_mute(codec_dai
, 1);
879 /* free any machine hw params */
880 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
881 rtd
->dai_link
->ops
->hw_free(substream
);
883 /* free any DMA resources */
884 if (platform
->driver
->ops
->hw_free
)
885 platform
->driver
->ops
->hw_free(substream
);
887 /* now free hw params for the DAI's */
888 if (codec_dai
->driver
->ops
->hw_free
)
889 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
891 if (cpu_dai
->driver
->ops
->hw_free
)
892 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
894 mutex_unlock(&pcm_mutex
);
898 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
900 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
901 struct snd_soc_platform
*platform
= rtd
->platform
;
902 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
903 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
906 if (codec_dai
->driver
->ops
->trigger
) {
907 ret
= codec_dai
->driver
->ops
->trigger(substream
, cmd
, codec_dai
);
912 if (platform
->driver
->ops
->trigger
) {
913 ret
= platform
->driver
->ops
->trigger(substream
, cmd
);
918 if (cpu_dai
->driver
->ops
->trigger
) {
919 ret
= cpu_dai
->driver
->ops
->trigger(substream
, cmd
, cpu_dai
);
927 * soc level wrapper for pointer callback
928 * If cpu_dai, codec_dai, platform driver has the delay callback, than
929 * the runtime->delay will be updated accordingly.
931 static snd_pcm_uframes_t
soc_pcm_pointer(struct snd_pcm_substream
*substream
)
933 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
934 struct snd_soc_platform
*platform
= rtd
->platform
;
935 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
936 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
937 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
938 snd_pcm_uframes_t offset
= 0;
939 snd_pcm_sframes_t delay
= 0;
941 if (platform
->driver
->ops
->pointer
)
942 offset
= platform
->driver
->ops
->pointer(substream
);
944 if (cpu_dai
->driver
->ops
->delay
)
945 delay
+= cpu_dai
->driver
->ops
->delay(substream
, cpu_dai
);
947 if (codec_dai
->driver
->ops
->delay
)
948 delay
+= codec_dai
->driver
->ops
->delay(substream
, codec_dai
);
950 if (platform
->driver
->delay
)
951 delay
+= platform
->driver
->delay(substream
, codec_dai
);
953 runtime
->delay
= delay
;
958 /* ASoC PCM operations */
959 static struct snd_pcm_ops soc_pcm_ops
= {
960 .open
= soc_pcm_open
,
961 .close
= soc_codec_close
,
962 .hw_params
= soc_pcm_hw_params
,
963 .hw_free
= soc_pcm_hw_free
,
964 .prepare
= soc_pcm_prepare
,
965 .trigger
= soc_pcm_trigger
,
966 .pointer
= soc_pcm_pointer
,
970 /* powers down audio subsystem for suspend */
971 static int soc_suspend(struct device
*dev
)
973 struct platform_device
*pdev
= to_platform_device(dev
);
974 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
977 /* If the initialization of this soc device failed, there is no codec
978 * associated with it. Just bail out in this case.
980 if (list_empty(&card
->codec_dev_list
))
983 /* Due to the resume being scheduled into a workqueue we could
984 * suspend before that's finished - wait for it to complete.
986 snd_power_lock(card
->snd_card
);
987 snd_power_wait(card
->snd_card
, SNDRV_CTL_POWER_D0
);
988 snd_power_unlock(card
->snd_card
);
990 /* we're going to block userspace touching us until resume completes */
991 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D3hot
);
993 /* mute any active DAC's */
994 for (i
= 0; i
< card
->num_rtd
; i
++) {
995 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
996 struct snd_soc_dai_driver
*drv
= dai
->driver
;
998 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1001 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
1002 drv
->ops
->digital_mute(dai
, 1);
1005 /* suspend all pcms */
1006 for (i
= 0; i
< card
->num_rtd
; i
++) {
1007 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1010 snd_pcm_suspend_all(card
->rtd
[i
].pcm
);
1013 if (card
->suspend_pre
)
1014 card
->suspend_pre(pdev
, PMSG_SUSPEND
);
1016 for (i
= 0; i
< card
->num_rtd
; i
++) {
1017 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1018 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1020 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1023 if (cpu_dai
->driver
->suspend
&& !cpu_dai
->driver
->ac97_control
)
1024 cpu_dai
->driver
->suspend(cpu_dai
);
1025 if (platform
->driver
->suspend
&& !platform
->suspended
) {
1026 platform
->driver
->suspend(cpu_dai
);
1027 platform
->suspended
= 1;
1031 /* close any waiting streams and save state */
1032 for (i
= 0; i
< card
->num_rtd
; i
++) {
1033 run_delayed_work(&card
->rtd
[i
].delayed_work
);
1034 card
->rtd
[i
].codec
->dapm
.suspend_bias_level
= card
->rtd
[i
].codec
->dapm
.bias_level
;
1037 for (i
= 0; i
< card
->num_rtd
; i
++) {
1038 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1040 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1043 if (driver
->playback
.stream_name
!= NULL
)
1044 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1045 SND_SOC_DAPM_STREAM_SUSPEND
);
1047 if (driver
->capture
.stream_name
!= NULL
)
1048 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1049 SND_SOC_DAPM_STREAM_SUSPEND
);
1052 /* suspend all CODECs */
1053 for (i
= 0; i
< card
->num_rtd
; i
++) {
1054 struct snd_soc_codec
*codec
= card
->rtd
[i
].codec
;
1055 /* If there are paths active then the CODEC will be held with
1056 * bias _ON and should not be suspended. */
1057 if (!codec
->suspended
&& codec
->driver
->suspend
) {
1058 switch (codec
->dapm
.bias_level
) {
1059 case SND_SOC_BIAS_STANDBY
:
1060 case SND_SOC_BIAS_OFF
:
1061 codec
->driver
->suspend(codec
, PMSG_SUSPEND
);
1062 codec
->suspended
= 1;
1065 dev_dbg(codec
->dev
, "CODEC is on over suspend\n");
1071 for (i
= 0; i
< card
->num_rtd
; i
++) {
1072 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1074 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1077 if (cpu_dai
->driver
->suspend
&& cpu_dai
->driver
->ac97_control
)
1078 cpu_dai
->driver
->suspend(cpu_dai
);
1081 if (card
->suspend_post
)
1082 card
->suspend_post(pdev
, PMSG_SUSPEND
);
1087 /* deferred resume work, so resume can complete before we finished
1088 * setting our codec back up, which can be very slow on I2C
1090 static void soc_resume_deferred(struct work_struct
*work
)
1092 struct snd_soc_card
*card
=
1093 container_of(work
, struct snd_soc_card
, deferred_resume_work
);
1094 struct platform_device
*pdev
= to_platform_device(card
->dev
);
1097 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1098 * so userspace apps are blocked from touching us
1101 dev_dbg(card
->dev
, "starting resume work\n");
1103 /* Bring us up into D2 so that DAPM starts enabling things */
1104 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D2
);
1106 if (card
->resume_pre
)
1107 card
->resume_pre(pdev
);
1109 /* resume AC97 DAIs */
1110 for (i
= 0; i
< card
->num_rtd
; i
++) {
1111 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1113 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1116 if (cpu_dai
->driver
->resume
&& cpu_dai
->driver
->ac97_control
)
1117 cpu_dai
->driver
->resume(cpu_dai
);
1120 for (i
= 0; i
< card
->num_rtd
; i
++) {
1121 struct snd_soc_codec
*codec
= card
->rtd
[i
].codec
;
1122 /* If the CODEC was idle over suspend then it will have been
1123 * left with bias OFF or STANDBY and suspended so we must now
1124 * resume. Otherwise the suspend was suppressed.
1126 if (codec
->driver
->resume
&& codec
->suspended
) {
1127 switch (codec
->dapm
.bias_level
) {
1128 case SND_SOC_BIAS_STANDBY
:
1129 case SND_SOC_BIAS_OFF
:
1130 codec
->driver
->resume(codec
);
1131 codec
->suspended
= 0;
1134 dev_dbg(codec
->dev
, "CODEC was on over suspend\n");
1140 for (i
= 0; i
< card
->num_rtd
; i
++) {
1141 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1143 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1146 if (driver
->playback
.stream_name
!= NULL
)
1147 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1148 SND_SOC_DAPM_STREAM_RESUME
);
1150 if (driver
->capture
.stream_name
!= NULL
)
1151 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1152 SND_SOC_DAPM_STREAM_RESUME
);
1155 /* unmute any active DACs */
1156 for (i
= 0; i
< card
->num_rtd
; i
++) {
1157 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
1158 struct snd_soc_dai_driver
*drv
= dai
->driver
;
1160 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1163 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
1164 drv
->ops
->digital_mute(dai
, 0);
1167 for (i
= 0; i
< card
->num_rtd
; i
++) {
1168 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1169 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1171 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1174 if (cpu_dai
->driver
->resume
&& !cpu_dai
->driver
->ac97_control
)
1175 cpu_dai
->driver
->resume(cpu_dai
);
1176 if (platform
->driver
->resume
&& platform
->suspended
) {
1177 platform
->driver
->resume(cpu_dai
);
1178 platform
->suspended
= 0;
1182 if (card
->resume_post
)
1183 card
->resume_post(pdev
);
1185 dev_dbg(card
->dev
, "resume work completed\n");
1187 /* userspace can access us now we are back as we were before */
1188 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D0
);
1191 /* powers up audio subsystem after a suspend */
1192 static int soc_resume(struct device
*dev
)
1194 struct platform_device
*pdev
= to_platform_device(dev
);
1195 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1198 /* AC97 devices might have other drivers hanging off them so
1199 * need to resume immediately. Other drivers don't have that
1200 * problem and may take a substantial amount of time to resume
1201 * due to I/O costs and anti-pop so handle them out of line.
1203 for (i
= 0; i
< card
->num_rtd
; i
++) {
1204 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1205 if (cpu_dai
->driver
->ac97_control
) {
1206 dev_dbg(dev
, "Resuming AC97 immediately\n");
1207 soc_resume_deferred(&card
->deferred_resume_work
);
1209 dev_dbg(dev
, "Scheduling resume work\n");
1210 if (!schedule_work(&card
->deferred_resume_work
))
1211 dev_err(dev
, "resume work item may be lost\n");
1218 #define soc_suspend NULL
1219 #define soc_resume NULL
1222 static struct snd_soc_dai_ops null_dai_ops
= {
1225 static int soc_bind_dai_link(struct snd_soc_card
*card
, int num
)
1227 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1228 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1229 struct snd_soc_codec
*codec
;
1230 struct snd_soc_platform
*platform
;
1231 struct snd_soc_dai
*codec_dai
, *cpu_dai
;
1235 dev_dbg(card
->dev
, "binding %s at idx %d\n", dai_link
->name
, num
);
1237 /* do we already have the CPU DAI for this link ? */
1241 /* no, then find CPU DAI from registered DAIs*/
1242 list_for_each_entry(cpu_dai
, &dai_list
, list
) {
1243 if (!strcmp(cpu_dai
->name
, dai_link
->cpu_dai_name
)) {
1245 if (!try_module_get(cpu_dai
->dev
->driver
->owner
))
1248 rtd
->cpu_dai
= cpu_dai
;
1252 dev_dbg(card
->dev
, "CPU DAI %s not registered\n",
1253 dai_link
->cpu_dai_name
);
1256 /* do we already have the CODEC for this link ? */
1261 /* no, then find CODEC from registered CODECs*/
1262 list_for_each_entry(codec
, &codec_list
, list
) {
1263 if (!strcmp(codec
->name
, dai_link
->codec_name
)) {
1266 if (!try_module_get(codec
->dev
->driver
->owner
))
1269 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1270 list_for_each_entry(codec_dai
, &dai_list
, list
) {
1271 if (codec
->dev
== codec_dai
->dev
&&
1272 !strcmp(codec_dai
->name
, dai_link
->codec_dai_name
)) {
1273 rtd
->codec_dai
= codec_dai
;
1277 dev_dbg(card
->dev
, "CODEC DAI %s not registered\n",
1278 dai_link
->codec_dai_name
);
1283 dev_dbg(card
->dev
, "CODEC %s not registered\n",
1284 dai_link
->codec_name
);
1287 /* do we already have the CODEC DAI for this link ? */
1288 if (rtd
->platform
) {
1291 /* no, then find CPU DAI from registered DAIs*/
1292 list_for_each_entry(platform
, &platform_list
, list
) {
1293 if (!strcmp(platform
->name
, dai_link
->platform_name
)) {
1295 if (!try_module_get(platform
->dev
->driver
->owner
))
1298 rtd
->platform
= platform
;
1303 dev_dbg(card
->dev
, "platform %s not registered\n",
1304 dai_link
->platform_name
);
1308 /* mark rtd as complete if we found all 4 of our client devices */
1309 if (rtd
->codec
&& rtd
->codec_dai
&& rtd
->platform
&& rtd
->cpu_dai
) {
1316 static void soc_remove_dai_link(struct snd_soc_card
*card
, int num
)
1318 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1319 struct snd_soc_codec
*codec
= rtd
->codec
;
1320 struct snd_soc_platform
*platform
= rtd
->platform
;
1321 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1324 /* unregister the rtd device */
1325 if (rtd
->dev_registered
) {
1326 device_remove_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1327 device_unregister(&rtd
->dev
);
1328 rtd
->dev_registered
= 0;
1331 /* remove the CODEC DAI */
1332 if (codec_dai
&& codec_dai
->probed
) {
1333 if (codec_dai
->driver
->remove
) {
1334 err
= codec_dai
->driver
->remove(codec_dai
);
1336 printk(KERN_ERR
"asoc: failed to remove %s\n", codec_dai
->name
);
1338 codec_dai
->probed
= 0;
1339 list_del(&codec_dai
->card_list
);
1342 /* remove the platform */
1343 if (platform
&& platform
->probed
) {
1344 if (platform
->driver
->remove
) {
1345 err
= platform
->driver
->remove(platform
);
1347 printk(KERN_ERR
"asoc: failed to remove %s\n", platform
->name
);
1349 platform
->probed
= 0;
1350 list_del(&platform
->card_list
);
1351 module_put(platform
->dev
->driver
->owner
);
1354 /* remove the CODEC */
1355 if (codec
&& codec
->probed
) {
1356 if (codec
->driver
->remove
) {
1357 err
= codec
->driver
->remove(codec
);
1359 printk(KERN_ERR
"asoc: failed to remove %s\n", codec
->name
);
1362 /* Make sure all DAPM widgets are freed */
1363 snd_soc_dapm_free(&codec
->dapm
);
1365 soc_cleanup_codec_debugfs(codec
);
1366 device_remove_file(&rtd
->dev
, &dev_attr_codec_reg
);
1368 list_del(&codec
->card_list
);
1369 module_put(codec
->dev
->driver
->owner
);
1372 /* remove the cpu_dai */
1373 if (cpu_dai
&& cpu_dai
->probed
) {
1374 if (cpu_dai
->driver
->remove
) {
1375 err
= cpu_dai
->driver
->remove(cpu_dai
);
1377 printk(KERN_ERR
"asoc: failed to remove %s\n", cpu_dai
->name
);
1379 cpu_dai
->probed
= 0;
1380 list_del(&cpu_dai
->card_list
);
1381 module_put(cpu_dai
->dev
->driver
->owner
);
1385 static void rtd_release(struct device
*dev
) {}
1387 static int soc_probe_dai_link(struct snd_soc_card
*card
, int num
)
1389 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1390 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1391 struct snd_soc_codec
*codec
= rtd
->codec
;
1392 struct snd_soc_platform
*platform
= rtd
->platform
;
1393 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1396 dev_dbg(card
->dev
, "probe %s dai link %d\n", card
->name
, num
);
1398 /* config components */
1399 codec_dai
->codec
= codec
;
1401 cpu_dai
->platform
= platform
;
1403 rtd
->dev
.parent
= card
->dev
;
1404 codec_dai
->card
= card
;
1405 cpu_dai
->card
= card
;
1407 /* set default power off timeout */
1408 rtd
->pmdown_time
= pmdown_time
;
1410 /* probe the cpu_dai */
1411 if (!cpu_dai
->probed
) {
1412 if (cpu_dai
->driver
->probe
) {
1413 ret
= cpu_dai
->driver
->probe(cpu_dai
);
1415 printk(KERN_ERR
"asoc: failed to probe CPU DAI %s\n",
1420 cpu_dai
->probed
= 1;
1421 /* mark cpu_dai as probed and add to card cpu_dai list */
1422 list_add(&cpu_dai
->card_list
, &card
->dai_dev_list
);
1425 /* probe the CODEC */
1426 if (!codec
->probed
) {
1427 if (codec
->driver
->probe
) {
1428 ret
= codec
->driver
->probe(codec
);
1430 printk(KERN_ERR
"asoc: failed to probe CODEC %s\n",
1436 soc_init_codec_debugfs(codec
);
1438 /* mark codec as probed and add to card codec list */
1440 list_add(&codec
->card_list
, &card
->codec_dev_list
);
1443 /* probe the platform */
1444 if (!platform
->probed
) {
1445 if (platform
->driver
->probe
) {
1446 ret
= platform
->driver
->probe(platform
);
1448 printk(KERN_ERR
"asoc: failed to probe platform %s\n",
1453 /* mark platform as probed and add to card platform list */
1454 platform
->probed
= 1;
1455 list_add(&platform
->card_list
, &card
->platform_dev_list
);
1458 /* probe the CODEC DAI */
1459 if (!codec_dai
->probed
) {
1460 if (codec_dai
->driver
->probe
) {
1461 ret
= codec_dai
->driver
->probe(codec_dai
);
1463 printk(KERN_ERR
"asoc: failed to probe CODEC DAI %s\n",
1469 /* mark cpu_dai as probed and add to card cpu_dai list */
1470 codec_dai
->probed
= 1;
1471 list_add(&codec_dai
->card_list
, &card
->dai_dev_list
);
1474 /* DAPM dai link stream work */
1475 INIT_DELAYED_WORK(&rtd
->delayed_work
, close_delayed_work
);
1477 /* now that all clients have probed, initialise the DAI link */
1478 if (dai_link
->init
) {
1479 ret
= dai_link
->init(rtd
);
1481 printk(KERN_ERR
"asoc: failed to init %s\n", dai_link
->stream_name
);
1486 /* Make sure all DAPM widgets are instantiated */
1487 snd_soc_dapm_new_widgets(&codec
->dapm
);
1488 snd_soc_dapm_sync(&codec
->dapm
);
1490 /* register the rtd device */
1491 rtd
->dev
.release
= rtd_release
;
1492 rtd
->dev
.init_name
= dai_link
->name
;
1493 ret
= device_register(&rtd
->dev
);
1495 printk(KERN_ERR
"asoc: failed to register DAI runtime device %d\n", ret
);
1499 rtd
->dev_registered
= 1;
1500 ret
= device_create_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1502 printk(KERN_WARNING
"asoc: failed to add pmdown_time sysfs\n");
1504 /* add DAPM sysfs entries for this codec */
1505 ret
= snd_soc_dapm_sys_add(&rtd
->dev
);
1507 printk(KERN_WARNING
"asoc: failed to add codec dapm sysfs entries\n");
1509 /* add codec sysfs entries */
1510 ret
= device_create_file(&rtd
->dev
, &dev_attr_codec_reg
);
1512 printk(KERN_WARNING
"asoc: failed to add codec sysfs files\n");
1514 /* create the pcm */
1515 ret
= soc_new_pcm(rtd
, num
);
1517 printk(KERN_ERR
"asoc: can't create pcm %s\n", dai_link
->stream_name
);
1521 /* add platform data for AC97 devices */
1522 if (rtd
->codec_dai
->driver
->ac97_control
)
1523 snd_ac97_dev_add_pdata(codec
->ac97
, rtd
->cpu_dai
->ac97_pdata
);
1528 #ifdef CONFIG_SND_SOC_AC97_BUS
1529 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime
*rtd
)
1533 /* Only instantiate AC97 if not already done by the adaptor
1534 * for the generic AC97 subsystem.
1536 if (rtd
->codec_dai
->driver
->ac97_control
&& !rtd
->codec
->ac97_registered
) {
1538 * It is possible that the AC97 device is already registered to
1539 * the device subsystem. This happens when the device is created
1540 * via snd_ac97_mixer(). Currently only SoC codec that does so
1541 * is the generic AC97 glue but others migh emerge.
1543 * In those cases we don't try to register the device again.
1545 if (!rtd
->codec
->ac97_created
)
1548 ret
= soc_ac97_dev_register(rtd
->codec
);
1550 printk(KERN_ERR
"asoc: AC97 device register failed\n");
1554 rtd
->codec
->ac97_registered
= 1;
1559 static void soc_unregister_ac97_dai_link(struct snd_soc_codec
*codec
)
1561 if (codec
->ac97_registered
) {
1562 soc_ac97_dev_unregister(codec
);
1563 codec
->ac97_registered
= 0;
1568 static void snd_soc_instantiate_card(struct snd_soc_card
*card
)
1570 struct platform_device
*pdev
= to_platform_device(card
->dev
);
1573 mutex_lock(&card
->mutex
);
1575 if (card
->instantiated
) {
1576 mutex_unlock(&card
->mutex
);
1581 for (i
= 0; i
< card
->num_links
; i
++)
1582 soc_bind_dai_link(card
, i
);
1584 /* bind completed ? */
1585 if (card
->num_rtd
!= card
->num_links
) {
1586 mutex_unlock(&card
->mutex
);
1590 /* card bind complete so register a sound card */
1591 ret
= snd_card_create(SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
1592 card
->owner
, 0, &card
->snd_card
);
1594 printk(KERN_ERR
"asoc: can't create sound card for card %s\n",
1596 mutex_unlock(&card
->mutex
);
1599 card
->snd_card
->dev
= card
->dev
;
1602 /* deferred resume work */
1603 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
1606 /* initialise the sound card only once */
1608 ret
= card
->probe(pdev
);
1610 goto card_probe_error
;
1613 for (i
= 0; i
< card
->num_links
; i
++) {
1614 ret
= soc_probe_dai_link(card
, i
);
1616 pr_err("asoc: failed to instantiate card %s: %d\n",
1622 snprintf(card
->snd_card
->shortname
, sizeof(card
->snd_card
->shortname
),
1624 snprintf(card
->snd_card
->longname
, sizeof(card
->snd_card
->longname
),
1627 ret
= snd_card_register(card
->snd_card
);
1629 printk(KERN_ERR
"asoc: failed to register soundcard for %s\n", card
->name
);
1633 #ifdef CONFIG_SND_SOC_AC97_BUS
1634 /* register any AC97 codecs */
1635 for (i
= 0; i
< card
->num_rtd
; i
++) {
1636 ret
= soc_register_ac97_dai_link(&card
->rtd
[i
]);
1638 printk(KERN_ERR
"asoc: failed to register AC97 %s\n", card
->name
);
1644 card
->instantiated
= 1;
1645 mutex_unlock(&card
->mutex
);
1649 for (i
= 0; i
< card
->num_links
; i
++)
1650 soc_remove_dai_link(card
, i
);
1656 snd_card_free(card
->snd_card
);
1658 mutex_unlock(&card
->mutex
);
1662 * Attempt to initialise any uninitialised cards. Must be called with
1665 static void snd_soc_instantiate_cards(void)
1667 struct snd_soc_card
*card
;
1668 list_for_each_entry(card
, &card_list
, list
)
1669 snd_soc_instantiate_card(card
);
1672 /* probes a new socdev */
1673 static int soc_probe(struct platform_device
*pdev
)
1675 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1678 /* Bodge while we unpick instantiation */
1679 card
->dev
= &pdev
->dev
;
1680 INIT_LIST_HEAD(&card
->dai_dev_list
);
1681 INIT_LIST_HEAD(&card
->codec_dev_list
);
1682 INIT_LIST_HEAD(&card
->platform_dev_list
);
1684 soc_init_card_debugfs(card
);
1686 ret
= snd_soc_register_card(card
);
1688 dev_err(&pdev
->dev
, "Failed to register card\n");
1695 /* removes a socdev */
1696 static int soc_remove(struct platform_device
*pdev
)
1698 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1701 if (card
->instantiated
) {
1703 /* make sure any delayed work runs */
1704 for (i
= 0; i
< card
->num_rtd
; i
++) {
1705 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1706 run_delayed_work(&rtd
->delayed_work
);
1709 /* remove and free each DAI */
1710 for (i
= 0; i
< card
->num_rtd
; i
++)
1711 soc_remove_dai_link(card
, i
);
1713 soc_cleanup_card_debugfs(card
);
1715 /* remove the card */
1720 snd_card_free(card
->snd_card
);
1722 snd_soc_unregister_card(card
);
1726 static int soc_poweroff(struct device
*dev
)
1728 struct platform_device
*pdev
= to_platform_device(dev
);
1729 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1732 if (!card
->instantiated
)
1735 /* Flush out pmdown_time work - we actually do want to run it
1736 * now, we're shutting down so no imminent restart. */
1737 for (i
= 0; i
< card
->num_rtd
; i
++) {
1738 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1739 run_delayed_work(&rtd
->delayed_work
);
1742 snd_soc_dapm_shutdown(card
);
1747 static const struct dev_pm_ops soc_pm_ops
= {
1748 .suspend
= soc_suspend
,
1749 .resume
= soc_resume
,
1750 .poweroff
= soc_poweroff
,
1753 /* ASoC platform driver */
1754 static struct platform_driver soc_driver
= {
1756 .name
= "soc-audio",
1757 .owner
= THIS_MODULE
,
1761 .remove
= soc_remove
,
1764 /* create a new pcm */
1765 static int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
)
1767 struct snd_soc_codec
*codec
= rtd
->codec
;
1768 struct snd_soc_platform
*platform
= rtd
->platform
;
1769 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
1770 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1771 struct snd_pcm
*pcm
;
1773 int ret
= 0, playback
= 0, capture
= 0;
1775 /* check client and interface hw capabilities */
1776 snprintf(new_name
, sizeof(new_name
), "%s %s-%d",
1777 rtd
->dai_link
->stream_name
, codec_dai
->name
, num
);
1779 if (codec_dai
->driver
->playback
.channels_min
)
1781 if (codec_dai
->driver
->capture
.channels_min
)
1784 dev_dbg(rtd
->card
->dev
, "registered pcm #%d %s\n",num
,new_name
);
1785 ret
= snd_pcm_new(rtd
->card
->snd_card
, new_name
,
1786 num
, playback
, capture
, &pcm
);
1788 printk(KERN_ERR
"asoc: can't create pcm for codec %s\n", codec
->name
);
1793 pcm
->private_data
= rtd
;
1794 soc_pcm_ops
.mmap
= platform
->driver
->ops
->mmap
;
1795 soc_pcm_ops
.pointer
= platform
->driver
->ops
->pointer
;
1796 soc_pcm_ops
.ioctl
= platform
->driver
->ops
->ioctl
;
1797 soc_pcm_ops
.copy
= platform
->driver
->ops
->copy
;
1798 soc_pcm_ops
.silence
= platform
->driver
->ops
->silence
;
1799 soc_pcm_ops
.ack
= platform
->driver
->ops
->ack
;
1800 soc_pcm_ops
.page
= platform
->driver
->ops
->page
;
1803 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &soc_pcm_ops
);
1806 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &soc_pcm_ops
);
1808 ret
= platform
->driver
->pcm_new(rtd
->card
->snd_card
, codec_dai
, pcm
);
1810 printk(KERN_ERR
"asoc: platform pcm constructor failed\n");
1814 pcm
->private_free
= platform
->driver
->pcm_free
;
1815 printk(KERN_INFO
"asoc: %s <-> %s mapping ok\n", codec_dai
->name
,
1821 * snd_soc_codec_volatile_register: Report if a register is volatile.
1823 * @codec: CODEC to query.
1824 * @reg: Register to query.
1826 * Boolean function indiciating if a CODEC register is volatile.
1828 int snd_soc_codec_volatile_register(struct snd_soc_codec
*codec
, int reg
)
1830 if (codec
->driver
->volatile_register
)
1831 return codec
->driver
->volatile_register(reg
);
1835 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register
);
1838 * snd_soc_new_ac97_codec - initailise AC97 device
1839 * @codec: audio codec
1840 * @ops: AC97 bus operations
1841 * @num: AC97 codec number
1843 * Initialises AC97 codec resources for use by ad-hoc devices only.
1845 int snd_soc_new_ac97_codec(struct snd_soc_codec
*codec
,
1846 struct snd_ac97_bus_ops
*ops
, int num
)
1848 mutex_lock(&codec
->mutex
);
1850 codec
->ac97
= kzalloc(sizeof(struct snd_ac97
), GFP_KERNEL
);
1851 if (codec
->ac97
== NULL
) {
1852 mutex_unlock(&codec
->mutex
);
1856 codec
->ac97
->bus
= kzalloc(sizeof(struct snd_ac97_bus
), GFP_KERNEL
);
1857 if (codec
->ac97
->bus
== NULL
) {
1860 mutex_unlock(&codec
->mutex
);
1864 codec
->ac97
->bus
->ops
= ops
;
1865 codec
->ac97
->num
= num
;
1868 * Mark the AC97 device to be created by us. This way we ensure that the
1869 * device will be registered with the device subsystem later on.
1871 codec
->ac97_created
= 1;
1873 mutex_unlock(&codec
->mutex
);
1876 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec
);
1879 * snd_soc_free_ac97_codec - free AC97 codec device
1880 * @codec: audio codec
1882 * Frees AC97 codec device resources.
1884 void snd_soc_free_ac97_codec(struct snd_soc_codec
*codec
)
1886 mutex_lock(&codec
->mutex
);
1887 #ifdef CONFIG_SND_SOC_AC97_BUS
1888 soc_unregister_ac97_dai_link(codec
);
1890 kfree(codec
->ac97
->bus
);
1893 codec
->ac97_created
= 0;
1894 mutex_unlock(&codec
->mutex
);
1896 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec
);
1898 unsigned int snd_soc_read(struct snd_soc_codec
*codec
, unsigned int reg
)
1902 ret
= codec
->driver
->read(codec
, reg
);
1903 dev_dbg(codec
->dev
, "read %x => %x\n", reg
, ret
);
1907 EXPORT_SYMBOL_GPL(snd_soc_read
);
1909 unsigned int snd_soc_write(struct snd_soc_codec
*codec
,
1910 unsigned int reg
, unsigned int val
)
1912 dev_dbg(codec
->dev
, "write %x = %x\n", reg
, val
);
1913 return codec
->driver
->write(codec
, reg
, val
);
1915 EXPORT_SYMBOL_GPL(snd_soc_write
);
1918 * snd_soc_update_bits - update codec register bits
1919 * @codec: audio codec
1920 * @reg: codec register
1921 * @mask: register mask
1924 * Writes new register value.
1926 * Returns 1 for change else 0.
1928 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1929 unsigned int mask
, unsigned int value
)
1932 unsigned int old
, new;
1934 old
= snd_soc_read(codec
, reg
);
1935 new = (old
& ~mask
) | value
;
1936 change
= old
!= new;
1938 snd_soc_write(codec
, reg
, new);
1942 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
1945 * snd_soc_update_bits_locked - update codec register bits
1946 * @codec: audio codec
1947 * @reg: codec register
1948 * @mask: register mask
1951 * Writes new register value, and takes the codec mutex.
1953 * Returns 1 for change else 0.
1955 int snd_soc_update_bits_locked(struct snd_soc_codec
*codec
,
1956 unsigned short reg
, unsigned int mask
,
1961 mutex_lock(&codec
->mutex
);
1962 change
= snd_soc_update_bits(codec
, reg
, mask
, value
);
1963 mutex_unlock(&codec
->mutex
);
1967 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked
);
1970 * snd_soc_test_bits - test register for change
1971 * @codec: audio codec
1972 * @reg: codec register
1973 * @mask: register mask
1976 * Tests a register with a new value and checks if the new value is
1977 * different from the old value.
1979 * Returns 1 for change else 0.
1981 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1982 unsigned int mask
, unsigned int value
)
1985 unsigned int old
, new;
1987 old
= snd_soc_read(codec
, reg
);
1988 new = (old
& ~mask
) | value
;
1989 change
= old
!= new;
1993 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
1996 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1997 * @substream: the pcm substream
1998 * @hw: the hardware parameters
2000 * Sets the substream runtime hardware parameters.
2002 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
2003 const struct snd_pcm_hardware
*hw
)
2005 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2006 runtime
->hw
.info
= hw
->info
;
2007 runtime
->hw
.formats
= hw
->formats
;
2008 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
2009 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
2010 runtime
->hw
.periods_min
= hw
->periods_min
;
2011 runtime
->hw
.periods_max
= hw
->periods_max
;
2012 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
2013 runtime
->hw
.fifo_size
= hw
->fifo_size
;
2016 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
2019 * snd_soc_cnew - create new control
2020 * @_template: control template
2021 * @data: control private data
2022 * @long_name: control long name
2024 * Create a new mixer control from a template control.
2026 * Returns 0 for success, else error.
2028 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
2029 void *data
, char *long_name
)
2031 struct snd_kcontrol_new
template;
2033 memcpy(&template, _template
, sizeof(template));
2035 template.name
= long_name
;
2038 return snd_ctl_new1(&template, data
);
2040 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
2043 * snd_soc_add_controls - add an array of controls to a codec.
2044 * Convienience function to add a list of controls. Many codecs were
2045 * duplicating this code.
2047 * @codec: codec to add controls to
2048 * @controls: array of controls to add
2049 * @num_controls: number of elements in the array
2051 * Return 0 for success, else error.
2053 int snd_soc_add_controls(struct snd_soc_codec
*codec
,
2054 const struct snd_kcontrol_new
*controls
, int num_controls
)
2056 struct snd_card
*card
= codec
->card
->snd_card
;
2059 for (i
= 0; i
< num_controls
; i
++) {
2060 const struct snd_kcontrol_new
*control
= &controls
[i
];
2061 err
= snd_ctl_add(card
, snd_soc_cnew(control
, codec
, NULL
));
2063 dev_err(codec
->dev
, "%s: Failed to add %s: %d\n",
2064 codec
->name
, control
->name
, err
);
2071 EXPORT_SYMBOL_GPL(snd_soc_add_controls
);
2074 * snd_soc_info_enum_double - enumerated double mixer info callback
2075 * @kcontrol: mixer control
2076 * @uinfo: control element information
2078 * Callback to provide information about a double enumerated
2081 * Returns 0 for success.
2083 int snd_soc_info_enum_double(struct snd_kcontrol
*kcontrol
,
2084 struct snd_ctl_elem_info
*uinfo
)
2086 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2088 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2089 uinfo
->count
= e
->shift_l
== e
->shift_r
? 1 : 2;
2090 uinfo
->value
.enumerated
.items
= e
->max
;
2092 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2093 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2094 strcpy(uinfo
->value
.enumerated
.name
,
2095 e
->texts
[uinfo
->value
.enumerated
.item
]);
2098 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double
);
2101 * snd_soc_get_enum_double - enumerated double mixer get callback
2102 * @kcontrol: mixer control
2103 * @ucontrol: control element information
2105 * Callback to get the value of a double enumerated mixer.
2107 * Returns 0 for success.
2109 int snd_soc_get_enum_double(struct snd_kcontrol
*kcontrol
,
2110 struct snd_ctl_elem_value
*ucontrol
)
2112 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2113 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2114 unsigned int val
, bitmask
;
2116 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2118 val
= snd_soc_read(codec
, e
->reg
);
2119 ucontrol
->value
.enumerated
.item
[0]
2120 = (val
>> e
->shift_l
) & (bitmask
- 1);
2121 if (e
->shift_l
!= e
->shift_r
)
2122 ucontrol
->value
.enumerated
.item
[1] =
2123 (val
>> e
->shift_r
) & (bitmask
- 1);
2127 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double
);
2130 * snd_soc_put_enum_double - enumerated double mixer put callback
2131 * @kcontrol: mixer control
2132 * @ucontrol: control element information
2134 * Callback to set the value of a double enumerated mixer.
2136 * Returns 0 for success.
2138 int snd_soc_put_enum_double(struct snd_kcontrol
*kcontrol
,
2139 struct snd_ctl_elem_value
*ucontrol
)
2141 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2142 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2144 unsigned int mask
, bitmask
;
2146 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2148 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2150 val
= ucontrol
->value
.enumerated
.item
[0] << e
->shift_l
;
2151 mask
= (bitmask
- 1) << e
->shift_l
;
2152 if (e
->shift_l
!= e
->shift_r
) {
2153 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2155 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2156 mask
|= (bitmask
- 1) << e
->shift_r
;
2159 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2161 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double
);
2164 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2165 * @kcontrol: mixer control
2166 * @ucontrol: control element information
2168 * Callback to get the value of a double semi enumerated mixer.
2170 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2171 * used for handling bitfield coded enumeration for example.
2173 * Returns 0 for success.
2175 int snd_soc_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2176 struct snd_ctl_elem_value
*ucontrol
)
2178 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2179 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2180 unsigned int reg_val
, val
, mux
;
2182 reg_val
= snd_soc_read(codec
, e
->reg
);
2183 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2184 for (mux
= 0; mux
< e
->max
; mux
++) {
2185 if (val
== e
->values
[mux
])
2188 ucontrol
->value
.enumerated
.item
[0] = mux
;
2189 if (e
->shift_l
!= e
->shift_r
) {
2190 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2191 for (mux
= 0; mux
< e
->max
; mux
++) {
2192 if (val
== e
->values
[mux
])
2195 ucontrol
->value
.enumerated
.item
[1] = mux
;
2200 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double
);
2203 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2204 * @kcontrol: mixer control
2205 * @ucontrol: control element information
2207 * Callback to set the value of a double semi enumerated mixer.
2209 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2210 * used for handling bitfield coded enumeration for example.
2212 * Returns 0 for success.
2214 int snd_soc_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2215 struct snd_ctl_elem_value
*ucontrol
)
2217 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2218 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2222 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2224 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2225 mask
= e
->mask
<< e
->shift_l
;
2226 if (e
->shift_l
!= e
->shift_r
) {
2227 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2229 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2230 mask
|= e
->mask
<< e
->shift_r
;
2233 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2235 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double
);
2238 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2239 * @kcontrol: mixer control
2240 * @uinfo: control element information
2242 * Callback to provide information about an external enumerated
2245 * Returns 0 for success.
2247 int snd_soc_info_enum_ext(struct snd_kcontrol
*kcontrol
,
2248 struct snd_ctl_elem_info
*uinfo
)
2250 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2252 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2254 uinfo
->value
.enumerated
.items
= e
->max
;
2256 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2257 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2258 strcpy(uinfo
->value
.enumerated
.name
,
2259 e
->texts
[uinfo
->value
.enumerated
.item
]);
2262 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext
);
2265 * snd_soc_info_volsw_ext - external single mixer info callback
2266 * @kcontrol: mixer control
2267 * @uinfo: control element information
2269 * Callback to provide information about a single external mixer control.
2271 * Returns 0 for success.
2273 int snd_soc_info_volsw_ext(struct snd_kcontrol
*kcontrol
,
2274 struct snd_ctl_elem_info
*uinfo
)
2276 int max
= kcontrol
->private_value
;
2278 if (max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2279 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2281 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2284 uinfo
->value
.integer
.min
= 0;
2285 uinfo
->value
.integer
.max
= max
;
2288 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext
);
2291 * snd_soc_info_volsw - single mixer info callback
2292 * @kcontrol: mixer control
2293 * @uinfo: control element information
2295 * Callback to provide information about a single mixer control.
2297 * Returns 0 for success.
2299 int snd_soc_info_volsw(struct snd_kcontrol
*kcontrol
,
2300 struct snd_ctl_elem_info
*uinfo
)
2302 struct soc_mixer_control
*mc
=
2303 (struct soc_mixer_control
*)kcontrol
->private_value
;
2305 unsigned int shift
= mc
->shift
;
2306 unsigned int rshift
= mc
->rshift
;
2308 if (!mc
->platform_max
)
2309 mc
->platform_max
= mc
->max
;
2310 platform_max
= mc
->platform_max
;
2312 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2313 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2315 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2317 uinfo
->count
= shift
== rshift
? 1 : 2;
2318 uinfo
->value
.integer
.min
= 0;
2319 uinfo
->value
.integer
.max
= platform_max
;
2322 EXPORT_SYMBOL_GPL(snd_soc_info_volsw
);
2325 * snd_soc_get_volsw - single mixer get callback
2326 * @kcontrol: mixer control
2327 * @ucontrol: control element information
2329 * Callback to get the value of a single mixer control.
2331 * Returns 0 for success.
2333 int snd_soc_get_volsw(struct snd_kcontrol
*kcontrol
,
2334 struct snd_ctl_elem_value
*ucontrol
)
2336 struct soc_mixer_control
*mc
=
2337 (struct soc_mixer_control
*)kcontrol
->private_value
;
2338 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2339 unsigned int reg
= mc
->reg
;
2340 unsigned int shift
= mc
->shift
;
2341 unsigned int rshift
= mc
->rshift
;
2343 unsigned int mask
= (1 << fls(max
)) - 1;
2344 unsigned int invert
= mc
->invert
;
2346 ucontrol
->value
.integer
.value
[0] =
2347 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2348 if (shift
!= rshift
)
2349 ucontrol
->value
.integer
.value
[1] =
2350 (snd_soc_read(codec
, reg
) >> rshift
) & mask
;
2352 ucontrol
->value
.integer
.value
[0] =
2353 max
- ucontrol
->value
.integer
.value
[0];
2354 if (shift
!= rshift
)
2355 ucontrol
->value
.integer
.value
[1] =
2356 max
- ucontrol
->value
.integer
.value
[1];
2361 EXPORT_SYMBOL_GPL(snd_soc_get_volsw
);
2364 * snd_soc_put_volsw - single mixer put callback
2365 * @kcontrol: mixer control
2366 * @ucontrol: control element information
2368 * Callback to set the value of a single mixer control.
2370 * Returns 0 for success.
2372 int snd_soc_put_volsw(struct snd_kcontrol
*kcontrol
,
2373 struct snd_ctl_elem_value
*ucontrol
)
2375 struct soc_mixer_control
*mc
=
2376 (struct soc_mixer_control
*)kcontrol
->private_value
;
2377 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2378 unsigned int reg
= mc
->reg
;
2379 unsigned int shift
= mc
->shift
;
2380 unsigned int rshift
= mc
->rshift
;
2382 unsigned int mask
= (1 << fls(max
)) - 1;
2383 unsigned int invert
= mc
->invert
;
2384 unsigned int val
, val2
, val_mask
;
2386 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2389 val_mask
= mask
<< shift
;
2391 if (shift
!= rshift
) {
2392 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2395 val_mask
|= mask
<< rshift
;
2396 val
|= val2
<< rshift
;
2398 return snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2400 EXPORT_SYMBOL_GPL(snd_soc_put_volsw
);
2403 * snd_soc_info_volsw_2r - double mixer info callback
2404 * @kcontrol: mixer control
2405 * @uinfo: control element information
2407 * Callback to provide information about a double mixer control that
2408 * spans 2 codec registers.
2410 * Returns 0 for success.
2412 int snd_soc_info_volsw_2r(struct snd_kcontrol
*kcontrol
,
2413 struct snd_ctl_elem_info
*uinfo
)
2415 struct soc_mixer_control
*mc
=
2416 (struct soc_mixer_control
*)kcontrol
->private_value
;
2419 if (!mc
->platform_max
)
2420 mc
->platform_max
= mc
->max
;
2421 platform_max
= mc
->platform_max
;
2423 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2424 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2426 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2429 uinfo
->value
.integer
.min
= 0;
2430 uinfo
->value
.integer
.max
= platform_max
;
2433 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r
);
2436 * snd_soc_get_volsw_2r - double mixer get callback
2437 * @kcontrol: mixer control
2438 * @ucontrol: control element information
2440 * Callback to get the value of a double mixer control that spans 2 registers.
2442 * Returns 0 for success.
2444 int snd_soc_get_volsw_2r(struct snd_kcontrol
*kcontrol
,
2445 struct snd_ctl_elem_value
*ucontrol
)
2447 struct soc_mixer_control
*mc
=
2448 (struct soc_mixer_control
*)kcontrol
->private_value
;
2449 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2450 unsigned int reg
= mc
->reg
;
2451 unsigned int reg2
= mc
->rreg
;
2452 unsigned int shift
= mc
->shift
;
2454 unsigned int mask
= (1 << fls(max
)) - 1;
2455 unsigned int invert
= mc
->invert
;
2457 ucontrol
->value
.integer
.value
[0] =
2458 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2459 ucontrol
->value
.integer
.value
[1] =
2460 (snd_soc_read(codec
, reg2
) >> shift
) & mask
;
2462 ucontrol
->value
.integer
.value
[0] =
2463 max
- ucontrol
->value
.integer
.value
[0];
2464 ucontrol
->value
.integer
.value
[1] =
2465 max
- ucontrol
->value
.integer
.value
[1];
2470 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r
);
2473 * snd_soc_put_volsw_2r - double mixer set callback
2474 * @kcontrol: mixer control
2475 * @ucontrol: control element information
2477 * Callback to set the value of a double mixer control that spans 2 registers.
2479 * Returns 0 for success.
2481 int snd_soc_put_volsw_2r(struct snd_kcontrol
*kcontrol
,
2482 struct snd_ctl_elem_value
*ucontrol
)
2484 struct soc_mixer_control
*mc
=
2485 (struct soc_mixer_control
*)kcontrol
->private_value
;
2486 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2487 unsigned int reg
= mc
->reg
;
2488 unsigned int reg2
= mc
->rreg
;
2489 unsigned int shift
= mc
->shift
;
2491 unsigned int mask
= (1 << fls(max
)) - 1;
2492 unsigned int invert
= mc
->invert
;
2494 unsigned int val
, val2
, val_mask
;
2496 val_mask
= mask
<< shift
;
2497 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2498 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2506 val2
= val2
<< shift
;
2508 err
= snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2512 err
= snd_soc_update_bits_locked(codec
, reg2
, val_mask
, val2
);
2515 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r
);
2518 * snd_soc_info_volsw_s8 - signed mixer info callback
2519 * @kcontrol: mixer control
2520 * @uinfo: control element information
2522 * Callback to provide information about a signed mixer control.
2524 * Returns 0 for success.
2526 int snd_soc_info_volsw_s8(struct snd_kcontrol
*kcontrol
,
2527 struct snd_ctl_elem_info
*uinfo
)
2529 struct soc_mixer_control
*mc
=
2530 (struct soc_mixer_control
*)kcontrol
->private_value
;
2534 if (!mc
->platform_max
)
2535 mc
->platform_max
= mc
->max
;
2536 platform_max
= mc
->platform_max
;
2538 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2540 uinfo
->value
.integer
.min
= 0;
2541 uinfo
->value
.integer
.max
= platform_max
- min
;
2544 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8
);
2547 * snd_soc_get_volsw_s8 - signed mixer get callback
2548 * @kcontrol: mixer control
2549 * @ucontrol: control element information
2551 * Callback to get the value of a signed mixer control.
2553 * Returns 0 for success.
2555 int snd_soc_get_volsw_s8(struct snd_kcontrol
*kcontrol
,
2556 struct snd_ctl_elem_value
*ucontrol
)
2558 struct soc_mixer_control
*mc
=
2559 (struct soc_mixer_control
*)kcontrol
->private_value
;
2560 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2561 unsigned int reg
= mc
->reg
;
2563 int val
= snd_soc_read(codec
, reg
);
2565 ucontrol
->value
.integer
.value
[0] =
2566 ((signed char)(val
& 0xff))-min
;
2567 ucontrol
->value
.integer
.value
[1] =
2568 ((signed char)((val
>> 8) & 0xff))-min
;
2571 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8
);
2574 * snd_soc_put_volsw_sgn - signed mixer put callback
2575 * @kcontrol: mixer control
2576 * @ucontrol: control element information
2578 * Callback to set the value of a signed mixer control.
2580 * Returns 0 for success.
2582 int snd_soc_put_volsw_s8(struct snd_kcontrol
*kcontrol
,
2583 struct snd_ctl_elem_value
*ucontrol
)
2585 struct soc_mixer_control
*mc
=
2586 (struct soc_mixer_control
*)kcontrol
->private_value
;
2587 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2588 unsigned int reg
= mc
->reg
;
2592 val
= (ucontrol
->value
.integer
.value
[0]+min
) & 0xff;
2593 val
|= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff) << 8;
2595 return snd_soc_update_bits_locked(codec
, reg
, 0xffff, val
);
2597 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8
);
2600 * snd_soc_limit_volume - Set new limit to an existing volume control.
2602 * @codec: where to look for the control
2603 * @name: Name of the control
2604 * @max: new maximum limit
2606 * Return 0 for success, else error.
2608 int snd_soc_limit_volume(struct snd_soc_codec
*codec
,
2609 const char *name
, int max
)
2611 struct snd_card
*card
= codec
->card
->snd_card
;
2612 struct snd_kcontrol
*kctl
;
2613 struct soc_mixer_control
*mc
;
2617 /* Sanity check for name and max */
2618 if (unlikely(!name
|| max
<= 0))
2621 list_for_each_entry(kctl
, &card
->controls
, list
) {
2622 if (!strncmp(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
))) {
2628 mc
= (struct soc_mixer_control
*)kctl
->private_value
;
2629 if (max
<= mc
->max
) {
2630 mc
->platform_max
= max
;
2636 EXPORT_SYMBOL_GPL(snd_soc_limit_volume
);
2639 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2640 * mixer info callback
2641 * @kcontrol: mixer control
2642 * @uinfo: control element information
2644 * Returns 0 for success.
2646 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2647 struct snd_ctl_elem_info
*uinfo
)
2649 struct soc_mixer_control
*mc
=
2650 (struct soc_mixer_control
*)kcontrol
->private_value
;
2654 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2656 uinfo
->value
.integer
.min
= 0;
2657 uinfo
->value
.integer
.max
= max
-min
;
2661 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx
);
2664 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2665 * mixer get callback
2666 * @kcontrol: mixer control
2667 * @uinfo: control element information
2669 * Returns 0 for success.
2671 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2672 struct snd_ctl_elem_value
*ucontrol
)
2674 struct soc_mixer_control
*mc
=
2675 (struct soc_mixer_control
*)kcontrol
->private_value
;
2676 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2677 unsigned int mask
= (1<<mc
->shift
)-1;
2679 int val
= snd_soc_read(codec
, mc
->reg
) & mask
;
2680 int valr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2682 ucontrol
->value
.integer
.value
[0] = ((val
& 0xff)-min
) & mask
;
2683 ucontrol
->value
.integer
.value
[1] = ((valr
& 0xff)-min
) & mask
;
2686 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx
);
2689 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2690 * mixer put callback
2691 * @kcontrol: mixer control
2692 * @uinfo: control element information
2694 * Returns 0 for success.
2696 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2697 struct snd_ctl_elem_value
*ucontrol
)
2699 struct soc_mixer_control
*mc
=
2700 (struct soc_mixer_control
*)kcontrol
->private_value
;
2701 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2702 unsigned int mask
= (1<<mc
->shift
)-1;
2705 unsigned int val
, valr
, oval
, ovalr
;
2707 val
= ((ucontrol
->value
.integer
.value
[0]+min
) & 0xff);
2709 valr
= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff);
2712 oval
= snd_soc_read(codec
, mc
->reg
) & mask
;
2713 ovalr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2717 ret
= snd_soc_write(codec
, mc
->reg
, val
);
2721 if (ovalr
!= valr
) {
2722 ret
= snd_soc_write(codec
, mc
->rreg
, valr
);
2729 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx
);
2732 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2734 * @clk_id: DAI specific clock ID
2735 * @freq: new clock frequency in Hz
2736 * @dir: new clock direction - input/output.
2738 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2740 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
2741 unsigned int freq
, int dir
)
2743 if (dai
->driver
&& dai
->driver
->ops
->set_sysclk
)
2744 return dai
->driver
->ops
->set_sysclk(dai
, clk_id
, freq
, dir
);
2748 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
2751 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2753 * @div_id: DAI specific clock divider ID
2754 * @div: new clock divisor.
2756 * Configures the clock dividers. This is used to derive the best DAI bit and
2757 * frame clocks from the system or master clock. It's best to set the DAI bit
2758 * and frame clocks as low as possible to save system power.
2760 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
2761 int div_id
, int div
)
2763 if (dai
->driver
&& dai
->driver
->ops
->set_clkdiv
)
2764 return dai
->driver
->ops
->set_clkdiv(dai
, div_id
, div
);
2768 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
2771 * snd_soc_dai_set_pll - configure DAI PLL.
2773 * @pll_id: DAI specific PLL ID
2774 * @source: DAI specific source for the PLL
2775 * @freq_in: PLL input clock frequency in Hz
2776 * @freq_out: requested PLL output clock frequency in Hz
2778 * Configures and enables PLL to generate output clock based on input clock.
2780 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
2781 unsigned int freq_in
, unsigned int freq_out
)
2783 if (dai
->driver
&& dai
->driver
->ops
->set_pll
)
2784 return dai
->driver
->ops
->set_pll(dai
, pll_id
, source
,
2789 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
2792 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2794 * @fmt: SND_SOC_DAIFMT_ format value.
2796 * Configures the DAI hardware format and clocking.
2798 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
2800 if (dai
->driver
&& dai
->driver
->ops
->set_fmt
)
2801 return dai
->driver
->ops
->set_fmt(dai
, fmt
);
2805 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
2808 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2810 * @tx_mask: bitmask representing active TX slots.
2811 * @rx_mask: bitmask representing active RX slots.
2812 * @slots: Number of slots in use.
2813 * @slot_width: Width in bits for each slot.
2815 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2818 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
2819 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
2821 if (dai
->driver
&& dai
->driver
->ops
->set_tdm_slot
)
2822 return dai
->driver
->ops
->set_tdm_slot(dai
, tx_mask
, rx_mask
,
2827 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
2830 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2832 * @tx_num: how many TX channels
2833 * @tx_slot: pointer to an array which imply the TX slot number channel
2835 * @rx_num: how many RX channels
2836 * @rx_slot: pointer to an array which imply the RX slot number channel
2839 * configure the relationship between channel number and TDM slot number.
2841 int snd_soc_dai_set_channel_map(struct snd_soc_dai
*dai
,
2842 unsigned int tx_num
, unsigned int *tx_slot
,
2843 unsigned int rx_num
, unsigned int *rx_slot
)
2845 if (dai
->driver
&& dai
->driver
->ops
->set_channel_map
)
2846 return dai
->driver
->ops
->set_channel_map(dai
, tx_num
, tx_slot
,
2851 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map
);
2854 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2856 * @tristate: tristate enable
2858 * Tristates the DAI so that others can use it.
2860 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
2862 if (dai
->driver
&& dai
->driver
->ops
->set_tristate
)
2863 return dai
->driver
->ops
->set_tristate(dai
, tristate
);
2867 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
2870 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2872 * @mute: mute enable
2874 * Mutes the DAI DAC.
2876 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
)
2878 if (dai
->driver
&& dai
->driver
->ops
->digital_mute
)
2879 return dai
->driver
->ops
->digital_mute(dai
, mute
);
2883 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
2886 * snd_soc_register_card - Register a card with the ASoC core
2888 * @card: Card to register
2890 * Note that currently this is an internal only function: it will be
2891 * exposed to machine drivers after further backporting of ASoC v2
2892 * registration APIs.
2894 static int snd_soc_register_card(struct snd_soc_card
*card
)
2898 if (!card
->name
|| !card
->dev
)
2901 card
->rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
) * card
->num_links
,
2903 if (card
->rtd
== NULL
)
2906 for (i
= 0; i
< card
->num_links
; i
++)
2907 card
->rtd
[i
].dai_link
= &card
->dai_link
[i
];
2909 INIT_LIST_HEAD(&card
->list
);
2910 card
->instantiated
= 0;
2911 mutex_init(&card
->mutex
);
2913 mutex_lock(&client_mutex
);
2914 list_add(&card
->list
, &card_list
);
2915 snd_soc_instantiate_cards();
2916 mutex_unlock(&client_mutex
);
2918 dev_dbg(card
->dev
, "Registered card '%s'\n", card
->name
);
2924 * snd_soc_unregister_card - Unregister a card with the ASoC core
2926 * @card: Card to unregister
2928 * Note that currently this is an internal only function: it will be
2929 * exposed to machine drivers after further backporting of ASoC v2
2930 * registration APIs.
2932 static int snd_soc_unregister_card(struct snd_soc_card
*card
)
2934 mutex_lock(&client_mutex
);
2935 list_del(&card
->list
);
2936 mutex_unlock(&client_mutex
);
2937 dev_dbg(card
->dev
, "Unregistered card '%s'\n", card
->name
);
2943 * Simplify DAI link configuration by removing ".-1" from device names
2944 * and sanitizing names.
2946 static inline char *fmt_single_name(struct device
*dev
, int *id
)
2948 char *found
, name
[NAME_SIZE
];
2951 if (dev_name(dev
) == NULL
)
2954 strncpy(name
, dev_name(dev
), NAME_SIZE
);
2956 /* are we a "%s.%d" name (platform and SPI components) */
2957 found
= strstr(name
, dev
->driver
->name
);
2960 if (sscanf(&found
[strlen(dev
->driver
->name
)], ".%d", id
) == 1) {
2962 /* discard ID from name if ID == -1 */
2964 found
[strlen(dev
->driver
->name
)] = '\0';
2968 /* I2C component devices are named "bus-addr" */
2969 if (sscanf(name
, "%x-%x", &id1
, &id2
) == 2) {
2970 char tmp
[NAME_SIZE
];
2972 /* create unique ID number from I2C addr and bus */
2973 *id
= ((id1
& 0xffff) << 16) + id2
;
2975 /* sanitize component name for DAI link creation */
2976 snprintf(tmp
, NAME_SIZE
, "%s.%s", dev
->driver
->name
, name
);
2977 strncpy(name
, tmp
, NAME_SIZE
);
2982 return kstrdup(name
, GFP_KERNEL
);
2986 * Simplify DAI link naming for single devices with multiple DAIs by removing
2987 * any ".-1" and using the DAI name (instead of device name).
2989 static inline char *fmt_multiple_name(struct device
*dev
,
2990 struct snd_soc_dai_driver
*dai_drv
)
2992 if (dai_drv
->name
== NULL
) {
2993 printk(KERN_ERR
"asoc: error - multiple DAI %s registered with no name\n",
2998 return kstrdup(dai_drv
->name
, GFP_KERNEL
);
3002 * snd_soc_register_dai - Register a DAI with the ASoC core
3004 * @dai: DAI to register
3006 int snd_soc_register_dai(struct device
*dev
,
3007 struct snd_soc_dai_driver
*dai_drv
)
3009 struct snd_soc_dai
*dai
;
3011 dev_dbg(dev
, "dai register %s\n", dev_name(dev
));
3013 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
3017 /* create DAI component name */
3018 dai
->name
= fmt_single_name(dev
, &dai
->id
);
3019 if (dai
->name
== NULL
) {
3025 dai
->driver
= dai_drv
;
3026 if (!dai
->driver
->ops
)
3027 dai
->driver
->ops
= &null_dai_ops
;
3029 mutex_lock(&client_mutex
);
3030 list_add(&dai
->list
, &dai_list
);
3031 snd_soc_instantiate_cards();
3032 mutex_unlock(&client_mutex
);
3034 pr_debug("Registered DAI '%s'\n", dai
->name
);
3038 EXPORT_SYMBOL_GPL(snd_soc_register_dai
);
3041 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3043 * @dai: DAI to unregister
3045 void snd_soc_unregister_dai(struct device
*dev
)
3047 struct snd_soc_dai
*dai
;
3049 list_for_each_entry(dai
, &dai_list
, list
) {
3050 if (dev
== dai
->dev
)
3056 mutex_lock(&client_mutex
);
3057 list_del(&dai
->list
);
3058 mutex_unlock(&client_mutex
);
3060 pr_debug("Unregistered DAI '%s'\n", dai
->name
);
3064 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai
);
3067 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3069 * @dai: Array of DAIs to register
3070 * @count: Number of DAIs
3072 int snd_soc_register_dais(struct device
*dev
,
3073 struct snd_soc_dai_driver
*dai_drv
, size_t count
)
3075 struct snd_soc_dai
*dai
;
3078 dev_dbg(dev
, "dai register %s #%Zu\n", dev_name(dev
), count
);
3080 for (i
= 0; i
< count
; i
++) {
3082 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
3088 /* create DAI component name */
3089 dai
->name
= fmt_multiple_name(dev
, &dai_drv
[i
]);
3090 if (dai
->name
== NULL
) {
3097 dai
->driver
= &dai_drv
[i
];
3098 if (dai
->driver
->id
)
3099 dai
->id
= dai
->driver
->id
;
3102 if (!dai
->driver
->ops
)
3103 dai
->driver
->ops
= &null_dai_ops
;
3105 mutex_lock(&client_mutex
);
3106 list_add(&dai
->list
, &dai_list
);
3107 mutex_unlock(&client_mutex
);
3109 pr_debug("Registered DAI '%s'\n", dai
->name
);
3112 snd_soc_instantiate_cards();
3116 for (i
--; i
>= 0; i
--)
3117 snd_soc_unregister_dai(dev
);
3121 EXPORT_SYMBOL_GPL(snd_soc_register_dais
);
3124 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3126 * @dai: Array of DAIs to unregister
3127 * @count: Number of DAIs
3129 void snd_soc_unregister_dais(struct device
*dev
, size_t count
)
3133 for (i
= 0; i
< count
; i
++)
3134 snd_soc_unregister_dai(dev
);
3136 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais
);
3139 * snd_soc_register_platform - Register a platform with the ASoC core
3141 * @platform: platform to register
3143 int snd_soc_register_platform(struct device
*dev
,
3144 struct snd_soc_platform_driver
*platform_drv
)
3146 struct snd_soc_platform
*platform
;
3148 dev_dbg(dev
, "platform register %s\n", dev_name(dev
));
3150 platform
= kzalloc(sizeof(struct snd_soc_platform
), GFP_KERNEL
);
3151 if (platform
== NULL
)
3154 /* create platform component name */
3155 platform
->name
= fmt_single_name(dev
, &platform
->id
);
3156 if (platform
->name
== NULL
) {
3161 platform
->dev
= dev
;
3162 platform
->driver
= platform_drv
;
3164 mutex_lock(&client_mutex
);
3165 list_add(&platform
->list
, &platform_list
);
3166 snd_soc_instantiate_cards();
3167 mutex_unlock(&client_mutex
);
3169 pr_debug("Registered platform '%s'\n", platform
->name
);
3173 EXPORT_SYMBOL_GPL(snd_soc_register_platform
);
3176 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3178 * @platform: platform to unregister
3180 void snd_soc_unregister_platform(struct device
*dev
)
3182 struct snd_soc_platform
*platform
;
3184 list_for_each_entry(platform
, &platform_list
, list
) {
3185 if (dev
== platform
->dev
)
3191 mutex_lock(&client_mutex
);
3192 list_del(&platform
->list
);
3193 mutex_unlock(&client_mutex
);
3195 pr_debug("Unregistered platform '%s'\n", platform
->name
);
3196 kfree(platform
->name
);
3199 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform
);
3201 static u64 codec_format_map
[] = {
3202 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
,
3203 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_U16_BE
,
3204 SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S24_BE
,
3205 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_U24_BE
,
3206 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
,
3207 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_U32_BE
,
3208 SNDRV_PCM_FMTBIT_S24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3209 SNDRV_PCM_FMTBIT_U24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3210 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S20_3BE
,
3211 SNDRV_PCM_FMTBIT_U20_3LE
| SNDRV_PCM_FMTBIT_U20_3BE
,
3212 SNDRV_PCM_FMTBIT_S18_3LE
| SNDRV_PCM_FMTBIT_S18_3BE
,
3213 SNDRV_PCM_FMTBIT_U18_3LE
| SNDRV_PCM_FMTBIT_U18_3BE
,
3214 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
,
3215 SNDRV_PCM_FMTBIT_FLOAT64_LE
| SNDRV_PCM_FMTBIT_FLOAT64_BE
,
3216 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3217 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
,
3220 /* Fix up the DAI formats for endianness: codecs don't actually see
3221 * the endianness of the data but we're using the CPU format
3222 * definitions which do need to include endianness so we ensure that
3223 * codec DAIs always have both big and little endian variants set.
3225 static void fixup_codec_formats(struct snd_soc_pcm_stream
*stream
)
3229 for (i
= 0; i
< ARRAY_SIZE(codec_format_map
); i
++)
3230 if (stream
->formats
& codec_format_map
[i
])
3231 stream
->formats
|= codec_format_map
[i
];
3235 * snd_soc_register_codec - Register a codec with the ASoC core
3237 * @codec: codec to register
3239 int snd_soc_register_codec(struct device
*dev
,
3240 struct snd_soc_codec_driver
*codec_drv
,
3241 struct snd_soc_dai_driver
*dai_drv
, int num_dai
)
3243 struct snd_soc_codec
*codec
;
3246 dev_dbg(dev
, "codec register %s\n", dev_name(dev
));
3248 codec
= kzalloc(sizeof(struct snd_soc_codec
), GFP_KERNEL
);
3252 /* create CODEC component name */
3253 codec
->name
= fmt_single_name(dev
, &codec
->id
);
3254 if (codec
->name
== NULL
) {
3259 INIT_LIST_HEAD(&codec
->dapm
.widgets
);
3260 INIT_LIST_HEAD(&codec
->dapm
.paths
);
3261 codec
->dapm
.bias_level
= SND_SOC_BIAS_OFF
;
3262 codec
->dapm
.dev
= dev
;
3263 codec
->dapm
.codec
= codec
;
3265 /* allocate CODEC register cache */
3266 if (codec_drv
->reg_cache_size
&& codec_drv
->reg_word_size
) {
3268 if (codec_drv
->reg_cache_default
)
3269 codec
->reg_cache
= kmemdup(codec_drv
->reg_cache_default
,
3270 codec_drv
->reg_cache_size
* codec_drv
->reg_word_size
, GFP_KERNEL
);
3272 codec
->reg_cache
= kzalloc(codec_drv
->reg_cache_size
*
3273 codec_drv
->reg_word_size
, GFP_KERNEL
);
3275 if (codec
->reg_cache
== NULL
) {
3283 codec
->driver
= codec_drv
;
3284 codec
->num_dai
= num_dai
;
3285 mutex_init(&codec
->mutex
);
3287 for (i
= 0; i
< num_dai
; i
++) {
3288 fixup_codec_formats(&dai_drv
[i
].playback
);
3289 fixup_codec_formats(&dai_drv
[i
].capture
);
3292 /* register any DAIs */
3294 ret
= snd_soc_register_dais(dev
, dai_drv
, num_dai
);
3299 mutex_lock(&client_mutex
);
3300 list_add(&codec
->list
, &codec_list
);
3301 snd_soc_instantiate_cards();
3302 mutex_unlock(&client_mutex
);
3304 pr_debug("Registered codec '%s'\n", codec
->name
);
3308 if (codec
->reg_cache
)
3309 kfree(codec
->reg_cache
);
3314 EXPORT_SYMBOL_GPL(snd_soc_register_codec
);
3317 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3319 * @codec: codec to unregister
3321 void snd_soc_unregister_codec(struct device
*dev
)
3323 struct snd_soc_codec
*codec
;
3326 list_for_each_entry(codec
, &codec_list
, list
) {
3327 if (dev
== codec
->dev
)
3334 for (i
= 0; i
< codec
->num_dai
; i
++)
3335 snd_soc_unregister_dai(dev
);
3337 mutex_lock(&client_mutex
);
3338 list_del(&codec
->list
);
3339 mutex_unlock(&client_mutex
);
3341 pr_debug("Unregistered codec '%s'\n", codec
->name
);
3343 if (codec
->reg_cache
)
3344 kfree(codec
->reg_cache
);
3348 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec
);
3350 static int __init
snd_soc_init(void)
3352 #ifdef CONFIG_DEBUG_FS
3353 debugfs_root
= debugfs_create_dir("asoc", NULL
);
3354 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
3356 "ASoC: Failed to create debugfs directory\n");
3357 debugfs_root
= NULL
;
3360 if (!debugfs_create_file("codecs", 0444, debugfs_root
, NULL
,
3362 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3364 if (!debugfs_create_file("dais", 0444, debugfs_root
, NULL
,
3366 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3368 if (!debugfs_create_file("platforms", 0444, debugfs_root
, NULL
,
3369 &platform_list_fops
))
3370 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3373 return platform_driver_register(&soc_driver
);
3375 module_init(snd_soc_init
);
3377 static void __exit
snd_soc_exit(void)
3379 #ifdef CONFIG_DEBUG_FS
3380 debugfs_remove_recursive(debugfs_root
);
3382 platform_driver_unregister(&soc_driver
);
3384 module_exit(snd_soc_exit
);
3386 /* Module information */
3387 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3388 MODULE_DESCRIPTION("ALSA SoC Core");
3389 MODULE_LICENSE("GPL");
3390 MODULE_ALIAS("platform:soc-audio");