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 struct dentry
*debugfs_card_root
= codec
->card
->debugfs_card_root
;
243 codec
->debugfs_codec_root
= debugfs_create_dir(codec
->name
,
245 if (!codec
->debugfs_codec_root
) {
247 "ASoC: Failed to create codec debugfs directory\n");
251 codec
->debugfs_reg
= debugfs_create_file("codec_reg", 0644,
252 codec
->debugfs_codec_root
,
253 codec
, &codec_reg_fops
);
254 if (!codec
->debugfs_reg
)
256 "ASoC: Failed to create codec register debugfs file\n");
258 codec
->dapm
.debugfs_dapm
= debugfs_create_dir("dapm",
259 codec
->debugfs_codec_root
);
260 if (!codec
->dapm
.debugfs_dapm
)
262 "Failed to create DAPM debugfs directory\n");
264 snd_soc_dapm_debugfs_init(&codec
->dapm
);
267 static void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
269 debugfs_remove_recursive(codec
->debugfs_codec_root
);
272 static ssize_t
codec_list_read_file(struct file
*file
, char __user
*user_buf
,
273 size_t count
, loff_t
*ppos
)
275 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
276 ssize_t len
, ret
= 0;
277 struct snd_soc_codec
*codec
;
282 list_for_each_entry(codec
, &codec_list
, list
) {
283 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
287 if (ret
> PAGE_SIZE
) {
294 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
301 static const struct file_operations codec_list_fops
= {
302 .read
= codec_list_read_file
,
303 .llseek
= default_llseek
,/* read accesses f_pos */
306 static ssize_t
dai_list_read_file(struct file
*file
, char __user
*user_buf
,
307 size_t count
, loff_t
*ppos
)
309 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
310 ssize_t len
, ret
= 0;
311 struct snd_soc_dai
*dai
;
316 list_for_each_entry(dai
, &dai_list
, list
) {
317 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n", dai
->name
);
320 if (ret
> PAGE_SIZE
) {
326 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
333 static const struct file_operations dai_list_fops
= {
334 .read
= dai_list_read_file
,
335 .llseek
= default_llseek
,/* read accesses f_pos */
338 static ssize_t
platform_list_read_file(struct file
*file
,
339 char __user
*user_buf
,
340 size_t count
, loff_t
*ppos
)
342 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
343 ssize_t len
, ret
= 0;
344 struct snd_soc_platform
*platform
;
349 list_for_each_entry(platform
, &platform_list
, list
) {
350 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
354 if (ret
> PAGE_SIZE
) {
360 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
367 static const struct file_operations platform_list_fops
= {
368 .read
= platform_list_read_file
,
369 .llseek
= default_llseek
,/* read accesses f_pos */
372 static void soc_init_card_debugfs(struct snd_soc_card
*card
)
374 card
->debugfs_card_root
= debugfs_create_dir(card
->name
,
376 if (!card
->debugfs_card_root
) {
378 "ASoC: Failed to create codec debugfs directory\n");
382 card
->debugfs_pop_time
= debugfs_create_u32("dapm_pop_time", 0644,
383 card
->debugfs_card_root
,
385 if (!card
->debugfs_pop_time
)
387 "Failed to create pop time debugfs file\n");
390 static void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
392 debugfs_remove_recursive(card
->debugfs_card_root
);
397 static inline void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
401 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
406 #ifdef CONFIG_SND_SOC_AC97_BUS
407 /* unregister ac97 codec */
408 static int soc_ac97_dev_unregister(struct snd_soc_codec
*codec
)
410 if (codec
->ac97
->dev
.bus
)
411 device_unregister(&codec
->ac97
->dev
);
415 /* stop no dev release warning */
416 static void soc_ac97_device_release(struct device
*dev
){}
418 /* register ac97 codec to bus */
419 static int soc_ac97_dev_register(struct snd_soc_codec
*codec
)
423 codec
->ac97
->dev
.bus
= &ac97_bus_type
;
424 codec
->ac97
->dev
.parent
= codec
->card
->dev
;
425 codec
->ac97
->dev
.release
= soc_ac97_device_release
;
427 dev_set_name(&codec
->ac97
->dev
, "%d-%d:%s",
428 codec
->card
->snd_card
->number
, 0, codec
->name
);
429 err
= device_register(&codec
->ac97
->dev
);
431 snd_printk(KERN_ERR
"Can't register ac97 bus\n");
432 codec
->ac97
->dev
.bus
= NULL
;
439 static int soc_pcm_apply_symmetry(struct snd_pcm_substream
*substream
)
441 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
442 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
443 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
446 if (codec_dai
->driver
->symmetric_rates
|| cpu_dai
->driver
->symmetric_rates
||
447 rtd
->dai_link
->symmetric_rates
) {
448 dev_dbg(&rtd
->dev
, "Symmetry forces %dHz rate\n",
451 ret
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
452 SNDRV_PCM_HW_PARAM_RATE
,
457 "Unable to apply rate symmetry constraint: %d\n", ret
);
466 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
467 * then initialized and any private data can be allocated. This also calls
468 * startup for the cpu DAI, platform, machine and codec DAI.
470 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
472 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
473 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
474 struct snd_soc_platform
*platform
= rtd
->platform
;
475 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
476 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
477 struct snd_soc_dai_driver
*cpu_dai_drv
= cpu_dai
->driver
;
478 struct snd_soc_dai_driver
*codec_dai_drv
= codec_dai
->driver
;
481 mutex_lock(&pcm_mutex
);
483 /* startup the audio subsystem */
484 if (cpu_dai
->driver
->ops
->startup
) {
485 ret
= cpu_dai
->driver
->ops
->startup(substream
, cpu_dai
);
487 printk(KERN_ERR
"asoc: can't open interface %s\n",
493 if (platform
->driver
->ops
->open
) {
494 ret
= platform
->driver
->ops
->open(substream
);
496 printk(KERN_ERR
"asoc: can't open platform %s\n", platform
->name
);
501 if (codec_dai
->driver
->ops
->startup
) {
502 ret
= codec_dai
->driver
->ops
->startup(substream
, codec_dai
);
504 printk(KERN_ERR
"asoc: can't open codec %s\n",
510 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->startup
) {
511 ret
= rtd
->dai_link
->ops
->startup(substream
);
513 printk(KERN_ERR
"asoc: %s startup failed\n", rtd
->dai_link
->name
);
518 /* Check that the codec and cpu DAI's are compatible */
519 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
520 runtime
->hw
.rate_min
=
521 max(codec_dai_drv
->playback
.rate_min
,
522 cpu_dai_drv
->playback
.rate_min
);
523 runtime
->hw
.rate_max
=
524 min(codec_dai_drv
->playback
.rate_max
,
525 cpu_dai_drv
->playback
.rate_max
);
526 runtime
->hw
.channels_min
=
527 max(codec_dai_drv
->playback
.channels_min
,
528 cpu_dai_drv
->playback
.channels_min
);
529 runtime
->hw
.channels_max
=
530 min(codec_dai_drv
->playback
.channels_max
,
531 cpu_dai_drv
->playback
.channels_max
);
532 runtime
->hw
.formats
=
533 codec_dai_drv
->playback
.formats
& cpu_dai_drv
->playback
.formats
;
535 codec_dai_drv
->playback
.rates
& cpu_dai_drv
->playback
.rates
;
536 if (codec_dai_drv
->playback
.rates
537 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
538 runtime
->hw
.rates
|= cpu_dai_drv
->playback
.rates
;
539 if (cpu_dai_drv
->playback
.rates
540 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
541 runtime
->hw
.rates
|= codec_dai_drv
->playback
.rates
;
543 runtime
->hw
.rate_min
=
544 max(codec_dai_drv
->capture
.rate_min
,
545 cpu_dai_drv
->capture
.rate_min
);
546 runtime
->hw
.rate_max
=
547 min(codec_dai_drv
->capture
.rate_max
,
548 cpu_dai_drv
->capture
.rate_max
);
549 runtime
->hw
.channels_min
=
550 max(codec_dai_drv
->capture
.channels_min
,
551 cpu_dai_drv
->capture
.channels_min
);
552 runtime
->hw
.channels_max
=
553 min(codec_dai_drv
->capture
.channels_max
,
554 cpu_dai_drv
->capture
.channels_max
);
555 runtime
->hw
.formats
=
556 codec_dai_drv
->capture
.formats
& cpu_dai_drv
->capture
.formats
;
558 codec_dai_drv
->capture
.rates
& cpu_dai_drv
->capture
.rates
;
559 if (codec_dai_drv
->capture
.rates
560 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
561 runtime
->hw
.rates
|= cpu_dai_drv
->capture
.rates
;
562 if (cpu_dai_drv
->capture
.rates
563 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
564 runtime
->hw
.rates
|= codec_dai_drv
->capture
.rates
;
567 snd_pcm_limit_hw_rates(runtime
);
568 if (!runtime
->hw
.rates
) {
569 printk(KERN_ERR
"asoc: %s <-> %s No matching rates\n",
570 codec_dai
->name
, cpu_dai
->name
);
573 if (!runtime
->hw
.formats
) {
574 printk(KERN_ERR
"asoc: %s <-> %s No matching formats\n",
575 codec_dai
->name
, cpu_dai
->name
);
578 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
) {
579 printk(KERN_ERR
"asoc: %s <-> %s No matching channels\n",
580 codec_dai
->name
, cpu_dai
->name
);
584 /* Symmetry only applies if we've already got an active stream. */
585 if (cpu_dai
->active
|| codec_dai
->active
) {
586 ret
= soc_pcm_apply_symmetry(substream
);
591 pr_debug("asoc: %s <-> %s info:\n",
592 codec_dai
->name
, cpu_dai
->name
);
593 pr_debug("asoc: rate mask 0x%x\n", runtime
->hw
.rates
);
594 pr_debug("asoc: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
595 runtime
->hw
.channels_max
);
596 pr_debug("asoc: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
597 runtime
->hw
.rate_max
);
599 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
600 cpu_dai
->playback_active
++;
601 codec_dai
->playback_active
++;
603 cpu_dai
->capture_active
++;
604 codec_dai
->capture_active
++;
608 rtd
->codec
->active
++;
609 mutex_unlock(&pcm_mutex
);
613 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
614 rtd
->dai_link
->ops
->shutdown(substream
);
617 if (codec_dai
->driver
->ops
->shutdown
)
618 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
621 if (platform
->driver
->ops
->close
)
622 platform
->driver
->ops
->close(substream
);
625 if (cpu_dai
->driver
->ops
->shutdown
)
626 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
628 mutex_unlock(&pcm_mutex
);
633 * Power down the audio subsystem pmdown_time msecs after close is called.
634 * This is to ensure there are no pops or clicks in between any music tracks
635 * due to DAPM power cycling.
637 static void close_delayed_work(struct work_struct
*work
)
639 struct snd_soc_pcm_runtime
*rtd
=
640 container_of(work
, struct snd_soc_pcm_runtime
, delayed_work
.work
);
641 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
643 mutex_lock(&pcm_mutex
);
645 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
646 codec_dai
->driver
->playback
.stream_name
,
647 codec_dai
->playback_active
? "active" : "inactive",
648 codec_dai
->pop_wait
? "yes" : "no");
650 /* are we waiting on this codec DAI stream */
651 if (codec_dai
->pop_wait
== 1) {
652 codec_dai
->pop_wait
= 0;
653 snd_soc_dapm_stream_event(rtd
,
654 codec_dai
->driver
->playback
.stream_name
,
655 SND_SOC_DAPM_STREAM_STOP
);
658 mutex_unlock(&pcm_mutex
);
662 * Called by ALSA when a PCM substream is closed. Private data can be
663 * freed here. The cpu DAI, codec DAI, machine and platform are also
666 static int soc_codec_close(struct snd_pcm_substream
*substream
)
668 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
669 struct snd_soc_platform
*platform
= rtd
->platform
;
670 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
671 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
672 struct snd_soc_codec
*codec
= rtd
->codec
;
674 mutex_lock(&pcm_mutex
);
676 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
677 cpu_dai
->playback_active
--;
678 codec_dai
->playback_active
--;
680 cpu_dai
->capture_active
--;
681 codec_dai
->capture_active
--;
688 /* Muting the DAC suppresses artifacts caused during digital
689 * shutdown, for example from stopping clocks.
691 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
692 snd_soc_dai_digital_mute(codec_dai
, 1);
694 if (cpu_dai
->driver
->ops
->shutdown
)
695 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
697 if (codec_dai
->driver
->ops
->shutdown
)
698 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
700 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
701 rtd
->dai_link
->ops
->shutdown(substream
);
703 if (platform
->driver
->ops
->close
)
704 platform
->driver
->ops
->close(substream
);
705 cpu_dai
->runtime
= NULL
;
707 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
708 /* start delayed pop wq here for playback streams */
709 codec_dai
->pop_wait
= 1;
710 schedule_delayed_work(&rtd
->delayed_work
,
711 msecs_to_jiffies(rtd
->pmdown_time
));
713 /* capture streams can be powered down now */
714 snd_soc_dapm_stream_event(rtd
,
715 codec_dai
->driver
->capture
.stream_name
,
716 SND_SOC_DAPM_STREAM_STOP
);
719 mutex_unlock(&pcm_mutex
);
724 * Called by ALSA when the PCM substream is prepared, can set format, sample
725 * rate, etc. This function is non atomic and can be called multiple times,
726 * it can refer to the runtime info.
728 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
730 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
731 struct snd_soc_platform
*platform
= rtd
->platform
;
732 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
733 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
736 mutex_lock(&pcm_mutex
);
738 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->prepare
) {
739 ret
= rtd
->dai_link
->ops
->prepare(substream
);
741 printk(KERN_ERR
"asoc: machine prepare error\n");
746 if (platform
->driver
->ops
->prepare
) {
747 ret
= platform
->driver
->ops
->prepare(substream
);
749 printk(KERN_ERR
"asoc: platform prepare error\n");
754 if (codec_dai
->driver
->ops
->prepare
) {
755 ret
= codec_dai
->driver
->ops
->prepare(substream
, codec_dai
);
757 printk(KERN_ERR
"asoc: codec DAI prepare error\n");
762 if (cpu_dai
->driver
->ops
->prepare
) {
763 ret
= cpu_dai
->driver
->ops
->prepare(substream
, cpu_dai
);
765 printk(KERN_ERR
"asoc: cpu DAI prepare error\n");
770 /* cancel any delayed stream shutdown that is pending */
771 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
772 codec_dai
->pop_wait
) {
773 codec_dai
->pop_wait
= 0;
774 cancel_delayed_work(&rtd
->delayed_work
);
777 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
778 snd_soc_dapm_stream_event(rtd
,
779 codec_dai
->driver
->playback
.stream_name
,
780 SND_SOC_DAPM_STREAM_START
);
782 snd_soc_dapm_stream_event(rtd
,
783 codec_dai
->driver
->capture
.stream_name
,
784 SND_SOC_DAPM_STREAM_START
);
786 snd_soc_dai_digital_mute(codec_dai
, 0);
789 mutex_unlock(&pcm_mutex
);
794 * Called by ALSA when the hardware params are set by application. This
795 * function can also be called multiple times and can allocate buffers
796 * (using snd_pcm_lib_* ). It's non-atomic.
798 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
799 struct snd_pcm_hw_params
*params
)
801 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
802 struct snd_soc_platform
*platform
= rtd
->platform
;
803 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
804 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
807 mutex_lock(&pcm_mutex
);
809 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_params
) {
810 ret
= rtd
->dai_link
->ops
->hw_params(substream
, params
);
812 printk(KERN_ERR
"asoc: machine hw_params failed\n");
817 if (codec_dai
->driver
->ops
->hw_params
) {
818 ret
= codec_dai
->driver
->ops
->hw_params(substream
, params
, codec_dai
);
820 printk(KERN_ERR
"asoc: can't set codec %s hw params\n",
826 if (cpu_dai
->driver
->ops
->hw_params
) {
827 ret
= cpu_dai
->driver
->ops
->hw_params(substream
, params
, cpu_dai
);
829 printk(KERN_ERR
"asoc: interface %s hw params failed\n",
835 if (platform
->driver
->ops
->hw_params
) {
836 ret
= platform
->driver
->ops
->hw_params(substream
, params
);
838 printk(KERN_ERR
"asoc: platform %s hw params failed\n",
844 rtd
->rate
= params_rate(params
);
847 mutex_unlock(&pcm_mutex
);
851 if (cpu_dai
->driver
->ops
->hw_free
)
852 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
855 if (codec_dai
->driver
->ops
->hw_free
)
856 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
859 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
860 rtd
->dai_link
->ops
->hw_free(substream
);
862 mutex_unlock(&pcm_mutex
);
867 * Free's resources allocated by hw_params, can be called multiple times
869 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
871 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
872 struct snd_soc_platform
*platform
= rtd
->platform
;
873 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
874 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
875 struct snd_soc_codec
*codec
= rtd
->codec
;
877 mutex_lock(&pcm_mutex
);
879 /* apply codec digital mute */
881 snd_soc_dai_digital_mute(codec_dai
, 1);
883 /* free any machine hw params */
884 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
885 rtd
->dai_link
->ops
->hw_free(substream
);
887 /* free any DMA resources */
888 if (platform
->driver
->ops
->hw_free
)
889 platform
->driver
->ops
->hw_free(substream
);
891 /* now free hw params for the DAI's */
892 if (codec_dai
->driver
->ops
->hw_free
)
893 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
895 if (cpu_dai
->driver
->ops
->hw_free
)
896 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
898 mutex_unlock(&pcm_mutex
);
902 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
904 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
905 struct snd_soc_platform
*platform
= rtd
->platform
;
906 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
907 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
910 if (codec_dai
->driver
->ops
->trigger
) {
911 ret
= codec_dai
->driver
->ops
->trigger(substream
, cmd
, codec_dai
);
916 if (platform
->driver
->ops
->trigger
) {
917 ret
= platform
->driver
->ops
->trigger(substream
, cmd
);
922 if (cpu_dai
->driver
->ops
->trigger
) {
923 ret
= cpu_dai
->driver
->ops
->trigger(substream
, cmd
, cpu_dai
);
931 * soc level wrapper for pointer callback
932 * If cpu_dai, codec_dai, platform driver has the delay callback, than
933 * the runtime->delay will be updated accordingly.
935 static snd_pcm_uframes_t
soc_pcm_pointer(struct snd_pcm_substream
*substream
)
937 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
938 struct snd_soc_platform
*platform
= rtd
->platform
;
939 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
940 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
941 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
942 snd_pcm_uframes_t offset
= 0;
943 snd_pcm_sframes_t delay
= 0;
945 if (platform
->driver
->ops
->pointer
)
946 offset
= platform
->driver
->ops
->pointer(substream
);
948 if (cpu_dai
->driver
->ops
->delay
)
949 delay
+= cpu_dai
->driver
->ops
->delay(substream
, cpu_dai
);
951 if (codec_dai
->driver
->ops
->delay
)
952 delay
+= codec_dai
->driver
->ops
->delay(substream
, codec_dai
);
954 if (platform
->driver
->delay
)
955 delay
+= platform
->driver
->delay(substream
, codec_dai
);
957 runtime
->delay
= delay
;
962 /* ASoC PCM operations */
963 static struct snd_pcm_ops soc_pcm_ops
= {
964 .open
= soc_pcm_open
,
965 .close
= soc_codec_close
,
966 .hw_params
= soc_pcm_hw_params
,
967 .hw_free
= soc_pcm_hw_free
,
968 .prepare
= soc_pcm_prepare
,
969 .trigger
= soc_pcm_trigger
,
970 .pointer
= soc_pcm_pointer
,
974 /* powers down audio subsystem for suspend */
975 static int soc_suspend(struct device
*dev
)
977 struct platform_device
*pdev
= to_platform_device(dev
);
978 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
981 /* If the initialization of this soc device failed, there is no codec
982 * associated with it. Just bail out in this case.
984 if (list_empty(&card
->codec_dev_list
))
987 /* Due to the resume being scheduled into a workqueue we could
988 * suspend before that's finished - wait for it to complete.
990 snd_power_lock(card
->snd_card
);
991 snd_power_wait(card
->snd_card
, SNDRV_CTL_POWER_D0
);
992 snd_power_unlock(card
->snd_card
);
994 /* we're going to block userspace touching us until resume completes */
995 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D3hot
);
997 /* mute any active DAC's */
998 for (i
= 0; i
< card
->num_rtd
; i
++) {
999 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
1000 struct snd_soc_dai_driver
*drv
= dai
->driver
;
1002 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1005 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
1006 drv
->ops
->digital_mute(dai
, 1);
1009 /* suspend all pcms */
1010 for (i
= 0; i
< card
->num_rtd
; i
++) {
1011 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1014 snd_pcm_suspend_all(card
->rtd
[i
].pcm
);
1017 if (card
->suspend_pre
)
1018 card
->suspend_pre(pdev
, PMSG_SUSPEND
);
1020 for (i
= 0; i
< card
->num_rtd
; i
++) {
1021 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1022 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1024 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1027 if (cpu_dai
->driver
->suspend
&& !cpu_dai
->driver
->ac97_control
)
1028 cpu_dai
->driver
->suspend(cpu_dai
);
1029 if (platform
->driver
->suspend
&& !platform
->suspended
) {
1030 platform
->driver
->suspend(cpu_dai
);
1031 platform
->suspended
= 1;
1035 /* close any waiting streams and save state */
1036 for (i
= 0; i
< card
->num_rtd
; i
++) {
1037 run_delayed_work(&card
->rtd
[i
].delayed_work
);
1038 card
->rtd
[i
].codec
->dapm
.suspend_bias_level
= card
->rtd
[i
].codec
->dapm
.bias_level
;
1041 for (i
= 0; i
< card
->num_rtd
; i
++) {
1042 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1044 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1047 if (driver
->playback
.stream_name
!= NULL
)
1048 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1049 SND_SOC_DAPM_STREAM_SUSPEND
);
1051 if (driver
->capture
.stream_name
!= NULL
)
1052 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1053 SND_SOC_DAPM_STREAM_SUSPEND
);
1056 /* suspend all CODECs */
1057 for (i
= 0; i
< card
->num_rtd
; i
++) {
1058 struct snd_soc_codec
*codec
= card
->rtd
[i
].codec
;
1059 /* If there are paths active then the CODEC will be held with
1060 * bias _ON and should not be suspended. */
1061 if (!codec
->suspended
&& codec
->driver
->suspend
) {
1062 switch (codec
->dapm
.bias_level
) {
1063 case SND_SOC_BIAS_STANDBY
:
1064 case SND_SOC_BIAS_OFF
:
1065 codec
->driver
->suspend(codec
, PMSG_SUSPEND
);
1066 codec
->suspended
= 1;
1069 dev_dbg(codec
->dev
, "CODEC is on over suspend\n");
1075 for (i
= 0; i
< card
->num_rtd
; i
++) {
1076 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1078 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1081 if (cpu_dai
->driver
->suspend
&& cpu_dai
->driver
->ac97_control
)
1082 cpu_dai
->driver
->suspend(cpu_dai
);
1085 if (card
->suspend_post
)
1086 card
->suspend_post(pdev
, PMSG_SUSPEND
);
1091 /* deferred resume work, so resume can complete before we finished
1092 * setting our codec back up, which can be very slow on I2C
1094 static void soc_resume_deferred(struct work_struct
*work
)
1096 struct snd_soc_card
*card
=
1097 container_of(work
, struct snd_soc_card
, deferred_resume_work
);
1098 struct platform_device
*pdev
= to_platform_device(card
->dev
);
1101 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1102 * so userspace apps are blocked from touching us
1105 dev_dbg(card
->dev
, "starting resume work\n");
1107 /* Bring us up into D2 so that DAPM starts enabling things */
1108 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D2
);
1110 if (card
->resume_pre
)
1111 card
->resume_pre(pdev
);
1113 /* resume AC97 DAIs */
1114 for (i
= 0; i
< card
->num_rtd
; i
++) {
1115 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1117 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1120 if (cpu_dai
->driver
->resume
&& cpu_dai
->driver
->ac97_control
)
1121 cpu_dai
->driver
->resume(cpu_dai
);
1124 for (i
= 0; i
< card
->num_rtd
; i
++) {
1125 struct snd_soc_codec
*codec
= card
->rtd
[i
].codec
;
1126 /* If the CODEC was idle over suspend then it will have been
1127 * left with bias OFF or STANDBY and suspended so we must now
1128 * resume. Otherwise the suspend was suppressed.
1130 if (codec
->driver
->resume
&& codec
->suspended
) {
1131 switch (codec
->dapm
.bias_level
) {
1132 case SND_SOC_BIAS_STANDBY
:
1133 case SND_SOC_BIAS_OFF
:
1134 codec
->driver
->resume(codec
);
1135 codec
->suspended
= 0;
1138 dev_dbg(codec
->dev
, "CODEC was on over suspend\n");
1144 for (i
= 0; i
< card
->num_rtd
; i
++) {
1145 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1147 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1150 if (driver
->playback
.stream_name
!= NULL
)
1151 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1152 SND_SOC_DAPM_STREAM_RESUME
);
1154 if (driver
->capture
.stream_name
!= NULL
)
1155 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1156 SND_SOC_DAPM_STREAM_RESUME
);
1159 /* unmute any active DACs */
1160 for (i
= 0; i
< card
->num_rtd
; i
++) {
1161 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
1162 struct snd_soc_dai_driver
*drv
= dai
->driver
;
1164 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1167 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
1168 drv
->ops
->digital_mute(dai
, 0);
1171 for (i
= 0; i
< card
->num_rtd
; i
++) {
1172 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1173 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1175 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1178 if (cpu_dai
->driver
->resume
&& !cpu_dai
->driver
->ac97_control
)
1179 cpu_dai
->driver
->resume(cpu_dai
);
1180 if (platform
->driver
->resume
&& platform
->suspended
) {
1181 platform
->driver
->resume(cpu_dai
);
1182 platform
->suspended
= 0;
1186 if (card
->resume_post
)
1187 card
->resume_post(pdev
);
1189 dev_dbg(card
->dev
, "resume work completed\n");
1191 /* userspace can access us now we are back as we were before */
1192 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D0
);
1195 /* powers up audio subsystem after a suspend */
1196 static int soc_resume(struct device
*dev
)
1198 struct platform_device
*pdev
= to_platform_device(dev
);
1199 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1202 /* AC97 devices might have other drivers hanging off them so
1203 * need to resume immediately. Other drivers don't have that
1204 * problem and may take a substantial amount of time to resume
1205 * due to I/O costs and anti-pop so handle them out of line.
1207 for (i
= 0; i
< card
->num_rtd
; i
++) {
1208 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1209 if (cpu_dai
->driver
->ac97_control
) {
1210 dev_dbg(dev
, "Resuming AC97 immediately\n");
1211 soc_resume_deferred(&card
->deferred_resume_work
);
1213 dev_dbg(dev
, "Scheduling resume work\n");
1214 if (!schedule_work(&card
->deferred_resume_work
))
1215 dev_err(dev
, "resume work item may be lost\n");
1222 #define soc_suspend NULL
1223 #define soc_resume NULL
1226 static struct snd_soc_dai_ops null_dai_ops
= {
1229 static int soc_bind_dai_link(struct snd_soc_card
*card
, int num
)
1231 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1232 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1233 struct snd_soc_codec
*codec
;
1234 struct snd_soc_platform
*platform
;
1235 struct snd_soc_dai
*codec_dai
, *cpu_dai
;
1239 dev_dbg(card
->dev
, "binding %s at idx %d\n", dai_link
->name
, num
);
1241 /* do we already have the CPU DAI for this link ? */
1245 /* no, then find CPU DAI from registered DAIs*/
1246 list_for_each_entry(cpu_dai
, &dai_list
, list
) {
1247 if (!strcmp(cpu_dai
->name
, dai_link
->cpu_dai_name
)) {
1249 if (!try_module_get(cpu_dai
->dev
->driver
->owner
))
1252 rtd
->cpu_dai
= cpu_dai
;
1256 dev_dbg(card
->dev
, "CPU DAI %s not registered\n",
1257 dai_link
->cpu_dai_name
);
1260 /* do we already have the CODEC for this link ? */
1265 /* no, then find CODEC from registered CODECs*/
1266 list_for_each_entry(codec
, &codec_list
, list
) {
1267 if (!strcmp(codec
->name
, dai_link
->codec_name
)) {
1270 if (!try_module_get(codec
->dev
->driver
->owner
))
1273 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1274 list_for_each_entry(codec_dai
, &dai_list
, list
) {
1275 if (codec
->dev
== codec_dai
->dev
&&
1276 !strcmp(codec_dai
->name
, dai_link
->codec_dai_name
)) {
1277 rtd
->codec_dai
= codec_dai
;
1281 dev_dbg(card
->dev
, "CODEC DAI %s not registered\n",
1282 dai_link
->codec_dai_name
);
1287 dev_dbg(card
->dev
, "CODEC %s not registered\n",
1288 dai_link
->codec_name
);
1291 /* do we already have the CODEC DAI for this link ? */
1292 if (rtd
->platform
) {
1295 /* no, then find CPU DAI from registered DAIs*/
1296 list_for_each_entry(platform
, &platform_list
, list
) {
1297 if (!strcmp(platform
->name
, dai_link
->platform_name
)) {
1299 if (!try_module_get(platform
->dev
->driver
->owner
))
1302 rtd
->platform
= platform
;
1307 dev_dbg(card
->dev
, "platform %s not registered\n",
1308 dai_link
->platform_name
);
1312 /* mark rtd as complete if we found all 4 of our client devices */
1313 if (rtd
->codec
&& rtd
->codec_dai
&& rtd
->platform
&& rtd
->cpu_dai
) {
1320 static void soc_remove_dai_link(struct snd_soc_card
*card
, int num
)
1322 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1323 struct snd_soc_codec
*codec
= rtd
->codec
;
1324 struct snd_soc_platform
*platform
= rtd
->platform
;
1325 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1328 /* unregister the rtd device */
1329 if (rtd
->dev_registered
) {
1330 device_remove_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1331 device_unregister(&rtd
->dev
);
1332 rtd
->dev_registered
= 0;
1335 /* remove the CODEC DAI */
1336 if (codec_dai
&& codec_dai
->probed
) {
1337 if (codec_dai
->driver
->remove
) {
1338 err
= codec_dai
->driver
->remove(codec_dai
);
1340 printk(KERN_ERR
"asoc: failed to remove %s\n", codec_dai
->name
);
1342 codec_dai
->probed
= 0;
1343 list_del(&codec_dai
->card_list
);
1346 /* remove the platform */
1347 if (platform
&& platform
->probed
) {
1348 if (platform
->driver
->remove
) {
1349 err
= platform
->driver
->remove(platform
);
1351 printk(KERN_ERR
"asoc: failed to remove %s\n", platform
->name
);
1353 platform
->probed
= 0;
1354 list_del(&platform
->card_list
);
1355 module_put(platform
->dev
->driver
->owner
);
1358 /* remove the CODEC */
1359 if (codec
&& codec
->probed
) {
1360 if (codec
->driver
->remove
) {
1361 err
= codec
->driver
->remove(codec
);
1363 printk(KERN_ERR
"asoc: failed to remove %s\n", codec
->name
);
1366 /* Make sure all DAPM widgets are freed */
1367 snd_soc_dapm_free(&codec
->dapm
);
1369 soc_cleanup_codec_debugfs(codec
);
1370 device_remove_file(&rtd
->dev
, &dev_attr_codec_reg
);
1372 list_del(&codec
->card_list
);
1373 module_put(codec
->dev
->driver
->owner
);
1376 /* remove the cpu_dai */
1377 if (cpu_dai
&& cpu_dai
->probed
) {
1378 if (cpu_dai
->driver
->remove
) {
1379 err
= cpu_dai
->driver
->remove(cpu_dai
);
1381 printk(KERN_ERR
"asoc: failed to remove %s\n", cpu_dai
->name
);
1383 cpu_dai
->probed
= 0;
1384 list_del(&cpu_dai
->card_list
);
1385 module_put(cpu_dai
->dev
->driver
->owner
);
1389 static void rtd_release(struct device
*dev
) {}
1391 static int soc_probe_dai_link(struct snd_soc_card
*card
, int num
)
1393 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1394 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1395 struct snd_soc_codec
*codec
= rtd
->codec
;
1396 struct snd_soc_platform
*platform
= rtd
->platform
;
1397 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1400 dev_dbg(card
->dev
, "probe %s dai link %d\n", card
->name
, num
);
1402 /* config components */
1403 codec_dai
->codec
= codec
;
1405 cpu_dai
->platform
= platform
;
1407 rtd
->dev
.parent
= card
->dev
;
1408 codec_dai
->card
= card
;
1409 cpu_dai
->card
= card
;
1411 /* set default power off timeout */
1412 rtd
->pmdown_time
= pmdown_time
;
1414 /* probe the cpu_dai */
1415 if (!cpu_dai
->probed
) {
1416 if (cpu_dai
->driver
->probe
) {
1417 ret
= cpu_dai
->driver
->probe(cpu_dai
);
1419 printk(KERN_ERR
"asoc: failed to probe CPU DAI %s\n",
1424 cpu_dai
->probed
= 1;
1425 /* mark cpu_dai as probed and add to card cpu_dai list */
1426 list_add(&cpu_dai
->card_list
, &card
->dai_dev_list
);
1429 /* probe the CODEC */
1430 if (!codec
->probed
) {
1431 codec
->dapm
.card
= card
;
1432 if (codec
->driver
->probe
) {
1433 ret
= codec
->driver
->probe(codec
);
1435 printk(KERN_ERR
"asoc: failed to probe CODEC %s\n",
1441 soc_init_codec_debugfs(codec
);
1443 /* mark codec as probed and add to card codec list */
1445 list_add(&codec
->card_list
, &card
->codec_dev_list
);
1448 /* probe the platform */
1449 if (!platform
->probed
) {
1450 if (platform
->driver
->probe
) {
1451 ret
= platform
->driver
->probe(platform
);
1453 printk(KERN_ERR
"asoc: failed to probe platform %s\n",
1458 /* mark platform as probed and add to card platform list */
1459 platform
->probed
= 1;
1460 list_add(&platform
->card_list
, &card
->platform_dev_list
);
1463 /* probe the CODEC DAI */
1464 if (!codec_dai
->probed
) {
1465 if (codec_dai
->driver
->probe
) {
1466 ret
= codec_dai
->driver
->probe(codec_dai
);
1468 printk(KERN_ERR
"asoc: failed to probe CODEC DAI %s\n",
1474 /* mark cpu_dai as probed and add to card cpu_dai list */
1475 codec_dai
->probed
= 1;
1476 list_add(&codec_dai
->card_list
, &card
->dai_dev_list
);
1479 /* DAPM dai link stream work */
1480 INIT_DELAYED_WORK(&rtd
->delayed_work
, close_delayed_work
);
1482 /* now that all clients have probed, initialise the DAI link */
1483 if (dai_link
->init
) {
1484 ret
= dai_link
->init(rtd
);
1486 printk(KERN_ERR
"asoc: failed to init %s\n", dai_link
->stream_name
);
1491 /* Make sure all DAPM widgets are instantiated */
1492 snd_soc_dapm_new_widgets(&codec
->dapm
);
1493 snd_soc_dapm_sync(&codec
->dapm
);
1495 /* register the rtd device */
1496 rtd
->dev
.release
= rtd_release
;
1497 rtd
->dev
.init_name
= dai_link
->name
;
1498 ret
= device_register(&rtd
->dev
);
1500 printk(KERN_ERR
"asoc: failed to register DAI runtime device %d\n", ret
);
1504 rtd
->dev_registered
= 1;
1505 ret
= device_create_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1507 printk(KERN_WARNING
"asoc: failed to add pmdown_time sysfs\n");
1509 /* add DAPM sysfs entries for this codec */
1510 ret
= snd_soc_dapm_sys_add(&rtd
->dev
);
1512 printk(KERN_WARNING
"asoc: failed to add codec dapm sysfs entries\n");
1514 /* add codec sysfs entries */
1515 ret
= device_create_file(&rtd
->dev
, &dev_attr_codec_reg
);
1517 printk(KERN_WARNING
"asoc: failed to add codec sysfs files\n");
1519 /* create the pcm */
1520 ret
= soc_new_pcm(rtd
, num
);
1522 printk(KERN_ERR
"asoc: can't create pcm %s\n", dai_link
->stream_name
);
1526 /* add platform data for AC97 devices */
1527 if (rtd
->codec_dai
->driver
->ac97_control
)
1528 snd_ac97_dev_add_pdata(codec
->ac97
, rtd
->cpu_dai
->ac97_pdata
);
1533 #ifdef CONFIG_SND_SOC_AC97_BUS
1534 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime
*rtd
)
1538 /* Only instantiate AC97 if not already done by the adaptor
1539 * for the generic AC97 subsystem.
1541 if (rtd
->codec_dai
->driver
->ac97_control
&& !rtd
->codec
->ac97_registered
) {
1543 * It is possible that the AC97 device is already registered to
1544 * the device subsystem. This happens when the device is created
1545 * via snd_ac97_mixer(). Currently only SoC codec that does so
1546 * is the generic AC97 glue but others migh emerge.
1548 * In those cases we don't try to register the device again.
1550 if (!rtd
->codec
->ac97_created
)
1553 ret
= soc_ac97_dev_register(rtd
->codec
);
1555 printk(KERN_ERR
"asoc: AC97 device register failed\n");
1559 rtd
->codec
->ac97_registered
= 1;
1564 static void soc_unregister_ac97_dai_link(struct snd_soc_codec
*codec
)
1566 if (codec
->ac97_registered
) {
1567 soc_ac97_dev_unregister(codec
);
1568 codec
->ac97_registered
= 0;
1573 static void snd_soc_instantiate_card(struct snd_soc_card
*card
)
1575 struct platform_device
*pdev
= to_platform_device(card
->dev
);
1578 mutex_lock(&card
->mutex
);
1580 if (card
->instantiated
) {
1581 mutex_unlock(&card
->mutex
);
1586 for (i
= 0; i
< card
->num_links
; i
++)
1587 soc_bind_dai_link(card
, i
);
1589 /* bind completed ? */
1590 if (card
->num_rtd
!= card
->num_links
) {
1591 mutex_unlock(&card
->mutex
);
1595 /* card bind complete so register a sound card */
1596 ret
= snd_card_create(SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
1597 card
->owner
, 0, &card
->snd_card
);
1599 printk(KERN_ERR
"asoc: can't create sound card for card %s\n",
1601 mutex_unlock(&card
->mutex
);
1604 card
->snd_card
->dev
= card
->dev
;
1607 /* deferred resume work */
1608 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
1611 /* initialise the sound card only once */
1613 ret
= card
->probe(pdev
);
1615 goto card_probe_error
;
1618 for (i
= 0; i
< card
->num_links
; i
++) {
1619 ret
= soc_probe_dai_link(card
, i
);
1621 pr_err("asoc: failed to instantiate card %s: %d\n",
1627 snprintf(card
->snd_card
->shortname
, sizeof(card
->snd_card
->shortname
),
1629 snprintf(card
->snd_card
->longname
, sizeof(card
->snd_card
->longname
),
1632 ret
= snd_card_register(card
->snd_card
);
1634 printk(KERN_ERR
"asoc: failed to register soundcard for %s\n", card
->name
);
1638 #ifdef CONFIG_SND_SOC_AC97_BUS
1639 /* register any AC97 codecs */
1640 for (i
= 0; i
< card
->num_rtd
; i
++) {
1641 ret
= soc_register_ac97_dai_link(&card
->rtd
[i
]);
1643 printk(KERN_ERR
"asoc: failed to register AC97 %s\n", card
->name
);
1649 card
->instantiated
= 1;
1650 mutex_unlock(&card
->mutex
);
1654 for (i
= 0; i
< card
->num_links
; i
++)
1655 soc_remove_dai_link(card
, i
);
1661 snd_card_free(card
->snd_card
);
1663 mutex_unlock(&card
->mutex
);
1667 * Attempt to initialise any uninitialised cards. Must be called with
1670 static void snd_soc_instantiate_cards(void)
1672 struct snd_soc_card
*card
;
1673 list_for_each_entry(card
, &card_list
, list
)
1674 snd_soc_instantiate_card(card
);
1677 /* probes a new socdev */
1678 static int soc_probe(struct platform_device
*pdev
)
1680 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1683 /* Bodge while we unpick instantiation */
1684 card
->dev
= &pdev
->dev
;
1685 INIT_LIST_HEAD(&card
->dai_dev_list
);
1686 INIT_LIST_HEAD(&card
->codec_dev_list
);
1687 INIT_LIST_HEAD(&card
->platform_dev_list
);
1689 soc_init_card_debugfs(card
);
1691 ret
= snd_soc_register_card(card
);
1693 dev_err(&pdev
->dev
, "Failed to register card\n");
1700 /* removes a socdev */
1701 static int soc_remove(struct platform_device
*pdev
)
1703 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1706 if (card
->instantiated
) {
1708 /* make sure any delayed work runs */
1709 for (i
= 0; i
< card
->num_rtd
; i
++) {
1710 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1711 run_delayed_work(&rtd
->delayed_work
);
1714 /* remove and free each DAI */
1715 for (i
= 0; i
< card
->num_rtd
; i
++)
1716 soc_remove_dai_link(card
, i
);
1718 soc_cleanup_card_debugfs(card
);
1720 /* remove the card */
1725 snd_card_free(card
->snd_card
);
1727 snd_soc_unregister_card(card
);
1731 static int soc_poweroff(struct device
*dev
)
1733 struct platform_device
*pdev
= to_platform_device(dev
);
1734 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1737 if (!card
->instantiated
)
1740 /* Flush out pmdown_time work - we actually do want to run it
1741 * now, we're shutting down so no imminent restart. */
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 snd_soc_dapm_shutdown(card
);
1752 static const struct dev_pm_ops soc_pm_ops
= {
1753 .suspend
= soc_suspend
,
1754 .resume
= soc_resume
,
1755 .poweroff
= soc_poweroff
,
1758 /* ASoC platform driver */
1759 static struct platform_driver soc_driver
= {
1761 .name
= "soc-audio",
1762 .owner
= THIS_MODULE
,
1766 .remove
= soc_remove
,
1769 /* create a new pcm */
1770 static int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
)
1772 struct snd_soc_codec
*codec
= rtd
->codec
;
1773 struct snd_soc_platform
*platform
= rtd
->platform
;
1774 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
1775 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1776 struct snd_pcm
*pcm
;
1778 int ret
= 0, playback
= 0, capture
= 0;
1780 /* check client and interface hw capabilities */
1781 snprintf(new_name
, sizeof(new_name
), "%s %s-%d",
1782 rtd
->dai_link
->stream_name
, codec_dai
->name
, num
);
1784 if (codec_dai
->driver
->playback
.channels_min
)
1786 if (codec_dai
->driver
->capture
.channels_min
)
1789 dev_dbg(rtd
->card
->dev
, "registered pcm #%d %s\n",num
,new_name
);
1790 ret
= snd_pcm_new(rtd
->card
->snd_card
, new_name
,
1791 num
, playback
, capture
, &pcm
);
1793 printk(KERN_ERR
"asoc: can't create pcm for codec %s\n", codec
->name
);
1798 pcm
->private_data
= rtd
;
1799 soc_pcm_ops
.mmap
= platform
->driver
->ops
->mmap
;
1800 soc_pcm_ops
.pointer
= platform
->driver
->ops
->pointer
;
1801 soc_pcm_ops
.ioctl
= platform
->driver
->ops
->ioctl
;
1802 soc_pcm_ops
.copy
= platform
->driver
->ops
->copy
;
1803 soc_pcm_ops
.silence
= platform
->driver
->ops
->silence
;
1804 soc_pcm_ops
.ack
= platform
->driver
->ops
->ack
;
1805 soc_pcm_ops
.page
= platform
->driver
->ops
->page
;
1808 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &soc_pcm_ops
);
1811 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &soc_pcm_ops
);
1813 ret
= platform
->driver
->pcm_new(rtd
->card
->snd_card
, codec_dai
, pcm
);
1815 printk(KERN_ERR
"asoc: platform pcm constructor failed\n");
1819 pcm
->private_free
= platform
->driver
->pcm_free
;
1820 printk(KERN_INFO
"asoc: %s <-> %s mapping ok\n", codec_dai
->name
,
1826 * snd_soc_codec_volatile_register: Report if a register is volatile.
1828 * @codec: CODEC to query.
1829 * @reg: Register to query.
1831 * Boolean function indiciating if a CODEC register is volatile.
1833 int snd_soc_codec_volatile_register(struct snd_soc_codec
*codec
, int reg
)
1835 if (codec
->driver
->volatile_register
)
1836 return codec
->driver
->volatile_register(reg
);
1840 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register
);
1843 * snd_soc_new_ac97_codec - initailise AC97 device
1844 * @codec: audio codec
1845 * @ops: AC97 bus operations
1846 * @num: AC97 codec number
1848 * Initialises AC97 codec resources for use by ad-hoc devices only.
1850 int snd_soc_new_ac97_codec(struct snd_soc_codec
*codec
,
1851 struct snd_ac97_bus_ops
*ops
, int num
)
1853 mutex_lock(&codec
->mutex
);
1855 codec
->ac97
= kzalloc(sizeof(struct snd_ac97
), GFP_KERNEL
);
1856 if (codec
->ac97
== NULL
) {
1857 mutex_unlock(&codec
->mutex
);
1861 codec
->ac97
->bus
= kzalloc(sizeof(struct snd_ac97_bus
), GFP_KERNEL
);
1862 if (codec
->ac97
->bus
== NULL
) {
1865 mutex_unlock(&codec
->mutex
);
1869 codec
->ac97
->bus
->ops
= ops
;
1870 codec
->ac97
->num
= num
;
1873 * Mark the AC97 device to be created by us. This way we ensure that the
1874 * device will be registered with the device subsystem later on.
1876 codec
->ac97_created
= 1;
1878 mutex_unlock(&codec
->mutex
);
1881 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec
);
1884 * snd_soc_free_ac97_codec - free AC97 codec device
1885 * @codec: audio codec
1887 * Frees AC97 codec device resources.
1889 void snd_soc_free_ac97_codec(struct snd_soc_codec
*codec
)
1891 mutex_lock(&codec
->mutex
);
1892 #ifdef CONFIG_SND_SOC_AC97_BUS
1893 soc_unregister_ac97_dai_link(codec
);
1895 kfree(codec
->ac97
->bus
);
1898 codec
->ac97_created
= 0;
1899 mutex_unlock(&codec
->mutex
);
1901 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec
);
1903 unsigned int snd_soc_read(struct snd_soc_codec
*codec
, unsigned int reg
)
1907 ret
= codec
->driver
->read(codec
, reg
);
1908 dev_dbg(codec
->dev
, "read %x => %x\n", reg
, ret
);
1912 EXPORT_SYMBOL_GPL(snd_soc_read
);
1914 unsigned int snd_soc_write(struct snd_soc_codec
*codec
,
1915 unsigned int reg
, unsigned int val
)
1917 dev_dbg(codec
->dev
, "write %x = %x\n", reg
, val
);
1918 return codec
->driver
->write(codec
, reg
, val
);
1920 EXPORT_SYMBOL_GPL(snd_soc_write
);
1923 * snd_soc_update_bits - update codec register bits
1924 * @codec: audio codec
1925 * @reg: codec register
1926 * @mask: register mask
1929 * Writes new register value.
1931 * Returns 1 for change else 0.
1933 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1934 unsigned int mask
, unsigned int value
)
1937 unsigned int old
, new;
1939 old
= snd_soc_read(codec
, reg
);
1940 new = (old
& ~mask
) | value
;
1941 change
= old
!= new;
1943 snd_soc_write(codec
, reg
, new);
1947 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
1950 * snd_soc_update_bits_locked - update codec register bits
1951 * @codec: audio codec
1952 * @reg: codec register
1953 * @mask: register mask
1956 * Writes new register value, and takes the codec mutex.
1958 * Returns 1 for change else 0.
1960 int snd_soc_update_bits_locked(struct snd_soc_codec
*codec
,
1961 unsigned short reg
, unsigned int mask
,
1966 mutex_lock(&codec
->mutex
);
1967 change
= snd_soc_update_bits(codec
, reg
, mask
, value
);
1968 mutex_unlock(&codec
->mutex
);
1972 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked
);
1975 * snd_soc_test_bits - test register for change
1976 * @codec: audio codec
1977 * @reg: codec register
1978 * @mask: register mask
1981 * Tests a register with a new value and checks if the new value is
1982 * different from the old value.
1984 * Returns 1 for change else 0.
1986 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1987 unsigned int mask
, unsigned int value
)
1990 unsigned int old
, new;
1992 old
= snd_soc_read(codec
, reg
);
1993 new = (old
& ~mask
) | value
;
1994 change
= old
!= new;
1998 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
2001 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2002 * @substream: the pcm substream
2003 * @hw: the hardware parameters
2005 * Sets the substream runtime hardware parameters.
2007 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
2008 const struct snd_pcm_hardware
*hw
)
2010 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2011 runtime
->hw
.info
= hw
->info
;
2012 runtime
->hw
.formats
= hw
->formats
;
2013 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
2014 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
2015 runtime
->hw
.periods_min
= hw
->periods_min
;
2016 runtime
->hw
.periods_max
= hw
->periods_max
;
2017 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
2018 runtime
->hw
.fifo_size
= hw
->fifo_size
;
2021 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
2024 * snd_soc_cnew - create new control
2025 * @_template: control template
2026 * @data: control private data
2027 * @long_name: control long name
2029 * Create a new mixer control from a template control.
2031 * Returns 0 for success, else error.
2033 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
2034 void *data
, char *long_name
)
2036 struct snd_kcontrol_new
template;
2038 memcpy(&template, _template
, sizeof(template));
2040 template.name
= long_name
;
2043 return snd_ctl_new1(&template, data
);
2045 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
2048 * snd_soc_add_controls - add an array of controls to a codec.
2049 * Convienience function to add a list of controls. Many codecs were
2050 * duplicating this code.
2052 * @codec: codec to add controls to
2053 * @controls: array of controls to add
2054 * @num_controls: number of elements in the array
2056 * Return 0 for success, else error.
2058 int snd_soc_add_controls(struct snd_soc_codec
*codec
,
2059 const struct snd_kcontrol_new
*controls
, int num_controls
)
2061 struct snd_card
*card
= codec
->card
->snd_card
;
2064 for (i
= 0; i
< num_controls
; i
++) {
2065 const struct snd_kcontrol_new
*control
= &controls
[i
];
2066 err
= snd_ctl_add(card
, snd_soc_cnew(control
, codec
, NULL
));
2068 dev_err(codec
->dev
, "%s: Failed to add %s: %d\n",
2069 codec
->name
, control
->name
, err
);
2076 EXPORT_SYMBOL_GPL(snd_soc_add_controls
);
2079 * snd_soc_info_enum_double - enumerated double mixer info callback
2080 * @kcontrol: mixer control
2081 * @uinfo: control element information
2083 * Callback to provide information about a double enumerated
2086 * Returns 0 for success.
2088 int snd_soc_info_enum_double(struct snd_kcontrol
*kcontrol
,
2089 struct snd_ctl_elem_info
*uinfo
)
2091 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2093 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2094 uinfo
->count
= e
->shift_l
== e
->shift_r
? 1 : 2;
2095 uinfo
->value
.enumerated
.items
= e
->max
;
2097 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2098 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2099 strcpy(uinfo
->value
.enumerated
.name
,
2100 e
->texts
[uinfo
->value
.enumerated
.item
]);
2103 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double
);
2106 * snd_soc_get_enum_double - enumerated double mixer get callback
2107 * @kcontrol: mixer control
2108 * @ucontrol: control element information
2110 * Callback to get the value of a double enumerated mixer.
2112 * Returns 0 for success.
2114 int snd_soc_get_enum_double(struct snd_kcontrol
*kcontrol
,
2115 struct snd_ctl_elem_value
*ucontrol
)
2117 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2118 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2119 unsigned int val
, bitmask
;
2121 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2123 val
= snd_soc_read(codec
, e
->reg
);
2124 ucontrol
->value
.enumerated
.item
[0]
2125 = (val
>> e
->shift_l
) & (bitmask
- 1);
2126 if (e
->shift_l
!= e
->shift_r
)
2127 ucontrol
->value
.enumerated
.item
[1] =
2128 (val
>> e
->shift_r
) & (bitmask
- 1);
2132 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double
);
2135 * snd_soc_put_enum_double - enumerated double mixer put callback
2136 * @kcontrol: mixer control
2137 * @ucontrol: control element information
2139 * Callback to set the value of a double enumerated mixer.
2141 * Returns 0 for success.
2143 int snd_soc_put_enum_double(struct snd_kcontrol
*kcontrol
,
2144 struct snd_ctl_elem_value
*ucontrol
)
2146 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2147 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2149 unsigned int mask
, bitmask
;
2151 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2153 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2155 val
= ucontrol
->value
.enumerated
.item
[0] << e
->shift_l
;
2156 mask
= (bitmask
- 1) << e
->shift_l
;
2157 if (e
->shift_l
!= e
->shift_r
) {
2158 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2160 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2161 mask
|= (bitmask
- 1) << e
->shift_r
;
2164 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2166 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double
);
2169 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2170 * @kcontrol: mixer control
2171 * @ucontrol: control element information
2173 * Callback to get the value of a double semi enumerated mixer.
2175 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2176 * used for handling bitfield coded enumeration for example.
2178 * Returns 0 for success.
2180 int snd_soc_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2181 struct snd_ctl_elem_value
*ucontrol
)
2183 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2184 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2185 unsigned int reg_val
, val
, mux
;
2187 reg_val
= snd_soc_read(codec
, e
->reg
);
2188 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2189 for (mux
= 0; mux
< e
->max
; mux
++) {
2190 if (val
== e
->values
[mux
])
2193 ucontrol
->value
.enumerated
.item
[0] = mux
;
2194 if (e
->shift_l
!= e
->shift_r
) {
2195 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2196 for (mux
= 0; mux
< e
->max
; mux
++) {
2197 if (val
== e
->values
[mux
])
2200 ucontrol
->value
.enumerated
.item
[1] = mux
;
2205 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double
);
2208 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2209 * @kcontrol: mixer control
2210 * @ucontrol: control element information
2212 * Callback to set the value of a double semi enumerated mixer.
2214 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2215 * used for handling bitfield coded enumeration for example.
2217 * Returns 0 for success.
2219 int snd_soc_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2220 struct snd_ctl_elem_value
*ucontrol
)
2222 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2223 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2227 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2229 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2230 mask
= e
->mask
<< e
->shift_l
;
2231 if (e
->shift_l
!= e
->shift_r
) {
2232 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2234 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2235 mask
|= e
->mask
<< e
->shift_r
;
2238 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2240 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double
);
2243 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2244 * @kcontrol: mixer control
2245 * @uinfo: control element information
2247 * Callback to provide information about an external enumerated
2250 * Returns 0 for success.
2252 int snd_soc_info_enum_ext(struct snd_kcontrol
*kcontrol
,
2253 struct snd_ctl_elem_info
*uinfo
)
2255 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2257 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2259 uinfo
->value
.enumerated
.items
= e
->max
;
2261 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2262 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2263 strcpy(uinfo
->value
.enumerated
.name
,
2264 e
->texts
[uinfo
->value
.enumerated
.item
]);
2267 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext
);
2270 * snd_soc_info_volsw_ext - external single mixer info callback
2271 * @kcontrol: mixer control
2272 * @uinfo: control element information
2274 * Callback to provide information about a single external mixer control.
2276 * Returns 0 for success.
2278 int snd_soc_info_volsw_ext(struct snd_kcontrol
*kcontrol
,
2279 struct snd_ctl_elem_info
*uinfo
)
2281 int max
= kcontrol
->private_value
;
2283 if (max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2284 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2286 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2289 uinfo
->value
.integer
.min
= 0;
2290 uinfo
->value
.integer
.max
= max
;
2293 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext
);
2296 * snd_soc_info_volsw - single mixer info callback
2297 * @kcontrol: mixer control
2298 * @uinfo: control element information
2300 * Callback to provide information about a single mixer control.
2302 * Returns 0 for success.
2304 int snd_soc_info_volsw(struct snd_kcontrol
*kcontrol
,
2305 struct snd_ctl_elem_info
*uinfo
)
2307 struct soc_mixer_control
*mc
=
2308 (struct soc_mixer_control
*)kcontrol
->private_value
;
2310 unsigned int shift
= mc
->shift
;
2311 unsigned int rshift
= mc
->rshift
;
2313 if (!mc
->platform_max
)
2314 mc
->platform_max
= mc
->max
;
2315 platform_max
= mc
->platform_max
;
2317 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2318 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2320 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2322 uinfo
->count
= shift
== rshift
? 1 : 2;
2323 uinfo
->value
.integer
.min
= 0;
2324 uinfo
->value
.integer
.max
= platform_max
;
2327 EXPORT_SYMBOL_GPL(snd_soc_info_volsw
);
2330 * snd_soc_get_volsw - single mixer get callback
2331 * @kcontrol: mixer control
2332 * @ucontrol: control element information
2334 * Callback to get the value of a single mixer control.
2336 * Returns 0 for success.
2338 int snd_soc_get_volsw(struct snd_kcontrol
*kcontrol
,
2339 struct snd_ctl_elem_value
*ucontrol
)
2341 struct soc_mixer_control
*mc
=
2342 (struct soc_mixer_control
*)kcontrol
->private_value
;
2343 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2344 unsigned int reg
= mc
->reg
;
2345 unsigned int shift
= mc
->shift
;
2346 unsigned int rshift
= mc
->rshift
;
2348 unsigned int mask
= (1 << fls(max
)) - 1;
2349 unsigned int invert
= mc
->invert
;
2351 ucontrol
->value
.integer
.value
[0] =
2352 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2353 if (shift
!= rshift
)
2354 ucontrol
->value
.integer
.value
[1] =
2355 (snd_soc_read(codec
, reg
) >> rshift
) & mask
;
2357 ucontrol
->value
.integer
.value
[0] =
2358 max
- ucontrol
->value
.integer
.value
[0];
2359 if (shift
!= rshift
)
2360 ucontrol
->value
.integer
.value
[1] =
2361 max
- ucontrol
->value
.integer
.value
[1];
2366 EXPORT_SYMBOL_GPL(snd_soc_get_volsw
);
2369 * snd_soc_put_volsw - single mixer put callback
2370 * @kcontrol: mixer control
2371 * @ucontrol: control element information
2373 * Callback to set the value of a single mixer control.
2375 * Returns 0 for success.
2377 int snd_soc_put_volsw(struct snd_kcontrol
*kcontrol
,
2378 struct snd_ctl_elem_value
*ucontrol
)
2380 struct soc_mixer_control
*mc
=
2381 (struct soc_mixer_control
*)kcontrol
->private_value
;
2382 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2383 unsigned int reg
= mc
->reg
;
2384 unsigned int shift
= mc
->shift
;
2385 unsigned int rshift
= mc
->rshift
;
2387 unsigned int mask
= (1 << fls(max
)) - 1;
2388 unsigned int invert
= mc
->invert
;
2389 unsigned int val
, val2
, val_mask
;
2391 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2394 val_mask
= mask
<< shift
;
2396 if (shift
!= rshift
) {
2397 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2400 val_mask
|= mask
<< rshift
;
2401 val
|= val2
<< rshift
;
2403 return snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2405 EXPORT_SYMBOL_GPL(snd_soc_put_volsw
);
2408 * snd_soc_info_volsw_2r - double mixer info callback
2409 * @kcontrol: mixer control
2410 * @uinfo: control element information
2412 * Callback to provide information about a double mixer control that
2413 * spans 2 codec registers.
2415 * Returns 0 for success.
2417 int snd_soc_info_volsw_2r(struct snd_kcontrol
*kcontrol
,
2418 struct snd_ctl_elem_info
*uinfo
)
2420 struct soc_mixer_control
*mc
=
2421 (struct soc_mixer_control
*)kcontrol
->private_value
;
2424 if (!mc
->platform_max
)
2425 mc
->platform_max
= mc
->max
;
2426 platform_max
= mc
->platform_max
;
2428 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2429 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2431 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2434 uinfo
->value
.integer
.min
= 0;
2435 uinfo
->value
.integer
.max
= platform_max
;
2438 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r
);
2441 * snd_soc_get_volsw_2r - double mixer get callback
2442 * @kcontrol: mixer control
2443 * @ucontrol: control element information
2445 * Callback to get the value of a double mixer control that spans 2 registers.
2447 * Returns 0 for success.
2449 int snd_soc_get_volsw_2r(struct snd_kcontrol
*kcontrol
,
2450 struct snd_ctl_elem_value
*ucontrol
)
2452 struct soc_mixer_control
*mc
=
2453 (struct soc_mixer_control
*)kcontrol
->private_value
;
2454 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2455 unsigned int reg
= mc
->reg
;
2456 unsigned int reg2
= mc
->rreg
;
2457 unsigned int shift
= mc
->shift
;
2459 unsigned int mask
= (1 << fls(max
)) - 1;
2460 unsigned int invert
= mc
->invert
;
2462 ucontrol
->value
.integer
.value
[0] =
2463 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2464 ucontrol
->value
.integer
.value
[1] =
2465 (snd_soc_read(codec
, reg2
) >> shift
) & mask
;
2467 ucontrol
->value
.integer
.value
[0] =
2468 max
- ucontrol
->value
.integer
.value
[0];
2469 ucontrol
->value
.integer
.value
[1] =
2470 max
- ucontrol
->value
.integer
.value
[1];
2475 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r
);
2478 * snd_soc_put_volsw_2r - double mixer set callback
2479 * @kcontrol: mixer control
2480 * @ucontrol: control element information
2482 * Callback to set the value of a double mixer control that spans 2 registers.
2484 * Returns 0 for success.
2486 int snd_soc_put_volsw_2r(struct snd_kcontrol
*kcontrol
,
2487 struct snd_ctl_elem_value
*ucontrol
)
2489 struct soc_mixer_control
*mc
=
2490 (struct soc_mixer_control
*)kcontrol
->private_value
;
2491 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2492 unsigned int reg
= mc
->reg
;
2493 unsigned int reg2
= mc
->rreg
;
2494 unsigned int shift
= mc
->shift
;
2496 unsigned int mask
= (1 << fls(max
)) - 1;
2497 unsigned int invert
= mc
->invert
;
2499 unsigned int val
, val2
, val_mask
;
2501 val_mask
= mask
<< shift
;
2502 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2503 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2511 val2
= val2
<< shift
;
2513 err
= snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2517 err
= snd_soc_update_bits_locked(codec
, reg2
, val_mask
, val2
);
2520 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r
);
2523 * snd_soc_info_volsw_s8 - signed mixer info callback
2524 * @kcontrol: mixer control
2525 * @uinfo: control element information
2527 * Callback to provide information about a signed mixer control.
2529 * Returns 0 for success.
2531 int snd_soc_info_volsw_s8(struct snd_kcontrol
*kcontrol
,
2532 struct snd_ctl_elem_info
*uinfo
)
2534 struct soc_mixer_control
*mc
=
2535 (struct soc_mixer_control
*)kcontrol
->private_value
;
2539 if (!mc
->platform_max
)
2540 mc
->platform_max
= mc
->max
;
2541 platform_max
= mc
->platform_max
;
2543 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2545 uinfo
->value
.integer
.min
= 0;
2546 uinfo
->value
.integer
.max
= platform_max
- min
;
2549 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8
);
2552 * snd_soc_get_volsw_s8 - signed mixer get callback
2553 * @kcontrol: mixer control
2554 * @ucontrol: control element information
2556 * Callback to get the value of a signed mixer control.
2558 * Returns 0 for success.
2560 int snd_soc_get_volsw_s8(struct snd_kcontrol
*kcontrol
,
2561 struct snd_ctl_elem_value
*ucontrol
)
2563 struct soc_mixer_control
*mc
=
2564 (struct soc_mixer_control
*)kcontrol
->private_value
;
2565 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2566 unsigned int reg
= mc
->reg
;
2568 int val
= snd_soc_read(codec
, reg
);
2570 ucontrol
->value
.integer
.value
[0] =
2571 ((signed char)(val
& 0xff))-min
;
2572 ucontrol
->value
.integer
.value
[1] =
2573 ((signed char)((val
>> 8) & 0xff))-min
;
2576 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8
);
2579 * snd_soc_put_volsw_sgn - signed mixer put callback
2580 * @kcontrol: mixer control
2581 * @ucontrol: control element information
2583 * Callback to set the value of a signed mixer control.
2585 * Returns 0 for success.
2587 int snd_soc_put_volsw_s8(struct snd_kcontrol
*kcontrol
,
2588 struct snd_ctl_elem_value
*ucontrol
)
2590 struct soc_mixer_control
*mc
=
2591 (struct soc_mixer_control
*)kcontrol
->private_value
;
2592 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2593 unsigned int reg
= mc
->reg
;
2597 val
= (ucontrol
->value
.integer
.value
[0]+min
) & 0xff;
2598 val
|= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff) << 8;
2600 return snd_soc_update_bits_locked(codec
, reg
, 0xffff, val
);
2602 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8
);
2605 * snd_soc_limit_volume - Set new limit to an existing volume control.
2607 * @codec: where to look for the control
2608 * @name: Name of the control
2609 * @max: new maximum limit
2611 * Return 0 for success, else error.
2613 int snd_soc_limit_volume(struct snd_soc_codec
*codec
,
2614 const char *name
, int max
)
2616 struct snd_card
*card
= codec
->card
->snd_card
;
2617 struct snd_kcontrol
*kctl
;
2618 struct soc_mixer_control
*mc
;
2622 /* Sanity check for name and max */
2623 if (unlikely(!name
|| max
<= 0))
2626 list_for_each_entry(kctl
, &card
->controls
, list
) {
2627 if (!strncmp(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
))) {
2633 mc
= (struct soc_mixer_control
*)kctl
->private_value
;
2634 if (max
<= mc
->max
) {
2635 mc
->platform_max
= max
;
2641 EXPORT_SYMBOL_GPL(snd_soc_limit_volume
);
2644 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2645 * mixer info callback
2646 * @kcontrol: mixer control
2647 * @uinfo: control element information
2649 * Returns 0 for success.
2651 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2652 struct snd_ctl_elem_info
*uinfo
)
2654 struct soc_mixer_control
*mc
=
2655 (struct soc_mixer_control
*)kcontrol
->private_value
;
2659 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2661 uinfo
->value
.integer
.min
= 0;
2662 uinfo
->value
.integer
.max
= max
-min
;
2666 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx
);
2669 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2670 * mixer get callback
2671 * @kcontrol: mixer control
2672 * @uinfo: control element information
2674 * Returns 0 for success.
2676 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2677 struct snd_ctl_elem_value
*ucontrol
)
2679 struct soc_mixer_control
*mc
=
2680 (struct soc_mixer_control
*)kcontrol
->private_value
;
2681 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2682 unsigned int mask
= (1<<mc
->shift
)-1;
2684 int val
= snd_soc_read(codec
, mc
->reg
) & mask
;
2685 int valr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2687 ucontrol
->value
.integer
.value
[0] = ((val
& 0xff)-min
) & mask
;
2688 ucontrol
->value
.integer
.value
[1] = ((valr
& 0xff)-min
) & mask
;
2691 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx
);
2694 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2695 * mixer put callback
2696 * @kcontrol: mixer control
2697 * @uinfo: control element information
2699 * Returns 0 for success.
2701 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2702 struct snd_ctl_elem_value
*ucontrol
)
2704 struct soc_mixer_control
*mc
=
2705 (struct soc_mixer_control
*)kcontrol
->private_value
;
2706 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2707 unsigned int mask
= (1<<mc
->shift
)-1;
2710 unsigned int val
, valr
, oval
, ovalr
;
2712 val
= ((ucontrol
->value
.integer
.value
[0]+min
) & 0xff);
2714 valr
= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff);
2717 oval
= snd_soc_read(codec
, mc
->reg
) & mask
;
2718 ovalr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2722 ret
= snd_soc_write(codec
, mc
->reg
, val
);
2726 if (ovalr
!= valr
) {
2727 ret
= snd_soc_write(codec
, mc
->rreg
, valr
);
2734 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx
);
2737 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2739 * @clk_id: DAI specific clock ID
2740 * @freq: new clock frequency in Hz
2741 * @dir: new clock direction - input/output.
2743 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2745 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
2746 unsigned int freq
, int dir
)
2748 if (dai
->driver
&& dai
->driver
->ops
->set_sysclk
)
2749 return dai
->driver
->ops
->set_sysclk(dai
, clk_id
, freq
, dir
);
2753 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
2756 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2758 * @div_id: DAI specific clock divider ID
2759 * @div: new clock divisor.
2761 * Configures the clock dividers. This is used to derive the best DAI bit and
2762 * frame clocks from the system or master clock. It's best to set the DAI bit
2763 * and frame clocks as low as possible to save system power.
2765 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
2766 int div_id
, int div
)
2768 if (dai
->driver
&& dai
->driver
->ops
->set_clkdiv
)
2769 return dai
->driver
->ops
->set_clkdiv(dai
, div_id
, div
);
2773 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
2776 * snd_soc_dai_set_pll - configure DAI PLL.
2778 * @pll_id: DAI specific PLL ID
2779 * @source: DAI specific source for the PLL
2780 * @freq_in: PLL input clock frequency in Hz
2781 * @freq_out: requested PLL output clock frequency in Hz
2783 * Configures and enables PLL to generate output clock based on input clock.
2785 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
2786 unsigned int freq_in
, unsigned int freq_out
)
2788 if (dai
->driver
&& dai
->driver
->ops
->set_pll
)
2789 return dai
->driver
->ops
->set_pll(dai
, pll_id
, source
,
2794 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
2797 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2799 * @fmt: SND_SOC_DAIFMT_ format value.
2801 * Configures the DAI hardware format and clocking.
2803 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
2805 if (dai
->driver
&& dai
->driver
->ops
->set_fmt
)
2806 return dai
->driver
->ops
->set_fmt(dai
, fmt
);
2810 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
2813 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2815 * @tx_mask: bitmask representing active TX slots.
2816 * @rx_mask: bitmask representing active RX slots.
2817 * @slots: Number of slots in use.
2818 * @slot_width: Width in bits for each slot.
2820 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2823 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
2824 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
2826 if (dai
->driver
&& dai
->driver
->ops
->set_tdm_slot
)
2827 return dai
->driver
->ops
->set_tdm_slot(dai
, tx_mask
, rx_mask
,
2832 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
2835 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2837 * @tx_num: how many TX channels
2838 * @tx_slot: pointer to an array which imply the TX slot number channel
2840 * @rx_num: how many RX channels
2841 * @rx_slot: pointer to an array which imply the RX slot number channel
2844 * configure the relationship between channel number and TDM slot number.
2846 int snd_soc_dai_set_channel_map(struct snd_soc_dai
*dai
,
2847 unsigned int tx_num
, unsigned int *tx_slot
,
2848 unsigned int rx_num
, unsigned int *rx_slot
)
2850 if (dai
->driver
&& dai
->driver
->ops
->set_channel_map
)
2851 return dai
->driver
->ops
->set_channel_map(dai
, tx_num
, tx_slot
,
2856 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map
);
2859 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2861 * @tristate: tristate enable
2863 * Tristates the DAI so that others can use it.
2865 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
2867 if (dai
->driver
&& dai
->driver
->ops
->set_tristate
)
2868 return dai
->driver
->ops
->set_tristate(dai
, tristate
);
2872 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
2875 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2877 * @mute: mute enable
2879 * Mutes the DAI DAC.
2881 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
)
2883 if (dai
->driver
&& dai
->driver
->ops
->digital_mute
)
2884 return dai
->driver
->ops
->digital_mute(dai
, mute
);
2888 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
2891 * snd_soc_register_card - Register a card with the ASoC core
2893 * @card: Card to register
2895 * Note that currently this is an internal only function: it will be
2896 * exposed to machine drivers after further backporting of ASoC v2
2897 * registration APIs.
2899 static int snd_soc_register_card(struct snd_soc_card
*card
)
2903 if (!card
->name
|| !card
->dev
)
2906 card
->rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
) * card
->num_links
,
2908 if (card
->rtd
== NULL
)
2911 for (i
= 0; i
< card
->num_links
; i
++)
2912 card
->rtd
[i
].dai_link
= &card
->dai_link
[i
];
2914 INIT_LIST_HEAD(&card
->list
);
2915 card
->instantiated
= 0;
2916 mutex_init(&card
->mutex
);
2918 mutex_lock(&client_mutex
);
2919 list_add(&card
->list
, &card_list
);
2920 snd_soc_instantiate_cards();
2921 mutex_unlock(&client_mutex
);
2923 dev_dbg(card
->dev
, "Registered card '%s'\n", card
->name
);
2929 * snd_soc_unregister_card - Unregister a card with the ASoC core
2931 * @card: Card to unregister
2933 * Note that currently this is an internal only function: it will be
2934 * exposed to machine drivers after further backporting of ASoC v2
2935 * registration APIs.
2937 static int snd_soc_unregister_card(struct snd_soc_card
*card
)
2939 mutex_lock(&client_mutex
);
2940 list_del(&card
->list
);
2941 mutex_unlock(&client_mutex
);
2942 dev_dbg(card
->dev
, "Unregistered card '%s'\n", card
->name
);
2948 * Simplify DAI link configuration by removing ".-1" from device names
2949 * and sanitizing names.
2951 static inline char *fmt_single_name(struct device
*dev
, int *id
)
2953 char *found
, name
[NAME_SIZE
];
2956 if (dev_name(dev
) == NULL
)
2959 strncpy(name
, dev_name(dev
), NAME_SIZE
);
2961 /* are we a "%s.%d" name (platform and SPI components) */
2962 found
= strstr(name
, dev
->driver
->name
);
2965 if (sscanf(&found
[strlen(dev
->driver
->name
)], ".%d", id
) == 1) {
2967 /* discard ID from name if ID == -1 */
2969 found
[strlen(dev
->driver
->name
)] = '\0';
2973 /* I2C component devices are named "bus-addr" */
2974 if (sscanf(name
, "%x-%x", &id1
, &id2
) == 2) {
2975 char tmp
[NAME_SIZE
];
2977 /* create unique ID number from I2C addr and bus */
2978 *id
= ((id1
& 0xffff) << 16) + id2
;
2980 /* sanitize component name for DAI link creation */
2981 snprintf(tmp
, NAME_SIZE
, "%s.%s", dev
->driver
->name
, name
);
2982 strncpy(name
, tmp
, NAME_SIZE
);
2987 return kstrdup(name
, GFP_KERNEL
);
2991 * Simplify DAI link naming for single devices with multiple DAIs by removing
2992 * any ".-1" and using the DAI name (instead of device name).
2994 static inline char *fmt_multiple_name(struct device
*dev
,
2995 struct snd_soc_dai_driver
*dai_drv
)
2997 if (dai_drv
->name
== NULL
) {
2998 printk(KERN_ERR
"asoc: error - multiple DAI %s registered with no name\n",
3003 return kstrdup(dai_drv
->name
, GFP_KERNEL
);
3007 * snd_soc_register_dai - Register a DAI with the ASoC core
3009 * @dai: DAI to register
3011 int snd_soc_register_dai(struct device
*dev
,
3012 struct snd_soc_dai_driver
*dai_drv
)
3014 struct snd_soc_dai
*dai
;
3016 dev_dbg(dev
, "dai register %s\n", dev_name(dev
));
3018 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
3022 /* create DAI component name */
3023 dai
->name
= fmt_single_name(dev
, &dai
->id
);
3024 if (dai
->name
== NULL
) {
3030 dai
->driver
= dai_drv
;
3031 if (!dai
->driver
->ops
)
3032 dai
->driver
->ops
= &null_dai_ops
;
3034 mutex_lock(&client_mutex
);
3035 list_add(&dai
->list
, &dai_list
);
3036 snd_soc_instantiate_cards();
3037 mutex_unlock(&client_mutex
);
3039 pr_debug("Registered DAI '%s'\n", dai
->name
);
3043 EXPORT_SYMBOL_GPL(snd_soc_register_dai
);
3046 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3048 * @dai: DAI to unregister
3050 void snd_soc_unregister_dai(struct device
*dev
)
3052 struct snd_soc_dai
*dai
;
3054 list_for_each_entry(dai
, &dai_list
, list
) {
3055 if (dev
== dai
->dev
)
3061 mutex_lock(&client_mutex
);
3062 list_del(&dai
->list
);
3063 mutex_unlock(&client_mutex
);
3065 pr_debug("Unregistered DAI '%s'\n", dai
->name
);
3069 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai
);
3072 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3074 * @dai: Array of DAIs to register
3075 * @count: Number of DAIs
3077 int snd_soc_register_dais(struct device
*dev
,
3078 struct snd_soc_dai_driver
*dai_drv
, size_t count
)
3080 struct snd_soc_dai
*dai
;
3083 dev_dbg(dev
, "dai register %s #%Zu\n", dev_name(dev
), count
);
3085 for (i
= 0; i
< count
; i
++) {
3087 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
3093 /* create DAI component name */
3094 dai
->name
= fmt_multiple_name(dev
, &dai_drv
[i
]);
3095 if (dai
->name
== NULL
) {
3102 dai
->driver
= &dai_drv
[i
];
3103 if (dai
->driver
->id
)
3104 dai
->id
= dai
->driver
->id
;
3107 if (!dai
->driver
->ops
)
3108 dai
->driver
->ops
= &null_dai_ops
;
3110 mutex_lock(&client_mutex
);
3111 list_add(&dai
->list
, &dai_list
);
3112 mutex_unlock(&client_mutex
);
3114 pr_debug("Registered DAI '%s'\n", dai
->name
);
3117 snd_soc_instantiate_cards();
3121 for (i
--; i
>= 0; i
--)
3122 snd_soc_unregister_dai(dev
);
3126 EXPORT_SYMBOL_GPL(snd_soc_register_dais
);
3129 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3131 * @dai: Array of DAIs to unregister
3132 * @count: Number of DAIs
3134 void snd_soc_unregister_dais(struct device
*dev
, size_t count
)
3138 for (i
= 0; i
< count
; i
++)
3139 snd_soc_unregister_dai(dev
);
3141 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais
);
3144 * snd_soc_register_platform - Register a platform with the ASoC core
3146 * @platform: platform to register
3148 int snd_soc_register_platform(struct device
*dev
,
3149 struct snd_soc_platform_driver
*platform_drv
)
3151 struct snd_soc_platform
*platform
;
3153 dev_dbg(dev
, "platform register %s\n", dev_name(dev
));
3155 platform
= kzalloc(sizeof(struct snd_soc_platform
), GFP_KERNEL
);
3156 if (platform
== NULL
)
3159 /* create platform component name */
3160 platform
->name
= fmt_single_name(dev
, &platform
->id
);
3161 if (platform
->name
== NULL
) {
3166 platform
->dev
= dev
;
3167 platform
->driver
= platform_drv
;
3169 mutex_lock(&client_mutex
);
3170 list_add(&platform
->list
, &platform_list
);
3171 snd_soc_instantiate_cards();
3172 mutex_unlock(&client_mutex
);
3174 pr_debug("Registered platform '%s'\n", platform
->name
);
3178 EXPORT_SYMBOL_GPL(snd_soc_register_platform
);
3181 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3183 * @platform: platform to unregister
3185 void snd_soc_unregister_platform(struct device
*dev
)
3187 struct snd_soc_platform
*platform
;
3189 list_for_each_entry(platform
, &platform_list
, list
) {
3190 if (dev
== platform
->dev
)
3196 mutex_lock(&client_mutex
);
3197 list_del(&platform
->list
);
3198 mutex_unlock(&client_mutex
);
3200 pr_debug("Unregistered platform '%s'\n", platform
->name
);
3201 kfree(platform
->name
);
3204 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform
);
3206 static u64 codec_format_map
[] = {
3207 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
,
3208 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_U16_BE
,
3209 SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S24_BE
,
3210 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_U24_BE
,
3211 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
,
3212 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_U32_BE
,
3213 SNDRV_PCM_FMTBIT_S24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3214 SNDRV_PCM_FMTBIT_U24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3215 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S20_3BE
,
3216 SNDRV_PCM_FMTBIT_U20_3LE
| SNDRV_PCM_FMTBIT_U20_3BE
,
3217 SNDRV_PCM_FMTBIT_S18_3LE
| SNDRV_PCM_FMTBIT_S18_3BE
,
3218 SNDRV_PCM_FMTBIT_U18_3LE
| SNDRV_PCM_FMTBIT_U18_3BE
,
3219 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
,
3220 SNDRV_PCM_FMTBIT_FLOAT64_LE
| SNDRV_PCM_FMTBIT_FLOAT64_BE
,
3221 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3222 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
,
3225 /* Fix up the DAI formats for endianness: codecs don't actually see
3226 * the endianness of the data but we're using the CPU format
3227 * definitions which do need to include endianness so we ensure that
3228 * codec DAIs always have both big and little endian variants set.
3230 static void fixup_codec_formats(struct snd_soc_pcm_stream
*stream
)
3234 for (i
= 0; i
< ARRAY_SIZE(codec_format_map
); i
++)
3235 if (stream
->formats
& codec_format_map
[i
])
3236 stream
->formats
|= codec_format_map
[i
];
3240 * snd_soc_register_codec - Register a codec with the ASoC core
3242 * @codec: codec to register
3244 int snd_soc_register_codec(struct device
*dev
,
3245 struct snd_soc_codec_driver
*codec_drv
,
3246 struct snd_soc_dai_driver
*dai_drv
, int num_dai
)
3248 struct snd_soc_codec
*codec
;
3251 dev_dbg(dev
, "codec register %s\n", dev_name(dev
));
3253 codec
= kzalloc(sizeof(struct snd_soc_codec
), GFP_KERNEL
);
3257 /* create CODEC component name */
3258 codec
->name
= fmt_single_name(dev
, &codec
->id
);
3259 if (codec
->name
== NULL
) {
3264 INIT_LIST_HEAD(&codec
->dapm
.widgets
);
3265 INIT_LIST_HEAD(&codec
->dapm
.paths
);
3266 codec
->dapm
.bias_level
= SND_SOC_BIAS_OFF
;
3267 codec
->dapm
.dev
= dev
;
3268 codec
->dapm
.codec
= codec
;
3270 /* allocate CODEC register cache */
3271 if (codec_drv
->reg_cache_size
&& codec_drv
->reg_word_size
) {
3273 if (codec_drv
->reg_cache_default
)
3274 codec
->reg_cache
= kmemdup(codec_drv
->reg_cache_default
,
3275 codec_drv
->reg_cache_size
* codec_drv
->reg_word_size
, GFP_KERNEL
);
3277 codec
->reg_cache
= kzalloc(codec_drv
->reg_cache_size
*
3278 codec_drv
->reg_word_size
, GFP_KERNEL
);
3280 if (codec
->reg_cache
== NULL
) {
3288 codec
->driver
= codec_drv
;
3289 codec
->num_dai
= num_dai
;
3290 mutex_init(&codec
->mutex
);
3292 for (i
= 0; i
< num_dai
; i
++) {
3293 fixup_codec_formats(&dai_drv
[i
].playback
);
3294 fixup_codec_formats(&dai_drv
[i
].capture
);
3297 /* register any DAIs */
3299 ret
= snd_soc_register_dais(dev
, dai_drv
, num_dai
);
3304 mutex_lock(&client_mutex
);
3305 list_add(&codec
->list
, &codec_list
);
3306 snd_soc_instantiate_cards();
3307 mutex_unlock(&client_mutex
);
3309 pr_debug("Registered codec '%s'\n", codec
->name
);
3313 if (codec
->reg_cache
)
3314 kfree(codec
->reg_cache
);
3319 EXPORT_SYMBOL_GPL(snd_soc_register_codec
);
3322 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3324 * @codec: codec to unregister
3326 void snd_soc_unregister_codec(struct device
*dev
)
3328 struct snd_soc_codec
*codec
;
3331 list_for_each_entry(codec
, &codec_list
, list
) {
3332 if (dev
== codec
->dev
)
3339 for (i
= 0; i
< codec
->num_dai
; i
++)
3340 snd_soc_unregister_dai(dev
);
3342 mutex_lock(&client_mutex
);
3343 list_del(&codec
->list
);
3344 mutex_unlock(&client_mutex
);
3346 pr_debug("Unregistered codec '%s'\n", codec
->name
);
3348 if (codec
->reg_cache
)
3349 kfree(codec
->reg_cache
);
3353 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec
);
3355 static int __init
snd_soc_init(void)
3357 #ifdef CONFIG_DEBUG_FS
3358 debugfs_root
= debugfs_create_dir("asoc", NULL
);
3359 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
3361 "ASoC: Failed to create debugfs directory\n");
3362 debugfs_root
= NULL
;
3365 if (!debugfs_create_file("codecs", 0444, debugfs_root
, NULL
,
3367 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3369 if (!debugfs_create_file("dais", 0444, debugfs_root
, NULL
,
3371 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3373 if (!debugfs_create_file("platforms", 0444, debugfs_root
, NULL
,
3374 &platform_list_fops
))
3375 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3378 return platform_driver_register(&soc_driver
);
3380 module_init(snd_soc_init
);
3382 static void __exit
snd_soc_exit(void)
3384 #ifdef CONFIG_DEBUG_FS
3385 debugfs_remove_recursive(debugfs_root
);
3387 platform_driver_unregister(&soc_driver
);
3389 module_exit(snd_soc_exit
);
3391 /* Module information */
3392 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3393 MODULE_DESCRIPTION("ALSA SoC Core");
3394 MODULE_LICENSE("GPL");
3395 MODULE_ALIAS("platform:soc-audio");