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/initval.h>
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/asoc.h>
46 static DEFINE_MUTEX(pcm_mutex
);
47 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq
);
49 #ifdef CONFIG_DEBUG_FS
50 static struct dentry
*debugfs_root
;
53 static DEFINE_MUTEX(client_mutex
);
54 static LIST_HEAD(card_list
);
55 static LIST_HEAD(dai_list
);
56 static LIST_HEAD(platform_list
);
57 static LIST_HEAD(codec_list
);
59 static int snd_soc_register_card(struct snd_soc_card
*card
);
60 static int snd_soc_unregister_card(struct snd_soc_card
*card
);
61 static int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
);
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
68 static int pmdown_time
= 5000;
69 module_param(pmdown_time
, int, 0);
70 MODULE_PARM_DESC(pmdown_time
, "DAPM stream powerdown time (msecs)");
73 * This function forces any delayed work to be queued and run.
75 static int run_delayed_work(struct delayed_work
*dwork
)
79 /* cancel any work waiting to be queued. */
80 ret
= cancel_delayed_work(dwork
);
82 /* if there was any work waiting then we run it now and
83 * wait for it's completion */
85 schedule_delayed_work(dwork
, 0);
86 flush_scheduled_work();
91 /* codec register dump */
92 static ssize_t
soc_codec_reg_show(struct snd_soc_codec
*codec
, char *buf
)
94 int ret
, i
, step
= 1, count
= 0;
96 if (!codec
->driver
->reg_cache_size
)
99 if (codec
->driver
->reg_cache_step
)
100 step
= codec
->driver
->reg_cache_step
;
102 count
+= sprintf(buf
, "%s registers\n", codec
->name
);
103 for (i
= 0; i
< codec
->driver
->reg_cache_size
; i
+= step
) {
104 if (codec
->driver
->readable_register
&& !codec
->driver
->readable_register(i
))
107 count
+= sprintf(buf
+ count
, "%2x: ", i
);
108 if (count
>= PAGE_SIZE
- 1)
111 if (codec
->driver
->display_register
) {
112 count
+= codec
->driver
->display_register(codec
, buf
+ count
,
113 PAGE_SIZE
- count
, i
);
115 /* If the read fails it's almost certainly due to
116 * the register being volatile and the device being
119 ret
= codec
->driver
->read(codec
, i
);
121 count
+= snprintf(buf
+ count
,
125 count
+= snprintf(buf
+ count
,
127 "<no data: %d>", ret
);
130 if (count
>= PAGE_SIZE
- 1)
133 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
134 if (count
>= PAGE_SIZE
- 1)
138 /* Truncate count; min() would cause a warning */
139 if (count
>= PAGE_SIZE
)
140 count
= PAGE_SIZE
- 1;
144 static ssize_t
codec_reg_show(struct device
*dev
,
145 struct device_attribute
*attr
, char *buf
)
147 struct snd_soc_pcm_runtime
*rtd
=
148 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
150 return soc_codec_reg_show(rtd
->codec
, buf
);
153 static DEVICE_ATTR(codec_reg
, 0444, codec_reg_show
, NULL
);
155 static ssize_t
pmdown_time_show(struct device
*dev
,
156 struct device_attribute
*attr
, char *buf
)
158 struct snd_soc_pcm_runtime
*rtd
=
159 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
161 return sprintf(buf
, "%ld\n", rtd
->pmdown_time
);
164 static ssize_t
pmdown_time_set(struct device
*dev
,
165 struct device_attribute
*attr
,
166 const char *buf
, size_t count
)
168 struct snd_soc_pcm_runtime
*rtd
=
169 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
172 ret
= strict_strtol(buf
, 10, &rtd
->pmdown_time
);
179 static DEVICE_ATTR(pmdown_time
, 0644, pmdown_time_show
, pmdown_time_set
);
181 #ifdef CONFIG_DEBUG_FS
182 static int codec_reg_open_file(struct inode
*inode
, struct file
*file
)
184 file
->private_data
= inode
->i_private
;
188 static ssize_t
codec_reg_read_file(struct file
*file
, char __user
*user_buf
,
189 size_t count
, loff_t
*ppos
)
192 struct snd_soc_codec
*codec
= file
->private_data
;
193 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
196 ret
= soc_codec_reg_show(codec
, buf
);
198 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
203 static ssize_t
codec_reg_write_file(struct file
*file
,
204 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
209 unsigned long reg
, value
;
211 struct snd_soc_codec
*codec
= file
->private_data
;
213 buf_size
= min(count
, (sizeof(buf
)-1));
214 if (copy_from_user(buf
, user_buf
, buf_size
))
218 if (codec
->driver
->reg_cache_step
)
219 step
= codec
->driver
->reg_cache_step
;
221 while (*start
== ' ')
223 reg
= simple_strtoul(start
, &start
, 16);
224 if ((reg
>= codec
->driver
->reg_cache_size
) || (reg
% step
))
226 while (*start
== ' ')
228 if (strict_strtoul(start
, 16, &value
))
230 codec
->driver
->write(codec
, reg
, value
);
234 static const struct file_operations codec_reg_fops
= {
235 .open
= codec_reg_open_file
,
236 .read
= codec_reg_read_file
,
237 .write
= codec_reg_write_file
,
238 .llseek
= default_llseek
,
241 static void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
243 struct dentry
*debugfs_card_root
= codec
->card
->debugfs_card_root
;
245 codec
->debugfs_codec_root
= debugfs_create_dir(codec
->name
,
247 if (!codec
->debugfs_codec_root
) {
249 "ASoC: Failed to create codec debugfs directory\n");
253 codec
->debugfs_reg
= debugfs_create_file("codec_reg", 0644,
254 codec
->debugfs_codec_root
,
255 codec
, &codec_reg_fops
);
256 if (!codec
->debugfs_reg
)
258 "ASoC: Failed to create codec register debugfs file\n");
260 codec
->dapm
.debugfs_dapm
= debugfs_create_dir("dapm",
261 codec
->debugfs_codec_root
);
262 if (!codec
->dapm
.debugfs_dapm
)
264 "Failed to create DAPM debugfs directory\n");
266 snd_soc_dapm_debugfs_init(&codec
->dapm
);
269 static void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
271 debugfs_remove_recursive(codec
->debugfs_codec_root
);
274 static ssize_t
codec_list_read_file(struct file
*file
, char __user
*user_buf
,
275 size_t count
, loff_t
*ppos
)
277 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
278 ssize_t len
, ret
= 0;
279 struct snd_soc_codec
*codec
;
284 list_for_each_entry(codec
, &codec_list
, list
) {
285 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
289 if (ret
> PAGE_SIZE
) {
296 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
303 static const struct file_operations codec_list_fops
= {
304 .read
= codec_list_read_file
,
305 .llseek
= default_llseek
,/* read accesses f_pos */
308 static ssize_t
dai_list_read_file(struct file
*file
, char __user
*user_buf
,
309 size_t count
, loff_t
*ppos
)
311 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
312 ssize_t len
, ret
= 0;
313 struct snd_soc_dai
*dai
;
318 list_for_each_entry(dai
, &dai_list
, list
) {
319 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n", dai
->name
);
322 if (ret
> PAGE_SIZE
) {
328 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
335 static const struct file_operations dai_list_fops
= {
336 .read
= dai_list_read_file
,
337 .llseek
= default_llseek
,/* read accesses f_pos */
340 static ssize_t
platform_list_read_file(struct file
*file
,
341 char __user
*user_buf
,
342 size_t count
, loff_t
*ppos
)
344 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
345 ssize_t len
, ret
= 0;
346 struct snd_soc_platform
*platform
;
351 list_for_each_entry(platform
, &platform_list
, list
) {
352 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
356 if (ret
> PAGE_SIZE
) {
362 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
369 static const struct file_operations platform_list_fops
= {
370 .read
= platform_list_read_file
,
371 .llseek
= default_llseek
,/* read accesses f_pos */
374 static void soc_init_card_debugfs(struct snd_soc_card
*card
)
376 card
->debugfs_card_root
= debugfs_create_dir(card
->name
,
378 if (!card
->debugfs_card_root
) {
380 "ASoC: Failed to create codec debugfs directory\n");
384 card
->debugfs_pop_time
= debugfs_create_u32("dapm_pop_time", 0644,
385 card
->debugfs_card_root
,
387 if (!card
->debugfs_pop_time
)
389 "Failed to create pop time debugfs file\n");
392 static void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
394 debugfs_remove_recursive(card
->debugfs_card_root
);
399 static inline void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
403 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
407 static inline void soc_init_card_debugfs(struct snd_soc_card
*card
)
411 static inline void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
416 #ifdef CONFIG_SND_SOC_AC97_BUS
417 /* unregister ac97 codec */
418 static int soc_ac97_dev_unregister(struct snd_soc_codec
*codec
)
420 if (codec
->ac97
->dev
.bus
)
421 device_unregister(&codec
->ac97
->dev
);
425 /* stop no dev release warning */
426 static void soc_ac97_device_release(struct device
*dev
){}
428 /* register ac97 codec to bus */
429 static int soc_ac97_dev_register(struct snd_soc_codec
*codec
)
433 codec
->ac97
->dev
.bus
= &ac97_bus_type
;
434 codec
->ac97
->dev
.parent
= codec
->card
->dev
;
435 codec
->ac97
->dev
.release
= soc_ac97_device_release
;
437 dev_set_name(&codec
->ac97
->dev
, "%d-%d:%s",
438 codec
->card
->snd_card
->number
, 0, codec
->name
);
439 err
= device_register(&codec
->ac97
->dev
);
441 snd_printk(KERN_ERR
"Can't register ac97 bus\n");
442 codec
->ac97
->dev
.bus
= NULL
;
449 static int soc_pcm_apply_symmetry(struct snd_pcm_substream
*substream
)
451 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
452 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
453 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
456 if (codec_dai
->driver
->symmetric_rates
|| cpu_dai
->driver
->symmetric_rates
||
457 rtd
->dai_link
->symmetric_rates
) {
458 dev_dbg(&rtd
->dev
, "Symmetry forces %dHz rate\n",
461 ret
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
462 SNDRV_PCM_HW_PARAM_RATE
,
467 "Unable to apply rate symmetry constraint: %d\n", ret
);
476 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
477 * then initialized and any private data can be allocated. This also calls
478 * startup for the cpu DAI, platform, machine and codec DAI.
480 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
482 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
483 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
484 struct snd_soc_platform
*platform
= rtd
->platform
;
485 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
486 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
487 struct snd_soc_dai_driver
*cpu_dai_drv
= cpu_dai
->driver
;
488 struct snd_soc_dai_driver
*codec_dai_drv
= codec_dai
->driver
;
491 mutex_lock(&pcm_mutex
);
493 /* startup the audio subsystem */
494 if (cpu_dai
->driver
->ops
->startup
) {
495 ret
= cpu_dai
->driver
->ops
->startup(substream
, cpu_dai
);
497 printk(KERN_ERR
"asoc: can't open interface %s\n",
503 if (platform
->driver
->ops
->open
) {
504 ret
= platform
->driver
->ops
->open(substream
);
506 printk(KERN_ERR
"asoc: can't open platform %s\n", platform
->name
);
511 if (codec_dai
->driver
->ops
->startup
) {
512 ret
= codec_dai
->driver
->ops
->startup(substream
, codec_dai
);
514 printk(KERN_ERR
"asoc: can't open codec %s\n",
520 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->startup
) {
521 ret
= rtd
->dai_link
->ops
->startup(substream
);
523 printk(KERN_ERR
"asoc: %s startup failed\n", rtd
->dai_link
->name
);
528 /* Check that the codec and cpu DAI's are compatible */
529 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
530 runtime
->hw
.rate_min
=
531 max(codec_dai_drv
->playback
.rate_min
,
532 cpu_dai_drv
->playback
.rate_min
);
533 runtime
->hw
.rate_max
=
534 min(codec_dai_drv
->playback
.rate_max
,
535 cpu_dai_drv
->playback
.rate_max
);
536 runtime
->hw
.channels_min
=
537 max(codec_dai_drv
->playback
.channels_min
,
538 cpu_dai_drv
->playback
.channels_min
);
539 runtime
->hw
.channels_max
=
540 min(codec_dai_drv
->playback
.channels_max
,
541 cpu_dai_drv
->playback
.channels_max
);
542 runtime
->hw
.formats
=
543 codec_dai_drv
->playback
.formats
& cpu_dai_drv
->playback
.formats
;
545 codec_dai_drv
->playback
.rates
& cpu_dai_drv
->playback
.rates
;
546 if (codec_dai_drv
->playback
.rates
547 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
548 runtime
->hw
.rates
|= cpu_dai_drv
->playback
.rates
;
549 if (cpu_dai_drv
->playback
.rates
550 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
551 runtime
->hw
.rates
|= codec_dai_drv
->playback
.rates
;
553 runtime
->hw
.rate_min
=
554 max(codec_dai_drv
->capture
.rate_min
,
555 cpu_dai_drv
->capture
.rate_min
);
556 runtime
->hw
.rate_max
=
557 min(codec_dai_drv
->capture
.rate_max
,
558 cpu_dai_drv
->capture
.rate_max
);
559 runtime
->hw
.channels_min
=
560 max(codec_dai_drv
->capture
.channels_min
,
561 cpu_dai_drv
->capture
.channels_min
);
562 runtime
->hw
.channels_max
=
563 min(codec_dai_drv
->capture
.channels_max
,
564 cpu_dai_drv
->capture
.channels_max
);
565 runtime
->hw
.formats
=
566 codec_dai_drv
->capture
.formats
& cpu_dai_drv
->capture
.formats
;
568 codec_dai_drv
->capture
.rates
& cpu_dai_drv
->capture
.rates
;
569 if (codec_dai_drv
->capture
.rates
570 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
571 runtime
->hw
.rates
|= cpu_dai_drv
->capture
.rates
;
572 if (cpu_dai_drv
->capture
.rates
573 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
574 runtime
->hw
.rates
|= codec_dai_drv
->capture
.rates
;
577 snd_pcm_limit_hw_rates(runtime
);
578 if (!runtime
->hw
.rates
) {
579 printk(KERN_ERR
"asoc: %s <-> %s No matching rates\n",
580 codec_dai
->name
, cpu_dai
->name
);
583 if (!runtime
->hw
.formats
) {
584 printk(KERN_ERR
"asoc: %s <-> %s No matching formats\n",
585 codec_dai
->name
, cpu_dai
->name
);
588 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
) {
589 printk(KERN_ERR
"asoc: %s <-> %s No matching channels\n",
590 codec_dai
->name
, cpu_dai
->name
);
594 /* Symmetry only applies if we've already got an active stream. */
595 if (cpu_dai
->active
|| codec_dai
->active
) {
596 ret
= soc_pcm_apply_symmetry(substream
);
601 pr_debug("asoc: %s <-> %s info:\n",
602 codec_dai
->name
, cpu_dai
->name
);
603 pr_debug("asoc: rate mask 0x%x\n", runtime
->hw
.rates
);
604 pr_debug("asoc: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
605 runtime
->hw
.channels_max
);
606 pr_debug("asoc: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
607 runtime
->hw
.rate_max
);
609 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
610 cpu_dai
->playback_active
++;
611 codec_dai
->playback_active
++;
613 cpu_dai
->capture_active
++;
614 codec_dai
->capture_active
++;
618 rtd
->codec
->active
++;
619 mutex_unlock(&pcm_mutex
);
623 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
624 rtd
->dai_link
->ops
->shutdown(substream
);
627 if (codec_dai
->driver
->ops
->shutdown
)
628 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
631 if (platform
->driver
->ops
->close
)
632 platform
->driver
->ops
->close(substream
);
635 if (cpu_dai
->driver
->ops
->shutdown
)
636 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
638 mutex_unlock(&pcm_mutex
);
643 * Power down the audio subsystem pmdown_time msecs after close is called.
644 * This is to ensure there are no pops or clicks in between any music tracks
645 * due to DAPM power cycling.
647 static void close_delayed_work(struct work_struct
*work
)
649 struct snd_soc_pcm_runtime
*rtd
=
650 container_of(work
, struct snd_soc_pcm_runtime
, delayed_work
.work
);
651 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
653 mutex_lock(&pcm_mutex
);
655 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
656 codec_dai
->driver
->playback
.stream_name
,
657 codec_dai
->playback_active
? "active" : "inactive",
658 codec_dai
->pop_wait
? "yes" : "no");
660 /* are we waiting on this codec DAI stream */
661 if (codec_dai
->pop_wait
== 1) {
662 codec_dai
->pop_wait
= 0;
663 snd_soc_dapm_stream_event(rtd
,
664 codec_dai
->driver
->playback
.stream_name
,
665 SND_SOC_DAPM_STREAM_STOP
);
668 mutex_unlock(&pcm_mutex
);
672 * Called by ALSA when a PCM substream is closed. Private data can be
673 * freed here. The cpu DAI, codec DAI, machine and platform are also
676 static int soc_codec_close(struct snd_pcm_substream
*substream
)
678 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
679 struct snd_soc_platform
*platform
= rtd
->platform
;
680 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
681 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
682 struct snd_soc_codec
*codec
= rtd
->codec
;
684 mutex_lock(&pcm_mutex
);
686 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
687 cpu_dai
->playback_active
--;
688 codec_dai
->playback_active
--;
690 cpu_dai
->capture_active
--;
691 codec_dai
->capture_active
--;
698 /* Muting the DAC suppresses artifacts caused during digital
699 * shutdown, for example from stopping clocks.
701 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
702 snd_soc_dai_digital_mute(codec_dai
, 1);
704 if (cpu_dai
->driver
->ops
->shutdown
)
705 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
707 if (codec_dai
->driver
->ops
->shutdown
)
708 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
710 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
711 rtd
->dai_link
->ops
->shutdown(substream
);
713 if (platform
->driver
->ops
->close
)
714 platform
->driver
->ops
->close(substream
);
715 cpu_dai
->runtime
= NULL
;
717 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
718 /* start delayed pop wq here for playback streams */
719 codec_dai
->pop_wait
= 1;
720 schedule_delayed_work(&rtd
->delayed_work
,
721 msecs_to_jiffies(rtd
->pmdown_time
));
723 /* capture streams can be powered down now */
724 snd_soc_dapm_stream_event(rtd
,
725 codec_dai
->driver
->capture
.stream_name
,
726 SND_SOC_DAPM_STREAM_STOP
);
729 mutex_unlock(&pcm_mutex
);
734 * Called by ALSA when the PCM substream is prepared, can set format, sample
735 * rate, etc. This function is non atomic and can be called multiple times,
736 * it can refer to the runtime info.
738 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
740 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
741 struct snd_soc_platform
*platform
= rtd
->platform
;
742 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
743 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
746 mutex_lock(&pcm_mutex
);
748 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->prepare
) {
749 ret
= rtd
->dai_link
->ops
->prepare(substream
);
751 printk(KERN_ERR
"asoc: machine prepare error\n");
756 if (platform
->driver
->ops
->prepare
) {
757 ret
= platform
->driver
->ops
->prepare(substream
);
759 printk(KERN_ERR
"asoc: platform prepare error\n");
764 if (codec_dai
->driver
->ops
->prepare
) {
765 ret
= codec_dai
->driver
->ops
->prepare(substream
, codec_dai
);
767 printk(KERN_ERR
"asoc: codec DAI prepare error\n");
772 if (cpu_dai
->driver
->ops
->prepare
) {
773 ret
= cpu_dai
->driver
->ops
->prepare(substream
, cpu_dai
);
775 printk(KERN_ERR
"asoc: cpu DAI prepare error\n");
780 /* cancel any delayed stream shutdown that is pending */
781 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
782 codec_dai
->pop_wait
) {
783 codec_dai
->pop_wait
= 0;
784 cancel_delayed_work(&rtd
->delayed_work
);
787 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
788 snd_soc_dapm_stream_event(rtd
,
789 codec_dai
->driver
->playback
.stream_name
,
790 SND_SOC_DAPM_STREAM_START
);
792 snd_soc_dapm_stream_event(rtd
,
793 codec_dai
->driver
->capture
.stream_name
,
794 SND_SOC_DAPM_STREAM_START
);
796 snd_soc_dai_digital_mute(codec_dai
, 0);
799 mutex_unlock(&pcm_mutex
);
804 * Called by ALSA when the hardware params are set by application. This
805 * function can also be called multiple times and can allocate buffers
806 * (using snd_pcm_lib_* ). It's non-atomic.
808 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
809 struct snd_pcm_hw_params
*params
)
811 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
812 struct snd_soc_platform
*platform
= rtd
->platform
;
813 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
814 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
817 mutex_lock(&pcm_mutex
);
819 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_params
) {
820 ret
= rtd
->dai_link
->ops
->hw_params(substream
, params
);
822 printk(KERN_ERR
"asoc: machine hw_params failed\n");
827 if (codec_dai
->driver
->ops
->hw_params
) {
828 ret
= codec_dai
->driver
->ops
->hw_params(substream
, params
, codec_dai
);
830 printk(KERN_ERR
"asoc: can't set codec %s hw params\n",
836 if (cpu_dai
->driver
->ops
->hw_params
) {
837 ret
= cpu_dai
->driver
->ops
->hw_params(substream
, params
, cpu_dai
);
839 printk(KERN_ERR
"asoc: interface %s hw params failed\n",
845 if (platform
->driver
->ops
->hw_params
) {
846 ret
= platform
->driver
->ops
->hw_params(substream
, params
);
848 printk(KERN_ERR
"asoc: platform %s hw params failed\n",
854 rtd
->rate
= params_rate(params
);
857 mutex_unlock(&pcm_mutex
);
861 if (cpu_dai
->driver
->ops
->hw_free
)
862 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
865 if (codec_dai
->driver
->ops
->hw_free
)
866 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
869 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
870 rtd
->dai_link
->ops
->hw_free(substream
);
872 mutex_unlock(&pcm_mutex
);
877 * Free's resources allocated by hw_params, can be called multiple times
879 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
881 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
882 struct snd_soc_platform
*platform
= rtd
->platform
;
883 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
884 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
885 struct snd_soc_codec
*codec
= rtd
->codec
;
887 mutex_lock(&pcm_mutex
);
889 /* apply codec digital mute */
891 snd_soc_dai_digital_mute(codec_dai
, 1);
893 /* free any machine hw params */
894 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
895 rtd
->dai_link
->ops
->hw_free(substream
);
897 /* free any DMA resources */
898 if (platform
->driver
->ops
->hw_free
)
899 platform
->driver
->ops
->hw_free(substream
);
901 /* now free hw params for the DAI's */
902 if (codec_dai
->driver
->ops
->hw_free
)
903 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
905 if (cpu_dai
->driver
->ops
->hw_free
)
906 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
908 mutex_unlock(&pcm_mutex
);
912 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
914 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
915 struct snd_soc_platform
*platform
= rtd
->platform
;
916 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
917 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
920 if (codec_dai
->driver
->ops
->trigger
) {
921 ret
= codec_dai
->driver
->ops
->trigger(substream
, cmd
, codec_dai
);
926 if (platform
->driver
->ops
->trigger
) {
927 ret
= platform
->driver
->ops
->trigger(substream
, cmd
);
932 if (cpu_dai
->driver
->ops
->trigger
) {
933 ret
= cpu_dai
->driver
->ops
->trigger(substream
, cmd
, cpu_dai
);
941 * soc level wrapper for pointer callback
942 * If cpu_dai, codec_dai, platform driver has the delay callback, than
943 * the runtime->delay will be updated accordingly.
945 static snd_pcm_uframes_t
soc_pcm_pointer(struct snd_pcm_substream
*substream
)
947 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
948 struct snd_soc_platform
*platform
= rtd
->platform
;
949 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
950 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
951 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
952 snd_pcm_uframes_t offset
= 0;
953 snd_pcm_sframes_t delay
= 0;
955 if (platform
->driver
->ops
->pointer
)
956 offset
= platform
->driver
->ops
->pointer(substream
);
958 if (cpu_dai
->driver
->ops
->delay
)
959 delay
+= cpu_dai
->driver
->ops
->delay(substream
, cpu_dai
);
961 if (codec_dai
->driver
->ops
->delay
)
962 delay
+= codec_dai
->driver
->ops
->delay(substream
, codec_dai
);
964 if (platform
->driver
->delay
)
965 delay
+= platform
->driver
->delay(substream
, codec_dai
);
967 runtime
->delay
= delay
;
972 /* ASoC PCM operations */
973 static struct snd_pcm_ops soc_pcm_ops
= {
974 .open
= soc_pcm_open
,
975 .close
= soc_codec_close
,
976 .hw_params
= soc_pcm_hw_params
,
977 .hw_free
= soc_pcm_hw_free
,
978 .prepare
= soc_pcm_prepare
,
979 .trigger
= soc_pcm_trigger
,
980 .pointer
= soc_pcm_pointer
,
984 /* powers down audio subsystem for suspend */
985 static int soc_suspend(struct device
*dev
)
987 struct platform_device
*pdev
= to_platform_device(dev
);
988 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
991 /* If the initialization of this soc device failed, there is no codec
992 * associated with it. Just bail out in this case.
994 if (list_empty(&card
->codec_dev_list
))
997 /* Due to the resume being scheduled into a workqueue we could
998 * suspend before that's finished - wait for it to complete.
1000 snd_power_lock(card
->snd_card
);
1001 snd_power_wait(card
->snd_card
, SNDRV_CTL_POWER_D0
);
1002 snd_power_unlock(card
->snd_card
);
1004 /* we're going to block userspace touching us until resume completes */
1005 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D3hot
);
1007 /* mute any active DAC's */
1008 for (i
= 0; i
< card
->num_rtd
; i
++) {
1009 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
1010 struct snd_soc_dai_driver
*drv
= dai
->driver
;
1012 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1015 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
1016 drv
->ops
->digital_mute(dai
, 1);
1019 /* suspend all pcms */
1020 for (i
= 0; i
< card
->num_rtd
; i
++) {
1021 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1024 snd_pcm_suspend_all(card
->rtd
[i
].pcm
);
1027 if (card
->suspend_pre
)
1028 card
->suspend_pre(pdev
, PMSG_SUSPEND
);
1030 for (i
= 0; i
< card
->num_rtd
; i
++) {
1031 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1032 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1034 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1037 if (cpu_dai
->driver
->suspend
&& !cpu_dai
->driver
->ac97_control
)
1038 cpu_dai
->driver
->suspend(cpu_dai
);
1039 if (platform
->driver
->suspend
&& !platform
->suspended
) {
1040 platform
->driver
->suspend(cpu_dai
);
1041 platform
->suspended
= 1;
1045 /* close any waiting streams and save state */
1046 for (i
= 0; i
< card
->num_rtd
; i
++) {
1047 run_delayed_work(&card
->rtd
[i
].delayed_work
);
1048 card
->rtd
[i
].codec
->dapm
.suspend_bias_level
= card
->rtd
[i
].codec
->dapm
.bias_level
;
1051 for (i
= 0; i
< card
->num_rtd
; i
++) {
1052 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1054 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1057 if (driver
->playback
.stream_name
!= NULL
)
1058 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1059 SND_SOC_DAPM_STREAM_SUSPEND
);
1061 if (driver
->capture
.stream_name
!= NULL
)
1062 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1063 SND_SOC_DAPM_STREAM_SUSPEND
);
1066 /* suspend all CODECs */
1067 for (i
= 0; i
< card
->num_rtd
; i
++) {
1068 struct snd_soc_codec
*codec
= card
->rtd
[i
].codec
;
1069 /* If there are paths active then the CODEC will be held with
1070 * bias _ON and should not be suspended. */
1071 if (!codec
->suspended
&& codec
->driver
->suspend
) {
1072 switch (codec
->dapm
.bias_level
) {
1073 case SND_SOC_BIAS_STANDBY
:
1074 case SND_SOC_BIAS_OFF
:
1075 codec
->driver
->suspend(codec
, PMSG_SUSPEND
);
1076 codec
->suspended
= 1;
1079 dev_dbg(codec
->dev
, "CODEC is on over suspend\n");
1085 for (i
= 0; i
< card
->num_rtd
; i
++) {
1086 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1088 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1091 if (cpu_dai
->driver
->suspend
&& cpu_dai
->driver
->ac97_control
)
1092 cpu_dai
->driver
->suspend(cpu_dai
);
1095 if (card
->suspend_post
)
1096 card
->suspend_post(pdev
, PMSG_SUSPEND
);
1101 /* deferred resume work, so resume can complete before we finished
1102 * setting our codec back up, which can be very slow on I2C
1104 static void soc_resume_deferred(struct work_struct
*work
)
1106 struct snd_soc_card
*card
=
1107 container_of(work
, struct snd_soc_card
, deferred_resume_work
);
1108 struct platform_device
*pdev
= to_platform_device(card
->dev
);
1111 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1112 * so userspace apps are blocked from touching us
1115 dev_dbg(card
->dev
, "starting resume work\n");
1117 /* Bring us up into D2 so that DAPM starts enabling things */
1118 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D2
);
1120 if (card
->resume_pre
)
1121 card
->resume_pre(pdev
);
1123 /* resume AC97 DAIs */
1124 for (i
= 0; i
< card
->num_rtd
; i
++) {
1125 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1127 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1130 if (cpu_dai
->driver
->resume
&& cpu_dai
->driver
->ac97_control
)
1131 cpu_dai
->driver
->resume(cpu_dai
);
1134 for (i
= 0; i
< card
->num_rtd
; i
++) {
1135 struct snd_soc_codec
*codec
= card
->rtd
[i
].codec
;
1136 /* If the CODEC was idle over suspend then it will have been
1137 * left with bias OFF or STANDBY and suspended so we must now
1138 * resume. Otherwise the suspend was suppressed.
1140 if (codec
->driver
->resume
&& codec
->suspended
) {
1141 switch (codec
->dapm
.bias_level
) {
1142 case SND_SOC_BIAS_STANDBY
:
1143 case SND_SOC_BIAS_OFF
:
1144 codec
->driver
->resume(codec
);
1145 codec
->suspended
= 0;
1148 dev_dbg(codec
->dev
, "CODEC was on over suspend\n");
1154 for (i
= 0; i
< card
->num_rtd
; i
++) {
1155 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1157 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1160 if (driver
->playback
.stream_name
!= NULL
)
1161 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1162 SND_SOC_DAPM_STREAM_RESUME
);
1164 if (driver
->capture
.stream_name
!= NULL
)
1165 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1166 SND_SOC_DAPM_STREAM_RESUME
);
1169 /* unmute any active DACs */
1170 for (i
= 0; i
< card
->num_rtd
; i
++) {
1171 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
1172 struct snd_soc_dai_driver
*drv
= dai
->driver
;
1174 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1177 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
1178 drv
->ops
->digital_mute(dai
, 0);
1181 for (i
= 0; i
< card
->num_rtd
; i
++) {
1182 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1183 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1185 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1188 if (cpu_dai
->driver
->resume
&& !cpu_dai
->driver
->ac97_control
)
1189 cpu_dai
->driver
->resume(cpu_dai
);
1190 if (platform
->driver
->resume
&& platform
->suspended
) {
1191 platform
->driver
->resume(cpu_dai
);
1192 platform
->suspended
= 0;
1196 if (card
->resume_post
)
1197 card
->resume_post(pdev
);
1199 dev_dbg(card
->dev
, "resume work completed\n");
1201 /* userspace can access us now we are back as we were before */
1202 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D0
);
1205 /* powers up audio subsystem after a suspend */
1206 static int soc_resume(struct device
*dev
)
1208 struct platform_device
*pdev
= to_platform_device(dev
);
1209 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1212 /* AC97 devices might have other drivers hanging off them so
1213 * need to resume immediately. Other drivers don't have that
1214 * problem and may take a substantial amount of time to resume
1215 * due to I/O costs and anti-pop so handle them out of line.
1217 for (i
= 0; i
< card
->num_rtd
; i
++) {
1218 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1219 if (cpu_dai
->driver
->ac97_control
) {
1220 dev_dbg(dev
, "Resuming AC97 immediately\n");
1221 soc_resume_deferred(&card
->deferred_resume_work
);
1223 dev_dbg(dev
, "Scheduling resume work\n");
1224 if (!schedule_work(&card
->deferred_resume_work
))
1225 dev_err(dev
, "resume work item may be lost\n");
1232 #define soc_suspend NULL
1233 #define soc_resume NULL
1236 static struct snd_soc_dai_ops null_dai_ops
= {
1239 static int soc_bind_dai_link(struct snd_soc_card
*card
, int num
)
1241 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1242 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1243 struct snd_soc_codec
*codec
;
1244 struct snd_soc_platform
*platform
;
1245 struct snd_soc_dai
*codec_dai
, *cpu_dai
;
1249 dev_dbg(card
->dev
, "binding %s at idx %d\n", dai_link
->name
, num
);
1251 /* do we already have the CPU DAI for this link ? */
1255 /* no, then find CPU DAI from registered DAIs*/
1256 list_for_each_entry(cpu_dai
, &dai_list
, list
) {
1257 if (!strcmp(cpu_dai
->name
, dai_link
->cpu_dai_name
)) {
1259 if (!try_module_get(cpu_dai
->dev
->driver
->owner
))
1262 rtd
->cpu_dai
= cpu_dai
;
1266 dev_dbg(card
->dev
, "CPU DAI %s not registered\n",
1267 dai_link
->cpu_dai_name
);
1270 /* do we already have the CODEC for this link ? */
1275 /* no, then find CODEC from registered CODECs*/
1276 list_for_each_entry(codec
, &codec_list
, list
) {
1277 if (!strcmp(codec
->name
, dai_link
->codec_name
)) {
1280 if (!try_module_get(codec
->dev
->driver
->owner
))
1283 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1284 list_for_each_entry(codec_dai
, &dai_list
, list
) {
1285 if (codec
->dev
== codec_dai
->dev
&&
1286 !strcmp(codec_dai
->name
, dai_link
->codec_dai_name
)) {
1287 rtd
->codec_dai
= codec_dai
;
1291 dev_dbg(card
->dev
, "CODEC DAI %s not registered\n",
1292 dai_link
->codec_dai_name
);
1297 dev_dbg(card
->dev
, "CODEC %s not registered\n",
1298 dai_link
->codec_name
);
1301 /* do we already have the CODEC DAI for this link ? */
1302 if (rtd
->platform
) {
1305 /* no, then find CPU DAI from registered DAIs*/
1306 list_for_each_entry(platform
, &platform_list
, list
) {
1307 if (!strcmp(platform
->name
, dai_link
->platform_name
)) {
1309 if (!try_module_get(platform
->dev
->driver
->owner
))
1312 rtd
->platform
= platform
;
1317 dev_dbg(card
->dev
, "platform %s not registered\n",
1318 dai_link
->platform_name
);
1322 /* mark rtd as complete if we found all 4 of our client devices */
1323 if (rtd
->codec
&& rtd
->codec_dai
&& rtd
->platform
&& rtd
->cpu_dai
) {
1330 static void soc_remove_dai_link(struct snd_soc_card
*card
, int num
)
1332 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1333 struct snd_soc_codec
*codec
= rtd
->codec
;
1334 struct snd_soc_platform
*platform
= rtd
->platform
;
1335 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1338 /* unregister the rtd device */
1339 if (rtd
->dev_registered
) {
1340 device_remove_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1341 device_unregister(&rtd
->dev
);
1342 rtd
->dev_registered
= 0;
1345 /* remove the CODEC DAI */
1346 if (codec_dai
&& codec_dai
->probed
) {
1347 if (codec_dai
->driver
->remove
) {
1348 err
= codec_dai
->driver
->remove(codec_dai
);
1350 printk(KERN_ERR
"asoc: failed to remove %s\n", codec_dai
->name
);
1352 codec_dai
->probed
= 0;
1353 list_del(&codec_dai
->card_list
);
1356 /* remove the platform */
1357 if (platform
&& platform
->probed
) {
1358 if (platform
->driver
->remove
) {
1359 err
= platform
->driver
->remove(platform
);
1361 printk(KERN_ERR
"asoc: failed to remove %s\n", platform
->name
);
1363 platform
->probed
= 0;
1364 list_del(&platform
->card_list
);
1365 module_put(platform
->dev
->driver
->owner
);
1368 /* remove the CODEC */
1369 if (codec
&& codec
->probed
) {
1370 if (codec
->driver
->remove
) {
1371 err
= codec
->driver
->remove(codec
);
1373 printk(KERN_ERR
"asoc: failed to remove %s\n", codec
->name
);
1376 /* Make sure all DAPM widgets are freed */
1377 snd_soc_dapm_free(&codec
->dapm
);
1379 soc_cleanup_codec_debugfs(codec
);
1380 device_remove_file(&rtd
->dev
, &dev_attr_codec_reg
);
1382 list_del(&codec
->card_list
);
1383 module_put(codec
->dev
->driver
->owner
);
1386 /* remove the cpu_dai */
1387 if (cpu_dai
&& cpu_dai
->probed
) {
1388 if (cpu_dai
->driver
->remove
) {
1389 err
= cpu_dai
->driver
->remove(cpu_dai
);
1391 printk(KERN_ERR
"asoc: failed to remove %s\n", cpu_dai
->name
);
1393 cpu_dai
->probed
= 0;
1394 list_del(&cpu_dai
->card_list
);
1395 module_put(cpu_dai
->dev
->driver
->owner
);
1399 static void soc_set_name_prefix(struct snd_soc_card
*card
,
1400 struct snd_soc_codec
*codec
)
1404 if (card
->prefix_map
== NULL
)
1407 for (i
= 0; i
< card
->num_prefixes
; i
++) {
1408 struct snd_soc_prefix_map
*map
= &card
->prefix_map
[i
];
1409 if (map
->dev_name
&& !strcmp(codec
->name
, map
->dev_name
)) {
1410 codec
->name_prefix
= map
->name_prefix
;
1416 static void rtd_release(struct device
*dev
) {}
1418 static int soc_probe_dai_link(struct snd_soc_card
*card
, int num
)
1420 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1421 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1422 struct snd_soc_codec
*codec
= rtd
->codec
;
1423 struct snd_soc_platform
*platform
= rtd
->platform
;
1424 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1428 dev_dbg(card
->dev
, "probe %s dai link %d\n", card
->name
, num
);
1430 /* config components */
1431 codec_dai
->codec
= codec
;
1433 cpu_dai
->platform
= platform
;
1435 rtd
->dev
.parent
= card
->dev
;
1436 codec_dai
->card
= card
;
1437 cpu_dai
->card
= card
;
1439 /* set default power off timeout */
1440 rtd
->pmdown_time
= pmdown_time
;
1442 /* probe the cpu_dai */
1443 if (!cpu_dai
->probed
) {
1444 if (cpu_dai
->driver
->probe
) {
1445 ret
= cpu_dai
->driver
->probe(cpu_dai
);
1447 printk(KERN_ERR
"asoc: failed to probe CPU DAI %s\n",
1452 cpu_dai
->probed
= 1;
1453 /* mark cpu_dai as probed and add to card cpu_dai list */
1454 list_add(&cpu_dai
->card_list
, &card
->dai_dev_list
);
1457 /* probe the CODEC */
1458 if (!codec
->probed
) {
1459 codec
->dapm
.card
= card
;
1460 soc_set_name_prefix(card
, codec
);
1461 if (codec
->driver
->probe
) {
1462 ret
= codec
->driver
->probe(codec
);
1464 printk(KERN_ERR
"asoc: failed to probe CODEC %s\n",
1470 soc_init_codec_debugfs(codec
);
1472 /* mark codec as probed and add to card codec list */
1474 list_add(&codec
->card_list
, &card
->codec_dev_list
);
1477 /* probe the platform */
1478 if (!platform
->probed
) {
1479 if (platform
->driver
->probe
) {
1480 ret
= platform
->driver
->probe(platform
);
1482 printk(KERN_ERR
"asoc: failed to probe platform %s\n",
1487 /* mark platform as probed and add to card platform list */
1488 platform
->probed
= 1;
1489 list_add(&platform
->card_list
, &card
->platform_dev_list
);
1492 /* probe the CODEC DAI */
1493 if (!codec_dai
->probed
) {
1494 if (codec_dai
->driver
->probe
) {
1495 ret
= codec_dai
->driver
->probe(codec_dai
);
1497 printk(KERN_ERR
"asoc: failed to probe CODEC DAI %s\n",
1503 /* mark cpu_dai as probed and add to card cpu_dai list */
1504 codec_dai
->probed
= 1;
1505 list_add(&codec_dai
->card_list
, &card
->dai_dev_list
);
1508 /* DAPM dai link stream work */
1509 INIT_DELAYED_WORK(&rtd
->delayed_work
, close_delayed_work
);
1511 /* now that all clients have probed, initialise the DAI link */
1512 if (dai_link
->init
) {
1513 /* machine controls, routes and widgets are not prefixed */
1514 temp
= rtd
->codec
->name_prefix
;
1515 rtd
->codec
->name_prefix
= NULL
;
1516 ret
= dai_link
->init(rtd
);
1518 printk(KERN_ERR
"asoc: failed to init %s\n", dai_link
->stream_name
);
1521 rtd
->codec
->name_prefix
= temp
;
1524 /* Make sure all DAPM widgets are instantiated */
1525 snd_soc_dapm_new_widgets(&codec
->dapm
);
1526 snd_soc_dapm_sync(&codec
->dapm
);
1528 /* register the rtd device */
1529 rtd
->dev
.release
= rtd_release
;
1530 rtd
->dev
.init_name
= dai_link
->name
;
1531 ret
= device_register(&rtd
->dev
);
1533 printk(KERN_ERR
"asoc: failed to register DAI runtime device %d\n", ret
);
1537 rtd
->dev_registered
= 1;
1538 ret
= device_create_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1540 printk(KERN_WARNING
"asoc: failed to add pmdown_time sysfs\n");
1542 /* add DAPM sysfs entries for this codec */
1543 ret
= snd_soc_dapm_sys_add(&rtd
->dev
);
1545 printk(KERN_WARNING
"asoc: failed to add codec dapm sysfs entries\n");
1547 /* add codec sysfs entries */
1548 ret
= device_create_file(&rtd
->dev
, &dev_attr_codec_reg
);
1550 printk(KERN_WARNING
"asoc: failed to add codec sysfs files\n");
1552 /* create the pcm */
1553 ret
= soc_new_pcm(rtd
, num
);
1555 printk(KERN_ERR
"asoc: can't create pcm %s\n", dai_link
->stream_name
);
1559 /* add platform data for AC97 devices */
1560 if (rtd
->codec_dai
->driver
->ac97_control
)
1561 snd_ac97_dev_add_pdata(codec
->ac97
, rtd
->cpu_dai
->ac97_pdata
);
1566 #ifdef CONFIG_SND_SOC_AC97_BUS
1567 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime
*rtd
)
1571 /* Only instantiate AC97 if not already done by the adaptor
1572 * for the generic AC97 subsystem.
1574 if (rtd
->codec_dai
->driver
->ac97_control
&& !rtd
->codec
->ac97_registered
) {
1576 * It is possible that the AC97 device is already registered to
1577 * the device subsystem. This happens when the device is created
1578 * via snd_ac97_mixer(). Currently only SoC codec that does so
1579 * is the generic AC97 glue but others migh emerge.
1581 * In those cases we don't try to register the device again.
1583 if (!rtd
->codec
->ac97_created
)
1586 ret
= soc_ac97_dev_register(rtd
->codec
);
1588 printk(KERN_ERR
"asoc: AC97 device register failed\n");
1592 rtd
->codec
->ac97_registered
= 1;
1597 static void soc_unregister_ac97_dai_link(struct snd_soc_codec
*codec
)
1599 if (codec
->ac97_registered
) {
1600 soc_ac97_dev_unregister(codec
);
1601 codec
->ac97_registered
= 0;
1606 static void snd_soc_instantiate_card(struct snd_soc_card
*card
)
1608 struct platform_device
*pdev
= to_platform_device(card
->dev
);
1611 mutex_lock(&card
->mutex
);
1613 if (card
->instantiated
) {
1614 mutex_unlock(&card
->mutex
);
1619 for (i
= 0; i
< card
->num_links
; i
++)
1620 soc_bind_dai_link(card
, i
);
1622 /* bind completed ? */
1623 if (card
->num_rtd
!= card
->num_links
) {
1624 mutex_unlock(&card
->mutex
);
1628 /* card bind complete so register a sound card */
1629 ret
= snd_card_create(SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
1630 card
->owner
, 0, &card
->snd_card
);
1632 printk(KERN_ERR
"asoc: can't create sound card for card %s\n",
1634 mutex_unlock(&card
->mutex
);
1637 card
->snd_card
->dev
= card
->dev
;
1640 /* deferred resume work */
1641 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
1644 /* initialise the sound card only once */
1646 ret
= card
->probe(pdev
);
1648 goto card_probe_error
;
1651 for (i
= 0; i
< card
->num_links
; i
++) {
1652 ret
= soc_probe_dai_link(card
, i
);
1654 pr_err("asoc: failed to instantiate card %s: %d\n",
1660 snprintf(card
->snd_card
->shortname
, sizeof(card
->snd_card
->shortname
),
1662 snprintf(card
->snd_card
->longname
, sizeof(card
->snd_card
->longname
),
1665 ret
= snd_card_register(card
->snd_card
);
1667 printk(KERN_ERR
"asoc: failed to register soundcard for %s\n", card
->name
);
1671 #ifdef CONFIG_SND_SOC_AC97_BUS
1672 /* register any AC97 codecs */
1673 for (i
= 0; i
< card
->num_rtd
; i
++) {
1674 ret
= soc_register_ac97_dai_link(&card
->rtd
[i
]);
1676 printk(KERN_ERR
"asoc: failed to register AC97 %s\n", card
->name
);
1682 card
->instantiated
= 1;
1683 mutex_unlock(&card
->mutex
);
1687 for (i
= 0; i
< card
->num_links
; i
++)
1688 soc_remove_dai_link(card
, i
);
1694 snd_card_free(card
->snd_card
);
1696 mutex_unlock(&card
->mutex
);
1700 * Attempt to initialise any uninitialised cards. Must be called with
1703 static void snd_soc_instantiate_cards(void)
1705 struct snd_soc_card
*card
;
1706 list_for_each_entry(card
, &card_list
, list
)
1707 snd_soc_instantiate_card(card
);
1710 /* probes a new socdev */
1711 static int soc_probe(struct platform_device
*pdev
)
1713 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1716 /* Bodge while we unpick instantiation */
1717 card
->dev
= &pdev
->dev
;
1718 INIT_LIST_HEAD(&card
->dai_dev_list
);
1719 INIT_LIST_HEAD(&card
->codec_dev_list
);
1720 INIT_LIST_HEAD(&card
->platform_dev_list
);
1722 soc_init_card_debugfs(card
);
1724 ret
= snd_soc_register_card(card
);
1726 dev_err(&pdev
->dev
, "Failed to register card\n");
1733 /* removes a socdev */
1734 static int soc_remove(struct platform_device
*pdev
)
1736 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1739 if (card
->instantiated
) {
1741 /* make sure any delayed work runs */
1742 for (i
= 0; i
< card
->num_rtd
; i
++) {
1743 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1744 run_delayed_work(&rtd
->delayed_work
);
1747 /* remove and free each DAI */
1748 for (i
= 0; i
< card
->num_rtd
; i
++)
1749 soc_remove_dai_link(card
, i
);
1751 soc_cleanup_card_debugfs(card
);
1753 /* remove the card */
1758 snd_card_free(card
->snd_card
);
1760 snd_soc_unregister_card(card
);
1764 static int soc_poweroff(struct device
*dev
)
1766 struct platform_device
*pdev
= to_platform_device(dev
);
1767 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1770 if (!card
->instantiated
)
1773 /* Flush out pmdown_time work - we actually do want to run it
1774 * now, we're shutting down so no imminent restart. */
1775 for (i
= 0; i
< card
->num_rtd
; i
++) {
1776 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1777 run_delayed_work(&rtd
->delayed_work
);
1780 snd_soc_dapm_shutdown(card
);
1785 static const struct dev_pm_ops soc_pm_ops
= {
1786 .suspend
= soc_suspend
,
1787 .resume
= soc_resume
,
1788 .poweroff
= soc_poweroff
,
1791 /* ASoC platform driver */
1792 static struct platform_driver soc_driver
= {
1794 .name
= "soc-audio",
1795 .owner
= THIS_MODULE
,
1799 .remove
= soc_remove
,
1802 /* create a new pcm */
1803 static int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
)
1805 struct snd_soc_codec
*codec
= rtd
->codec
;
1806 struct snd_soc_platform
*platform
= rtd
->platform
;
1807 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
1808 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1809 struct snd_pcm
*pcm
;
1811 int ret
= 0, playback
= 0, capture
= 0;
1813 /* check client and interface hw capabilities */
1814 snprintf(new_name
, sizeof(new_name
), "%s %s-%d",
1815 rtd
->dai_link
->stream_name
, codec_dai
->name
, num
);
1817 if (codec_dai
->driver
->playback
.channels_min
)
1819 if (codec_dai
->driver
->capture
.channels_min
)
1822 dev_dbg(rtd
->card
->dev
, "registered pcm #%d %s\n",num
,new_name
);
1823 ret
= snd_pcm_new(rtd
->card
->snd_card
, new_name
,
1824 num
, playback
, capture
, &pcm
);
1826 printk(KERN_ERR
"asoc: can't create pcm for codec %s\n", codec
->name
);
1831 pcm
->private_data
= rtd
;
1832 soc_pcm_ops
.mmap
= platform
->driver
->ops
->mmap
;
1833 soc_pcm_ops
.pointer
= platform
->driver
->ops
->pointer
;
1834 soc_pcm_ops
.ioctl
= platform
->driver
->ops
->ioctl
;
1835 soc_pcm_ops
.copy
= platform
->driver
->ops
->copy
;
1836 soc_pcm_ops
.silence
= platform
->driver
->ops
->silence
;
1837 soc_pcm_ops
.ack
= platform
->driver
->ops
->ack
;
1838 soc_pcm_ops
.page
= platform
->driver
->ops
->page
;
1841 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &soc_pcm_ops
);
1844 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &soc_pcm_ops
);
1846 ret
= platform
->driver
->pcm_new(rtd
->card
->snd_card
, codec_dai
, pcm
);
1848 printk(KERN_ERR
"asoc: platform pcm constructor failed\n");
1852 pcm
->private_free
= platform
->driver
->pcm_free
;
1853 printk(KERN_INFO
"asoc: %s <-> %s mapping ok\n", codec_dai
->name
,
1859 * snd_soc_codec_volatile_register: Report if a register is volatile.
1861 * @codec: CODEC to query.
1862 * @reg: Register to query.
1864 * Boolean function indiciating if a CODEC register is volatile.
1866 int snd_soc_codec_volatile_register(struct snd_soc_codec
*codec
, int reg
)
1868 if (codec
->driver
->volatile_register
)
1869 return codec
->driver
->volatile_register(reg
);
1873 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register
);
1876 * snd_soc_new_ac97_codec - initailise AC97 device
1877 * @codec: audio codec
1878 * @ops: AC97 bus operations
1879 * @num: AC97 codec number
1881 * Initialises AC97 codec resources for use by ad-hoc devices only.
1883 int snd_soc_new_ac97_codec(struct snd_soc_codec
*codec
,
1884 struct snd_ac97_bus_ops
*ops
, int num
)
1886 mutex_lock(&codec
->mutex
);
1888 codec
->ac97
= kzalloc(sizeof(struct snd_ac97
), GFP_KERNEL
);
1889 if (codec
->ac97
== NULL
) {
1890 mutex_unlock(&codec
->mutex
);
1894 codec
->ac97
->bus
= kzalloc(sizeof(struct snd_ac97_bus
), GFP_KERNEL
);
1895 if (codec
->ac97
->bus
== NULL
) {
1898 mutex_unlock(&codec
->mutex
);
1902 codec
->ac97
->bus
->ops
= ops
;
1903 codec
->ac97
->num
= num
;
1906 * Mark the AC97 device to be created by us. This way we ensure that the
1907 * device will be registered with the device subsystem later on.
1909 codec
->ac97_created
= 1;
1911 mutex_unlock(&codec
->mutex
);
1914 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec
);
1917 * snd_soc_free_ac97_codec - free AC97 codec device
1918 * @codec: audio codec
1920 * Frees AC97 codec device resources.
1922 void snd_soc_free_ac97_codec(struct snd_soc_codec
*codec
)
1924 mutex_lock(&codec
->mutex
);
1925 #ifdef CONFIG_SND_SOC_AC97_BUS
1926 soc_unregister_ac97_dai_link(codec
);
1928 kfree(codec
->ac97
->bus
);
1931 codec
->ac97_created
= 0;
1932 mutex_unlock(&codec
->mutex
);
1934 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec
);
1936 unsigned int snd_soc_read(struct snd_soc_codec
*codec
, unsigned int reg
)
1940 ret
= codec
->driver
->read(codec
, reg
);
1941 dev_dbg(codec
->dev
, "read %x => %x\n", reg
, ret
);
1942 trace_snd_soc_reg_read(codec
, reg
, ret
);
1946 EXPORT_SYMBOL_GPL(snd_soc_read
);
1948 unsigned int snd_soc_write(struct snd_soc_codec
*codec
,
1949 unsigned int reg
, unsigned int val
)
1951 dev_dbg(codec
->dev
, "write %x = %x\n", reg
, val
);
1952 trace_snd_soc_reg_write(codec
, reg
, val
);
1953 return codec
->driver
->write(codec
, reg
, val
);
1955 EXPORT_SYMBOL_GPL(snd_soc_write
);
1958 * snd_soc_update_bits - update codec register bits
1959 * @codec: audio codec
1960 * @reg: codec register
1961 * @mask: register mask
1964 * Writes new register value.
1966 * Returns 1 for change else 0.
1968 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1969 unsigned int mask
, unsigned int value
)
1972 unsigned int old
, new;
1974 old
= snd_soc_read(codec
, reg
);
1975 new = (old
& ~mask
) | value
;
1976 change
= old
!= new;
1978 snd_soc_write(codec
, reg
, new);
1982 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
1985 * snd_soc_update_bits_locked - update codec register bits
1986 * @codec: audio codec
1987 * @reg: codec register
1988 * @mask: register mask
1991 * Writes new register value, and takes the codec mutex.
1993 * Returns 1 for change else 0.
1995 int snd_soc_update_bits_locked(struct snd_soc_codec
*codec
,
1996 unsigned short reg
, unsigned int mask
,
2001 mutex_lock(&codec
->mutex
);
2002 change
= snd_soc_update_bits(codec
, reg
, mask
, value
);
2003 mutex_unlock(&codec
->mutex
);
2007 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked
);
2010 * snd_soc_test_bits - test register for change
2011 * @codec: audio codec
2012 * @reg: codec register
2013 * @mask: register mask
2016 * Tests a register with a new value and checks if the new value is
2017 * different from the old value.
2019 * Returns 1 for change else 0.
2021 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
2022 unsigned int mask
, unsigned int value
)
2025 unsigned int old
, new;
2027 old
= snd_soc_read(codec
, reg
);
2028 new = (old
& ~mask
) | value
;
2029 change
= old
!= new;
2033 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
2036 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2037 * @substream: the pcm substream
2038 * @hw: the hardware parameters
2040 * Sets the substream runtime hardware parameters.
2042 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
2043 const struct snd_pcm_hardware
*hw
)
2045 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2046 runtime
->hw
.info
= hw
->info
;
2047 runtime
->hw
.formats
= hw
->formats
;
2048 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
2049 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
2050 runtime
->hw
.periods_min
= hw
->periods_min
;
2051 runtime
->hw
.periods_max
= hw
->periods_max
;
2052 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
2053 runtime
->hw
.fifo_size
= hw
->fifo_size
;
2056 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
2059 * snd_soc_cnew - create new control
2060 * @_template: control template
2061 * @data: control private data
2062 * @long_name: control long name
2064 * Create a new mixer control from a template control.
2066 * Returns 0 for success, else error.
2068 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
2069 void *data
, char *long_name
)
2071 struct snd_kcontrol_new
template;
2073 memcpy(&template, _template
, sizeof(template));
2075 template.name
= long_name
;
2078 return snd_ctl_new1(&template, data
);
2080 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
2083 * snd_soc_add_controls - add an array of controls to a codec.
2084 * Convienience function to add a list of controls. Many codecs were
2085 * duplicating this code.
2087 * @codec: codec to add controls to
2088 * @controls: array of controls to add
2089 * @num_controls: number of elements in the array
2091 * Return 0 for success, else error.
2093 int snd_soc_add_controls(struct snd_soc_codec
*codec
,
2094 const struct snd_kcontrol_new
*controls
, int num_controls
)
2096 struct snd_card
*card
= codec
->card
->snd_card
;
2097 char prefixed_name
[44], *name
;
2100 for (i
= 0; i
< num_controls
; i
++) {
2101 const struct snd_kcontrol_new
*control
= &controls
[i
];
2102 if (codec
->name_prefix
) {
2103 snprintf(prefixed_name
, sizeof(prefixed_name
), "%s %s",
2104 codec
->name_prefix
, control
->name
);
2105 name
= prefixed_name
;
2107 name
= control
->name
;
2109 err
= snd_ctl_add(card
, snd_soc_cnew(control
, codec
, name
));
2111 dev_err(codec
->dev
, "%s: Failed to add %s: %d\n",
2112 codec
->name
, name
, err
);
2119 EXPORT_SYMBOL_GPL(snd_soc_add_controls
);
2122 * snd_soc_info_enum_double - enumerated double mixer info callback
2123 * @kcontrol: mixer control
2124 * @uinfo: control element information
2126 * Callback to provide information about a double enumerated
2129 * Returns 0 for success.
2131 int snd_soc_info_enum_double(struct snd_kcontrol
*kcontrol
,
2132 struct snd_ctl_elem_info
*uinfo
)
2134 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2136 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2137 uinfo
->count
= e
->shift_l
== e
->shift_r
? 1 : 2;
2138 uinfo
->value
.enumerated
.items
= e
->max
;
2140 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2141 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2142 strcpy(uinfo
->value
.enumerated
.name
,
2143 e
->texts
[uinfo
->value
.enumerated
.item
]);
2146 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double
);
2149 * snd_soc_get_enum_double - enumerated double mixer get callback
2150 * @kcontrol: mixer control
2151 * @ucontrol: control element information
2153 * Callback to get the value of a double enumerated mixer.
2155 * Returns 0 for success.
2157 int snd_soc_get_enum_double(struct snd_kcontrol
*kcontrol
,
2158 struct snd_ctl_elem_value
*ucontrol
)
2160 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2161 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2162 unsigned int val
, bitmask
;
2164 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2166 val
= snd_soc_read(codec
, e
->reg
);
2167 ucontrol
->value
.enumerated
.item
[0]
2168 = (val
>> e
->shift_l
) & (bitmask
- 1);
2169 if (e
->shift_l
!= e
->shift_r
)
2170 ucontrol
->value
.enumerated
.item
[1] =
2171 (val
>> e
->shift_r
) & (bitmask
- 1);
2175 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double
);
2178 * snd_soc_put_enum_double - enumerated double mixer put callback
2179 * @kcontrol: mixer control
2180 * @ucontrol: control element information
2182 * Callback to set the value of a double enumerated mixer.
2184 * Returns 0 for success.
2186 int snd_soc_put_enum_double(struct snd_kcontrol
*kcontrol
,
2187 struct snd_ctl_elem_value
*ucontrol
)
2189 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2190 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2192 unsigned int mask
, bitmask
;
2194 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2196 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2198 val
= ucontrol
->value
.enumerated
.item
[0] << e
->shift_l
;
2199 mask
= (bitmask
- 1) << e
->shift_l
;
2200 if (e
->shift_l
!= e
->shift_r
) {
2201 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2203 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2204 mask
|= (bitmask
- 1) << e
->shift_r
;
2207 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2209 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double
);
2212 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2213 * @kcontrol: mixer control
2214 * @ucontrol: control element information
2216 * Callback to get the value of a double semi enumerated mixer.
2218 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2219 * used for handling bitfield coded enumeration for example.
2221 * Returns 0 for success.
2223 int snd_soc_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2224 struct snd_ctl_elem_value
*ucontrol
)
2226 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2227 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2228 unsigned int reg_val
, val
, mux
;
2230 reg_val
= snd_soc_read(codec
, e
->reg
);
2231 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2232 for (mux
= 0; mux
< e
->max
; mux
++) {
2233 if (val
== e
->values
[mux
])
2236 ucontrol
->value
.enumerated
.item
[0] = mux
;
2237 if (e
->shift_l
!= e
->shift_r
) {
2238 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2239 for (mux
= 0; mux
< e
->max
; mux
++) {
2240 if (val
== e
->values
[mux
])
2243 ucontrol
->value
.enumerated
.item
[1] = mux
;
2248 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double
);
2251 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2252 * @kcontrol: mixer control
2253 * @ucontrol: control element information
2255 * Callback to set the value of a double semi enumerated mixer.
2257 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2258 * used for handling bitfield coded enumeration for example.
2260 * Returns 0 for success.
2262 int snd_soc_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2263 struct snd_ctl_elem_value
*ucontrol
)
2265 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2266 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2270 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2272 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2273 mask
= e
->mask
<< e
->shift_l
;
2274 if (e
->shift_l
!= e
->shift_r
) {
2275 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2277 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2278 mask
|= e
->mask
<< e
->shift_r
;
2281 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2283 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double
);
2286 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2287 * @kcontrol: mixer control
2288 * @uinfo: control element information
2290 * Callback to provide information about an external enumerated
2293 * Returns 0 for success.
2295 int snd_soc_info_enum_ext(struct snd_kcontrol
*kcontrol
,
2296 struct snd_ctl_elem_info
*uinfo
)
2298 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2300 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2302 uinfo
->value
.enumerated
.items
= e
->max
;
2304 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2305 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2306 strcpy(uinfo
->value
.enumerated
.name
,
2307 e
->texts
[uinfo
->value
.enumerated
.item
]);
2310 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext
);
2313 * snd_soc_info_volsw_ext - external single mixer info callback
2314 * @kcontrol: mixer control
2315 * @uinfo: control element information
2317 * Callback to provide information about a single external mixer control.
2319 * Returns 0 for success.
2321 int snd_soc_info_volsw_ext(struct snd_kcontrol
*kcontrol
,
2322 struct snd_ctl_elem_info
*uinfo
)
2324 int max
= kcontrol
->private_value
;
2326 if (max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2327 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2329 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2332 uinfo
->value
.integer
.min
= 0;
2333 uinfo
->value
.integer
.max
= max
;
2336 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext
);
2339 * snd_soc_info_volsw - single mixer info callback
2340 * @kcontrol: mixer control
2341 * @uinfo: control element information
2343 * Callback to provide information about a single mixer control.
2345 * Returns 0 for success.
2347 int snd_soc_info_volsw(struct snd_kcontrol
*kcontrol
,
2348 struct snd_ctl_elem_info
*uinfo
)
2350 struct soc_mixer_control
*mc
=
2351 (struct soc_mixer_control
*)kcontrol
->private_value
;
2353 unsigned int shift
= mc
->shift
;
2354 unsigned int rshift
= mc
->rshift
;
2356 if (!mc
->platform_max
)
2357 mc
->platform_max
= mc
->max
;
2358 platform_max
= mc
->platform_max
;
2360 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2361 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2363 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2365 uinfo
->count
= shift
== rshift
? 1 : 2;
2366 uinfo
->value
.integer
.min
= 0;
2367 uinfo
->value
.integer
.max
= platform_max
;
2370 EXPORT_SYMBOL_GPL(snd_soc_info_volsw
);
2373 * snd_soc_get_volsw - single mixer get callback
2374 * @kcontrol: mixer control
2375 * @ucontrol: control element information
2377 * Callback to get the value of a single mixer control.
2379 * Returns 0 for success.
2381 int snd_soc_get_volsw(struct snd_kcontrol
*kcontrol
,
2382 struct snd_ctl_elem_value
*ucontrol
)
2384 struct soc_mixer_control
*mc
=
2385 (struct soc_mixer_control
*)kcontrol
->private_value
;
2386 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2387 unsigned int reg
= mc
->reg
;
2388 unsigned int shift
= mc
->shift
;
2389 unsigned int rshift
= mc
->rshift
;
2391 unsigned int mask
= (1 << fls(max
)) - 1;
2392 unsigned int invert
= mc
->invert
;
2394 ucontrol
->value
.integer
.value
[0] =
2395 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2396 if (shift
!= rshift
)
2397 ucontrol
->value
.integer
.value
[1] =
2398 (snd_soc_read(codec
, reg
) >> rshift
) & mask
;
2400 ucontrol
->value
.integer
.value
[0] =
2401 max
- ucontrol
->value
.integer
.value
[0];
2402 if (shift
!= rshift
)
2403 ucontrol
->value
.integer
.value
[1] =
2404 max
- ucontrol
->value
.integer
.value
[1];
2409 EXPORT_SYMBOL_GPL(snd_soc_get_volsw
);
2412 * snd_soc_put_volsw - single mixer put callback
2413 * @kcontrol: mixer control
2414 * @ucontrol: control element information
2416 * Callback to set the value of a single mixer control.
2418 * Returns 0 for success.
2420 int snd_soc_put_volsw(struct snd_kcontrol
*kcontrol
,
2421 struct snd_ctl_elem_value
*ucontrol
)
2423 struct soc_mixer_control
*mc
=
2424 (struct soc_mixer_control
*)kcontrol
->private_value
;
2425 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2426 unsigned int reg
= mc
->reg
;
2427 unsigned int shift
= mc
->shift
;
2428 unsigned int rshift
= mc
->rshift
;
2430 unsigned int mask
= (1 << fls(max
)) - 1;
2431 unsigned int invert
= mc
->invert
;
2432 unsigned int val
, val2
, val_mask
;
2434 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2437 val_mask
= mask
<< shift
;
2439 if (shift
!= rshift
) {
2440 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2443 val_mask
|= mask
<< rshift
;
2444 val
|= val2
<< rshift
;
2446 return snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2448 EXPORT_SYMBOL_GPL(snd_soc_put_volsw
);
2451 * snd_soc_info_volsw_2r - double mixer info callback
2452 * @kcontrol: mixer control
2453 * @uinfo: control element information
2455 * Callback to provide information about a double mixer control that
2456 * spans 2 codec registers.
2458 * Returns 0 for success.
2460 int snd_soc_info_volsw_2r(struct snd_kcontrol
*kcontrol
,
2461 struct snd_ctl_elem_info
*uinfo
)
2463 struct soc_mixer_control
*mc
=
2464 (struct soc_mixer_control
*)kcontrol
->private_value
;
2467 if (!mc
->platform_max
)
2468 mc
->platform_max
= mc
->max
;
2469 platform_max
= mc
->platform_max
;
2471 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2472 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2474 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2477 uinfo
->value
.integer
.min
= 0;
2478 uinfo
->value
.integer
.max
= platform_max
;
2481 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r
);
2484 * snd_soc_get_volsw_2r - double mixer get callback
2485 * @kcontrol: mixer control
2486 * @ucontrol: control element information
2488 * Callback to get the value of a double mixer control that spans 2 registers.
2490 * Returns 0 for success.
2492 int snd_soc_get_volsw_2r(struct snd_kcontrol
*kcontrol
,
2493 struct snd_ctl_elem_value
*ucontrol
)
2495 struct soc_mixer_control
*mc
=
2496 (struct soc_mixer_control
*)kcontrol
->private_value
;
2497 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2498 unsigned int reg
= mc
->reg
;
2499 unsigned int reg2
= mc
->rreg
;
2500 unsigned int shift
= mc
->shift
;
2502 unsigned int mask
= (1 << fls(max
)) - 1;
2503 unsigned int invert
= mc
->invert
;
2505 ucontrol
->value
.integer
.value
[0] =
2506 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2507 ucontrol
->value
.integer
.value
[1] =
2508 (snd_soc_read(codec
, reg2
) >> shift
) & mask
;
2510 ucontrol
->value
.integer
.value
[0] =
2511 max
- ucontrol
->value
.integer
.value
[0];
2512 ucontrol
->value
.integer
.value
[1] =
2513 max
- ucontrol
->value
.integer
.value
[1];
2518 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r
);
2521 * snd_soc_put_volsw_2r - double mixer set callback
2522 * @kcontrol: mixer control
2523 * @ucontrol: control element information
2525 * Callback to set the value of a double mixer control that spans 2 registers.
2527 * Returns 0 for success.
2529 int snd_soc_put_volsw_2r(struct snd_kcontrol
*kcontrol
,
2530 struct snd_ctl_elem_value
*ucontrol
)
2532 struct soc_mixer_control
*mc
=
2533 (struct soc_mixer_control
*)kcontrol
->private_value
;
2534 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2535 unsigned int reg
= mc
->reg
;
2536 unsigned int reg2
= mc
->rreg
;
2537 unsigned int shift
= mc
->shift
;
2539 unsigned int mask
= (1 << fls(max
)) - 1;
2540 unsigned int invert
= mc
->invert
;
2542 unsigned int val
, val2
, val_mask
;
2544 val_mask
= mask
<< shift
;
2545 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2546 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2554 val2
= val2
<< shift
;
2556 err
= snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2560 err
= snd_soc_update_bits_locked(codec
, reg2
, val_mask
, val2
);
2563 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r
);
2566 * snd_soc_info_volsw_s8 - signed mixer info callback
2567 * @kcontrol: mixer control
2568 * @uinfo: control element information
2570 * Callback to provide information about a signed mixer control.
2572 * Returns 0 for success.
2574 int snd_soc_info_volsw_s8(struct snd_kcontrol
*kcontrol
,
2575 struct snd_ctl_elem_info
*uinfo
)
2577 struct soc_mixer_control
*mc
=
2578 (struct soc_mixer_control
*)kcontrol
->private_value
;
2582 if (!mc
->platform_max
)
2583 mc
->platform_max
= mc
->max
;
2584 platform_max
= mc
->platform_max
;
2586 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2588 uinfo
->value
.integer
.min
= 0;
2589 uinfo
->value
.integer
.max
= platform_max
- min
;
2592 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8
);
2595 * snd_soc_get_volsw_s8 - signed mixer get callback
2596 * @kcontrol: mixer control
2597 * @ucontrol: control element information
2599 * Callback to get the value of a signed mixer control.
2601 * Returns 0 for success.
2603 int snd_soc_get_volsw_s8(struct snd_kcontrol
*kcontrol
,
2604 struct snd_ctl_elem_value
*ucontrol
)
2606 struct soc_mixer_control
*mc
=
2607 (struct soc_mixer_control
*)kcontrol
->private_value
;
2608 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2609 unsigned int reg
= mc
->reg
;
2611 int val
= snd_soc_read(codec
, reg
);
2613 ucontrol
->value
.integer
.value
[0] =
2614 ((signed char)(val
& 0xff))-min
;
2615 ucontrol
->value
.integer
.value
[1] =
2616 ((signed char)((val
>> 8) & 0xff))-min
;
2619 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8
);
2622 * snd_soc_put_volsw_sgn - signed mixer put callback
2623 * @kcontrol: mixer control
2624 * @ucontrol: control element information
2626 * Callback to set the value of a signed mixer control.
2628 * Returns 0 for success.
2630 int snd_soc_put_volsw_s8(struct snd_kcontrol
*kcontrol
,
2631 struct snd_ctl_elem_value
*ucontrol
)
2633 struct soc_mixer_control
*mc
=
2634 (struct soc_mixer_control
*)kcontrol
->private_value
;
2635 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2636 unsigned int reg
= mc
->reg
;
2640 val
= (ucontrol
->value
.integer
.value
[0]+min
) & 0xff;
2641 val
|= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff) << 8;
2643 return snd_soc_update_bits_locked(codec
, reg
, 0xffff, val
);
2645 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8
);
2648 * snd_soc_limit_volume - Set new limit to an existing volume control.
2650 * @codec: where to look for the control
2651 * @name: Name of the control
2652 * @max: new maximum limit
2654 * Return 0 for success, else error.
2656 int snd_soc_limit_volume(struct snd_soc_codec
*codec
,
2657 const char *name
, int max
)
2659 struct snd_card
*card
= codec
->card
->snd_card
;
2660 struct snd_kcontrol
*kctl
;
2661 struct soc_mixer_control
*mc
;
2665 /* Sanity check for name and max */
2666 if (unlikely(!name
|| max
<= 0))
2669 list_for_each_entry(kctl
, &card
->controls
, list
) {
2670 if (!strncmp(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
))) {
2676 mc
= (struct soc_mixer_control
*)kctl
->private_value
;
2677 if (max
<= mc
->max
) {
2678 mc
->platform_max
= max
;
2684 EXPORT_SYMBOL_GPL(snd_soc_limit_volume
);
2687 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2688 * mixer info callback
2689 * @kcontrol: mixer control
2690 * @uinfo: control element information
2692 * Returns 0 for success.
2694 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2695 struct snd_ctl_elem_info
*uinfo
)
2697 struct soc_mixer_control
*mc
=
2698 (struct soc_mixer_control
*)kcontrol
->private_value
;
2702 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2704 uinfo
->value
.integer
.min
= 0;
2705 uinfo
->value
.integer
.max
= max
-min
;
2709 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx
);
2712 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2713 * mixer get callback
2714 * @kcontrol: mixer control
2715 * @uinfo: control element information
2717 * Returns 0 for success.
2719 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2720 struct snd_ctl_elem_value
*ucontrol
)
2722 struct soc_mixer_control
*mc
=
2723 (struct soc_mixer_control
*)kcontrol
->private_value
;
2724 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2725 unsigned int mask
= (1<<mc
->shift
)-1;
2727 int val
= snd_soc_read(codec
, mc
->reg
) & mask
;
2728 int valr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2730 ucontrol
->value
.integer
.value
[0] = ((val
& 0xff)-min
) & mask
;
2731 ucontrol
->value
.integer
.value
[1] = ((valr
& 0xff)-min
) & mask
;
2734 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx
);
2737 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2738 * mixer put callback
2739 * @kcontrol: mixer control
2740 * @uinfo: control element information
2742 * Returns 0 for success.
2744 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2745 struct snd_ctl_elem_value
*ucontrol
)
2747 struct soc_mixer_control
*mc
=
2748 (struct soc_mixer_control
*)kcontrol
->private_value
;
2749 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2750 unsigned int mask
= (1<<mc
->shift
)-1;
2753 unsigned int val
, valr
, oval
, ovalr
;
2755 val
= ((ucontrol
->value
.integer
.value
[0]+min
) & 0xff);
2757 valr
= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff);
2760 oval
= snd_soc_read(codec
, mc
->reg
) & mask
;
2761 ovalr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2765 ret
= snd_soc_write(codec
, mc
->reg
, val
);
2769 if (ovalr
!= valr
) {
2770 ret
= snd_soc_write(codec
, mc
->rreg
, valr
);
2777 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx
);
2780 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2782 * @clk_id: DAI specific clock ID
2783 * @freq: new clock frequency in Hz
2784 * @dir: new clock direction - input/output.
2786 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2788 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
2789 unsigned int freq
, int dir
)
2791 if (dai
->driver
&& dai
->driver
->ops
->set_sysclk
)
2792 return dai
->driver
->ops
->set_sysclk(dai
, clk_id
, freq
, dir
);
2796 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
2799 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2801 * @div_id: DAI specific clock divider ID
2802 * @div: new clock divisor.
2804 * Configures the clock dividers. This is used to derive the best DAI bit and
2805 * frame clocks from the system or master clock. It's best to set the DAI bit
2806 * and frame clocks as low as possible to save system power.
2808 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
2809 int div_id
, int div
)
2811 if (dai
->driver
&& dai
->driver
->ops
->set_clkdiv
)
2812 return dai
->driver
->ops
->set_clkdiv(dai
, div_id
, div
);
2816 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
2819 * snd_soc_dai_set_pll - configure DAI PLL.
2821 * @pll_id: DAI specific PLL ID
2822 * @source: DAI specific source for the PLL
2823 * @freq_in: PLL input clock frequency in Hz
2824 * @freq_out: requested PLL output clock frequency in Hz
2826 * Configures and enables PLL to generate output clock based on input clock.
2828 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
2829 unsigned int freq_in
, unsigned int freq_out
)
2831 if (dai
->driver
&& dai
->driver
->ops
->set_pll
)
2832 return dai
->driver
->ops
->set_pll(dai
, pll_id
, source
,
2837 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
2840 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2842 * @fmt: SND_SOC_DAIFMT_ format value.
2844 * Configures the DAI hardware format and clocking.
2846 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
2848 if (dai
->driver
&& dai
->driver
->ops
->set_fmt
)
2849 return dai
->driver
->ops
->set_fmt(dai
, fmt
);
2853 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
2856 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2858 * @tx_mask: bitmask representing active TX slots.
2859 * @rx_mask: bitmask representing active RX slots.
2860 * @slots: Number of slots in use.
2861 * @slot_width: Width in bits for each slot.
2863 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2866 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
2867 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
2869 if (dai
->driver
&& dai
->driver
->ops
->set_tdm_slot
)
2870 return dai
->driver
->ops
->set_tdm_slot(dai
, tx_mask
, rx_mask
,
2875 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
2878 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2880 * @tx_num: how many TX channels
2881 * @tx_slot: pointer to an array which imply the TX slot number channel
2883 * @rx_num: how many RX channels
2884 * @rx_slot: pointer to an array which imply the RX slot number channel
2887 * configure the relationship between channel number and TDM slot number.
2889 int snd_soc_dai_set_channel_map(struct snd_soc_dai
*dai
,
2890 unsigned int tx_num
, unsigned int *tx_slot
,
2891 unsigned int rx_num
, unsigned int *rx_slot
)
2893 if (dai
->driver
&& dai
->driver
->ops
->set_channel_map
)
2894 return dai
->driver
->ops
->set_channel_map(dai
, tx_num
, tx_slot
,
2899 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map
);
2902 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2904 * @tristate: tristate enable
2906 * Tristates the DAI so that others can use it.
2908 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
2910 if (dai
->driver
&& dai
->driver
->ops
->set_tristate
)
2911 return dai
->driver
->ops
->set_tristate(dai
, tristate
);
2915 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
2918 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2920 * @mute: mute enable
2922 * Mutes the DAI DAC.
2924 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
)
2926 if (dai
->driver
&& dai
->driver
->ops
->digital_mute
)
2927 return dai
->driver
->ops
->digital_mute(dai
, mute
);
2931 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
2934 * snd_soc_register_card - Register a card with the ASoC core
2936 * @card: Card to register
2938 * Note that currently this is an internal only function: it will be
2939 * exposed to machine drivers after further backporting of ASoC v2
2940 * registration APIs.
2942 static int snd_soc_register_card(struct snd_soc_card
*card
)
2946 if (!card
->name
|| !card
->dev
)
2949 card
->rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
) * card
->num_links
,
2951 if (card
->rtd
== NULL
)
2954 for (i
= 0; i
< card
->num_links
; i
++)
2955 card
->rtd
[i
].dai_link
= &card
->dai_link
[i
];
2957 INIT_LIST_HEAD(&card
->list
);
2958 card
->instantiated
= 0;
2959 mutex_init(&card
->mutex
);
2961 mutex_lock(&client_mutex
);
2962 list_add(&card
->list
, &card_list
);
2963 snd_soc_instantiate_cards();
2964 mutex_unlock(&client_mutex
);
2966 dev_dbg(card
->dev
, "Registered card '%s'\n", card
->name
);
2972 * snd_soc_unregister_card - Unregister a card with the ASoC core
2974 * @card: Card to unregister
2976 * Note that currently this is an internal only function: it will be
2977 * exposed to machine drivers after further backporting of ASoC v2
2978 * registration APIs.
2980 static int snd_soc_unregister_card(struct snd_soc_card
*card
)
2982 mutex_lock(&client_mutex
);
2983 list_del(&card
->list
);
2984 mutex_unlock(&client_mutex
);
2985 dev_dbg(card
->dev
, "Unregistered card '%s'\n", card
->name
);
2991 * Simplify DAI link configuration by removing ".-1" from device names
2992 * and sanitizing names.
2994 static inline char *fmt_single_name(struct device
*dev
, int *id
)
2996 char *found
, name
[NAME_SIZE
];
2999 if (dev_name(dev
) == NULL
)
3002 strncpy(name
, dev_name(dev
), NAME_SIZE
);
3004 /* are we a "%s.%d" name (platform and SPI components) */
3005 found
= strstr(name
, dev
->driver
->name
);
3008 if (sscanf(&found
[strlen(dev
->driver
->name
)], ".%d", id
) == 1) {
3010 /* discard ID from name if ID == -1 */
3012 found
[strlen(dev
->driver
->name
)] = '\0';
3016 /* I2C component devices are named "bus-addr" */
3017 if (sscanf(name
, "%x-%x", &id1
, &id2
) == 2) {
3018 char tmp
[NAME_SIZE
];
3020 /* create unique ID number from I2C addr and bus */
3021 *id
= ((id1
& 0xffff) << 16) + id2
;
3023 /* sanitize component name for DAI link creation */
3024 snprintf(tmp
, NAME_SIZE
, "%s.%s", dev
->driver
->name
, name
);
3025 strncpy(name
, tmp
, NAME_SIZE
);
3030 return kstrdup(name
, GFP_KERNEL
);
3034 * Simplify DAI link naming for single devices with multiple DAIs by removing
3035 * any ".-1" and using the DAI name (instead of device name).
3037 static inline char *fmt_multiple_name(struct device
*dev
,
3038 struct snd_soc_dai_driver
*dai_drv
)
3040 if (dai_drv
->name
== NULL
) {
3041 printk(KERN_ERR
"asoc: error - multiple DAI %s registered with no name\n",
3046 return kstrdup(dai_drv
->name
, GFP_KERNEL
);
3050 * snd_soc_register_dai - Register a DAI with the ASoC core
3052 * @dai: DAI to register
3054 int snd_soc_register_dai(struct device
*dev
,
3055 struct snd_soc_dai_driver
*dai_drv
)
3057 struct snd_soc_dai
*dai
;
3059 dev_dbg(dev
, "dai register %s\n", dev_name(dev
));
3061 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
3065 /* create DAI component name */
3066 dai
->name
= fmt_single_name(dev
, &dai
->id
);
3067 if (dai
->name
== NULL
) {
3073 dai
->driver
= dai_drv
;
3074 if (!dai
->driver
->ops
)
3075 dai
->driver
->ops
= &null_dai_ops
;
3077 mutex_lock(&client_mutex
);
3078 list_add(&dai
->list
, &dai_list
);
3079 snd_soc_instantiate_cards();
3080 mutex_unlock(&client_mutex
);
3082 pr_debug("Registered DAI '%s'\n", dai
->name
);
3086 EXPORT_SYMBOL_GPL(snd_soc_register_dai
);
3089 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3091 * @dai: DAI to unregister
3093 void snd_soc_unregister_dai(struct device
*dev
)
3095 struct snd_soc_dai
*dai
;
3097 list_for_each_entry(dai
, &dai_list
, list
) {
3098 if (dev
== dai
->dev
)
3104 mutex_lock(&client_mutex
);
3105 list_del(&dai
->list
);
3106 mutex_unlock(&client_mutex
);
3108 pr_debug("Unregistered DAI '%s'\n", dai
->name
);
3112 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai
);
3115 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3117 * @dai: Array of DAIs to register
3118 * @count: Number of DAIs
3120 int snd_soc_register_dais(struct device
*dev
,
3121 struct snd_soc_dai_driver
*dai_drv
, size_t count
)
3123 struct snd_soc_dai
*dai
;
3126 dev_dbg(dev
, "dai register %s #%Zu\n", dev_name(dev
), count
);
3128 for (i
= 0; i
< count
; i
++) {
3130 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
3136 /* create DAI component name */
3137 dai
->name
= fmt_multiple_name(dev
, &dai_drv
[i
]);
3138 if (dai
->name
== NULL
) {
3145 dai
->driver
= &dai_drv
[i
];
3146 if (dai
->driver
->id
)
3147 dai
->id
= dai
->driver
->id
;
3150 if (!dai
->driver
->ops
)
3151 dai
->driver
->ops
= &null_dai_ops
;
3153 mutex_lock(&client_mutex
);
3154 list_add(&dai
->list
, &dai_list
);
3155 mutex_unlock(&client_mutex
);
3157 pr_debug("Registered DAI '%s'\n", dai
->name
);
3160 snd_soc_instantiate_cards();
3164 for (i
--; i
>= 0; i
--)
3165 snd_soc_unregister_dai(dev
);
3169 EXPORT_SYMBOL_GPL(snd_soc_register_dais
);
3172 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3174 * @dai: Array of DAIs to unregister
3175 * @count: Number of DAIs
3177 void snd_soc_unregister_dais(struct device
*dev
, size_t count
)
3181 for (i
= 0; i
< count
; i
++)
3182 snd_soc_unregister_dai(dev
);
3184 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais
);
3187 * snd_soc_register_platform - Register a platform with the ASoC core
3189 * @platform: platform to register
3191 int snd_soc_register_platform(struct device
*dev
,
3192 struct snd_soc_platform_driver
*platform_drv
)
3194 struct snd_soc_platform
*platform
;
3196 dev_dbg(dev
, "platform register %s\n", dev_name(dev
));
3198 platform
= kzalloc(sizeof(struct snd_soc_platform
), GFP_KERNEL
);
3199 if (platform
== NULL
)
3202 /* create platform component name */
3203 platform
->name
= fmt_single_name(dev
, &platform
->id
);
3204 if (platform
->name
== NULL
) {
3209 platform
->dev
= dev
;
3210 platform
->driver
= platform_drv
;
3212 mutex_lock(&client_mutex
);
3213 list_add(&platform
->list
, &platform_list
);
3214 snd_soc_instantiate_cards();
3215 mutex_unlock(&client_mutex
);
3217 pr_debug("Registered platform '%s'\n", platform
->name
);
3221 EXPORT_SYMBOL_GPL(snd_soc_register_platform
);
3224 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3226 * @platform: platform to unregister
3228 void snd_soc_unregister_platform(struct device
*dev
)
3230 struct snd_soc_platform
*platform
;
3232 list_for_each_entry(platform
, &platform_list
, list
) {
3233 if (dev
== platform
->dev
)
3239 mutex_lock(&client_mutex
);
3240 list_del(&platform
->list
);
3241 mutex_unlock(&client_mutex
);
3243 pr_debug("Unregistered platform '%s'\n", platform
->name
);
3244 kfree(platform
->name
);
3247 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform
);
3249 static u64 codec_format_map
[] = {
3250 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
,
3251 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_U16_BE
,
3252 SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S24_BE
,
3253 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_U24_BE
,
3254 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
,
3255 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_U32_BE
,
3256 SNDRV_PCM_FMTBIT_S24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3257 SNDRV_PCM_FMTBIT_U24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3258 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S20_3BE
,
3259 SNDRV_PCM_FMTBIT_U20_3LE
| SNDRV_PCM_FMTBIT_U20_3BE
,
3260 SNDRV_PCM_FMTBIT_S18_3LE
| SNDRV_PCM_FMTBIT_S18_3BE
,
3261 SNDRV_PCM_FMTBIT_U18_3LE
| SNDRV_PCM_FMTBIT_U18_3BE
,
3262 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
,
3263 SNDRV_PCM_FMTBIT_FLOAT64_LE
| SNDRV_PCM_FMTBIT_FLOAT64_BE
,
3264 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3265 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
,
3268 /* Fix up the DAI formats for endianness: codecs don't actually see
3269 * the endianness of the data but we're using the CPU format
3270 * definitions which do need to include endianness so we ensure that
3271 * codec DAIs always have both big and little endian variants set.
3273 static void fixup_codec_formats(struct snd_soc_pcm_stream
*stream
)
3277 for (i
= 0; i
< ARRAY_SIZE(codec_format_map
); i
++)
3278 if (stream
->formats
& codec_format_map
[i
])
3279 stream
->formats
|= codec_format_map
[i
];
3283 * snd_soc_register_codec - Register a codec with the ASoC core
3285 * @codec: codec to register
3287 int snd_soc_register_codec(struct device
*dev
,
3288 struct snd_soc_codec_driver
*codec_drv
,
3289 struct snd_soc_dai_driver
*dai_drv
, int num_dai
)
3291 struct snd_soc_codec
*codec
;
3294 dev_dbg(dev
, "codec register %s\n", dev_name(dev
));
3296 codec
= kzalloc(sizeof(struct snd_soc_codec
), GFP_KERNEL
);
3300 /* create CODEC component name */
3301 codec
->name
= fmt_single_name(dev
, &codec
->id
);
3302 if (codec
->name
== NULL
) {
3307 INIT_LIST_HEAD(&codec
->dapm
.widgets
);
3308 INIT_LIST_HEAD(&codec
->dapm
.paths
);
3309 codec
->dapm
.bias_level
= SND_SOC_BIAS_OFF
;
3310 codec
->dapm
.dev
= dev
;
3311 codec
->dapm
.codec
= codec
;
3313 codec
->driver
= codec_drv
;
3314 codec
->num_dai
= num_dai
;
3315 mutex_init(&codec
->mutex
);
3317 /* allocate CODEC register cache */
3318 if (codec_drv
->reg_cache_size
&& codec_drv
->reg_word_size
) {
3319 ret
= snd_soc_cache_init(codec
);
3321 dev_err(codec
->dev
, "Failed to set cache compression type: %d\n",
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 snd_soc_cache_exit(codec
);
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
);
3387 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec
);
3389 static int __init
snd_soc_init(void)
3391 #ifdef CONFIG_DEBUG_FS
3392 debugfs_root
= debugfs_create_dir("asoc", NULL
);
3393 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
3395 "ASoC: Failed to create debugfs directory\n");
3396 debugfs_root
= NULL
;
3399 if (!debugfs_create_file("codecs", 0444, debugfs_root
, NULL
,
3401 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3403 if (!debugfs_create_file("dais", 0444, debugfs_root
, NULL
,
3405 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3407 if (!debugfs_create_file("platforms", 0444, debugfs_root
, NULL
,
3408 &platform_list_fops
))
3409 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3412 return platform_driver_register(&soc_driver
);
3414 module_init(snd_soc_init
);
3416 static void __exit
snd_soc_exit(void)
3418 #ifdef CONFIG_DEBUG_FS
3419 debugfs_remove_recursive(debugfs_root
);
3421 platform_driver_unregister(&soc_driver
);
3423 module_exit(snd_soc_exit
);
3425 /* Module information */
3426 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3427 MODULE_DESCRIPTION("ALSA SoC Core");
3428 MODULE_LICENSE("GPL");
3429 MODULE_ALIAS("platform:soc-audio");