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
);
169 strict_strtol(buf
, 10, &rtd
->pmdown_time
);
174 static DEVICE_ATTR(pmdown_time
, 0644, pmdown_time_show
, pmdown_time_set
);
176 #ifdef CONFIG_DEBUG_FS
177 static int codec_reg_open_file(struct inode
*inode
, struct file
*file
)
179 file
->private_data
= inode
->i_private
;
183 static ssize_t
codec_reg_read_file(struct file
*file
, char __user
*user_buf
,
184 size_t count
, loff_t
*ppos
)
187 struct snd_soc_codec
*codec
= file
->private_data
;
188 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
191 ret
= soc_codec_reg_show(codec
, buf
);
193 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
198 static ssize_t
codec_reg_write_file(struct file
*file
,
199 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
204 unsigned long reg
, value
;
206 struct snd_soc_codec
*codec
= file
->private_data
;
208 buf_size
= min(count
, (sizeof(buf
)-1));
209 if (copy_from_user(buf
, user_buf
, buf_size
))
213 if (codec
->driver
->reg_cache_step
)
214 step
= codec
->driver
->reg_cache_step
;
216 while (*start
== ' ')
218 reg
= simple_strtoul(start
, &start
, 16);
219 if ((reg
>= codec
->driver
->reg_cache_size
) || (reg
% step
))
221 while (*start
== ' ')
223 if (strict_strtoul(start
, 16, &value
))
225 codec
->driver
->write(codec
, reg
, value
);
229 static const struct file_operations codec_reg_fops
= {
230 .open
= codec_reg_open_file
,
231 .read
= codec_reg_read_file
,
232 .write
= codec_reg_write_file
,
233 .llseek
= default_llseek
,
236 static void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
238 codec
->debugfs_codec_root
= debugfs_create_dir(codec
->name
,
240 if (!codec
->debugfs_codec_root
) {
242 "ASoC: Failed to create codec debugfs directory\n");
246 codec
->debugfs_reg
= debugfs_create_file("codec_reg", 0644,
247 codec
->debugfs_codec_root
,
248 codec
, &codec_reg_fops
);
249 if (!codec
->debugfs_reg
)
251 "ASoC: Failed to create codec register debugfs file\n");
253 codec
->debugfs_pop_time
= debugfs_create_u32("dapm_pop_time", 0644,
254 codec
->debugfs_codec_root
,
256 if (!codec
->debugfs_pop_time
)
258 "Failed to create pop time debugfs file\n");
260 codec
->debugfs_dapm
= debugfs_create_dir("dapm",
261 codec
->debugfs_codec_root
);
262 if (!codec
->debugfs_dapm
)
264 "Failed to create DAPM debugfs directory\n");
266 snd_soc_dapm_debugfs_init(codec
);
269 static void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
271 debugfs_remove_recursive(codec
->debugfs_codec_root
);
274 static ssize_t
codec_list_read_file(struct file
*file
, char __user
*user_buf
,
275 size_t count
, loff_t
*ppos
)
277 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
278 ssize_t len
, ret
= 0;
279 struct snd_soc_codec
*codec
;
284 list_for_each_entry(codec
, &codec_list
, list
) {
285 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
289 if (ret
> PAGE_SIZE
) {
296 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
303 static const struct file_operations codec_list_fops
= {
304 .read
= codec_list_read_file
,
305 .llseek
= default_llseek
,/* read accesses f_pos */
308 static ssize_t
dai_list_read_file(struct file
*file
, char __user
*user_buf
,
309 size_t count
, loff_t
*ppos
)
311 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
312 ssize_t len
, ret
= 0;
313 struct snd_soc_dai
*dai
;
318 list_for_each_entry(dai
, &dai_list
, list
) {
319 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n", dai
->name
);
322 if (ret
> PAGE_SIZE
) {
328 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
335 static const struct file_operations dai_list_fops
= {
336 .read
= dai_list_read_file
,
337 .llseek
= default_llseek
,/* read accesses f_pos */
340 static ssize_t
platform_list_read_file(struct file
*file
,
341 char __user
*user_buf
,
342 size_t count
, loff_t
*ppos
)
344 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
345 ssize_t len
, ret
= 0;
346 struct snd_soc_platform
*platform
;
351 list_for_each_entry(platform
, &platform_list
, list
) {
352 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
356 if (ret
> PAGE_SIZE
) {
362 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
369 static const struct file_operations platform_list_fops
= {
370 .read
= platform_list_read_file
,
371 .llseek
= default_llseek
,/* read accesses f_pos */
376 static inline void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
380 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
385 #ifdef CONFIG_SND_SOC_AC97_BUS
386 /* unregister ac97 codec */
387 static int soc_ac97_dev_unregister(struct snd_soc_codec
*codec
)
389 if (codec
->ac97
->dev
.bus
)
390 device_unregister(&codec
->ac97
->dev
);
394 /* stop no dev release warning */
395 static void soc_ac97_device_release(struct device
*dev
){}
397 /* register ac97 codec to bus */
398 static int soc_ac97_dev_register(struct snd_soc_codec
*codec
)
402 codec
->ac97
->dev
.bus
= &ac97_bus_type
;
403 codec
->ac97
->dev
.parent
= codec
->card
->dev
;
404 codec
->ac97
->dev
.release
= soc_ac97_device_release
;
406 dev_set_name(&codec
->ac97
->dev
, "%d-%d:%s",
407 codec
->card
->snd_card
->number
, 0, codec
->name
);
408 err
= device_register(&codec
->ac97
->dev
);
410 snd_printk(KERN_ERR
"Can't register ac97 bus\n");
411 codec
->ac97
->dev
.bus
= NULL
;
418 static int soc_pcm_apply_symmetry(struct snd_pcm_substream
*substream
)
420 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
421 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
422 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
425 if (codec_dai
->driver
->symmetric_rates
|| cpu_dai
->driver
->symmetric_rates
||
426 rtd
->dai_link
->symmetric_rates
) {
427 dev_dbg(&rtd
->dev
, "Symmetry forces %dHz rate\n",
430 ret
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
431 SNDRV_PCM_HW_PARAM_RATE
,
436 "Unable to apply rate symmetry constraint: %d\n", ret
);
445 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
446 * then initialized and any private data can be allocated. This also calls
447 * startup for the cpu DAI, platform, machine and codec DAI.
449 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
451 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
452 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
453 struct snd_soc_platform
*platform
= rtd
->platform
;
454 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
455 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
456 struct snd_soc_dai_driver
*cpu_dai_drv
= cpu_dai
->driver
;
457 struct snd_soc_dai_driver
*codec_dai_drv
= codec_dai
->driver
;
460 mutex_lock(&pcm_mutex
);
462 /* startup the audio subsystem */
463 if (cpu_dai
->driver
->ops
->startup
) {
464 ret
= cpu_dai
->driver
->ops
->startup(substream
, cpu_dai
);
466 printk(KERN_ERR
"asoc: can't open interface %s\n",
472 if (platform
->driver
->ops
->open
) {
473 ret
= platform
->driver
->ops
->open(substream
);
475 printk(KERN_ERR
"asoc: can't open platform %s\n", platform
->name
);
480 if (codec_dai
->driver
->ops
->startup
) {
481 ret
= codec_dai
->driver
->ops
->startup(substream
, codec_dai
);
483 printk(KERN_ERR
"asoc: can't open codec %s\n",
489 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->startup
) {
490 ret
= rtd
->dai_link
->ops
->startup(substream
);
492 printk(KERN_ERR
"asoc: %s startup failed\n", rtd
->dai_link
->name
);
497 /* Check that the codec and cpu DAI's are compatible */
498 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
499 runtime
->hw
.rate_min
=
500 max(codec_dai_drv
->playback
.rate_min
,
501 cpu_dai_drv
->playback
.rate_min
);
502 runtime
->hw
.rate_max
=
503 min(codec_dai_drv
->playback
.rate_max
,
504 cpu_dai_drv
->playback
.rate_max
);
505 runtime
->hw
.channels_min
=
506 max(codec_dai_drv
->playback
.channels_min
,
507 cpu_dai_drv
->playback
.channels_min
);
508 runtime
->hw
.channels_max
=
509 min(codec_dai_drv
->playback
.channels_max
,
510 cpu_dai_drv
->playback
.channels_max
);
511 runtime
->hw
.formats
=
512 codec_dai_drv
->playback
.formats
& cpu_dai_drv
->playback
.formats
;
514 codec_dai_drv
->playback
.rates
& cpu_dai_drv
->playback
.rates
;
515 if (codec_dai_drv
->playback
.rates
516 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
517 runtime
->hw
.rates
|= cpu_dai_drv
->playback
.rates
;
518 if (cpu_dai_drv
->playback
.rates
519 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
520 runtime
->hw
.rates
|= codec_dai_drv
->playback
.rates
;
522 runtime
->hw
.rate_min
=
523 max(codec_dai_drv
->capture
.rate_min
,
524 cpu_dai_drv
->capture
.rate_min
);
525 runtime
->hw
.rate_max
=
526 min(codec_dai_drv
->capture
.rate_max
,
527 cpu_dai_drv
->capture
.rate_max
);
528 runtime
->hw
.channels_min
=
529 max(codec_dai_drv
->capture
.channels_min
,
530 cpu_dai_drv
->capture
.channels_min
);
531 runtime
->hw
.channels_max
=
532 min(codec_dai_drv
->capture
.channels_max
,
533 cpu_dai_drv
->capture
.channels_max
);
534 runtime
->hw
.formats
=
535 codec_dai_drv
->capture
.formats
& cpu_dai_drv
->capture
.formats
;
537 codec_dai_drv
->capture
.rates
& cpu_dai_drv
->capture
.rates
;
538 if (codec_dai_drv
->capture
.rates
539 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
540 runtime
->hw
.rates
|= cpu_dai_drv
->capture
.rates
;
541 if (cpu_dai_drv
->capture
.rates
542 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
543 runtime
->hw
.rates
|= codec_dai_drv
->capture
.rates
;
546 snd_pcm_limit_hw_rates(runtime
);
547 if (!runtime
->hw
.rates
) {
548 printk(KERN_ERR
"asoc: %s <-> %s No matching rates\n",
549 codec_dai
->name
, cpu_dai
->name
);
552 if (!runtime
->hw
.formats
) {
553 printk(KERN_ERR
"asoc: %s <-> %s No matching formats\n",
554 codec_dai
->name
, cpu_dai
->name
);
557 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
) {
558 printk(KERN_ERR
"asoc: %s <-> %s No matching channels\n",
559 codec_dai
->name
, cpu_dai
->name
);
563 /* Symmetry only applies if we've already got an active stream. */
564 if (cpu_dai
->active
|| codec_dai
->active
) {
565 ret
= soc_pcm_apply_symmetry(substream
);
570 pr_debug("asoc: %s <-> %s info:\n",
571 codec_dai
->name
, cpu_dai
->name
);
572 pr_debug("asoc: rate mask 0x%x\n", runtime
->hw
.rates
);
573 pr_debug("asoc: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
574 runtime
->hw
.channels_max
);
575 pr_debug("asoc: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
576 runtime
->hw
.rate_max
);
578 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
579 cpu_dai
->playback_active
++;
580 codec_dai
->playback_active
++;
582 cpu_dai
->capture_active
++;
583 codec_dai
->capture_active
++;
587 rtd
->codec
->active
++;
588 mutex_unlock(&pcm_mutex
);
592 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
593 rtd
->dai_link
->ops
->shutdown(substream
);
596 if (codec_dai
->driver
->ops
->shutdown
)
597 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
600 if (platform
->driver
->ops
->close
)
601 platform
->driver
->ops
->close(substream
);
604 if (cpu_dai
->driver
->ops
->shutdown
)
605 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
607 mutex_unlock(&pcm_mutex
);
612 * Power down the audio subsystem pmdown_time msecs after close is called.
613 * This is to ensure there are no pops or clicks in between any music tracks
614 * due to DAPM power cycling.
616 static void close_delayed_work(struct work_struct
*work
)
618 struct snd_soc_pcm_runtime
*rtd
=
619 container_of(work
, struct snd_soc_pcm_runtime
, delayed_work
.work
);
620 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
622 mutex_lock(&pcm_mutex
);
624 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
625 codec_dai
->driver
->playback
.stream_name
,
626 codec_dai
->playback_active
? "active" : "inactive",
627 codec_dai
->pop_wait
? "yes" : "no");
629 /* are we waiting on this codec DAI stream */
630 if (codec_dai
->pop_wait
== 1) {
631 codec_dai
->pop_wait
= 0;
632 snd_soc_dapm_stream_event(rtd
,
633 codec_dai
->driver
->playback
.stream_name
,
634 SND_SOC_DAPM_STREAM_STOP
);
637 mutex_unlock(&pcm_mutex
);
641 * Called by ALSA when a PCM substream is closed. Private data can be
642 * freed here. The cpu DAI, codec DAI, machine and platform are also
645 static int soc_codec_close(struct snd_pcm_substream
*substream
)
647 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
648 struct snd_soc_platform
*platform
= rtd
->platform
;
649 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
650 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
651 struct snd_soc_codec
*codec
= rtd
->codec
;
653 mutex_lock(&pcm_mutex
);
655 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
656 cpu_dai
->playback_active
--;
657 codec_dai
->playback_active
--;
659 cpu_dai
->capture_active
--;
660 codec_dai
->capture_active
--;
667 /* Muting the DAC suppresses artifacts caused during digital
668 * shutdown, for example from stopping clocks.
670 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
671 snd_soc_dai_digital_mute(codec_dai
, 1);
673 if (cpu_dai
->driver
->ops
->shutdown
)
674 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
676 if (codec_dai
->driver
->ops
->shutdown
)
677 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
679 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
680 rtd
->dai_link
->ops
->shutdown(substream
);
682 if (platform
->driver
->ops
->close
)
683 platform
->driver
->ops
->close(substream
);
684 cpu_dai
->runtime
= NULL
;
686 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
687 /* start delayed pop wq here for playback streams */
688 codec_dai
->pop_wait
= 1;
689 schedule_delayed_work(&rtd
->delayed_work
,
690 msecs_to_jiffies(rtd
->pmdown_time
));
692 /* capture streams can be powered down now */
693 snd_soc_dapm_stream_event(rtd
,
694 codec_dai
->driver
->capture
.stream_name
,
695 SND_SOC_DAPM_STREAM_STOP
);
698 mutex_unlock(&pcm_mutex
);
703 * Called by ALSA when the PCM substream is prepared, can set format, sample
704 * rate, etc. This function is non atomic and can be called multiple times,
705 * it can refer to the runtime info.
707 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
709 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
710 struct snd_soc_platform
*platform
= rtd
->platform
;
711 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
712 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
715 mutex_lock(&pcm_mutex
);
717 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->prepare
) {
718 ret
= rtd
->dai_link
->ops
->prepare(substream
);
720 printk(KERN_ERR
"asoc: machine prepare error\n");
725 if (platform
->driver
->ops
->prepare
) {
726 ret
= platform
->driver
->ops
->prepare(substream
);
728 printk(KERN_ERR
"asoc: platform prepare error\n");
733 if (codec_dai
->driver
->ops
->prepare
) {
734 ret
= codec_dai
->driver
->ops
->prepare(substream
, codec_dai
);
736 printk(KERN_ERR
"asoc: codec DAI prepare error\n");
741 if (cpu_dai
->driver
->ops
->prepare
) {
742 ret
= cpu_dai
->driver
->ops
->prepare(substream
, cpu_dai
);
744 printk(KERN_ERR
"asoc: cpu DAI prepare error\n");
749 /* cancel any delayed stream shutdown that is pending */
750 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
751 codec_dai
->pop_wait
) {
752 codec_dai
->pop_wait
= 0;
753 cancel_delayed_work(&rtd
->delayed_work
);
756 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
757 snd_soc_dapm_stream_event(rtd
,
758 codec_dai
->driver
->playback
.stream_name
,
759 SND_SOC_DAPM_STREAM_START
);
761 snd_soc_dapm_stream_event(rtd
,
762 codec_dai
->driver
->capture
.stream_name
,
763 SND_SOC_DAPM_STREAM_START
);
765 snd_soc_dai_digital_mute(codec_dai
, 0);
768 mutex_unlock(&pcm_mutex
);
773 * Called by ALSA when the hardware params are set by application. This
774 * function can also be called multiple times and can allocate buffers
775 * (using snd_pcm_lib_* ). It's non-atomic.
777 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
778 struct snd_pcm_hw_params
*params
)
780 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
781 struct snd_soc_platform
*platform
= rtd
->platform
;
782 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
783 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
786 mutex_lock(&pcm_mutex
);
788 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_params
) {
789 ret
= rtd
->dai_link
->ops
->hw_params(substream
, params
);
791 printk(KERN_ERR
"asoc: machine hw_params failed\n");
796 if (codec_dai
->driver
->ops
->hw_params
) {
797 ret
= codec_dai
->driver
->ops
->hw_params(substream
, params
, codec_dai
);
799 printk(KERN_ERR
"asoc: can't set codec %s hw params\n",
805 if (cpu_dai
->driver
->ops
->hw_params
) {
806 ret
= cpu_dai
->driver
->ops
->hw_params(substream
, params
, cpu_dai
);
808 printk(KERN_ERR
"asoc: interface %s hw params failed\n",
814 if (platform
->driver
->ops
->hw_params
) {
815 ret
= platform
->driver
->ops
->hw_params(substream
, params
);
817 printk(KERN_ERR
"asoc: platform %s hw params failed\n",
823 rtd
->rate
= params_rate(params
);
826 mutex_unlock(&pcm_mutex
);
830 if (cpu_dai
->driver
->ops
->hw_free
)
831 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
834 if (codec_dai
->driver
->ops
->hw_free
)
835 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
838 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
839 rtd
->dai_link
->ops
->hw_free(substream
);
841 mutex_unlock(&pcm_mutex
);
846 * Free's resources allocated by hw_params, can be called multiple times
848 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
850 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
851 struct snd_soc_platform
*platform
= rtd
->platform
;
852 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
853 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
854 struct snd_soc_codec
*codec
= rtd
->codec
;
856 mutex_lock(&pcm_mutex
);
858 /* apply codec digital mute */
860 snd_soc_dai_digital_mute(codec_dai
, 1);
862 /* free any machine hw params */
863 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
864 rtd
->dai_link
->ops
->hw_free(substream
);
866 /* free any DMA resources */
867 if (platform
->driver
->ops
->hw_free
)
868 platform
->driver
->ops
->hw_free(substream
);
870 /* now free hw params for the DAI's */
871 if (codec_dai
->driver
->ops
->hw_free
)
872 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
874 if (cpu_dai
->driver
->ops
->hw_free
)
875 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
877 mutex_unlock(&pcm_mutex
);
881 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
883 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
884 struct snd_soc_platform
*platform
= rtd
->platform
;
885 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
886 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
889 if (codec_dai
->driver
->ops
->trigger
) {
890 ret
= codec_dai
->driver
->ops
->trigger(substream
, cmd
, codec_dai
);
895 if (platform
->driver
->ops
->trigger
) {
896 ret
= platform
->driver
->ops
->trigger(substream
, cmd
);
901 if (cpu_dai
->driver
->ops
->trigger
) {
902 ret
= cpu_dai
->driver
->ops
->trigger(substream
, cmd
, cpu_dai
);
910 * soc level wrapper for pointer callback
911 * If cpu_dai, codec_dai, platform driver has the delay callback, than
912 * the runtime->delay will be updated accordingly.
914 static snd_pcm_uframes_t
soc_pcm_pointer(struct snd_pcm_substream
*substream
)
916 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
917 struct snd_soc_platform
*platform
= rtd
->platform
;
918 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
919 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
920 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
921 snd_pcm_uframes_t offset
= 0;
922 snd_pcm_sframes_t delay
= 0;
924 if (platform
->driver
->ops
->pointer
)
925 offset
= platform
->driver
->ops
->pointer(substream
);
927 if (cpu_dai
->driver
->ops
->delay
)
928 delay
+= cpu_dai
->driver
->ops
->delay(substream
, cpu_dai
);
930 if (codec_dai
->driver
->ops
->delay
)
931 delay
+= codec_dai
->driver
->ops
->delay(substream
, codec_dai
);
933 if (platform
->driver
->delay
)
934 delay
+= platform
->driver
->delay(substream
, codec_dai
);
936 runtime
->delay
= delay
;
941 /* ASoC PCM operations */
942 static struct snd_pcm_ops soc_pcm_ops
= {
943 .open
= soc_pcm_open
,
944 .close
= soc_codec_close
,
945 .hw_params
= soc_pcm_hw_params
,
946 .hw_free
= soc_pcm_hw_free
,
947 .prepare
= soc_pcm_prepare
,
948 .trigger
= soc_pcm_trigger
,
949 .pointer
= soc_pcm_pointer
,
953 /* powers down audio subsystem for suspend */
954 static int soc_suspend(struct device
*dev
)
956 struct platform_device
*pdev
= to_platform_device(dev
);
957 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
960 /* If the initialization of this soc device failed, there is no codec
961 * associated with it. Just bail out in this case.
963 if (list_empty(&card
->codec_dev_list
))
966 /* Due to the resume being scheduled into a workqueue we could
967 * suspend before that's finished - wait for it to complete.
969 snd_power_lock(card
->snd_card
);
970 snd_power_wait(card
->snd_card
, SNDRV_CTL_POWER_D0
);
971 snd_power_unlock(card
->snd_card
);
973 /* we're going to block userspace touching us until resume completes */
974 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D3hot
);
976 /* mute any active DAC's */
977 for (i
= 0; i
< card
->num_rtd
; i
++) {
978 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
979 struct snd_soc_dai_driver
*drv
= dai
->driver
;
981 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
984 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
985 drv
->ops
->digital_mute(dai
, 1);
988 /* suspend all pcms */
989 for (i
= 0; i
< card
->num_rtd
; i
++) {
990 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
993 snd_pcm_suspend_all(card
->rtd
[i
].pcm
);
996 if (card
->suspend_pre
)
997 card
->suspend_pre(pdev
, PMSG_SUSPEND
);
999 for (i
= 0; i
< card
->num_rtd
; i
++) {
1000 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1001 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1003 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1006 if (cpu_dai
->driver
->suspend
&& !cpu_dai
->driver
->ac97_control
)
1007 cpu_dai
->driver
->suspend(cpu_dai
);
1008 if (platform
->driver
->suspend
&& !platform
->suspended
) {
1009 platform
->driver
->suspend(cpu_dai
);
1010 platform
->suspended
= 1;
1014 /* close any waiting streams and save state */
1015 for (i
= 0; i
< card
->num_rtd
; i
++) {
1016 run_delayed_work(&card
->rtd
[i
].delayed_work
);
1017 card
->rtd
[i
].codec
->suspend_bias_level
= card
->rtd
[i
].codec
->bias_level
;
1020 for (i
= 0; i
< card
->num_rtd
; i
++) {
1021 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1023 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1026 if (driver
->playback
.stream_name
!= NULL
)
1027 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1028 SND_SOC_DAPM_STREAM_SUSPEND
);
1030 if (driver
->capture
.stream_name
!= NULL
)
1031 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1032 SND_SOC_DAPM_STREAM_SUSPEND
);
1035 /* suspend all CODECs */
1036 for (i
= 0; i
< card
->num_rtd
; i
++) {
1037 struct snd_soc_codec
*codec
= card
->rtd
[i
].codec
;
1038 /* If there are paths active then the CODEC will be held with
1039 * bias _ON and should not be suspended. */
1040 if (!codec
->suspended
&& codec
->driver
->suspend
) {
1041 switch (codec
->bias_level
) {
1042 case SND_SOC_BIAS_STANDBY
:
1043 case SND_SOC_BIAS_OFF
:
1044 codec
->driver
->suspend(codec
, PMSG_SUSPEND
);
1045 codec
->suspended
= 1;
1048 dev_dbg(codec
->dev
, "CODEC is on over suspend\n");
1054 for (i
= 0; i
< card
->num_rtd
; i
++) {
1055 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1057 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1060 if (cpu_dai
->driver
->suspend
&& cpu_dai
->driver
->ac97_control
)
1061 cpu_dai
->driver
->suspend(cpu_dai
);
1064 if (card
->suspend_post
)
1065 card
->suspend_post(pdev
, PMSG_SUSPEND
);
1070 /* deferred resume work, so resume can complete before we finished
1071 * setting our codec back up, which can be very slow on I2C
1073 static void soc_resume_deferred(struct work_struct
*work
)
1075 struct snd_soc_card
*card
=
1076 container_of(work
, struct snd_soc_card
, deferred_resume_work
);
1077 struct platform_device
*pdev
= to_platform_device(card
->dev
);
1080 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1081 * so userspace apps are blocked from touching us
1084 dev_dbg(card
->dev
, "starting resume work\n");
1086 /* Bring us up into D2 so that DAPM starts enabling things */
1087 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D2
);
1089 if (card
->resume_pre
)
1090 card
->resume_pre(pdev
);
1092 /* resume AC97 DAIs */
1093 for (i
= 0; i
< card
->num_rtd
; i
++) {
1094 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1096 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1099 if (cpu_dai
->driver
->resume
&& cpu_dai
->driver
->ac97_control
)
1100 cpu_dai
->driver
->resume(cpu_dai
);
1103 for (i
= 0; i
< card
->num_rtd
; i
++) {
1104 struct snd_soc_codec
*codec
= card
->rtd
[i
].codec
;
1105 /* If the CODEC was idle over suspend then it will have been
1106 * left with bias OFF or STANDBY and suspended so we must now
1107 * resume. Otherwise the suspend was suppressed.
1109 if (codec
->driver
->resume
&& codec
->suspended
) {
1110 switch (codec
->bias_level
) {
1111 case SND_SOC_BIAS_STANDBY
:
1112 case SND_SOC_BIAS_OFF
:
1113 codec
->driver
->resume(codec
);
1114 codec
->suspended
= 0;
1117 dev_dbg(codec
->dev
, "CODEC was on over suspend\n");
1123 for (i
= 0; i
< card
->num_rtd
; i
++) {
1124 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1126 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1129 if (driver
->playback
.stream_name
!= NULL
)
1130 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1131 SND_SOC_DAPM_STREAM_RESUME
);
1133 if (driver
->capture
.stream_name
!= NULL
)
1134 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1135 SND_SOC_DAPM_STREAM_RESUME
);
1138 /* unmute any active DACs */
1139 for (i
= 0; i
< card
->num_rtd
; i
++) {
1140 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
1141 struct snd_soc_dai_driver
*drv
= dai
->driver
;
1143 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1146 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
1147 drv
->ops
->digital_mute(dai
, 0);
1150 for (i
= 0; i
< card
->num_rtd
; i
++) {
1151 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1152 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1154 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1157 if (cpu_dai
->driver
->resume
&& !cpu_dai
->driver
->ac97_control
)
1158 cpu_dai
->driver
->resume(cpu_dai
);
1159 if (platform
->driver
->resume
&& platform
->suspended
) {
1160 platform
->driver
->resume(cpu_dai
);
1161 platform
->suspended
= 0;
1165 if (card
->resume_post
)
1166 card
->resume_post(pdev
);
1168 dev_dbg(card
->dev
, "resume work completed\n");
1170 /* userspace can access us now we are back as we were before */
1171 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D0
);
1174 /* powers up audio subsystem after a suspend */
1175 static int soc_resume(struct device
*dev
)
1177 struct platform_device
*pdev
= to_platform_device(dev
);
1178 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1181 /* AC97 devices might have other drivers hanging off them so
1182 * need to resume immediately. Other drivers don't have that
1183 * problem and may take a substantial amount of time to resume
1184 * due to I/O costs and anti-pop so handle them out of line.
1186 for (i
= 0; i
< card
->num_rtd
; i
++) {
1187 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1188 if (cpu_dai
->driver
->ac97_control
) {
1189 dev_dbg(dev
, "Resuming AC97 immediately\n");
1190 soc_resume_deferred(&card
->deferred_resume_work
);
1192 dev_dbg(dev
, "Scheduling resume work\n");
1193 if (!schedule_work(&card
->deferred_resume_work
))
1194 dev_err(dev
, "resume work item may be lost\n");
1201 #define soc_suspend NULL
1202 #define soc_resume NULL
1205 static struct snd_soc_dai_ops null_dai_ops
= {
1208 static int soc_bind_dai_link(struct snd_soc_card
*card
, int num
)
1210 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1211 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1212 struct snd_soc_codec
*codec
;
1213 struct snd_soc_platform
*platform
;
1214 struct snd_soc_dai
*codec_dai
, *cpu_dai
;
1218 dev_dbg(card
->dev
, "binding %s at idx %d\n", dai_link
->name
, num
);
1220 /* do we already have the CPU DAI for this link ? */
1224 /* no, then find CPU DAI from registered DAIs*/
1225 list_for_each_entry(cpu_dai
, &dai_list
, list
) {
1226 if (!strcmp(cpu_dai
->name
, dai_link
->cpu_dai_name
)) {
1228 if (!try_module_get(cpu_dai
->dev
->driver
->owner
))
1231 rtd
->cpu_dai
= cpu_dai
;
1235 dev_dbg(card
->dev
, "CPU DAI %s not registered\n",
1236 dai_link
->cpu_dai_name
);
1239 /* do we already have the CODEC for this link ? */
1244 /* no, then find CODEC from registered CODECs*/
1245 list_for_each_entry(codec
, &codec_list
, list
) {
1246 if (!strcmp(codec
->name
, dai_link
->codec_name
)) {
1249 if (!try_module_get(codec
->dev
->driver
->owner
))
1252 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1253 list_for_each_entry(codec_dai
, &dai_list
, list
) {
1254 if (codec
->dev
== codec_dai
->dev
&&
1255 !strcmp(codec_dai
->name
, dai_link
->codec_dai_name
)) {
1256 rtd
->codec_dai
= codec_dai
;
1260 dev_dbg(card
->dev
, "CODEC DAI %s not registered\n",
1261 dai_link
->codec_dai_name
);
1266 dev_dbg(card
->dev
, "CODEC %s not registered\n",
1267 dai_link
->codec_name
);
1270 /* do we already have the CODEC DAI for this link ? */
1271 if (rtd
->platform
) {
1274 /* no, then find CPU DAI from registered DAIs*/
1275 list_for_each_entry(platform
, &platform_list
, list
) {
1276 if (!strcmp(platform
->name
, dai_link
->platform_name
)) {
1278 if (!try_module_get(platform
->dev
->driver
->owner
))
1281 rtd
->platform
= platform
;
1286 dev_dbg(card
->dev
, "platform %s not registered\n",
1287 dai_link
->platform_name
);
1291 /* mark rtd as complete if we found all 4 of our client devices */
1292 if (rtd
->codec
&& rtd
->codec_dai
&& rtd
->platform
&& rtd
->cpu_dai
) {
1299 static void soc_remove_dai_link(struct snd_soc_card
*card
, int num
)
1301 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1302 struct snd_soc_codec
*codec
= rtd
->codec
;
1303 struct snd_soc_platform
*platform
= rtd
->platform
;
1304 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1307 /* unregister the rtd device */
1308 if (rtd
->dev_registered
) {
1309 device_remove_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1310 device_unregister(&rtd
->dev
);
1311 rtd
->dev_registered
= 0;
1314 /* remove the CODEC DAI */
1315 if (codec_dai
&& codec_dai
->probed
) {
1316 if (codec_dai
->driver
->remove
) {
1317 err
= codec_dai
->driver
->remove(codec_dai
);
1319 printk(KERN_ERR
"asoc: failed to remove %s\n", codec_dai
->name
);
1321 codec_dai
->probed
= 0;
1322 list_del(&codec_dai
->card_list
);
1325 /* remove the platform */
1326 if (platform
&& platform
->probed
) {
1327 if (platform
->driver
->remove
) {
1328 err
= platform
->driver
->remove(platform
);
1330 printk(KERN_ERR
"asoc: failed to remove %s\n", platform
->name
);
1332 platform
->probed
= 0;
1333 list_del(&platform
->card_list
);
1334 module_put(platform
->dev
->driver
->owner
);
1337 /* remove the CODEC */
1338 if (codec
&& codec
->probed
) {
1339 if (codec
->driver
->remove
) {
1340 err
= codec
->driver
->remove(codec
);
1342 printk(KERN_ERR
"asoc: failed to remove %s\n", codec
->name
);
1345 /* Make sure all DAPM widgets are freed */
1346 snd_soc_dapm_free(codec
);
1348 soc_cleanup_codec_debugfs(codec
);
1349 device_remove_file(&rtd
->dev
, &dev_attr_codec_reg
);
1351 list_del(&codec
->card_list
);
1352 module_put(codec
->dev
->driver
->owner
);
1355 /* remove the cpu_dai */
1356 if (cpu_dai
&& cpu_dai
->probed
) {
1357 if (cpu_dai
->driver
->remove
) {
1358 err
= cpu_dai
->driver
->remove(cpu_dai
);
1360 printk(KERN_ERR
"asoc: failed to remove %s\n", cpu_dai
->name
);
1362 cpu_dai
->probed
= 0;
1363 list_del(&cpu_dai
->card_list
);
1364 module_put(cpu_dai
->dev
->driver
->owner
);
1368 static void rtd_release(struct device
*dev
) {}
1370 static int soc_probe_dai_link(struct snd_soc_card
*card
, int num
)
1372 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1373 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1374 struct snd_soc_codec
*codec
= rtd
->codec
;
1375 struct snd_soc_platform
*platform
= rtd
->platform
;
1376 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1379 dev_dbg(card
->dev
, "probe %s dai link %d\n", card
->name
, num
);
1381 /* config components */
1382 codec_dai
->codec
= codec
;
1384 cpu_dai
->platform
= platform
;
1386 rtd
->dev
.parent
= card
->dev
;
1387 codec_dai
->card
= card
;
1388 cpu_dai
->card
= card
;
1390 /* set default power off timeout */
1391 rtd
->pmdown_time
= pmdown_time
;
1393 /* probe the cpu_dai */
1394 if (!cpu_dai
->probed
) {
1395 if (cpu_dai
->driver
->probe
) {
1396 ret
= cpu_dai
->driver
->probe(cpu_dai
);
1398 printk(KERN_ERR
"asoc: failed to probe CPU DAI %s\n",
1403 cpu_dai
->probed
= 1;
1404 /* mark cpu_dai as probed and add to card cpu_dai list */
1405 list_add(&cpu_dai
->card_list
, &card
->dai_dev_list
);
1408 /* probe the CODEC */
1409 if (!codec
->probed
) {
1410 if (codec
->driver
->probe
) {
1411 ret
= codec
->driver
->probe(codec
);
1413 printk(KERN_ERR
"asoc: failed to probe CODEC %s\n",
1419 soc_init_codec_debugfs(codec
);
1421 /* mark codec as probed and add to card codec list */
1423 list_add(&codec
->card_list
, &card
->codec_dev_list
);
1426 /* probe the platform */
1427 if (!platform
->probed
) {
1428 if (platform
->driver
->probe
) {
1429 ret
= platform
->driver
->probe(platform
);
1431 printk(KERN_ERR
"asoc: failed to probe platform %s\n",
1436 /* mark platform as probed and add to card platform list */
1437 platform
->probed
= 1;
1438 list_add(&platform
->card_list
, &card
->platform_dev_list
);
1441 /* probe the CODEC DAI */
1442 if (!codec_dai
->probed
) {
1443 if (codec_dai
->driver
->probe
) {
1444 ret
= codec_dai
->driver
->probe(codec_dai
);
1446 printk(KERN_ERR
"asoc: failed to probe CODEC DAI %s\n",
1452 /* mark cpu_dai as probed and add to card cpu_dai list */
1453 codec_dai
->probed
= 1;
1454 list_add(&codec_dai
->card_list
, &card
->dai_dev_list
);
1457 /* DAPM dai link stream work */
1458 INIT_DELAYED_WORK(&rtd
->delayed_work
, close_delayed_work
);
1460 /* now that all clients have probed, initialise the DAI link */
1461 if (dai_link
->init
) {
1462 ret
= dai_link
->init(rtd
);
1464 printk(KERN_ERR
"asoc: failed to init %s\n", dai_link
->stream_name
);
1469 /* Make sure all DAPM widgets are instantiated */
1470 snd_soc_dapm_new_widgets(codec
);
1471 snd_soc_dapm_sync(codec
);
1473 /* register the rtd device */
1474 rtd
->dev
.release
= rtd_release
;
1475 rtd
->dev
.init_name
= dai_link
->name
;
1476 ret
= device_register(&rtd
->dev
);
1478 printk(KERN_ERR
"asoc: failed to register DAI runtime device %d\n", ret
);
1482 rtd
->dev_registered
= 1;
1483 ret
= device_create_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1485 printk(KERN_WARNING
"asoc: failed to add pmdown_time sysfs\n");
1487 /* add DAPM sysfs entries for this codec */
1488 ret
= snd_soc_dapm_sys_add(&rtd
->dev
);
1490 printk(KERN_WARNING
"asoc: failed to add codec dapm sysfs entries\n");
1492 /* add codec sysfs entries */
1493 ret
= device_create_file(&rtd
->dev
, &dev_attr_codec_reg
);
1495 printk(KERN_WARNING
"asoc: failed to add codec sysfs files\n");
1497 /* create the pcm */
1498 ret
= soc_new_pcm(rtd
, num
);
1500 printk(KERN_ERR
"asoc: can't create pcm %s\n", dai_link
->stream_name
);
1504 /* add platform data for AC97 devices */
1505 if (rtd
->codec_dai
->driver
->ac97_control
)
1506 snd_ac97_dev_add_pdata(codec
->ac97
, rtd
->cpu_dai
->ac97_pdata
);
1511 #ifdef CONFIG_SND_SOC_AC97_BUS
1512 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime
*rtd
)
1516 /* Only instantiate AC97 if not already done by the adaptor
1517 * for the generic AC97 subsystem.
1519 if (rtd
->codec_dai
->driver
->ac97_control
&& !rtd
->codec
->ac97_registered
) {
1521 * It is possible that the AC97 device is already registered to
1522 * the device subsystem. This happens when the device is created
1523 * via snd_ac97_mixer(). Currently only SoC codec that does so
1524 * is the generic AC97 glue but others migh emerge.
1526 * In those cases we don't try to register the device again.
1528 if (!rtd
->codec
->ac97_created
)
1531 ret
= soc_ac97_dev_register(rtd
->codec
);
1533 printk(KERN_ERR
"asoc: AC97 device register failed\n");
1537 rtd
->codec
->ac97_registered
= 1;
1542 static void soc_unregister_ac97_dai_link(struct snd_soc_codec
*codec
)
1544 if (codec
->ac97_registered
) {
1545 soc_ac97_dev_unregister(codec
);
1546 codec
->ac97_registered
= 0;
1551 static void snd_soc_instantiate_card(struct snd_soc_card
*card
)
1553 struct platform_device
*pdev
= to_platform_device(card
->dev
);
1556 mutex_lock(&card
->mutex
);
1558 if (card
->instantiated
) {
1559 mutex_unlock(&card
->mutex
);
1564 for (i
= 0; i
< card
->num_links
; i
++)
1565 soc_bind_dai_link(card
, i
);
1567 /* bind completed ? */
1568 if (card
->num_rtd
!= card
->num_links
) {
1569 mutex_unlock(&card
->mutex
);
1573 /* card bind complete so register a sound card */
1574 ret
= snd_card_create(SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
1575 card
->owner
, 0, &card
->snd_card
);
1577 printk(KERN_ERR
"asoc: can't create sound card for card %s\n",
1579 mutex_unlock(&card
->mutex
);
1582 card
->snd_card
->dev
= card
->dev
;
1585 /* deferred resume work */
1586 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
1589 /* initialise the sound card only once */
1591 ret
= card
->probe(pdev
);
1593 goto card_probe_error
;
1596 for (i
= 0; i
< card
->num_links
; i
++) {
1597 ret
= soc_probe_dai_link(card
, i
);
1599 pr_err("asoc: failed to instantiate card %s: %d\n",
1605 snprintf(card
->snd_card
->shortname
, sizeof(card
->snd_card
->shortname
),
1607 snprintf(card
->snd_card
->longname
, sizeof(card
->snd_card
->longname
),
1610 ret
= snd_card_register(card
->snd_card
);
1612 printk(KERN_ERR
"asoc: failed to register soundcard for %s\n", card
->name
);
1616 #ifdef CONFIG_SND_SOC_AC97_BUS
1617 /* register any AC97 codecs */
1618 for (i
= 0; i
< card
->num_rtd
; i
++) {
1619 ret
= soc_register_ac97_dai_link(&card
->rtd
[i
]);
1621 printk(KERN_ERR
"asoc: failed to register AC97 %s\n", card
->name
);
1627 card
->instantiated
= 1;
1628 mutex_unlock(&card
->mutex
);
1632 for (i
= 0; i
< card
->num_links
; i
++)
1633 soc_remove_dai_link(card
, i
);
1639 snd_card_free(card
->snd_card
);
1641 mutex_unlock(&card
->mutex
);
1645 * Attempt to initialise any uninitialised cards. Must be called with
1648 static void snd_soc_instantiate_cards(void)
1650 struct snd_soc_card
*card
;
1651 list_for_each_entry(card
, &card_list
, list
)
1652 snd_soc_instantiate_card(card
);
1655 /* probes a new socdev */
1656 static int soc_probe(struct platform_device
*pdev
)
1658 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1661 /* Bodge while we unpick instantiation */
1662 card
->dev
= &pdev
->dev
;
1663 INIT_LIST_HEAD(&card
->dai_dev_list
);
1664 INIT_LIST_HEAD(&card
->codec_dev_list
);
1665 INIT_LIST_HEAD(&card
->platform_dev_list
);
1667 ret
= snd_soc_register_card(card
);
1669 dev_err(&pdev
->dev
, "Failed to register card\n");
1676 /* removes a socdev */
1677 static int soc_remove(struct platform_device
*pdev
)
1679 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1682 if (card
->instantiated
) {
1684 /* make sure any delayed work runs */
1685 for (i
= 0; i
< card
->num_rtd
; i
++) {
1686 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1687 run_delayed_work(&rtd
->delayed_work
);
1690 /* remove and free each DAI */
1691 for (i
= 0; i
< card
->num_rtd
; i
++)
1692 soc_remove_dai_link(card
, i
);
1694 /* remove the card */
1699 snd_card_free(card
->snd_card
);
1701 snd_soc_unregister_card(card
);
1705 static int soc_poweroff(struct device
*dev
)
1707 struct platform_device
*pdev
= to_platform_device(dev
);
1708 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1711 if (!card
->instantiated
)
1714 /* Flush out pmdown_time work - we actually do want to run it
1715 * now, we're shutting down so no imminent restart. */
1716 for (i
= 0; i
< card
->num_rtd
; i
++) {
1717 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1718 run_delayed_work(&rtd
->delayed_work
);
1721 snd_soc_dapm_shutdown(card
);
1726 static const struct dev_pm_ops soc_pm_ops
= {
1727 .suspend
= soc_suspend
,
1728 .resume
= soc_resume
,
1729 .poweroff
= soc_poweroff
,
1732 /* ASoC platform driver */
1733 static struct platform_driver soc_driver
= {
1735 .name
= "soc-audio",
1736 .owner
= THIS_MODULE
,
1740 .remove
= soc_remove
,
1743 /* create a new pcm */
1744 static int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
)
1746 struct snd_soc_codec
*codec
= rtd
->codec
;
1747 struct snd_soc_platform
*platform
= rtd
->platform
;
1748 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
1749 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1750 struct snd_pcm
*pcm
;
1752 int ret
= 0, playback
= 0, capture
= 0;
1754 /* check client and interface hw capabilities */
1755 snprintf(new_name
, sizeof(new_name
), "%s %s-%d",
1756 rtd
->dai_link
->stream_name
, codec_dai
->name
, num
);
1758 if (codec_dai
->driver
->playback
.channels_min
)
1760 if (codec_dai
->driver
->capture
.channels_min
)
1763 dev_dbg(rtd
->card
->dev
, "registered pcm #%d %s\n",num
,new_name
);
1764 ret
= snd_pcm_new(rtd
->card
->snd_card
, new_name
,
1765 num
, playback
, capture
, &pcm
);
1767 printk(KERN_ERR
"asoc: can't create pcm for codec %s\n", codec
->name
);
1772 pcm
->private_data
= rtd
;
1773 soc_pcm_ops
.mmap
= platform
->driver
->ops
->mmap
;
1774 soc_pcm_ops
.pointer
= platform
->driver
->ops
->pointer
;
1775 soc_pcm_ops
.ioctl
= platform
->driver
->ops
->ioctl
;
1776 soc_pcm_ops
.copy
= platform
->driver
->ops
->copy
;
1777 soc_pcm_ops
.silence
= platform
->driver
->ops
->silence
;
1778 soc_pcm_ops
.ack
= platform
->driver
->ops
->ack
;
1779 soc_pcm_ops
.page
= platform
->driver
->ops
->page
;
1782 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &soc_pcm_ops
);
1785 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &soc_pcm_ops
);
1787 ret
= platform
->driver
->pcm_new(rtd
->card
->snd_card
, codec_dai
, pcm
);
1789 printk(KERN_ERR
"asoc: platform pcm constructor failed\n");
1793 pcm
->private_free
= platform
->driver
->pcm_free
;
1794 printk(KERN_INFO
"asoc: %s <-> %s mapping ok\n", codec_dai
->name
,
1800 * snd_soc_codec_volatile_register: Report if a register is volatile.
1802 * @codec: CODEC to query.
1803 * @reg: Register to query.
1805 * Boolean function indiciating if a CODEC register is volatile.
1807 int snd_soc_codec_volatile_register(struct snd_soc_codec
*codec
, int reg
)
1809 if (codec
->driver
->volatile_register
)
1810 return codec
->driver
->volatile_register(reg
);
1814 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register
);
1817 * snd_soc_new_ac97_codec - initailise AC97 device
1818 * @codec: audio codec
1819 * @ops: AC97 bus operations
1820 * @num: AC97 codec number
1822 * Initialises AC97 codec resources for use by ad-hoc devices only.
1824 int snd_soc_new_ac97_codec(struct snd_soc_codec
*codec
,
1825 struct snd_ac97_bus_ops
*ops
, int num
)
1827 mutex_lock(&codec
->mutex
);
1829 codec
->ac97
= kzalloc(sizeof(struct snd_ac97
), GFP_KERNEL
);
1830 if (codec
->ac97
== NULL
) {
1831 mutex_unlock(&codec
->mutex
);
1835 codec
->ac97
->bus
= kzalloc(sizeof(struct snd_ac97_bus
), GFP_KERNEL
);
1836 if (codec
->ac97
->bus
== NULL
) {
1839 mutex_unlock(&codec
->mutex
);
1843 codec
->ac97
->bus
->ops
= ops
;
1844 codec
->ac97
->num
= num
;
1847 * Mark the AC97 device to be created by us. This way we ensure that the
1848 * device will be registered with the device subsystem later on.
1850 codec
->ac97_created
= 1;
1852 mutex_unlock(&codec
->mutex
);
1855 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec
);
1858 * snd_soc_free_ac97_codec - free AC97 codec device
1859 * @codec: audio codec
1861 * Frees AC97 codec device resources.
1863 void snd_soc_free_ac97_codec(struct snd_soc_codec
*codec
)
1865 mutex_lock(&codec
->mutex
);
1866 #ifdef CONFIG_SND_SOC_AC97_BUS
1867 soc_unregister_ac97_dai_link(codec
);
1869 kfree(codec
->ac97
->bus
);
1872 codec
->ac97_created
= 0;
1873 mutex_unlock(&codec
->mutex
);
1875 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec
);
1878 * snd_soc_update_bits - update codec register bits
1879 * @codec: audio codec
1880 * @reg: codec register
1881 * @mask: register mask
1884 * Writes new register value.
1886 * Returns 1 for change else 0.
1888 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1889 unsigned int mask
, unsigned int value
)
1892 unsigned int old
, new;
1894 old
= snd_soc_read(codec
, reg
);
1895 new = (old
& ~mask
) | value
;
1896 change
= old
!= new;
1898 snd_soc_write(codec
, reg
, new);
1902 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
1905 * snd_soc_update_bits_locked - update codec register bits
1906 * @codec: audio codec
1907 * @reg: codec register
1908 * @mask: register mask
1911 * Writes new register value, and takes the codec mutex.
1913 * Returns 1 for change else 0.
1915 int snd_soc_update_bits_locked(struct snd_soc_codec
*codec
,
1916 unsigned short reg
, unsigned int mask
,
1921 mutex_lock(&codec
->mutex
);
1922 change
= snd_soc_update_bits(codec
, reg
, mask
, value
);
1923 mutex_unlock(&codec
->mutex
);
1927 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked
);
1930 * snd_soc_test_bits - test register for change
1931 * @codec: audio codec
1932 * @reg: codec register
1933 * @mask: register mask
1936 * Tests a register with a new value and checks if the new value is
1937 * different from the old value.
1939 * Returns 1 for change else 0.
1941 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1942 unsigned int mask
, unsigned int value
)
1945 unsigned int old
, new;
1947 old
= snd_soc_read(codec
, reg
);
1948 new = (old
& ~mask
) | value
;
1949 change
= old
!= new;
1953 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
1956 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1957 * @substream: the pcm substream
1958 * @hw: the hardware parameters
1960 * Sets the substream runtime hardware parameters.
1962 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
1963 const struct snd_pcm_hardware
*hw
)
1965 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1966 runtime
->hw
.info
= hw
->info
;
1967 runtime
->hw
.formats
= hw
->formats
;
1968 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
1969 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
1970 runtime
->hw
.periods_min
= hw
->periods_min
;
1971 runtime
->hw
.periods_max
= hw
->periods_max
;
1972 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
1973 runtime
->hw
.fifo_size
= hw
->fifo_size
;
1976 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
1979 * snd_soc_cnew - create new control
1980 * @_template: control template
1981 * @data: control private data
1982 * @long_name: control long name
1984 * Create a new mixer control from a template control.
1986 * Returns 0 for success, else error.
1988 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
1989 void *data
, char *long_name
)
1991 struct snd_kcontrol_new
template;
1993 memcpy(&template, _template
, sizeof(template));
1995 template.name
= long_name
;
1998 return snd_ctl_new1(&template, data
);
2000 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
2003 * snd_soc_add_controls - add an array of controls to a codec.
2004 * Convienience function to add a list of controls. Many codecs were
2005 * duplicating this code.
2007 * @codec: codec to add controls to
2008 * @controls: array of controls to add
2009 * @num_controls: number of elements in the array
2011 * Return 0 for success, else error.
2013 int snd_soc_add_controls(struct snd_soc_codec
*codec
,
2014 const struct snd_kcontrol_new
*controls
, int num_controls
)
2016 struct snd_card
*card
= codec
->card
->snd_card
;
2019 for (i
= 0; i
< num_controls
; i
++) {
2020 const struct snd_kcontrol_new
*control
= &controls
[i
];
2021 err
= snd_ctl_add(card
, snd_soc_cnew(control
, codec
, NULL
));
2023 dev_err(codec
->dev
, "%s: Failed to add %s: %d\n",
2024 codec
->name
, control
->name
, err
);
2031 EXPORT_SYMBOL_GPL(snd_soc_add_controls
);
2034 * snd_soc_info_enum_double - enumerated double mixer info callback
2035 * @kcontrol: mixer control
2036 * @uinfo: control element information
2038 * Callback to provide information about a double enumerated
2041 * Returns 0 for success.
2043 int snd_soc_info_enum_double(struct snd_kcontrol
*kcontrol
,
2044 struct snd_ctl_elem_info
*uinfo
)
2046 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2048 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2049 uinfo
->count
= e
->shift_l
== e
->shift_r
? 1 : 2;
2050 uinfo
->value
.enumerated
.items
= e
->max
;
2052 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2053 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2054 strcpy(uinfo
->value
.enumerated
.name
,
2055 e
->texts
[uinfo
->value
.enumerated
.item
]);
2058 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double
);
2061 * snd_soc_get_enum_double - enumerated double mixer get callback
2062 * @kcontrol: mixer control
2063 * @ucontrol: control element information
2065 * Callback to get the value of a double enumerated mixer.
2067 * Returns 0 for success.
2069 int snd_soc_get_enum_double(struct snd_kcontrol
*kcontrol
,
2070 struct snd_ctl_elem_value
*ucontrol
)
2072 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2073 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2074 unsigned int val
, bitmask
;
2076 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2078 val
= snd_soc_read(codec
, e
->reg
);
2079 ucontrol
->value
.enumerated
.item
[0]
2080 = (val
>> e
->shift_l
) & (bitmask
- 1);
2081 if (e
->shift_l
!= e
->shift_r
)
2082 ucontrol
->value
.enumerated
.item
[1] =
2083 (val
>> e
->shift_r
) & (bitmask
- 1);
2087 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double
);
2090 * snd_soc_put_enum_double - enumerated double mixer put callback
2091 * @kcontrol: mixer control
2092 * @ucontrol: control element information
2094 * Callback to set the value of a double enumerated mixer.
2096 * Returns 0 for success.
2098 int snd_soc_put_enum_double(struct snd_kcontrol
*kcontrol
,
2099 struct snd_ctl_elem_value
*ucontrol
)
2101 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2102 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2104 unsigned int mask
, bitmask
;
2106 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2108 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2110 val
= ucontrol
->value
.enumerated
.item
[0] << e
->shift_l
;
2111 mask
= (bitmask
- 1) << e
->shift_l
;
2112 if (e
->shift_l
!= e
->shift_r
) {
2113 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2115 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2116 mask
|= (bitmask
- 1) << e
->shift_r
;
2119 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2121 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double
);
2124 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2125 * @kcontrol: mixer control
2126 * @ucontrol: control element information
2128 * Callback to get the value of a double semi enumerated mixer.
2130 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2131 * used for handling bitfield coded enumeration for example.
2133 * Returns 0 for success.
2135 int snd_soc_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2136 struct snd_ctl_elem_value
*ucontrol
)
2138 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2139 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2140 unsigned int reg_val
, val
, mux
;
2142 reg_val
= snd_soc_read(codec
, e
->reg
);
2143 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2144 for (mux
= 0; mux
< e
->max
; mux
++) {
2145 if (val
== e
->values
[mux
])
2148 ucontrol
->value
.enumerated
.item
[0] = mux
;
2149 if (e
->shift_l
!= e
->shift_r
) {
2150 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2151 for (mux
= 0; mux
< e
->max
; mux
++) {
2152 if (val
== e
->values
[mux
])
2155 ucontrol
->value
.enumerated
.item
[1] = mux
;
2160 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double
);
2163 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2164 * @kcontrol: mixer control
2165 * @ucontrol: control element information
2167 * Callback to set the value of a double semi enumerated mixer.
2169 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2170 * used for handling bitfield coded enumeration for example.
2172 * Returns 0 for success.
2174 int snd_soc_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2175 struct snd_ctl_elem_value
*ucontrol
)
2177 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2178 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2182 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2184 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2185 mask
= e
->mask
<< e
->shift_l
;
2186 if (e
->shift_l
!= e
->shift_r
) {
2187 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2189 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2190 mask
|= e
->mask
<< e
->shift_r
;
2193 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2195 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double
);
2198 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2199 * @kcontrol: mixer control
2200 * @uinfo: control element information
2202 * Callback to provide information about an external enumerated
2205 * Returns 0 for success.
2207 int snd_soc_info_enum_ext(struct snd_kcontrol
*kcontrol
,
2208 struct snd_ctl_elem_info
*uinfo
)
2210 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2212 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2214 uinfo
->value
.enumerated
.items
= e
->max
;
2216 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2217 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2218 strcpy(uinfo
->value
.enumerated
.name
,
2219 e
->texts
[uinfo
->value
.enumerated
.item
]);
2222 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext
);
2225 * snd_soc_info_volsw_ext - external single mixer info callback
2226 * @kcontrol: mixer control
2227 * @uinfo: control element information
2229 * Callback to provide information about a single external mixer control.
2231 * Returns 0 for success.
2233 int snd_soc_info_volsw_ext(struct snd_kcontrol
*kcontrol
,
2234 struct snd_ctl_elem_info
*uinfo
)
2236 int max
= kcontrol
->private_value
;
2238 if (max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2239 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2241 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2244 uinfo
->value
.integer
.min
= 0;
2245 uinfo
->value
.integer
.max
= max
;
2248 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext
);
2251 * snd_soc_info_volsw - single mixer info callback
2252 * @kcontrol: mixer control
2253 * @uinfo: control element information
2255 * Callback to provide information about a single mixer control.
2257 * Returns 0 for success.
2259 int snd_soc_info_volsw(struct snd_kcontrol
*kcontrol
,
2260 struct snd_ctl_elem_info
*uinfo
)
2262 struct soc_mixer_control
*mc
=
2263 (struct soc_mixer_control
*)kcontrol
->private_value
;
2265 unsigned int shift
= mc
->shift
;
2266 unsigned int rshift
= mc
->rshift
;
2268 if (!mc
->platform_max
)
2269 mc
->platform_max
= mc
->max
;
2270 platform_max
= mc
->platform_max
;
2272 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2273 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2275 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2277 uinfo
->count
= shift
== rshift
? 1 : 2;
2278 uinfo
->value
.integer
.min
= 0;
2279 uinfo
->value
.integer
.max
= platform_max
;
2282 EXPORT_SYMBOL_GPL(snd_soc_info_volsw
);
2285 * snd_soc_get_volsw - single mixer get callback
2286 * @kcontrol: mixer control
2287 * @ucontrol: control element information
2289 * Callback to get the value of a single mixer control.
2291 * Returns 0 for success.
2293 int snd_soc_get_volsw(struct snd_kcontrol
*kcontrol
,
2294 struct snd_ctl_elem_value
*ucontrol
)
2296 struct soc_mixer_control
*mc
=
2297 (struct soc_mixer_control
*)kcontrol
->private_value
;
2298 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2299 unsigned int reg
= mc
->reg
;
2300 unsigned int shift
= mc
->shift
;
2301 unsigned int rshift
= mc
->rshift
;
2303 unsigned int mask
= (1 << fls(max
)) - 1;
2304 unsigned int invert
= mc
->invert
;
2306 ucontrol
->value
.integer
.value
[0] =
2307 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2308 if (shift
!= rshift
)
2309 ucontrol
->value
.integer
.value
[1] =
2310 (snd_soc_read(codec
, reg
) >> rshift
) & mask
;
2312 ucontrol
->value
.integer
.value
[0] =
2313 max
- ucontrol
->value
.integer
.value
[0];
2314 if (shift
!= rshift
)
2315 ucontrol
->value
.integer
.value
[1] =
2316 max
- ucontrol
->value
.integer
.value
[1];
2321 EXPORT_SYMBOL_GPL(snd_soc_get_volsw
);
2324 * snd_soc_put_volsw - single mixer put callback
2325 * @kcontrol: mixer control
2326 * @ucontrol: control element information
2328 * Callback to set the value of a single mixer control.
2330 * Returns 0 for success.
2332 int snd_soc_put_volsw(struct snd_kcontrol
*kcontrol
,
2333 struct snd_ctl_elem_value
*ucontrol
)
2335 struct soc_mixer_control
*mc
=
2336 (struct soc_mixer_control
*)kcontrol
->private_value
;
2337 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2338 unsigned int reg
= mc
->reg
;
2339 unsigned int shift
= mc
->shift
;
2340 unsigned int rshift
= mc
->rshift
;
2342 unsigned int mask
= (1 << fls(max
)) - 1;
2343 unsigned int invert
= mc
->invert
;
2344 unsigned int val
, val2
, val_mask
;
2346 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2349 val_mask
= mask
<< shift
;
2351 if (shift
!= rshift
) {
2352 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2355 val_mask
|= mask
<< rshift
;
2356 val
|= val2
<< rshift
;
2358 return snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2360 EXPORT_SYMBOL_GPL(snd_soc_put_volsw
);
2363 * snd_soc_info_volsw_2r - double mixer info callback
2364 * @kcontrol: mixer control
2365 * @uinfo: control element information
2367 * Callback to provide information about a double mixer control that
2368 * spans 2 codec registers.
2370 * Returns 0 for success.
2372 int snd_soc_info_volsw_2r(struct snd_kcontrol
*kcontrol
,
2373 struct snd_ctl_elem_info
*uinfo
)
2375 struct soc_mixer_control
*mc
=
2376 (struct soc_mixer_control
*)kcontrol
->private_value
;
2379 if (!mc
->platform_max
)
2380 mc
->platform_max
= mc
->max
;
2381 platform_max
= mc
->platform_max
;
2383 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2384 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2386 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2389 uinfo
->value
.integer
.min
= 0;
2390 uinfo
->value
.integer
.max
= platform_max
;
2393 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r
);
2396 * snd_soc_get_volsw_2r - double mixer get callback
2397 * @kcontrol: mixer control
2398 * @ucontrol: control element information
2400 * Callback to get the value of a double mixer control that spans 2 registers.
2402 * Returns 0 for success.
2404 int snd_soc_get_volsw_2r(struct snd_kcontrol
*kcontrol
,
2405 struct snd_ctl_elem_value
*ucontrol
)
2407 struct soc_mixer_control
*mc
=
2408 (struct soc_mixer_control
*)kcontrol
->private_value
;
2409 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2410 unsigned int reg
= mc
->reg
;
2411 unsigned int reg2
= mc
->rreg
;
2412 unsigned int shift
= mc
->shift
;
2414 unsigned int mask
= (1 << fls(max
)) - 1;
2415 unsigned int invert
= mc
->invert
;
2417 ucontrol
->value
.integer
.value
[0] =
2418 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2419 ucontrol
->value
.integer
.value
[1] =
2420 (snd_soc_read(codec
, reg2
) >> shift
) & mask
;
2422 ucontrol
->value
.integer
.value
[0] =
2423 max
- ucontrol
->value
.integer
.value
[0];
2424 ucontrol
->value
.integer
.value
[1] =
2425 max
- ucontrol
->value
.integer
.value
[1];
2430 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r
);
2433 * snd_soc_put_volsw_2r - double mixer set callback
2434 * @kcontrol: mixer control
2435 * @ucontrol: control element information
2437 * Callback to set the value of a double mixer control that spans 2 registers.
2439 * Returns 0 for success.
2441 int snd_soc_put_volsw_2r(struct snd_kcontrol
*kcontrol
,
2442 struct snd_ctl_elem_value
*ucontrol
)
2444 struct soc_mixer_control
*mc
=
2445 (struct soc_mixer_control
*)kcontrol
->private_value
;
2446 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2447 unsigned int reg
= mc
->reg
;
2448 unsigned int reg2
= mc
->rreg
;
2449 unsigned int shift
= mc
->shift
;
2451 unsigned int mask
= (1 << fls(max
)) - 1;
2452 unsigned int invert
= mc
->invert
;
2454 unsigned int val
, val2
, val_mask
;
2456 val_mask
= mask
<< shift
;
2457 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2458 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2466 val2
= val2
<< shift
;
2468 err
= snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2472 err
= snd_soc_update_bits_locked(codec
, reg2
, val_mask
, val2
);
2475 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r
);
2478 * snd_soc_info_volsw_s8 - signed mixer info callback
2479 * @kcontrol: mixer control
2480 * @uinfo: control element information
2482 * Callback to provide information about a signed mixer control.
2484 * Returns 0 for success.
2486 int snd_soc_info_volsw_s8(struct snd_kcontrol
*kcontrol
,
2487 struct snd_ctl_elem_info
*uinfo
)
2489 struct soc_mixer_control
*mc
=
2490 (struct soc_mixer_control
*)kcontrol
->private_value
;
2494 if (!mc
->platform_max
)
2495 mc
->platform_max
= mc
->max
;
2496 platform_max
= mc
->platform_max
;
2498 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2500 uinfo
->value
.integer
.min
= 0;
2501 uinfo
->value
.integer
.max
= platform_max
- min
;
2504 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8
);
2507 * snd_soc_get_volsw_s8 - signed mixer get callback
2508 * @kcontrol: mixer control
2509 * @ucontrol: control element information
2511 * Callback to get the value of a signed mixer control.
2513 * Returns 0 for success.
2515 int snd_soc_get_volsw_s8(struct snd_kcontrol
*kcontrol
,
2516 struct snd_ctl_elem_value
*ucontrol
)
2518 struct soc_mixer_control
*mc
=
2519 (struct soc_mixer_control
*)kcontrol
->private_value
;
2520 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2521 unsigned int reg
= mc
->reg
;
2523 int val
= snd_soc_read(codec
, reg
);
2525 ucontrol
->value
.integer
.value
[0] =
2526 ((signed char)(val
& 0xff))-min
;
2527 ucontrol
->value
.integer
.value
[1] =
2528 ((signed char)((val
>> 8) & 0xff))-min
;
2531 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8
);
2534 * snd_soc_put_volsw_sgn - signed mixer put callback
2535 * @kcontrol: mixer control
2536 * @ucontrol: control element information
2538 * Callback to set the value of a signed mixer control.
2540 * Returns 0 for success.
2542 int snd_soc_put_volsw_s8(struct snd_kcontrol
*kcontrol
,
2543 struct snd_ctl_elem_value
*ucontrol
)
2545 struct soc_mixer_control
*mc
=
2546 (struct soc_mixer_control
*)kcontrol
->private_value
;
2547 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2548 unsigned int reg
= mc
->reg
;
2552 val
= (ucontrol
->value
.integer
.value
[0]+min
) & 0xff;
2553 val
|= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff) << 8;
2555 return snd_soc_update_bits_locked(codec
, reg
, 0xffff, val
);
2557 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8
);
2560 * snd_soc_limit_volume - Set new limit to an existing volume control.
2562 * @codec: where to look for the control
2563 * @name: Name of the control
2564 * @max: new maximum limit
2566 * Return 0 for success, else error.
2568 int snd_soc_limit_volume(struct snd_soc_codec
*codec
,
2569 const char *name
, int max
)
2571 struct snd_card
*card
= codec
->card
->snd_card
;
2572 struct snd_kcontrol
*kctl
;
2573 struct soc_mixer_control
*mc
;
2577 /* Sanity check for name and max */
2578 if (unlikely(!name
|| max
<= 0))
2581 list_for_each_entry(kctl
, &card
->controls
, list
) {
2582 if (!strncmp(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
))) {
2588 mc
= (struct soc_mixer_control
*)kctl
->private_value
;
2589 if (max
<= mc
->max
) {
2590 mc
->platform_max
= max
;
2596 EXPORT_SYMBOL_GPL(snd_soc_limit_volume
);
2599 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2600 * mixer info callback
2601 * @kcontrol: mixer control
2602 * @uinfo: control element information
2604 * Returns 0 for success.
2606 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2607 struct snd_ctl_elem_info
*uinfo
)
2609 struct soc_mixer_control
*mc
=
2610 (struct soc_mixer_control
*)kcontrol
->private_value
;
2614 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2616 uinfo
->value
.integer
.min
= 0;
2617 uinfo
->value
.integer
.max
= max
-min
;
2621 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx
);
2624 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2625 * mixer get callback
2626 * @kcontrol: mixer control
2627 * @uinfo: control element information
2629 * Returns 0 for success.
2631 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2632 struct snd_ctl_elem_value
*ucontrol
)
2634 struct soc_mixer_control
*mc
=
2635 (struct soc_mixer_control
*)kcontrol
->private_value
;
2636 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2637 unsigned int mask
= (1<<mc
->shift
)-1;
2639 int val
= snd_soc_read(codec
, mc
->reg
) & mask
;
2640 int valr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2642 ucontrol
->value
.integer
.value
[0] = ((val
& 0xff)-min
) & mask
;
2643 ucontrol
->value
.integer
.value
[1] = ((valr
& 0xff)-min
) & mask
;
2646 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx
);
2649 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2650 * mixer put callback
2651 * @kcontrol: mixer control
2652 * @uinfo: control element information
2654 * Returns 0 for success.
2656 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2657 struct snd_ctl_elem_value
*ucontrol
)
2659 struct soc_mixer_control
*mc
=
2660 (struct soc_mixer_control
*)kcontrol
->private_value
;
2661 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2662 unsigned int mask
= (1<<mc
->shift
)-1;
2665 unsigned int val
, valr
, oval
, ovalr
;
2667 val
= ((ucontrol
->value
.integer
.value
[0]+min
) & 0xff);
2669 valr
= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff);
2672 oval
= snd_soc_read(codec
, mc
->reg
) & mask
;
2673 ovalr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2677 ret
= snd_soc_write(codec
, mc
->reg
, val
);
2681 if (ovalr
!= valr
) {
2682 ret
= snd_soc_write(codec
, mc
->rreg
, valr
);
2689 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx
);
2692 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2694 * @clk_id: DAI specific clock ID
2695 * @freq: new clock frequency in Hz
2696 * @dir: new clock direction - input/output.
2698 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2700 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
2701 unsigned int freq
, int dir
)
2703 if (dai
->driver
&& dai
->driver
->ops
->set_sysclk
)
2704 return dai
->driver
->ops
->set_sysclk(dai
, clk_id
, freq
, dir
);
2708 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
2711 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2713 * @div_id: DAI specific clock divider ID
2714 * @div: new clock divisor.
2716 * Configures the clock dividers. This is used to derive the best DAI bit and
2717 * frame clocks from the system or master clock. It's best to set the DAI bit
2718 * and frame clocks as low as possible to save system power.
2720 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
2721 int div_id
, int div
)
2723 if (dai
->driver
&& dai
->driver
->ops
->set_clkdiv
)
2724 return dai
->driver
->ops
->set_clkdiv(dai
, div_id
, div
);
2728 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
2731 * snd_soc_dai_set_pll - configure DAI PLL.
2733 * @pll_id: DAI specific PLL ID
2734 * @source: DAI specific source for the PLL
2735 * @freq_in: PLL input clock frequency in Hz
2736 * @freq_out: requested PLL output clock frequency in Hz
2738 * Configures and enables PLL to generate output clock based on input clock.
2740 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
2741 unsigned int freq_in
, unsigned int freq_out
)
2743 if (dai
->driver
&& dai
->driver
->ops
->set_pll
)
2744 return dai
->driver
->ops
->set_pll(dai
, pll_id
, source
,
2749 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
2752 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2754 * @fmt: SND_SOC_DAIFMT_ format value.
2756 * Configures the DAI hardware format and clocking.
2758 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
2760 if (dai
->driver
&& dai
->driver
->ops
->set_fmt
)
2761 return dai
->driver
->ops
->set_fmt(dai
, fmt
);
2765 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
2768 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2770 * @tx_mask: bitmask representing active TX slots.
2771 * @rx_mask: bitmask representing active RX slots.
2772 * @slots: Number of slots in use.
2773 * @slot_width: Width in bits for each slot.
2775 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2778 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
2779 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
2781 if (dai
->driver
&& dai
->driver
->ops
->set_tdm_slot
)
2782 return dai
->driver
->ops
->set_tdm_slot(dai
, tx_mask
, rx_mask
,
2787 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
2790 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2792 * @tx_num: how many TX channels
2793 * @tx_slot: pointer to an array which imply the TX slot number channel
2795 * @rx_num: how many RX channels
2796 * @rx_slot: pointer to an array which imply the RX slot number channel
2799 * configure the relationship between channel number and TDM slot number.
2801 int snd_soc_dai_set_channel_map(struct snd_soc_dai
*dai
,
2802 unsigned int tx_num
, unsigned int *tx_slot
,
2803 unsigned int rx_num
, unsigned int *rx_slot
)
2805 if (dai
->driver
&& dai
->driver
->ops
->set_channel_map
)
2806 return dai
->driver
->ops
->set_channel_map(dai
, tx_num
, tx_slot
,
2811 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map
);
2814 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2816 * @tristate: tristate enable
2818 * Tristates the DAI so that others can use it.
2820 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
2822 if (dai
->driver
&& dai
->driver
->ops
->set_tristate
)
2823 return dai
->driver
->ops
->set_tristate(dai
, tristate
);
2827 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
2830 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2832 * @mute: mute enable
2834 * Mutes the DAI DAC.
2836 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
)
2838 if (dai
->driver
&& dai
->driver
->ops
->digital_mute
)
2839 return dai
->driver
->ops
->digital_mute(dai
, mute
);
2843 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
2846 * snd_soc_register_card - Register a card with the ASoC core
2848 * @card: Card to register
2850 * Note that currently this is an internal only function: it will be
2851 * exposed to machine drivers after further backporting of ASoC v2
2852 * registration APIs.
2854 static int snd_soc_register_card(struct snd_soc_card
*card
)
2858 if (!card
->name
|| !card
->dev
)
2861 card
->rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
) * card
->num_links
,
2863 if (card
->rtd
== NULL
)
2866 for (i
= 0; i
< card
->num_links
; i
++)
2867 card
->rtd
[i
].dai_link
= &card
->dai_link
[i
];
2869 INIT_LIST_HEAD(&card
->list
);
2870 card
->instantiated
= 0;
2871 mutex_init(&card
->mutex
);
2873 mutex_lock(&client_mutex
);
2874 list_add(&card
->list
, &card_list
);
2875 snd_soc_instantiate_cards();
2876 mutex_unlock(&client_mutex
);
2878 dev_dbg(card
->dev
, "Registered card '%s'\n", card
->name
);
2884 * snd_soc_unregister_card - Unregister a card with the ASoC core
2886 * @card: Card to unregister
2888 * Note that currently this is an internal only function: it will be
2889 * exposed to machine drivers after further backporting of ASoC v2
2890 * registration APIs.
2892 static int snd_soc_unregister_card(struct snd_soc_card
*card
)
2894 mutex_lock(&client_mutex
);
2895 list_del(&card
->list
);
2896 mutex_unlock(&client_mutex
);
2897 dev_dbg(card
->dev
, "Unregistered card '%s'\n", card
->name
);
2903 * Simplify DAI link configuration by removing ".-1" from device names
2904 * and sanitizing names.
2906 static inline char *fmt_single_name(struct device
*dev
, int *id
)
2908 char *found
, name
[NAME_SIZE
];
2911 if (dev_name(dev
) == NULL
)
2914 strncpy(name
, dev_name(dev
), NAME_SIZE
);
2916 /* are we a "%s.%d" name (platform and SPI components) */
2917 found
= strstr(name
, dev
->driver
->name
);
2920 if (sscanf(&found
[strlen(dev
->driver
->name
)], ".%d", id
) == 1) {
2922 /* discard ID from name if ID == -1 */
2924 found
[strlen(dev
->driver
->name
)] = '\0';
2928 /* I2C component devices are named "bus-addr" */
2929 if (sscanf(name
, "%x-%x", &id1
, &id2
) == 2) {
2930 char tmp
[NAME_SIZE
];
2932 /* create unique ID number from I2C addr and bus */
2933 *id
= ((id1
& 0xffff) << 16) + id2
;
2935 /* sanitize component name for DAI link creation */
2936 snprintf(tmp
, NAME_SIZE
, "%s.%s", dev
->driver
->name
, name
);
2937 strncpy(name
, tmp
, NAME_SIZE
);
2942 return kstrdup(name
, GFP_KERNEL
);
2946 * Simplify DAI link naming for single devices with multiple DAIs by removing
2947 * any ".-1" and using the DAI name (instead of device name).
2949 static inline char *fmt_multiple_name(struct device
*dev
,
2950 struct snd_soc_dai_driver
*dai_drv
)
2952 if (dai_drv
->name
== NULL
) {
2953 printk(KERN_ERR
"asoc: error - multiple DAI %s registered with no name\n",
2958 return kstrdup(dai_drv
->name
, GFP_KERNEL
);
2962 * snd_soc_register_dai - Register a DAI with the ASoC core
2964 * @dai: DAI to register
2966 int snd_soc_register_dai(struct device
*dev
,
2967 struct snd_soc_dai_driver
*dai_drv
)
2969 struct snd_soc_dai
*dai
;
2971 dev_dbg(dev
, "dai register %s\n", dev_name(dev
));
2973 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
2977 /* create DAI component name */
2978 dai
->name
= fmt_single_name(dev
, &dai
->id
);
2979 if (dai
->name
== NULL
) {
2985 dai
->driver
= dai_drv
;
2986 if (!dai
->driver
->ops
)
2987 dai
->driver
->ops
= &null_dai_ops
;
2989 mutex_lock(&client_mutex
);
2990 list_add(&dai
->list
, &dai_list
);
2991 snd_soc_instantiate_cards();
2992 mutex_unlock(&client_mutex
);
2994 pr_debug("Registered DAI '%s'\n", dai
->name
);
2998 EXPORT_SYMBOL_GPL(snd_soc_register_dai
);
3001 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3003 * @dai: DAI to unregister
3005 void snd_soc_unregister_dai(struct device
*dev
)
3007 struct snd_soc_dai
*dai
;
3009 list_for_each_entry(dai
, &dai_list
, list
) {
3010 if (dev
== dai
->dev
)
3016 mutex_lock(&client_mutex
);
3017 list_del(&dai
->list
);
3018 mutex_unlock(&client_mutex
);
3020 pr_debug("Unregistered DAI '%s'\n", dai
->name
);
3024 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai
);
3027 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3029 * @dai: Array of DAIs to register
3030 * @count: Number of DAIs
3032 int snd_soc_register_dais(struct device
*dev
,
3033 struct snd_soc_dai_driver
*dai_drv
, size_t count
)
3035 struct snd_soc_dai
*dai
;
3038 dev_dbg(dev
, "dai register %s #%Zu\n", dev_name(dev
), count
);
3040 for (i
= 0; i
< count
; i
++) {
3042 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
3046 /* create DAI component name */
3047 dai
->name
= fmt_multiple_name(dev
, &dai_drv
[i
]);
3048 if (dai
->name
== NULL
) {
3055 dai
->driver
= &dai_drv
[i
];
3056 if (dai
->driver
->id
)
3057 dai
->id
= dai
->driver
->id
;
3060 if (!dai
->driver
->ops
)
3061 dai
->driver
->ops
= &null_dai_ops
;
3063 mutex_lock(&client_mutex
);
3064 list_add(&dai
->list
, &dai_list
);
3065 mutex_unlock(&client_mutex
);
3067 pr_debug("Registered DAI '%s'\n", dai
->name
);
3070 snd_soc_instantiate_cards();
3074 for (i
--; i
>= 0; i
--)
3075 snd_soc_unregister_dai(dev
);
3079 EXPORT_SYMBOL_GPL(snd_soc_register_dais
);
3082 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3084 * @dai: Array of DAIs to unregister
3085 * @count: Number of DAIs
3087 void snd_soc_unregister_dais(struct device
*dev
, size_t count
)
3091 for (i
= 0; i
< count
; i
++)
3092 snd_soc_unregister_dai(dev
);
3094 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais
);
3097 * snd_soc_register_platform - Register a platform with the ASoC core
3099 * @platform: platform to register
3101 int snd_soc_register_platform(struct device
*dev
,
3102 struct snd_soc_platform_driver
*platform_drv
)
3104 struct snd_soc_platform
*platform
;
3106 dev_dbg(dev
, "platform register %s\n", dev_name(dev
));
3108 platform
= kzalloc(sizeof(struct snd_soc_platform
), GFP_KERNEL
);
3109 if (platform
== NULL
)
3112 /* create platform component name */
3113 platform
->name
= fmt_single_name(dev
, &platform
->id
);
3114 if (platform
->name
== NULL
) {
3119 platform
->dev
= dev
;
3120 platform
->driver
= platform_drv
;
3122 mutex_lock(&client_mutex
);
3123 list_add(&platform
->list
, &platform_list
);
3124 snd_soc_instantiate_cards();
3125 mutex_unlock(&client_mutex
);
3127 pr_debug("Registered platform '%s'\n", platform
->name
);
3131 EXPORT_SYMBOL_GPL(snd_soc_register_platform
);
3134 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3136 * @platform: platform to unregister
3138 void snd_soc_unregister_platform(struct device
*dev
)
3140 struct snd_soc_platform
*platform
;
3142 list_for_each_entry(platform
, &platform_list
, list
) {
3143 if (dev
== platform
->dev
)
3149 mutex_lock(&client_mutex
);
3150 list_del(&platform
->list
);
3151 mutex_unlock(&client_mutex
);
3153 pr_debug("Unregistered platform '%s'\n", platform
->name
);
3154 kfree(platform
->name
);
3157 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform
);
3159 static u64 codec_format_map
[] = {
3160 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
,
3161 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_U16_BE
,
3162 SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S24_BE
,
3163 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_U24_BE
,
3164 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
,
3165 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_U32_BE
,
3166 SNDRV_PCM_FMTBIT_S24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3167 SNDRV_PCM_FMTBIT_U24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3168 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S20_3BE
,
3169 SNDRV_PCM_FMTBIT_U20_3LE
| SNDRV_PCM_FMTBIT_U20_3BE
,
3170 SNDRV_PCM_FMTBIT_S18_3LE
| SNDRV_PCM_FMTBIT_S18_3BE
,
3171 SNDRV_PCM_FMTBIT_U18_3LE
| SNDRV_PCM_FMTBIT_U18_3BE
,
3172 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
,
3173 SNDRV_PCM_FMTBIT_FLOAT64_LE
| SNDRV_PCM_FMTBIT_FLOAT64_BE
,
3174 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3175 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
,
3178 /* Fix up the DAI formats for endianness: codecs don't actually see
3179 * the endianness of the data but we're using the CPU format
3180 * definitions which do need to include endianness so we ensure that
3181 * codec DAIs always have both big and little endian variants set.
3183 static void fixup_codec_formats(struct snd_soc_pcm_stream
*stream
)
3187 for (i
= 0; i
< ARRAY_SIZE(codec_format_map
); i
++)
3188 if (stream
->formats
& codec_format_map
[i
])
3189 stream
->formats
|= codec_format_map
[i
];
3193 * snd_soc_register_codec - Register a codec with the ASoC core
3195 * @codec: codec to register
3197 int snd_soc_register_codec(struct device
*dev
,
3198 struct snd_soc_codec_driver
*codec_drv
,
3199 struct snd_soc_dai_driver
*dai_drv
, int num_dai
)
3201 struct snd_soc_codec
*codec
;
3204 dev_dbg(dev
, "codec register %s\n", dev_name(dev
));
3206 codec
= kzalloc(sizeof(struct snd_soc_codec
), GFP_KERNEL
);
3210 /* create CODEC component name */
3211 codec
->name
= fmt_single_name(dev
, &codec
->id
);
3212 if (codec
->name
== NULL
) {
3217 /* allocate CODEC register cache */
3218 if (codec_drv
->reg_cache_size
&& codec_drv
->reg_word_size
) {
3220 if (codec_drv
->reg_cache_default
)
3221 codec
->reg_cache
= kmemdup(codec_drv
->reg_cache_default
,
3222 codec_drv
->reg_cache_size
* codec_drv
->reg_word_size
, GFP_KERNEL
);
3224 codec
->reg_cache
= kzalloc(codec_drv
->reg_cache_size
*
3225 codec_drv
->reg_word_size
, GFP_KERNEL
);
3227 if (codec
->reg_cache
== NULL
) {
3235 codec
->driver
= codec_drv
;
3236 codec
->bias_level
= SND_SOC_BIAS_OFF
;
3237 codec
->num_dai
= num_dai
;
3238 mutex_init(&codec
->mutex
);
3239 INIT_LIST_HEAD(&codec
->dapm_widgets
);
3240 INIT_LIST_HEAD(&codec
->dapm_paths
);
3242 for (i
= 0; i
< num_dai
; i
++) {
3243 fixup_codec_formats(&dai_drv
[i
].playback
);
3244 fixup_codec_formats(&dai_drv
[i
].capture
);
3247 /* register any DAIs */
3249 ret
= snd_soc_register_dais(dev
, dai_drv
, num_dai
);
3254 mutex_lock(&client_mutex
);
3255 list_add(&codec
->list
, &codec_list
);
3256 snd_soc_instantiate_cards();
3257 mutex_unlock(&client_mutex
);
3259 pr_debug("Registered codec '%s'\n", codec
->name
);
3263 for (i
--; i
>= 0; i
--)
3264 snd_soc_unregister_dai(dev
);
3266 if (codec
->reg_cache
)
3267 kfree(codec
->reg_cache
);
3272 EXPORT_SYMBOL_GPL(snd_soc_register_codec
);
3275 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3277 * @codec: codec to unregister
3279 void snd_soc_unregister_codec(struct device
*dev
)
3281 struct snd_soc_codec
*codec
;
3284 list_for_each_entry(codec
, &codec_list
, list
) {
3285 if (dev
== codec
->dev
)
3292 for (i
= 0; i
< codec
->num_dai
; i
++)
3293 snd_soc_unregister_dai(dev
);
3295 mutex_lock(&client_mutex
);
3296 list_del(&codec
->list
);
3297 mutex_unlock(&client_mutex
);
3299 pr_debug("Unregistered codec '%s'\n", codec
->name
);
3301 if (codec
->reg_cache
)
3302 kfree(codec
->reg_cache
);
3306 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec
);
3308 static int __init
snd_soc_init(void)
3310 #ifdef CONFIG_DEBUG_FS
3311 debugfs_root
= debugfs_create_dir("asoc", NULL
);
3312 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
3314 "ASoC: Failed to create debugfs directory\n");
3315 debugfs_root
= NULL
;
3318 if (!debugfs_create_file("codecs", 0444, debugfs_root
, NULL
,
3320 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3322 if (!debugfs_create_file("dais", 0444, debugfs_root
, NULL
,
3324 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3326 if (!debugfs_create_file("platforms", 0444, debugfs_root
, NULL
,
3327 &platform_list_fops
))
3328 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3331 return platform_driver_register(&soc_driver
);
3333 module_init(snd_soc_init
);
3335 static void __exit
snd_soc_exit(void)
3337 #ifdef CONFIG_DEBUG_FS
3338 debugfs_remove_recursive(debugfs_root
);
3340 platform_driver_unregister(&soc_driver
);
3342 module_exit(snd_soc_exit
);
3344 /* Module information */
3345 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3346 MODULE_DESCRIPTION("ALSA SoC Core");
3347 MODULE_LICENSE("GPL");
3348 MODULE_ALIAS("platform:soc-audio");