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
,
238 static void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
240 codec
->debugfs_codec_root
= debugfs_create_dir(codec
->name
,
242 if (!codec
->debugfs_codec_root
) {
244 "ASoC: Failed to create codec debugfs directory\n");
248 codec
->debugfs_reg
= debugfs_create_file("codec_reg", 0644,
249 codec
->debugfs_codec_root
,
250 codec
, &codec_reg_fops
);
251 if (!codec
->debugfs_reg
)
253 "ASoC: Failed to create codec register debugfs file\n");
255 codec
->debugfs_pop_time
= debugfs_create_u32("dapm_pop_time", 0644,
256 codec
->debugfs_codec_root
,
258 if (!codec
->debugfs_pop_time
)
260 "Failed to create pop time debugfs file\n");
262 codec
->debugfs_dapm
= debugfs_create_dir("dapm",
263 codec
->debugfs_codec_root
);
264 if (!codec
->debugfs_dapm
)
266 "Failed to create DAPM debugfs directory\n");
268 snd_soc_dapm_debugfs_init(codec
);
271 static void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
273 debugfs_remove_recursive(codec
->debugfs_codec_root
);
276 static ssize_t
codec_list_read_file(struct file
*file
, char __user
*user_buf
,
277 size_t count
, loff_t
*ppos
)
279 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
280 ssize_t len
, ret
= 0;
281 struct snd_soc_codec
*codec
;
286 list_for_each_entry(codec
, &codec_list
, list
) {
287 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
291 if (ret
> PAGE_SIZE
) {
298 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
305 static const struct file_operations codec_list_fops
= {
306 .read
= codec_list_read_file
,
307 .llseek
= default_llseek
,/* read accesses f_pos */
310 static ssize_t
dai_list_read_file(struct file
*file
, char __user
*user_buf
,
311 size_t count
, loff_t
*ppos
)
313 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
314 ssize_t len
, ret
= 0;
315 struct snd_soc_dai
*dai
;
320 list_for_each_entry(dai
, &dai_list
, list
) {
321 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n", dai
->name
);
324 if (ret
> PAGE_SIZE
) {
330 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
337 static const struct file_operations dai_list_fops
= {
338 .read
= dai_list_read_file
,
339 .llseek
= default_llseek
,/* read accesses f_pos */
342 static ssize_t
platform_list_read_file(struct file
*file
,
343 char __user
*user_buf
,
344 size_t count
, loff_t
*ppos
)
346 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
347 ssize_t len
, ret
= 0;
348 struct snd_soc_platform
*platform
;
353 list_for_each_entry(platform
, &platform_list
, list
) {
354 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
358 if (ret
> PAGE_SIZE
) {
364 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
371 static const struct file_operations platform_list_fops
= {
372 .read
= platform_list_read_file
,
373 .llseek
= default_llseek
,/* read accesses f_pos */
378 static inline void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
382 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
387 #ifdef CONFIG_SND_SOC_AC97_BUS
388 /* unregister ac97 codec */
389 static int soc_ac97_dev_unregister(struct snd_soc_codec
*codec
)
391 if (codec
->ac97
->dev
.bus
)
392 device_unregister(&codec
->ac97
->dev
);
396 /* stop no dev release warning */
397 static void soc_ac97_device_release(struct device
*dev
){}
399 /* register ac97 codec to bus */
400 static int soc_ac97_dev_register(struct snd_soc_codec
*codec
)
404 codec
->ac97
->dev
.bus
= &ac97_bus_type
;
405 codec
->ac97
->dev
.parent
= codec
->card
->dev
;
406 codec
->ac97
->dev
.release
= soc_ac97_device_release
;
408 dev_set_name(&codec
->ac97
->dev
, "%d-%d:%s",
409 codec
->card
->snd_card
->number
, 0, codec
->name
);
410 err
= device_register(&codec
->ac97
->dev
);
412 snd_printk(KERN_ERR
"Can't register ac97 bus\n");
413 codec
->ac97
->dev
.bus
= NULL
;
420 static int soc_pcm_apply_symmetry(struct snd_pcm_substream
*substream
)
422 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
423 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
424 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
427 if (codec_dai
->driver
->symmetric_rates
|| cpu_dai
->driver
->symmetric_rates
||
428 rtd
->dai_link
->symmetric_rates
) {
429 dev_dbg(&rtd
->dev
, "Symmetry forces %dHz rate\n",
432 ret
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
433 SNDRV_PCM_HW_PARAM_RATE
,
438 "Unable to apply rate symmetry constraint: %d\n", ret
);
447 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
448 * then initialized and any private data can be allocated. This also calls
449 * startup for the cpu DAI, platform, machine and codec DAI.
451 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
453 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
454 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
455 struct snd_soc_platform
*platform
= rtd
->platform
;
456 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
457 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
458 struct snd_soc_dai_driver
*cpu_dai_drv
= cpu_dai
->driver
;
459 struct snd_soc_dai_driver
*codec_dai_drv
= codec_dai
->driver
;
462 mutex_lock(&pcm_mutex
);
464 /* startup the audio subsystem */
465 if (cpu_dai
->driver
->ops
->startup
) {
466 ret
= cpu_dai
->driver
->ops
->startup(substream
, cpu_dai
);
468 printk(KERN_ERR
"asoc: can't open interface %s\n",
474 if (platform
->driver
->ops
->open
) {
475 ret
= platform
->driver
->ops
->open(substream
);
477 printk(KERN_ERR
"asoc: can't open platform %s\n", platform
->name
);
482 if (codec_dai
->driver
->ops
->startup
) {
483 ret
= codec_dai
->driver
->ops
->startup(substream
, codec_dai
);
485 printk(KERN_ERR
"asoc: can't open codec %s\n",
491 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->startup
) {
492 ret
= rtd
->dai_link
->ops
->startup(substream
);
494 printk(KERN_ERR
"asoc: %s startup failed\n", rtd
->dai_link
->name
);
499 /* Check that the codec and cpu DAI's are compatible */
500 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
501 runtime
->hw
.rate_min
=
502 max(codec_dai_drv
->playback
.rate_min
,
503 cpu_dai_drv
->playback
.rate_min
);
504 runtime
->hw
.rate_max
=
505 min(codec_dai_drv
->playback
.rate_max
,
506 cpu_dai_drv
->playback
.rate_max
);
507 runtime
->hw
.channels_min
=
508 max(codec_dai_drv
->playback
.channels_min
,
509 cpu_dai_drv
->playback
.channels_min
);
510 runtime
->hw
.channels_max
=
511 min(codec_dai_drv
->playback
.channels_max
,
512 cpu_dai_drv
->playback
.channels_max
);
513 runtime
->hw
.formats
=
514 codec_dai_drv
->playback
.formats
& cpu_dai_drv
->playback
.formats
;
516 codec_dai_drv
->playback
.rates
& cpu_dai_drv
->playback
.rates
;
517 if (codec_dai_drv
->playback
.rates
518 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
519 runtime
->hw
.rates
|= cpu_dai_drv
->playback
.rates
;
520 if (cpu_dai_drv
->playback
.rates
521 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
522 runtime
->hw
.rates
|= codec_dai_drv
->playback
.rates
;
524 runtime
->hw
.rate_min
=
525 max(codec_dai_drv
->capture
.rate_min
,
526 cpu_dai_drv
->capture
.rate_min
);
527 runtime
->hw
.rate_max
=
528 min(codec_dai_drv
->capture
.rate_max
,
529 cpu_dai_drv
->capture
.rate_max
);
530 runtime
->hw
.channels_min
=
531 max(codec_dai_drv
->capture
.channels_min
,
532 cpu_dai_drv
->capture
.channels_min
);
533 runtime
->hw
.channels_max
=
534 min(codec_dai_drv
->capture
.channels_max
,
535 cpu_dai_drv
->capture
.channels_max
);
536 runtime
->hw
.formats
=
537 codec_dai_drv
->capture
.formats
& cpu_dai_drv
->capture
.formats
;
539 codec_dai_drv
->capture
.rates
& cpu_dai_drv
->capture
.rates
;
540 if (codec_dai_drv
->capture
.rates
541 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
542 runtime
->hw
.rates
|= cpu_dai_drv
->capture
.rates
;
543 if (cpu_dai_drv
->capture
.rates
544 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
545 runtime
->hw
.rates
|= codec_dai_drv
->capture
.rates
;
548 snd_pcm_limit_hw_rates(runtime
);
549 if (!runtime
->hw
.rates
) {
550 printk(KERN_ERR
"asoc: %s <-> %s No matching rates\n",
551 codec_dai
->name
, cpu_dai
->name
);
554 if (!runtime
->hw
.formats
) {
555 printk(KERN_ERR
"asoc: %s <-> %s No matching formats\n",
556 codec_dai
->name
, cpu_dai
->name
);
559 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
) {
560 printk(KERN_ERR
"asoc: %s <-> %s No matching channels\n",
561 codec_dai
->name
, cpu_dai
->name
);
565 /* Symmetry only applies if we've already got an active stream. */
566 if (cpu_dai
->active
|| codec_dai
->active
) {
567 ret
= soc_pcm_apply_symmetry(substream
);
572 pr_debug("asoc: %s <-> %s info:\n",
573 codec_dai
->name
, cpu_dai
->name
);
574 pr_debug("asoc: rate mask 0x%x\n", runtime
->hw
.rates
);
575 pr_debug("asoc: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
576 runtime
->hw
.channels_max
);
577 pr_debug("asoc: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
578 runtime
->hw
.rate_max
);
580 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
581 cpu_dai
->playback_active
++;
582 codec_dai
->playback_active
++;
584 cpu_dai
->capture_active
++;
585 codec_dai
->capture_active
++;
589 rtd
->codec
->active
++;
590 mutex_unlock(&pcm_mutex
);
594 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
595 rtd
->dai_link
->ops
->shutdown(substream
);
598 if (codec_dai
->driver
->ops
->shutdown
)
599 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
602 if (platform
->driver
->ops
->close
)
603 platform
->driver
->ops
->close(substream
);
606 if (cpu_dai
->driver
->ops
->shutdown
)
607 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
609 mutex_unlock(&pcm_mutex
);
614 * Power down the audio subsystem pmdown_time msecs after close is called.
615 * This is to ensure there are no pops or clicks in between any music tracks
616 * due to DAPM power cycling.
618 static void close_delayed_work(struct work_struct
*work
)
620 struct snd_soc_pcm_runtime
*rtd
=
621 container_of(work
, struct snd_soc_pcm_runtime
, delayed_work
.work
);
622 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
624 mutex_lock(&pcm_mutex
);
626 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
627 codec_dai
->driver
->playback
.stream_name
,
628 codec_dai
->playback_active
? "active" : "inactive",
629 codec_dai
->pop_wait
? "yes" : "no");
631 /* are we waiting on this codec DAI stream */
632 if (codec_dai
->pop_wait
== 1) {
633 codec_dai
->pop_wait
= 0;
634 snd_soc_dapm_stream_event(rtd
,
635 codec_dai
->driver
->playback
.stream_name
,
636 SND_SOC_DAPM_STREAM_STOP
);
639 mutex_unlock(&pcm_mutex
);
643 * Called by ALSA when a PCM substream is closed. Private data can be
644 * freed here. The cpu DAI, codec DAI, machine and platform are also
647 static int soc_codec_close(struct snd_pcm_substream
*substream
)
649 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
650 struct snd_soc_platform
*platform
= rtd
->platform
;
651 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
652 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
653 struct snd_soc_codec
*codec
= rtd
->codec
;
655 mutex_lock(&pcm_mutex
);
657 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
658 cpu_dai
->playback_active
--;
659 codec_dai
->playback_active
--;
661 cpu_dai
->capture_active
--;
662 codec_dai
->capture_active
--;
669 /* Muting the DAC suppresses artifacts caused during digital
670 * shutdown, for example from stopping clocks.
672 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
673 snd_soc_dai_digital_mute(codec_dai
, 1);
675 if (cpu_dai
->driver
->ops
->shutdown
)
676 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
678 if (codec_dai
->driver
->ops
->shutdown
)
679 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
681 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
682 rtd
->dai_link
->ops
->shutdown(substream
);
684 if (platform
->driver
->ops
->close
)
685 platform
->driver
->ops
->close(substream
);
686 cpu_dai
->runtime
= NULL
;
688 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
689 /* start delayed pop wq here for playback streams */
690 codec_dai
->pop_wait
= 1;
691 schedule_delayed_work(&rtd
->delayed_work
,
692 msecs_to_jiffies(rtd
->pmdown_time
));
694 /* capture streams can be powered down now */
695 snd_soc_dapm_stream_event(rtd
,
696 codec_dai
->driver
->capture
.stream_name
,
697 SND_SOC_DAPM_STREAM_STOP
);
700 mutex_unlock(&pcm_mutex
);
705 * Called by ALSA when the PCM substream is prepared, can set format, sample
706 * rate, etc. This function is non atomic and can be called multiple times,
707 * it can refer to the runtime info.
709 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
711 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
712 struct snd_soc_platform
*platform
= rtd
->platform
;
713 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
714 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
717 mutex_lock(&pcm_mutex
);
719 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->prepare
) {
720 ret
= rtd
->dai_link
->ops
->prepare(substream
);
722 printk(KERN_ERR
"asoc: machine prepare error\n");
727 if (platform
->driver
->ops
->prepare
) {
728 ret
= platform
->driver
->ops
->prepare(substream
);
730 printk(KERN_ERR
"asoc: platform prepare error\n");
735 if (codec_dai
->driver
->ops
->prepare
) {
736 ret
= codec_dai
->driver
->ops
->prepare(substream
, codec_dai
);
738 printk(KERN_ERR
"asoc: codec DAI prepare error\n");
743 if (cpu_dai
->driver
->ops
->prepare
) {
744 ret
= cpu_dai
->driver
->ops
->prepare(substream
, cpu_dai
);
746 printk(KERN_ERR
"asoc: cpu DAI prepare error\n");
751 /* cancel any delayed stream shutdown that is pending */
752 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
753 codec_dai
->pop_wait
) {
754 codec_dai
->pop_wait
= 0;
755 cancel_delayed_work(&rtd
->delayed_work
);
758 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
759 snd_soc_dapm_stream_event(rtd
,
760 codec_dai
->driver
->playback
.stream_name
,
761 SND_SOC_DAPM_STREAM_START
);
763 snd_soc_dapm_stream_event(rtd
,
764 codec_dai
->driver
->capture
.stream_name
,
765 SND_SOC_DAPM_STREAM_START
);
767 snd_soc_dai_digital_mute(codec_dai
, 0);
770 mutex_unlock(&pcm_mutex
);
775 * Called by ALSA when the hardware params are set by application. This
776 * function can also be called multiple times and can allocate buffers
777 * (using snd_pcm_lib_* ). It's non-atomic.
779 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
780 struct snd_pcm_hw_params
*params
)
782 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
783 struct snd_soc_platform
*platform
= rtd
->platform
;
784 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
785 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
788 mutex_lock(&pcm_mutex
);
790 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_params
) {
791 ret
= rtd
->dai_link
->ops
->hw_params(substream
, params
);
793 printk(KERN_ERR
"asoc: machine hw_params failed\n");
798 if (codec_dai
->driver
->ops
->hw_params
) {
799 ret
= codec_dai
->driver
->ops
->hw_params(substream
, params
, codec_dai
);
801 printk(KERN_ERR
"asoc: can't set codec %s hw params\n",
807 if (cpu_dai
->driver
->ops
->hw_params
) {
808 ret
= cpu_dai
->driver
->ops
->hw_params(substream
, params
, cpu_dai
);
810 printk(KERN_ERR
"asoc: interface %s hw params failed\n",
816 if (platform
->driver
->ops
->hw_params
) {
817 ret
= platform
->driver
->ops
->hw_params(substream
, params
);
819 printk(KERN_ERR
"asoc: platform %s hw params failed\n",
825 rtd
->rate
= params_rate(params
);
828 mutex_unlock(&pcm_mutex
);
832 if (cpu_dai
->driver
->ops
->hw_free
)
833 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
836 if (codec_dai
->driver
->ops
->hw_free
)
837 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
840 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
841 rtd
->dai_link
->ops
->hw_free(substream
);
843 mutex_unlock(&pcm_mutex
);
848 * Free's resources allocated by hw_params, can be called multiple times
850 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
852 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
853 struct snd_soc_platform
*platform
= rtd
->platform
;
854 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
855 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
856 struct snd_soc_codec
*codec
= rtd
->codec
;
858 mutex_lock(&pcm_mutex
);
860 /* apply codec digital mute */
862 snd_soc_dai_digital_mute(codec_dai
, 1);
864 /* free any machine hw params */
865 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
866 rtd
->dai_link
->ops
->hw_free(substream
);
868 /* free any DMA resources */
869 if (platform
->driver
->ops
->hw_free
)
870 platform
->driver
->ops
->hw_free(substream
);
872 /* now free hw params for the DAI's */
873 if (codec_dai
->driver
->ops
->hw_free
)
874 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
876 if (cpu_dai
->driver
->ops
->hw_free
)
877 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
879 mutex_unlock(&pcm_mutex
);
883 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
885 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
886 struct snd_soc_platform
*platform
= rtd
->platform
;
887 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
888 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
891 if (codec_dai
->driver
->ops
->trigger
) {
892 ret
= codec_dai
->driver
->ops
->trigger(substream
, cmd
, codec_dai
);
897 if (platform
->driver
->ops
->trigger
) {
898 ret
= platform
->driver
->ops
->trigger(substream
, cmd
);
903 if (cpu_dai
->driver
->ops
->trigger
) {
904 ret
= cpu_dai
->driver
->ops
->trigger(substream
, cmd
, cpu_dai
);
912 * soc level wrapper for pointer callback
913 * If cpu_dai, codec_dai, platform driver has the delay callback, than
914 * the runtime->delay will be updated accordingly.
916 static snd_pcm_uframes_t
soc_pcm_pointer(struct snd_pcm_substream
*substream
)
918 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
919 struct snd_soc_platform
*platform
= rtd
->platform
;
920 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
921 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
922 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
923 snd_pcm_uframes_t offset
= 0;
924 snd_pcm_sframes_t delay
= 0;
926 if (platform
->driver
->ops
->pointer
)
927 offset
= platform
->driver
->ops
->pointer(substream
);
929 if (cpu_dai
->driver
->ops
->delay
)
930 delay
+= cpu_dai
->driver
->ops
->delay(substream
, cpu_dai
);
932 if (codec_dai
->driver
->ops
->delay
)
933 delay
+= codec_dai
->driver
->ops
->delay(substream
, codec_dai
);
935 if (platform
->driver
->delay
)
936 delay
+= platform
->driver
->delay(substream
, codec_dai
);
938 runtime
->delay
= delay
;
943 /* ASoC PCM operations */
944 static struct snd_pcm_ops soc_pcm_ops
= {
945 .open
= soc_pcm_open
,
946 .close
= soc_codec_close
,
947 .hw_params
= soc_pcm_hw_params
,
948 .hw_free
= soc_pcm_hw_free
,
949 .prepare
= soc_pcm_prepare
,
950 .trigger
= soc_pcm_trigger
,
951 .pointer
= soc_pcm_pointer
,
955 /* powers down audio subsystem for suspend */
956 static int soc_suspend(struct device
*dev
)
958 struct platform_device
*pdev
= to_platform_device(dev
);
959 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
962 /* If the initialization of this soc device failed, there is no codec
963 * associated with it. Just bail out in this case.
965 if (list_empty(&card
->codec_dev_list
))
968 /* Due to the resume being scheduled into a workqueue we could
969 * suspend before that's finished - wait for it to complete.
971 snd_power_lock(card
->snd_card
);
972 snd_power_wait(card
->snd_card
, SNDRV_CTL_POWER_D0
);
973 snd_power_unlock(card
->snd_card
);
975 /* we're going to block userspace touching us until resume completes */
976 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D3hot
);
978 /* mute any active DAC's */
979 for (i
= 0; i
< card
->num_rtd
; i
++) {
980 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
981 struct snd_soc_dai_driver
*drv
= dai
->driver
;
983 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
986 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
987 drv
->ops
->digital_mute(dai
, 1);
990 /* suspend all pcms */
991 for (i
= 0; i
< card
->num_rtd
; i
++) {
992 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
995 snd_pcm_suspend_all(card
->rtd
[i
].pcm
);
998 if (card
->suspend_pre
)
999 card
->suspend_pre(pdev
, PMSG_SUSPEND
);
1001 for (i
= 0; i
< card
->num_rtd
; i
++) {
1002 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1003 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1005 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1008 if (cpu_dai
->driver
->suspend
&& !cpu_dai
->driver
->ac97_control
)
1009 cpu_dai
->driver
->suspend(cpu_dai
);
1010 if (platform
->driver
->suspend
&& !platform
->suspended
) {
1011 platform
->driver
->suspend(cpu_dai
);
1012 platform
->suspended
= 1;
1016 /* close any waiting streams and save state */
1017 for (i
= 0; i
< card
->num_rtd
; i
++) {
1018 run_delayed_work(&card
->rtd
[i
].delayed_work
);
1019 card
->rtd
[i
].codec
->suspend_bias_level
= card
->rtd
[i
].codec
->bias_level
;
1022 for (i
= 0; i
< card
->num_rtd
; i
++) {
1023 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1025 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1028 if (driver
->playback
.stream_name
!= NULL
)
1029 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1030 SND_SOC_DAPM_STREAM_SUSPEND
);
1032 if (driver
->capture
.stream_name
!= NULL
)
1033 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1034 SND_SOC_DAPM_STREAM_SUSPEND
);
1037 /* suspend all CODECs */
1038 for (i
= 0; i
< card
->num_rtd
; i
++) {
1039 struct snd_soc_codec
*codec
= card
->rtd
[i
].codec
;
1040 /* If there are paths active then the CODEC will be held with
1041 * bias _ON and should not be suspended. */
1042 if (!codec
->suspended
&& codec
->driver
->suspend
) {
1043 switch (codec
->bias_level
) {
1044 case SND_SOC_BIAS_STANDBY
:
1045 case SND_SOC_BIAS_OFF
:
1046 codec
->driver
->suspend(codec
, PMSG_SUSPEND
);
1047 codec
->suspended
= 1;
1050 dev_dbg(codec
->dev
, "CODEC is on over suspend\n");
1056 for (i
= 0; i
< card
->num_rtd
; i
++) {
1057 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1059 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1062 if (cpu_dai
->driver
->suspend
&& cpu_dai
->driver
->ac97_control
)
1063 cpu_dai
->driver
->suspend(cpu_dai
);
1066 if (card
->suspend_post
)
1067 card
->suspend_post(pdev
, PMSG_SUSPEND
);
1072 /* deferred resume work, so resume can complete before we finished
1073 * setting our codec back up, which can be very slow on I2C
1075 static void soc_resume_deferred(struct work_struct
*work
)
1077 struct snd_soc_card
*card
=
1078 container_of(work
, struct snd_soc_card
, deferred_resume_work
);
1079 struct platform_device
*pdev
= to_platform_device(card
->dev
);
1082 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1083 * so userspace apps are blocked from touching us
1086 dev_dbg(card
->dev
, "starting resume work\n");
1088 /* Bring us up into D2 so that DAPM starts enabling things */
1089 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D2
);
1091 if (card
->resume_pre
)
1092 card
->resume_pre(pdev
);
1094 /* resume AC97 DAIs */
1095 for (i
= 0; i
< card
->num_rtd
; i
++) {
1096 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1098 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1101 if (cpu_dai
->driver
->resume
&& cpu_dai
->driver
->ac97_control
)
1102 cpu_dai
->driver
->resume(cpu_dai
);
1105 for (i
= 0; i
< card
->num_rtd
; i
++) {
1106 struct snd_soc_codec
*codec
= card
->rtd
[i
].codec
;
1107 /* If the CODEC was idle over suspend then it will have been
1108 * left with bias OFF or STANDBY and suspended so we must now
1109 * resume. Otherwise the suspend was suppressed.
1111 if (codec
->driver
->resume
&& codec
->suspended
) {
1112 switch (codec
->bias_level
) {
1113 case SND_SOC_BIAS_STANDBY
:
1114 case SND_SOC_BIAS_OFF
:
1115 codec
->driver
->resume(codec
);
1116 codec
->suspended
= 0;
1119 dev_dbg(codec
->dev
, "CODEC was on over suspend\n");
1125 for (i
= 0; i
< card
->num_rtd
; i
++) {
1126 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1128 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1131 if (driver
->playback
.stream_name
!= NULL
)
1132 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1133 SND_SOC_DAPM_STREAM_RESUME
);
1135 if (driver
->capture
.stream_name
!= NULL
)
1136 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1137 SND_SOC_DAPM_STREAM_RESUME
);
1140 /* unmute any active DACs */
1141 for (i
= 0; i
< card
->num_rtd
; i
++) {
1142 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
1143 struct snd_soc_dai_driver
*drv
= dai
->driver
;
1145 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1148 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
1149 drv
->ops
->digital_mute(dai
, 0);
1152 for (i
= 0; i
< card
->num_rtd
; i
++) {
1153 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1154 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1156 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1159 if (cpu_dai
->driver
->resume
&& !cpu_dai
->driver
->ac97_control
)
1160 cpu_dai
->driver
->resume(cpu_dai
);
1161 if (platform
->driver
->resume
&& platform
->suspended
) {
1162 platform
->driver
->resume(cpu_dai
);
1163 platform
->suspended
= 0;
1167 if (card
->resume_post
)
1168 card
->resume_post(pdev
);
1170 dev_dbg(card
->dev
, "resume work completed\n");
1172 /* userspace can access us now we are back as we were before */
1173 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D0
);
1176 /* powers up audio subsystem after a suspend */
1177 static int soc_resume(struct device
*dev
)
1179 struct platform_device
*pdev
= to_platform_device(dev
);
1180 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1183 /* AC97 devices might have other drivers hanging off them so
1184 * need to resume immediately. Other drivers don't have that
1185 * problem and may take a substantial amount of time to resume
1186 * due to I/O costs and anti-pop so handle them out of line.
1188 for (i
= 0; i
< card
->num_rtd
; i
++) {
1189 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1190 if (cpu_dai
->driver
->ac97_control
) {
1191 dev_dbg(dev
, "Resuming AC97 immediately\n");
1192 soc_resume_deferred(&card
->deferred_resume_work
);
1194 dev_dbg(dev
, "Scheduling resume work\n");
1195 if (!schedule_work(&card
->deferred_resume_work
))
1196 dev_err(dev
, "resume work item may be lost\n");
1203 #define soc_suspend NULL
1204 #define soc_resume NULL
1207 static struct snd_soc_dai_ops null_dai_ops
= {
1210 static int soc_bind_dai_link(struct snd_soc_card
*card
, int num
)
1212 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1213 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1214 struct snd_soc_codec
*codec
;
1215 struct snd_soc_platform
*platform
;
1216 struct snd_soc_dai
*codec_dai
, *cpu_dai
;
1220 dev_dbg(card
->dev
, "binding %s at idx %d\n", dai_link
->name
, num
);
1222 /* do we already have the CPU DAI for this link ? */
1226 /* no, then find CPU DAI from registered DAIs*/
1227 list_for_each_entry(cpu_dai
, &dai_list
, list
) {
1228 if (!strcmp(cpu_dai
->name
, dai_link
->cpu_dai_name
)) {
1230 if (!try_module_get(cpu_dai
->dev
->driver
->owner
))
1233 rtd
->cpu_dai
= cpu_dai
;
1237 dev_dbg(card
->dev
, "CPU DAI %s not registered\n",
1238 dai_link
->cpu_dai_name
);
1241 /* do we already have the CODEC for this link ? */
1246 /* no, then find CODEC from registered CODECs*/
1247 list_for_each_entry(codec
, &codec_list
, list
) {
1248 if (!strcmp(codec
->name
, dai_link
->codec_name
)) {
1251 if (!try_module_get(codec
->dev
->driver
->owner
))
1254 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1255 list_for_each_entry(codec_dai
, &dai_list
, list
) {
1256 if (codec
->dev
== codec_dai
->dev
&&
1257 !strcmp(codec_dai
->name
, dai_link
->codec_dai_name
)) {
1258 rtd
->codec_dai
= codec_dai
;
1262 dev_dbg(card
->dev
, "CODEC DAI %s not registered\n",
1263 dai_link
->codec_dai_name
);
1268 dev_dbg(card
->dev
, "CODEC %s not registered\n",
1269 dai_link
->codec_name
);
1272 /* do we already have the CODEC DAI for this link ? */
1273 if (rtd
->platform
) {
1276 /* no, then find CPU DAI from registered DAIs*/
1277 list_for_each_entry(platform
, &platform_list
, list
) {
1278 if (!strcmp(platform
->name
, dai_link
->platform_name
)) {
1280 if (!try_module_get(platform
->dev
->driver
->owner
))
1283 rtd
->platform
= platform
;
1288 dev_dbg(card
->dev
, "platform %s not registered\n",
1289 dai_link
->platform_name
);
1293 /* mark rtd as complete if we found all 4 of our client devices */
1294 if (rtd
->codec
&& rtd
->codec_dai
&& rtd
->platform
&& rtd
->cpu_dai
) {
1301 static void soc_remove_dai_link(struct snd_soc_card
*card
, int num
)
1303 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1304 struct snd_soc_codec
*codec
= rtd
->codec
;
1305 struct snd_soc_platform
*platform
= rtd
->platform
;
1306 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1309 /* unregister the rtd device */
1310 if (rtd
->dev_registered
) {
1311 device_remove_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1312 device_unregister(&rtd
->dev
);
1313 rtd
->dev_registered
= 0;
1316 /* remove the CODEC DAI */
1317 if (codec_dai
&& codec_dai
->probed
) {
1318 if (codec_dai
->driver
->remove
) {
1319 err
= codec_dai
->driver
->remove(codec_dai
);
1321 printk(KERN_ERR
"asoc: failed to remove %s\n", codec_dai
->name
);
1323 codec_dai
->probed
= 0;
1324 list_del(&codec_dai
->card_list
);
1327 /* remove the platform */
1328 if (platform
&& platform
->probed
) {
1329 if (platform
->driver
->remove
) {
1330 err
= platform
->driver
->remove(platform
);
1332 printk(KERN_ERR
"asoc: failed to remove %s\n", platform
->name
);
1334 platform
->probed
= 0;
1335 list_del(&platform
->card_list
);
1336 module_put(platform
->dev
->driver
->owner
);
1339 /* remove the CODEC */
1340 if (codec
&& codec
->probed
) {
1341 if (codec
->driver
->remove
) {
1342 err
= codec
->driver
->remove(codec
);
1344 printk(KERN_ERR
"asoc: failed to remove %s\n", codec
->name
);
1347 /* Make sure all DAPM widgets are freed */
1348 snd_soc_dapm_free(codec
);
1350 soc_cleanup_codec_debugfs(codec
);
1351 device_remove_file(&rtd
->dev
, &dev_attr_codec_reg
);
1353 list_del(&codec
->card_list
);
1354 module_put(codec
->dev
->driver
->owner
);
1357 /* remove the cpu_dai */
1358 if (cpu_dai
&& cpu_dai
->probed
) {
1359 if (cpu_dai
->driver
->remove
) {
1360 err
= cpu_dai
->driver
->remove(cpu_dai
);
1362 printk(KERN_ERR
"asoc: failed to remove %s\n", cpu_dai
->name
);
1364 cpu_dai
->probed
= 0;
1365 list_del(&cpu_dai
->card_list
);
1366 module_put(cpu_dai
->dev
->driver
->owner
);
1370 static void rtd_release(struct device
*dev
) {}
1372 static int soc_probe_dai_link(struct snd_soc_card
*card
, int num
)
1374 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1375 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1376 struct snd_soc_codec
*codec
= rtd
->codec
;
1377 struct snd_soc_platform
*platform
= rtd
->platform
;
1378 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1381 dev_dbg(card
->dev
, "probe %s dai link %d\n", card
->name
, num
);
1383 /* config components */
1384 codec_dai
->codec
= codec
;
1386 cpu_dai
->platform
= platform
;
1388 rtd
->dev
.parent
= card
->dev
;
1389 codec_dai
->card
= card
;
1390 cpu_dai
->card
= card
;
1392 /* set default power off timeout */
1393 rtd
->pmdown_time
= pmdown_time
;
1395 /* probe the cpu_dai */
1396 if (!cpu_dai
->probed
) {
1397 if (cpu_dai
->driver
->probe
) {
1398 ret
= cpu_dai
->driver
->probe(cpu_dai
);
1400 printk(KERN_ERR
"asoc: failed to probe CPU DAI %s\n",
1405 cpu_dai
->probed
= 1;
1406 /* mark cpu_dai as probed and add to card cpu_dai list */
1407 list_add(&cpu_dai
->card_list
, &card
->dai_dev_list
);
1410 /* probe the CODEC */
1411 if (!codec
->probed
) {
1412 if (codec
->driver
->probe
) {
1413 ret
= codec
->driver
->probe(codec
);
1415 printk(KERN_ERR
"asoc: failed to probe CODEC %s\n",
1421 soc_init_codec_debugfs(codec
);
1423 /* mark codec as probed and add to card codec list */
1425 list_add(&codec
->card_list
, &card
->codec_dev_list
);
1428 /* probe the platform */
1429 if (!platform
->probed
) {
1430 if (platform
->driver
->probe
) {
1431 ret
= platform
->driver
->probe(platform
);
1433 printk(KERN_ERR
"asoc: failed to probe platform %s\n",
1438 /* mark platform as probed and add to card platform list */
1439 platform
->probed
= 1;
1440 list_add(&platform
->card_list
, &card
->platform_dev_list
);
1443 /* probe the CODEC DAI */
1444 if (!codec_dai
->probed
) {
1445 if (codec_dai
->driver
->probe
) {
1446 ret
= codec_dai
->driver
->probe(codec_dai
);
1448 printk(KERN_ERR
"asoc: failed to probe CODEC DAI %s\n",
1454 /* mark cpu_dai as probed and add to card cpu_dai list */
1455 codec_dai
->probed
= 1;
1456 list_add(&codec_dai
->card_list
, &card
->dai_dev_list
);
1459 /* DAPM dai link stream work */
1460 INIT_DELAYED_WORK(&rtd
->delayed_work
, close_delayed_work
);
1462 /* now that all clients have probed, initialise the DAI link */
1463 if (dai_link
->init
) {
1464 ret
= dai_link
->init(rtd
);
1466 printk(KERN_ERR
"asoc: failed to init %s\n", dai_link
->stream_name
);
1471 /* Make sure all DAPM widgets are instantiated */
1472 snd_soc_dapm_new_widgets(codec
);
1473 snd_soc_dapm_sync(codec
);
1475 /* register the rtd device */
1476 rtd
->dev
.release
= rtd_release
;
1477 rtd
->dev
.init_name
= dai_link
->name
;
1478 ret
= device_register(&rtd
->dev
);
1480 printk(KERN_ERR
"asoc: failed to register DAI runtime device %d\n", ret
);
1484 rtd
->dev_registered
= 1;
1485 ret
= device_create_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1487 printk(KERN_WARNING
"asoc: failed to add pmdown_time sysfs\n");
1489 /* add DAPM sysfs entries for this codec */
1490 ret
= snd_soc_dapm_sys_add(&rtd
->dev
);
1492 printk(KERN_WARNING
"asoc: failed to add codec dapm sysfs entries\n");
1494 /* add codec sysfs entries */
1495 ret
= device_create_file(&rtd
->dev
, &dev_attr_codec_reg
);
1497 printk(KERN_WARNING
"asoc: failed to add codec sysfs files\n");
1499 /* create the pcm */
1500 ret
= soc_new_pcm(rtd
, num
);
1502 printk(KERN_ERR
"asoc: can't create pcm %s\n", dai_link
->stream_name
);
1506 /* add platform data for AC97 devices */
1507 if (rtd
->codec_dai
->driver
->ac97_control
)
1508 snd_ac97_dev_add_pdata(codec
->ac97
, rtd
->cpu_dai
->ac97_pdata
);
1513 #ifdef CONFIG_SND_SOC_AC97_BUS
1514 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime
*rtd
)
1518 /* Only instantiate AC97 if not already done by the adaptor
1519 * for the generic AC97 subsystem.
1521 if (rtd
->codec_dai
->driver
->ac97_control
&& !rtd
->codec
->ac97_registered
) {
1523 * It is possible that the AC97 device is already registered to
1524 * the device subsystem. This happens when the device is created
1525 * via snd_ac97_mixer(). Currently only SoC codec that does so
1526 * is the generic AC97 glue but others migh emerge.
1528 * In those cases we don't try to register the device again.
1530 if (!rtd
->codec
->ac97_created
)
1533 ret
= soc_ac97_dev_register(rtd
->codec
);
1535 printk(KERN_ERR
"asoc: AC97 device register failed\n");
1539 rtd
->codec
->ac97_registered
= 1;
1544 static void soc_unregister_ac97_dai_link(struct snd_soc_codec
*codec
)
1546 if (codec
->ac97_registered
) {
1547 soc_ac97_dev_unregister(codec
);
1548 codec
->ac97_registered
= 0;
1553 static void snd_soc_instantiate_card(struct snd_soc_card
*card
)
1555 struct platform_device
*pdev
= to_platform_device(card
->dev
);
1558 mutex_lock(&card
->mutex
);
1560 if (card
->instantiated
) {
1561 mutex_unlock(&card
->mutex
);
1566 for (i
= 0; i
< card
->num_links
; i
++)
1567 soc_bind_dai_link(card
, i
);
1569 /* bind completed ? */
1570 if (card
->num_rtd
!= card
->num_links
) {
1571 mutex_unlock(&card
->mutex
);
1575 /* card bind complete so register a sound card */
1576 ret
= snd_card_create(SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
1577 card
->owner
, 0, &card
->snd_card
);
1579 printk(KERN_ERR
"asoc: can't create sound card for card %s\n",
1581 mutex_unlock(&card
->mutex
);
1584 card
->snd_card
->dev
= card
->dev
;
1587 /* deferred resume work */
1588 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
1591 /* initialise the sound card only once */
1593 ret
= card
->probe(pdev
);
1595 goto card_probe_error
;
1598 for (i
= 0; i
< card
->num_links
; i
++) {
1599 ret
= soc_probe_dai_link(card
, i
);
1601 pr_err("asoc: failed to instantiate card %s: %d\n",
1607 snprintf(card
->snd_card
->shortname
, sizeof(card
->snd_card
->shortname
),
1609 snprintf(card
->snd_card
->longname
, sizeof(card
->snd_card
->longname
),
1612 ret
= snd_card_register(card
->snd_card
);
1614 printk(KERN_ERR
"asoc: failed to register soundcard for %s\n", card
->name
);
1618 #ifdef CONFIG_SND_SOC_AC97_BUS
1619 /* register any AC97 codecs */
1620 for (i
= 0; i
< card
->num_rtd
; i
++) {
1621 ret
= soc_register_ac97_dai_link(&card
->rtd
[i
]);
1623 printk(KERN_ERR
"asoc: failed to register AC97 %s\n", card
->name
);
1629 card
->instantiated
= 1;
1630 mutex_unlock(&card
->mutex
);
1634 for (i
= 0; i
< card
->num_links
; i
++)
1635 soc_remove_dai_link(card
, i
);
1641 snd_card_free(card
->snd_card
);
1643 mutex_unlock(&card
->mutex
);
1647 * Attempt to initialise any uninitialised cards. Must be called with
1650 static void snd_soc_instantiate_cards(void)
1652 struct snd_soc_card
*card
;
1653 list_for_each_entry(card
, &card_list
, list
)
1654 snd_soc_instantiate_card(card
);
1657 /* probes a new socdev */
1658 static int soc_probe(struct platform_device
*pdev
)
1660 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1663 /* Bodge while we unpick instantiation */
1664 card
->dev
= &pdev
->dev
;
1665 INIT_LIST_HEAD(&card
->dai_dev_list
);
1666 INIT_LIST_HEAD(&card
->codec_dev_list
);
1667 INIT_LIST_HEAD(&card
->platform_dev_list
);
1669 ret
= snd_soc_register_card(card
);
1671 dev_err(&pdev
->dev
, "Failed to register card\n");
1678 /* removes a socdev */
1679 static int soc_remove(struct platform_device
*pdev
)
1681 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1684 if (card
->instantiated
) {
1686 /* make sure any delayed work runs */
1687 for (i
= 0; i
< card
->num_rtd
; i
++) {
1688 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1689 run_delayed_work(&rtd
->delayed_work
);
1692 /* remove and free each DAI */
1693 for (i
= 0; i
< card
->num_rtd
; i
++)
1694 soc_remove_dai_link(card
, i
);
1696 /* remove the card */
1701 snd_card_free(card
->snd_card
);
1703 snd_soc_unregister_card(card
);
1707 static int soc_poweroff(struct device
*dev
)
1709 struct platform_device
*pdev
= to_platform_device(dev
);
1710 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1713 if (!card
->instantiated
)
1716 /* Flush out pmdown_time work - we actually do want to run it
1717 * now, we're shutting down so no imminent restart. */
1718 for (i
= 0; i
< card
->num_rtd
; i
++) {
1719 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1720 run_delayed_work(&rtd
->delayed_work
);
1723 snd_soc_dapm_shutdown(card
);
1728 static const struct dev_pm_ops soc_pm_ops
= {
1729 .suspend
= soc_suspend
,
1730 .resume
= soc_resume
,
1731 .poweroff
= soc_poweroff
,
1734 /* ASoC platform driver */
1735 static struct platform_driver soc_driver
= {
1737 .name
= "soc-audio",
1738 .owner
= THIS_MODULE
,
1742 .remove
= soc_remove
,
1745 /* create a new pcm */
1746 static int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
)
1748 struct snd_soc_codec
*codec
= rtd
->codec
;
1749 struct snd_soc_platform
*platform
= rtd
->platform
;
1750 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
1751 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1752 struct snd_pcm
*pcm
;
1754 int ret
= 0, playback
= 0, capture
= 0;
1756 /* check client and interface hw capabilities */
1757 snprintf(new_name
, sizeof(new_name
), "%s %s-%d",
1758 rtd
->dai_link
->stream_name
, codec_dai
->name
, num
);
1760 if (codec_dai
->driver
->playback
.channels_min
)
1762 if (codec_dai
->driver
->capture
.channels_min
)
1765 dev_dbg(rtd
->card
->dev
, "registered pcm #%d %s\n",num
,new_name
);
1766 ret
= snd_pcm_new(rtd
->card
->snd_card
, new_name
,
1767 num
, playback
, capture
, &pcm
);
1769 printk(KERN_ERR
"asoc: can't create pcm for codec %s\n", codec
->name
);
1774 pcm
->private_data
= rtd
;
1775 soc_pcm_ops
.mmap
= platform
->driver
->ops
->mmap
;
1776 soc_pcm_ops
.pointer
= platform
->driver
->ops
->pointer
;
1777 soc_pcm_ops
.ioctl
= platform
->driver
->ops
->ioctl
;
1778 soc_pcm_ops
.copy
= platform
->driver
->ops
->copy
;
1779 soc_pcm_ops
.silence
= platform
->driver
->ops
->silence
;
1780 soc_pcm_ops
.ack
= platform
->driver
->ops
->ack
;
1781 soc_pcm_ops
.page
= platform
->driver
->ops
->page
;
1784 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &soc_pcm_ops
);
1787 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &soc_pcm_ops
);
1789 ret
= platform
->driver
->pcm_new(rtd
->card
->snd_card
, codec_dai
, pcm
);
1791 printk(KERN_ERR
"asoc: platform pcm constructor failed\n");
1795 pcm
->private_free
= platform
->driver
->pcm_free
;
1796 printk(KERN_INFO
"asoc: %s <-> %s mapping ok\n", codec_dai
->name
,
1802 * snd_soc_codec_volatile_register: Report if a register is volatile.
1804 * @codec: CODEC to query.
1805 * @reg: Register to query.
1807 * Boolean function indiciating if a CODEC register is volatile.
1809 int snd_soc_codec_volatile_register(struct snd_soc_codec
*codec
, int reg
)
1811 if (codec
->driver
->volatile_register
)
1812 return codec
->driver
->volatile_register(reg
);
1816 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register
);
1819 * snd_soc_new_ac97_codec - initailise AC97 device
1820 * @codec: audio codec
1821 * @ops: AC97 bus operations
1822 * @num: AC97 codec number
1824 * Initialises AC97 codec resources for use by ad-hoc devices only.
1826 int snd_soc_new_ac97_codec(struct snd_soc_codec
*codec
,
1827 struct snd_ac97_bus_ops
*ops
, int num
)
1829 mutex_lock(&codec
->mutex
);
1831 codec
->ac97
= kzalloc(sizeof(struct snd_ac97
), GFP_KERNEL
);
1832 if (codec
->ac97
== NULL
) {
1833 mutex_unlock(&codec
->mutex
);
1837 codec
->ac97
->bus
= kzalloc(sizeof(struct snd_ac97_bus
), GFP_KERNEL
);
1838 if (codec
->ac97
->bus
== NULL
) {
1841 mutex_unlock(&codec
->mutex
);
1845 codec
->ac97
->bus
->ops
= ops
;
1846 codec
->ac97
->num
= num
;
1849 * Mark the AC97 device to be created by us. This way we ensure that the
1850 * device will be registered with the device subsystem later on.
1852 codec
->ac97_created
= 1;
1854 mutex_unlock(&codec
->mutex
);
1857 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec
);
1860 * snd_soc_free_ac97_codec - free AC97 codec device
1861 * @codec: audio codec
1863 * Frees AC97 codec device resources.
1865 void snd_soc_free_ac97_codec(struct snd_soc_codec
*codec
)
1867 mutex_lock(&codec
->mutex
);
1868 #ifdef CONFIG_SND_SOC_AC97_BUS
1869 soc_unregister_ac97_dai_link(codec
);
1871 kfree(codec
->ac97
->bus
);
1874 codec
->ac97_created
= 0;
1875 mutex_unlock(&codec
->mutex
);
1877 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec
);
1880 * snd_soc_update_bits - update codec register bits
1881 * @codec: audio codec
1882 * @reg: codec register
1883 * @mask: register mask
1886 * Writes new register value.
1888 * Returns 1 for change else 0.
1890 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1891 unsigned int mask
, unsigned int value
)
1894 unsigned int old
, new;
1896 old
= snd_soc_read(codec
, reg
);
1897 new = (old
& ~mask
) | value
;
1898 change
= old
!= new;
1900 snd_soc_write(codec
, reg
, new);
1904 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
1907 * snd_soc_update_bits_locked - update codec register bits
1908 * @codec: audio codec
1909 * @reg: codec register
1910 * @mask: register mask
1913 * Writes new register value, and takes the codec mutex.
1915 * Returns 1 for change else 0.
1917 int snd_soc_update_bits_locked(struct snd_soc_codec
*codec
,
1918 unsigned short reg
, unsigned int mask
,
1923 mutex_lock(&codec
->mutex
);
1924 change
= snd_soc_update_bits(codec
, reg
, mask
, value
);
1925 mutex_unlock(&codec
->mutex
);
1929 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked
);
1932 * snd_soc_test_bits - test register for change
1933 * @codec: audio codec
1934 * @reg: codec register
1935 * @mask: register mask
1938 * Tests a register with a new value and checks if the new value is
1939 * different from the old value.
1941 * Returns 1 for change else 0.
1943 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1944 unsigned int mask
, unsigned int value
)
1947 unsigned int old
, new;
1949 old
= snd_soc_read(codec
, reg
);
1950 new = (old
& ~mask
) | value
;
1951 change
= old
!= new;
1955 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
1958 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1959 * @substream: the pcm substream
1960 * @hw: the hardware parameters
1962 * Sets the substream runtime hardware parameters.
1964 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
1965 const struct snd_pcm_hardware
*hw
)
1967 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1968 runtime
->hw
.info
= hw
->info
;
1969 runtime
->hw
.formats
= hw
->formats
;
1970 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
1971 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
1972 runtime
->hw
.periods_min
= hw
->periods_min
;
1973 runtime
->hw
.periods_max
= hw
->periods_max
;
1974 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
1975 runtime
->hw
.fifo_size
= hw
->fifo_size
;
1978 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
1981 * snd_soc_cnew - create new control
1982 * @_template: control template
1983 * @data: control private data
1984 * @long_name: control long name
1986 * Create a new mixer control from a template control.
1988 * Returns 0 for success, else error.
1990 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
1991 void *data
, char *long_name
)
1993 struct snd_kcontrol_new
template;
1995 memcpy(&template, _template
, sizeof(template));
1997 template.name
= long_name
;
2000 return snd_ctl_new1(&template, data
);
2002 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
2005 * snd_soc_add_controls - add an array of controls to a codec.
2006 * Convienience function to add a list of controls. Many codecs were
2007 * duplicating this code.
2009 * @codec: codec to add controls to
2010 * @controls: array of controls to add
2011 * @num_controls: number of elements in the array
2013 * Return 0 for success, else error.
2015 int snd_soc_add_controls(struct snd_soc_codec
*codec
,
2016 const struct snd_kcontrol_new
*controls
, int num_controls
)
2018 struct snd_card
*card
= codec
->card
->snd_card
;
2021 for (i
= 0; i
< num_controls
; i
++) {
2022 const struct snd_kcontrol_new
*control
= &controls
[i
];
2023 err
= snd_ctl_add(card
, snd_soc_cnew(control
, codec
, NULL
));
2025 dev_err(codec
->dev
, "%s: Failed to add %s: %d\n",
2026 codec
->name
, control
->name
, err
);
2033 EXPORT_SYMBOL_GPL(snd_soc_add_controls
);
2036 * snd_soc_info_enum_double - enumerated double mixer info callback
2037 * @kcontrol: mixer control
2038 * @uinfo: control element information
2040 * Callback to provide information about a double enumerated
2043 * Returns 0 for success.
2045 int snd_soc_info_enum_double(struct snd_kcontrol
*kcontrol
,
2046 struct snd_ctl_elem_info
*uinfo
)
2048 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2050 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2051 uinfo
->count
= e
->shift_l
== e
->shift_r
? 1 : 2;
2052 uinfo
->value
.enumerated
.items
= e
->max
;
2054 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2055 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2056 strcpy(uinfo
->value
.enumerated
.name
,
2057 e
->texts
[uinfo
->value
.enumerated
.item
]);
2060 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double
);
2063 * snd_soc_get_enum_double - enumerated double mixer get callback
2064 * @kcontrol: mixer control
2065 * @ucontrol: control element information
2067 * Callback to get the value of a double enumerated mixer.
2069 * Returns 0 for success.
2071 int snd_soc_get_enum_double(struct snd_kcontrol
*kcontrol
,
2072 struct snd_ctl_elem_value
*ucontrol
)
2074 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2075 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2076 unsigned int val
, bitmask
;
2078 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2080 val
= snd_soc_read(codec
, e
->reg
);
2081 ucontrol
->value
.enumerated
.item
[0]
2082 = (val
>> e
->shift_l
) & (bitmask
- 1);
2083 if (e
->shift_l
!= e
->shift_r
)
2084 ucontrol
->value
.enumerated
.item
[1] =
2085 (val
>> e
->shift_r
) & (bitmask
- 1);
2089 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double
);
2092 * snd_soc_put_enum_double - enumerated double mixer put callback
2093 * @kcontrol: mixer control
2094 * @ucontrol: control element information
2096 * Callback to set the value of a double enumerated mixer.
2098 * Returns 0 for success.
2100 int snd_soc_put_enum_double(struct snd_kcontrol
*kcontrol
,
2101 struct snd_ctl_elem_value
*ucontrol
)
2103 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2104 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2106 unsigned int mask
, bitmask
;
2108 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2110 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2112 val
= ucontrol
->value
.enumerated
.item
[0] << e
->shift_l
;
2113 mask
= (bitmask
- 1) << e
->shift_l
;
2114 if (e
->shift_l
!= e
->shift_r
) {
2115 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2117 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2118 mask
|= (bitmask
- 1) << e
->shift_r
;
2121 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2123 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double
);
2126 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2127 * @kcontrol: mixer control
2128 * @ucontrol: control element information
2130 * Callback to get the value of a double semi enumerated mixer.
2132 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2133 * used for handling bitfield coded enumeration for example.
2135 * Returns 0 for success.
2137 int snd_soc_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2138 struct snd_ctl_elem_value
*ucontrol
)
2140 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2141 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2142 unsigned int reg_val
, val
, mux
;
2144 reg_val
= snd_soc_read(codec
, e
->reg
);
2145 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2146 for (mux
= 0; mux
< e
->max
; mux
++) {
2147 if (val
== e
->values
[mux
])
2150 ucontrol
->value
.enumerated
.item
[0] = mux
;
2151 if (e
->shift_l
!= e
->shift_r
) {
2152 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2153 for (mux
= 0; mux
< e
->max
; mux
++) {
2154 if (val
== e
->values
[mux
])
2157 ucontrol
->value
.enumerated
.item
[1] = mux
;
2162 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double
);
2165 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2166 * @kcontrol: mixer control
2167 * @ucontrol: control element information
2169 * Callback to set the value of a double semi enumerated mixer.
2171 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2172 * used for handling bitfield coded enumeration for example.
2174 * Returns 0 for success.
2176 int snd_soc_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2177 struct snd_ctl_elem_value
*ucontrol
)
2179 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2180 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2184 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2186 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2187 mask
= e
->mask
<< e
->shift_l
;
2188 if (e
->shift_l
!= e
->shift_r
) {
2189 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2191 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2192 mask
|= e
->mask
<< e
->shift_r
;
2195 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2197 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double
);
2200 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2201 * @kcontrol: mixer control
2202 * @uinfo: control element information
2204 * Callback to provide information about an external enumerated
2207 * Returns 0 for success.
2209 int snd_soc_info_enum_ext(struct snd_kcontrol
*kcontrol
,
2210 struct snd_ctl_elem_info
*uinfo
)
2212 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2214 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2216 uinfo
->value
.enumerated
.items
= e
->max
;
2218 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2219 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2220 strcpy(uinfo
->value
.enumerated
.name
,
2221 e
->texts
[uinfo
->value
.enumerated
.item
]);
2224 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext
);
2227 * snd_soc_info_volsw_ext - external single mixer info callback
2228 * @kcontrol: mixer control
2229 * @uinfo: control element information
2231 * Callback to provide information about a single external mixer control.
2233 * Returns 0 for success.
2235 int snd_soc_info_volsw_ext(struct snd_kcontrol
*kcontrol
,
2236 struct snd_ctl_elem_info
*uinfo
)
2238 int max
= kcontrol
->private_value
;
2240 if (max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2241 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2243 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2246 uinfo
->value
.integer
.min
= 0;
2247 uinfo
->value
.integer
.max
= max
;
2250 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext
);
2253 * snd_soc_info_volsw - single mixer info callback
2254 * @kcontrol: mixer control
2255 * @uinfo: control element information
2257 * Callback to provide information about a single mixer control.
2259 * Returns 0 for success.
2261 int snd_soc_info_volsw(struct snd_kcontrol
*kcontrol
,
2262 struct snd_ctl_elem_info
*uinfo
)
2264 struct soc_mixer_control
*mc
=
2265 (struct soc_mixer_control
*)kcontrol
->private_value
;
2267 unsigned int shift
= mc
->shift
;
2268 unsigned int rshift
= mc
->rshift
;
2270 if (!mc
->platform_max
)
2271 mc
->platform_max
= mc
->max
;
2272 platform_max
= mc
->platform_max
;
2274 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2275 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2277 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2279 uinfo
->count
= shift
== rshift
? 1 : 2;
2280 uinfo
->value
.integer
.min
= 0;
2281 uinfo
->value
.integer
.max
= platform_max
;
2284 EXPORT_SYMBOL_GPL(snd_soc_info_volsw
);
2287 * snd_soc_get_volsw - single mixer get callback
2288 * @kcontrol: mixer control
2289 * @ucontrol: control element information
2291 * Callback to get the value of a single mixer control.
2293 * Returns 0 for success.
2295 int snd_soc_get_volsw(struct snd_kcontrol
*kcontrol
,
2296 struct snd_ctl_elem_value
*ucontrol
)
2298 struct soc_mixer_control
*mc
=
2299 (struct soc_mixer_control
*)kcontrol
->private_value
;
2300 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2301 unsigned int reg
= mc
->reg
;
2302 unsigned int shift
= mc
->shift
;
2303 unsigned int rshift
= mc
->rshift
;
2305 unsigned int mask
= (1 << fls(max
)) - 1;
2306 unsigned int invert
= mc
->invert
;
2308 ucontrol
->value
.integer
.value
[0] =
2309 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2310 if (shift
!= rshift
)
2311 ucontrol
->value
.integer
.value
[1] =
2312 (snd_soc_read(codec
, reg
) >> rshift
) & mask
;
2314 ucontrol
->value
.integer
.value
[0] =
2315 max
- ucontrol
->value
.integer
.value
[0];
2316 if (shift
!= rshift
)
2317 ucontrol
->value
.integer
.value
[1] =
2318 max
- ucontrol
->value
.integer
.value
[1];
2323 EXPORT_SYMBOL_GPL(snd_soc_get_volsw
);
2326 * snd_soc_put_volsw - single mixer put callback
2327 * @kcontrol: mixer control
2328 * @ucontrol: control element information
2330 * Callback to set the value of a single mixer control.
2332 * Returns 0 for success.
2334 int snd_soc_put_volsw(struct snd_kcontrol
*kcontrol
,
2335 struct snd_ctl_elem_value
*ucontrol
)
2337 struct soc_mixer_control
*mc
=
2338 (struct soc_mixer_control
*)kcontrol
->private_value
;
2339 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2340 unsigned int reg
= mc
->reg
;
2341 unsigned int shift
= mc
->shift
;
2342 unsigned int rshift
= mc
->rshift
;
2344 unsigned int mask
= (1 << fls(max
)) - 1;
2345 unsigned int invert
= mc
->invert
;
2346 unsigned int val
, val2
, val_mask
;
2348 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2351 val_mask
= mask
<< shift
;
2353 if (shift
!= rshift
) {
2354 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2357 val_mask
|= mask
<< rshift
;
2358 val
|= val2
<< rshift
;
2360 return snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2362 EXPORT_SYMBOL_GPL(snd_soc_put_volsw
);
2365 * snd_soc_info_volsw_2r - double mixer info callback
2366 * @kcontrol: mixer control
2367 * @uinfo: control element information
2369 * Callback to provide information about a double mixer control that
2370 * spans 2 codec registers.
2372 * Returns 0 for success.
2374 int snd_soc_info_volsw_2r(struct snd_kcontrol
*kcontrol
,
2375 struct snd_ctl_elem_info
*uinfo
)
2377 struct soc_mixer_control
*mc
=
2378 (struct soc_mixer_control
*)kcontrol
->private_value
;
2381 if (!mc
->platform_max
)
2382 mc
->platform_max
= mc
->max
;
2383 platform_max
= mc
->platform_max
;
2385 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2386 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2388 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2391 uinfo
->value
.integer
.min
= 0;
2392 uinfo
->value
.integer
.max
= platform_max
;
2395 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r
);
2398 * snd_soc_get_volsw_2r - double mixer get callback
2399 * @kcontrol: mixer control
2400 * @ucontrol: control element information
2402 * Callback to get the value of a double mixer control that spans 2 registers.
2404 * Returns 0 for success.
2406 int snd_soc_get_volsw_2r(struct snd_kcontrol
*kcontrol
,
2407 struct snd_ctl_elem_value
*ucontrol
)
2409 struct soc_mixer_control
*mc
=
2410 (struct soc_mixer_control
*)kcontrol
->private_value
;
2411 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2412 unsigned int reg
= mc
->reg
;
2413 unsigned int reg2
= mc
->rreg
;
2414 unsigned int shift
= mc
->shift
;
2416 unsigned int mask
= (1 << fls(max
)) - 1;
2417 unsigned int invert
= mc
->invert
;
2419 ucontrol
->value
.integer
.value
[0] =
2420 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2421 ucontrol
->value
.integer
.value
[1] =
2422 (snd_soc_read(codec
, reg2
) >> shift
) & mask
;
2424 ucontrol
->value
.integer
.value
[0] =
2425 max
- ucontrol
->value
.integer
.value
[0];
2426 ucontrol
->value
.integer
.value
[1] =
2427 max
- ucontrol
->value
.integer
.value
[1];
2432 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r
);
2435 * snd_soc_put_volsw_2r - double mixer set callback
2436 * @kcontrol: mixer control
2437 * @ucontrol: control element information
2439 * Callback to set the value of a double mixer control that spans 2 registers.
2441 * Returns 0 for success.
2443 int snd_soc_put_volsw_2r(struct snd_kcontrol
*kcontrol
,
2444 struct snd_ctl_elem_value
*ucontrol
)
2446 struct soc_mixer_control
*mc
=
2447 (struct soc_mixer_control
*)kcontrol
->private_value
;
2448 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2449 unsigned int reg
= mc
->reg
;
2450 unsigned int reg2
= mc
->rreg
;
2451 unsigned int shift
= mc
->shift
;
2453 unsigned int mask
= (1 << fls(max
)) - 1;
2454 unsigned int invert
= mc
->invert
;
2456 unsigned int val
, val2
, val_mask
;
2458 val_mask
= mask
<< shift
;
2459 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2460 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2468 val2
= val2
<< shift
;
2470 err
= snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2474 err
= snd_soc_update_bits_locked(codec
, reg2
, val_mask
, val2
);
2477 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r
);
2480 * snd_soc_info_volsw_s8 - signed mixer info callback
2481 * @kcontrol: mixer control
2482 * @uinfo: control element information
2484 * Callback to provide information about a signed mixer control.
2486 * Returns 0 for success.
2488 int snd_soc_info_volsw_s8(struct snd_kcontrol
*kcontrol
,
2489 struct snd_ctl_elem_info
*uinfo
)
2491 struct soc_mixer_control
*mc
=
2492 (struct soc_mixer_control
*)kcontrol
->private_value
;
2496 if (!mc
->platform_max
)
2497 mc
->platform_max
= mc
->max
;
2498 platform_max
= mc
->platform_max
;
2500 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2502 uinfo
->value
.integer
.min
= 0;
2503 uinfo
->value
.integer
.max
= platform_max
- min
;
2506 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8
);
2509 * snd_soc_get_volsw_s8 - signed mixer get callback
2510 * @kcontrol: mixer control
2511 * @ucontrol: control element information
2513 * Callback to get the value of a signed mixer control.
2515 * Returns 0 for success.
2517 int snd_soc_get_volsw_s8(struct snd_kcontrol
*kcontrol
,
2518 struct snd_ctl_elem_value
*ucontrol
)
2520 struct soc_mixer_control
*mc
=
2521 (struct soc_mixer_control
*)kcontrol
->private_value
;
2522 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2523 unsigned int reg
= mc
->reg
;
2525 int val
= snd_soc_read(codec
, reg
);
2527 ucontrol
->value
.integer
.value
[0] =
2528 ((signed char)(val
& 0xff))-min
;
2529 ucontrol
->value
.integer
.value
[1] =
2530 ((signed char)((val
>> 8) & 0xff))-min
;
2533 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8
);
2536 * snd_soc_put_volsw_sgn - signed mixer put callback
2537 * @kcontrol: mixer control
2538 * @ucontrol: control element information
2540 * Callback to set the value of a signed mixer control.
2542 * Returns 0 for success.
2544 int snd_soc_put_volsw_s8(struct snd_kcontrol
*kcontrol
,
2545 struct snd_ctl_elem_value
*ucontrol
)
2547 struct soc_mixer_control
*mc
=
2548 (struct soc_mixer_control
*)kcontrol
->private_value
;
2549 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2550 unsigned int reg
= mc
->reg
;
2554 val
= (ucontrol
->value
.integer
.value
[0]+min
) & 0xff;
2555 val
|= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff) << 8;
2557 return snd_soc_update_bits_locked(codec
, reg
, 0xffff, val
);
2559 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8
);
2562 * snd_soc_limit_volume - Set new limit to an existing volume control.
2564 * @codec: where to look for the control
2565 * @name: Name of the control
2566 * @max: new maximum limit
2568 * Return 0 for success, else error.
2570 int snd_soc_limit_volume(struct snd_soc_codec
*codec
,
2571 const char *name
, int max
)
2573 struct snd_card
*card
= codec
->card
->snd_card
;
2574 struct snd_kcontrol
*kctl
;
2575 struct soc_mixer_control
*mc
;
2579 /* Sanity check for name and max */
2580 if (unlikely(!name
|| max
<= 0))
2583 list_for_each_entry(kctl
, &card
->controls
, list
) {
2584 if (!strncmp(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
))) {
2590 mc
= (struct soc_mixer_control
*)kctl
->private_value
;
2591 if (max
<= mc
->max
) {
2592 mc
->platform_max
= max
;
2598 EXPORT_SYMBOL_GPL(snd_soc_limit_volume
);
2601 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2602 * mixer info callback
2603 * @kcontrol: mixer control
2604 * @uinfo: control element information
2606 * Returns 0 for success.
2608 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2609 struct snd_ctl_elem_info
*uinfo
)
2611 struct soc_mixer_control
*mc
=
2612 (struct soc_mixer_control
*)kcontrol
->private_value
;
2616 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2618 uinfo
->value
.integer
.min
= 0;
2619 uinfo
->value
.integer
.max
= max
-min
;
2623 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx
);
2626 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2627 * mixer get callback
2628 * @kcontrol: mixer control
2629 * @uinfo: control element information
2631 * Returns 0 for success.
2633 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2634 struct snd_ctl_elem_value
*ucontrol
)
2636 struct soc_mixer_control
*mc
=
2637 (struct soc_mixer_control
*)kcontrol
->private_value
;
2638 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2639 unsigned int mask
= (1<<mc
->shift
)-1;
2641 int val
= snd_soc_read(codec
, mc
->reg
) & mask
;
2642 int valr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2644 ucontrol
->value
.integer
.value
[0] = ((val
& 0xff)-min
) & mask
;
2645 ucontrol
->value
.integer
.value
[1] = ((valr
& 0xff)-min
) & mask
;
2648 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx
);
2651 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2652 * mixer put callback
2653 * @kcontrol: mixer control
2654 * @uinfo: control element information
2656 * Returns 0 for success.
2658 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2659 struct snd_ctl_elem_value
*ucontrol
)
2661 struct soc_mixer_control
*mc
=
2662 (struct soc_mixer_control
*)kcontrol
->private_value
;
2663 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2664 unsigned int mask
= (1<<mc
->shift
)-1;
2667 unsigned int val
, valr
, oval
, ovalr
;
2669 val
= ((ucontrol
->value
.integer
.value
[0]+min
) & 0xff);
2671 valr
= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff);
2674 oval
= snd_soc_read(codec
, mc
->reg
) & mask
;
2675 ovalr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2679 ret
= snd_soc_write(codec
, mc
->reg
, val
);
2683 if (ovalr
!= valr
) {
2684 ret
= snd_soc_write(codec
, mc
->rreg
, valr
);
2691 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx
);
2694 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2696 * @clk_id: DAI specific clock ID
2697 * @freq: new clock frequency in Hz
2698 * @dir: new clock direction - input/output.
2700 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2702 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
2703 unsigned int freq
, int dir
)
2705 if (dai
->driver
&& dai
->driver
->ops
->set_sysclk
)
2706 return dai
->driver
->ops
->set_sysclk(dai
, clk_id
, freq
, dir
);
2710 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
2713 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2715 * @div_id: DAI specific clock divider ID
2716 * @div: new clock divisor.
2718 * Configures the clock dividers. This is used to derive the best DAI bit and
2719 * frame clocks from the system or master clock. It's best to set the DAI bit
2720 * and frame clocks as low as possible to save system power.
2722 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
2723 int div_id
, int div
)
2725 if (dai
->driver
&& dai
->driver
->ops
->set_clkdiv
)
2726 return dai
->driver
->ops
->set_clkdiv(dai
, div_id
, div
);
2730 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
2733 * snd_soc_dai_set_pll - configure DAI PLL.
2735 * @pll_id: DAI specific PLL ID
2736 * @source: DAI specific source for the PLL
2737 * @freq_in: PLL input clock frequency in Hz
2738 * @freq_out: requested PLL output clock frequency in Hz
2740 * Configures and enables PLL to generate output clock based on input clock.
2742 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
2743 unsigned int freq_in
, unsigned int freq_out
)
2745 if (dai
->driver
&& dai
->driver
->ops
->set_pll
)
2746 return dai
->driver
->ops
->set_pll(dai
, pll_id
, source
,
2751 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
2754 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2756 * @fmt: SND_SOC_DAIFMT_ format value.
2758 * Configures the DAI hardware format and clocking.
2760 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
2762 if (dai
->driver
&& dai
->driver
->ops
->set_fmt
)
2763 return dai
->driver
->ops
->set_fmt(dai
, fmt
);
2767 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
2770 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2772 * @tx_mask: bitmask representing active TX slots.
2773 * @rx_mask: bitmask representing active RX slots.
2774 * @slots: Number of slots in use.
2775 * @slot_width: Width in bits for each slot.
2777 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2780 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
2781 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
2783 if (dai
->driver
&& dai
->driver
->ops
->set_tdm_slot
)
2784 return dai
->driver
->ops
->set_tdm_slot(dai
, tx_mask
, rx_mask
,
2789 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
2792 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2794 * @tx_num: how many TX channels
2795 * @tx_slot: pointer to an array which imply the TX slot number channel
2797 * @rx_num: how many RX channels
2798 * @rx_slot: pointer to an array which imply the RX slot number channel
2801 * configure the relationship between channel number and TDM slot number.
2803 int snd_soc_dai_set_channel_map(struct snd_soc_dai
*dai
,
2804 unsigned int tx_num
, unsigned int *tx_slot
,
2805 unsigned int rx_num
, unsigned int *rx_slot
)
2807 if (dai
->driver
&& dai
->driver
->ops
->set_channel_map
)
2808 return dai
->driver
->ops
->set_channel_map(dai
, tx_num
, tx_slot
,
2813 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map
);
2816 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2818 * @tristate: tristate enable
2820 * Tristates the DAI so that others can use it.
2822 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
2824 if (dai
->driver
&& dai
->driver
->ops
->set_tristate
)
2825 return dai
->driver
->ops
->set_tristate(dai
, tristate
);
2829 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
2832 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2834 * @mute: mute enable
2836 * Mutes the DAI DAC.
2838 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
)
2840 if (dai
->driver
&& dai
->driver
->ops
->digital_mute
)
2841 return dai
->driver
->ops
->digital_mute(dai
, mute
);
2845 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
2848 * snd_soc_register_card - Register a card with the ASoC core
2850 * @card: Card to register
2852 * Note that currently this is an internal only function: it will be
2853 * exposed to machine drivers after further backporting of ASoC v2
2854 * registration APIs.
2856 static int snd_soc_register_card(struct snd_soc_card
*card
)
2860 if (!card
->name
|| !card
->dev
)
2863 card
->rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
) * card
->num_links
,
2865 if (card
->rtd
== NULL
)
2868 for (i
= 0; i
< card
->num_links
; i
++)
2869 card
->rtd
[i
].dai_link
= &card
->dai_link
[i
];
2871 INIT_LIST_HEAD(&card
->list
);
2872 card
->instantiated
= 0;
2873 mutex_init(&card
->mutex
);
2875 mutex_lock(&client_mutex
);
2876 list_add(&card
->list
, &card_list
);
2877 snd_soc_instantiate_cards();
2878 mutex_unlock(&client_mutex
);
2880 dev_dbg(card
->dev
, "Registered card '%s'\n", card
->name
);
2886 * snd_soc_unregister_card - Unregister a card with the ASoC core
2888 * @card: Card to unregister
2890 * Note that currently this is an internal only function: it will be
2891 * exposed to machine drivers after further backporting of ASoC v2
2892 * registration APIs.
2894 static int snd_soc_unregister_card(struct snd_soc_card
*card
)
2896 mutex_lock(&client_mutex
);
2897 list_del(&card
->list
);
2898 mutex_unlock(&client_mutex
);
2899 dev_dbg(card
->dev
, "Unregistered card '%s'\n", card
->name
);
2905 * Simplify DAI link configuration by removing ".-1" from device names
2906 * and sanitizing names.
2908 static inline char *fmt_single_name(struct device
*dev
, int *id
)
2910 char *found
, name
[NAME_SIZE
];
2913 if (dev_name(dev
) == NULL
)
2916 strncpy(name
, dev_name(dev
), NAME_SIZE
);
2918 /* are we a "%s.%d" name (platform and SPI components) */
2919 found
= strstr(name
, dev
->driver
->name
);
2922 if (sscanf(&found
[strlen(dev
->driver
->name
)], ".%d", id
) == 1) {
2924 /* discard ID from name if ID == -1 */
2926 found
[strlen(dev
->driver
->name
)] = '\0';
2930 /* I2C component devices are named "bus-addr" */
2931 if (sscanf(name
, "%x-%x", &id1
, &id2
) == 2) {
2932 char tmp
[NAME_SIZE
];
2934 /* create unique ID number from I2C addr and bus */
2935 *id
= ((id1
& 0xffff) << 16) + id2
;
2937 /* sanitize component name for DAI link creation */
2938 snprintf(tmp
, NAME_SIZE
, "%s.%s", dev
->driver
->name
, name
);
2939 strncpy(name
, tmp
, NAME_SIZE
);
2944 return kstrdup(name
, GFP_KERNEL
);
2948 * Simplify DAI link naming for single devices with multiple DAIs by removing
2949 * any ".-1" and using the DAI name (instead of device name).
2951 static inline char *fmt_multiple_name(struct device
*dev
,
2952 struct snd_soc_dai_driver
*dai_drv
)
2954 if (dai_drv
->name
== NULL
) {
2955 printk(KERN_ERR
"asoc: error - multiple DAI %s registered with no name\n",
2960 return kstrdup(dai_drv
->name
, GFP_KERNEL
);
2964 * snd_soc_register_dai - Register a DAI with the ASoC core
2966 * @dai: DAI to register
2968 int snd_soc_register_dai(struct device
*dev
,
2969 struct snd_soc_dai_driver
*dai_drv
)
2971 struct snd_soc_dai
*dai
;
2973 dev_dbg(dev
, "dai register %s\n", dev_name(dev
));
2975 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
2979 /* create DAI component name */
2980 dai
->name
= fmt_single_name(dev
, &dai
->id
);
2981 if (dai
->name
== NULL
) {
2987 dai
->driver
= dai_drv
;
2988 if (!dai
->driver
->ops
)
2989 dai
->driver
->ops
= &null_dai_ops
;
2991 mutex_lock(&client_mutex
);
2992 list_add(&dai
->list
, &dai_list
);
2993 snd_soc_instantiate_cards();
2994 mutex_unlock(&client_mutex
);
2996 pr_debug("Registered DAI '%s'\n", dai
->name
);
3000 EXPORT_SYMBOL_GPL(snd_soc_register_dai
);
3003 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3005 * @dai: DAI to unregister
3007 void snd_soc_unregister_dai(struct device
*dev
)
3009 struct snd_soc_dai
*dai
;
3011 list_for_each_entry(dai
, &dai_list
, list
) {
3012 if (dev
== dai
->dev
)
3018 mutex_lock(&client_mutex
);
3019 list_del(&dai
->list
);
3020 mutex_unlock(&client_mutex
);
3022 pr_debug("Unregistered DAI '%s'\n", dai
->name
);
3026 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai
);
3029 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3031 * @dai: Array of DAIs to register
3032 * @count: Number of DAIs
3034 int snd_soc_register_dais(struct device
*dev
,
3035 struct snd_soc_dai_driver
*dai_drv
, size_t count
)
3037 struct snd_soc_dai
*dai
;
3040 dev_dbg(dev
, "dai register %s #%Zu\n", dev_name(dev
), count
);
3042 for (i
= 0; i
< count
; i
++) {
3044 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
3048 /* create DAI component name */
3049 dai
->name
= fmt_multiple_name(dev
, &dai_drv
[i
]);
3050 if (dai
->name
== NULL
) {
3057 dai
->driver
= &dai_drv
[i
];
3058 if (dai
->driver
->id
)
3059 dai
->id
= dai
->driver
->id
;
3062 if (!dai
->driver
->ops
)
3063 dai
->driver
->ops
= &null_dai_ops
;
3065 mutex_lock(&client_mutex
);
3066 list_add(&dai
->list
, &dai_list
);
3067 mutex_unlock(&client_mutex
);
3069 pr_debug("Registered DAI '%s'\n", dai
->name
);
3072 snd_soc_instantiate_cards();
3076 for (i
--; i
>= 0; i
--)
3077 snd_soc_unregister_dai(dev
);
3081 EXPORT_SYMBOL_GPL(snd_soc_register_dais
);
3084 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3086 * @dai: Array of DAIs to unregister
3087 * @count: Number of DAIs
3089 void snd_soc_unregister_dais(struct device
*dev
, size_t count
)
3093 for (i
= 0; i
< count
; i
++)
3094 snd_soc_unregister_dai(dev
);
3096 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais
);
3099 * snd_soc_register_platform - Register a platform with the ASoC core
3101 * @platform: platform to register
3103 int snd_soc_register_platform(struct device
*dev
,
3104 struct snd_soc_platform_driver
*platform_drv
)
3106 struct snd_soc_platform
*platform
;
3108 dev_dbg(dev
, "platform register %s\n", dev_name(dev
));
3110 platform
= kzalloc(sizeof(struct snd_soc_platform
), GFP_KERNEL
);
3111 if (platform
== NULL
)
3114 /* create platform component name */
3115 platform
->name
= fmt_single_name(dev
, &platform
->id
);
3116 if (platform
->name
== NULL
) {
3121 platform
->dev
= dev
;
3122 platform
->driver
= platform_drv
;
3124 mutex_lock(&client_mutex
);
3125 list_add(&platform
->list
, &platform_list
);
3126 snd_soc_instantiate_cards();
3127 mutex_unlock(&client_mutex
);
3129 pr_debug("Registered platform '%s'\n", platform
->name
);
3133 EXPORT_SYMBOL_GPL(snd_soc_register_platform
);
3136 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3138 * @platform: platform to unregister
3140 void snd_soc_unregister_platform(struct device
*dev
)
3142 struct snd_soc_platform
*platform
;
3144 list_for_each_entry(platform
, &platform_list
, list
) {
3145 if (dev
== platform
->dev
)
3151 mutex_lock(&client_mutex
);
3152 list_del(&platform
->list
);
3153 mutex_unlock(&client_mutex
);
3155 pr_debug("Unregistered platform '%s'\n", platform
->name
);
3156 kfree(platform
->name
);
3159 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform
);
3161 static u64 codec_format_map
[] = {
3162 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
,
3163 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_U16_BE
,
3164 SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S24_BE
,
3165 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_U24_BE
,
3166 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
,
3167 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_U32_BE
,
3168 SNDRV_PCM_FMTBIT_S24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3169 SNDRV_PCM_FMTBIT_U24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3170 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S20_3BE
,
3171 SNDRV_PCM_FMTBIT_U20_3LE
| SNDRV_PCM_FMTBIT_U20_3BE
,
3172 SNDRV_PCM_FMTBIT_S18_3LE
| SNDRV_PCM_FMTBIT_S18_3BE
,
3173 SNDRV_PCM_FMTBIT_U18_3LE
| SNDRV_PCM_FMTBIT_U18_3BE
,
3174 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
,
3175 SNDRV_PCM_FMTBIT_FLOAT64_LE
| SNDRV_PCM_FMTBIT_FLOAT64_BE
,
3176 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3177 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
,
3180 /* Fix up the DAI formats for endianness: codecs don't actually see
3181 * the endianness of the data but we're using the CPU format
3182 * definitions which do need to include endianness so we ensure that
3183 * codec DAIs always have both big and little endian variants set.
3185 static void fixup_codec_formats(struct snd_soc_pcm_stream
*stream
)
3189 for (i
= 0; i
< ARRAY_SIZE(codec_format_map
); i
++)
3190 if (stream
->formats
& codec_format_map
[i
])
3191 stream
->formats
|= codec_format_map
[i
];
3195 * snd_soc_register_codec - Register a codec with the ASoC core
3197 * @codec: codec to register
3199 int snd_soc_register_codec(struct device
*dev
,
3200 struct snd_soc_codec_driver
*codec_drv
,
3201 struct snd_soc_dai_driver
*dai_drv
, int num_dai
)
3203 struct snd_soc_codec
*codec
;
3206 dev_dbg(dev
, "codec register %s\n", dev_name(dev
));
3208 codec
= kzalloc(sizeof(struct snd_soc_codec
), GFP_KERNEL
);
3212 /* create CODEC component name */
3213 codec
->name
= fmt_single_name(dev
, &codec
->id
);
3214 if (codec
->name
== NULL
) {
3219 /* allocate CODEC register cache */
3220 if (codec_drv
->reg_cache_size
&& codec_drv
->reg_word_size
) {
3222 if (codec_drv
->reg_cache_default
)
3223 codec
->reg_cache
= kmemdup(codec_drv
->reg_cache_default
,
3224 codec_drv
->reg_cache_size
* codec_drv
->reg_word_size
, GFP_KERNEL
);
3226 codec
->reg_cache
= kzalloc(codec_drv
->reg_cache_size
*
3227 codec_drv
->reg_word_size
, GFP_KERNEL
);
3229 if (codec
->reg_cache
== NULL
) {
3237 codec
->driver
= codec_drv
;
3238 codec
->bias_level
= SND_SOC_BIAS_OFF
;
3239 codec
->num_dai
= num_dai
;
3240 mutex_init(&codec
->mutex
);
3241 INIT_LIST_HEAD(&codec
->dapm_widgets
);
3242 INIT_LIST_HEAD(&codec
->dapm_paths
);
3244 for (i
= 0; i
< num_dai
; i
++) {
3245 fixup_codec_formats(&dai_drv
[i
].playback
);
3246 fixup_codec_formats(&dai_drv
[i
].capture
);
3249 /* register any DAIs */
3251 ret
= snd_soc_register_dais(dev
, dai_drv
, num_dai
);
3256 mutex_lock(&client_mutex
);
3257 list_add(&codec
->list
, &codec_list
);
3258 snd_soc_instantiate_cards();
3259 mutex_unlock(&client_mutex
);
3261 pr_debug("Registered codec '%s'\n", codec
->name
);
3265 for (i
--; i
>= 0; i
--)
3266 snd_soc_unregister_dai(dev
);
3268 if (codec
->reg_cache
)
3269 kfree(codec
->reg_cache
);
3274 EXPORT_SYMBOL_GPL(snd_soc_register_codec
);
3277 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3279 * @codec: codec to unregister
3281 void snd_soc_unregister_codec(struct device
*dev
)
3283 struct snd_soc_codec
*codec
;
3286 list_for_each_entry(codec
, &codec_list
, list
) {
3287 if (dev
== codec
->dev
)
3294 for (i
= 0; i
< codec
->num_dai
; i
++)
3295 snd_soc_unregister_dai(dev
);
3297 mutex_lock(&client_mutex
);
3298 list_del(&codec
->list
);
3299 mutex_unlock(&client_mutex
);
3301 pr_debug("Unregistered codec '%s'\n", codec
->name
);
3303 if (codec
->reg_cache
)
3304 kfree(codec
->reg_cache
);
3308 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec
);
3310 static int __init
snd_soc_init(void)
3312 #ifdef CONFIG_DEBUG_FS
3313 debugfs_root
= debugfs_create_dir("asoc", NULL
);
3314 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
3316 "ASoC: Failed to create debugfs directory\n");
3317 debugfs_root
= NULL
;
3320 if (!debugfs_create_file("codecs", 0444, debugfs_root
, NULL
,
3322 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3324 if (!debugfs_create_file("dais", 0444, debugfs_root
, NULL
,
3326 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3328 if (!debugfs_create_file("platforms", 0444, debugfs_root
, NULL
,
3329 &platform_list_fops
))
3330 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3333 return platform_driver_register(&soc_driver
);
3335 module_init(snd_soc_init
);
3337 static void __exit
snd_soc_exit(void)
3339 #ifdef CONFIG_DEBUG_FS
3340 debugfs_remove_recursive(debugfs_root
);
3342 platform_driver_unregister(&soc_driver
);
3344 module_exit(snd_soc_exit
);
3346 /* Module information */
3347 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3348 MODULE_DESCRIPTION("ALSA SoC Core");
3349 MODULE_LICENSE("GPL");
3350 MODULE_ALIAS("platform:soc-audio");